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