1 /* $NetBSD: main.c,v 1.10 2001/08/18 17:10:06 ad Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1986, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/param.h> 37 #include <sys/time.h> 38 #include <sys/mount.h> 39 #include <ufs/ufs/dinode.h> 40 #include <sys/mount.h> 41 #include <ufs/ufs/ufsmount.h> 42 #include <ufs/lfs/lfs.h> 43 #include <fstab.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <ctype.h> 47 #include <stdio.h> 48 #include <unistd.h> 49 #include <err.h> 50 51 #include "fsck.h" 52 #include "extern.h" 53 #include "fsutil.h" 54 55 int returntosingle; 56 int fake_cleanseg = -1; 57 58 int main(int, char *[]); 59 60 static int argtoi(int, char *, char *, int); 61 static int checkfilesys(const char *, char *, long, int); 62 #if 0 63 static int docheck(struct fstab *); 64 #endif 65 static void usage(void); 66 67 int 68 main(int argc, char **argv) 69 { 70 int ch; 71 int ret = 0; 72 char *optstring = "b:C:dfi:m:npy"; 73 74 sync(); 75 skipclean = 1; 76 exitonfail = 0; 77 idaddr = 0x0; 78 while ((ch = getopt(argc, argv, optstring)) != -1) { 79 switch (ch) { 80 case 'b': 81 skipclean = 0; 82 bflag = argtoi('b', "number", optarg, 10); 83 printf("Alternate super block location: %d\n", bflag); 84 break; 85 case 'C': 86 fake_cleanseg = atoi(optarg); 87 break; 88 case 'd': 89 debug++; 90 break; 91 case 'e': 92 exitonfail++; 93 break; 94 case 'f': 95 skipclean = 0; 96 break; 97 case 'i': 98 idaddr = strtol(optarg, NULL, 0); 99 break; 100 case 'm': 101 lfmode = argtoi('m', "mode", optarg, 8); 102 if (lfmode & ~07777) 103 errexit("bad mode to -m: %o\n", lfmode); 104 printf("** lost+found creation mode %o\n", lfmode); 105 break; 106 107 case 'n': 108 nflag++; 109 yflag = 0; 110 break; 111 112 case 'p': 113 preen++; 114 break; 115 116 case 'y': 117 yflag++; 118 nflag = 0; 119 break; 120 121 default: 122 usage(); 123 } 124 } 125 #if 0 126 if (nflag == 0) { 127 errexit("fsck_lfs cannot write to the filesystem yet; the -n flag is required.\n"); 128 } 129 #endif 130 131 argc -= optind; 132 argv += optind; 133 134 if (!argc) 135 usage(); 136 137 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 138 (void)signal(SIGINT, catch); 139 if (preen) 140 (void)signal(SIGQUIT, catchquit); 141 142 while (argc-- > 0) 143 (void)checkfilesys(blockcheck(*argv++), 0, 0L, 0); 144 145 if (returntosingle) 146 ret = 2; 147 148 exit(ret); 149 } 150 151 static int 152 argtoi(int flag, char *req, char *str, int base) 153 { 154 char *cp; 155 int ret; 156 157 ret = (int)strtol(str, &cp, base); 158 if (cp == str || *cp) 159 errexit("-%c flag requires a %s\n", flag, req); 160 return (ret); 161 } 162 163 #if 0 164 /* 165 * Determine whether a filesystem should be checked. 166 */ 167 static int 168 docheck(struct fstab * fsp) 169 { 170 171 if ((strcmp(fsp->fs_vfstype, "ufs") && 172 strcmp(fsp->fs_vfstype, "ffs")) || 173 (strcmp(fsp->fs_type, FSTAB_RW) && 174 strcmp(fsp->fs_type, FSTAB_RO)) || 175 fsp->fs_passno == 0) 176 return (0); 177 return (1); 178 } 179 #endif 180 181 /* 182 * Check the specified filesystem. 183 */ 184 /* ARGSUSED */ 185 static int 186 checkfilesys(const char *filesys, char *mntpt, long auxdata, int child) 187 { 188 daddr_t n_ffree = 0, n_bfree = 0; 189 struct dups *dp; 190 struct zlncnt *zlnp; 191 192 if (preen && child) 193 (void)signal(SIGQUIT, voidquit); 194 setcdevname(filesys, preen); 195 if (debug && preen) 196 pwarn("starting\n"); 197 switch (setup(filesys)) { 198 case 0: 199 if (preen) 200 pfatal("CAN'T CHECK FILE SYSTEM."); 201 case -1: 202 return (0); 203 } 204 205 if (preen == 0) { 206 printf("** Last Mounted on %s\n", sblock.lfs_fsmnt); 207 if (hotroot()) 208 printf("** Root file system\n"); 209 } 210 /* 211 * 0: check segment checksums, inode ranges 212 */ 213 if (preen == 0) 214 printf("** Phase 0 - Check Segment Summaries and Inode Free List\n"); 215 if (idaddr != sblock.lfs_idaddr) 216 pwarn("-i given, skipping free list check\n"); 217 else 218 pass0(); 219 220 if (preen == 0) { 221 /* 222 * 1: scan inodes tallying blocks used 223 */ 224 if (preen == 0) 225 printf("** Phase 1 - Check Blocks and Sizes\n"); 226 pass1(); 227 228 /* 229 * 2: traverse directories from root to mark all connected directories 230 */ 231 if (preen == 0) 232 printf("** Phase 2 - Check Pathnames\n"); 233 pass2(); 234 235 /* 236 * 3: scan inodes looking for disconnected directories 237 */ 238 if (preen == 0) 239 printf("** Phase 3 - Check Connectivity\n"); 240 pass3(); 241 242 /* 243 * 4: scan inodes looking for disconnected files; check reference counts 244 */ 245 if (preen == 0) 246 printf("** Phase 4 - Check Reference Counts\n"); 247 pass4(); 248 249 250 /* 251 * 5: check segment byte totals and dirty flags 252 */ 253 if (preen == 0) 254 printf("** Phase 5 - Check Segment Block Accounting\n"); 255 pass5(); 256 257 /* 258 * print out summary statistics 259 */ 260 pwarn("%d files, %d used, %d free ", 261 n_files, n_blks, n_ffree + sblock.lfs_frag * n_bfree); 262 putchar('\n'); 263 } 264 if (debug) { 265 if (duplist != NULL) { 266 printf("The following duplicate blocks remain:"); 267 for (dp = duplist; dp; dp = dp->next) 268 printf(" %d,", dp->dup); 269 printf("\n"); 270 } 271 if (zlnhead != NULL) { 272 printf("The following zero link count inodes remain:"); 273 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 274 printf(" %u,", zlnp->zlncnt); 275 printf("\n"); 276 } 277 } 278 zlnhead = (struct zlncnt *)0; 279 duplist = (struct dups *)0; 280 muldup = (struct dups *)0; 281 inocleanup(); 282 283 ckfini(1); 284 free(blockmap); 285 free(statemap); 286 free((char *)lncntp); 287 if (!fsmodified) 288 return (0); 289 if (!preen) 290 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 291 if (rerun) 292 printf("\n***** PLEASE RERUN FSCK *****\n"); 293 if (hotroot()) { 294 struct statfs stfs_buf; 295 /* 296 * We modified the root. Do a mount update on 297 * it, unless it is read-write, so we can continue. 298 */ 299 if (statfs("/", &stfs_buf) == 0) { 300 long flags = stfs_buf.f_flags; 301 struct ufs_args args; 302 int ret; 303 304 if (flags & MNT_RDONLY) { 305 args.fspec = 0; 306 args.export.ex_flags = 0; 307 args.export.ex_root = 0; 308 flags |= MNT_UPDATE | MNT_RELOAD; 309 ret = mount(MOUNT_LFS, "/", flags, &args); 310 if (ret == 0) 311 return (0); 312 } 313 } 314 if (!preen) 315 printf("\n***** REBOOT NOW *****\n"); 316 sync(); 317 return (4); 318 } 319 return (0); 320 } 321 322 static void 323 usage() 324 { 325 326 (void)fprintf(stderr, 327 "Usage: %s [-dnpy] [-b block] [-m mode] filesystem ...\n", 328 getprogname()); 329 exit(1); 330 } 331