1 /* $NetBSD: main.c,v 1.27 2005/09/23 12:10:34 jmmv 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/time.h> 34 #include <sys/mount.h> 35 #include <ufs/ufs/dinode.h> 36 #include <sys/mount.h> 37 #include <ufs/ufs/ufsmount.h> 38 #include <ufs/lfs/lfs.h> 39 40 #include <fstab.h> 41 #include <stdarg.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <ctype.h> 45 #include <stdio.h> 46 #include <unistd.h> 47 #include <err.h> 48 49 #include "fsck.h" 50 #include "extern.h" 51 #include "fsutil.h" 52 53 int returntosingle; 54 55 static int argtoi(int, const char *, const char *, int); 56 static int checkfilesys(const char *, char *, long, int); 57 static void usage(void); 58 extern void (*panic_func)(int, const char *, va_list); 59 60 int 61 main(int argc, char **argv) 62 { 63 int ch; 64 int ret = 0; 65 const char *optstring = "b:dfi:m:npPqy"; 66 67 sync(); 68 skipclean = 1; 69 exitonfail = 0; 70 idaddr = 0x0; 71 panic_func = vmsg; 72 while ((ch = getopt(argc, argv, optstring)) != -1) { 73 switch (ch) { 74 case 'b': 75 skipclean = 0; 76 bflag = argtoi('b', "number", optarg, 0); 77 printf("Alternate super block location: %d\n", bflag); 78 break; 79 case 'd': 80 debug++; 81 break; 82 case 'e': 83 exitonfail++; 84 break; 85 case 'f': 86 skipclean = 0; 87 break; 88 case 'i': 89 idaddr = strtol(optarg, NULL, 0); 90 break; 91 case 'm': 92 lfmode = argtoi('m', "mode", optarg, 8); 93 if (lfmode & ~07777) 94 err(1, "bad mode to -m: %o\n", lfmode); 95 printf("** lost+found creation mode %o\n", lfmode); 96 break; 97 98 case 'n': 99 nflag++; 100 yflag = 0; 101 break; 102 103 case 'p': 104 preen++; 105 break; 106 107 case 'P': /* Progress meter not implemented. */ 108 break; 109 110 case 'q': 111 quiet++; 112 break; 113 114 case 'y': 115 yflag++; 116 nflag = 0; 117 break; 118 119 default: 120 usage(); 121 } 122 } 123 124 argc -= optind; 125 argv += optind; 126 127 if (!argc) 128 usage(); 129 130 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 131 (void) signal(SIGINT, catch); 132 if (preen) 133 (void) signal(SIGQUIT, catchquit); 134 135 while (argc-- > 0) 136 (void) checkfilesys(blockcheck(*argv++), 0, 0L, 0); 137 138 if (returntosingle) 139 ret = 2; 140 141 exit(ret); 142 } 143 144 static int 145 argtoi(int flag, const char *req, const char *str, int base) 146 { 147 char *cp; 148 int ret; 149 150 ret = (int) strtol(str, &cp, base); 151 if (cp == str || *cp) 152 err(1, "-%c flag requires a %s\n", flag, req); 153 return (ret); 154 } 155 156 /* 157 * Check the specified filesystem. 158 */ 159 160 /* ARGSUSED */ 161 static int 162 checkfilesys(const char *filesys, char *mntpt, long auxdata, int child) 163 { 164 struct dups *dp; 165 struct zlncnt *zlnp; 166 167 if (preen && child) 168 (void) signal(SIGQUIT, voidquit); 169 setcdevname(filesys, preen); 170 if (debug && preen) 171 pwarn("starting\n"); 172 switch (setup(filesys)) { 173 case 0: 174 if (preen) 175 pfatal("CAN'T CHECK FILE SYSTEM."); 176 case -1: 177 return (0); 178 } 179 180 /* 181 * For LFS, "preen" means "roll forward". We don't check anything 182 * else. 183 */ 184 if (preen == 0) { 185 printf("** Last Mounted on %s\n", fs->lfs_fsmnt); 186 if (hotroot()) 187 printf("** Root file system\n"); 188 /* 189 * 0: check segment checksums, inode ranges 190 */ 191 printf("** Phase 0 - Check Inode Free List\n"); 192 } 193 if (idaddr) 194 pwarn("-i given, skipping free list check\n"); 195 else 196 pass0(); 197 198 if (preen == 0) { 199 /* 200 * 1: scan inodes tallying blocks used 201 */ 202 printf("** Phase 1 - Check Blocks and Sizes\n"); 203 pass1(); 204 205 /* 206 * 2: traverse directories from root to mark all connected directories 207 */ 208 printf("** Phase 2 - Check Pathnames\n"); 209 pass2(); 210 211 /* 212 * 3: scan inodes looking for disconnected directories 213 */ 214 printf("** Phase 3 - Check Connectivity\n"); 215 pass3(); 216 217 /* 218 * 4: scan inodes looking for disconnected files; check reference counts 219 */ 220 printf("** Phase 4 - Check Reference Counts\n"); 221 pass4(); 222 } 223 224 /* 225 * 5: check segment byte totals and dirty flags, and cleanerinfo 226 */ 227 if (!preen) 228 printf("** Phase 5 - Check Segment Block Accounting\n"); 229 pass5(); 230 231 if (debug && !preen) { 232 if (duplist != NULL) { 233 printf("The following duplicate blocks remain:"); 234 for (dp = duplist; dp; dp = dp->next) 235 printf(" %lld,", (long long) dp->dup); 236 printf("\n"); 237 } 238 if (zlnhead != NULL) { 239 printf("The following zero link count inodes remain:"); 240 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 241 printf(" %llu,", 242 (unsigned long long)zlnp->zlncnt); 243 printf("\n"); 244 } 245 } 246 247 if (!rerun) { 248 if (!preen) 249 printf("** Phase 6 - Roll Forward\n"); 250 pass6(); 251 } 252 zlnhead = (struct zlncnt *) 0; 253 duplist = (struct dups *) 0; 254 muldup = (struct dups *) 0; 255 inocleanup(); 256 257 /* 258 * print out summary statistics 259 */ 260 pwarn("%llu files, %lld used, %lld free\n", 261 (unsigned long long)n_files, (long long) n_blks, 262 (long long) fs->lfs_bfree); 263 264 ckfini(1); 265 266 free(blockmap); 267 free(statemap); 268 free((char *)lncntp); 269 if (!fsmodified) { 270 return (0); 271 } 272 if (!preen) 273 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 274 if (rerun) 275 printf("\n***** PLEASE RERUN FSCK *****\n"); 276 if (hotroot()) { 277 struct statvfs stfs_buf; 278 /* 279 * We modified the root. Do a mount update on 280 * it, unless it is read-write, so we can continue. 281 */ 282 if (statvfs("/", &stfs_buf) == 0) { 283 long flags = stfs_buf.f_flag; 284 struct ufs_args args; 285 int ret; 286 287 if (flags & MNT_RDONLY) { 288 args.fspec = 0; 289 flags |= MNT_UPDATE | MNT_RELOAD; 290 ret = mount(MOUNT_LFS, "/", flags, &args); 291 if (ret == 0) 292 return (0); 293 } 294 } 295 if (!preen) 296 printf("\n***** REBOOT NOW *****\n"); 297 sync(); 298 return (4); 299 } 300 return (0); 301 } 302 303 static void 304 usage(void) 305 { 306 307 (void) fprintf(stderr, 308 "usage: %s [-dfpq] [-b block] [-m mode] [-y | -n] filesystem ...\n", 309 getprogname()); 310 exit(1); 311 } 312