main.c revision 1.27
1/* $NetBSD: main.c,v 1.27 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/* 33 * Copyright (c) 1997 Manuel Bouyer. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. All advertising materials mentioning features or use of this software 44 * must display the following acknowledgement: 45 * This product includes software developed by Manuel Bouyer. 46 * 4. The name of the author may not be used to endorse or promote products 47 * derived from this software without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 52 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 */ 60 61#include <sys/cdefs.h> 62#ifndef lint 63__COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\n\ 64 The Regents of the University of California. All rights reserved.\n"); 65#endif /* not lint */ 66 67#ifndef lint 68#if 0 69static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/23/94"; 70#else 71__RCSID("$NetBSD: main.c,v 1.27 2008/02/23 21:41:48 christos Exp $"); 72#endif 73#endif /* not lint */ 74 75#include <sys/param.h> 76#include <sys/time.h> 77#include <sys/mount.h> 78#include <ufs/ufs/ufsmount.h> 79#include <ufs/ext2fs/ext2fs_dinode.h> 80#include <ufs/ext2fs/ext2fs.h> 81#include <fstab.h> 82#include <stdlib.h> 83#include <string.h> 84#include <ctype.h> 85#include <stdio.h> 86#include <time.h> 87#include <unistd.h> 88#include <signal.h> 89 90#include "fsck.h" 91#include "extern.h" 92#include "fsutil.h" 93#include "exitvalues.h" 94 95int returntosingle = 0; 96 97 98static int argtoi(int, const char *, const char *, int); 99static int checkfilesys(const char *, char *, long, int); 100static void usage(void) __dead; 101 102int 103main(int argc, char *argv[]) 104{ 105 int ch; 106 int ret = FSCK_EXIT_OK; 107 108 sync(); 109 skipclean = 1; 110 while ((ch = getopt(argc, argv, "b:dfm:npqy")) != -1) { 111 switch (ch) { 112 case 'b': 113 skipclean = 0; 114 bflag = argtoi('b', "number", optarg, 10); 115 printf("Alternate super block location: %d\n", bflag); 116 break; 117 118 case 'd': 119 debug++; 120 break; 121 122 case 'f': 123 skipclean = 0; 124 break; 125 126 case 'm': 127 lfmode = argtoi('m', "mode", optarg, 8); 128 if (lfmode &~ 07777) 129 errexit("bad mode to -m: %o\n", lfmode); 130 printf("** lost+found creation mode %o\n", lfmode); 131 break; 132 133 case 'n': 134 nflag++; 135 yflag = 0; 136 break; 137 138 case 'p': 139 preen++; 140 break; 141 142 case 'P': 143 /* Progress meter not implemented. */ 144 break; 145 146 case 'q': /* Quiet not implemented */ 147 break; 148 149 case 'y': 150 yflag++; 151 nflag = 0; 152 break; 153 154 default: 155 usage(); 156 } 157 } 158 159 argc -= optind; 160 argv += optind; 161 162 if (!argc) 163 usage(); 164 165 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 166 (void)signal(SIGINT, catch); 167 if (preen) 168 (void)signal(SIGQUIT, catchquit); 169 170 while (argc-- > 0) { 171 int nret = checkfilesys(blockcheck(*argv++), 0, 0L, 0); 172 if (ret < nret) 173 ret = nret; 174 } 175 176 return returntosingle ? FSCK_EXIT_UNRESOLVED : ret; 177} 178 179static int 180argtoi(int flag, const char *req, const char *str, int base) 181{ 182 char *cp; 183 int ret; 184 185 ret = (int)strtol(str, &cp, base); 186 if (cp == str || *cp) 187 errexit("-%c flag requires a %s\n", flag, req); 188 return (ret); 189} 190 191/* 192 * Check the specified filesystem. 193 */ 194/* ARGSUSED */ 195static int 196checkfilesys(const char *filesys, char *mntpt, long auxdata, int child) 197{ 198 daddr_t n_bfree; 199 struct dups *dp; 200 struct zlncnt *zlnp; 201 int i; 202 203 if (preen && child) 204 (void)signal(SIGQUIT, voidquit); 205 setcdevname(filesys, preen); 206 if (debug && preen) 207 pwarn("starting\n"); 208 switch (setup(filesys)) { 209 case 0: 210 if (preen) 211 pfatal("CAN'T CHECK FILE SYSTEM."); 212 case -1: 213 return FSCK_EXIT_OK; 214 } 215 /* 216 * 1: scan inodes tallying blocks used 217 */ 218 if (preen == 0) { 219 if (sblock.e2fs.e2fs_rev > E2FS_REV0) { 220 printf("** Last Mounted on %s\n", 221 sblock.e2fs.e2fs_fsmnt); 222 } 223 if (hotroot()) 224 printf("** Root file system\n"); 225 printf("** Phase 1 - Check Blocks and Sizes\n"); 226 } 227 pass1(); 228 229 /* 230 * 1b: locate first references to duplicates, if any 231 */ 232 if (duplist) { 233 if (preen) 234 pfatal("INTERNAL ERROR: dups with -p"); 235 printf("** Phase 1b - Rescan For More DUPS\n"); 236 pass1b(); 237 } 238 239 /* 240 * 2: traverse directories from root to mark all connected directories 241 */ 242 if (preen == 0) 243 printf("** Phase 2 - Check Pathnames\n"); 244 pass2(); 245 246 /* 247 * 3: scan inodes looking for disconnected directories 248 */ 249 if (preen == 0) 250 printf("** Phase 3 - Check Connectivity\n"); 251 pass3(); 252 253 /* 254 * 4: scan inodes looking for disconnected files; check reference counts 255 */ 256 if (preen == 0) 257 printf("** Phase 4 - Check Reference Counts\n"); 258 pass4(); 259 260 /* 261 * 5: check and repair resource counts in cylinder groups 262 */ 263 if (preen == 0) 264 printf("** Phase 5 - Check Cyl groups\n"); 265 pass5(); 266 267 /* 268 * print out summary statistics 269 */ 270 n_bfree = sblock.e2fs.e2fs_fbcount; 271 272 pwarn("%lld files, %lld used, %lld free\n", 273 (long long)n_files, (long long)n_blks, (long long)n_bfree); 274 if (debug && 275 /* 9 reserved and unused inodes in FS */ 276 (n_files -= maxino - 9 - sblock.e2fs.e2fs_ficount)) 277 printf("%lld files missing\n", (long long)n_files); 278 if (debug) { 279 for (i = 0; i < sblock.e2fs_ncg; i++) 280 n_blks += cgoverhead(i); 281 n_blks += sblock.e2fs.e2fs_first_dblock; 282 if (n_blks -= maxfsblock - n_bfree) 283 printf("%lld blocks missing\n", (long long)n_blks); 284 if (duplist != NULL) { 285 printf("The following duplicate blocks remain:"); 286 for (dp = duplist; dp; dp = dp->next) 287 printf(" %lld,", (long long)dp->dup); 288 printf("\n"); 289 } 290 if (zlnhead != NULL) { 291 printf("The following zero link count inodes remain:"); 292 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 293 printf(" %llu,", 294 (unsigned long long)zlnp->zlncnt); 295 printf("\n"); 296 } 297 } 298 zlnhead = (struct zlncnt *)0; 299 duplist = (struct dups *)0; 300 muldup = (struct dups *)0; 301 inocleanup(); 302 if (fsmodified) { 303 time_t t; 304 (void)time(&t); 305 sblock.e2fs.e2fs_wtime = t; 306 sblock.e2fs.e2fs_lastfsck = t; 307 sbdirty(); 308 } 309 ckfini(1); 310 free(blockmap); 311 free(statemap); 312 free((char *)lncntp); 313 if (!fsmodified) 314 return FSCK_EXIT_OK; 315 if (!preen) 316 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 317 if (rerun) 318 printf("\n***** PLEASE RERUN FSCK *****\n"); 319 if (hotroot()) { 320 struct statvfs stfs_buf; 321 /* 322 * We modified the root. Do a mount update on 323 * it, unless it is read-write, so we can continue. 324 */ 325 if (statvfs("/", &stfs_buf) == 0) { 326 long flags = stfs_buf.f_flag; 327 struct ufs_args args; 328 329 if (flags & MNT_RDONLY) { 330 args.fspec = 0; 331 flags |= MNT_UPDATE | MNT_RELOAD; 332 if (mount(MOUNT_EXT2FS, "/", flags, 333 &args, sizeof args) == 0) 334 return FSCK_EXIT_OK; 335 } 336 } 337 if (!preen) 338 printf("\n***** REBOOT NOW *****\n"); 339 sync(); 340 return FSCK_EXIT_ROOT_CHANGED; 341 } 342 return FSCK_EXIT_OK; 343} 344 345static void 346usage(void) 347{ 348 349 (void) fprintf(stderr, 350 "Usage: %s [-dfnpy] [-b block] [-c level] [-m mode] filesystem ...\n", 351 getprogname()); 352 exit(FSCK_EXIT_USAGE); 353} 354 355