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