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