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