1 /* $OpenBSD: main.c,v 1.55 2024/02/03 18:51:57 beck 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 int preen; /* just fix normal inconsistencies */ 81 char resolved; /* cleared if unresolved changes => not clean */ 82 char havesb; /* superblock has been read */ 83 char skipclean; /* skip clean file systems if preening */ 84 int fsmodified; /* 1 => write done to file system */ 85 int fsreadfd; /* file descriptor for reading file system */ 86 int fswritefd; /* file descriptor for writing file system */ 87 int rerun; /* rerun fsck. Only used in non-preen mode */ 88 89 daddr_t maxfsblock; /* number of blocks in the file system */ 90 char *blockmap; /* ptr to primary blk allocation map */ 91 ino_t maxino; /* number of inodes in file system */ 92 ino_t lastino; /* last inode in use */ 93 94 ino_t lfdir; /* lost & found directory inode number */ 95 96 daddr_t n_blks; /* number of blocks in use */ 97 int64_t n_files; /* number of files in use */ 98 99 struct ufs1_dinode ufs1_zino; 100 struct ufs2_dinode ufs2_zino; 101 102 void 103 usage(void) 104 { 105 fprintf(stderr, "usage: %s [-fnpy] [-b block#] [-c level] " 106 "[-m mode] filesystem\n", __progname); 107 exit(1); 108 } 109 int 110 main(int argc, char *argv[]) 111 { 112 int ch; 113 int ret = 0; 114 115 checkroot(); 116 117 sync(); 118 skipclean = 1; 119 while ((ch = getopt(argc, argv, "dfpnNyYb:c:m:")) != -1) { 120 switch (ch) { 121 case 'p': 122 preen = 1; 123 break; 124 125 case 'b': 126 skipclean = 0; 127 bflag = argtoi('b', "number", optarg, 10); 128 printf("Alternate super block location: %lld\n", 129 (long long)bflag); 130 break; 131 132 case 'c': 133 skipclean = 0; 134 cvtlevel = argtoi('c', "conversion level", optarg, 10); 135 if (cvtlevel < 3) 136 errexit("cannot do level %d conversion\n", 137 cvtlevel); 138 break; 139 140 case 'd': 141 debug = 1; 142 break; 143 144 case 'f': 145 skipclean = 0; 146 break; 147 148 case 'm': 149 lfmode = argtoi('m', "mode", optarg, 8); 150 if (lfmode &~ 07777) 151 errexit("bad mode to -m: %o\n", lfmode); 152 printf("** lost+found creation mode %o\n", lfmode); 153 break; 154 155 case 'n': 156 case 'N': 157 nflag = 1; 158 yflag = 0; 159 break; 160 161 case 'y': 162 case 'Y': 163 yflag = 1; 164 nflag = 0; 165 break; 166 167 default: 168 usage(); 169 } 170 } 171 argc -= optind; 172 argv += optind; 173 174 if (argc != 1) 175 usage(); 176 177 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 178 (void)signal(SIGINT, catch); 179 if (preen) 180 (void)signal(SIGQUIT, catchquit); 181 catchinfo(0); 182 183 (void)checkfilesys(blockcheck(*argv), 0, 0L, 0); 184 185 if (returntosingle) 186 ret = 2; 187 188 exit(ret); 189 } 190 191 long long 192 argtoi(int flag, char *req, char *str, int base) 193 { 194 char *cp; 195 long long ret; 196 197 ret = strtoll(str, &cp, base); 198 if (cp == str || *cp) 199 errexit("-%c flag requires a %s\n", flag, req); 200 return (ret); 201 } 202 203 /* 204 * Check the specified filesystem. 205 */ 206 int 207 checkfilesys(char *filesys, char *mntpt, long auxdata, int child) 208 { 209 daddr_t n_ffree, n_bfree; 210 struct dups *dp; 211 struct zlncnt *zlnp; 212 int cylno; 213 214 if (preen && child) 215 (void)signal(SIGQUIT, voidquit); 216 setcdevname(filesys, NULL, preen); 217 if (debug && preen) 218 pwarn("starting\n"); 219 220 switch (setup(filesys, 0)) { 221 case 0: 222 if (preen) 223 pfatal("CAN'T CHECK FILE SYSTEM."); 224 /* FALLTHROUGH */ 225 case -1: 226 if (fsreadfd != -1) { 227 (void)close(fsreadfd); 228 fsreadfd = -1; 229 } 230 if (fswritefd != -1) { 231 (void)close(fswritefd); 232 fswritefd = -1; 233 } 234 return (0); 235 } 236 info_filesys = filesys; 237 238 /* 239 * Cleared if any questions answered no. Used to decide if 240 * the superblock should be marked clean. 241 */ 242 resolved = 1; 243 244 /* 245 * 1: scan inodes tallying blocks used 246 */ 247 if (preen == 0) { 248 printf("** Last Mounted on %s\n", sblock.fs_fsmnt); 249 if (hotroot()) 250 printf("** Root file system\n"); 251 printf("** Phase 1 - Check Blocks and Sizes\n"); 252 } 253 pass1(); 254 255 /* 256 * 1b: locate first references to duplicates, if any 257 */ 258 if (duplist) { 259 if (preen) 260 pfatal("INTERNAL ERROR: dups with -p"); 261 printf("** Phase 1b - Rescan For More DUPS\n"); 262 pass1b(); 263 } 264 265 /* 266 * 2: traverse directories from root to mark all connected directories 267 */ 268 if (preen == 0) 269 printf("** Phase 2 - Check Pathnames\n"); 270 pass2(); 271 272 /* 273 * 3: scan inodes looking for disconnected directories 274 */ 275 if (preen == 0) 276 printf("** Phase 3 - Check Connectivity\n"); 277 pass3(); 278 279 /* 280 * 4: scan inodes looking for disconnected files; check reference counts 281 */ 282 if (preen == 0) 283 printf("** Phase 4 - Check Reference Counts\n"); 284 pass4(); 285 286 /* 287 * 5: check and repair resource counts in cylinder groups 288 */ 289 if (preen == 0) 290 printf("** Phase 5 - Check Cyl groups\n"); 291 pass5(); 292 293 /* 294 * print out summary statistics 295 */ 296 n_ffree = sblock.fs_cstotal.cs_nffree; 297 n_bfree = sblock.fs_cstotal.cs_nbfree; 298 pwarn("%lld files, %lld used, %lld free ", 299 n_files, (long long)n_blks, 300 (long long)(n_ffree + sblock.fs_frag * n_bfree)); 301 printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n", 302 (long long)n_ffree, (long long)n_bfree, 303 (long long)((n_ffree * 100) / sblock.fs_dsize), 304 (long long)(((n_ffree * 1000 + sblock.fs_dsize / 2) / 305 sblock.fs_dsize) % 10)); 306 if (debug && 307 (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree)) 308 printf("%lld files missing\n", n_files); 309 if (debug) { 310 n_blks += sblock.fs_ncg * 311 (cgdmin(&sblock, 0) - cgsblock(&sblock, 0)); 312 n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0); 313 n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize); 314 if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree)) 315 printf("%lld blocks missing\n", (long long)n_blks); 316 if (duplist != NULL) { 317 printf("The following duplicate blocks remain:"); 318 for (dp = duplist; dp; dp = dp->next) 319 printf(" %lld,", (long long)dp->dup); 320 printf("\n"); 321 } 322 if (zlnhead != NULL) { 323 printf("The following zero link count inodes remain:"); 324 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 325 printf(" %llu,", 326 (unsigned long long)zlnp->zlncnt); 327 printf("\n"); 328 } 329 } 330 zlnhead = NULL; 331 duplist = NULL; 332 muldup = NULL; 333 inocleanup(); 334 if (fsmodified) { 335 sblock.fs_time = (time_t)time(NULL); 336 sbdirty(); 337 } 338 if (cvtlevel && sblk.b_dirty) { 339 /* 340 * Write out the duplicate super blocks 341 */ 342 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 343 bwrite(fswritefd, (char *)&sblock, 344 fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE); 345 } 346 if (rerun) 347 resolved = 0; 348 ckfini(resolved); /* Don't mark fs clean if fsck needs to be re-run */ 349 350 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 351 free(inostathead[cylno].il_stat); 352 free(inostathead); 353 inostathead = NULL; 354 355 free(blockmap); 356 blockmap = NULL; 357 free(sblock.fs_csp); 358 free(sblk.b_un.b_buf); 359 free(asblk.b_un.b_buf); 360 361 if (!fsmodified) 362 return (0); 363 if (!preen) 364 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 365 if (rerun || !resolved) 366 printf("\n***** PLEASE RERUN FSCK *****\n"); 367 if (hotroot()) { 368 struct statfs stfs_buf; 369 /* 370 * We modified the root. Do a mount update on 371 * it, unless it is read-write, so we can continue. 372 */ 373 if (statfs("/", &stfs_buf) == 0) { 374 long flags = stfs_buf.f_flags; 375 struct ufs_args args; 376 int ret; 377 378 if (flags & MNT_RDONLY) { 379 args.fspec = 0; 380 args.export_info.ex_flags = 0; 381 args.export_info.ex_root = 0; 382 flags |= MNT_UPDATE | MNT_RELOAD; 383 ret = mount(MOUNT_FFS, "/", flags, &args); 384 if (ret == 0) 385 return(0); 386 } 387 } 388 if (!preen) 389 printf("\n***** REBOOT NOW *****\n"); 390 sync(); 391 return (4); 392 } 393 return (0); 394 } 395