]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/fuse/fuse_file.c
Update mandoc to 1.14.5
[FreeBSD/FreeBSD.git] / sys / fs / fuse / fuse_file.c
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
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
60
61 #include <sys/param.h>
62 #include <sys/module.h>
63 #include <sys/systm.h>
64 #include <sys/errno.h>
65 #include <sys/kernel.h>
66 #include <sys/conf.h>
67 #include <sys/uio.h>
68 #include <sys/malloc.h>
69 #include <sys/queue.h>
70 #include <sys/lock.h>
71 #include <sys/sx.h>
72 #include <sys/mutex.h>
73 #include <sys/proc.h>
74 #include <sys/mount.h>
75 #include <sys/vnode.h>
76 #include <sys/sdt.h>
77 #include <sys/sysctl.h>
78
79 #include "fuse.h"
80 #include "fuse_file.h"
81 #include "fuse_internal.h"
82 #include "fuse_ipc.h"
83 #include "fuse_node.h"
84
85 SDT_PROVIDER_DECLARE(fuse);
86 /* 
87  * Fuse trace probe:
88  * arg0: verbosity.  Higher numbers give more verbose messages
89  * arg1: Textual message
90  */
91 SDT_PROBE_DEFINE2(fuse, , file, trace, "int", "char*");
92
93 static int fuse_fh_count = 0;
94
95 SYSCTL_INT(_vfs_fusefs, OID_AUTO, filehandle_count, CTLFLAG_RD,
96     &fuse_fh_count, 0, "number of open FUSE filehandles");
97
98 int
99 fuse_filehandle_open(struct vnode *vp, fufh_type_t fufh_type,
100     struct fuse_filehandle **fufhp, struct thread *td, struct ucred *cred)
101 {
102         struct fuse_dispatcher fdi;
103         struct fuse_open_in *foi;
104         struct fuse_open_out *foo;
105
106         int err = 0;
107         int oflags = 0;
108         int op = FUSE_OPEN;
109
110         if (fuse_filehandle_valid(vp, fufh_type)) {
111                 panic("FUSE: filehandle_open called despite valid fufh (type=%d)",
112                     fufh_type);
113                 /* NOTREACHED */
114         }
115         /*
116          * Note that this means we are effectively FILTERING OUT open() flags.
117          */
118         oflags = fuse_filehandle_xlate_to_oflags(fufh_type);
119
120         if (vnode_isdir(vp)) {
121                 op = FUSE_OPENDIR;
122                 if (fufh_type != FUFH_RDONLY) {
123                         SDT_PROBE2(fuse, , file, trace, 1,
124                                 "non-rdonly fh requested for a directory?");
125                         printf("FUSE:non-rdonly fh requested for a directory?\n");
126                         fufh_type = FUFH_RDONLY;
127                 }
128         }
129         fdisp_init(&fdi, sizeof(*foi));
130         fdisp_make_vp(&fdi, op, vp, td, cred);
131
132         foi = fdi.indata;
133         foi->flags = oflags;
134
135         if ((err = fdisp_wait_answ(&fdi))) {
136                 SDT_PROBE2(fuse, , file, trace, 1,
137                         "OUCH ... daemon didn't give fh");
138                 if (err == ENOENT) {
139                         fuse_internal_vnode_disappear(vp);
140                 }
141                 goto out;
142         }
143         foo = fdi.answ;
144
145         fuse_filehandle_init(vp, fufh_type, fufhp, foo->fh);
146
147         /*
148          * For WRONLY opens, force DIRECT_IO.  This is necessary
149          * since writing a partial block through the buffer cache
150          * will result in a read of the block and that read won't
151          * be allowed by the WRONLY open.
152          */
153         if (fufh_type == FUFH_WRONLY)
154                 fuse_vnode_open(vp, foo->open_flags | FOPEN_DIRECT_IO, td);
155         else
156                 fuse_vnode_open(vp, foo->open_flags, td);
157
158 out:
159         fdisp_destroy(&fdi);
160         return err;
161 }
162
163 int
164 fuse_filehandle_close(struct vnode *vp, fufh_type_t fufh_type,
165     struct thread *td, struct ucred *cred)
166 {
167         struct fuse_dispatcher fdi;
168         struct fuse_release_in *fri;
169         struct fuse_vnode_data *fvdat = VTOFUD(vp);
170         struct fuse_filehandle *fufh = NULL;
171
172         int err = 0;
173         int op = FUSE_RELEASE;
174
175         fufh = &(fvdat->fufh[fufh_type]);
176         if (!FUFH_IS_VALID(fufh)) {
177                 panic("FUSE: filehandle_put called on invalid fufh (type=%d)",
178                     fufh_type);
179                 /* NOTREACHED */
180         }
181         if (fuse_isdeadfs(vp)) {
182                 goto out;
183         }
184         if (vnode_isdir(vp))
185                 op = FUSE_RELEASEDIR;
186         fdisp_init(&fdi, sizeof(*fri));
187         fdisp_make_vp(&fdi, op, vp, td, cred);
188         fri = fdi.indata;
189         fri->fh = fufh->fh_id;
190         fri->flags = fuse_filehandle_xlate_to_oflags(fufh_type);
191
192         err = fdisp_wait_answ(&fdi);
193         fdisp_destroy(&fdi);
194
195 out:
196         atomic_subtract_acq_int(&fuse_fh_count, 1);
197         fufh->fh_id = (uint64_t)-1;
198         fufh->fh_type = FUFH_INVALID;
199
200         return err;
201 }
202
203 int
204 fuse_filehandle_valid(struct vnode *vp, fufh_type_t fufh_type)
205 {
206         struct fuse_vnode_data *fvdat = VTOFUD(vp);
207         struct fuse_filehandle *fufh;
208
209         fufh = &(fvdat->fufh[fufh_type]);
210         return FUFH_IS_VALID(fufh);
211 }
212
213 /*
214  * Check for a valid file handle, first the type requested, but if that
215  * isn't valid, try for FUFH_RDWR.
216  * Return the FUFH type that is valid or FUFH_INVALID if there are none.
217  * This is a variant of fuse_filehandle_vaild() analogous to
218  * fuse_filehandle_getrw().
219  */
220 fufh_type_t
221 fuse_filehandle_validrw(struct vnode *vp, fufh_type_t fufh_type)
222 {
223         struct fuse_vnode_data *fvdat = VTOFUD(vp);
224         struct fuse_filehandle *fufh;
225
226         fufh = &fvdat->fufh[fufh_type];
227         if (FUFH_IS_VALID(fufh) != 0)
228                 return (fufh_type);
229         fufh = &fvdat->fufh[FUFH_RDWR];
230         if (FUFH_IS_VALID(fufh) != 0)
231                 return (FUFH_RDWR);
232         return (FUFH_INVALID);
233 }
234
235 int
236 fuse_filehandle_get(struct vnode *vp, fufh_type_t fufh_type,
237     struct fuse_filehandle **fufhp)
238 {
239         struct fuse_vnode_data *fvdat = VTOFUD(vp);
240         struct fuse_filehandle *fufh;
241
242         fufh = &(fvdat->fufh[fufh_type]);
243         if (!FUFH_IS_VALID(fufh))
244                 return EBADF;
245         if (fufhp != NULL)
246                 *fufhp = fufh;
247         return 0;
248 }
249
250 int
251 fuse_filehandle_getrw(struct vnode *vp, fufh_type_t fufh_type,
252     struct fuse_filehandle **fufhp)
253 {
254         struct fuse_vnode_data *fvdat = VTOFUD(vp);
255         struct fuse_filehandle *fufh;
256
257         fufh = &(fvdat->fufh[fufh_type]);
258         if (!FUFH_IS_VALID(fufh)) {
259                 fufh_type = FUFH_RDWR;
260         }
261         return fuse_filehandle_get(vp, fufh_type, fufhp);
262 }
263
264 void
265 fuse_filehandle_init(struct vnode *vp, fufh_type_t fufh_type,
266     struct fuse_filehandle **fufhp, uint64_t fh_id)
267 {
268         struct fuse_vnode_data *fvdat = VTOFUD(vp);
269         struct fuse_filehandle *fufh;
270
271         fufh = &(fvdat->fufh[fufh_type]);
272         MPASS(!FUFH_IS_VALID(fufh));
273         fufh->fh_id = fh_id;
274         fufh->fh_type = fufh_type;
275         if (!FUFH_IS_VALID(fufh)) {
276                 panic("FUSE: init: invalid filehandle id (type=%d)", fufh_type);
277         }
278         if (fufhp != NULL)
279                 *fufhp = fufh;
280
281         atomic_add_acq_int(&fuse_fh_count, 1);
282 }