1 /* $NetBSD: main.c,v 1.89 2020/04/06 09:54:24 martin 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\ 35 The Regents of the University of California. All rights reserved."); 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.89 2020/04/06 09:54:24 martin 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 <errno.h> 59 #include <fstab.h> 60 #include <string.h> 61 #include <time.h> 62 #include <stdio.h> 63 #include <stdlib.h> 64 #include <unistd.h> 65 #include <signal.h> 66 67 #include "fsck.h" 68 #include "extern.h" 69 #include "fsutil.h" 70 #include "exitvalues.h" 71 #include "snapshot.h" 72 73 struct bufarea bufhead; 74 struct bufarea sblk; 75 struct bufarea asblk; 76 struct bufarea cgblk; 77 struct bufarea appleufsblk; 78 struct bufarea *pdirbp; 79 struct bufarea *pbp; 80 struct fs *sblock; 81 struct fs *altsblock; 82 struct cg *cgrp; 83 struct fs *sblocksave; 84 struct dups *duplist; 85 struct dups *muldup; 86 struct zlncnt *zlnhead; 87 struct inoinfo **inphead, **inpsort; 88 long numdirs, dirhash, listmax, inplast; 89 struct uquot_hash *uquot_user_hash; 90 struct uquot_hash *uquot_group_hash; 91 uint8_t q2h_hash_shift; 92 uint16_t q2h_hash_mask; 93 struct inostatlist *inostathead; 94 long dev_bsize; 95 long secsize; 96 char nflag; 97 char yflag; 98 int Uflag; 99 int bflag; 100 int debug; 101 int zflag; 102 int cvtlevel; 103 int doinglevel1; 104 int doinglevel2; 105 int newinofmt; 106 char usedsoftdep; 107 int preen; 108 int forceimage; 109 int is_ufs2; 110 int markclean; 111 char havesb; 112 char skipclean; 113 int fsmodified; 114 int fsreadfd; 115 int fswritefd; 116 int rerun; 117 char resolved; 118 #ifndef NO_FFS_EI 119 int endian; 120 int doswap; 121 int needswap; 122 int do_blkswap; 123 int do_dirswap; 124 #endif 125 #ifndef NO_APPLE_UFS 126 int isappleufs; 127 #endif 128 daddr_t maxfsblock; 129 char *blockmap; 130 ino_t maxino; 131 int dirblksiz; 132 daddr_t n_blks; 133 ino_t n_files; 134 long countdirs; 135 int got_siginfo; 136 struct ufs1_dinode ufs1_zino; 137 struct ufs2_dinode ufs2_zino; 138 139 int progress = 0; 140 141 static int argtoi(int, const char *, const char *, int); 142 static int checkfilesys(const char *, const char *, int); 143 __dead static void usage(void); 144 145 int 146 main(int argc, char *argv[]) 147 { 148 struct rlimit r; 149 int ch; 150 int ret = FSCK_EXIT_OK; 151 char *snap_backup = NULL; 152 int snap_internal = 0; 153 154 ckfinish = ckfini; 155 156 if (getrlimit(RLIMIT_DATA, &r) == 0) { 157 r.rlim_cur = r.rlim_max; 158 (void) setrlimit(RLIMIT_DATA, &r); 159 } 160 sync(); 161 skipclean = 1; 162 markclean = 1; 163 forceimage = 0; 164 #ifndef NO_FFS_EI 165 endian = 0; 166 #endif 167 #ifndef NO_APPLE_UFS 168 isappleufs = 0; 169 #endif 170 while ((ch = getopt(argc, argv, "aB:b:c:dFfm:npPqUyx:Xz")) != -1) { 171 switch (ch) { 172 #ifndef NO_APPLE_UFS 173 case 'a': 174 isappleufs = 1; 175 break; 176 #endif 177 178 #ifndef NO_FFS_EI 179 case 'B': 180 if (strcmp(optarg, "be") == 0) 181 endian = BIG_ENDIAN; 182 else if (strcmp(optarg, "le") == 0) 183 endian = LITTLE_ENDIAN; 184 else 185 usage(); 186 break; 187 #endif 188 189 case 'b': 190 skipclean = 0; 191 bflag = argtoi('b', "number", optarg, 10); 192 printf("Alternate super block location: %d\n", bflag); 193 break; 194 195 case 'c': 196 skipclean = 0; 197 cvtlevel = argtoi('c', "conversion level", optarg, 10); 198 if (cvtlevel > 4) { 199 cvtlevel = 4; 200 warnx("Using maximum conversion level of %d", 201 cvtlevel); 202 } 203 break; 204 205 case 'd': 206 debug++; 207 break; 208 209 case 'F': 210 forceimage = 1; 211 break; 212 213 case 'f': 214 skipclean = 0; 215 break; 216 217 case 'm': 218 lfmode = argtoi('m', "mode", optarg, 8); 219 if (lfmode &~ 07777) 220 errx(FSCK_EXIT_USAGE, "bad mode to -m: %o", 221 lfmode); 222 printf("** lost+found creation mode %o\n", lfmode); 223 break; 224 225 case 'n': 226 nflag++; 227 yflag = 0; 228 break; 229 230 case 'p': 231 preen++; 232 break; 233 234 case 'P': 235 progress = 1; 236 break; 237 238 case 'q': 239 quiet++; 240 break; 241 #ifndef SMALL 242 case 'U': 243 Uflag++; 244 break; 245 #endif 246 247 case 'x': 248 snap_backup = optarg; 249 break; 250 251 case 'X': 252 snap_internal = 1; 253 break; 254 255 case 'y': 256 yflag++; 257 nflag = 0; 258 break; 259 260 case 'z': 261 zflag++; 262 break; 263 264 default: 265 usage(); 266 } 267 } 268 269 if (snap_backup || snap_internal) { 270 if (!nflag || yflag) { 271 warnx("Cannot use -x or -X without -n"); 272 snap_backup = NULL; 273 snap_internal = 0; 274 } 275 } 276 277 278 argc -= optind; 279 argv += optind; 280 281 if (!argc) 282 usage(); 283 284 if (debug) 285 progress = 0; 286 287 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 288 (void)signal(SIGINT, catch); 289 if (preen) 290 (void)signal(SIGQUIT, catchquit); 291 #ifdef PROGRESS 292 if (progress) { 293 progress_ttywidth(0); 294 (void)signal(SIGWINCH, progress_ttywidth); 295 } 296 #endif /* ! PROGRESS */ 297 signal(SIGINFO, infohandler); 298 299 while (argc-- > 0) { 300 int nret; 301 char *path; 302 303 if (!forceimage) 304 path = strdup(blockcheck(*argv)); 305 else 306 path = strdup(*argv); 307 308 if (path == NULL) 309 pfatal("Can't check %s\n", *argv); 310 311 if (snap_backup || snap_internal) { 312 char *snap_dev; 313 int snapfd; 314 315 snapfd = snap_open(*argv, snap_backup, NULL, &snap_dev); 316 if (snapfd < 0) { 317 warn("can't take snapshot of %s", *argv); 318 goto next; 319 } 320 nret = checkfilesys(blockcheck(snap_dev), path, 0); 321 if (ret < nret) 322 ret = nret; 323 close(snapfd); 324 } else { 325 nret = checkfilesys(path, path, 0); 326 if (ret < nret) 327 ret = nret; 328 } 329 next: 330 free(path); 331 argv++; 332 } 333 334 return returntosingle ? FSCK_EXIT_UNRESOLVED : ret; 335 } 336 337 static int 338 argtoi(int flag, const char *req, const char *str, int base) 339 { 340 char *cp; 341 int ret; 342 343 ret = (int)strtol(str, &cp, base); 344 if (cp == str || *cp) 345 errx(FSCK_EXIT_USAGE, "-%c flag requires a %s", 346 flag, req); 347 return (ret); 348 } 349 350 /* 351 * Check the specified filesystem. 352 */ 353 /* ARGSUSED */ 354 static int 355 checkfilesys(const char *filesys, const char *origfs, int child) 356 { 357 daddr_t n_ffree, n_bfree; 358 struct dups *dp; 359 struct zlncnt *zlnp; 360 int cylno; 361 #ifdef LITE2BORKEN 362 int flags; 363 #endif 364 #ifdef PROGRESS 365 /* 366 * In prune mode, how far does the progress bar travel during 367 * each pass? (In non-prune mode, each pass has a separate 368 * progress bar that travels from 0 to 100%.) 369 * 370 * The numbers below are percentages, intended to correspond 371 * roughly to the cumulative time up to the end of each pass. 372 * They don't have to be accurate. In reality, on a large 373 * file system, Pass 1 and Pass 2 together are likely to use 374 * significantly more than the 95% reflected below, so users 375 * will get a pleasant surprise when the last 5% of the progress 376 * bar runs more quickly than they had expected. 377 */ 378 static int progress_limits[] = {0, 20, 95, 96, 97, 100}; 379 #endif /* PROGRESS */ 380 381 if (preen && child) 382 (void)signal(SIGQUIT, voidquit); 383 setcdevname(filesys, preen); 384 if (debug && preen) 385 pwarn("starting\n"); 386 switch (setup(filesys, origfs)) { 387 case 0: 388 if (preen) 389 pfatal("CAN'T CHECK FILE SYSTEM."); 390 /* fall through */ 391 case -1: 392 return FSCK_EXIT_OK; 393 } 394 /* 395 * Cleared if any questions answered no. Used to decide if 396 * the superblock should be marked clean. 397 */ 398 resolved = 1; 399 400 #ifdef PROGRESS 401 progress_switch(progress); 402 progress_init(); 403 #endif /* PROGRESS */ 404 405 /* 406 * 1: scan inodes tallying blocks used 407 */ 408 if (preen == 0) { 409 pwarn("** Last Mounted on %s\n", sblock->fs_fsmnt); 410 if (hotroot()) 411 pwarn("** Root file system\n"); 412 pwarn("** Phase 1 - Check Blocks and Sizes\n"); 413 } 414 #ifdef PROGRESS 415 if (preen) 416 progress_setrange(0, progress_limits[1]); 417 #endif /* PROGRESS */ 418 pass1(); 419 420 /* 421 * 1b: locate first references to duplicates, if any 422 */ 423 if (duplist) { 424 if (preen) 425 pfatal("INTERNAL ERROR: dups with -p\n"); 426 if (usedsoftdep) 427 pfatal("INTERNAL ERROR: dups with softdep\n"); 428 pwarn("** Phase 1b - Rescan For More DUPS\n"); 429 pass1b(); 430 } 431 432 /* 433 * 2: traverse directories from root to mark all connected directories 434 */ 435 if (preen == 0) 436 pwarn("** Phase 2 - Check Pathnames\n"); 437 #ifdef PROGRESS 438 if (preen) 439 progress_sethighlim(progress_limits[2]); 440 #endif /* PROGRESS */ 441 pass2(); 442 443 /* 444 * 3: scan inodes looking for disconnected directories 445 */ 446 if (preen == 0) 447 pwarn("** Phase 3 - Check Connectivity\n"); 448 #ifdef PROGRESS 449 if (preen) 450 progress_sethighlim(progress_limits[3]); 451 #endif /* PROGRESS */ 452 pass3(); 453 454 /* 455 * 4: scan inodes looking for disconnected files; check reference counts 456 */ 457 if (preen == 0) 458 pwarn("** Phase 4 - Check Reference Counts\n"); 459 #ifdef PROGRESS 460 if (preen) 461 progress_sethighlim(progress_limits[4]); 462 #endif /* PROGRESS */ 463 pass4(); 464 465 /* 466 * 5: check and repair resource counts in cylinder groups 467 */ 468 if (preen == 0) 469 pwarn("** Phase 5 - Check Cyl groups\n"); 470 #ifdef PROGRESS 471 if (preen) 472 progress_sethighlim(progress_limits[5]); 473 #endif /* PROGRESS */ 474 pass5(); 475 if (uquot_user_hash != NULL) { 476 if (preen == 0) 477 pwarn("** Phase 6 - Check Quotas\n"); 478 pass6(); 479 } 480 481 /* 482 * print out summary statistics 483 */ 484 n_ffree = sblock->fs_cstotal.cs_nffree; 485 n_bfree = sblock->fs_cstotal.cs_nbfree; 486 pwarn("%llu files, %lld used, %lld free ", 487 (unsigned long long)n_files, (long long)n_blks, 488 (long long)(n_ffree + sblock->fs_frag * n_bfree)); 489 printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n", 490 (long long)n_ffree, (long long)n_bfree, 491 (long long)(n_ffree * 100 / (daddr_t)sblock->fs_dsize), 492 (long long)(((n_ffree * 1000 + (daddr_t)sblock->fs_dsize / 2) 493 / (daddr_t)sblock->fs_dsize) % 10)); 494 if (debug && 495 (n_files -= maxino - UFS_ROOTINO - sblock->fs_cstotal.cs_nifree)) 496 printf("%llu files missing\n", (unsigned long long)n_files); 497 if (debug) { 498 n_blks += sblock->fs_ncg * 499 (cgdmin(sblock, 0) - cgsblock(sblock, 0)); 500 n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0); 501 n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize); 502 if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree)) 503 printf("%lld blocks missing\n", (long long)n_blks); 504 if (duplist != NULL) { 505 printf("The following duplicate blocks remain:"); 506 for (dp = duplist; dp; dp = dp->next) 507 printf(" %lld,", (long long)dp->dup); 508 printf("\n"); 509 } 510 if (zlnhead != NULL) { 511 printf("The following zero link count inodes remain:"); 512 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 513 printf(" %llu,", 514 (unsigned long long)zlnp->zlncnt); 515 printf("\n"); 516 } 517 } 518 zlnhead = (struct zlncnt *)0; 519 duplist = (struct dups *)0; 520 muldup = (struct dups *)0; 521 inocleanup(); 522 if (fsmodified) { 523 sblock->fs_time = time(NULL); 524 sbdirty(); 525 } 526 if (rerun) 527 markclean = 0; 528 #if LITE2BORKEN 529 if (!hotroot()) { 530 ckfini(1); 531 } else { 532 struct statvfs stfs_buf; 533 /* 534 * Check to see if root is mounted read-write. 535 */ 536 if (statvfs("/", &stfs_buf) == 0) 537 flags = stfs_buf.f_flag; 538 else 539 flags = 0; 540 if (markclean) 541 markclean = flags & MNT_RDONLY; 542 ckfini(1); 543 } 544 #else 545 ckfini(1); 546 #endif 547 for (cylno = 0; cylno < sblock->fs_ncg; cylno++) 548 if (inostathead[cylno].il_stat != NULL) 549 free(inostathead[cylno].il_stat); 550 free(inostathead); 551 inostathead = NULL; 552 553 if (!resolved || rerun) { 554 pwarn("\n***** UNRESOLVED INCONSISTENCIES REMAIN *****\n"); 555 returntosingle = 1; 556 } 557 if (!fsmodified) 558 return FSCK_EXIT_OK; 559 if (!preen) 560 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 561 if (rerun) 562 pwarn("\n***** PLEASE RERUN FSCK *****\n"); 563 if (hotroot()) { 564 struct statvfs stfs_buf; 565 /* 566 * We modified the root. Do a mount update on 567 * it, unless it is read-write, so we can continue. 568 */ 569 if (statvfs("/", &stfs_buf) == 0) { 570 long flags = stfs_buf.f_flag; 571 struct ufs_args args; 572 573 if (flags & MNT_RDONLY) { 574 args.fspec = 0; 575 flags |= MNT_UPDATE | MNT_RELOAD; 576 if (mount(MOUNT_FFS, "/", flags, 577 &args, sizeof args) == 0) 578 return FSCK_EXIT_OK; 579 } 580 } 581 if (!preen) 582 pwarn("\n***** REBOOT NOW *****\n"); 583 sync(); 584 return FSCK_EXIT_ROOT_CHANGED; 585 } 586 return FSCK_EXIT_OK; 587 } 588 589 static void 590 usage(void) 591 { 592 593 (void) fprintf(stderr, 594 "usage: %s [-" 595 #ifndef NO_APPLE_UFS 596 "a" 597 #endif 598 "dFfPpqUX] " 599 #ifndef NO_FFS_EI 600 "[-B byteorder] " 601 #endif 602 "[-b block] [-c level] [-m mode]\n" 603 "\t[-x snap-backup] [-y | -n] filesystem ...\n", 604 getprogname()); 605 exit(FSCK_EXIT_USAGE); 606 } 607