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