forked from fuse4x/kext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuse_node.c
277 lines (231 loc) · 7.84 KB
/
fuse_node.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* 'rebel' branch modifications:
* Copyright (C) 2010 Tuxera. All Rights Reserved.
*/
/*
* Copyright (C) 2006-2008 Google. All Rights Reserved.
* Amit Singh <singh@>
*/
#include "fuse.h"
#include "fuse_internal.h"
#include "fuse_ipc.h"
#include "fuse_locking.h"
#include "fuse_node.h"
#include "fuse_sysctl.h"
#include <stdbool.h>
#if M_FUSE4X_ENABLE_BIGLOCK
#include "fuse_biglock_vnops.h"
#endif
void
FSNodeScrub(struct fuse_vnode_data *fvdat)
{
lck_mtx_free(fvdat->createlock, fuse_lock_group);
#if M_FUSE4X_ENABLE_TSLOCKING
lck_rw_free(fvdat->nodelock, fuse_lock_group);
lck_rw_free(fvdat->truncatelock, fuse_lock_group);
#endif
fvdat->fMagic = kFSNodeBadMagic;
}
errno_t
FSNodeGetOrCreateFileVNodeByID(vnode_t *vnPtr,
uint32_t flags,
struct fuse_entry_out *feo,
mount_t mp,
vnode_t dvp,
vfs_context_t context,
uint32_t *oflags)
{
int err;
int junk;
vnode_t vn = NULLVP;
HNodeRef hn = NULL;
vnode_t dirVN = NULLVP;
struct fuse_vnode_data *fvdat = NULL;
struct fuse_data *mntdata = NULL;
fuse_device_t dummy_device;
enum vtype vtyp = IFTOVT(feo->attr.mode);
if ((vtyp >= VBAD) || (vtyp < 0) || (vtyp == VNON)) {
return EINVAL;
}
bool markroot = (flags & FN_IS_ROOT) ? true : false;
uint64_t size = (flags & FN_IS_ROOT) ? 0 : feo->attr.size;
uint32_t rdev = (flags & FN_IS_ROOT) ? 0 : feo->attr.rdev;
uint64_t generation = feo->generation;
mntdata = fuse_get_mpdata(mp);
dummy_device = mntdata->fdev;
err = HNodeLookupCreatingIfNecessary(dummy_device, feo->nodeid,
0 /* fork index */, &hn, &vn);
if ((err == 0) && (vn == NULL)) {
struct vnode_fsparam params;
fvdat = (struct fuse_vnode_data *)FSNodeGenericFromHNode(hn);
if (!fvdat->fInitialised) {
/* check */
fvdat->fMagic = kFSNodeMagic;
fvdat->fInitialised = true;
/* self */
fvdat->vp = NULLVP; /* hold on */
fvdat->nodeid = feo->nodeid;
fvdat->generation = generation;
/* parent */
fvdat->parentvp = dvp;
if (dvp) {
fvdat->parent_nodeid = VTOI(dvp);
} else {
fvdat->parent_nodeid = 0;
}
/* I/O */
{
int k;
for (k = 0; k < FUFH_MAXTYPE; k++) {
FUFH_USE_RESET(&(fvdat->fufh[k]));
}
}
/* flags */
fvdat->flag = flags;
fvdat->c_flag = 0;
/* meta */
/* XXX: truncation */
fvdat->entry_valid.tv_sec = (time_t)feo->entry_valid;
fvdat->entry_valid.tv_nsec = feo->entry_valid_nsec;
/* XXX: truncation */
fvdat->attr_valid.tv_sec = (time_t)feo->attr_valid;
fvdat->attr_valid.tv_nsec = feo->attr_valid_nsec;
/* XXX: truncation */
fvdat->modify_time.tv_sec = (time_t)feo->attr.mtime;
fvdat->modify_time.tv_nsec = feo->attr.mtimensec;
fvdat->filesize = size;
fvdat->nlookup = 0;
fvdat->vtype = vtyp;
/* locking */
fvdat->createlock = lck_mtx_alloc_init(fuse_lock_group,
fuse_lock_attr);
fvdat->creator = current_thread();
#if M_FUSE4X_ENABLE_TSLOCKING
fvdat->nodelock = lck_rw_alloc_init(fuse_lock_group,
fuse_lock_attr);
fvdat->nodelockowner = NULL;
fvdat->truncatelock = lck_rw_alloc_init(fuse_lock_group,
fuse_lock_attr);
#endif
}
if (err == 0) {
params.vnfs_mp = mp;
params.vnfs_vtype = vtyp;
params.vnfs_str = NULL;
params.vnfs_dvp = dvp; /* NULLVP for the root vnode */
params.vnfs_fsnode = hn;
#if M_FUSE4X_ENABLE_SPECFS
if ((vtyp == VBLK) || (vtyp == VCHR)) {
params.vnfs_vops = fuse_spec_operations;
params.vnfs_rdev = (dev_t)rdev;
#else
if (0) {
#endif
#if M_FUSE4X_ENABLE_FIFOFS
} else if (vtyp == VFIFO) {
params.vnfs_vops = fuse_fifo_operations;
params.vnfs_rdev = 0;
(void)rdev;
#else
} else if (0) {
#endif
} else {
params.vnfs_vops = fuse_vnode_operations;
params.vnfs_rdev = 0;
(void)rdev;
}
params.vnfs_marksystem = false;
params.vnfs_cnp = NULL;
params.vnfs_flags = VNFS_NOCACHE | VNFS_CANTCACHE;
params.vnfs_filesize = size;
params.vnfs_markroot = markroot;
#if M_FUSE4X_ENABLE_BIGLOCK
fuse_biglock_unlock(mntdata->biglock);
#endif
err = vnode_create(VNCREATE_FLAVOR, (uint32_t)sizeof(params),
¶ms, &vn);
#if M_FUSE4X_ENABLE_BIGLOCK
fuse_biglock_lock(mntdata->biglock);
#endif
}
if (err == 0) {
if (markroot) {
fvdat->parentvp = vn;
} else {
fvdat->parentvp = dvp;
}
if (oflags) {
*oflags |= MAKEENTRY;
}
HNodeAttachVNodeSucceeded(hn, 0 /* forkIndex */, vn);
OSAddAtomic(1, (SInt32 *)&fuse_vnodes_current);
} else {
if (HNodeAttachVNodeFailed(hn, 0 /* forkIndex */)) {
FSNodeScrub(fvdat);
HNodeScrubDone(hn);
}
}
}
if (err == 0) {
if (vnode_vtype(vn) != vtyp) {
log("fuse4x: vnode changed type behind us (old=%d, new=%d)\n",
vnode_vtype(vn), vtyp);
#if M_FUSE4X_ENABLE_BIGLOCK
fuse_biglock_unlock(mntdata->biglock);
#endif
fuse_internal_vnode_disappear(vn, context, REVOKE_SOFT);
#if M_FUSE4X_ENABLE_BIGLOCK
fuse_biglock_lock(mntdata->biglock);
#endif
vnode_put(vn);
err = EIO;
} else if (VTOFUD(vn)->generation != generation) {
log("fuse4x: vnode changed generation\n");
#if M_FUSE4X_ENABLE_BIGLOCK
fuse_biglock_unlock(mntdata->biglock);
#endif
fuse_internal_vnode_disappear(vn, context, REVOKE_SOFT);
#if M_FUSE4X_ENABLE_BIGLOCK
fuse_biglock_lock(mntdata->biglock);
#endif
vnode_put(vn);
err = ESTALE;
}
}
if (err == 0) {
*vnPtr = vn;
/* Need VT_FUSE4X from xnu */
vnode_settag(vn, VT_OTHER);
}
if (dirVN != NULL) {
junk = vnode_put(dirVN);
/* assert(junk == 0); */
}
/* assert((err == 0) == (*vnPtr != NULL); */
return err;
}
int
fuse_vget_i(vnode_t *vpp,
uint32_t flags,
struct fuse_entry_out *feo,
struct componentname *cnp,
vnode_t dvp,
mount_t mp,
vfs_context_t context)
{
int err = 0;
if (!feo) {
return EINVAL;
}
err = FSNodeGetOrCreateFileVNodeByID(vpp, flags, feo, mp, dvp,
context, NULL);
if (err) {
return err;
}
if (!fuse_isnovncache_mp(mp) && (cnp->cn_flags & MAKEENTRY)) {
fuse_vncache_enter(dvp, *vpp, cnp);
}
/* found: */
VTOFUD(*vpp)->nlookup++;
return 0;
}