1 /* $NetBSD: main.c,v 1.36 2007/07/16 17:06:52 pooka 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 <ufs/ufs/ufsmount.h> 37 #include <ufs/lfs/lfs.h> 38 39 #include <fstab.h> 40 #include <stdarg.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <ctype.h> 44 #include <stdio.h> 45 #include <unistd.h> 46 #include <err.h> 47 #include <util.h> 48 #include <signal.h> 49 50 #include "fsck.h" 51 #include "extern.h" 52 #include "fsutil.h" 53 54 int returntosingle; 55 56 static int argtoi(int, const char *, const char *, int); 57 static int checkfilesys(const char *, char *, long, int); 58 static void usage(void); 59 static void efun(int, const char *, ...); 60 extern void (*panic_func)(int, const char *, va_list); 61 62 static void 63 efun(int eval, const char *fmt, ...) 64 { 65 va_list ap; 66 va_start(ap, fmt); 67 verr(EEXIT, fmt, ap); 68 va_end(ap); 69 } 70 71 int 72 main(int argc, char **argv) 73 { 74 int ch; 75 int ret = 0; 76 const char *optstring = "b:dfi:m:npPqy"; 77 78 skipclean = 1; 79 exitonfail = 0; 80 idaddr = 0x0; 81 panic_func = vmsg; 82 esetfunc(efun); 83 while ((ch = getopt(argc, argv, optstring)) != -1) { 84 switch (ch) { 85 case 'b': 86 skipclean = 0; 87 bflag = argtoi('b', "number", optarg, 0); 88 printf("Alternate super block location: %d\n", bflag); 89 break; 90 case 'd': 91 debug++; 92 break; 93 case 'e': 94 exitonfail++; 95 break; 96 case 'f': 97 skipclean = 0; 98 break; 99 case 'i': 100 idaddr = strtol(optarg, NULL, 0); 101 break; 102 case 'm': 103 lfmode = argtoi('m', "mode", optarg, 8); 104 if (lfmode & ~07777) 105 err(1, "bad mode to -m: %o\n", lfmode); 106 printf("** lost+found creation mode %o\n", lfmode); 107 break; 108 109 case 'n': 110 nflag++; 111 yflag = 0; 112 break; 113 114 case 'p': 115 preen++; 116 break; 117 118 case 'P': /* Progress meter not implemented. */ 119 break; 120 121 case 'q': 122 quiet++; 123 break; 124 125 case 'y': 126 yflag++; 127 nflag = 0; 128 break; 129 130 default: 131 usage(); 132 } 133 } 134 135 argc -= optind; 136 argv += optind; 137 138 if (!argc) 139 usage(); 140 141 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 142 (void) signal(SIGINT, catch); 143 if (preen) 144 (void) signal(SIGQUIT, catchquit); 145 146 while (argc-- > 0) 147 (void) checkfilesys(blockcheck(*argv++), 0, 0L, 0); 148 149 if (returntosingle) 150 ret = 2; 151 152 exit(ret); 153 } 154 155 static int 156 argtoi(int flag, const char *req, const char *str, int base) 157 { 158 char *cp; 159 int ret; 160 161 ret = (int) strtol(str, &cp, base); 162 if (cp == str || *cp) 163 err(1, "-%c flag requires a %s\n", flag, req); 164 return (ret); 165 } 166 167 /* 168 * Check the specified filesystem. 169 */ 170 171 /* ARGSUSED */ 172 static int 173 checkfilesys(const char *filesys, char *mntpt, long auxdata, int child) 174 { 175 struct dups *dp; 176 struct zlncnt *zlnp; 177 178 if (preen && child) 179 (void) signal(SIGQUIT, voidquit); 180 setcdevname(filesys, preen); 181 if (debug && preen) 182 pwarn("starting\n"); 183 switch (setup(filesys)) { 184 case 0: 185 if (preen) 186 pfatal("CAN'T CHECK FILE SYSTEM."); 187 case -1: 188 return (0); 189 } 190 191 /* 192 * For LFS, "preen" means "roll forward". We don't check anything 193 * else. 194 */ 195 if (preen == 0) { 196 printf("** Last Mounted on %s\n", fs->lfs_fsmnt); 197 if (hotroot()) 198 printf("** Root file system\n"); 199 /* 200 * 0: check segment checksums, inode ranges 201 */ 202 printf("** Phase 0 - Check Inode Free List\n"); 203 } 204 205 /* 206 * Check inode free list - we do this even if idaddr is set, 207 * since if we're writing we don't want to write a bad list. 208 */ 209 pass0(); 210 211 if (preen == 0) { 212 /* 213 * 1: scan inodes tallying blocks used 214 */ 215 printf("** Phase 1 - Check Blocks and Sizes\n"); 216 pass1(); 217 218 /* 219 * 2: traverse directories from root to mark all connected directories 220 */ 221 printf("** Phase 2 - Check Pathnames\n"); 222 pass2(); 223 224 /* 225 * 3: scan inodes looking for disconnected directories 226 */ 227 printf("** Phase 3 - Check Connectivity\n"); 228 pass3(); 229 230 /* 231 * 4: scan inodes looking for disconnected files; check reference counts 232 */ 233 printf("** Phase 4 - Check Reference Counts\n"); 234 pass4(); 235 } 236 237 /* 238 * 5: check segment byte totals and dirty flags, and cleanerinfo 239 */ 240 if (!preen) 241 printf("** Phase 5 - Check Segment Block Accounting\n"); 242 pass5(); 243 244 if (debug && !preen) { 245 if (duplist != NULL) { 246 printf("The following duplicate blocks remain:"); 247 for (dp = duplist; dp; dp = dp->next) 248 printf(" %lld,", (long long) dp->dup); 249 printf("\n"); 250 } 251 if (zlnhead != NULL) { 252 printf("The following zero link count inodes remain:"); 253 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 254 printf(" %llu,", 255 (unsigned long long)zlnp->zlncnt); 256 printf("\n"); 257 } 258 } 259 260 if (!rerun) { 261 if (!preen) { 262 if (reply("ROLL FILESYSTEM FORWARD") == 1) { 263 printf("** Phase 6 - Roll Forward\n"); 264 pass6(); 265 } 266 } 267 else { 268 pass6(); 269 } 270 } 271 zlnhead = (struct zlncnt *) 0; 272 orphead = (struct zlncnt *) 0; 273 duplist = (struct dups *) 0; 274 muldup = (struct dups *) 0; 275 inocleanup(); 276 277 /* 278 * print out summary statistics 279 */ 280 pwarn("%llu files, %lld used, %lld free\n", 281 (unsigned long long)n_files, (long long) n_blks, 282 (long long) fs->lfs_bfree); 283 284 ckfini(1); 285 286 free(blockmap); 287 free(statemap); 288 free((char *)lncntp); 289 if (!fsmodified) { 290 return (0); 291 } 292 if (!preen) 293 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 294 if (rerun) 295 printf("\n***** PLEASE RERUN FSCK *****\n"); 296 if (hotroot()) { 297 struct statvfs stfs_buf; 298 /* 299 * We modified the root. Do a mount update on 300 * it, unless it is read-write, so we can continue. 301 */ 302 if (statvfs("/", &stfs_buf) == 0) { 303 long flags = stfs_buf.f_flag; 304 struct ufs_args args; 305 int ret; 306 307 if (flags & MNT_RDONLY) { 308 args.fspec = 0; 309 flags |= MNT_UPDATE | MNT_RELOAD; 310 ret = mount(MOUNT_LFS, "/", flags, 311 &args, sizeof args); 312 if (ret == 0) 313 return (0); 314 } 315 } 316 if (!preen) 317 printf("\n***** REBOOT NOW *****\n"); 318 sync(); 319 return (4); 320 } 321 return (0); 322 } 323 324 static void 325 usage(void) 326 { 327 328 (void) fprintf(stderr, 329 "usage: %s [-dfpq] [-b block] [-m mode] [-y | -n] filesystem ...\n", 330 getprogname()); 331 exit(1); 332 } 333