forked from fuse4x/kext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuse_file.c
179 lines (150 loc) · 4.5 KB
/
fuse_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright (C) 2006-2008 Google. All Rights Reserved.
* Amit Singh <singh@>
*/
#include "fuse.h"
#include "fuse_file.h"
#include "fuse_internal.h"
#include "fuse_ipc.h"
#include "fuse_node.h"
#include "fuse_sysctl.h"
#if M_FUSE4X_ENABLE_BIGLOCK
#include "fuse_biglock_vnops.h"
#endif
/*
* Because of the vagaries of how a filehandle can be used, we try not to
* be too smart in here (we try to be smart elsewhere). It is required that
* you come in here only if you really do not have the said filehandle--else
* we panic.
*/
int
fuse_filehandle_get(vnode_t vp,
vfs_context_t context,
fufh_type_t fufh_type,
int mode)
{
struct fuse_dispatcher fdi;
struct fuse_open_in *foi;
struct fuse_open_out *foo;
struct fuse_filehandle *fufh;
struct fuse_vnode_data *fvdat = VTOFUD(vp);
int err = 0;
int isdir = 0;
int oflags = 0;
int op = FUSE_OPEN;
fuse_trace_printf("fuse_filehandle_get(vp=%p, fufh_type=%d, mode=%x)\n",
vp, fufh_type, mode);
fufh = &(fvdat->fufh[fufh_type]);
if (FUFH_IS_VALID(fufh)) {
panic("fuse4x: filehandle_get called despite valid fufh (type=%d)",
fufh_type);
/* NOTREACHED */
}
/*
* Note that this means we are effectively FILTERING OUT open() flags.
*/
(void)mode;
oflags = fuse_filehandle_xlate_to_oflags(fufh_type);
if (vnode_isdir(vp)) {
isdir = 1;
op = FUSE_OPENDIR;
if (fufh_type != FUFH_RDONLY) {
log("fuse4x: non-rdonly fufh requested for directory\n");
fufh_type = FUFH_RDONLY;
}
}
fdisp_init(&fdi, sizeof(*foi));
fdisp_make_vp(&fdi, op, vp, context);
if (vnode_islnk(vp) && (mode & O_SYMLINK)) {
oflags |= O_SYMLINK;
}
foi = fdi.indata;
foi->flags = oflags;
OSAddAtomic(1, (SInt32 *)&fuse_fh_upcall_count);
if ((err = fdisp_wait_answ(&fdi))) {
#if M_FUSE4X_ENABLE_UNSUPPORTED
const char *vname = vnode_getname(vp);
#endif /* M_FUSE4X_ENABLE_UNSUPPORTED */
if (err == ENOENT) {
/*
* See comment in fuse_vnop_reclaim().
*/
cache_purge(vp);
}
#if M_FUSE4X_ENABLE_UNSUPPORTED
log("fuse4x: filehandle_get: failed for %s "
"(type=%d, err=%d, caller=%p)\n",
(vname) ? vname : "?", fufh_type, err,
__builtin_return_address(0));
if (vname) {
vnode_putname(vname);
}
#endif /* M_FUSE4X_ENABLE_UNSUPPORTED */
if (err == ENOENT) {
#if M_FUSE4X_ENABLE_BIGLOCK
struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
fuse_biglock_unlock(data->biglock);
#endif
fuse_internal_vnode_disappear(vp, context, REVOKE_SOFT);
#if M_FUSE4X_ENABLE_BIGLOCK
fuse_biglock_lock(data->biglock);
#endif
}
return err;
}
OSAddAtomic(1, (SInt32 *)&fuse_fh_current);
foo = fdi.answ;
fufh->fh_id = foo->fh;
fufh->open_count = 1;
fufh->open_flags = oflags;
fufh->fuse_open_flags = foo->open_flags;
fufh->aux_count = 0;
fuse_ticket_drop(fdi.tick);
return 0;
}
int
fuse_filehandle_put(vnode_t vp, vfs_context_t context, fufh_type_t fufh_type)
{
struct fuse_dispatcher fdi;
struct fuse_release_in *fri;
struct fuse_vnode_data *fvdat = VTOFUD(vp);
struct fuse_filehandle *fufh = NULL;
int err = 0;
int isdir = 0;
int op = FUSE_RELEASE;
const bool wait_for_completion = true;
fuse_trace_printf("fuse_filehandle_put(vp=%p, fufh_type=%d)\n",
vp, fufh_type);
fufh = &(fvdat->fufh[fufh_type]);
if (FUFH_IS_VALID(fufh)) {
panic("fuse4x: filehandle_put called on a valid fufh (type=%d)",
fufh_type);
/* NOTREACHED */
}
if (fuse_isdeadfs(vp)) {
goto out;
}
if (vnode_isdir(vp)) {
op = FUSE_RELEASEDIR;
isdir = 1;
}
fdisp_init(&fdi, sizeof(*fri));
fdisp_make_vp(&fdi, op, vp, context);
fri = fdi.indata;
fri->fh = fufh->fh_id;
fri->flags = fufh->open_flags;
if (wait_for_completion) {
if ((err = fdisp_wait_answ(&fdi))) {
goto out;
} else {
fuse_ticket_drop(fdi.tick);
}
} else {
fuse_insert_callback(fdi.tick, NULL);
fuse_insert_message(fdi.tick);
}
out:
OSAddAtomic(-1, (SInt32 *)&fuse_fh_current);
fuse_invalidate_attr(vp);
return err;
}