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