xref: /netbsd-src/sbin/fsck_v7fs/main.c (revision 514b0270dd0ac6e8ffce7a47d2bc570d815599f3)
1*514b0270Shannken /*	$NetBSD: main.c,v 1.2 2022/02/11 10:55:15 hannken Exp $	*/
29255b46fSuch 
39255b46fSuch /*-
49255b46fSuch  * Copyright (c) 2011 The NetBSD Foundation, Inc.
59255b46fSuch  * All rights reserved.
69255b46fSuch  *
79255b46fSuch  * This code is derived from software contributed to The NetBSD Foundation
89255b46fSuch  * by UCHIYAMA Yasushi.
99255b46fSuch  *
109255b46fSuch  * Redistribution and use in source and binary forms, with or without
119255b46fSuch  * modification, are permitted provided that the following conditions
129255b46fSuch  * are met:
139255b46fSuch  * 1. Redistributions of source code must retain the above copyright
149255b46fSuch  *    notice, this list of conditions and the following disclaimer.
159255b46fSuch  * 2. Redistributions in binary form must reproduce the above copyright
169255b46fSuch  *    notice, this list of conditions and the following disclaimer in the
179255b46fSuch  *    documentation and/or other materials provided with the distribution.
189255b46fSuch  *
199255b46fSuch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209255b46fSuch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219255b46fSuch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229255b46fSuch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239255b46fSuch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249255b46fSuch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259255b46fSuch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269255b46fSuch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279255b46fSuch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289255b46fSuch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299255b46fSuch  * POSSIBILITY OF SUCH DAMAGE.
309255b46fSuch  */
319255b46fSuch 
329255b46fSuch #include <sys/cdefs.h>
339255b46fSuch #ifndef lint
34*514b0270Shannken __RCSID("$NetBSD: main.c,v 1.2 2022/02/11 10:55:15 hannken Exp $");
359255b46fSuch #endif /* not lint */
369255b46fSuch 
379255b46fSuch #include <stdio.h>
389255b46fSuch #include <string.h>
399255b46fSuch #include <errno.h>
409255b46fSuch #include <err.h>
419255b46fSuch #include <time.h>
429255b46fSuch 
439255b46fSuch #include "v7fs.h"
449255b46fSuch #include "v7fs_impl.h"
459255b46fSuch #include "v7fs_superblock.h"
469255b46fSuch #include "v7fs_inode.h"
479255b46fSuch #include "v7fs_file.h"
489255b46fSuch 
499255b46fSuch #include "fsck_v7fs.h"
509255b46fSuch #include "progress.h"
519255b46fSuch 
529255b46fSuch static int check_filesystem(struct v7fs_self *, int);
539255b46fSuch static int make_lost_and_found(struct v7fs_self *, struct v7fs_inode *);
549255b46fSuch 
559255b46fSuch struct v7fs_inode lost_and_found;
569255b46fSuch 
579255b46fSuch int
v7fs_fsck(const struct v7fs_mount_device * mount,int flags)589255b46fSuch v7fs_fsck(const struct v7fs_mount_device *mount, int flags)
599255b46fSuch {
609255b46fSuch 	struct v7fs_self *fs;
619255b46fSuch 	int error;
629255b46fSuch 
639255b46fSuch 	if ((error = v7fs_io_init(&fs, mount, V7FS_BSIZE))) {
649255b46fSuch 		pfatal("I/O setup failed");
659255b46fSuch 		return FSCK_EXIT_CHECK_FAILED;
669255b46fSuch 	}
679255b46fSuch 
689255b46fSuch 	if ((error = v7fs_superblock_load(fs))) {
699255b46fSuch 		if ((error != EINVAL)) {
709255b46fSuch 			pfatal("Can't load superblock");
719255b46fSuch 			return FSCK_EXIT_CHECK_FAILED;
729255b46fSuch 		}
739255b46fSuch 		pwarn("Inavalid superblock");
749255b46fSuch 		if (!reply_trivial("CONTINUE?")) {
759255b46fSuch 			return FSCK_EXIT_CHECK_FAILED;
769255b46fSuch 		}
779255b46fSuch 	}
789255b46fSuch 
799255b46fSuch 	error = check_filesystem(fs, flags);
809255b46fSuch 
819255b46fSuch 	printf("write backing...(no progress report)"); fflush(stdout);
829255b46fSuch 	v7fs_io_fini(fs);
839255b46fSuch 	printf("done.\n");
849255b46fSuch 
859255b46fSuch 	return error;
869255b46fSuch }
879255b46fSuch 
889255b46fSuch int
check_filesystem(struct v7fs_self * fs,int flags)899255b46fSuch check_filesystem(struct v7fs_self *fs, int flags)
909255b46fSuch {
919255b46fSuch 	int error;
929255b46fSuch 
939255b46fSuch 	/* Check superblock cached freeinode list. */
949255b46fSuch 	pwarn("[Superblock information]\n");
959255b46fSuch 	v7fs_superblock_dump(fs);
969255b46fSuch 	pwarn("[1] checking free inode in superblock...\n");
979255b46fSuch 	if ((error = freeinode_check(fs)))
989255b46fSuch 		return error;
999255b46fSuch 
1009255b46fSuch 	/* Check free block linked list. */
1019255b46fSuch 	pwarn("[2] checking free block link...\n");
1029255b46fSuch 	if ((error = freeblock_check(fs)))
1039255b46fSuch 		return error;
1049255b46fSuch 
1059255b46fSuch 	/* Check inode all. */
1069255b46fSuch 	pwarn("[3] checking all ilist...\n");
1079255b46fSuch 	if ((error = ilist_check(fs)))
1089255b46fSuch 		return error;
1099255b46fSuch 
1109255b46fSuch 	/* Setup lost+found. */
1119255b46fSuch 	pwarn("prepare lost+found\n");
1129255b46fSuch 	if ((error = make_lost_and_found(fs, &lost_and_found)))
1139255b46fSuch 		return FSCK_EXIT_CHECK_FAILED;
1149255b46fSuch 
1159255b46fSuch 	/* Check path(child and parent). Orphans are linked to lost+found. */
1169255b46fSuch 	pwarn("[4] checking path name...\n");
1179255b46fSuch 	if ((error = pathname_check(fs)))
1189255b46fSuch 		return error;
1199255b46fSuch 
1209255b46fSuch 	if (flags & V7FS_FSCK_FREEBLOCK_DUP) {
1219255b46fSuch 		/* Check duplicated block in freeblock. */
1229255b46fSuch 		pwarn("[5] checking freeblock duplication...\n");
1239255b46fSuch 		if ((error = freeblock_vs_freeblock_check(fs)))
1249255b46fSuch 			return error;
1259255b46fSuch 	}
1269255b46fSuch 
1279255b46fSuch 	if (flags & V7FS_FSCK_DATABLOCK_DUP) {
1289255b46fSuch 		/* Check duplicated block in datablock. */
1299255b46fSuch 		pwarn("[6] checking datablock duplication(vs datablock)...\n");
1309255b46fSuch 		if ((error = datablock_vs_datablock_check(fs)))
1319255b46fSuch 			return error;
1329255b46fSuch 	}
1339255b46fSuch 
1349255b46fSuch 	if ((flags & V7FS_FSCK_DATABLOCK_DUP) && (flags &
1359255b46fSuch 		V7FS_FSCK_FREEBLOCK_DUP)) {
1369255b46fSuch 		/* Check off-diagonal. */
1379255b46fSuch 		pwarn("[7] checking datablock duplication (vs freeblock)...\n");
1389255b46fSuch 		if ((error = datablock_vs_freeblock_check(fs)))
1399255b46fSuch 			return error;
1409255b46fSuch 	}
1419255b46fSuch 
1429255b46fSuch 	return 0;
1439255b46fSuch }
1449255b46fSuch 
1459255b46fSuch static int
reply_subr(const char * str,bool trivial)1469255b46fSuch reply_subr(const char *str, bool trivial)
1479255b46fSuch {
1489255b46fSuch 	int c;
1499255b46fSuch 	char *line = NULL;
1509255b46fSuch 	size_t linesize = 0;
1519255b46fSuch 	ssize_t linelen;
1529255b46fSuch 
1539255b46fSuch 	printf("%s ", str);
1549255b46fSuch 	switch (fsck_operate) {
1559255b46fSuch 	case PREEN:
1569255b46fSuch 		return trivial;
1579255b46fSuch 	case ALWAYS_YES:
1589255b46fSuch 		printf("[Y]\n");
1599255b46fSuch 		return 1;
1609255b46fSuch 	case ALWAYS_NO:
1619255b46fSuch 		if (strstr(str, "CONTINUE")) {
1629255b46fSuch 			printf("[Y]\n");
1639255b46fSuch 			return 1;
1649255b46fSuch 		}
1659255b46fSuch 		printf("[N]\n");
1669255b46fSuch 		return 0;
1679255b46fSuch 	case ASK:
1689255b46fSuch 		break;
1699255b46fSuch 	}
1709255b46fSuch 	fflush(stdout);
1719255b46fSuch 
1729255b46fSuch 	if ((linelen = getline(&line, &linesize, stdin)) == -1)	{
1739255b46fSuch 		clearerr(stdin);
1749255b46fSuch 		return 0;
1759255b46fSuch 	}
1769255b46fSuch 	c = line[0];
1779255b46fSuch 
1789255b46fSuch 	return c == 'y' || c == 'Y';
1799255b46fSuch }
1809255b46fSuch 
1819255b46fSuch int
reply(const char * str)1829255b46fSuch reply(const char *str)
1839255b46fSuch {
1849255b46fSuch 	return reply_subr(str, false);
1859255b46fSuch }
1869255b46fSuch 
1879255b46fSuch int
reply_trivial(const char * str)1889255b46fSuch reply_trivial(const char *str)
1899255b46fSuch {
1909255b46fSuch 	return reply_subr(str, true);
1919255b46fSuch }
1929255b46fSuch 
1939255b46fSuch void
progress(const struct progress_arg * p)1949255b46fSuch progress(const struct progress_arg *p)
1959255b46fSuch {
1969255b46fSuch 	static struct progress_arg Progress;
1979255b46fSuch 	static char cdev[32];
1989255b46fSuch 	static char label[32];
1999255b46fSuch 
2009255b46fSuch 	if (p) {
2019255b46fSuch 		if (Progress.tick) {
2029255b46fSuch 			progress_done();
2039255b46fSuch 		}
2049255b46fSuch 		Progress = *p;
2059255b46fSuch 		if (p->cdev)
2069255b46fSuch 			strcpy(cdev, p->cdev);
2079255b46fSuch 		if (p->label)
2089255b46fSuch 			strcpy(label, p->label);
2099255b46fSuch 	}
2109255b46fSuch 
2119255b46fSuch 	if (fsck_operate == PREEN)
2129255b46fSuch 		return;
2139255b46fSuch 	if (!Progress.tick)
2149255b46fSuch 		return;
2159255b46fSuch 	if (++Progress.cnt > Progress.tick) {
2169255b46fSuch 		Progress.cnt = 0;
2179255b46fSuch 		Progress.total++;
2189255b46fSuch 		progress_bar(cdev, label, Progress.total, PROGRESS_BAR_GRANULE);
2199255b46fSuch 	}
2209255b46fSuch }
2219255b46fSuch 
2229255b46fSuch int
make_lost_and_found(struct v7fs_self * fs,struct v7fs_inode * p)2239255b46fSuch make_lost_and_found(struct v7fs_self *fs, struct v7fs_inode *p)
2249255b46fSuch {
2259255b46fSuch 	struct v7fs_inode root_inode;
2269255b46fSuch 	struct v7fs_fileattr attr;
2279255b46fSuch 	v7fs_ino_t ino;
2289255b46fSuch 	int error = 0;
2299255b46fSuch 
2309255b46fSuch 	if ((error = v7fs_inode_load(fs, &root_inode, V7FS_ROOT_INODE))) {
2319255b46fSuch 		errno = error;
2329255b46fSuch 		warn("(%s) / missing.", __func__);
2339255b46fSuch 		return error;
2349255b46fSuch 	}
2359255b46fSuch 
2369255b46fSuch 	memset(&attr, 0, sizeof(attr));
2379255b46fSuch 	attr.uid = 0;
2389255b46fSuch 	attr.gid = 0;
2399255b46fSuch 	attr.mode = V7FS_IFDIR | 0755;
2409255b46fSuch 	attr.device = 0;
2419255b46fSuch 	attr.ctime = attr.mtime = attr.atime = (v7fs_time_t)time(NULL);
2429255b46fSuch 
2439255b46fSuch 	/* If lost+found already exists, returns EEXIST */
244*514b0270Shannken 	if (!(error = v7fs_file_allocate(fs, &root_inode,
245*514b0270Shannken 	    "lost+found", strlen("lost+found"), &attr, &ino)))
2469255b46fSuch 		v7fs_superblock_writeback(fs);
2479255b46fSuch 
2489255b46fSuch 	if (error == EEXIST)
2499255b46fSuch 		error = 0;
2509255b46fSuch 
2519255b46fSuch 	if (error) {
2529255b46fSuch 		errno = error;
2539255b46fSuch 		warn("(%s) Couldn't create lost+found", __func__);
2549255b46fSuch 		return error;
2559255b46fSuch 	}
2569255b46fSuch 
2579255b46fSuch 	if ((error = v7fs_inode_load(fs, p, ino))) {
2589255b46fSuch 		errno = error;
2599255b46fSuch 		warn("(%s:)lost+found is created, but missing.", __func__);
2609255b46fSuch 	}
2619255b46fSuch 
2629255b46fSuch 	return error;
2639255b46fSuch }
264