Home | History | Annotate | Line # | Download | only in nilfs
nilfs_subr.c revision 1.3
      1 /* $NetBSD: nilfs_subr.c,v 1.3 2009/07/29 13:23:23 reinoud Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2008, 2009 Reinoud Zandijk
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  *
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #ifndef lint
     31 __KERNEL_RCSID(0, "$NetBSD: nilfs_subr.c,v 1.3 2009/07/29 13:23:23 reinoud Exp $");
     32 #endif /* not lint */
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/namei.h>
     37 #include <sys/resourcevar.h>	/* defines plimit structure in proc struct */
     38 #include <sys/kernel.h>
     39 #include <sys/file.h>		/* define FWRITE ... */
     40 #include <sys/stat.h>
     41 #include <sys/buf.h>
     42 #include <sys/proc.h>
     43 #include <sys/mount.h>
     44 #include <sys/vnode.h>
     45 #include <sys/signalvar.h>
     46 #include <sys/malloc.h>
     47 #include <sys/dirent.h>
     48 #include <sys/lockf.h>
     49 #include <sys/kauth.h>
     50 #include <sys/dirhash.h>
     51 
     52 #include <miscfs/genfs/genfs.h>
     53 #include <uvm/uvm_extern.h>
     54 
     55 #include <fs/nilfs/nilfs_mount.h>
     56 #include "nilfs.h"
     57 #include "nilfs_subr.h"
     58 #include "nilfs_bswap.h"
     59 
     60 
     61 #define VTOI(vnode) ((struct nilfs_node *) (vnode)->v_data)
     62 
     63 /* basic calculators */
     64 uint64_t nilfs_get_segnum_of_block(struct nilfs_device *nilfsdev,
     65 	uint64_t blocknr)
     66 {
     67 	return blocknr / nilfs_rw32(nilfsdev->super.s_blocks_per_segment);
     68 }
     69 
     70 
     71 void
     72 nilfs_get_segment_range(struct nilfs_device *nilfsdev, uint64_t segnum,
     73         uint64_t *seg_start, uint64_t *seg_end)
     74 {
     75         uint64_t blks_per_seg;
     76 
     77         blks_per_seg = nilfs_rw64(nilfsdev->super.s_blocks_per_segment);
     78         *seg_start = blks_per_seg * segnum;
     79         *seg_end   = *seg_start + blks_per_seg -1;
     80         if (segnum == 0)
     81                 *seg_start = nilfs_rw64(nilfsdev->super.s_first_data_block);
     82 }
     83 
     84 
     85 void nilfs_calc_mdt_consts(struct nilfs_device *nilfsdev,
     86 	struct nilfs_mdt *mdt, int entry_size)
     87 {
     88 	uint32_t blocksize = nilfsdev->blocksize;
     89 
     90 	mdt->entries_per_group = blocksize * 8;	   /* bits in sector */
     91 	mdt->entries_per_block = blocksize / entry_size;
     92 
     93 	mdt->blocks_per_group  =
     94 		(mdt->entries_per_group -1) / mdt->entries_per_block + 1 + 1;
     95 	mdt->groups_per_desc_block =
     96 		blocksize / sizeof(struct nilfs_block_group_desc);
     97 	mdt->blocks_per_desc_block =
     98 		mdt->groups_per_desc_block * mdt->blocks_per_group + 1;
     99 }
    100 
    101 
    102 /* from NetBSD's src/sys/net/if_ethersubr.c */
    103 uint32_t
    104 crc32_le(uint32_t crc, const uint8_t *buf, size_t len)
    105 {
    106         static const uint32_t crctab[] = {
    107                 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
    108                 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
    109                 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
    110                 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
    111         };
    112         size_t i;
    113 
    114         for (i = 0; i < len; i++) {
    115                 crc ^= buf[i];
    116                 crc = (crc >> 4) ^ crctab[crc & 0xf];
    117                 crc = (crc >> 4) ^ crctab[crc & 0xf];
    118         }
    119 
    120         return (crc);
    121 }
    122 
    123 
    124 static int
    125 nilfs_calchash(uint64_t ino)
    126 {
    127 	return (int) ino;
    128 }
    129 
    130 
    131 /* dev reading */
    132 static int
    133 nilfs_dev_bread(struct nilfs_device *nilfsdev, uint64_t blocknr,
    134 	struct kauth_cred *cred, int flags, struct buf **bpp)
    135 {
    136 	int blk2dev = nilfsdev->blocksize / DEV_BSIZE;
    137 
    138 	return bread(nilfsdev->devvp, blocknr * blk2dev, nilfsdev->blocksize,
    139 		NOCRED, 0, bpp);
    140 }
    141 
    142 
    143 /* read on a node */
    144 int
    145 nilfs_bread(struct nilfs_node *node, uint64_t blocknr,
    146 	struct kauth_cred *cred, int flags, struct buf **bpp)
    147 {
    148 	return bread(node->vnode, blocknr, node->nilfsdev->blocksize,
    149 		cred, flags, bpp);
    150 }
    151 
    152 
    153 /* segment-log reading */
    154 int
    155 nilfs_get_segment_log(struct nilfs_device *nilfsdev, uint64_t *blocknr,
    156 	uint64_t *offset, struct buf **bpp, int len, void *blob)
    157 {
    158 	int blocksize = nilfsdev->blocksize;
    159 	int error;
    160 
    161 	KASSERT(len <= blocksize);
    162 
    163 	if (*offset + len > blocksize) {
    164 		*blocknr = *blocknr + 1;
    165 		*offset = 0;
    166 	}
    167 	if (*offset == 0) {
    168 		if (*bpp)
    169 			brelse(*bpp, BC_AGE);
    170 		/* read in block */
    171 		error = nilfs_dev_bread(nilfsdev, *blocknr, NOCRED, 0, bpp);
    172 		if (error)
    173 			return error;
    174 	}
    175 	memcpy(blob, ((uint8_t *) (*bpp)->b_data) + *offset, len);
    176 	*offset += len;
    177 
    178 	return 0;
    179 }
    180 
    181 /* -------------------------------------------------------------------------- */
    182 
    183 /* btree operations */
    184 
    185 static int
    186 nilfs_btree_lookup_level(struct nilfs_node *node, uint64_t lblocknr,
    187 		uint64_t btree_vblknr, int level, uint64_t *vblocknr)
    188 {
    189 	struct nilfs_device *nilfsdev = node->nilfsdev;
    190 	struct nilfs_btree_node *btree_hdr;
    191 	struct buf *bp;
    192 	uint64_t btree_blknr;
    193 	uint64_t *dkeys, *dptrs, child_btree_blk;
    194 	uint8_t  *pos;
    195 	int i, error, selected;
    196 
    197 	DPRINTF(TRANSLATE, ("nilfs_btree_lookup_level ino %"PRIu64", "
    198 		"lblocknr %"PRIu64", btree_vblknr %"PRIu64", level %d\n",
    199 		node->ino, lblocknr, btree_vblknr, level));
    200 
    201 	/* translate btree_vblknr */
    202 	error = nilfs_nvtop(node, 1, &btree_vblknr, &btree_blknr);
    203 	if (error)
    204 		return error;
    205 
    206 	/* get our block */
    207 	error = nilfs_dev_bread(nilfsdev, btree_blknr, NOCRED, 0, &bp);
    208 	if (error) {
    209 		brelse(bp, BC_AGE);
    210 		return error;
    211 	}
    212 
    213 	btree_hdr = (struct nilfs_btree_node *) bp->b_data;
    214 	pos =   (uint8_t *) bp->b_data +
    215 		sizeof(struct nilfs_btree_node) +
    216 		NILFS_BTREE_NODE_EXTRA_PAD_SIZE;
    217 	dkeys = (uint64_t *) pos;
    218 	dptrs = dkeys + NILFS_BTREE_NODE_NCHILDREN_MAX(nilfsdev->blocksize);
    219 
    220 	assert((btree_hdr->bn_flags & NILFS_BTREE_NODE_ROOT) == 0);
    221 
    222 	/* select matching child XXX could use binary search */
    223 	selected = 0;
    224 	for (i = 0; i < nilfs_rw16(btree_hdr->bn_nchildren); i++) {
    225 		if (dkeys[i] > lblocknr)
    226 			break;
    227 		selected = i;
    228 	}
    229 
    230 	if (level == 1) {
    231 		/* if found it mapped */
    232 		if (dkeys[selected] == lblocknr)
    233 			*vblocknr = dptrs[selected];
    234 		brelse(bp, BC_AGE);
    235 		return 0;
    236 	}
    237 
    238 	/* lookup in selected child */
    239 	assert(dkeys[selected] <= lblocknr);
    240 	child_btree_blk = dptrs[selected];
    241 	brelse(bp, BC_AGE);
    242 
    243 	return nilfs_btree_lookup_level(node, lblocknr,
    244 			child_btree_blk, btree_hdr->bn_level-1, vblocknr);
    245 }
    246 
    247 
    248 /* internal function */
    249 static int
    250 nilfs_btree_lookup(struct nilfs_node *node, uint64_t lblocknr,
    251 		uint64_t *vblocknr)
    252 {
    253 	struct nilfs_inode  *inode    = &node->inode;
    254 	struct nilfs_btree_node  *btree_hdr;
    255 	uint64_t *dkeys, *dptrs, *dtrans;
    256 	int i, selected;
    257 	int error;
    258 
    259 	DPRINTF(TRANSLATE, ("nilfs_btree_lookup ino %"PRIu64", "
    260 		"lblocknr %"PRIu64"\n", node->ino, lblocknr));
    261 
    262 	btree_hdr  = (struct nilfs_btree_node *) &inode->i_bmap[0];
    263 	dkeys  = &inode->i_bmap[1];
    264 	dptrs  = dkeys + NILFS_BTREE_ROOT_NCHILDREN_MAX;
    265 	dtrans = &inode->i_bmap[1];
    266 
    267 	/* SMALL, direct lookup */
    268 	*vblocknr = 0;
    269 	if ((btree_hdr->bn_flags & NILFS_BMAP_LARGE) == 0) {
    270 		if (lblocknr < NILFS_DIRECT_NBLOCKS) {
    271 			*vblocknr = dtrans[lblocknr];
    272 			return 0;
    273 		}
    274 		/* not mapped XXX could be considered error here */
    275 		return 0;
    276 	}
    277 
    278 	/* LARGE, select matching child; XXX could use binary search */
    279 	dtrans = NULL;
    280 	error = 0;
    281 	selected = 0;
    282 	for (i = 0; i < nilfs_rw16(btree_hdr->bn_nchildren); i++) {
    283 		if (dkeys[i] > lblocknr)
    284 			break;
    285 		selected = i;
    286 	}
    287 
    288 	/* if selected key > lblocknr, its not mapped */
    289 	if (dkeys[selected] > lblocknr)
    290 		return 0;
    291 
    292 	/* overshooting? then not mapped */
    293 	if (selected == nilfs_rw16(btree_hdr->bn_nchildren))
    294 		return 0;
    295 
    296 	/* level should be > 1 or otherwise it should be a direct one */
    297 	assert(btree_hdr->bn_level > 1);
    298 
    299 	/* lookup in selected child */
    300 	assert(dkeys[selected] <= lblocknr);
    301 	error = nilfs_btree_lookup_level(node, lblocknr,
    302 			dptrs[selected], btree_hdr->bn_level-1, vblocknr);
    303 
    304 	return error;
    305 }
    306 
    307 
    308 /* node should be locked on entry to prevent btree changes (unlikely) */
    309 int
    310 nilfs_btree_nlookup(struct nilfs_node *node, uint64_t from, uint64_t blks,
    311 		uint64_t *l2vmap)
    312 {
    313 	uint64_t lblocknr, *vblocknr;
    314 	int i, error;
    315 
    316 	/* TODO / OPTI multiple translations in one go possible */
    317 	error = EINVAL;
    318 	for (i = 0; i < blks; i++) {
    319 		lblocknr  = from + i;
    320 		vblocknr  = l2vmap + i;
    321 		error = nilfs_btree_lookup(node, lblocknr, vblocknr);
    322 
    323 		DPRINTF(TRANSLATE, ("btree_nlookup ino %"PRIu64", "
    324 			"lblocknr %"PRIu64" -> %"PRIu64"\n",
    325 			node->ino, lblocknr, *vblocknr));
    326 		if (error)
    327 			break;
    328 	}
    329 
    330 	return error;
    331 }
    332 
    333 /* --------------------------------------------------------------------- */
    334 
    335 /* vtop operations */
    336 
    337 /* translate index to a file block number and an entry */
    338 static void
    339 nilfs_mdt_trans(struct nilfs_mdt *mdt, uint64_t index,
    340 	uint64_t *blocknr, uint32_t *entry_in_block)
    341 {
    342 	uint64_t blknr;
    343 	uint64_t group, group_offset, blocknr_in_group;
    344 	uint64_t desc_block, desc_offset;
    345 
    346 	/* calculate our offset in the file */
    347 	group             = index / mdt->entries_per_group;
    348 	group_offset      = index % mdt->entries_per_group;
    349 	desc_block        = group / mdt->groups_per_desc_block;
    350 	desc_offset       = group % mdt->groups_per_desc_block;
    351 	blocknr_in_group  = group_offset / mdt->entries_per_block;
    352 
    353 	/* to descgroup offset */
    354 	blknr = 1 + desc_block * mdt->blocks_per_desc_block;
    355 
    356 	/* to group offset */
    357 	blknr += desc_offset * mdt->blocks_per_group;
    358 
    359 	/* to actual file block */
    360 	blknr += 1 + blocknr_in_group;
    361 
    362 	*blocknr        = blknr;
    363 	*entry_in_block = group_offset % mdt->entries_per_block;
    364 }
    365 
    366 
    367 static int
    368 nilfs_vtop(struct nilfs_device *nilfsdev, uint64_t vblocknr, uint64_t *pblocknr)
    369 {
    370 	struct nilfs_dat_entry *entry;
    371 	struct buf *bp;
    372 	uint64_t  ldatblknr;
    373 	uint32_t  entry_in_block;
    374 	int error;
    375 
    376 	nilfs_mdt_trans(&nilfsdev->dat_mdt, vblocknr,
    377 		&ldatblknr, &entry_in_block);
    378 
    379 	error = nilfs_bread(nilfsdev->dat_node, ldatblknr, NOCRED, 0, &bp);
    380 	if (error) {
    381 		printf("vtop: can't read in DAT block %"PRIu64"!\n", ldatblknr);
    382 		brelse(bp, BC_AGE);
    383 		return error;
    384 	}
    385 
    386 	/* get our translation */
    387 	entry = ((struct nilfs_dat_entry *) bp->b_data) + entry_in_block;
    388 #if 0
    389 	printf("\tvblk %4"PRIu64" -> %"PRIu64" for "
    390 		"checkpoint %"PRIu64" to %"PRIu64"\n",
    391 		vblocknr,
    392 		nilfs_rw64(entry->de_blocknr),
    393 		nilfs_rw64(entry->de_start),
    394 		nilfs_rw64(entry->de_end));
    395 #endif
    396 
    397 	*pblocknr = nilfs_rw64(entry->de_blocknr);
    398 	brelse(bp, BC_AGE);
    399 
    400 	return 0;
    401 }
    402 
    403 
    404 int
    405 nilfs_nvtop(struct nilfs_node *node, uint64_t blks, uint64_t *l2vmap,
    406 		uint64_t *v2pmap)
    407 {
    408 	uint64_t vblocknr, *pblocknr;
    409 	int i, error;
    410 
    411 	/* the DAT inode is the only one not mapped virtual */
    412 	if (node->ino == NILFS_DAT_INO) {
    413 		memcpy(v2pmap, l2vmap, blks * sizeof(uint64_t));
    414 		return 0;
    415 	}
    416 
    417 	/* TODO / OPTI more translations in one go */
    418 	error = 0;
    419 	for (i = 0; i < blks; i++) {
    420 		vblocknr  = l2vmap[i];
    421 		pblocknr  = v2pmap + i;
    422 		*pblocknr = 0;
    423 
    424 		/* only translate valid vblocknrs */
    425 		if (vblocknr == 0)
    426 			continue;
    427 		error = nilfs_vtop(node->nilfsdev, vblocknr, pblocknr);
    428 		if (error)
    429 			break;
    430 	}
    431 
    432 	return error;
    433 }
    434 
    435 /* --------------------------------------------------------------------- */
    436 
    437 struct nilfs_recover_info {
    438 	uint64_t segnum;
    439 	uint64_t pseg;
    440 
    441 	struct nilfs_segment_summary segsum;
    442 	struct nilfs_super_root      super_root;
    443 	STAILQ_ENTRY(nilfs_recover_info) next;
    444 };
    445 
    446 
    447 /*
    448  * Helper functions of nilfs_mount() that actually mounts the disc.
    449  */
    450 static int
    451 nilfs_load_segsum(struct nilfs_device *nilfsdev,
    452 	struct nilfs_recover_info *ri)
    453 {
    454 	struct buf *bp;
    455 	uint64_t blocknr, offset;
    456 	uint32_t segsum_struct_size;
    457 	uint32_t magic;
    458 	int error;
    459 
    460 	segsum_struct_size = sizeof(struct nilfs_segment_summary);
    461 
    462 	/* read in segsum structure */
    463 	bp      = NULL;
    464 	blocknr = ri->pseg;
    465 	offset  = 0;
    466 	error = nilfs_get_segment_log(nilfsdev,
    467 			&blocknr, &offset, &bp,
    468 			segsum_struct_size, (void *) &ri->segsum);
    469 	if (error)
    470 		goto out;
    471 
    472 	/* sanity checks */
    473 	magic = nilfs_rw32(ri->segsum.ss_magic);
    474 	if (magic != NILFS_SEGSUM_MAGIC) {
    475 		DPRINTF(VOLUMES, ("nilfs: bad magic in pseg %"PRIu64"\n",
    476 			ri->pseg));
    477 		error = EINVAL;
    478 		goto out;
    479 	}
    480 
    481 	/* TODO check segment summary checksum */
    482 	/* TODO check data checksum */
    483 
    484 	/* adjust our walking point if we have an odd size */
    485 	if (segsum_struct_size != nilfs_rw32(ri->segsum.ss_bytes)) {
    486 		printf("nilfs: WARNING encountered segsum_struct size %d in "
    487 			"pseg %"PRIu64"\n",
    488 			nilfs_rw32(ri->segsum.ss_bytes), ri->pseg);
    489 		/* XXX report as an error? */
    490 	}
    491 
    492 out:
    493 	if (bp)
    494 		brelse(bp, BC_AGE);
    495 
    496 	return error;
    497 }
    498 
    499 
    500 static int
    501 nilfs_load_super_root(struct nilfs_device *nilfsdev,
    502 	struct nilfs_recover_info *ri)
    503 {
    504 	struct nilfs_segment_summary *segsum = &ri->segsum;
    505 	struct buf *bp;
    506 	uint64_t blocknr, offset;
    507 	uint32_t segsum_size, size;
    508 	uint32_t nsumblk, nfileblk;
    509 	int error;
    510 
    511 	bp = NULL;
    512 
    513 	/* process segment summary */
    514 	segsum_size = nilfs_rw32(segsum->ss_sumbytes);
    515 	nsumblk     = (segsum_size - 1) / nilfsdev->blocksize + 1;
    516 	nfileblk    = nilfs_rw32(segsum->ss_nblocks) - nsumblk;
    517 
    518 	/* check if there is a superroot */
    519 	if ((nilfs_rw16(segsum->ss_flags) & NILFS_SS_SR) == 0) {
    520 		DPRINTF(VOLUMES, ("nilfs: no super root in pseg %"PRIu64"\n",
    521 			ri->pseg));
    522 		error = ENOENT;
    523 		goto out;
    524 	}
    525 
    526 	/* get our super root, located at the end of the pseg */
    527 	blocknr = ri->pseg + nsumblk + nfileblk - 1;
    528 	offset = 0;
    529 	size = sizeof(struct nilfs_super_root);
    530 	error = nilfs_get_segment_log(nilfsdev,
    531 			&blocknr, &offset, &bp,
    532 			size, (void *) &nilfsdev->super_root);
    533 	if (error) {
    534 		printf("read in of superroot failed\n");
    535 		error = EIO;
    536 	}
    537 	/* else got our super root! */
    538 	DPRINTF(VOLUMES, ("    got superroot\n"));
    539 
    540 out:
    541 	if (bp)
    542 		brelse(bp, BC_AGE);
    543 
    544 	return error;
    545 }
    546 
    547 /*
    548  * Search for the last super root recorded.
    549  */
    550 void
    551 nilfs_search_super_root(struct nilfs_device *nilfsdev)
    552 {
    553 	struct nilfs_super_block *super;
    554 	struct nilfs_segment_summary *segsum;
    555 	struct nilfs_recover_info *ri, *ori, *i_ri;
    556 	STAILQ_HEAD(,nilfs_recover_info) ri_list;
    557 	uint64_t seg_start, seg_end, cno;
    558 	uint32_t segsum_size;
    559 	uint32_t nsumblk, nfileblk;
    560 	int error;
    561 
    562 	STAILQ_INIT(&ri_list);
    563 
    564 	/* search for last super root */
    565 	ri = malloc(sizeof(struct nilfs_recover_info), M_NILFSTEMP, M_WAITOK);
    566 	memset(ri, 0, sizeof(struct nilfs_recover_info));
    567 
    568 	/* if enabled, start from the specified position */
    569 	if (0) {
    570 		/* start from set start */
    571 		nilfsdev->super.s_last_pseg = nilfsdev->super.s_first_data_block;
    572 		nilfsdev->super.s_last_cno  = nilfs_rw64(1);
    573 	}
    574 
    575 	ri->pseg   = nilfs_rw64(nilfsdev->super.s_last_pseg); /* blknr */
    576 	ri->segnum = nilfs_get_segnum_of_block(nilfsdev, ri->pseg);
    577 
    578 	error = 0;
    579 	cno = nilfs_rw64(nilfsdev->super.s_last_cno);
    580 	DPRINTF(VOLUMES, ("nilfs: seach_super_root start in pseg %"PRIu64"\n",
    581 			ri->pseg));
    582 	for (;;) {
    583 		DPRINTF(VOLUMES, (" at pseg %"PRIu64"\n", ri->pseg));
    584 		error = nilfs_load_segsum(nilfsdev, ri);
    585 		if (error)
    586 			break;
    587 
    588 		segsum = &ri->segsum;
    589 
    590 		/* try to load super root */
    591 		if (nilfs_rw16(segsum->ss_flags) & NILFS_SS_SR) {
    592 			DPRINTF(VOLUMES, (" try super root\n"));
    593 			error = nilfs_load_super_root(nilfsdev, ri);
    594 			if (error)
    595 				break;	/* confused */
    596 			/* wipe current list of ri */
    597 			while (!STAILQ_EMPTY(&ri_list)) {
    598 				i_ri = STAILQ_FIRST(&ri_list);
    599 				STAILQ_REMOVE_HEAD(&ri_list, next);
    600 				free(i_ri, M_NILFSTEMP);
    601 			}
    602 			super = &nilfsdev->super;
    603 
    604 			super->s_last_pseg = nilfs_rw64(ri->pseg);
    605 			super->s_last_cno  = cno++;
    606 			super->s_last_seq  = segsum->ss_seq;
    607 			super->s_state     = nilfs_rw16(NILFS_VALID_FS);
    608 		} else {
    609 			STAILQ_INSERT_TAIL(&ri_list, ri, next);
    610 			ori = ri;
    611 			ri = malloc(sizeof(struct nilfs_recover_info),
    612 				M_NILFSTEMP, M_WAITOK);
    613 			memset(ri, 0, sizeof(struct nilfs_recover_info));
    614 			ri->segnum = ori->segnum;
    615 			ri->pseg   = ori->pseg;
    616 			/* segsum keeps pointing to the `old' ri */
    617 		}
    618 
    619 		/* continue to the next pseg */
    620 		segsum_size = nilfs_rw32(segsum->ss_sumbytes);
    621 		nsumblk     = (segsum_size - 1) / nilfsdev->blocksize + 1;
    622 		nfileblk    = nilfs_rw32(segsum->ss_nblocks) - nsumblk;
    623 
    624 		/* calculate next partial segment location */
    625 		ri->pseg += nsumblk + nfileblk;
    626 
    627 		/* did we reach the end of the segment? if so, go to the next */
    628 		nilfs_get_segment_range(nilfsdev, ri->segnum, &seg_start, &seg_end);
    629 		if (ri->pseg >= seg_end)
    630 			ri->pseg = nilfs_rw64(segsum->ss_next);
    631 		ri->segnum = nilfs_get_segnum_of_block(nilfsdev, ri->pseg);
    632 	}
    633 
    634 	/*
    635 	 * XXX No roll-forward yet of the remaining partial segments.
    636 	 */
    637 
    638 	/* wipe current list of ri */
    639 	while (!STAILQ_EMPTY(&ri_list)) {
    640 		i_ri = STAILQ_FIRST(&ri_list);
    641 		STAILQ_REMOVE_HEAD(&ri_list, next);
    642 		printf("nilfs: ignoring pseg at %"PRIu64"\n", i_ri->pseg);
    643 		free(i_ri, M_NILFSTEMP);
    644 	}
    645 	free(ri, M_NILFSTEMP);
    646 }
    647 
    648 /* --------------------------------------------------------------------- */
    649 
    650 /*
    651  * Genfs interfacing
    652  *
    653  * static const struct genfs_ops nilfs_genfsops = {
    654  * 	.gop_size = genfs_size,
    655  * 		size of transfers
    656  * 	.gop_alloc = nilfs_gop_alloc,
    657  * 		allocate len bytes at offset
    658  * 	.gop_write = genfs_gop_write,
    659  * 		putpages interface code
    660  * 	.gop_markupdate = nilfs_gop_markupdate,
    661  * 		set update/modify flags etc.
    662  * }
    663  */
    664 
    665 /*
    666  * Callback from genfs to allocate len bytes at offset off; only called when
    667  * filling up gaps in the allocation.
    668  */
    669 static int
    670 nilfs_gop_alloc(struct vnode *vp, off_t off,
    671     off_t len, int flags, kauth_cred_t cred)
    672 {
    673 	DPRINTF(NOTIMPL, ("nilfs_gop_alloc not implemented\n"));
    674 	DPRINTF(ALLOC, ("nilfs_gop_alloc called for %"PRIu64" bytes\n", len));
    675 
    676 	return 0;
    677 }
    678 
    679 
    680 /*
    681  * callback from genfs to update our flags
    682  */
    683 static void
    684 nilfs_gop_markupdate(struct vnode *vp, int flags)
    685 {
    686 	struct nilfs_node *nilfs_node = VTOI(vp);
    687 	u_long mask = 0;
    688 
    689 	if ((flags & GOP_UPDATE_ACCESSED) != 0) {
    690 		mask = IN_ACCESS;
    691 	}
    692 	if ((flags & GOP_UPDATE_MODIFIED) != 0) {
    693 		if (vp->v_type == VREG) {
    694 			mask |= IN_CHANGE | IN_UPDATE;
    695 		} else {
    696 			mask |= IN_MODIFY;
    697 		}
    698 	}
    699 	if (mask) {
    700 		nilfs_node->i_flags |= mask;
    701 	}
    702 }
    703 
    704 
    705 static const struct genfs_ops nilfs_genfsops = {
    706 	.gop_size = genfs_size,
    707 	.gop_alloc = nilfs_gop_alloc,
    708 	.gop_write = genfs_gop_write_rwmap,
    709 	.gop_markupdate = nilfs_gop_markupdate,
    710 };
    711 
    712 /* --------------------------------------------------------------------- */
    713 
    714 static void
    715 nilfs_register_node(struct nilfs_node *node)
    716 {
    717 	struct nilfs_mount *ump;
    718 	struct nilfs_node *chk;
    719 	uint32_t hashline;
    720 
    721 	ump = node->ump;
    722 	mutex_enter(&ump->ihash_lock);
    723 
    724 	/* add to our hash table */
    725 	hashline = nilfs_calchash(node->ino) & NILFS_INODE_HASHMASK;
    726 #ifdef DEBUG
    727 	LIST_FOREACH(chk, &ump->nilfs_nodes[hashline], hashchain) {
    728 		assert(chk);
    729 		if (chk->ino == node->ino)
    730 			panic("Double node entered\n");
    731 	}
    732 #else
    733 	chk = NULL;
    734 #endif
    735 	LIST_INSERT_HEAD(&ump->nilfs_nodes[hashline], node, hashchain);
    736 
    737 	mutex_exit(&ump->ihash_lock);
    738 }
    739 
    740 
    741 static void
    742 nilfs_deregister_node(struct nilfs_node *node)
    743 {
    744 	struct nilfs_mount *ump;
    745 
    746 	ump = node->ump;
    747 	mutex_enter(&ump->ihash_lock);
    748 
    749 	/* remove from hash list */
    750 	LIST_REMOVE(node, hashchain);
    751 
    752 	mutex_exit(&ump->ihash_lock);
    753 }
    754 
    755 
    756 static struct nilfs_node *
    757 nilfs_hash_lookup(struct nilfs_mount *ump, ino_t ino)
    758 {
    759 	struct nilfs_node *node;
    760 	struct vnode *vp;
    761 	uint32_t hashline;
    762 
    763 loop:
    764 	mutex_enter(&ump->ihash_lock);
    765 
    766 	/* search our hash table */
    767 	hashline = nilfs_calchash(ino) & NILFS_INODE_HASHMASK;
    768 	LIST_FOREACH(node, &ump->nilfs_nodes[hashline], hashchain) {
    769 		assert(node);
    770 		if (node->ino == ino) {
    771 			vp = node->vnode;
    772 			assert(vp);
    773 			mutex_enter(&vp->v_interlock);
    774 			mutex_exit(&ump->ihash_lock);
    775 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    776 				goto loop;
    777 			return node;
    778 		}
    779 	}
    780 	mutex_exit(&ump->ihash_lock);
    781 
    782 	return NULL;
    783 }
    784 
    785 
    786 /* node action implementators */
    787 extern int (**nilfs_vnodeop_p)(void *);
    788 
    789 int
    790 nilfs_get_node_raw(struct nilfs_device *nilfsdev, struct nilfs_mount *ump,
    791 	uint64_t ino, struct nilfs_inode *inode, struct nilfs_node **nodep)
    792 {
    793 	struct nilfs_node *node;
    794 	struct vnode *nvp;
    795 	struct mount *mp;
    796 	int (**vnodeops)(void *);
    797 	int error;
    798 
    799 	*nodep = NULL;
    800 	vnodeops = nilfs_vnodeop_p;
    801 
    802 	/* associate with mountpoint if present*/
    803 	mp = ump? ump->vfs_mountp : NULL;
    804 	error = getnewvnode(VT_NILFS, mp, vnodeops, &nvp);
    805 	if (error)
    806 		return error;
    807 
    808 	/* lock node */
    809 	error = vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY);
    810 	if (error) {
    811 		nvp->v_data = NULL;
    812 		ungetnewvnode(nvp);
    813 		return error;
    814 	}
    815 
    816 	node = pool_get(&nilfs_node_pool, PR_WAITOK);
    817 	memset(node, 0, sizeof(struct nilfs_node));
    818 
    819 	/* crosslink */
    820 	node->vnode    = nvp;
    821 	node->ump      = ump;
    822 	node->nilfsdev = nilfsdev;
    823 	nvp->v_data    = node;
    824 
    825 	/* initiase nilfs node */
    826 	node->ino   = ino;
    827 	node->inode = *inode;
    828 	node->lockf = NULL;
    829 
    830 	/* needed? */
    831 	mutex_init(&node->node_mutex, MUTEX_DEFAULT, IPL_NONE);
    832 	cv_init(&node->node_lock, "nilfs_nlk");
    833 
    834 	/* initialise genfs */
    835 	genfs_node_init(nvp, &nilfs_genfsops);
    836 
    837 	/* check if we're fetching the root */
    838 	if (ino == NILFS_ROOT_INO)
    839 		nvp->v_vflag |= VV_ROOT;
    840 
    841 	/* update vnode's file type XXX is there a function for this? */
    842 	nvp->v_type = VREG;
    843 	if (S_ISDIR(inode->i_mode))
    844 		nvp->v_type = VDIR;
    845 	if (S_ISLNK(inode->i_mode))
    846 		nvp->v_type = VLNK;
    847 #if 0
    848 	if (S_ISCHR(inode->i_mode))
    849 		nvp->v_type = VCHR;
    850 	if (S_ISBLK(inode->i_mode))
    851 		nvp->v_type = VBLK;
    852 #endif
    853 	/* XXX what else? */
    854 
    855 	/* fixup inode size for system nodes */
    856 	if ((ino < NILFS_USER_INO) && (ino != NILFS_ROOT_INO)) {
    857 		DPRINTF(VOLUMES, ("NEED TO GET my size for inode %"PRIu64"\n",
    858 			ino));
    859 		/* for now set it to maximum, -1 is illegal */
    860 		inode->i_size = nilfs_rw64(((uint64_t) -2));
    861 	}
    862 
    863 	uvm_vnp_setsize(nvp, nilfs_rw64(inode->i_size));
    864 
    865 	if (ump)
    866 		nilfs_register_node(node);
    867 
    868 	/* return node */
    869 	*nodep = node;
    870 	return 0;
    871 }
    872 
    873 
    874 int
    875 nilfs_get_node(struct nilfs_mount *ump, uint64_t ino, struct nilfs_node **nodep)
    876 {
    877 	struct nilfs_device *nilfsdev;
    878 	struct nilfs_inode   inode, *entry;
    879 	struct buf *bp;
    880 	uint64_t ivblocknr;
    881 	uint32_t entry_in_block;
    882 	int error;
    883 
    884 	/* lookup node in hash table */
    885 	*nodep = nilfs_hash_lookup(ump, ino);
    886 	if (*nodep)
    887 		return 0;
    888 
    889 	/* lock to disallow simultanious creation of same udf_node */
    890 	mutex_enter(&ump->get_node_lock);
    891 
    892 	/* relookup since it could be created while waiting for the mutex */
    893 	*nodep = nilfs_hash_lookup(ump, ino);
    894 	if (*nodep) {
    895 		mutex_exit(&ump->get_node_lock);
    896 		return 0;
    897 	}
    898 
    899 	/* create new inode; XXX check could be handier */
    900 	if ((ino < NILFS_ATIME_INO) && (ino != NILFS_ROOT_INO)) {
    901 		printf("nilfs_get_node: system ino %"PRIu64" not in mount "
    902 			"point!\n", ino);
    903 		mutex_exit(&ump->get_node_lock);
    904 		return ENOENT;
    905 	}
    906 
    907 	/* lookup inode in the ifile */
    908 	DPRINTF(NODE, ("lookup ino %"PRIu64"\n", ino));
    909 
    910 	/* lookup inode structure in mountpoints ifile */
    911 	nilfsdev = ump->nilfsdev;
    912 	nilfs_mdt_trans(&nilfsdev->ifile_mdt, ino, &ivblocknr, &entry_in_block);
    913 
    914 	error = nilfs_bread(ump->ifile_node, ivblocknr, NOCRED, 0, &bp);
    915 	if (error) {
    916 		mutex_exit(&ump->get_node_lock);
    917 		return ENOENT;
    918 	}
    919 
    920 	/* get inode entry */
    921 	entry =  (struct nilfs_inode *) bp->b_data + entry_in_block;
    922 	inode = *entry;
    923 	brelse(bp, BC_AGE);
    924 
    925 	/* get node */
    926 	error = nilfs_get_node_raw(ump->nilfsdev, ump, ino, &inode, nodep);
    927 	mutex_exit(&ump->get_node_lock);
    928 
    929 	return error;
    930 }
    931 
    932 
    933 void
    934 nilfs_dispose_node(struct nilfs_node **nodep)
    935 {
    936 	struct vnode *vp;
    937 	struct nilfs_node *node;
    938 
    939 	/* protect against rogue values */
    940 	if (!*nodep)
    941 		return;
    942 
    943 	node = *nodep;
    944 	vp = node->vnode;
    945 
    946 	/* remove dirhash if present */
    947 	dirhash_purge(&node->dir_hash);
    948 
    949 	/* remove from our hash lookup table */
    950 	if (node->ump)
    951 		nilfs_deregister_node(node);
    952 
    953 	/* destroy our locks */
    954 	mutex_destroy(&node->node_mutex);
    955 	cv_destroy(&node->node_lock);
    956 
    957 	/* dissociate from our vnode */
    958 	genfs_node_destroy(node->vnode);
    959 	vp->v_data = NULL;
    960 
    961 	/* free our associated memory */
    962 	pool_put(&nilfs_node_pool, node);
    963 
    964 	*nodep = NULL;
    965 }
    966 
    967 
    968 void
    969 nilfs_itimes(struct nilfs_node *node, struct timespec *acc,
    970 	struct timespec *mod, struct timespec *birth)
    971 {
    972 }
    973 
    974 
    975 int
    976 nilfs_update(struct vnode *node, struct timespec *acc,
    977 	struct timespec *mod, struct timespec *birth, int updflags)
    978 {
    979 	return EROFS;
    980 }
    981 
    982 
    983 int
    984 nilfs_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred)
    985 {
    986 	return EROFS;
    987 }
    988 
    989 
    990 
    991 int
    992 nilfs_grow_node(struct nilfs_node *node, uint64_t new_size)
    993 {
    994 	return EROFS;
    995 }
    996 
    997 
    998 int
    999 nilfs_shrink_node(struct nilfs_node *node, uint64_t new_size)
   1000 {
   1001 	return EROFS;
   1002 }
   1003 
   1004 
   1005 static int
   1006 dirhash_fill(struct nilfs_node *dir_node)
   1007 {
   1008 	struct vnode *dvp = dir_node->vnode;
   1009 	struct dirhash *dirh;
   1010 	struct nilfs_dir_entry *ndirent;
   1011 	struct dirent dirent;
   1012 	struct buf *bp;
   1013 	uint64_t file_size, diroffset, blkoff;
   1014 	uint64_t blocknr;
   1015 	uint32_t blocksize = dir_node->nilfsdev->blocksize;
   1016 	uint8_t *pos, name_len;
   1017 	int error;
   1018 
   1019 	DPRINTF(CALL, ("dirhash_fill called\n"));
   1020 
   1021 	if (dvp->v_type != VDIR)
   1022 		return ENOTDIR;
   1023 
   1024 	/* make sure we have a dirhash to work on */
   1025 	dirh = dir_node->dir_hash;
   1026 	KASSERT(dirh);
   1027 	KASSERT(dirh->refcnt > 0);
   1028 
   1029 	if (dirh->flags & DIRH_BROKEN)
   1030 		return EIO;
   1031 
   1032 	if (dirh->flags & DIRH_COMPLETE)
   1033 		return 0;
   1034 
   1035 	DPRINTF(DIRHASH, ("Filling directory hash\n"));
   1036 
   1037 	/* make sure we have a clean dirhash to add to */
   1038 	dirhash_purge_entries(dirh);
   1039 
   1040 	/* get directory filesize */
   1041 	file_size = nilfs_rw64(dir_node->inode.i_size);
   1042 
   1043 	/* walk the directory */
   1044 	error = 0;
   1045 	diroffset = 0;
   1046 
   1047 	blocknr = diroffset / blocksize;
   1048 	blkoff  = diroffset % blocksize;
   1049 	error = nilfs_bread(dir_node, blocknr, NOCRED, 0, &bp);
   1050 	if (error) {
   1051 		dirh->flags |= DIRH_BROKEN;
   1052 		dirhash_purge_entries(dirh);
   1053 		return EIO;
   1054 	}
   1055 	while (diroffset < file_size) {
   1056 		DPRINTF(READDIR, ("filldir : offset = %"PRIu64"\n",
   1057 			diroffset));
   1058 		if (blkoff >= blocksize) {
   1059 			blkoff = 0; blocknr++;
   1060 			brelse(bp, BC_AGE);
   1061 			error = nilfs_bread(dir_node, blocknr, NOCRED, 0,
   1062 					&bp);
   1063 			if (error) {
   1064 				dirh->flags |= DIRH_BROKEN;
   1065 				dirhash_purge_entries(dirh);
   1066 				return EIO;
   1067 			}
   1068 		}
   1069 
   1070 		/* read in one dirent */
   1071 		pos = (uint8_t *) bp->b_data + blkoff;
   1072 		ndirent = (struct nilfs_dir_entry *) pos;
   1073 		name_len = ndirent->name_len;
   1074 
   1075 		memset(&dirent, 0, sizeof(struct dirent));
   1076 		dirent.d_fileno = nilfs_rw64(ndirent->inode);
   1077 		dirent.d_type   = ndirent->file_type;	/* 1:1 ? */
   1078 		dirent.d_namlen = name_len;
   1079 		strncpy(dirent.d_name, ndirent->name, name_len);
   1080 		dirent.d_reclen = _DIRENT_SIZE(&dirent);
   1081 		DPRINTF(DIRHASH, ("copying `%*.*s`\n", name_len,
   1082 			name_len, dirent.d_name));
   1083 
   1084 		/* XXX is it deleted? extra free space? */
   1085 		dirhash_enter(dirh, &dirent, diroffset,
   1086 			nilfs_rw16(ndirent->rec_len), 0);
   1087 
   1088 		/* advance */
   1089 		diroffset += nilfs_rw16(ndirent->rec_len);
   1090 		blkoff    += nilfs_rw16(ndirent->rec_len);
   1091 	}
   1092 	brelse(bp, BC_AGE);
   1093 
   1094 	dirh->flags |= DIRH_COMPLETE;
   1095 
   1096 	return 0;
   1097 }
   1098 
   1099 
   1100 int
   1101 nilfs_lookup_name_in_dir(struct vnode *dvp, const char *name, int namelen,
   1102 		uint64_t *ino, int *found)
   1103 {
   1104 	struct nilfs_node	*dir_node = VTOI(dvp);
   1105 	struct nilfs_dir_entry *ndirent;
   1106 	struct dirhash		*dirh;
   1107 	struct dirhash_entry	*dirh_ep;
   1108 	struct buf *bp;
   1109 	uint64_t diroffset, blkoff;
   1110 	uint64_t blocknr;
   1111 	uint32_t blocksize = dir_node->nilfsdev->blocksize;
   1112 	uint8_t *pos;
   1113 	int hit, error;
   1114 
   1115 	/* set default return */
   1116 	*found = 0;
   1117 
   1118 	/* get our dirhash and make sure its read in */
   1119 	dirhash_get(&dir_node->dir_hash);
   1120 	error = dirhash_fill(dir_node);
   1121 	if (error) {
   1122 		dirhash_put(dir_node->dir_hash);
   1123 		return error;
   1124 	}
   1125 	dirh = dir_node->dir_hash;
   1126 
   1127 	/* allocate temporary space for fid */
   1128 
   1129 	DPRINTF(DIRHASH, ("dirhash_lookup looking for `%*.*s`\n",
   1130 		namelen, namelen, name));
   1131 
   1132 	/* search our dirhash hits */
   1133 	*ino = 0;
   1134 	dirh_ep = NULL;
   1135 	for (;;) {
   1136 		hit = dirhash_lookup(dirh, name, namelen, &dirh_ep);
   1137 		/* if no hit, abort the search */
   1138 		if (!hit)
   1139 			break;
   1140 
   1141 		/* check this hit */
   1142 		diroffset = dirh_ep->offset;
   1143 
   1144 		blocknr = diroffset / blocksize;
   1145 		blkoff  = diroffset % blocksize;
   1146 		error = nilfs_bread(dir_node, blocknr, NOCRED, 0, &bp);
   1147 		if (error)
   1148 			return EIO;
   1149 
   1150 		/* read in one dirent */
   1151 		pos = (uint8_t *) bp->b_data + blkoff;
   1152 		ndirent = (struct nilfs_dir_entry *) pos;
   1153 
   1154 		DPRINTF(DIRHASH, ("dirhash_lookup\tchecking `%*.*s`\n",
   1155 			ndirent->name_len, ndirent->name_len, ndirent->name));
   1156 
   1157 		/* see if its our entry */
   1158 		KASSERT(ndirent->name_len == namelen);
   1159 		if (strncmp(ndirent->name, name, namelen) == 0) {
   1160 			*found = 1;
   1161 			*ino = nilfs_rw64(ndirent->inode);
   1162 			brelse(bp, BC_AGE);
   1163 			break;
   1164 		}
   1165 		brelse(bp, BC_AGE);
   1166 	}
   1167 
   1168 	dirhash_put(dir_node->dir_hash);
   1169 
   1170 	return error;
   1171 }
   1172 
   1173 
   1174 int
   1175 nilfs_dir_detach(struct nilfs_mount *ump, struct nilfs_node *dir_node, struct nilfs_node *node, struct componentname *cnp)
   1176 {
   1177 	return EROFS;
   1178 }
   1179 
   1180 
   1181 int
   1182 nilfs_dir_attach(struct nilfs_mount *ump, struct nilfs_node *dir_node, struct nilfs_node *node, struct vattr *vap, struct componentname *cnp)
   1183 {
   1184 	return EROFS;
   1185 }
   1186 
   1187 
   1188 /* XXX return vnode? */
   1189 int
   1190 nilfs_create_node(struct vnode *dvp, struct vnode **vpp, struct vattr *vap, struct componentname *cnp)
   1191 {
   1192 	return EROFS;
   1193 }
   1194 
   1195 
   1196 void
   1197 nilfs_delete_node(struct nilfs_node *node)
   1198 {
   1199 }
   1200 
   1201 
   1202