1 /* $NetBSD: main.c,v 1.60 2005/09/23 12:10:34 jmmv 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 40 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95"; 41 #else 42 __RCSID("$NetBSD: main.c,v 1.60 2005/09/23 12:10:34 jmmv 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 70 int returntosingle; 71 int progress = 0; 72 73 static int argtoi(int, const char *, const char *, int); 74 static int checkfilesys(const char *, char *, long, int); 75 static void usage(void); 76 77 int 78 main(int argc, char *argv[]) 79 { 80 struct rlimit r; 81 int ch; 82 int ret = 0; 83 84 if (getrlimit(RLIMIT_DATA, &r) == 0) { 85 r.rlim_cur = r.rlim_max; 86 (void) setrlimit(RLIMIT_DATA, &r); 87 } 88 sync(); 89 skipclean = 1; 90 markclean = 1; 91 forceimage = 0; 92 endian = 0; 93 isappleufs = 0; 94 while ((ch = getopt(argc, argv, "aB:b:c:dFfm:npPqy")) != -1) { 95 switch (ch) { 96 case 'a': 97 isappleufs = 1; 98 break; 99 100 case 'B': 101 if (strcmp(optarg, "be") == 0) 102 endian = BIG_ENDIAN; 103 else if (strcmp(optarg, "le") == 0) 104 endian = LITTLE_ENDIAN; 105 else usage(); 106 break; 107 108 case 'b': 109 skipclean = 0; 110 bflag = argtoi('b', "number", optarg, 10); 111 printf("Alternate super block location: %d\n", bflag); 112 break; 113 114 case 'c': 115 skipclean = 0; 116 cvtlevel = argtoi('c', "conversion level", optarg, 10); 117 if (cvtlevel > 4) { 118 cvtlevel = 4; 119 warnx("Using maximum conversion level of %d\n",cvtlevel); 120 } 121 break; 122 123 case 'd': 124 debug++; 125 break; 126 127 case 'F': 128 forceimage = 1; 129 break; 130 131 case 'f': 132 skipclean = 0; 133 break; 134 135 case 'm': 136 lfmode = argtoi('m', "mode", optarg, 8); 137 if (lfmode &~ 07777) 138 errx(EEXIT, "bad mode to -m: %o", lfmode); 139 printf("** lost+found creation mode %o\n", lfmode); 140 break; 141 142 case 'n': 143 nflag++; 144 yflag = 0; 145 break; 146 147 case 'p': 148 preen++; 149 break; 150 151 case 'P': 152 progress = 1; 153 break; 154 155 case 'q': 156 quiet++; 157 break; 158 159 case 'y': 160 yflag++; 161 nflag = 0; 162 break; 163 164 default: 165 usage(); 166 } 167 } 168 169 argc -= optind; 170 argv += optind; 171 172 if (!argc) 173 usage(); 174 175 if (debug) 176 progress = 0; 177 178 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 179 (void)signal(SIGINT, catch); 180 if (preen) 181 (void)signal(SIGQUIT, catchquit); 182 #ifdef PROGRESS 183 if (progress) { 184 progress_ttywidth(0); 185 (void)signal(SIGWINCH, progress_ttywidth); 186 } 187 #endif /* ! PROGRESS */ 188 signal(SIGINFO, infohandler); 189 190 while (argc-- > 0) { 191 const char *path = blockcheck(*argv); 192 193 if (path == NULL) 194 pfatal("Can't check %s\n", *argv); 195 else 196 (void)checkfilesys(blockcheck(*argv), 0, 0L, 0); 197 argv++; 198 } 199 200 if (returntosingle) 201 ret = 2; 202 203 exit(ret); 204 } 205 206 static int 207 argtoi(int flag, const char *req, const char *str, int base) 208 { 209 char *cp; 210 int ret; 211 212 ret = (int)strtol(str, &cp, base); 213 if (cp == str || *cp) 214 errx(EEXIT, "-%c flag requires a %s", flag, req); 215 return (ret); 216 } 217 218 /* 219 * Check the specified filesystem. 220 */ 221 /* ARGSUSED */ 222 static int 223 checkfilesys(const char *filesys, char *mntpt, long auxdata, int child) 224 { 225 daddr_t n_ffree, n_bfree; 226 struct dups *dp; 227 struct zlncnt *zlnp; 228 int cylno; 229 #ifdef LITE2BORKEN 230 int flags; 231 #endif 232 #ifdef PROGRESS 233 off_t progress_total = 0; 234 #endif /* PROGRESS */ 235 236 if (preen && child) 237 (void)signal(SIGQUIT, voidquit); 238 setcdevname(filesys, preen); 239 if (debug && preen) 240 pwarn("starting\n"); 241 switch (setup(filesys)) { 242 case 0: 243 if (preen) 244 pfatal("CAN'T CHECK FILE SYSTEM."); 245 /* fall through */ 246 case -1: 247 return (0); 248 } 249 /* 250 * Cleared if any questions answered no. Used to decide if 251 * the superblock should be marked clean. 252 */ 253 resolved = 1; 254 255 #ifdef PROGRESS 256 /* 257 * Pass 1, Pass 4, and Pass 5 all iterate over cylinder 258 * groups. Account for those now. We'll never need to 259 * add in Pass 1b, since that pass is never executed when 260 * preening. 261 * 262 * Pass 2 and Pass 3 iterate over directory inodes, but we 263 * don't know how many of those exist until after Pass 1. 264 * We'll add those in after Pass 1 has completed. 265 */ 266 if (preen) 267 progress_total += sblock->fs_ncg * 3; 268 #endif /* PROGRESS */ 269 270 /* 271 * 1: scan inodes tallying blocks used 272 */ 273 if (preen == 0) { 274 pwarn("** Last Mounted on %s\n", sblock->fs_fsmnt); 275 if (hotroot()) 276 pwarn("** Root file system\n"); 277 pwarn("** Phase 1 - Check Blocks and Sizes\n"); 278 } 279 pass1(); 280 281 #ifdef PROGRESS 282 /* Account for number of directory inodes (used twice). */ 283 if (preen) 284 progress_total += inplast * 2; 285 progress_switch(progress); 286 progress_init(progress_total); 287 #endif /* PROGRESS */ 288 289 290 /* 291 * 1b: locate first references to duplicates, if any 292 */ 293 if (duplist) { 294 if (preen) 295 pfatal("INTERNAL ERROR: dups with -p\n"); 296 if (usedsoftdep) 297 pfatal("INTERNAL ERROR: dups with softdep\n"); 298 pwarn("** Phase 1b - Rescan For More DUPS\n"); 299 pass1b(); 300 } 301 302 /* 303 * 2: traverse directories from root to mark all connected directories 304 */ 305 if (preen == 0) 306 pwarn("** Phase 2 - Check Pathnames\n"); 307 pass2(); 308 309 /* 310 * 3: scan inodes looking for disconnected directories 311 */ 312 if (preen == 0) 313 pwarn("** Phase 3 - Check Connectivity\n"); 314 pass3(); 315 316 /* 317 * 4: scan inodes looking for disconnected files; check reference counts 318 */ 319 if (preen == 0) 320 pwarn("** Phase 4 - Check Reference Counts\n"); 321 pass4(); 322 323 /* 324 * 5: check and repair resource counts in cylinder groups 325 */ 326 if (preen == 0) 327 pwarn("** Phase 5 - Check Cyl groups\n"); 328 pass5(); 329 330 /* 331 * print out summary statistics 332 */ 333 n_ffree = sblock->fs_cstotal.cs_nffree; 334 n_bfree = sblock->fs_cstotal.cs_nbfree; 335 pwarn("%llu files, %lld used, %lld free ", 336 (unsigned long long)n_files, (long long)n_blks, 337 (long long)(n_ffree + sblock->fs_frag * n_bfree)); 338 printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n", 339 (long long)n_ffree, (long long)n_bfree, 340 (long long)(n_ffree * 100 / (daddr_t)sblock->fs_dsize), 341 (long long)(((n_ffree * 1000 + (daddr_t)sblock->fs_dsize / 2) 342 / (daddr_t)sblock->fs_dsize) % 10)); 343 if (debug && 344 (n_files -= maxino - ROOTINO - sblock->fs_cstotal.cs_nifree)) 345 printf("%llu files missing\n", (unsigned long long)n_files); 346 if (debug) { 347 n_blks += sblock->fs_ncg * 348 (cgdmin(sblock, 0) - cgsblock(sblock, 0)); 349 n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0); 350 n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize); 351 if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree)) 352 printf("%lld blocks missing\n", (long long)n_blks); 353 if (duplist != NULL) { 354 printf("The following duplicate blocks remain:"); 355 for (dp = duplist; dp; dp = dp->next) 356 printf(" %lld,", (long long)dp->dup); 357 printf("\n"); 358 } 359 if (zlnhead != NULL) { 360 printf("The following zero link count inodes remain:"); 361 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 362 printf(" %llu,", 363 (unsigned long long)zlnp->zlncnt); 364 printf("\n"); 365 } 366 } 367 zlnhead = (struct zlncnt *)0; 368 duplist = (struct dups *)0; 369 muldup = (struct dups *)0; 370 inocleanup(); 371 if (fsmodified) { 372 sblock->fs_time = time(NULL); 373 sbdirty(); 374 } 375 if (rerun) 376 markclean = 0; 377 #if LITE2BORKEN 378 if (!hotroot()) { 379 ckfini(); 380 } else { 381 struct statvfs stfs_buf; 382 /* 383 * Check to see if root is mounted read-write. 384 */ 385 if (statvfs("/", &stfs_buf) == 0) 386 flags = stfs_buf.f_flag; 387 else 388 flags = 0; 389 if (markclean) 390 markclean = flags & MNT_RDONLY; 391 ckfini(); 392 } 393 #else 394 ckfini(); 395 #endif 396 for (cylno = 0; cylno < sblock->fs_ncg; cylno++) 397 if (inostathead[cylno].il_stat != NULL) 398 free(inostathead[cylno].il_stat); 399 free(inostathead); 400 inostathead = NULL; 401 402 if (!resolved || rerun) { 403 pwarn("\n***** UNRESOLVED INCONSISTENCIES REMAIN *****\n"); 404 returntosingle = 1; 405 } 406 if (!fsmodified) 407 return (0); 408 if (!preen) 409 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 410 if (rerun) 411 pwarn("\n***** PLEASE RERUN FSCK *****\n"); 412 if (hotroot()) { 413 struct statvfs stfs_buf; 414 /* 415 * We modified the root. Do a mount update on 416 * it, unless it is read-write, so we can continue. 417 */ 418 if (statvfs("/", &stfs_buf) == 0) { 419 long flags = stfs_buf.f_flag; 420 struct ufs_args args; 421 int ret; 422 423 if (flags & MNT_RDONLY) { 424 args.fspec = 0; 425 flags |= MNT_UPDATE | MNT_RELOAD; 426 ret = mount(MOUNT_FFS, "/", flags, &args); 427 if (ret == 0) 428 return(0); 429 } 430 } 431 if (!preen) 432 pwarn("\n***** REBOOT NOW *****\n"); 433 sync(); 434 return (4); 435 } 436 return (0); 437 } 438 439 static void 440 usage(void) 441 { 442 443 (void) fprintf(stderr, 444 "usage: %s [-adFfnPpqy] [-B be|le] [-b block] [-c level] [-m mode]" 445 " filesystem ...\n", 446 getprogname()); 447 exit(1); 448 } 449 450