-
Notifications
You must be signed in to change notification settings - Fork 7
/
fuse_locking.c
515 lines (439 loc) · 12.3 KB
/
fuse_locking.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
* Copyright (C) 2006-2008 Google. All Rights Reserved.
* Copyright (C) 2010 Tuxera. All Rights Reserved.
*/
/*
* Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
*
* This file contains Original Code and/or Modifications of Original Code as
* defined in and that are subject to the Apple Public Source License Version
* 2.0 (the 'License'). You may not use this file except in compliance with
* the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see
* the License for the specific language governing rights and limitations
* under the License.
*/
#include "fuse.h"
#include "fuse_ipc.h"
#include "fuse_node.h"
#include "fuse_locking.h"
#include <stdbool.h>
lck_attr_t *fuse_lock_attr = NULL;
lck_grp_attr_t *fuse_group_attr = NULL;
lck_grp_t *fuse_lock_group = NULL;
lck_mtx_t *fuse_device_mutex = NULL;
#ifdef FUSE4X_ENABLE_TSLOCKING
#include <sys/ubc.h>
/*
* Largely identical to HFS+ locking. Much of the code is from hfs_cnode.c.
*/
/*
* Lock a fusenode.
*/
__private_extern__
int
fusefs_lock(fusenode_t cp, enum fusefslocktype locktype)
{
void *thread = current_thread();
if (locktype == FUSEFS_SHARED_LOCK) {
lck_rw_lock_shared(cp->nodelock);
cp->nodelockowner = FUSEFS_SHARED_OWNER;
} else {
lck_rw_lock_exclusive(cp->nodelock);
cp->nodelockowner = thread;
}
/*
* Skip nodes that no longer exist (were deleted).
*/
if ((locktype != FUSEFS_FORCE_LOCK) && (cp->c_flag & C_NOEXISTS)) {
fusefs_unlock(cp);
return ENOENT;
}
return 0;
}
/*
* Lock a pair of fusenodes.
*/
__private_extern__
int
fusefs_lockpair(fusenode_t cp1, fusenode_t cp2, enum fusefslocktype locktype)
{
fusenode_t first, last;
int error;
/*
* If cnodes match then just lock one.
*/
if (cp1 == cp2) {
return fusefs_lock(cp1, locktype);
}
/*
* Lock in cnode parent-child order (if there is a relationship);
* otherwise lock in cnode address order.
*/
if ((cp1->vtype == VDIR) && (cp1->nodeid == cp2->parent_nodeid)) {
first = cp1;
last = cp2;
} else if (cp1 < cp2) {
first = cp1;
last = cp2;
} else {
first = cp2;
last = cp1;
}
if ( (error = fusefs_lock(first, locktype))) {
return error;
}
if ( (error = fusefs_lock(last, locktype))) {
fusefs_unlock(first);
return error;
}
return 0;
}
/*
* Check ordering of two fusenodes. Return true if they are are in-order.
*/
static int
fusefs_isordered(fusenode_t cp1, fusenode_t cp2)
{
if (cp1 == cp2) {
return 0;
}
if (cp1 == NULL || cp2 == (fusenode_t)0xffffffff) {
return 1;
}
if (cp2 == NULL || cp1 == (fusenode_t)0xffffffff) {
return 0;
}
if (cp1->nodeid == cp2->parent_nodeid) {
return 1; /* cp1 is the parent and should go first */
}
if (cp2->nodeid == cp1->parent_nodeid) {
return 0; /* cp1 is the child and should go last */
}
return (cp1 < cp2); /* fall-back is to use address order */
}
/*
* Acquire 4 fusenode locks.
* - locked in fusenode parent-child order (if there is a relationship)
* otherwise lock in fusenode address order (lesser address first).
* - all or none of the locks are taken
* - only one lock taken per fusenode (dup fusenodes are skipped)
* - some of the fusenode pointers may be null
*/
__private_extern__
int
fusefs_lockfour(fusenode_t cp1, fusenode_t cp2, fusenode_t cp3, fusenode_t cp4,
enum fusefslocktype locktype)
{
fusenode_t a[3];
fusenode_t b[3];
fusenode_t list[4];
fusenode_t tmp;
int i, j, k;
int error;
if (fusefs_isordered(cp1, cp2)) {
a[0] = cp1; a[1] = cp2;
} else {
a[0] = cp2; a[1] = cp1;
}
if (fusefs_isordered(cp3, cp4)) {
b[0] = cp3; b[1] = cp4;
} else {
b[0] = cp4; b[1] = cp3;
}
a[2] = (fusenode_t)0xffffffff; /* sentinel value */
b[2] = (fusenode_t)0xffffffff; /* sentinel value */
/*
* Build the lock list, skipping over duplicates
*/
for (i = 0, j = 0, k = 0; (i < 2 || j < 2); ) {
tmp = fusefs_isordered(a[i], b[j]) ? a[i++] : b[j++];
if (k == 0 || tmp != list[k-1])
list[k++] = tmp;
}
/*
* Now we can lock using list[0 - k].
* Skip over NULL entries.
*/
for (i = 0; i < k; ++i) {
if (list[i])
if ((error = fusefs_lock(list[i], locktype))) {
/* Drop any locks we acquired. */
while (--i >= 0) {
if (list[i])
fusefs_unlock(list[i]);
}
return error;
}
}
return 0;
}
/*
* Unlock a fusenode.
*/
__private_extern__
void
fusefs_unlock(fusenode_t cp)
{
u_int32_t c_flag;
vnode_t vp = NULLVP;
c_flag = cp->c_flag;
cp->c_flag &= ~(C_NEED_DVNODE_PUT | C_NEED_DATA_SETSIZE);
if (c_flag & (C_NEED_DVNODE_PUT | C_NEED_DATA_SETSIZE)) {
vp = cp->vp;
}
cp->nodelockowner = NULL;
fusefs_lck_rw_done(cp->nodelock);
/* Perform any vnode post processing after fusenode lock is dropped. */
if (vp) {
if (c_flag & C_NEED_DATA_SETSIZE) {
ubc_setsize(vp, 0);
}
if (c_flag & C_NEED_DVNODE_PUT) {
vnode_put(vp);
}
}
}
/*
* Unlock a pair of fusenodes.
*/
__private_extern__
void
fusefs_unlockpair(fusenode_t cp1, fusenode_t cp2)
{
fusefs_unlock(cp1);
if (cp2 != cp1) {
fusefs_unlock(cp2);
}
}
/*
* Unlock a group of fusenodes.
*/
__private_extern__
void
fusefs_unlockfour(fusenode_t cp1, fusenode_t cp2,
fusenode_t cp3, fusenode_t cp4)
{
fusenode_t list[4];
int i, k = 0;
if (cp1) {
fusefs_unlock(cp1);
list[k++] = cp1;
}
if (cp2) {
for (i = 0; i < k; ++i) {
if (list[i] == cp2)
goto skip1;
}
fusefs_unlock(cp2);
list[k++] = cp2;
}
skip1:
if (cp3) {
for (i = 0; i < k; ++i) {
if (list[i] == cp3)
goto skip2;
}
fusefs_unlock(cp3);
list[k++] = cp3;
}
skip2:
if (cp4) {
for (i = 0; i < k; ++i) {
if (list[i] == cp4)
return;
}
fusefs_unlock(cp4);
}
}
/*
* Protect a fusenode against truncation.
*
* Used mainly by read/write since they don't hold the fusenode lock across
* calls to the cluster layer.
*
* The process doing a truncation must take the lock exclusive. The read/write
* processes can take it non-exclusive.
*/
__private_extern__
void
fusefs_lock_truncate(fusenode_t cp, lck_rw_type_t lck_rw_type)
{
if (cp->nodelockowner == current_thread()) {
panic("fuse4x: fusefs_lock_truncate: cnode %p locked!", cp);
}
lck_rw_lock(cp->truncatelock, lck_rw_type);
}
__private_extern__
void
fusefs_unlock_truncate(fusenode_t cp)
{
fusefs_lck_rw_done(cp->truncatelock);
}
#endif
#include <IOKit/IOLocks.h>
__private_extern__
void
fusefs_lck_rw_done(lck_rw_t *lock)
{
IORWLockUnlock((IORWLock *)lock);
}
#ifdef FUSE4X_ENABLE_BIGLOCK
/* Recursive lock used to lock the vfs functions awaiting more fine-grained
* locking. Code was taken from IOLocks.cpp to imitate how an IORecursiveLock
* behaves. */
struct _fusefs_recursive_lock {
lck_mtx_t *mutex;
lck_grp_t *group;
thread_t thread;
UInt32 count;
};
static fusefs_recursive_lock* fusefs_recursive_lock_alloc_with_lock_group(
lck_grp_t *lock_group)
{
struct _fusefs_recursive_lock *lock;
if (lock_group == NULL)
return 0;
lock = (struct _fusefs_recursive_lock*) FUSE_OSMalloc(
sizeof(struct _fusefs_recursive_lock), fuse_malloc_tag);
if (!lock)
return NULL;
lock->mutex = lck_mtx_alloc_init(lock_group, LCK_ATTR_NULL);
if (lock->mutex) {
lock->group = lock_group;
lock->thread = 0;
lock->count = 0;
} else {
FUSE_OSFree(lock, sizeof(struct _fusefs_recursive_lock),
fuse_malloc_tag);
lock = 0;
}
return (fusefs_recursive_lock*) lock;
}
fusefs_recursive_lock* fusefs_recursive_lock_alloc(void)
{
return fusefs_recursive_lock_alloc_with_lock_group(fuse_lock_group);
}
void fusefs_recursive_lock_free(fusefs_recursive_lock* lock)
{
lck_mtx_free(lock->mutex, lock->group);
FUSE_OSFree(lock, sizeof(fusefs_recursive_lock), fuse_malloc_tag);
}
/* Currently not exported in header as we don't use it anywhere. */
#if 0
lck_mtx_t* fusefs_recursive_lock_get_mach_lock(fusefs_recursive_lock* lock)
{
return lock->mutex;
}
#endif
void fusefs_recursive_lock_lock(fusefs_recursive_lock *lock)
{
if (lock->thread == current_thread())
lock->count++;
else {
lck_mtx_lock(lock->mutex);
assert(lock->thread == 0);
assert(lock->count == 0);
lock->thread = current_thread();
lock->count = 1;
}
}
/* Currently not exported in header as we don't use it anywhere. */
/* Can't find lck_mtx_try_lock in headers, so this function can't compile. */
#if 0
bool fusefs_recursive_lock_try_lock(fusefs_recursive_lock *lock)
{
if (lock->thread == current_thread()) {
lock->count++;
return true;
} else {
if (lck_mtx_try_lock(lock->mutex)) {
assert(lock->thread == 0);
assert(lock->count == 0);
lock->thread = current_thread();
lock->count = 1;
return true;
}
}
return false;
}
#endif
void fusefs_recursive_lock_unlock(fusefs_recursive_lock *lock)
{
assert(lock->thread == current_thread());
if(lock->count == 0)
panic("Attempted to unlock non-locked recursive lock.");
if (0 == (--lock->count)) {
lock->thread = 0;
lck_mtx_unlock(lock->mutex);
}
}
/* Currently not exported in header as we don't use it anywhere. */
#if 0
bool fusefs_recursive_lock_have_lock(fusefs_recursive_lock *lock)
{
return lock->thread == current_thread();
}
#endif
/* Currently not exported in header as we don't use it anywhere. */
#if 0
int fusefs_recursive_lock_sleep(fusefs_recursive_lock *lock,
void *event, UInt32 interType)
{
UInt32 count = lock->count;
int res;
assert(lock->thread == current_thread());
lock->count = 0;
lock->thread = 0;
res = lck_mtx_sleep(lock->mutex, LCK_SLEEP_DEFAULT, (event_t) event,
(wait_interrupt_t) interType);
// Must re-establish the recursive lock no matter why we woke up
// otherwise we would potentially leave the return path corrupted.
assert(lock->thread == 0);
assert(lock->count == 0);
lock->thread = current_thread();
lock->count = count;
return res;
}
#endif
/* Currently not exported in header as we don't use it anywhere. */
/* __OSAbsoluteTime is KERNEL_PRIVATE, so this function can't compile. */
#if 0
int fusefs_recursive_lock_sleep_deadline(fusefs_recursive_lock *lock,
void *event, AbsoluteTime deadline, UInt32 interType)
{
UInt32 count = lock->count;
int res;
assert(lock->thread == current_thread());
lock->count = 0;
lock->thread = 0;
res = lck_mtx_sleep_deadline(lock->mutex, LCK_SLEEP_DEFAULT,
(event_t) event, (wait_interrupt_t) interType,
__OSAbsoluteTime(deadline));
// Must re-establish the recursive lock no matter why we woke up
// otherwise we would potentially leave the return path corrupted.
assert(lock->thread == 0);
assert(lock->count == 0);
lock->thread = current_thread();
lock->count = count;
return res;
}
#endif
/* Currently not exported in header as we don't use it anywhere. */
#if 0
void fusefs_recursive_lock_wakeup(__unused fusefs_recursive_lock *lock,
void *event, bool oneThread)
{
thread_wakeup_prim((event_t) event, oneThread, THREAD_AWAKENED);
}
#endif
#endif /* FUSE4X_ENABLE_BIGLOCK */
#ifdef FUSE4X_SERIALIZE_LOGGING
lck_mtx_t *fuse_log_lock = NULL;
#endif /* FUSE4X_SERIALIZE_LOGGING */