1 /* $NetBSD: main.c,v 1.2 2022/02/11 10:55:15 hannken Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: main.c,v 1.2 2022/02/11 10:55:15 hannken Exp $");
35 #endif /* not lint */
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <err.h>
41 #include <time.h>
42
43 #include "v7fs.h"
44 #include "v7fs_impl.h"
45 #include "v7fs_superblock.h"
46 #include "v7fs_inode.h"
47 #include "v7fs_file.h"
48
49 #include "fsck_v7fs.h"
50 #include "progress.h"
51
52 static int check_filesystem(struct v7fs_self *, int);
53 static int make_lost_and_found(struct v7fs_self *, struct v7fs_inode *);
54
55 struct v7fs_inode lost_and_found;
56
57 int
v7fs_fsck(const struct v7fs_mount_device * mount,int flags)58 v7fs_fsck(const struct v7fs_mount_device *mount, int flags)
59 {
60 struct v7fs_self *fs;
61 int error;
62
63 if ((error = v7fs_io_init(&fs, mount, V7FS_BSIZE))) {
64 pfatal("I/O setup failed");
65 return FSCK_EXIT_CHECK_FAILED;
66 }
67
68 if ((error = v7fs_superblock_load(fs))) {
69 if ((error != EINVAL)) {
70 pfatal("Can't load superblock");
71 return FSCK_EXIT_CHECK_FAILED;
72 }
73 pwarn("Inavalid superblock");
74 if (!reply_trivial("CONTINUE?")) {
75 return FSCK_EXIT_CHECK_FAILED;
76 }
77 }
78
79 error = check_filesystem(fs, flags);
80
81 printf("write backing...(no progress report)"); fflush(stdout);
82 v7fs_io_fini(fs);
83 printf("done.\n");
84
85 return error;
86 }
87
88 int
check_filesystem(struct v7fs_self * fs,int flags)89 check_filesystem(struct v7fs_self *fs, int flags)
90 {
91 int error;
92
93 /* Check superblock cached freeinode list. */
94 pwarn("[Superblock information]\n");
95 v7fs_superblock_dump(fs);
96 pwarn("[1] checking free inode in superblock...\n");
97 if ((error = freeinode_check(fs)))
98 return error;
99
100 /* Check free block linked list. */
101 pwarn("[2] checking free block link...\n");
102 if ((error = freeblock_check(fs)))
103 return error;
104
105 /* Check inode all. */
106 pwarn("[3] checking all ilist...\n");
107 if ((error = ilist_check(fs)))
108 return error;
109
110 /* Setup lost+found. */
111 pwarn("prepare lost+found\n");
112 if ((error = make_lost_and_found(fs, &lost_and_found)))
113 return FSCK_EXIT_CHECK_FAILED;
114
115 /* Check path(child and parent). Orphans are linked to lost+found. */
116 pwarn("[4] checking path name...\n");
117 if ((error = pathname_check(fs)))
118 return error;
119
120 if (flags & V7FS_FSCK_FREEBLOCK_DUP) {
121 /* Check duplicated block in freeblock. */
122 pwarn("[5] checking freeblock duplication...\n");
123 if ((error = freeblock_vs_freeblock_check(fs)))
124 return error;
125 }
126
127 if (flags & V7FS_FSCK_DATABLOCK_DUP) {
128 /* Check duplicated block in datablock. */
129 pwarn("[6] checking datablock duplication(vs datablock)...\n");
130 if ((error = datablock_vs_datablock_check(fs)))
131 return error;
132 }
133
134 if ((flags & V7FS_FSCK_DATABLOCK_DUP) && (flags &
135 V7FS_FSCK_FREEBLOCK_DUP)) {
136 /* Check off-diagonal. */
137 pwarn("[7] checking datablock duplication (vs freeblock)...\n");
138 if ((error = datablock_vs_freeblock_check(fs)))
139 return error;
140 }
141
142 return 0;
143 }
144
145 static int
reply_subr(const char * str,bool trivial)146 reply_subr(const char *str, bool trivial)
147 {
148 int c;
149 char *line = NULL;
150 size_t linesize = 0;
151 ssize_t linelen;
152
153 printf("%s ", str);
154 switch (fsck_operate) {
155 case PREEN:
156 return trivial;
157 case ALWAYS_YES:
158 printf("[Y]\n");
159 return 1;
160 case ALWAYS_NO:
161 if (strstr(str, "CONTINUE")) {
162 printf("[Y]\n");
163 return 1;
164 }
165 printf("[N]\n");
166 return 0;
167 case ASK:
168 break;
169 }
170 fflush(stdout);
171
172 if ((linelen = getline(&line, &linesize, stdin)) == -1) {
173 clearerr(stdin);
174 return 0;
175 }
176 c = line[0];
177
178 return c == 'y' || c == 'Y';
179 }
180
181 int
reply(const char * str)182 reply(const char *str)
183 {
184 return reply_subr(str, false);
185 }
186
187 int
reply_trivial(const char * str)188 reply_trivial(const char *str)
189 {
190 return reply_subr(str, true);
191 }
192
193 void
progress(const struct progress_arg * p)194 progress(const struct progress_arg *p)
195 {
196 static struct progress_arg Progress;
197 static char cdev[32];
198 static char label[32];
199
200 if (p) {
201 if (Progress.tick) {
202 progress_done();
203 }
204 Progress = *p;
205 if (p->cdev)
206 strcpy(cdev, p->cdev);
207 if (p->label)
208 strcpy(label, p->label);
209 }
210
211 if (fsck_operate == PREEN)
212 return;
213 if (!Progress.tick)
214 return;
215 if (++Progress.cnt > Progress.tick) {
216 Progress.cnt = 0;
217 Progress.total++;
218 progress_bar(cdev, label, Progress.total, PROGRESS_BAR_GRANULE);
219 }
220 }
221
222 int
make_lost_and_found(struct v7fs_self * fs,struct v7fs_inode * p)223 make_lost_and_found(struct v7fs_self *fs, struct v7fs_inode *p)
224 {
225 struct v7fs_inode root_inode;
226 struct v7fs_fileattr attr;
227 v7fs_ino_t ino;
228 int error = 0;
229
230 if ((error = v7fs_inode_load(fs, &root_inode, V7FS_ROOT_INODE))) {
231 errno = error;
232 warn("(%s) / missing.", __func__);
233 return error;
234 }
235
236 memset(&attr, 0, sizeof(attr));
237 attr.uid = 0;
238 attr.gid = 0;
239 attr.mode = V7FS_IFDIR | 0755;
240 attr.device = 0;
241 attr.ctime = attr.mtime = attr.atime = (v7fs_time_t)time(NULL);
242
243 /* If lost+found already exists, returns EEXIST */
244 if (!(error = v7fs_file_allocate(fs, &root_inode,
245 "lost+found", strlen("lost+found"), &attr, &ino)))
246 v7fs_superblock_writeback(fs);
247
248 if (error == EEXIST)
249 error = 0;
250
251 if (error) {
252 errno = error;
253 warn("(%s) Couldn't create lost+found", __func__);
254 return error;
255 }
256
257 if ((error = v7fs_inode_load(fs, p, ino))) {
258 errno = error;
259 warn("(%s:)lost+found is created, but missing.", __func__);
260 }
261
262 return error;
263 }
264