1 /* 2 * Copyright (c) 1980, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #if 0 31 #ifndef lint 32 static const char copyright[] = 33 "@(#) Copyright (c) 1980, 1986, 1993\n\ 34 The Regents of the University of California. All rights reserved.\n"; 35 #endif /* not lint */ 36 37 #ifndef lint 38 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95"; 39 #endif /* not lint */ 40 #endif 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include <sys/param.h> 45 #include <sys/stat.h> 46 #include <sys/file.h> 47 #include <sys/time.h> 48 #include <sys/mount.h> 49 #include <sys/resource.h> 50 #include <sys/sysctl.h> 51 #include <sys/disklabel.h> 52 53 #include <ufs/ufs/dinode.h> 54 #include <ufs/ufs/ufsmount.h> 55 #include <ufs/ffs/fs.h> 56 57 #include <err.h> 58 #include <errno.h> 59 #include <fstab.h> 60 #include <grp.h> 61 #include <paths.h> 62 #include <stdint.h> 63 #include <string.h> 64 65 #include "fsck.h" 66 67 static void usage(void) __dead2; 68 static int argtoi(int flag, const char *req, const char *str, int base); 69 static int checkfilesys(char *filesys); 70 static int chkdoreload(struct statfs *mntp); 71 static struct statfs *getmntpt(const char *); 72 73 int 74 main(int argc, char *argv[]) 75 { 76 int ch; 77 struct rlimit rlimit; 78 struct itimerval itimerval; 79 int ret = 0; 80 81 sync(); 82 skipclean = 1; 83 while ((ch = getopt(argc, argv, "b:Bc:dfFm:npy")) != -1) { 84 switch (ch) { 85 case 'b': 86 skipclean = 0; 87 bflag = argtoi('b', "number", optarg, 10); 88 printf("Alternate super block location: %d\n", bflag); 89 break; 90 91 case 'B': 92 bkgrdflag = 1; 93 break; 94 95 case 'c': 96 skipclean = 0; 97 cvtlevel = argtoi('c', "conversion level", optarg, 10); 98 if (cvtlevel < 3) 99 errx(EEXIT, "cannot do level %d conversion", 100 cvtlevel); 101 break; 102 103 case 'd': 104 debug++; 105 break; 106 107 case 'f': 108 skipclean = 0; 109 break; 110 111 case 'F': 112 bkgrdcheck = 1; 113 break; 114 115 case 'm': 116 lfmode = argtoi('m', "mode", optarg, 8); 117 if (lfmode &~ 07777) 118 errx(EEXIT, "bad mode to -m: %o", lfmode); 119 printf("** lost+found creation mode %o\n", lfmode); 120 break; 121 122 case 'n': 123 nflag++; 124 yflag = 0; 125 break; 126 127 case 'p': 128 preen++; 129 break; 130 131 case 'y': 132 yflag++; 133 nflag = 0; 134 break; 135 136 default: 137 usage(); 138 } 139 } 140 argc -= optind; 141 argv += optind; 142 143 if (!argc) 144 usage(); 145 146 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 147 (void)signal(SIGINT, catch); 148 if (preen) 149 (void)signal(SIGQUIT, catchquit); 150 signal(SIGINFO, infohandler); 151 if (bkgrdflag) { 152 signal(SIGALRM, alarmhandler); 153 itimerval.it_interval.tv_sec = 5; 154 itimerval.it_interval.tv_usec = 0; 155 itimerval.it_value.tv_sec = 5; 156 itimerval.it_value.tv_usec = 0; 157 setitimer(ITIMER_REAL, &itimerval, NULL); 158 } 159 /* 160 * Push up our allowed memory limit so we can cope 161 * with huge file systems. 162 */ 163 if (getrlimit(RLIMIT_DATA, &rlimit) == 0) { 164 rlimit.rlim_cur = rlimit.rlim_max; 165 (void)setrlimit(RLIMIT_DATA, &rlimit); 166 } 167 while (argc-- > 0) 168 (void)checkfilesys(*argv++); 169 170 if (returntosingle) 171 ret = 2; 172 exit(ret); 173 } 174 175 static int 176 argtoi(int flag, const char *req, const char *str, int base) 177 { 178 char *cp; 179 int ret; 180 181 ret = (int)strtol(str, &cp, base); 182 if (cp == str || *cp) 183 errx(EEXIT, "-%c flag requires a %s", flag, req); 184 return (ret); 185 } 186 187 /* 188 * Check the specified file system. 189 */ 190 /* ARGSUSED */ 191 static int 192 checkfilesys(char *filesys) 193 { 194 ufs2_daddr_t n_ffree, n_bfree; 195 struct ufs_args args; 196 struct dups *dp; 197 struct statfs *mntp; 198 struct stat snapdir; 199 struct group *grp; 200 ufs2_daddr_t blks; 201 int cylno; 202 ino_t files; 203 size_t size; 204 205 cdevname = filesys; 206 if (debug && preen) 207 pwarn("starting\n"); 208 /* 209 * Make best effort to get the disk name. Check first to see 210 * if it is listed among the mounted file systems. Failing that 211 * check to see if it is listed in /etc/fstab. 212 */ 213 mntp = getmntpt(filesys); 214 if (mntp != NULL) 215 filesys = mntp->f_mntfromname; 216 else 217 filesys = blockcheck(filesys); 218 /* 219 * If -F flag specified, check to see whether a background check 220 * is possible and needed. If possible and needed, exit with 221 * status zero. Otherwise exit with status non-zero. A non-zero 222 * exit status will cause a foreground check to be run. 223 */ 224 sblock_init(); 225 if (bkgrdcheck) { 226 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0) 227 exit(3); /* Cannot read superblock */ 228 close(fsreadfd); 229 if (sblock.fs_flags & FS_NEEDSFSCK) 230 exit(4); /* Earlier background failed */ 231 if ((sblock.fs_flags & FS_DOSOFTDEP) == 0) 232 exit(5); /* Not running soft updates */ 233 size = MIBSIZE; 234 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0) 235 exit(6); /* Lacks kernel support */ 236 if ((mntp == NULL && sblock.fs_clean == 1) || 237 (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0)) 238 exit(7); /* Filesystem clean, report it now */ 239 exit(0); 240 } 241 if (preen && skipclean) { 242 /* 243 * If file system is gjournaled, check it here. 244 */ 245 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0) 246 exit(3); /* Cannot read superblock */ 247 close(fsreadfd); 248 if ((sblock.fs_flags & FS_GJOURNAL) != 0) { 249 //printf("GJournaled file system detected on %s.\n", 250 // filesys); 251 if (sblock.fs_clean == 1) { 252 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n"); 253 exit(0); 254 } 255 if ((sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) { 256 gjournal_check(filesys); 257 if (chkdoreload(mntp) == 0) 258 exit(0); 259 exit(4); 260 } else { 261 pfatal("UNEXPECTED INCONSISTENCY, %s\n", 262 "CANNOT RUN FAST FSCK\n"); 263 } 264 } 265 } 266 /* 267 * If we are to do a background check: 268 * Get the mount point information of the file system 269 * create snapshot file 270 * return created snapshot file 271 * if not found, clear bkgrdflag and proceed with normal fsck 272 */ 273 if (bkgrdflag) { 274 if (mntp == NULL) { 275 bkgrdflag = 0; 276 pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n"); 277 } else if ((mntp->f_flags & MNT_SOFTDEP) == 0) { 278 bkgrdflag = 0; 279 pfatal("NOT USING SOFT UPDATES, %s\n", 280 "CANNOT RUN IN BACKGROUND"); 281 } else if ((mntp->f_flags & MNT_RDONLY) != 0) { 282 bkgrdflag = 0; 283 pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n"); 284 } else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) { 285 if (readsb(0) != 0) { 286 if (sblock.fs_flags & FS_NEEDSFSCK) { 287 bkgrdflag = 0; 288 pfatal("UNEXPECTED INCONSISTENCY, %s\n", 289 "CANNOT RUN IN BACKGROUND\n"); 290 } 291 if ((sblock.fs_flags & FS_UNCLEAN) == 0 && 292 skipclean && preen) { 293 /* 294 * file system is clean; 295 * skip snapshot and report it clean 296 */ 297 pwarn("FILE SYSTEM CLEAN; %s\n", 298 "SKIPPING CHECKS"); 299 goto clean; 300 } 301 } 302 close(fsreadfd); 303 } 304 if (bkgrdflag) { 305 snprintf(snapname, sizeof snapname, "%s/.snap", 306 mntp->f_mntonname); 307 if (stat(snapname, &snapdir) < 0) { 308 if (errno != ENOENT) { 309 bkgrdflag = 0; 310 pfatal("CANNOT FIND %s %s: %s, %s\n", 311 "SNAPSHOT DIRECTORY", 312 snapname, strerror(errno), 313 "CANNOT RUN IN BACKGROUND"); 314 } else if ((grp = getgrnam("operator")) == 0 || 315 mkdir(snapname, 0770) < 0 || 316 chown(snapname, -1, grp->gr_gid) < 0 || 317 chmod(snapname, 0770) < 0) { 318 bkgrdflag = 0; 319 pfatal("CANNOT CREATE %s %s: %s, %s\n", 320 "SNAPSHOT DIRECTORY", 321 snapname, strerror(errno), 322 "CANNOT RUN IN BACKGROUND"); 323 } 324 } else if (!S_ISDIR(snapdir.st_mode)) { 325 bkgrdflag = 0; 326 pfatal("%s IS NOT A DIRECTORY, %s\n", snapname, 327 "CANNOT RUN IN BACKGROUND"); 328 } 329 } 330 if (bkgrdflag) { 331 snprintf(snapname, sizeof snapname, 332 "%s/.snap/fsck_snapshot", mntp->f_mntonname); 333 memset(&args, 0, sizeof args); 334 args.fspec = snapname; 335 while (mount("ffs", mntp->f_mntonname, 336 mntp->f_flags | MNT_UPDATE | MNT_SNAPSHOT, 337 &args) < 0) { 338 if (errno == EEXIST && unlink(snapname) == 0) 339 continue; 340 bkgrdflag = 0; 341 pfatal("CANNOT CREATE SNAPSHOT %s: %s\n", 342 snapname, strerror(errno)); 343 break; 344 } 345 if (bkgrdflag != 0) 346 filesys = snapname; 347 } 348 } 349 350 switch (setup(filesys)) { 351 case 0: 352 if (preen) 353 pfatal("CAN'T CHECK FILE SYSTEM."); 354 return (0); 355 case -1: 356 clean: 357 pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree + 358 sblock.fs_frag * sblock.fs_cstotal.cs_nbfree)); 359 printf("(%lld frags, %lld blocks, %.1f%% fragmentation)\n", 360 (long long)sblock.fs_cstotal.cs_nffree, 361 (long long)sblock.fs_cstotal.cs_nbfree, 362 sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize); 363 return (0); 364 } 365 366 /* 367 * Cleared if any questions answered no. Used to decide if 368 * the superblock should be marked clean. 369 */ 370 resolved = 1; 371 /* 372 * 1: scan inodes tallying blocks used 373 */ 374 if (preen == 0) { 375 printf("** Last Mounted on %s\n", sblock.fs_fsmnt); 376 if (mntp != NULL && mntp->f_flags & MNT_ROOTFS) 377 printf("** Root file system\n"); 378 printf("** Phase 1 - Check Blocks and Sizes\n"); 379 } 380 pass1(); 381 382 /* 383 * 1b: locate first references to duplicates, if any 384 */ 385 if (duplist) { 386 if (preen || usedsoftdep) 387 pfatal("INTERNAL ERROR: dups with -p"); 388 printf("** Phase 1b - Rescan For More DUPS\n"); 389 pass1b(); 390 } 391 392 /* 393 * 2: traverse directories from root to mark all connected directories 394 */ 395 if (preen == 0) 396 printf("** Phase 2 - Check Pathnames\n"); 397 pass2(); 398 399 /* 400 * 3: scan inodes looking for disconnected directories 401 */ 402 if (preen == 0) 403 printf("** Phase 3 - Check Connectivity\n"); 404 pass3(); 405 406 /* 407 * 4: scan inodes looking for disconnected files; check reference counts 408 */ 409 if (preen == 0) 410 printf("** Phase 4 - Check Reference Counts\n"); 411 pass4(); 412 413 /* 414 * 5: check and repair resource counts in cylinder groups 415 */ 416 if (preen == 0) 417 printf("** Phase 5 - Check Cyl groups\n"); 418 pass5(); 419 420 /* 421 * print out summary statistics 422 */ 423 n_ffree = sblock.fs_cstotal.cs_nffree; 424 n_bfree = sblock.fs_cstotal.cs_nbfree; 425 files = maxino - ROOTINO - sblock.fs_cstotal.cs_nifree - n_files; 426 blks = n_blks + 427 sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0)); 428 blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0); 429 blks += howmany(sblock.fs_cssize, sblock.fs_fsize); 430 blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks; 431 if (bkgrdflag && (files > 0 || blks > 0)) { 432 countdirs = sblock.fs_cstotal.cs_ndir - countdirs; 433 pwarn("Reclaimed: %ld directories, %ld files, %lld fragments\n", 434 countdirs, (long)files - countdirs, (long long)blks); 435 } 436 pwarn("%ld files, %jd used, %ju free ", 437 (long)n_files, (intmax_t)n_blks, 438 (uintmax_t)n_ffree + sblock.fs_frag * n_bfree); 439 printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n", 440 (uintmax_t)n_ffree, (uintmax_t)n_bfree, 441 n_ffree * 100.0 / sblock.fs_dsize); 442 if (debug) { 443 if (files < 0) 444 printf("%d inodes missing\n", -files); 445 if (blks < 0) 446 printf("%lld blocks missing\n", -(long long)blks); 447 if (duplist != NULL) { 448 printf("The following duplicate blocks remain:"); 449 for (dp = duplist; dp; dp = dp->next) 450 printf(" %lld,", (long long)dp->dup); 451 printf("\n"); 452 } 453 } 454 duplist = (struct dups *)0; 455 muldup = (struct dups *)0; 456 inocleanup(); 457 if (fsmodified) { 458 sblock.fs_time = time(NULL); 459 sbdirty(); 460 } 461 if (cvtlevel && sblk.b_dirty) { 462 /* 463 * Write out the duplicate super blocks 464 */ 465 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 466 blwrite(fswritefd, (char *)&sblock, 467 fsbtodb(&sblock, cgsblock(&sblock, cylno)), 468 SBLOCKSIZE); 469 } 470 if (rerun) 471 resolved = 0; 472 473 /* 474 * Check to see if the file system is mounted read-write. 475 */ 476 if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0) 477 resolved = 0; 478 ckfini(resolved); 479 480 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 481 if (inostathead[cylno].il_stat != NULL) 482 free((char *)inostathead[cylno].il_stat); 483 free((char *)inostathead); 484 inostathead = NULL; 485 if (fsmodified && !preen) 486 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 487 if (rerun) 488 printf("\n***** PLEASE RERUN FSCK *****\n"); 489 if (chkdoreload(mntp) != 0) { 490 if (!fsmodified) 491 return (0); 492 if (!preen) 493 printf("\n***** REBOOT NOW *****\n"); 494 sync(); 495 return (4); 496 } 497 return (0); 498 } 499 500 static int 501 chkdoreload(struct statfs *mntp) 502 { 503 struct ufs_args args; 504 505 if (mntp == NULL) 506 return (0); 507 /* 508 * We modified a mounted file system. Do a mount update on 509 * it unless it is read-write, so we can continue using it 510 * as safely as possible. 511 */ 512 if (mntp->f_flags & MNT_RDONLY) { 513 memset(&args, 0, sizeof args); 514 /* 515 * args.fspec = 0; 516 * args.export.ex_flags = 0; 517 * args.export.ex_root = 0; 518 */ 519 if (mount("ufs", mntp->f_mntonname, 520 mntp->f_flags | MNT_UPDATE | MNT_RELOAD, &args) == 0) { 521 return (0); 522 } 523 pwarn("mount reload of '%s' failed: %s\n\n", 524 mntp->f_mntonname, strerror(errno)); 525 return (1); 526 } 527 return (0); 528 } 529 530 /* 531 * Get the mount point information for name. 532 */ 533 static struct statfs * 534 getmntpt(const char *name) 535 { 536 struct stat devstat, mntdevstat; 537 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN]; 538 char *ddevname; 539 struct statfs *mntbuf, *statfsp; 540 int i, mntsize, isdev; 541 542 if (stat(name, &devstat) != 0) 543 return (NULL); 544 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode)) 545 isdev = 1; 546 else 547 isdev = 0; 548 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 549 for (i = 0; i < mntsize; i++) { 550 statfsp = &mntbuf[i]; 551 ddevname = statfsp->f_mntfromname; 552 if (*ddevname != '/') { 553 strcpy(device, _PATH_DEV); 554 strcat(device, ddevname); 555 strcpy(statfsp->f_mntfromname, device); 556 } 557 if (isdev == 0) { 558 if (strcmp(name, statfsp->f_mntonname)) 559 continue; 560 return (statfsp); 561 } 562 if (stat(ddevname, &mntdevstat) == 0 && 563 mntdevstat.st_rdev == devstat.st_rdev) 564 return (statfsp); 565 } 566 statfsp = NULL; 567 return (statfsp); 568 } 569 570 static void 571 usage(void) 572 { 573 (void) fprintf(stderr, 574 "usage: %s [-BFpfny] [-b block] [-c level] [-m mode] " 575 "filesystem ...\n", 576 getprogname()); 577 exit(1); 578 } 579