1 /* $NetBSD: main.c,v 1.6 2000/06/14 18:43:58 perseant 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:di:m:npy"; 73 74 sync(); 75 skipclean = 1; 76 exitonfail = 0; 77 idaddr = 0x0; 78 while ((ch = getopt(argc, argv, optstring)) != EOF) { 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 'i': 95 idaddr = strtol(optarg, NULL, 0); 96 break; 97 case 'm': 98 lfmode = argtoi('m', "mode", optarg, 8); 99 if (lfmode & ~07777) 100 errexit("bad mode to -m: %o\n", lfmode); 101 printf("** lost+found creation mode %o\n", lfmode); 102 break; 103 104 case 'n': 105 nflag++; 106 yflag = 0; 107 break; 108 109 case 'p': 110 preen++; 111 break; 112 113 case 'y': 114 yflag++; 115 nflag = 0; 116 break; 117 118 default: 119 usage(); 120 } 121 } 122 #if 0 123 if (nflag == 0) { 124 errexit("fsck_lfs cannot write to the filesystem yet; the -n flag is required.\n"); 125 } 126 #endif 127 128 argc -= optind; 129 argv += optind; 130 131 if (!argc) 132 usage(); 133 134 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 135 (void)signal(SIGINT, catch); 136 if (preen) 137 (void)signal(SIGQUIT, catchquit); 138 139 while (argc-- > 0) 140 (void)checkfilesys(blockcheck(*argv++), 0, 0L, 0); 141 142 if (returntosingle) 143 ret = 2; 144 145 exit(ret); 146 } 147 148 static int 149 argtoi(int flag, char *req, char *str, int base) 150 { 151 char *cp; 152 int ret; 153 154 ret = (int)strtol(str, &cp, base); 155 if (cp == str || *cp) 156 errexit("-%c flag requires a %s\n", flag, req); 157 return (ret); 158 } 159 160 #if 0 161 /* 162 * Determine whether a filesystem should be checked. 163 */ 164 static int 165 docheck(struct fstab * fsp) 166 { 167 168 if ((strcmp(fsp->fs_vfstype, "ufs") && 169 strcmp(fsp->fs_vfstype, "ffs")) || 170 (strcmp(fsp->fs_type, FSTAB_RW) && 171 strcmp(fsp->fs_type, FSTAB_RO)) || 172 fsp->fs_passno == 0) 173 return (0); 174 return (1); 175 } 176 #endif 177 178 /* 179 * Check the specified filesystem. 180 */ 181 /* ARGSUSED */ 182 static int 183 checkfilesys(const char *filesys, char *mntpt, long auxdata, int child) 184 { 185 daddr_t n_ffree = 0, n_bfree = 0; 186 struct dups *dp; 187 struct zlncnt *zlnp; 188 189 if (preen && child) 190 (void)signal(SIGQUIT, voidquit); 191 setcdevname(filesys, preen); 192 if (debug && preen) 193 pwarn("starting\n"); 194 switch (setup(filesys)) { 195 case 0: 196 if (preen) 197 pfatal("CAN'T CHECK FILE SYSTEM."); 198 case -1: 199 return (0); 200 } 201 202 if (preen == 0) { 203 printf("** Last Mounted on %s\n", sblock.lfs_fsmnt); 204 if (hotroot()) 205 printf("** Root file system\n"); 206 } 207 /* 208 * 0: check segment checksums, inode ranges 209 */ 210 if (preen == 0) 211 printf("** Phase 0 - Check Segment Summaries and Inode Free List\n"); 212 if (idaddr != sblock.lfs_idaddr) 213 pwarn("-i given, skipping free list check\n"); 214 else 215 pass0(); 216 217 if (preen == 0) { 218 /* 219 * 1: scan inodes tallying blocks used 220 */ 221 if (preen == 0) 222 printf("** Phase 1 - Check Blocks and Sizes\n"); 223 pass1(); 224 225 /* 226 * 2: traverse directories from root to mark all connected directories 227 */ 228 if (preen == 0) 229 printf("** Phase 2 - Check Pathnames\n"); 230 pass2(); 231 232 /* 233 * 3: scan inodes looking for disconnected directories 234 */ 235 if (preen == 0) 236 printf("** Phase 3 - Check Connectivity\n"); 237 pass3(); 238 239 /* 240 * 4: scan inodes looking for disconnected files; check reference counts 241 */ 242 if (preen == 0) 243 printf("** Phase 4 - Check Reference Counts\n"); 244 pass4(); 245 246 247 /* 248 * 5: check segment byte totals and dirty flags 249 */ 250 if (preen == 0) 251 printf("** Phase 5 - Check Segment Block Accounting\n"); 252 pass5(); 253 254 /* 255 * print out summary statistics 256 */ 257 pwarn("%d files, %d used, %d free ", 258 n_files, n_blks, n_ffree + sblock.lfs_frag * n_bfree); 259 putchar('\n'); 260 } 261 if (debug) { 262 if (duplist != NULL) { 263 printf("The following duplicate blocks remain:"); 264 for (dp = duplist; dp; dp = dp->next) 265 printf(" %d,", dp->dup); 266 printf("\n"); 267 } 268 if (zlnhead != NULL) { 269 printf("The following zero link count inodes remain:"); 270 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 271 printf(" %u,", zlnp->zlncnt); 272 printf("\n"); 273 } 274 } 275 zlnhead = (struct zlncnt *)0; 276 duplist = (struct dups *)0; 277 muldup = (struct dups *)0; 278 inocleanup(); 279 280 ckfini(1); 281 free(blockmap); 282 free(statemap); 283 free((char *)lncntp); 284 if (!fsmodified) 285 return (0); 286 if (!preen) 287 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 288 if (rerun) 289 printf("\n***** PLEASE RERUN FSCK *****\n"); 290 if (hotroot()) { 291 struct statfs stfs_buf; 292 /* 293 * We modified the root. Do a mount update on 294 * it, unless it is read-write, so we can continue. 295 */ 296 if (statfs("/", &stfs_buf) == 0) { 297 long flags = stfs_buf.f_flags; 298 struct ufs_args args; 299 int ret; 300 301 if (flags & MNT_RDONLY) { 302 args.fspec = 0; 303 args.export.ex_flags = 0; 304 args.export.ex_root = 0; 305 flags |= MNT_UPDATE | MNT_RELOAD; 306 ret = mount(MOUNT_LFS, "/", flags, &args); 307 if (ret == 0) 308 return (0); 309 } 310 } 311 if (!preen) 312 printf("\n***** REBOOT NOW *****\n"); 313 sync(); 314 return (4); 315 } 316 return (0); 317 } 318 319 static void 320 usage() 321 { 322 extern char *__progname; 323 324 (void)fprintf(stderr, 325 "Usage: %s [-dnpy] [-b block] [-m mode] filesystem ...\n", 326 __progname); 327 exit(1); 328 } 329