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