Home | History | Annotate | Line # | Download | only in nilfs
      1 /*	$NetBSD: nilfs_vnops.c,v 1.48 2022/10/07 22:33:42 andvar 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_vnops.c,v 1.48 2022/10/07 22:33:42 andvar Exp $");
     32 #endif /* not lint */
     33 
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/namei.h>
     38 #include <sys/resourcevar.h>	/* defines plimit structure in proc struct */
     39 #include <sys/kernel.h>
     40 #include <sys/file.h>		/* define FWRITE ... */
     41 #include <sys/stat.h>
     42 #include <sys/buf.h>
     43 #include <sys/proc.h>
     44 #include <sys/mount.h>
     45 #include <sys/vnode.h>
     46 #include <sys/signalvar.h>
     47 #include <sys/malloc.h>
     48 #include <sys/dirent.h>
     49 #include <sys/lockf.h>
     50 #include <sys/kauth.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 
     64 /* implementations of vnode functions; table follows at end */
     65 /* --------------------------------------------------------------------- */
     66 
     67 int
     68 nilfs_inactive(void *v)
     69 {
     70 	struct vop_inactive_v2_args /* {
     71 		struct vnode *a_vp;
     72 		bool         *a_recycle;
     73 	} */ *ap = v;
     74 	struct vnode *vp = ap->a_vp;
     75 	struct nilfs_node *nilfs_node = VTOI(vp);
     76 
     77 	DPRINTF(NODE, ("nilfs_inactive called for nilfs_node %p\n", VTOI(vp)));
     78 
     79 	if (nilfs_node == NULL) {
     80 		DPRINTF(NODE, ("nilfs_inactive: inactive NULL NILFS node\n"));
     81 		VOP_UNLOCK(vp);
     82 		return 0;
     83 	}
     84 
     85 	/*
     86 	 * Optionally flush metadata to disc. If the file has not been
     87 	 * referenced anymore in a directory we ought to free up the resources
     88 	 * on disc if applicable.
     89 	 */
     90 
     91 	return 0;
     92 }
     93 
     94 /* --------------------------------------------------------------------- */
     95 
     96 int
     97 nilfs_reclaim(void *v)
     98 {
     99 	struct vop_reclaim_v2_args /* {
    100 		struct vnode *a_vp;
    101 	} */ *ap = v;
    102 	struct vnode *vp = ap->a_vp;
    103 	struct nilfs_node *nilfs_node = VTOI(vp);
    104 
    105 	VOP_UNLOCK(vp);
    106 
    107 	DPRINTF(NODE, ("nilfs_reclaim called for node %p\n", nilfs_node));
    108 
    109 	if (nilfs_node == NULL) {
    110 		DPRINTF(NODE, ("nilfs_reclaim(): null nilfsnode\n"));
    111 		return 0;
    112 	}
    113 
    114 	/* update note for closure */
    115 	nilfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
    116 
    117 	/* dispose all node knowledge */
    118 	genfs_node_destroy(vp);
    119 	nilfs_dispose_node(&nilfs_node);
    120 
    121 	vp->v_data = NULL;
    122 
    123 	return 0;
    124 }
    125 
    126 /* --------------------------------------------------------------------- */
    127 
    128 int
    129 nilfs_read(void *v)
    130 {
    131 	struct vop_read_args /* {
    132 		struct vnode *a_vp;
    133 		struct uio *a_uio;
    134 		int a_ioflag;
    135 		kauth_cred_t a_cred;
    136 	} */ *ap = v;
    137 	struct vnode *vp     = ap->a_vp;
    138 	struct uio   *uio    = ap->a_uio;
    139 	int           ioflag = ap->a_ioflag;
    140 	int           advice = IO_ADV_DECODE(ap->a_ioflag);
    141 	struct uvm_object    *uobj;
    142 	struct nilfs_node      *nilfs_node = VTOI(vp);
    143 	uint64_t file_size;
    144 	vsize_t len;
    145 	int error;
    146 
    147 	DPRINTF(READ, ("nilfs_read called\n"));
    148 
    149 	/* can this happen? some filingsystems have this check */
    150 	if (uio->uio_offset < 0)
    151 		return EINVAL;
    152 	if (uio->uio_resid == 0)
    153 		return 0;
    154 
    155 	/* protect against rogue programs reading raw directories and links */
    156 	if ((ioflag & IO_ALTSEMANTICS) == 0) {
    157 		if (vp->v_type == VDIR)
    158 			return EISDIR;
    159 		/* all but regular files just give EINVAL */
    160 		if (vp->v_type != VREG)
    161 			return EINVAL;
    162 	}
    163 
    164 	assert(nilfs_node);
    165 	file_size = nilfs_rw64(nilfs_node->inode.i_size);
    166 
    167 	/* read contents using buffercache */
    168 	uobj = &vp->v_uobj;
    169 	error = 0;
    170 	while (uio->uio_resid > 0) {
    171 		/* reached end? */
    172 		if (file_size <= uio->uio_offset)
    173 			break;
    174 
    175 		/* maximise length to file extremity */
    176 		len = MIN(file_size - uio->uio_offset, uio->uio_resid);
    177 		if (len == 0)
    178 			break;
    179 
    180 		/* ubc, here we come, prepare to trap */
    181 		error = ubc_uiomove(uobj, uio, len, advice,
    182 		    UBC_READ | UBC_PARTIALOK | UBC_VNODE_FLAGS(vp));
    183 		if (error)
    184 			break;
    185 	}
    186 
    187 	/* note access time unless not requested */
    188 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
    189 		nilfs_node->i_flags |= IN_ACCESS;
    190 		if ((ioflag & IO_SYNC) == IO_SYNC)
    191 			error = nilfs_update(vp, NULL, NULL, NULL, UPDATE_WAIT);
    192 	}
    193 
    194 	return error;
    195 }
    196 
    197 /* --------------------------------------------------------------------- */
    198 
    199 int
    200 nilfs_write(void *v)
    201 {
    202 	struct vop_write_args /* {
    203 		struct vnode *a_vp;
    204 		struct uio *a_uio;
    205 		int a_ioflag;
    206 		kauth_cred_t a_cred;
    207 	} */ *ap = v;
    208 	struct vnode *vp     = ap->a_vp;
    209 	struct uio   *uio    = ap->a_uio;
    210 	int           ioflag = ap->a_ioflag;
    211 	int           advice = IO_ADV_DECODE(ap->a_ioflag);
    212 	struct uvm_object    *uobj;
    213 	struct nilfs_node      *nilfs_node = VTOI(vp);
    214 	uint64_t file_size;
    215 	vsize_t len;
    216 	int error, resid;
    217 
    218 	DPRINTF(WRITE, ("nilfs_write called\n"));
    219 
    220 	/* can this happen? some filingsystems have this check */
    221 	if (uio->uio_offset < 0)
    222 		return EINVAL;
    223 	if (uio->uio_resid == 0)
    224 		return 0;
    225 
    226 	/* protect against rogue programs writing raw directories or links */
    227 	if ((ioflag & IO_ALTSEMANTICS) == 0) {
    228 		if (vp->v_type == VDIR)
    229 			return EISDIR;
    230 		/* all but regular files just give EINVAL for now */
    231 		if (vp->v_type != VREG)
    232 			return EINVAL;
    233 	}
    234 
    235 	assert(nilfs_node);
    236 	panic("nilfs_write() called\n");
    237 
    238 	/* remember old file size */
    239 	assert(nilfs_node);
    240 	file_size = nilfs_rw64(nilfs_node->inode.i_size);
    241 
    242 	/* if explicitly asked to append, uio_offset can be wrong? */
    243 	if (ioflag & IO_APPEND)
    244 		uio->uio_offset = file_size;
    245 
    246 #if 0
    247 	extended = (uio->uio_offset + uio->uio_resid > file_size);
    248 	if (extended) {
    249 		DPRINTF(WRITE, ("extending file from %"PRIu64" to %"PRIu64"\n",
    250 			file_size, uio->uio_offset + uio->uio_resid));
    251 		error = nilfs_grow_node(nilfs_node, uio->uio_offset + uio->uio_resid);
    252 		if (error)
    253 			return error;
    254 		file_size = uio->uio_offset + uio->uio_resid;
    255 	}
    256 #endif
    257 
    258 	/* write contents using buffercache */
    259 	uobj = &vp->v_uobj;
    260 	resid = uio->uio_resid;
    261 	error = 0;
    262 
    263 	uvm_vnp_setwritesize(vp, file_size);
    264 	while (uio->uio_resid > 0) {
    265 		/* maximise length to file extremity */
    266 		len = MIN(file_size - uio->uio_offset, uio->uio_resid);
    267 		if (len == 0)
    268 			break;
    269 
    270 		/* ubc, here we come, prepare to trap */
    271 		error = ubc_uiomove(uobj, uio, len, advice,
    272 		    UBC_WRITE | UBC_VNODE_FLAGS(vp));
    273 		if (error)
    274 			break;
    275 	}
    276 	uvm_vnp_setsize(vp, file_size);
    277 
    278 	/* mark node changed and request update */
    279 	nilfs_node->i_flags |= IN_CHANGE | IN_UPDATE;
    280 	if (vp->v_mount->mnt_flag & MNT_RELATIME)
    281 		nilfs_node->i_flags |= IN_ACCESS;
    282 
    283 	/*
    284 	 * XXX TODO FFS has code here to reset setuid & setgid when we're not
    285 	 * the superuser as a precaution against tampering.
    286 	 */
    287 
    288 	if (error) {
    289 		/* bring back file size to its former size */
    290 		/* take notice of its errors? */
    291 //		(void) nilfs_chsize(vp, (u_quad_t) old_size, NOCRED);
    292 
    293 		/* roll back uio */
    294 		uio->uio_offset -= resid - uio->uio_resid;
    295 		uio->uio_resid = resid;
    296 	} else {
    297 		/* if we write and we're synchronous, update node */
    298 		if ((resid > uio->uio_resid) && ((ioflag & IO_SYNC) == IO_SYNC))
    299 			error = nilfs_update(vp, NULL, NULL, NULL, UPDATE_WAIT);
    300 	}
    301 
    302 	return error;
    303 }
    304 
    305 
    306 /* --------------------------------------------------------------------- */
    307 
    308 /*
    309  * bmap functionality that translates logical block numbers to the virtual
    310  * block numbers to be stored on the vnode itself.
    311  *
    312  * Important alert!
    313  *
    314  * If runp is not NULL, the number of contiguous blocks __starting from the
    315  * next block after the queried block__ will be returned in runp.
    316  */
    317 
    318 int
    319 nilfs_trivial_bmap(void *v)
    320 {
    321 	struct vop_bmap_args /* {
    322 		struct vnode *a_vp;
    323 		daddr_t a_bn;
    324 		struct vnode **a_vpp;
    325 		daddr_t *a_bnp;
    326 		int *a_runp;
    327 	} */ *ap = v;
    328 	struct vnode  *vp  = ap->a_vp;	/* our node	*/
    329 	struct vnode **vpp = ap->a_vpp;	/* return node	*/
    330 	daddr_t *bnp  = ap->a_bnp;	/* translated	*/
    331 	daddr_t  bn   = ap->a_bn;	/* original	*/
    332 	int     *runp = ap->a_runp;
    333 	struct nilfs_node *node = VTOI(vp);
    334 	uint64_t *l2vmap;
    335 	uint32_t blocksize;
    336 	int blks, run, error;
    337 
    338 	DPRINTF(TRANSLATE, ("nilfs_bmap() called\n"));
    339 	/* XXX could return `-1' to indicate holes/zero's */
    340 
    341 	blocksize = node->nilfsdev->blocksize;
    342 	blks = MAXPHYS / blocksize;
    343 
    344 	/* get mapping memory */
    345 	l2vmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK);
    346 
    347 	/* get virtual block numbers for the vnode's buffer span */
    348 	error = nilfs_btree_nlookup(node, bn, blks, l2vmap);
    349 	if (error) {
    350 		free(l2vmap, M_TEMP);
    351 		return error;
    352 	}
    353 
    354 	/* store virtual blocks on our own vp */
    355 	if (vpp)
    356 		*vpp = vp;
    357 
    358 	/* start at virt[0] */
    359 	*bnp = l2vmap[0];
    360 
    361 	/* get runlength */
    362 	run = 1;
    363 	while ((run < blks) && (l2vmap[run] == *bnp + run))
    364 		run++;
    365 	run--;	/* see comment at start of function */
    366 
    367 	/* set runlength */
    368 	if (runp)
    369 		*runp = run;
    370 
    371 	DPRINTF(TRANSLATE, ("\tstart %"PRIu64" -> %"PRIu64" run %d\n",
    372 		bn, *bnp, run));
    373 
    374 	/* mark not translated on virtual block number 0 */
    375 	if (*bnp == 0)
    376 		*bnp = -1;
    377 
    378 	/* return success */
    379 	free(l2vmap, M_TEMP);
    380 	return 0;
    381 }
    382 
    383 /* --------------------------------------------------------------------- */
    384 
    385 static void
    386 nilfs_read_filebuf(struct nilfs_node *node, struct buf *bp)
    387 {
    388 	struct nilfs_device *nilfsdev = node->nilfsdev;
    389 	struct buf *nbp;
    390 	uint64_t *l2vmap, *v2pmap;
    391 	uint64_t from, blks;
    392 	uint32_t blocksize, buf_offset;
    393 	uint8_t  *buf_pos;
    394 	int blk2dev = nilfsdev->blocksize / DEV_BSIZE;
    395 	int i, error;
    396 
    397 	/*
    398 	 * Translate all the block sectors into a series of buffers to read
    399 	 * asynchronously from the nilfs device. Note that this lookup may
    400 	 * induce readin's too.
    401 	 */
    402 
    403 	blocksize = nilfsdev->blocksize;
    404 
    405 	from = bp->b_blkno;
    406 	blks = bp->b_bcount / blocksize;
    407 
    408 	DPRINTF(READ, ("\tread in from inode %"PRIu64" blkno %"PRIu64" "
    409 			"+ %"PRIu64" blocks\n", node->ino, from, blks));
    410 
    411 	DPRINTF(READ, ("\t\tblkno %"PRIu64" "
    412 			"+ %d bytes\n", bp->b_blkno, bp->b_bcount));
    413 
    414 	/* get mapping memory */
    415 	l2vmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK);
    416 	v2pmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK);
    417 
    418 	/* get virtual block numbers for the vnode's buffer span */
    419 	for (i = 0; i < blks; i++)
    420 		l2vmap[i] = from + i;
    421 
    422 	/* translate virtual block numbers to physical block numbers */
    423 	error = nilfs_nvtop(node, blks, l2vmap, v2pmap);
    424 	if (error)
    425 		goto out;
    426 
    427 	/* issue translated blocks */
    428 	bp->b_resid = bp->b_bcount;
    429 	for (i = 0; i < blks; i++) {
    430 		DPRINTF(READ, ("read_filebuf : ino %"PRIu64" blk %d -> "
    431 			"%"PRIu64" -> %"PRIu64"\n",
    432 			node->ino, i, l2vmap[i], v2pmap[i]));
    433 
    434 		buf_offset = i * blocksize;
    435 		buf_pos    = (uint8_t *) bp->b_data + buf_offset;
    436 
    437 		/* note virtual block 0 marks not mapped */
    438 		if (l2vmap[i] == 0) {
    439 			memset(buf_pos, 0, blocksize);
    440 			nestiobuf_done(bp, blocksize, 0);
    441 			continue;
    442 		}
    443 
    444 		/* nest iobuf */
    445 		nbp = getiobuf(NULL, true);
    446 		nestiobuf_setup(bp, nbp, buf_offset, blocksize);
    447 		KASSERT(nbp->b_vp == node->vnode);
    448 		/* nbp is B_ASYNC */
    449 
    450 		nbp->b_lblkno   = i;
    451 		nbp->b_blkno    = v2pmap[i] * blk2dev;	/* in DEV_BSIZE */
    452 		nbp->b_rawblkno = nbp->b_blkno;
    453 
    454 		VOP_STRATEGY(nilfsdev->devvp, nbp);
    455 	}
    456 
    457 	if ((bp->b_flags & B_ASYNC) == 0)
    458 		biowait(bp);
    459 
    460 out:
    461 	free(l2vmap, M_TEMP);
    462 	free(v2pmap, M_TEMP);
    463 	if (error) {
    464 		bp->b_error = EIO;
    465 		biodone(bp);
    466 	}
    467 }
    468 
    469 
    470 static void
    471 nilfs_write_filebuf(struct nilfs_node *node, struct buf *bp)
    472 {
    473 	/* TODO pass on to segment collector */
    474 	panic("nilfs_strategy writing called\n");
    475 }
    476 
    477 
    478 int
    479 nilfs_vfsstrategy(void *v)
    480 {
    481 	struct vop_strategy_args /* {
    482 		struct vnode *a_vp;
    483 		struct buf *a_bp;
    484 	} */ *ap = v;
    485 	struct vnode *vp = ap->a_vp;
    486 	struct buf   *bp = ap->a_bp;
    487 	struct nilfs_node *node = VTOI(vp);
    488 
    489 	DPRINTF(STRATEGY, ("nilfs_strategy called\n"));
    490 
    491 	/* check if we ought to be here */
    492 	if (vp->v_type == VBLK || vp->v_type == VCHR)
    493 		panic("nilfs_strategy: spec");
    494 
    495 	/* translate if needed and pass on */
    496 	if (bp->b_flags & B_READ) {
    497 		nilfs_read_filebuf(node, bp);
    498 		return bp->b_error;
    499 	}
    500 
    501 	/* send to segment collector */
    502 	nilfs_write_filebuf(node, bp);
    503 	return bp->b_error;
    504 }
    505 
    506 /* --------------------------------------------------------------------- */
    507 
    508 int
    509 nilfs_readdir(void *v)
    510 {
    511 	struct vop_readdir_args /* {
    512 		struct vnode *a_vp;
    513 		struct uio *a_uio;
    514 		kauth_cred_t a_cred;
    515 		int *a_eofflag;
    516 		off_t **a_cookies;
    517 		int *a_ncookies;
    518 	} */ *ap = v;
    519 	struct uio *uio = ap->a_uio;
    520 	struct vnode *vp = ap->a_vp;
    521 	struct nilfs_node *node = VTOI(vp);
    522 	struct nilfs_dir_entry *ndirent;
    523 	struct dirent dirent;
    524 	struct buf *bp;
    525 	uint64_t file_size, diroffset, transoffset, blkoff;
    526 	uint64_t blocknr;
    527 	uint32_t blocksize = node->nilfsdev->blocksize;
    528 	uint8_t *pos, name_len;
    529 	int error;
    530 
    531 	DPRINTF(READDIR, ("nilfs_readdir called\n"));
    532 
    533 	if (vp->v_type != VDIR)
    534 		return ENOTDIR;
    535 
    536 	file_size = nilfs_rw64(node->inode.i_size);
    537 
    538 	/* we are called just as long as we keep on pushing data in */
    539 	error = 0;
    540 	if ((uio->uio_offset < file_size) &&
    541 	    (uio->uio_resid >= sizeof(struct dirent))) {
    542 		diroffset   = uio->uio_offset;
    543 		transoffset = diroffset;
    544 
    545 		blocknr = diroffset / blocksize;
    546 		blkoff  = diroffset % blocksize;
    547 		error = nilfs_bread(node, blocknr, 0, &bp);
    548 		if (error)
    549 			return EIO;
    550 		while (diroffset < file_size) {
    551 			DPRINTF(READDIR, ("readdir : offset = %"PRIu64"\n",
    552 				diroffset));
    553 			if (blkoff >= blocksize) {
    554 				blkoff = 0; blocknr++;
    555 				brelse(bp, BC_AGE);
    556 				error = nilfs_bread(node, blocknr, 0, &bp);
    557 				if (error)
    558 					return EIO;
    559 			}
    560 
    561 			/* read in one dirent */
    562 			pos = (uint8_t *) bp->b_data + blkoff;
    563 			ndirent = (struct nilfs_dir_entry *) pos;
    564 
    565 			name_len = ndirent->name_len;
    566 			memset(&dirent, 0, sizeof(struct dirent));
    567 			dirent.d_fileno = nilfs_rw64(ndirent->inode);
    568 			dirent.d_type   = ndirent->file_type;	/* 1:1 ? */
    569 			dirent.d_namlen = name_len;
    570 			strncpy(dirent.d_name, ndirent->name, name_len);
    571 			dirent.d_reclen = _DIRENT_SIZE(&dirent);
    572 			DPRINTF(READDIR, ("copying `%*.*s`\n", name_len,
    573 				name_len, dirent.d_name));
    574 
    575 			/*
    576 			 * If there isn't enough space in the uio to return a
    577 			 * whole dirent, break off read
    578 			 */
    579 			if (uio->uio_resid < _DIRENT_SIZE(&dirent))
    580 				break;
    581 
    582 			/* transfer */
    583 			if (name_len)
    584 				uiomove(&dirent, _DIRENT_SIZE(&dirent), uio);
    585 
    586 			/* advance */
    587 			diroffset += nilfs_rw16(ndirent->rec_len);
    588 			blkoff    += nilfs_rw16(ndirent->rec_len);
    589 
    590 			/* remember the last entry we transferred */
    591 			transoffset = diroffset;
    592 		}
    593 		brelse(bp, BC_AGE);
    594 
    595 		/* pass on last transferred offset */
    596 		uio->uio_offset = transoffset;
    597 	}
    598 
    599 	if (ap->a_eofflag)
    600 		*ap->a_eofflag = (uio->uio_offset >= file_size);
    601 
    602 	return error;
    603 }
    604 
    605 /* --------------------------------------------------------------------- */
    606 
    607 int
    608 nilfs_lookup(void *v)
    609 {
    610 	struct vop_lookup_v2_args /* {
    611 		struct vnode *a_dvp;
    612 		struct vnode **a_vpp;
    613 		struct componentname *a_cnp;
    614 	} */ *ap = v;
    615 	struct vnode *dvp = ap->a_dvp;
    616 	struct vnode **vpp = ap->a_vpp;
    617 	struct componentname *cnp = ap->a_cnp;
    618 	struct mount *mp = dvp->v_mount;
    619 	uint64_t ino;
    620 	const char *name;
    621 	int namelen, nameiop, islastcn, mounted_ro;
    622 	int vnodetp;
    623 	int error, found;
    624 
    625 	*vpp = NULL;
    626 
    627 	DPRINTF(LOOKUP, ("nilfs_lookup called\n"));
    628 
    629 	/* simplify/clarification flags */
    630 	nameiop     = cnp->cn_nameiop;
    631 	islastcn    = cnp->cn_flags & ISLASTCN;
    632 	mounted_ro  = mp->mnt_flag & MNT_RDONLY;
    633 
    634 	/* check exec/dirread permissions first */
    635 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
    636 	if (error)
    637 		return error;
    638 
    639 	DPRINTF(LOOKUP, ("\taccess ok\n"));
    640 
    641 	/*
    642 	 * If requesting a modify on the last path element on a read-only
    643 	 * filingsystem, reject lookup; XXX why is this repeated in every FS ?
    644 	 */
    645 	if (islastcn && mounted_ro && (nameiop == DELETE || nameiop == RENAME))
    646 		return EROFS;
    647 
    648 	DPRINTF(LOOKUP, ("\tlooking up cnp->cn_nameptr '%s'\n",
    649 	    cnp->cn_nameptr));
    650 	/* look in the namecache */
    651 	if (cache_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
    652 			 cnp->cn_nameiop, cnp->cn_flags, NULL, vpp)) {
    653 		return *vpp == NULLVP ? ENOENT : 0;
    654 	}
    655 
    656 	DPRINTF(LOOKUP, ("\tNOT found in cache\n"));
    657 
    658 	/*
    659 	 * Obviously, the file is not (anymore) in the namecache, we have to
    660 	 * search for it. There are three basic cases: '.', '..' and others.
    661 	 *
    662 	 * Following the guidelines of VOP_LOOKUP manpage and tmpfs.
    663 	 */
    664 	error = 0;
    665 	if ((cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.')) {
    666 		DPRINTF(LOOKUP, ("\tlookup '.'\n"));
    667 		/* special case 1 '.' */
    668 		vref(dvp);
    669 		*vpp = dvp;
    670 		/* done */
    671 	} else if (cnp->cn_flags & ISDOTDOT) {
    672 		/* special case 2 '..' */
    673 		DPRINTF(LOOKUP, ("\tlookup '..'\n"));
    674 
    675 		/* get our node */
    676 		name    = "..";
    677 		namelen = 2;
    678 		error = nilfs_lookup_name_in_dir(dvp, name, namelen,
    679 				&ino, &found);
    680 		if (error)
    681 			goto out;
    682 		if (!found)
    683 			error = ENOENT;
    684 
    685 		if (error == 0) {
    686 			DPRINTF(LOOKUP, ("\tfound '..'\n"));
    687 			/* try to create/reuse the node */
    688 			error = vcache_get(mp, &ino, sizeof(ino), vpp);
    689 
    690 			if (!error) {
    691 				DPRINTF(LOOKUP,
    692 					("\tnode retrieved/created OK\n"));
    693 			}
    694 		}
    695 	} else {
    696 		DPRINTF(LOOKUP, ("\tlookup file\n"));
    697 		/* all other files */
    698 		/* lookup filename in the directory returning its inode */
    699 		name    = cnp->cn_nameptr;
    700 		namelen = cnp->cn_namelen;
    701 		error = nilfs_lookup_name_in_dir(dvp, name, namelen,
    702 				&ino, &found);
    703 		if (error)
    704 			goto out;
    705 		if (!found) {
    706 			DPRINTF(LOOKUP, ("\tNOT found\n"));
    707 			/*
    708 			 * UGH, didn't find name. If we're creating or
    709 			 * renaming on the last name this is OK and we ought
    710 			 * to return EJUSTRETURN if its allowed to be created.
    711 			 */
    712 			error = ENOENT;
    713 			if (islastcn &&
    714 				(nameiop == CREATE || nameiop == RENAME))
    715 					error = 0;
    716 			if (!error) {
    717 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
    718 				if (!error) {
    719 					error = EJUSTRETURN;
    720 				}
    721 			}
    722 			/* done */
    723 		} else {
    724 			/* try to create/reuse the node */
    725 			error = vcache_get(mp, &ino, sizeof(ino), vpp);
    726 			if (!error) {
    727 				/*
    728 				 * If we are not at the last path component
    729 				 * and found a non-directory or non-link entry
    730 				 * (which may itself be pointing to a
    731 				 * directory), raise an error.
    732 				 */
    733 				vnodetp = (*vpp)->v_type;
    734 				if ((vnodetp != VDIR) && (vnodetp != VLNK)) {
    735 					if (!islastcn) {
    736 						vrele(*vpp);
    737 						*vpp = NULL;
    738 						error = ENOTDIR;
    739 					}
    740 				}
    741 
    742 			}
    743 		}
    744 	}
    745 
    746 out:
    747 	/*
    748 	 * Store result in the cache if requested. If we are creating a file,
    749 	 * the file might not be found and thus putting it into the namecache
    750 	 * might be seen as negative caching.
    751 	 */
    752 	if (error == 0 && nameiop != CREATE)
    753 		cache_enter(dvp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
    754 			    cnp->cn_flags);
    755 
    756 	DPRINTFIF(LOOKUP, error, ("nilfs_lookup returning error %d\n", error));
    757 
    758 	if (error)
    759 		return error;
    760 	return 0;
    761 }
    762 
    763 /* --------------------------------------------------------------------- */
    764 
    765 static void
    766 nilfs_ctime_to_timespec(struct timespec *ts, uint64_t ctime)
    767 {
    768 	ts->tv_sec  = ctime;
    769 	ts->tv_nsec = 0;
    770 }
    771 
    772 
    773 int
    774 nilfs_getattr(void *v)
    775 {
    776 	struct vop_getattr_args /* {
    777 		struct vnode *a_vp;
    778 		struct vattr *a_vap;
    779 		kauth_cred_t a_cred;
    780 		struct lwp   *a_l;
    781 	} */ *ap = v;
    782 	struct vnode       *vp  = ap->a_vp;
    783 	struct vattr       *vap = ap->a_vap;
    784 	struct nilfs_node  *node = VTOI(vp);
    785 	struct nilfs_inode *inode = &node->inode;
    786 
    787 	DPRINTF(VFSCALL, ("nilfs_getattr called\n"));
    788 
    789 	/* basic info */
    790 	vattr_null(vap);
    791 	vap->va_type      = vp->v_type;
    792 	vap->va_mode      = nilfs_rw16(inode->i_mode) & ALLPERMS;
    793 	vap->va_nlink     = nilfs_rw16(inode->i_links_count);
    794 	vap->va_uid       = nilfs_rw32(inode->i_uid);
    795 	vap->va_gid       = nilfs_rw32(inode->i_gid);
    796 	vap->va_fsid      = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    797 	vap->va_fileid    = node->ino;
    798 	vap->va_size      = nilfs_rw64(inode->i_size);
    799 	vap->va_blocksize = node->nilfsdev->blocksize;
    800 
    801 	/* times */
    802 	nilfs_ctime_to_timespec(&vap->va_atime, nilfs_rw64(inode->i_mtime));
    803 	nilfs_ctime_to_timespec(&vap->va_mtime, nilfs_rw64(inode->i_mtime));
    804 	nilfs_ctime_to_timespec(&vap->va_ctime, nilfs_rw64(inode->i_ctime));
    805 	nilfs_ctime_to_timespec(&vap->va_birthtime, nilfs_rw64(inode->i_ctime));
    806 
    807 	vap->va_gen       = nilfs_rw32(inode->i_generation);
    808 	vap->va_flags     = 0;	/* vattr flags */
    809 	vap->va_bytes     = nilfs_rw64(inode->i_blocks) * vap->va_blocksize;
    810 	vap->va_filerev   = vap->va_gen;  /* XXX file revision? same as gen? */
    811 	vap->va_vaflags   = 0;  /* XXX chflags flags */
    812 
    813 	return 0;
    814 }
    815 
    816 /* --------------------------------------------------------------------- */
    817 
    818 #if 0
    819 static int
    820 nilfs_chown(struct vnode *vp, uid_t new_uid, gid_t new_gid,
    821 	  kauth_cred_t cred)
    822 {
    823 	return EINVAL;
    824 }
    825 
    826 
    827 static int
    828 nilfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred)
    829 {
    830 
    831 	return EINVAL;
    832 }
    833 
    834 
    835 /* exported */
    836 int
    837 nilfs_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred)
    838 {
    839 	return EINVAL;
    840 }
    841 
    842 
    843 static int
    844 nilfs_chflags(struct vnode *vp, mode_t mode, kauth_cred_t cred)
    845 {
    846 	return EINVAL;
    847 }
    848 
    849 
    850 static int
    851 nilfs_chtimes(struct vnode *vp,
    852 	struct timespec *atime, struct timespec *mtime,
    853 	struct timespec *birthtime, int setattrflags,
    854 	kauth_cred_t cred)
    855 {
    856 	return EINVAL;
    857 }
    858 #endif
    859 
    860 
    861 int
    862 nilfs_setattr(void *v)
    863 {
    864 	struct vop_setattr_args /* {
    865 		struct vnode *a_vp;
    866 		struct vattr *a_vap;
    867 		kauth_cred_t a_cred;
    868 		struct lwp   *a_l;
    869 	} */ *ap = v;
    870 	struct vnode *vp = ap->a_vp;
    871 
    872 	vp = vp;
    873 	DPRINTF(VFSCALL, ("nilfs_setattr called\n"));
    874 	return EINVAL;
    875 }
    876 
    877 /* --------------------------------------------------------------------- */
    878 
    879 /*
    880  * Return POSIX pathconf information for NILFS file systems.
    881  */
    882 int
    883 nilfs_pathconf(void *v)
    884 {
    885 	struct vop_pathconf_args /* {
    886 		struct vnode *a_vp;
    887 		int a_name;
    888 		register_t *a_retval;
    889 	} */ *ap = v;
    890 	uint32_t bits;
    891 
    892 	DPRINTF(VFSCALL, ("nilfs_pathconf called\n"));
    893 
    894 	switch (ap->a_name) {
    895 	case _PC_LINK_MAX:
    896 		*ap->a_retval = (1<<16)-1;	/* 16 bits */
    897 		return 0;
    898 	case _PC_NAME_MAX:
    899 		*ap->a_retval = NILFS_MAXNAMLEN;
    900 		return 0;
    901 	case _PC_PATH_MAX:
    902 		*ap->a_retval = PATH_MAX;
    903 		return 0;
    904 	case _PC_PIPE_BUF:
    905 		*ap->a_retval = PIPE_BUF;
    906 		return 0;
    907 	case _PC_CHOWN_RESTRICTED:
    908 		*ap->a_retval = 1;
    909 		return 0;
    910 	case _PC_NO_TRUNC:
    911 		*ap->a_retval = 1;
    912 		return 0;
    913 	case _PC_SYNC_IO:
    914 		*ap->a_retval = 0;     /* synchronised is off for performance */
    915 		return 0;
    916 	case _PC_FILESIZEBITS:
    917 		/* 64 bit file offsets -> 2+floor(2log(2^64-1)) = 2 + 63 = 65 */
    918 		bits = 64; /* XXX ought to deliver 65 */
    919 #if 0
    920 		if (nilfs_node)
    921 			bits = 64 * vp->v_mount->mnt_dev_bshift;
    922 #endif
    923 		*ap->a_retval = bits;
    924 		return 0;
    925 	default:
    926 		return genfs_pathconf(ap);
    927 	}
    928 }
    929 
    930 
    931 /* --------------------------------------------------------------------- */
    932 
    933 int
    934 nilfs_open(void *v)
    935 {
    936 	struct vop_open_args /* {
    937 		struct vnode *a_vp;
    938 		int a_mode;
    939 		kauth_cred_t a_cred;
    940 		struct proc *a_p;
    941 	} */ *ap = v;
    942 	int flags;
    943 
    944 	DPRINTF(VFSCALL, ("nilfs_open called\n"));
    945 
    946 	/*
    947 	 * Files marked append-only must be opened for appending.
    948 	 */
    949 	flags = 0;
    950 	if ((flags & APPEND) && (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
    951 		return (EPERM);
    952 
    953 	return 0;
    954 }
    955 
    956 
    957 /* --------------------------------------------------------------------- */
    958 
    959 int
    960 nilfs_close(void *v)
    961 {
    962 	struct vop_close_args /* {
    963 		struct vnode *a_vp;
    964 		int a_fflag;
    965 		kauth_cred_t a_cred;
    966 		struct proc *a_p;
    967 	} */ *ap = v;
    968 	struct vnode *vp = ap->a_vp;
    969 	struct nilfs_node *nilfs_node = VTOI(vp);
    970 
    971 	DPRINTF(VFSCALL, ("nilfs_close called\n"));
    972 	nilfs_node = nilfs_node;	/* shut up gcc */
    973 
    974 	mutex_enter(vp->v_interlock);
    975 	if (vrefcnt(vp) > 1)
    976 		nilfs_itimes(nilfs_node, NULL, NULL, NULL);
    977 	mutex_exit(vp->v_interlock);
    978 
    979 	return 0;
    980 }
    981 
    982 
    983 /* --------------------------------------------------------------------- */
    984 
    985 static int
    986 nilfs_check_possible(struct vnode *vp, struct vattr *vap, accmode_t accmode)
    987 {
    988 	int flags;
    989 
    990 	/* check if we are allowed to write */
    991 	switch (vap->va_type) {
    992 	case VDIR:
    993 	case VLNK:
    994 	case VREG:
    995 		/*
    996 		 * normal nodes: check if we're on a read-only mounted
    997 		 * filingsystem and bomb out if we're trying to write.
    998 		 */
    999 		if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY))
   1000 			return EROFS;
   1001 		break;
   1002 	case VBLK:
   1003 	case VCHR:
   1004 	case VSOCK:
   1005 	case VFIFO:
   1006 		/*
   1007 		 * special nodes: even on read-only mounted filingsystems
   1008 		 * these are allowed to be written to if permissions allow.
   1009 		 */
   1010 		break;
   1011 	default:
   1012 		/* no idea what this is */
   1013 		return EINVAL;
   1014 	}
   1015 
   1016 	/* noone may write immutable files */
   1017 	/* TODO: get chflags(2) flags */
   1018 	flags = 0;
   1019 	if ((accmode & VWRITE) && (flags & IMMUTABLE))
   1020 		return EPERM;
   1021 
   1022 	return 0;
   1023 }
   1024 
   1025 static int
   1026 nilfs_check_permitted(struct vnode *vp, struct vattr *vap, accmode_t accmode,
   1027     kauth_cred_t cred)
   1028 {
   1029 
   1030 	/* ask the generic genfs_can_access to advice on security */
   1031 	return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(accmode,
   1032 	    vp->v_type, vap->va_mode), vp, NULL, genfs_can_access(vp, cred,
   1033 	    vap->va_uid, vap->va_gid, vap->va_mode, NULL, accmode));
   1034 }
   1035 
   1036 int
   1037 nilfs_access(void *v)
   1038 {
   1039 	struct vop_access_args /* {
   1040 		struct vnode *a_vp;
   1041 		accmode_t a_accmode;
   1042 		kauth_cred_t a_cred;
   1043 		struct proc *a_p;
   1044 	} */ *ap = v;
   1045 	struct vnode    *vp   = ap->a_vp;
   1046 	accmode_t        accmode = ap->a_accmode;
   1047 	kauth_cred_t     cred = ap->a_cred;
   1048 	/* struct nilfs_node *nilfs_node = VTOI(vp); */
   1049 	struct vattr vap;
   1050 	int error;
   1051 
   1052 	DPRINTF(VFSCALL, ("nilfs_access called\n"));
   1053 
   1054 	error = VOP_GETATTR(vp, &vap, NULL);
   1055 	if (error)
   1056 		return error;
   1057 
   1058 	error = nilfs_check_possible(vp, &vap, accmode);
   1059 	if (error)
   1060 		return error;
   1061 
   1062 	error = nilfs_check_permitted(vp, &vap, accmode, cred);
   1063 
   1064 	return error;
   1065 }
   1066 
   1067 /* --------------------------------------------------------------------- */
   1068 
   1069 int
   1070 nilfs_create(void *v)
   1071 {
   1072 	struct vop_create_v3_args /* {
   1073 		struct vnode *a_dvp;
   1074 		struct vnode **a_vpp;
   1075 		struct componentname *a_cnp;
   1076 		struct vattr *a_vap;
   1077 	} */ *ap = v;
   1078 	struct vnode  *dvp = ap->a_dvp;
   1079 	struct vnode **vpp = ap->a_vpp;
   1080 	struct vattr  *vap  = ap->a_vap;
   1081 	struct componentname *cnp = ap->a_cnp;
   1082 	int error;
   1083 
   1084 	DPRINTF(VFSCALL, ("nilfs_create called\n"));
   1085 	error = nilfs_create_node(dvp, vpp, vap, cnp);
   1086 
   1087 	return error;
   1088 }
   1089 
   1090 /* --------------------------------------------------------------------- */
   1091 
   1092 int
   1093 nilfs_mknod(void *v)
   1094 {
   1095 	struct vop_mknod_v3_args /* {
   1096 		struct vnode *a_dvp;
   1097 		struct vnode **a_vpp;
   1098 		struct componentname *a_cnp;
   1099 		struct vattr *a_vap;
   1100 	} */ *ap = v;
   1101 	struct vnode  *dvp = ap->a_dvp;
   1102 	struct vnode **vpp = ap->a_vpp;
   1103 	struct vattr  *vap  = ap->a_vap;
   1104 	struct componentname *cnp = ap->a_cnp;
   1105 	int error;
   1106 
   1107 	DPRINTF(VFSCALL, ("nilfs_mknod called\n"));
   1108 	error = nilfs_create_node(dvp, vpp, vap, cnp);
   1109 
   1110 	return error;
   1111 }
   1112 
   1113 /* --------------------------------------------------------------------- */
   1114 
   1115 int
   1116 nilfs_mkdir(void *v)
   1117 {
   1118 	struct vop_mkdir_v3_args /* {
   1119 		struct vnode *a_dvp;
   1120 		struct vnode **a_vpp;
   1121 		struct componentname *a_cnp;
   1122 		struct vattr *a_vap;
   1123 	} */ *ap = v;
   1124 	struct vnode  *dvp = ap->a_dvp;
   1125 	struct vnode **vpp = ap->a_vpp;
   1126 	struct vattr  *vap  = ap->a_vap;
   1127 	struct componentname *cnp = ap->a_cnp;
   1128 	int error;
   1129 
   1130 	DPRINTF(VFSCALL, ("nilfs_mkdir called\n"));
   1131 	error = nilfs_create_node(dvp, vpp, vap, cnp);
   1132 
   1133 	return error;
   1134 }
   1135 
   1136 /* --------------------------------------------------------------------- */
   1137 
   1138 static int
   1139 nilfs_do_link(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
   1140 {
   1141 	struct nilfs_node *nilfs_node, *dir_node;
   1142 	struct vattr vap;
   1143 	int error;
   1144 
   1145 	DPRINTF(VFSCALL, ("nilfs_link called\n"));
   1146 	KASSERT(dvp != vp);
   1147 	KASSERT(vp->v_type != VDIR);
   1148 	KASSERT(dvp->v_mount == vp->v_mount);
   1149 
   1150 	/* lock node */
   1151 	error = vn_lock(vp, LK_EXCLUSIVE);
   1152 	if (error)
   1153 		return error;
   1154 
   1155 	/* get attributes */
   1156 	dir_node = VTOI(dvp);
   1157 	nilfs_node = VTOI(vp);
   1158 
   1159 	error = VOP_GETATTR(vp, &vap, FSCRED);
   1160 	if (error) {
   1161 		VOP_UNLOCK(vp);
   1162 		return error;
   1163 	}
   1164 
   1165 	/* check link count overflow */
   1166 	if (vap.va_nlink >= (1<<16)-1) {	/* uint16_t */
   1167 		VOP_UNLOCK(vp);
   1168 		return EMLINK;
   1169 	}
   1170 
   1171 	error = nilfs_dir_attach(dir_node->ump, dir_node, nilfs_node,
   1172 	    &vap, cnp);
   1173 	if (error)
   1174 		VOP_UNLOCK(vp);
   1175 	return error;
   1176 }
   1177 
   1178 int
   1179 nilfs_link(void *v)
   1180 {
   1181 	struct vop_link_v2_args /* {
   1182 		struct vnode *a_dvp;
   1183 		struct vnode *a_vp;
   1184 		struct componentname *a_cnp;
   1185 	} */ *ap = v;
   1186 	struct vnode *dvp = ap->a_dvp;
   1187 	struct vnode *vp  = ap->a_vp;
   1188 	struct componentname *cnp = ap->a_cnp;
   1189 	int error;
   1190 
   1191 	error = nilfs_do_link(dvp, vp, cnp);
   1192 	if (error)
   1193 		VOP_ABORTOP(dvp, cnp);
   1194 
   1195 	return error;
   1196 }
   1197 
   1198 /* --------------------------------------------------------------------- */
   1199 
   1200 static int
   1201 nilfs_do_symlink(struct nilfs_node *nilfs_node, char *target)
   1202 {
   1203 	return EROFS;
   1204 }
   1205 
   1206 
   1207 int
   1208 nilfs_symlink(void *v)
   1209 {
   1210 	struct vop_symlink_v3_args /* {
   1211 		struct vnode *a_dvp;
   1212 		struct vnode **a_vpp;
   1213 		struct componentname *a_cnp;
   1214 		struct vattr *a_vap;
   1215 		char *a_target;
   1216 	} */ *ap = v;
   1217 	struct vnode  *dvp = ap->a_dvp;
   1218 	struct vnode **vpp = ap->a_vpp;
   1219 	struct vattr  *vap  = ap->a_vap;
   1220 	struct componentname *cnp = ap->a_cnp;
   1221 	struct nilfs_node *dir_node;
   1222 	struct nilfs_node *nilfs_node;
   1223 	int error;
   1224 
   1225 	DPRINTF(VFSCALL, ("nilfs_symlink called\n"));
   1226 	DPRINTF(VFSCALL, ("\tlinking to `%s`\n",  ap->a_target));
   1227 	error = nilfs_create_node(dvp, vpp, vap, cnp);
   1228 	KASSERT(((error == 0) && (*vpp != NULL)) || ((error && (*vpp == NULL))));
   1229 	if (!error) {
   1230 		dir_node = VTOI(dvp);
   1231 		nilfs_node = VTOI(*vpp);
   1232 		KASSERT(nilfs_node);
   1233 		error = nilfs_do_symlink(nilfs_node, ap->a_target);
   1234 		if (error) {
   1235 			/* remove node */
   1236 			nilfs_shrink_node(nilfs_node, 0);
   1237 			nilfs_dir_detach(nilfs_node->ump, dir_node, nilfs_node, cnp);
   1238 		}
   1239 	}
   1240 	return error;
   1241 }
   1242 
   1243 /* --------------------------------------------------------------------- */
   1244 
   1245 int
   1246 nilfs_readlink(void *v)
   1247 {
   1248 	struct vop_readlink_args /* {
   1249 		struct vnode *a_vp;
   1250 		struct uio *a_uio;
   1251 		kauth_cred_t a_cred;
   1252 	} */ *ap = v;
   1253 #if 0
   1254 	struct vnode *vp = ap->a_vp;
   1255 	struct uio *uio = ap->a_uio;
   1256 	kauth_cred_t cred = ap->a_cred;
   1257 	struct nilfs_node *nilfs_node;
   1258 	struct pathcomp pathcomp;
   1259 	struct vattr vattr;
   1260 	uint8_t *pathbuf, *targetbuf, *tmpname;
   1261 	uint8_t *pathpos, *targetpos;
   1262 	char *mntonname;
   1263 	int pathlen, targetlen, namelen, mntonnamelen, len, l_ci;
   1264 	int first, error;
   1265 #endif
   1266 	ap = ap;
   1267 
   1268 	DPRINTF(VFSCALL, ("nilfs_readlink called\n"));
   1269 
   1270 	return EROFS;
   1271 }
   1272 
   1273 /* --------------------------------------------------------------------- */
   1274 
   1275 /* note: i tried to follow the logic of the tmpfs rename code */
   1276 int
   1277 nilfs_rename(void *v)
   1278 {
   1279 	struct vop_rename_args /* {
   1280 		struct vnode *a_fdvp;
   1281 		struct vnode *a_fvp;
   1282 		struct componentname *a_fcnp;
   1283 		struct vnode *a_tdvp;
   1284 		struct vnode *a_tvp;
   1285 		struct componentname *a_tcnp;
   1286 	} */ *ap = v;
   1287 	struct vnode *tvp = ap->a_tvp;
   1288 	struct vnode *tdvp = ap->a_tdvp;
   1289 	struct vnode *fvp = ap->a_fvp;
   1290 	struct vnode *fdvp = ap->a_fdvp;
   1291 	struct componentname *tcnp = ap->a_tcnp;
   1292 	struct componentname *fcnp = ap->a_fcnp;
   1293 	struct nilfs_node *fnode, *fdnode, *tnode, *tdnode;
   1294 	struct vattr fvap, tvap;
   1295 	int error;
   1296 
   1297 	DPRINTF(VFSCALL, ("nilfs_rename called\n"));
   1298 
   1299 	/* disallow cross-device renames */
   1300 	if (fvp->v_mount != tdvp->v_mount ||
   1301 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
   1302 		error = EXDEV;
   1303 		goto out_unlocked;
   1304 	}
   1305 
   1306 	fnode  = VTOI(fvp);
   1307 	fdnode = VTOI(fdvp);
   1308 	tnode  = (tvp == NULL) ? NULL : VTOI(tvp);
   1309 	tdnode = VTOI(tdvp);
   1310 
   1311 	/* lock our source dir */
   1312 	if (fdnode != tdnode) {
   1313 		error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
   1314 		if (error != 0)
   1315 			goto out_unlocked;
   1316 	}
   1317 
   1318 	/* get info about the node to be moved */
   1319 	vn_lock(fvp, LK_SHARED | LK_RETRY);
   1320 	error = VOP_GETATTR(fvp, &fvap, FSCRED);
   1321 	VOP_UNLOCK(fvp);
   1322 	KASSERT(error == 0);
   1323 
   1324 	/* check when to delete the old already existing entry */
   1325 	if (tvp) {
   1326 		/* get info about the node to be moved to */
   1327 		error = VOP_GETATTR(tvp, &tvap, FSCRED);
   1328 		KASSERT(error == 0);
   1329 
   1330 		/* if both dirs, make sure the destination is empty */
   1331 		if (fvp->v_type == VDIR && tvp->v_type == VDIR) {
   1332 			if (tvap.va_nlink > 2) {
   1333 				error = ENOTEMPTY;
   1334 				goto out;
   1335 			}
   1336 		}
   1337 		/* if moving dir, make sure destination is dir too */
   1338 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   1339 			error = ENOTDIR;
   1340 			goto out;
   1341 		}
   1342 		/* if we're moving a non-directory, make sure dest is no dir */
   1343 		if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   1344 			error = EISDIR;
   1345 			goto out;
   1346 		}
   1347 	}
   1348 
   1349 	/* don't allow renaming directories across directory for now */
   1350 	if (fdnode != tdnode) {
   1351 		if (fvp->v_type == VDIR) {
   1352 			error = EINVAL;
   1353 			goto out;
   1354 		}
   1355 	}
   1356 
   1357 	/* remove existing entry if present */
   1358 	if (tvp)
   1359 		nilfs_dir_detach(tdnode->ump, tdnode, tnode, tcnp);
   1360 
   1361 	/* create new directory entry for the node */
   1362 	error = nilfs_dir_attach(tdnode->ump, tdnode, fnode, &fvap, tcnp);
   1363 	if (error)
   1364 		goto out;
   1365 
   1366 	/* unlink old directory entry for the node, if failing, unattach new */
   1367 	error = nilfs_dir_detach(tdnode->ump, fdnode, fnode, fcnp);
   1368 	if (error)
   1369 		nilfs_dir_detach(tdnode->ump, tdnode, fnode, tcnp);
   1370 
   1371 out:
   1372         if (fdnode != tdnode)
   1373                 VOP_UNLOCK(fdvp);
   1374 
   1375 out_unlocked:
   1376 	VOP_ABORTOP(tdvp, tcnp);
   1377 	if (tdvp == tvp)
   1378 		vrele(tdvp);
   1379 	else
   1380 		vput(tdvp);
   1381 	if (tvp)
   1382 		vput(tvp);
   1383 	VOP_ABORTOP(fdvp, fcnp);
   1384 
   1385 	/* release source nodes. */
   1386 	vrele(fdvp);
   1387 	vrele(fvp);
   1388 
   1389 	return error;
   1390 }
   1391 
   1392 /* --------------------------------------------------------------------- */
   1393 
   1394 int
   1395 nilfs_remove(void *v)
   1396 {
   1397 	struct vop_remove_v3_args /* {
   1398 		struct vnode *a_dvp;
   1399 		struct vnode *a_vp;
   1400 		struct componentname *a_cnp;
   1401 		nlink_t ctx_vp_new_nlink;
   1402 	} */ *ap = v;
   1403 	struct vnode *dvp = ap->a_dvp;
   1404 	struct vnode *vp  = ap->a_vp;
   1405 	struct componentname *cnp = ap->a_cnp;
   1406 	struct nilfs_node *dir_node = VTOI(dvp);
   1407 	struct nilfs_node *nilfs_node = VTOI(vp);
   1408 	struct nilfs_mount *ump = dir_node->ump;
   1409 	int error;
   1410 
   1411 	DPRINTF(VFSCALL, ("nilfs_remove called\n"));
   1412 	if (vp->v_type != VDIR) {
   1413 		error = nilfs_dir_detach(ump, dir_node, nilfs_node, cnp);
   1414 		DPRINTFIF(NODE, error, ("\tgot error removing file\n"));
   1415 	} else {
   1416 		DPRINTF(NODE, ("\tis a directory: perm. denied\n"));
   1417 		error = EPERM;
   1418 	}
   1419 
   1420 	if (dvp == vp)
   1421 		vrele(vp);
   1422 	else
   1423 		vput(vp);
   1424 
   1425 	return error;
   1426 }
   1427 
   1428 /* --------------------------------------------------------------------- */
   1429 
   1430 int
   1431 nilfs_rmdir(void *v)
   1432 {
   1433 	struct vop_rmdir_v2_args /* {
   1434 		struct vnode *a_dvp;
   1435 		struct vnode *a_vp;
   1436 		struct componentname *a_cnp;
   1437 	} */ *ap = v;
   1438 	struct vnode *vp = ap->a_vp;
   1439 	struct vnode *dvp = ap->a_dvp;
   1440 	struct componentname *cnp = ap->a_cnp;
   1441 	struct nilfs_node *dir_node = VTOI(dvp);
   1442 	struct nilfs_node *nilfs_node = VTOI(vp);
   1443 	struct nilfs_mount *ump = dir_node->ump;
   1444 	int refcnt, error;
   1445 
   1446 	DPRINTF(NOTIMPL, ("nilfs_rmdir called\n"));
   1447 
   1448 	/* don't allow '.' to be deleted */
   1449 	if (dir_node == nilfs_node) {
   1450 		vrele(vp);
   1451 		return EINVAL;
   1452 	}
   1453 
   1454 	/* check to see if the directory is empty */
   1455 	error = 0;
   1456 	refcnt = 2; /* XXX */
   1457 	if (refcnt > 1) {
   1458 		/* NOT empty */
   1459 		vput(vp);
   1460 		return ENOTEMPTY;
   1461 	}
   1462 
   1463 	/* detach the node from the directory */
   1464 	error = nilfs_dir_detach(ump, dir_node, nilfs_node, cnp);
   1465 	if (error == 0) {
   1466 		cache_purge(vp);
   1467 //		cache_purge(dvp);	/* XXX from msdosfs, why? */
   1468 	}
   1469 	DPRINTFIF(NODE, error, ("\tgot error removing file\n"));
   1470 
   1471 	/* put the node and exit */
   1472 	vput(vp);
   1473 
   1474 	return error;
   1475 }
   1476 
   1477 /* --------------------------------------------------------------------- */
   1478 
   1479 int
   1480 nilfs_fsync(void *v)
   1481 {
   1482 	struct vop_fsync_args /* {
   1483 		struct vnode *a_vp;
   1484 		kauth_cred_t a_cred;
   1485 		int a_flags;
   1486 		off_t offlo;
   1487 		off_t offhi;
   1488 		struct proc *a_p;
   1489 	} */ *ap = v;
   1490 	struct vnode *vp = ap->a_vp;
   1491 //	struct nilfs_node *nilfs_node = VTOI(vp);
   1492 //	int error, flags, wait;
   1493 
   1494 	DPRINTF(STRATEGY, ("nilfs_fsync called : %s, %s\n",
   1495 		(ap->a_flags & FSYNC_WAIT)     ? "wait":"no wait",
   1496 		(ap->a_flags & FSYNC_DATAONLY) ? "data_only":"complete"));
   1497 
   1498 	vp = vp;
   1499 	return 0;
   1500 }
   1501 
   1502 /* --------------------------------------------------------------------- */
   1503 
   1504 int
   1505 nilfs_advlock(void *v)
   1506 {
   1507 	struct vop_advlock_args /* {
   1508 		struct vnode *a_vp;
   1509 		void *a_id;
   1510 		int a_op;
   1511 		struct flock *a_fl;
   1512 		int a_flags;
   1513 	} */ *ap = v;
   1514 	struct vnode *vp = ap->a_vp;
   1515 	struct nilfs_node *nilfs_node = VTOI(vp);
   1516 	uint64_t file_size;
   1517 
   1518 	DPRINTF(LOCKING, ("nilfs_advlock called\n"));
   1519 
   1520 	assert(nilfs_node);
   1521 	file_size = nilfs_rw64(nilfs_node->inode.i_size);
   1522 
   1523 	return lf_advlock(ap, &nilfs_node->lockf, file_size);
   1524 }
   1525 
   1526 /* --------------------------------------------------------------------- */
   1527 
   1528 
   1529 /* Global vfs vnode data structures for nilfss */
   1530 int (**nilfs_vnodeop_p) __P((void *));
   1531 
   1532 const struct vnodeopv_entry_desc nilfs_vnodeop_entries[] = {
   1533 	{ &vop_default_desc, vn_default_error },
   1534 	{ &vop_parsepath_desc, genfs_parsepath }, /* parsepath */
   1535 	{ &vop_lookup_desc, nilfs_lookup },	/* lookup */
   1536 	{ &vop_create_desc, nilfs_create },	/* create */
   1537 	{ &vop_mknod_desc, nilfs_mknod },	/* mknod */	/* TODO */
   1538 	{ &vop_open_desc, nilfs_open },		/* open */
   1539 	{ &vop_close_desc, nilfs_close },	/* close */
   1540 	{ &vop_access_desc, nilfs_access },	/* access */
   1541 	{ &vop_accessx_desc, genfs_accessx },	/* accessx */
   1542 	{ &vop_getattr_desc, nilfs_getattr },	/* getattr */
   1543 	{ &vop_setattr_desc, nilfs_setattr },	/* setattr */	/* TODO chflags */
   1544 	{ &vop_read_desc, nilfs_read },		/* read */
   1545 	{ &vop_write_desc, nilfs_write },	/* write */	/* WRITE */
   1546 	{ &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
   1547 	{ &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
   1548 	{ &vop_fcntl_desc, genfs_fcntl },	/* fcntl */	/* TODO? */
   1549 	{ &vop_ioctl_desc, genfs_enoioctl },	/* ioctl */	/* TODO? */
   1550 	{ &vop_poll_desc, genfs_poll },		/* poll */	/* TODO/OK? */
   1551 	{ &vop_kqfilter_desc, genfs_kqfilter },	/* kqfilter */	/* ? */
   1552 	{ &vop_revoke_desc, genfs_revoke },	/* revoke */	/* TODO? */
   1553 	{ &vop_mmap_desc, genfs_mmap },		/* mmap */	/* OK? */
   1554 	{ &vop_fsync_desc, nilfs_fsync },	/* fsync */
   1555 	{ &vop_seek_desc, genfs_seek },		/* seek */
   1556 	{ &vop_remove_desc, nilfs_remove },	/* remove */
   1557 	{ &vop_link_desc, nilfs_link },		/* link */	/* TODO */
   1558 	{ &vop_rename_desc, nilfs_rename },	/* rename */ 	/* TODO */
   1559 	{ &vop_mkdir_desc, nilfs_mkdir },	/* mkdir */
   1560 	{ &vop_rmdir_desc, nilfs_rmdir },	/* rmdir */
   1561 	{ &vop_symlink_desc, nilfs_symlink },	/* symlink */	/* TODO */
   1562 	{ &vop_readdir_desc, nilfs_readdir },	/* readdir */
   1563 	{ &vop_readlink_desc, nilfs_readlink },	/* readlink */	/* TEST ME */
   1564 	{ &vop_abortop_desc, genfs_abortop },	/* abortop */	/* TODO/OK? */
   1565 	{ &vop_inactive_desc, nilfs_inactive },	/* inactive */
   1566 	{ &vop_reclaim_desc, nilfs_reclaim },	/* reclaim */
   1567 	{ &vop_lock_desc, genfs_lock },		/* lock */
   1568 	{ &vop_unlock_desc, genfs_unlock },	/* unlock */
   1569 	{ &vop_bmap_desc, nilfs_trivial_bmap },	/* bmap */	/* 1:1 bmap */
   1570 	{ &vop_strategy_desc, nilfs_vfsstrategy },/* strategy */
   1571 /*	{ &vop_print_desc, nilfs_print },	*/	/* print */
   1572 	{ &vop_islocked_desc, genfs_islocked },	/* islocked */
   1573 	{ &vop_pathconf_desc, nilfs_pathconf },	/* pathconf */
   1574 	{ &vop_advlock_desc, nilfs_advlock },	/* advlock */	/* TEST ME */
   1575 	{ &vop_bwrite_desc, vn_bwrite },	/* bwrite */	/* ->strategy */
   1576 	{ &vop_getpages_desc, genfs_getpages },	/* getpages */
   1577 	{ &vop_putpages_desc, genfs_putpages },	/* putpages */
   1578 	{ NULL, NULL }
   1579 };
   1580 
   1581 
   1582 const struct vnodeopv_desc nilfs_vnodeop_opv_desc = {
   1583 	&nilfs_vnodeop_p, nilfs_vnodeop_entries
   1584 };
   1585 
   1586