1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2007-2009 Google Inc. and Amit Singh 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are 9 * met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following disclaimer 15 * in the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Google Inc. nor the names of its 18 * contributors may be used to endorse or promote products derived from 19 * this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 * Copyright (C) 2005 Csaba Henk. 34 * All rights reserved. 35 * 36 * Copyright (c) 2019 The FreeBSD Foundation 37 * 38 * Portions of this software were developed by BFF Storage Systems, LLC under 39 * sponsorship from the FreeBSD Foundation. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 50 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 */ 62 63 #ifndef _FUSE_NODE_H_ 64 #define _FUSE_NODE_H_ 65 66 #include <sys/fnv_hash.h> 67 #include <sys/types.h> 68 #include <sys/mutex.h> 69 #include <sys/buf.h> 70 71 #include "fuse_file.h" 72 73 #define FN_REVOKED 0x00000020 74 /* 75 * Indicates that the file's size is dirty; the kernel has changed it but not 76 * yet send the change to the daemon. When this bit is set, the 77 * cache_attrs.va_size field does not time out. 78 */ 79 #define FN_SIZECHANGE 0x00000100 80 /* 81 * Whether I/O to this vnode should bypass the cache. 82 * XXX BUG: this should be part of the file handle, not the vnode data. 83 * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=293088 84 */ 85 #define FN_DIRECTIO 0x00000200 86 /* Indicates that parent_nid is valid */ 87 #define FN_PARENT_NID 0x00000400 88 89 /* 90 * Indicates that the file's cached timestamps are dirty. They will be flushed 91 * during the next SETATTR or WRITE. Until then, the cached fields will not 92 * time out. 93 */ 94 #define FN_MTIMECHANGE 0x00000800 95 #define FN_CTIMECHANGE 0x00001000 96 #define FN_ATIMECHANGE 0x00002000 97 98 /* vop_delayed_setsize should truncate the file */ 99 #define FN_DELAYED_TRUNCATE 0x00004000 100 101 #define CACHED_ATTR_LOCK(vp) \ 102 do { \ 103 ASSERT_VOP_LOCKED(vp, __func__); \ 104 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) \ 105 mtx_lock(&VTOFUD(vp)->cached_attr_mtx); \ 106 } while(0) 107 108 #define CACHED_ATTR_UNLOCK(vp) \ 109 do { \ 110 ASSERT_VOP_LOCKED(vp, __func__); \ 111 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) \ 112 mtx_unlock(&VTOFUD(vp)->cached_attr_mtx); \ 113 } while(0) 114 115 struct fuse_vnode_data { 116 /* self's node id, similar to an inode number. Immutable. */ 117 uint64_t nid; 118 /* 119 * Generation number. Distinguishes files with same nid but that don't 120 * overlap in time. Immutable. 121 */ 122 uint64_t generation; 123 124 /* parent's node id. Protected by the vnode lock. */ 125 uint64_t parent_nid; 126 127 /** I/O **/ 128 /* 129 * List of file handles for all of the vnode's open file descriptors. 130 * Protected by the vnode lock. 131 */ 132 LIST_HEAD(, fuse_filehandle) handles; 133 134 /* Protects flag, attr_cache_timeout and cached_attrs */ 135 struct mtx cached_attr_mtx; 136 137 /* 138 * The monotonic time after which the attr cache is invalid 139 * Protected by an exclusive vnode lock or the cached_attr_mtx 140 */ 141 struct bintime attr_cache_timeout; 142 143 /* 144 * Monotonic time after which the entry is invalid. Used for lookups 145 * by nodeid instead of pathname. Protected by the vnode lock. 146 */ 147 struct bintime entry_cache_timeout; 148 149 /* 150 * Monotonic time of the last FUSE operation that modified the file 151 * size. Used to avoid races between mutator ops like VOP_SETATTR and 152 * unlocked accessor ops like VOP_LOOKUP. Protected by the vnode lock. 153 */ 154 struct timespec last_local_modify; 155 156 /* Protected by an exclusive vnode lock or the cached_attr_mtx */ 157 struct vattr cached_attrs; 158 159 /* Number of FUSE_LOOKUPs minus FUSE_FORGETs. Protected by vnode lock */ 160 uint64_t nlookup; 161 162 /* 163 * Misc flags. Protected by an exclusive vnode lock or the 164 * cached_attr_mtx, because some of the flags reflect the contents of 165 * cached_attrs. 166 */ 167 uint32_t flag; 168 169 /* Vnode type. Immutable */ 170 __enum_uint8(vtype) vtype; 171 172 /* State for clustered writes. Protected by vnode lock */ 173 struct vn_clusterw clusterw; 174 }; 175 176 /* 177 * This overlays the fid structure (see mount.h). Mostly the same as the types 178 * used by UFS and ext2. 179 */ 180 struct fuse_fid { 181 uint16_t len; /* Length of structure. */ 182 uint16_t pad; /* Force 32-bit alignment. */ 183 uint32_t gen; /* Generation number. */ 184 uint64_t nid; /* FUSE node id. */ 185 }; 186 187 #define VTOFUD(vp) \ 188 ((struct fuse_vnode_data *)((vp)->v_data)) 189 #define VTOI(vp) (VTOFUD(vp)->nid) 190 191 #define ASSERT_CACHED_ATTRS_LOCKED(vp) \ 192 do { \ 193 ASSERT_VOP_LOCKED(vp, __func__); \ 194 VNASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE || \ 195 mtx_owned(&VTOFUD(vp)->cached_attr_mtx), vp, \ 196 ("cached attrs not locked")); \ 197 } while(0) 198 199 static inline bool 200 fuse_vnode_attr_cache_valid(struct vnode *vp) 201 { 202 struct fuse_vnode_data *fvdat = VTOFUD(vp); 203 struct bintime now; 204 205 ASSERT_CACHED_ATTRS_LOCKED(vp); 206 207 getbinuptime(&now); 208 return (bintime_cmp(&fvdat->attr_cache_timeout, &now, >)); 209 } 210 211 static inline struct vattr* 212 VTOVA(struct vnode *vp) 213 { 214 ASSERT_CACHED_ATTRS_LOCKED(vp); 215 216 if (fuse_vnode_attr_cache_valid(vp)) 217 return &(VTOFUD(vp)->cached_attrs); 218 else 219 return NULL; 220 } 221 222 static inline void 223 fuse_vnode_clear_attr_cache(struct vnode *vp) 224 { 225 ASSERT_CACHED_ATTRS_LOCKED(vp); 226 227 bintime_clear(&VTOFUD(vp)->attr_cache_timeout); 228 } 229 230 static uint32_t inline 231 fuse_vnode_hash(uint64_t id) 232 { 233 return (fnv_32_buf(&id, sizeof(id), FNV1_32_INIT)); 234 } 235 236 #define VTOILLU(vp) ((uint64_t)(VTOFUD(vp) ? VTOI(vp) : 0)) 237 238 #define FUSE_NULL_ID 0 239 240 extern struct vop_vector fuse_fifoops; 241 extern struct vop_vector fuse_vnops; 242 243 int fuse_vnode_cmp(struct vnode *vp, void *nidp); 244 245 static inline void 246 fuse_vnode_setparent(struct vnode *vp, struct vnode *dvp) 247 { 248 if (dvp != NULL && vp->v_type == VDIR) { 249 ASSERT_VOP_ELOCKED(vp, __func__); /* for parent_nid */ 250 251 MPASS(dvp->v_type == VDIR); 252 VTOFUD(vp)->parent_nid = VTOI(dvp); 253 VTOFUD(vp)->flag |= FN_PARENT_NID; 254 } else { 255 ASSERT_CACHED_ATTRS_LOCKED(vp); 256 257 VTOFUD(vp)->flag &= ~FN_PARENT_NID; 258 } 259 } 260 261 int fuse_vnode_size(struct vnode *vp, off_t *filesize, struct ucred *cred, 262 struct thread *td); 263 264 void fuse_vnode_destroy(struct vnode *vp); 265 266 int fuse_vnode_get(struct mount *mp, struct fuse_entry_out *feo, 267 uint64_t nodeid, struct vnode *dvp, struct vnode **vpp, 268 struct componentname *cnp, __enum_uint8(vtype) vtyp); 269 270 void fuse_vnode_open(struct vnode *vp, int32_t fuse_open_flags, 271 struct thread *td); 272 273 int fuse_vnode_savesize(struct vnode *vp, struct ucred *cred, pid_t pid); 274 275 int fuse_vnode_setsize(struct vnode *vp, off_t newsize, bool from_server); 276 int fuse_vnode_setsize_immediate(struct vnode *vp, bool shrink); 277 278 void fuse_vnode_undirty_cached_timestamps(struct vnode *vp, bool atime); 279 280 void fuse_vnode_update(struct vnode *vp, int flags); 281 282 void fuse_node_init(void); 283 void fuse_node_destroy(void); 284 #endif /* _FUSE_NODE_H_ */ 285