151369649SPedro F. Giffuni /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 45fe58019SAttilio Rao * Copyright (c) 2007-2009 Google Inc. and Amit Singh 55fe58019SAttilio Rao * All rights reserved. 65fe58019SAttilio Rao * 75fe58019SAttilio Rao * Redistribution and use in source and binary forms, with or without 85fe58019SAttilio Rao * modification, are permitted provided that the following conditions are 95fe58019SAttilio Rao * met: 105fe58019SAttilio Rao * 115fe58019SAttilio Rao * * Redistributions of source code must retain the above copyright 125fe58019SAttilio Rao * notice, this list of conditions and the following disclaimer. 135fe58019SAttilio Rao * * Redistributions in binary form must reproduce the above 145fe58019SAttilio Rao * copyright notice, this list of conditions and the following disclaimer 155fe58019SAttilio Rao * in the documentation and/or other materials provided with the 165fe58019SAttilio Rao * distribution. 175fe58019SAttilio Rao * * Neither the name of Google Inc. nor the names of its 185fe58019SAttilio Rao * contributors may be used to endorse or promote products derived from 195fe58019SAttilio Rao * this software without specific prior written permission. 205fe58019SAttilio Rao * 215fe58019SAttilio Rao * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 225fe58019SAttilio Rao * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 235fe58019SAttilio Rao * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 245fe58019SAttilio Rao * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 255fe58019SAttilio Rao * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 265fe58019SAttilio Rao * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 275fe58019SAttilio Rao * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 285fe58019SAttilio Rao * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 295fe58019SAttilio Rao * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 305fe58019SAttilio Rao * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 315fe58019SAttilio Rao * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 325fe58019SAttilio Rao * 335fe58019SAttilio Rao * Copyright (C) 2005 Csaba Henk. 345fe58019SAttilio Rao * All rights reserved. 355fe58019SAttilio Rao * 365fe58019SAttilio Rao * Redistribution and use in source and binary forms, with or without 375fe58019SAttilio Rao * modification, are permitted provided that the following conditions 385fe58019SAttilio Rao * are met: 395fe58019SAttilio Rao * 1. Redistributions of source code must retain the above copyright 405fe58019SAttilio Rao * notice, this list of conditions and the following disclaimer. 415fe58019SAttilio Rao * 2. Redistributions in binary form must reproduce the above copyright 425fe58019SAttilio Rao * notice, this list of conditions and the following disclaimer in the 435fe58019SAttilio Rao * documentation and/or other materials provided with the distribution. 445fe58019SAttilio Rao * 455fe58019SAttilio Rao * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 465fe58019SAttilio Rao * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 475fe58019SAttilio Rao * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 485fe58019SAttilio Rao * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 495fe58019SAttilio Rao * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 505fe58019SAttilio Rao * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 515fe58019SAttilio Rao * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 525fe58019SAttilio Rao * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 535fe58019SAttilio Rao * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 545fe58019SAttilio Rao * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 555fe58019SAttilio Rao * SUCH DAMAGE. 565fe58019SAttilio Rao */ 575fe58019SAttilio Rao 585fe58019SAttilio Rao #include <sys/cdefs.h> 595fe58019SAttilio Rao __FBSDID("$FreeBSD$"); 605fe58019SAttilio Rao 61cf169498SAlan Somers #include <sys/param.h> 625fe58019SAttilio Rao #include <sys/module.h> 635fe58019SAttilio Rao #include <sys/systm.h> 645fe58019SAttilio Rao #include <sys/errno.h> 655fe58019SAttilio Rao #include <sys/kernel.h> 665fe58019SAttilio Rao #include <sys/conf.h> 675fe58019SAttilio Rao #include <sys/uio.h> 685fe58019SAttilio Rao #include <sys/malloc.h> 695fe58019SAttilio Rao #include <sys/queue.h> 705fe58019SAttilio Rao #include <sys/lock.h> 7189f6b863SAttilio Rao #include <sys/rwlock.h> 725fe58019SAttilio Rao #include <sys/sx.h> 735fe58019SAttilio Rao #include <sys/proc.h> 745fe58019SAttilio Rao #include <sys/mount.h> 755fe58019SAttilio Rao #include <sys/vnode.h> 765fe58019SAttilio Rao #include <sys/namei.h> 7704660064SFedor Uporov #include <sys/extattr.h> 785fe58019SAttilio Rao #include <sys/stat.h> 795fe58019SAttilio Rao #include <sys/unistd.h> 805fe58019SAttilio Rao #include <sys/filedesc.h> 815fe58019SAttilio Rao #include <sys/file.h> 825fe58019SAttilio Rao #include <sys/fcntl.h> 835fe58019SAttilio Rao #include <sys/dirent.h> 845fe58019SAttilio Rao #include <sys/bio.h> 855fe58019SAttilio Rao #include <sys/buf.h> 865fe58019SAttilio Rao #include <sys/sysctl.h> 87ca148cdaSGleb Smirnoff #include <sys/vmmeter.h> 885fe58019SAttilio Rao 895fe58019SAttilio Rao #include <vm/vm.h> 905fe58019SAttilio Rao #include <vm/vm_extern.h> 915fe58019SAttilio Rao #include <vm/pmap.h> 925fe58019SAttilio Rao #include <vm/vm_map.h> 935fe58019SAttilio Rao #include <vm/vm_page.h> 945fe58019SAttilio Rao #include <vm/vm_param.h> 955fe58019SAttilio Rao #include <vm/vm_object.h> 965fe58019SAttilio Rao #include <vm/vm_pager.h> 975fe58019SAttilio Rao #include <vm/vnode_pager.h> 985fe58019SAttilio Rao #include <vm/vm_object.h> 995fe58019SAttilio Rao 1005fe58019SAttilio Rao #include "fuse.h" 1015fe58019SAttilio Rao #include "fuse_file.h" 1025fe58019SAttilio Rao #include "fuse_internal.h" 1035fe58019SAttilio Rao #include "fuse_ipc.h" 1045fe58019SAttilio Rao #include "fuse_node.h" 1055fe58019SAttilio Rao #include "fuse_io.h" 1065fe58019SAttilio Rao 1075fe58019SAttilio Rao #include <sys/priv.h> 1085fe58019SAttilio Rao 109fd2749f2SAlan Somers /* Maximum number of hardlinks to a single FUSE file */ 110fd2749f2SAlan Somers #define FUSE_LINK_MAX UINT32_MAX 111fd2749f2SAlan Somers 112cf169498SAlan Somers SDT_PROVIDER_DECLARE(fuse); 113cf169498SAlan Somers /* 114cf169498SAlan Somers * Fuse trace probe: 115cf169498SAlan Somers * arg0: verbosity. Higher numbers give more verbose messages 116cf169498SAlan Somers * arg1: Textual message 117cf169498SAlan Somers */ 118cf169498SAlan Somers SDT_PROBE_DEFINE2(fuse, , vnops, trace, "int", "char*"); 1195fe58019SAttilio Rao 1205fe58019SAttilio Rao /* vnode ops */ 1215fe58019SAttilio Rao static vop_access_t fuse_vnop_access; 1225fe58019SAttilio Rao static vop_close_t fuse_vnop_close; 1235fe58019SAttilio Rao static vop_create_t fuse_vnop_create; 12404660064SFedor Uporov static vop_deleteextattr_t fuse_vnop_deleteextattr; 125915012e0SAlan Somers static vop_fdatasync_t fuse_vnop_fdatasync; 1265fe58019SAttilio Rao static vop_fsync_t fuse_vnop_fsync; 1275fe58019SAttilio Rao static vop_getattr_t fuse_vnop_getattr; 12804660064SFedor Uporov static vop_getextattr_t fuse_vnop_getextattr; 1295fe58019SAttilio Rao static vop_inactive_t fuse_vnop_inactive; 1305fe58019SAttilio Rao static vop_link_t fuse_vnop_link; 13104660064SFedor Uporov static vop_listextattr_t fuse_vnop_listextattr; 1325fe58019SAttilio Rao static vop_lookup_t fuse_vnop_lookup; 1335fe58019SAttilio Rao static vop_mkdir_t fuse_vnop_mkdir; 1345fe58019SAttilio Rao static vop_mknod_t fuse_vnop_mknod; 1355fe58019SAttilio Rao static vop_open_t fuse_vnop_open; 136746c92e0SJohn Baldwin static vop_pathconf_t fuse_vnop_pathconf; 1375fe58019SAttilio Rao static vop_read_t fuse_vnop_read; 1385fe58019SAttilio Rao static vop_readdir_t fuse_vnop_readdir; 1395fe58019SAttilio Rao static vop_readlink_t fuse_vnop_readlink; 1405fe58019SAttilio Rao static vop_reclaim_t fuse_vnop_reclaim; 1415fe58019SAttilio Rao static vop_remove_t fuse_vnop_remove; 1425fe58019SAttilio Rao static vop_rename_t fuse_vnop_rename; 1435fe58019SAttilio Rao static vop_rmdir_t fuse_vnop_rmdir; 1445fe58019SAttilio Rao static vop_setattr_t fuse_vnop_setattr; 14504660064SFedor Uporov static vop_setextattr_t fuse_vnop_setextattr; 1465fe58019SAttilio Rao static vop_strategy_t fuse_vnop_strategy; 1475fe58019SAttilio Rao static vop_symlink_t fuse_vnop_symlink; 1485fe58019SAttilio Rao static vop_write_t fuse_vnop_write; 1495fe58019SAttilio Rao static vop_getpages_t fuse_vnop_getpages; 1505fe58019SAttilio Rao static vop_putpages_t fuse_vnop_putpages; 1515fe58019SAttilio Rao static vop_print_t fuse_vnop_print; 1525fe58019SAttilio Rao 1535fe58019SAttilio Rao struct vop_vector fuse_vnops = { 1545fe58019SAttilio Rao .vop_default = &default_vnodeops, 1555fe58019SAttilio Rao .vop_access = fuse_vnop_access, 1565fe58019SAttilio Rao .vop_close = fuse_vnop_close, 1575fe58019SAttilio Rao .vop_create = fuse_vnop_create, 15804660064SFedor Uporov .vop_deleteextattr = fuse_vnop_deleteextattr, 1595fe58019SAttilio Rao .vop_fsync = fuse_vnop_fsync, 160915012e0SAlan Somers .vop_fdatasync = fuse_vnop_fdatasync, 1615fe58019SAttilio Rao .vop_getattr = fuse_vnop_getattr, 16204660064SFedor Uporov .vop_getextattr = fuse_vnop_getextattr, 1635fe58019SAttilio Rao .vop_inactive = fuse_vnop_inactive, 1645fe58019SAttilio Rao .vop_link = fuse_vnop_link, 16504660064SFedor Uporov .vop_listextattr = fuse_vnop_listextattr, 1665fe58019SAttilio Rao .vop_lookup = fuse_vnop_lookup, 1675fe58019SAttilio Rao .vop_mkdir = fuse_vnop_mkdir, 1685fe58019SAttilio Rao .vop_mknod = fuse_vnop_mknod, 1695fe58019SAttilio Rao .vop_open = fuse_vnop_open, 170746c92e0SJohn Baldwin .vop_pathconf = fuse_vnop_pathconf, 1715fe58019SAttilio Rao .vop_read = fuse_vnop_read, 1725fe58019SAttilio Rao .vop_readdir = fuse_vnop_readdir, 1735fe58019SAttilio Rao .vop_readlink = fuse_vnop_readlink, 1745fe58019SAttilio Rao .vop_reclaim = fuse_vnop_reclaim, 1755fe58019SAttilio Rao .vop_remove = fuse_vnop_remove, 1765fe58019SAttilio Rao .vop_rename = fuse_vnop_rename, 1775fe58019SAttilio Rao .vop_rmdir = fuse_vnop_rmdir, 1785fe58019SAttilio Rao .vop_setattr = fuse_vnop_setattr, 17904660064SFedor Uporov .vop_setextattr = fuse_vnop_setextattr, 1805fe58019SAttilio Rao .vop_strategy = fuse_vnop_strategy, 1815fe58019SAttilio Rao .vop_symlink = fuse_vnop_symlink, 1825fe58019SAttilio Rao .vop_write = fuse_vnop_write, 1835fe58019SAttilio Rao .vop_getpages = fuse_vnop_getpages, 1845fe58019SAttilio Rao .vop_putpages = fuse_vnop_putpages, 1855fe58019SAttilio Rao .vop_print = fuse_vnop_print, 1865fe58019SAttilio Rao }; 1875fe58019SAttilio Rao 1885fe58019SAttilio Rao static u_long fuse_lookup_cache_hits = 0; 1895fe58019SAttilio Rao 190123af6ecSAlan Somers SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_hits, CTLFLAG_RD, 191bd4cb2a4SConrad Meyer &fuse_lookup_cache_hits, 0, "number of positive cache hits in lookup"); 1925fe58019SAttilio Rao 1935fe58019SAttilio Rao static u_long fuse_lookup_cache_misses = 0; 1945fe58019SAttilio Rao 195123af6ecSAlan Somers SYSCTL_ULONG(_vfs_fusefs, OID_AUTO, lookup_cache_misses, CTLFLAG_RD, 196bd4cb2a4SConrad Meyer &fuse_lookup_cache_misses, 0, "number of cache misses in lookup"); 1975fe58019SAttilio Rao 1985fe58019SAttilio Rao /* 1995fe58019SAttilio Rao * XXX: This feature is highly experimental and can bring to instabilities, 2005fe58019SAttilio Rao * needs revisiting before to be enabled by default. 2015fe58019SAttilio Rao */ 2025fe58019SAttilio Rao static int fuse_reclaim_revoked = 0; 2035fe58019SAttilio Rao 204123af6ecSAlan Somers SYSCTL_INT(_vfs_fusefs, OID_AUTO, reclaim_revoked, CTLFLAG_RW, 2055fe58019SAttilio Rao &fuse_reclaim_revoked, 0, ""); 2065fe58019SAttilio Rao 207756a5412SGleb Smirnoff uma_zone_t fuse_pbuf_zone; 2085fe58019SAttilio Rao 2095fe58019SAttilio Rao #define fuse_vm_page_lock(m) vm_page_lock((m)); 2105fe58019SAttilio Rao #define fuse_vm_page_unlock(m) vm_page_unlock((m)); 2115fe58019SAttilio Rao #define fuse_vm_page_lock_queues() ((void)0) 2125fe58019SAttilio Rao #define fuse_vm_page_unlock_queues() ((void)0) 2135fe58019SAttilio Rao 214666f8543SAlan Somers /* Check permission for extattr operations, much like extattr_check_cred */ 215666f8543SAlan Somers static int 216666f8543SAlan Somers fuse_extattr_check_cred(struct vnode *vp, int ns, struct ucred *cred, 217666f8543SAlan Somers struct thread *td, accmode_t accmode) 218666f8543SAlan Somers { 219666f8543SAlan Somers struct mount *mp = vnode_mount(vp); 220666f8543SAlan Somers struct fuse_data *data = fuse_get_mpdata(mp); 221666f8543SAlan Somers 222666f8543SAlan Somers /* 223666f8543SAlan Somers * Kernel-invoked always succeeds. 224666f8543SAlan Somers */ 225666f8543SAlan Somers if (cred == NOCRED) 226666f8543SAlan Somers return (0); 227666f8543SAlan Somers 228666f8543SAlan Somers /* 229666f8543SAlan Somers * Do not allow privileged processes in jail to directly manipulate 230666f8543SAlan Somers * system attributes. 231666f8543SAlan Somers */ 232666f8543SAlan Somers switch (ns) { 233666f8543SAlan Somers case EXTATTR_NAMESPACE_SYSTEM: 234666f8543SAlan Somers if (data->dataflags & FSESS_DEFAULT_PERMISSIONS) { 235666f8543SAlan Somers return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM)); 236666f8543SAlan Somers } 237666f8543SAlan Somers /* FALLTHROUGH */ 238666f8543SAlan Somers case EXTATTR_NAMESPACE_USER: 239666f8543SAlan Somers return (fuse_internal_access(vp, accmode, td, cred)); 240666f8543SAlan Somers default: 241666f8543SAlan Somers return (EPERM); 242666f8543SAlan Somers } 243666f8543SAlan Somers } 244666f8543SAlan Somers 245f8d4af10SAlan Somers /* Get a filehandle for a directory */ 246f8d4af10SAlan Somers static int 247f8d4af10SAlan Somers fuse_filehandle_get_dir(struct vnode *vp, struct fuse_filehandle **fufhp, 248f8d4af10SAlan Somers struct ucred *cred, pid_t pid) 249f8d4af10SAlan Somers { 2509f10f423SAlan Somers if (fuse_filehandle_get(vp, FREAD, fufhp, cred, pid) == 0) 251f8d4af10SAlan Somers return 0; 2529f10f423SAlan Somers return fuse_filehandle_get(vp, FEXEC, fufhp, cred, pid); 2539f10f423SAlan Somers } 2549f10f423SAlan Somers 2559f10f423SAlan Somers /* Send FUSE_FLUSH for this vnode */ 2569f10f423SAlan Somers static int 2579f10f423SAlan Somers fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag) 2589f10f423SAlan Somers { 2599f10f423SAlan Somers struct fuse_flush_in *ffi; 2609f10f423SAlan Somers struct fuse_filehandle *fufh; 2619f10f423SAlan Somers struct fuse_dispatcher fdi; 2629f10f423SAlan Somers struct thread *td = curthread; 2639f10f423SAlan Somers struct mount *mp = vnode_mount(vp); 2649f10f423SAlan Somers int err; 2659f10f423SAlan Somers 2669f10f423SAlan Somers if (!fsess_isimpl(vnode_mount(vp), FUSE_FLUSH)) 2679f10f423SAlan Somers return 0; 2689f10f423SAlan Somers 2699f10f423SAlan Somers err = fuse_filehandle_get(vp, fflag, &fufh, cred, pid); 2709f10f423SAlan Somers if (err) 2719f10f423SAlan Somers return err; 2729f10f423SAlan Somers 2739f10f423SAlan Somers fdisp_init(&fdi, sizeof(*ffi)); 2749f10f423SAlan Somers fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred); 2759f10f423SAlan Somers ffi = fdi.indata; 2769f10f423SAlan Somers ffi->fh = fufh->fh_id; 2779f10f423SAlan Somers 2789f10f423SAlan Somers err = fdisp_wait_answ(&fdi); 2799f10f423SAlan Somers if (err == ENOSYS) { 2809f10f423SAlan Somers fsess_set_notimpl(mp, FUSE_FLUSH); 2819f10f423SAlan Somers err = 0; 2829f10f423SAlan Somers } 2839f10f423SAlan Somers fdisp_destroy(&fdi); 2849f10f423SAlan Somers return err; 285f8d4af10SAlan Somers } 286f8d4af10SAlan Somers 2875fe58019SAttilio Rao /* 2885fe58019SAttilio Rao struct vnop_access_args { 2895fe58019SAttilio Rao struct vnode *a_vp; 2905fe58019SAttilio Rao #if VOP_ACCESS_TAKES_ACCMODE_T 2915fe58019SAttilio Rao accmode_t a_accmode; 2925fe58019SAttilio Rao #else 2935fe58019SAttilio Rao int a_mode; 2945fe58019SAttilio Rao #endif 2955fe58019SAttilio Rao struct ucred *a_cred; 2965fe58019SAttilio Rao struct thread *a_td; 2975fe58019SAttilio Rao }; 2985fe58019SAttilio Rao */ 2995fe58019SAttilio Rao static int 3005fe58019SAttilio Rao fuse_vnop_access(struct vop_access_args *ap) 3015fe58019SAttilio Rao { 3025fe58019SAttilio Rao struct vnode *vp = ap->a_vp; 3035fe58019SAttilio Rao int accmode = ap->a_accmode; 3045fe58019SAttilio Rao struct ucred *cred = ap->a_cred; 3055fe58019SAttilio Rao 3065fe58019SAttilio Rao struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp)); 3075fe58019SAttilio Rao 3085fe58019SAttilio Rao int err; 3095fe58019SAttilio Rao 3105fe58019SAttilio Rao if (fuse_isdeadfs(vp)) { 3115fe58019SAttilio Rao if (vnode_isvroot(vp)) { 3125fe58019SAttilio Rao return 0; 3135fe58019SAttilio Rao } 3145fe58019SAttilio Rao return ENXIO; 3155fe58019SAttilio Rao } 3165fe58019SAttilio Rao if (!(data->dataflags & FSESS_INITED)) { 3175fe58019SAttilio Rao if (vnode_isvroot(vp)) { 318cc426dd3SMateusz Guzik if (priv_check_cred(cred, PRIV_VFS_ADMIN) || 3195fe58019SAttilio Rao (fuse_match_cred(data->daemoncred, cred) == 0)) { 3205fe58019SAttilio Rao return 0; 3215fe58019SAttilio Rao } 3225fe58019SAttilio Rao } 3235fe58019SAttilio Rao return EBADF; 3245fe58019SAttilio Rao } 3255fe58019SAttilio Rao if (vnode_islnk(vp)) { 3265fe58019SAttilio Rao return 0; 3275fe58019SAttilio Rao } 3285fe58019SAttilio Rao 329666f8543SAlan Somers err = fuse_internal_access(vp, accmode, ap->a_td, ap->a_cred); 3305fe58019SAttilio Rao return err; 3315fe58019SAttilio Rao } 3325fe58019SAttilio Rao 3335fe58019SAttilio Rao /* 3349f10f423SAlan Somers struct vop_close_args { 3355fe58019SAttilio Rao struct vnode *a_vp; 3365fe58019SAttilio Rao int a_fflag; 3375fe58019SAttilio Rao struct ucred *a_cred; 3385fe58019SAttilio Rao struct thread *a_td; 3395fe58019SAttilio Rao }; 3405fe58019SAttilio Rao */ 3415fe58019SAttilio Rao static int 3425fe58019SAttilio Rao fuse_vnop_close(struct vop_close_args *ap) 3435fe58019SAttilio Rao { 3445fe58019SAttilio Rao struct vnode *vp = ap->a_vp; 3455fe58019SAttilio Rao struct ucred *cred = ap->a_cred; 3465fe58019SAttilio Rao int fflag = ap->a_fflag; 347f8d4af10SAlan Somers struct thread *td = ap->a_td; 348f8d4af10SAlan Somers pid_t pid = td->td_proc->p_pid; 3499f10f423SAlan Somers int err = 0; 3505fe58019SAttilio Rao 35135cf0e7eSAlan Somers if (fuse_isdeadfs(vp)) 3525fe58019SAttilio Rao return 0; 35335cf0e7eSAlan Somers if (vnode_isdir(vp)) 35435cf0e7eSAlan Somers return 0; 35535cf0e7eSAlan Somers if (fflag & IO_NDELAY) 35635cf0e7eSAlan Somers return 0; 3575ec10aa5SAlan Somers 3589f10f423SAlan Somers err = fuse_flush(vp, cred, pid, fflag); 3595ec10aa5SAlan Somers /* TODO: close the file handle, if we're sure it's no longer used */ 3605fe58019SAttilio Rao if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) { 361f8d4af10SAlan Somers fuse_vnode_savesize(vp, cred, td->td_proc->p_pid); 3625fe58019SAttilio Rao } 3639f10f423SAlan Somers return err; 3645fe58019SAttilio Rao } 3655fe58019SAttilio Rao 36619ef317dSAlan Somers static void 36719ef317dSAlan Somers fdisp_make_mknod_for_fallback( 36819ef317dSAlan Somers struct fuse_dispatcher *fdip, 36919ef317dSAlan Somers struct componentname *cnp, 37019ef317dSAlan Somers struct vnode *dvp, 37119ef317dSAlan Somers uint64_t parentnid, 37219ef317dSAlan Somers struct thread *td, 37319ef317dSAlan Somers struct ucred *cred, 37419ef317dSAlan Somers mode_t mode, 37519ef317dSAlan Somers enum fuse_opcode *op) 37619ef317dSAlan Somers { 37719ef317dSAlan Somers struct fuse_mknod_in *fmni; 37819ef317dSAlan Somers 37919ef317dSAlan Somers fdisp_init(fdip, sizeof(*fmni) + cnp->cn_namelen + 1); 38019ef317dSAlan Somers *op = FUSE_MKNOD; 38119ef317dSAlan Somers fdisp_make(fdip, *op, vnode_mount(dvp), parentnid, td, cred); 38219ef317dSAlan Somers fmni = fdip->indata; 38319ef317dSAlan Somers fmni->mode = mode; 38419ef317dSAlan Somers fmni->rdev = 0; 38519ef317dSAlan Somers memcpy((char *)fdip->indata + sizeof(*fmni), cnp->cn_nameptr, 38619ef317dSAlan Somers cnp->cn_namelen); 38719ef317dSAlan Somers ((char *)fdip->indata)[sizeof(*fmni) + cnp->cn_namelen] = '\0'; 38819ef317dSAlan Somers } 3895fe58019SAttilio Rao /* 3905fe58019SAttilio Rao struct vnop_create_args { 3915fe58019SAttilio Rao struct vnode *a_dvp; 3925fe58019SAttilio Rao struct vnode **a_vpp; 3935fe58019SAttilio Rao struct componentname *a_cnp; 3945fe58019SAttilio Rao struct vattr *a_vap; 3955fe58019SAttilio Rao }; 3965fe58019SAttilio Rao */ 3975fe58019SAttilio Rao static int 3985fe58019SAttilio Rao fuse_vnop_create(struct vop_create_args *ap) 3995fe58019SAttilio Rao { 4005fe58019SAttilio Rao struct vnode *dvp = ap->a_dvp; 4015fe58019SAttilio Rao struct vnode **vpp = ap->a_vpp; 4025fe58019SAttilio Rao struct componentname *cnp = ap->a_cnp; 4035fe58019SAttilio Rao struct vattr *vap = ap->a_vap; 4045fe58019SAttilio Rao struct thread *td = cnp->cn_thread; 4055fe58019SAttilio Rao struct ucred *cred = cnp->cn_cred; 4065fe58019SAttilio Rao 4075fe58019SAttilio Rao struct fuse_open_in *foi; 4085fe58019SAttilio Rao struct fuse_entry_out *feo; 40919ef317dSAlan Somers struct fuse_open_out *foo; 41019ef317dSAlan Somers struct fuse_dispatcher fdi, fdi2; 4115fe58019SAttilio Rao struct fuse_dispatcher *fdip = &fdi; 41219ef317dSAlan Somers struct fuse_dispatcher *fdip2 = NULL; 4135fe58019SAttilio Rao 4145fe58019SAttilio Rao int err; 4155fe58019SAttilio Rao 4165fe58019SAttilio Rao struct mount *mp = vnode_mount(dvp); 4175fe58019SAttilio Rao uint64_t parentnid = VTOFUD(dvp)->nid; 4185fe58019SAttilio Rao mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode); 41919ef317dSAlan Somers enum fuse_opcode op; 4209e444871SAlan Somers int flags; 4219e444871SAlan Somers 4229e444871SAlan Somers /* 4239e444871SAlan Somers * VOP_CREATE doesn't tell us the open(2) flags, so we guess. Only a 4249e444871SAlan Somers * writable mode makes sense, and we might as well include readability 4259e444871SAlan Somers * too. 4269e444871SAlan Somers */ 4279e444871SAlan Somers flags = O_RDWR; 4285fe58019SAttilio Rao 4295fe58019SAttilio Rao if (fuse_isdeadfs(dvp)) { 4305fe58019SAttilio Rao return ENXIO; 4315fe58019SAttilio Rao } 4325fe58019SAttilio Rao bzero(&fdi, sizeof(fdi)); 4335fe58019SAttilio Rao 43419ef317dSAlan Somers if ((vap->va_type != VREG)) 435372b97d0SRick Macklem return (EINVAL); 4365fe58019SAttilio Rao 4375fe58019SAttilio Rao if (!fsess_isimpl(mp, FUSE_CREATE)) { 43819ef317dSAlan Somers /* Fallback to FUSE_MKNOD/FUSE_OPEN */ 43919ef317dSAlan Somers fdisp_make_mknod_for_fallback(fdip, cnp, dvp, parentnid, td, 44019ef317dSAlan Somers cred, mode, &op); 44119ef317dSAlan Somers } else { 44219ef317dSAlan Somers /* Use FUSE_CREATE */ 44319ef317dSAlan Somers op = FUSE_CREATE; 44419ef317dSAlan Somers fdisp_init(fdip, sizeof(*foi) + cnp->cn_namelen + 1); 44519ef317dSAlan Somers fdisp_make(fdip, op, vnode_mount(dvp), parentnid, td, cred); 4465fe58019SAttilio Rao foi = fdip->indata; 4475fe58019SAttilio Rao foi->mode = mode; 4489e444871SAlan Somers foi->flags = O_CREAT | flags; 4495fe58019SAttilio Rao memcpy((char *)fdip->indata + sizeof(*foi), cnp->cn_nameptr, 4505fe58019SAttilio Rao cnp->cn_namelen); 4515fe58019SAttilio Rao ((char *)fdip->indata)[sizeof(*foi) + cnp->cn_namelen] = '\0'; 45219ef317dSAlan Somers } 4535fe58019SAttilio Rao 4545fe58019SAttilio Rao err = fdisp_wait_answ(fdip); 4555fe58019SAttilio Rao 4566de3b00dSAttilio Rao if (err) { 45719ef317dSAlan Somers if (err == ENOSYS && op == FUSE_CREATE) { 4585fe58019SAttilio Rao fsess_set_notimpl(mp, FUSE_CREATE); 45919ef317dSAlan Somers fdisp_make_mknod_for_fallback(fdip, cnp, dvp, 46019ef317dSAlan Somers parentnid, td, cred, mode, &op); 46119ef317dSAlan Somers err = fdisp_wait_answ(fdip); 46219ef317dSAlan Somers } 46319ef317dSAlan Somers if (err) 4645fe58019SAttilio Rao goto out; 4655fe58019SAttilio Rao } 466372b97d0SRick Macklem 4675fe58019SAttilio Rao feo = fdip->answ; 4685fe58019SAttilio Rao 4695fe58019SAttilio Rao if ((err = fuse_internal_checkentry(feo, VREG))) { 4705fe58019SAttilio Rao goto out; 4715fe58019SAttilio Rao } 47219ef317dSAlan Somers 47319ef317dSAlan Somers if (op == FUSE_CREATE) { 47419ef317dSAlan Somers foo = (struct fuse_open_out*)(feo + 1); 47519ef317dSAlan Somers } else { 47619ef317dSAlan Somers /* Issue a separate FUSE_OPEN */ 47719ef317dSAlan Somers fdip2 = &fdi2; 47819ef317dSAlan Somers fdisp_init(fdip2, sizeof(*foi)); 47919ef317dSAlan Somers fdisp_make(fdip2, FUSE_OPEN, vnode_mount(dvp), feo->nodeid, td, 48019ef317dSAlan Somers cred); 48119ef317dSAlan Somers foi = fdip2->indata; 48219ef317dSAlan Somers foi->mode = mode; 4839e444871SAlan Somers foi->flags = flags; 48419ef317dSAlan Somers err = fdisp_wait_answ(fdip2); 48519ef317dSAlan Somers if (err) 48619ef317dSAlan Somers goto out; 48719ef317dSAlan Somers foo = fdip2->answ; 48819ef317dSAlan Somers } 48909176f09SConrad Meyer err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG); 4905fe58019SAttilio Rao if (err) { 4915fe58019SAttilio Rao struct fuse_release_in *fri; 4925fe58019SAttilio Rao uint64_t nodeid = feo->nodeid; 49319ef317dSAlan Somers uint64_t fh_id = foo->fh; 4945fe58019SAttilio Rao 4955fe58019SAttilio Rao fdisp_init(fdip, sizeof(*fri)); 4965fe58019SAttilio Rao fdisp_make(fdip, FUSE_RELEASE, mp, nodeid, td, cred); 4975fe58019SAttilio Rao fri = fdip->indata; 4985fe58019SAttilio Rao fri->fh = fh_id; 4999e444871SAlan Somers fri->flags = flags; 5005fe58019SAttilio Rao fuse_insert_callback(fdip->tick, fuse_internal_forget_callback); 5015fe58019SAttilio Rao fuse_insert_message(fdip->tick); 50219ef317dSAlan Somers goto out; 5035fe58019SAttilio Rao } 5045fe58019SAttilio Rao ASSERT_VOP_ELOCKED(*vpp, "fuse_vnop_create"); 505cad67791SAlan Somers fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid, 506cad67791SAlan Somers feo->attr_valid_nsec, NULL); 5075fe58019SAttilio Rao 508a7e81cb3SAlan Somers fuse_filehandle_init(*vpp, FUFH_RDWR, NULL, td, cred, foo); 50919ef317dSAlan Somers fuse_vnode_open(*vpp, foo->open_flags, td); 5105fe58019SAttilio Rao cache_purge_negative(dvp); 5115fe58019SAttilio Rao 5125fe58019SAttilio Rao out: 51319ef317dSAlan Somers if (fdip2) 51419ef317dSAlan Somers fdisp_destroy(fdip2); 5155fe58019SAttilio Rao fdisp_destroy(fdip); 5165fe58019SAttilio Rao return err; 5175fe58019SAttilio Rao } 5185fe58019SAttilio Rao 5195fe58019SAttilio Rao /* 520915012e0SAlan Somers struct vnop_fdatasync_args { 521915012e0SAlan Somers struct vop_generic_args a_gen; 522915012e0SAlan Somers struct vnode * a_vp; 523915012e0SAlan Somers struct thread * a_td; 524915012e0SAlan Somers }; 5255fe58019SAttilio Rao */ 526915012e0SAlan Somers static int 527915012e0SAlan Somers fuse_vnop_fdatasync(struct vop_fdatasync_args *ap) 528915012e0SAlan Somers { 529915012e0SAlan Somers struct vnode *vp = ap->a_vp; 530915012e0SAlan Somers struct thread *td = ap->a_td; 531915012e0SAlan Somers int waitfor = MNT_WAIT; 532915012e0SAlan Somers 533915012e0SAlan Somers int err = 0; 534915012e0SAlan Somers 535915012e0SAlan Somers if (fuse_isdeadfs(vp)) { 536915012e0SAlan Somers return 0; 537915012e0SAlan Somers } 538915012e0SAlan Somers if ((err = vop_stdfdatasync_buf(ap))) 539915012e0SAlan Somers return err; 540915012e0SAlan Somers 541915012e0SAlan Somers return fuse_internal_fsync(vp, td, waitfor, true); 542915012e0SAlan Somers } 5435fe58019SAttilio Rao 5445fe58019SAttilio Rao /* 5455fe58019SAttilio Rao struct vnop_fsync_args { 546915012e0SAlan Somers struct vop_generic_args a_gen; 5475fe58019SAttilio Rao struct vnode * a_vp; 5485fe58019SAttilio Rao int a_waitfor; 5495fe58019SAttilio Rao struct thread * a_td; 5505fe58019SAttilio Rao }; 5515fe58019SAttilio Rao */ 5525fe58019SAttilio Rao static int 5535fe58019SAttilio Rao fuse_vnop_fsync(struct vop_fsync_args *ap) 5545fe58019SAttilio Rao { 5555fe58019SAttilio Rao struct vnode *vp = ap->a_vp; 5565fe58019SAttilio Rao struct thread *td = ap->a_td; 55790612f3cSAlan Somers int waitfor = ap->a_waitfor; 558915012e0SAlan Somers int err = 0; 5595fe58019SAttilio Rao 5605fe58019SAttilio Rao if (fuse_isdeadfs(vp)) { 5615fe58019SAttilio Rao return 0; 5625fe58019SAttilio Rao } 5635fe58019SAttilio Rao if ((err = vop_stdfsync(ap))) 5645fe58019SAttilio Rao return err; 5655fe58019SAttilio Rao 566915012e0SAlan Somers return fuse_internal_fsync(vp, td, waitfor, false); 5675fe58019SAttilio Rao } 5685fe58019SAttilio Rao 5695fe58019SAttilio Rao /* 5705fe58019SAttilio Rao struct vnop_getattr_args { 5715fe58019SAttilio Rao struct vnode *a_vp; 5725fe58019SAttilio Rao struct vattr *a_vap; 5735fe58019SAttilio Rao struct ucred *a_cred; 5745fe58019SAttilio Rao struct thread *a_td; 5755fe58019SAttilio Rao }; 5765fe58019SAttilio Rao */ 5775fe58019SAttilio Rao static int 5785fe58019SAttilio Rao fuse_vnop_getattr(struct vop_getattr_args *ap) 5795fe58019SAttilio Rao { 5805fe58019SAttilio Rao struct vnode *vp = ap->a_vp; 5815fe58019SAttilio Rao struct vattr *vap = ap->a_vap; 5825fe58019SAttilio Rao struct ucred *cred = ap->a_cred; 5835fe58019SAttilio Rao struct thread *td = curthread; 5845fe58019SAttilio Rao 5855fe58019SAttilio Rao int err = 0; 5865fe58019SAttilio Rao int dataflags; 5875fe58019SAttilio Rao 5885fe58019SAttilio Rao dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags; 5895fe58019SAttilio Rao 5905fe58019SAttilio Rao /* Note that we are not bailing out on a dead file system just yet. */ 5915fe58019SAttilio Rao 5925fe58019SAttilio Rao if (!(dataflags & FSESS_INITED)) { 5935fe58019SAttilio Rao if (!vnode_isvroot(vp)) { 5945fe58019SAttilio Rao fdata_set_dead(fuse_get_mpdata(vnode_mount(vp))); 5955fe58019SAttilio Rao err = ENOTCONN; 5965fe58019SAttilio Rao return err; 5975fe58019SAttilio Rao } else { 5985fe58019SAttilio Rao goto fake; 5995fe58019SAttilio Rao } 6005fe58019SAttilio Rao } 601cad67791SAlan Somers err = fuse_internal_getattr(vp, vap, cred, td); 602cad67791SAlan Somers if (err == ENOTCONN && vnode_isvroot(vp)) { 603cf169498SAlan Somers /* see comment in fuse_vfsop_statfs() */ 6045fe58019SAttilio Rao goto fake; 605cad67791SAlan Somers } else { 6065fe58019SAttilio Rao return err; 607cad67791SAlan Somers } 6085fe58019SAttilio Rao 6095fe58019SAttilio Rao fake: 6105fe58019SAttilio Rao bzero(vap, sizeof(*vap)); 6115fe58019SAttilio Rao vap->va_type = vnode_vtype(vp); 6125fe58019SAttilio Rao 6135fe58019SAttilio Rao return 0; 6145fe58019SAttilio Rao } 6155fe58019SAttilio Rao 6165fe58019SAttilio Rao /* 6175fe58019SAttilio Rao struct vnop_inactive_args { 6185fe58019SAttilio Rao struct vnode *a_vp; 6195fe58019SAttilio Rao struct thread *a_td; 6205fe58019SAttilio Rao }; 6215fe58019SAttilio Rao */ 6225fe58019SAttilio Rao static int 6235fe58019SAttilio Rao fuse_vnop_inactive(struct vop_inactive_args *ap) 6245fe58019SAttilio Rao { 6255fe58019SAttilio Rao struct vnode *vp = ap->a_vp; 6265fe58019SAttilio Rao struct thread *td = ap->a_td; 6275fe58019SAttilio Rao 6285fe58019SAttilio Rao struct fuse_vnode_data *fvdat = VTOFUD(vp); 6295ec10aa5SAlan Somers struct fuse_filehandle *fufh, *fufh_tmp; 6305fe58019SAttilio Rao 6315ec10aa5SAlan Somers int need_flush = 1; 6325fe58019SAttilio Rao 6335ec10aa5SAlan Somers LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) { 6345fe58019SAttilio Rao if (need_flush && vp->v_type == VREG) { 6355fe58019SAttilio Rao if ((VTOFUD(vp)->flag & FN_SIZECHANGE) != 0) { 636f8d4af10SAlan Somers fuse_vnode_savesize(vp, NULL, 0); 6375fe58019SAttilio Rao } 6385fe58019SAttilio Rao if (fuse_data_cache_invalidate || 6395fe58019SAttilio Rao (fvdat->flag & FN_REVOKED) != 0) 6405fe58019SAttilio Rao fuse_io_invalbuf(vp, td); 6415fe58019SAttilio Rao else 6425fe58019SAttilio Rao fuse_io_flushbuf(vp, MNT_WAIT, td); 6435fe58019SAttilio Rao need_flush = 0; 6445fe58019SAttilio Rao } 6455ec10aa5SAlan Somers fuse_filehandle_close(vp, fufh, td, NULL); 6465fe58019SAttilio Rao } 6475fe58019SAttilio Rao 6485fe58019SAttilio Rao if ((fvdat->flag & FN_REVOKED) != 0 && fuse_reclaim_revoked) { 6495fe58019SAttilio Rao vrecycle(vp); 6505fe58019SAttilio Rao } 6515fe58019SAttilio Rao return 0; 6525fe58019SAttilio Rao } 6535fe58019SAttilio Rao 6545fe58019SAttilio Rao /* 6555fe58019SAttilio Rao struct vnop_link_args { 6565fe58019SAttilio Rao struct vnode *a_tdvp; 6575fe58019SAttilio Rao struct vnode *a_vp; 6585fe58019SAttilio Rao struct componentname *a_cnp; 6595fe58019SAttilio Rao }; 6605fe58019SAttilio Rao */ 6615fe58019SAttilio Rao static int 6625fe58019SAttilio Rao fuse_vnop_link(struct vop_link_args *ap) 6635fe58019SAttilio Rao { 6645fe58019SAttilio Rao struct vnode *vp = ap->a_vp; 6655fe58019SAttilio Rao struct vnode *tdvp = ap->a_tdvp; 6665fe58019SAttilio Rao struct componentname *cnp = ap->a_cnp; 6675fe58019SAttilio Rao 6685fe58019SAttilio Rao struct vattr *vap = VTOVA(vp); 6695fe58019SAttilio Rao 6705fe58019SAttilio Rao struct fuse_dispatcher fdi; 6715fe58019SAttilio Rao struct fuse_entry_out *feo; 6725fe58019SAttilio Rao struct fuse_link_in fli; 6735fe58019SAttilio Rao 6745fe58019SAttilio Rao int err; 6755fe58019SAttilio Rao 6765fe58019SAttilio Rao if (fuse_isdeadfs(vp)) { 6775fe58019SAttilio Rao return ENXIO; 6785fe58019SAttilio Rao } 6795fe58019SAttilio Rao if (vnode_mount(tdvp) != vnode_mount(vp)) { 6805fe58019SAttilio Rao return EXDEV; 6815fe58019SAttilio Rao } 68278a7722fSConrad Meyer 68378a7722fSConrad Meyer /* 68478a7722fSConrad Meyer * This is a seatbelt check to protect naive userspace filesystems from 68578a7722fSConrad Meyer * themselves and the limitations of the FUSE IPC protocol. If a 68678a7722fSConrad Meyer * filesystem does not allow attribute caching, assume it is capable of 68778a7722fSConrad Meyer * validating that nlink does not overflow. 68878a7722fSConrad Meyer */ 68978a7722fSConrad Meyer if (vap != NULL && vap->va_nlink >= FUSE_LINK_MAX) 6905fe58019SAttilio Rao return EMLINK; 6915fe58019SAttilio Rao fli.oldnodeid = VTOI(vp); 6925fe58019SAttilio Rao 6935fe58019SAttilio Rao fdisp_init(&fdi, 0); 6945fe58019SAttilio Rao fuse_internal_newentry_makerequest(vnode_mount(tdvp), VTOI(tdvp), cnp, 6955fe58019SAttilio Rao FUSE_LINK, &fli, sizeof(fli), &fdi); 6965fe58019SAttilio Rao if ((err = fdisp_wait_answ(&fdi))) { 6975fe58019SAttilio Rao goto out; 6985fe58019SAttilio Rao } 6995fe58019SAttilio Rao feo = fdi.answ; 7005fe58019SAttilio Rao 7015fe58019SAttilio Rao err = fuse_internal_checkentry(feo, vnode_vtype(vp)); 7025fe58019SAttilio Rao out: 7035fe58019SAttilio Rao fdisp_destroy(&fdi); 7045fe58019SAttilio Rao return err; 7055fe58019SAttilio Rao } 7065fe58019SAttilio Rao 707dc14d593SAlan Somers struct fuse_lookup_alloc_arg { 708dc14d593SAlan Somers struct fuse_entry_out *feo; 709dc14d593SAlan Somers struct componentname *cnp; 710dc14d593SAlan Somers uint64_t nid; 711dc14d593SAlan Somers enum vtype vtyp; 712dc14d593SAlan Somers }; 713dc14d593SAlan Somers 714dc14d593SAlan Somers /* Callback for vn_get_ino */ 715dc14d593SAlan Somers static int 716dc14d593SAlan Somers fuse_lookup_alloc(struct mount *mp, void *arg, int lkflags, struct vnode **vpp) 717dc14d593SAlan Somers { 718dc14d593SAlan Somers struct fuse_lookup_alloc_arg *flaa = arg; 719dc14d593SAlan Somers 720dc14d593SAlan Somers return fuse_vnode_get(mp, flaa->feo, flaa->nid, NULL, vpp, flaa->cnp, 721dc14d593SAlan Somers flaa->vtyp); 722dc14d593SAlan Somers } 723dc14d593SAlan Somers 724ccb75e49SAlan Somers SDT_PROBE_DEFINE3(fuse, , vnops, cache_lookup, 725ccb75e49SAlan Somers "int", "struct timespec*", "struct timespec*"); 7265fe58019SAttilio Rao /* 7275fe58019SAttilio Rao struct vnop_lookup_args { 7285fe58019SAttilio Rao struct vnodeop_desc *a_desc; 7295fe58019SAttilio Rao struct vnode *a_dvp; 7305fe58019SAttilio Rao struct vnode **a_vpp; 7315fe58019SAttilio Rao struct componentname *a_cnp; 7325fe58019SAttilio Rao }; 7335fe58019SAttilio Rao */ 7345fe58019SAttilio Rao int 7355fe58019SAttilio Rao fuse_vnop_lookup(struct vop_lookup_args *ap) 7365fe58019SAttilio Rao { 7375fe58019SAttilio Rao struct vnode *dvp = ap->a_dvp; 7385fe58019SAttilio Rao struct vnode **vpp = ap->a_vpp; 7395fe58019SAttilio Rao struct componentname *cnp = ap->a_cnp; 7405fe58019SAttilio Rao struct thread *td = cnp->cn_thread; 7415fe58019SAttilio Rao struct ucred *cred = cnp->cn_cred; 7425fe58019SAttilio Rao 7435fe58019SAttilio Rao int nameiop = cnp->cn_nameiop; 7445fe58019SAttilio Rao int flags = cnp->cn_flags; 7455fe58019SAttilio Rao int wantparent = flags & (LOCKPARENT | WANTPARENT); 7465fe58019SAttilio Rao int islastcn = flags & ISLASTCN; 7475fe58019SAttilio Rao struct mount *mp = vnode_mount(dvp); 7485fe58019SAttilio Rao 7495fe58019SAttilio Rao int err = 0; 7505fe58019SAttilio Rao int lookup_err = 0; 7515fe58019SAttilio Rao struct vnode *vp = NULL; 7525fe58019SAttilio Rao 7535fe58019SAttilio Rao struct fuse_dispatcher fdi; 754438b8a6fSAlan Somers bool did_lookup = false; 75544f10c6eSAlan Somers struct fuse_entry_out *feo = NULL; 756438b8a6fSAlan Somers enum vtype vtyp; /* vnode type of target */ 757438b8a6fSAlan Somers off_t filesize; /* filesize of target */ 7585fe58019SAttilio Rao 7595fe58019SAttilio Rao uint64_t nid; 7605fe58019SAttilio Rao 7615fe58019SAttilio Rao if (fuse_isdeadfs(dvp)) { 7625fe58019SAttilio Rao *vpp = NULL; 7635fe58019SAttilio Rao return ENXIO; 7645fe58019SAttilio Rao } 76573825da3SAlan Somers if (!vnode_isdir(dvp)) 7665fe58019SAttilio Rao return ENOTDIR; 7675fe58019SAttilio Rao 76873825da3SAlan Somers if (islastcn && vfs_isrdonly(mp) && (nameiop != LOOKUP)) 76973825da3SAlan Somers return EROFS; 77073825da3SAlan Somers 77173825da3SAlan Somers if ((err = fuse_internal_access(dvp, VEXEC, td, cred))) 7725fe58019SAttilio Rao return err; 77373825da3SAlan Somers 7745fe58019SAttilio Rao if (flags & ISDOTDOT) { 7755fe58019SAttilio Rao nid = VTOFUD(dvp)->parent_nid; 776438b8a6fSAlan Somers if (nid == 0) 7775fe58019SAttilio Rao return ENOENT; 778438b8a6fSAlan Somers /* .. is obviously a directory */ 779438b8a6fSAlan Somers vtyp = VDIR; 780438b8a6fSAlan Somers filesize = 0; 7815fe58019SAttilio Rao } else if (cnp->cn_namelen == 1 && *(cnp->cn_nameptr) == '.') { 7825fe58019SAttilio Rao nid = VTOI(dvp); 783438b8a6fSAlan Somers /* . is obviously a directory */ 784438b8a6fSAlan Somers vtyp = VDIR; 785438b8a6fSAlan Somers filesize = 0; 786ccb75e49SAlan Somers } else { 7873f2c630cSAlan Somers struct timespec now, timeout; 7883f2c630cSAlan Somers 7893f2c630cSAlan Somers err = cache_lookup(dvp, vpp, cnp, &timeout, NULL); 7903f2c630cSAlan Somers getnanouptime(&now); 791ccb75e49SAlan Somers SDT_PROBE3(fuse, , vnops, cache_lookup, err, &timeout, &now); 792ccb75e49SAlan Somers switch (err) { 793ccb75e49SAlan Somers case -1: /* positive match */ 794ccb75e49SAlan Somers if (timespeccmp(&timeout, &now, >)) { 7953f2c630cSAlan Somers atomic_add_acq_long(&fuse_lookup_cache_hits, 1); 7963f2c630cSAlan Somers } else { 7973f2c630cSAlan Somers /* Cache timeout */ 7983f2c630cSAlan Somers atomic_add_acq_long(&fuse_lookup_cache_misses, 7993f2c630cSAlan Somers 1); 800*6124fd71SAlan Somers /* 801*6124fd71SAlan Somers * XXX is fuse_internal_vnode_disappear ok to 802*6124fd71SAlan Somers * call if another process is still using the 803*6124fd71SAlan Somers * vnode? 804*6124fd71SAlan Somers */ 805ccb75e49SAlan Somers fuse_internal_vnode_disappear(*vpp); 806ccb75e49SAlan Somers if (dvp != *vpp) 807ccb75e49SAlan Somers vput(*vpp); 808ccb75e49SAlan Somers else 809ccb75e49SAlan Somers vrele(*vpp); 810ccb75e49SAlan Somers *vpp = NULL; 8113f2c630cSAlan Somers break; 8123f2c630cSAlan Somers } 8133f2c630cSAlan Somers return 0; 8143f2c630cSAlan Somers 8153f2c630cSAlan Somers case 0: /* no match in cache */ 8163f2c630cSAlan Somers atomic_add_acq_long(&fuse_lookup_cache_misses, 1); 8173f2c630cSAlan Somers break; 8183f2c630cSAlan Somers 8193f2c630cSAlan Somers case ENOENT: /* negative match */ 8203f2c630cSAlan Somers getnanouptime(&now); 821ccb75e49SAlan Somers if (timespeccmp(&timeout, &now, <=)) { 8223f2c630cSAlan Somers /* Cache timeout */ 823ccb75e49SAlan Somers cache_purge_negative(dvp); 8243f2c630cSAlan Somers break; 8253f2c630cSAlan Somers } 8263f2c630cSAlan Somers /* fall through */ 8273f2c630cSAlan Somers default: 8283f2c630cSAlan Somers return err; 8293f2c630cSAlan Somers } 830438b8a6fSAlan Somers 8315fe58019SAttilio Rao nid = VTOI(dvp); 8325fe58019SAttilio Rao fdisp_init(&fdi, cnp->cn_namelen + 1); 833438b8a6fSAlan Somers fdisp_make(&fdi, FUSE_LOOKUP, mp, nid, td, cred); 8345fe58019SAttilio Rao 8355fe58019SAttilio Rao memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen); 8365fe58019SAttilio Rao ((char *)fdi.indata)[cnp->cn_namelen] = '\0'; 8375fe58019SAttilio Rao lookup_err = fdisp_wait_answ(&fdi); 838438b8a6fSAlan Somers did_lookup = true; 8395fe58019SAttilio Rao 840438b8a6fSAlan Somers if (!lookup_err) { 841ccb75e49SAlan Somers /* lookup call succeeded */ 8425fe58019SAttilio Rao nid = ((struct fuse_entry_out *)fdi.answ)->nodeid; 843438b8a6fSAlan Somers feo = (struct fuse_entry_out *)fdi.answ; 84444f10c6eSAlan Somers if (nid == 0) { 84544f10c6eSAlan Somers /* zero nodeid means ENOENT and cache it */ 84644f10c6eSAlan Somers struct timespec timeout; 84744f10c6eSAlan Somers 8486248288eSAlan Somers fdi.answ_stat = ENOENT; 8495fe58019SAttilio Rao lookup_err = ENOENT; 85044f10c6eSAlan Somers if (cnp->cn_flags & MAKEENTRY) { 85144f10c6eSAlan Somers fuse_validity_2_timespec(feo, &timeout); 852438b8a6fSAlan Somers cache_enter_time(dvp, *vpp, cnp, 853438b8a6fSAlan Somers &timeout, NULL); 85444f10c6eSAlan Somers } 8555fe58019SAttilio Rao } else if (nid == FUSE_ROOT_ID) { 8565fe58019SAttilio Rao lookup_err = EINVAL; 8575fe58019SAttilio Rao } 858438b8a6fSAlan Somers vtyp = IFTOVT(feo->attr.mode); 859438b8a6fSAlan Somers filesize = feo->attr.size; 8605fe58019SAttilio Rao } 861438b8a6fSAlan Somers if (lookup_err && (!fdi.answ_stat || lookup_err != ENOENT)) { 8625fe58019SAttilio Rao fdisp_destroy(&fdi); 8635fe58019SAttilio Rao return lookup_err; 8645fe58019SAttilio Rao } 865438b8a6fSAlan Somers } 8665fe58019SAttilio Rao /* lookup_err, if non-zero, must be ENOENT at this point */ 8675fe58019SAttilio Rao 8685fe58019SAttilio Rao if (lookup_err) { 869ff4fbdf5SAlan Somers /* Entry not found */ 870ff4fbdf5SAlan Somers if ((nameiop == CREATE || nameiop == RENAME) && islastcn) { 871666f8543SAlan Somers err = fuse_internal_access(dvp, VWRITE, td, cred); 872*6124fd71SAlan Somers if (!err) { 8735fe58019SAttilio Rao /* 874dc14d593SAlan Somers * Set the SAVENAME flag to hold onto the 875*6124fd71SAlan Somers * pathname for use later in VOP_CREATE or 876*6124fd71SAlan Somers * VOP_RENAME. 8775fe58019SAttilio Rao */ 8785fe58019SAttilio Rao cnp->cn_flags |= SAVENAME; 8795fe58019SAttilio Rao 8805fe58019SAttilio Rao err = EJUSTRETURN; 8815fe58019SAttilio Rao } 882*6124fd71SAlan Somers } else { 8835fe58019SAttilio Rao err = ENOENT; 884*6124fd71SAlan Somers } 8855fe58019SAttilio Rao } else { 886ff4fbdf5SAlan Somers /* Entry was found */ 8875fe58019SAttilio Rao if (flags & ISDOTDOT) { 888dc14d593SAlan Somers struct fuse_lookup_alloc_arg flaa; 8895fe58019SAttilio Rao 890dc14d593SAlan Somers flaa.nid = nid; 891dc14d593SAlan Somers flaa.feo = feo; 892dc14d593SAlan Somers flaa.cnp = cnp; 893dc14d593SAlan Somers flaa.vtyp = vtyp; 894dc14d593SAlan Somers err = vn_vget_ino_gen(dvp, fuse_lookup_alloc, &flaa, 0, 895dc14d593SAlan Somers &vp); 8965fe58019SAttilio Rao *vpp = vp; 8975fe58019SAttilio Rao } else if (nid == VTOI(dvp)) { 8985fe58019SAttilio Rao vref(dvp); 8995fe58019SAttilio Rao *vpp = dvp; 9005fe58019SAttilio Rao } else { 9013c324b94SConrad Meyer struct fuse_vnode_data *fvdat; 9023c324b94SConrad Meyer 90309176f09SConrad Meyer err = fuse_vnode_get(vnode_mount(dvp), feo, nid, dvp, 904438b8a6fSAlan Somers &vp, cnp, vtyp); 905*6124fd71SAlan Somers if (err) 9065fe58019SAttilio Rao goto out; 907*6124fd71SAlan Somers *vpp = vp; 908*6124fd71SAlan Somers 9095fe58019SAttilio Rao fuse_vnode_setparent(vp, dvp); 9103c324b94SConrad Meyer 9113c324b94SConrad Meyer /* 9123c324b94SConrad Meyer * In the case where we are looking up a FUSE node 9133c324b94SConrad Meyer * represented by an existing cached vnode, and the 9143c324b94SConrad Meyer * true size reported by FUSE_LOOKUP doesn't match 9153c324b94SConrad Meyer * the vnode's cached size, fix the vnode cache to 9163c324b94SConrad Meyer * match the real object size. 9173c324b94SConrad Meyer * 918*6124fd71SAlan Somers * We can get here: 919*6124fd71SAlan Somers * * following attribute cache expiration, or 920*6124fd71SAlan Somers * * due a bug in the daemon, or 921*6124fd71SAlan Somers * * the first time that we looked up the file. 9223c324b94SConrad Meyer */ 9233c324b94SConrad Meyer fvdat = VTOFUD(vp); 9243c324b94SConrad Meyer if (vnode_isreg(vp) && 925438b8a6fSAlan Somers filesize != fvdat->filesize) { 9263c324b94SConrad Meyer /* 9273c324b94SConrad Meyer * The FN_SIZECHANGE flag reflects a dirty 9283c324b94SConrad Meyer * append. If userspace lets us know our cache 9293c324b94SConrad Meyer * is invalid, that write was lost. (Dirty 9303c324b94SConrad Meyer * writes that do not cause append are also 9313c324b94SConrad Meyer * lost, but we don't detect them here.) 9323c324b94SConrad Meyer * 9333c324b94SConrad Meyer * XXX: Maybe disable WB caching on this mount. 9343c324b94SConrad Meyer */ 9353c324b94SConrad Meyer if (fvdat->flag & FN_SIZECHANGE) 9363c324b94SConrad Meyer printf("%s: WB cache incoherent on " 9373c324b94SConrad Meyer "%s!\n", __func__, 9383c324b94SConrad Meyer vnode_mount(vp)->mnt_stat.f_mntonname); 9393c324b94SConrad Meyer 940438b8a6fSAlan Somers (void)fuse_vnode_setsize(vp, cred, filesize); 9413c324b94SConrad Meyer fvdat->flag &= ~FN_SIZECHANGE; 9423c324b94SConrad Meyer } 9435fe58019SAttilio Rao 944*6124fd71SAlan Somers MPASS(feo != NULL); 945438b8a6fSAlan Somers fuse_internal_cache_attrs(*vpp, &feo->attr, 946438b8a6fSAlan Somers feo->attr_valid, feo->attr_valid_nsec, NULL); 947*6124fd71SAlan Somers 948*6124fd71SAlan Somers if ((nameiop == DELETE || nameiop == RENAME) && 949*6124fd71SAlan Somers islastcn) 950*6124fd71SAlan Somers { 951*6124fd71SAlan Somers struct vattr dvattr; 952*6124fd71SAlan Somers 953*6124fd71SAlan Somers err = fuse_internal_access(dvp, VWRITE, td, 954*6124fd71SAlan Somers cred); 955*6124fd71SAlan Somers if (err != 0) 956*6124fd71SAlan Somers goto out; 957*6124fd71SAlan Somers /* 958*6124fd71SAlan Somers * if the parent's sticky bit is set, check 959*6124fd71SAlan Somers * whether we're allowed to remove the file. 960*6124fd71SAlan Somers * Need to figure out the vnode locking to make 961*6124fd71SAlan Somers * this work. 962*6124fd71SAlan Somers */ 963*6124fd71SAlan Somers fuse_internal_getattr(dvp, &dvattr, cred, td); 964*6124fd71SAlan Somers if ((dvattr.va_mode & S_ISTXT) && 965*6124fd71SAlan Somers fuse_internal_access(dvp, VADMIN, td, 966*6124fd71SAlan Somers cred) && 967*6124fd71SAlan Somers fuse_internal_access(*vpp, VADMIN, td, 968*6124fd71SAlan Somers cred)) { 969*6124fd71SAlan Somers err = EPERM; 970*6124fd71SAlan Somers goto out; 971*6124fd71SAlan Somers } 972*6124fd71SAlan Somers } 973*6124fd71SAlan Somers 974*6124fd71SAlan Somers if (islastcn && ( 975*6124fd71SAlan Somers (nameiop == DELETE) || 976*6124fd71SAlan Somers (nameiop == RENAME && wantparent))) { 977*6124fd71SAlan Somers cnp->cn_flags |= SAVENAME; 978*6124fd71SAlan Somers } 979*6124fd71SAlan Somers 9805fe58019SAttilio Rao } 9815fe58019SAttilio Rao } 9825fe58019SAttilio Rao out: 983*6124fd71SAlan Somers if (err) { 984*6124fd71SAlan Somers if (vp != NULL && dvp != vp) 985*6124fd71SAlan Somers vput(vp); 986*6124fd71SAlan Somers else if (vp != NULL) 987*6124fd71SAlan Somers vrele(vp); 988*6124fd71SAlan Somers *vpp = NULL; 9895fe58019SAttilio Rao } 990438b8a6fSAlan Somers if (did_lookup) 9915fe58019SAttilio Rao fdisp_destroy(&fdi); 9925fe58019SAttilio Rao 9935fe58019SAttilio Rao return err; 9945fe58019SAttilio Rao } 9955fe58019SAttilio Rao 9965fe58019SAttilio Rao /* 9975fe58019SAttilio Rao struct vnop_mkdir_args { 9985fe58019SAttilio Rao struct vnode *a_dvp; 9995fe58019SAttilio Rao struct vnode **a_vpp; 10005fe58019SAttilio Rao struct componentname *a_cnp; 10015fe58019SAttilio Rao struct vattr *a_vap; 10025fe58019SAttilio Rao }; 10035fe58019SAttilio Rao */ 10045fe58019SAttilio Rao static int 10055fe58019SAttilio Rao fuse_vnop_mkdir(struct vop_mkdir_args *ap) 10065fe58019SAttilio Rao { 10075fe58019SAttilio Rao struct vnode *dvp = ap->a_dvp; 10085fe58019SAttilio Rao struct vnode **vpp = ap->a_vpp; 10095fe58019SAttilio Rao struct componentname *cnp = ap->a_cnp; 10105fe58019SAttilio Rao struct vattr *vap = ap->a_vap; 10115fe58019SAttilio Rao 10125fe58019SAttilio Rao struct fuse_mkdir_in fmdi; 10135fe58019SAttilio Rao 10145fe58019SAttilio Rao if (fuse_isdeadfs(dvp)) { 10155fe58019SAttilio Rao return ENXIO; 10165fe58019SAttilio Rao } 10175fe58019SAttilio Rao fmdi.mode = MAKEIMODE(vap->va_type, vap->va_mode); 10185fe58019SAttilio Rao 1019d9454fabSAttilio Rao return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKDIR, &fmdi, 1020d9454fabSAttilio Rao sizeof(fmdi), VDIR)); 10215fe58019SAttilio Rao } 10225fe58019SAttilio Rao 10235fe58019SAttilio Rao /* 10245fe58019SAttilio Rao struct vnop_mknod_args { 10255fe58019SAttilio Rao struct vnode *a_dvp; 10265fe58019SAttilio Rao struct vnode **a_vpp; 10275fe58019SAttilio Rao struct componentname *a_cnp; 10285fe58019SAttilio Rao struct vattr *a_vap; 10295fe58019SAttilio Rao }; 10305fe58019SAttilio Rao */ 10315fe58019SAttilio Rao static int 10325fe58019SAttilio Rao fuse_vnop_mknod(struct vop_mknod_args *ap) 10335fe58019SAttilio Rao { 10345fe58019SAttilio Rao 1035bf4d7084SAlan Somers struct vnode *dvp = ap->a_dvp; 1036bf4d7084SAlan Somers struct vnode **vpp = ap->a_vpp; 1037bf4d7084SAlan Somers struct componentname *cnp = ap->a_cnp; 1038bf4d7084SAlan Somers struct vattr *vap = ap->a_vap; 1039bf4d7084SAlan Somers struct fuse_mknod_in fmni; 10405fe58019SAttilio Rao 1041bf4d7084SAlan Somers if (fuse_isdeadfs(dvp)) 1042bf4d7084SAlan Somers return ENXIO; 1043bf4d7084SAlan Somers 1044bf4d7084SAlan Somers fmni.mode = MAKEIMODE(vap->va_type, vap->va_mode); 1045bf4d7084SAlan Somers fmni.rdev = vap->va_rdev; 1046bf4d7084SAlan Somers return (fuse_internal_newentry(dvp, vpp, cnp, FUSE_MKNOD, &fmni, 1047bf4d7084SAlan Somers sizeof(fmni), vap->va_type)); 1048bf4d7084SAlan Somers } 10495fe58019SAttilio Rao 10505fe58019SAttilio Rao /* 10515fe58019SAttilio Rao struct vnop_open_args { 10525fe58019SAttilio Rao struct vnode *a_vp; 10535fe58019SAttilio Rao int a_mode; 10545fe58019SAttilio Rao struct ucred *a_cred; 10555fe58019SAttilio Rao struct thread *a_td; 10565fe58019SAttilio Rao int a_fdidx; / struct file *a_fp; 10575fe58019SAttilio Rao }; 10585fe58019SAttilio Rao */ 10595fe58019SAttilio Rao static int 10605fe58019SAttilio Rao fuse_vnop_open(struct vop_open_args *ap) 10615fe58019SAttilio Rao { 10625fe58019SAttilio Rao struct vnode *vp = ap->a_vp; 10639e444871SAlan Somers int a_mode = ap->a_mode; 10645fe58019SAttilio Rao struct thread *td = ap->a_td; 10655fe58019SAttilio Rao struct ucred *cred = ap->a_cred; 1066f8d4af10SAlan Somers pid_t pid = td->td_proc->p_pid; 10675fe58019SAttilio Rao struct fuse_vnode_data *fvdat; 10685fe58019SAttilio Rao 1069bf4d7084SAlan Somers if (fuse_isdeadfs(vp)) 10705fe58019SAttilio Rao return ENXIO; 1071bf4d7084SAlan Somers if (vp->v_type == VCHR || vp->v_type == VBLK || vp->v_type == VFIFO) 1072bf4d7084SAlan Somers return (EOPNOTSUPP); 10739e444871SAlan Somers if ((a_mode & (FREAD | FWRITE | FEXEC)) == 0) 1074e7df9886SConrad Meyer return EINVAL; 1075e7df9886SConrad Meyer 10765fe58019SAttilio Rao fvdat = VTOFUD(vp); 10775fe58019SAttilio Rao 10789e444871SAlan Somers if (fuse_filehandle_validrw(vp, a_mode, cred, pid)) { 10795fccbf31SAlan Somers fuse_vnode_open(vp, 0, td); 10805fe58019SAttilio Rao return 0; 10815fe58019SAttilio Rao } 10825fe58019SAttilio Rao 10839e444871SAlan Somers return fuse_filehandle_open(vp, a_mode, NULL, td, cred); 10845fe58019SAttilio Rao } 10855fe58019SAttilio Rao 1086746c92e0SJohn Baldwin static int 1087746c92e0SJohn Baldwin fuse_vnop_pathconf(struct vop_pathconf_args *ap) 1088746c92e0SJohn Baldwin { 1089746c92e0SJohn Baldwin 1090746c92e0SJohn Baldwin switch (ap->a_name) { 1091746c92e0SJohn Baldwin case _PC_FILESIZEBITS: 1092746c92e0SJohn Baldwin *ap->a_retval = 64; 1093746c92e0SJohn Baldwin return (0); 1094599afe53SJohn Baldwin case _PC_NAME_MAX: 1095599afe53SJohn Baldwin *ap->a_retval = NAME_MAX; 1096599afe53SJohn Baldwin return (0); 1097599afe53SJohn Baldwin case _PC_LINK_MAX: 1098f83f3d79SJohn Baldwin *ap->a_retval = MIN(LONG_MAX, FUSE_LINK_MAX); 1099599afe53SJohn Baldwin return (0); 1100746c92e0SJohn Baldwin case _PC_SYMLINK_MAX: 1101746c92e0SJohn Baldwin *ap->a_retval = MAXPATHLEN; 1102746c92e0SJohn Baldwin return (0); 1103746c92e0SJohn Baldwin case _PC_NO_TRUNC: 1104746c92e0SJohn Baldwin *ap->a_retval = 1; 1105746c92e0SJohn Baldwin return (0); 1106746c92e0SJohn Baldwin default: 1107746c92e0SJohn Baldwin return (vop_stdpathconf(ap)); 1108746c92e0SJohn Baldwin } 1109746c92e0SJohn Baldwin } 1110746c92e0SJohn Baldwin 11115fe58019SAttilio Rao /* 11125fe58019SAttilio Rao struct vnop_read_args { 11135fe58019SAttilio Rao struct vnode *a_vp; 11145fe58019SAttilio Rao struct uio *a_uio; 11155fe58019SAttilio Rao int a_ioflag; 11165fe58019SAttilio Rao struct ucred *a_cred; 11175fe58019SAttilio Rao }; 11185fe58019SAttilio Rao */ 11195fe58019SAttilio Rao static int 11205fe58019SAttilio Rao fuse_vnop_read(struct vop_read_args *ap) 11215fe58019SAttilio Rao { 11225fe58019SAttilio Rao struct vnode *vp = ap->a_vp; 11235fe58019SAttilio Rao struct uio *uio = ap->a_uio; 11245fe58019SAttilio Rao int ioflag = ap->a_ioflag; 11255fe58019SAttilio Rao struct ucred *cred = ap->a_cred; 1126f8d4af10SAlan Somers pid_t pid = curthread->td_proc->p_pid; 11275fe58019SAttilio Rao 11285fe58019SAttilio Rao if (fuse_isdeadfs(vp)) { 11295fe58019SAttilio Rao return ENXIO; 11305fe58019SAttilio Rao } 1131ead063e0SEdward Tomasz Napierala 1132ead063e0SEdward Tomasz Napierala if (VTOFUD(vp)->flag & FN_DIRECTIO) { 1133ead063e0SEdward Tomasz Napierala ioflag |= IO_DIRECT; 1134ead063e0SEdward Tomasz Napierala } 1135ead063e0SEdward Tomasz Napierala 1136f8d4af10SAlan Somers return fuse_io_dispatch(vp, uio, ioflag, cred, pid); 11375fe58019SAttilio Rao } 11385fe58019SAttilio Rao 11395fe58019SAttilio Rao /* 11405fe58019SAttilio Rao struct vnop_readdir_args { 11415fe58019SAttilio Rao struct vnode *a_vp; 11425fe58019SAttilio Rao struct uio *a_uio; 11435fe58019SAttilio Rao struct ucred *a_cred; 11445fe58019SAttilio Rao int *a_eofflag; 11455fe58019SAttilio Rao int *ncookies; 11465fe58019SAttilio Rao u_long **a_cookies; 11475fe58019SAttilio Rao }; 11485fe58019SAttilio Rao */ 11495fe58019SAttilio Rao static int 11505fe58019SAttilio Rao fuse_vnop_readdir(struct vop_readdir_args *ap) 11515fe58019SAttilio Rao { 11525fe58019SAttilio Rao struct vnode *vp = ap->a_vp; 11535fe58019SAttilio Rao struct uio *uio = ap->a_uio; 11545fe58019SAttilio Rao struct ucred *cred = ap->a_cred; 11555fe58019SAttilio Rao struct fuse_filehandle *fufh = NULL; 11565fe58019SAttilio Rao struct fuse_iov cookediov; 11575fe58019SAttilio Rao int err = 0; 1158f8d4af10SAlan Somers pid_t pid = curthread->td_proc->p_pid; 11595fe58019SAttilio Rao 11605fe58019SAttilio Rao if (fuse_isdeadfs(vp)) { 11615fe58019SAttilio Rao return ENXIO; 11625fe58019SAttilio Rao } 11635fe58019SAttilio Rao if ( /* XXXIP ((uio_iovcnt(uio) > 1)) || */ 11645fe58019SAttilio Rao (uio_resid(uio) < sizeof(struct dirent))) { 11655fe58019SAttilio Rao return EINVAL; 11665fe58019SAttilio Rao } 11675fe58019SAttilio Rao 116835cf0e7eSAlan Somers err = fuse_filehandle_get_dir(vp, &fufh, cred, pid); 116935cf0e7eSAlan Somers if (err) 11705fe58019SAttilio Rao return (err); 11715fe58019SAttilio Rao #define DIRCOOKEDSIZE FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + MAXNAMLEN + 1) 11725fe58019SAttilio Rao fiov_init(&cookediov, DIRCOOKEDSIZE); 11735fe58019SAttilio Rao 11745fe58019SAttilio Rao err = fuse_internal_readdir(vp, uio, fufh, &cookediov); 11755fe58019SAttilio Rao 11765fe58019SAttilio Rao fiov_teardown(&cookediov); 11775ec10aa5SAlan Somers 11785fe58019SAttilio Rao return err; 11795fe58019SAttilio Rao } 11805fe58019SAttilio Rao 11815fe58019SAttilio Rao /* 11825fe58019SAttilio Rao struct vnop_readlink_args { 11835fe58019SAttilio Rao struct vnode *a_vp; 11845fe58019SAttilio Rao struct uio *a_uio; 11855fe58019SAttilio Rao struct ucred *a_cred; 11865fe58019SAttilio Rao }; 11875fe58019SAttilio Rao */ 11885fe58019SAttilio Rao static int 11895fe58019SAttilio Rao fuse_vnop_readlink(struct vop_readlink_args *ap) 11905fe58019SAttilio Rao { 11915fe58019SAttilio Rao struct vnode *vp = ap->a_vp; 11925fe58019SAttilio Rao struct uio *uio = ap->a_uio; 11935fe58019SAttilio Rao struct ucred *cred = ap->a_cred; 11945fe58019SAttilio Rao 11955fe58019SAttilio Rao struct fuse_dispatcher fdi; 11965fe58019SAttilio Rao int err; 11975fe58019SAttilio Rao 11985fe58019SAttilio Rao if (fuse_isdeadfs(vp)) { 11995fe58019SAttilio Rao return ENXIO; 12005fe58019SAttilio Rao } 12015fe58019SAttilio Rao if (!vnode_islnk(vp)) { 12025fe58019SAttilio Rao return EINVAL; 12035fe58019SAttilio Rao } 12045fe58019SAttilio Rao fdisp_init(&fdi, 0); 12055fe58019SAttilio Rao err = fdisp_simple_putget_vp(&fdi, FUSE_READLINK, vp, curthread, cred); 12065fe58019SAttilio Rao if (err) { 12075fe58019SAttilio Rao goto out; 12085fe58019SAttilio Rao } 12095fe58019SAttilio Rao if (((char *)fdi.answ)[0] == '/' && 12105fe58019SAttilio Rao fuse_get_mpdata(vnode_mount(vp))->dataflags & FSESS_PUSH_SYMLINKS_IN) { 12115fe58019SAttilio Rao char *mpth = vnode_mount(vp)->mnt_stat.f_mntonname; 12125fe58019SAttilio Rao 12135fe58019SAttilio Rao err = uiomove(mpth, strlen(mpth), uio); 12145fe58019SAttilio Rao } 12155fe58019SAttilio Rao if (!err) { 12165fe58019SAttilio Rao err = uiomove(fdi.answ, fdi.iosize, uio); 12175fe58019SAttilio Rao } 12185fe58019SAttilio Rao out: 12195fe58019SAttilio Rao fdisp_destroy(&fdi); 12205fe58019SAttilio Rao return err; 12215fe58019SAttilio Rao } 12225fe58019SAttilio Rao 12235fe58019SAttilio Rao /* 12245fe58019SAttilio Rao struct vnop_reclaim_args { 12255fe58019SAttilio Rao struct vnode *a_vp; 12265fe58019SAttilio Rao struct thread *a_td; 12275fe58019SAttilio Rao }; 12285fe58019SAttilio Rao */ 12295fe58019SAttilio Rao static int 12305fe58019SAttilio Rao fuse_vnop_reclaim(struct vop_reclaim_args *ap) 12315fe58019SAttilio Rao { 12325fe58019SAttilio Rao struct vnode *vp = ap->a_vp; 12335fe58019SAttilio Rao struct thread *td = ap->a_td; 12345fe58019SAttilio Rao struct fuse_vnode_data *fvdat = VTOFUD(vp); 12355ec10aa5SAlan Somers struct fuse_filehandle *fufh, *fufh_tmp; 12365fe58019SAttilio Rao 12375fe58019SAttilio Rao if (!fvdat) { 12385fe58019SAttilio Rao panic("FUSE: no vnode data during recycling"); 12395fe58019SAttilio Rao } 12405ec10aa5SAlan Somers LIST_FOREACH_SAFE(fufh, &fvdat->handles, next, fufh_tmp) { 12415ec10aa5SAlan Somers printf("FUSE: vnode being reclaimed with open fufh " 12429e444871SAlan Somers "(type=%#x)", fufh->fufh_type); 12435ec10aa5SAlan Somers fuse_filehandle_close(vp, fufh, td, NULL); 12445fe58019SAttilio Rao } 12455fe58019SAttilio Rao 12465fe58019SAttilio Rao if ((!fuse_isdeadfs(vp)) && (fvdat->nlookup)) { 12475fe58019SAttilio Rao fuse_internal_forget_send(vnode_mount(vp), td, NULL, VTOI(vp), 12485fe58019SAttilio Rao fvdat->nlookup); 12495fe58019SAttilio Rao } 12505fe58019SAttilio Rao fuse_vnode_setparent(vp, NULL); 12515fe58019SAttilio Rao cache_purge(vp); 12525fe58019SAttilio Rao vfs_hash_remove(vp); 12535fe58019SAttilio Rao vnode_destroy_vobject(vp); 12545fe58019SAttilio Rao fuse_vnode_destroy(vp); 12555fe58019SAttilio Rao 12565fe58019SAttilio Rao return 0; 12575fe58019SAttilio Rao } 12585fe58019SAttilio Rao 12595fe58019SAttilio Rao /* 12605fe58019SAttilio Rao struct vnop_remove_args { 12615fe58019SAttilio Rao struct vnode *a_dvp; 12625fe58019SAttilio Rao struct vnode *a_vp; 12635fe58019SAttilio Rao struct componentname *a_cnp; 12645fe58019SAttilio Rao }; 12655fe58019SAttilio Rao */ 12665fe58019SAttilio Rao static int 12675fe58019SAttilio Rao fuse_vnop_remove(struct vop_remove_args *ap) 12685fe58019SAttilio Rao { 12695fe58019SAttilio Rao struct vnode *dvp = ap->a_dvp; 12705fe58019SAttilio Rao struct vnode *vp = ap->a_vp; 12715fe58019SAttilio Rao struct componentname *cnp = ap->a_cnp; 12725fe58019SAttilio Rao 12735fe58019SAttilio Rao int err; 12745fe58019SAttilio Rao 12755fe58019SAttilio Rao if (fuse_isdeadfs(vp)) { 12765fe58019SAttilio Rao return ENXIO; 12775fe58019SAttilio Rao } 12785fe58019SAttilio Rao if (vnode_isdir(vp)) { 12795fe58019SAttilio Rao return EPERM; 12805fe58019SAttilio Rao } 12815fe58019SAttilio Rao cache_purge(vp); 12825fe58019SAttilio Rao 12835fe58019SAttilio Rao err = fuse_internal_remove(dvp, vp, cnp, FUSE_UNLINK); 12845fe58019SAttilio Rao 1285d9454fabSAttilio Rao if (err == 0) 12865fe58019SAttilio Rao fuse_internal_vnode_disappear(vp); 12875fe58019SAttilio Rao return err; 12885fe58019SAttilio Rao } 12895fe58019SAttilio Rao 12905fe58019SAttilio Rao /* 12915fe58019SAttilio Rao struct vnop_rename_args { 12925fe58019SAttilio Rao struct vnode *a_fdvp; 12935fe58019SAttilio Rao struct vnode *a_fvp; 12945fe58019SAttilio Rao struct componentname *a_fcnp; 12955fe58019SAttilio Rao struct vnode *a_tdvp; 12965fe58019SAttilio Rao struct vnode *a_tvp; 12975fe58019SAttilio Rao struct componentname *a_tcnp; 12985fe58019SAttilio Rao }; 12995fe58019SAttilio Rao */ 13005fe58019SAttilio Rao static int 13015fe58019SAttilio Rao fuse_vnop_rename(struct vop_rename_args *ap) 13025fe58019SAttilio Rao { 13035fe58019SAttilio Rao struct vnode *fdvp = ap->a_fdvp; 13045fe58019SAttilio Rao struct vnode *fvp = ap->a_fvp; 13055fe58019SAttilio Rao struct componentname *fcnp = ap->a_fcnp; 13065fe58019SAttilio Rao struct vnode *tdvp = ap->a_tdvp; 13075fe58019SAttilio Rao struct vnode *tvp = ap->a_tvp; 13085fe58019SAttilio Rao struct componentname *tcnp = ap->a_tcnp; 13095fe58019SAttilio Rao struct fuse_data *data; 13105fe58019SAttilio Rao 13115fe58019SAttilio Rao int err = 0; 13125fe58019SAttilio Rao 13135fe58019SAttilio Rao if (fuse_isdeadfs(fdvp)) { 13145fe58019SAttilio Rao return ENXIO; 13155fe58019SAttilio Rao } 13165fe58019SAttilio Rao if (fvp->v_mount != tdvp->v_mount || 13175fe58019SAttilio Rao (tvp && fvp->v_mount != tvp->v_mount)) { 1318cf169498SAlan Somers SDT_PROBE2(fuse, , vnops, trace, 1, "cross-device rename"); 13195fe58019SAttilio Rao err = EXDEV; 13205fe58019SAttilio Rao goto out; 13215fe58019SAttilio Rao } 13225fe58019SAttilio Rao cache_purge(fvp); 13235fe58019SAttilio Rao 13245fe58019SAttilio Rao /* 13255fe58019SAttilio Rao * FUSE library is expected to check if target directory is not 13265fe58019SAttilio Rao * under the source directory in the file system tree. 13275fe58019SAttilio Rao * Linux performs this check at VFS level. 13285fe58019SAttilio Rao */ 13295fe58019SAttilio Rao data = fuse_get_mpdata(vnode_mount(tdvp)); 13305fe58019SAttilio Rao sx_xlock(&data->rename_lock); 13315fe58019SAttilio Rao err = fuse_internal_rename(fdvp, fcnp, tdvp, tcnp); 13325fe58019SAttilio Rao if (err == 0) { 1333d9454fabSAttilio Rao if (tdvp != fdvp) 13345fe58019SAttilio Rao fuse_vnode_setparent(fvp, tdvp); 13355fe58019SAttilio Rao if (tvp != NULL) 13365fe58019SAttilio Rao fuse_vnode_setparent(tvp, NULL); 13375fe58019SAttilio Rao } 13385fe58019SAttilio Rao sx_unlock(&data->rename_lock); 13395fe58019SAttilio Rao 13405fe58019SAttilio Rao if (tvp != NULL && tvp != fvp) { 13415fe58019SAttilio Rao cache_purge(tvp); 13425fe58019SAttilio Rao } 13435fe58019SAttilio Rao if (vnode_isdir(fvp)) { 13445fe58019SAttilio Rao if ((tvp != NULL) && vnode_isdir(tvp)) { 13455fe58019SAttilio Rao cache_purge(tdvp); 13465fe58019SAttilio Rao } 13475fe58019SAttilio Rao cache_purge(fdvp); 13485fe58019SAttilio Rao } 13495fe58019SAttilio Rao out: 13505fe58019SAttilio Rao if (tdvp == tvp) { 13515fe58019SAttilio Rao vrele(tdvp); 13525fe58019SAttilio Rao } else { 13535fe58019SAttilio Rao vput(tdvp); 13545fe58019SAttilio Rao } 13555fe58019SAttilio Rao if (tvp != NULL) { 13565fe58019SAttilio Rao vput(tvp); 13575fe58019SAttilio Rao } 13585fe58019SAttilio Rao vrele(fdvp); 13595fe58019SAttilio Rao vrele(fvp); 13605fe58019SAttilio Rao 13615fe58019SAttilio Rao return err; 13625fe58019SAttilio Rao } 13635fe58019SAttilio Rao 13645fe58019SAttilio Rao /* 13655fe58019SAttilio Rao struct vnop_rmdir_args { 13665fe58019SAttilio Rao struct vnode *a_dvp; 13675fe58019SAttilio Rao struct vnode *a_vp; 13685fe58019SAttilio Rao struct componentname *a_cnp; 13695fe58019SAttilio Rao } *ap; 13705fe58019SAttilio Rao */ 13715fe58019SAttilio Rao static int 13725fe58019SAttilio Rao fuse_vnop_rmdir(struct vop_rmdir_args *ap) 13735fe58019SAttilio Rao { 13745fe58019SAttilio Rao struct vnode *dvp = ap->a_dvp; 13755fe58019SAttilio Rao struct vnode *vp = ap->a_vp; 13765fe58019SAttilio Rao 13775fe58019SAttilio Rao int err; 13785fe58019SAttilio Rao 13795fe58019SAttilio Rao if (fuse_isdeadfs(vp)) { 13805fe58019SAttilio Rao return ENXIO; 13815fe58019SAttilio Rao } 13825fe58019SAttilio Rao if (VTOFUD(vp) == VTOFUD(dvp)) { 13835fe58019SAttilio Rao return EINVAL; 13845fe58019SAttilio Rao } 13855fe58019SAttilio Rao err = fuse_internal_remove(dvp, vp, ap->a_cnp, FUSE_RMDIR); 13865fe58019SAttilio Rao 1387d9454fabSAttilio Rao if (err == 0) 13885fe58019SAttilio Rao fuse_internal_vnode_disappear(vp); 13895fe58019SAttilio Rao return err; 13905fe58019SAttilio Rao } 13915fe58019SAttilio Rao 13925fe58019SAttilio Rao /* 13935fe58019SAttilio Rao struct vnop_setattr_args { 13945fe58019SAttilio Rao struct vnode *a_vp; 13955fe58019SAttilio Rao struct vattr *a_vap; 13965fe58019SAttilio Rao struct ucred *a_cred; 13975fe58019SAttilio Rao struct thread *a_td; 13985fe58019SAttilio Rao }; 13995fe58019SAttilio Rao */ 14005fe58019SAttilio Rao static int 14015fe58019SAttilio Rao fuse_vnop_setattr(struct vop_setattr_args *ap) 14025fe58019SAttilio Rao { 14035fe58019SAttilio Rao struct vnode *vp = ap->a_vp; 14045fe58019SAttilio Rao struct vattr *vap = ap->a_vap; 14055fe58019SAttilio Rao struct ucred *cred = ap->a_cred; 14065fe58019SAttilio Rao struct thread *td = curthread; 14075fe58019SAttilio Rao struct fuse_dispatcher fdi; 14085fe58019SAttilio Rao struct fuse_setattr_in *fsai; 1409f8d4af10SAlan Somers pid_t pid = td->td_proc->p_pid; 14105fe58019SAttilio Rao 14115fe58019SAttilio Rao int err = 0; 14125fe58019SAttilio Rao enum vtype vtyp; 14135fe58019SAttilio Rao int sizechanged = 0; 14145fe58019SAttilio Rao uint64_t newsize = 0; 1415ff4fbdf5SAlan Somers accmode_t accmode = 0; 14165fe58019SAttilio Rao 14175fe58019SAttilio Rao if (fuse_isdeadfs(vp)) { 14185fe58019SAttilio Rao return ENXIO; 14195fe58019SAttilio Rao } 14205fe58019SAttilio Rao fdisp_init(&fdi, sizeof(*fsai)); 14215fe58019SAttilio Rao fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred); 14225fe58019SAttilio Rao fsai = fdi.indata; 14235fe58019SAttilio Rao fsai->valid = 0; 14245fe58019SAttilio Rao 14255fe58019SAttilio Rao if (vap->va_uid != (uid_t)VNOVAL) { 14265fe58019SAttilio Rao fsai->uid = vap->va_uid; 14275fe58019SAttilio Rao fsai->valid |= FATTR_UID; 1428ff4fbdf5SAlan Somers accmode |= VADMIN; 14295fe58019SAttilio Rao } 14305fe58019SAttilio Rao if (vap->va_gid != (gid_t)VNOVAL) { 14315fe58019SAttilio Rao fsai->gid = vap->va_gid; 14325fe58019SAttilio Rao fsai->valid |= FATTR_GID; 1433ff4fbdf5SAlan Somers accmode |= VADMIN; 14345fe58019SAttilio Rao } 14355fe58019SAttilio Rao if (vap->va_size != VNOVAL) { 14365fe58019SAttilio Rao 14375fe58019SAttilio Rao struct fuse_filehandle *fufh = NULL; 14385fe58019SAttilio Rao 14395fe58019SAttilio Rao /*Truncate to a new value. */ 14405fe58019SAttilio Rao fsai->size = vap->va_size; 14415fe58019SAttilio Rao sizechanged = 1; 14425fe58019SAttilio Rao newsize = vap->va_size; 14435fe58019SAttilio Rao fsai->valid |= FATTR_SIZE; 1444ff4fbdf5SAlan Somers accmode |= VWRITE; 14455fe58019SAttilio Rao 14469f10f423SAlan Somers fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid); 14475fe58019SAttilio Rao if (fufh) { 14485fe58019SAttilio Rao fsai->fh = fufh->fh_id; 14495fe58019SAttilio Rao fsai->valid |= FATTR_FH; 14505fe58019SAttilio Rao } 14515fe58019SAttilio Rao } 14525fe58019SAttilio Rao if (vap->va_atime.tv_sec != VNOVAL) { 14535fe58019SAttilio Rao fsai->atime = vap->va_atime.tv_sec; 14545fe58019SAttilio Rao fsai->atimensec = vap->va_atime.tv_nsec; 14555fe58019SAttilio Rao fsai->valid |= FATTR_ATIME; 1456ff4fbdf5SAlan Somers accmode |= VADMIN; 1457ff4fbdf5SAlan Somers /* 1458ff4fbdf5SAlan Somers * TODO: only require VWRITE if UTIMENS_NULL is set. PR 237181 1459ff4fbdf5SAlan Somers */ 14605fe58019SAttilio Rao } 14615fe58019SAttilio Rao if (vap->va_mtime.tv_sec != VNOVAL) { 14625fe58019SAttilio Rao fsai->mtime = vap->va_mtime.tv_sec; 14635fe58019SAttilio Rao fsai->mtimensec = vap->va_mtime.tv_nsec; 14645fe58019SAttilio Rao fsai->valid |= FATTR_MTIME; 1465ff4fbdf5SAlan Somers accmode |= VADMIN; 1466ff4fbdf5SAlan Somers /* 1467ff4fbdf5SAlan Somers * TODO: only require VWRITE if UTIMENS_NULL is set. PR 237181 1468ff4fbdf5SAlan Somers */ 14695fe58019SAttilio Rao } 14705fe58019SAttilio Rao if (vap->va_mode != (mode_t)VNOVAL) { 14715fe58019SAttilio Rao fsai->mode = vap->va_mode & ALLPERMS; 14725fe58019SAttilio Rao fsai->valid |= FATTR_MODE; 1473ff4fbdf5SAlan Somers accmode |= VADMIN; 14745fe58019SAttilio Rao } 14755fe58019SAttilio Rao if (!fsai->valid) { 14765fe58019SAttilio Rao goto out; 14775fe58019SAttilio Rao } 14785fe58019SAttilio Rao vtyp = vnode_vtype(vp); 14795fe58019SAttilio Rao 14805fe58019SAttilio Rao if (fsai->valid & FATTR_SIZE && vtyp == VDIR) { 14815fe58019SAttilio Rao err = EISDIR; 14825fe58019SAttilio Rao goto out; 14835fe58019SAttilio Rao } 14845fe58019SAttilio Rao if (vfs_isrdonly(vnode_mount(vp)) && (fsai->valid & ~FATTR_SIZE || vtyp == VREG)) { 14855fe58019SAttilio Rao err = EROFS; 14865fe58019SAttilio Rao goto out; 14875fe58019SAttilio Rao } 1488666f8543SAlan Somers err = fuse_internal_access(vp, accmode, td, cred); 1489ff4fbdf5SAlan Somers if (err) 1490ff4fbdf5SAlan Somers goto out; 14915fe58019SAttilio Rao 1492d9454fabSAttilio Rao if ((err = fdisp_wait_answ(&fdi))) 14935fe58019SAttilio Rao goto out; 14945fe58019SAttilio Rao vtyp = IFTOVT(((struct fuse_attr_out *)fdi.answ)->attr.mode); 14955fe58019SAttilio Rao 14965fe58019SAttilio Rao if (vnode_vtype(vp) != vtyp) { 14975fe58019SAttilio Rao if (vnode_vtype(vp) == VNON && vtyp != VNON) { 1498cf169498SAlan Somers SDT_PROBE2(fuse, , vnops, trace, 1, "FUSE: Dang! " 1499cf169498SAlan Somers "vnode_vtype is VNON and vtype isn't."); 15005fe58019SAttilio Rao } else { 15015fe58019SAttilio Rao /* 15025fe58019SAttilio Rao * STALE vnode, ditch 15035fe58019SAttilio Rao * 1504cf169498SAlan Somers * The vnode has changed its type "behind our back". 1505cf169498SAlan Somers * There's nothing really we can do, so let us just 1506cf169498SAlan Somers * force an internal revocation and tell the caller to 1507cf169498SAlan Somers * try again, if interested. 15085fe58019SAttilio Rao */ 15095fe58019SAttilio Rao fuse_internal_vnode_disappear(vp); 15105fe58019SAttilio Rao err = EAGAIN; 15115fe58019SAttilio Rao } 15125fe58019SAttilio Rao } 15137e4844f7SAlan Somers if (err == 0) { 15147e4844f7SAlan Somers struct fuse_attr_out *fao = (struct fuse_attr_out*)fdi.answ; 15157e4844f7SAlan Somers fuse_internal_cache_attrs(vp, &fao->attr, fao->attr_valid, 15167e4844f7SAlan Somers fao->attr_valid_nsec, NULL); 15177e4844f7SAlan Somers } 1518194e691aSConrad Meyer 15195fe58019SAttilio Rao out: 15205fe58019SAttilio Rao fdisp_destroy(&fdi); 15215fe58019SAttilio Rao if (!err && sizechanged) { 15225fe58019SAttilio Rao fuse_vnode_setsize(vp, cred, newsize); 15235fe58019SAttilio Rao VTOFUD(vp)->flag &= ~FN_SIZECHANGE; 15245fe58019SAttilio Rao } 15255fe58019SAttilio Rao return err; 15265fe58019SAttilio Rao } 15275fe58019SAttilio Rao 15285fe58019SAttilio Rao /* 15295fe58019SAttilio Rao struct vnop_strategy_args { 15305fe58019SAttilio Rao struct vnode *a_vp; 15315fe58019SAttilio Rao struct buf *a_bp; 15325fe58019SAttilio Rao }; 15335fe58019SAttilio Rao */ 15345fe58019SAttilio Rao static int 15355fe58019SAttilio Rao fuse_vnop_strategy(struct vop_strategy_args *ap) 15365fe58019SAttilio Rao { 15375fe58019SAttilio Rao struct vnode *vp = ap->a_vp; 15385fe58019SAttilio Rao struct buf *bp = ap->a_bp; 15395fe58019SAttilio Rao 15405fe58019SAttilio Rao if (!vp || fuse_isdeadfs(vp)) { 15415fe58019SAttilio Rao bp->b_ioflags |= BIO_ERROR; 15425fe58019SAttilio Rao bp->b_error = ENXIO; 15435fe58019SAttilio Rao bufdone(bp); 154498852a32SAlan Somers return 0; 15455fe58019SAttilio Rao } 1546f203d173SAlan Somers if (bp->b_iocmd == BIO_WRITE) { 1547f203d173SAlan Somers int err; 15485fe58019SAttilio Rao 1549f203d173SAlan Somers err = fuse_vnode_refreshsize(vp, NOCRED); 1550f203d173SAlan Somers if (err) { 1551f203d173SAlan Somers bp->b_ioflags |= BIO_ERROR; 1552f203d173SAlan Somers bp->b_error = err; 155398852a32SAlan Somers bufdone(bp); 1554f203d173SAlan Somers return 0; 1555f203d173SAlan Somers } 1556f203d173SAlan Somers } 15575fe58019SAttilio Rao 15585fe58019SAttilio Rao /* 1559f203d173SAlan Somers * VOP_STRATEGY always returns zero and signals error via bp->b_ioflags. 1560f203d173SAlan Somers * fuse_io_strategy sets bp's error fields 15615fe58019SAttilio Rao */ 1562f203d173SAlan Somers (void)fuse_io_strategy(vp, bp); 1563f203d173SAlan Somers 15645fe58019SAttilio Rao return 0; 15655fe58019SAttilio Rao } 15665fe58019SAttilio Rao 15675fe58019SAttilio Rao 15685fe58019SAttilio Rao /* 15695fe58019SAttilio Rao struct vnop_symlink_args { 15705fe58019SAttilio Rao struct vnode *a_dvp; 15715fe58019SAttilio Rao struct vnode **a_vpp; 15725fe58019SAttilio Rao struct componentname *a_cnp; 15735fe58019SAttilio Rao struct vattr *a_vap; 15745fe58019SAttilio Rao char *a_target; 15755fe58019SAttilio Rao }; 15765fe58019SAttilio Rao */ 15775fe58019SAttilio Rao static int 15785fe58019SAttilio Rao fuse_vnop_symlink(struct vop_symlink_args *ap) 15795fe58019SAttilio Rao { 15805fe58019SAttilio Rao struct vnode *dvp = ap->a_dvp; 15815fe58019SAttilio Rao struct vnode **vpp = ap->a_vpp; 15825fe58019SAttilio Rao struct componentname *cnp = ap->a_cnp; 15831493c2eeSBrooks Davis const char *target = ap->a_target; 15845fe58019SAttilio Rao 15855fe58019SAttilio Rao struct fuse_dispatcher fdi; 15865fe58019SAttilio Rao 15875fe58019SAttilio Rao int err; 15885fe58019SAttilio Rao size_t len; 15895fe58019SAttilio Rao 15905fe58019SAttilio Rao if (fuse_isdeadfs(dvp)) { 15915fe58019SAttilio Rao return ENXIO; 15925fe58019SAttilio Rao } 15935fe58019SAttilio Rao /* 15945fe58019SAttilio Rao * Unlike the other creator type calls, here we have to create a message 15955fe58019SAttilio Rao * where the name of the new entry comes first, and the data describing 15965fe58019SAttilio Rao * the entry comes second. 15975fe58019SAttilio Rao * Hence we can't rely on our handy fuse_internal_newentry() routine, 15985fe58019SAttilio Rao * but put together the message manually and just call the core part. 15995fe58019SAttilio Rao */ 16005fe58019SAttilio Rao 16015fe58019SAttilio Rao len = strlen(target) + 1; 16025fe58019SAttilio Rao fdisp_init(&fdi, len + cnp->cn_namelen + 1); 16035fe58019SAttilio Rao fdisp_make_vp(&fdi, FUSE_SYMLINK, dvp, curthread, NULL); 16045fe58019SAttilio Rao 16055fe58019SAttilio Rao memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen); 16065fe58019SAttilio Rao ((char *)fdi.indata)[cnp->cn_namelen] = '\0'; 16075fe58019SAttilio Rao memcpy((char *)fdi.indata + cnp->cn_namelen + 1, target, len); 16085fe58019SAttilio Rao 16095fe58019SAttilio Rao err = fuse_internal_newentry_core(dvp, vpp, cnp, VLNK, &fdi); 16105fe58019SAttilio Rao fdisp_destroy(&fdi); 16115fe58019SAttilio Rao return err; 16125fe58019SAttilio Rao } 16135fe58019SAttilio Rao 16145fe58019SAttilio Rao /* 16155fe58019SAttilio Rao struct vnop_write_args { 16165fe58019SAttilio Rao struct vnode *a_vp; 16175fe58019SAttilio Rao struct uio *a_uio; 16185fe58019SAttilio Rao int a_ioflag; 16195fe58019SAttilio Rao struct ucred *a_cred; 16205fe58019SAttilio Rao }; 16215fe58019SAttilio Rao */ 16225fe58019SAttilio Rao static int 16235fe58019SAttilio Rao fuse_vnop_write(struct vop_write_args *ap) 16245fe58019SAttilio Rao { 16255fe58019SAttilio Rao struct vnode *vp = ap->a_vp; 16265fe58019SAttilio Rao struct uio *uio = ap->a_uio; 16275fe58019SAttilio Rao int ioflag = ap->a_ioflag; 16285fe58019SAttilio Rao struct ucred *cred = ap->a_cred; 1629f8d4af10SAlan Somers pid_t pid = curthread->td_proc->p_pid; 1630f203d173SAlan Somers int err; 16315fe58019SAttilio Rao 16325fe58019SAttilio Rao if (fuse_isdeadfs(vp)) { 16335fe58019SAttilio Rao return ENXIO; 16345fe58019SAttilio Rao } 1635f203d173SAlan Somers err = fuse_vnode_refreshsize(vp, cred); 1636f203d173SAlan Somers if (err) 1637f203d173SAlan Somers return err; 16385fe58019SAttilio Rao 1639ead063e0SEdward Tomasz Napierala if (VTOFUD(vp)->flag & FN_DIRECTIO) { 1640ead063e0SEdward Tomasz Napierala ioflag |= IO_DIRECT; 1641ead063e0SEdward Tomasz Napierala } 1642ead063e0SEdward Tomasz Napierala 1643f8d4af10SAlan Somers return fuse_io_dispatch(vp, uio, ioflag, cred, pid); 16445fe58019SAttilio Rao } 16455fe58019SAttilio Rao 1646cf169498SAlan Somers SDT_PROBE_DEFINE1(fuse, , vnops, vnop_getpages_error, "int"); 16475fe58019SAttilio Rao /* 16485fe58019SAttilio Rao struct vnop_getpages_args { 16495fe58019SAttilio Rao struct vnode *a_vp; 16505fe58019SAttilio Rao vm_page_t *a_m; 16515fe58019SAttilio Rao int a_count; 16525fe58019SAttilio Rao int a_reqpage; 16535fe58019SAttilio Rao }; 16545fe58019SAttilio Rao */ 16555fe58019SAttilio Rao static int 16565fe58019SAttilio Rao fuse_vnop_getpages(struct vop_getpages_args *ap) 16575fe58019SAttilio Rao { 16585fe58019SAttilio Rao int i, error, nextoff, size, toff, count, npages; 16595fe58019SAttilio Rao struct uio uio; 16605fe58019SAttilio Rao struct iovec iov; 16615fe58019SAttilio Rao vm_offset_t kva; 16625fe58019SAttilio Rao struct buf *bp; 16635fe58019SAttilio Rao struct vnode *vp; 16645fe58019SAttilio Rao struct thread *td; 16655fe58019SAttilio Rao struct ucred *cred; 16665fe58019SAttilio Rao vm_page_t *pages; 1667f8d4af10SAlan Somers pid_t pid = curthread->td_proc->p_pid; 16685fe58019SAttilio Rao 16695fe58019SAttilio Rao vp = ap->a_vp; 16705fe58019SAttilio Rao KASSERT(vp->v_object, ("objectless vp passed to getpages")); 16715fe58019SAttilio Rao td = curthread; /* XXX */ 16725fe58019SAttilio Rao cred = curthread->td_ucred; /* XXX */ 16735fe58019SAttilio Rao pages = ap->a_m; 1674f17f88d3SGleb Smirnoff npages = ap->a_count; 16755fe58019SAttilio Rao 16765fe58019SAttilio Rao if (!fsess_opt_mmap(vnode_mount(vp))) { 1677cf169498SAlan Somers SDT_PROBE2(fuse, , vnops, trace, 1, 1678cf169498SAlan Somers "called on non-cacheable vnode??\n"); 16795fe58019SAttilio Rao return (VM_PAGER_ERROR); 16805fe58019SAttilio Rao } 16815fe58019SAttilio Rao 16825fe58019SAttilio Rao /* 1683b0cd2017SGleb Smirnoff * If the last page is partially valid, just return it and allow 1684b0cd2017SGleb Smirnoff * the pager to zero-out the blanks. Partially valid pages can 1685b0cd2017SGleb Smirnoff * only occur at the file EOF. 1686b0cd2017SGleb Smirnoff * 1687b0cd2017SGleb Smirnoff * XXXGL: is that true for FUSE, which is a local filesystem, 1688b0cd2017SGleb Smirnoff * but still somewhat disconnected from the kernel? 16895fe58019SAttilio Rao */ 169089f6b863SAttilio Rao VM_OBJECT_WLOCK(vp->v_object); 1691f17f88d3SGleb Smirnoff if (pages[npages - 1]->valid != 0 && --npages == 0) 1692f17f88d3SGleb Smirnoff goto out; 169389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(vp->v_object); 16945fe58019SAttilio Rao 16955fe58019SAttilio Rao /* 16965fe58019SAttilio Rao * We use only the kva address for the buffer, but this is extremely 1697b3a15dddSPedro F. Giffuni * convenient and fast. 16985fe58019SAttilio Rao */ 1699756a5412SGleb Smirnoff bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK); 17005fe58019SAttilio Rao 17015fe58019SAttilio Rao kva = (vm_offset_t)bp->b_data; 17025fe58019SAttilio Rao pmap_qenter(kva, pages, npages); 170383c9dea1SGleb Smirnoff VM_CNT_INC(v_vnodein); 170483c9dea1SGleb Smirnoff VM_CNT_ADD(v_vnodepgsin, npages); 17055fe58019SAttilio Rao 1706f17f88d3SGleb Smirnoff count = npages << PAGE_SHIFT; 17075fe58019SAttilio Rao iov.iov_base = (caddr_t)kva; 17085fe58019SAttilio Rao iov.iov_len = count; 17095fe58019SAttilio Rao uio.uio_iov = &iov; 17105fe58019SAttilio Rao uio.uio_iovcnt = 1; 17115fe58019SAttilio Rao uio.uio_offset = IDX_TO_OFF(pages[0]->pindex); 17125fe58019SAttilio Rao uio.uio_resid = count; 17135fe58019SAttilio Rao uio.uio_segflg = UIO_SYSSPACE; 17145fe58019SAttilio Rao uio.uio_rw = UIO_READ; 17155fe58019SAttilio Rao uio.uio_td = td; 17165fe58019SAttilio Rao 1717f8d4af10SAlan Somers error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid); 17185fe58019SAttilio Rao pmap_qremove(kva, npages); 17195fe58019SAttilio Rao 1720756a5412SGleb Smirnoff uma_zfree(fuse_pbuf_zone, bp); 17215fe58019SAttilio Rao 17225fe58019SAttilio Rao if (error && (uio.uio_resid == count)) { 1723cf169498SAlan Somers SDT_PROBE1(fuse, , vnops, vnop_getpages_error, error); 17245fe58019SAttilio Rao return VM_PAGER_ERROR; 17255fe58019SAttilio Rao } 17265fe58019SAttilio Rao /* 17275fe58019SAttilio Rao * Calculate the number of bytes read and validate only that number 17285fe58019SAttilio Rao * of bytes. Note that due to pending writes, size may be 0. This 17295fe58019SAttilio Rao * does not mean that the remaining data is invalid! 17305fe58019SAttilio Rao */ 17315fe58019SAttilio Rao 17325fe58019SAttilio Rao size = count - uio.uio_resid; 173389f6b863SAttilio Rao VM_OBJECT_WLOCK(vp->v_object); 17345fe58019SAttilio Rao fuse_vm_page_lock_queues(); 17355fe58019SAttilio Rao for (i = 0, toff = 0; i < npages; i++, toff = nextoff) { 17365fe58019SAttilio Rao vm_page_t m; 17375fe58019SAttilio Rao 17385fe58019SAttilio Rao nextoff = toff + PAGE_SIZE; 17395fe58019SAttilio Rao m = pages[i]; 17405fe58019SAttilio Rao 17415fe58019SAttilio Rao if (nextoff <= size) { 17425fe58019SAttilio Rao /* 17435fe58019SAttilio Rao * Read operation filled an entire page 17445fe58019SAttilio Rao */ 17455fe58019SAttilio Rao m->valid = VM_PAGE_BITS_ALL; 17465fe58019SAttilio Rao KASSERT(m->dirty == 0, 17475fe58019SAttilio Rao ("fuse_getpages: page %p is dirty", m)); 17485fe58019SAttilio Rao } else if (size > toff) { 17495fe58019SAttilio Rao /* 17505fe58019SAttilio Rao * Read operation filled a partial page. 17515fe58019SAttilio Rao */ 17525fe58019SAttilio Rao m->valid = 0; 17535fe58019SAttilio Rao vm_page_set_valid_range(m, 0, size - toff); 17545fe58019SAttilio Rao KASSERT(m->dirty == 0, 17555fe58019SAttilio Rao ("fuse_getpages: page %p is dirty", m)); 17565fe58019SAttilio Rao } else { 17575fe58019SAttilio Rao /* 1758b3a15dddSPedro F. Giffuni * Read operation was short. If no error occurred 17595fe58019SAttilio Rao * we may have hit a zero-fill section. We simply 17605fe58019SAttilio Rao * leave valid set to 0. 17615fe58019SAttilio Rao */ 17625fe58019SAttilio Rao ; 17635fe58019SAttilio Rao } 17645fe58019SAttilio Rao } 17655fe58019SAttilio Rao fuse_vm_page_unlock_queues(); 1766f17f88d3SGleb Smirnoff out: 176789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(vp->v_object); 1768f17f88d3SGleb Smirnoff if (ap->a_rbehind) 1769f17f88d3SGleb Smirnoff *ap->a_rbehind = 0; 1770f17f88d3SGleb Smirnoff if (ap->a_rahead) 1771f17f88d3SGleb Smirnoff *ap->a_rahead = 0; 1772f17f88d3SGleb Smirnoff return (VM_PAGER_OK); 17735fe58019SAttilio Rao } 17745fe58019SAttilio Rao 17755fe58019SAttilio Rao /* 17765fe58019SAttilio Rao struct vnop_putpages_args { 17775fe58019SAttilio Rao struct vnode *a_vp; 17785fe58019SAttilio Rao vm_page_t *a_m; 17795fe58019SAttilio Rao int a_count; 17805fe58019SAttilio Rao int a_sync; 17815fe58019SAttilio Rao int *a_rtvals; 17825fe58019SAttilio Rao vm_ooffset_t a_offset; 17835fe58019SAttilio Rao }; 17845fe58019SAttilio Rao */ 17855fe58019SAttilio Rao static int 17865fe58019SAttilio Rao fuse_vnop_putpages(struct vop_putpages_args *ap) 17875fe58019SAttilio Rao { 17885fe58019SAttilio Rao struct uio uio; 17895fe58019SAttilio Rao struct iovec iov; 17905fe58019SAttilio Rao vm_offset_t kva; 17915fe58019SAttilio Rao struct buf *bp; 17925fe58019SAttilio Rao int i, error, npages, count; 17935fe58019SAttilio Rao off_t offset; 17945fe58019SAttilio Rao int *rtvals; 17955fe58019SAttilio Rao struct vnode *vp; 17965fe58019SAttilio Rao struct thread *td; 17975fe58019SAttilio Rao struct ucred *cred; 17985fe58019SAttilio Rao vm_page_t *pages; 17995fe58019SAttilio Rao vm_ooffset_t fsize; 1800f8d4af10SAlan Somers pid_t pid = curthread->td_proc->p_pid; 18015fe58019SAttilio Rao 18025fe58019SAttilio Rao vp = ap->a_vp; 18035fe58019SAttilio Rao KASSERT(vp->v_object, ("objectless vp passed to putpages")); 18045fe58019SAttilio Rao fsize = vp->v_object->un_pager.vnp.vnp_size; 18055fe58019SAttilio Rao td = curthread; /* XXX */ 18065fe58019SAttilio Rao cred = curthread->td_ucred; /* XXX */ 18075fe58019SAttilio Rao pages = ap->a_m; 18085fe58019SAttilio Rao count = ap->a_count; 18095fe58019SAttilio Rao rtvals = ap->a_rtvals; 18105fe58019SAttilio Rao npages = btoc(count); 18115fe58019SAttilio Rao offset = IDX_TO_OFF(pages[0]->pindex); 18125fe58019SAttilio Rao 18135fe58019SAttilio Rao if (!fsess_opt_mmap(vnode_mount(vp))) { 1814cf169498SAlan Somers SDT_PROBE2(fuse, , vnops, trace, 1, 1815cf169498SAlan Somers "called on non-cacheable vnode??\n"); 18165fe58019SAttilio Rao } 18175fe58019SAttilio Rao for (i = 0; i < npages; i++) 18185fe58019SAttilio Rao rtvals[i] = VM_PAGER_AGAIN; 18195fe58019SAttilio Rao 18205fe58019SAttilio Rao /* 18215fe58019SAttilio Rao * When putting pages, do not extend file past EOF. 18225fe58019SAttilio Rao */ 18235fe58019SAttilio Rao 18245fe58019SAttilio Rao if (offset + count > fsize) { 18255fe58019SAttilio Rao count = fsize - offset; 18265fe58019SAttilio Rao if (count < 0) 18275fe58019SAttilio Rao count = 0; 18285fe58019SAttilio Rao } 18295fe58019SAttilio Rao /* 18305fe58019SAttilio Rao * We use only the kva address for the buffer, but this is extremely 1831b3a15dddSPedro F. Giffuni * convenient and fast. 18325fe58019SAttilio Rao */ 1833756a5412SGleb Smirnoff bp = uma_zalloc(fuse_pbuf_zone, M_WAITOK); 18345fe58019SAttilio Rao 18355fe58019SAttilio Rao kva = (vm_offset_t)bp->b_data; 18365fe58019SAttilio Rao pmap_qenter(kva, pages, npages); 183783c9dea1SGleb Smirnoff VM_CNT_INC(v_vnodeout); 183883c9dea1SGleb Smirnoff VM_CNT_ADD(v_vnodepgsout, count); 18395fe58019SAttilio Rao 18405fe58019SAttilio Rao iov.iov_base = (caddr_t)kva; 18415fe58019SAttilio Rao iov.iov_len = count; 18425fe58019SAttilio Rao uio.uio_iov = &iov; 18435fe58019SAttilio Rao uio.uio_iovcnt = 1; 18445fe58019SAttilio Rao uio.uio_offset = offset; 18455fe58019SAttilio Rao uio.uio_resid = count; 18465fe58019SAttilio Rao uio.uio_segflg = UIO_SYSSPACE; 18475fe58019SAttilio Rao uio.uio_rw = UIO_WRITE; 18485fe58019SAttilio Rao uio.uio_td = td; 18495fe58019SAttilio Rao 1850f8d4af10SAlan Somers error = fuse_io_dispatch(vp, &uio, IO_DIRECT, cred, pid); 18515fe58019SAttilio Rao 18525fe58019SAttilio Rao pmap_qremove(kva, npages); 1853756a5412SGleb Smirnoff uma_zfree(fuse_pbuf_zone, bp); 18545fe58019SAttilio Rao 18555fe58019SAttilio Rao if (!error) { 18565fe58019SAttilio Rao int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE; 18575fe58019SAttilio Rao 18585fe58019SAttilio Rao for (i = 0; i < nwritten; i++) { 18595fe58019SAttilio Rao rtvals[i] = VM_PAGER_OK; 186089f6b863SAttilio Rao VM_OBJECT_WLOCK(pages[i]->object); 18615fe58019SAttilio Rao vm_page_undirty(pages[i]); 186289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(pages[i]->object); 18635fe58019SAttilio Rao } 18645fe58019SAttilio Rao } 18655fe58019SAttilio Rao return rtvals[0]; 18665fe58019SAttilio Rao } 18675fe58019SAttilio Rao 186804660064SFedor Uporov static const char extattr_namespace_separator = '.'; 186904660064SFedor Uporov 187004660064SFedor Uporov /* 187104660064SFedor Uporov struct vop_getextattr_args { 187204660064SFedor Uporov struct vop_generic_args a_gen; 187304660064SFedor Uporov struct vnode *a_vp; 187404660064SFedor Uporov int a_attrnamespace; 187504660064SFedor Uporov const char *a_name; 187604660064SFedor Uporov struct uio *a_uio; 187704660064SFedor Uporov size_t *a_size; 187804660064SFedor Uporov struct ucred *a_cred; 187904660064SFedor Uporov struct thread *a_td; 188004660064SFedor Uporov }; 188104660064SFedor Uporov */ 188204660064SFedor Uporov static int 188304660064SFedor Uporov fuse_vnop_getextattr(struct vop_getextattr_args *ap) 188404660064SFedor Uporov { 188504660064SFedor Uporov struct vnode *vp = ap->a_vp; 188604660064SFedor Uporov struct uio *uio = ap->a_uio; 188728f4f623SFedor Uporov struct fuse_dispatcher fdi; 188804660064SFedor Uporov struct fuse_getxattr_in *get_xattr_in; 188904660064SFedor Uporov struct fuse_getxattr_out *get_xattr_out; 189004660064SFedor Uporov struct mount *mp = vnode_mount(vp); 189104660064SFedor Uporov struct thread *td = ap->a_td; 189204660064SFedor Uporov struct ucred *cred = ap->a_cred; 189328f4f623SFedor Uporov char *prefix; 189428f4f623SFedor Uporov char *attr_str; 189528f4f623SFedor Uporov size_t len; 189628f4f623SFedor Uporov int err; 189704660064SFedor Uporov 189804660064SFedor Uporov if (fuse_isdeadfs(vp)) 189928f4f623SFedor Uporov return (ENXIO); 190004660064SFedor Uporov 1901666f8543SAlan Somers err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD); 1902ff4fbdf5SAlan Somers if (err) 1903ff4fbdf5SAlan Somers return err; 1904ff4fbdf5SAlan Somers 190504660064SFedor Uporov /* Default to looking for user attributes. */ 190604660064SFedor Uporov if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM) 190704660064SFedor Uporov prefix = EXTATTR_NAMESPACE_SYSTEM_STRING; 190804660064SFedor Uporov else 190904660064SFedor Uporov prefix = EXTATTR_NAMESPACE_USER_STRING; 191004660064SFedor Uporov 191104660064SFedor Uporov len = strlen(prefix) + sizeof(extattr_namespace_separator) + 191204660064SFedor Uporov strlen(ap->a_name) + 1; 191304660064SFedor Uporov 191404660064SFedor Uporov fdisp_init(&fdi, len + sizeof(*get_xattr_in)); 191504660064SFedor Uporov fdisp_make_vp(&fdi, FUSE_GETXATTR, vp, td, cred); 191604660064SFedor Uporov 191704660064SFedor Uporov get_xattr_in = fdi.indata; 191804660064SFedor Uporov /* 191904660064SFedor Uporov * Check to see whether we're querying the available size or 192004660064SFedor Uporov * issuing the actual request. If we pass in 0, we get back struct 192104660064SFedor Uporov * fuse_getxattr_out. If we pass in a non-zero size, we get back 192204660064SFedor Uporov * that much data, without the struct fuse_getxattr_out header. 192304660064SFedor Uporov */ 1924493b4a8cSFedor Uporov if (uio == NULL) 192504660064SFedor Uporov get_xattr_in->size = 0; 192604660064SFedor Uporov else 192704660064SFedor Uporov get_xattr_in->size = uio->uio_resid; 192804660064SFedor Uporov 192904660064SFedor Uporov attr_str = (char *)fdi.indata + sizeof(*get_xattr_in); 193004660064SFedor Uporov snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator, 193104660064SFedor Uporov ap->a_name); 193204660064SFedor Uporov 193304660064SFedor Uporov err = fdisp_wait_answ(&fdi); 193404660064SFedor Uporov if (err != 0) { 193504660064SFedor Uporov if (err == ENOSYS) 193604660064SFedor Uporov fsess_set_notimpl(mp, FUSE_GETXATTR); 193704660064SFedor Uporov goto out; 193804660064SFedor Uporov } 193904660064SFedor Uporov 194004660064SFedor Uporov get_xattr_out = fdi.answ; 194104660064SFedor Uporov 1942493b4a8cSFedor Uporov if (ap->a_size != NULL) 194304660064SFedor Uporov *ap->a_size = get_xattr_out->size; 1944493b4a8cSFedor Uporov 1945493b4a8cSFedor Uporov if (uio != NULL) 194604660064SFedor Uporov err = uiomove(fdi.answ, fdi.iosize, uio); 194704660064SFedor Uporov 194804660064SFedor Uporov out: 194904660064SFedor Uporov fdisp_destroy(&fdi); 195004660064SFedor Uporov return (err); 195104660064SFedor Uporov } 195204660064SFedor Uporov 195304660064SFedor Uporov /* 195404660064SFedor Uporov struct vop_setextattr_args { 195504660064SFedor Uporov struct vop_generic_args a_gen; 195604660064SFedor Uporov struct vnode *a_vp; 195704660064SFedor Uporov int a_attrnamespace; 195804660064SFedor Uporov const char *a_name; 195904660064SFedor Uporov struct uio *a_uio; 196004660064SFedor Uporov struct ucred *a_cred; 196104660064SFedor Uporov struct thread *a_td; 196204660064SFedor Uporov }; 196304660064SFedor Uporov */ 196404660064SFedor Uporov static int 196504660064SFedor Uporov fuse_vnop_setextattr(struct vop_setextattr_args *ap) 196604660064SFedor Uporov { 196704660064SFedor Uporov struct vnode *vp = ap->a_vp; 196804660064SFedor Uporov struct uio *uio = ap->a_uio; 196928f4f623SFedor Uporov struct fuse_dispatcher fdi; 197004660064SFedor Uporov struct fuse_setxattr_in *set_xattr_in; 197104660064SFedor Uporov struct mount *mp = vnode_mount(vp); 197228f4f623SFedor Uporov struct thread *td = ap->a_td; 197328f4f623SFedor Uporov struct ucred *cred = ap->a_cred; 197404660064SFedor Uporov char *prefix; 197504660064SFedor Uporov size_t len; 197604660064SFedor Uporov char *attr_str; 197728f4f623SFedor Uporov int err; 197804660064SFedor Uporov 197904660064SFedor Uporov if (fuse_isdeadfs(vp)) 198028f4f623SFedor Uporov return (ENXIO); 198104660064SFedor Uporov 1982666f8543SAlan Somers if (vfs_isrdonly(mp)) 1983666f8543SAlan Somers return EROFS; 1984666f8543SAlan Somers 1985ff4fbdf5SAlan Somers /* Deleting xattrs must use VOP_DELETEEXTATTR instead */ 1986ff4fbdf5SAlan Somers if (ap->a_uio == NULL) 1987ff4fbdf5SAlan Somers return (EINVAL); 1988ff4fbdf5SAlan Somers 1989666f8543SAlan Somers err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, 1990666f8543SAlan Somers VWRITE); 1991ff4fbdf5SAlan Somers if (err) 1992ff4fbdf5SAlan Somers return err; 1993ff4fbdf5SAlan Somers 199404660064SFedor Uporov /* Default to looking for user attributes. */ 199504660064SFedor Uporov if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM) 199604660064SFedor Uporov prefix = EXTATTR_NAMESPACE_SYSTEM_STRING; 199704660064SFedor Uporov else 199804660064SFedor Uporov prefix = EXTATTR_NAMESPACE_USER_STRING; 199904660064SFedor Uporov 200004660064SFedor Uporov len = strlen(prefix) + sizeof(extattr_namespace_separator) + 200104660064SFedor Uporov strlen(ap->a_name) + 1; 200204660064SFedor Uporov 200304660064SFedor Uporov fdisp_init(&fdi, len + sizeof(*set_xattr_in) + uio->uio_resid); 200404660064SFedor Uporov fdisp_make_vp(&fdi, FUSE_SETXATTR, vp, td, cred); 200504660064SFedor Uporov 200604660064SFedor Uporov set_xattr_in = fdi.indata; 200704660064SFedor Uporov set_xattr_in->size = uio->uio_resid; 200804660064SFedor Uporov 200904660064SFedor Uporov attr_str = (char *)fdi.indata + sizeof(*set_xattr_in); 201004660064SFedor Uporov snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator, 201104660064SFedor Uporov ap->a_name); 201204660064SFedor Uporov 201304660064SFedor Uporov err = uiomove((char *)fdi.indata + sizeof(*set_xattr_in) + len, 201404660064SFedor Uporov uio->uio_resid, uio); 201504660064SFedor Uporov if (err != 0) { 201604660064SFedor Uporov goto out; 201704660064SFedor Uporov } 201804660064SFedor Uporov 201904660064SFedor Uporov err = fdisp_wait_answ(&fdi); 202004660064SFedor Uporov 202104660064SFedor Uporov if (err != 0) { 202204660064SFedor Uporov if (err == ENOSYS) 202304660064SFedor Uporov fsess_set_notimpl(mp, FUSE_SETXATTR); 202404660064SFedor Uporov goto out; 202504660064SFedor Uporov } 202604660064SFedor Uporov 202704660064SFedor Uporov out: 202804660064SFedor Uporov fdisp_destroy(&fdi); 202904660064SFedor Uporov return (err); 203004660064SFedor Uporov } 203104660064SFedor Uporov 203204660064SFedor Uporov /* 203304660064SFedor Uporov * The Linux / FUSE extended attribute list is simply a collection of 203404660064SFedor Uporov * NUL-terminated strings. The FreeBSD extended attribute list is a single 203504660064SFedor Uporov * byte length followed by a non-NUL terminated string. So, this allows 203604660064SFedor Uporov * conversion of the Linux / FUSE format to the FreeBSD format in place. 203704660064SFedor Uporov * Linux attribute names are reported with the namespace as a prefix (e.g. 203804660064SFedor Uporov * "user.attribute_name"), but in FreeBSD they are reported without the 203904660064SFedor Uporov * namespace prefix (e.g. "attribute_name"). So, we're going from: 204004660064SFedor Uporov * 204104660064SFedor Uporov * user.attr_name1\0user.attr_name2\0 204204660064SFedor Uporov * 204304660064SFedor Uporov * to: 204404660064SFedor Uporov * 204504660064SFedor Uporov * <num>attr_name1<num>attr_name2 204604660064SFedor Uporov * 204704660064SFedor Uporov * Where "<num>" is a single byte number of characters in the attribute name. 204804660064SFedor Uporov * 204904660064SFedor Uporov * Args: 205004660064SFedor Uporov * prefix - exattr namespace prefix string 205104660064SFedor Uporov * list, list_len - input list with namespace prefixes 205204660064SFedor Uporov * bsd_list, bsd_list_len - output list compatible with bsd vfs 205304660064SFedor Uporov */ 205404660064SFedor Uporov static int 205504660064SFedor Uporov fuse_xattrlist_convert(char *prefix, const char *list, int list_len, 205604660064SFedor Uporov char *bsd_list, int *bsd_list_len) 205704660064SFedor Uporov { 205804660064SFedor Uporov int len, pos, dist_to_next, prefix_len; 205904660064SFedor Uporov 206004660064SFedor Uporov pos = 0; 206104660064SFedor Uporov *bsd_list_len = 0; 206204660064SFedor Uporov prefix_len = strlen(prefix); 206304660064SFedor Uporov 206404660064SFedor Uporov while (pos < list_len && list[pos] != '\0') { 206504660064SFedor Uporov dist_to_next = strlen(&list[pos]) + 1; 206604660064SFedor Uporov if (bcmp(&list[pos], prefix, prefix_len) == 0 && 206704660064SFedor Uporov list[pos + prefix_len] == extattr_namespace_separator) { 206804660064SFedor Uporov len = dist_to_next - 206904660064SFedor Uporov (prefix_len + sizeof(extattr_namespace_separator)) - 1; 207004660064SFedor Uporov if (len >= EXTATTR_MAXNAMELEN) 207104660064SFedor Uporov return (ENAMETOOLONG); 207204660064SFedor Uporov 207304660064SFedor Uporov bsd_list[*bsd_list_len] = len; 207404660064SFedor Uporov memcpy(&bsd_list[*bsd_list_len + 1], 207504660064SFedor Uporov &list[pos + prefix_len + 207604660064SFedor Uporov sizeof(extattr_namespace_separator)], len); 207704660064SFedor Uporov 207804660064SFedor Uporov *bsd_list_len += len + 1; 207904660064SFedor Uporov } 208004660064SFedor Uporov 208104660064SFedor Uporov pos += dist_to_next; 208204660064SFedor Uporov } 208304660064SFedor Uporov 208404660064SFedor Uporov return (0); 208504660064SFedor Uporov } 208604660064SFedor Uporov 208704660064SFedor Uporov /* 208804660064SFedor Uporov struct vop_listextattr_args { 208904660064SFedor Uporov struct vop_generic_args a_gen; 209004660064SFedor Uporov struct vnode *a_vp; 209104660064SFedor Uporov int a_attrnamespace; 209204660064SFedor Uporov struct uio *a_uio; 209304660064SFedor Uporov size_t *a_size; 209404660064SFedor Uporov struct ucred *a_cred; 209504660064SFedor Uporov struct thread *a_td; 209604660064SFedor Uporov }; 209704660064SFedor Uporov */ 209804660064SFedor Uporov static int 209904660064SFedor Uporov fuse_vnop_listextattr(struct vop_listextattr_args *ap) 210004660064SFedor Uporov { 210104660064SFedor Uporov struct vnode *vp = ap->a_vp; 210204660064SFedor Uporov struct uio *uio = ap->a_uio; 210328f4f623SFedor Uporov struct fuse_dispatcher fdi; 2104493b4a8cSFedor Uporov struct fuse_listxattr_in *list_xattr_in; 2105493b4a8cSFedor Uporov struct fuse_listxattr_out *list_xattr_out; 210604660064SFedor Uporov struct mount *mp = vnode_mount(vp); 210728f4f623SFedor Uporov struct thread *td = ap->a_td; 210828f4f623SFedor Uporov struct ucred *cred = ap->a_cred; 210904660064SFedor Uporov size_t len; 211004660064SFedor Uporov char *prefix; 211104660064SFedor Uporov char *attr_str; 211204660064SFedor Uporov char *bsd_list = NULL; 2113493b4a8cSFedor Uporov char *linux_list; 211404660064SFedor Uporov int bsd_list_len; 2115493b4a8cSFedor Uporov int linux_list_len; 211628f4f623SFedor Uporov int err; 211704660064SFedor Uporov 211804660064SFedor Uporov if (fuse_isdeadfs(vp)) 211928f4f623SFedor Uporov return (ENXIO); 212004660064SFedor Uporov 2121666f8543SAlan Somers err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, VREAD); 2122ff4fbdf5SAlan Somers if (err) 2123ff4fbdf5SAlan Somers return err; 2124ff4fbdf5SAlan Somers 212504660064SFedor Uporov /* 212604660064SFedor Uporov * Add space for a NUL and the period separator if enabled. 212704660064SFedor Uporov * Default to looking for user attributes. 212804660064SFedor Uporov */ 212904660064SFedor Uporov if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM) 213004660064SFedor Uporov prefix = EXTATTR_NAMESPACE_SYSTEM_STRING; 213104660064SFedor Uporov else 213204660064SFedor Uporov prefix = EXTATTR_NAMESPACE_USER_STRING; 213304660064SFedor Uporov 213404660064SFedor Uporov len = strlen(prefix) + sizeof(extattr_namespace_separator) + 1; 213504660064SFedor Uporov 2136493b4a8cSFedor Uporov fdisp_init(&fdi, sizeof(*list_xattr_in) + len); 213704660064SFedor Uporov fdisp_make_vp(&fdi, FUSE_LISTXATTR, vp, td, cred); 213804660064SFedor Uporov 2139493b4a8cSFedor Uporov /* 2140493b4a8cSFedor Uporov * Retrieve Linux / FUSE compatible list size. 2141493b4a8cSFedor Uporov */ 2142493b4a8cSFedor Uporov list_xattr_in = fdi.indata; 2143493b4a8cSFedor Uporov list_xattr_in->size = 0; 2144493b4a8cSFedor Uporov attr_str = (char *)fdi.indata + sizeof(*list_xattr_in); 214504660064SFedor Uporov snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator); 214604660064SFedor Uporov 214704660064SFedor Uporov err = fdisp_wait_answ(&fdi); 214804660064SFedor Uporov if (err != 0) { 214904660064SFedor Uporov if (err == ENOSYS) 215004660064SFedor Uporov fsess_set_notimpl(mp, FUSE_LISTXATTR); 215104660064SFedor Uporov goto out; 215204660064SFedor Uporov } 215304660064SFedor Uporov 2154493b4a8cSFedor Uporov list_xattr_out = fdi.answ; 2155493b4a8cSFedor Uporov linux_list_len = list_xattr_out->size; 2156493b4a8cSFedor Uporov if (linux_list_len == 0) { 2157493b4a8cSFedor Uporov if (ap->a_size != NULL) 2158493b4a8cSFedor Uporov *ap->a_size = linux_list_len; 215904660064SFedor Uporov goto out; 216004660064SFedor Uporov } 216104660064SFedor Uporov 216204660064SFedor Uporov /* 2163493b4a8cSFedor Uporov * Retrieve Linux / FUSE compatible list values. 2164493b4a8cSFedor Uporov */ 216512292a99SAlan Somers fdisp_refresh_vp(&fdi, FUSE_LISTXATTR, vp, td, cred); 2166493b4a8cSFedor Uporov list_xattr_in = fdi.indata; 2167493b4a8cSFedor Uporov list_xattr_in->size = linux_list_len + sizeof(*list_xattr_out); 216812292a99SAlan Somers list_xattr_in->flags = 0; 2169493b4a8cSFedor Uporov attr_str = (char *)fdi.indata + sizeof(*list_xattr_in); 2170493b4a8cSFedor Uporov snprintf(attr_str, len, "%s%c", prefix, extattr_namespace_separator); 2171493b4a8cSFedor Uporov 2172493b4a8cSFedor Uporov err = fdisp_wait_answ(&fdi); 2173493b4a8cSFedor Uporov if (err != 0) 2174493b4a8cSFedor Uporov goto out; 2175493b4a8cSFedor Uporov 2176493b4a8cSFedor Uporov linux_list = fdi.answ; 2177493b4a8cSFedor Uporov linux_list_len = fdi.iosize; 2178493b4a8cSFedor Uporov 2179493b4a8cSFedor Uporov /* 2180493b4a8cSFedor Uporov * Retrieve the BSD compatible list values. 218104660064SFedor Uporov * The Linux / FUSE attribute list format isn't the same 218204660064SFedor Uporov * as FreeBSD's format. So we need to transform it into 218304660064SFedor Uporov * FreeBSD's format before giving it to the user. 218404660064SFedor Uporov */ 2185493b4a8cSFedor Uporov bsd_list = malloc(linux_list_len, M_TEMP, M_WAITOK); 2186493b4a8cSFedor Uporov err = fuse_xattrlist_convert(prefix, linux_list, linux_list_len, 218704660064SFedor Uporov bsd_list, &bsd_list_len); 218804660064SFedor Uporov if (err != 0) 218904660064SFedor Uporov goto out; 219004660064SFedor Uporov 2191493b4a8cSFedor Uporov if (ap->a_size != NULL) 2192493b4a8cSFedor Uporov *ap->a_size = bsd_list_len; 2193493b4a8cSFedor Uporov 2194493b4a8cSFedor Uporov if (uio != NULL) 219504660064SFedor Uporov err = uiomove(bsd_list, bsd_list_len, uio); 219604660064SFedor Uporov 219704660064SFedor Uporov out: 219804660064SFedor Uporov free(bsd_list, M_TEMP); 219904660064SFedor Uporov fdisp_destroy(&fdi); 220004660064SFedor Uporov return (err); 220104660064SFedor Uporov } 220204660064SFedor Uporov 220304660064SFedor Uporov /* 220404660064SFedor Uporov struct vop_deleteextattr_args { 220504660064SFedor Uporov struct vop_generic_args a_gen; 220604660064SFedor Uporov struct vnode *a_vp; 220704660064SFedor Uporov int a_attrnamespace; 220804660064SFedor Uporov const char *a_name; 220904660064SFedor Uporov struct ucred *a_cred; 221004660064SFedor Uporov struct thread *a_td; 221104660064SFedor Uporov }; 221204660064SFedor Uporov */ 221304660064SFedor Uporov static int 221404660064SFedor Uporov fuse_vnop_deleteextattr(struct vop_deleteextattr_args *ap) 221504660064SFedor Uporov { 221604660064SFedor Uporov struct vnode *vp = ap->a_vp; 221728f4f623SFedor Uporov struct fuse_dispatcher fdi; 221804660064SFedor Uporov struct mount *mp = vnode_mount(vp); 221928f4f623SFedor Uporov struct thread *td = ap->a_td; 222028f4f623SFedor Uporov struct ucred *cred = ap->a_cred; 222104660064SFedor Uporov char *prefix; 222204660064SFedor Uporov size_t len; 222304660064SFedor Uporov char *attr_str; 222404660064SFedor Uporov int err; 222504660064SFedor Uporov 222604660064SFedor Uporov if (fuse_isdeadfs(vp)) 222728f4f623SFedor Uporov return (ENXIO); 222804660064SFedor Uporov 2229666f8543SAlan Somers if (vfs_isrdonly(mp)) 2230666f8543SAlan Somers return EROFS; 2231666f8543SAlan Somers 2232666f8543SAlan Somers err = fuse_extattr_check_cred(vp, ap->a_attrnamespace, cred, td, 2233666f8543SAlan Somers VWRITE); 2234ff4fbdf5SAlan Somers if (err) 2235ff4fbdf5SAlan Somers return err; 2236ff4fbdf5SAlan Somers 223704660064SFedor Uporov /* Default to looking for user attributes. */ 223804660064SFedor Uporov if (ap->a_attrnamespace == EXTATTR_NAMESPACE_SYSTEM) 223904660064SFedor Uporov prefix = EXTATTR_NAMESPACE_SYSTEM_STRING; 224004660064SFedor Uporov else 224104660064SFedor Uporov prefix = EXTATTR_NAMESPACE_USER_STRING; 224204660064SFedor Uporov 224304660064SFedor Uporov len = strlen(prefix) + sizeof(extattr_namespace_separator) + 224404660064SFedor Uporov strlen(ap->a_name) + 1; 224504660064SFedor Uporov 224604660064SFedor Uporov fdisp_init(&fdi, len); 224704660064SFedor Uporov fdisp_make_vp(&fdi, FUSE_REMOVEXATTR, vp, td, cred); 224804660064SFedor Uporov 224904660064SFedor Uporov attr_str = fdi.indata; 225004660064SFedor Uporov snprintf(attr_str, len, "%s%c%s", prefix, extattr_namespace_separator, 225104660064SFedor Uporov ap->a_name); 225204660064SFedor Uporov 225304660064SFedor Uporov err = fdisp_wait_answ(&fdi); 225404660064SFedor Uporov if (err != 0) { 225504660064SFedor Uporov if (err == ENOSYS) 225604660064SFedor Uporov fsess_set_notimpl(mp, FUSE_REMOVEXATTR); 225704660064SFedor Uporov } 225804660064SFedor Uporov 225904660064SFedor Uporov fdisp_destroy(&fdi); 226004660064SFedor Uporov return (err); 226104660064SFedor Uporov } 226204660064SFedor Uporov 22635fe58019SAttilio Rao /* 22645fe58019SAttilio Rao struct vnop_print_args { 22655fe58019SAttilio Rao struct vnode *a_vp; 22665fe58019SAttilio Rao }; 22675fe58019SAttilio Rao */ 22685fe58019SAttilio Rao static int 22695fe58019SAttilio Rao fuse_vnop_print(struct vop_print_args *ap) 22705fe58019SAttilio Rao { 22715fe58019SAttilio Rao struct fuse_vnode_data *fvdat = VTOFUD(ap->a_vp); 22725fe58019SAttilio Rao 22735fe58019SAttilio Rao printf("nodeid: %ju, parent nodeid: %ju, nlookup: %ju, flag: %#x\n", 22745fe58019SAttilio Rao (uintmax_t)VTOILLU(ap->a_vp), (uintmax_t)fvdat->parent_nid, 22755fe58019SAttilio Rao (uintmax_t)fvdat->nlookup, 22765fe58019SAttilio Rao fvdat->flag); 22775fe58019SAttilio Rao 22785fe58019SAttilio Rao return 0; 22795fe58019SAttilio Rao } 2280