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