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