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