xref: /netbsd-src/sys/fs/nilfs/nilfs_vnops.c (revision 3816d47b2c42fcd6e549e3407f842a5b1a1d23ad)
1 /* $NetBSD: nilfs_vnops.c,v 1.4 2010/01/08 11:35:08 pooka 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 2010/01/08 11:35:08 pooka 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 static int
975 nilfs_check_possible(struct vnode *vp, struct vattr *vap, mode_t mode)
976 {
977 	int flags;
978 
979 	/* check if we are allowed to write */
980 	switch (vap->va_type) {
981 	case VDIR:
982 	case VLNK:
983 	case VREG:
984 		/*
985 		 * normal nodes: check if we're on a read-only mounted
986 		 * filingsystem and bomb out if we're trying to write.
987 		 */
988 		if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY))
989 			return EROFS;
990 		break;
991 	case VBLK:
992 	case VCHR:
993 	case VSOCK:
994 	case VFIFO:
995 		/*
996 		 * special nodes: even on read-only mounted filingsystems
997 		 * these are allowed to be written to if permissions allow.
998 		 */
999 		break;
1000 	default:
1001 		/* no idea what this is */
1002 		return EINVAL;
1003 	}
1004 
1005 	/* noone may write immutable files */
1006 	/* TODO: get chflags(2) flags */
1007 	flags = 0;
1008 	if ((mode & VWRITE) && (flags & IMMUTABLE))
1009 		return EPERM;
1010 
1011 	return 0;
1012 }
1013 
1014 static int
1015 nilfs_check_permitted(struct vnode *vp, struct vattr *vap, mode_t mode,
1016     kauth_cred_t cred)
1017 {
1018 
1019 	/* ask the generic genfs_can_access to advice on security */
1020 	return genfs_can_access(vp->v_type,
1021 			vap->va_mode, vap->va_uid, vap->va_gid,
1022 			mode, cred);
1023 }
1024 
1025 int
1026 nilfs_access(void *v)
1027 {
1028 	struct vop_access_args /* {
1029 		struct vnode *a_vp;
1030 		int a_mode;
1031 		kauth_cred_t a_cred;
1032 		struct proc *a_p;
1033 	} */ *ap = v;
1034 	struct vnode    *vp   = ap->a_vp;
1035 	mode_t	         mode = ap->a_mode;
1036 	kauth_cred_t     cred = ap->a_cred;
1037 	/* struct nilfs_node *nilfs_node = VTOI(vp); */
1038 	struct vattr vap;
1039 	int error;
1040 
1041 	DPRINTF(VFSCALL, ("nilfs_access called\n"));
1042 
1043 	error = VOP_GETATTR(vp, &vap, NULL);
1044 	if (error)
1045 		return error;
1046 
1047 	error = nilfs_check_possible(vp, &vap, mode);
1048 	if (error)
1049 		return error;
1050 
1051 	error = nilfs_check_permitted(vp, &vap, mode, cred);
1052 
1053 	return error;
1054 }
1055 
1056 /* --------------------------------------------------------------------- */
1057 
1058 int
1059 nilfs_create(void *v)
1060 {
1061 	struct vop_create_args /* {
1062 		struct vnode *a_dvp;
1063 		struct vnode **a_vpp;
1064 		struct componentname *a_cnp;
1065 		struct vattr *a_vap;
1066 	} */ *ap = v;
1067 	struct vnode  *dvp = ap->a_dvp;
1068 	struct vnode **vpp = ap->a_vpp;
1069 	struct vattr  *vap  = ap->a_vap;
1070 	struct componentname *cnp = ap->a_cnp;
1071 	int error;
1072 
1073 	DPRINTF(VFSCALL, ("nilfs_create called\n"));
1074 	error = nilfs_create_node(dvp, vpp, vap, cnp);
1075 
1076 	if (error || !(cnp->cn_flags & SAVESTART))
1077 		PNBUF_PUT(cnp->cn_pnbuf);
1078 	vput(dvp);
1079 	return error;
1080 }
1081 
1082 /* --------------------------------------------------------------------- */
1083 
1084 int
1085 nilfs_mknod(void *v)
1086 {
1087 	struct vop_mknod_args /* {
1088 		struct vnode *a_dvp;
1089 		struct vnode **a_vpp;
1090 		struct componentname *a_cnp;
1091 		struct vattr *a_vap;
1092 	} */ *ap = v;
1093 	struct vnode  *dvp = ap->a_dvp;
1094 	struct vnode **vpp = ap->a_vpp;
1095 	struct vattr  *vap  = ap->a_vap;
1096 	struct componentname *cnp = ap->a_cnp;
1097 	int error;
1098 
1099 	DPRINTF(VFSCALL, ("nilfs_mknod called\n"));
1100 	error = nilfs_create_node(dvp, vpp, vap, cnp);
1101 
1102 	if (error || !(cnp->cn_flags & SAVESTART))
1103 		PNBUF_PUT(cnp->cn_pnbuf);
1104 	vput(dvp);
1105 	return error;
1106 }
1107 
1108 /* --------------------------------------------------------------------- */
1109 
1110 int
1111 nilfs_mkdir(void *v)
1112 {
1113 	struct vop_mkdir_args /* {
1114 		struct vnode *a_dvp;
1115 		struct vnode **a_vpp;
1116 		struct componentname *a_cnp;
1117 		struct vattr *a_vap;
1118 	} */ *ap = v;
1119 	struct vnode  *dvp = ap->a_dvp;
1120 	struct vnode **vpp = ap->a_vpp;
1121 	struct vattr  *vap  = ap->a_vap;
1122 	struct componentname *cnp = ap->a_cnp;
1123 	int error;
1124 
1125 	DPRINTF(VFSCALL, ("nilfs_mkdir called\n"));
1126 	error = nilfs_create_node(dvp, vpp, vap, cnp);
1127 
1128 	if (error || !(cnp->cn_flags & SAVESTART))
1129 		PNBUF_PUT(cnp->cn_pnbuf);
1130 	vput(dvp);
1131 	return error;
1132 }
1133 
1134 /* --------------------------------------------------------------------- */
1135 
1136 static int
1137 nilfs_do_link(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1138 {
1139 	struct nilfs_node *nilfs_node, *dir_node;
1140 	struct vattr vap;
1141 	int error;
1142 
1143 	DPRINTF(VFSCALL, ("nilfs_link called\n"));
1144 	error = 0;
1145 
1146 	/* some quick checks */
1147 	if (vp->v_type == VDIR)
1148 		return EPERM;		/* can't link a directory */
1149 	if (dvp->v_mount != vp->v_mount)
1150 		return EXDEV;		/* can't link across devices */
1151 	if (dvp == vp)
1152 		return EPERM;		/* can't be the same */
1153 
1154 	/* lock node */
1155 	error = vn_lock(vp, LK_EXCLUSIVE);
1156 	if (error)
1157 		return error;
1158 
1159 	/* get attributes */
1160 	dir_node = VTOI(dvp);
1161 	nilfs_node = VTOI(vp);
1162 
1163 	error = VOP_GETATTR(vp, &vap, FSCRED);
1164 	if (error)
1165 		return error;
1166 
1167 	/* check link count overflow */
1168 	if (vap.va_nlink >= (1<<16)-1)	/* uint16_t */
1169 		return EMLINK;
1170 
1171 	return nilfs_dir_attach(dir_node->ump, dir_node, nilfs_node, &vap, cnp);
1172 }
1173 
1174 int
1175 nilfs_link(void *v)
1176 {
1177 	struct vop_link_args /* {
1178 		struct vnode *a_dvp;
1179 		struct vnode *a_vp;
1180 		struct componentname *a_cnp;
1181 	} */ *ap = v;
1182 	struct vnode *dvp = ap->a_dvp;
1183 	struct vnode *vp  = ap->a_vp;
1184 	struct componentname *cnp = ap->a_cnp;
1185 	int error;
1186 
1187 	error = nilfs_do_link(dvp, vp, cnp);
1188 	if (error)
1189 		VOP_ABORTOP(dvp, cnp);
1190 
1191 	if ((vp != dvp) && (VOP_ISLOCKED(vp) == LK_EXCLUSIVE))
1192 		VOP_UNLOCK(vp, 0);
1193 
1194 	VN_KNOTE(vp, NOTE_LINK);
1195 	VN_KNOTE(dvp, NOTE_WRITE);
1196 	vput(dvp);
1197 
1198 	return error;
1199 }
1200 
1201 /* --------------------------------------------------------------------- */
1202 
1203 static int
1204 nilfs_do_symlink(struct nilfs_node *nilfs_node, char *target)
1205 {
1206 	return EROFS;
1207 }
1208 
1209 
1210 int
1211 nilfs_symlink(void *v)
1212 {
1213 	struct vop_symlink_args /* {
1214 		struct vnode *a_dvp;
1215 		struct vnode **a_vpp;
1216 		struct componentname *a_cnp;
1217 		struct vattr *a_vap;
1218 		char *a_target;
1219 	} */ *ap = v;
1220 	struct vnode  *dvp = ap->a_dvp;
1221 	struct vnode **vpp = ap->a_vpp;
1222 	struct vattr  *vap  = ap->a_vap;
1223 	struct componentname *cnp = ap->a_cnp;
1224 	struct nilfs_node *dir_node;
1225 	struct nilfs_node *nilfs_node;
1226 	int error;
1227 
1228 	DPRINTF(VFSCALL, ("nilfs_symlink called\n"));
1229 	DPRINTF(VFSCALL, ("\tlinking to `%s`\n",  ap->a_target));
1230 	error = nilfs_create_node(dvp, vpp, vap, cnp);
1231 	KASSERT(((error == 0) && (*vpp != NULL)) || ((error && (*vpp == NULL))));
1232 	if (!error) {
1233 		dir_node = VTOI(dvp);
1234 		nilfs_node = VTOI(*vpp);
1235 		KASSERT(nilfs_node);
1236 		error = nilfs_do_symlink(nilfs_node, ap->a_target);
1237 		if (error) {
1238 			/* remove node */
1239 			nilfs_shrink_node(nilfs_node, 0);
1240 			nilfs_dir_detach(nilfs_node->ump, dir_node, nilfs_node, cnp);
1241 		}
1242 	}
1243 	if (error || !(cnp->cn_flags & SAVESTART))
1244 		PNBUF_PUT(cnp->cn_pnbuf);
1245 	vput(dvp);
1246 	return error;
1247 }
1248 
1249 /* --------------------------------------------------------------------- */
1250 
1251 int
1252 nilfs_readlink(void *v)
1253 {
1254 	struct vop_readlink_args /* {
1255 		struct vnode *a_vp;
1256 		struct uio *a_uio;
1257 		kauth_cred_t a_cred;
1258 	} */ *ap = v;
1259 #if 0
1260 	struct vnode *vp = ap->a_vp;
1261 	struct uio *uio = ap->a_uio;
1262 	kauth_cred_t cred = ap->a_cred;
1263 	struct nilfs_node *nilfs_node;
1264 	struct pathcomp pathcomp;
1265 	struct vattr vattr;
1266 	uint8_t *pathbuf, *targetbuf, *tmpname;
1267 	uint8_t *pathpos, *targetpos;
1268 	char *mntonname;
1269 	int pathlen, targetlen, namelen, mntonnamelen, len, l_ci;
1270 	int first, error;
1271 #endif
1272 	ap = ap;
1273 
1274 	DPRINTF(VFSCALL, ("nilfs_readlink called\n"));
1275 
1276 	return EROFS;
1277 }
1278 
1279 /* --------------------------------------------------------------------- */
1280 
1281 /* note: i tried to follow the logics of the tmpfs rename code */
1282 int
1283 nilfs_rename(void *v)
1284 {
1285 	struct vop_rename_args /* {
1286 		struct vnode *a_fdvp;
1287 		struct vnode *a_fvp;
1288 		struct componentname *a_fcnp;
1289 		struct vnode *a_tdvp;
1290 		struct vnode *a_tvp;
1291 		struct componentname *a_tcnp;
1292 	} */ *ap = v;
1293 	struct vnode *tvp = ap->a_tvp;
1294 	struct vnode *tdvp = ap->a_tdvp;
1295 	struct vnode *fvp = ap->a_fvp;
1296 	struct vnode *fdvp = ap->a_fdvp;
1297 	struct componentname *tcnp = ap->a_tcnp;
1298 	struct componentname *fcnp = ap->a_fcnp;
1299 	struct nilfs_node *fnode, *fdnode, *tnode, *tdnode;
1300 	struct vattr fvap, tvap;
1301 	int error;
1302 
1303 	DPRINTF(VFSCALL, ("nilfs_rename called\n"));
1304 
1305 	/* disallow cross-device renames */
1306 	if (fvp->v_mount != tdvp->v_mount ||
1307 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1308 		error = EXDEV;
1309 		goto out_unlocked;
1310 	}
1311 
1312 	fnode  = VTOI(fvp);
1313 	fdnode = VTOI(fdvp);
1314 	tnode  = (tvp == NULL) ? NULL : VTOI(tvp);
1315 	tdnode = VTOI(tdvp);
1316 
1317 	/* lock our source dir */
1318 	if (fdnode != tdnode) {
1319 		error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
1320 		if (error != 0)
1321 			goto out_unlocked;
1322 	}
1323 
1324 	/* get info about the node to be moved */
1325 	error = VOP_GETATTR(fvp, &fvap, FSCRED);
1326 	KASSERT(error == 0);
1327 
1328 	/* check when to delete the old already existing entry */
1329 	if (tvp) {
1330 		/* get info about the node to be moved to */
1331 		error = VOP_GETATTR(fvp, &tvap, FSCRED);
1332 		KASSERT(error == 0);
1333 
1334 		/* if both dirs, make sure the destination is empty */
1335 		if (fvp->v_type == VDIR && tvp->v_type == VDIR) {
1336 			if (tvap.va_nlink > 2) {
1337 				error = ENOTEMPTY;
1338 				goto out;
1339 			}
1340 		}
1341 		/* if moving dir, make sure destination is dir too */
1342 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1343 			error = ENOTDIR;
1344 			goto out;
1345 		}
1346 		/* if we're moving a non-directory, make sure dest is no dir */
1347 		if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1348 			error = EISDIR;
1349 			goto out;
1350 		}
1351 	}
1352 
1353 	/* dont allow renaming directories acros directory for now */
1354 	if (fdnode != tdnode) {
1355 		if (fvp->v_type == VDIR) {
1356 			error = EINVAL;
1357 			goto out;
1358 		}
1359 	}
1360 
1361 	/* remove existing entry if present */
1362 	if (tvp)
1363 		nilfs_dir_detach(tdnode->ump, tdnode, tnode, tcnp);
1364 
1365 	/* create new directory entry for the node */
1366 	error = nilfs_dir_attach(tdnode->ump, tdnode, fnode, &fvap, tcnp);
1367 	if (error)
1368 		goto out;
1369 
1370 	/* unlink old directory entry for the node, if failing, unattach new */
1371 	error = nilfs_dir_detach(tdnode->ump, fdnode, fnode, fcnp);
1372 	if (error)
1373 		nilfs_dir_detach(tdnode->ump, tdnode, fnode, tcnp);
1374 
1375 out:
1376         if (fdnode != tdnode)
1377                 VOP_UNLOCK(fdvp, 0);
1378 
1379 out_unlocked:
1380 	VOP_ABORTOP(tdvp, tcnp);
1381 	if (tdvp == tvp)
1382 		vrele(tdvp);
1383 	else
1384 		vput(tdvp);
1385 	if (tvp)
1386 		vput(tvp);
1387 	VOP_ABORTOP(fdvp, fcnp);
1388 
1389 	/* release source nodes. */
1390 	vrele(fdvp);
1391 	vrele(fvp);
1392 
1393 	return error;
1394 }
1395 
1396 /* --------------------------------------------------------------------- */
1397 
1398 int
1399 nilfs_remove(void *v)
1400 {
1401 	struct vop_remove_args /* {
1402 		struct vnode *a_dvp;
1403 		struct vnode *a_vp;
1404 		struct componentname *a_cnp;
1405 	} */ *ap = v;
1406 	struct vnode *dvp = ap->a_dvp;
1407 	struct vnode *vp  = ap->a_vp;
1408 	struct componentname *cnp = ap->a_cnp;
1409 	struct nilfs_node *dir_node = VTOI(dvp);
1410 	struct nilfs_node *nilfs_node = VTOI(vp);
1411 	struct nilfs_mount *ump = dir_node->ump;
1412 	int error;
1413 
1414 	DPRINTF(VFSCALL, ("nilfs_remove called\n"));
1415 	if (vp->v_type != VDIR) {
1416 		error = nilfs_dir_detach(ump, dir_node, nilfs_node, cnp);
1417 		DPRINTFIF(NODE, error, ("\tgot error removing file\n"));
1418 	} else {
1419 		DPRINTF(NODE, ("\tis a directory: perm. denied\n"));
1420 		error = EPERM;
1421 	}
1422 
1423 	if (error == 0) {
1424 		VN_KNOTE(vp, NOTE_DELETE);
1425 		VN_KNOTE(dvp, NOTE_WRITE);
1426 	}
1427 
1428 	if (dvp == vp)
1429 		vrele(vp);
1430 	else
1431 		vput(vp);
1432 	vput(dvp);
1433 
1434 	return error;
1435 }
1436 
1437 /* --------------------------------------------------------------------- */
1438 
1439 int
1440 nilfs_rmdir(void *v)
1441 {
1442 	struct vop_rmdir_args /* {
1443 		struct vnode *a_dvp;
1444 		struct vnode *a_vp;
1445 		struct componentname *a_cnp;
1446 	} */ *ap = v;
1447 	struct vnode *vp = ap->a_vp;
1448 	struct vnode *dvp = ap->a_dvp;
1449 	struct componentname *cnp = ap->a_cnp;
1450 	struct nilfs_node *dir_node = VTOI(dvp);
1451 	struct nilfs_node *nilfs_node = VTOI(vp);
1452 	struct nilfs_mount *ump = dir_node->ump;
1453 	int refcnt, error;
1454 
1455 	DPRINTF(NOTIMPL, ("nilfs_rmdir called\n"));
1456 
1457 	/* don't allow '.' to be deleted */
1458 	if (dir_node == nilfs_node) {
1459 		vrele(dvp);
1460 		vput(vp);
1461 		return EINVAL;
1462 	}
1463 
1464 	/* check to see if the directory is empty */
1465 	error = 0;
1466 	refcnt = 2; /* XXX */
1467 	if (refcnt > 1) {
1468 		/* NOT empty */
1469 		vput(dvp);
1470 		vput(vp);
1471 		return ENOTEMPTY;
1472 	}
1473 
1474 	/* detach the node from the directory */
1475 	error = nilfs_dir_detach(ump, dir_node, nilfs_node, cnp);
1476 	if (error == 0) {
1477 		cache_purge(vp);
1478 //		cache_purge(dvp);	/* XXX from msdosfs, why? */
1479 		VN_KNOTE(vp, NOTE_DELETE);
1480 	}
1481 	DPRINTFIF(NODE, error, ("\tgot error removing file\n"));
1482 
1483 	/* unput the nodes and exit */
1484 	vput(dvp);
1485 	vput(vp);
1486 
1487 	return error;
1488 }
1489 
1490 /* --------------------------------------------------------------------- */
1491 
1492 int
1493 nilfs_fsync(void *v)
1494 {
1495 	struct vop_fsync_args /* {
1496 		struct vnode *a_vp;
1497 		kauth_cred_t a_cred;
1498 		int a_flags;
1499 		off_t offlo;
1500 		off_t offhi;
1501 		struct proc *a_p;
1502 	} */ *ap = v;
1503 	struct vnode *vp = ap->a_vp;
1504 //	struct nilfs_node *nilfs_node = VTOI(vp);
1505 //	int error, flags, wait;
1506 
1507 	DPRINTF(STRATEGY, ("nilfs_fsync called : %s, %s\n",
1508 		(ap->a_flags & FSYNC_WAIT)     ? "wait":"no wait",
1509 		(ap->a_flags & FSYNC_DATAONLY) ? "data_only":"complete"));
1510 
1511 	vp = vp;
1512 	return 0;
1513 }
1514 
1515 /* --------------------------------------------------------------------- */
1516 
1517 int
1518 nilfs_advlock(void *v)
1519 {
1520 	struct vop_advlock_args /* {
1521 		struct vnode *a_vp;
1522 		void *a_id;
1523 		int a_op;
1524 		struct flock *a_fl;
1525 		int a_flags;
1526 	} */ *ap = v;
1527 	struct vnode *vp = ap->a_vp;
1528 	struct nilfs_node *nilfs_node = VTOI(vp);
1529 	uint64_t file_size;
1530 
1531 	DPRINTF(LOCKING, ("nilfs_advlock called\n"));
1532 
1533 	assert(nilfs_node);
1534 	file_size = nilfs_rw64(nilfs_node->inode.i_size);
1535 
1536 	return lf_advlock(ap, &nilfs_node->lockf, file_size);
1537 }
1538 
1539 /* --------------------------------------------------------------------- */
1540 
1541 
1542 /* Global vfs vnode data structures for nilfss */
1543 int (**nilfs_vnodeop_p) __P((void *));
1544 
1545 const struct vnodeopv_entry_desc nilfs_vnodeop_entries[] = {
1546 	{ &vop_default_desc, vn_default_error },
1547 	{ &vop_lookup_desc, nilfs_lookup },	/* lookup */
1548 	{ &vop_create_desc, nilfs_create },	/* create */
1549 	{ &vop_mknod_desc, nilfs_mknod },	/* mknod */	/* TODO */
1550 	{ &vop_open_desc, nilfs_open },		/* open */
1551 	{ &vop_close_desc, nilfs_close },	/* close */
1552 	{ &vop_access_desc, nilfs_access },	/* access */
1553 	{ &vop_getattr_desc, nilfs_getattr },	/* getattr */
1554 	{ &vop_setattr_desc, nilfs_setattr },	/* setattr */	/* TODO chflags */
1555 	{ &vop_read_desc, nilfs_read },		/* read */
1556 	{ &vop_write_desc, nilfs_write },	/* write */	/* WRITE */
1557 	{ &vop_fcntl_desc, genfs_fcntl },	/* fcntl */	/* TODO? */
1558 	{ &vop_ioctl_desc, genfs_enoioctl },	/* ioctl */	/* TODO? */
1559 	{ &vop_poll_desc, genfs_poll },		/* poll */	/* TODO/OK? */
1560 	{ &vop_kqfilter_desc, genfs_kqfilter },	/* kqfilter */	/* ? */
1561 	{ &vop_revoke_desc, genfs_revoke },	/* revoke */	/* TODO? */
1562 	{ &vop_mmap_desc, genfs_mmap },		/* mmap */	/* OK? */
1563 	{ &vop_fsync_desc, nilfs_fsync },	/* fsync */
1564 	{ &vop_seek_desc, genfs_seek },		/* seek */
1565 	{ &vop_remove_desc, nilfs_remove },	/* remove */
1566 	{ &vop_link_desc, nilfs_link },		/* link */	/* TODO */
1567 	{ &vop_rename_desc, nilfs_rename },	/* rename */ 	/* TODO */
1568 	{ &vop_mkdir_desc, nilfs_mkdir },	/* mkdir */
1569 	{ &vop_rmdir_desc, nilfs_rmdir },	/* rmdir */
1570 	{ &vop_symlink_desc, nilfs_symlink },	/* symlink */	/* TODO */
1571 	{ &vop_readdir_desc, nilfs_readdir },	/* readdir */
1572 	{ &vop_readlink_desc, nilfs_readlink },	/* readlink */	/* TEST ME */
1573 	{ &vop_abortop_desc, genfs_abortop },	/* abortop */	/* TODO/OK? */
1574 	{ &vop_inactive_desc, nilfs_inactive },	/* inactive */
1575 	{ &vop_reclaim_desc, nilfs_reclaim },	/* reclaim */
1576 	{ &vop_lock_desc, genfs_lock },		/* lock */
1577 	{ &vop_unlock_desc, genfs_unlock },	/* unlock */
1578 	{ &vop_bmap_desc, nilfs_trivial_bmap },	/* bmap */	/* 1:1 bmap */
1579 	{ &vop_strategy_desc, nilfs_vfsstrategy },/* strategy */
1580 /*	{ &vop_print_desc, nilfs_print },	*/	/* print */
1581 	{ &vop_islocked_desc, genfs_islocked },	/* islocked */
1582 	{ &vop_pathconf_desc, nilfs_pathconf },	/* pathconf */
1583 	{ &vop_advlock_desc, nilfs_advlock },	/* advlock */	/* TEST ME */
1584 	{ &vop_bwrite_desc, vn_bwrite },	/* bwrite */	/* ->strategy */
1585 	{ &vop_getpages_desc, genfs_getpages },	/* getpages */
1586 	{ &vop_putpages_desc, genfs_putpages },	/* putpages */
1587 	{ NULL, NULL }
1588 };
1589 
1590 
1591 const struct vnodeopv_desc nilfs_vnodeop_opv_desc = {
1592 	&nilfs_vnodeop_p, nilfs_vnodeop_entries
1593 };
1594 
1595