xref: /freebsd/sys/fs/fuse/fuse_internal.h (revision 988a9f7e39e1bea4eed03eb6f69df4cb0ec9289a)
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  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  *
57  * $FreeBSD$
58  */
59 
60 #ifndef _FUSE_INTERNAL_H_
61 #define _FUSE_INTERNAL_H_
62 
63 #include <sys/types.h>
64 #include <sys/uio.h>
65 #include <sys/stat.h>
66 #include <sys/vnode.h>
67 
68 #include "fuse_ipc.h"
69 #include "fuse_node.h"
70 
71 static __inline int
72 vfs_isrdonly(struct mount *mp)
73 {
74     return ((mp->mnt_flag & MNT_RDONLY) != 0 ? 1 : 0);
75 }
76 
77 static __inline struct mount *
78 vnode_mount(struct vnode *vp)
79 {
80 	return (vp->v_mount);
81 }
82 
83 static __inline int
84 vnode_mountedhere(struct vnode *vp)
85 {
86 	return (vp->v_mountedhere != NULL ? 1 : 0);
87 }
88 
89 static __inline enum vtype
90 vnode_vtype(struct vnode *vp)
91 {
92     return (vp->v_type);
93 }
94 
95 static __inline int
96 vnode_isvroot(struct vnode *vp)
97 {
98     return ((vp->v_vflag & VV_ROOT) != 0 ? 1 : 0);
99 }
100 
101 static __inline int
102 vnode_isreg(struct vnode *vp)
103 {
104     return (vp->v_type == VREG ? 1 : 0);
105 }
106 
107 static __inline int
108 vnode_isdir(struct vnode *vp)
109 {
110     return (vp->v_type == VDIR ? 1 : 0);
111 }
112 
113 static __inline int
114 vnode_islnk(struct vnode *vp)
115 {
116     return (vp->v_type == VLNK ? 1 : 0);
117 }
118 
119 static __inline ssize_t
120 uio_resid(struct uio *uio)
121 {
122     return (uio->uio_resid);
123 }
124 
125 static __inline off_t
126 uio_offset(struct uio *uio)
127 {
128     return (uio->uio_offset);
129 }
130 
131 static __inline void
132 uio_setoffset(struct uio *uio, off_t offset)
133 {
134     uio->uio_offset = offset;
135 }
136 
137 static __inline void
138 uio_setresid(struct uio *uio, ssize_t resid)
139 {
140     uio->uio_resid = resid;
141 }
142 
143 /* miscellaneous */
144 
145 static __inline__
146 int
147 fuse_isdeadfs(struct vnode *vp)
148 {
149     struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
150 
151     return (data->dataflags & FSESS_DEAD);
152 }
153 
154 static __inline__
155 int
156 fuse_iosize(struct vnode *vp)
157 {
158     return vp->v_mount->mnt_stat.f_iosize;
159 }
160 
161 /* access */
162 
163 #define FVP_ACCESS_NOOP   0x01
164 
165 #define FACCESS_VA_VALID   0x01
166 #define FACCESS_DO_ACCESS  0x02
167 #define FACCESS_STICKY     0x04
168 #define FACCESS_CHOWN      0x08
169 #define FACCESS_NOCHECKSPY 0x10
170 #define FACCESS_SETGID     0x12
171 
172 #define FACCESS_XQUERIES FACCESS_STICKY | FACCESS_CHOWN | FACCESS_SETGID
173 
174 struct fuse_access_param {
175     uid_t    xuid;
176     gid_t    xgid;
177     uint32_t facc_flags;
178 };
179 
180 static __inline int
181 fuse_match_cred(struct ucred *basecred, struct ucred *usercred)
182 {
183 	if (basecred->cr_uid == usercred->cr_uid             &&
184 	    basecred->cr_uid == usercred->cr_ruid            &&
185 	    basecred->cr_uid == usercred->cr_svuid           &&
186 	    basecred->cr_groups[0] == usercred->cr_groups[0] &&
187 	    basecred->cr_groups[0] == usercred->cr_rgid      &&
188 	    basecred->cr_groups[0] == usercred->cr_svgid)
189 		return 0;
190 
191 	return EPERM;
192 }
193 
194 int
195 fuse_internal_access(struct vnode *vp,
196                      mode_t mode,
197                      struct fuse_access_param *facp,
198                      struct thread *td,
199                      struct ucred *cred);
200 
201 /* attributes */
202 
203 /*
204  * Cache FUSE attributes 'fat', with nominal expiration
205  * 'attr_valid'.'attr_valid_nsec', in attr cache associated with vnode 'vp'.
206  * Optionally, if argument 'vap' is not NULL, store a copy of the converted
207  * attributes there as well.
208  *
209  * If the nominal attribute cache TTL is zero, do not cache on the 'vp' (but do
210  * return the result to the caller).
211  */
212 static __inline
213 void
214 fuse_internal_attr_fat2vat(struct vnode *vp,
215                            struct fuse_attr *fat,
216                            uint64_t attr_valid,
217                            uint32_t attr_valid_nsec,
218                            struct vattr *vap)
219 {
220     struct mount *mp;
221     struct fuse_vnode_data *fvdat;
222     struct vattr *vp_cache_at;
223 
224     mp = vnode_mount(vp);
225     fvdat = VTOFUD(vp);
226 
227     DEBUGX(FUSE_DEBUG_INTERNAL,
228         "node #%ju, mode 0%o\n", (uintmax_t)fat->ino, fat->mode);
229 
230     /* Honor explicit do-not-cache requests from user filesystems. */
231     if (attr_valid == 0 && attr_valid_nsec == 0)
232         fvdat->valid_attr_cache = false;
233     else
234         fvdat->valid_attr_cache = true;
235 
236     vp_cache_at = VTOVA(vp);
237 
238     if (vap == NULL && vp_cache_at == NULL)
239         return;
240 
241     if (vap == NULL)
242         vap = vp_cache_at;
243 
244     vattr_null(vap);
245 
246     vap->va_fsid = mp->mnt_stat.f_fsid.val[0];
247     vap->va_fileid = fat->ino;
248     vap->va_mode = fat->mode & ~S_IFMT;
249     vap->va_nlink     = fat->nlink;
250     vap->va_uid       = fat->uid;
251     vap->va_gid       = fat->gid;
252     vap->va_rdev      = fat->rdev;
253     vap->va_size      = fat->size;
254     vap->va_atime.tv_sec  = fat->atime; /* XXX on some platforms cast from 64 bits to 32 */
255     vap->va_atime.tv_nsec = fat->atimensec;
256     vap->va_mtime.tv_sec  = fat->mtime;
257     vap->va_mtime.tv_nsec = fat->mtimensec;
258     vap->va_ctime.tv_sec  = fat->ctime;
259     vap->va_ctime.tv_nsec = fat->ctimensec;
260     vap->va_blocksize = PAGE_SIZE;
261     vap->va_type = IFTOVT(fat->mode);
262     vap->va_bytes = fat->blocks * S_BLKSIZE;
263     vap->va_flags = 0;
264 
265     if (vap != vp_cache_at && vp_cache_at != NULL)
266         memcpy(vp_cache_at, vap, sizeof(*vap));
267 }
268 
269 
270 #define	cache_attrs(vp, fuse_out, vap_out)				\
271         fuse_internal_attr_fat2vat((vp), &(fuse_out)->attr,		\
272             (fuse_out)->attr_valid, (fuse_out)->attr_valid_nsec, (vap_out))
273 
274 /* fsync */
275 
276 int
277 fuse_internal_fsync(struct vnode           *vp,
278                     struct thread          *td,
279                     struct ucred           *cred,
280                     struct fuse_filehandle *fufh);
281 
282 int
283 fuse_internal_fsync_callback(struct fuse_ticket *tick, struct uio *uio);
284 
285 /* readdir */
286 
287 struct pseudo_dirent {
288     uint32_t d_namlen;
289 };
290 
291 int
292 fuse_internal_readdir(struct vnode           *vp,
293                       struct uio             *uio,
294                       struct fuse_filehandle *fufh,
295                       struct fuse_iov        *cookediov);
296 
297 int
298 fuse_internal_readdir_processdata(struct uio *uio,
299                                   size_t reqsize,
300                                   void *buf,
301                                   size_t bufsize,
302                                   void *param);
303 
304 /* remove */
305 
306 int
307 fuse_internal_remove(struct vnode *dvp,
308                      struct vnode *vp,
309                      struct componentname *cnp,
310                      enum fuse_opcode op);
311 
312 /* rename */
313 
314 int
315 fuse_internal_rename(struct vnode *fdvp,
316                      struct componentname *fcnp,
317                      struct vnode *tdvp,
318                      struct componentname *tcnp);
319 /* revoke */
320 
321 void
322 fuse_internal_vnode_disappear(struct vnode *vp);
323 
324 /* strategy */
325 
326 /* entity creation */
327 
328 static __inline
329 int
330 fuse_internal_checkentry(struct fuse_entry_out *feo, enum vtype vtyp)
331 {
332     DEBUGX(FUSE_DEBUG_INTERNAL,
333         "feo=%p, vtype=%d\n", feo, vtyp);
334 
335     if (vtyp != IFTOVT(feo->attr.mode)) {
336         DEBUGX(FUSE_DEBUG_INTERNAL,
337             "EINVAL -- %x != %x\n", vtyp, IFTOVT(feo->attr.mode));
338         return EINVAL;
339     }
340 
341     if (feo->nodeid == FUSE_NULL_ID) {
342         DEBUGX(FUSE_DEBUG_INTERNAL,
343             "EINVAL -- feo->nodeid is NULL\n");
344         return EINVAL;
345     }
346 
347     if (feo->nodeid == FUSE_ROOT_ID) {
348         DEBUGX(FUSE_DEBUG_INTERNAL,
349             "EINVAL -- feo->nodeid is FUSE_ROOT_ID\n");
350         return EINVAL;
351     }
352 
353     return 0;
354 }
355 
356 int
357 fuse_internal_newentry(struct vnode *dvp,
358                        struct vnode **vpp,
359                        struct componentname *cnp,
360                        enum fuse_opcode op,
361                        void *buf,
362                        size_t bufsize,
363                        enum vtype vtyp);
364 
365 void
366 fuse_internal_newentry_makerequest(struct mount *mp,
367                                    uint64_t dnid,
368                                    struct componentname *cnp,
369                                    enum fuse_opcode op,
370                                    void *buf,
371                                    size_t bufsize,
372                                    struct fuse_dispatcher *fdip);
373 
374 int
375 fuse_internal_newentry_core(struct vnode *dvp,
376                             struct vnode **vpp,
377                             struct componentname   *cnp,
378                             enum vtype vtyp,
379                             struct fuse_dispatcher *fdip);
380 
381 /* entity destruction */
382 
383 int
384 fuse_internal_forget_callback(struct fuse_ticket *tick, struct uio *uio);
385 
386 void
387 fuse_internal_forget_send(struct mount *mp,
388                           struct thread *td,
389                           struct ucred *cred,
390                           uint64_t nodeid,
391                           uint64_t nlookup);
392 
393 /* fuse start/stop */
394 
395 int fuse_internal_init_callback(struct fuse_ticket *tick, struct uio *uio);
396 void fuse_internal_send_init(struct fuse_data *data, struct thread *td);
397 
398 #endif /* _FUSE_INTERNAL_H_ */
399