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