main.c revision 1.70 1 /* $NetBSD: main.c,v 1.70 2008/08/30 14:07:32 dogcow 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. 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.
18 *
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.
30 */
31
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 */
37
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.70 2008/08/30 14:07:32 dogcow Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/time.h>
48 #include <sys/mount.h>
49 #include <sys/resource.h>
50
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>
55
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>
66
67 #include "fsck.h"
68 #include "extern.h"
69 #include "fsutil.h"
70 #include "exitvalues.h"
71 #include "snapshot.h"
72
73 int progress = 0;
74 int returntosingle = 0;
75
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 *);
80
81 int
82 main(int argc, char *argv[])
83 {
84 struct rlimit r;
85 int ch;
86 int ret = FSCK_EXIT_OK;
87 char *snap_backup = NULL;
88 int snap_internal = 0;
89
90 if (getrlimit(RLIMIT_DATA, &r) == 0) {
91 r.rlim_cur = r.rlim_max;
92 (void) setrlimit(RLIMIT_DATA, &r);
93 }
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:npPqyx:X")) != -1) {
101 switch (ch) {
102 case 'a':
103 isappleufs = 1;
104 break;
105
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;
113
114 case 'b':
115 skipclean = 0;
116 bflag = argtoi('b', "number", optarg, 10);
117 printf("Alternate super block location: %d\n", bflag);
118 break;
119
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);
127 }
128 break;
129
130 case 'd':
131 debug++;
132 break;
133
134 case 'F':
135 forceimage = 1;
136 break;
137
138 case 'f':
139 skipclean = 0;
140 break;
141
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;
149
150 case 'n':
151 nflag++;
152 yflag = 0;
153 break;
154
155 case 'p':
156 preen++;
157 break;
158
159 case 'P':
160 progress = 1;
161 break;
162
163 case 'q':
164 quiet++;
165 break;
166
167 case 'y':
168 yflag++;
169 nflag = 0;
170 break;
171 case 'x':
172 snap_backup = optarg;
173 break;
174 case 'X':
175 snap_internal = 1;
176 break;
177
178 default:
179 usage();
180 }
181 }
182
183 if (snap_backup || snap_internal) {
184 if (!nflag || yflag) {
185 warnx("Cannot use -x or -X without -n\n");
186 snap_backup = NULL;
187 snap_internal = 0;
188 }
189 }
190
191
192 argc -= optind;
193 argv += optind;
194
195 if (!argc)
196 usage();
197
198 if (debug)
199 progress = 0;
200
201 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
202 (void)signal(SIGINT, catch);
203 if (preen)
204 (void)signal(SIGQUIT, catchquit);
205 #ifdef PROGRESS
206 if (progress) {
207 progress_ttywidth(0);
208 (void)signal(SIGWINCH, progress_ttywidth);
209 }
210 #endif /* ! PROGRESS */
211 signal(SIGINFO, infohandler);
212
213 while (argc-- > 0) {
214 int nret;
215 char *path = strdup(blockcheck(*argv));
216
217 if (path == NULL)
218 pfatal("Can't check %s\n", *argv);
219
220 if (snap_backup || snap_internal) {
221 char *mpt;
222 char *snap_dev;
223 int snapfd;
224
225 mpt = get_snap_device(*argv);
226 if (mpt == NULL)
227 goto next;
228 snapfd = snap_open(mpt, snap_backup, NULL, &snap_dev);
229 if (snapfd < 0) {
230 warn("can't take snapshot of %s", mpt);
231 free(mpt);
232 goto next;
233 }
234 nret = checkfilesys(blockcheck(snap_dev), path, 0);
235 if (ret < nret)
236 ret = nret;
237 free(mpt);
238 close(snapfd);
239 } else {
240 nret = checkfilesys(path, path, 0);
241 if (ret < nret)
242 ret = nret;
243 }
244 next:
245 free(path);
246 argv++;
247 }
248
249 return returntosingle ? FSCK_EXIT_UNRESOLVED : ret;
250 }
251
252 static int
253 argtoi(int flag, const char *req, const char *str, int base)
254 {
255 char *cp;
256 int ret;
257
258 ret = (int)strtol(str, &cp, base);
259 if (cp == str || *cp)
260 errx(FSCK_EXIT_USAGE, "-%c flag requires a %s",
261 flag, req);
262 return (ret);
263 }
264
265 /*
266 * Check the specified filesystem.
267 */
268 /* ARGSUSED */
269 static int
270 checkfilesys(const char *filesys, const char *origfs, int child)
271 {
272 daddr_t n_ffree, n_bfree;
273 struct dups *dp;
274 struct zlncnt *zlnp;
275 int cylno;
276 #ifdef LITE2BORKEN
277 int flags;
278 #endif
279 #ifdef PROGRESS
280 /*
281 * In prune mode, how far does the progress bar travel during
282 * each pass? (In non-prune mode, each pass has a separate
283 * progress bar that travels from 0 to 100%.)
284 *
285 * The numbers below are percentages, intended to correspond
286 * roughly to the cumulative time up to the end of each pass.
287 * They don't have to be accurate. In reality, on a large
288 * file system, Pass 1 and Pass 2 together are likely to use
289 * significantly more than the 95% reflected below, so users
290 * will get a pleasant surprise when the last 5% of the progress
291 * bar runs more quickly than they had expected.
292 */
293 static int progress_limits[] = {0, 20, 95, 96, 97, 100};
294 #endif /* PROGRESS */
295
296 if (preen && child)
297 (void)signal(SIGQUIT, voidquit);
298 setcdevname(filesys, preen);
299 if (debug && preen)
300 pwarn("starting\n");
301 switch (setup(filesys, origfs)) {
302 case 0:
303 if (preen)
304 pfatal("CAN'T CHECK FILE SYSTEM.");
305 /* fall through */
306 case -1:
307 return FSCK_EXIT_OK;
308 }
309 /*
310 * Cleared if any questions answered no. Used to decide if
311 * the superblock should be marked clean.
312 */
313 resolved = 1;
314
315 #ifdef PROGRESS
316 progress_switch(progress);
317 progress_init();
318 #endif /* PROGRESS */
319
320 /*
321 * 1: scan inodes tallying blocks used
322 */
323 if (preen == 0) {
324 pwarn("** Last Mounted on %s\n", sblock->fs_fsmnt);
325 if (hotroot())
326 pwarn("** Root file system\n");
327 pwarn("** Phase 1 - Check Blocks and Sizes\n");
328 }
329 #ifdef PROGRESS
330 if (preen)
331 progress_setrange(0, progress_limits[1]);
332 #endif /* PROGRESS */
333 pass1();
334
335 /*
336 * 1b: locate first references to duplicates, if any
337 */
338 if (duplist) {
339 if (preen)
340 pfatal("INTERNAL ERROR: dups with -p\n");
341 if (usedsoftdep)
342 pfatal("INTERNAL ERROR: dups with softdep\n");
343 pwarn("** Phase 1b - Rescan For More DUPS\n");
344 pass1b();
345 }
346
347 /*
348 * 2: traverse directories from root to mark all connected directories
349 */
350 if (preen == 0)
351 pwarn("** Phase 2 - Check Pathnames\n");
352 #ifdef PROGRESS
353 if (preen)
354 progress_sethighlim(progress_limits[2]);
355 #endif /* PROGRESS */
356 pass2();
357
358 /*
359 * 3: scan inodes looking for disconnected directories
360 */
361 if (preen == 0)
362 pwarn("** Phase 3 - Check Connectivity\n");
363 #ifdef PROGRESS
364 if (preen)
365 progress_sethighlim(progress_limits[3]);
366 #endif /* PROGRESS */
367 pass3();
368
369 /*
370 * 4: scan inodes looking for disconnected files; check reference counts
371 */
372 if (preen == 0)
373 pwarn("** Phase 4 - Check Reference Counts\n");
374 #ifdef PROGRESS
375 if (preen)
376 progress_sethighlim(progress_limits[4]);
377 #endif /* PROGRESS */
378 pass4();
379
380 /*
381 * 5: check and repair resource counts in cylinder groups
382 */
383 if (preen == 0)
384 pwarn("** Phase 5 - Check Cyl groups\n");
385 #ifdef PROGRESS
386 if (preen)
387 progress_sethighlim(progress_limits[5]);
388 #endif /* PROGRESS */
389 pass5();
390
391 /*
392 * print out summary statistics
393 */
394 n_ffree = sblock->fs_cstotal.cs_nffree;
395 n_bfree = sblock->fs_cstotal.cs_nbfree;
396 pwarn("%llu files, %lld used, %lld free ",
397 (unsigned long long)n_files, (long long)n_blks,
398 (long long)(n_ffree + sblock->fs_frag * n_bfree));
399 printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n",
400 (long long)n_ffree, (long long)n_bfree,
401 (long long)(n_ffree * 100 / (daddr_t)sblock->fs_dsize),
402 (long long)(((n_ffree * 1000 + (daddr_t)sblock->fs_dsize / 2)
403 / (daddr_t)sblock->fs_dsize) % 10));
404 if (debug &&
405 (n_files -= maxino - ROOTINO - sblock->fs_cstotal.cs_nifree))
406 printf("%llu files missing\n", (unsigned long long)n_files);
407 if (debug) {
408 n_blks += sblock->fs_ncg *
409 (cgdmin(sblock, 0) - cgsblock(sblock, 0));
410 n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0);
411 n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize);
412 if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree))
413 printf("%lld blocks missing\n", (long long)n_blks);
414 if (duplist != NULL) {
415 printf("The following duplicate blocks remain:");
416 for (dp = duplist; dp; dp = dp->next)
417 printf(" %lld,", (long long)dp->dup);
418 printf("\n");
419 }
420 if (zlnhead != NULL) {
421 printf("The following zero link count inodes remain:");
422 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
423 printf(" %llu,",
424 (unsigned long long)zlnp->zlncnt);
425 printf("\n");
426 }
427 }
428 zlnhead = (struct zlncnt *)0;
429 duplist = (struct dups *)0;
430 muldup = (struct dups *)0;
431 inocleanup();
432 if (fsmodified) {
433 sblock->fs_time = time(NULL);
434 sbdirty();
435 }
436 if (rerun)
437 markclean = 0;
438 #if LITE2BORKEN
439 if (!hotroot()) {
440 ckfini();
441 } else {
442 struct statvfs stfs_buf;
443 /*
444 * Check to see if root is mounted read-write.
445 */
446 if (statvfs("/", &stfs_buf) == 0)
447 flags = stfs_buf.f_flag;
448 else
449 flags = 0;
450 if (markclean)
451 markclean = flags & MNT_RDONLY;
452 ckfini();
453 }
454 #else
455 ckfini();
456 #endif
457 for (cylno = 0; cylno < sblock->fs_ncg; cylno++)
458 if (inostathead[cylno].il_stat != NULL)
459 free(inostathead[cylno].il_stat);
460 free(inostathead);
461 inostathead = NULL;
462
463 if (!resolved || rerun) {
464 pwarn("\n***** UNRESOLVED INCONSISTENCIES REMAIN *****\n");
465 returntosingle = 1;
466 }
467 if (!fsmodified)
468 return FSCK_EXIT_OK;
469 if (!preen)
470 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
471 if (rerun)
472 pwarn("\n***** PLEASE RERUN FSCK *****\n");
473 if (hotroot()) {
474 struct statvfs stfs_buf;
475 /*
476 * We modified the root. Do a mount update on
477 * it, unless it is read-write, so we can continue.
478 */
479 if (statvfs("/", &stfs_buf) == 0) {
480 long flags = stfs_buf.f_flag;
481 struct ufs_args args;
482
483 if (flags & MNT_RDONLY) {
484 args.fspec = 0;
485 flags |= MNT_UPDATE | MNT_RELOAD;
486 if (mount(MOUNT_FFS, "/", flags,
487 &args, sizeof args) == 0)
488 return FSCK_EXIT_OK;
489 }
490 }
491 if (!preen)
492 pwarn("\n***** REBOOT NOW *****\n");
493 sync();
494 return FSCK_EXIT_ROOT_CHANGED;
495 }
496 return FSCK_EXIT_OK;
497 }
498
499 static void
500 usage(void)
501 {
502
503 (void) fprintf(stderr,
504 "usage: %s [-adFfnPpqyX] [-B be|le] [-b block] [-c level] [-m mode]"
505 " [-x snap-backup] filesystem ...\n",
506 getprogname());
507 exit(FSCK_EXIT_USAGE);
508 }
509
510 static
511 char *get_snap_device(char *file)
512 {
513 char *mountpoint = NULL;
514 struct statvfs *mntbuf, *fs, fsbuf;
515 struct stat sb;
516
517 /* find the mount point */
518 if (lstat(file, &sb) == -1) {
519 warn("can't stat %s", file);
520 return NULL;
521 }
522 if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
523 int mntbufc, i;
524 if ((mntbufc = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
525 pfatal("can't get mount list: %s\n", strerror(errno));
526 for (fs = mntbuf, i = 0;
527 i < mntbufc; i++, fs++) {
528 if (strcmp(fs->f_fstypename, "ufs") != 0 &&
529 strcmp(fs->f_fstypename, "ffs") != 0)
530 continue;
531 if (fs->f_flag & ST_RDONLY) {
532 warnx("Cannot use -x or -X "
533 "on read-only filesystem");
534 free(mntbuf);
535 return NULL;
536 }
537 if (strcmp(fs->f_mntfromname, unrawname(file)) == 0) {
538 mountpoint = strdup(fs->f_mntonname);
539 free(mntbuf);
540 return mountpoint;
541 }
542 }
543 warnx("Cannot use -x or -X on unmounted device");
544 free(mntbuf);
545 return NULL;
546 }
547 if (S_ISDIR(sb.st_mode)) {
548 if (statvfs(file, &fsbuf) == -1)
549 pfatal("can't statvfs %s: %s\n", file, strerror(errno));
550 if (strcmp(fsbuf.f_mntonname, file))
551 pfatal("%s is not a mount point\n", file);
552 if (fsbuf.f_flag & ST_RDONLY) {
553 warnx("Cannot use -x or -X "
554 "on read-only filesystem");
555 return NULL;
556 }
557 mountpoint = strdup(file);
558 return mountpoint;
559 }
560 pfatal("%s is not a mount point\n", file);
561 return NULL;
562 }
563