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