Remove a check that is always false.
[dragonfly.git] / sbin / fsck / 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  * 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  * @(#) Copyright (c) 1980, 1986, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)main.c   8.6 (Berkeley) 5/14/95
35  * $FreeBSD: src/sbin/fsck/main.c,v 1.21.2.1 2001/01/23 23:11:07 iedowse Exp $
36  * $DragonFly: src/sbin/fsck/main.c,v 1.9 2005/11/06 12:13:53 swildner Exp $
37  */
38
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <sys/mount.h>
43 #include <sys/resource.h>
44
45 #include <vfs/ufs/dinode.h>
46 #include <vfs/ufs/ufsmount.h>
47 #include <vfs/ufs/fs.h>
48
49 #include <err.h>
50 #include <errno.h>
51 #include <fstab.h>
52 #include <paths.h>
53 #include <string.h>
54
55 #include "fsck.h"
56
57 static int argtoi(int flag, char *req, char *str, int base);
58 static int docheck(struct fstab *fsp);
59 static int checkfilesys(char *filesys, char *mntpt, long auxdata,
60                 int child);
61 static struct statfs *getmntpt(const char *);
62 int main(int argc, char *argv[]);
63
64 int
65 main(int argc, char **argv)
66 {
67         int ch;
68         int ret, maxrun = 0;
69         struct rlimit rlimit;
70
71         sync();
72         while ((ch = getopt(argc, argv, "dfpnNyYb:c:l:m:")) != -1) {
73                 switch (ch) {
74                 case 'p':
75                         preen++;
76                         break;
77
78                 case 'b':
79                         bflag = argtoi('b', "number", optarg, 10);
80                         printf("Alternate super block location: %d\n", bflag);
81                         break;
82
83                 case 'c':
84                         cvtlevel = argtoi('c', "conversion level", optarg, 10);
85                         break;
86
87                 case 'd':
88                         debug++;
89                         break;
90
91                 case 'f':
92                         fflag++;
93                         break;
94
95                 case 'l':
96                         maxrun = argtoi('l', "number", optarg, 10);
97                         break;
98
99                 case 'm':
100                         lfmode = argtoi('m', "mode", optarg, 8);
101                         if (lfmode &~ 07777)
102                                 errx(EEXIT, "bad mode to -m: %o", lfmode);
103                         printf("** lost+found creation mode %o\n", lfmode);
104                         break;
105
106                 case 'n':
107                 case 'N':
108                         nflag++;
109                         yflag = 0;
110                         break;
111
112                 case 'y':
113                 case 'Y':
114                         yflag++;
115                         nflag = 0;
116                         break;
117
118                 default:
119                         errx(EEXIT, "%c option?", ch);
120                 }
121         }
122         argc -= optind;
123         argv += optind;
124         if (signal(SIGINT, SIG_IGN) != SIG_IGN)
125                 signal(SIGINT, catch);
126         if (preen)
127                 signal(SIGQUIT, catchquit);
128         signal(SIGINFO, infohandler);
129         /*
130          * Push up our allowed memory limit so we can cope
131          * with huge filesystems.
132          */
133         if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
134                 rlimit.rlim_cur = rlimit.rlim_max;
135                 setrlimit(RLIMIT_DATA, &rlimit);
136         }
137         if (argc) {
138                 while (argc-- > 0) {
139                         char *path = blockcheck(*argv);
140
141                         if (path == NULL)
142                                 pfatal("Can't check %s\n", *argv);
143                         else
144                                 checkfilesys(path, 0, 0L, 0);
145                         ++argv;
146                 }
147                 exit(0);
148         }
149         ret = checkfstab(preen, maxrun, docheck, checkfilesys);
150         if (returntosingle)
151                 exit(2);
152         exit(ret);
153 }
154
155 static int
156 argtoi(int flag, char *req, char *str, int base)
157 {
158         char *cp;
159         int ret;
160
161         ret = (int)strtol(str, &cp, base);
162         if (cp == str || *cp)
163                 errx(EEXIT, "-%c flag requires a %s", flag, req);
164         return (ret);
165 }
166
167 /*
168  * Determine whether a filesystem should be checked.
169  */
170 static int
171 docheck(struct fstab *fsp)
172 {
173
174         if (strcmp(fsp->fs_vfstype, "ufs") ||
175             (strcmp(fsp->fs_type, FSTAB_RW) &&
176              strcmp(fsp->fs_type, FSTAB_RO)) ||
177             fsp->fs_passno == 0)
178                 return (0);
179         return (1);
180 }
181
182 /*
183  * Check the specified filesystem.
184  */
185 /* ARGSUSED */
186 static int
187 checkfilesys(char *filesys, char *mntpt, long auxdata, int child)
188 {
189         ufs_daddr_t n_ffree, n_bfree;
190         struct dups *dp;
191         struct statfs *mntbuf;
192         struct zlncnt *zlnp;
193         int cylno;
194
195         if (preen && child)
196                 signal(SIGQUIT, voidquit);
197         cdevname = filesys;
198         if (debug && preen)
199                 pwarn("starting\n");
200         switch (setup(filesys)) {
201         case 0:
202                 if (preen)
203                         pfatal("CAN'T CHECK FILE SYSTEM.");
204                 return (0);
205         case -1:
206                 pwarn("clean, %ld free ", sblock.fs_cstotal.cs_nffree +
207                     sblock.fs_frag * sblock.fs_cstotal.cs_nbfree);
208                 printf("(%d frags, %d blocks, %.1f%% fragmentation)\n",
209                     sblock.fs_cstotal.cs_nffree, sblock.fs_cstotal.cs_nbfree,
210                     sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
211                 return (0);
212         }
213         got_siginfo = 0;
214
215         /*
216          * Get the mount point information of the filesystem, if
217          * it is available.
218          */
219         mntbuf = getmntpt(filesys);
220         
221         /*
222          * Cleared if any questions answered no. Used to decide if
223          * the superblock should be marked clean.
224          */
225         resolved = 1;
226         /*
227          * 1: scan inodes tallying blocks used
228          */
229         if (preen == 0) {
230                 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
231                 if (mntbuf != NULL && mntbuf->f_flags & MNT_ROOTFS)
232                         printf("** Root file system\n");
233                 printf("** Phase 1 - Check Blocks and Sizes\n");
234         }
235         pass1();
236
237         /*
238          * 1b: locate first references to duplicates, if any
239          */
240         if (duplist) {
241                 if (preen || usedsoftdep)
242                         pfatal("INTERNAL ERROR: dups with -p");
243                 printf("** Phase 1b - Rescan For More DUPS\n");
244                 pass1b();
245         }
246
247         /*
248          * 2: traverse directories from root to mark all connected directories
249          */
250         if (preen == 0)
251                 printf("** Phase 2 - Check Pathnames\n");
252         pass2();
253
254         /*
255          * 3: scan inodes looking for disconnected directories
256          */
257         if (preen == 0)
258                 printf("** Phase 3 - Check Connectivity\n");
259         pass3();
260
261         /*
262          * 4: scan inodes looking for disconnected files; check reference counts
263          */
264         if (preen == 0)
265                 printf("** Phase 4 - Check Reference Counts\n");
266         pass4();
267
268         /*
269          * 5: check and repair resource counts in cylinder groups
270          */
271         if (preen == 0)
272                 printf("** Phase 5 - Check Cyl groups\n");
273         pass5();
274
275         /*
276          * print out summary statistics
277          */
278         n_ffree = sblock.fs_cstotal.cs_nffree;
279         n_bfree = sblock.fs_cstotal.cs_nbfree;
280         pwarn("%ld files, %ld used, %ld free ",
281             n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
282         printf("(%d frags, %d blocks, %.1f%% fragmentation)\n",
283             n_ffree, n_bfree, n_ffree * 100.0 / sblock.fs_dsize);
284         if (debug &&
285             (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
286                 printf("%d files missing\n", n_files);
287         if (debug) {
288                 n_blks += sblock.fs_ncg *
289                         (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
290                 n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
291                 n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
292                 if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
293                         printf("%d blocks missing\n", n_blks);
294                 if (duplist != NULL) {
295                         printf("The following duplicate blocks remain:");
296                         for (dp = duplist; dp; dp = dp->next)
297                                 printf(" %d,", dp->dup);
298                         printf("\n");
299                 }
300                 if (zlnhead != NULL) {
301                         printf("The following zero link count inodes remain:");
302                         for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
303                                 printf(" %u,", zlnp->zlncnt);
304                         printf("\n");
305                 }
306         }
307         zlnhead = (struct zlncnt *)0;
308         duplist = (struct dups *)0;
309         muldup = (struct dups *)0;
310         inocleanup();
311         if (fsmodified) {
312                 sblock.fs_time = time(NULL);
313                 sbdirty();
314         }
315         if (cvtlevel && sblk.b_dirty) {
316                 /*
317                  * Write out the duplicate super blocks
318                  */
319                 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
320                         bwrite(fswritefd, (char *)&sblock,
321                             fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
322         }
323         if (rerun)
324                 resolved = 0;
325
326         /*
327          * Check to see if the filesystem is mounted read-write.
328          */
329         if (mntbuf != NULL && (mntbuf->f_flags & MNT_RDONLY) == 0)
330                 resolved = 0;
331         ckfini(resolved);
332
333         for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
334                 if (inostathead[cylno].il_stat != NULL)
335                         free((char *)inostathead[cylno].il_stat);
336         free((char *)inostathead);
337         inostathead = NULL;
338         if (fsmodified && !preen)
339                 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
340         if (rerun)
341                 printf("\n***** PLEASE RERUN FSCK *****\n");
342         if (mntbuf != NULL) {
343                 struct ufs_args args;
344                 int ret;
345                 /*
346                  * We modified a mounted filesystem.  Do a mount update on
347                  * it unless it is read-write, so we can continue using it
348                  * as safely as possible.
349                  */
350                 if (mntbuf->f_flags & MNT_RDONLY) {
351                         args.fspec = 0;
352                         args.export.ex_flags = 0;
353                         args.export.ex_root = 0;
354                         ret = mount("ufs", mntbuf->f_mntonname,
355                             mntbuf->f_flags | MNT_UPDATE | MNT_RELOAD, &args);
356                         if (ret == 0)
357                                 return (0);
358                         pwarn("mount reload of '%s' failed: %s\n\n",
359                             mntbuf->f_mntonname, strerror(errno));
360                 }
361                 if (!fsmodified)
362                         return (0);
363                 if (!preen)
364                         printf("\n***** REBOOT NOW *****\n");
365                 sync();
366                 return (4);
367         }
368         return (0);
369 }
370
371 /*
372  * Get the directory that the device is mounted on.
373  */
374 static struct statfs *
375 getmntpt(const char *name)
376 {
377         struct stat devstat, mntdevstat;
378         char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
379         char *devname;
380         struct statfs *mntbuf;
381         int i, mntsize;
382
383         if (stat(name, &devstat) != 0 ||
384             !(S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode)))
385                 return (NULL);
386         mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
387         for (i = 0; i < mntsize; i++) {
388                 if (strcmp(mntbuf[i].f_fstypename, "ufs") != 0)
389                         continue;
390                 devname = mntbuf[i].f_mntfromname;
391                 if (*devname != '/') {
392                         strcpy(device, _PATH_DEV);
393                         strcat(device, devname);
394                         devname = device;
395                 }
396                 if (stat(devname, &mntdevstat) == 0 &&
397                     mntdevstat.st_rdev == devstat.st_rdev)
398                         return (&mntbuf[i]);
399         }
400         return (NULL);
401 }