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