freeblock.c revision 1.1 1 1.1 uch /* $NetBSD: freeblock.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: freeblock.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 <stdbool.h>
39 1.1 uch #include <string.h>
40 1.1 uch
41 1.1 uch #include "v7fs.h"
42 1.1 uch #include "v7fs_superblock.h"
43 1.1 uch #include "v7fs_inode.h"
44 1.1 uch #include "v7fs_impl.h"
45 1.1 uch #include "v7fs_datablock.h"
46 1.1 uch #include "fsck_v7fs.h"
47 1.1 uch
48 1.1 uch struct freeblock_arg {
49 1.1 uch v7fs_daddr_t i;
50 1.1 uch v7fs_daddr_t j;
51 1.1 uch v7fs_daddr_t blk;
52 1.1 uch };
53 1.1 uch
54 1.1 uch static int
55 1.1 uch freeblock_subr_cnt(struct v7fs_self *fs __unused, void *ctx,
56 1.1 uch v7fs_daddr_t blk __unused)
57 1.1 uch {
58 1.1 uch ((struct freeblock_arg *)ctx)->blk++;
59 1.1 uch progress(0);
60 1.1 uch
61 1.1 uch return 0;
62 1.1 uch }
63 1.1 uch
64 1.1 uch static int
65 1.1 uch freeblock_subr_j(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk)
66 1.1 uch {
67 1.1 uch struct freeblock_arg *arg = (struct freeblock_arg *)ctx;
68 1.1 uch
69 1.1 uch if (!datablock_number_sanity(fs, blk)) {
70 1.1 uch pwarn("invalid block#%d in freeblock", blk);
71 1.1 uch /* This problem should be fixed at freeblock_check(). */
72 1.1 uch return FSCK_EXIT_CHECK_FAILED;
73 1.1 uch }
74 1.1 uch
75 1.1 uch if (arg->j >= arg->i)
76 1.1 uch return V7FS_ITERATOR_BREAK;
77 1.1 uch
78 1.1 uch progress(0);
79 1.1 uch
80 1.1 uch if (arg->blk == blk) {
81 1.1 uch pwarn("freeblock duplicate %d %d blk=%d", arg->i, arg->j, blk);
82 1.1 uch if (reply("CORRECT?")) {
83 1.1 uch freeblock_dup_remove(fs, blk);
84 1.1 uch }
85 1.1 uch return FSCK_EXIT_UNRESOLVED; /* Rescan needed. */
86 1.1 uch }
87 1.1 uch
88 1.1 uch arg->j++;
89 1.1 uch
90 1.1 uch return 0; /*continue */
91 1.1 uch }
92 1.1 uch
93 1.1 uch static int
94 1.1 uch freeblock_subr_i(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk)
95 1.1 uch {
96 1.1 uch struct freeblock_arg *arg = (struct freeblock_arg *)ctx;
97 1.1 uch int ret;
98 1.1 uch
99 1.1 uch if (!datablock_number_sanity(fs, blk)) {
100 1.1 uch pwarn("invalid block#%d in freeblock", blk);
101 1.1 uch /* This problem should be fixed at freeblock_check(). */
102 1.1 uch return FSCK_EXIT_CHECK_FAILED;
103 1.1 uch }
104 1.1 uch
105 1.1 uch arg->j = 0;
106 1.1 uch arg->blk = blk;
107 1.1 uch ret = v7fs_freeblock_foreach(fs, freeblock_subr_j, ctx);
108 1.1 uch if (!((ret == 0) || (ret == V7FS_ITERATOR_BREAK)))
109 1.1 uch return ret;
110 1.1 uch
111 1.1 uch arg->i++;
112 1.1 uch
113 1.1 uch return 0;
114 1.1 uch }
115 1.1 uch
116 1.1 uch int
117 1.1 uch freeblock_vs_freeblock_check(struct v7fs_self *fs)
118 1.1 uch {
119 1.1 uch struct v7fs_superblock *sb = &fs->superblock;
120 1.1 uch int n = sb->total_freeblock;
121 1.1 uch
122 1.1 uch progress(&(struct progress_arg){ .label = "free-free", .tick = (n / 2)
123 1.1 uch * ((n - 1) / PROGRESS_BAR_GRANULE) });
124 1.1 uch
125 1.1 uch return v7fs_freeblock_foreach(fs, freeblock_subr_i,
126 1.1 uch &(struct freeblock_arg){ .i = 0 });
127 1.1 uch }
128 1.1 uch
129 1.1 uch /*
130 1.1 uch * Remove duplicated block.
131 1.1 uch */
132 1.1 uch void
133 1.1 uch freeblock_dup_remove(struct v7fs_self *fs, v7fs_daddr_t dupblk)
134 1.1 uch {
135 1.1 uch struct v7fs_superblock *sb = &fs->superblock;
136 1.1 uch struct v7fs_freeblock *fb;
137 1.1 uch int i, total, n;
138 1.1 uch void *buf;
139 1.1 uch v7fs_daddr_t blk;
140 1.1 uch
141 1.1 uch n = sb->total_freeblock;
142 1.1 uch
143 1.1 uch /* Superblock cache. */
144 1.1 uch total = 0;
145 1.1 uch for (i = sb->nfreeblock - 1; (i > 0) && (n >= 0); i--, n--, total++) {
146 1.1 uch if (sb->freeblock[i] == dupblk) { /* Duplicate found. */
147 1.1 uch memmove(sb->freeblock + i, sb->freeblock + i + 1,
148 1.1 uch sb->nfreeblock - 1 - i);
149 1.1 uch sb->nfreeblock--;
150 1.1 uch sb->modified = 1;
151 1.1 uch v7fs_superblock_writeback(fs);
152 1.1 uch pwarn("remove duplicated freeblock %d"
153 1.1 uch "from superblock", dupblk);
154 1.1 uch return;
155 1.1 uch }
156 1.1 uch }
157 1.1 uch if (!n)
158 1.1 uch return;
159 1.1 uch blk = sb->freeblock[0];
160 1.1 uch
161 1.1 uch do {
162 1.1 uch if (!blk)
163 1.1 uch break;
164 1.1 uch buf = scratch_read(fs, blk);
165 1.1 uch fb = (struct v7fs_freeblock *)buf;
166 1.1 uch v7fs_freeblock_endian_convert(fs, fb);
167 1.1 uch
168 1.1 uch if (blk == dupblk) {
169 1.1 uch /* This is difficult probrem. give up! */
170 1.1 uch /* or newly allocate block, and copy it and link. */
171 1.1 uch pwarn("duplicated block is freeblock list."
172 1.1 uch "Shortage freeblock %d->%d.",
173 1.1 uch sb->nfreeblock, total);
174 1.1 uch sb->nfreeblock = total; /*shotage freeblock list. */
175 1.1 uch sb->modified = 1;
176 1.1 uch v7fs_superblock_writeback(fs);
177 1.1 uch return;
178 1.1 uch }
179 1.1 uch total++;
180 1.1 uch
181 1.1 uch blk = fb->freeblock[0]; /* next freeblock list */
182 1.1 uch
183 1.1 uch for (i = fb->nfreeblock - 1; (i > 0) && (n >= 0);
184 1.1 uch i--, n--, total++) {
185 1.1 uch if (fb->freeblock[i] == dupblk) {
186 1.1 uch pwarn("remove duplicated freeblock"
187 1.1 uch "%d from list %d", dupblk, blk);
188 1.1 uch memmove(fb->freeblock + i, fb->freeblock + i +
189 1.1 uch 1, fb->nfreeblock - 1 - i);
190 1.1 uch /* Writeback superblock. */
191 1.1 uch sb->nfreeblock--;
192 1.1 uch sb->modified = 1;
193 1.1 uch v7fs_superblock_writeback(fs);
194 1.1 uch /* Writeback freeblock list block. */
195 1.1 uch v7fs_freeblock_endian_convert(fs, fb);
196 1.1 uch fs->io.write(fs->io.cookie, buf, blk);
197 1.1 uch return;
198 1.1 uch }
199 1.1 uch }
200 1.1 uch scratch_free(fs, buf);
201 1.1 uch } while (n);
202 1.1 uch
203 1.1 uch return;
204 1.1 uch }
205 1.1 uch
206 1.1 uch int
207 1.1 uch v7fs_freeblock_foreach(struct v7fs_self *fs,
208 1.1 uch int (*func)(struct v7fs_self *, void *, v7fs_daddr_t), void *ctx)
209 1.1 uch {
210 1.1 uch struct v7fs_superblock *sb = &fs->superblock;
211 1.1 uch struct v7fs_freeblock *fb;
212 1.1 uch int i, n;
213 1.1 uch void *buf;
214 1.1 uch v7fs_daddr_t blk;
215 1.1 uch int ret;
216 1.1 uch
217 1.1 uch n = sb->total_freeblock;
218 1.1 uch
219 1.1 uch /* Superblock cache. */
220 1.1 uch for (i = sb->nfreeblock - 1; (i > 0) && (n >= 0); i--, n--) {
221 1.1 uch if ((ret = func(fs, ctx, sb->freeblock[i])))
222 1.1 uch return ret;
223 1.1 uch }
224 1.1 uch if (!n)
225 1.1 uch return 0;
226 1.1 uch blk = sb->freeblock[0];
227 1.1 uch if (!datablock_number_sanity(fs, blk)) {
228 1.1 uch pwarn("invalid freeblock list block#%d.", blk);
229 1.1 uch return 0;
230 1.1 uch }
231 1.1 uch do {
232 1.1 uch if (!blk)
233 1.1 uch break;
234 1.1 uch if (!(buf = scratch_read(fs, blk)))
235 1.1 uch return 0;
236 1.1 uch fb = (struct v7fs_freeblock *)buf;
237 1.1 uch
238 1.1 uch if (v7fs_freeblock_endian_convert(fs, fb)) {
239 1.1 uch pwarn("***corrupt freeblock list blk#%d", blk);
240 1.1 uch return 0;
241 1.1 uch }
242 1.1 uch
243 1.1 uch /* freeblock list is used as freeblock. */
244 1.1 uch if ((ret = func(fs, ctx, blk)))
245 1.1 uch return ret;
246 1.1 uch
247 1.1 uch blk = fb->freeblock[0]; /* next freeblock list */
248 1.1 uch
249 1.1 uch for (i = fb->nfreeblock - 1; (i > 0) && (n >= 0); i--, n--)
250 1.1 uch if ((ret = func(fs, ctx, fb->freeblock[i]))) {
251 1.1 uch scratch_free(fs, buf);
252 1.1 uch return ret;
253 1.1 uch }
254 1.1 uch scratch_free(fs, buf);
255 1.1 uch } while (n);
256 1.1 uch
257 1.1 uch return 0;
258 1.1 uch }
259 1.1 uch
260 1.1 uch int
261 1.1 uch freeblock_check(struct v7fs_self *fs)
262 1.1 uch {
263 1.1 uch struct v7fs_superblock *sb = &fs->superblock;
264 1.1 uch struct freeblock_arg freeblock_arg = { .blk = 0 };
265 1.1 uch v7fs_daddr_t blk;
266 1.1 uch v7fs_daddr_t datablock_size = sb->volume_size -
267 1.1 uch sb->datablock_start_sector;
268 1.1 uch int error = 0;
269 1.1 uch struct progress_arg progress_arg = { .label = "freeblock", .tick =
270 1.1 uch sb->total_freeblock / PROGRESS_BAR_GRANULE };
271 1.1 uch
272 1.1 uch progress(&progress_arg);
273 1.1 uch v7fs_freeblock_foreach(fs, freeblock_subr_cnt, &freeblock_arg);
274 1.1 uch progress(&progress_arg);
275 1.1 uch
276 1.1 uch blk = freeblock_arg.blk;
277 1.1 uch pwarn("\ndatablock usage: %d/%d (%d)\n", datablock_size - blk,
278 1.1 uch datablock_size, blk);
279 1.1 uch
280 1.1 uch if (sb->total_freeblock != blk) {
281 1.1 uch pwarn("corrupt # of freeblocks. %d(sb) != %d(cnt)",
282 1.1 uch sb->total_freeblock, blk);
283 1.1 uch if (reply_trivial("CORRECT?")) {
284 1.1 uch sb->total_freeblock = blk;
285 1.1 uch sb->modified = 1;
286 1.1 uch v7fs_superblock_writeback(fs);
287 1.1 uch } else {
288 1.1 uch error = FSCK_EXIT_CHECK_FAILED;
289 1.1 uch }
290 1.1 uch }
291 1.1 uch
292 1.1 uch
293 1.1 uch return error;
294 1.1 uch }
295