fuse_vnops.c revision 300978
1/*
2 * Copyright (c) 2007-2009 Google Inc. and Amit Singh
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 *   notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 *   copyright notice, this list of conditions and the following disclaimer
13 *   in the documentation and/or other materials provided with the
14 *   distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 *   contributors may be used to endorse or promote products derived from
17 *   this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Copyright (C) 2005 Csaba Henk.
32 * All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 *    notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 *    notice, this list of conditions and the following disclaimer in the
41 *    documentation and/or other materials provided with the distribution.
42 *
43 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 */
55
56#include <sys/cdefs.h>
57__FBSDID("$FreeBSD: stable/10/sys/fs/fuse/fuse_vnops.c 300978 2016-05-29 23:30:36Z rmacklem $");
58
59#include <sys/types.h>
60#include <sys/module.h>
61#include <sys/systm.h>
62#include <sys/errno.h>
63#include <sys/param.h>
64#include <sys/kernel.h>
65#include <sys/conf.h>
66#include <sys/uio.h>
67#include <sys/malloc.h>
68#include <sys/queue.h>
69#include <sys/lock.h>
70#include <sys/rwlock.h>
71#include <sys/sx.h>
72#include <sys/proc.h>
73#include <sys/mount.h>
74#include <sys/vnode.h>
75#include <sys/namei.h>
76#include <sys/stat.h>
77#include <sys/unistd.h>
78#include <sys/filedesc.h>
79#include <sys/file.h>
80#include <sys/fcntl.h>
81#include <sys/dirent.h>
82#include <sys/bio.h>
83#include <sys/buf.h>
84#include <sys/sysctl.h>
85
86#include <vm/vm.h>
87#include <vm/vm_extern.h>
88#include <vm/pmap.h>
89#include <vm/vm_map.h>
90#include <vm/vm_page.h>
91#include <vm/vm_param.h>
92#include <vm/vm_object.h>
93#include <vm/vm_pager.h>
94#include <vm/vnode_pager.h>
95#include <vm/vm_object.h>
96
97#include "fuse.h"
98#include "fuse_file.h"
99#include "fuse_internal.h"
100#include "fuse_ipc.h"
101#include "fuse_node.h"
102#include "fuse_param.h"
103#include "fuse_io.h"
104
105#include <sys/priv.h>
106
107#define FUSE_DEBUG_MODULE VNOPS
108#include "fuse_debug.h"
109
110/* vnode ops */
111static vop_access_t fuse_vnop_access;
112static vop_close_t fuse_vnop_close;
113static vop_create_t fuse_vnop_create;
114static vop_fsync_t fuse_vnop_fsync;
115static vop_getattr_t fuse_vnop_getattr;
116static vop_inactive_t fuse_vnop_inactive;
117static vop_link_t fuse_vnop_link;
118static vop_lookup_t fuse_vnop_lookup;
119static vop_mkdir_t fuse_vnop_mkdir;
120static vop_mknod_t fuse_vnop_mknod;
121static vop_open_t fuse_vnop_open;
122static vop_read_t fuse_vnop_read;
123static vop_readdir_t fuse_vnop_readdir;
124static vop_readlink_t fuse_vnop_readlink;
125static vop_reclaim_t fuse_vnop_reclaim;
126static vop_remove_t fuse_vnop_remove;
127static vop_rename_t fuse_vnop_rename;
128static vop_rmdir_t fuse_vnop_rmdir;
129static vop_setattr_t fuse_vnop_setattr;
130static vop_strategy_t fuse_vnop_strategy;
131static vop_symlink_t fuse_vnop_symlink;
132static vop_write_t fuse_vnop_write;
133static vop_getpages_t fuse_vnop_getpages;
134static vop_putpages_t fuse_vnop_putpages;
135static vop_print_t fuse_vnop_print;
136
137struct vop_vector fuse_vnops = {
138	.vop_default = &default_vnodeops,
139	.vop_access = fuse_vnop_access,
140	.vop_close = fuse_vnop_close,
141	.vop_create = fuse_vnop_create,
142	.vop_fsync = fuse_vnop_fsync,
143	.vop_getattr = fuse_vnop_getattr,
144	.vop_inactive = fuse_vnop_inactive,
145	.vop_link = fuse_vnop_link,
146	.vop_lookup = fuse_vnop_lookup,
147	.vop_mkdir = fuse_vnop_mkdir,
148	.vop_mknod = fuse_vnop_mknod,
149	.vop_open = fuse_vnop_open,
150	.vop_pathconf = vop_stdpathconf,
151	.vop_read = fuse_vnop_read,
152	.vop_readdir = fuse_vnop_readdir,
153	.vop_readlink = fuse_vnop_readlink,
154	.vop_reclaim = fuse_vnop_reclaim,
155	.vop_remove = fuse_vnop_remove,
156	.vop_rename = fuse_vnop_rename,
157	.vop_rmdir = fuse_vnop_rmdir,
158	.vop_setattr = fuse_vnop_setattr,
159	.vop_strategy = fuse_vnop_strategy,
160	.vop_symlink = fuse_vnop_symlink,
161	.vop_write = fuse_vnop_write,
162	.vop_getpages = fuse_vnop_getpages,
163	.vop_putpages = fuse_vnop_putpages,
164	.vop_print = fuse_vnop_print,
165};
166
167static u_long fuse_lookup_cache_hits = 0;
168
169SYSCTL_ULONG(_vfs_fuse, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
170    &fuse_lookup_cache_hits, 0, "");
171
172static u_long fuse_lookup_cache_misses = 0;
173
174SYSCTL_ULONG(_vfs_fuse, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
175    &fuse_lookup_cache_misses, 0, "");
176
177int	fuse_lookup_cache_enable = 1;
178
179SYSCTL_INT(_vfs_fuse, OID_AUTO, lookup_cache_enable, CTLFLAG_RW,
180    &fuse_lookup_cache_enable, 0, "");
181
182/*
183 * XXX: This feature is highly experimental and can bring to instabilities,
184 * needs revisiting before to be enabled by default.
185 */
186static int fuse_reclaim_revoked = 0;
187
188SYSCTL_INT(_vfs_fuse, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
189    &fuse_reclaim_revoked, 0, "");
190
191int	fuse_pbuf_freecnt = -1;
192
193#define fuse_vm_page_lock(m)		vm_page_lock((m));
194#define fuse_vm_page_unlock(m)		vm_page_unlock((m));
195#define fuse_vm_page_lock_queues()	((void)0)
196#define fuse_vm_page_unlock_queues()	((void)0)
197
198/*
199    struct vnop_access_args {
200        struct vnode *a_vp;
201#if VOP_ACCESS_TAKES_ACCMODE_T
202        accmode_t a_accmode;
203#else
204        int a_mode;
205#endif
206        struct ucred *a_cred;
207        struct thread *a_td;
208    };
209*/
210static int
211fuse_vnop_access(struct vop_access_args *ap)
212{
213	struct vnode *vp = ap->a_vp;
214	int accmode = ap->a_accmode;
215	struct ucred *cred = ap->a_cred;
216
217	struct fuse_access_param facp;
218	struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
219
220	int err;
221
222	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
223
224	if (fuse_isdeadfs(vp)) {
225		if (vnode_isvroot(vp)) {
226			return 0;
227		}
228		return ENXIO;
229	}
230	if (!(data->dataflags & FSESS_INITED)) {
231		if (vnode_isvroot(vp)) {
232			if (priv_check_cred(cred, PRIV_VFS_ADMIN, 0) ||
233			    (fuse_match_cred(data->daemoncred, cred) == 0)) {
234				return 0;
235			}
236		}
237		return EBADF;
238	}
239	if (vnode_islnk(vp)) {
240		return 0;
241	}
242	bzero(&facp, sizeof(facp));
243
244	err = fuse_internal_access(vp, accmode, &facp, ap->a_td, ap->a_cred);
245	FS_DEBUG2G("err=%d accmode=0x%x\n", err, accmode);
246	return err;
247}
248
249/*
250    struct vnop_close_args {
251	struct vnode *a_vp;
252	int  a_fflag;
253	struct ucred *a_cred;
254	struct thread *a_td;
255    };
256*/
257static int
258fuse_vnop_close(struct vop_close_args *ap)
259{
260	struct vnode *vp = ap->a_vp;
261	struct ucred *cred = ap->a_cred;
262	int fflag = ap->a_fflag;
263	fufh_type_t fufh_type;
264
265	fuse_trace_printf_vnop();
266
267	if (fuse_isdeadfs(vp)) {
268		return 0;
269	}
270	if (vnode_isdir(vp)) {
271		if (fuse_filehandle_valid(vp, FUFH_RDONLY)) {
272			fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
273		}
274		return 0;
275	}
276	if (fflag & IO_NDELAY) {
277		return 0;
278	}
279	fufh_type = fuse_filehandle_xlate_from_fflags(fflag);
280
281	if (!fuse_filehandle_valid(vp, fufh_type)) {
282		int i;
283
284		for (i = 0; i < FUFH_MAXTYPE; i++)
285			if (fuse_filehandle_valid(vp, i))
286				break;
287		if (i == FUFH_MAXTYPE)
288			panic("FUSE: fufh type %d found to be invalid in close"
289			      " (fflag=0x%x)\n",
290			      fufh_type, fflag);
291	}
292	if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
293		fuse_vnode_savesize(vp, cred);
294	}
295	return 0;
296}
297
298/*
299    struct vnop_create_args {
300	struct vnode *a_dvp;
301	struct vnode **a_vpp;
302	struct componentname *a_cnp;
303	struct vattr *a_vap;
304    };
305*/
306static int
307fuse_vnop_create(struct vop_create_args *ap)
308{
309	struct vnode *dvp = ap->a_dvp;
310	struct vnode **vpp = ap->a_vpp;
311	struct componentname *cnp = ap->a_cnp;
312	struct vattr *vap = ap->a_vap;
313	struct thread *td = cnp->cn_thread;
314	struct ucred *cred = cnp->cn_cred;
315
316	struct fuse_open_in *foi;
317	struct fuse_entry_out *feo;
318	struct fuse_dispatcher fdi;
319	struct fuse_dispatcher *fdip = &fdi;
320
321	int err;
322
323	struct mount *mp = vnode_mount(dvp);
324	uint64_t parentnid = VTOFUD(dvp)->nid;
325	mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
326	uint64_t x_fh_id;
327	uint32_t x_open_flags;
328
329	fuse_trace_printf_vnop();
330
331	if (fuse_isdeadfs(dvp)) {
332		return ENXIO;
333	}
334	bzero(&fdi, sizeof(fdi));
335
336	/* XXX:	Will we ever want devices ? */
337	if ((vap->va_type != VREG)) {
338		MPASS(vap->va_type != VFIFO);
339		goto bringup;
340	}
341	debug_printf("parent nid = %ju, mode = %x\n", (uintmax_t)parentnid,
342	    mode);
343
344	fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
345	if (!fsess_isimpl(mp, FUSE_CREATE)) {
346		debug_printf("eh, daemon doesn't implement create?\n");
347		return (EINVAL);
348	}
349	fdisp_make(fdip, FUSE_CREATE, vnode_mount(dvp), parentnid, td, cred);
350
351	foi = fdip->indata;
352	foi->mode = mode;
353	foi->flags = O_CREAT | O_RDWR;
354
355	memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
356	    cnp->cn_namelen);
357	((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
358
359	err = fdisp_wait_answ(fdip);
360
361	if (err) {
362		if (err == ENOSYS)
363			fsess_set_notimpl(mp, FUSE_CREATE);
364		debug_printf("create: got err=%d from daemon\n", err);
365		goto out;
366	}
367bringup:
368	feo = fdip->answ;
369
370	if ((err = fuse_internal_checkentry(feo, VREG))) {
371		goto out;
372	}
373	err = fuse_vnode_get(mp, feo->nodeid, dvp, vpp, cnp, VREG);
374	if (err) {
375		struct fuse_release_in *fri;
376		uint64_t nodeid = feo->nodeid;
377		uint64_t fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
378
379		fdisp_init(fdip, sizeof(*fri));
380		fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
381		fri = fdip->indata;
382		fri->fh = fh_id;
383		fri->flags = OFLAGS(mode);
384		fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
385		fuse_insert_message(fdip->tick);
386		return err;
387	}
388	ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
389
390	fdip->answ = feo + 1;
391
392	x_fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
393	x_open_flags = ((struct fuse_open_out *)(feo + 1))->open_flags;
394	fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, x_fh_id);
395	fuse_vnode_open(*vpp, x_open_flags, td);
396	cache_purge_negative(dvp);
397
398out:
399	fdisp_destroy(fdip);
400	return err;
401}
402
403/*
404 * Our vnop_fsync roughly corresponds to the FUSE_FSYNC method. The Linux
405 * version of FUSE also has a FUSE_FLUSH method.
406 *
407 * On Linux, fsync() synchronizes a file's complete in-core state with that
408 * on disk. The call is not supposed to return until the system has completed
409 * that action or until an error is detected.
410 *
411 * Linux also has an fdatasync() call that is similar to fsync() but is not
412 * required to update the metadata such as access time and modification time.
413 */
414
415/*
416    struct vnop_fsync_args {
417	struct vnodeop_desc *a_desc;
418	struct vnode * a_vp;
419	struct ucred * a_cred;
420	int  a_waitfor;
421	struct thread * a_td;
422    };
423*/
424static int
425fuse_vnop_fsync(struct vop_fsync_args *ap)
426{
427	struct vnode *vp = ap->a_vp;
428	struct thread *td = ap->a_td;
429
430	struct fuse_filehandle *fufh;
431	struct fuse_vnode_data *fvdat = VTOFUD(vp);
432
433	int type, err = 0;
434
435	fuse_trace_printf_vnop();
436
437	if (fuse_isdeadfs(vp)) {
438		return 0;
439	}
440	if ((err = vop_stdfsync(ap)))
441		return err;
442
443	if (!fsess_isimpl(vnode_mount(vp),
444	    (vnode_vtype(vp) == VDIR ? FUSE_FSYNCDIR : FUSE_FSYNC))) {
445		goto out;
446	}
447	for (type = 0; type < FUFH_MAXTYPE; type++) {
448		fufh = &(fvdat->fufh[type]);
449		if (FUFH_IS_VALID(fufh)) {
450			fuse_internal_fsync(vp, td, NULL, fufh);
451		}
452	}
453
454out:
455	return 0;
456}
457
458/*
459    struct vnop_getattr_args {
460	struct vnode *a_vp;
461	struct vattr *a_vap;
462	struct ucred *a_cred;
463	struct thread *a_td;
464    };
465*/
466static int
467fuse_vnop_getattr(struct vop_getattr_args *ap)
468{
469	struct vnode *vp = ap->a_vp;
470	struct vattr *vap = ap->a_vap;
471	struct ucred *cred = ap->a_cred;
472	struct thread *td = curthread;
473	struct fuse_vnode_data *fvdat = VTOFUD(vp);
474
475	int err = 0;
476	int dataflags;
477	struct fuse_dispatcher fdi;
478
479	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
480
481	dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
482
483	/* Note that we are not bailing out on a dead file system just yet. */
484
485	if (!(dataflags & FSESS_INITED)) {
486		if (!vnode_isvroot(vp)) {
487			fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
488			err = ENOTCONN;
489			debug_printf("fuse_getattr b: returning ENOTCONN\n");
490			return err;
491		} else {
492			goto fake;
493		}
494	}
495	fdisp_init(&fdi, 0);
496	if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
497		if ((err == ENOTCONN) && vnode_isvroot(vp)) {
498			/* see comment at similar place in fuse_statfs() */
499			fdisp_destroy(&fdi);
500			goto fake;
501		}
502		if (err == ENOENT) {
503			fuse_internal_vnode_disappear(vp);
504		}
505		goto out;
506	}
507	cache_attrs(vp, (struct fuse_attr_out *)fdi.answ);
508	if (vap != VTOVA(vp)) {
509		memcpy(vap, VTOVA(vp), sizeof(*vap));
510	}
511	if (vap->va_type != vnode_vtype(vp)) {
512		fuse_internal_vnode_disappear(vp);
513		err = ENOENT;
514		goto out;
515	}
516	if ((fvdat->flag & FN_SIZECHANGE) != 0)
517		vap->va_size = fvdat->filesize;
518
519	if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
520		/*
521	         * This is for those cases when the file size changed without us
522	         * knowing, and we want to catch up.
523	         */
524		off_t new_filesize = ((struct fuse_attr_out *)
525				      fdi.answ)->attr.size;
526
527		if (fvdat->filesize != new_filesize) {
528			fuse_vnode_setsize(vp, cred, new_filesize);
529		}
530	}
531	debug_printf("fuse_getattr e: returning 0\n");
532
533out:
534	fdisp_destroy(&fdi);
535	return err;
536
537fake:
538	bzero(vap, sizeof(*vap));
539	vap->va_type = vnode_vtype(vp);
540
541	return 0;
542}
543
544/*
545    struct vnop_inactive_args {
546	struct vnode *a_vp;
547	struct thread *a_td;
548    };
549*/
550static int
551fuse_vnop_inactive(struct vop_inactive_args *ap)
552{
553	struct vnode *vp = ap->a_vp;
554	struct thread *td = ap->a_td;
555
556	struct fuse_vnode_data *fvdat = VTOFUD(vp);
557	struct fuse_filehandle *fufh = NULL;
558
559	int type, need_flush = 1;
560
561	FS_DEBUG("inode=%ju\n", (uintmax_t)VTOI(vp));
562
563	for (type = 0; type < FUFH_MAXTYPE; type++) {
564		fufh = &(fvdat->fufh[type]);
565		if (FUFH_IS_VALID(fufh)) {
566			if (need_flush && vp->v_type == VREG) {
567				if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
568					fuse_vnode_savesize(vp, NULL);
569				}
570				if (fuse_data_cache_invalidate ||
571				    (fvdat->flag & FN_REVOKED) != 0)
572					fuse_io_invalbuf(vp, td);
573				else
574					fuse_io_flushbuf(vp, MNT_WAIT, td);
575				need_flush = 0;
576			}
577			fuse_filehandle_close(vp, type, td, NULL);
578		}
579	}
580
581	if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
582		vrecycle(vp);
583	}
584	return 0;
585}
586
587/*
588    struct vnop_link_args {
589	struct vnode *a_tdvp;
590	struct vnode *a_vp;
591	struct componentname *a_cnp;
592    };
593*/
594static int
595fuse_vnop_link(struct vop_link_args *ap)
596{
597	struct vnode *vp = ap->a_vp;
598	struct vnode *tdvp = ap->a_tdvp;
599	struct componentname *cnp = ap->a_cnp;
600
601	struct vattr *vap = VTOVA(vp);
602
603	struct fuse_dispatcher fdi;
604	struct fuse_entry_out *feo;
605	struct fuse_link_in fli;
606
607	int err;
608
609	fuse_trace_printf_vnop();
610
611	if (fuse_isdeadfs(vp)) {
612		return ENXIO;
613	}
614	if (vnode_mount(tdvp) != vnode_mount(vp)) {
615		return EXDEV;
616	}
617	if (vap->va_nlink >= FUSE_LINK_MAX) {
618		return EMLINK;
619	}
620	fli.oldnodeid = VTOI(vp);
621
622	fdisp_init(&fdi, 0);
623	fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
624	    FUSE_LINK, &fli, sizeof(fli), &fdi);
625	if ((err = fdisp_wait_answ(&fdi))) {
626		goto out;
627	}
628	feo = fdi.answ;
629
630	err = fuse_internal_checkentry(feo, vnode_vtype(vp));
631out:
632	fdisp_destroy(&fdi);
633	return err;
634}
635
636/*
637    struct vnop_lookup_args {
638	struct vnodeop_desc *a_desc;
639	struct vnode *a_dvp;
640	struct vnode **a_vpp;
641	struct componentname *a_cnp;
642    };
643*/
644int
645fuse_vnop_lookup(struct vop_lookup_args *ap)
646{
647	struct vnode *dvp = ap->a_dvp;
648	struct vnode **vpp = ap->a_vpp;
649	struct componentname *cnp = ap->a_cnp;
650	struct thread *td = cnp->cn_thread;
651	struct ucred *cred = cnp->cn_cred;
652
653	int nameiop = cnp->cn_nameiop;
654	int flags = cnp->cn_flags;
655	int wantparent = flags & (LOCKPARENT | WANTPARENT);
656	int islastcn = flags & ISLASTCN;
657	struct mount *mp = vnode_mount(dvp);
658
659	int err = 0;
660	int lookup_err = 0;
661	struct vnode *vp = NULL;
662
663	struct fuse_dispatcher fdi;
664	enum fuse_opcode op;
665
666	uint64_t nid;
667	struct fuse_access_param facp;
668
669	FS_DEBUG2G("parent_inode=%ju - %*s\n",
670	    (uintmax_t)VTOI(dvp), (int)cnp->cn_namelen, cnp->cn_nameptr);
671
672	if (fuse_isdeadfs(dvp)) {
673		*vpp = NULL;
674		return ENXIO;
675	}
676	if (!vnode_isdir(dvp)) {
677		return ENOTDIR;
678	}
679	if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
680		return EROFS;
681	}
682	/*
683         * We do access check prior to doing anything else only in the case
684         * when we are at fs root (we'd like to say, "we are at the first
685         * component", but that's not exactly the same... nevermind).
686         * See further comments at further access checks.
687         */
688
689	bzero(&facp, sizeof(facp));
690	if (vnode_isvroot(dvp)) {	/* early permission check hack */
691		if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
692			return err;
693		}
694	}
695	if (flags & ISDOTDOT) {
696		nid = VTOFUD(dvp)->parent_nid;
697		if (nid == 0) {
698			return ENOENT;
699		}
700		fdisp_init(&fdi, 0);
701		op = FUSE_GETATTR;
702		goto calldaemon;
703	} else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
704		nid = VTOI(dvp);
705		fdisp_init(&fdi, 0);
706		op = FUSE_GETATTR;
707		goto calldaemon;
708	} else if (fuse_lookup_cache_enable) {
709		err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
710		switch (err) {
711
712		case -1:		/* positive match */
713			atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
714			return 0;
715
716		case 0:		/* no match in cache */
717			atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
718			break;
719
720		case ENOENT:		/* negative match */
721			/* fall through */
722		default:
723			return err;
724		}
725	}
726	nid = VTOI(dvp);
727	fdisp_init(&fdi, cnp->cn_namelen + 1);
728	op = FUSE_LOOKUP;
729
730calldaemon:
731	fdisp_make(&fdi, op, mp, nid, td, cred);
732
733	if (op == FUSE_LOOKUP) {
734		memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
735		((char *)fdi.indata)[cnp->cn_namelen] = '\0';
736	}
737	lookup_err = fdisp_wait_answ(&fdi);
738
739	if ((op == FUSE_LOOKUP) && !lookup_err) {	/* lookup call succeeded */
740		nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
741		if (!nid) {
742			/*
743	                 * zero nodeid is the same as "not found",
744	                 * but it's also cacheable (which we keep
745	                 * keep on doing not as of writing this)
746	                 */
747			lookup_err = ENOENT;
748		} else if (nid == FUSE_ROOT_ID) {
749			lookup_err = EINVAL;
750		}
751	}
752	if (lookup_err &&
753	    (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
754		fdisp_destroy(&fdi);
755		return lookup_err;
756	}
757	/* lookup_err, if non-zero, must be ENOENT at this point */
758
759	if (lookup_err) {
760
761		if ((nameiop == CREATE || nameiop == RENAME) && islastcn
762		     /* && directory dvp has not been removed */ ) {
763
764			if (vfs_isrdonly(mp)) {
765				err = EROFS;
766				goto out;
767			}
768#if 0 /* THINK_ABOUT_THIS */
769			if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
770				goto out;
771			}
772#endif
773
774			/*
775	                 * Possibly record the position of a slot in the
776	                 * directory large enough for the new component name.
777	                 * This can be recorded in the vnode private data for
778	                 * dvp. Set the SAVENAME flag to hold onto the
779	                 * pathname for use later in VOP_CREATE or VOP_RENAME.
780	                 */
781			cnp->cn_flags |= SAVENAME;
782
783			err = EJUSTRETURN;
784			goto out;
785		}
786		/* Consider inserting name into cache. */
787
788		/*
789	         * No we can't use negative caching, as the fs
790	         * changes are out of our control.
791	         * False positives' falseness turns out just as things
792	         * go by, but false negatives' falseness doesn't.
793	         * (and aiding the caching mechanism with extra control
794	         * mechanisms comes quite close to beating the whole purpose
795	         * caching...)
796	         */
797#if 0
798		if ((cnp->cn_flags & MAKEENTRY) != 0) {
799			FS_DEBUG("inserting NULL into cache\n");
800			cache_enter(dvp, NULL, cnp);
801		}
802#endif
803		err = ENOENT;
804		goto out;
805
806	} else {
807
808		/* !lookup_err */
809
810		struct fuse_entry_out *feo = NULL;
811		struct fuse_attr *fattr = NULL;
812
813		if (op == FUSE_GETATTR) {
814			fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
815		} else {
816			feo = (struct fuse_entry_out *)fdi.answ;
817			fattr = &(feo->attr);
818		}
819
820		/*
821	         * If deleting, and at end of pathname, return parameters
822	         * which can be used to remove file.  If the wantparent flag
823	         * isn't set, we return only the directory, otherwise we go on
824	         * and lock the inode, being careful with ".".
825	         */
826		if (nameiop == DELETE && islastcn) {
827			/*
828	                 * Check for write access on directory.
829	                 */
830			facp.xuid = fattr->uid;
831			facp.facc_flags |= FACCESS_STICKY;
832			err = fuse_internal_access(dvp, VWRITE, &facp, td, cred);
833			facp.facc_flags &= ~FACCESS_XQUERIES;
834
835			if (err) {
836				goto out;
837			}
838			if (nid == VTOI(dvp)) {
839				vref(dvp);
840				*vpp = dvp;
841			} else {
842				err = fuse_vnode_get(dvp->v_mount, nid, dvp,
843				    &vp, cnp, IFTOVT(fattr->mode));
844				if (err)
845					goto out;
846				*vpp = vp;
847			}
848
849			/*
850			 * Save the name for use in VOP_RMDIR and VOP_REMOVE
851			 * later.
852			 */
853			cnp->cn_flags |= SAVENAME;
854			goto out;
855
856		}
857		/*
858	         * If rewriting (RENAME), return the inode and the
859	         * information required to rewrite the present directory
860	         * Must get inode of directory entry to verify it's a
861	         * regular file, or empty directory.
862	         */
863		if (nameiop == RENAME && wantparent && islastcn) {
864
865#if 0 /* THINK_ABOUT_THIS */
866			if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
867				goto out;
868			}
869#endif
870
871			/*
872	                 * Check for "."
873	                 */
874			if (nid == VTOI(dvp)) {
875				err = EISDIR;
876				goto out;
877			}
878			err = fuse_vnode_get(vnode_mount(dvp),
879			    nid,
880			    dvp,
881			    &vp,
882			    cnp,
883			    IFTOVT(fattr->mode));
884			if (err) {
885				goto out;
886			}
887			*vpp = vp;
888			/*
889	                 * Save the name for use in VOP_RENAME later.
890	                 */
891			cnp->cn_flags |= SAVENAME;
892
893			goto out;
894		}
895		if (flags & ISDOTDOT) {
896			struct mount *mp;
897			int ltype;
898
899			/*
900			 * Expanded copy of vn_vget_ino() so that
901			 * fuse_vnode_get() can be used.
902			 */
903			mp = dvp->v_mount;
904			ltype = VOP_ISLOCKED(dvp);
905			err = vfs_busy(mp, MBF_NOWAIT);
906			if (err != 0) {
907				vfs_ref(mp);
908				VOP_UNLOCK(dvp, 0);
909				err = vfs_busy(mp, 0);
910				vn_lock(dvp, ltype | LK_RETRY);
911				vfs_rel(mp);
912				if (err)
913					goto out;
914				if ((dvp->v_iflag & VI_DOOMED) != 0) {
915					err = ENOENT;
916					vfs_unbusy(mp);
917					goto out;
918				}
919			}
920			VOP_UNLOCK(dvp, 0);
921			err = fuse_vnode_get(vnode_mount(dvp),
922			    nid,
923			    NULL,
924			    &vp,
925			    cnp,
926			    IFTOVT(fattr->mode));
927			vfs_unbusy(mp);
928			vn_lock(dvp, ltype | LK_RETRY);
929			if ((dvp->v_iflag & VI_DOOMED) != 0) {
930				if (err == 0)
931					vput(vp);
932				err = ENOENT;
933			}
934			if (err)
935				goto out;
936			*vpp = vp;
937		} else if (nid == VTOI(dvp)) {
938			vref(dvp);
939			*vpp = dvp;
940		} else {
941			err = fuse_vnode_get(vnode_mount(dvp),
942			    nid,
943			    dvp,
944			    &vp,
945			    cnp,
946			    IFTOVT(fattr->mode));
947			if (err) {
948				goto out;
949			}
950			fuse_vnode_setparent(vp, dvp);
951			*vpp = vp;
952		}
953
954		if (op == FUSE_GETATTR) {
955			cache_attrs(*vpp, (struct fuse_attr_out *)fdi.answ);
956		} else {
957			cache_attrs(*vpp, (struct fuse_entry_out *)fdi.answ);
958		}
959
960		/* Insert name into cache if appropriate. */
961
962		/*
963	         * Nooo, caching is evil. With caching, we can't avoid stale
964	         * information taking over the playground (cached info is not
965	         * just positive/negative, it does have qualitative aspects,
966	         * too). And a (VOP/FUSE)_GETATTR is always thrown anyway, when
967	         * walking down along cached path components, and that's not
968	         * any cheaper than FUSE_LOOKUP. This might change with
969	         * implementing kernel side attr caching, but... In Linux,
970	         * lookup results are not cached, and the daemon is bombarded
971	         * with FUSE_LOOKUPS on and on. This shows that by design, the
972	         * daemon is expected to handle frequent lookup queries
973	         * efficiently, do its caching in userspace, and so on.
974	         *
975	         * So just leave the name cache alone.
976	         */
977
978		/*
979	         * Well, now I know, Linux caches lookups, but with a
980	         * timeout... So it's the same thing as attribute caching:
981	         * we can deal with it when implement timeouts.
982	         */
983#if 0
984		if (cnp->cn_flags & MAKEENTRY) {
985			cache_enter(dvp, *vpp, cnp);
986		}
987#endif
988	}
989out:
990	if (!lookup_err) {
991
992		/* No lookup error; need to clean up. */
993
994		if (err) {		/* Found inode; exit with no vnode. */
995			if (op == FUSE_LOOKUP) {
996				fuse_internal_forget_send(vnode_mount(dvp), td, cred,
997				    nid, 1);
998			}
999			fdisp_destroy(&fdi);
1000			return err;
1001		} else {
1002#ifndef NO_EARLY_PERM_CHECK_HACK
1003			if (!islastcn) {
1004				/*
1005				 * We have the attributes of the next item
1006				 * *now*, and it's a fact, and we do not
1007				 * have to do extra work for it (ie, beg the
1008				 * daemon), and it neither depends on such
1009				 * accidental things like attr caching. So
1010				 * the big idea: check credentials *now*,
1011				 * not at the beginning of the next call to
1012				 * lookup.
1013				 *
1014				 * The first item of the lookup chain (fs root)
1015				 * won't be checked then here, of course, as
1016				 * its never "the next". But go and see that
1017				 * the root is taken care about at the very
1018				 * beginning of this function.
1019				 *
1020				 * Now, given we want to do the access check
1021				 * this way, one might ask: so then why not
1022				 * do the access check just after fetching
1023				 * the inode and its attributes from the
1024				 * daemon? Why bother with producing the
1025				 * corresponding vnode at all if something
1026				 * is not OK? We know what's the deal as
1027				 * soon as we get those attrs... There is
1028				 * one bit of info though not given us by
1029				 * the daemon: whether his response is
1030				 * authorative or not... His response should
1031				 * be ignored if something is mounted over
1032				 * the dir in question. But that can be
1033				 * known only by having the vnode...
1034				 */
1035				int tmpvtype = vnode_vtype(*vpp);
1036
1037				bzero(&facp, sizeof(facp));
1038				/*the early perm check hack */
1039				    facp.facc_flags |= FACCESS_VA_VALID;
1040
1041				if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
1042					err = ENOTDIR;
1043				}
1044				if (!err && !vnode_mountedhere(*vpp)) {
1045					err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
1046				}
1047				if (err) {
1048					if (tmpvtype == VLNK)
1049						FS_DEBUG("weird, permission error with a symlink?\n");
1050					vput(*vpp);
1051					*vpp = NULL;
1052				}
1053			}
1054#endif
1055		}
1056	}
1057	fdisp_destroy(&fdi);
1058
1059	return err;
1060}
1061
1062/*
1063    struct vnop_mkdir_args {
1064	struct vnode *a_dvp;
1065	struct vnode **a_vpp;
1066	struct componentname *a_cnp;
1067	struct vattr *a_vap;
1068    };
1069*/
1070static int
1071fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1072{
1073	struct vnode *dvp = ap->a_dvp;
1074	struct vnode **vpp = ap->a_vpp;
1075	struct componentname *cnp = ap->a_cnp;
1076	struct vattr *vap = ap->a_vap;
1077
1078	struct fuse_mkdir_in fmdi;
1079
1080	fuse_trace_printf_vnop();
1081
1082	if (fuse_isdeadfs(dvp)) {
1083		return ENXIO;
1084	}
1085	fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1086
1087	return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1088	    sizeof(fmdi), VDIR));
1089}
1090
1091/*
1092    struct vnop_mknod_args {
1093	struct vnode *a_dvp;
1094	struct vnode **a_vpp;
1095	struct componentname *a_cnp;
1096	struct vattr *a_vap;
1097    };
1098*/
1099static int
1100fuse_vnop_mknod(struct vop_mknod_args *ap)
1101{
1102
1103	return (EINVAL);
1104}
1105
1106
1107/*
1108    struct vnop_open_args {
1109	struct vnode *a_vp;
1110	int  a_mode;
1111	struct ucred *a_cred;
1112	struct thread *a_td;
1113	int a_fdidx; / struct file *a_fp;
1114    };
1115*/
1116static int
1117fuse_vnop_open(struct vop_open_args *ap)
1118{
1119	struct vnode *vp = ap->a_vp;
1120	int mode = ap->a_mode;
1121	struct thread *td = ap->a_td;
1122	struct ucred *cred = ap->a_cred;
1123
1124	fufh_type_t fufh_type;
1125	struct fuse_vnode_data *fvdat;
1126
1127	int error, isdir = 0;
1128	int32_t fuse_open_flags;
1129
1130	FS_DEBUG2G("inode=%ju mode=0x%x\n", (uintmax_t)VTOI(vp), mode);
1131
1132	if (fuse_isdeadfs(vp)) {
1133		return ENXIO;
1134	}
1135	fvdat = VTOFUD(vp);
1136
1137	if (vnode_isdir(vp)) {
1138		isdir = 1;
1139	}
1140	fuse_open_flags = 0;
1141	if (isdir) {
1142		fufh_type = FUFH_RDONLY;
1143	} else {
1144		fufh_type = fuse_filehandle_xlate_from_fflags(mode);
1145		/*
1146		 * For WRONLY opens, force DIRECT_IO.  This is necessary
1147		 * since writing a partial block through the buffer cache
1148		 * will result in a read of the block and that read won't
1149		 * be allowed by the WRONLY open.
1150		 */
1151		if (fufh_type == FUFH_WRONLY ||
1152		    (fvdat->flag & FN_DIRECTIO) != 0)
1153			fuse_open_flags = FOPEN_DIRECT_IO;
1154	}
1155
1156	if (fuse_filehandle_validrw(vp, fufh_type) != FUFH_INVALID) {
1157		fuse_vnode_open(vp, fuse_open_flags, td);
1158		return 0;
1159	}
1160	error = fuse_filehandle_open(vp, fufh_type, NULL, td, cred);
1161
1162	return error;
1163}
1164
1165/*
1166    struct vnop_read_args {
1167	struct vnode *a_vp;
1168	struct uio *a_uio;
1169	int  a_ioflag;
1170	struct ucred *a_cred;
1171    };
1172*/
1173static int
1174fuse_vnop_read(struct vop_read_args *ap)
1175{
1176	struct vnode *vp = ap->a_vp;
1177	struct uio *uio = ap->a_uio;
1178	int ioflag = ap->a_ioflag;
1179	struct ucred *cred = ap->a_cred;
1180
1181	FS_DEBUG2G("inode=%ju offset=%jd resid=%zd\n",
1182	    (uintmax_t)VTOI(vp), uio->uio_offset, uio->uio_resid);
1183
1184	if (fuse_isdeadfs(vp)) {
1185		return ENXIO;
1186	}
1187
1188	if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1189		ioflag |= IO_DIRECT;
1190	}
1191
1192	return fuse_io_dispatch(vp, uio, ioflag, cred);
1193}
1194
1195/*
1196    struct vnop_readdir_args {
1197	struct vnode *a_vp;
1198	struct uio *a_uio;
1199	struct ucred *a_cred;
1200	int *a_eofflag;
1201	int *ncookies;
1202	u_long **a_cookies;
1203    };
1204*/
1205static int
1206fuse_vnop_readdir(struct vop_readdir_args *ap)
1207{
1208	struct vnode *vp = ap->a_vp;
1209	struct uio *uio = ap->a_uio;
1210	struct ucred *cred = ap->a_cred;
1211
1212	struct fuse_filehandle *fufh = NULL;
1213	struct fuse_vnode_data *fvdat;
1214	struct fuse_iov cookediov;
1215
1216	int err = 0;
1217	int freefufh = 0;
1218
1219	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1220
1221	if (fuse_isdeadfs(vp)) {
1222		return ENXIO;
1223	}
1224	if (				/* XXXIP ((uio_iovcnt(uio) > 1)) || */
1225	    (uio_resid(uio) < sizeof(struct dirent))) {
1226		return EINVAL;
1227	}
1228	fvdat = VTOFUD(vp);
1229
1230	if (!fuse_filehandle_valid(vp, FUFH_RDONLY)) {
1231		FS_DEBUG("calling readdir() before open()");
1232		err = fuse_filehandle_open(vp, FUFH_RDONLY, &fufh, NULL, cred);
1233		freefufh = 1;
1234	} else {
1235		err = fuse_filehandle_get(vp, FUFH_RDONLY, &fufh);
1236	}
1237	if (err) {
1238		return (err);
1239	}
1240#define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1241	fiov_init(&cookediov, DIRCOOKEDSIZE);
1242
1243	err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1244
1245	fiov_teardown(&cookediov);
1246	if (freefufh) {
1247		fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
1248	}
1249	return err;
1250}
1251
1252/*
1253    struct vnop_readlink_args {
1254	struct vnode *a_vp;
1255	struct uio *a_uio;
1256	struct ucred *a_cred;
1257    };
1258*/
1259static int
1260fuse_vnop_readlink(struct vop_readlink_args *ap)
1261{
1262	struct vnode *vp = ap->a_vp;
1263	struct uio *uio = ap->a_uio;
1264	struct ucred *cred = ap->a_cred;
1265
1266	struct fuse_dispatcher fdi;
1267	int err;
1268
1269	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1270
1271	if (fuse_isdeadfs(vp)) {
1272		return ENXIO;
1273	}
1274	if (!vnode_islnk(vp)) {
1275		return EINVAL;
1276	}
1277	fdisp_init(&fdi, 0);
1278	err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1279	if (err) {
1280		goto out;
1281	}
1282	if (((char *)fdi.answ)[0] == '/' &&
1283	    fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1284		char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1285
1286		err = uiomove(mpth, strlen(mpth), uio);
1287	}
1288	if (!err) {
1289		err = uiomove(fdi.answ, fdi.iosize, uio);
1290	}
1291out:
1292	fdisp_destroy(&fdi);
1293	return err;
1294}
1295
1296/*
1297    struct vnop_reclaim_args {
1298	struct vnode *a_vp;
1299	struct thread *a_td;
1300    };
1301*/
1302static int
1303fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1304{
1305	struct vnode *vp = ap->a_vp;
1306	struct thread *td = ap->a_td;
1307
1308	struct fuse_vnode_data *fvdat = VTOFUD(vp);
1309	struct fuse_filehandle *fufh = NULL;
1310
1311	int type;
1312
1313	if (!fvdat) {
1314		panic("FUSE: no vnode data during recycling");
1315	}
1316	FS_DEBUG("inode=%ju\n", (uintmax_t)VTOI(vp));
1317
1318	for (type = 0; type < FUFH_MAXTYPE; type++) {
1319		fufh = &(fvdat->fufh[type]);
1320		if (FUFH_IS_VALID(fufh)) {
1321			printf("FUSE: vnode being reclaimed but fufh (type=%d) is valid",
1322			    type);
1323			fuse_filehandle_close(vp, type, td, NULL);
1324		}
1325	}
1326
1327	if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1328		fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1329		    fvdat->nlookup);
1330	}
1331	fuse_vnode_setparent(vp, NULL);
1332	cache_purge(vp);
1333	vfs_hash_remove(vp);
1334	vnode_destroy_vobject(vp);
1335	fuse_vnode_destroy(vp);
1336
1337	return 0;
1338}
1339
1340/*
1341    struct vnop_remove_args {
1342	struct vnode *a_dvp;
1343	struct vnode *a_vp;
1344	struct componentname *a_cnp;
1345    };
1346*/
1347static int
1348fuse_vnop_remove(struct vop_remove_args *ap)
1349{
1350	struct vnode *dvp = ap->a_dvp;
1351	struct vnode *vp = ap->a_vp;
1352	struct componentname *cnp = ap->a_cnp;
1353
1354	int err;
1355
1356	FS_DEBUG2G("inode=%ju name=%*s\n",
1357	    (uintmax_t)VTOI(vp), (int)cnp->cn_namelen, cnp->cn_nameptr);
1358
1359	if (fuse_isdeadfs(vp)) {
1360		return ENXIO;
1361	}
1362	if (vnode_isdir(vp)) {
1363		return EPERM;
1364	}
1365	cache_purge(vp);
1366
1367	err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1368
1369	if (err == 0)
1370		fuse_internal_vnode_disappear(vp);
1371	return err;
1372}
1373
1374/*
1375    struct vnop_rename_args {
1376	struct vnode *a_fdvp;
1377	struct vnode *a_fvp;
1378	struct componentname *a_fcnp;
1379	struct vnode *a_tdvp;
1380	struct vnode *a_tvp;
1381	struct componentname *a_tcnp;
1382    };
1383*/
1384static int
1385fuse_vnop_rename(struct vop_rename_args *ap)
1386{
1387	struct vnode *fdvp = ap->a_fdvp;
1388	struct vnode *fvp = ap->a_fvp;
1389	struct componentname *fcnp = ap->a_fcnp;
1390	struct vnode *tdvp = ap->a_tdvp;
1391	struct vnode *tvp = ap->a_tvp;
1392	struct componentname *tcnp = ap->a_tcnp;
1393	struct fuse_data *data;
1394
1395	int err = 0;
1396
1397	FS_DEBUG2G("from: inode=%ju name=%*s -> to: inode=%ju name=%*s\n",
1398	    (uintmax_t)VTOI(fvp), (int)fcnp->cn_namelen, fcnp->cn_nameptr,
1399	    (uintmax_t)(tvp == NULL ? -1 : VTOI(tvp)),
1400	    (int)tcnp->cn_namelen, tcnp->cn_nameptr);
1401
1402	if (fuse_isdeadfs(fdvp)) {
1403		return ENXIO;
1404	}
1405	if (fvp->v_mount != tdvp->v_mount ||
1406	    (tvp && fvp->v_mount != tvp->v_mount)) {
1407		FS_DEBUG("cross-device rename: %s -> %s\n",
1408		    fcnp->cn_nameptr, (tcnp != NULL ? tcnp->cn_nameptr : "(NULL)"));
1409		err = EXDEV;
1410		goto out;
1411	}
1412	cache_purge(fvp);
1413
1414	/*
1415         * FUSE library is expected to check if target directory is not
1416         * under the source directory in the file system tree.
1417         * Linux performs this check at VFS level.
1418         */
1419	data = fuse_get_mpdata(vnode_mount(tdvp));
1420	sx_xlock(&data->rename_lock);
1421	err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1422	if (err == 0) {
1423		if (tdvp != fdvp)
1424			fuse_vnode_setparent(fvp, tdvp);
1425		if (tvp != NULL)
1426			fuse_vnode_setparent(tvp, NULL);
1427	}
1428	sx_unlock(&data->rename_lock);
1429
1430	if (tvp != NULL && tvp != fvp) {
1431		cache_purge(tvp);
1432	}
1433	if (vnode_isdir(fvp)) {
1434		if ((tvp != NULL) && vnode_isdir(tvp)) {
1435			cache_purge(tdvp);
1436		}
1437		cache_purge(fdvp);
1438	}
1439out:
1440	if (tdvp == tvp) {
1441		vrele(tdvp);
1442	} else {
1443		vput(tdvp);
1444	}
1445	if (tvp != NULL) {
1446		vput(tvp);
1447	}
1448	vrele(fdvp);
1449	vrele(fvp);
1450
1451	return err;
1452}
1453
1454/*
1455    struct vnop_rmdir_args {
1456	    struct vnode *a_dvp;
1457	    struct vnode *a_vp;
1458	    struct componentname *a_cnp;
1459    } *ap;
1460*/
1461static int
1462fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1463{
1464	struct vnode *dvp = ap->a_dvp;
1465	struct vnode *vp = ap->a_vp;
1466
1467	int err;
1468
1469	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1470
1471	if (fuse_isdeadfs(vp)) {
1472		return ENXIO;
1473	}
1474	if (VTOFUD(vp) == VTOFUD(dvp)) {
1475		return EINVAL;
1476	}
1477	err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1478
1479	if (err == 0)
1480		fuse_internal_vnode_disappear(vp);
1481	return err;
1482}
1483
1484/*
1485    struct vnop_setattr_args {
1486	struct vnode *a_vp;
1487	struct vattr *a_vap;
1488	struct ucred *a_cred;
1489	struct thread *a_td;
1490    };
1491*/
1492static int
1493fuse_vnop_setattr(struct vop_setattr_args *ap)
1494{
1495	struct vnode *vp = ap->a_vp;
1496	struct vattr *vap = ap->a_vap;
1497	struct ucred *cred = ap->a_cred;
1498	struct thread *td = curthread;
1499
1500	struct fuse_dispatcher fdi;
1501	struct fuse_setattr_in *fsai;
1502	struct fuse_access_param facp;
1503
1504	int err = 0;
1505	enum vtype vtyp;
1506	int sizechanged = 0;
1507	uint64_t newsize = 0;
1508
1509	FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1510
1511	if (fuse_isdeadfs(vp)) {
1512		return ENXIO;
1513	}
1514	fdisp_init(&fdi, sizeof(*fsai));
1515	fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1516	fsai = fdi.indata;
1517	fsai->valid = 0;
1518
1519	bzero(&facp, sizeof(facp));
1520
1521	facp.xuid = vap->va_uid;
1522	facp.xgid = vap->va_gid;
1523
1524	if (vap->va_uid != (uid_t)VNOVAL) {
1525		facp.facc_flags |= FACCESS_CHOWN;
1526		fsai->uid = vap->va_uid;
1527		fsai->valid |= FATTR_UID;
1528	}
1529	if (vap->va_gid != (gid_t)VNOVAL) {
1530		facp.facc_flags |= FACCESS_CHOWN;
1531		fsai->gid = vap->va_gid;
1532		fsai->valid |= FATTR_GID;
1533	}
1534	if (vap->va_size != VNOVAL) {
1535
1536		struct fuse_filehandle *fufh = NULL;
1537
1538		/*Truncate to a new value. */
1539		    fsai->size = vap->va_size;
1540		sizechanged = 1;
1541		newsize = vap->va_size;
1542		fsai->valid |= FATTR_SIZE;
1543
1544		fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh);
1545		if (fufh) {
1546			fsai->fh = fufh->fh_id;
1547			fsai->valid |= FATTR_FH;
1548		}
1549	}
1550	if (vap->va_atime.tv_sec != VNOVAL) {
1551		fsai->atime = vap->va_atime.tv_sec;
1552		fsai->atimensec = vap->va_atime.tv_nsec;
1553		fsai->valid |= FATTR_ATIME;
1554	}
1555	if (vap->va_mtime.tv_sec != VNOVAL) {
1556		fsai->mtime = vap->va_mtime.tv_sec;
1557		fsai->mtimensec = vap->va_mtime.tv_nsec;
1558		fsai->valid |= FATTR_MTIME;
1559	}
1560	if (vap->va_mode != (mode_t)VNOVAL) {
1561		fsai->mode = vap->va_mode & ALLPERMS;
1562		fsai->valid |= FATTR_MODE;
1563	}
1564	if (!fsai->valid) {
1565		goto out;
1566	}
1567	vtyp = vnode_vtype(vp);
1568
1569	if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1570		err = EISDIR;
1571		goto out;
1572	}
1573	if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1574		err = EROFS;
1575		goto out;
1576	}
1577	if (fsai->valid & ~FATTR_SIZE) {
1578	  /*err = fuse_internal_access(vp, VADMIN, context, &facp); */
1579	  /*XXX */
1580		    err = 0;
1581	}
1582	facp.facc_flags &= ~FACCESS_XQUERIES;
1583
1584	if (err && !(fsai->valid & ~(FATTR_ATIME | FATTR_MTIME)) &&
1585	    vap->va_vaflags & VA_UTIMES_NULL) {
1586		err = fuse_internal_access(vp, VWRITE, &facp, td, cred);
1587	}
1588	if (err)
1589		goto out;
1590	if ((err = fdisp_wait_answ(&fdi)))
1591		goto out;
1592	vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1593
1594	if (vnode_vtype(vp) != vtyp) {
1595		if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1596			debug_printf("FUSE: Dang! vnode_vtype is VNON and vtype isn't.\n");
1597		} else {
1598			/*
1599	                 * STALE vnode, ditch
1600	                 *
1601	                 * The vnode has changed its type "behind our back". There's
1602	                 * nothing really we can do, so let us just force an internal
1603	                 * revocation and tell the caller to try again, if interested.
1604	                 */
1605			fuse_internal_vnode_disappear(vp);
1606			err = EAGAIN;
1607		}
1608	}
1609	if (!err && !sizechanged) {
1610		cache_attrs(vp, (struct fuse_attr_out *)fdi.answ);
1611	}
1612out:
1613	fdisp_destroy(&fdi);
1614	if (!err && sizechanged) {
1615		fuse_vnode_setsize(vp, cred, newsize);
1616		VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1617	}
1618	return err;
1619}
1620
1621/*
1622    struct vnop_strategy_args {
1623	struct vnode *a_vp;
1624	struct buf *a_bp;
1625    };
1626*/
1627static int
1628fuse_vnop_strategy(struct vop_strategy_args *ap)
1629{
1630	struct vnode *vp = ap->a_vp;
1631	struct buf *bp = ap->a_bp;
1632
1633	fuse_trace_printf_vnop();
1634
1635	if (!vp || fuse_isdeadfs(vp)) {
1636		bp->b_ioflags |= BIO_ERROR;
1637		bp->b_error = ENXIO;
1638		bufdone(bp);
1639		return ENXIO;
1640	}
1641	if (bp->b_iocmd == BIO_WRITE)
1642		fuse_vnode_refreshsize(vp, NOCRED);
1643
1644	(void)fuse_io_strategy(vp, bp);
1645
1646	/*
1647	 * This is a dangerous function. If returns error, that might mean a
1648	 * panic. We prefer pretty much anything over being forced to panic
1649	 * by a malicious daemon (a demon?). So we just return 0 anyway. You
1650	 * should never mind this: this function has its own error
1651	 * propagation mechanism via the argument buffer, so
1652	 * not-that-melodramatic residents of the call chain still will be
1653	 * able to know what to do.
1654	 */
1655	return 0;
1656}
1657
1658
1659/*
1660    struct vnop_symlink_args {
1661	struct vnode *a_dvp;
1662	struct vnode **a_vpp;
1663	struct componentname *a_cnp;
1664	struct vattr *a_vap;
1665	char *a_target;
1666    };
1667*/
1668static int
1669fuse_vnop_symlink(struct vop_symlink_args *ap)
1670{
1671	struct vnode *dvp = ap->a_dvp;
1672	struct vnode **vpp = ap->a_vpp;
1673	struct componentname *cnp = ap->a_cnp;
1674	char *target = ap->a_target;
1675
1676	struct fuse_dispatcher fdi;
1677
1678	int err;
1679	size_t len;
1680
1681	FS_DEBUG2G("inode=%ju name=%*s\n",
1682	    (uintmax_t)VTOI(dvp), (int)cnp->cn_namelen, cnp->cn_nameptr);
1683
1684	if (fuse_isdeadfs(dvp)) {
1685		return ENXIO;
1686	}
1687	/*
1688         * Unlike the other creator type calls, here we have to create a message
1689         * where the name of the new entry comes first, and the data describing
1690         * the entry comes second.
1691         * Hence we can't rely on our handy fuse_internal_newentry() routine,
1692         * but put together the message manually and just call the core part.
1693         */
1694
1695	len = strlen(target) + 1;
1696	fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1697	fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1698
1699	memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1700	((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1701	memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1702
1703	err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1704	fdisp_destroy(&fdi);
1705	return err;
1706}
1707
1708/*
1709    struct vnop_write_args {
1710	struct vnode *a_vp;
1711	struct uio *a_uio;
1712	int  a_ioflag;
1713	struct ucred *a_cred;
1714    };
1715*/
1716static int
1717fuse_vnop_write(struct vop_write_args *ap)
1718{
1719	struct vnode *vp = ap->a_vp;
1720	struct uio *uio = ap->a_uio;
1721	int ioflag = ap->a_ioflag;
1722	struct ucred *cred = ap->a_cred;
1723
1724	fuse_trace_printf_vnop();
1725
1726	if (fuse_isdeadfs(vp)) {
1727		return ENXIO;
1728	}
1729	fuse_vnode_refreshsize(vp, cred);
1730
1731	if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1732		ioflag |= IO_DIRECT;
1733	}
1734
1735	return fuse_io_dispatch(vp, uio, ioflag, cred);
1736}
1737
1738/*
1739    struct vnop_getpages_args {
1740        struct vnode *a_vp;
1741        vm_page_t *a_m;
1742        int a_count;
1743        int a_reqpage;
1744        vm_ooffset_t a_offset;
1745    };
1746*/
1747static int
1748fuse_vnop_getpages(struct vop_getpages_args *ap)
1749{
1750	int i, error, nextoff, size, toff, count, npages;
1751	struct uio uio;
1752	struct iovec iov;
1753	vm_offset_t kva;
1754	struct buf *bp;
1755	struct vnode *vp;
1756	struct thread *td;
1757	struct ucred *cred;
1758	vm_page_t *pages;
1759
1760	FS_DEBUG2G("heh\n");
1761
1762	vp = ap->a_vp;
1763	KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1764	td = curthread;			/* XXX */
1765	cred = curthread->td_ucred;	/* XXX */
1766	pages = ap->a_m;
1767	count = ap->a_count;
1768
1769	if (!fsess_opt_mmap(vnode_mount(vp))) {
1770		FS_DEBUG("called on non-cacheable vnode??\n");
1771		return (VM_PAGER_ERROR);
1772	}
1773	npages = btoc(count);
1774
1775	/*
1776	 * If the requested page is partially valid, just return it and
1777	 * allow the pager to zero-out the blanks.  Partially valid pages
1778	 * can only occur at the file EOF.
1779	 */
1780
1781	VM_OBJECT_WLOCK(vp->v_object);
1782	fuse_vm_page_lock_queues();
1783	if (pages[ap->a_reqpage]->valid != 0) {
1784		for (i = 0; i < npages; ++i) {
1785			if (i != ap->a_reqpage) {
1786				fuse_vm_page_lock(pages[i]);
1787				vm_page_free(pages[i]);
1788				fuse_vm_page_unlock(pages[i]);
1789			}
1790		}
1791		fuse_vm_page_unlock_queues();
1792		VM_OBJECT_WUNLOCK(vp->v_object);
1793		return 0;
1794	}
1795	fuse_vm_page_unlock_queues();
1796	VM_OBJECT_WUNLOCK(vp->v_object);
1797
1798	/*
1799	 * We use only the kva address for the buffer, but this is extremely
1800	 * convienient and fast.
1801	 */
1802	bp = getpbuf(&fuse_pbuf_freecnt);
1803
1804	kva = (vm_offset_t)bp->b_data;
1805	pmap_qenter(kva, pages, npages);
1806	PCPU_INC(cnt.v_vnodein);
1807	PCPU_ADD(cnt.v_vnodepgsin, npages);
1808
1809	iov.iov_base = (caddr_t)kva;
1810	iov.iov_len = count;
1811	uio.uio_iov = &iov;
1812	uio.uio_iovcnt = 1;
1813	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1814	uio.uio_resid = count;
1815	uio.uio_segflg = UIO_SYSSPACE;
1816	uio.uio_rw = UIO_READ;
1817	uio.uio_td = td;
1818
1819	error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1820	pmap_qremove(kva, npages);
1821
1822	relpbuf(bp, &fuse_pbuf_freecnt);
1823
1824	if (error && (uio.uio_resid == count)) {
1825		FS_DEBUG("error %d\n", error);
1826		VM_OBJECT_WLOCK(vp->v_object);
1827		fuse_vm_page_lock_queues();
1828		for (i = 0; i < npages; ++i) {
1829			if (i != ap->a_reqpage) {
1830				fuse_vm_page_lock(pages[i]);
1831				vm_page_free(pages[i]);
1832				fuse_vm_page_unlock(pages[i]);
1833			}
1834		}
1835		fuse_vm_page_unlock_queues();
1836		VM_OBJECT_WUNLOCK(vp->v_object);
1837		return VM_PAGER_ERROR;
1838	}
1839	/*
1840	 * Calculate the number of bytes read and validate only that number
1841	 * of bytes.  Note that due to pending writes, size may be 0.  This
1842	 * does not mean that the remaining data is invalid!
1843	 */
1844
1845	size = count - uio.uio_resid;
1846	VM_OBJECT_WLOCK(vp->v_object);
1847	fuse_vm_page_lock_queues();
1848	for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1849		vm_page_t m;
1850
1851		nextoff = toff + PAGE_SIZE;
1852		m = pages[i];
1853
1854		if (nextoff <= size) {
1855			/*
1856			 * Read operation filled an entire page
1857			 */
1858			m->valid = VM_PAGE_BITS_ALL;
1859			KASSERT(m->dirty == 0,
1860			    ("fuse_getpages: page %p is dirty", m));
1861		} else if (size > toff) {
1862			/*
1863			 * Read operation filled a partial page.
1864			 */
1865			m->valid = 0;
1866			vm_page_set_valid_range(m, 0, size - toff);
1867			KASSERT(m->dirty == 0,
1868			    ("fuse_getpages: page %p is dirty", m));
1869		} else {
1870			/*
1871			 * Read operation was short.  If no error occured
1872			 * we may have hit a zero-fill section.   We simply
1873			 * leave valid set to 0.
1874			 */
1875			;
1876		}
1877		if (i != ap->a_reqpage)
1878			vm_page_readahead_finish(m);
1879	}
1880	fuse_vm_page_unlock_queues();
1881	VM_OBJECT_WUNLOCK(vp->v_object);
1882	return 0;
1883}
1884
1885/*
1886    struct vnop_putpages_args {
1887        struct vnode *a_vp;
1888        vm_page_t *a_m;
1889        int a_count;
1890        int a_sync;
1891        int *a_rtvals;
1892        vm_ooffset_t a_offset;
1893    };
1894*/
1895static int
1896fuse_vnop_putpages(struct vop_putpages_args *ap)
1897{
1898	struct uio uio;
1899	struct iovec iov;
1900	vm_offset_t kva;
1901	struct buf *bp;
1902	int i, error, npages, count;
1903	off_t offset;
1904	int *rtvals;
1905	struct vnode *vp;
1906	struct thread *td;
1907	struct ucred *cred;
1908	vm_page_t *pages;
1909	vm_ooffset_t fsize;
1910
1911	FS_DEBUG2G("heh\n");
1912
1913	vp = ap->a_vp;
1914	KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1915	fsize = vp->v_object->un_pager.vnp.vnp_size;
1916	td = curthread;			/* XXX */
1917	cred = curthread->td_ucred;	/* XXX */
1918	pages = ap->a_m;
1919	count = ap->a_count;
1920	rtvals = ap->a_rtvals;
1921	npages = btoc(count);
1922	offset = IDX_TO_OFF(pages[0]->pindex);
1923
1924	if (!fsess_opt_mmap(vnode_mount(vp))) {
1925		FS_DEBUG("called on non-cacheable vnode??\n");
1926	}
1927	for (i = 0; i < npages; i++)
1928		rtvals[i] = VM_PAGER_AGAIN;
1929
1930	/*
1931	 * When putting pages, do not extend file past EOF.
1932	 */
1933
1934	if (offset + count > fsize) {
1935		count = fsize - offset;
1936		if (count < 0)
1937			count = 0;
1938	}
1939	/*
1940	 * We use only the kva address for the buffer, but this is extremely
1941	 * convienient and fast.
1942	 */
1943	bp = getpbuf(&fuse_pbuf_freecnt);
1944
1945	kva = (vm_offset_t)bp->b_data;
1946	pmap_qenter(kva, pages, npages);
1947	PCPU_INC(cnt.v_vnodeout);
1948	PCPU_ADD(cnt.v_vnodepgsout, count);
1949
1950	iov.iov_base = (caddr_t)kva;
1951	iov.iov_len = count;
1952	uio.uio_iov = &iov;
1953	uio.uio_iovcnt = 1;
1954	uio.uio_offset = offset;
1955	uio.uio_resid = count;
1956	uio.uio_segflg = UIO_SYSSPACE;
1957	uio.uio_rw = UIO_WRITE;
1958	uio.uio_td = td;
1959
1960	error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1961
1962	pmap_qremove(kva, npages);
1963	relpbuf(bp, &fuse_pbuf_freecnt);
1964
1965	if (!error) {
1966		int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
1967
1968		for (i = 0; i < nwritten; i++) {
1969			rtvals[i] = VM_PAGER_OK;
1970			VM_OBJECT_WLOCK(pages[i]->object);
1971			vm_page_undirty(pages[i]);
1972			VM_OBJECT_WUNLOCK(pages[i]->object);
1973		}
1974	}
1975	return rtvals[0];
1976}
1977
1978/*
1979    struct vnop_print_args {
1980        struct vnode *a_vp;
1981    };
1982*/
1983static int
1984fuse_vnop_print(struct vop_print_args *ap)
1985{
1986	struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
1987
1988	printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
1989	    (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
1990	    (uintmax_t)fvdat->nlookup,
1991	    fvdat->flag);
1992
1993	return 0;
1994}
1995