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