Do not call callout_stop() unless the structure was previoulsy
[netbsd-mini2440.git] / sbin / fsck_ffs / main.c
bloba954cff222a52018517bb851674d9acb1b52f5d1
1 /* $NetBSD: main.c,v 1.72 2008/10/09 18:38:24 wiz Exp $ */
3 /*
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\
35 The Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95";
41 #else
42 __RCSID("$NetBSD: main.c,v 1.72 2008/10/09 18:38:24 wiz Exp $");
43 #endif
44 #endif /* not lint */
46 #include <sys/param.h>
47 #include <sys/time.h>
48 #include <sys/mount.h>
49 #include <sys/resource.h>
51 #include <ufs/ufs/dinode.h>
52 #include <ufs/ufs/ufsmount.h>
53 #include <ufs/ffs/fs.h>
54 #include <ufs/ffs/ffs_extern.h>
56 #include <ctype.h>
57 #include <err.h>
58 #include <errno.h>
59 #include <fstab.h>
60 #include <string.h>
61 #include <time.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <unistd.h>
65 #include <signal.h>
67 #include "fsck.h"
68 #include "extern.h"
69 #include "fsutil.h"
70 #include "exitvalues.h"
71 #include "snapshot.h"
73 int progress = 0;
74 int returntosingle = 0;
76 static int argtoi(int, const char *, const char *, int);
77 static int checkfilesys(const char *, const char *, int);
78 static void usage(void);
79 static char *get_snap_device(char *);
81 int
82 main(int argc, char *argv[])
84 struct rlimit r;
85 int ch;
86 int ret = FSCK_EXIT_OK;
87 char *snap_backup = NULL;
88 int snap_internal = 0;
90 if (getrlimit(RLIMIT_DATA, &r) == 0) {
91 r.rlim_cur = r.rlim_max;
92 (void) setrlimit(RLIMIT_DATA, &r);
94 sync();
95 skipclean = 1;
96 markclean = 1;
97 forceimage = 0;
98 endian = 0;
99 isappleufs = 0;
100 while ((ch = getopt(argc, argv, "aB:b:c:dFfm:npPqUyx:X")) != -1) {
101 switch (ch) {
102 case 'a':
103 isappleufs = 1;
104 break;
106 case 'B':
107 if (strcmp(optarg, "be") == 0)
108 endian = BIG_ENDIAN;
109 else if (strcmp(optarg, "le") == 0)
110 endian = LITTLE_ENDIAN;
111 else usage();
112 break;
114 case 'b':
115 skipclean = 0;
116 bflag = argtoi('b', "number", optarg, 10);
117 printf("Alternate super block location: %d\n", bflag);
118 break;
120 case 'c':
121 skipclean = 0;
122 cvtlevel = argtoi('c', "conversion level", optarg, 10);
123 if (cvtlevel > 4) {
124 cvtlevel = 4;
125 warnx("Using maximum conversion level of %d\n",
126 cvtlevel);
128 break;
130 case 'd':
131 debug++;
132 break;
134 case 'F':
135 forceimage = 1;
136 break;
138 case 'f':
139 skipclean = 0;
140 break;
142 case 'm':
143 lfmode = argtoi('m', "mode", optarg, 8);
144 if (lfmode &~ 07777)
145 errx(FSCK_EXIT_USAGE, "bad mode to -m: %o",
146 lfmode);
147 printf("** lost+found creation mode %o\n", lfmode);
148 break;
150 case 'n':
151 nflag++;
152 yflag = 0;
153 break;
155 case 'p':
156 preen++;
157 break;
159 case 'P':
160 progress = 1;
161 break;
163 case 'q':
164 quiet++;
165 break;
166 #ifndef SMALL
167 case 'U':
168 Uflag++;
169 break;
170 #endif
172 case 'y':
173 yflag++;
174 nflag = 0;
175 break;
176 case 'x':
177 snap_backup = optarg;
178 break;
179 case 'X':
180 snap_internal = 1;
181 break;
183 default:
184 usage();
188 if (snap_backup || snap_internal) {
189 if (!nflag || yflag) {
190 warnx("Cannot use -x or -X without -n\n");
191 snap_backup = NULL;
192 snap_internal = 0;
197 argc -= optind;
198 argv += optind;
200 if (!argc)
201 usage();
203 if (debug)
204 progress = 0;
206 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
207 (void)signal(SIGINT, catch);
208 if (preen)
209 (void)signal(SIGQUIT, catchquit);
210 #ifdef PROGRESS
211 if (progress) {
212 progress_ttywidth(0);
213 (void)signal(SIGWINCH, progress_ttywidth);
215 #endif /* ! PROGRESS */
216 signal(SIGINFO, infohandler);
218 while (argc-- > 0) {
219 int nret;
220 char *path = strdup(blockcheck(*argv));
222 if (path == NULL)
223 pfatal("Can't check %s\n", *argv);
225 if (snap_backup || snap_internal) {
226 char *mpt;
227 char *snap_dev;
228 int snapfd;
230 mpt = get_snap_device(*argv);
231 if (mpt == NULL)
232 goto next;
233 snapfd = snap_open(mpt, snap_backup, NULL, &snap_dev);
234 if (snapfd < 0) {
235 warn("can't take snapshot of %s", mpt);
236 free(mpt);
237 goto next;
239 nret = checkfilesys(blockcheck(snap_dev), path, 0);
240 if (ret < nret)
241 ret = nret;
242 free(mpt);
243 close(snapfd);
244 } else {
245 nret = checkfilesys(path, path, 0);
246 if (ret < nret)
247 ret = nret;
249 next:
250 free(path);
251 argv++;
254 return returntosingle ? FSCK_EXIT_UNRESOLVED : ret;
257 static int
258 argtoi(int flag, const char *req, const char *str, int base)
260 char *cp;
261 int ret;
263 ret = (int)strtol(str, &cp, base);
264 if (cp == str || *cp)
265 errx(FSCK_EXIT_USAGE, "-%c flag requires a %s",
266 flag, req);
267 return (ret);
271 * Check the specified filesystem.
273 /* ARGSUSED */
274 static int
275 checkfilesys(const char *filesys, const char *origfs, int child)
277 daddr_t n_ffree, n_bfree;
278 struct dups *dp;
279 struct zlncnt *zlnp;
280 int cylno;
281 #ifdef LITE2BORKEN
282 int flags;
283 #endif
284 #ifdef PROGRESS
286 * In prune mode, how far does the progress bar travel during
287 * each pass? (In non-prune mode, each pass has a separate
288 * progress bar that travels from 0 to 100%.)
290 * The numbers below are percentages, intended to correspond
291 * roughly to the cumulative time up to the end of each pass.
292 * They don't have to be accurate. In reality, on a large
293 * file system, Pass 1 and Pass 2 together are likely to use
294 * significantly more than the 95% reflected below, so users
295 * will get a pleasant surprise when the last 5% of the progress
296 * bar runs more quickly than they had expected.
298 static int progress_limits[] = {0, 20, 95, 96, 97, 100};
299 #endif /* PROGRESS */
301 if (preen && child)
302 (void)signal(SIGQUIT, voidquit);
303 setcdevname(filesys, preen);
304 if (debug && preen)
305 pwarn("starting\n");
306 switch (setup(filesys, origfs)) {
307 case 0:
308 if (preen)
309 pfatal("CAN'T CHECK FILE SYSTEM.");
310 /* fall through */
311 case -1:
312 return FSCK_EXIT_OK;
315 * Cleared if any questions answered no. Used to decide if
316 * the superblock should be marked clean.
318 resolved = 1;
320 #ifdef PROGRESS
321 progress_switch(progress);
322 progress_init();
323 #endif /* PROGRESS */
326 * 1: scan inodes tallying blocks used
328 if (preen == 0) {
329 pwarn("** Last Mounted on %s\n", sblock->fs_fsmnt);
330 if (hotroot())
331 pwarn("** Root file system\n");
332 pwarn("** Phase 1 - Check Blocks and Sizes\n");
334 #ifdef PROGRESS
335 if (preen)
336 progress_setrange(0, progress_limits[1]);
337 #endif /* PROGRESS */
338 pass1();
341 * 1b: locate first references to duplicates, if any
343 if (duplist) {
344 if (preen)
345 pfatal("INTERNAL ERROR: dups with -p\n");
346 if (usedsoftdep)
347 pfatal("INTERNAL ERROR: dups with softdep\n");
348 pwarn("** Phase 1b - Rescan For More DUPS\n");
349 pass1b();
353 * 2: traverse directories from root to mark all connected directories
355 if (preen == 0)
356 pwarn("** Phase 2 - Check Pathnames\n");
357 #ifdef PROGRESS
358 if (preen)
359 progress_sethighlim(progress_limits[2]);
360 #endif /* PROGRESS */
361 pass2();
364 * 3: scan inodes looking for disconnected directories
366 if (preen == 0)
367 pwarn("** Phase 3 - Check Connectivity\n");
368 #ifdef PROGRESS
369 if (preen)
370 progress_sethighlim(progress_limits[3]);
371 #endif /* PROGRESS */
372 pass3();
375 * 4: scan inodes looking for disconnected files; check reference counts
377 if (preen == 0)
378 pwarn("** Phase 4 - Check Reference Counts\n");
379 #ifdef PROGRESS
380 if (preen)
381 progress_sethighlim(progress_limits[4]);
382 #endif /* PROGRESS */
383 pass4();
386 * 5: check and repair resource counts in cylinder groups
388 if (preen == 0)
389 pwarn("** Phase 5 - Check Cyl groups\n");
390 #ifdef PROGRESS
391 if (preen)
392 progress_sethighlim(progress_limits[5]);
393 #endif /* PROGRESS */
394 pass5();
397 * print out summary statistics
399 n_ffree = sblock->fs_cstotal.cs_nffree;
400 n_bfree = sblock->fs_cstotal.cs_nbfree;
401 pwarn("%llu files, %lld used, %lld free ",
402 (unsigned long long)n_files, (long long)n_blks,
403 (long long)(n_ffree + sblock->fs_frag * n_bfree));
404 printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n",
405 (long long)n_ffree, (long long)n_bfree,
406 (long long)(n_ffree * 100 / (daddr_t)sblock->fs_dsize),
407 (long long)(((n_ffree * 1000 + (daddr_t)sblock->fs_dsize / 2)
408 / (daddr_t)sblock->fs_dsize) % 10));
409 if (debug &&
410 (n_files -= maxino - ROOTINO - sblock->fs_cstotal.cs_nifree))
411 printf("%llu files missing\n", (unsigned long long)n_files);
412 if (debug) {
413 n_blks += sblock->fs_ncg *
414 (cgdmin(sblock, 0) - cgsblock(sblock, 0));
415 n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0);
416 n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize);
417 if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree))
418 printf("%lld blocks missing\n", (long long)n_blks);
419 if (duplist != NULL) {
420 printf("The following duplicate blocks remain:");
421 for (dp = duplist; dp; dp = dp->next)
422 printf(" %lld,", (long long)dp->dup);
423 printf("\n");
425 if (zlnhead != NULL) {
426 printf("The following zero link count inodes remain:");
427 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
428 printf(" %llu,",
429 (unsigned long long)zlnp->zlncnt);
430 printf("\n");
433 zlnhead = (struct zlncnt *)0;
434 duplist = (struct dups *)0;
435 muldup = (struct dups *)0;
436 inocleanup();
437 if (fsmodified) {
438 sblock->fs_time = time(NULL);
439 sbdirty();
441 if (rerun)
442 markclean = 0;
443 #if LITE2BORKEN
444 if (!hotroot()) {
445 ckfini();
446 } else {
447 struct statvfs stfs_buf;
449 * Check to see if root is mounted read-write.
451 if (statvfs("/", &stfs_buf) == 0)
452 flags = stfs_buf.f_flag;
453 else
454 flags = 0;
455 if (markclean)
456 markclean = flags & MNT_RDONLY;
457 ckfini();
459 #else
460 ckfini();
461 #endif
462 for (cylno = 0; cylno < sblock->fs_ncg; cylno++)
463 if (inostathead[cylno].il_stat != NULL)
464 free(inostathead[cylno].il_stat);
465 free(inostathead);
466 inostathead = NULL;
468 if (!resolved || rerun) {
469 pwarn("\n***** UNRESOLVED INCONSISTENCIES REMAIN *****\n");
470 returntosingle = 1;
472 if (!fsmodified)
473 return FSCK_EXIT_OK;
474 if (!preen)
475 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
476 if (rerun)
477 pwarn("\n***** PLEASE RERUN FSCK *****\n");
478 if (hotroot()) {
479 struct statvfs stfs_buf;
481 * We modified the root. Do a mount update on
482 * it, unless it is read-write, so we can continue.
484 if (statvfs("/", &stfs_buf) == 0) {
485 long flags = stfs_buf.f_flag;
486 struct ufs_args args;
488 if (flags & MNT_RDONLY) {
489 args.fspec = 0;
490 flags |= MNT_UPDATE | MNT_RELOAD;
491 if (mount(MOUNT_FFS, "/", flags,
492 &args, sizeof args) == 0)
493 return FSCK_EXIT_OK;
496 if (!preen)
497 pwarn("\n***** REBOOT NOW *****\n");
498 sync();
499 return FSCK_EXIT_ROOT_CHANGED;
501 return FSCK_EXIT_OK;
504 static void
505 usage(void)
508 (void) fprintf(stderr,
509 "usage: %s [-adFfPpqUX] [-B byteorder] [-b block] [-c level] "
510 "[-m mode]\n"
511 "\t[-x snap-backup] [-y | -n] filesystem ...\n",
512 getprogname());
513 exit(FSCK_EXIT_USAGE);
516 static
517 char *get_snap_device(char *file)
519 char *mountpoint = NULL;
520 struct statvfs *mntbuf, *fs, fsbuf;
521 struct stat sb;
523 /* find the mount point */
524 if (lstat(file, &sb) == -1) {
525 warn("can't stat %s", file);
526 return NULL;
528 if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
529 int mntbufc, i;
530 if ((mntbufc = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
531 pfatal("can't get mount list: %s\n", strerror(errno));
532 for (fs = mntbuf, i = 0;
533 i < mntbufc; i++, fs++) {
534 if (strcmp(fs->f_fstypename, "ufs") != 0 &&
535 strcmp(fs->f_fstypename, "ffs") != 0)
536 continue;
537 if (fs->f_flag & ST_RDONLY) {
538 warnx("Cannot use -x or -X "
539 "on read-only filesystem");
540 free(mntbuf);
541 return NULL;
543 if (strcmp(fs->f_mntfromname, unrawname(file)) == 0) {
544 mountpoint = strdup(fs->f_mntonname);
545 free(mntbuf);
546 return mountpoint;
549 warnx("Cannot use -x or -X on unmounted device");
550 free(mntbuf);
551 return NULL;
553 if (S_ISDIR(sb.st_mode)) {
554 if (statvfs(file, &fsbuf) == -1)
555 pfatal("can't statvfs %s: %s\n", file, strerror(errno));
556 if (strcmp(fsbuf.f_mntonname, file))
557 pfatal("%s is not a mount point\n", file);
558 if (fsbuf.f_flag & ST_RDONLY) {
559 warnx("Cannot use -x or -X "
560 "on read-only filesystem");
561 return NULL;
563 mountpoint = strdup(file);
564 return mountpoint;
566 pfatal("%s is not a mount point\n", file);
567 return NULL;