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