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