main.c revision 1.52
1/* $NetBSD: main.c,v 1.52 2004/10/11 15:24:09 dbj Exp $ */ 2 3/* 4 * Copyright (c) 1980, 1986, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33#ifndef lint 34__COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"); 36#endif /* not lint */ 37 38#ifndef lint 39#if 0 40static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95"; 41#else 42__RCSID("$NetBSD: main.c,v 1.52 2004/10/11 15:24:09 dbj Exp $"); 43#endif 44#endif /* not lint */ 45 46#include <sys/param.h> 47#include <sys/time.h> 48#include <sys/mount.h> 49#include <sys/resource.h> 50 51#include <ufs/ufs/dinode.h> 52#include <ufs/ufs/ufsmount.h> 53#include <ufs/ffs/fs.h> 54#include <ufs/ffs/ffs_extern.h> 55 56#include <ctype.h> 57#include <err.h> 58#include <fstab.h> 59#include <string.h> 60#include <time.h> 61#include <ctype.h> 62#include <stdio.h> 63#include <stdlib.h> 64#include <unistd.h> 65 66#include "fsck.h" 67#include "extern.h" 68#include "fsutil.h" 69 70int returntosingle; 71 72int main __P((int, char *[])); 73 74static int argtoi __P((int, char *, char *, int)); 75static int checkfilesys __P((const char *, char *, long, int)); 76static void usage __P((void)); 77 78int 79main(argc, argv) 80 int argc; 81 char *argv[]; 82{ 83 struct rlimit r; 84 int ch; 85 int ret = 0; 86 87 if (getrlimit(RLIMIT_DATA, &r) == 0) { 88 r.rlim_cur = r.rlim_max; 89 (void) setrlimit(RLIMIT_DATA, &r); 90 } 91 sync(); 92 skipclean = 1; 93 markclean = 1; 94 forceimage = 0; 95 endian = 0; 96 isappleufs = 0; 97 while ((ch = getopt(argc, argv, "aB:b:c:dFfm:npqy")) != -1) { 98 switch (ch) { 99 case 'a': 100 isappleufs = 1; 101 break; 102 103 case 'B': 104 if (strcmp(optarg, "be") == 0) 105 endian = BIG_ENDIAN; 106 else if (strcmp(optarg, "le") == 0) 107 endian = LITTLE_ENDIAN; 108 else usage(); 109 break; 110 111 case 'b': 112 skipclean = 0; 113 bflag = argtoi('b', "number", optarg, 10); 114 printf("Alternate super block location: %d\n", bflag); 115 break; 116 117 case 'c': 118 skipclean = 0; 119 cvtlevel = argtoi('c', "conversion level", optarg, 10); 120 if (cvtlevel > 4) { 121 cvtlevel = 4; 122 warnx("Using maximum conversion level of %d\n",cvtlevel); 123 } 124 break; 125 126 case 'd': 127 debug++; 128 break; 129 130 case 'F': 131 forceimage = 1; 132 break; 133 134 case 'f': 135 skipclean = 0; 136 break; 137 138 case 'm': 139 lfmode = argtoi('m', "mode", optarg, 8); 140 if (lfmode &~ 07777) 141 errx(EEXIT, "bad mode to -m: %o", lfmode); 142 printf("** lost+found creation mode %o\n", lfmode); 143 break; 144 145 case 'n': 146 nflag++; 147 yflag = 0; 148 break; 149 150 case 'p': 151 preen++; 152 break; 153 154 case 'q': 155 quiet++; 156 break; 157 158 case 'y': 159 yflag++; 160 nflag = 0; 161 break; 162 163 default: 164 usage(); 165 } 166 } 167 168 argc -= optind; 169 argv += optind; 170 171 if (!argc) 172 usage(); 173 174 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 175 (void)signal(SIGINT, catch); 176 if (preen) 177 (void)signal(SIGQUIT, catchquit); 178 signal(SIGINFO, infohandler); 179 180 while (argc-- > 0) { 181 const char *path = blockcheck(*argv); 182 183 if (path == NULL) 184 pfatal("Can't check %s\n", *argv); 185 else 186 (void)checkfilesys(blockcheck(*argv), 0, 0L, 0); 187 argv++; 188 } 189 190 if (returntosingle) 191 ret = 2; 192 193 exit(ret); 194} 195 196static int 197argtoi(flag, req, str, base) 198 int flag; 199 char *req, *str; 200 int base; 201{ 202 char *cp; 203 int ret; 204 205 ret = (int)strtol(str, &cp, base); 206 if (cp == str || *cp) 207 errx(EEXIT, "-%c flag requires a %s", flag, req); 208 return (ret); 209} 210 211/* 212 * Check the specified filesystem. 213 */ 214/* ARGSUSED */ 215static int 216checkfilesys(filesys, mntpt, auxdata, child) 217 const char *filesys; 218 char *mntpt; 219 long auxdata; 220 int child; 221{ 222 daddr_t n_ffree, n_bfree; 223 struct dups *dp; 224 struct zlncnt *zlnp; 225 int cylno; 226#ifdef LITE2BORKEN 227 int flags; 228#endif 229 230 if (preen && child) 231 (void)signal(SIGQUIT, voidquit); 232 setcdevname(filesys, preen); 233 if (debug && preen) 234 pwarn("starting\n"); 235 switch (setup(filesys)) { 236 case 0: 237 if (preen) 238 pfatal("CAN'T CHECK FILE SYSTEM."); 239 /* fall through */ 240 case -1: 241 return (0); 242 } 243 /* 244 * Cleared if any questions answered no. Used to decide if 245 * the superblock should be marked clean. 246 */ 247 resolved = 1; 248 /* 249 * 1: scan inodes tallying blocks used 250 */ 251 if (preen == 0) { 252 pwarn("** Last Mounted on %s\n", sblock->fs_fsmnt); 253 if (hotroot()) 254 pwarn("** Root file system\n"); 255 pwarn("** Phase 1 - Check Blocks and Sizes\n"); 256 } 257 pass1(); 258 259 /* 260 * 1b: locate first references to duplicates, if any 261 */ 262 if (duplist) { 263 if (preen) 264 pfatal("INTERNAL ERROR: dups with -p\n"); 265 if (usedsoftdep) 266 pfatal("INTERNAL ERROR: dups with softdep\n"); 267 pwarn("** Phase 1b - Rescan For More DUPS\n"); 268 pass1b(); 269 } 270 271 /* 272 * 2: traverse directories from root to mark all connected directories 273 */ 274 if (preen == 0) 275 pwarn("** Phase 2 - Check Pathnames\n"); 276 pass2(); 277 278 /* 279 * 3: scan inodes looking for disconnected directories 280 */ 281 if (preen == 0) 282 pwarn("** Phase 3 - Check Connectivity\n"); 283 pass3(); 284 285 /* 286 * 4: scan inodes looking for disconnected files; check reference counts 287 */ 288 if (preen == 0) 289 pwarn("** Phase 4 - Check Reference Counts\n"); 290 pass4(); 291 292 /* 293 * 5: check and repair resource counts in cylinder groups 294 */ 295 if (preen == 0) 296 pwarn("** Phase 5 - Check Cyl groups\n"); 297 pass5(); 298 299 /* 300 * print out summary statistics 301 */ 302 n_ffree = sblock->fs_cstotal.cs_nffree; 303 n_bfree = sblock->fs_cstotal.cs_nbfree; 304 pwarn("%d files, %lld used, %lld free ", 305 n_files, (long long)n_blks, 306 (long long)(n_ffree + sblock->fs_frag * n_bfree)); 307 printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n", 308 (long long)n_ffree, (long long)n_bfree, 309 (long long)(n_ffree * 100 / (daddr_t)sblock->fs_dsize), 310 (long long)(((n_ffree * 1000 + (daddr_t)sblock->fs_dsize / 2) 311 / (daddr_t)sblock->fs_dsize) % 10)); 312 if (debug && 313 (n_files -= maxino - ROOTINO - sblock->fs_cstotal.cs_nifree)) 314 printf("%d files missing\n", n_files); 315 if (debug) { 316 n_blks += sblock->fs_ncg * 317 (cgdmin(sblock, 0) - cgsblock(sblock, 0)); 318 n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0); 319 n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize); 320 if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree)) 321 printf("%lld blocks missing\n", (long long)n_blks); 322 if (duplist != NULL) { 323 printf("The following duplicate blocks remain:"); 324 for (dp = duplist; dp; dp = dp->next) 325 printf(" %lld,", (long long)dp->dup); 326 printf("\n"); 327 } 328 if (zlnhead != NULL) { 329 printf("The following zero link count inodes remain:"); 330 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 331 printf(" %u,", zlnp->zlncnt); 332 printf("\n"); 333 } 334 } 335 zlnhead = (struct zlncnt *)0; 336 duplist = (struct dups *)0; 337 muldup = (struct dups *)0; 338 inocleanup(); 339 if (fsmodified) { 340 sblock->fs_time = time(NULL); 341 sbdirty(); 342 } 343 if (rerun) 344 markclean = 0; 345#if LITE2BORKEN 346 if (!hotroot()) { 347 ckfini(); 348 } else { 349 struct statvfs stfs_buf; 350 /* 351 * Check to see if root is mounted read-write. 352 */ 353 if (statvfs("/", &stfs_buf) == 0) 354 flags = stfs_buf.f_flag; 355 else 356 flags = 0; 357 if (markclean) 358 markclean = flags & MNT_RDONLY; 359 ckfini(); 360 } 361#else 362 ckfini(); 363#endif 364 for (cylno = 0; cylno < sblock->fs_ncg; cylno++) 365 if (inostathead[cylno].il_stat != NULL) 366 free(inostathead[cylno].il_stat); 367 free(inostathead); 368 inostathead = NULL; 369 370 if (!resolved || rerun) { 371 pwarn("\n***** UNRESOLVED INCONSISTENCIES REMAIN *****\n"); 372 returntosingle = 1; 373 } 374 if (!fsmodified) 375 return (0); 376 if (!preen) 377 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 378 if (rerun) 379 pwarn("\n***** PLEASE RERUN FSCK *****\n"); 380 if (hotroot()) { 381 struct statvfs stfs_buf; 382 /* 383 * We modified the root. Do a mount update on 384 * it, unless it is read-write, so we can continue. 385 */ 386 if (statvfs("/", &stfs_buf) == 0) { 387 long flags = stfs_buf.f_flag; 388 struct ufs_args args; 389 int ret; 390 391 if (flags & MNT_RDONLY) { 392 args.fspec = 0; 393 args.export.ex_flags = 0; 394 args.export.ex_root = 0; 395 flags |= MNT_UPDATE | MNT_RELOAD; 396 ret = mount(MOUNT_FFS, "/", flags, &args); 397 if (ret == 0) 398 return(0); 399 } 400 } 401 if (!preen) 402 pwarn("\n***** REBOOT NOW *****\n"); 403 sync(); 404 return (4); 405 } 406 return (0); 407} 408 409static void 410usage() 411{ 412 413 (void) fprintf(stderr, 414 "usage: %s [-adFfnpqy] [-B be|le] [-b block] [-c level] [-m mode]" 415 " filesystem ...\n", 416 getprogname()); 417 exit(1); 418} 419 420