]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/fuse/fuse_internal.h
fusefs: WIP supporting -o default_permissions
[FreeBSD/FreeBSD.git] / sys / fs / fuse / fuse_internal.h
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 bool
72 vfs_isrdonly(struct mount *mp)
73 {
74         return ((mp->mnt_flag & MNT_RDONLY) != 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 bool
84 vnode_mountedhere(struct vnode *vp)
85 {
86         return (vp->v_mountedhere != NULL);
87 }
88
89 static inline enum vtype
90 vnode_vtype(struct vnode *vp)
91 {
92         return (vp->v_type);
93 }
94
95 static inline bool
96 vnode_isvroot(struct vnode *vp)
97 {
98         return ((vp->v_vflag & VV_ROOT) != 0);
99 }
100
101 static inline bool
102 vnode_isreg(struct vnode *vp)
103 {
104         return (vp->v_type == VREG);
105 }
106
107 static inline bool
108 vnode_isdir(struct vnode *vp)
109 {
110         return (vp->v_type == VDIR);
111 }
112
113 static inline bool
114 vnode_islnk(struct vnode *vp)
115 {
116         return (vp->v_type == VLNK);
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 /* miscellaneous */
138
139 static inline bool
140 fuse_isdeadfs(struct vnode *vp)
141 {
142         struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
143
144         return (data->dataflags & FSESS_DEAD);
145 }
146
147 static inline uint64_t
148 fuse_iosize(struct vnode *vp)
149 {
150         return (vp->v_mount->mnt_stat.f_iosize);
151 }
152
153 /*
154  * Make a cacheable timeout in bintime format value based on a fuse_attr_out
155  * response
156  */
157 static inline void
158 fuse_validity_2_bintime(uint64_t attr_valid, uint32_t attr_valid_nsec,
159         struct bintime *timeout)
160 {
161         struct timespec now, duration, timeout_ts;
162
163         getnanouptime(&now);
164         /* "+ 2" is the bound of attr_valid_nsec + now.tv_nsec */
165         /* Why oh why isn't there a TIME_MAX defined? */
166         if (attr_valid >= INT_MAX || attr_valid + now.tv_sec + 2 >= INT_MAX) {
167                 timeout->sec = INT_MAX;
168         } else {
169                 duration.tv_sec = attr_valid;
170                 duration.tv_nsec = attr_valid_nsec;
171                 timespecadd(&duration, &now, &timeout_ts);
172                 timespec2bintime(&timeout_ts, timeout);
173         }
174 }
175
176 /*
177  * Make a cacheable timeout value in timespec format based on the fuse_entry_out
178  * response
179  */
180 static inline void
181 fuse_validity_2_timespec(const struct fuse_entry_out *feo,
182         struct timespec *timeout)
183 {
184         struct timespec duration, now;
185
186         getnanouptime(&now);
187         /* "+ 2" is the bound of entry_valid_nsec + now.tv_nsec */
188         if (feo->entry_valid >= INT_MAX ||
189             feo->entry_valid + now.tv_sec + 2 >= INT_MAX) {
190                 timeout->tv_sec = INT_MAX;
191         } else {
192                 duration.tv_sec = feo->entry_valid;
193                 duration.tv_nsec = feo->entry_valid_nsec;
194                 timespecadd(&duration, &now, timeout);
195         }
196 }
197
198
199 /* access */
200
201 #define FVP_ACCESS_NOOP         0x01
202
203 #define FACCESS_VA_VALID        0x01
204 /* 
205  * Caller must be the directory's owner, or the superuser, or the sticky bit
206  * must not be set
207  */
208 #define FACCESS_STICKY          0x04
209 /* Caller requires access to change file's owner */
210 #define FACCESS_CHOWN           0x08
211 #define FACCESS_SETGID          0x12
212
213 #define FACCESS_XQUERIES        (FACCESS_STICKY | FACCESS_CHOWN | FACCESS_SETGID)
214
215 struct fuse_access_param {
216         uid_t           xuid;
217         gid_t           xgid;
218         uint32_t        facc_flags;
219 };
220
221 static inline int
222 fuse_match_cred(struct ucred *basecred, struct ucred *usercred)
223 {
224         if (basecred->cr_uid == usercred->cr_uid             &&
225             basecred->cr_uid == usercred->cr_ruid            &&
226             basecred->cr_uid == usercred->cr_svuid           &&
227             basecred->cr_groups[0] == usercred->cr_groups[0] &&
228             basecred->cr_groups[0] == usercred->cr_rgid      &&
229             basecred->cr_groups[0] == usercred->cr_svgid)
230                 return (0);
231
232         return (EPERM);
233 }
234
235 int fuse_internal_access(struct vnode *vp, accmode_t mode,
236     struct fuse_access_param *facp, struct thread *td, struct ucred *cred);
237
238 /* attributes */
239 void fuse_internal_cache_attrs(struct vnode *vp, struct fuse_attr *attr,
240         uint64_t attr_valid, uint32_t attr_valid_nsec, struct vattr *vap);
241
242 /* fsync */
243
244 int fuse_internal_fsync(struct vnode *vp, struct thread *td, int waitfor,
245         bool datasync);
246 int fuse_internal_fsync_callback(struct fuse_ticket *tick, struct uio *uio);
247
248 /* getattr */
249 int fuse_internal_getattr(struct vnode *vp, struct vattr *vap,
250         struct ucred *cred, struct thread *td);
251
252 /* readdir */
253
254 struct pseudo_dirent {
255         uint32_t d_namlen;
256 };
257
258 int fuse_internal_readdir(struct vnode *vp, struct uio *uio,
259     struct fuse_filehandle *fufh, struct fuse_iov *cookediov);
260 int fuse_internal_readdir_processdata(struct uio *uio, size_t reqsize,
261     void *buf, size_t bufsize, void *param);
262
263 /* remove */
264
265 int fuse_internal_remove(struct vnode *dvp, struct vnode *vp,
266     struct componentname *cnp, enum fuse_opcode op);
267
268 /* rename */
269
270 int fuse_internal_rename(struct vnode *fdvp, struct componentname *fcnp,
271     struct vnode *tdvp, struct componentname *tcnp);
272
273 /* revoke */
274
275 void fuse_internal_vnode_disappear(struct vnode *vp);
276
277 /* strategy */
278
279 /* entity creation */
280
281 static inline int
282 fuse_internal_checkentry(struct fuse_entry_out *feo, enum vtype vtyp)
283 {
284         if (vtyp != IFTOVT(feo->attr.mode)) {
285                 return (EINVAL);
286         }
287
288         if (feo->nodeid == FUSE_NULL_ID) {
289                 return (EINVAL);
290         }
291
292         if (feo->nodeid == FUSE_ROOT_ID) {
293                 return (EINVAL);
294         }
295
296         return (0);
297 }
298
299 int fuse_internal_newentry(struct vnode *dvp, struct vnode **vpp,
300     struct componentname *cnp, enum fuse_opcode op, void *buf, size_t bufsize,
301     enum vtype vtyp);
302
303 void fuse_internal_newentry_makerequest(struct mount *mp, uint64_t dnid,
304     struct componentname *cnp, enum fuse_opcode op, void *buf, size_t bufsize,
305     struct fuse_dispatcher *fdip);
306
307 int fuse_internal_newentry_core(struct vnode *dvp, struct vnode **vpp,
308     struct componentname *cnp, enum vtype vtyp, struct fuse_dispatcher *fdip);
309
310 /* entity destruction */
311
312 int fuse_internal_forget_callback(struct fuse_ticket *tick, struct uio *uio);
313 void fuse_internal_forget_send(struct mount *mp, struct thread *td,
314     struct ucred *cred, uint64_t nodeid, uint64_t nlookup);
315
316 /* fuse start/stop */
317
318 int fuse_internal_init_callback(struct fuse_ticket *tick, struct uio *uio);
319 void fuse_internal_send_init(struct fuse_data *data, struct thread *td);
320
321 #endif /* _FUSE_INTERNAL_H_ */