1 /*        $NetBSD: main.c,v 1.41 2020/04/05 15:25:39 joerg 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 /*
33  * Copyright (c) 1997 Manuel Bouyer.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  *
44  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
45  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
48  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
53  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54  */
55 
56 #include <sys/cdefs.h>
57 #ifndef lint
58 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\
59  The Regents of the University of California.  All rights reserved.");
60 #endif /* not lint */
61 
62 #ifndef lint
63 #if 0
64 static char sccsid[] = "@(#)main.c      8.2 (Berkeley) 1/23/94";
65 #else
66 __RCSID("$NetBSD: main.c,v 1.41 2020/04/05 15:25:39 joerg Exp $");
67 #endif
68 #endif /* not lint */
69 
70 #include <sys/param.h>
71 #include <sys/time.h>
72 #include <sys/mount.h>
73 #include <ufs/ufs/ufsmount.h>
74 #include <ufs/ext2fs/ext2fs_dinode.h>
75 #include <ufs/ext2fs/ext2fs.h>
76 #include <fstab.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #include <ctype.h>
80 #include <stdio.h>
81 #include <time.h>
82 #include <unistd.h>
83 #include <signal.h>
84 
85 #include "fsck.h"
86 #include "extern.h"
87 #include "fsutil.h"
88 #include "exitvalues.h"
89 
90 struct bufarea bufhead;
91 struct bufarea sblk;
92 struct bufarea asblk;
93 struct bufarea *pdirbp;
94 struct bufarea *pbp;
95 struct bufarea *getdatablk(daddr_t, long);
96 struct m_ext2fs sblock;
97 struct dups *duplist;
98 struct dups *muldup;
99 struct zlncnt *zlnhead;
100 struct inoinfo **inphead, **inpsort;
101 long numdirs, listmax, inplast;
102 long      dev_bsize;
103 long      secsize;
104 char      nflag;
105 char      yflag;
106 int       bflag;
107 int       Uflag;
108 int       debug;
109 int       preen;
110 char      havesb;
111 char      skipclean;
112 int       fsmodified;
113 int       fsreadfd;
114 int       fswritefd;
115 int       rerun;
116 daddr_t   maxfsblock;
117 char      *blockmap;
118 ino_t     maxino;
119 ino_t     lastino;
120 char      *statemap;
121 u_char    *typemap;
122 int16_t   *lncntp;
123 ino_t     lfdir;
124 daddr_t   n_blks;
125 daddr_t   n_files;
126 struct    ext2fs_dinode zino;
127 
128 static int          argtoi(int, const char *, const char *, int);
129 static int          checkfilesys(const char *, char *, long, int);
130 static void         usage(void) __dead;
131 
132 int
main(int argc,char * argv[])133 main(int argc, char *argv[])
134 {
135           int ch;
136           int ret = FSCK_EXIT_OK;
137 
138           ckfinish = ckfini;
139           sync();
140           skipclean = 1;
141           while ((ch = getopt(argc, argv, "b:dfm:npPqUy")) != -1) {
142                     switch (ch) {
143                     case 'b':
144                               skipclean = 0;
145                               bflag = argtoi('b', "number", optarg, 10);
146                               printf("Alternate super block location: %d\n", bflag);
147                               break;
148 
149                     case 'd':
150                               debug++;
151                               break;
152 
153                     case 'f':
154                               skipclean = 0;
155                               break;
156 
157                     case 'm':
158                               lfmode = argtoi('m', "mode", optarg, 8);
159                               if (lfmode &~ 07777)
160                                         errexit("bad mode to -m: %o", lfmode);
161                               printf("** lost+found creation mode %o\n", lfmode);
162                               break;
163 
164                     case 'n':
165                               nflag++;
166                               yflag = 0;
167                               break;
168 
169                     case 'p':
170                               preen++;
171                               break;
172 
173                     case 'P':
174                               /* Progress meter not implemented. */
175                               break;
176 
177                     case 'q':           /* Quiet not implemented */
178                               break;
179 
180 #ifndef SMALL
181                     case 'U':
182                               Uflag++;
183                               break;
184 #endif
185 
186                     case 'y':
187                               yflag++;
188                               nflag = 0;
189                               break;
190 
191                     default:
192                               usage();
193                     }
194           }
195 
196           argc -= optind;
197           argv += optind;
198 
199           if (!argc)
200                     usage();
201 
202           if (signal(SIGINT, SIG_IGN) != SIG_IGN)
203                     (void)signal(SIGINT, catch);
204           if (preen)
205                     (void)signal(SIGQUIT, catchquit);
206 
207           while (argc-- > 0) {
208                     int nret = checkfilesys(blockcheck(*argv++), 0, 0L, 0);
209                     if (ret < nret)
210                               ret = nret;
211           }
212 
213           return returntosingle ? FSCK_EXIT_UNRESOLVED : ret;
214 }
215 
216 static int
argtoi(int flag,const char * req,const char * str,int base)217 argtoi(int flag, const char *req, const char *str, int base)
218 {
219           char *cp;
220           int ret;
221 
222           ret = (int)strtol(str, &cp, base);
223           if (cp == str || *cp)
224                     errexit("-%c flag requires a %s", flag, req);
225           return (ret);
226 }
227 
228 /*
229  * Check the specified filesystem.
230  */
231 /* ARGSUSED */
232 static int
checkfilesys(const char * filesys,char * mntpt,long auxdata,int child)233 checkfilesys(const char *filesys, char *mntpt, long auxdata, int child)
234 {
235           daddr_t n_bfree;
236           struct dups *dp;
237           struct zlncnt *zlnp;
238           int i;
239 
240           if (preen && child)
241                     (void)signal(SIGQUIT, voidquit);
242           setcdevname(filesys, preen);
243           if (debug && preen)
244                     pwarn("starting\n");
245           switch (setup(filesys)) {
246           case 0:
247                     if (preen)
248                               pfatal("CAN'T CHECK FILE SYSTEM.");
249                     /* FALLTHROUGH */
250           case -1:
251                     return FSCK_EXIT_OK;
252           }
253           /*
254            * 1: scan inodes tallying blocks used
255            */
256           if (preen == 0) {
257                     if (sblock.e2fs.e2fs_rev > E2FS_REV0) {
258                               printf("** Last Mounted on %s\n",
259                                   sblock.e2fs.e2fs_fsmnt);
260                     }
261                     if (hotroot())
262                               printf("** Root file system\n");
263                     printf("** Phase 1 - Check Blocks and Sizes\n");
264           }
265           pass1();
266 
267           /*
268            * 1b: locate first references to duplicates, if any
269            */
270           if (duplist) {
271                     if (preen)
272                               pfatal("INTERNAL ERROR: dups with -p");
273                     printf("** Phase 1b - Rescan For More DUPS\n");
274                     pass1b();
275           }
276 
277           /*
278            * 2: traverse directories from root to mark all connected directories
279            */
280           if (preen == 0)
281                     printf("** Phase 2 - Check Pathnames\n");
282           pass2();
283 
284           /*
285            * 3: scan inodes looking for disconnected directories
286            */
287           if (preen == 0)
288                     printf("** Phase 3 - Check Connectivity\n");
289           pass3();
290 
291           /*
292            * 4: scan inodes looking for disconnected files; check reference counts
293            */
294           if (preen == 0)
295                     printf("** Phase 4 - Check Reference Counts\n");
296           pass4();
297 
298           /*
299            * 5: check and repair resource counts in cylinder groups
300            */
301           if (preen == 0)
302                     printf("** Phase 5 - Check Cyl groups\n");
303           pass5();
304 
305           /*
306            * print out summary statistics
307            */
308           n_bfree = sblock.e2fs.e2fs_fbcount;
309 
310           pwarn("%lld files, %lld used, %lld free\n",
311               (long long)n_files, (long long)n_blks, (long long)n_bfree);
312           if (debug &&
313                     /* 9 reserved and unused inodes in FS */
314               (n_files -= maxino - 9 - sblock.e2fs.e2fs_ficount))
315                     printf("%lld files missing\n", (long long)n_files);
316           if (debug) {
317                     for (i = 0; i < sblock.e2fs_ncg; i++)
318                               n_blks +=  cgoverhead(i);
319                     n_blks += sblock.e2fs.e2fs_first_dblock;
320                     if (n_blks -= maxfsblock - n_bfree)
321                               printf("%lld blocks missing\n", (long long)n_blks);
322                     if (duplist != NULL) {
323                               printf("The following duplicate blocks remain:");
324                               for (dp = duplist; dp; dp = dp->next)
325                                         printf(" %lld,", (long long)dp->dup);
326                               printf("\n");
327                     }
328                     if (zlnhead != NULL) {
329                               printf("The following zero link count inodes remain:");
330                               for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
331                                         printf(" %llu,",
332                                             (unsigned long long)zlnp->zlncnt);
333                               printf("\n");
334                     }
335           }
336           zlnhead = (struct zlncnt *)0;
337           duplist = (struct dups *)0;
338           muldup = (struct dups *)0;
339           inocleanup();
340           if (fsmodified) {
341                     time_t t;
342                     (void)time(&t);
343                     sblock.e2fs.e2fs_wtime = t;
344                     sblock.e2fs.e2fs_lastfsck = t;
345                     sbdirty();
346           }
347           ckfini(1);
348           free(blockmap);
349           free(statemap);
350           free((char *)lncntp);
351           if (!fsmodified)
352                     return FSCK_EXIT_OK;
353           if (!preen)
354                     printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
355           if (rerun)
356                     printf("\n***** PLEASE RERUN FSCK *****\n");
357           if (hotroot()) {
358                     struct statvfs stfs_buf;
359                     /*
360                      * We modified the root.  Do a mount update on
361                      * it, unless it is read-write, so we can continue.
362                      */
363                     if (statvfs("/", &stfs_buf) == 0) {
364                               long flags = stfs_buf.f_flag;
365                               struct ufs_args args;
366 
367                               if (flags & MNT_RDONLY) {
368                                         args.fspec = 0;
369                                         flags |= MNT_UPDATE | MNT_RELOAD;
370                                         if (mount(MOUNT_EXT2FS, "/", flags,
371                                             &args, sizeof args) == 0)
372                                                   return FSCK_EXIT_OK;
373                               }
374                     }
375                     if (!preen)
376                               printf("\n***** REBOOT NOW *****\n");
377                     sync();
378                     return FSCK_EXIT_ROOT_CHANGED;
379           }
380           return FSCK_EXIT_OK;
381 }
382 
383 static void
usage(void)384 usage(void)
385 {
386 
387           (void) fprintf(stderr,
388               "usage: %s [-dfnpUy] [-b block] [-m mode] filesystem ...\n",
389               getprogname());
390           exit(FSCK_EXIT_USAGE);
391 }
392