main.c revision 55275
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 35static 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 42static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95"; 43#endif 44static const char rcsid[] = 45 "$FreeBSD: head/sbin/fsck_ffs/main.c 55275 1999-12-30 16:32:40Z peter $"; 46#endif /* not lint */ 47 48#include <sys/param.h> 49#include <sys/stat.h> 50#include <sys/time.h> 51#include <sys/mount.h> 52#include <sys/resource.h> 53 54#include <ufs/ufs/dinode.h> 55#include <ufs/ufs/ufsmount.h> 56#include <ufs/ffs/fs.h> 57 58#include <err.h> 59#include <fstab.h> 60 61#include "fsck.h" 62 63static int argtoi __P((int flag, char *req, char *str, int base)); 64static int docheck __P((struct fstab *fsp)); 65static int checkfilesys __P((char *filesys, char *mntpt, long auxdata, 66 int child)); 67static struct statfs *getmntpt __P((const char *)); 68int main __P((int argc, char *argv[])); 69 70int 71main(argc, argv) 72 int argc; 73 char *argv[]; 74{ 75 int ch; 76 int ret, maxrun = 0; 77 struct rlimit rlimit; 78 79 sync(); 80 while ((ch = getopt(argc, argv, "dfpnNyYb:c:l:m:")) != -1) { 81 switch (ch) { 82 case 'p': 83 preen++; 84 break; 85 86 case 'b': 87 bflag = argtoi('b', "number", optarg, 10); 88 printf("Alternate super block location: %d\n", bflag); 89 break; 90 91 case 'c': 92 cvtlevel = argtoi('c', "conversion level", optarg, 10); 93 break; 94 95 case 'd': 96 debug++; 97 break; 98 99 case 'f': 100 fflag++; 101 break; 102 103 case 'l': 104 maxrun = argtoi('l', "number", optarg, 10); 105 break; 106 107 case 'm': 108 lfmode = argtoi('m', "mode", optarg, 8); 109 if (lfmode &~ 07777) 110 errx(EEXIT, "bad mode to -m: %o", lfmode); 111 printf("** lost+found creation mode %o\n", lfmode); 112 break; 113 114 case 'n': 115 case 'N': 116 nflag++; 117 yflag = 0; 118 break; 119 120 case 'y': 121 case 'Y': 122 yflag++; 123 nflag = 0; 124 break; 125 126 default: 127 errx(EEXIT, "%c option?", ch); 128 } 129 } 130 argc -= optind; 131 argv += optind; 132 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 133 (void)signal(SIGINT, catch); 134 if (preen) 135 (void)signal(SIGQUIT, catchquit); 136 /* 137 * Push up our allowed memory limit so we can cope 138 * with huge filesystems. 139 */ 140 if (getrlimit(RLIMIT_DATA, &rlimit) == 0) { 141 rlimit.rlim_cur = rlimit.rlim_max; 142 (void)setrlimit(RLIMIT_DATA, &rlimit); 143 } 144 if (argc) { 145 while (argc-- > 0) { 146 char *path = blockcheck(*argv); 147 148 if (path == NULL) 149 pfatal("Can't check %s\n", *argv); 150 else 151 (void)checkfilesys(path, 0, 0L, 0); 152 ++argv; 153 } 154 exit(0); 155 } 156 ret = checkfstab(preen, maxrun, docheck, checkfilesys); 157 if (returntosingle) 158 exit(2); 159 exit(ret); 160} 161 162static int 163argtoi(flag, req, str, base) 164 int flag; 165 char *req, *str; 166 int base; 167{ 168 char *cp; 169 int ret; 170 171 ret = (int)strtol(str, &cp, base); 172 if (cp == str || *cp) 173 errx(EEXIT, "-%c flag requires a %s", flag, req); 174 return (ret); 175} 176 177/* 178 * Determine whether a filesystem should be checked. 179 */ 180static int 181docheck(fsp) 182 register struct fstab *fsp; 183{ 184 185 if (strcmp(fsp->fs_vfstype, "ufs") || 186 (strcmp(fsp->fs_type, FSTAB_RW) && 187 strcmp(fsp->fs_type, FSTAB_RO)) || 188 fsp->fs_passno == 0) 189 return (0); 190 return (1); 191} 192 193/* 194 * Check the specified filesystem. 195 */ 196/* ARGSUSED */ 197static int 198checkfilesys(filesys, mntpt, auxdata, child) 199 char *filesys, *mntpt; 200 long auxdata; 201 int child; 202{ 203 ufs_daddr_t n_ffree, n_bfree; 204 struct dups *dp; 205 struct zlncnt *zlnp; 206 struct statfs *mntbuf; 207 int cylno; 208 209 if (preen && child) 210 (void)signal(SIGQUIT, voidquit); 211 cdevname = filesys; 212 if (debug && preen) 213 pwarn("starting\n"); 214 switch (setup(filesys)) { 215 case 0: 216 if (preen) 217 pfatal("CAN'T CHECK FILE SYSTEM."); 218 return (0); 219 case -1: 220 pwarn("clean, %ld free ", sblock.fs_cstotal.cs_nffree + 221 sblock.fs_frag * sblock.fs_cstotal.cs_nbfree); 222 printf("(%d frags, %d blocks, %.1f%% fragmentation)\n", 223 sblock.fs_cstotal.cs_nffree, sblock.fs_cstotal.cs_nbfree, 224 sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize); 225 return (0); 226 } 227 228 /* 229 * get the mount point information of the filesystem, if 230 * it is available. 231 */ 232 mntbuf = getmntpt(filesys); 233 234 /* 235 * Cleared if any questions answered no. Used to decide if 236 * the superblock should be marked clean. 237 */ 238 resolved = 1; 239 /* 240 * 1: scan inodes tallying blocks used 241 */ 242 if (preen == 0) { 243 printf("** Last Mounted on %s\n", sblock.fs_fsmnt); 244 if (mntbuf != NULL && mntbuf->f_flags & MNT_ROOTFS) 245 printf("** Root file system\n"); 246 printf("** Phase 1 - Check Blocks and Sizes\n"); 247 } 248 pass1(); 249 250 /* 251 * 1b: locate first references to duplicates, if any 252 */ 253 if (duplist) { 254 if (preen || usedsoftdep) 255 pfatal("INTERNAL ERROR: dups with -p"); 256 printf("** Phase 1b - Rescan For More DUPS\n"); 257 pass1b(); 258 } 259 260 /* 261 * 2: traverse directories from root to mark all connected directories 262 */ 263 if (preen == 0) 264 printf("** Phase 2 - Check Pathnames\n"); 265 pass2(); 266 267 /* 268 * 3: scan inodes looking for disconnected directories 269 */ 270 if (preen == 0) 271 printf("** Phase 3 - Check Connectivity\n"); 272 pass3(); 273 274 /* 275 * 4: scan inodes looking for disconnected files; check reference counts 276 */ 277 if (preen == 0) 278 printf("** Phase 4 - Check Reference Counts\n"); 279 pass4(); 280 281 /* 282 * 5: check and repair resource counts in cylinder groups 283 */ 284 if (preen == 0) 285 printf("** Phase 5 - Check Cyl groups\n"); 286 pass5(); 287 288 /* 289 * print out summary statistics 290 */ 291 n_ffree = sblock.fs_cstotal.cs_nffree; 292 n_bfree = sblock.fs_cstotal.cs_nbfree; 293 pwarn("%ld files, %ld used, %ld free ", 294 n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree); 295 printf("(%d frags, %d blocks, %.1f%% fragmentation)\n", 296 n_ffree, n_bfree, n_ffree * 100.0 / sblock.fs_dsize); 297 if (debug && 298 (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree)) 299 printf("%d files missing\n", n_files); 300 if (debug) { 301 n_blks += sblock.fs_ncg * 302 (cgdmin(&sblock, 0) - cgsblock(&sblock, 0)); 303 n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0); 304 n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize); 305 if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree)) 306 printf("%d blocks missing\n", n_blks); 307 if (duplist != NULL) { 308 printf("The following duplicate blocks remain:"); 309 for (dp = duplist; dp; dp = dp->next) 310 printf(" %d,", dp->dup); 311 printf("\n"); 312 } 313 if (zlnhead != NULL) { 314 printf("The following zero link count inodes remain:"); 315 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 316 printf(" %u,", zlnp->zlncnt); 317 printf("\n"); 318 } 319 } 320 zlnhead = (struct zlncnt *)0; 321 duplist = (struct dups *)0; 322 muldup = (struct dups *)0; 323 inocleanup(); 324 if (fsmodified) { 325 sblock.fs_time = time(NULL); 326 sbdirty(); 327 } 328 if (cvtlevel && sblk.b_dirty) { 329 /* 330 * Write out the duplicate super blocks 331 */ 332 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 333 bwrite(fswritefd, (char *)&sblock, 334 fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE); 335 } 336 if (rerun) 337 resolved = 0; 338 339 /* Check to see if the filesystem if mounted read-write */ 340 if (mntbuf != NULL && (mntbuf->f_flags & MNT_RDONLY) == 0) 341 resolved = 0; 342 ckfini(resolved); 343 344 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 345 if (inostathead[cylno].il_stat != NULL) 346 free((char *)inostathead[cylno].il_stat); 347 free((char *)inostathead); 348 inostathead = NULL; 349 if (fsmodified && !preen) 350 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 351 if (rerun) 352 printf("\n***** PLEASE RERUN FSCK *****\n"); 353 354 /* 355 * Always do a mount update if the current filesystem 356 * is mounted read-only. 357 */ 358 if (mntbuf != NULL && mntbuf->f_flags & MNT_RDONLY) { 359 struct ufs_args args; 360 int ret; 361 362 args.fspec = 0; 363 args.export.ex_flags = 0; 364 args.export.ex_root = 0; 365 mntbuf->f_flags |= MNT_UPDATE | MNT_RELOAD; 366 ret = mount("ufs", mntbuf->f_mntonname , mntbuf->f_flags, &args); 367 if (ret < 0) 368 perror("mount"); 369 if (ret == 0) 370 return 0; 371 if (!fsmodified) 372 return (0); 373 if (!preen) 374 printf("\n***** REBOOT NOW *****\n"); 375 sync(); 376 return (4); 377 } 378 return (0); 379} 380 381/* 382 * get the directory the device is mounted on. 383 */ 384static struct statfs * 385getmntpt(name) 386 const char *name; 387{ 388 struct statfs *mntbuf; 389 struct stat devstat, mntdevstat; 390 char device[MAXPATHLEN]; 391 int i, mntsize; 392 393 if (realpath(name, device) == NULL) 394 return (NULL); 395 396 if (stat(device, &devstat) != 0 || 397 !(S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))) 398 return (NULL); 399 400 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 401 for (i = 0; i < mntsize; i++) { 402 if (realpath(mntbuf[i].f_mntfromname, device) == NULL || 403 stat(device, &mntdevstat) != 0 || 404 !(S_ISCHR(mntdevstat.st_mode) || 405 S_ISBLK(mntdevstat.st_mode))) 406 continue; 407 if (mntdevstat.st_rdev == devstat.st_rdev) 408 return (&mntbuf[i]); 409 } 410 return (NULL); 411} 412