Merge remote-tracking branch 'origin/vendor/LIBEDIT'
[dragonfly.git] / sys / vfs / fuse / fuse_file.c
1 /*-
2  * Copyright (c) 2019 Tomohiro Kusumi <tkusumi@netbsd.org>
3  * Copyright (c) 2019 The DragonFly Project
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include "fuse.h"
29
30 static MALLOC_DEFINE(M_FUSE_FH, "fuse_fh", "FUSE fh");
31
32 static struct objcache *fuse_fh_objcache = NULL;
33 static struct objcache_malloc_args fuse_fh_args = {
34         sizeof(uint64_t), M_FUSE_FH,
35 };
36
37 uint64_t fuse_fh(struct file *fp)
38 {
39         uint64_t *fhp = fp->private_data;
40         KKASSERT(fhp);
41
42         fuse_dbg("fh=%jx\n", *fhp);
43         return *fhp;
44 }
45
46 void fuse_get_fh(struct file *fp, uint64_t fh)
47 {
48         uint64_t *fhp = objcache_get(fuse_fh_objcache, M_WAITOK);
49         KKASSERT(fhp);
50
51         *fhp = fh;
52         fuse_dbg("fh=%jx\n", *fhp);
53
54         KKASSERT(!fp->private_data);
55         fp->private_data = fhp;
56 }
57
58 void fuse_put_fh(struct file *fp)
59 {
60         uint64_t *fhp = fp->private_data;
61         KKASSERT(fhp);
62
63         fuse_dbg("fh=%jx\n", *fhp);
64
65         objcache_put(fuse_fh_objcache, fhp);
66         fp->private_data = NULL;
67 }
68
69 /*
70  * nfh - per node fh (ad-hoc hack)
71  *
72  * XXX This should be gone, as the concept of nfh is already wrong.
73  * This exists due to how BSD VFS is implemented.
74  * There are situations where FUSE VOP's can't access fh required by FUSE ops.
75  */
76 /*
77  * The main problem is that due to difference vs Linux in ref counting of open
78  * file, FUSE can issue FUSE_RELEASE via VOP_CLOSE() when mmap(2) mapping still
79  * exists. By the time VOP_GETPAGES() or VOP_PUTPAGES() gets called, FUSE may
80  * have already released open file via FUSE_RELEASE, hence I/O fails.
81  *
82  * In Linux this is different because ->release() of open file gets called only
83  * after mmap(2) mapping is gone. FUSE_RELEASE naturally works under Linux VFS
84  * semantics.
85  */
86 uint64_t fuse_nfh(struct fuse_node *fnp)
87 {
88         fuse_dbg("ino=%ju fh=%jx\n", fnp->ino, fnp->fh);
89         return fnp->fh;
90 }
91
92 void fuse_get_nfh(struct fuse_node *fnp, uint64_t fh)
93 {
94         fnp->fh = fh;
95         fuse_dbg("ino=%ju fh=%jx\n", fnp->ino, fnp->fh);
96 }
97
98 void fuse_put_nfh(struct fuse_node *fnp)
99 {
100         fuse_dbg("ino=%ju fh=%jx\n", fnp->ino, fnp->fh);
101 }
102
103 void
104 fuse_file_init(void)
105 {
106         fuse_fh_objcache = objcache_create("fuse_fh", 0, 0,
107             NULL, NULL, NULL,
108             objcache_malloc_alloc_zero, objcache_malloc_free, &fuse_fh_args);
109 }
110
111 void
112 fuse_file_cleanup(void)
113 {
114         objcache_destroy(fuse_fh_objcache);
115 }