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