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