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