]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsck_ffs/main.c
This commit was generated by cvs2svn to compensate for changes in r154180,
[FreeBSD/FreeBSD.git] / sbin / fsck_ffs / main.c
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  * 4. 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
30 #if 0
31 #ifndef lint
32 static const char copyright[] =
33 "@(#) Copyright (c) 1980, 1986, 1993\n\
34         The Regents of the University of California.  All rights reserved.\n";
35 #endif /* not lint */
36
37 #ifndef lint
38 static char sccsid[] = "@(#)main.c      8.6 (Berkeley) 5/14/95";
39 #endif /* not lint */
40 #endif
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 #include <sys/file.h>
47 #include <sys/time.h>
48 #include <sys/mount.h>
49 #include <sys/resource.h>
50 #include <sys/sysctl.h>
51 #include <sys/disklabel.h>
52
53 #include <ufs/ufs/dinode.h>
54 #include <ufs/ufs/ufsmount.h>
55 #include <ufs/ffs/fs.h>
56
57 #include <err.h>
58 #include <errno.h>
59 #include <fstab.h>
60 #include <grp.h>
61 #include <paths.h>
62 #include <stdint.h>
63 #include <string.h>
64
65 #include "fsck.h"
66
67 static void usage(void) __dead2;
68 static int argtoi(int flag, const char *req, const char *str, int base);
69 static int checkfilesys(char *filesys);
70 static struct statfs *getmntpt(const char *);
71
72 int
73 main(int argc, char *argv[])
74 {
75         int ch;
76         struct rlimit rlimit;
77         struct itimerval itimerval;
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                         if (cvtlevel < 3)
98                                 errx(EEXIT, "cannot do level %d conversion",
99                                     cvtlevel);
100                         break;
101
102                 case 'd':
103                         debug++;
104                         break;
105
106                 case 'f':
107                         skipclean = 0;
108                         break;
109
110                 case 'F':
111                         bkgrdcheck = 1;
112                         break;
113
114                 case 'm':
115                         lfmode = argtoi('m', "mode", optarg, 8);
116                         if (lfmode &~ 07777)
117                                 errx(EEXIT, "bad mode to -m: %o", lfmode);
118                         printf("** lost+found creation mode %o\n", lfmode);
119                         break;
120
121                 case 'n':
122                         nflag++;
123                         yflag = 0;
124                         break;
125
126                 case 'p':
127                         preen++;
128                         break;
129
130                 case 'y':
131                         yflag++;
132                         nflag = 0;
133                         break;
134
135                 default:
136                         usage();
137                 }
138         }
139         argc -= optind;
140         argv += optind;
141
142         if (!argc)
143                 usage();
144
145         if (signal(SIGINT, SIG_IGN) != SIG_IGN)
146                 (void)signal(SIGINT, catch);
147         if (preen)
148                 (void)signal(SIGQUIT, catchquit);
149         signal(SIGINFO, infohandler);
150         if (bkgrdflag) {
151                 signal(SIGALRM, alarmhandler);
152                 itimerval.it_interval.tv_sec = 5;
153                 itimerval.it_interval.tv_usec = 0;
154                 itimerval.it_value.tv_sec = 5;
155                 itimerval.it_value.tv_usec = 0;
156                 setitimer(ITIMER_REAL, &itimerval, NULL);
157         }
158         /*
159          * Push up our allowed memory limit so we can cope
160          * with huge file systems.
161          */
162         if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
163                 rlimit.rlim_cur = rlimit.rlim_max;
164                 (void)setrlimit(RLIMIT_DATA, &rlimit);
165         }
166         while (argc-- > 0)
167                 (void)checkfilesys(*argv++);
168
169         if (returntosingle)
170                 ret = 2;
171         exit(ret);
172 }
173
174 static int
175 argtoi(int flag, const char *req, const char *str, int base)
176 {
177         char *cp;
178         int ret;
179
180         ret = (int)strtol(str, &cp, base);
181         if (cp == str || *cp)
182                 errx(EEXIT, "-%c flag requires a %s", flag, req);
183         return (ret);
184 }
185
186 /*
187  * Check the specified file system.
188  */
189 /* ARGSUSED */
190 static int
191 checkfilesys(char *filesys)
192 {
193         ufs2_daddr_t n_ffree, n_bfree;
194         struct ufs_args args;
195         struct dups *dp;
196         struct statfs *mntp;
197         struct stat snapdir;
198         struct group *grp;
199         ufs2_daddr_t blks;
200         int cylno, ret;
201         ino_t files;
202         size_t size;
203
204         cdevname = filesys;
205         if (debug && preen)
206                 pwarn("starting\n");
207         /*
208          * Make best effort to get the disk name. Check first to see
209          * if it is listed among the mounted file systems. Failing that
210          * check to see if it is listed in /etc/fstab.
211          */
212         mntp = getmntpt(filesys);
213         if (mntp != NULL)
214                 filesys = mntp->f_mntfromname;
215         else
216                 filesys = blockcheck(filesys);
217         /*
218          * If -F flag specified, check to see whether a background check
219          * is possible and needed. If possible and needed, exit with
220          * status zero. Otherwise exit with status non-zero. A non-zero
221          * exit status will cause a foreground check to be run.
222          */
223         sblock_init();
224         if (bkgrdcheck) {
225                 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
226                         exit(3);        /* Cannot read superblock */
227                 close(fsreadfd);
228                 if (sblock.fs_flags & FS_NEEDSFSCK)
229                         exit(4);        /* Earlier background failed */
230                 if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
231                         exit(5);        /* Not running soft updates */
232                 size = MIBSIZE;
233                 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
234                         exit(6);        /* Lacks kernel support */
235                 if ((mntp == NULL && sblock.fs_clean == 1) ||
236                     (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
237                         exit(7);        /* Filesystem clean, report it now */
238                 exit(0);
239         }
240         /*
241          * If we are to do a background check:
242          *      Get the mount point information of the file system
243          *      create snapshot file
244          *      return created snapshot file
245          *      if not found, clear bkgrdflag and proceed with normal fsck
246          */
247         if (bkgrdflag) {
248                 if (mntp == NULL) {
249                         bkgrdflag = 0;
250                         pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
251                 } else if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
252                         bkgrdflag = 0;
253                         pfatal("NOT USING SOFT UPDATES, %s\n",
254                             "CANNOT RUN IN BACKGROUND");
255                 } else if ((mntp->f_flags & MNT_RDONLY) != 0) {
256                         bkgrdflag = 0;
257                         pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
258                 } else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) {
259                         if (readsb(0) != 0) {
260                                 if (sblock.fs_flags & FS_NEEDSFSCK) {
261                                         bkgrdflag = 0;
262                                         pfatal("UNEXPECTED INCONSISTENCY, %s\n",
263                                             "CANNOT RUN IN BACKGROUND\n");
264                                 }
265                                 if ((sblock.fs_flags & FS_UNCLEAN) == 0 &&
266                                     skipclean && preen) {
267                                         /*
268                                          * file system is clean;
269                                          * skip snapshot and report it clean
270                                          */
271                                         pwarn("FILE SYSTEM CLEAN; %s\n",
272                                             "SKIPPING CHECKS");
273                                         goto clean;
274                                 }
275                         }
276                         close(fsreadfd);
277                 }
278                 if (bkgrdflag) {
279                         snprintf(snapname, sizeof snapname, "%s/.snap",
280                             mntp->f_mntonname);
281                         if (stat(snapname, &snapdir) < 0) {
282                                 if (errno != ENOENT) {
283                                         bkgrdflag = 0;
284                                         pfatal("CANNOT FIND %s %s: %s, %s\n",
285                                             "SNAPSHOT DIRECTORY",
286                                             snapname, strerror(errno),
287                                             "CANNOT RUN IN BACKGROUND");
288                                 } else if ((grp = getgrnam("operator")) == 0 ||
289                                     mkdir(snapname, 0770) < 0 ||
290                                     chown(snapname, -1, grp->gr_gid) < 0 ||
291                                     chmod(snapname, 0770) < 0) {
292                                         bkgrdflag = 0;
293                                         pfatal("CANNOT CREATE %s %s: %s, %s\n",
294                                             "SNAPSHOT DIRECTORY",
295                                             snapname, strerror(errno),
296                                             "CANNOT RUN IN BACKGROUND");
297                                 }
298                         } else if (!S_ISDIR(snapdir.st_mode)) {
299                                 bkgrdflag = 0;
300                                 pfatal("%s IS NOT A DIRECTORY, %s\n", snapname,
301                                     "CANNOT RUN IN BACKGROUND");
302                         }
303                 }
304                 if (bkgrdflag) {
305                         snprintf(snapname, sizeof snapname,
306                             "%s/.snap/fsck_snapshot", mntp->f_mntonname);
307                         memset(&args, 0, sizeof args);
308                         args.fspec = snapname;
309                         while (mount("ffs", mntp->f_mntonname,
310                             mntp->f_flags | MNT_UPDATE | MNT_SNAPSHOT,
311                             &args) < 0) {
312                                 if (errno == EEXIST && unlink(snapname) == 0)
313                                         continue;
314                                 bkgrdflag = 0;
315                                 pfatal("CANNOT CREATE SNAPSHOT %s: %s\n",
316                                     snapname, strerror(errno));
317                                 break;
318                         }
319                         if (bkgrdflag != 0)
320                                 filesys = snapname;
321                 }
322         }
323
324         switch (setup(filesys)) {
325         case 0:
326                 if (preen)
327                         pfatal("CAN'T CHECK FILE SYSTEM.");
328                 return (0);
329         case -1:
330         clean:
331                 pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
332                     sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
333                 printf("(%lld frags, %lld blocks, %.1f%% fragmentation)\n",
334                     (long long)sblock.fs_cstotal.cs_nffree,
335                     (long long)sblock.fs_cstotal.cs_nbfree,
336                     sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
337                 return (0);
338         }
339         
340         /*
341          * Cleared if any questions answered no. Used to decide if
342          * the superblock should be marked clean.
343          */
344         resolved = 1;
345         /*
346          * 1: scan inodes tallying blocks used
347          */
348         if (preen == 0) {
349                 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
350                 if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
351                         printf("** Root file system\n");
352                 printf("** Phase 1 - Check Blocks and Sizes\n");
353         }
354         pass1();
355
356         /*
357          * 1b: locate first references to duplicates, if any
358          */
359         if (duplist) {
360                 if (preen || usedsoftdep)
361                         pfatal("INTERNAL ERROR: dups with -p");
362                 printf("** Phase 1b - Rescan For More DUPS\n");
363                 pass1b();
364         }
365
366         /*
367          * 2: traverse directories from root to mark all connected directories
368          */
369         if (preen == 0)
370                 printf("** Phase 2 - Check Pathnames\n");
371         pass2();
372
373         /*
374          * 3: scan inodes looking for disconnected directories
375          */
376         if (preen == 0)
377                 printf("** Phase 3 - Check Connectivity\n");
378         pass3();
379
380         /*
381          * 4: scan inodes looking for disconnected files; check reference counts
382          */
383         if (preen == 0)
384                 printf("** Phase 4 - Check Reference Counts\n");
385         pass4();
386
387         /*
388          * 5: check and repair resource counts in cylinder groups
389          */
390         if (preen == 0)
391                 printf("** Phase 5 - Check Cyl groups\n");
392         pass5();
393
394         /*
395          * print out summary statistics
396          */
397         n_ffree = sblock.fs_cstotal.cs_nffree;
398         n_bfree = sblock.fs_cstotal.cs_nbfree;
399         files = maxino - ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
400         blks = n_blks +
401             sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
402         blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
403         blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
404         blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
405         if (bkgrdflag && (files > 0 || blks > 0)) {
406                 countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
407                 pwarn("Reclaimed: %ld directories, %ld files, %lld fragments\n",
408                     countdirs, (long)files - countdirs, (long long)blks);
409         }
410         pwarn("%ld files, %jd used, %ju free ",
411             (long)n_files, (intmax_t)n_blks,
412             (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
413         printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
414             (uintmax_t)n_ffree, (uintmax_t)n_bfree,
415             n_ffree * 100.0 / sblock.fs_dsize);
416         if (debug) {
417                 if (files < 0)
418                         printf("%d inodes missing\n", -files);
419                 if (blks < 0)
420                         printf("%lld blocks missing\n", -(long long)blks);
421                 if (duplist != NULL) {
422                         printf("The following duplicate blocks remain:");
423                         for (dp = duplist; dp; dp = dp->next)
424                                 printf(" %lld,", (long long)dp->dup);
425                         printf("\n");
426                 }
427         }
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 (cvtlevel && sblk.b_dirty) {
436                 /*
437                  * Write out the duplicate super blocks
438                  */
439                 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
440                         bwrite(fswritefd, (char *)&sblock,
441                             fsbtodb(&sblock, cgsblock(&sblock, cylno)),
442                             SBLOCKSIZE);
443         }
444         if (rerun)
445                 resolved = 0;
446
447         /*
448          * Check to see if the file system is mounted read-write.
449          */
450         if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
451                 resolved = 0;
452         ckfini(resolved);
453
454         for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
455                 if (inostathead[cylno].il_stat != NULL)
456                         free((char *)inostathead[cylno].il_stat);
457         free((char *)inostathead);
458         inostathead = NULL;
459         if (fsmodified && !preen)
460                 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
461         if (rerun)
462                 printf("\n***** PLEASE RERUN FSCK *****\n");
463         if (mntp != NULL) {
464                 /*
465                  * We modified a mounted file system.  Do a mount update on
466                  * it unless it is read-write, so we can continue using it
467                  * as safely as possible.
468                  */
469                 if (mntp->f_flags & MNT_RDONLY) {
470                         args.fspec = 0;
471                         args.export.ex_flags = 0;
472                         args.export.ex_root = 0;
473                         ret = mount("ufs", mntp->f_mntonname,
474                             mntp->f_flags | MNT_UPDATE | MNT_RELOAD, &args);
475                         if (ret == 0)
476                                 return (0);
477                         pwarn("mount reload of '%s' failed: %s\n\n",
478                             mntp->f_mntonname, strerror(errno));
479                 }
480                 if (!fsmodified)
481                         return (0);
482                 if (!preen)
483                         printf("\n***** REBOOT NOW *****\n");
484                 sync();
485                 return (4);
486         }
487         return (0);
488 }
489
490 /*
491  * Get the mount point information for name.
492  */
493 static struct statfs *
494 getmntpt(const char *name)
495 {
496         struct stat devstat, mntdevstat;
497         char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
498         char *ddevname;
499         struct statfs *mntbuf, *statfsp;
500         int i, mntsize, isdev;
501
502         if (stat(name, &devstat) != 0)
503                 return (NULL);
504         if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
505                 isdev = 1;
506         else
507                 isdev = 0;
508         mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
509         for (i = 0; i < mntsize; i++) {
510                 statfsp = &mntbuf[i];
511                 ddevname = statfsp->f_mntfromname;
512                 if (*ddevname != '/') {
513                         strcpy(device, _PATH_DEV);
514                         strcat(device, ddevname);
515                         strcpy(statfsp->f_mntfromname, device);
516                 }
517                 if (isdev == 0) {
518                         if (strcmp(name, statfsp->f_mntonname))
519                                 continue;
520                         return (statfsp);
521                 }
522                 if (stat(ddevname, &mntdevstat) == 0 &&
523                     mntdevstat.st_rdev == devstat.st_rdev)
524                         return (statfsp);
525         }
526         statfsp = NULL;
527         return (statfsp);
528 }
529
530 static void
531 usage(void)
532 {
533         (void) fprintf(stderr,
534             "usage: %s [-BFpfny] [-b block] [-c level] [-m mode] "
535                         "filesystem ...\n",
536             getprogname());
537         exit(1);
538 }