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