1 /* $NetBSD: main.c,v 1.45 2003/04/02 10:39:26 fvdl Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1986, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95"; 45 #else 46 __RCSID("$NetBSD: main.c,v 1.45 2003/04/02 10:39:26 fvdl Exp $"); 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/time.h> 52 #include <sys/mount.h> 53 #include <sys/resource.h> 54 55 #include <ufs/ufs/dinode.h> 56 #include <ufs/ufs/ufsmount.h> 57 #include <ufs/ffs/fs.h> 58 #include <ufs/ffs/ffs_extern.h> 59 60 #include <ctype.h> 61 #include <err.h> 62 #include <fstab.h> 63 #include <string.h> 64 #include <time.h> 65 #include <ctype.h> 66 #include <stdio.h> 67 #include <stdlib.h> 68 #include <unistd.h> 69 70 #include "fsck.h" 71 #include "extern.h" 72 #include "fsutil.h" 73 74 int returntosingle; 75 76 int main __P((int, char *[])); 77 78 static int argtoi __P((int, char *, char *, int)); 79 static int checkfilesys __P((const char *, char *, long, int)); 80 static void usage __P((void)); 81 82 int 83 main(argc, argv) 84 int argc; 85 char *argv[]; 86 { 87 struct rlimit r; 88 int ch; 89 int ret = 0; 90 91 if (getrlimit(RLIMIT_DATA, &r) == 0) { 92 r.rlim_cur = r.rlim_max; 93 (void) setrlimit(RLIMIT_DATA, &r); 94 } 95 sync(); 96 skipclean = 1; 97 markclean = 1; 98 forceimage = 0; 99 endian = 0; 100 isappleufs = 0; 101 while ((ch = getopt(argc, argv, "aB:b:c:dFfm:npy")) != -1) { 102 switch (ch) { 103 case 'a': 104 isappleufs = 1; 105 break; 106 107 case 'B': 108 if (strcmp(optarg, "be") == 0) 109 endian = BIG_ENDIAN; 110 else if (strcmp(optarg, "le") == 0) 111 endian = LITTLE_ENDIAN; 112 else usage(); 113 break; 114 115 case 'b': 116 skipclean = 0; 117 bflag = argtoi('b', "number", optarg, 10); 118 printf("Alternate super block location: %d\n", bflag); 119 break; 120 121 case 'c': 122 skipclean = 0; 123 cvtlevel = argtoi('c', "conversion level", optarg, 10); 124 break; 125 126 case 'd': 127 debug++; 128 break; 129 130 case 'F': 131 forceimage = 1; 132 break; 133 134 case 'f': 135 skipclean = 0; 136 break; 137 138 case 'm': 139 lfmode = argtoi('m', "mode", optarg, 8); 140 if (lfmode &~ 07777) 141 errx(EEXIT, "bad mode to -m: %o", lfmode); 142 printf("** lost+found creation mode %o\n", lfmode); 143 break; 144 145 case 'n': 146 nflag++; 147 yflag = 0; 148 break; 149 150 case 'p': 151 preen++; 152 break; 153 154 case 'y': 155 yflag++; 156 nflag = 0; 157 break; 158 159 default: 160 usage(); 161 } 162 } 163 164 argc -= optind; 165 argv += optind; 166 167 if (!argc) 168 usage(); 169 170 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 171 (void)signal(SIGINT, catch); 172 if (preen) 173 (void)signal(SIGQUIT, catchquit); 174 signal(SIGINFO, infohandler); 175 176 while (argc-- > 0) { 177 const char *path = blockcheck(*argv); 178 179 if (path == NULL) 180 pfatal("Can't check %s\n", *argv); 181 else 182 (void)checkfilesys(blockcheck(*argv), 0, 0L, 0); 183 argv++; 184 } 185 186 if (returntosingle) 187 ret = 2; 188 189 exit(ret); 190 } 191 192 static int 193 argtoi(flag, req, str, base) 194 int flag; 195 char *req, *str; 196 int base; 197 { 198 char *cp; 199 int ret; 200 201 ret = (int)strtol(str, &cp, base); 202 if (cp == str || *cp) 203 errx(EEXIT, "-%c flag requires a %s", flag, req); 204 return (ret); 205 } 206 207 /* 208 * Check the specified filesystem. 209 */ 210 /* ARGSUSED */ 211 static int 212 checkfilesys(filesys, mntpt, auxdata, child) 213 const char *filesys; 214 char *mntpt; 215 long auxdata; 216 int child; 217 { 218 daddr_t n_ffree, n_bfree; 219 struct dups *dp; 220 struct zlncnt *zlnp; 221 int cylno; 222 #ifdef LITE2BORKEN 223 int flags; 224 #endif 225 226 if (preen && child) 227 (void)signal(SIGQUIT, voidquit); 228 setcdevname(filesys, preen); 229 if (debug && preen) 230 pwarn("starting\n"); 231 switch (setup(filesys)) { 232 case 0: 233 if (preen) 234 pfatal("CAN'T CHECK FILE SYSTEM."); 235 /* fall through */ 236 case -1: 237 return (0); 238 } 239 /* 240 * Cleared if any questions answered no. Used to decide if 241 * the superblock should be marked clean. 242 */ 243 resolved = 1; 244 /* 245 * 1: scan inodes tallying blocks used 246 */ 247 if (preen == 0) { 248 printf("** Last Mounted on %s\n", sblock->fs_fsmnt); 249 if (hotroot()) 250 printf("** Root file system\n"); 251 printf("** Phase 1 - Check Blocks and Sizes\n"); 252 } 253 pass1(); 254 255 /* 256 * 1b: locate first references to duplicates, if any 257 */ 258 if (duplist) { 259 if (preen) 260 pfatal("INTERNAL ERROR: dups with -p\n"); 261 if (usedsoftdep) 262 pfatal("INTERNAL ERROR: dups with softdep\n"); 263 printf("** Phase 1b - Rescan For More DUPS\n"); 264 pass1b(); 265 } 266 267 /* 268 * 2: traverse directories from root to mark all connected directories 269 */ 270 if (preen == 0) 271 printf("** Phase 2 - Check Pathnames\n"); 272 pass2(); 273 274 /* 275 * 3: scan inodes looking for disconnected directories 276 */ 277 if (preen == 0) 278 printf("** Phase 3 - Check Connectivity\n"); 279 pass3(); 280 281 /* 282 * 4: scan inodes looking for disconnected files; check reference counts 283 */ 284 if (preen == 0) 285 printf("** Phase 4 - Check Reference Counts\n"); 286 pass4(); 287 288 /* 289 * 5: check and repair resource counts in cylinder groups 290 */ 291 if (preen == 0) 292 printf("** Phase 5 - Check Cyl groups\n"); 293 pass5(); 294 295 /* 296 * print out summary statistics 297 */ 298 n_ffree = sblock->fs_cstotal.cs_nffree; 299 n_bfree = sblock->fs_cstotal.cs_nbfree; 300 pwarn("%d files, %lld used, %lld free ", 301 n_files, (long long)n_blks, 302 (long long)(n_ffree + sblock->fs_frag * n_bfree)); 303 printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n", 304 (long long)n_ffree, (long long)n_bfree, 305 (long long)(n_ffree * 100 / (daddr_t)sblock->fs_dsize), 306 (long long)(((n_ffree * 1000 + (daddr_t)sblock->fs_dsize / 2) 307 / (daddr_t)sblock->fs_dsize) % 10)); 308 if (debug && 309 (n_files -= maxino - ROOTINO - sblock->fs_cstotal.cs_nifree)) 310 printf("%d files missing\n", n_files); 311 if (debug) { 312 n_blks += sblock->fs_ncg * 313 (cgdmin(sblock, 0) - cgsblock(sblock, 0)); 314 n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0); 315 n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize); 316 if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree)) 317 printf("%lld blocks missing\n", (long long)n_blks); 318 if (duplist != NULL) { 319 printf("The following duplicate blocks remain:"); 320 for (dp = duplist; dp; dp = dp->next) 321 printf(" %lld,", (long long)dp->dup); 322 printf("\n"); 323 } 324 if (zlnhead != NULL) { 325 printf("The following zero link count inodes remain:"); 326 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 327 printf(" %u,", zlnp->zlncnt); 328 printf("\n"); 329 } 330 } 331 zlnhead = (struct zlncnt *)0; 332 duplist = (struct dups *)0; 333 muldup = (struct dups *)0; 334 inocleanup(); 335 if (fsmodified) { 336 sblock->fs_time = time(NULL); 337 sbdirty(); 338 } 339 if (rerun) 340 markclean = 0; 341 #if LITE2BORKEN 342 if (!hotroot()) { 343 ckfini(); 344 } else { 345 struct statfs stfs_buf; 346 /* 347 * Check to see if root is mounted read-write. 348 */ 349 if (statfs("/", &stfs_buf) == 0) 350 flags = stfs_buf.f_flags; 351 else 352 flags = 0; 353 if (markclean) 354 markclean = flags & MNT_RDONLY; 355 ckfini(); 356 } 357 #else 358 ckfini(); 359 #endif 360 for (cylno = 0; cylno < sblock->fs_ncg; cylno++) 361 if (inostathead[cylno].il_stat != NULL) 362 free(inostathead[cylno].il_stat); 363 free(inostathead); 364 inostathead = NULL; 365 366 if (!fsmodified) 367 return (0); 368 if (!preen) 369 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 370 if (rerun) 371 printf("\n***** PLEASE RERUN FSCK *****\n"); 372 if (hotroot()) { 373 struct statfs stfs_buf; 374 /* 375 * We modified the root. Do a mount update on 376 * it, unless it is read-write, so we can continue. 377 */ 378 if (statfs("/", &stfs_buf) == 0) { 379 long flags = stfs_buf.f_flags; 380 struct ufs_args args; 381 int ret; 382 383 if (flags & MNT_RDONLY) { 384 args.fspec = 0; 385 args.export.ex_flags = 0; 386 args.export.ex_root = 0; 387 flags |= MNT_UPDATE | MNT_RELOAD; 388 ret = mount(MOUNT_FFS, "/", flags, &args); 389 if (ret == 0) 390 return(0); 391 } 392 } 393 if (!preen) 394 printf("\n***** REBOOT NOW *****\n"); 395 sync(); 396 return (4); 397 } 398 return (0); 399 } 400 401 static void 402 usage() 403 { 404 405 (void) fprintf(stderr, 406 "Usage: %s [-dFfnpy] [-B be|le] [-b block] [-c level] [-m mode]" 407 " filesystem ...\n", 408 getprogname()); 409 exit(1); 410 } 411 412