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