1*5b133f3fSguenther /* $OpenBSD: main.c,v 1.30 2023/03/08 04:43:06 guenther Exp $ */
25ff4e0c8Sdownsj /* $NetBSD: main.c,v 1.1 1997/06/11 11:21:50 bouyer Exp $ */
38c424e8eSdownsj
48c424e8eSdownsj /*
55ff4e0c8Sdownsj * Copyright (c) 1997 Manuel Bouyer.
68c424e8eSdownsj * Copyright (c) 1980, 1986, 1993
78c424e8eSdownsj * The Regents of the University of California. All rights reserved.
88c424e8eSdownsj *
98c424e8eSdownsj * Redistribution and use in source and binary forms, with or without
108c424e8eSdownsj * modification, are permitted provided that the following conditions
118c424e8eSdownsj * are met:
128c424e8eSdownsj * 1. Redistributions of source code must retain the above copyright
138c424e8eSdownsj * notice, this list of conditions and the following disclaimer.
148c424e8eSdownsj * 2. Redistributions in binary form must reproduce the above copyright
158c424e8eSdownsj * notice, this list of conditions and the following disclaimer in the
168c424e8eSdownsj * documentation and/or other materials provided with the distribution.
171ef0d710Smillert * 3. Neither the name of the University nor the names of its contributors
188c424e8eSdownsj * may be used to endorse or promote products derived from this software
198c424e8eSdownsj * without specific prior written permission.
208c424e8eSdownsj *
218c424e8eSdownsj * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
228c424e8eSdownsj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238c424e8eSdownsj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248c424e8eSdownsj * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
258c424e8eSdownsj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
268c424e8eSdownsj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
278c424e8eSdownsj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
288c424e8eSdownsj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
298c424e8eSdownsj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
308c424e8eSdownsj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318c424e8eSdownsj * SUCH DAMAGE.
328c424e8eSdownsj */
338c424e8eSdownsj
34b9fc9a72Sderaadt #include <sys/types.h>
35b9fc9a72Sderaadt #include <sys/signal.h>
368c424e8eSdownsj #include <sys/time.h>
378c424e8eSdownsj #include <sys/mount.h>
388c424e8eSdownsj #include <ufs/ext2fs/ext2fs_dinode.h>
398c424e8eSdownsj #include <ufs/ext2fs/ext2fs.h>
408c424e8eSdownsj #include <fstab.h>
418c424e8eSdownsj #include <stdlib.h>
428c424e8eSdownsj #include <string.h>
438c424e8eSdownsj #include <ctype.h>
448c424e8eSdownsj #include <stdio.h>
4565348f21Sjasoni #include <time.h>
468c424e8eSdownsj #include <unistd.h>
47e384f6efSderaadt #include <err.h>
488c424e8eSdownsj
498c424e8eSdownsj #include "fsck.h"
508c424e8eSdownsj #include "extern.h"
518c424e8eSdownsj #include "fsutil.h"
528c424e8eSdownsj
535cb46d43Sderaadt volatile sig_atomic_t returntosingle;
548c424e8eSdownsj
55c72b5b24Smillert int main(int, char *[]);
568c424e8eSdownsj
57c72b5b24Smillert static int argtoi(int, char *, char *, int);
58c72b5b24Smillert static int checkfilesys(char *, char *, long, int);
59c72b5b24Smillert static void usage(void);
608c424e8eSdownsj
610986c204Sderaadt struct bufarea bufhead; /* head of list of other blks in filesys */
620986c204Sderaadt struct bufarea sblk; /* file system superblock */
630986c204Sderaadt struct bufarea asblk; /* first alternate superblock */
640986c204Sderaadt struct bufarea *pdirbp; /* current directory contents */
650986c204Sderaadt struct bufarea *pbp; /* current inode block */
660986c204Sderaadt struct bufarea *getdatablk(daddr32_t, long);
670986c204Sderaadt struct m_ext2fs sblock;
680986c204Sderaadt
690986c204Sderaadt struct dups *duplist; /* head of dup list */
700986c204Sderaadt struct dups *muldup; /* end of unique duplicate dup block numbers */
710986c204Sderaadt
720986c204Sderaadt struct zlncnt *zlnhead; /* head of zero link count list */
730986c204Sderaadt
740986c204Sderaadt struct inoinfo **inphead, **inpsort;
750986c204Sderaadt long numdirs, listmax, inplast;
760986c204Sderaadt
770986c204Sderaadt long secsize; /* actual disk sector size */
780986c204Sderaadt char nflag; /* assume a no response */
790986c204Sderaadt char yflag; /* assume a yes response */
800986c204Sderaadt int bflag; /* location of alternate super block */
810986c204Sderaadt int debug; /* output debugging info */
820986c204Sderaadt int preen; /* just fix normal inconsistencies */
830986c204Sderaadt char havesb; /* superblock has been read */
840986c204Sderaadt char skipclean; /* skip clean file systems if preening */
850986c204Sderaadt int fsmodified; /* 1 => write done to file system */
860986c204Sderaadt int fsreadfd; /* file descriptor for reading file system */
870986c204Sderaadt int fswritefd; /* file descriptor for writing file system */
880986c204Sderaadt int rerun; /* rerun fsck. Only used in non-preen mode */
890986c204Sderaadt
900986c204Sderaadt daddr32_t maxfsblock; /* number of blocks in the file system */
910986c204Sderaadt char *blockmap; /* ptr to primary blk allocation map */
920986c204Sderaadt ino_t maxino; /* number of inodes in file system */
930986c204Sderaadt ino_t lastino; /* last inode in use */
940986c204Sderaadt char *statemap; /* ptr to inode state table */
950986c204Sderaadt u_char *typemap; /* ptr to inode type table */
960986c204Sderaadt int16_t *lncntp; /* ptr to link count table */
970986c204Sderaadt
980986c204Sderaadt ino_t lfdir; /* lost & found directory inode number */
990986c204Sderaadt
1000986c204Sderaadt daddr32_t n_blks; /* number of blocks in use */
1010986c204Sderaadt daddr32_t n_files; /* number of files in use */
1020986c204Sderaadt
1030986c204Sderaadt struct ext2fs_dinode zino;
1048c424e8eSdownsj
1058c424e8eSdownsj int
main(int argc,char * argv[])1068809fabbSderaadt main(int argc, char *argv[])
1078c424e8eSdownsj {
1088c424e8eSdownsj int ch;
1098c424e8eSdownsj int ret = 0;
1108c424e8eSdownsj
111c9112980Sderaadt checkroot();
112c9112980Sderaadt
1138c424e8eSdownsj sync();
1148c424e8eSdownsj skipclean = 1;
11567e491c4Saaron while ((ch = getopt(argc, argv, "b:dfm:npy")) != -1) {
1168c424e8eSdownsj switch (ch) {
1178c424e8eSdownsj case 'b':
1188c424e8eSdownsj skipclean = 0;
1198c424e8eSdownsj bflag = argtoi('b', "number", optarg, 10);
1208c424e8eSdownsj printf("Alternate super block location: %d\n", bflag);
1218c424e8eSdownsj break;
1228c424e8eSdownsj
1238c424e8eSdownsj case 'd':
1243579491dSderaadt debug = 1;
1258c424e8eSdownsj break;
1268c424e8eSdownsj
1278c424e8eSdownsj case 'f':
1288c424e8eSdownsj skipclean = 0;
1298c424e8eSdownsj break;
1308c424e8eSdownsj
1318c424e8eSdownsj case 'm':
1328c424e8eSdownsj lfmode = argtoi('m', "mode", optarg, 8);
1338c424e8eSdownsj if (lfmode &~ 07777)
1348c424e8eSdownsj errexit("bad mode to -m: %o\n", lfmode);
1358c424e8eSdownsj printf("** lost+found creation mode %o\n", lfmode);
1368c424e8eSdownsj break;
1378c424e8eSdownsj
1388c424e8eSdownsj case 'n':
1393579491dSderaadt nflag = 1;
1408c424e8eSdownsj yflag = 0;
1418c424e8eSdownsj break;
1428c424e8eSdownsj
1438c424e8eSdownsj case 'p':
1443579491dSderaadt preen = 1;
1458c424e8eSdownsj break;
1468c424e8eSdownsj
1478c424e8eSdownsj case 'y':
1483579491dSderaadt yflag = 1;
1498c424e8eSdownsj nflag = 0;
1508c424e8eSdownsj break;
1518c424e8eSdownsj
1528c424e8eSdownsj default:
1538c424e8eSdownsj usage();
1548c424e8eSdownsj }
1558c424e8eSdownsj }
1568c424e8eSdownsj
1578c424e8eSdownsj argc -= optind;
1588c424e8eSdownsj argv += optind;
1598c424e8eSdownsj
160fd4c6d5dSderaadt if (argc != 1)
1618c424e8eSdownsj usage();
1628c424e8eSdownsj
1638c424e8eSdownsj if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1648c424e8eSdownsj (void)signal(SIGINT, catch);
1658c424e8eSdownsj if (preen)
1668c424e8eSdownsj (void)signal(SIGQUIT, catchquit);
1678c424e8eSdownsj
168fd4c6d5dSderaadt (void)checkfilesys(blockcheck(*argv), 0, 0L, 0);
1698c424e8eSdownsj
1708c424e8eSdownsj if (returntosingle)
1718c424e8eSdownsj ret = 2;
1728c424e8eSdownsj
1738c424e8eSdownsj exit(ret);
1748c424e8eSdownsj }
1758c424e8eSdownsj
1768c424e8eSdownsj static int
argtoi(int flag,char * req,char * str,int base)1778809fabbSderaadt argtoi(int flag, char *req, char *str, int base)
1788c424e8eSdownsj {
1798c424e8eSdownsj char *cp;
1808c424e8eSdownsj int ret;
1818c424e8eSdownsj
1828c424e8eSdownsj ret = (int)strtol(str, &cp, base);
1838c424e8eSdownsj if (cp == str || *cp)
1848c424e8eSdownsj errexit("-%c flag requires a %s\n", flag, req);
1858c424e8eSdownsj return (ret);
1868c424e8eSdownsj }
1878c424e8eSdownsj
1888c424e8eSdownsj /*
1898c424e8eSdownsj * Check the specified filesystem.
1908c424e8eSdownsj */
1918c424e8eSdownsj static int
checkfilesys(char * filesys,char * mntpt,long auxdata,int child)1928809fabbSderaadt checkfilesys(char *filesys, char *mntpt, long auxdata, int child)
1938c424e8eSdownsj {
194b6d2e2d5Sderaadt daddr32_t n_bfree;
1958c424e8eSdownsj struct dups *dp;
1968c424e8eSdownsj struct zlncnt *zlnp;
19765348f21Sjasoni int i;
1988c424e8eSdownsj
1998c424e8eSdownsj if (preen && child)
2008c424e8eSdownsj (void)signal(SIGQUIT, voidquit);
201e729ad4aSjsing setcdevname(filesys, NULL, preen);
2028c424e8eSdownsj if (debug && preen)
2038c424e8eSdownsj pwarn("starting\n");
204e384f6efSderaadt
2058c424e8eSdownsj switch (setup(filesys)) {
2068c424e8eSdownsj case 0:
2078c424e8eSdownsj if (preen)
2088c424e8eSdownsj pfatal("CAN'T CHECK FILE SYSTEM.");
2098c424e8eSdownsj case -1:
2108c424e8eSdownsj return (0);
2118c424e8eSdownsj }
2128c424e8eSdownsj /*
2138c424e8eSdownsj * 1: scan inodes tallying blocks used
2148c424e8eSdownsj */
2158c424e8eSdownsj if (preen == 0) {
21665348f21Sjasoni if (sblock.e2fs.e2fs_rev > E2FS_REV0) {
21765348f21Sjasoni printf("** Last Mounted on %s\n",
21865348f21Sjasoni sblock.e2fs.e2fs_fsmnt);
21965348f21Sjasoni }
2208c424e8eSdownsj if (hotroot())
2218c424e8eSdownsj printf("** Root file system\n");
2228c424e8eSdownsj printf("** Phase 1 - Check Blocks and Sizes\n");
2238c424e8eSdownsj }
2248c424e8eSdownsj pass1();
2258c424e8eSdownsj
2268c424e8eSdownsj /*
2278c424e8eSdownsj * 1b: locate first references to duplicates, if any
2288c424e8eSdownsj */
2298c424e8eSdownsj if (duplist) {
2308c424e8eSdownsj if (preen)
2318c424e8eSdownsj pfatal("INTERNAL ERROR: dups with -p");
2328c424e8eSdownsj printf("** Phase 1b - Rescan For More DUPS\n");
2338c424e8eSdownsj pass1b();
2348c424e8eSdownsj }
2358c424e8eSdownsj
2368c424e8eSdownsj /*
2378c424e8eSdownsj * 2: traverse directories from root to mark all connected directories
2388c424e8eSdownsj */
2398c424e8eSdownsj if (preen == 0)
2408c424e8eSdownsj printf("** Phase 2 - Check Pathnames\n");
2418c424e8eSdownsj pass2();
2428c424e8eSdownsj
2438c424e8eSdownsj /*
2448c424e8eSdownsj * 3: scan inodes looking for disconnected directories
2458c424e8eSdownsj */
2468c424e8eSdownsj if (preen == 0)
2478c424e8eSdownsj printf("** Phase 3 - Check Connectivity\n");
2488c424e8eSdownsj pass3();
2498c424e8eSdownsj
2508c424e8eSdownsj /*
2518c424e8eSdownsj * 4: scan inodes looking for disconnected files; check reference counts
2528c424e8eSdownsj */
2538c424e8eSdownsj if (preen == 0)
2548c424e8eSdownsj printf("** Phase 4 - Check Reference Counts\n");
2558c424e8eSdownsj pass4();
2568c424e8eSdownsj
2578c424e8eSdownsj /*
2588c424e8eSdownsj * 5: check and repair resource counts in cylinder groups
2598c424e8eSdownsj */
2608c424e8eSdownsj if (preen == 0)
2618c424e8eSdownsj printf("** Phase 5 - Check Cyl groups\n");
2628c424e8eSdownsj pass5();
2638c424e8eSdownsj
2648c424e8eSdownsj /*
2658c424e8eSdownsj * print out summary statistics
2668c424e8eSdownsj */
2678c424e8eSdownsj n_bfree = sblock.e2fs.e2fs_fbcount;
2688c424e8eSdownsj
2698c424e8eSdownsj pwarn("%d files, %d used, %d free\n",
2708c424e8eSdownsj n_files, n_blks, n_bfree);
2718c424e8eSdownsj if (debug &&
2728c424e8eSdownsj /* 9 reserved and unused inodes in FS */
2738c424e8eSdownsj (n_files -= maxino - 9 - sblock.e2fs.e2fs_ficount))
2748c424e8eSdownsj printf("%d files missing\n", n_files);
2758c424e8eSdownsj if (debug) {
27665348f21Sjasoni for (i = 0; i < sblock.e2fs_ncg; i++)
27765348f21Sjasoni n_blks += cgoverhead(i);
2788c424e8eSdownsj n_blks += sblock.e2fs.e2fs_first_dblock;
2798c424e8eSdownsj if (n_blks -= maxfsblock - n_bfree)
2808c424e8eSdownsj printf("%d blocks missing\n", n_blks);
2818c424e8eSdownsj if (duplist != NULL) {
2828c424e8eSdownsj printf("The following duplicate blocks remain:");
2838c424e8eSdownsj for (dp = duplist; dp; dp = dp->next)
2848c424e8eSdownsj printf(" %d,", dp->dup);
2858c424e8eSdownsj printf("\n");
2868c424e8eSdownsj }
2878c424e8eSdownsj if (zlnhead != NULL) {
2888c424e8eSdownsj printf("The following zero link count inodes remain:");
2898c424e8eSdownsj for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
290520eb379Sotto printf(" %llu,",
291520eb379Sotto (unsigned long long)zlnp->zlncnt);
2928c424e8eSdownsj printf("\n");
2938c424e8eSdownsj }
2948c424e8eSdownsj }
295c9899b11Skrw zlnhead = NULL;
296c9899b11Skrw duplist = NULL;
297c9899b11Skrw muldup = NULL;
2988c424e8eSdownsj inocleanup();
2998c424e8eSdownsj if (fsmodified) {
3008c424e8eSdownsj time_t t;
3018c424e8eSdownsj (void)time(&t);
3028c424e8eSdownsj sblock.e2fs.e2fs_wtime = t;
3038c424e8eSdownsj sblock.e2fs.e2fs_lastfsck = t;
3048c424e8eSdownsj sbdirty();
3058c424e8eSdownsj }
3068c424e8eSdownsj ckfini(1);
3078c424e8eSdownsj free(blockmap);
3088c424e8eSdownsj free(statemap);
3098c424e8eSdownsj free((char *)lncntp);
3108c424e8eSdownsj if (!fsmodified)
3118c424e8eSdownsj return (0);
3128c424e8eSdownsj if (!preen)
3138c424e8eSdownsj printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
3148c424e8eSdownsj if (rerun)
3158c424e8eSdownsj printf("\n***** PLEASE RERUN FSCK *****\n");
3168c424e8eSdownsj if (hotroot()) {
3178c424e8eSdownsj struct statfs stfs_buf;
3188c424e8eSdownsj /*
3198c424e8eSdownsj * We modified the root. Do a mount update on
3208c424e8eSdownsj * it, unless it is read-write, so we can continue.
3218c424e8eSdownsj */
3228c424e8eSdownsj if (statfs("/", &stfs_buf) == 0) {
3238c424e8eSdownsj long flags = stfs_buf.f_flags;
3248c424e8eSdownsj struct ufs_args args;
3258c424e8eSdownsj int ret;
3268c424e8eSdownsj
3278c424e8eSdownsj if (flags & MNT_RDONLY) {
3288c424e8eSdownsj args.fspec = 0;
329ef9317a4Sespie args.export_info.ex_flags = 0;
330ef9317a4Sespie args.export_info.ex_root = 0;
3318c424e8eSdownsj flags |= MNT_UPDATE | MNT_RELOAD;
3328c424e8eSdownsj ret = mount(MOUNT_EXT2FS, "/", flags, &args);
3338c424e8eSdownsj if (ret == 0)
3348c424e8eSdownsj return(0);
3358c424e8eSdownsj }
3368c424e8eSdownsj }
3378c424e8eSdownsj if (!preen)
3388c424e8eSdownsj printf("\n***** REBOOT NOW *****\n");
3398c424e8eSdownsj sync();
3408c424e8eSdownsj return (4);
3418c424e8eSdownsj }
3428c424e8eSdownsj return (0);
3438c424e8eSdownsj }
3448c424e8eSdownsj
3458c424e8eSdownsj static void
usage(void)3468809fabbSderaadt usage(void)
3478c424e8eSdownsj {
3488c424e8eSdownsj extern char *__progname;
3498c424e8eSdownsj
3508c424e8eSdownsj (void) fprintf(stderr,
351fd4c6d5dSderaadt "usage: %s [-dfnpy] [-b block#] [-m mode] filesystem\n",
3528c424e8eSdownsj __progname);
3538c424e8eSdownsj exit(1);
3548c424e8eSdownsj }
355