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