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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1980, 1986, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95";
43#endif
44static const char rcsid[] =
45 "$FreeBSD: head/sbin/fsck_ffs/main.c 92839 2002-03-20 22:57:10Z imp $";
46#endif /* not lint */
47
48#include <sys/param.h>
49#include <sys/stat.h>
50#include <sys/file.h>
51#include <sys/time.h>
52#include <sys/mount.h>
53#include <sys/resource.h>
54#include <sys/sysctl.h>
55
56#include <ufs/ufs/dinode.h>
57#include <ufs/ufs/ufsmount.h>
58#include <ufs/ffs/fs.h>
59
60#include <err.h>
61#include <errno.h>
62#include <fstab.h>
63#include <paths.h>
64#include <string.h>
65
66#include "fsck.h"
67
68static void usage(void) __dead2;
69static int argtoi(int flag, char *req, char *str, int base);
70static int checkfilesys(char *filesys);
71static struct statfs *getmntpt(const char *);
72
73int
74main(int argc, char *argv[])
75{
76 int ch;
77 struct rlimit rlimit;
78 int ret = 0;
79
80 sync();
81 skipclean = 1;
82 while ((ch = getopt(argc, argv, "b:Bc:dfFm:npy")) != -1) {
83 switch (ch) {
84 case 'b':
85 skipclean = 0;
86 bflag = argtoi('b', "number", optarg, 10);
87 printf("Alternate super block location: %d\n", bflag);
88 break;
89
90 case 'B':
91 bkgrdflag = 1;
92 break;
93
94 case 'c':
95 skipclean = 0;
96 cvtlevel = argtoi('c', "conversion level", optarg, 10);
97 break;
98
99 case 'd':
100 debug++;
101 break;
102
103 case 'f':
104 skipclean = 0;
105 break;
106
107 case 'F':
108 bkgrdcheck = 1;
109 break;
110
111 case 'm':
112 lfmode = argtoi('m', "mode", optarg, 8);
113 if (lfmode &~ 07777)
114 errx(EEXIT, "bad mode to -m: %o", lfmode);
115 printf("** lost+found creation mode %o\n", lfmode);
116 break;
117
118 case 'n':
119 nflag++;
120 yflag = 0;
121 break;
122
123 case 'p':
124 preen++;
125 break;
126
127 case 'y':
128 yflag++;
129 nflag = 0;
130 break;
131
132 default:
133 usage();
134 }
135 }
136 argc -= optind;
137 argv += optind;
138
139 if (!argc)
140 usage();
141
142 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
143 (void)signal(SIGINT, catch);
144 if (preen)
145 (void)signal(SIGQUIT, catchquit);
146 signal(SIGINFO, infohandler);
147 /*
148 * Push up our allowed memory limit so we can cope
149 * with huge filesystems.
150 */
151 if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
152 rlimit.rlim_cur = rlimit.rlim_max;
153 (void)setrlimit(RLIMIT_DATA, &rlimit);
154 }
155 while (argc-- > 0)
156 (void)checkfilesys(*argv++);
157
158 if (returntosingle)
159 ret = 2;
160 exit(ret);
161}
162
163static int
164argtoi(int flag, char *req, char *str, int base)
165{
166 char *cp;
167 int ret;
168
169 ret = (int)strtol(str, &cp, base);
170 if (cp == str || *cp)
171 errx(EEXIT, "-%c flag requires a %s", flag, req);
172 return (ret);
173}
174
175/*
176 * Check the specified filesystem.
177 */
178/* ARGSUSED */
179static int
180checkfilesys(char *filesys)
181{
182 ufs_daddr_t n_ffree, n_bfree;
183 struct ufs_args args;
184 struct dups *dp;
185 struct statfs *mntp;
186 struct zlncnt *zlnp;
187 ufs_daddr_t blks;
188 ufs_daddr_t files;
189 int cylno, size;
190
191 cdevname = filesys;
192 if (debug && preen)
193 pwarn("starting\n");
194 /*
195 * Make best effort to get the disk name. Check first to see
196 * if it is listed among the mounted filesystems. Failing that
197 * check to see if it is listed in /etc/fstab.
198 */
199 mntp = getmntpt(filesys);
200 if (mntp != NULL)
201 filesys = mntp->f_mntfromname;
202 else
203 filesys = blockcheck(filesys);
204 /*
205 * If -F flag specified, check to see whether a background check
206 * is possible and needed. If possible and needed, exit with
207 * status zero. Otherwise exit with status non-zero. A non-zero
208 * exit status will cause a foreground check to be run.
209 */
210 sblock_init();
211 if (bkgrdcheck) {
212 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
213 exit(3); /* Cannot read superblock */
214 close(fsreadfd);
215 if (sblock.fs_flags & FS_NEEDSFSCK)
216 exit(4); /* Earlier background failed */
217 if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
218 exit(5); /* Not running soft updates */
219 size = MIBSIZE;
220 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
221 exit(6); /* Lacks kernel support */
222 if ((mntp == NULL && sblock.fs_clean == 1) ||
223 (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
224 exit(7); /* Filesystem clean, report it now */
225 exit(0);
226 }
227 /*
228 * If we are to do a background check:
229 * Get the mount point information of the filesystem
230 * create snapshot file
231 * return created snapshot file
232 * if not found, clear bkgrdflag and proceed with normal fsck
233 */
234 if (bkgrdflag) {
235 if (mntp == NULL) {
236 bkgrdflag = 0;
237 pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
238 } else if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
239 bkgrdflag = 0;
240 pfatal("NOT USING SOFT UPDATES, %s\n",
241 "CANNOT RUN IN BACKGROUND");
242 } else if ((mntp->f_flags & MNT_RDONLY) != 0) {
243 bkgrdflag = 0;
244 pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
245 } else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) {
246 if (readsb(0) != 0) {
247 if (sblock.fs_flags & FS_NEEDSFSCK) {
248 bkgrdflag = 0;
249 pfatal("UNEXPECTED INCONSISTENCY, %s\n",
250 "CANNOT RUN IN BACKGROUND\n");
251 }
252 if ((sblock.fs_flags & FS_UNCLEAN) == 0 &&
253 skipclean && preen) {
254 /*
255 * filesystem is clean;
256 * skip snapshot and report it clean
257 */
258 pwarn("FILESYSTEM CLEAN; %s\n",
259 "SKIPPING CHECKS");
260 goto clean;
261 }
262 }
263 close(fsreadfd);
264 }
265 if (bkgrdflag) {
266 snprintf(snapname, sizeof snapname, "%s/.fsck_snapshot",
267 mntp->f_mntonname);
268 args.fspec = snapname;
269 while (mount("ffs", mntp->f_mntonname,
270 mntp->f_flags | MNT_UPDATE | MNT_SNAPSHOT,
271 &args) < 0) {
272 if (errno == EEXIST && unlink(snapname) == 0)
273 continue;
274 bkgrdflag = 0;
275 pfatal("CANNOT CREATE SNAPSHOT %s: %s\n",
276 snapname, strerror(errno));
277 break;
278 }
279 if (bkgrdflag != 0)
280 filesys = snapname;
281 }
282 }
283
284 switch (setup(filesys)) {
285 case 0:
286 if (preen)
287 pfatal("CAN'T CHECK FILE SYSTEM.");
288 return (0);
289 case -1:
290 clean:
291 pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
292 sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
293 printf("(%d frags, %d blocks, %.1f%% fragmentation)\n",
294 sblock.fs_cstotal.cs_nffree, sblock.fs_cstotal.cs_nbfree,
295 sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
296 return (0);
297 }
298
299 /*
300 * Cleared if any questions answered no. Used to decide if
301 * the superblock should be marked clean.
302 */
303 resolved = 1;
304 /*
305 * 1: scan inodes tallying blocks used
306 */
307 if (preen == 0) {
308 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
309 if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
310 printf("** Root file system\n");
311 printf("** Phase 1 - Check Blocks and Sizes\n");
312 }
313 pass1();
314
315 /*
316 * 1b: locate first references to duplicates, if any
317 */
318 if (duplist) {
319 if (preen || usedsoftdep)
320 pfatal("INTERNAL ERROR: dups with -p");
321 printf("** Phase 1b - Rescan For More DUPS\n");
322 pass1b();
323 }
324
325 /*
326 * 2: traverse directories from root to mark all connected directories
327 */
328 if (preen == 0)
329 printf("** Phase 2 - Check Pathnames\n");
330 pass2();
331
332 /*
333 * 3: scan inodes looking for disconnected directories
334 */
335 if (preen == 0)
336 printf("** Phase 3 - Check Connectivity\n");
337 pass3();
338
339 /*
340 * 4: scan inodes looking for disconnected files; check reference counts
341 */
342 if (preen == 0)
343 printf("** Phase 4 - Check Reference Counts\n");
344 pass4();
345
346 /*
347 * 5: check and repair resource counts in cylinder groups
348 */
349 if (preen == 0)
350 printf("** Phase 5 - Check Cyl groups\n");
351 pass5();
352
353 /*
354 * print out summary statistics
355 */
356 n_ffree = sblock.fs_cstotal.cs_nffree;
357 n_bfree = sblock.fs_cstotal.cs_nbfree;
358 files = maxino - ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
359 blks = n_blks +
360 sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
361 blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
362 blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
363 blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
364 if (bkgrdflag && (files > 0 || blks > 0)) {
365 countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
366 pwarn("Reclaimed: %ld directories, %ld files, %d fragments\n",
367 countdirs, (long)files - countdirs, blks);
368 }
369 pwarn("%ld files, %ld used, %ld free ",
370 (long)n_files, (long)n_blks, (long)(n_ffree +
371 sblock.fs_frag * n_bfree));
372 printf("(%d frags, %d blocks, %.1f%% fragmentation)\n",
373 n_ffree, n_bfree, n_ffree * 100.0 / sblock.fs_dsize);
374 if (debug) {
375 if (files < 0)
376 printf("%d inodes missing\n", -files);
377 if (blks < 0)
378 printf("%d blocks missing\n", -blks);
379 if (duplist != NULL) {
380 printf("The following duplicate blocks remain:");
381 for (dp = duplist; dp; dp = dp->next)
382 printf(" %d,", dp->dup);
383 printf("\n");
384 }
385 if (zlnhead != NULL) {
386 printf("The following zero link count inodes remain:");
387 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
388 printf(" %u,", zlnp->zlncnt);
389 printf("\n");
390 }
391 }
392 zlnhead = (struct zlncnt *)0;
393 duplist = (struct dups *)0;
394 muldup = (struct dups *)0;
395 inocleanup();
396 if (fsmodified) {
397 sblock.fs_time = time(NULL);
398 sbdirty();
399 }
400 if (cvtlevel && sblk.b_dirty) {
401 /*
402 * Write out the duplicate super blocks
403 */
404 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
405 bwrite(fswritefd, (char *)&sblock,
406 fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
407 }
408 if (rerun)
409 resolved = 0;
410
411 /*
412 * Check to see if the filesystem is mounted read-write.
413 */
414 if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
415 resolved = 0;
416 ckfini(resolved);
417
418 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
419 if (inostathead[cylno].il_stat != NULL)
420 free((char *)inostathead[cylno].il_stat);
421 free((char *)inostathead);
422 inostathead = NULL;
423 if (fsmodified && !preen)
424 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
425 if (rerun)
426 printf("\n***** PLEASE RERUN FSCK *****\n");
427 if (mntp != NULL) {
428 struct ufs_args args;
429 int ret;
430 /*
431 * We modified a mounted filesystem. Do a mount update on
432 * it unless it is read-write, so we can continue using it
433 * as safely as possible.
434 */
435 if (mntp->f_flags & MNT_RDONLY) {
436 args.fspec = 0;
437 args.export.ex_flags = 0;
438 args.export.ex_root = 0;
439 ret = mount("ufs", mntp->f_mntonname,
440 mntp->f_flags | MNT_UPDATE | MNT_RELOAD, &args);
441 if (ret == 0)
442 return (0);
443 pwarn("mount reload of '%s' failed: %s\n\n",
444 mntp->f_mntonname, strerror(errno));
445 }
446 if (!fsmodified)
447 return (0);
448 if (!preen)
449 printf("\n***** REBOOT NOW *****\n");
450 sync();
451 return (4);
452 }
453 return (0);
454}
455
456/*
457 * Get the mount point information for name.
458 */
459static struct statfs *
460getmntpt(const char *name)
461{
462 struct stat devstat, mntdevstat;
463 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
464 char *devname;
465 struct statfs *mntbuf, *statfsp;
466 int i, mntsize, isdev;
467
468 if (stat(name, &devstat) != 0)
469 return (NULL);
470 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
471 isdev = 1;
472 else
473 isdev = 0;
474 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
475 for (i = 0; i < mntsize; i++) {
476 statfsp = &mntbuf[i];
477 devname = statfsp->f_mntfromname;
478 if (*devname != '/') {
479 strcpy(device, _PATH_DEV);
480 strcat(device, devname);
481 strcpy(statfsp->f_mntfromname, device);
482 }
483 if (isdev == 0) {
484 if (strcmp(name, statfsp->f_mntonname))
485 continue;
486 return (statfsp);
487 }
488 if (stat(devname, &mntdevstat) == 0 &&
489 mntdevstat.st_rdev == devstat.st_rdev)
490 return (statfsp);
491 }
492 statfsp = NULL;
493 return (statfsp);
494}
495
496static void
497usage(void)
498{
499 extern char *__progname;
500
501 (void) fprintf(stderr,
502 "Usage: %s [-BFpfny] [-b block] [-c level] [-m mode] "
503 "filesystem ...\n",
504 __progname);
505 exit(1);
506}
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1980, 1986, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95";
43#endif
44static const char rcsid[] =
45 "$FreeBSD: head/sbin/fsck_ffs/main.c 92839 2002-03-20 22:57:10Z imp $";
46#endif /* not lint */
47
48#include <sys/param.h>
49#include <sys/stat.h>
50#include <sys/file.h>
51#include <sys/time.h>
52#include <sys/mount.h>
53#include <sys/resource.h>
54#include <sys/sysctl.h>
55
56#include <ufs/ufs/dinode.h>
57#include <ufs/ufs/ufsmount.h>
58#include <ufs/ffs/fs.h>
59
60#include <err.h>
61#include <errno.h>
62#include <fstab.h>
63#include <paths.h>
64#include <string.h>
65
66#include "fsck.h"
67
68static void usage(void) __dead2;
69static int argtoi(int flag, char *req, char *str, int base);
70static int checkfilesys(char *filesys);
71static struct statfs *getmntpt(const char *);
72
73int
74main(int argc, char *argv[])
75{
76 int ch;
77 struct rlimit rlimit;
78 int ret = 0;
79
80 sync();
81 skipclean = 1;
82 while ((ch = getopt(argc, argv, "b:Bc:dfFm:npy")) != -1) {
83 switch (ch) {
84 case 'b':
85 skipclean = 0;
86 bflag = argtoi('b', "number", optarg, 10);
87 printf("Alternate super block location: %d\n", bflag);
88 break;
89
90 case 'B':
91 bkgrdflag = 1;
92 break;
93
94 case 'c':
95 skipclean = 0;
96 cvtlevel = argtoi('c', "conversion level", optarg, 10);
97 break;
98
99 case 'd':
100 debug++;
101 break;
102
103 case 'f':
104 skipclean = 0;
105 break;
106
107 case 'F':
108 bkgrdcheck = 1;
109 break;
110
111 case 'm':
112 lfmode = argtoi('m', "mode", optarg, 8);
113 if (lfmode &~ 07777)
114 errx(EEXIT, "bad mode to -m: %o", lfmode);
115 printf("** lost+found creation mode %o\n", lfmode);
116 break;
117
118 case 'n':
119 nflag++;
120 yflag = 0;
121 break;
122
123 case 'p':
124 preen++;
125 break;
126
127 case 'y':
128 yflag++;
129 nflag = 0;
130 break;
131
132 default:
133 usage();
134 }
135 }
136 argc -= optind;
137 argv += optind;
138
139 if (!argc)
140 usage();
141
142 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
143 (void)signal(SIGINT, catch);
144 if (preen)
145 (void)signal(SIGQUIT, catchquit);
146 signal(SIGINFO, infohandler);
147 /*
148 * Push up our allowed memory limit so we can cope
149 * with huge filesystems.
150 */
151 if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
152 rlimit.rlim_cur = rlimit.rlim_max;
153 (void)setrlimit(RLIMIT_DATA, &rlimit);
154 }
155 while (argc-- > 0)
156 (void)checkfilesys(*argv++);
157
158 if (returntosingle)
159 ret = 2;
160 exit(ret);
161}
162
163static int
164argtoi(int flag, char *req, char *str, int base)
165{
166 char *cp;
167 int ret;
168
169 ret = (int)strtol(str, &cp, base);
170 if (cp == str || *cp)
171 errx(EEXIT, "-%c flag requires a %s", flag, req);
172 return (ret);
173}
174
175/*
176 * Check the specified filesystem.
177 */
178/* ARGSUSED */
179static int
180checkfilesys(char *filesys)
181{
182 ufs_daddr_t n_ffree, n_bfree;
183 struct ufs_args args;
184 struct dups *dp;
185 struct statfs *mntp;
186 struct zlncnt *zlnp;
187 ufs_daddr_t blks;
188 ufs_daddr_t files;
189 int cylno, size;
190
191 cdevname = filesys;
192 if (debug && preen)
193 pwarn("starting\n");
194 /*
195 * Make best effort to get the disk name. Check first to see
196 * if it is listed among the mounted filesystems. Failing that
197 * check to see if it is listed in /etc/fstab.
198 */
199 mntp = getmntpt(filesys);
200 if (mntp != NULL)
201 filesys = mntp->f_mntfromname;
202 else
203 filesys = blockcheck(filesys);
204 /*
205 * If -F flag specified, check to see whether a background check
206 * is possible and needed. If possible and needed, exit with
207 * status zero. Otherwise exit with status non-zero. A non-zero
208 * exit status will cause a foreground check to be run.
209 */
210 sblock_init();
211 if (bkgrdcheck) {
212 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
213 exit(3); /* Cannot read superblock */
214 close(fsreadfd);
215 if (sblock.fs_flags & FS_NEEDSFSCK)
216 exit(4); /* Earlier background failed */
217 if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
218 exit(5); /* Not running soft updates */
219 size = MIBSIZE;
220 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
221 exit(6); /* Lacks kernel support */
222 if ((mntp == NULL && sblock.fs_clean == 1) ||
223 (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
224 exit(7); /* Filesystem clean, report it now */
225 exit(0);
226 }
227 /*
228 * If we are to do a background check:
229 * Get the mount point information of the filesystem
230 * create snapshot file
231 * return created snapshot file
232 * if not found, clear bkgrdflag and proceed with normal fsck
233 */
234 if (bkgrdflag) {
235 if (mntp == NULL) {
236 bkgrdflag = 0;
237 pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
238 } else if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
239 bkgrdflag = 0;
240 pfatal("NOT USING SOFT UPDATES, %s\n",
241 "CANNOT RUN IN BACKGROUND");
242 } else if ((mntp->f_flags & MNT_RDONLY) != 0) {
243 bkgrdflag = 0;
244 pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
245 } else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) {
246 if (readsb(0) != 0) {
247 if (sblock.fs_flags & FS_NEEDSFSCK) {
248 bkgrdflag = 0;
249 pfatal("UNEXPECTED INCONSISTENCY, %s\n",
250 "CANNOT RUN IN BACKGROUND\n");
251 }
252 if ((sblock.fs_flags & FS_UNCLEAN) == 0 &&
253 skipclean && preen) {
254 /*
255 * filesystem is clean;
256 * skip snapshot and report it clean
257 */
258 pwarn("FILESYSTEM CLEAN; %s\n",
259 "SKIPPING CHECKS");
260 goto clean;
261 }
262 }
263 close(fsreadfd);
264 }
265 if (bkgrdflag) {
266 snprintf(snapname, sizeof snapname, "%s/.fsck_snapshot",
267 mntp->f_mntonname);
268 args.fspec = snapname;
269 while (mount("ffs", mntp->f_mntonname,
270 mntp->f_flags | MNT_UPDATE | MNT_SNAPSHOT,
271 &args) < 0) {
272 if (errno == EEXIST && unlink(snapname) == 0)
273 continue;
274 bkgrdflag = 0;
275 pfatal("CANNOT CREATE SNAPSHOT %s: %s\n",
276 snapname, strerror(errno));
277 break;
278 }
279 if (bkgrdflag != 0)
280 filesys = snapname;
281 }
282 }
283
284 switch (setup(filesys)) {
285 case 0:
286 if (preen)
287 pfatal("CAN'T CHECK FILE SYSTEM.");
288 return (0);
289 case -1:
290 clean:
291 pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
292 sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
293 printf("(%d frags, %d blocks, %.1f%% fragmentation)\n",
294 sblock.fs_cstotal.cs_nffree, sblock.fs_cstotal.cs_nbfree,
295 sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
296 return (0);
297 }
298
299 /*
300 * Cleared if any questions answered no. Used to decide if
301 * the superblock should be marked clean.
302 */
303 resolved = 1;
304 /*
305 * 1: scan inodes tallying blocks used
306 */
307 if (preen == 0) {
308 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
309 if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
310 printf("** Root file system\n");
311 printf("** Phase 1 - Check Blocks and Sizes\n");
312 }
313 pass1();
314
315 /*
316 * 1b: locate first references to duplicates, if any
317 */
318 if (duplist) {
319 if (preen || usedsoftdep)
320 pfatal("INTERNAL ERROR: dups with -p");
321 printf("** Phase 1b - Rescan For More DUPS\n");
322 pass1b();
323 }
324
325 /*
326 * 2: traverse directories from root to mark all connected directories
327 */
328 if (preen == 0)
329 printf("** Phase 2 - Check Pathnames\n");
330 pass2();
331
332 /*
333 * 3: scan inodes looking for disconnected directories
334 */
335 if (preen == 0)
336 printf("** Phase 3 - Check Connectivity\n");
337 pass3();
338
339 /*
340 * 4: scan inodes looking for disconnected files; check reference counts
341 */
342 if (preen == 0)
343 printf("** Phase 4 - Check Reference Counts\n");
344 pass4();
345
346 /*
347 * 5: check and repair resource counts in cylinder groups
348 */
349 if (preen == 0)
350 printf("** Phase 5 - Check Cyl groups\n");
351 pass5();
352
353 /*
354 * print out summary statistics
355 */
356 n_ffree = sblock.fs_cstotal.cs_nffree;
357 n_bfree = sblock.fs_cstotal.cs_nbfree;
358 files = maxino - ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
359 blks = n_blks +
360 sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
361 blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
362 blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
363 blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
364 if (bkgrdflag && (files > 0 || blks > 0)) {
365 countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
366 pwarn("Reclaimed: %ld directories, %ld files, %d fragments\n",
367 countdirs, (long)files - countdirs, blks);
368 }
369 pwarn("%ld files, %ld used, %ld free ",
370 (long)n_files, (long)n_blks, (long)(n_ffree +
371 sblock.fs_frag * n_bfree));
372 printf("(%d frags, %d blocks, %.1f%% fragmentation)\n",
373 n_ffree, n_bfree, n_ffree * 100.0 / sblock.fs_dsize);
374 if (debug) {
375 if (files < 0)
376 printf("%d inodes missing\n", -files);
377 if (blks < 0)
378 printf("%d blocks missing\n", -blks);
379 if (duplist != NULL) {
380 printf("The following duplicate blocks remain:");
381 for (dp = duplist; dp; dp = dp->next)
382 printf(" %d,", dp->dup);
383 printf("\n");
384 }
385 if (zlnhead != NULL) {
386 printf("The following zero link count inodes remain:");
387 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
388 printf(" %u,", zlnp->zlncnt);
389 printf("\n");
390 }
391 }
392 zlnhead = (struct zlncnt *)0;
393 duplist = (struct dups *)0;
394 muldup = (struct dups *)0;
395 inocleanup();
396 if (fsmodified) {
397 sblock.fs_time = time(NULL);
398 sbdirty();
399 }
400 if (cvtlevel && sblk.b_dirty) {
401 /*
402 * Write out the duplicate super blocks
403 */
404 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
405 bwrite(fswritefd, (char *)&sblock,
406 fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
407 }
408 if (rerun)
409 resolved = 0;
410
411 /*
412 * Check to see if the filesystem is mounted read-write.
413 */
414 if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
415 resolved = 0;
416 ckfini(resolved);
417
418 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
419 if (inostathead[cylno].il_stat != NULL)
420 free((char *)inostathead[cylno].il_stat);
421 free((char *)inostathead);
422 inostathead = NULL;
423 if (fsmodified && !preen)
424 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
425 if (rerun)
426 printf("\n***** PLEASE RERUN FSCK *****\n");
427 if (mntp != NULL) {
428 struct ufs_args args;
429 int ret;
430 /*
431 * We modified a mounted filesystem. Do a mount update on
432 * it unless it is read-write, so we can continue using it
433 * as safely as possible.
434 */
435 if (mntp->f_flags & MNT_RDONLY) {
436 args.fspec = 0;
437 args.export.ex_flags = 0;
438 args.export.ex_root = 0;
439 ret = mount("ufs", mntp->f_mntonname,
440 mntp->f_flags | MNT_UPDATE | MNT_RELOAD, &args);
441 if (ret == 0)
442 return (0);
443 pwarn("mount reload of '%s' failed: %s\n\n",
444 mntp->f_mntonname, strerror(errno));
445 }
446 if (!fsmodified)
447 return (0);
448 if (!preen)
449 printf("\n***** REBOOT NOW *****\n");
450 sync();
451 return (4);
452 }
453 return (0);
454}
455
456/*
457 * Get the mount point information for name.
458 */
459static struct statfs *
460getmntpt(const char *name)
461{
462 struct stat devstat, mntdevstat;
463 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
464 char *devname;
465 struct statfs *mntbuf, *statfsp;
466 int i, mntsize, isdev;
467
468 if (stat(name, &devstat) != 0)
469 return (NULL);
470 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
471 isdev = 1;
472 else
473 isdev = 0;
474 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
475 for (i = 0; i < mntsize; i++) {
476 statfsp = &mntbuf[i];
477 devname = statfsp->f_mntfromname;
478 if (*devname != '/') {
479 strcpy(device, _PATH_DEV);
480 strcat(device, devname);
481 strcpy(statfsp->f_mntfromname, device);
482 }
483 if (isdev == 0) {
484 if (strcmp(name, statfsp->f_mntonname))
485 continue;
486 return (statfsp);
487 }
488 if (stat(devname, &mntdevstat) == 0 &&
489 mntdevstat.st_rdev == devstat.st_rdev)
490 return (statfsp);
491 }
492 statfsp = NULL;
493 return (statfsp);
494}
495
496static void
497usage(void)
498{
499 extern char *__progname;
500
501 (void) fprintf(stderr,
502 "Usage: %s [-BFpfny] [-b block] [-c level] [-m mode] "
503 "filesystem ...\n",
504 __progname);
505 exit(1);
506}