Home | History | Annotate | Line # | Download | only in nilfs
nilfs_vnops.c revision 1.4.4.3
      1 /* $NetBSD: nilfs_vnops.c,v 1.4.4.3 2011/03/05 20:55:06 rmind Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2008, 2009 Reinoud Zandijk
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  *
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #ifndef lint
     31 __KERNEL_RCSID(0, "$NetBSD: nilfs_vnops.c,v 1.4.4.3 2011/03/05 20:55:06 rmind Exp $");
     32 #endif /* not lint */
     33 
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/namei.h>
     38 #include <sys/resourcevar.h>	/* defines plimit structure in proc struct */
     39 #include <sys/kernel.h>
     40 #include <sys/file.h>		/* define FWRITE ... */
     41 #include <sys/stat.h>
     42 #include <sys/buf.h>
     43 #include <sys/proc.h>
     44 #include <sys/mount.h>
     45 #include <sys/vnode.h>
     46 #include <sys/signalvar.h>
     47 #include <sys/malloc.h>
     48 #include <sys/dirent.h>
     49 #include <sys/lockf.h>
     50 #include <sys/kauth.h>
     51 
     52 #include <miscfs/genfs/genfs.h>
     53 #include <uvm/uvm_extern.h>
     54 
     55 #include <fs/nilfs/nilfs_mount.h>
     56 #include "nilfs.h"
     57 #include "nilfs_subr.h"
     58 #include "nilfs_bswap.h"
     59 
     60 
     61 #define VTOI(vnode) ((struct nilfs_node *) (vnode)->v_data)
     62 
     63 
     64 /* externs */
     65 extern int prtactive;
     66 
     67 /* implementations of vnode functions; table follows at end */
     68 /* --------------------------------------------------------------------- */
     69 
     70 int
     71 nilfs_inactive(void *v)
     72 {
     73 	struct vop_inactive_args /* {
     74 		struct vnode *a_vp;
     75 		bool         *a_recycle;
     76 	} */ *ap = v;
     77 	struct vnode *vp = ap->a_vp;
     78 	struct nilfs_node *nilfs_node = VTOI(vp);
     79 
     80 	DPRINTF(NODE, ("nilfs_inactive called for nilfs_node %p\n", VTOI(vp)));
     81 
     82 	if (nilfs_node == NULL) {
     83 		DPRINTF(NODE, ("nilfs_inactive: inactive NULL NILFS node\n"));
     84 		VOP_UNLOCK(vp);
     85 		return 0;
     86 	}
     87 
     88 	/*
     89 	 * Optionally flush metadata to disc. If the file has not been
     90 	 * referenced anymore in a directory we ought to free up the resources
     91 	 * on disc if applicable.
     92 	 */
     93 	VOP_UNLOCK(vp);
     94 
     95 	return 0;
     96 }
     97 
     98 /* --------------------------------------------------------------------- */
     99 
    100 int
    101 nilfs_reclaim(void *v)
    102 {
    103 	struct vop_reclaim_args /* {
    104 		struct vnode *a_vp;
    105 	} */ *ap = v;
    106 	struct vnode *vp = ap->a_vp;
    107 	struct nilfs_node *nilfs_node = VTOI(vp);
    108 
    109 	DPRINTF(NODE, ("nilfs_reclaim called for node %p\n", nilfs_node));
    110 	if (prtactive && vp->v_usecount > 1)
    111 		vprint("nilfs_reclaim(): pushing active", vp);
    112 
    113 	if (nilfs_node == NULL) {
    114 		DPRINTF(NODE, ("nilfs_reclaim(): null nilfsnode\n"));
    115 		return 0;
    116 	}
    117 
    118 	/* update note for closure */
    119 	nilfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
    120 
    121 	/* 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 					error = EJUSTRETURN;
    736 				}
    737 			}
    738 			/* done */
    739 		} else {
    740 			/* try to create/reuse the node */
    741 			error = nilfs_get_node(ump, ino, &res_node);
    742 			if (!error) {
    743 				/*
    744 				 * If we are not at the last path component
    745 				 * and found a non-directory or non-link entry
    746 				 * (which may itself be pointing to a
    747 				 * directory), raise an error.
    748 				 */
    749 				vnodetp = res_node->vnode->v_type;
    750 				if ((vnodetp != VDIR) && (vnodetp != VLNK)) {
    751 					if (!islastcn)
    752 						error = ENOTDIR;
    753 				}
    754 
    755 			}
    756 			if (!error) {
    757 				*vpp = res_node->vnode;
    758 			}
    759 		}
    760 	}
    761 
    762 out:
    763 	/*
    764 	 * Store result in the cache if requested. If we are creating a file,
    765 	 * the file might not be found and thus putting it into the namecache
    766 	 * might be seen as negative caching.
    767 	 */
    768 	if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
    769 		cache_enter(dvp, *vpp, cnp);
    770 
    771 	DPRINTFIF(LOOKUP, error, ("nilfs_lookup returing error %d\n", error));
    772 
    773 	return error;
    774 }
    775 
    776 /* --------------------------------------------------------------------- */
    777 
    778 static void
    779 nilfs_ctime_to_timespec(struct timespec *ts, uint64_t ctime)
    780 {
    781 	ts->tv_sec  = ctime;
    782 	ts->tv_nsec = 0;
    783 }
    784 
    785 
    786 int
    787 nilfs_getattr(void *v)
    788 {
    789 	struct vop_getattr_args /* {
    790 		struct vnode *a_vp;
    791 		struct vattr *a_vap;
    792 		kauth_cred_t a_cred;
    793 		struct lwp   *a_l;
    794 	} */ *ap = v;
    795 	struct vnode       *vp  = ap->a_vp;
    796 	struct vattr       *vap = ap->a_vap;
    797 	struct nilfs_node  *node = VTOI(vp);
    798 	struct nilfs_inode *inode = &node->inode;
    799 
    800 	DPRINTF(VFSCALL, ("nilfs_getattr called\n"));
    801 
    802 	/* basic info */
    803 	vattr_null(vap);
    804 	vap->va_type      = vp->v_type;
    805 	vap->va_mode      = nilfs_rw16(inode->i_mode);	/* XXX same? */
    806 	vap->va_nlink     = nilfs_rw16(inode->i_links_count);
    807 	vap->va_uid       = nilfs_rw32(inode->i_uid);
    808 	vap->va_gid       = nilfs_rw32(inode->i_gid);
    809 	vap->va_fsid      = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    810 	vap->va_fileid    = node->ino;
    811 	vap->va_size      = nilfs_rw64(inode->i_size);
    812 	vap->va_blocksize = node->nilfsdev->blocksize;
    813 
    814 	/* times */
    815 	nilfs_ctime_to_timespec(&vap->va_atime, nilfs_rw64(inode->i_mtime));
    816 	nilfs_ctime_to_timespec(&vap->va_mtime, nilfs_rw64(inode->i_mtime));
    817 	nilfs_ctime_to_timespec(&vap->va_ctime, nilfs_rw64(inode->i_ctime));
    818 	nilfs_ctime_to_timespec(&vap->va_birthtime, nilfs_rw64(inode->i_ctime));
    819 
    820 	vap->va_gen       = nilfs_rw32(inode->i_generation);
    821 	vap->va_flags     = 0;	/* vattr flags */
    822 	vap->va_bytes     = nilfs_rw64(inode->i_blocks) * vap->va_blocksize;
    823 	vap->va_filerev   = vap->va_gen;  /* XXX file revision? same as gen? */
    824 	vap->va_vaflags   = 0;  /* XXX chflags flags */
    825 
    826 	return 0;
    827 }
    828 
    829 /* --------------------------------------------------------------------- */
    830 
    831 #if 0
    832 static int
    833 nilfs_chown(struct vnode *vp, uid_t new_uid, gid_t new_gid,
    834 	  kauth_cred_t cred)
    835 {
    836 	return EINVAL;
    837 }
    838 
    839 
    840 static int
    841 nilfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred)
    842 {
    843 
    844 	return EINVAL;
    845 }
    846 
    847 
    848 /* exported */
    849 int
    850 nilfs_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred)
    851 {
    852 	return EINVAL;
    853 }
    854 
    855 
    856 static int
    857 nilfs_chflags(struct vnode *vp, mode_t mode, kauth_cred_t cred)
    858 {
    859 	return EINVAL;
    860 }
    861 
    862 
    863 static int
    864 nilfs_chtimes(struct vnode *vp,
    865 	struct timespec *atime, struct timespec *mtime,
    866 	struct timespec *birthtime, int setattrflags,
    867 	kauth_cred_t cred)
    868 {
    869 	return EINVAL;
    870 }
    871 #endif
    872 
    873 
    874 int
    875 nilfs_setattr(void *v)
    876 {
    877 	struct vop_setattr_args /* {
    878 		struct vnode *a_vp;
    879 		struct vattr *a_vap;
    880 		kauth_cred_t a_cred;
    881 		struct lwp   *a_l;
    882 	} */ *ap = v;
    883 	struct vnode *vp = ap->a_vp;
    884 
    885 	vp = vp;
    886 	DPRINTF(VFSCALL, ("nilfs_setattr called\n"));
    887 	return EINVAL;
    888 }
    889 
    890 /* --------------------------------------------------------------------- */
    891 
    892 /*
    893  * Return POSIX pathconf information for NILFS file systems.
    894  */
    895 int
    896 nilfs_pathconf(void *v)
    897 {
    898 	struct vop_pathconf_args /* {
    899 		struct vnode *a_vp;
    900 		int a_name;
    901 		register_t *a_retval;
    902 	} */ *ap = v;
    903 	uint32_t bits;
    904 
    905 	DPRINTF(VFSCALL, ("nilfs_pathconf called\n"));
    906 
    907 	switch (ap->a_name) {
    908 	case _PC_LINK_MAX:
    909 		*ap->a_retval = (1<<16)-1;	/* 16 bits */
    910 		return 0;
    911 	case _PC_NAME_MAX:
    912 		*ap->a_retval = NAME_MAX;
    913 		return 0;
    914 	case _PC_PATH_MAX:
    915 		*ap->a_retval = PATH_MAX;
    916 		return 0;
    917 	case _PC_PIPE_BUF:
    918 		*ap->a_retval = PIPE_BUF;
    919 		return 0;
    920 	case _PC_CHOWN_RESTRICTED:
    921 		*ap->a_retval = 1;
    922 		return 0;
    923 	case _PC_NO_TRUNC:
    924 		*ap->a_retval = 1;
    925 		return 0;
    926 	case _PC_SYNC_IO:
    927 		*ap->a_retval = 0;     /* synchronised is off for performance */
    928 		return 0;
    929 	case _PC_FILESIZEBITS:
    930 		/* 64 bit file offsets -> 2+floor(2log(2^64-1)) = 2 + 63 = 65 */
    931 		bits = 64; /* XXX ought to deliver 65 */
    932 #if 0
    933 		if (nilfs_node)
    934 			bits = 64 * vp->v_mount->mnt_dev_bshift;
    935 #endif
    936 		*ap->a_retval = bits;
    937 		return 0;
    938 	}
    939 
    940 	return EINVAL;
    941 }
    942 
    943 
    944 /* --------------------------------------------------------------------- */
    945 
    946 int
    947 nilfs_open(void *v)
    948 {
    949 	struct vop_open_args /* {
    950 		struct vnode *a_vp;
    951 		int a_mode;
    952 		kauth_cred_t a_cred;
    953 		struct proc *a_p;
    954 	} */ *ap = v;
    955 	int flags;
    956 
    957 	DPRINTF(VFSCALL, ("nilfs_open called\n"));
    958 
    959 	/*
    960 	 * Files marked append-only must be opened for appending.
    961 	 */
    962 	flags = 0;
    963 	if ((flags & APPEND) && (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
    964 		return (EPERM);
    965 
    966 	return 0;
    967 }
    968 
    969 
    970 /* --------------------------------------------------------------------- */
    971 
    972 int
    973 nilfs_close(void *v)
    974 {
    975 	struct vop_close_args /* {
    976 		struct vnode *a_vp;
    977 		int a_fflag;
    978 		kauth_cred_t a_cred;
    979 		struct proc *a_p;
    980 	} */ *ap = v;
    981 	struct vnode *vp = ap->a_vp;
    982 	struct nilfs_node *nilfs_node = VTOI(vp);
    983 
    984 	DPRINTF(VFSCALL, ("nilfs_close called\n"));
    985 	nilfs_node = nilfs_node;	/* shut up gcc */
    986 
    987 	mutex_enter(vp->v_interlock);
    988 		if (vp->v_usecount > 1)
    989 			nilfs_itimes(nilfs_node, NULL, NULL, NULL);
    990 	mutex_exit(vp->v_interlock);
    991 
    992 	return 0;
    993 }
    994 
    995 
    996 /* --------------------------------------------------------------------- */
    997 
    998 static int
    999 nilfs_check_possible(struct vnode *vp, struct vattr *vap, mode_t mode)
   1000 {
   1001 	int flags;
   1002 
   1003 	/* check if we are allowed to write */
   1004 	switch (vap->va_type) {
   1005 	case VDIR:
   1006 	case VLNK:
   1007 	case VREG:
   1008 		/*
   1009 		 * normal nodes: check if we're on a read-only mounted
   1010 		 * filingsystem and bomb out if we're trying to write.
   1011 		 */
   1012 		if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY))
   1013 			return EROFS;
   1014 		break;
   1015 	case VBLK:
   1016 	case VCHR:
   1017 	case VSOCK:
   1018 	case VFIFO:
   1019 		/*
   1020 		 * special nodes: even on read-only mounted filingsystems
   1021 		 * these are allowed to be written to if permissions allow.
   1022 		 */
   1023 		break;
   1024 	default:
   1025 		/* no idea what this is */
   1026 		return EINVAL;
   1027 	}
   1028 
   1029 	/* noone may write immutable files */
   1030 	/* TODO: get chflags(2) flags */
   1031 	flags = 0;
   1032 	if ((mode & VWRITE) && (flags & IMMUTABLE))
   1033 		return EPERM;
   1034 
   1035 	return 0;
   1036 }
   1037 
   1038 static int
   1039 nilfs_check_permitted(struct vnode *vp, struct vattr *vap, mode_t mode,
   1040     kauth_cred_t cred)
   1041 {
   1042 
   1043 	/* ask the generic genfs_can_access to advice on security */
   1044 	return genfs_can_access(vp->v_type,
   1045 			vap->va_mode, vap->va_uid, vap->va_gid,
   1046 			mode, cred);
   1047 }
   1048 
   1049 int
   1050 nilfs_access(void *v)
   1051 {
   1052 	struct vop_access_args /* {
   1053 		struct vnode *a_vp;
   1054 		int a_mode;
   1055 		kauth_cred_t a_cred;
   1056 		struct proc *a_p;
   1057 	} */ *ap = v;
   1058 	struct vnode    *vp   = ap->a_vp;
   1059 	mode_t	         mode = ap->a_mode;
   1060 	kauth_cred_t     cred = ap->a_cred;
   1061 	/* struct nilfs_node *nilfs_node = VTOI(vp); */
   1062 	struct vattr vap;
   1063 	int error;
   1064 
   1065 	DPRINTF(VFSCALL, ("nilfs_access called\n"));
   1066 
   1067 	error = VOP_GETATTR(vp, &vap, NULL);
   1068 	if (error)
   1069 		return error;
   1070 
   1071 	error = nilfs_check_possible(vp, &vap, mode);
   1072 	if (error)
   1073 		return error;
   1074 
   1075 	error = nilfs_check_permitted(vp, &vap, mode, cred);
   1076 
   1077 	return error;
   1078 }
   1079 
   1080 /* --------------------------------------------------------------------- */
   1081 
   1082 int
   1083 nilfs_create(void *v)
   1084 {
   1085 	struct vop_create_args /* {
   1086 		struct vnode *a_dvp;
   1087 		struct vnode **a_vpp;
   1088 		struct componentname *a_cnp;
   1089 		struct vattr *a_vap;
   1090 	} */ *ap = v;
   1091 	struct vnode  *dvp = ap->a_dvp;
   1092 	struct vnode **vpp = ap->a_vpp;
   1093 	struct vattr  *vap  = ap->a_vap;
   1094 	struct componentname *cnp = ap->a_cnp;
   1095 	int error;
   1096 
   1097 	DPRINTF(VFSCALL, ("nilfs_create called\n"));
   1098 	error = nilfs_create_node(dvp, vpp, vap, cnp);
   1099 
   1100 	vput(dvp);
   1101 	return error;
   1102 }
   1103 
   1104 /* --------------------------------------------------------------------- */
   1105 
   1106 int
   1107 nilfs_mknod(void *v)
   1108 {
   1109 	struct vop_mknod_args /* {
   1110 		struct vnode *a_dvp;
   1111 		struct vnode **a_vpp;
   1112 		struct componentname *a_cnp;
   1113 		struct vattr *a_vap;
   1114 	} */ *ap = v;
   1115 	struct vnode  *dvp = ap->a_dvp;
   1116 	struct vnode **vpp = ap->a_vpp;
   1117 	struct vattr  *vap  = ap->a_vap;
   1118 	struct componentname *cnp = ap->a_cnp;
   1119 	int error;
   1120 
   1121 	DPRINTF(VFSCALL, ("nilfs_mknod called\n"));
   1122 	error = nilfs_create_node(dvp, vpp, vap, cnp);
   1123 
   1124 	vput(dvp);
   1125 	return error;
   1126 }
   1127 
   1128 /* --------------------------------------------------------------------- */
   1129 
   1130 int
   1131 nilfs_mkdir(void *v)
   1132 {
   1133 	struct vop_mkdir_args /* {
   1134 		struct vnode *a_dvp;
   1135 		struct vnode **a_vpp;
   1136 		struct componentname *a_cnp;
   1137 		struct vattr *a_vap;
   1138 	} */ *ap = v;
   1139 	struct vnode  *dvp = ap->a_dvp;
   1140 	struct vnode **vpp = ap->a_vpp;
   1141 	struct vattr  *vap  = ap->a_vap;
   1142 	struct componentname *cnp = ap->a_cnp;
   1143 	int error;
   1144 
   1145 	DPRINTF(VFSCALL, ("nilfs_mkdir called\n"));
   1146 	error = nilfs_create_node(dvp, vpp, vap, cnp);
   1147 
   1148 	vput(dvp);
   1149 	return error;
   1150 }
   1151 
   1152 /* --------------------------------------------------------------------- */
   1153 
   1154 static int
   1155 nilfs_do_link(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
   1156 {
   1157 	struct nilfs_node *nilfs_node, *dir_node;
   1158 	struct vattr vap;
   1159 	int error;
   1160 
   1161 	DPRINTF(VFSCALL, ("nilfs_link called\n"));
   1162 	error = 0;
   1163 
   1164 	/* some quick checks */
   1165 	if (vp->v_type == VDIR)
   1166 		return EPERM;		/* can't link a directory */
   1167 	if (dvp->v_mount != vp->v_mount)
   1168 		return EXDEV;		/* can't link across devices */
   1169 	if (dvp == vp)
   1170 		return EPERM;		/* can't be the same */
   1171 
   1172 	/* lock node */
   1173 	error = vn_lock(vp, LK_EXCLUSIVE);
   1174 	if (error)
   1175 		return error;
   1176 
   1177 	/* get attributes */
   1178 	dir_node = VTOI(dvp);
   1179 	nilfs_node = VTOI(vp);
   1180 
   1181 	error = VOP_GETATTR(vp, &vap, FSCRED);
   1182 	if (error) {
   1183 		VOP_UNLOCK(vp);
   1184 		return error;
   1185 	}
   1186 
   1187 	/* check link count overflow */
   1188 	if (vap.va_nlink >= (1<<16)-1) {	/* uint16_t */
   1189 		VOP_UNLOCK(vp);
   1190 		return EMLINK;
   1191 	}
   1192 
   1193 	error = nilfs_dir_attach(dir_node->ump, dir_node, nilfs_node,
   1194 	    &vap, cnp);
   1195 	if (error)
   1196 		VOP_UNLOCK(vp);
   1197 	return error;
   1198 }
   1199 
   1200 int
   1201 nilfs_link(void *v)
   1202 {
   1203 	struct vop_link_args /* {
   1204 		struct vnode *a_dvp;
   1205 		struct vnode *a_vp;
   1206 		struct componentname *a_cnp;
   1207 	} */ *ap = v;
   1208 	struct vnode *dvp = ap->a_dvp;
   1209 	struct vnode *vp  = ap->a_vp;
   1210 	struct componentname *cnp = ap->a_cnp;
   1211 	int error;
   1212 
   1213 	error = nilfs_do_link(dvp, vp, cnp);
   1214 	if (error)
   1215 		VOP_ABORTOP(dvp, cnp);
   1216 
   1217 	VN_KNOTE(vp, NOTE_LINK);
   1218 	VN_KNOTE(dvp, NOTE_WRITE);
   1219 	vput(dvp);
   1220 
   1221 	return error;
   1222 }
   1223 
   1224 /* --------------------------------------------------------------------- */
   1225 
   1226 static int
   1227 nilfs_do_symlink(struct nilfs_node *nilfs_node, char *target)
   1228 {
   1229 	return EROFS;
   1230 }
   1231 
   1232 
   1233 int
   1234 nilfs_symlink(void *v)
   1235 {
   1236 	struct vop_symlink_args /* {
   1237 		struct vnode *a_dvp;
   1238 		struct vnode **a_vpp;
   1239 		struct componentname *a_cnp;
   1240 		struct vattr *a_vap;
   1241 		char *a_target;
   1242 	} */ *ap = v;
   1243 	struct vnode  *dvp = ap->a_dvp;
   1244 	struct vnode **vpp = ap->a_vpp;
   1245 	struct vattr  *vap  = ap->a_vap;
   1246 	struct componentname *cnp = ap->a_cnp;
   1247 	struct nilfs_node *dir_node;
   1248 	struct nilfs_node *nilfs_node;
   1249 	int error;
   1250 
   1251 	DPRINTF(VFSCALL, ("nilfs_symlink called\n"));
   1252 	DPRINTF(VFSCALL, ("\tlinking to `%s`\n",  ap->a_target));
   1253 	error = nilfs_create_node(dvp, vpp, vap, cnp);
   1254 	KASSERT(((error == 0) && (*vpp != NULL)) || ((error && (*vpp == NULL))));
   1255 	if (!error) {
   1256 		dir_node = VTOI(dvp);
   1257 		nilfs_node = VTOI(*vpp);
   1258 		KASSERT(nilfs_node);
   1259 		error = nilfs_do_symlink(nilfs_node, ap->a_target);
   1260 		if (error) {
   1261 			/* remove node */
   1262 			nilfs_shrink_node(nilfs_node, 0);
   1263 			nilfs_dir_detach(nilfs_node->ump, dir_node, nilfs_node, cnp);
   1264 		}
   1265 	}
   1266 	vput(dvp);
   1267 	return error;
   1268 }
   1269 
   1270 /* --------------------------------------------------------------------- */
   1271 
   1272 int
   1273 nilfs_readlink(void *v)
   1274 {
   1275 	struct vop_readlink_args /* {
   1276 		struct vnode *a_vp;
   1277 		struct uio *a_uio;
   1278 		kauth_cred_t a_cred;
   1279 	} */ *ap = v;
   1280 #if 0
   1281 	struct vnode *vp = ap->a_vp;
   1282 	struct uio *uio = ap->a_uio;
   1283 	kauth_cred_t cred = ap->a_cred;
   1284 	struct nilfs_node *nilfs_node;
   1285 	struct pathcomp pathcomp;
   1286 	struct vattr vattr;
   1287 	uint8_t *pathbuf, *targetbuf, *tmpname;
   1288 	uint8_t *pathpos, *targetpos;
   1289 	char *mntonname;
   1290 	int pathlen, targetlen, namelen, mntonnamelen, len, l_ci;
   1291 	int first, error;
   1292 #endif
   1293 	ap = ap;
   1294 
   1295 	DPRINTF(VFSCALL, ("nilfs_readlink called\n"));
   1296 
   1297 	return EROFS;
   1298 }
   1299 
   1300 /* --------------------------------------------------------------------- */
   1301 
   1302 /* note: i tried to follow the logics of the tmpfs rename code */
   1303 int
   1304 nilfs_rename(void *v)
   1305 {
   1306 	struct vop_rename_args /* {
   1307 		struct vnode *a_fdvp;
   1308 		struct vnode *a_fvp;
   1309 		struct componentname *a_fcnp;
   1310 		struct vnode *a_tdvp;
   1311 		struct vnode *a_tvp;
   1312 		struct componentname *a_tcnp;
   1313 	} */ *ap = v;
   1314 	struct vnode *tvp = ap->a_tvp;
   1315 	struct vnode *tdvp = ap->a_tdvp;
   1316 	struct vnode *fvp = ap->a_fvp;
   1317 	struct vnode *fdvp = ap->a_fdvp;
   1318 	struct componentname *tcnp = ap->a_tcnp;
   1319 	struct componentname *fcnp = ap->a_fcnp;
   1320 	struct nilfs_node *fnode, *fdnode, *tnode, *tdnode;
   1321 	struct vattr fvap, tvap;
   1322 	int error;
   1323 
   1324 	DPRINTF(VFSCALL, ("nilfs_rename called\n"));
   1325 
   1326 	/* disallow cross-device renames */
   1327 	if (fvp->v_mount != tdvp->v_mount ||
   1328 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
   1329 		error = EXDEV;
   1330 		goto out_unlocked;
   1331 	}
   1332 
   1333 	fnode  = VTOI(fvp);
   1334 	fdnode = VTOI(fdvp);
   1335 	tnode  = (tvp == NULL) ? NULL : VTOI(tvp);
   1336 	tdnode = VTOI(tdvp);
   1337 
   1338 	/* lock our source dir */
   1339 	if (fdnode != tdnode) {
   1340 		error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
   1341 		if (error != 0)
   1342 			goto out_unlocked;
   1343 	}
   1344 
   1345 	/* get info about the node to be moved */
   1346 	error = VOP_GETATTR(fvp, &fvap, FSCRED);
   1347 	KASSERT(error == 0);
   1348 
   1349 	/* check when to delete the old already existing entry */
   1350 	if (tvp) {
   1351 		/* get info about the node to be moved to */
   1352 		error = VOP_GETATTR(fvp, &tvap, FSCRED);
   1353 		KASSERT(error == 0);
   1354 
   1355 		/* if both dirs, make sure the destination is empty */
   1356 		if (fvp->v_type == VDIR && tvp->v_type == VDIR) {
   1357 			if (tvap.va_nlink > 2) {
   1358 				error = ENOTEMPTY;
   1359 				goto out;
   1360 			}
   1361 		}
   1362 		/* if moving dir, make sure destination is dir too */
   1363 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   1364 			error = ENOTDIR;
   1365 			goto out;
   1366 		}
   1367 		/* if we're moving a non-directory, make sure dest is no dir */
   1368 		if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   1369 			error = EISDIR;
   1370 			goto out;
   1371 		}
   1372 	}
   1373 
   1374 	/* dont allow renaming directories acros directory for now */
   1375 	if (fdnode != tdnode) {
   1376 		if (fvp->v_type == VDIR) {
   1377 			error = EINVAL;
   1378 			goto out;
   1379 		}
   1380 	}
   1381 
   1382 	/* remove existing entry if present */
   1383 	if (tvp)
   1384 		nilfs_dir_detach(tdnode->ump, tdnode, tnode, tcnp);
   1385 
   1386 	/* create new directory entry for the node */
   1387 	error = nilfs_dir_attach(tdnode->ump, tdnode, fnode, &fvap, tcnp);
   1388 	if (error)
   1389 		goto out;
   1390 
   1391 	/* unlink old directory entry for the node, if failing, unattach new */
   1392 	error = nilfs_dir_detach(tdnode->ump, fdnode, fnode, fcnp);
   1393 	if (error)
   1394 		nilfs_dir_detach(tdnode->ump, tdnode, fnode, tcnp);
   1395 
   1396 out:
   1397         if (fdnode != tdnode)
   1398                 VOP_UNLOCK(fdvp);
   1399 
   1400 out_unlocked:
   1401 	VOP_ABORTOP(tdvp, tcnp);
   1402 	if (tdvp == tvp)
   1403 		vrele(tdvp);
   1404 	else
   1405 		vput(tdvp);
   1406 	if (tvp)
   1407 		vput(tvp);
   1408 	VOP_ABORTOP(fdvp, fcnp);
   1409 
   1410 	/* release source nodes. */
   1411 	vrele(fdvp);
   1412 	vrele(fvp);
   1413 
   1414 	return error;
   1415 }
   1416 
   1417 /* --------------------------------------------------------------------- */
   1418 
   1419 int
   1420 nilfs_remove(void *v)
   1421 {
   1422 	struct vop_remove_args /* {
   1423 		struct vnode *a_dvp;
   1424 		struct vnode *a_vp;
   1425 		struct componentname *a_cnp;
   1426 	} */ *ap = v;
   1427 	struct vnode *dvp = ap->a_dvp;
   1428 	struct vnode *vp  = ap->a_vp;
   1429 	struct componentname *cnp = ap->a_cnp;
   1430 	struct nilfs_node *dir_node = VTOI(dvp);
   1431 	struct nilfs_node *nilfs_node = VTOI(vp);
   1432 	struct nilfs_mount *ump = dir_node->ump;
   1433 	int error;
   1434 
   1435 	DPRINTF(VFSCALL, ("nilfs_remove called\n"));
   1436 	if (vp->v_type != VDIR) {
   1437 		error = nilfs_dir_detach(ump, dir_node, nilfs_node, cnp);
   1438 		DPRINTFIF(NODE, error, ("\tgot error removing file\n"));
   1439 	} else {
   1440 		DPRINTF(NODE, ("\tis a directory: perm. denied\n"));
   1441 		error = EPERM;
   1442 	}
   1443 
   1444 	if (error == 0) {
   1445 		VN_KNOTE(vp, NOTE_DELETE);
   1446 		VN_KNOTE(dvp, NOTE_WRITE);
   1447 	}
   1448 
   1449 	if (dvp == vp)
   1450 		vrele(vp);
   1451 	else
   1452 		vput(vp);
   1453 	vput(dvp);
   1454 
   1455 	return error;
   1456 }
   1457 
   1458 /* --------------------------------------------------------------------- */
   1459 
   1460 int
   1461 nilfs_rmdir(void *v)
   1462 {
   1463 	struct vop_rmdir_args /* {
   1464 		struct vnode *a_dvp;
   1465 		struct vnode *a_vp;
   1466 		struct componentname *a_cnp;
   1467 	} */ *ap = v;
   1468 	struct vnode *vp = ap->a_vp;
   1469 	struct vnode *dvp = ap->a_dvp;
   1470 	struct componentname *cnp = ap->a_cnp;
   1471 	struct nilfs_node *dir_node = VTOI(dvp);
   1472 	struct nilfs_node *nilfs_node = VTOI(vp);
   1473 	struct nilfs_mount *ump = dir_node->ump;
   1474 	int refcnt, error;
   1475 
   1476 	DPRINTF(NOTIMPL, ("nilfs_rmdir called\n"));
   1477 
   1478 	/* don't allow '.' to be deleted */
   1479 	if (dir_node == nilfs_node) {
   1480 		vrele(dvp);
   1481 		vput(vp);
   1482 		return EINVAL;
   1483 	}
   1484 
   1485 	/* check to see if the directory is empty */
   1486 	error = 0;
   1487 	refcnt = 2; /* XXX */
   1488 	if (refcnt > 1) {
   1489 		/* NOT empty */
   1490 		vput(dvp);
   1491 		vput(vp);
   1492 		return ENOTEMPTY;
   1493 	}
   1494 
   1495 	/* detach the node from the directory */
   1496 	error = nilfs_dir_detach(ump, dir_node, nilfs_node, cnp);
   1497 	if (error == 0) {
   1498 		cache_purge(vp);
   1499 //		cache_purge(dvp);	/* XXX from msdosfs, why? */
   1500 		VN_KNOTE(vp, NOTE_DELETE);
   1501 	}
   1502 	DPRINTFIF(NODE, error, ("\tgot error removing file\n"));
   1503 
   1504 	/* unput the nodes and exit */
   1505 	vput(dvp);
   1506 	vput(vp);
   1507 
   1508 	return error;
   1509 }
   1510 
   1511 /* --------------------------------------------------------------------- */
   1512 
   1513 int
   1514 nilfs_fsync(void *v)
   1515 {
   1516 	struct vop_fsync_args /* {
   1517 		struct vnode *a_vp;
   1518 		kauth_cred_t a_cred;
   1519 		int a_flags;
   1520 		off_t offlo;
   1521 		off_t offhi;
   1522 		struct proc *a_p;
   1523 	} */ *ap = v;
   1524 	struct vnode *vp = ap->a_vp;
   1525 //	struct nilfs_node *nilfs_node = VTOI(vp);
   1526 //	int error, flags, wait;
   1527 
   1528 	DPRINTF(STRATEGY, ("nilfs_fsync called : %s, %s\n",
   1529 		(ap->a_flags & FSYNC_WAIT)     ? "wait":"no wait",
   1530 		(ap->a_flags & FSYNC_DATAONLY) ? "data_only":"complete"));
   1531 
   1532 	vp = vp;
   1533 	return 0;
   1534 }
   1535 
   1536 /* --------------------------------------------------------------------- */
   1537 
   1538 int
   1539 nilfs_advlock(void *v)
   1540 {
   1541 	struct vop_advlock_args /* {
   1542 		struct vnode *a_vp;
   1543 		void *a_id;
   1544 		int a_op;
   1545 		struct flock *a_fl;
   1546 		int a_flags;
   1547 	} */ *ap = v;
   1548 	struct vnode *vp = ap->a_vp;
   1549 	struct nilfs_node *nilfs_node = VTOI(vp);
   1550 	uint64_t file_size;
   1551 
   1552 	DPRINTF(LOCKING, ("nilfs_advlock called\n"));
   1553 
   1554 	assert(nilfs_node);
   1555 	file_size = nilfs_rw64(nilfs_node->inode.i_size);
   1556 
   1557 	return lf_advlock(ap, &nilfs_node->lockf, file_size);
   1558 }
   1559 
   1560 /* --------------------------------------------------------------------- */
   1561 
   1562 
   1563 /* Global vfs vnode data structures for nilfss */
   1564 int (**nilfs_vnodeop_p) __P((void *));
   1565 
   1566 const struct vnodeopv_entry_desc nilfs_vnodeop_entries[] = {
   1567 	{ &vop_default_desc, vn_default_error },
   1568 	{ &vop_lookup_desc, nilfs_lookup },	/* lookup */
   1569 	{ &vop_create_desc, nilfs_create },	/* create */
   1570 	{ &vop_mknod_desc, nilfs_mknod },	/* mknod */	/* TODO */
   1571 	{ &vop_open_desc, nilfs_open },		/* open */
   1572 	{ &vop_close_desc, nilfs_close },	/* close */
   1573 	{ &vop_access_desc, nilfs_access },	/* access */
   1574 	{ &vop_getattr_desc, nilfs_getattr },	/* getattr */
   1575 	{ &vop_setattr_desc, nilfs_setattr },	/* setattr */	/* TODO chflags */
   1576 	{ &vop_read_desc, nilfs_read },		/* read */
   1577 	{ &vop_write_desc, nilfs_write },	/* write */	/* WRITE */
   1578 	{ &vop_fcntl_desc, genfs_fcntl },	/* fcntl */	/* TODO? */
   1579 	{ &vop_ioctl_desc, genfs_enoioctl },	/* ioctl */	/* TODO? */
   1580 	{ &vop_poll_desc, genfs_poll },		/* poll */	/* TODO/OK? */
   1581 	{ &vop_kqfilter_desc, genfs_kqfilter },	/* kqfilter */	/* ? */
   1582 	{ &vop_revoke_desc, genfs_revoke },	/* revoke */	/* TODO? */
   1583 	{ &vop_mmap_desc, genfs_mmap },		/* mmap */	/* OK? */
   1584 	{ &vop_fsync_desc, nilfs_fsync },	/* fsync */
   1585 	{ &vop_seek_desc, genfs_seek },		/* seek */
   1586 	{ &vop_remove_desc, nilfs_remove },	/* remove */
   1587 	{ &vop_link_desc, nilfs_link },		/* link */	/* TODO */
   1588 	{ &vop_rename_desc, nilfs_rename },	/* rename */ 	/* TODO */
   1589 	{ &vop_mkdir_desc, nilfs_mkdir },	/* mkdir */
   1590 	{ &vop_rmdir_desc, nilfs_rmdir },	/* rmdir */
   1591 	{ &vop_symlink_desc, nilfs_symlink },	/* symlink */	/* TODO */
   1592 	{ &vop_readdir_desc, nilfs_readdir },	/* readdir */
   1593 	{ &vop_readlink_desc, nilfs_readlink },	/* readlink */	/* TEST ME */
   1594 	{ &vop_abortop_desc, genfs_abortop },	/* abortop */	/* TODO/OK? */
   1595 	{ &vop_inactive_desc, nilfs_inactive },	/* inactive */
   1596 	{ &vop_reclaim_desc, nilfs_reclaim },	/* reclaim */
   1597 	{ &vop_lock_desc, genfs_lock },		/* lock */
   1598 	{ &vop_unlock_desc, genfs_unlock },	/* unlock */
   1599 	{ &vop_bmap_desc, nilfs_trivial_bmap },	/* bmap */	/* 1:1 bmap */
   1600 	{ &vop_strategy_desc, nilfs_vfsstrategy },/* strategy */
   1601 /*	{ &vop_print_desc, nilfs_print },	*/	/* print */
   1602 	{ &vop_islocked_desc, genfs_islocked },	/* islocked */
   1603 	{ &vop_pathconf_desc, nilfs_pathconf },	/* pathconf */
   1604 	{ &vop_advlock_desc, nilfs_advlock },	/* advlock */	/* TEST ME */
   1605 	{ &vop_bwrite_desc, vn_bwrite },	/* bwrite */	/* ->strategy */
   1606 	{ &vop_getpages_desc, genfs_getpages },	/* getpages */
   1607 	{ &vop_putpages_desc, genfs_putpages },	/* putpages */
   1608 	{ NULL, NULL }
   1609 };
   1610 
   1611 
   1612 const struct vnodeopv_desc nilfs_vnodeop_opv_desc = {
   1613 	&nilfs_vnodeop_p, nilfs_vnodeop_entries
   1614 };
   1615 
   1616