1 /* $OpenBSD: main.c,v 1.53 2021/01/27 05:03:25 deraadt Exp $ */ 2 /* $NetBSD: main.c,v 1.22 1996/10/11 20:15:48 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1980, 1986, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/time.h> 34 #include <sys/signal.h> 35 #include <sys/mount.h> 36 #include <ufs/ufs/dinode.h> 37 #include <ufs/ffs/fs.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <ctype.h> 41 #include <stdio.h> 42 #include <unistd.h> 43 #include <err.h> 44 45 #include "fsck.h" 46 #include "extern.h" 47 #include "fsutil.h" 48 49 volatile sig_atomic_t returntosingle; 50 51 long long argtoi(int, char *, char *, int); 52 int checkfilesys(char *, char *, long, int); 53 int main(int, char *[]); 54 55 extern char *__progname; 56 57 struct inostatlist *inostathead; 58 59 struct bufarea bufhead; /* head of list of other blks in filesys */ 60 struct bufarea sblk; /* file system superblock */ 61 struct bufarea asblk; /* alternate file system superblock */ 62 struct bufarea *pdirbp; /* current directory contents */ 63 struct bufarea *pbp; /* current inode block */ 64 65 struct dups *duplist; /* head of dup list */ 66 struct dups *muldup; /* end of unique duplicate dup block numbers */ 67 68 struct zlncnt *zlnhead; /* head of zero link count list */ 69 70 struct inoinfo **inphead, **inpsort; 71 72 extern long numdirs, listmax, inplast; 73 74 long secsize; /* actual disk sector size */ 75 char nflag; /* assume a no response */ 76 char yflag; /* assume a yes response */ 77 daddr_t bflag; /* location of alternate super block */ 78 int debug; /* output debugging info */ 79 int cvtlevel; /* convert to newer file system format */ 80 char usedsoftdep; /* just fix soft dependency inconsistencies */ 81 int preen; /* just fix normal inconsistencies */ 82 char resolved; /* cleared if unresolved changes => not clean */ 83 char havesb; /* superblock has been read */ 84 char skipclean; /* skip clean file systems if preening */ 85 int fsmodified; /* 1 => write done to file system */ 86 int fsreadfd; /* file descriptor for reading file system */ 87 int fswritefd; /* file descriptor for writing file system */ 88 int rerun; /* rerun fsck. Only used in non-preen mode */ 89 90 daddr_t maxfsblock; /* number of blocks in the file system */ 91 char *blockmap; /* ptr to primary blk allocation map */ 92 ino_t maxino; /* number of inodes in file system */ 93 ino_t lastino; /* last inode in use */ 94 95 ino_t lfdir; /* lost & found directory inode number */ 96 97 daddr_t n_blks; /* number of blocks in use */ 98 int64_t n_files; /* number of files in use */ 99 100 struct ufs1_dinode ufs1_zino; 101 struct ufs2_dinode ufs2_zino; 102 103 void 104 usage(void) 105 { 106 fprintf(stderr, "usage: %s [-fnpy] [-b block#] [-c level] " 107 "[-m mode] filesystem\n", __progname); 108 exit(1); 109 } 110 int 111 main(int argc, char *argv[]) 112 { 113 int ch; 114 int ret = 0; 115 116 checkroot(); 117 118 sync(); 119 skipclean = 1; 120 while ((ch = getopt(argc, argv, "dfpnNyYb:c:m:")) != -1) { 121 switch (ch) { 122 case 'p': 123 preen = 1; 124 break; 125 126 case 'b': 127 skipclean = 0; 128 bflag = argtoi('b', "number", optarg, 10); 129 printf("Alternate super block location: %lld\n", 130 (long long)bflag); 131 break; 132 133 case 'c': 134 skipclean = 0; 135 cvtlevel = argtoi('c', "conversion level", optarg, 10); 136 if (cvtlevel < 3) 137 errexit("cannot do level %d conversion\n", 138 cvtlevel); 139 break; 140 141 case 'd': 142 debug = 1; 143 break; 144 145 case 'f': 146 skipclean = 0; 147 break; 148 149 case 'm': 150 lfmode = argtoi('m', "mode", optarg, 8); 151 if (lfmode &~ 07777) 152 errexit("bad mode to -m: %o\n", lfmode); 153 printf("** lost+found creation mode %o\n", lfmode); 154 break; 155 156 case 'n': 157 case 'N': 158 nflag = 1; 159 yflag = 0; 160 break; 161 162 case 'y': 163 case 'Y': 164 yflag = 1; 165 nflag = 0; 166 break; 167 168 default: 169 usage(); 170 } 171 } 172 argc -= optind; 173 argv += optind; 174 175 if (argc != 1) 176 usage(); 177 178 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 179 (void)signal(SIGINT, catch); 180 if (preen) 181 (void)signal(SIGQUIT, catchquit); 182 catchinfo(0); 183 184 (void)checkfilesys(blockcheck(*argv), 0, 0L, 0); 185 186 if (returntosingle) 187 ret = 2; 188 189 exit(ret); 190 } 191 192 long long 193 argtoi(int flag, char *req, char *str, int base) 194 { 195 char *cp; 196 long long ret; 197 198 ret = strtoll(str, &cp, base); 199 if (cp == str || *cp) 200 errexit("-%c flag requires a %s\n", flag, req); 201 return (ret); 202 } 203 204 /* 205 * Check the specified filesystem. 206 */ 207 /* ARGSUSED */ 208 int 209 checkfilesys(char *filesys, char *mntpt, long auxdata, int child) 210 { 211 daddr_t n_ffree, n_bfree; 212 struct dups *dp; 213 struct zlncnt *zlnp; 214 int cylno; 215 216 if (preen && child) 217 (void)signal(SIGQUIT, voidquit); 218 setcdevname(filesys, NULL, preen); 219 if (debug && preen) 220 pwarn("starting\n"); 221 222 switch (setup(filesys, 0)) { 223 case 0: 224 if (preen) 225 pfatal("CAN'T CHECK FILE SYSTEM."); 226 /* FALLTHROUGH */ 227 case -1: 228 if (fsreadfd != -1) { 229 (void)close(fsreadfd); 230 fsreadfd = -1; 231 } 232 if (fswritefd != -1) { 233 (void)close(fswritefd); 234 fswritefd = -1; 235 } 236 return (0); 237 } 238 info_filesys = filesys; 239 240 /* 241 * Cleared if any questions answered no. Used to decide if 242 * the superblock should be marked clean. 243 */ 244 resolved = 1; 245 246 /* 247 * 1: scan inodes tallying blocks used 248 */ 249 if (preen == 0) { 250 printf("** Last Mounted on %s\n", sblock.fs_fsmnt); 251 if (hotroot()) 252 printf("** Root file system\n"); 253 printf("** Phase 1 - Check Blocks and Sizes\n"); 254 } 255 pass1(); 256 257 /* 258 * 1b: locate first references to duplicates, if any 259 */ 260 if (duplist) { 261 if (preen || usedsoftdep) 262 pfatal("INTERNAL ERROR: dups with -p"); 263 printf("** Phase 1b - Rescan For More DUPS\n"); 264 pass1b(); 265 } 266 267 /* 268 * 2: traverse directories from root to mark all connected directories 269 */ 270 if (preen == 0) 271 printf("** Phase 2 - Check Pathnames\n"); 272 pass2(); 273 274 /* 275 * 3: scan inodes looking for disconnected directories 276 */ 277 if (preen == 0) 278 printf("** Phase 3 - Check Connectivity\n"); 279 pass3(); 280 281 /* 282 * 4: scan inodes looking for disconnected files; check reference counts 283 */ 284 if (preen == 0) 285 printf("** Phase 4 - Check Reference Counts\n"); 286 pass4(); 287 288 /* 289 * 5: check and repair resource counts in cylinder groups 290 */ 291 if (preen == 0) 292 printf("** Phase 5 - Check Cyl groups\n"); 293 pass5(); 294 295 /* 296 * print out summary statistics 297 */ 298 n_ffree = sblock.fs_cstotal.cs_nffree; 299 n_bfree = sblock.fs_cstotal.cs_nbfree; 300 pwarn("%lld files, %lld used, %lld free ", 301 n_files, (long long)n_blks, 302 (long long)(n_ffree + sblock.fs_frag * n_bfree)); 303 printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n", 304 (long long)n_ffree, (long long)n_bfree, 305 (long long)((n_ffree * 100) / sblock.fs_dsize), 306 (long long)(((n_ffree * 1000 + sblock.fs_dsize / 2) / 307 sblock.fs_dsize) % 10)); 308 if (debug && 309 (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree)) 310 printf("%lld files missing\n", n_files); 311 if (debug) { 312 n_blks += sblock.fs_ncg * 313 (cgdmin(&sblock, 0) - cgsblock(&sblock, 0)); 314 n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0); 315 n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize); 316 if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree)) 317 printf("%lld blocks missing\n", (long long)n_blks); 318 if (duplist != NULL) { 319 printf("The following duplicate blocks remain:"); 320 for (dp = duplist; dp; dp = dp->next) 321 printf(" %lld,", (long long)dp->dup); 322 printf("\n"); 323 } 324 if (zlnhead != NULL) { 325 printf("The following zero link count inodes remain:"); 326 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 327 printf(" %llu,", 328 (unsigned long long)zlnp->zlncnt); 329 printf("\n"); 330 } 331 } 332 zlnhead = NULL; 333 duplist = NULL; 334 muldup = NULL; 335 inocleanup(); 336 if (fsmodified) { 337 sblock.fs_time = (time_t)time(NULL); 338 sbdirty(); 339 } 340 if (cvtlevel && sblk.b_dirty) { 341 /* 342 * Write out the duplicate super blocks 343 */ 344 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 345 bwrite(fswritefd, (char *)&sblock, 346 fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE); 347 } 348 if (rerun) 349 resolved = 0; 350 ckfini(resolved); /* Don't mark fs clean if fsck needs to be re-run */ 351 352 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 353 free(inostathead[cylno].il_stat); 354 free(inostathead); 355 inostathead = NULL; 356 357 free(blockmap); 358 blockmap = NULL; 359 free(sblock.fs_csp); 360 free(sblk.b_un.b_buf); 361 free(asblk.b_un.b_buf); 362 363 if (!fsmodified) 364 return (0); 365 if (!preen) 366 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 367 if (rerun || !resolved) 368 printf("\n***** PLEASE RERUN FSCK *****\n"); 369 if (hotroot()) { 370 struct statfs stfs_buf; 371 /* 372 * We modified the root. Do a mount update on 373 * it, unless it is read-write, so we can continue. 374 */ 375 if (statfs("/", &stfs_buf) == 0) { 376 long flags = stfs_buf.f_flags; 377 struct ufs_args args; 378 int ret; 379 380 if (flags & MNT_RDONLY) { 381 args.fspec = 0; 382 args.export_info.ex_flags = 0; 383 args.export_info.ex_root = 0; 384 flags |= MNT_UPDATE | MNT_RELOAD; 385 ret = mount(MOUNT_FFS, "/", flags, &args); 386 if (ret == 0) 387 return(0); 388 } 389 } 390 if (!preen) 391 printf("\n***** REBOOT NOW *****\n"); 392 sync(); 393 return (4); 394 } 395 return (0); 396 } 397