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