]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/fuse/fuse_node.c
fusefs: cache file attributes
[FreeBSD/FreeBSD.git] / sys / fs / fuse / fuse_node.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/vnode.h>
76 #include <sys/namei.h>
77 #include <sys/mount.h>
78 #include <sys/sysctl.h>
79 #include <sys/fcntl.h>
80 #include <sys/fnv_hash.h>
81 #include <sys/priv.h>
82 #include <sys/buf.h>
83 #include <security/mac/mac_framework.h>
84 #include <vm/vm.h>
85 #include <vm/vm_extern.h>
86
87 #include "fuse.h"
88 #include "fuse_node.h"
89 #include "fuse_internal.h"
90 #include "fuse_io.h"
91 #include "fuse_ipc.h"
92
93 SDT_PROVIDER_DECLARE(fuse);
94 /* 
95  * Fuse trace probe:
96  * arg0: verbosity.  Higher numbers give more verbose messages
97  * arg1: Textual message
98  */
99 SDT_PROBE_DEFINE2(fuse, , node, trace, "int", "char*");
100
101 MALLOC_DEFINE(M_FUSEVN, "fuse_vnode", "fuse vnode private data");
102
103 static int sysctl_fuse_cache_mode(SYSCTL_HANDLER_ARGS);
104
105 static int fuse_node_count = 0;
106
107 SYSCTL_INT(_vfs_fusefs, OID_AUTO, node_count, CTLFLAG_RD,
108     &fuse_node_count, 0, "Count of FUSE vnodes");
109
110 int     fuse_data_cache_mode = FUSE_CACHE_WT;
111
112 SYSCTL_PROC(_vfs_fusefs, OID_AUTO, data_cache_mode, CTLTYPE_INT|CTLFLAG_RW,
113     &fuse_data_cache_mode, 0, sysctl_fuse_cache_mode, "I",
114     "Zero: disable caching of FUSE file data; One: write-through caching "
115     "(default); Two: write-back caching (generally unsafe)");
116
117 int     fuse_data_cache_invalidate = 0;
118
119 SYSCTL_INT(_vfs_fusefs, OID_AUTO, data_cache_invalidate, CTLFLAG_RW,
120     &fuse_data_cache_invalidate, 0,
121     "If non-zero, discard cached clean file data when there are no active file"
122     " users");
123
124 int     fuse_mmap_enable = 1;
125
126 SYSCTL_INT(_vfs_fusefs, OID_AUTO, mmap_enable, CTLFLAG_RW,
127     &fuse_mmap_enable, 0,
128     "If non-zero, and data_cache_mode is also non-zero, enable mmap(2) of "
129     "FUSE files");
130
131 int     fuse_refresh_size = 0;
132
133 SYSCTL_INT(_vfs_fusefs, OID_AUTO, refresh_size, CTLFLAG_RW,
134     &fuse_refresh_size, 0,
135     "If non-zero, and no dirty file extension data is buffered, fetch file "
136     "size before write operations");
137
138 int     fuse_sync_resize = 1;
139
140 SYSCTL_INT(_vfs_fusefs, OID_AUTO, sync_resize, CTLFLAG_RW,
141     &fuse_sync_resize, 0,
142     "If a cached write extended a file, inform FUSE filesystem of the changed"
143     "size immediately subsequent to the issued writes");
144
145 int     fuse_fix_broken_io = 0;
146
147 SYSCTL_INT(_vfs_fusefs, OID_AUTO, fix_broken_io, CTLFLAG_RW,
148     &fuse_fix_broken_io, 0,
149     "If non-zero, print a diagnostic warning if a userspace filesystem returns"
150     " EIO on reads of recently extended portions of files");
151
152 static int
153 sysctl_fuse_cache_mode(SYSCTL_HANDLER_ARGS)
154 {
155         int val, error;
156
157         val = *(int *)arg1;
158         error = sysctl_handle_int(oidp, &val, 0, req);
159         if (error || !req->newptr)
160                 return (error);
161
162         switch (val) {
163         case FUSE_CACHE_UC:
164         case FUSE_CACHE_WT:
165         case FUSE_CACHE_WB:
166                 *(int *)arg1 = val;
167                 break;
168         default:
169                 return (EDOM);
170         }
171         return (0);
172 }
173
174 static void
175 fuse_vnode_init(struct vnode *vp, struct fuse_vnode_data *fvdat,
176     uint64_t nodeid, enum vtype vtyp)
177 {
178         fvdat->nid = nodeid;
179         LIST_INIT(&fvdat->handles);
180         vattr_null(&fvdat->cached_attrs);
181         if (nodeid == FUSE_ROOT_ID) {
182                 vp->v_vflag |= VV_ROOT;
183         }
184         vp->v_type = vtyp;
185         vp->v_data = fvdat;
186
187         atomic_add_acq_int(&fuse_node_count, 1);
188 }
189
190 void
191 fuse_vnode_destroy(struct vnode *vp)
192 {
193         struct fuse_vnode_data *fvdat = vp->v_data;
194
195         vp->v_data = NULL;
196         KASSERT(LIST_EMPTY(&fvdat->handles),
197                 ("Destroying fuse vnode with open files!"));
198         free(fvdat, M_FUSEVN);
199
200         atomic_subtract_acq_int(&fuse_node_count, 1);
201 }
202
203 static int
204 fuse_vnode_cmp(struct vnode *vp, void *nidp)
205 {
206         return (VTOI(vp) != *((uint64_t *)nidp));
207 }
208
209 static uint32_t inline
210 fuse_vnode_hash(uint64_t id)
211 {
212         return (fnv_32_buf(&id, sizeof(id), FNV1_32_INIT));
213 }
214
215 static int
216 fuse_vnode_alloc(struct mount *mp,
217     struct thread *td,
218     uint64_t nodeid,
219     enum vtype vtyp,
220     struct vnode **vpp)
221 {
222         struct fuse_vnode_data *fvdat;
223         struct vnode *vp2;
224         int err = 0;
225
226         if (vtyp == VNON) {
227                 return EINVAL;
228         }
229         *vpp = NULL;
230         err = vfs_hash_get(mp, fuse_vnode_hash(nodeid), LK_EXCLUSIVE, td, vpp,
231             fuse_vnode_cmp, &nodeid);
232         if (err)
233                 return (err);
234
235         if (*vpp) {
236                 MPASS((*vpp)->v_type == vtyp && (*vpp)->v_data != NULL);
237                 SDT_PROBE2(fuse, , node, trace, 1, "vnode taken from hash");
238                 return (0);
239         }
240         fvdat = malloc(sizeof(*fvdat), M_FUSEVN, M_WAITOK | M_ZERO);
241         err = getnewvnode("fuse", mp, &fuse_vnops, vpp);
242         if (err) {
243                 free(fvdat, M_FUSEVN);
244                 return (err);
245         }
246         lockmgr((*vpp)->v_vnlock, LK_EXCLUSIVE, NULL);
247         fuse_vnode_init(*vpp, fvdat, nodeid, vtyp);
248         err = insmntque(*vpp, mp);
249         ASSERT_VOP_ELOCKED(*vpp, "fuse_vnode_alloc");
250         if (err) {
251                 free(fvdat, M_FUSEVN);
252                 *vpp = NULL;
253                 return (err);
254         }
255         err = vfs_hash_insert(*vpp, fuse_vnode_hash(nodeid), LK_EXCLUSIVE,
256             td, &vp2, fuse_vnode_cmp, &nodeid);
257         if (err)
258                 return (err);
259         if (vp2 != NULL) {
260                 *vpp = vp2;
261                 return (0);
262         }
263
264         ASSERT_VOP_ELOCKED(*vpp, "fuse_vnode_alloc");
265
266         return (0);
267 }
268
269 int
270 fuse_vnode_get(struct mount *mp,
271     struct fuse_entry_out *feo,
272     uint64_t nodeid,
273     struct vnode *dvp,
274     struct vnode **vpp,
275     struct componentname *cnp,
276     enum vtype vtyp)
277 {
278         struct thread *td = (cnp != NULL ? cnp->cn_thread : curthread);
279         int err = 0;
280
281         err = fuse_vnode_alloc(mp, td, nodeid, vtyp, vpp);
282         if (err) {
283                 return err;
284         }
285         if (dvp != NULL) {
286                 MPASS(cnp && (cnp->cn_flags & ISDOTDOT) == 0);
287                 MPASS(cnp &&
288                         !(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.'));
289                 fuse_vnode_setparent(*vpp, dvp);
290         }
291         if (dvp != NULL && cnp != NULL && (cnp->cn_flags & MAKEENTRY) != 0 &&
292             feo != NULL &&
293             (feo->entry_valid != 0 || feo->entry_valid_nsec != 0)) {
294                 ASSERT_VOP_LOCKED(*vpp, "fuse_vnode_get");
295                 ASSERT_VOP_LOCKED(dvp, "fuse_vnode_get");
296                 cache_enter(dvp, *vpp, cnp);
297         }
298
299         /*
300          * In userland, libfuse uses cached lookups for dot and dotdot entries,
301          * thus it does not really bump the nlookup counter for forget.
302          * Follow the same semantic and avoid tu bump it in order to keep
303          * nlookup counters consistent.
304          */
305         if (cnp == NULL || ((cnp->cn_flags & ISDOTDOT) == 0 &&
306             (cnp->cn_namelen != 1 || cnp->cn_nameptr[0] != '.')))
307                 VTOFUD(*vpp)->nlookup++;
308
309         return 0;
310 }
311
312 void
313 fuse_vnode_open(struct vnode *vp, int32_t fuse_open_flags, struct thread *td)
314 {
315         /*
316          * Function is called for every vnode open.
317          * Merge fuse_open_flags it may be 0
318          */
319         /*
320          * Ideally speaking, direct io should be enabled on
321          * fd's but do not see of any way of providing that
322          * this implementation.
323          *
324          * Also cannot think of a reason why would two
325          * different fd's on same vnode would like
326          * have DIRECT_IO turned on and off. But linux
327          * based implementation works on an fd not an
328          * inode and provides such a feature.
329          *
330          * XXXIP: Handle fd based DIRECT_IO
331          */
332         if (vnode_vtype(vp) == VREG) {
333                 /* XXXIP prevent getattr, by using cached node size */
334                 vnode_create_vobject(vp, 0, td);
335         }
336 }
337
338 int
339 fuse_vnode_savesize(struct vnode *vp, struct ucred *cred, pid_t pid)
340 {
341         struct fuse_vnode_data *fvdat = VTOFUD(vp);
342         struct thread *td = curthread;
343         struct fuse_filehandle *fufh = NULL;
344         struct fuse_dispatcher fdi;
345         struct fuse_setattr_in *fsai;
346         int err = 0;
347
348         ASSERT_VOP_ELOCKED(vp, "fuse_io_extend");
349
350         if (fuse_isdeadfs(vp)) {
351                 return EBADF;
352         }
353         if (vnode_vtype(vp) == VDIR) {
354                 return EISDIR;
355         }
356         if (vfs_isrdonly(vnode_mount(vp))) {
357                 return EROFS;
358         }
359         if (cred == NULL) {
360                 cred = td->td_ucred;
361         }
362         fdisp_init(&fdi, sizeof(*fsai));
363         fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
364         fsai = fdi.indata;
365         fsai->valid = 0;
366
367         /* Truncate to a new value. */
368         fsai->size = fvdat->filesize;
369         fsai->valid |= FATTR_SIZE;
370
371         fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
372         if (fufh) {
373                 fsai->fh = fufh->fh_id;
374                 fsai->valid |= FATTR_FH;
375         }
376         err = fdisp_wait_answ(&fdi);
377         fdisp_destroy(&fdi);
378         if (err == 0)
379                 fvdat->flag &= ~FN_SIZECHANGE;
380
381         return err;
382 }
383
384 int
385 fuse_vnode_refreshsize(struct vnode *vp, struct ucred *cred)
386 {
387
388         struct fuse_vnode_data *fvdat = VTOFUD(vp);
389         struct vattr va;
390         int err;
391
392         if ((fvdat->flag & FN_SIZECHANGE) != 0 ||
393             fuse_data_cache_mode == FUSE_CACHE_UC ||
394             (fuse_refresh_size == 0 && fvdat->filesize != 0))
395                 return 0;
396
397         err = VOP_GETATTR(vp, &va, cred);
398         SDT_PROBE2(fuse, , node, trace, 1, "refreshed file size");
399         return err;
400 }
401
402 int
403 fuse_vnode_setsize(struct vnode *vp, struct ucred *cred, off_t newsize)
404 {
405         struct fuse_vnode_data *fvdat = VTOFUD(vp);
406         struct vattr *attrs;
407         off_t oldsize;
408         size_t iosize;
409         struct buf *bp = NULL;
410         int err = 0;
411
412         ASSERT_VOP_ELOCKED(vp, "fuse_vnode_setsize");
413
414         iosize = fuse_iosize(vp);
415         oldsize = fvdat->filesize;
416         fvdat->filesize = newsize;
417         if ((attrs = VTOVA(vp)) != NULL)
418                 attrs->va_size = newsize;
419         fvdat->flag |= FN_SIZECHANGE;
420
421         if (newsize < oldsize) {
422                 daddr_t lbn;
423                 size_t zsize;
424
425                 err = vtruncbuf(vp, cred, newsize, fuse_iosize(vp));
426                 if (err)
427                         goto out;
428                 if (newsize % iosize == 0)
429                         goto out;
430                 /* 
431                  * Zero the contents of the last partial block.
432                  * Sure seems like vtruncbuf should do this for us.
433                  */
434
435                 lbn = newsize / iosize;
436                 bp = getblk(vp, lbn, iosize, PCATCH, 0, 0);
437                 if (!bp) {
438                         err = EINTR;
439                         goto out;
440                 }
441                 if (!(bp->b_flags & B_CACHE))
442                         goto out;       /* Nothing to do */
443                 zsize = (lbn + 1) * iosize - newsize;
444                 bzero(bp->b_data + newsize - lbn * iosize, zsize);
445         }
446 out:
447         if (bp)
448                 brelse(bp);
449         vnode_pager_setsize(vp, newsize);
450         return err;
451 }