1 /* $NetBSD: main.c,v 1.42 2002/09/28 20:11:06 dbj 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.42 2002/09/28 20:11:06 dbj 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 (void)checkfilesys(blockcheck(*argv++), 0, 0L, 0); 178 179 if (returntosingle) 180 ret = 2; 181 182 exit(ret); 183 } 184 185 static int 186 argtoi(flag, req, str, base) 187 int flag; 188 char *req, *str; 189 int base; 190 { 191 char *cp; 192 int ret; 193 194 ret = (int)strtol(str, &cp, base); 195 if (cp == str || *cp) 196 errx(EEXIT, "-%c flag requires a %s", flag, req); 197 return (ret); 198 } 199 200 /* 201 * Check the specified filesystem. 202 */ 203 /* ARGSUSED */ 204 static int 205 checkfilesys(filesys, mntpt, auxdata, child) 206 const char *filesys; 207 char *mntpt; 208 long auxdata; 209 int child; 210 { 211 ufs_daddr_t n_ffree, n_bfree; 212 struct dups *dp; 213 struct zlncnt *zlnp; 214 #ifdef LITE2BORKEN 215 int flags; 216 #endif 217 218 if (preen && child) 219 (void)signal(SIGQUIT, voidquit); 220 setcdevname(filesys, preen); 221 if (debug && preen) 222 pwarn("starting\n"); 223 switch (setup(filesys)) { 224 case 0: 225 if (preen) 226 pfatal("CAN'T CHECK FILE SYSTEM."); 227 /* fall through */ 228 case -1: 229 return (0); 230 } 231 /* 232 * Cleared if any questions answered no. Used to decide if 233 * the superblock should be marked clean. 234 */ 235 resolved = 1; 236 /* 237 * 1: scan inodes tallying blocks used 238 */ 239 if (preen == 0) { 240 printf("** Last Mounted on %s\n", sblock->fs_fsmnt); 241 if (hotroot()) 242 printf("** Root file system\n"); 243 printf("** Phase 1 - Check Blocks and Sizes\n"); 244 } 245 pass1(); 246 247 /* 248 * 1b: locate first references to duplicates, if any 249 */ 250 if (duplist) { 251 if (preen) 252 pfatal("INTERNAL ERROR: dups with -p\n"); 253 if (usedsoftdep) 254 pfatal("INTERNAL ERROR: dups with softdep\n"); 255 printf("** Phase 1b - Rescan For More DUPS\n"); 256 pass1b(); 257 } 258 259 /* 260 * 2: traverse directories from root to mark all connected directories 261 */ 262 if (preen == 0) 263 printf("** Phase 2 - Check Pathnames\n"); 264 pass2(); 265 266 /* 267 * 3: scan inodes looking for disconnected directories 268 */ 269 if (preen == 0) 270 printf("** Phase 3 - Check Connectivity\n"); 271 pass3(); 272 273 /* 274 * 4: scan inodes looking for disconnected files; check reference counts 275 */ 276 if (preen == 0) 277 printf("** Phase 4 - Check Reference Counts\n"); 278 pass4(); 279 280 /* 281 * 5: check and repair resource counts in cylinder groups 282 */ 283 if (preen == 0) 284 printf("** Phase 5 - Check Cyl groups\n"); 285 pass5(); 286 287 /* 288 * print out summary statistics 289 */ 290 n_ffree = sblock->fs_cstotal.cs_nffree; 291 n_bfree = sblock->fs_cstotal.cs_nbfree; 292 pwarn("%d files, %d used, %d free ", 293 n_files, n_blks, n_ffree + sblock->fs_frag * n_bfree); 294 printf("(%d frags, %d blocks, %d.%d%% fragmentation)\n", 295 n_ffree, n_bfree, (n_ffree * 100) / sblock->fs_dsize, 296 ((n_ffree * 1000 + sblock->fs_dsize / 2) / sblock->fs_dsize) % 10); 297 if (debug && 298 (n_files -= maxino - ROOTINO - sblock->fs_cstotal.cs_nifree)) 299 printf("%d files missing\n", n_files); 300 if (debug) { 301 n_blks += sblock->fs_ncg * 302 (cgdmin(sblock, 0) - cgsblock(sblock, 0)); 303 n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0); 304 n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize); 305 if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree)) 306 printf("%d blocks missing\n", n_blks); 307 if (duplist != NULL) { 308 printf("The following duplicate blocks remain:"); 309 for (dp = duplist; dp; dp = dp->next) 310 printf(" %d,", dp->dup); 311 printf("\n"); 312 } 313 if (zlnhead != NULL) { 314 printf("The following zero link count inodes remain:"); 315 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 316 printf(" %u,", zlnp->zlncnt); 317 printf("\n"); 318 } 319 } 320 zlnhead = (struct zlncnt *)0; 321 duplist = (struct dups *)0; 322 muldup = (struct dups *)0; 323 inocleanup(); 324 if (fsmodified) { 325 time_t t; 326 (void)time(&t); 327 sblock->fs_time = t; 328 sbdirty(); 329 } 330 if (rerun) 331 markclean = 0; 332 #if LITE2BORKEN 333 if (!hotroot()) { 334 ckfini(); 335 } else { 336 struct statfs stfs_buf; 337 /* 338 * Check to see if root is mounted read-write. 339 */ 340 if (statfs("/", &stfs_buf) == 0) 341 flags = stfs_buf.f_flags; 342 else 343 flags = 0; 344 if (markclean) 345 markclean = flags & MNT_RDONLY; 346 ckfini(); 347 } 348 #else 349 ckfini(); 350 #endif 351 free(blockmap); 352 free(statemap); 353 free((char *)lncntp); 354 if (!fsmodified) 355 return (0); 356 if (!preen) 357 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 358 if (rerun) 359 printf("\n***** PLEASE RERUN FSCK *****\n"); 360 if (hotroot()) { 361 struct statfs stfs_buf; 362 /* 363 * We modified the root. Do a mount update on 364 * it, unless it is read-write, so we can continue. 365 */ 366 if (statfs("/", &stfs_buf) == 0) { 367 long flags = stfs_buf.f_flags; 368 struct ufs_args args; 369 int ret; 370 371 if (flags & MNT_RDONLY) { 372 args.fspec = 0; 373 args.export.ex_flags = 0; 374 args.export.ex_root = 0; 375 flags |= MNT_UPDATE | MNT_RELOAD; 376 ret = mount(MOUNT_FFS, "/", flags, &args); 377 if (ret == 0) 378 return(0); 379 } 380 } 381 if (!preen) 382 printf("\n***** REBOOT NOW *****\n"); 383 sync(); 384 return (4); 385 } 386 return (0); 387 } 388 389 static void 390 usage() 391 { 392 393 (void) fprintf(stderr, 394 "Usage: %s [-dFfnpy] [-B be|le] [-b block] [-c level] [-m mode]" 395 " filesystem ...\n", 396 getprogname()); 397 exit(1); 398 } 399 400