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