Home | History | Annotate | Line # | Download | only in nilfs
nilfs_vnops.c revision 1.12
      1 /* $NetBSD: nilfs_vnops.c,v 1.12 2011/06/12 03:35:53 rmind 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.12 2011/06/12 03:35:53 rmind Exp $");
     32 #endif /* not lint */
     33 
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/namei.h>
     38 #include <sys/resourcevar.h>	/* defines plimit structure in proc struct */
     39 #include <sys/kernel.h>
     40 #include <sys/file.h>		/* define FWRITE ... */
     41 #include <sys/stat.h>
     42 #include <sys/buf.h>
     43 #include <sys/proc.h>
     44 #include <sys/mount.h>
     45 #include <sys/vnode.h>
     46 #include <sys/signalvar.h>
     47 #include <sys/malloc.h>
     48 #include <sys/dirent.h>
     49 #include <sys/lockf.h>
     50 #include <sys/kauth.h>
     51 
     52 #include <miscfs/genfs/genfs.h>
     53 #include <uvm/uvm_extern.h>
     54 
     55 #include <fs/nilfs/nilfs_mount.h>
     56 #include "nilfs.h"
     57 #include "nilfs_subr.h"
     58 #include "nilfs_bswap.h"
     59 
     60 
     61 #define VTOI(vnode) ((struct nilfs_node *) (vnode)->v_data)
     62 
     63 
     64 /* externs */
     65 extern int prtactive;
     66 
     67 /* implementations of vnode functions; table follows at end */
     68 /* --------------------------------------------------------------------- */
     69 
     70 int
     71 nilfs_inactive(void *v)
     72 {
     73 	struct vop_inactive_args /* {
     74 		struct vnode *a_vp;
     75 		bool         *a_recycle;
     76 	} */ *ap = v;
     77 	struct vnode *vp = ap->a_vp;
     78 	struct nilfs_node *nilfs_node = VTOI(vp);
     79 
     80 	DPRINTF(NODE, ("nilfs_inactive called for nilfs_node %p\n", VTOI(vp)));
     81 
     82 	if (nilfs_node == NULL) {
     83 		DPRINTF(NODE, ("nilfs_inactive: inactive NULL NILFS node\n"));
     84 		VOP_UNLOCK(vp);
     85 		return 0;
     86 	}
     87 
     88 	/*
     89 	 * Optionally flush metadata to disc. If the file has not been
     90 	 * referenced anymore in a directory we ought to free up the resources
     91 	 * on disc if applicable.
     92 	 */
     93 	VOP_UNLOCK(vp);
     94 
     95 	return 0;
     96 }
     97 
     98 /* --------------------------------------------------------------------- */
     99 
    100 int
    101 nilfs_reclaim(void *v)
    102 {
    103 	struct vop_reclaim_args /* {
    104 		struct vnode *a_vp;
    105 	} */ *ap = v;
    106 	struct vnode *vp = ap->a_vp;
    107 	struct nilfs_node *nilfs_node = VTOI(vp);
    108 
    109 	DPRINTF(NODE, ("nilfs_reclaim called for node %p\n", nilfs_node));
    110 	if (prtactive && vp->v_usecount > 1)
    111 		vprint("nilfs_reclaim(): pushing active", vp);
    112 
    113 	if (nilfs_node == NULL) {
    114 		DPRINTF(NODE, ("nilfs_reclaim(): null nilfsnode\n"));
    115 		return 0;
    116 	}
    117 
    118 	/* update note for closure */
    119 	nilfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
    120 
    121 	/* dispose all node knowledge */
    122 	nilfs_dispose_node(&nilfs_node);
    123 
    124 	return 0;
    125 }
    126 
    127 /* --------------------------------------------------------------------- */
    128 
    129 int
    130 nilfs_read(void *v)
    131 {
    132 	struct vop_read_args /* {
    133 		struct vnode *a_vp;
    134 		struct uio *a_uio;
    135 		int a_ioflag;
    136 		kauth_cred_t a_cred;
    137 	} */ *ap = v;
    138 	struct vnode *vp     = ap->a_vp;
    139 	struct uio   *uio    = ap->a_uio;
    140 	int           ioflag = ap->a_ioflag;
    141 	int           advice = IO_ADV_DECODE(ap->a_ioflag);
    142 	struct uvm_object    *uobj;
    143 	struct nilfs_node      *nilfs_node = VTOI(vp);
    144 	uint64_t file_size;
    145 	vsize_t len;
    146 	int error;
    147 	int flags;
    148 
    149 	DPRINTF(READ, ("nilfs_read called\n"));
    150 
    151 	/* can this happen? some filingsystems have this check */
    152 	if (uio->uio_offset < 0)
    153 		return EINVAL;
    154 	if (uio->uio_resid == 0)
    155 		return 0;
    156 
    157 	/* protect against rogue programs reading raw directories and links */
    158 	if ((ioflag & IO_ALTSEMANTICS) == 0) {
    159 		if (vp->v_type == VDIR)
    160 			return EISDIR;
    161 		/* all but regular files just give EINVAL */
    162 		if (vp->v_type != VREG)
    163 			return EINVAL;
    164 	}
    165 
    166 	assert(nilfs_node);
    167 	file_size = nilfs_rw64(nilfs_node->inode.i_size);
    168 
    169 	/* read contents using buffercache */
    170 	uobj = &vp->v_uobj;
    171 	flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
    172 	error = 0;
    173 	while (uio->uio_resid > 0) {
    174 		/* reached end? */
    175 		if (file_size <= uio->uio_offset)
    176 			break;
    177 
    178 		/* maximise length to file extremity */
    179 		len = MIN(file_size - uio->uio_offset, uio->uio_resid);
    180 		if (len == 0)
    181 			break;
    182 
    183 		/* ubc, here we come, prepare to trap */
    184 		error = ubc_uiomove(uobj, uio, len, advice,
    185 		    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
    186 		if (error)
    187 			break;
    188 	}
    189 
    190 	/* note access time unless not requested */
    191 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
    192 		nilfs_node->i_flags |= IN_ACCESS;
    193 		if ((ioflag & IO_SYNC) == IO_SYNC)
    194 			error = nilfs_update(vp, NULL, NULL, NULL, UPDATE_WAIT);
    195 	}
    196 
    197 	return error;
    198 }
    199 
    200 /* --------------------------------------------------------------------- */
    201 
    202 int
    203 nilfs_write(void *v)
    204 {
    205 	struct vop_write_args /* {
    206 		struct vnode *a_vp;
    207 		struct uio *a_uio;
    208 		int a_ioflag;
    209 		kauth_cred_t a_cred;
    210 	} */ *ap = v;
    211 	struct vnode *vp     = ap->a_vp;
    212 	struct uio   *uio    = ap->a_uio;
    213 	int           ioflag = ap->a_ioflag;
    214 	int           advice = IO_ADV_DECODE(ap->a_ioflag);
    215 	struct uvm_object    *uobj;
    216 	struct nilfs_node      *nilfs_node = VTOI(vp);
    217 	uint64_t file_size, old_size;
    218 	vsize_t len;
    219 	int error;
    220 	int flags, resid, extended;
    221 
    222 	DPRINTF(WRITE, ("nilfs_write called\n"));
    223 
    224 	/* can this happen? some filingsystems have this check */
    225 	if (uio->uio_offset < 0)
    226 		return EINVAL;
    227 	if (uio->uio_resid == 0)
    228 		return 0;
    229 
    230 	/* protect against rogue programs writing raw directories or links */
    231 	if ((ioflag & IO_ALTSEMANTICS) == 0) {
    232 		if (vp->v_type == VDIR)
    233 			return EISDIR;
    234 		/* all but regular files just give EINVAL for now */
    235 		if (vp->v_type != VREG)
    236 			return EINVAL;
    237 	}
    238 
    239 	assert(nilfs_node);
    240 	panic("nilfs_write() called\n");
    241 return EIO;
    242 
    243 	/* remember old file size */
    244 	assert(nilfs_node);
    245 	file_size = nilfs_rw64(nilfs_node->inode.i_size);
    246 	old_size = file_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 	flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
    267 	resid = uio->uio_resid;
    268 	error = 0;
    269 
    270 	uvm_vnp_setwritesize(vp, file_size);
    271 	while (uio->uio_resid > 0) {
    272 		/* maximise length to file extremity */
    273 		len = MIN(file_size - uio->uio_offset, uio->uio_resid);
    274 		if (len == 0)
    275 			break;
    276 
    277 		/* ubc, here we come, prepare to trap */
    278 		error = ubc_uiomove(uobj, uio, len, advice,
    279 		    UBC_WRITE | UBC_UNMAP_FLAG(vp));
    280 		if (error)
    281 			break;
    282 	}
    283 	uvm_vnp_setsize(vp, file_size);
    284 
    285 	/* mark node changed and request update */
    286 	nilfs_node->i_flags |= IN_CHANGE | IN_UPDATE;
    287 
    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 
    322 int
    323 nilfs_trivial_bmap(void *v)
    324 {
    325 	struct vop_bmap_args /* {
    326 		struct vnode *a_vp;
    327 		daddr_t a_bn;
    328 		struct vnode **a_vpp;
    329 		daddr_t *a_bnp;
    330 		int *a_runp;
    331 	} */ *ap = v;
    332 	struct vnode  *vp  = ap->a_vp;	/* our node	*/
    333 	struct vnode **vpp = ap->a_vpp;	/* return node	*/
    334 	daddr_t *bnp  = ap->a_bnp;	/* translated	*/
    335 	daddr_t  bn   = ap->a_bn;	/* origional	*/
    336 	int     *runp = ap->a_runp;
    337 	struct nilfs_node *node = VTOI(vp);
    338 	uint64_t *l2vmap;
    339 	uint32_t blocksize;
    340 	int blks, run, error;
    341 
    342 	DPRINTF(TRANSLATE, ("nilfs_bmap() called\n"));
    343 	/* XXX could return `-1' to indicate holes/zero's */
    344 
    345 	blocksize = node->nilfsdev->blocksize;
    346 	blks = MAXPHYS / blocksize;
    347 
    348 	/* get mapping memory */
    349 	l2vmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK);
    350 
    351 	/* get virtual block numbers for the vnode's buffer span */
    352 	error = nilfs_btree_nlookup(node, bn, blks, l2vmap);
    353 	if (error) {
    354 		free(l2vmap, M_TEMP);
    355 		return error;
    356 	}
    357 
    358 	/* store virtual blocks on our own vp */
    359 	if (vpp)
    360 		*vpp = vp;
    361 
    362 	/* start at virt[0] */
    363 	*bnp = l2vmap[0];
    364 
    365 	/* get runlength */
    366 	run = 1;
    367 	while ((run < blks) && (l2vmap[run] == *bnp + run))
    368 		run++;
    369 
    370 	/* set runlength */
    371 	if (runp)
    372 		*runp = run;
    373 
    374 	DPRINTF(TRANSLATE, ("\tstart %"PRIu64" -> %"PRIu64" run %d\n",
    375 		bn, *bnp, run));
    376 
    377 	/* mark not translated on virtual block number 0 */
    378 	if (*bnp == 0)
    379 		*bnp = -1;
    380 
    381 	/* return success */
    382 	free(l2vmap, M_TEMP);
    383 	return 0;
    384 }
    385 
    386 /* --------------------------------------------------------------------- */
    387 
    388 static void
    389 nilfs_read_filebuf(struct nilfs_node *node, struct buf *bp)
    390 {
    391 	struct nilfs_device *nilfsdev = node->nilfsdev;
    392 	struct buf *nbp;
    393 	uint64_t *l2vmap, *v2pmap;
    394 	uint64_t from, blks;
    395 	uint32_t blocksize, buf_offset;
    396 	uint8_t  *buf_pos;
    397 	int blk2dev = nilfsdev->blocksize / DEV_BSIZE;
    398 	int i, error;
    399 
    400 	/*
    401 	 * Translate all the block sectors into a series of buffers to read
    402 	 * asynchronously from the nilfs device. Note that this lookup may
    403 	 * induce readin's too.
    404 	 */
    405 
    406 	blocksize = nilfsdev->blocksize;
    407 
    408 	from = bp->b_blkno;
    409 	blks = bp->b_bcount / blocksize;
    410 
    411 	DPRINTF(READ, ("\tread in from inode %"PRIu64" blkno %"PRIu64" "
    412 			"+ %"PRIu64" blocks\n", node->ino, from, blks));
    413 
    414 	DPRINTF(READ, ("\t\tblkno %"PRIu64" "
    415 			"+ %d bytes\n", bp->b_blkno, bp->b_bcount));
    416 
    417 	/* get mapping memory */
    418 	l2vmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK);
    419 	v2pmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK);
    420 
    421 	/* get virtual block numbers for the vnode's buffer span */
    422 	for (i = 0; i < blks; i++)
    423 		l2vmap[i] = from + i;
    424 
    425 	/* translate virtual block numbers to physical block numbers */
    426 	error = nilfs_nvtop(node, blks, l2vmap, v2pmap);
    427 	if (error)
    428 		goto out;
    429 
    430 	/* issue translated blocks */
    431 	bp->b_resid = bp->b_bcount;
    432 	for (i = 0; i < blks; i++) {
    433 		DPRINTF(READ, ("read_filebuf : ino %"PRIu64" blk %d -> "
    434 			"%"PRIu64" -> %"PRIu64"\n",
    435 			node->ino, i, l2vmap[i], v2pmap[i]));
    436 
    437 		buf_offset = i * blocksize;
    438 		buf_pos    = (uint8_t *) bp->b_data + buf_offset;
    439 
    440 		/* note virtual block 0 marks not mapped */
    441 		if (l2vmap[i] == 0) {
    442 			memset(buf_pos, 0, blocksize);
    443 			nestiobuf_done(bp, blocksize, 0);
    444 			continue;
    445 		}
    446 
    447 		/* nest iobuf */
    448 		nbp = getiobuf(NULL, true);
    449 		nestiobuf_setup(bp, nbp, buf_offset, blocksize);
    450 		KASSERT(nbp->b_vp == node->vnode);
    451 		/* nbp is B_ASYNC */
    452 
    453 		nbp->b_lblkno   = i;
    454 		nbp->b_blkno    = v2pmap[i] * blk2dev;	/* in DEV_BSIZE */
    455 		nbp->b_rawblkno = nbp->b_blkno;
    456 
    457 		VOP_STRATEGY(nilfsdev->devvp, nbp);
    458 	}
    459 
    460 	if ((bp->b_flags & B_ASYNC) == 0)
    461 		biowait(bp);
    462 
    463 out:
    464 	free(l2vmap, M_TEMP);
    465 	free(v2pmap, M_TEMP);
    466 	if (error) {
    467 		bp->b_error = EIO;
    468 		biodone(bp);
    469 	}
    470 }
    471 
    472 
    473 static void
    474 nilfs_write_filebuf(struct nilfs_node *node, struct buf *bp)
    475 {
    476 	/* TODO pass on to segment collector */
    477 	panic("nilfs_strategy writing called\n");
    478 }
    479 
    480 
    481 int
    482 nilfs_vfsstrategy(void *v)
    483 {
    484 	struct vop_strategy_args /* {
    485 		struct vnode *a_vp;
    486 		struct buf *a_bp;
    487 	} */ *ap = v;
    488 	struct vnode *vp = ap->a_vp;
    489 	struct buf   *bp = ap->a_bp;
    490 	struct nilfs_node *node = VTOI(vp);
    491 
    492 	DPRINTF(STRATEGY, ("nilfs_strategy called\n"));
    493 
    494 	/* check if we ought to be here */
    495 	if (vp->v_type == VBLK || vp->v_type == VCHR)
    496 		panic("nilfs_strategy: spec");
    497 
    498 	/* translate if needed and pass on */
    499 	if (bp->b_flags & B_READ) {
    500 		nilfs_read_filebuf(node, bp);
    501 		return bp->b_error;
    502 	}
    503 
    504 	/* send to segment collector */
    505 	nilfs_write_filebuf(node, bp);
    506 	return bp->b_error;
    507 }
    508 
    509 /* --------------------------------------------------------------------- */
    510 
    511 int
    512 nilfs_readdir(void *v)
    513 {
    514 	struct vop_readdir_args /* {
    515 		struct vnode *a_vp;
    516 		struct uio *a_uio;
    517 		kauth_cred_t a_cred;
    518 		int *a_eofflag;
    519 		off_t **a_cookies;
    520 		int *a_ncookies;
    521 	} */ *ap = v;
    522 	struct uio *uio = ap->a_uio;
    523 	struct vnode *vp = ap->a_vp;
    524 	struct nilfs_node *node = VTOI(vp);
    525 	struct nilfs_dir_entry *ndirent;
    526 	struct dirent dirent;
    527 	struct buf *bp;
    528 	uint64_t file_size, diroffset, transoffset, blkoff;
    529 	uint64_t blocknr;
    530 	uint32_t blocksize = node->nilfsdev->blocksize;
    531 	uint8_t *pos, name_len;
    532 	int error;
    533 
    534 	DPRINTF(READDIR, ("nilfs_readdir called\n"));
    535 
    536 	if (vp->v_type != VDIR)
    537 		return ENOTDIR;
    538 
    539 	file_size = nilfs_rw64(node->inode.i_size);
    540 
    541 	/* we are called just as long as we keep on pushing data in */
    542 	error = 0;
    543 	if ((uio->uio_offset < file_size) &&
    544 	    (uio->uio_resid >= sizeof(struct dirent))) {
    545 		diroffset   = uio->uio_offset;
    546 		transoffset = diroffset;
    547 
    548 		blocknr = diroffset / blocksize;
    549 		blkoff  = diroffset % blocksize;
    550 		error = nilfs_bread(node, blocknr, NOCRED, 0, &bp);
    551 		if (error)
    552 			return EIO;
    553 		while (diroffset < file_size) {
    554 			DPRINTF(READDIR, ("readdir : offset = %"PRIu64"\n",
    555 				diroffset));
    556 			if (blkoff >= blocksize) {
    557 				blkoff = 0; blocknr++;
    558 				brelse(bp, BC_AGE);
    559 				error = nilfs_bread(node, blocknr, NOCRED, 0,
    560 						&bp);
    561 				if (error)
    562 					return EIO;
    563 			}
    564 
    565 			/* read in one dirent */
    566 			pos = (uint8_t *) bp->b_data + blkoff;
    567 			ndirent = (struct nilfs_dir_entry *) pos;
    568 
    569 			name_len = ndirent->name_len;
    570 			memset(&dirent, 0, sizeof(struct dirent));
    571 			dirent.d_fileno = nilfs_rw64(ndirent->inode);
    572 			dirent.d_type   = ndirent->file_type;	/* 1:1 ? */
    573 			dirent.d_namlen = name_len;
    574 			strncpy(dirent.d_name, ndirent->name, name_len);
    575 			dirent.d_reclen = _DIRENT_SIZE(&dirent);
    576 			DPRINTF(READDIR, ("copying `%*.*s`\n", name_len,
    577 				name_len, dirent.d_name));
    578 
    579 			/*
    580 			 * If there isn't enough space in the uio to return a
    581 			 * whole dirent, break off read
    582 			 */
    583 			if (uio->uio_resid < _DIRENT_SIZE(&dirent))
    584 				break;
    585 
    586 			/* transfer */
    587 			if (name_len)
    588 				uiomove(&dirent, _DIRENT_SIZE(&dirent), uio);
    589 
    590 			/* advance */
    591 			diroffset += nilfs_rw16(ndirent->rec_len);
    592 			blkoff    += nilfs_rw16(ndirent->rec_len);
    593 
    594 			/* remember the last entry we transfered */
    595 			transoffset = diroffset;
    596 		}
    597 		brelse(bp, BC_AGE);
    598 
    599 		/* pass on last transfered offset */
    600 		uio->uio_offset = transoffset;
    601 	}
    602 
    603 	if (ap->a_eofflag)
    604 		*ap->a_eofflag = (uio->uio_offset >= file_size);
    605 
    606 	return error;
    607 }
    608 
    609 /* --------------------------------------------------------------------- */
    610 
    611 int
    612 nilfs_lookup(void *v)
    613 {
    614 	struct vop_lookup_args /* {
    615 		struct vnode *a_dvp;
    616 		struct vnode **a_vpp;
    617 		struct componentname *a_cnp;
    618 	} */ *ap = v;
    619 	struct vnode *dvp = ap->a_dvp;
    620 	struct vnode **vpp = ap->a_vpp;
    621 	struct componentname *cnp = ap->a_cnp;
    622 	struct nilfs_node  *dir_node, *res_node;
    623 	struct nilfs_mount *ump;
    624 	uint64_t ino;
    625 	const char *name;
    626 	int namelen, nameiop, islastcn, mounted_ro;
    627 	int vnodetp;
    628 	int error, found;
    629 
    630 	dir_node = VTOI(dvp);
    631 	ump = dir_node->ump;
    632 	*vpp = NULL;
    633 
    634 	DPRINTF(LOOKUP, ("nilfs_lookup called\n"));
    635 
    636 	/* simplify/clarification flags */
    637 	nameiop     = cnp->cn_nameiop;
    638 	islastcn    = cnp->cn_flags & ISLASTCN;
    639 	mounted_ro  = dvp->v_mount->mnt_flag & MNT_RDONLY;
    640 
    641 	/* check exec/dirread permissions first */
    642 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
    643 	if (error)
    644 		return error;
    645 
    646 	DPRINTF(LOOKUP, ("\taccess ok\n"));
    647 
    648 	/*
    649 	 * If requesting a modify on the last path element on a read-only
    650 	 * filingsystem, reject lookup; XXX why is this repeated in every FS ?
    651 	 */
    652 	if (islastcn && mounted_ro && (nameiop == DELETE || nameiop == RENAME))
    653 		return EROFS;
    654 
    655 	DPRINTF(LOOKUP, ("\tlooking up cnp->cn_nameptr '%s'\n",
    656 	    cnp->cn_nameptr));
    657 	/* look in the nami cache; returns 0 on success!! */
    658 	error = cache_lookup(dvp, vpp, cnp);
    659 	if (error >= 0)
    660 		return error;
    661 
    662 	DPRINTF(LOOKUP, ("\tNOT found in cache\n"));
    663 
    664 	/*
    665 	 * Obviously, the file is not (anymore) in the namecache, we have to
    666 	 * search for it. There are three basic cases: '.', '..' and others.
    667 	 *
    668 	 * Following the guidelines of VOP_LOOKUP manpage and tmpfs.
    669 	 */
    670 	error = 0;
    671 	if ((cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.')) {
    672 		DPRINTF(LOOKUP, ("\tlookup '.'\n"));
    673 		/* special case 1 '.' */
    674 		vref(dvp);
    675 		*vpp = dvp;
    676 		/* done */
    677 	} else if (cnp->cn_flags & ISDOTDOT) {
    678 		/* special case 2 '..' */
    679 		DPRINTF(LOOKUP, ("\tlookup '..'\n"));
    680 
    681 		/* get our node */
    682 		name    = "..";
    683 		namelen = 2;
    684 		error = nilfs_lookup_name_in_dir(dvp, name, namelen,
    685 				&ino, &found);
    686 		if (error)
    687 			goto out;
    688 		if (!found)
    689 			error = ENOENT;
    690 
    691 		/* first unlock parent */
    692 		VOP_UNLOCK(dvp);
    693 
    694 		if (error == 0) {
    695 			DPRINTF(LOOKUP, ("\tfound '..'\n"));
    696 			/* try to create/reuse the node */
    697 			error = nilfs_get_node(ump, ino, &res_node);
    698 
    699 			if (!error) {
    700 				DPRINTF(LOOKUP,
    701 					("\tnode retrieved/created OK\n"));
    702 				*vpp = res_node->vnode;
    703 			}
    704 		}
    705 
    706 		/* try to relock parent */
    707 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    708 	} else {
    709 		DPRINTF(LOOKUP, ("\tlookup file\n"));
    710 		/* all other files */
    711 		/* lookup filename in the directory returning its inode */
    712 		name    = cnp->cn_nameptr;
    713 		namelen = cnp->cn_namelen;
    714 		error = nilfs_lookup_name_in_dir(dvp, name, namelen,
    715 				&ino, &found);
    716 		if (error)
    717 			goto out;
    718 		if (!found) {
    719 			DPRINTF(LOOKUP, ("\tNOT found\n"));
    720 			/*
    721 			 * UGH, didn't find name. If we're creating or
    722 			 * renaming on the last name this is OK and we ought
    723 			 * to return EJUSTRETURN if its allowed to be created.
    724 			 */
    725 			error = ENOENT;
    726 			if (islastcn &&
    727 				(nameiop == CREATE || nameiop == RENAME))
    728 					error = 0;
    729 			if (!error) {
    730 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
    731 				if (!error) {
    732 					error = EJUSTRETURN;
    733 				}
    734 			}
    735 			/* done */
    736 		} else {
    737 			/* try to create/reuse the node */
    738 			error = nilfs_get_node(ump, ino, &res_node);
    739 			if (!error) {
    740 				/*
    741 				 * If we are not at the last path component
    742 				 * and found a non-directory or non-link entry
    743 				 * (which may itself be pointing to a
    744 				 * directory), raise an error.
    745 				 */
    746 				vnodetp = res_node->vnode->v_type;
    747 				if ((vnodetp != VDIR) && (vnodetp != VLNK)) {
    748 					if (!islastcn)
    749 						error = ENOTDIR;
    750 				}
    751 
    752 			}
    753 			if (!error) {
    754 				*vpp = res_node->vnode;
    755 			}
    756 		}
    757 	}
    758 
    759 out:
    760 	/*
    761 	 * Store result in the cache if requested. If we are creating a file,
    762 	 * the file might not be found and thus putting it into the namecache
    763 	 * might be seen as negative caching.
    764 	 */
    765 	if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
    766 		cache_enter(dvp, *vpp, cnp);
    767 
    768 	DPRINTFIF(LOOKUP, error, ("nilfs_lookup returing error %d\n", error));
    769 
    770 	return error;
    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);	/* XXX same? */
    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 = NAME_MAX;
    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 genfs_can_access(vp->v_type,
   1042 			vap->va_mode, vap->va_uid, vap->va_gid,
   1043 			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_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 	vput(dvp);
   1098 	return error;
   1099 }
   1100 
   1101 /* --------------------------------------------------------------------- */
   1102 
   1103 int
   1104 nilfs_mknod(void *v)
   1105 {
   1106 	struct vop_mknod_args /* {
   1107 		struct vnode *a_dvp;
   1108 		struct vnode **a_vpp;
   1109 		struct componentname *a_cnp;
   1110 		struct vattr *a_vap;
   1111 	} */ *ap = v;
   1112 	struct vnode  *dvp = ap->a_dvp;
   1113 	struct vnode **vpp = ap->a_vpp;
   1114 	struct vattr  *vap  = ap->a_vap;
   1115 	struct componentname *cnp = ap->a_cnp;
   1116 	int error;
   1117 
   1118 	DPRINTF(VFSCALL, ("nilfs_mknod called\n"));
   1119 	error = nilfs_create_node(dvp, vpp, vap, cnp);
   1120 
   1121 	vput(dvp);
   1122 	return error;
   1123 }
   1124 
   1125 /* --------------------------------------------------------------------- */
   1126 
   1127 int
   1128 nilfs_mkdir(void *v)
   1129 {
   1130 	struct vop_mkdir_args /* {
   1131 		struct vnode *a_dvp;
   1132 		struct vnode **a_vpp;
   1133 		struct componentname *a_cnp;
   1134 		struct vattr *a_vap;
   1135 	} */ *ap = v;
   1136 	struct vnode  *dvp = ap->a_dvp;
   1137 	struct vnode **vpp = ap->a_vpp;
   1138 	struct vattr  *vap  = ap->a_vap;
   1139 	struct componentname *cnp = ap->a_cnp;
   1140 	int error;
   1141 
   1142 	DPRINTF(VFSCALL, ("nilfs_mkdir called\n"));
   1143 	error = nilfs_create_node(dvp, vpp, vap, cnp);
   1144 
   1145 	vput(dvp);
   1146 	return error;
   1147 }
   1148 
   1149 /* --------------------------------------------------------------------- */
   1150 
   1151 static int
   1152 nilfs_do_link(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
   1153 {
   1154 	struct nilfs_node *nilfs_node, *dir_node;
   1155 	struct vattr vap;
   1156 	int error;
   1157 
   1158 	DPRINTF(VFSCALL, ("nilfs_link called\n"));
   1159 	KASSERT(dvp != vp);
   1160 	KASSERT(vp->v_type != VDIR);
   1161 	KASSERT(dvp->v_mount == vp->v_mount);
   1162 
   1163 	/* lock node */
   1164 	error = vn_lock(vp, LK_EXCLUSIVE);
   1165 	if (error)
   1166 		return error;
   1167 
   1168 	/* get attributes */
   1169 	dir_node = VTOI(dvp);
   1170 	nilfs_node = VTOI(vp);
   1171 
   1172 	error = VOP_GETATTR(vp, &vap, FSCRED);
   1173 	if (error) {
   1174 		VOP_UNLOCK(vp);
   1175 		return error;
   1176 	}
   1177 
   1178 	/* check link count overflow */
   1179 	if (vap.va_nlink >= (1<<16)-1) {	/* uint16_t */
   1180 		VOP_UNLOCK(vp);
   1181 		return EMLINK;
   1182 	}
   1183 
   1184 	error = nilfs_dir_attach(dir_node->ump, dir_node, nilfs_node,
   1185 	    &vap, cnp);
   1186 	if (error)
   1187 		VOP_UNLOCK(vp);
   1188 	return error;
   1189 }
   1190 
   1191 int
   1192 nilfs_link(void *v)
   1193 {
   1194 	struct vop_link_args /* {
   1195 		struct vnode *a_dvp;
   1196 		struct vnode *a_vp;
   1197 		struct componentname *a_cnp;
   1198 	} */ *ap = v;
   1199 	struct vnode *dvp = ap->a_dvp;
   1200 	struct vnode *vp  = ap->a_vp;
   1201 	struct componentname *cnp = ap->a_cnp;
   1202 	int error;
   1203 
   1204 	error = nilfs_do_link(dvp, vp, cnp);
   1205 	if (error)
   1206 		VOP_ABORTOP(dvp, cnp);
   1207 
   1208 	VN_KNOTE(vp, NOTE_LINK);
   1209 	VN_KNOTE(dvp, NOTE_WRITE);
   1210 	vput(dvp);
   1211 
   1212 	return error;
   1213 }
   1214 
   1215 /* --------------------------------------------------------------------- */
   1216 
   1217 static int
   1218 nilfs_do_symlink(struct nilfs_node *nilfs_node, char *target)
   1219 {
   1220 	return EROFS;
   1221 }
   1222 
   1223 
   1224 int
   1225 nilfs_symlink(void *v)
   1226 {
   1227 	struct vop_symlink_args /* {
   1228 		struct vnode *a_dvp;
   1229 		struct vnode **a_vpp;
   1230 		struct componentname *a_cnp;
   1231 		struct vattr *a_vap;
   1232 		char *a_target;
   1233 	} */ *ap = v;
   1234 	struct vnode  *dvp = ap->a_dvp;
   1235 	struct vnode **vpp = ap->a_vpp;
   1236 	struct vattr  *vap  = ap->a_vap;
   1237 	struct componentname *cnp = ap->a_cnp;
   1238 	struct nilfs_node *dir_node;
   1239 	struct nilfs_node *nilfs_node;
   1240 	int error;
   1241 
   1242 	DPRINTF(VFSCALL, ("nilfs_symlink called\n"));
   1243 	DPRINTF(VFSCALL, ("\tlinking to `%s`\n",  ap->a_target));
   1244 	error = nilfs_create_node(dvp, vpp, vap, cnp);
   1245 	KASSERT(((error == 0) && (*vpp != NULL)) || ((error && (*vpp == NULL))));
   1246 	if (!error) {
   1247 		dir_node = VTOI(dvp);
   1248 		nilfs_node = VTOI(*vpp);
   1249 		KASSERT(nilfs_node);
   1250 		error = nilfs_do_symlink(nilfs_node, ap->a_target);
   1251 		if (error) {
   1252 			/* remove node */
   1253 			nilfs_shrink_node(nilfs_node, 0);
   1254 			nilfs_dir_detach(nilfs_node->ump, dir_node, nilfs_node, cnp);
   1255 		}
   1256 	}
   1257 	vput(dvp);
   1258 	return error;
   1259 }
   1260 
   1261 /* --------------------------------------------------------------------- */
   1262 
   1263 int
   1264 nilfs_readlink(void *v)
   1265 {
   1266 	struct vop_readlink_args /* {
   1267 		struct vnode *a_vp;
   1268 		struct uio *a_uio;
   1269 		kauth_cred_t a_cred;
   1270 	} */ *ap = v;
   1271 #if 0
   1272 	struct vnode *vp = ap->a_vp;
   1273 	struct uio *uio = ap->a_uio;
   1274 	kauth_cred_t cred = ap->a_cred;
   1275 	struct nilfs_node *nilfs_node;
   1276 	struct pathcomp pathcomp;
   1277 	struct vattr vattr;
   1278 	uint8_t *pathbuf, *targetbuf, *tmpname;
   1279 	uint8_t *pathpos, *targetpos;
   1280 	char *mntonname;
   1281 	int pathlen, targetlen, namelen, mntonnamelen, len, l_ci;
   1282 	int first, error;
   1283 #endif
   1284 	ap = ap;
   1285 
   1286 	DPRINTF(VFSCALL, ("nilfs_readlink called\n"));
   1287 
   1288 	return EROFS;
   1289 }
   1290 
   1291 /* --------------------------------------------------------------------- */
   1292 
   1293 /* note: i tried to follow the logics of the tmpfs rename code */
   1294 int
   1295 nilfs_rename(void *v)
   1296 {
   1297 	struct vop_rename_args /* {
   1298 		struct vnode *a_fdvp;
   1299 		struct vnode *a_fvp;
   1300 		struct componentname *a_fcnp;
   1301 		struct vnode *a_tdvp;
   1302 		struct vnode *a_tvp;
   1303 		struct componentname *a_tcnp;
   1304 	} */ *ap = v;
   1305 	struct vnode *tvp = ap->a_tvp;
   1306 	struct vnode *tdvp = ap->a_tdvp;
   1307 	struct vnode *fvp = ap->a_fvp;
   1308 	struct vnode *fdvp = ap->a_fdvp;
   1309 	struct componentname *tcnp = ap->a_tcnp;
   1310 	struct componentname *fcnp = ap->a_fcnp;
   1311 	struct nilfs_node *fnode, *fdnode, *tnode, *tdnode;
   1312 	struct vattr fvap, tvap;
   1313 	int error;
   1314 
   1315 	DPRINTF(VFSCALL, ("nilfs_rename called\n"));
   1316 
   1317 	/* disallow cross-device renames */
   1318 	if (fvp->v_mount != tdvp->v_mount ||
   1319 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
   1320 		error = EXDEV;
   1321 		goto out_unlocked;
   1322 	}
   1323 
   1324 	fnode  = VTOI(fvp);
   1325 	fdnode = VTOI(fdvp);
   1326 	tnode  = (tvp == NULL) ? NULL : VTOI(tvp);
   1327 	tdnode = VTOI(tdvp);
   1328 
   1329 	/* lock our source dir */
   1330 	if (fdnode != tdnode) {
   1331 		error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
   1332 		if (error != 0)
   1333 			goto out_unlocked;
   1334 	}
   1335 
   1336 	/* get info about the node to be moved */
   1337 	error = VOP_GETATTR(fvp, &fvap, FSCRED);
   1338 	KASSERT(error == 0);
   1339 
   1340 	/* check when to delete the old already existing entry */
   1341 	if (tvp) {
   1342 		/* get info about the node to be moved to */
   1343 		error = VOP_GETATTR(fvp, &tvap, FSCRED);
   1344 		KASSERT(error == 0);
   1345 
   1346 		/* if both dirs, make sure the destination is empty */
   1347 		if (fvp->v_type == VDIR && tvp->v_type == VDIR) {
   1348 			if (tvap.va_nlink > 2) {
   1349 				error = ENOTEMPTY;
   1350 				goto out;
   1351 			}
   1352 		}
   1353 		/* if moving dir, make sure destination is dir too */
   1354 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   1355 			error = ENOTDIR;
   1356 			goto out;
   1357 		}
   1358 		/* if we're moving a non-directory, make sure dest is no dir */
   1359 		if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   1360 			error = EISDIR;
   1361 			goto out;
   1362 		}
   1363 	}
   1364 
   1365 	/* dont allow renaming directories acros directory for now */
   1366 	if (fdnode != tdnode) {
   1367 		if (fvp->v_type == VDIR) {
   1368 			error = EINVAL;
   1369 			goto out;
   1370 		}
   1371 	}
   1372 
   1373 	/* remove existing entry if present */
   1374 	if (tvp)
   1375 		nilfs_dir_detach(tdnode->ump, tdnode, tnode, tcnp);
   1376 
   1377 	/* create new directory entry for the node */
   1378 	error = nilfs_dir_attach(tdnode->ump, tdnode, fnode, &fvap, tcnp);
   1379 	if (error)
   1380 		goto out;
   1381 
   1382 	/* unlink old directory entry for the node, if failing, unattach new */
   1383 	error = nilfs_dir_detach(tdnode->ump, fdnode, fnode, fcnp);
   1384 	if (error)
   1385 		nilfs_dir_detach(tdnode->ump, tdnode, fnode, tcnp);
   1386 
   1387 out:
   1388         if (fdnode != tdnode)
   1389                 VOP_UNLOCK(fdvp);
   1390 
   1391 out_unlocked:
   1392 	VOP_ABORTOP(tdvp, tcnp);
   1393 	if (tdvp == tvp)
   1394 		vrele(tdvp);
   1395 	else
   1396 		vput(tdvp);
   1397 	if (tvp)
   1398 		vput(tvp);
   1399 	VOP_ABORTOP(fdvp, fcnp);
   1400 
   1401 	/* release source nodes. */
   1402 	vrele(fdvp);
   1403 	vrele(fvp);
   1404 
   1405 	return error;
   1406 }
   1407 
   1408 /* --------------------------------------------------------------------- */
   1409 
   1410 int
   1411 nilfs_remove(void *v)
   1412 {
   1413 	struct vop_remove_args /* {
   1414 		struct vnode *a_dvp;
   1415 		struct vnode *a_vp;
   1416 		struct componentname *a_cnp;
   1417 	} */ *ap = v;
   1418 	struct vnode *dvp = ap->a_dvp;
   1419 	struct vnode *vp  = ap->a_vp;
   1420 	struct componentname *cnp = ap->a_cnp;
   1421 	struct nilfs_node *dir_node = VTOI(dvp);
   1422 	struct nilfs_node *nilfs_node = VTOI(vp);
   1423 	struct nilfs_mount *ump = dir_node->ump;
   1424 	int error;
   1425 
   1426 	DPRINTF(VFSCALL, ("nilfs_remove called\n"));
   1427 	if (vp->v_type != VDIR) {
   1428 		error = nilfs_dir_detach(ump, dir_node, nilfs_node, cnp);
   1429 		DPRINTFIF(NODE, error, ("\tgot error removing file\n"));
   1430 	} else {
   1431 		DPRINTF(NODE, ("\tis a directory: perm. denied\n"));
   1432 		error = EPERM;
   1433 	}
   1434 
   1435 	if (error == 0) {
   1436 		VN_KNOTE(vp, NOTE_DELETE);
   1437 		VN_KNOTE(dvp, NOTE_WRITE);
   1438 	}
   1439 
   1440 	if (dvp == vp)
   1441 		vrele(vp);
   1442 	else
   1443 		vput(vp);
   1444 	vput(dvp);
   1445 
   1446 	return error;
   1447 }
   1448 
   1449 /* --------------------------------------------------------------------- */
   1450 
   1451 int
   1452 nilfs_rmdir(void *v)
   1453 {
   1454 	struct vop_rmdir_args /* {
   1455 		struct vnode *a_dvp;
   1456 		struct vnode *a_vp;
   1457 		struct componentname *a_cnp;
   1458 	} */ *ap = v;
   1459 	struct vnode *vp = ap->a_vp;
   1460 	struct vnode *dvp = ap->a_dvp;
   1461 	struct componentname *cnp = ap->a_cnp;
   1462 	struct nilfs_node *dir_node = VTOI(dvp);
   1463 	struct nilfs_node *nilfs_node = VTOI(vp);
   1464 	struct nilfs_mount *ump = dir_node->ump;
   1465 	int refcnt, error;
   1466 
   1467 	DPRINTF(NOTIMPL, ("nilfs_rmdir called\n"));
   1468 
   1469 	/* don't allow '.' to be deleted */
   1470 	if (dir_node == nilfs_node) {
   1471 		vrele(dvp);
   1472 		vput(vp);
   1473 		return EINVAL;
   1474 	}
   1475 
   1476 	/* check to see if the directory is empty */
   1477 	error = 0;
   1478 	refcnt = 2; /* XXX */
   1479 	if (refcnt > 1) {
   1480 		/* NOT empty */
   1481 		vput(dvp);
   1482 		vput(vp);
   1483 		return ENOTEMPTY;
   1484 	}
   1485 
   1486 	/* detach the node from the directory */
   1487 	error = nilfs_dir_detach(ump, dir_node, nilfs_node, cnp);
   1488 	if (error == 0) {
   1489 		cache_purge(vp);
   1490 //		cache_purge(dvp);	/* XXX from msdosfs, why? */
   1491 		VN_KNOTE(vp, NOTE_DELETE);
   1492 	}
   1493 	DPRINTFIF(NODE, error, ("\tgot error removing file\n"));
   1494 
   1495 	/* unput the nodes and exit */
   1496 	vput(dvp);
   1497 	vput(vp);
   1498 
   1499 	return error;
   1500 }
   1501 
   1502 /* --------------------------------------------------------------------- */
   1503 
   1504 int
   1505 nilfs_fsync(void *v)
   1506 {
   1507 	struct vop_fsync_args /* {
   1508 		struct vnode *a_vp;
   1509 		kauth_cred_t a_cred;
   1510 		int a_flags;
   1511 		off_t offlo;
   1512 		off_t offhi;
   1513 		struct proc *a_p;
   1514 	} */ *ap = v;
   1515 	struct vnode *vp = ap->a_vp;
   1516 //	struct nilfs_node *nilfs_node = VTOI(vp);
   1517 //	int error, flags, wait;
   1518 
   1519 	DPRINTF(STRATEGY, ("nilfs_fsync called : %s, %s\n",
   1520 		(ap->a_flags & FSYNC_WAIT)     ? "wait":"no wait",
   1521 		(ap->a_flags & FSYNC_DATAONLY) ? "data_only":"complete"));
   1522 
   1523 	vp = vp;
   1524 	return 0;
   1525 }
   1526 
   1527 /* --------------------------------------------------------------------- */
   1528 
   1529 int
   1530 nilfs_advlock(void *v)
   1531 {
   1532 	struct vop_advlock_args /* {
   1533 		struct vnode *a_vp;
   1534 		void *a_id;
   1535 		int a_op;
   1536 		struct flock *a_fl;
   1537 		int a_flags;
   1538 	} */ *ap = v;
   1539 	struct vnode *vp = ap->a_vp;
   1540 	struct nilfs_node *nilfs_node = VTOI(vp);
   1541 	uint64_t file_size;
   1542 
   1543 	DPRINTF(LOCKING, ("nilfs_advlock called\n"));
   1544 
   1545 	assert(nilfs_node);
   1546 	file_size = nilfs_rw64(nilfs_node->inode.i_size);
   1547 
   1548 	return lf_advlock(ap, &nilfs_node->lockf, file_size);
   1549 }
   1550 
   1551 /* --------------------------------------------------------------------- */
   1552 
   1553 
   1554 /* Global vfs vnode data structures for nilfss */
   1555 int (**nilfs_vnodeop_p) __P((void *));
   1556 
   1557 const struct vnodeopv_entry_desc nilfs_vnodeop_entries[] = {
   1558 	{ &vop_default_desc, vn_default_error },
   1559 	{ &vop_lookup_desc, nilfs_lookup },	/* lookup */
   1560 	{ &vop_create_desc, nilfs_create },	/* create */
   1561 	{ &vop_mknod_desc, nilfs_mknod },	/* mknod */	/* TODO */
   1562 	{ &vop_open_desc, nilfs_open },		/* open */
   1563 	{ &vop_close_desc, nilfs_close },	/* close */
   1564 	{ &vop_access_desc, nilfs_access },	/* access */
   1565 	{ &vop_getattr_desc, nilfs_getattr },	/* getattr */
   1566 	{ &vop_setattr_desc, nilfs_setattr },	/* setattr */	/* TODO chflags */
   1567 	{ &vop_read_desc, nilfs_read },		/* read */
   1568 	{ &vop_write_desc, nilfs_write },	/* write */	/* WRITE */
   1569 	{ &vop_fcntl_desc, genfs_fcntl },	/* fcntl */	/* TODO? */
   1570 	{ &vop_ioctl_desc, genfs_enoioctl },	/* ioctl */	/* TODO? */
   1571 	{ &vop_poll_desc, genfs_poll },		/* poll */	/* TODO/OK? */
   1572 	{ &vop_kqfilter_desc, genfs_kqfilter },	/* kqfilter */	/* ? */
   1573 	{ &vop_revoke_desc, genfs_revoke },	/* revoke */	/* TODO? */
   1574 	{ &vop_mmap_desc, genfs_mmap },		/* mmap */	/* OK? */
   1575 	{ &vop_fsync_desc, nilfs_fsync },	/* fsync */
   1576 	{ &vop_seek_desc, genfs_seek },		/* seek */
   1577 	{ &vop_remove_desc, nilfs_remove },	/* remove */
   1578 	{ &vop_link_desc, nilfs_link },		/* link */	/* TODO */
   1579 	{ &vop_rename_desc, nilfs_rename },	/* rename */ 	/* TODO */
   1580 	{ &vop_mkdir_desc, nilfs_mkdir },	/* mkdir */
   1581 	{ &vop_rmdir_desc, nilfs_rmdir },	/* rmdir */
   1582 	{ &vop_symlink_desc, nilfs_symlink },	/* symlink */	/* TODO */
   1583 	{ &vop_readdir_desc, nilfs_readdir },	/* readdir */
   1584 	{ &vop_readlink_desc, nilfs_readlink },	/* readlink */	/* TEST ME */
   1585 	{ &vop_abortop_desc, genfs_abortop },	/* abortop */	/* TODO/OK? */
   1586 	{ &vop_inactive_desc, nilfs_inactive },	/* inactive */
   1587 	{ &vop_reclaim_desc, nilfs_reclaim },	/* reclaim */
   1588 	{ &vop_lock_desc, genfs_lock },		/* lock */
   1589 	{ &vop_unlock_desc, genfs_unlock },	/* unlock */
   1590 	{ &vop_bmap_desc, nilfs_trivial_bmap },	/* bmap */	/* 1:1 bmap */
   1591 	{ &vop_strategy_desc, nilfs_vfsstrategy },/* strategy */
   1592 /*	{ &vop_print_desc, nilfs_print },	*/	/* print */
   1593 	{ &vop_islocked_desc, genfs_islocked },	/* islocked */
   1594 	{ &vop_pathconf_desc, nilfs_pathconf },	/* pathconf */
   1595 	{ &vop_advlock_desc, nilfs_advlock },	/* advlock */	/* TEST ME */
   1596 	{ &vop_bwrite_desc, vn_bwrite },	/* bwrite */	/* ->strategy */
   1597 	{ &vop_getpages_desc, genfs_getpages },	/* getpages */
   1598 	{ &vop_putpages_desc, genfs_putpages },	/* putpages */
   1599 	{ NULL, NULL }
   1600 };
   1601 
   1602 
   1603 const struct vnodeopv_desc nilfs_vnodeop_opv_desc = {
   1604 	&nilfs_vnodeop_p, nilfs_vnodeop_entries
   1605 };
   1606 
   1607