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