v7fs_datablock.c revision 1.2 1 1.2 uch /* $NetBSD: v7fs_datablock.c,v 1.2 2011/07/13 12:18:22 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.2 uch __KERNEL_RCSID(0, "$NetBSD: v7fs_datablock.c,v 1.2 2011/07/13 12:18:22 uch Exp $");
34 1.1 uch #if defined _KERNEL_OPT
35 1.1 uch #include "opt_v7fs.h"
36 1.1 uch #endif
37 1.1 uch
38 1.1 uch #include <sys/types.h>
39 1.1 uch #ifdef _KERNEL
40 1.1 uch #include <sys/systm.h>
41 1.1 uch #include <sys/param.h>
42 1.1 uch #else
43 1.1 uch #include <stdio.h>
44 1.1 uch #include <string.h>
45 1.1 uch #include <errno.h>
46 1.1 uch #endif
47 1.1 uch
48 1.1 uch #include "v7fs.h"
49 1.1 uch #include "v7fs_impl.h"
50 1.1 uch #include "v7fs_endian.h"
51 1.1 uch #include "v7fs_inode.h"
52 1.1 uch #include "v7fs_datablock.h"
53 1.1 uch #include "v7fs_superblock.h"
54 1.1 uch
55 1.1 uch #ifdef V7FS_DATABLOCK_DEBUG
56 1.1 uch #define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args)
57 1.1 uch #else
58 1.1 uch #define DPRINTF(fmt, args...) ((void)0)
59 1.1 uch #endif
60 1.1 uch
61 1.1 uch struct v7fs_daddr_map {
62 1.1 uch int level; /* direct, index1, index2, index3 */
63 1.1 uch v7fs_daddr_t index[3];
64 1.1 uch };
65 1.1 uch
66 1.1 uch static int v7fs_datablock_addr(size_t, struct v7fs_daddr_map *);
67 1.1 uch static int v7fs_datablock_deallocate(struct v7fs_self *, v7fs_daddr_t);
68 1.1 uch static int loop1(struct v7fs_self *, v7fs_daddr_t, size_t *,
69 1.1 uch int (*)(struct v7fs_self *, void *, v7fs_daddr_t, size_t), void *);
70 1.1 uch static int loop2(struct v7fs_self *, v7fs_daddr_t, size_t *,
71 1.1 uch int (*)(struct v7fs_self *, void *, v7fs_daddr_t, size_t), void *);
72 1.1 uch static v7fs_daddr_t link(struct v7fs_self *, v7fs_daddr_t, int);
73 1.1 uch static v7fs_daddr_t add_leaf(struct v7fs_self *, v7fs_daddr_t, int);
74 1.1 uch static v7fs_daddr_t unlink(struct v7fs_self *, v7fs_daddr_t, int);
75 1.1 uch static v7fs_daddr_t remove_leaf(struct v7fs_self *, v7fs_daddr_t, int);
76 1.1 uch static v7fs_daddr_t remove_self(struct v7fs_self *, v7fs_daddr_t);
77 1.1 uch
78 1.1 uch #ifdef V7FS_DATABLOCK_DEBUG
79 1.1 uch void daddr_map_dump(const struct v7fs_daddr_map *);
80 1.1 uch #else
81 1.1 uch #define daddr_map_dump(x) ((void)0)
82 1.1 uch #endif
83 1.1 uch
84 1.1 uch bool
85 1.1 uch datablock_number_sanity(const struct v7fs_self *fs, v7fs_daddr_t blk)
86 1.1 uch {
87 1.1 uch const struct v7fs_superblock *sb = &fs->superblock;
88 1.1 uch bool ok = (blk >= sb->datablock_start_sector) &&
89 1.1 uch (blk < sb->volume_size);
90 1.1 uch
91 1.1 uch #ifdef V7FS_DATABLOCK_DEBUG
92 1.1 uch if (!ok) {
93 1.1 uch DPRINTF("Bad data block #%d\n", blk);
94 1.1 uch }
95 1.1 uch #endif
96 1.1 uch
97 1.1 uch return ok;
98 1.1 uch }
99 1.1 uch
100 1.1 uch int
101 1.1 uch v7fs_datablock_allocate(struct v7fs_self *fs, v7fs_daddr_t *block_number)
102 1.1 uch {
103 1.1 uch struct v7fs_superblock *sb = &fs->superblock;
104 1.1 uch v7fs_daddr_t blk;
105 1.1 uch int error = 0;
106 1.1 uch
107 1.1 uch *block_number = 0;
108 1.1 uch SUPERB_LOCK(fs);
109 1.1 uch do {
110 1.1 uch if (!sb->total_freeblock) {
111 1.1 uch DPRINTF("free block exhausted!!!\n");
112 1.1 uch SUPERB_UNLOCK(fs);
113 1.1 uch return ENOSPC;
114 1.1 uch }
115 1.1 uch
116 1.1 uch /* Get free block from superblock cache. */
117 1.1 uch blk = sb->freeblock[--sb->nfreeblock];
118 1.1 uch sb->total_freeblock--;
119 1.1 uch sb->modified = 1;
120 1.1 uch
121 1.1 uch /* If nfreeblock is zero, it block is next freeblock link. */
122 1.1 uch if (sb->nfreeblock == 0) {
123 1.1 uch if ((error = v7fs_freeblock_update(fs, blk))) {
124 1.1 uch DPRINTF("no freeblock!!!\n");
125 1.1 uch SUPERB_UNLOCK(fs);
126 1.1 uch return error;
127 1.1 uch }
128 1.1 uch /* This freeblock link is no longer required. */
129 1.1 uch /* use as data block. */
130 1.1 uch }
131 1.1 uch } while (!datablock_number_sanity(fs, blk)); /* skip bogus block. */
132 1.1 uch SUPERB_UNLOCK(fs);
133 1.1 uch
134 1.1 uch DPRINTF("Get freeblock %d\n", blk);
135 1.1 uch /* Zero clear datablock. */
136 1.1 uch void *buf;
137 1.1 uch if (!(buf = scratch_read(fs, blk)))
138 1.1 uch return EIO;
139 1.1 uch memset(buf, 0, V7FS_BSIZE);
140 1.1 uch if (!fs->io.write(fs->io.cookie, buf, blk))
141 1.1 uch error = EIO;
142 1.1 uch scratch_free(fs, buf);
143 1.1 uch
144 1.1 uch if (error == 0)
145 1.1 uch *block_number = blk;
146 1.1 uch
147 1.1 uch return error;
148 1.1 uch }
149 1.1 uch
150 1.1 uch static int
151 1.1 uch v7fs_datablock_deallocate(struct v7fs_self *fs, v7fs_daddr_t blk)
152 1.1 uch {
153 1.1 uch struct v7fs_superblock *sb = &fs->superblock;
154 1.1 uch void *buf;
155 1.1 uch int error = 0;
156 1.1 uch
157 1.1 uch if (!datablock_number_sanity(fs, blk))
158 1.1 uch return EIO;
159 1.1 uch
160 1.1 uch /* Add to in-core freelist. */
161 1.1 uch SUPERB_LOCK(fs);
162 1.1 uch if (sb->nfreeblock < V7FS_MAX_FREEBLOCK) {
163 1.1 uch sb->freeblock[sb->nfreeblock++] = blk;
164 1.1 uch sb->total_freeblock++;
165 1.1 uch sb->modified = 1;
166 1.1 uch DPRINTF("n_freeblock=%d\n", sb->total_freeblock);
167 1.1 uch SUPERB_UNLOCK(fs);
168 1.1 uch return 0;
169 1.1 uch }
170 1.1 uch
171 1.1 uch /* No space to push. */
172 1.1 uch /* Make this block to freeblock list.and current cache moved to this. */
173 1.1 uch if (!(buf = scratch_read(fs, blk))) {
174 1.1 uch SUPERB_UNLOCK(fs);
175 1.1 uch return EIO; /* Fatal */
176 1.1 uch }
177 1.1 uch
178 1.1 uch struct v7fs_freeblock *fb = (struct v7fs_freeblock *)buf;
179 1.1 uch fb->nfreeblock = V7FS_MAX_FREEBLOCK;
180 1.1 uch int i;
181 1.1 uch for (i = 0; i < V7FS_MAX_FREEBLOCK; i++)
182 1.1 uch fb->freeblock[i] = V7FS_VAL32(fs, sb->freeblock[i]);
183 1.1 uch
184 1.1 uch if (!fs->io.write(fs->io.cookie, (uint8_t *)fb, blk)) {
185 1.1 uch error = EIO; /* Fatal */
186 1.1 uch } else {
187 1.1 uch /* Link. on next allocate, this block is used as datablock, */
188 1.1 uch /* and swap outed freeblock list is restored. */
189 1.1 uch sb->freeblock[0] = blk;
190 1.1 uch sb->nfreeblock = 1;
191 1.1 uch sb->total_freeblock++;
192 1.1 uch sb->modified = 1;
193 1.1 uch DPRINTF("n_freeblock=%d\n", sb->total_freeblock);
194 1.1 uch }
195 1.1 uch SUPERB_UNLOCK(fs);
196 1.1 uch scratch_free(fs, buf);
197 1.1 uch
198 1.1 uch return error;
199 1.1 uch }
200 1.1 uch
201 1.1 uch static int
202 1.1 uch v7fs_datablock_addr(size_t sz, struct v7fs_daddr_map *map)
203 1.1 uch {
204 1.1 uch #define NIDX V7FS_DADDR_PER_BLOCK
205 1.1 uch #define DIRECT_SZ (V7FS_NADDR_DIRECT * V7FS_BSIZE)
206 1.1 uch #define IDX1_SZ (NIDX * V7FS_BSIZE)
207 1.1 uch #define IDX2_SZ (NIDX * NIDX * V7FS_BSIZE)
208 1.1 uch #define ROUND(x, a) ((((x) + ((a) - 1)) & ~((a) - 1)))
209 1.1 uch if (!sz) {
210 1.1 uch map->level = 0;
211 1.1 uch map->index[0] = 0;
212 1.1 uch return 0;
213 1.1 uch }
214 1.1 uch
215 1.1 uch sz = V7FS_ROUND_BSIZE(sz);
216 1.1 uch
217 1.1 uch /* Direct */
218 1.1 uch if (sz <= DIRECT_SZ) {
219 1.1 uch map->level = 0;
220 1.1 uch map->index[0] = (sz >> V7FS_BSHIFT) - 1;
221 1.1 uch return 0;
222 1.1 uch }
223 1.1 uch /* Index 1 */
224 1.1 uch sz -= DIRECT_SZ;
225 1.1 uch
226 1.1 uch if (sz <= IDX1_SZ) {
227 1.1 uch map->level = 1;
228 1.1 uch map->index[0] = (sz >> V7FS_BSHIFT) - 1;
229 1.1 uch return 0;
230 1.1 uch }
231 1.1 uch sz -= IDX1_SZ;
232 1.1 uch
233 1.1 uch /* Index 2 */
234 1.1 uch if (sz <= IDX2_SZ) {
235 1.1 uch map->level = 2;
236 1.1 uch map->index[0] = ROUND(sz, IDX1_SZ) / IDX1_SZ - 1;
237 1.1 uch map->index[1] = ((sz - (map->index[0] * IDX1_SZ)) >>
238 1.1 uch V7FS_BSHIFT) - 1;
239 1.1 uch return 0;
240 1.1 uch }
241 1.1 uch sz -= IDX2_SZ;
242 1.1 uch
243 1.1 uch /* Index 3 */
244 1.1 uch map->level = 3;
245 1.1 uch map->index[0] = ROUND(sz, IDX2_SZ) / IDX2_SZ - 1;
246 1.1 uch sz -= map->index[0] * IDX2_SZ;
247 1.1 uch map->index[1] = ROUND(sz, IDX1_SZ) / IDX1_SZ - 1;
248 1.1 uch sz -= map->index[1] * IDX1_SZ;
249 1.1 uch map->index[2] = (sz >> V7FS_BSHIFT) - 1;
250 1.1 uch
251 1.1 uch return map->index[2] >= NIDX ? ENOSPC : 0;
252 1.1 uch }
253 1.1 uch
254 1.1 uch int
255 1.1 uch v7fs_datablock_foreach(struct v7fs_self *fs, struct v7fs_inode *p,
256 1.1 uch int (*func)(struct v7fs_self *, void *, v7fs_daddr_t, size_t), void *ctx)
257 1.1 uch {
258 1.1 uch size_t i;
259 1.1 uch v7fs_daddr_t blk, blk2;
260 1.1 uch size_t filesize;
261 1.1 uch bool last;
262 1.1 uch int ret;
263 1.1 uch
264 1.1 uch if (!(filesize = v7fs_inode_filesize(p)))
265 1.2 uch return V7FS_ITERATOR_END;
266 1.1 uch #ifdef V7FS_DATABLOCK_DEBUG
267 1.1 uch size_t sz = filesize;
268 1.1 uch #endif
269 1.1 uch
270 1.1 uch /* Direct */
271 1.1 uch for (i = 0; i < V7FS_NADDR_DIRECT; i++, filesize -= V7FS_BSIZE) {
272 1.1 uch blk = p->addr[i];
273 1.1 uch if (!datablock_number_sanity(fs, blk)) {
274 1.1 uch DPRINTF("inode#%d direct=%zu filesize=%zu\n",
275 1.1 uch p->inode_number, i, sz);
276 1.1 uch return EIO;
277 1.1 uch }
278 1.1 uch
279 1.1 uch last = filesize <= V7FS_BSIZE;
280 1.1 uch if ((ret = func(fs, ctx, blk, last ? filesize : V7FS_BSIZE)))
281 1.1 uch return ret;
282 1.1 uch if (last)
283 1.1 uch return V7FS_ITERATOR_END;
284 1.1 uch }
285 1.1 uch
286 1.1 uch /* Index 1 */
287 1.1 uch blk = p->addr[V7FS_NADDR_INDEX1];
288 1.1 uch if (!datablock_number_sanity(fs, blk))
289 1.1 uch return EIO;
290 1.1 uch
291 1.1 uch if ((ret = loop1(fs, blk, &filesize, func, ctx)))
292 1.1 uch return ret;
293 1.1 uch
294 1.1 uch /* Index 2 */
295 1.1 uch blk = p->addr[V7FS_NADDR_INDEX2];
296 1.1 uch if (!datablock_number_sanity(fs, blk))
297 1.1 uch return EIO;
298 1.1 uch
299 1.1 uch if ((ret = loop2(fs, blk, &filesize, func, ctx)))
300 1.1 uch return ret;
301 1.1 uch
302 1.1 uch /* Index 3 */
303 1.1 uch blk = p->addr[V7FS_NADDR_INDEX3];
304 1.1 uch if (!datablock_number_sanity(fs, blk))
305 1.1 uch return EIO;
306 1.1 uch
307 1.1 uch for (i = 0; i < V7FS_DADDR_PER_BLOCK; i++) {
308 1.1 uch blk2 = link(fs, blk, i);
309 1.1 uch if (!datablock_number_sanity(fs, blk))
310 1.1 uch return EIO;
311 1.1 uch
312 1.1 uch if ((ret = loop2(fs, blk2, &filesize, func, ctx)))
313 1.1 uch return ret;
314 1.1 uch }
315 1.1 uch
316 1.1 uch return EFBIG;
317 1.1 uch }
318 1.1 uch
319 1.1 uch static int
320 1.1 uch loop2(struct v7fs_self *fs, v7fs_daddr_t listblk, size_t *filesize,
321 1.1 uch int (*func)(struct v7fs_self *, void *, v7fs_daddr_t, size_t), void *ctx)
322 1.1 uch {
323 1.1 uch v7fs_daddr_t blk;
324 1.1 uch int ret;
325 1.1 uch size_t j;
326 1.1 uch
327 1.1 uch for (j = 0; j < V7FS_DADDR_PER_BLOCK; j++) {
328 1.1 uch blk = link(fs, listblk, j);
329 1.1 uch if (!datablock_number_sanity(fs, blk))
330 1.1 uch return EIO;
331 1.1 uch if ((ret = loop1(fs, blk, filesize, func, ctx)))
332 1.1 uch return ret;
333 1.1 uch }
334 1.1 uch
335 1.1 uch return 0;
336 1.1 uch }
337 1.1 uch
338 1.1 uch static int
339 1.1 uch loop1(struct v7fs_self *fs, v7fs_daddr_t listblk, size_t *filesize,
340 1.1 uch int (*func)(struct v7fs_self *, void *, v7fs_daddr_t, size_t), void *ctx)
341 1.1 uch {
342 1.1 uch v7fs_daddr_t blk;
343 1.1 uch bool last;
344 1.1 uch int ret;
345 1.1 uch size_t k;
346 1.1 uch
347 1.1 uch for (k = 0; k < V7FS_DADDR_PER_BLOCK; k++, *filesize -= V7FS_BSIZE) {
348 1.1 uch blk = link(fs, listblk, k);
349 1.1 uch if (!datablock_number_sanity(fs, blk))
350 1.1 uch return EIO;
351 1.1 uch last = *filesize <= V7FS_BSIZE;
352 1.1 uch if ((ret = func(fs, ctx, blk, last ? *filesize : V7FS_BSIZE)))
353 1.1 uch return ret;
354 1.1 uch if (last)
355 1.1 uch return V7FS_ITERATOR_END;
356 1.1 uch }
357 1.1 uch
358 1.1 uch return 0;
359 1.1 uch }
360 1.1 uch
361 1.1 uch v7fs_daddr_t
362 1.1 uch v7fs_datablock_last(struct v7fs_self *fs, struct v7fs_inode *inode,
363 1.1 uch v7fs_off_t ofs)
364 1.1 uch {
365 1.1 uch struct v7fs_daddr_map map;
366 1.1 uch v7fs_daddr_t blk = 0;
367 1.1 uch v7fs_daddr_t *addr = inode->addr;
368 1.1 uch
369 1.1 uch /* Inquire last data block location. */
370 1.1 uch if (v7fs_datablock_addr(ofs, &map) != 0)
371 1.1 uch return 0;
372 1.1 uch
373 1.1 uch switch (map.level)
374 1.1 uch {
375 1.1 uch case 0: /*Direct */
376 1.1 uch blk = inode->addr[map.index[0]];
377 1.1 uch break;
378 1.1 uch case 1: /*Index1 */
379 1.1 uch blk = link(fs, addr[V7FS_NADDR_INDEX1], map.index[0]);
380 1.1 uch break;
381 1.1 uch case 2: /*Index2 */
382 1.1 uch blk = link(fs, link(fs, addr[V7FS_NADDR_INDEX2], map.index[0]),
383 1.1 uch map.index[1]);
384 1.1 uch break;
385 1.1 uch case 3: /*Index3 */
386 1.1 uch blk = link(fs, link(fs, link(fs, addr[V7FS_NADDR_INDEX3],
387 1.1 uch map.index[0]), map.index[1]), map.index[2]);
388 1.1 uch break;
389 1.1 uch }
390 1.1 uch
391 1.1 uch return blk;
392 1.1 uch }
393 1.1 uch
394 1.1 uch int
395 1.1 uch v7fs_datablock_expand(struct v7fs_self *fs, struct v7fs_inode *inode, size_t sz)
396 1.1 uch {
397 1.1 uch size_t old_filesize = inode->filesize;
398 1.1 uch size_t new_filesize = old_filesize + sz;
399 1.1 uch struct v7fs_daddr_map oldmap, newmap;
400 1.1 uch v7fs_daddr_t blk, idxblk;
401 1.1 uch int error;
402 1.1 uch v7fs_daddr_t old_nblk = V7FS_ROUND_BSIZE(old_filesize) >> V7FS_BSHIFT;
403 1.1 uch v7fs_daddr_t new_nblk = V7FS_ROUND_BSIZE(new_filesize) >> V7FS_BSHIFT;
404 1.1 uch
405 1.1 uch if (old_nblk == new_nblk) {
406 1.1 uch inode->filesize += sz;
407 1.1 uch v7fs_inode_writeback(fs, inode);
408 1.1 uch return 0; /* no need to expand. */
409 1.1 uch }
410 1.1 uch struct v7fs_inode backup = *inode;
411 1.1 uch v7fs_daddr_t required_blk = new_nblk - old_nblk;
412 1.1 uch
413 1.1 uch DPRINTF("%zu->%zu, required block=%d\n", old_filesize, new_filesize,
414 1.1 uch required_blk);
415 1.1 uch
416 1.1 uch v7fs_datablock_addr(old_filesize, &oldmap);
417 1.1 uch v7fs_daddr_t i;
418 1.1 uch for (i = 0; i < required_blk; i++) {
419 1.1 uch v7fs_datablock_addr(old_filesize + (i+1) * V7FS_BSIZE, &newmap);
420 1.1 uch daddr_map_dump(&oldmap);
421 1.1 uch daddr_map_dump(&newmap);
422 1.1 uch
423 1.1 uch if (oldmap.level != newmap.level) {
424 1.1 uch /* Allocate index area */
425 1.1 uch if ((error = v7fs_datablock_allocate(fs, &idxblk)))
426 1.1 uch return error;
427 1.1 uch
428 1.1 uch switch (newmap.level) {
429 1.1 uch case 1:
430 1.1 uch DPRINTF("0->1\n");
431 1.1 uch inode->addr[V7FS_NADDR_INDEX1] = idxblk;
432 1.1 uch blk = add_leaf(fs, idxblk, 0);
433 1.1 uch break;
434 1.1 uch case 2:
435 1.1 uch DPRINTF("1->2\n");
436 1.1 uch inode->addr[V7FS_NADDR_INDEX2] = idxblk;
437 1.1 uch blk = add_leaf(fs, add_leaf(fs, idxblk, 0), 0);
438 1.1 uch break;
439 1.1 uch case 3:
440 1.1 uch DPRINTF("2->3\n");
441 1.1 uch inode->addr[V7FS_NADDR_INDEX3] = idxblk;
442 1.1 uch blk = add_leaf(fs, add_leaf(fs, add_leaf(fs,
443 1.1 uch idxblk, 0), 0), 0);
444 1.1 uch break;
445 1.1 uch }
446 1.1 uch } else {
447 1.1 uch switch (newmap.level) {
448 1.1 uch case 0:
449 1.1 uch if ((error = v7fs_datablock_allocate(fs, &blk)))
450 1.1 uch return error;
451 1.1 uch inode->addr[newmap.index[0]] = blk;
452 1.1 uch DPRINTF("direct index %d = blk%d\n",
453 1.1 uch newmap.index[0], blk);
454 1.1 uch break;
455 1.1 uch case 1:
456 1.1 uch idxblk = inode->addr[V7FS_NADDR_INDEX1];
457 1.1 uch blk = add_leaf(fs, idxblk, newmap.index[0]);
458 1.1 uch break;
459 1.1 uch case 2:
460 1.1 uch idxblk = inode->addr[V7FS_NADDR_INDEX2];
461 1.1 uch if (oldmap.index[0] != newmap.index[0])
462 1.1 uch add_leaf(fs, idxblk, newmap.index[0]);
463 1.1 uch blk = add_leaf(fs, link(fs,idxblk,
464 1.1 uch newmap.index[0]), newmap.index[1]);
465 1.1 uch break;
466 1.1 uch case 3:
467 1.1 uch idxblk = inode->addr[V7FS_NADDR_INDEX3];
468 1.1 uch
469 1.1 uch if (oldmap.index[0] != newmap.index[0])
470 1.1 uch add_leaf(fs, idxblk, newmap.index[0]);
471 1.1 uch
472 1.1 uch if (oldmap.index[1] != newmap.index[1])
473 1.1 uch add_leaf(fs, link(fs, idxblk,
474 1.1 uch newmap.index[0]), newmap.index[1]);
475 1.1 uch blk = add_leaf(fs, link(fs, link(fs, idxblk,
476 1.1 uch newmap.index[0]), newmap.index[1]),
477 1.1 uch newmap.index[2]);
478 1.1 uch break;
479 1.1 uch }
480 1.1 uch }
481 1.1 uch if (!blk) {
482 1.1 uch *inode = backup; /* structure copy; */
483 1.1 uch return ENOSPC;
484 1.1 uch }
485 1.1 uch oldmap = newmap;
486 1.1 uch }
487 1.1 uch inode->filesize += sz;
488 1.1 uch v7fs_inode_writeback(fs, inode);
489 1.1 uch
490 1.1 uch return 0;
491 1.1 uch }
492 1.1 uch
493 1.1 uch static v7fs_daddr_t
494 1.1 uch link(struct v7fs_self *fs, v7fs_daddr_t listblk, int n)
495 1.1 uch {
496 1.1 uch v7fs_daddr_t *list;
497 1.1 uch v7fs_daddr_t blk;
498 1.1 uch void *buf;
499 1.1 uch
500 1.1 uch if (!datablock_number_sanity(fs, listblk))
501 1.1 uch return 0;
502 1.1 uch if (!(buf = scratch_read(fs, listblk)))
503 1.1 uch return 0;
504 1.1 uch list = (v7fs_daddr_t *)buf;
505 1.1 uch blk = V7FS_VAL32(fs, list[n]);
506 1.1 uch scratch_free(fs, buf);
507 1.1 uch
508 1.1 uch if (!datablock_number_sanity(fs, blk))
509 1.1 uch return 0;
510 1.1 uch
511 1.1 uch return blk;
512 1.1 uch }
513 1.1 uch
514 1.1 uch static v7fs_daddr_t
515 1.1 uch add_leaf(struct v7fs_self *fs, v7fs_daddr_t up, int idx)
516 1.1 uch {
517 1.1 uch v7fs_daddr_t newblk;
518 1.1 uch v7fs_daddr_t *daddr_list;
519 1.1 uch int error = 0;
520 1.1 uch void *buf;
521 1.1 uch
522 1.1 uch if (!up)
523 1.1 uch return 0;
524 1.1 uch if (!datablock_number_sanity(fs, up))
525 1.1 uch return 0;
526 1.1 uch
527 1.1 uch if ((error = v7fs_datablock_allocate(fs, &newblk)))
528 1.1 uch return 0;
529 1.1 uch if (!(buf = scratch_read(fs, up)))
530 1.1 uch return 0;
531 1.1 uch daddr_list = (v7fs_daddr_t *)buf;
532 1.1 uch daddr_list[idx] = V7FS_VAL32(fs, newblk);
533 1.1 uch if (!fs->io.write(fs->io.cookie, buf, up))
534 1.1 uch newblk = 0;
535 1.1 uch scratch_free(fs, buf);
536 1.1 uch
537 1.1 uch return newblk;
538 1.1 uch }
539 1.1 uch
540 1.1 uch int
541 1.1 uch v7fs_datablock_contract(struct v7fs_self *fs, struct v7fs_inode *inode,
542 1.1 uch size_t sz)
543 1.1 uch {
544 1.1 uch size_t old_filesize = inode->filesize;
545 1.1 uch size_t new_filesize = old_filesize - sz;
546 1.1 uch struct v7fs_daddr_map oldmap, newmap;
547 1.1 uch v7fs_daddr_t blk, idxblk;
548 1.1 uch int error = 0;
549 1.1 uch v7fs_daddr_t old_nblk = V7FS_ROUND_BSIZE(old_filesize) >> V7FS_BSHIFT;
550 1.1 uch v7fs_daddr_t new_nblk = V7FS_ROUND_BSIZE(new_filesize) >> V7FS_BSHIFT;
551 1.1 uch
552 1.1 uch if (old_nblk == new_nblk) {
553 1.1 uch inode->filesize -= sz;
554 1.1 uch v7fs_inode_writeback(fs, inode);
555 1.1 uch return 0; /* no need to contract; */
556 1.1 uch }
557 1.1 uch v7fs_daddr_t erase_blk = old_nblk - new_nblk;
558 1.1 uch
559 1.1 uch DPRINTF("%zu->%zu # of erased block=%d\n", old_filesize, new_filesize,
560 1.1 uch erase_blk);
561 1.1 uch
562 1.1 uch v7fs_datablock_addr(old_filesize, &oldmap);
563 1.1 uch v7fs_daddr_t i;
564 1.1 uch for (i = 0; i < erase_blk; i++) {
565 1.1 uch v7fs_datablock_addr(old_filesize - (i+1) * V7FS_BSIZE, &newmap);
566 1.1 uch
567 1.1 uch if (oldmap.level != newmap.level) {
568 1.1 uch switch (newmap.level) {
569 1.1 uch case 0: /*1->0 */
570 1.1 uch DPRINTF("1->0\n");
571 1.1 uch idxblk = inode->addr[V7FS_NADDR_INDEX1];
572 1.1 uch inode->addr[V7FS_NADDR_INDEX1] = 0;
573 1.1 uch error = v7fs_datablock_deallocate(fs,
574 1.1 uch remove_self(fs, idxblk));
575 1.1 uch break;
576 1.1 uch case 1: /*2->1 */
577 1.1 uch DPRINTF("2->1\n");
578 1.1 uch idxblk = inode->addr[V7FS_NADDR_INDEX2];
579 1.1 uch inode->addr[V7FS_NADDR_INDEX2] = 0;
580 1.1 uch error = v7fs_datablock_deallocate(fs,
581 1.1 uch remove_self(fs, remove_self(fs, idxblk)));
582 1.1 uch break;
583 1.1 uch case 2:/*3->2 */
584 1.1 uch DPRINTF("3->2\n");
585 1.1 uch idxblk = inode->addr[V7FS_NADDR_INDEX3];
586 1.1 uch inode->addr[V7FS_NADDR_INDEX3] = 0;
587 1.1 uch error = v7fs_datablock_deallocate(fs,
588 1.1 uch remove_self(fs, remove_self(fs,
589 1.1 uch remove_self(fs, idxblk))));
590 1.1 uch break;
591 1.1 uch }
592 1.1 uch } else {
593 1.1 uch switch (newmap.level) {
594 1.1 uch case 0:
595 1.1 uch DPRINTF("[0] %d\n", oldmap.index[0]);
596 1.1 uch blk = inode->addr[oldmap.index[0]];
597 1.1 uch error = v7fs_datablock_deallocate(fs, blk);
598 1.1 uch break;
599 1.1 uch case 1:
600 1.1 uch DPRINTF("[1] %d\n", oldmap.index[0]);
601 1.1 uch idxblk = inode->addr[V7FS_NADDR_INDEX1];
602 1.1 uch remove_leaf(fs, idxblk, oldmap.index[0]);
603 1.1 uch
604 1.1 uch break;
605 1.1 uch case 2:
606 1.1 uch DPRINTF("[2] %d %d\n", oldmap.index[0],
607 1.1 uch oldmap.index[1]);
608 1.1 uch idxblk = inode->addr[V7FS_NADDR_INDEX2];
609 1.1 uch remove_leaf(fs, link(fs, idxblk,
610 1.1 uch oldmap.index[0]), oldmap.index[1]);
611 1.1 uch if (oldmap.index[0] != newmap.index[0]) {
612 1.1 uch remove_leaf(fs, idxblk,
613 1.1 uch oldmap.index[0]);
614 1.1 uch }
615 1.1 uch break;
616 1.1 uch case 3:
617 1.1 uch DPRINTF("[2] %d %d %d\n", oldmap.index[0],
618 1.1 uch oldmap.index[1], oldmap.index[2]);
619 1.1 uch idxblk = inode->addr[V7FS_NADDR_INDEX3];
620 1.1 uch remove_leaf(fs, link(fs, link(fs, idxblk,
621 1.1 uch oldmap.index[0]), oldmap.index[1]),
622 1.1 uch oldmap.index[2]);
623 1.1 uch
624 1.1 uch if (oldmap.index[1] != newmap.index[1]) {
625 1.1 uch remove_leaf(fs, link(fs, idxblk,
626 1.1 uch oldmap.index[0]), oldmap.index[1]);
627 1.1 uch }
628 1.1 uch if (oldmap.index[0] != newmap.index[0]) {
629 1.1 uch remove_leaf(fs, idxblk,
630 1.1 uch oldmap.index[0]);
631 1.1 uch }
632 1.1 uch break;
633 1.1 uch }
634 1.1 uch }
635 1.1 uch oldmap = newmap;
636 1.1 uch }
637 1.1 uch inode->filesize -= sz;
638 1.1 uch v7fs_inode_writeback(fs, inode);
639 1.1 uch
640 1.1 uch return error;
641 1.1 uch }
642 1.1 uch
643 1.1 uch static v7fs_daddr_t
644 1.1 uch unlink(struct v7fs_self *fs, v7fs_daddr_t idxblk, int n)
645 1.1 uch {
646 1.1 uch v7fs_daddr_t *daddr_list;
647 1.1 uch v7fs_daddr_t blk;
648 1.1 uch void *buf;
649 1.1 uch
650 1.1 uch if (!(buf = scratch_read(fs, idxblk)))
651 1.1 uch return 0;
652 1.1 uch daddr_list = (v7fs_daddr_t *)buf;
653 1.1 uch blk = V7FS_VAL32(fs, daddr_list[n]);
654 1.1 uch daddr_list[n] = 0;
655 1.1 uch fs->io.write(fs->io.cookie, buf, idxblk);
656 1.1 uch scratch_free(fs, buf);
657 1.1 uch
658 1.1 uch return blk; /* unlinked block. */
659 1.1 uch }
660 1.1 uch
661 1.1 uch static v7fs_daddr_t
662 1.1 uch remove_self(struct v7fs_self *fs, v7fs_daddr_t up)
663 1.1 uch {
664 1.1 uch v7fs_daddr_t down;
665 1.1 uch
666 1.1 uch if (!datablock_number_sanity(fs, up))
667 1.1 uch return 0;
668 1.1 uch
669 1.1 uch /* At 1st, remove from datablock list. */
670 1.1 uch down = unlink(fs, up, 0);
671 1.1 uch
672 1.1 uch /* link self to freelist. */
673 1.1 uch v7fs_datablock_deallocate(fs, up);
674 1.1 uch
675 1.1 uch return down;
676 1.1 uch }
677 1.1 uch
678 1.1 uch static v7fs_daddr_t
679 1.1 uch remove_leaf(struct v7fs_self *fs, v7fs_daddr_t up, int n)
680 1.1 uch {
681 1.1 uch v7fs_daddr_t down;
682 1.1 uch
683 1.1 uch if (!datablock_number_sanity(fs, up))
684 1.1 uch return 0;
685 1.1 uch
686 1.1 uch /* At 1st, remove from datablock list. */
687 1.1 uch down = unlink(fs, up, n);
688 1.1 uch
689 1.1 uch /* link leaf to freelist. */
690 1.1 uch v7fs_datablock_deallocate(fs, down);
691 1.1 uch
692 1.1 uch return down;
693 1.1 uch }
694 1.1 uch
695 1.1 uch int
696 1.1 uch v7fs_datablock_size_change(struct v7fs_self *fs, size_t newsz,
697 1.1 uch struct v7fs_inode *inode)
698 1.1 uch {
699 1.1 uch ssize_t diff = newsz - v7fs_inode_filesize(inode);
700 1.1 uch int error = 0;
701 1.1 uch
702 1.1 uch if (diff > 0)
703 1.1 uch error = v7fs_datablock_expand(fs, inode, diff);
704 1.1 uch else if (diff < 0)
705 1.1 uch error = v7fs_datablock_contract(fs, inode, -diff);
706 1.1 uch
707 1.1 uch return error;
708 1.1 uch }
709 1.1 uch
710 1.1 uch #ifdef V7FS_DATABLOCK_DEBUG
711 1.1 uch void
712 1.1 uch daddr_map_dump(const struct v7fs_daddr_map *map)
713 1.1 uch {
714 1.1 uch
715 1.1 uch DPRINTF("level %d ", map->level);
716 1.1 uch int m, n = !map->level ? 1 : map->level;
717 1.1 uch for (m = 0; m < n; m++)
718 1.1 uch printf("[%d]", map->index[m]);
719 1.1 uch printf("\n");
720 1.1 uch }
721 1.1 uch #endif
722