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