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