1 /* 2 * Copyright (c) 1980, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#) Copyright (c) 1980, 1986, 1993 The Regents of the University of California. All rights reserved. 30 * @(#)main.c 8.6 (Berkeley) 5/14/95 31 * $FreeBSD: src/sbin/fsck/main.c,v 1.21.2.1 2001/01/23 23:11:07 iedowse Exp $ 32 * $DragonFly: src/sbin/fsck/main.c,v 1.10 2007/04/20 22:20:10 dillon Exp $ 33 */ 34 35 #include <sys/param.h> 36 #include <sys/stat.h> 37 #include <sys/time.h> 38 #include <sys/mount.h> 39 #include <sys/resource.h> 40 41 #include <vfs/ufs/dinode.h> 42 #include <vfs/ufs/ufsmount.h> 43 #include <vfs/ufs/fs.h> 44 45 #include <err.h> 46 #include <errno.h> 47 #include <fstab.h> 48 #include <paths.h> 49 #include <string.h> 50 51 #include "fsck.h" 52 53 static int argtoi(int flag, char *req, char *str, int base); 54 static int docheck(struct fstab *fsp); 55 static int checkfilesys(char *filesys, char *mntpt, long auxdata, 56 int child); 57 static struct statfs *getmntpt(const char *); 58 int main(int argc, char *argv[]); 59 60 int 61 main(int argc, char **argv) 62 { 63 int ch; 64 int ret, maxrun = 0; 65 struct rlimit rlimit; 66 67 sync(); 68 while ((ch = getopt(argc, argv, "dfLpnNyYb:c:l:m:")) != -1) { 69 switch (ch) { 70 case 'p': 71 preen++; 72 break; 73 74 case 'b': 75 bflag = argtoi('b', "number", optarg, 10); 76 printf("Alternate super block location: %d\n", bflag); 77 break; 78 79 case 'c': 80 cvtlevel = argtoi('c', "conversion level", optarg, 10); 81 break; 82 83 case 'd': 84 debug++; 85 break; 86 87 case 'f': 88 fflag++; 89 break; 90 91 case 'l': 92 maxrun = argtoi('l', "number", optarg, 10); 93 break; 94 95 case 'L': 96 lastmntonly++; 97 break; 98 99 case 'm': 100 lfmode = argtoi('m', "mode", optarg, 8); 101 if (lfmode &~ 07777) 102 errx(EEXIT, "bad mode to -m: %o", lfmode); 103 printf("** lost+found creation mode %o\n", lfmode); 104 break; 105 106 case 'n': 107 case 'N': 108 nflag++; 109 yflag = 0; 110 break; 111 112 case 'y': 113 case 'Y': 114 yflag++; 115 nflag = 0; 116 break; 117 118 default: 119 errx(EEXIT, "%c option?", ch); 120 } 121 } 122 argc -= optind; 123 argv += optind; 124 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 125 signal(SIGINT, catch); 126 if (preen) 127 signal(SIGQUIT, catchquit); 128 signal(SIGINFO, infohandler); 129 /* 130 * Push up our allowed memory limit so we can cope 131 * with huge filesystems. 132 */ 133 if (getrlimit(RLIMIT_DATA, &rlimit) == 0) { 134 rlimit.rlim_cur = rlimit.rlim_max; 135 setrlimit(RLIMIT_DATA, &rlimit); 136 } 137 if (argc) { 138 while (argc-- > 0) { 139 char *path = blockcheck(*argv); 140 141 if (path == NULL) 142 pfatal("Can't check %s\n", *argv); 143 else 144 checkfilesys(path, 0, 0L, 0); 145 ++argv; 146 } 147 exit(0); 148 } 149 ret = checkfstab(preen, maxrun, docheck, checkfilesys); 150 if (returntosingle) 151 exit(2); 152 exit(ret); 153 } 154 155 static int 156 argtoi(int flag, char *req, char *str, int base) 157 { 158 char *cp; 159 int ret; 160 161 ret = (int)strtol(str, &cp, base); 162 if (cp == str || *cp) 163 errx(EEXIT, "-%c flag requires a %s", flag, req); 164 return (ret); 165 } 166 167 /* 168 * Determine whether a filesystem should be checked. 169 */ 170 static int 171 docheck(struct fstab *fsp) 172 { 173 174 if (strcmp(fsp->fs_vfstype, "ufs") || 175 (strcmp(fsp->fs_type, FSTAB_RW) && 176 strcmp(fsp->fs_type, FSTAB_RO)) || 177 fsp->fs_passno == 0) 178 return (0); 179 return (1); 180 } 181 182 /* 183 * Check the specified filesystem. 184 */ 185 /* ARGSUSED */ 186 static int 187 checkfilesys(char *filesys, char *mntpt, long auxdata, int child) 188 { 189 ufs_daddr_t n_ffree, n_bfree; 190 struct dups *dp; 191 struct statfs *mntbuf; 192 struct zlncnt *zlnp; 193 int cylno; 194 195 if (preen && child) 196 signal(SIGQUIT, voidquit); 197 cdevname = filesys; 198 if (debug && preen) 199 pwarn("starting\n"); 200 switch (setup(filesys)) { 201 case 0: 202 if (preen) 203 pfatal("CAN'T CHECK FILE SYSTEM."); 204 return (0); 205 case -1: 206 pwarn("clean, %ld free ", sblock.fs_cstotal.cs_nffree + 207 sblock.fs_frag * sblock.fs_cstotal.cs_nbfree); 208 printf("(%d frags, %d blocks, %.1f%% fragmentation)\n", 209 sblock.fs_cstotal.cs_nffree, sblock.fs_cstotal.cs_nbfree, 210 sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize); 211 return (0); 212 } 213 got_siginfo = 0; 214 215 /* 216 * Get the mount point information of the filesystem, if 217 * it is available. 218 */ 219 mntbuf = getmntpt(filesys); 220 221 /* 222 * Cleared if any questions answered no. Used to decide if 223 * the superblock should be marked clean. 224 */ 225 resolved = 1; 226 /* 227 * 1: scan inodes tallying blocks used 228 */ 229 if (lastmntonly) { 230 printf("** %s Last Mounted on %s\n", filesys, sblock.fs_fsmnt); 231 return (0); 232 } 233 if (preen == 0) { 234 printf("** Last Mounted on %s\n", sblock.fs_fsmnt); 235 if (mntbuf != NULL && mntbuf->f_flags & MNT_ROOTFS) 236 printf("** Root file system\n"); 237 printf("** Phase 1 - Check Blocks and Sizes\n"); 238 } 239 pass1(); 240 241 /* 242 * 1b: locate first references to duplicates, if any 243 */ 244 if (duplist) { 245 if (preen || usedsoftdep) 246 pfatal("INTERNAL ERROR: dups with -p"); 247 printf("** Phase 1b - Rescan For More DUPS\n"); 248 pass1b(); 249 } 250 251 /* 252 * 2: traverse directories from root to mark all connected directories 253 */ 254 if (preen == 0) 255 printf("** Phase 2 - Check Pathnames\n"); 256 pass2(); 257 258 /* 259 * 3: scan inodes looking for disconnected directories 260 */ 261 if (preen == 0) 262 printf("** Phase 3 - Check Connectivity\n"); 263 pass3(); 264 265 /* 266 * 4: scan inodes looking for disconnected files; check reference counts 267 */ 268 if (preen == 0) 269 printf("** Phase 4 - Check Reference Counts\n"); 270 pass4(); 271 272 /* 273 * 5: check and repair resource counts in cylinder groups 274 */ 275 if (preen == 0) 276 printf("** Phase 5 - Check Cyl groups\n"); 277 pass5(); 278 279 /* 280 * print out summary statistics 281 */ 282 n_ffree = sblock.fs_cstotal.cs_nffree; 283 n_bfree = sblock.fs_cstotal.cs_nbfree; 284 pwarn("%ld files, %ld used, %ld free ", 285 n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree); 286 printf("(%d frags, %d blocks, %.1f%% fragmentation)\n", 287 n_ffree, n_bfree, n_ffree * 100.0 / sblock.fs_dsize); 288 if (debug && 289 (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree)) 290 printf("%d files missing\n", n_files); 291 if (debug) { 292 n_blks += sblock.fs_ncg * 293 (cgdmin(&sblock, 0) - cgsblock(&sblock, 0)); 294 n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0); 295 n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize); 296 if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree)) 297 printf("%d blocks missing\n", n_blks); 298 if (duplist != NULL) { 299 printf("The following duplicate blocks remain:"); 300 for (dp = duplist; dp; dp = dp->next) 301 printf(" %d,", dp->dup); 302 printf("\n"); 303 } 304 if (zlnhead != NULL) { 305 printf("The following zero link count inodes remain:"); 306 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 307 printf(" %u,", zlnp->zlncnt); 308 printf("\n"); 309 } 310 } 311 zlnhead = NULL; 312 duplist = NULL; 313 muldup = NULL; 314 inocleanup(); 315 if (fsmodified) { 316 sblock.fs_time = time(NULL); 317 sbdirty(); 318 } 319 if (cvtlevel && sblk.b_dirty) { 320 /* 321 * Write out the duplicate super blocks 322 */ 323 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 324 bwrite(fswritefd, (char *)&sblock, 325 fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE); 326 } 327 if (rerun) 328 resolved = 0; 329 330 /* 331 * Check to see if the filesystem is mounted read-write. 332 */ 333 if (mntbuf != NULL && (mntbuf->f_flags & MNT_RDONLY) == 0) 334 resolved = 0; 335 ckfini(resolved); 336 337 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 338 if (inostathead[cylno].il_stat != NULL) 339 free((char *)inostathead[cylno].il_stat); 340 free((char *)inostathead); 341 inostathead = NULL; 342 if (fsmodified && !preen) 343 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 344 if (rerun) 345 printf("\n***** PLEASE RERUN FSCK *****\n"); 346 if (mntbuf != NULL) { 347 struct ufs_args args; 348 int ret; 349 /* 350 * We modified a mounted filesystem. Do a mount update on 351 * it unless it is read-write, so we can continue using it 352 * as safely as possible. 353 */ 354 if (mntbuf->f_flags & MNT_RDONLY) { 355 args.fspec = 0; 356 args.export.ex_flags = 0; 357 args.export.ex_root = 0; 358 ret = mount("ufs", mntbuf->f_mntonname, 359 mntbuf->f_flags | MNT_UPDATE | MNT_RELOAD, &args); 360 if (ret == 0) 361 return (0); 362 pwarn("mount reload of '%s' failed: %s\n\n", 363 mntbuf->f_mntonname, strerror(errno)); 364 } 365 if (!fsmodified) 366 return (0); 367 if (!preen) 368 printf("\n***** REBOOT NOW *****\n"); 369 sync(); 370 return (4); 371 } 372 return (0); 373 } 374 375 /* 376 * Get the directory that the device is mounted on. 377 */ 378 static struct statfs * 379 getmntpt(const char *name) 380 { 381 struct stat devstat, mntdevstat; 382 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN]; 383 char *devname; 384 struct statfs *mntbuf; 385 int i, mntsize; 386 387 if (stat(name, &devstat) != 0 || 388 !(S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))) 389 return (NULL); 390 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 391 for (i = 0; i < mntsize; i++) { 392 if (strcmp(mntbuf[i].f_fstypename, "ufs") != 0) 393 continue; 394 devname = mntbuf[i].f_mntfromname; 395 if (*devname != '/') { 396 strcpy(device, _PATH_DEV); 397 strcat(device, devname); 398 devname = device; 399 } 400 if (stat(devname, &mntdevstat) == 0 && 401 mntdevstat.st_rdev == devstat.st_rdev) 402 return (&mntbuf[i]); 403 } 404 return (NULL); 405 } 406