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>
77 #include <sys/unistd.h>
78 #include <sys/filedesc.h>
80 #include <sys/fcntl.h>
81 #include <sys/dirent.h>
84 #include <sys/sysctl.h>
87 #include <vm/vm_extern.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>
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"
105 #include <sys/priv.h>
107 #define FUSE_DEBUG_MODULE VNOPS
108 #include "fuse_debug.h"
111 static vop_access_t fuse_vnop_access;
112 static vop_close_t fuse_vnop_close;
113 static vop_create_t fuse_vnop_create;
114 static vop_fsync_t fuse_vnop_fsync;
115 static vop_getattr_t fuse_vnop_getattr;
116 static vop_inactive_t fuse_vnop_inactive;
117 static vop_link_t fuse_vnop_link;
118 static vop_lookup_t fuse_vnop_lookup;
119 static vop_mkdir_t fuse_vnop_mkdir;
120 static vop_mknod_t fuse_vnop_mknod;
121 static vop_open_t fuse_vnop_open;
122 static vop_read_t fuse_vnop_read;
123 static vop_readdir_t fuse_vnop_readdir;
124 static vop_readlink_t fuse_vnop_readlink;
125 static vop_reclaim_t fuse_vnop_reclaim;
126 static vop_remove_t fuse_vnop_remove;
127 static vop_rename_t fuse_vnop_rename;
128 static vop_rmdir_t fuse_vnop_rmdir;
129 static vop_setattr_t fuse_vnop_setattr;
130 static vop_strategy_t fuse_vnop_strategy;
131 static vop_symlink_t fuse_vnop_symlink;
132 static vop_write_t fuse_vnop_write;
133 static vop_getpages_t fuse_vnop_getpages;
134 static vop_putpages_t fuse_vnop_putpages;
135 static vop_print_t fuse_vnop_print;
137 struct 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,
167 static u_long fuse_lookup_cache_hits = 0;
169 SYSCTL_ULONG(_vfs_fuse, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
170 &fuse_lookup_cache_hits, 0, "");
172 static u_long fuse_lookup_cache_misses = 0;
174 SYSCTL_ULONG(_vfs_fuse, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
175 &fuse_lookup_cache_misses, 0, "");
177 int fuse_lookup_cache_enable = 1;
179 SYSCTL_INT(_vfs_fuse, OID_AUTO, lookup_cache_enable, CTLFLAG_RW,
180 &fuse_lookup_cache_enable, 0, "");
183 * XXX: This feature is highly experimental and can bring to instabilities,
184 * needs revisiting before to be enabled by default.
186 static int fuse_reclaim_revoked = 0;
188 SYSCTL_INT(_vfs_fuse, OID_AUTO, reclaim_revoked, CTLFLAG_RW,
189 &fuse_reclaim_revoked, 0, "");
191 int fuse_pbuf_freecnt = -1;
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)
199 struct vnop_access_args {
201 #if VOP_ACCESS_TAKES_ACCMODE_T
206 struct ucred *a_cred;
211 fuse_vnop_access(struct vop_access_args *ap)
213 struct vnode *vp = ap->a_vp;
214 int accmode = ap->a_accmode;
215 struct ucred *cred = ap->a_cred;
217 struct fuse_access_param facp;
218 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
222 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
224 if (fuse_isdeadfs(vp)) {
225 if (vnode_isvroot(vp)) {
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)) {
239 if (vnode_islnk(vp)) {
242 bzero(&facp, sizeof(facp));
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);
250 struct vnop_close_args {
253 struct ucred *a_cred;
258 fuse_vnop_close(struct vop_close_args *ap)
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;
265 fuse_trace_printf_vnop();
267 if (fuse_isdeadfs(vp)) {
270 if (vnode_isdir(vp)) {
271 if (fuse_filehandle_valid(vp, FUFH_RDONLY)) {
272 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
276 if (fflag & IO_NDELAY) {
279 fufh_type = fuse_filehandle_xlate_from_fflags(fflag);
281 if (!fuse_filehandle_valid(vp, fufh_type)) {
284 for (i = 0; i < FUFH_MAXTYPE; i++)
285 if (fuse_filehandle_valid(vp, i))
287 if (i == FUFH_MAXTYPE)
288 panic("FUSE: fufh type %d found to be invalid in close"
292 if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) {
293 fuse_vnode_savesize(vp, cred);
299 struct vnop_create_args {
301 struct vnode **a_vpp;
302 struct componentname *a_cnp;
307 fuse_vnop_create(struct vop_create_args *ap)
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;
316 struct fuse_open_in *foi;
317 struct fuse_entry_out *feo;
318 struct fuse_dispatcher fdi;
319 struct fuse_dispatcher *fdip = &fdi;
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);
327 uint32_t x_open_flags;
329 fuse_trace_printf_vnop();
331 if (fuse_isdeadfs(dvp)) {
334 bzero(&fdi, sizeof(fdi));
336 /* XXX: Will we ever want devices ? */
337 if ((vap->va_type != VREG)) {
338 MPASS(vap->va_type != VFIFO);
341 debug_printf("parent nid = %ju, mode = %x\n", (uintmax_t)parentnid,
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");
349 fdisp_make(fdip, FUSE_CREATE, vnode_mount(dvp), parentnid, td, cred);
353 foi->flags = O_CREAT | O_RDWR;
355 memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr,
357 ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0';
359 err = fdisp_wait_answ(fdip);
363 fsess_set_notimpl(mp, FUSE_CREATE);
364 debug_printf("create: got err=%d from daemon\n", err);
370 if ((err = fuse_internal_checkentry(feo, VREG))) {
373 err = fuse_vnode_get(mp, feo->nodeid, dvp, vpp, cnp, VREG);
375 struct fuse_release_in *fri;
376 uint64_t nodeid = feo->nodeid;
377 uint64_t fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
379 fdisp_init(fdip, sizeof(*fri));
380 fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred);
383 fri->flags = OFLAGS(mode);
384 fuse_insert_callback(fdip->tick, fuse_internal_forget_callback);
385 fuse_insert_message(fdip->tick);
388 ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create");
390 fdip->answ = feo + 1;
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);
404 * Our vnop_fsync roughly corresponds to the FUSE_FSYNC method. The Linux
405 * version of FUSE also has a FUSE_FLUSH method.
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.
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.
416 struct vnop_fsync_args {
417 struct vnodeop_desc *a_desc;
419 struct ucred * a_cred;
421 struct thread * a_td;
425 fuse_vnop_fsync(struct vop_fsync_args *ap)
427 struct vnode *vp = ap->a_vp;
428 struct thread *td = ap->a_td;
430 struct fuse_filehandle *fufh;
431 struct fuse_vnode_data *fvdat = VTOFUD(vp);
435 fuse_trace_printf_vnop();
437 if (fuse_isdeadfs(vp)) {
440 if ((err = vop_stdfsync(ap)))
443 if (!fsess_isimpl(vnode_mount(vp),
444 (vnode_vtype(vp) == VDIR ? FUSE_FSYNCDIR : FUSE_FSYNC))) {
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);
459 struct vnop_getattr_args {
462 struct ucred *a_cred;
467 fuse_vnop_getattr(struct vop_getattr_args *ap)
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);
477 struct fuse_dispatcher fdi;
479 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
481 dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
483 /* Note that we are not bailing out on a dead file system just yet. */
485 if (!(dataflags & FSESS_INITED)) {
486 if (!vnode_isvroot(vp)) {
487 fdata_set_dead(fuse_get_mpdata(vnode_mount(vp)));
489 debug_printf("fuse_getattr b: returning ENOTCONN\n");
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() */
503 fuse_internal_vnode_disappear(vp);
507 cache_attrs(vp, (struct fuse_attr_out *)fdi.answ);
508 if (vap != VTOVA(vp)) {
509 memcpy(vap, VTOVA(vp), sizeof(*vap));
511 if (vap->va_type != vnode_vtype(vp)) {
512 fuse_internal_vnode_disappear(vp);
516 if ((fvdat->flag & FN_SIZECHANGE) != 0)
517 vap->va_size = fvdat->filesize;
519 if (vnode_isreg(vp) && (fvdat->flag & FN_SIZECHANGE) == 0) {
521 * This is for those cases when the file size changed without us
522 * knowing, and we want to catch up.
524 off_t new_filesize = ((struct fuse_attr_out *)
525 fdi.answ)->attr.size;
527 if (fvdat->filesize != new_filesize) {
528 fuse_vnode_setsize(vp, cred, new_filesize);
531 debug_printf("fuse_getattr e: returning 0\n");
538 bzero(vap, sizeof(*vap));
539 vap->va_type = vnode_vtype(vp);
545 struct vnop_inactive_args {
551 fuse_vnop_inactive(struct vop_inactive_args *ap)
553 struct vnode *vp = ap->a_vp;
554 struct thread *td = ap->a_td;
556 struct fuse_vnode_data *fvdat = VTOFUD(vp);
557 struct fuse_filehandle *fufh = NULL;
559 int type, need_flush = 1;
561 FS_DEBUG("inode=%ju\n", (uintmax_t)VTOI(vp));
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);
570 if (fuse_data_cache_invalidate ||
571 (fvdat->flag & FN_REVOKED) != 0)
572 fuse_io_invalbuf(vp, td);
574 fuse_io_flushbuf(vp, MNT_WAIT, td);
577 fuse_filehandle_close(vp, type, td, NULL);
581 if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) {
588 struct vnop_link_args {
589 struct vnode *a_tdvp;
591 struct componentname *a_cnp;
595 fuse_vnop_link(struct vop_link_args *ap)
597 struct vnode *vp = ap->a_vp;
598 struct vnode *tdvp = ap->a_tdvp;
599 struct componentname *cnp = ap->a_cnp;
601 struct vattr *vap = VTOVA(vp);
603 struct fuse_dispatcher fdi;
604 struct fuse_entry_out *feo;
605 struct fuse_link_in fli;
609 fuse_trace_printf_vnop();
611 if (fuse_isdeadfs(vp)) {
614 if (vnode_mount(tdvp) != vnode_mount(vp)) {
617 if (vap->va_nlink >= FUSE_LINK_MAX) {
620 fli.oldnodeid = VTOI(vp);
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))) {
630 err = fuse_internal_checkentry(feo, vnode_vtype(vp));
637 struct vnop_lookup_args {
638 struct vnodeop_desc *a_desc;
640 struct vnode **a_vpp;
641 struct componentname *a_cnp;
645 fuse_vnop_lookup(struct vop_lookup_args *ap)
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;
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);
661 struct vnode *vp = NULL;
663 struct fuse_dispatcher fdi;
667 struct fuse_access_param facp;
669 FS_DEBUG2G("parent_inode=%ju - %*s\n",
670 (uintmax_t)VTOI(dvp), (int)cnp->cn_namelen, cnp->cn_nameptr);
672 if (fuse_isdeadfs(dvp)) {
676 if (!vnode_isdir(dvp)) {
679 if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) {
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.
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))) {
695 if (flags & ISDOTDOT) {
696 nid = VTOFUD(dvp)->parent_nid;
703 } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') {
708 } else if (fuse_lookup_cache_enable) {
709 err = cache_lookup(dvp, vpp, cnp, NULL, NULL);
712 case -1: /* positive match */
713 atomic_add_acq_long(&fuse_lookup_cache_hits, 1);
716 case 0: /* no match in cache */
717 atomic_add_acq_long(&fuse_lookup_cache_misses, 1);
720 case ENOENT: /* negative match */
727 fdisp_init(&fdi, cnp->cn_namelen + 1);
731 fdisp_make(&fdi, op, mp, nid, td, cred);
733 if (op == FUSE_LOOKUP) {
734 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
735 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
737 lookup_err = fdisp_wait_answ(&fdi);
739 if ((op == FUSE_LOOKUP) && !lookup_err) { /* lookup call succeeded */
740 nid = ((struct fuse_entry_out *)fdi.answ)->nodeid;
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)
748 } else if (nid == FUSE_ROOT_ID) {
753 (!fdi.answ_stat || lookup_err != ENOENT || op != FUSE_LOOKUP)) {
757 /* lookup_err, if non-zero, must be ENOENT at this point */
761 if ((nameiop == CREATE || nameiop == RENAME) && islastcn
762 /* && directory dvp has not been removed */ ) {
764 if (vfs_isrdonly(mp)) {
768 #if 0 /* THINK_ABOUT_THIS */
769 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
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.
781 cnp->cn_flags |= SAVENAME;
786 /* Consider inserting name into cache. */
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
798 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE) {
799 FS_DEBUG("inserting NULL into cache\n");
800 cache_enter(dvp, NULL, cnp);
810 struct fuse_entry_out *feo = NULL;
811 struct fuse_attr *fattr = NULL;
813 if (op == FUSE_GETATTR) {
814 fattr = &((struct fuse_attr_out *)fdi.answ)->attr;
816 feo = (struct fuse_entry_out *)fdi.answ;
817 fattr = &(feo->attr);
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 ".".
826 if (nameiop == DELETE && islastcn) {
828 * Check for write access on directory.
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;
838 if (nid == VTOI(dvp)) {
842 err = fuse_vnode_get(dvp->v_mount, nid, dvp,
843 &vp, cnp, IFTOVT(fattr->mode));
850 * Save the name for use in VOP_RMDIR and VOP_REMOVE
853 cnp->cn_flags |= SAVENAME;
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.
863 if (nameiop == RENAME && wantparent && islastcn) {
865 #if 0 /* THINK_ABOUT_THIS */
866 if ((err = fuse_internal_access(dvp, VWRITE, cred, td, &facp))) {
874 if (nid == VTOI(dvp)) {
878 err = fuse_vnode_get(vnode_mount(dvp),
883 IFTOVT(fattr->mode));
889 * Save the name for use in VOP_RENAME later.
891 cnp->cn_flags |= SAVENAME;
895 if (flags & ISDOTDOT) {
900 * Expanded copy of vn_vget_ino() so that
901 * fuse_vnode_get() can be used.
904 ltype = VOP_ISLOCKED(dvp);
905 err = vfs_busy(mp, MBF_NOWAIT);
909 err = vfs_busy(mp, 0);
910 vn_lock(dvp, ltype | LK_RETRY);
914 if ((dvp->v_iflag & VI_DOOMED) != 0) {
921 err = fuse_vnode_get(vnode_mount(dvp),
926 IFTOVT(fattr->mode));
928 vn_lock(dvp, ltype | LK_RETRY);
929 if ((dvp->v_iflag & VI_DOOMED) != 0) {
937 } else if (nid == VTOI(dvp)) {
941 err = fuse_vnode_get(vnode_mount(dvp),
946 IFTOVT(fattr->mode));
950 fuse_vnode_setparent(vp, dvp);
954 if (op == FUSE_GETATTR) {
955 cache_attrs(*vpp, (struct fuse_attr_out *)fdi.answ);
957 cache_attrs(*vpp, (struct fuse_entry_out *)fdi.answ);
960 /* Insert name into cache if appropriate. */
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.
975 * So just leave the name cache alone.
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.
984 if (cnp->cn_flags & MAKEENTRY) {
985 cache_enter(dvp, *vpp, cnp);
992 /* No lookup error; need to clean up. */
994 if (err) { /* Found inode; exit with no vnode. */
995 if (op == FUSE_LOOKUP) {
996 fuse_internal_forget_send(vnode_mount(dvp), td, cred,
1002 #ifndef NO_EARLY_PERM_CHECK_HACK
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
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.
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...
1035 int tmpvtype = vnode_vtype(*vpp);
1037 bzero(&facp, sizeof(facp));
1038 /*the early perm check hack */
1039 facp.facc_flags |= FACCESS_VA_VALID;
1041 if ((tmpvtype != VDIR) && (tmpvtype != VLNK)) {
1044 if (!err && !vnode_mountedhere(*vpp)) {
1045 err = fuse_internal_access(*vpp, VEXEC, &facp, td, cred);
1048 if (tmpvtype == VLNK)
1049 FS_DEBUG("weird, permission error with a symlink?\n");
1057 fdisp_destroy(&fdi);
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;
1071 fuse_vnop_mkdir(struct vop_mkdir_args *ap)
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;
1078 struct fuse_mkdir_in fmdi;
1080 fuse_trace_printf_vnop();
1082 if (fuse_isdeadfs(dvp)) {
1085 fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode);
1087 return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi,
1088 sizeof(fmdi), VDIR));
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;
1100 fuse_vnop_mknod(struct vop_mknod_args *ap)
1108 struct vnop_open_args {
1111 struct ucred *a_cred;
1112 struct thread *a_td;
1113 int a_fdidx; / struct file *a_fp;
1117 fuse_vnop_open(struct vop_open_args *ap)
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;
1124 fufh_type_t fufh_type;
1125 struct fuse_vnode_data *fvdat;
1127 int error, isdir = 0;
1129 FS_DEBUG2G("inode=%ju mode=0x%x\n", (uintmax_t)VTOI(vp), mode);
1131 if (fuse_isdeadfs(vp)) {
1136 if (vnode_isdir(vp)) {
1140 fufh_type = FUFH_RDONLY;
1142 fufh_type = fuse_filehandle_xlate_from_fflags(mode);
1145 if (fuse_filehandle_valid(vp, fufh_type)) {
1146 fuse_vnode_open(vp, 0, td);
1149 error = fuse_filehandle_open(vp, fufh_type, NULL, td, cred);
1155 struct vnop_read_args {
1159 struct ucred *a_cred;
1163 fuse_vnop_read(struct vop_read_args *ap)
1165 struct vnode *vp = ap->a_vp;
1166 struct uio *uio = ap->a_uio;
1167 int ioflag = ap->a_ioflag;
1168 struct ucred *cred = ap->a_cred;
1170 FS_DEBUG2G("inode=%ju offset=%jd resid=%zd\n",
1171 (uintmax_t)VTOI(vp), uio->uio_offset, uio->uio_resid);
1173 if (fuse_isdeadfs(vp)) {
1176 return fuse_io_dispatch(vp, uio, ioflag, cred);
1180 struct vnop_readdir_args {
1183 struct ucred *a_cred;
1190 fuse_vnop_readdir(struct vop_readdir_args *ap)
1192 struct vnode *vp = ap->a_vp;
1193 struct uio *uio = ap->a_uio;
1194 struct ucred *cred = ap->a_cred;
1196 struct fuse_filehandle *fufh = NULL;
1197 struct fuse_vnode_data *fvdat;
1198 struct fuse_iov cookediov;
1203 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1205 if (fuse_isdeadfs(vp)) {
1208 if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */
1209 (uio_resid(uio) < sizeof(struct dirent))) {
1214 if (!fuse_filehandle_valid(vp, FUFH_RDONLY)) {
1215 FS_DEBUG("calling readdir() before open()");
1216 err = fuse_filehandle_open(vp, FUFH_RDONLY, &fufh, NULL, cred);
1219 err = fuse_filehandle_get(vp, FUFH_RDONLY, &fufh);
1224 #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1)
1225 fiov_init(&cookediov, DIRCOOKEDSIZE);
1227 err = fuse_internal_readdir(vp, uio, fufh, &cookediov);
1229 fiov_teardown(&cookediov);
1231 fuse_filehandle_close(vp, FUFH_RDONLY, NULL, cred);
1237 struct vnop_readlink_args {
1240 struct ucred *a_cred;
1244 fuse_vnop_readlink(struct vop_readlink_args *ap)
1246 struct vnode *vp = ap->a_vp;
1247 struct uio *uio = ap->a_uio;
1248 struct ucred *cred = ap->a_cred;
1250 struct fuse_dispatcher fdi;
1253 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1255 if (fuse_isdeadfs(vp)) {
1258 if (!vnode_islnk(vp)) {
1261 fdisp_init(&fdi, 0);
1262 err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred);
1266 if (((char *)fdi.answ)[0] == '/' &&
1267 fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) {
1268 char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname;
1270 err = uiomove(mpth, strlen(mpth), uio);
1273 err = uiomove(fdi.answ, fdi.iosize, uio);
1276 fdisp_destroy(&fdi);
1281 struct vnop_reclaim_args {
1283 struct thread *a_td;
1287 fuse_vnop_reclaim(struct vop_reclaim_args *ap)
1289 struct vnode *vp = ap->a_vp;
1290 struct thread *td = ap->a_td;
1292 struct fuse_vnode_data *fvdat = VTOFUD(vp);
1293 struct fuse_filehandle *fufh = NULL;
1298 panic("FUSE: no vnode data during recycling");
1300 FS_DEBUG("inode=%ju\n", (uintmax_t)VTOI(vp));
1302 for (type = 0; type < FUFH_MAXTYPE; type++) {
1303 fufh = &(fvdat->fufh[type]);
1304 if (FUFH_IS_VALID(fufh)) {
1305 printf("FUSE: vnode being reclaimed but fufh (type=%d) is valid",
1307 fuse_filehandle_close(vp, type, td, NULL);
1311 if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) {
1312 fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp),
1315 fuse_vnode_setparent(vp, NULL);
1317 vfs_hash_remove(vp);
1318 vnode_destroy_vobject(vp);
1319 fuse_vnode_destroy(vp);
1325 struct vnop_remove_args {
1326 struct vnode *a_dvp;
1328 struct componentname *a_cnp;
1332 fuse_vnop_remove(struct vop_remove_args *ap)
1334 struct vnode *dvp = ap->a_dvp;
1335 struct vnode *vp = ap->a_vp;
1336 struct componentname *cnp = ap->a_cnp;
1340 FS_DEBUG2G("inode=%ju name=%*s\n",
1341 (uintmax_t)VTOI(vp), (int)cnp->cn_namelen, cnp->cn_nameptr);
1343 if (fuse_isdeadfs(vp)) {
1346 if (vnode_isdir(vp)) {
1351 err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK);
1354 fuse_internal_vnode_disappear(vp);
1359 struct vnop_rename_args {
1360 struct vnode *a_fdvp;
1361 struct vnode *a_fvp;
1362 struct componentname *a_fcnp;
1363 struct vnode *a_tdvp;
1364 struct vnode *a_tvp;
1365 struct componentname *a_tcnp;
1369 fuse_vnop_rename(struct vop_rename_args *ap)
1371 struct vnode *fdvp = ap->a_fdvp;
1372 struct vnode *fvp = ap->a_fvp;
1373 struct componentname *fcnp = ap->a_fcnp;
1374 struct vnode *tdvp = ap->a_tdvp;
1375 struct vnode *tvp = ap->a_tvp;
1376 struct componentname *tcnp = ap->a_tcnp;
1377 struct fuse_data *data;
1381 FS_DEBUG2G("from: inode=%ju name=%*s -> to: inode=%ju name=%*s\n",
1382 (uintmax_t)VTOI(fvp), (int)fcnp->cn_namelen, fcnp->cn_nameptr,
1383 (uintmax_t)(tvp == NULL ? -1 : VTOI(tvp)),
1384 (int)tcnp->cn_namelen, tcnp->cn_nameptr);
1386 if (fuse_isdeadfs(fdvp)) {
1389 if (fvp->v_mount != tdvp->v_mount ||
1390 (tvp && fvp->v_mount != tvp->v_mount)) {
1391 FS_DEBUG("cross-device rename: %s -> %s\n",
1392 fcnp->cn_nameptr, (tcnp != NULL ? tcnp->cn_nameptr : "(NULL)"));
1399 * FUSE library is expected to check if target directory is not
1400 * under the source directory in the file system tree.
1401 * Linux performs this check at VFS level.
1403 data = fuse_get_mpdata(vnode_mount(tdvp));
1404 sx_xlock(&data->rename_lock);
1405 err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp);
1408 fuse_vnode_setparent(fvp, tdvp);
1410 fuse_vnode_setparent(tvp, NULL);
1412 sx_unlock(&data->rename_lock);
1414 if (tvp != NULL && tvp != fvp) {
1417 if (vnode_isdir(fvp)) {
1418 if ((tvp != NULL) && vnode_isdir(tvp)) {
1439 struct vnop_rmdir_args {
1440 struct vnode *a_dvp;
1442 struct componentname *a_cnp;
1446 fuse_vnop_rmdir(struct vop_rmdir_args *ap)
1448 struct vnode *dvp = ap->a_dvp;
1449 struct vnode *vp = ap->a_vp;
1453 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1455 if (fuse_isdeadfs(vp)) {
1458 if (VTOFUD(vp) == VTOFUD(dvp)) {
1461 err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR);
1464 fuse_internal_vnode_disappear(vp);
1469 struct vnop_setattr_args {
1471 struct vattr *a_vap;
1472 struct ucred *a_cred;
1473 struct thread *a_td;
1477 fuse_vnop_setattr(struct vop_setattr_args *ap)
1479 struct vnode *vp = ap->a_vp;
1480 struct vattr *vap = ap->a_vap;
1481 struct ucred *cred = ap->a_cred;
1482 struct thread *td = curthread;
1484 struct fuse_dispatcher fdi;
1485 struct fuse_setattr_in *fsai;
1486 struct fuse_access_param facp;
1490 int sizechanged = 0;
1491 uint64_t newsize = 0;
1493 FS_DEBUG2G("inode=%ju\n", (uintmax_t)VTOI(vp));
1495 if (fuse_isdeadfs(vp)) {
1498 fdisp_init(&fdi, sizeof(*fsai));
1499 fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
1503 bzero(&facp, sizeof(facp));
1505 facp.xuid = vap->va_uid;
1506 facp.xgid = vap->va_gid;
1508 if (vap->va_uid != (uid_t)VNOVAL) {
1509 facp.facc_flags |= FACCESS_CHOWN;
1510 fsai->uid = vap->va_uid;
1511 fsai->valid |= FATTR_UID;
1513 if (vap->va_gid != (gid_t)VNOVAL) {
1514 facp.facc_flags |= FACCESS_CHOWN;
1515 fsai->gid = vap->va_gid;
1516 fsai->valid |= FATTR_GID;
1518 if (vap->va_size != VNOVAL) {
1520 struct fuse_filehandle *fufh = NULL;
1522 /*Truncate to a new value. */
1523 fsai->size = vap->va_size;
1525 newsize = vap->va_size;
1526 fsai->valid |= FATTR_SIZE;
1528 fuse_filehandle_getrw(vp, FUFH_WRONLY, &fufh);
1530 fsai->fh = fufh->fh_id;
1531 fsai->valid |= FATTR_FH;
1534 if (vap->va_atime.tv_sec != VNOVAL) {
1535 fsai->atime = vap->va_atime.tv_sec;
1536 fsai->atimensec = vap->va_atime.tv_nsec;
1537 fsai->valid |= FATTR_ATIME;
1539 if (vap->va_mtime.tv_sec != VNOVAL) {
1540 fsai->mtime = vap->va_mtime.tv_sec;
1541 fsai->mtimensec = vap->va_mtime.tv_nsec;
1542 fsai->valid |= FATTR_MTIME;
1544 if (vap->va_mode != (mode_t)VNOVAL) {
1545 fsai->mode = vap->va_mode & ALLPERMS;
1546 fsai->valid |= FATTR_MODE;
1551 vtyp = vnode_vtype(vp);
1553 if (fsai->valid & FATTR_SIZE && vtyp == VDIR) {
1557 if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) {
1561 if (fsai->valid & ~FATTR_SIZE) {
1562 /*err = fuse_internal_access(vp, VADMIN, context, &facp); */
1566 facp.facc_flags &= ~FACCESS_XQUERIES;
1568 if (err && !(fsai->valid & ~(FATTR_ATIME | FATTR_MTIME)) &&
1569 vap->va_vaflags & VA_UTIMES_NULL) {
1570 err = fuse_internal_access(vp, VWRITE, &facp, td, cred);
1574 if ((err = fdisp_wait_answ(&fdi)))
1576 vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode);
1578 if (vnode_vtype(vp) != vtyp) {
1579 if (vnode_vtype(vp) == VNON && vtyp != VNON) {
1580 debug_printf("FUSE: Dang! vnode_vtype is VNON and vtype isn't.\n");
1583 * STALE vnode, ditch
1585 * The vnode has changed its type "behind our back". There's
1586 * nothing really we can do, so let us just force an internal
1587 * revocation and tell the caller to try again, if interested.
1589 fuse_internal_vnode_disappear(vp);
1593 if (!err && !sizechanged) {
1594 cache_attrs(vp, (struct fuse_attr_out *)fdi.answ);
1597 fdisp_destroy(&fdi);
1598 if (!err && sizechanged) {
1599 fuse_vnode_setsize(vp, cred, newsize);
1600 VTOFUD(vp)->flag &= ~FN_SIZECHANGE;
1606 struct vnop_strategy_args {
1612 fuse_vnop_strategy(struct vop_strategy_args *ap)
1614 struct vnode *vp = ap->a_vp;
1615 struct buf *bp = ap->a_bp;
1617 fuse_trace_printf_vnop();
1619 if (!vp || fuse_isdeadfs(vp)) {
1620 bp->b_ioflags |= BIO_ERROR;
1621 bp->b_error = ENXIO;
1625 if (bp->b_iocmd == BIO_WRITE)
1626 fuse_vnode_refreshsize(vp, NOCRED);
1628 (void)fuse_io_strategy(vp, bp);
1631 * This is a dangerous function. If returns error, that might mean a
1632 * panic. We prefer pretty much anything over being forced to panic
1633 * by a malicious daemon (a demon?). So we just return 0 anyway. You
1634 * should never mind this: this function has its own error
1635 * propagation mechanism via the argument buffer, so
1636 * not-that-melodramatic residents of the call chain still will be
1637 * able to know what to do.
1644 struct vnop_symlink_args {
1645 struct vnode *a_dvp;
1646 struct vnode **a_vpp;
1647 struct componentname *a_cnp;
1648 struct vattr *a_vap;
1653 fuse_vnop_symlink(struct vop_symlink_args *ap)
1655 struct vnode *dvp = ap->a_dvp;
1656 struct vnode **vpp = ap->a_vpp;
1657 struct componentname *cnp = ap->a_cnp;
1658 char *target = ap->a_target;
1660 struct fuse_dispatcher fdi;
1665 FS_DEBUG2G("inode=%ju name=%*s\n",
1666 (uintmax_t)VTOI(dvp), (int)cnp->cn_namelen, cnp->cn_nameptr);
1668 if (fuse_isdeadfs(dvp)) {
1672 * Unlike the other creator type calls, here we have to create a message
1673 * where the name of the new entry comes first, and the data describing
1674 * the entry comes second.
1675 * Hence we can't rely on our handy fuse_internal_newentry() routine,
1676 * but put together the message manually and just call the core part.
1679 len = strlen(target) + 1;
1680 fdisp_init(&fdi, len + cnp->cn_namelen + 1);
1681 fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL);
1683 memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
1684 ((char *)fdi.indata)[cnp->cn_namelen] = '\0';
1685 memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len);
1687 err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi);
1688 fdisp_destroy(&fdi);
1693 struct vnop_write_args {
1697 struct ucred *a_cred;
1701 fuse_vnop_write(struct vop_write_args *ap)
1703 struct vnode *vp = ap->a_vp;
1704 struct uio *uio = ap->a_uio;
1705 int ioflag = ap->a_ioflag;
1706 struct ucred *cred = ap->a_cred;
1708 fuse_trace_printf_vnop();
1710 if (fuse_isdeadfs(vp)) {
1713 fuse_vnode_refreshsize(vp, cred);
1715 return fuse_io_dispatch(vp, uio, ioflag, cred);
1719 struct vnop_getpages_args {
1724 vm_ooffset_t a_offset;
1728 fuse_vnop_getpages(struct vop_getpages_args *ap)
1730 int i, error, nextoff, size, toff, count, npages;
1740 FS_DEBUG2G("heh\n");
1743 KASSERT(vp->v_object, ("objectless vp passed to getpages"));
1744 td = curthread; /* XXX */
1745 cred = curthread->td_ucred; /* XXX */
1747 count = ap->a_count;
1749 if (!fsess_opt_mmap(vnode_mount(vp))) {
1750 FS_DEBUG("called on non-cacheable vnode??\n");
1751 return (VM_PAGER_ERROR);
1753 npages = btoc(count);
1756 * If the requested page is partially valid, just return it and
1757 * allow the pager to zero-out the blanks. Partially valid pages
1758 * can only occur at the file EOF.
1761 VM_OBJECT_WLOCK(vp->v_object);
1762 fuse_vm_page_lock_queues();
1763 if (pages[ap->a_reqpage]->valid != 0) {
1764 for (i = 0; i < npages; ++i) {
1765 if (i != ap->a_reqpage) {
1766 fuse_vm_page_lock(pages[i]);
1767 vm_page_free(pages[i]);
1768 fuse_vm_page_unlock(pages[i]);
1771 fuse_vm_page_unlock_queues();
1772 VM_OBJECT_WUNLOCK(vp->v_object);
1775 fuse_vm_page_unlock_queues();
1776 VM_OBJECT_WUNLOCK(vp->v_object);
1779 * We use only the kva address for the buffer, but this is extremely
1780 * convienient and fast.
1782 bp = getpbuf(&fuse_pbuf_freecnt);
1784 kva = (vm_offset_t)bp->b_data;
1785 pmap_qenter(kva, pages, npages);
1786 PCPU_INC(cnt.v_vnodein);
1787 PCPU_ADD(cnt.v_vnodepgsin, npages);
1789 iov.iov_base = (caddr_t)kva;
1790 iov.iov_len = count;
1793 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
1794 uio.uio_resid = count;
1795 uio.uio_segflg = UIO_SYSSPACE;
1796 uio.uio_rw = UIO_READ;
1799 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1800 pmap_qremove(kva, npages);
1802 relpbuf(bp, &fuse_pbuf_freecnt);
1804 if (error && (uio.uio_resid == count)) {
1805 FS_DEBUG("error %d\n", error);
1806 VM_OBJECT_WLOCK(vp->v_object);
1807 fuse_vm_page_lock_queues();
1808 for (i = 0; i < npages; ++i) {
1809 if (i != ap->a_reqpage) {
1810 fuse_vm_page_lock(pages[i]);
1811 vm_page_free(pages[i]);
1812 fuse_vm_page_unlock(pages[i]);
1815 fuse_vm_page_unlock_queues();
1816 VM_OBJECT_WUNLOCK(vp->v_object);
1817 return VM_PAGER_ERROR;
1820 * Calculate the number of bytes read and validate only that number
1821 * of bytes. Note that due to pending writes, size may be 0. This
1822 * does not mean that the remaining data is invalid!
1825 size = count - uio.uio_resid;
1826 VM_OBJECT_WLOCK(vp->v_object);
1827 fuse_vm_page_lock_queues();
1828 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
1831 nextoff = toff + PAGE_SIZE;
1834 if (nextoff <= size) {
1836 * Read operation filled an entire page
1838 m->valid = VM_PAGE_BITS_ALL;
1839 KASSERT(m->dirty == 0,
1840 ("fuse_getpages: page %p is dirty", m));
1841 } else if (size > toff) {
1843 * Read operation filled a partial page.
1846 vm_page_set_valid_range(m, 0, size - toff);
1847 KASSERT(m->dirty == 0,
1848 ("fuse_getpages: page %p is dirty", m));
1851 * Read operation was short. If no error occured
1852 * we may have hit a zero-fill section. We simply
1853 * leave valid set to 0.
1857 if (i != ap->a_reqpage)
1858 vm_page_readahead_finish(m);
1860 fuse_vm_page_unlock_queues();
1861 VM_OBJECT_WUNLOCK(vp->v_object);
1866 struct vnop_putpages_args {
1872 vm_ooffset_t a_offset;
1876 fuse_vnop_putpages(struct vop_putpages_args *ap)
1882 int i, error, npages, count;
1891 FS_DEBUG2G("heh\n");
1894 KASSERT(vp->v_object, ("objectless vp passed to putpages"));
1895 fsize = vp->v_object->un_pager.vnp.vnp_size;
1896 td = curthread; /* XXX */
1897 cred = curthread->td_ucred; /* XXX */
1899 count = ap->a_count;
1900 rtvals = ap->a_rtvals;
1901 npages = btoc(count);
1902 offset = IDX_TO_OFF(pages[0]->pindex);
1904 if (!fsess_opt_mmap(vnode_mount(vp))) {
1905 FS_DEBUG("called on non-cacheable vnode??\n");
1907 for (i = 0; i < npages; i++)
1908 rtvals[i] = VM_PAGER_AGAIN;
1911 * When putting pages, do not extend file past EOF.
1914 if (offset + count > fsize) {
1915 count = fsize - offset;
1920 * We use only the kva address for the buffer, but this is extremely
1921 * convienient and fast.
1923 bp = getpbuf(&fuse_pbuf_freecnt);
1925 kva = (vm_offset_t)bp->b_data;
1926 pmap_qenter(kva, pages, npages);
1927 PCPU_INC(cnt.v_vnodeout);
1928 PCPU_ADD(cnt.v_vnodepgsout, count);
1930 iov.iov_base = (caddr_t)kva;
1931 iov.iov_len = count;
1934 uio.uio_offset = offset;
1935 uio.uio_resid = count;
1936 uio.uio_segflg = UIO_SYSSPACE;
1937 uio.uio_rw = UIO_WRITE;
1940 error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred);
1942 pmap_qremove(kva, npages);
1943 relpbuf(bp, &fuse_pbuf_freecnt);
1946 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
1948 for (i = 0; i < nwritten; i++) {
1949 rtvals[i] = VM_PAGER_OK;
1950 VM_OBJECT_WLOCK(pages[i]->object);
1951 vm_page_undirty(pages[i]);
1952 VM_OBJECT_WUNLOCK(pages[i]->object);
1959 struct vnop_print_args {
1964 fuse_vnop_print(struct vop_print_args *ap)
1966 struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp);
1968 printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n",
1969 (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid,
1970 (uintmax_t)fvdat->nlookup,