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 #define FN_FLUSHINPROG 0x00000040
75 #define FN_FLUSHWANT 0x00000080
76 /*
77 * Indicates that the file's size is dirty; the kernel has changed it but not
78 * yet send the change to the daemon. When this bit is set, the
79 * cache_attrs.va_size field does not time out.
80 */
81 #define FN_SIZECHANGE 0x00000100
82 /*
83 * Whether I/O to this vnode should bypass the cache.
84 * XXX BUG: this should be part of the file handle, not the vnode data.
85 * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=293088
86 */
87 #define FN_DIRECTIO 0x00000200
88 /* Indicates that parent_nid is valid */
89 #define FN_PARENT_NID 0x00000400
90
91 /*
92 * Indicates that the file's cached timestamps are dirty. They will be flushed
93 * during the next SETATTR or WRITE. Until then, the cached fields will not
94 * time out.
95 */
96 #define FN_MTIMECHANGE 0x00000800
97 #define FN_CTIMECHANGE 0x00001000
98 #define FN_ATIMECHANGE 0x00002000
99
100 /* vop_delayed_setsize should truncate the file */
101 #define FN_DELAYED_TRUNCATE 0x00004000
102
103 #define CACHED_ATTR_LOCK(vp) \
104 do { \
105 ASSERT_VOP_LOCKED(vp, __func__); \
106 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) \
107 mtx_lock(&VTOFUD(vp)->cached_attr_mtx); \
108 } while(0)
109
110 #define CACHED_ATTR_UNLOCK(vp) \
111 do { \
112 ASSERT_VOP_LOCKED(vp, __func__); \
113 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) \
114 mtx_unlock(&VTOFUD(vp)->cached_attr_mtx); \
115 } while(0)
116
117 struct fuse_vnode_data {
118 /* self's node id, similar to an inode number. Immutable. */
119 uint64_t nid;
120 /*
121 * Generation number. Distinguishes files with same nid but that don't
122 * overlap in time. Immutable.
123 */
124 uint64_t generation;
125
126 /* parent's node id. Protected by the vnode lock. */
127 uint64_t parent_nid;
128
129 /** I/O **/
130 /*
131 * List of file handles for all of the vnode's open file descriptors.
132 * Protected by the vnode lock.
133 */
134 LIST_HEAD(, fuse_filehandle) handles;
135
136 /* Protects flag, attr_cache_timeout and cached_attrs */
137 struct mtx cached_attr_mtx;
138
139 /*
140 * The monotonic time after which the attr cache is invalid
141 * Protected by an exclusive vnode lock or the cached_attr_mtx
142 */
143 struct bintime attr_cache_timeout;
144
145 /*
146 * Monotonic time after which the entry is invalid. Used for lookups
147 * by nodeid instead of pathname. Protected by the vnode lock.
148 */
149 struct bintime entry_cache_timeout;
150
151 /*
152 * Monotonic time of the last FUSE operation that modified the file
153 * size. Used to avoid races between mutator ops like VOP_SETATTR and
154 * unlocked accessor ops like VOP_LOOKUP. Protected by the vnode lock.
155 */
156 struct timespec last_local_modify;
157
158 /* Protected by an exclusive vnode lock or the cached_attr_mtx */
159 struct vattr cached_attrs;
160
161 /* Number of FUSE_LOOKUPs minus FUSE_FORGETs. Protected by vnode lock */
162 uint64_t nlookup;
163
164 /*
165 * Misc flags. Protected by an exclusive vnode lock or the
166 * cached_attr_mtx, because some of the flags reflect the contents of
167 * cached_attrs.
168 */
169 uint32_t flag;
170
171 /* Vnode type. Immutable */
172 __enum_uint8(vtype) vtype;
173
174 /* State for clustered writes. Protected by vnode lock */
175 struct vn_clusterw clusterw;
176 };
177
178 /*
179 * This overlays the fid structure (see mount.h). Mostly the same as the types
180 * used by UFS and ext2.
181 */
182 struct fuse_fid {
183 uint16_t len; /* Length of structure. */
184 uint16_t pad; /* Force 32-bit alignment. */
185 uint32_t gen; /* Generation number. */
186 uint64_t nid; /* FUSE node id. */
187 };
188
189 #define VTOFUD(vp) \
190 ((struct fuse_vnode_data *)((vp)->v_data))
191 #define VTOI(vp) (VTOFUD(vp)->nid)
192
193 #define ASSERT_CACHED_ATTRS_LOCKED(vp) \
194 do { \
195 ASSERT_VOP_LOCKED(vp, __func__); \
196 VNASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE || \
197 mtx_owned(&VTOFUD(vp)->cached_attr_mtx), vp, \
198 ("cached attrs not locked")); \
199 } while(0)
200
201 static inline bool
fuse_vnode_attr_cache_valid(struct vnode * vp)202 fuse_vnode_attr_cache_valid(struct vnode *vp)
203 {
204 struct fuse_vnode_data *fvdat = VTOFUD(vp);
205 struct bintime now;
206
207 ASSERT_CACHED_ATTRS_LOCKED(vp);
208
209 getbinuptime(&now);
210 return (bintime_cmp(&fvdat->attr_cache_timeout, &now, >));
211 }
212
213 static inline struct vattr*
VTOVA(struct vnode * vp)214 VTOVA(struct vnode *vp)
215 {
216 ASSERT_CACHED_ATTRS_LOCKED(vp);
217
218 if (fuse_vnode_attr_cache_valid(vp))
219 return &(VTOFUD(vp)->cached_attrs);
220 else
221 return NULL;
222 }
223
224 static inline void
fuse_vnode_clear_attr_cache(struct vnode * vp)225 fuse_vnode_clear_attr_cache(struct vnode *vp)
226 {
227 ASSERT_CACHED_ATTRS_LOCKED(vp);
228
229 bintime_clear(&VTOFUD(vp)->attr_cache_timeout);
230 }
231
232 static uint32_t inline
fuse_vnode_hash(uint64_t id)233 fuse_vnode_hash(uint64_t id)
234 {
235 return (fnv_32_buf(&id, sizeof(id), FNV1_32_INIT));
236 }
237
238 #define VTOILLU(vp) ((uint64_t)(VTOFUD(vp) ? VTOI(vp) : 0))
239
240 #define FUSE_NULL_ID 0
241
242 extern struct vop_vector fuse_fifoops;
243 extern struct vop_vector fuse_vnops;
244
245 int fuse_vnode_cmp(struct vnode *vp, void *nidp);
246
247 static inline void
fuse_vnode_setparent(struct vnode * vp,struct vnode * dvp)248 fuse_vnode_setparent(struct vnode *vp, struct vnode *dvp)
249 {
250 if (dvp != NULL && vp->v_type == VDIR) {
251 ASSERT_VOP_ELOCKED(vp, __func__); /* for parent_nid */
252
253 MPASS(dvp->v_type == VDIR);
254 VTOFUD(vp)->parent_nid = VTOI(dvp);
255 VTOFUD(vp)->flag |= FN_PARENT_NID;
256 } else {
257 ASSERT_CACHED_ATTRS_LOCKED(vp);
258
259 VTOFUD(vp)->flag &= ~FN_PARENT_NID;
260 }
261 }
262
263 int fuse_vnode_size(struct vnode *vp, off_t *filesize, struct ucred *cred,
264 struct thread *td);
265
266 void fuse_vnode_destroy(struct vnode *vp);
267
268 int fuse_vnode_get(struct mount *mp, struct fuse_entry_out *feo,
269 uint64_t nodeid, struct vnode *dvp, struct vnode **vpp,
270 struct componentname *cnp, __enum_uint8(vtype) vtyp);
271
272 void fuse_vnode_open(struct vnode *vp, int32_t fuse_open_flags,
273 struct thread *td);
274
275 int fuse_vnode_savesize(struct vnode *vp, struct ucred *cred, pid_t pid);
276
277 int fuse_vnode_setsize(struct vnode *vp, off_t newsize, bool from_server);
278 int fuse_vnode_setsize_immediate(struct vnode *vp, bool shrink);
279
280 void fuse_vnode_undirty_cached_timestamps(struct vnode *vp, bool atime);
281
282 void fuse_vnode_update(struct vnode *vp, int flags);
283
284 void fuse_node_init(void);
285 void fuse_node_destroy(void);
286 #endif /* _FUSE_NODE_H_ */
287