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