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