2 * Copyright (c) 2007-2009 Google Inc. and Amit Singh
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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
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.
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.
31 * Copyright (C) 2005 Csaba Henk.
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
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.
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
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
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>
67 #include <sys/malloc.h>
68 #include <sys/queue.h>
70 #include <sys/rwlock.h>
73 #include <sys/mount.h>
74 #include <sys/vnode.h>
75 #include <sys/namei.h>
76 #include <sys/extattr.h>
78 #include <sys/unistd.h>
79 #include <sys/filedesc.h>
81 #include <sys/fcntl.h>
82 #include <sys/dirent.h>
85 #include <sys/sysctl.h>
86 #include <sys/vmmeter.h>
89 #include <vm/vm_extern.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_param.h>
94 #include <vm/vm_object.h>
95 #include <vm/vm_pager.h>
96 #include <vm/vnode_pager.h>
97 #include <vm/vm_object.h>
100 #include "fuse_file.h"
101 #include "fuse_internal.h"
102 #include "fuse_ipc.h"
103 #include "fuse_node.h"
104 #include "fuse_param.h"
107 #include <sys/priv.h>
109 #define FUSE_DEBUG_MODULE VNOPS
110 #include "fuse_debug.h"
113 static vop_access_t fuse_vnop_access;
114 static vop_close_t fuse_vnop_close;
115 static vop_create_t fuse_vnop_create;
116 static vop_deleteextattr_t fuse_vnop_deleteextattr;
117 static vop_fsync_t fuse_vnop_fsync;
118 static vop_getattr_t fuse_vnop_getattr;
119 static vop_getextattr_t fuse_vnop_getextattr;
120 static vop_inactive_t fuse_vnop_inactive;
121 static vop_link_t fuse_vnop_link;
122 static vop_listextattr_t fuse_vnop_listextattr;
123 static vop_lookup_t fuse_vnop_lookup;
124 static vop_mkdir_t fuse_vnop_mkdir;
125 static vop_mknod_t fuse_vnop_mknod;
126 static vop_open_t fuse_vnop_open;
127 static vop_read_t fuse_vnop_read;
128 static vop_readdir_t fuse_vnop_readdir;
129 static vop_readlink_t fuse_vnop_readlink;
130 static vop_reclaim_t fuse_vnop_reclaim;
131 static vop_remove_t fuse_vnop_remove;
132 static vop_rename_t fuse_vnop_rename;
133 static vop_rmdir_t fuse_vnop_rmdir;
134 static vop_setattr_t fuse_vnop_setattr;
135 static vop_setextattr_t fuse_vnop_setextattr;
136 static vop_strategy_t fuse_vnop_strategy;
137 static vop_symlink_t fuse_vnop_symlink;
138 static vop_write_t fuse_vnop_write;
139 static vop_getpages_t fuse_vnop_getpages;
140 static vop_putpages_t fuse_vnop_putpages;
141 static vop_print_t fuse_vnop_print;
143 struct vop_vector fuse_vnops = {
144 .vop_default = &default_vnodeops,
145 .vop_access = fuse_vnop_access,
146 .vop_close = fuse_vnop_close,
147 .vop_create = fuse_vnop_create,
148 .vop_deleteextattr = fuse_vnop_deleteextattr,
149 .vop_fsync = fuse_vnop_fsync,
150 .vop_getattr = fuse_vnop_getattr,
151 .vop_getextattr = fuse_vnop_getextattr,
152 .vop_inactive = fuse_vnop_inactive,
153 .vop_link = fuse_vnop_link,
154 .vop_listextattr = fuse_vnop_listextattr,
155 .vop_lookup = fuse_vnop_lookup,
156 .vop_mkdir = fuse_vnop_mkdir,
157 .vop_mknod = fuse_vnop_mknod,
158 .vop_open = fuse_vnop_open,
159 .vop_pathconf = vop_stdpathconf,
160 .vop_read = fuse_vnop_read,
161 .vop_readdir = fuse_vnop_readdir,
162 .vop_readlink = fuse_vnop_readlink,
163 .vop_reclaim = fuse_vnop_reclaim,
164 .vop_remove = fuse_vnop_remove,
165 .vop_rename = fuse_vnop_rename,
166 .vop_rmdir = fuse_vnop_rmdir,
167 .vop_setattr = fuse_vnop_setattr,
168 .vop_setextattr = fuse_vnop_setextattr,
169 .vop_strategy = fuse_vnop_strategy,
170 .vop_symlink = fuse_vnop_symlink,
171 .vop_write = fuse_vnop_write,
172 .vop_getpages = fuse_vnop_getpages,
173 .vop_putpages = fuse_vnop_putpages,
174 .vop_print = fuse_vnop_print,
177 static u_long fuse_lookup_cache_hits = 0;
179 SYSCTL_ULONG(_vfs_fuse, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
180 &fuse_lookup_cache_hits, 0, "");
182 static u_long fuse_lookup_cache_misses = 0;
184 SYSCTL_ULONG(_vfs_fuse, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
185 &fuse_lookup_cache_misses, 0, "");
187 int fuse_lookup_cache_enable = 1;
189 SYSCTL_INT(_vfs_fuse, OID_AUTO, lookup_cache_enable, CTLFLAG_RW,
190 &fuse_lookup_cache_enable, 0, "");
193 * XXX: This feature is highly experimental and can bring to instabilities,
194 * needs revisiting before to be enabled by default.
196 static int fuse_reclaim_revoked = 0;
198 SYSCTL_INT(_vfs_fuse, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
199 &fuse_reclaim_revoked, 0, "");
201 int fuse_pbuf_freecnt = -1;
203 #define fuse_vm_page_lock(m) vm_page_lock((m));
204 #define fuse_vm_page_unlock(m) vm_page_unlock((m));
205 #define fuse_vm_page_lock_queues() ((void)0)
206 #define fuse_vm_page_unlock_queues() ((void)0)
209 struct vnop_access_args {
211 #if VOP_ACCESS_TAKES_ACCMODE_T
216 struct ucred *a_cred;
221 fuse_vnop_access(struct vop_access_args *ap)
223 struct vnode *vp = ap->a_vp;
224 int accmode = ap->a_accmode;
225 struct ucred *cred = ap->a_cred;
227 struct fuse_access_param facp;
228 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
232 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
234 if (fuse_isdeadfs(vp)) {
235 if (vnode_isvroot(vp)) {
240 if (!(data->dataflags & FSESS_INITED)) {
241 if (vnode_isvroot(vp)) {
242 if (priv_check_cred(cred, PRIV_VFS_ADMIN, 0) ||
243 (fuse_match_cred(data->daemoncred, cred) == 0)) {
249 if (vnode_islnk(vp)) {
252 bzero(&facp, sizeof(facp));
254 err = fuse_internal_access(vp, accmode, &facp, ap->a_td, ap->a_cred);
255 FS_DEBUG2G("err=%d accmode=0x%x\n", err, accmode);
260 struct vnop_close_args {
263 struct ucred *a_cred;
268 fuse_vnop_close(struct vop_close_args *ap)
270 struct vnode *vp = ap->a_vp;
271 struct ucred *cred = ap->a_cred;
272 int fflag = ap->a_fflag;
273 fufh_type_t fufh_type;
275 fuse_trace_printf_vnop();
277 if (fuse_isdeadfs(vp)) {
280 if (vnode_isdir(vp)) {
281 if (fuse_filehandle_valid(vp, FUFH_RDONLY)) {
282 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
286 if (fflag & IO_NDELAY) {
289 fufh_type = fuse_filehandle_xlate_from_fflags(fflag);
291 if (!fuse_filehandle_valid(vp, fufh_type)) {
294 for (i = 0; i < FUFH_MAXTYPE; i++)
295 if (fuse_filehandle_valid(vp, i))
297 if (i == FUFH_MAXTYPE)
298 panic("FUSE: fufh type %d found to be invalid in close"
302 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
303 fuse_vnode_savesize(vp, cred);
309 struct vnop_create_args {
311 struct vnode **a_vpp;
312 struct componentname *a_cnp;
317 fuse_vnop_create(struct vop_create_args *ap)
319 struct vnode *dvp = ap->a_dvp;
320 struct vnode **vpp = ap->a_vpp;
321 struct componentname *cnp = ap->a_cnp;
322 struct vattr *vap = ap->a_vap;
323 struct thread *td = cnp->cn_thread;
324 struct ucred *cred = cnp->cn_cred;
326 struct fuse_open_in *foi;
327 struct fuse_entry_out *feo;
328 struct fuse_dispatcher fdi;
329 struct fuse_dispatcher *fdip = &fdi;
333 struct mount *mp = vnode_mount(dvp);
334 uint64_t parentnid = VTOFUD(dvp)->nid;
335 mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
337 uint32_t x_open_flags;
339 fuse_trace_printf_vnop();
341 if (fuse_isdeadfs(dvp)) {
344 bzero(&fdi, sizeof(fdi));
346 /* XXX: Will we ever want devices ? */
347 if ((vap->va_type != VREG)) {
348 printf("fuse_vnop_create: unsupported va_type %d\n",
352 debug_printf("parent nid = %ju, mode = %x\n", (uintmax_t)parentnid,
355 fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1);
356 if (!fsess_isimpl(mp, FUSE_CREATE)) {
357 debug_printf("eh, daemon doesn't implement create?\n");
360 fdisp_make(fdip, FUSE_CREATE, vnode_mount(dvp), parentnid, td, cred);
364 foi->flags = O_CREAT | O_RDWR;
366 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
368 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
370 err = fdisp_wait_answ(fdip);
374 fsess_set_notimpl(mp, FUSE_CREATE);
375 debug_printf("create: got err=%d from daemon\n", err);
381 if ((err = fuse_internal_checkentry(feo, VREG))) {
384 err = fuse_vnode_get(mp, feo->nodeid, dvp, vpp, cnp, VREG);
386 struct fuse_release_in *fri;
387 uint64_t nodeid = feo->nodeid;
388 uint64_t fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
390 fdisp_init(fdip, sizeof(*fri));
391 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
394 fri->flags = OFLAGS(mode);
395 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
396 fuse_insert_message(fdip->tick);
399 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
401 fdip->answ = feo + 1;
403 x_fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
404 x_open_flags = ((struct fuse_open_out *)(feo + 1))->open_flags;
405 fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, x_fh_id);
406 fuse_vnode_open(*vpp, x_open_flags, td);
407 cache_purge_negative(dvp);
415 * Our vnop_fsync roughly corresponds to the FUSE_FSYNC method. The Linux
416 * version of FUSE also has a FUSE_FLUSH method.
418 * On Linux, fsync() synchronizes a file's complete in-core state with that
419 * on disk. The call is not supposed to return until the system has completed
420 * that action or until an error is detected.
422 * Linux also has an fdatasync() call that is similar to fsync() but is not
423 * required to update the metadata such as access time and modification time.
427 struct vnop_fsync_args {
428 struct vnodeop_desc *a_desc;
430 struct ucred * a_cred;
432 struct thread * a_td;
436 fuse_vnop_fsync(struct vop_fsync_args *ap)
438 struct vnode *vp = ap->a_vp;
439 struct thread *td = ap->a_td;
441 struct fuse_filehandle *fufh;
442 struct fuse_vnode_data *fvdat = VTOFUD(vp);
446 fuse_trace_printf_vnop();
448 if (fuse_isdeadfs(vp)) {
451 if ((err = vop_stdfsync(ap)))
454 if (!fsess_isimpl(vnode_mount(vp),
455 (vnode_vtype(vp) == VDIR ? FUSE_FSYNCDIR : FUSE_FSYNC))) {
458 for (type = 0; type < FUFH_MAXTYPE; type++) {
459 fufh = &(fvdat->fufh[type]);
460 if (FUFH_IS_VALID(fufh)) {
461 fuse_internal_fsync(vp, td, NULL, fufh);
470 struct vnop_getattr_args {
473 struct ucred *a_cred;
478 fuse_vnop_getattr(struct vop_getattr_args *ap)
480 struct vnode *vp = ap->a_vp;
481 struct vattr *vap = ap->a_vap;
482 struct ucred *cred = ap->a_cred;
483 struct thread *td = curthread;
484 struct fuse_vnode_data *fvdat = VTOFUD(vp);
488 struct fuse_dispatcher fdi;
490 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
492 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
494 /* Note that we are not bailing out on a dead file system just yet. */
496 if (!(dataflags & FSESS_INITED)) {
497 if (!vnode_isvroot(vp)) {
498 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
500 debug_printf("fuse_getattr b: returning ENOTCONN\n");
507 if ((err = fdisp_simple_putget_vp(&fdi, FUSE_GETATTR, vp, td, cred))) {
508 if ((err == ENOTCONN) && vnode_isvroot(vp)) {
509 /* see comment at similar place in fuse_statfs() */
514 fuse_internal_vnode_disappear(vp);
518 cache_attrs(vp, (struct fuse_attr_out *)fdi.answ);
519 if (vap != VTOVA(vp)) {
520 memcpy(vap, VTOVA(vp), sizeof(*vap));
522 if (vap->va_type != vnode_vtype(vp)) {
523 fuse_internal_vnode_disappear(vp);
527 if ((fvdat->flag & FN_SIZECHANGE) != 0)
528 vap->va_size = fvdat->filesize;
530 if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
532 * This is for those cases when the file size changed without us
533 * knowing, and we want to catch up.
535 off_t new_filesize = ((struct fuse_attr_out *)
536 fdi.answ)->attr.size;
538 if (fvdat->filesize != new_filesize) {
539 fuse_vnode_setsize(vp, cred, new_filesize);
542 debug_printf("fuse_getattr e: returning 0\n");
549 bzero(vap, sizeof(*vap));
550 vap->va_type = vnode_vtype(vp);
556 struct vnop_inactive_args {
562 fuse_vnop_inactive(struct vop_inactive_args *ap)
564 struct vnode *vp = ap->a_vp;
565 struct thread *td = ap->a_td;
567 struct fuse_vnode_data *fvdat = VTOFUD(vp);
568 struct fuse_filehandle *fufh = NULL;
570 int type, need_flush = 1;
572 FS_DEBUG("inode=%ju\n", (uintmax_t)VTOI(vp));
574 for (type = 0; type < FUFH_MAXTYPE; type++) {
575 fufh = &(fvdat->fufh[type]);
576 if (FUFH_IS_VALID(fufh)) {
577 if (need_flush && vp->v_type == VREG) {
578 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
579 fuse_vnode_savesize(vp, NULL);
581 if (fuse_data_cache_invalidate ||
582 (fvdat->flag & FN_REVOKED) != 0)
583 fuse_io_invalbuf(vp, td);
585 fuse_io_flushbuf(vp, MNT_WAIT, td);
588 fuse_filehandle_close(vp, type, td, NULL);
592 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
599 struct vnop_link_args {
600 struct vnode *a_tdvp;
602 struct componentname *a_cnp;
606 fuse_vnop_link(struct vop_link_args *ap)
608 struct vnode *vp = ap->a_vp;
609 struct vnode *tdvp = ap->a_tdvp;
610 struct componentname *cnp = ap->a_cnp;
612 struct vattr *vap = VTOVA(vp);
614 struct fuse_dispatcher fdi;
615 struct fuse_entry_out *feo;
616 struct fuse_link_in fli;
620 fuse_trace_printf_vnop();
622 if (fuse_isdeadfs(vp)) {
625 if (vnode_mount(tdvp) != vnode_mount(vp)) {
628 if (vap->va_nlink >= FUSE_LINK_MAX) {
631 fli.oldnodeid = VTOI(vp);
634 fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp,
635 FUSE_LINK, &fli, sizeof(fli), &fdi);
636 if ((err = fdisp_wait_answ(&fdi))) {
641 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
648 struct vnop_lookup_args {
649 struct vnodeop_desc *a_desc;
651 struct vnode **a_vpp;
652 struct componentname *a_cnp;
656 fuse_vnop_lookup(struct vop_lookup_args *ap)
658 struct vnode *dvp = ap->a_dvp;
659 struct vnode **vpp = ap->a_vpp;
660 struct componentname *cnp = ap->a_cnp;
661 struct thread *td = cnp->cn_thread;
662 struct ucred *cred = cnp->cn_cred;
664 int nameiop = cnp->cn_nameiop;
665 int flags = cnp->cn_flags;
666 int wantparent = flags & (LOCKPARENT | WANTPARENT);
667 int islastcn = flags & ISLASTCN;
668 struct mount *mp = vnode_mount(dvp);
672 struct vnode *vp = NULL;
674 struct fuse_dispatcher fdi;
678 struct fuse_access_param facp;
680 FS_DEBUG2G("parent_inode=%ju - %*s\n",
681 (uintmax_t)VTOI(dvp), (int)cnp->cn_namelen, cnp->cn_nameptr);
683 if (fuse_isdeadfs(dvp)) {
687 if (!vnode_isdir(dvp)) {
690 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
694 * We do access check prior to doing anything else only in the case
695 * when we are at fs root (we'd like to say, "we are at the first
696 * component", but that's not exactly the same... nevermind).
697 * See further comments at further access checks.
700 bzero(&facp, sizeof(facp));
701 if (vnode_isvroot(dvp)) { /* early permission check hack */
702 if ((err = fuse_internal_access(dvp, VEXEC, &facp, td, cred))) {
706 if (flags & ISDOTDOT) {
707 nid = VTOFUD(dvp)->parent_nid;
714 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
719 } else if (fuse_lookup_cache_enable) {
720 err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
723 case -1: /* positive match */
724 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
727 case 0: /* no match in cache */
728 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
731 case ENOENT: /* negative match */
738 fdisp_init(&fdi, cnp->cn_namelen + 1);
742 fdisp_make(&fdi, op, mp, nid, td, cred);
744 if (op == FUSE_LOOKUP) {
745 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
746 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
748 lookup_err = fdisp_wait_answ(&fdi);
750 if ((op == FUSE_LOOKUP) && !lookup_err) { /* lookup call succeeded */
751 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
754 * zero nodeid is the same as "not found",
755 * but it's also cacheable (which we keep
756 * keep on doing not as of writing this)
759 } else if (nid == FUSE_ROOT_ID) {
764 (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
768 /* lookup_err, if non-zero, must be ENOENT at this point */
772 if ((nameiop == CREATE || nameiop == RENAME) && islastcn
773 /* && directory dvp has not been removed */ ) {
775 if (vfs_isrdonly(mp)) {
779 #if 0 /* THINK_ABOUT_THIS */
780 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
786 * Possibly record the position of a slot in the
787 * directory large enough for the new component name.
788 * This can be recorded in the vnode private data for
789 * dvp. Set the SAVENAME flag to hold onto the
790 * pathname for use later in VOP_CREATE or VOP_RENAME.
792 cnp->cn_flags |= SAVENAME;
797 /* Consider inserting name into cache. */
800 * No we can't use negative caching, as the fs
801 * changes are out of our control.
802 * False positives' falseness turns out just as things
803 * go by, but false negatives' falseness doesn't.
804 * (and aiding the caching mechanism with extra control
805 * mechanisms comes quite close to beating the whole purpose
809 if ((cnp->cn_flags & MAKEENTRY) != 0) {
810 FS_DEBUG("inserting NULL into cache\n");
811 cache_enter(dvp, NULL, cnp);
821 struct fuse_entry_out *feo = NULL;
822 struct fuse_attr *fattr = NULL;
824 if (op == FUSE_GETATTR) {
825 fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
827 feo = (struct fuse_entry_out *)fdi.answ;
828 fattr = &(feo->attr);
832 * If deleting, and at end of pathname, return parameters
833 * which can be used to remove file. If the wantparent flag
834 * isn't set, we return only the directory, otherwise we go on
835 * and lock the inode, being careful with ".".
837 if (nameiop == DELETE && islastcn) {
839 * Check for write access on directory.
841 facp.xuid = fattr->uid;
842 facp.facc_flags |= FACCESS_STICKY;
843 err = fuse_internal_access(dvp, VWRITE, &facp, td, cred);
844 facp.facc_flags &= ~FACCESS_XQUERIES;
849 if (nid == VTOI(dvp)) {
853 err = fuse_vnode_get(dvp->v_mount, nid, dvp,
854 &vp, cnp, IFTOVT(fattr->mode));
861 * Save the name for use in VOP_RMDIR and VOP_REMOVE
864 cnp->cn_flags |= SAVENAME;
869 * If rewriting (RENAME), return the inode and the
870 * information required to rewrite the present directory
871 * Must get inode of directory entry to verify it's a
872 * regular file, or empty directory.
874 if (nameiop == RENAME && wantparent && islastcn) {
876 #if 0 /* THINK_ABOUT_THIS */
877 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
885 if (nid == VTOI(dvp)) {
889 err = fuse_vnode_get(vnode_mount(dvp),
894 IFTOVT(fattr->mode));
900 * Save the name for use in VOP_RENAME later.
902 cnp->cn_flags |= SAVENAME;
906 if (flags & ISDOTDOT) {
911 * Expanded copy of vn_vget_ino() so that
912 * fuse_vnode_get() can be used.
915 ltype = VOP_ISLOCKED(dvp);
916 err = vfs_busy(mp, MBF_NOWAIT);
920 err = vfs_busy(mp, 0);
921 vn_lock(dvp, ltype | LK_RETRY);
925 if ((dvp->v_iflag & VI_DOOMED) != 0) {
932 err = fuse_vnode_get(vnode_mount(dvp),
937 IFTOVT(fattr->mode));
939 vn_lock(dvp, ltype | LK_RETRY);
940 if ((dvp->v_iflag & VI_DOOMED) != 0) {
948 } else if (nid == VTOI(dvp)) {
952 err = fuse_vnode_get(vnode_mount(dvp),
957 IFTOVT(fattr->mode));
961 fuse_vnode_setparent(vp, dvp);
965 if (op == FUSE_GETATTR) {
966 cache_attrs(*vpp, (struct fuse_attr_out *)fdi.answ);
968 cache_attrs(*vpp, (struct fuse_entry_out *)fdi.answ);
971 /* Insert name into cache if appropriate. */
974 * Nooo, caching is evil. With caching, we can't avoid stale
975 * information taking over the playground (cached info is not
976 * just positive/negative, it does have qualitative aspects,
977 * too). And a (VOP/FUSE)_GETATTR is always thrown anyway, when
978 * walking down along cached path components, and that's not
979 * any cheaper than FUSE_LOOKUP. This might change with
980 * implementing kernel side attr caching, but... In Linux,
981 * lookup results are not cached, and the daemon is bombarded
982 * with FUSE_LOOKUPS on and on. This shows that by design, the
983 * daemon is expected to handle frequent lookup queries
984 * efficiently, do its caching in userspace, and so on.
986 * So just leave the name cache alone.
990 * Well, now I know, Linux caches lookups, but with a
991 * timeout... So it's the same thing as attribute caching:
992 * we can deal with it when implement timeouts.
995 if (cnp->cn_flags & MAKEENTRY) {
996 cache_enter(dvp, *vpp, cnp);
1003 /* No lookup error; need to clean up. */
1005 if (err) { /* Found inode; exit with no vnode. */
1006 if (op == FUSE_LOOKUP) {
1007 fuse_internal_forget_send(vnode_mount(dvp), td, cred,
1010 fdisp_destroy(&fdi);
1013 #ifndef NO_EARLY_PERM_CHECK_HACK
1016 * We have the attributes of the next item
1017 * *now*, and it's a fact, and we do not
1018 * have to do extra work for it (ie, beg the
1019 * daemon), and it neither depends on such
1020 * accidental things like attr caching. So
1021 * the big idea: check credentials *now*,
1022 * not at the beginning of the next call to
1025 * The first item of the lookup chain (fs root)
1026 * won't be checked then here, of course, as
1027 * its never "the next". But go and see that
1028 * the root is taken care about at the very
1029 * beginning of this function.
1031 * Now, given we want to do the access check
1032 * this way, one might ask: so then why not
1033 * do the access check just after fetching
1034 * the inode and its attributes from the
1035 * daemon? Why bother with producing the
1036 * corresponding vnode at all if something
1037 * is not OK? We know what's the deal as
1038 * soon as we get those attrs... There is
1039 * one bit of info though not given us by
1040 * the daemon: whether his response is
1041 * authoritative or not... His response should
1042 * be ignored if something is mounted over
1043 * the dir in question. But that can be
1044 * known only by having the vnode...
1046 int tmpvtype = vnode_vtype(*vpp);
1048 bzero(&facp, sizeof(facp));
1049 /*the early perm check hack */
1050 facp.facc_flags |= FACCESS_VA_VALID;
1052 if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
1055 if (!err && !vnode_mountedhere(*vpp)) {
1056 err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
1059 if (tmpvtype == VLNK)
1060 FS_DEBUG("weird, permission error with a symlink?\n");
1068 fdisp_destroy(&fdi);
1074 struct vnop_mkdir_args {
1075 struct vnode *a_dvp;
1076 struct vnode **a_vpp;
1077 struct componentname *a_cnp;
1078 struct vattr *a_vap;
1082 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
1084 struct vnode *dvp = ap->a_dvp;
1085 struct vnode **vpp = ap->a_vpp;
1086 struct componentname *cnp = ap->a_cnp;
1087 struct vattr *vap = ap->a_vap;
1089 struct fuse_mkdir_in fmdi;
1091 fuse_trace_printf_vnop();
1093 if (fuse_isdeadfs(dvp)) {
1096 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1098 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1099 sizeof(fmdi), VDIR));
1103 struct vnop_mknod_args {
1104 struct vnode *a_dvp;
1105 struct vnode **a_vpp;
1106 struct componentname *a_cnp;
1107 struct vattr *a_vap;
1111 fuse_vnop_mknod(struct vop_mknod_args *ap)
1119 struct vnop_open_args {
1122 struct ucred *a_cred;
1123 struct thread *a_td;
1124 int a_fdidx; / struct file *a_fp;
1128 fuse_vnop_open(struct vop_open_args *ap)
1130 struct vnode *vp = ap->a_vp;
1131 int mode = ap->a_mode;
1132 struct thread *td = ap->a_td;
1133 struct ucred *cred = ap->a_cred;
1135 fufh_type_t fufh_type;
1136 struct fuse_vnode_data *fvdat;
1138 int error, isdir = 0;
1139 int32_t fuse_open_flags;
1141 FS_DEBUG2G("inode=%ju mode=0x%x\n", (uintmax_t)VTOI(vp), mode);
1143 if (fuse_isdeadfs(vp)) {
1148 if (vnode_isdir(vp)) {
1151 fuse_open_flags = 0;
1153 fufh_type = FUFH_RDONLY;
1155 fufh_type = fuse_filehandle_xlate_from_fflags(mode);
1157 * For WRONLY opens, force DIRECT_IO. This is necessary
1158 * since writing a partial block through the buffer cache
1159 * will result in a read of the block and that read won't
1160 * be allowed by the WRONLY open.
1162 if (fufh_type == FUFH_WRONLY ||
1163 (fvdat->flag & FN_DIRECTIO) != 0)
1164 fuse_open_flags = FOPEN_DIRECT_IO;
1167 if (fuse_filehandle_validrw(vp, fufh_type) != FUFH_INVALID) {
1168 fuse_vnode_open(vp, fuse_open_flags, td);
1171 error = fuse_filehandle_open(vp, fufh_type, NULL, td, cred);
1177 struct vnop_read_args {
1181 struct ucred *a_cred;
1185 fuse_vnop_read(struct vop_read_args *ap)
1187 struct vnode *vp = ap->a_vp;
1188 struct uio *uio = ap->a_uio;
1189 int ioflag = ap->a_ioflag;
1190 struct ucred *cred = ap->a_cred;
1192 FS_DEBUG2G("inode=%ju offset=%jd resid=%zd\n",
1193 (uintmax_t)VTOI(vp), uio->uio_offset, uio->uio_resid);
1195 if (fuse_isdeadfs(vp)) {
1199 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1200 ioflag |= IO_DIRECT;
1203 return fuse_io_dispatch(vp, uio, ioflag, cred);
1207 struct vnop_readdir_args {
1210 struct ucred *a_cred;
1217 fuse_vnop_readdir(struct vop_readdir_args *ap)
1219 struct vnode *vp = ap->a_vp;
1220 struct uio *uio = ap->a_uio;
1221 struct ucred *cred = ap->a_cred;
1223 struct fuse_filehandle *fufh = NULL;
1224 struct fuse_vnode_data *fvdat;
1225 struct fuse_iov cookediov;
1230 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1232 if (fuse_isdeadfs(vp)) {
1235 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1236 (uio_resid(uio) < sizeof(struct dirent))) {
1241 if (!fuse_filehandle_valid(vp, FUFH_RDONLY)) {
1242 FS_DEBUG("calling readdir() before open()");
1243 err = fuse_filehandle_open(vp, FUFH_RDONLY, &fufh, NULL, cred);
1246 err = fuse_filehandle_get(vp, FUFH_RDONLY, &fufh);
1251 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1252 fiov_init(&cookediov, DIRCOOKEDSIZE);
1254 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1256 fiov_teardown(&cookediov);
1258 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
1264 struct vnop_readlink_args {
1267 struct ucred *a_cred;
1271 fuse_vnop_readlink(struct vop_readlink_args *ap)
1273 struct vnode *vp = ap->a_vp;
1274 struct uio *uio = ap->a_uio;
1275 struct ucred *cred = ap->a_cred;
1277 struct fuse_dispatcher fdi;
1280 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1282 if (fuse_isdeadfs(vp)) {
1285 if (!vnode_islnk(vp)) {
1288 fdisp_init(&fdi, 0);
1289 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1293 if (((char *)fdi.answ)[0] == '/' &&
1294 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1295 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1297 err = uiomove(mpth, strlen(mpth), uio);
1300 err = uiomove(fdi.answ, fdi.iosize, uio);
1303 fdisp_destroy(&fdi);
1308 struct vnop_reclaim_args {
1310 struct thread *a_td;
1314 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1316 struct vnode *vp = ap->a_vp;
1317 struct thread *td = ap->a_td;
1319 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1320 struct fuse_filehandle *fufh = NULL;
1325 panic("FUSE: no vnode data during recycling");
1327 FS_DEBUG("inode=%ju\n", (uintmax_t)VTOI(vp));
1329 for (type = 0; type < FUFH_MAXTYPE; type++) {
1330 fufh = &(fvdat->fufh[type]);
1331 if (FUFH_IS_VALID(fufh)) {
1332 printf("FUSE: vnode being reclaimed but fufh (type=%d) is valid",
1334 fuse_filehandle_close(vp, type, td, NULL);
1338 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1339 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1342 fuse_vnode_setparent(vp, NULL);
1344 vfs_hash_remove(vp);
1345 vnode_destroy_vobject(vp);
1346 fuse_vnode_destroy(vp);
1352 struct vnop_remove_args {
1353 struct vnode *a_dvp;
1355 struct componentname *a_cnp;
1359 fuse_vnop_remove(struct vop_remove_args *ap)
1361 struct vnode *dvp = ap->a_dvp;
1362 struct vnode *vp = ap->a_vp;
1363 struct componentname *cnp = ap->a_cnp;
1367 FS_DEBUG2G("inode=%ju name=%*s\n",
1368 (uintmax_t)VTOI(vp), (int)cnp->cn_namelen, cnp->cn_nameptr);
1370 if (fuse_isdeadfs(vp)) {
1373 if (vnode_isdir(vp)) {
1378 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1381 fuse_internal_vnode_disappear(vp);
1386 struct vnop_rename_args {
1387 struct vnode *a_fdvp;
1388 struct vnode *a_fvp;
1389 struct componentname *a_fcnp;
1390 struct vnode *a_tdvp;
1391 struct vnode *a_tvp;
1392 struct componentname *a_tcnp;
1396 fuse_vnop_rename(struct vop_rename_args *ap)
1398 struct vnode *fdvp = ap->a_fdvp;
1399 struct vnode *fvp = ap->a_fvp;
1400 struct componentname *fcnp = ap->a_fcnp;
1401 struct vnode *tdvp = ap->a_tdvp;
1402 struct vnode *tvp = ap->a_tvp;
1403 struct componentname *tcnp = ap->a_tcnp;
1404 struct fuse_data *data;
1408 FS_DEBUG2G("from: inode=%ju name=%*s -> to: inode=%ju name=%*s\n",
1409 (uintmax_t)VTOI(fvp), (int)fcnp->cn_namelen, fcnp->cn_nameptr,
1410 (uintmax_t)(tvp == NULL ? -1 : VTOI(tvp)),
1411 (int)tcnp->cn_namelen, tcnp->cn_nameptr);
1413 if (fuse_isdeadfs(fdvp)) {
1416 if (fvp->v_mount != tdvp->v_mount ||
1417 (tvp && fvp->v_mount != tvp->v_mount)) {
1418 FS_DEBUG("cross-device rename: %s -> %s\n",
1419 fcnp->cn_nameptr, (tcnp != NULL ? tcnp->cn_nameptr : "(NULL)"));
1426 * FUSE library is expected to check if target directory is not
1427 * under the source directory in the file system tree.
1428 * Linux performs this check at VFS level.
1430 data = fuse_get_mpdata(vnode_mount(tdvp));
1431 sx_xlock(&data->rename_lock);
1432 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1435 fuse_vnode_setparent(fvp, tdvp);
1437 fuse_vnode_setparent(tvp, NULL);
1439 sx_unlock(&data->rename_lock);
1441 if (tvp != NULL && tvp != fvp) {
1444 if (vnode_isdir(fvp)) {
1445 if ((tvp != NULL) && vnode_isdir(tvp)) {
1466 struct vnop_rmdir_args {
1467 struct vnode *a_dvp;
1469 struct componentname *a_cnp;
1473 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1475 struct vnode *dvp = ap->a_dvp;
1476 struct vnode *vp = ap->a_vp;
1480 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1482 if (fuse_isdeadfs(vp)) {
1485 if (VTOFUD(vp) == VTOFUD(dvp)) {
1488 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1491 fuse_internal_vnode_disappear(vp);
1496 struct vnop_setattr_args {
1498 struct vattr *a_vap;
1499 struct ucred *a_cred;
1500 struct thread *a_td;
1504 fuse_vnop_setattr(struct vop_setattr_args *ap)
1506 struct vnode *vp = ap->a_vp;
1507 struct vattr *vap = ap->a_vap;
1508 struct ucred *cred = ap->a_cred;
1509 struct thread *td = curthread;
1511 struct fuse_dispatcher fdi;
1512 struct fuse_setattr_in *fsai;
1513 struct fuse_access_param facp;
1517 int sizechanged = 0;
1518 uint64_t newsize = 0;
1520 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1522 if (fuse_isdeadfs(vp)) {
1525 fdisp_init(&fdi, sizeof(*fsai));
1526 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1530 bzero(&facp, sizeof(facp));
1532 facp.xuid = vap->va_uid;
1533 facp.xgid = vap->va_gid;
1535 if (vap->va_uid != (uid_t)VNOVAL) {
1536 facp.facc_flags |= FACCESS_CHOWN;
1537 fsai->uid = vap->va_uid;
1538 fsai->valid |= FATTR_UID;
1540 if (vap->va_gid != (gid_t)VNOVAL) {
1541 facp.facc_flags |= FACCESS_CHOWN;
1542 fsai->gid = vap->va_gid;
1543 fsai->valid |= FATTR_GID;
1545 if (vap->va_size != VNOVAL) {
1547 struct fuse_filehandle *fufh = NULL;
1549 /*Truncate to a new value. */
1550 fsai->size = vap->va_size;
1552 newsize = vap->va_size;
1553 fsai->valid |= FATTR_SIZE;
1555 fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh);
1557 fsai->fh = fufh->fh_id;
1558 fsai->valid |= FATTR_FH;
1561 if (vap->va_atime.tv_sec != VNOVAL) {
1562 fsai->atime = vap->va_atime.tv_sec;
1563 fsai->atimensec = vap->va_atime.tv_nsec;
1564 fsai->valid |= FATTR_ATIME;
1566 if (vap->va_mtime.tv_sec != VNOVAL) {
1567 fsai->mtime = vap->va_mtime.tv_sec;
1568 fsai->mtimensec = vap->va_mtime.tv_nsec;
1569 fsai->valid |= FATTR_MTIME;
1571 if (vap->va_mode != (mode_t)VNOVAL) {
1572 fsai->mode = vap->va_mode & ALLPERMS;
1573 fsai->valid |= FATTR_MODE;
1578 vtyp = vnode_vtype(vp);
1580 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1584 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1588 if (fsai->valid & ~FATTR_SIZE) {
1589 /*err = fuse_internal_access(vp, VADMIN, context, &facp); */
1593 facp.facc_flags &= ~FACCESS_XQUERIES;
1595 if (err && !(fsai->valid & ~(FATTR_ATIME | FATTR_MTIME)) &&
1596 vap->va_vaflags & VA_UTIMES_NULL) {
1597 err = fuse_internal_access(vp, VWRITE, &facp, td, cred);
1601 if ((err = fdisp_wait_answ(&fdi)))
1603 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1605 if (vnode_vtype(vp) != vtyp) {
1606 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1607 debug_printf("FUSE: Dang! vnode_vtype is VNON and vtype isn't.\n");
1610 * STALE vnode, ditch
1612 * The vnode has changed its type "behind our back". There's
1613 * nothing really we can do, so let us just force an internal
1614 * revocation and tell the caller to try again, if interested.
1616 fuse_internal_vnode_disappear(vp);
1620 if (!err && !sizechanged) {
1621 cache_attrs(vp, (struct fuse_attr_out *)fdi.answ);
1624 fdisp_destroy(&fdi);
1625 if (!err && sizechanged) {
1626 fuse_vnode_setsize(vp, cred, newsize);
1627 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1633 struct vnop_strategy_args {
1639 fuse_vnop_strategy(struct vop_strategy_args *ap)
1641 struct vnode *vp = ap->a_vp;
1642 struct buf *bp = ap->a_bp;
1644 fuse_trace_printf_vnop();
1646 if (!vp || fuse_isdeadfs(vp)) {
1647 bp->b_ioflags |= BIO_ERROR;
1648 bp->b_error = ENXIO;
1652 if (bp->b_iocmd == BIO_WRITE)
1653 fuse_vnode_refreshsize(vp, NOCRED);
1655 (void)fuse_io_strategy(vp, bp);
1658 * This is a dangerous function. If returns error, that might mean a
1659 * panic. We prefer pretty much anything over being forced to panic
1660 * by a malicious daemon (a demon?). So we just return 0 anyway. You
1661 * should never mind this: this function has its own error
1662 * propagation mechanism via the argument buffer, so
1663 * not-that-melodramatic residents of the call chain still will be
1664 * able to know what to do.
1671 struct vnop_symlink_args {
1672 struct vnode *a_dvp;
1673 struct vnode **a_vpp;
1674 struct componentname *a_cnp;
1675 struct vattr *a_vap;
1680 fuse_vnop_symlink(struct vop_symlink_args *ap)
1682 struct vnode *dvp = ap->a_dvp;
1683 struct vnode **vpp = ap->a_vpp;
1684 struct componentname *cnp = ap->a_cnp;
1685 char *target = ap->a_target;
1687 struct fuse_dispatcher fdi;
1692 FS_DEBUG2G("inode=%ju name=%*s\n",
1693 (uintmax_t)VTOI(dvp), (int)cnp->cn_namelen, cnp->cn_nameptr);
1695 if (fuse_isdeadfs(dvp)) {
1699 * Unlike the other creator type calls, here we have to create a message
1700 * where the name of the new entry comes first, and the data describing
1701 * the entry comes second.
1702 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1703 * but put together the message manually and just call the core part.
1706 len = strlen(target) + 1;
1707 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1708 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1710 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1711 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1712 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1714 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1715 fdisp_destroy(&fdi);
1720 struct vnop_write_args {
1724 struct ucred *a_cred;
1728 fuse_vnop_write(struct vop_write_args *ap)
1730 struct vnode *vp = ap->a_vp;
1731 struct uio *uio = ap->a_uio;
1732 int ioflag = ap->a_ioflag;
1733 struct ucred *cred = ap->a_cred;
1735 fuse_trace_printf_vnop();
1737 if (fuse_isdeadfs(vp)) {
1740 fuse_vnode_refreshsize(vp, cred);
1742 if (VTOFUD(vp)->flag & FN_DIRECTIO) {
1743 ioflag |= IO_DIRECT;
1746 return fuse_io_dispatch(vp, uio, ioflag, cred);
1750 struct vnop_getpages_args {
1758 fuse_vnop_getpages(struct vop_getpages_args *ap)
1760 int i, error, nextoff, size, toff, count, npages;
1770 FS_DEBUG2G("heh\n");
1773 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1774 td = curthread; /* XXX */
1775 cred = curthread->td_ucred; /* XXX */
1777 npages = ap->a_count;
1779 if (!fsess_opt_mmap(vnode_mount(vp))) {
1780 FS_DEBUG("called on non-cacheable vnode??\n");
1781 return (VM_PAGER_ERROR);
1785 * If the last page is partially valid, just return it and allow
1786 * the pager to zero-out the blanks. Partially valid pages can
1787 * only occur at the file EOF.
1789 * XXXGL: is that true for FUSE, which is a local filesystem,
1790 * but still somewhat disconnected from the kernel?
1792 VM_OBJECT_WLOCK(vp->v_object);
1793 if (pages[npages - 1]->valid != 0 && --npages == 0)
1795 VM_OBJECT_WUNLOCK(vp->v_object);
1798 * We use only the kva address for the buffer, but this is extremely
1799 * convenient and fast.
1801 bp = getpbuf(&fuse_pbuf_freecnt);
1803 kva = (vm_offset_t)bp->b_data;
1804 pmap_qenter(kva, pages, npages);
1805 VM_CNT_INC(v_vnodein);
1806 VM_CNT_ADD(v_vnodepgsin, npages);
1808 count = npages << PAGE_SHIFT;
1809 iov.iov_base = (caddr_t)kva;
1810 iov.iov_len = count;
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;
1819 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1820 pmap_qremove(kva, npages);
1822 relpbuf(bp, &fuse_pbuf_freecnt);
1824 if (error && (uio.uio_resid == count)) {
1825 FS_DEBUG("error %d\n", error);
1826 return VM_PAGER_ERROR;
1829 * Calculate the number of bytes read and validate only that number
1830 * of bytes. Note that due to pending writes, size may be 0. This
1831 * does not mean that the remaining data is invalid!
1834 size = count - uio.uio_resid;
1835 VM_OBJECT_WLOCK(vp->v_object);
1836 fuse_vm_page_lock_queues();
1837 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1840 nextoff = toff + PAGE_SIZE;
1843 if (nextoff <= size) {
1845 * Read operation filled an entire page
1847 m->valid = VM_PAGE_BITS_ALL;
1848 KASSERT(m->dirty == 0,
1849 ("fuse_getpages: page %p is dirty", m));
1850 } else if (size > toff) {
1852 * Read operation filled a partial page.
1855 vm_page_set_valid_range(m, 0, size - toff);
1856 KASSERT(m->dirty == 0,
1857 ("fuse_getpages: page %p is dirty", m));
1860 * Read operation was short. If no error occurred
1861 * we may have hit a zero-fill section. We simply
1862 * leave valid set to 0.
1867 fuse_vm_page_unlock_queues();
1869 VM_OBJECT_WUNLOCK(vp->v_object);
1874 return (VM_PAGER_OK);
1878 struct vnop_putpages_args {
1884 vm_ooffset_t a_offset;
1888 fuse_vnop_putpages(struct vop_putpages_args *ap)
1894 int i, error, npages, count;
1903 FS_DEBUG2G("heh\n");
1906 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1907 fsize = vp->v_object->un_pager.vnp.vnp_size;
1908 td = curthread; /* XXX */
1909 cred = curthread->td_ucred; /* XXX */
1911 count = ap->a_count;
1912 rtvals = ap->a_rtvals;
1913 npages = btoc(count);
1914 offset = IDX_TO_OFF(pages[0]->pindex);
1916 if (!fsess_opt_mmap(vnode_mount(vp))) {
1917 FS_DEBUG("called on non-cacheable vnode??\n");
1919 for (i = 0; i < npages; i++)
1920 rtvals[i] = VM_PAGER_AGAIN;
1923 * When putting pages, do not extend file past EOF.
1926 if (offset + count > fsize) {
1927 count = fsize - offset;
1932 * We use only the kva address for the buffer, but this is extremely
1933 * convenient and fast.
1935 bp = getpbuf(&fuse_pbuf_freecnt);
1937 kva = (vm_offset_t)bp->b_data;
1938 pmap_qenter(kva, pages, npages);
1939 VM_CNT_INC(v_vnodeout);
1940 VM_CNT_ADD(v_vnodepgsout, count);
1942 iov.iov_base = (caddr_t)kva;
1943 iov.iov_len = count;
1946 uio.uio_offset = offset;
1947 uio.uio_resid = count;
1948 uio.uio_segflg = UIO_SYSSPACE;
1949 uio.uio_rw = UIO_WRITE;
1952 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1954 pmap_qremove(kva, npages);
1955 relpbuf(bp, &fuse_pbuf_freecnt);
1958 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
1960 for (i = 0; i < nwritten; i++) {
1961 rtvals[i] = VM_PAGER_OK;
1962 VM_OBJECT_WLOCK(pages[i]->object);
1963 vm_page_undirty(pages[i]);
1964 VM_OBJECT_WUNLOCK(pages[i]->object);
1970 static const char extattr_namespace_separator = '.';
1973 struct vop_getextattr_args {
1974 struct vop_generic_args a_gen;
1976 int a_attrnamespace;
1980 struct ucred *a_cred;
1981 struct thread *a_td;
1985 fuse_vnop_getextattr(struct vop_getextattr_args *ap)
1987 struct vnode *vp = ap->a_vp;
1988 struct uio *uio = ap->a_uio;
1989 struct fuse_dispatcher fdi = {0};
1990 struct fuse_getxattr_in *get_xattr_in;
1991 struct fuse_getxattr_out *get_xattr_out;
1992 struct mount *mp = vnode_mount(vp);
1996 struct thread *td = ap->a_td;
1997 struct ucred *cred = ap->a_cred;
2000 fuse_trace_printf_vnop();
2002 if (fuse_isdeadfs(vp))
2005 /* Default to looking for user attributes. */
2006 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2007 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2009 prefix = EXTATTR_NAMESPACE_USER_STRING;
2011 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2012 strlen(ap->a_name) + 1;
2014 fdisp_init(&fdi, len + sizeof(*get_xattr_in));
2015 fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred);
2017 get_xattr_in = fdi.indata;
2019 * Check to see whether we're querying the available size or
2020 * issuing the actual request. If we pass in 0, we get back struct
2021 * fuse_getxattr_out. If we pass in a non-zero size, we get back
2022 * that much data, without the struct fuse_getxattr_out header.
2024 if (ap->a_size != NULL)
2025 get_xattr_in->size = 0;
2027 get_xattr_in->size = uio->uio_resid;
2029 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2030 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2033 err = fdisp_wait_answ(&fdi);
2037 fsess_set_notimpl(mp, FUSE_GETXATTR);
2038 debug_printf("getxattr: got err=%d from daemon\n", err);
2043 * If we get to this point (i.e. no error), we should have a valid
2044 * answer of some sort. i.e. non-zero iosize and a valid pointer.
2046 if ((fdi.answ == NULL) || (fdi.iosize == 0)) {
2047 debug_printf("getxattr: err = 0, but answ = %p, iosize = %zu\n",
2048 fdi.answ, fdi.iosize);
2052 get_xattr_out = fdi.answ;
2054 if (ap->a_size != NULL) {
2055 *ap->a_size = get_xattr_out->size;
2056 } else if (fdi.iosize > 0) {
2057 err = uiomove(fdi.answ, fdi.iosize, uio);
2063 fdisp_destroy(&fdi);
2068 struct vop_setextattr_args {
2069 struct vop_generic_args a_gen;
2071 int a_attrnamespace;
2074 struct ucred *a_cred;
2075 struct thread *a_td;
2079 fuse_vnop_setextattr(struct vop_setextattr_args *ap)
2081 struct vnode *vp = ap->a_vp;
2082 struct uio *uio = ap->a_uio;
2083 struct fuse_dispatcher fdi = {0};
2084 struct fuse_setxattr_in *set_xattr_in;
2085 struct mount *mp = vnode_mount(vp);
2089 struct thread *td = ap->a_td;
2090 struct ucred *cred = ap->a_cred;
2093 fuse_trace_printf_vnop();
2095 if (fuse_isdeadfs(vp))
2098 /* Default to looking for user attributes. */
2099 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2100 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2102 prefix = EXTATTR_NAMESPACE_USER_STRING;
2104 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2105 strlen(ap->a_name) + 1;
2107 fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid);
2108 fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred);
2110 set_xattr_in = fdi.indata;
2111 set_xattr_in->size = uio->uio_resid;
2113 attr_str = (char *)fdi.indata + sizeof(*set_xattr_in);
2114 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2117 err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len,
2118 uio->uio_resid, uio);
2120 debug_printf("setxattr: got error %d from uiomove\n", err);
2124 err = fdisp_wait_answ(&fdi);
2128 fsess_set_notimpl(mp, FUSE_SETXATTR);
2129 debug_printf("setxattr: got err=%d from daemon\n", err);
2134 fdisp_destroy(&fdi);
2139 * The Linux / FUSE extended attribute list is simply a collection of
2140 * NUL-terminated strings. The FreeBSD extended attribute list is a single
2141 * byte length followed by a non-NUL terminated string. So, this allows
2142 * conversion of the Linux / FUSE format to the FreeBSD format in place.
2143 * Linux attribute names are reported with the namespace as a prefix (e.g.
2144 * "user.attribute_name"), but in FreeBSD they are reported without the
2145 * namespace prefix (e.g. "attribute_name"). So, we're going from:
2147 * user.attr_name1\0user.attr_name2\0
2151 * <num>attr_name1<num>attr_name2
2153 * Where "<num>" is a single byte number of characters in the attribute name.
2156 * prefix - exattr namespace prefix string
2157 * list, list_len - input list with namespace prefixes
2158 * bsd_list, bsd_list_len - output list compatible with bsd vfs
2161 fuse_xattrlist_convert(char *prefix, const char *list, int list_len,
2162 char *bsd_list, int *bsd_list_len)
2164 int len, pos, dist_to_next, prefix_len;
2168 prefix_len = strlen(prefix);
2170 while (pos < list_len && list[pos] != '\0') {
2171 dist_to_next = strlen(&list[pos]) + 1;
2172 if (bcmp(&list[pos], prefix, prefix_len) == 0 &&
2173 list[pos + prefix_len] == extattr_namespace_separator) {
2174 len = dist_to_next -
2175 (prefix_len + sizeof(extattr_namespace_separator)) - 1;
2176 if (len >= EXTATTR_MAXNAMELEN)
2177 return (ENAMETOOLONG);
2179 bsd_list[*bsd_list_len] = len;
2180 memcpy(&bsd_list[*bsd_list_len + 1],
2181 &list[pos + prefix_len +
2182 sizeof(extattr_namespace_separator)], len);
2184 *bsd_list_len += len + 1;
2187 pos += dist_to_next;
2194 struct vop_listextattr_args {
2195 struct vop_generic_args a_gen;
2197 int a_attrnamespace;
2200 struct ucred *a_cred;
2201 struct thread *a_td;
2205 fuse_vnop_listextattr(struct vop_listextattr_args *ap)
2207 struct vnode *vp = ap->a_vp;
2208 struct uio *uio = ap->a_uio;
2209 struct fuse_dispatcher fdi = {0};
2210 struct fuse_getxattr_in *get_xattr_in;
2211 struct fuse_getxattr_out *get_xattr_out;
2212 struct mount *mp = vnode_mount(vp);
2216 char *bsd_list = NULL;
2218 struct thread *td = ap->a_td;
2219 struct ucred *cred = ap->a_cred;
2222 fuse_trace_printf_vnop();
2224 if (fuse_isdeadfs(vp))
2228 * Add space for a NUL and the period separator if enabled.
2229 * Default to looking for user attributes.
2231 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2232 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2234 prefix = EXTATTR_NAMESPACE_USER_STRING;
2236 len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1;
2238 fdisp_init(&fdi, sizeof(*get_xattr_in) + len);
2239 fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred);
2241 get_xattr_in = fdi.indata;
2242 if (ap->a_size != NULL)
2243 get_xattr_in->size = 0;
2245 get_xattr_in->size = uio->uio_resid + sizeof(*get_xattr_out);
2248 attr_str = (char *)fdi.indata + sizeof(*get_xattr_in);
2249 snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator);
2251 err = fdisp_wait_answ(&fdi);
2254 fsess_set_notimpl(mp, FUSE_LISTXATTR);
2255 debug_printf("listextattr: got err=%d from daemon\n", err);
2259 if ((fdi.answ == NULL) || (fdi.iosize == 0)) {
2263 get_xattr_out = fdi.answ;
2265 if (ap->a_size != NULL) {
2266 *ap->a_size = get_xattr_out->size;
2267 } else if (fdi.iosize > 0) {
2269 * The Linux / FUSE attribute list format isn't the same
2270 * as FreeBSD's format. So we need to transform it into
2271 * FreeBSD's format before giving it to the user.
2273 bsd_list = malloc(fdi.iosize, M_TEMP, M_WAITOK);
2274 err = fuse_xattrlist_convert(prefix, fdi.answ, fdi.iosize,
2275 bsd_list, &bsd_list_len);
2279 err = uiomove(bsd_list, bsd_list_len, uio);
2281 debug_printf("listextattr: returned iosize %zu for %s attribute list is "
2282 "too small\n", fdi.iosize, prefix);
2287 free(bsd_list, M_TEMP);
2288 fdisp_destroy(&fdi);
2293 struct vop_deleteextattr_args {
2294 struct vop_generic_args a_gen;
2296 int a_attrnamespace;
2298 struct ucred *a_cred;
2299 struct thread *a_td;
2303 fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap)
2305 struct vnode *vp = ap->a_vp;
2306 struct fuse_dispatcher fdi = {0};
2307 struct mount *mp = vnode_mount(vp);
2311 struct thread *td = ap->a_td;
2312 struct ucred *cred = ap->a_cred;
2315 fuse_trace_printf_vnop();
2317 if (fuse_isdeadfs(vp))
2320 /* Default to looking for user attributes. */
2321 if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
2322 prefix = EXTATTR_NAMESPACE_SYSTEM_STRING;
2324 prefix = EXTATTR_NAMESPACE_USER_STRING;
2326 len = strlen(prefix) + sizeof(extattr_namespace_separator) +
2327 strlen(ap->a_name) + 1;
2329 fdisp_init(&fdi, len);
2330 fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred);
2332 attr_str = fdi.indata;
2333 snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator,
2336 err = fdisp_wait_answ(&fdi);
2339 fsess_set_notimpl(mp, FUSE_REMOVEXATTR);
2340 debug_printf("removexattr: got err=%d from daemon\n", err);
2343 fdisp_destroy(&fdi);
2348 struct vnop_print_args {
2353 fuse_vnop_print(struct vop_print_args *ap)
2355 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
2357 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
2358 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
2359 (uintmax_t)fvdat->nlookup,