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