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  * $FreeBSD: stable/12/sys/fs/fuse/fuse_internal.h 370910 2021-10-17 18:49:47Z asomers $
63  */
64 
65 #ifndef _FUSE_INTERNAL_H_
66 #define _FUSE_INTERNAL_H_
67 
68 #include <sys/types.h>
69 #include <sys/counter.h>
70 #include <sys/limits.h>
71 #include <sys/uio.h>
72 #include <sys/stat.h>
73 #include <sys/vnode.h>
74 
75 #include "fuse_ipc.h"
76 #include "fuse_node.h"
77 
78 extern counter_u64_t fuse_lookup_cache_hits;
79 extern counter_u64_t fuse_lookup_cache_misses;
80 
81 static inline bool
vfs_isrdonly(struct mount * mp)82 vfs_isrdonly(struct mount *mp)
83 {
84 	return ((mp->mnt_flag & MNT_RDONLY) != 0);
85 }
86 
87 static inline struct mount *
vnode_mount(struct vnode * vp)88 vnode_mount(struct vnode *vp)
89 {
90 	return (vp->v_mount);
91 }
92 
93 static inline enum vtype
vnode_vtype(struct vnode * vp)94 vnode_vtype(struct vnode *vp)
95 {
96 	return (vp->v_type);
97 }
98 
99 static inline bool
vnode_isvroot(struct vnode * vp)100 vnode_isvroot(struct vnode *vp)
101 {
102 	return ((vp->v_vflag & VV_ROOT) != 0);
103 }
104 
105 static inline bool
vnode_isreg(struct vnode * vp)106 vnode_isreg(struct vnode *vp)
107 {
108 	return (vp->v_type == VREG);
109 }
110 
111 static inline bool
vnode_isdir(struct vnode * vp)112 vnode_isdir(struct vnode *vp)
113 {
114 	return (vp->v_type == VDIR);
115 }
116 
117 static inline bool
vnode_islnk(struct vnode * vp)118 vnode_islnk(struct vnode *vp)
119 {
120 	return (vp->v_type == VLNK);
121 }
122 
123 static inline ssize_t
uio_resid(struct uio * uio)124 uio_resid(struct uio *uio)
125 {
126 	return (uio->uio_resid);
127 }
128 
129 static inline off_t
uio_offset(struct uio * uio)130 uio_offset(struct uio *uio)
131 {
132 	return (uio->uio_offset);
133 }
134 
135 static inline void
uio_setoffset(struct uio * uio,off_t offset)136 uio_setoffset(struct uio *uio, off_t offset)
137 {
138 	uio->uio_offset = offset;
139 }
140 
141 /* miscellaneous */
142 
143 static inline bool
fuse_isdeadfs(struct vnode * vp)144 fuse_isdeadfs(struct vnode *vp)
145 {
146 	struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
147 
148 	return (data->dataflags & FSESS_DEAD);
149 }
150 
151 static inline uint64_t
fuse_iosize(struct vnode * vp)152 fuse_iosize(struct vnode *vp)
153 {
154 	return (vp->v_mount->mnt_stat.f_iosize);
155 }
156 
157 /*
158  * Make a cacheable timeout in bintime format value based on a fuse_attr_out
159  * response
160  */
161 static inline void
fuse_validity_2_bintime(uint64_t attr_valid,uint32_t attr_valid_nsec,struct bintime * timeout)162 fuse_validity_2_bintime(uint64_t attr_valid, uint32_t attr_valid_nsec,
163 	struct bintime *timeout)
164 {
165 	struct timespec now, duration, timeout_ts;
166 
167 	getnanouptime(&now);
168 	/* "+ 2" is the bound of attr_valid_nsec + now.tv_nsec */
169 	/* Why oh why isn't there a TIME_MAX defined? */
170 	if (attr_valid >= INT_MAX || attr_valid + now.tv_sec + 2 >= INT_MAX) {
171 		timeout->sec = INT_MAX;
172 	} else {
173 		duration.tv_sec = attr_valid;
174 		duration.tv_nsec = attr_valid_nsec;
175 		timespecadd(&duration, &now, &timeout_ts);
176 		timespec2bintime(&timeout_ts, timeout);
177 	}
178 }
179 
180 /*
181  * Make a cacheable timeout value in timespec format based on the fuse_entry_out
182  * response
183  */
184 static inline void
fuse_validity_2_timespec(const struct fuse_entry_out * feo,struct timespec * timeout)185 fuse_validity_2_timespec(const struct fuse_entry_out *feo,
186 	struct timespec *timeout)
187 {
188 	struct timespec duration, now;
189 
190 	getnanouptime(&now);
191 	/* "+ 2" is the bound of entry_valid_nsec + now.tv_nsec */
192 	if (feo->entry_valid >= INT_MAX ||
193 	    feo->entry_valid + now.tv_sec + 2 >= INT_MAX) {
194 		timeout->tv_sec = INT_MAX;
195 	} else {
196 		duration.tv_sec = feo->entry_valid;
197 		duration.tv_nsec = feo->entry_valid_nsec;
198 		timespecadd(&duration, &now, timeout);
199 	}
200 }
201 
202 
203 /* VFS ops */
204 int
205 fuse_internal_get_cached_vnode(struct mount*, ino_t, int, struct vnode**);
206 
207 /* access */
208 static inline int
fuse_match_cred(struct ucred * basecred,struct ucred * usercred)209 fuse_match_cred(struct ucred *basecred, struct ucred *usercred)
210 {
211 	if (basecred->cr_uid == usercred->cr_uid             &&
212 	    basecred->cr_uid == usercred->cr_ruid            &&
213 	    basecred->cr_uid == usercred->cr_svuid           &&
214 	    basecred->cr_groups[0] == usercred->cr_groups[0] &&
215 	    basecred->cr_groups[0] == usercred->cr_rgid      &&
216 	    basecred->cr_groups[0] == usercred->cr_svgid)
217 		return (0);
218 
219 	return (EPERM);
220 }
221 
222 int fuse_internal_access(struct vnode *vp, accmode_t mode,
223     struct thread *td, struct ucred *cred);
224 
225 /* attributes */
226 void fuse_internal_cache_attrs(struct vnode *vp, struct fuse_attr *attr,
227 	uint64_t attr_valid, uint32_t attr_valid_nsec, struct vattr *vap,
228 	bool from_server);
229 
230 /* fsync */
231 
232 int fuse_internal_fsync(struct vnode *vp, struct thread *td, int waitfor,
233 	bool datasync);
234 int fuse_internal_fsync_callback(struct fuse_ticket *tick, struct uio *uio);
235 
236 /* getattr */
237 int fuse_internal_do_getattr(struct vnode *vp, struct vattr *vap,
238 	struct ucred *cred, struct thread *td);
239 int fuse_internal_getattr(struct vnode *vp, struct vattr *vap,
240 	struct ucred *cred, struct thread *td);
241 
242 /* asynchronous invalidation */
243 int fuse_internal_invalidate_entry(struct mount *mp, struct uio *uio);
244 int fuse_internal_invalidate_inode(struct mount *mp, struct uio *uio);
245 
246 /* mknod */
247 int fuse_internal_mknod(struct vnode *dvp, struct vnode **vpp,
248 	struct componentname *cnp, struct vattr *vap);
249 
250 /* readdir */
251 struct pseudo_dirent {
252 	uint32_t d_namlen;
253 };
254 int fuse_internal_readdir(struct vnode *vp, struct uio *uio, off_t startoff,
255     struct fuse_filehandle *fufh, struct fuse_iov *cookediov, int *ncookies,
256     u_long *cookies);
257 int fuse_internal_readdir_processdata(struct uio *uio, off_t startoff,
258     int *fnd_start, size_t reqsize, void *buf, size_t bufsize,
259     struct fuse_iov *cookediov, int *ncookies, u_long **cookiesp);
260 
261 /* remove */
262 
263 int fuse_internal_remove(struct vnode *dvp, struct vnode *vp,
264     struct componentname *cnp, enum fuse_opcode op);
265 
266 /* rename */
267 
268 int fuse_internal_rename(struct vnode *fdvp, struct componentname *fcnp,
269     struct vnode *tdvp, struct componentname *tcnp);
270 
271 /* revoke */
272 
273 void fuse_internal_vnode_disappear(struct vnode *vp);
274 
275 /* setattr */
276 int fuse_internal_setattr(struct vnode *vp, struct vattr *va,
277 	struct thread *td, struct ucred *cred);
278 
279 /* strategy */
280 
281 /* entity creation */
282 
283 static inline int
fuse_internal_checkentry(struct fuse_entry_out * feo,enum vtype vtyp)284 fuse_internal_checkentry(struct fuse_entry_out *feo, enum vtype vtyp)
285 {
286 	if (vtyp != IFTOVT(feo->attr.mode)) {
287 		return (EINVAL);
288 	}
289 
290 	if (feo->nodeid == FUSE_NULL_ID) {
291 		return (EINVAL);
292 	}
293 
294 	if (feo->nodeid == FUSE_ROOT_ID) {
295 		return (EINVAL);
296 	}
297 
298 	return (0);
299 }
300 
301 int fuse_internal_newentry(struct vnode *dvp, struct vnode **vpp,
302     struct componentname *cnp, enum fuse_opcode op, void *buf, size_t bufsize,
303     enum vtype vtyp);
304 
305 void fuse_internal_newentry_makerequest(struct mount *mp, uint64_t dnid,
306     struct componentname *cnp, enum fuse_opcode op, void *buf, size_t bufsize,
307     struct fuse_dispatcher *fdip);
308 
309 int fuse_internal_newentry_core(struct vnode *dvp, struct vnode **vpp,
310     struct componentname *cnp, enum vtype vtyp, struct fuse_dispatcher *fdip);
311 
312 /* entity destruction */
313 
314 int fuse_internal_forget_callback(struct fuse_ticket *tick, struct uio *uio);
315 void fuse_internal_forget_send(struct mount *mp, struct thread *td,
316     struct ucred *cred, uint64_t nodeid, uint64_t nlookup);
317 
318 /* fuse start/stop */
319 
320 int fuse_internal_init_callback(struct fuse_ticket *tick, struct uio *uio);
321 void fuse_internal_send_init(struct fuse_data *data, struct thread *td);
322 
323 /* module load/unload */
324 void fuse_internal_init(void);
325 void fuse_internal_destroy(void);
326 
327 #endif /* _FUSE_INTERNAL_H_ */
328