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