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