3c9eda73044de4bffbf9bd77a17d4a4ba60e8989
[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 uint64_t fuse_nfh(struct fuse_node *fnp)
77 {
78         fuse_dbg("ino=%ju fh=%jx\n", fnp->ino, fnp->fh);
79         return fnp->fh;
80 }
81
82 void fuse_get_nfh(struct fuse_node *fnp, uint64_t fh)
83 {
84         fnp->fh = fh;
85         fuse_dbg("ino=%ju fh=%jx\n", fnp->ino, fnp->fh);
86 }
87
88 void fuse_put_nfh(struct fuse_node *fnp)
89 {
90         fuse_dbg("ino=%ju fh=%jx\n", fnp->ino, fnp->fh);
91 }
92
93 void
94 fuse_file_init(void)
95 {
96         fuse_fh_objcache = objcache_create("fuse_fh", 0, 0,
97             NULL, NULL, NULL,
98             objcache_malloc_alloc_zero, objcache_malloc_free, &fuse_fh_args);
99 }
100
101 void
102 fuse_file_cleanup(void)
103 {
104         objcache_destroy(fuse_fh_objcache);
105 }