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