-
Notifications
You must be signed in to change notification settings - Fork 7
/
fuse_sysctl.c
446 lines (382 loc) · 15.3 KB
/
fuse_sysctl.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
/*
* Copyright (C) 2006-2008 Google. All Rights Reserved.
* Copyright (C) 2011 Anatol Pomozov. All Rights Reserved.
*/
#include "fuse.h"
#include "fuse_kernel.h"
#include "fuse_device.h"
#include "fuse_sysctl.h"
#include <fuse_param.h>
#include <fuse_version.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/sysctl.h>
/* NB: none of these are bigger than unsigned 32-bit. */
int32_t fuse_admin_group = 0; // rw
int32_t fuse_allow_other = 0; // rw
uint32_t fuse_api_major = FUSE_KERNEL_VERSION; // r
uint32_t fuse_api_minor = FUSE_KERNEL_MINOR_VERSION; // r
int32_t fuse_fh_current = 0; // r
uint32_t fuse_fh_reuse_count = 0; // r
uint32_t fuse_fh_upcall_count = 0; // r
uint32_t fuse_fh_zombies = 0; // r
int32_t fuse_iov_credit = FUSE_DEFAULT_IOV_CREDIT; // rw
int32_t fuse_iov_current = 0; // r
uint32_t fuse_iov_permanent_bufsize = FUSE_DEFAULT_IOV_PERMANENT_BUFSIZE; // rw
int32_t fuse_kill = -1; // w
int32_t fuse_print_vnodes = -1; // w
uint32_t fuse_lookup_cache_hits = 0; // r
uint32_t fuse_lookup_cache_misses = 0; // r
uint32_t fuse_lookup_cache_overrides = 0; // r
uint32_t fuse_max_freetickets = FUSE_DEFAULT_MAX_FREE_TICKETS; // rw
uint32_t fuse_max_tickets = 0; // rw
int32_t fuse_mount_count = 0; // r
int32_t fuse_realloc_count = 0; // r
int32_t fuse_tickets_current = 0; // r
uint32_t fuse_userkernel_bufsize = FUSE_DEFAULT_USERKERNEL_BUFSIZE; // rw
int32_t fuse_vnodes_current = 0; // r
#ifdef FUSE4X_ENABLE_MACFUSE_MODE
int32_t fuse_macfuse_mode = 0; // w
#endif
#ifdef FUSE4X_COUNT_MEMORY
int32_t fuse_memory_allocated = 0; // r
#endif
SYSCTL_DECL(_vfs_generic);
SYSCTL_NODE(_vfs_generic, OID_AUTO, fuse4x, CTLFLAG_RW, 0,
"fuse4x Sysctl Interface");
SYSCTL_NODE(_vfs_generic_fuse4x, OID_AUTO, control, CTLFLAG_RW, 0,
"fuse4x Controls");
SYSCTL_NODE(_vfs_generic_fuse4x, OID_AUTO, counters, CTLFLAG_RW, 0,
"fuse4x Monotonic Counters");
SYSCTL_NODE(_vfs_generic_fuse4x, OID_AUTO, resourceusage, CTLFLAG_RW, 0,
"fuse4x Resource Usage");
SYSCTL_NODE(_vfs_generic_fuse4x, OID_AUTO, tunables, CTLFLAG_RW, 0,
"fuse4x Tunables");
SYSCTL_NODE(_vfs_generic_fuse4x, OID_AUTO, version, CTLFLAG_RW, 0,
"fuse4x Version Information");
/* fuse.control */
int sysctl_fuse4x_control_kill_handler SYSCTL_HANDLER_ARGS;
#ifdef FUSE4X_ENABLE_MACFUSE_MODE
int sysctl_fuse4x_control_macfuse_mode_handler SYSCTL_HANDLER_ARGS;
#endif
int sysctl_fuse4x_control_print_vnodes_handler SYSCTL_HANDLER_ARGS;
int sysctl_fuse4x_tunables_userkernel_bufsize_handler SYSCTL_HANDLER_ARGS;
int
sysctl_fuse4x_control_kill_handler SYSCTL_HANDLER_ARGS
{
int error = 0;
(void)oidp;
if (arg1) {
error = SYSCTL_OUT(req, arg1, sizeof(int));
} else {
error = SYSCTL_OUT(req, &arg2, sizeof(int));
}
if (error || !req->newptr) {
return error;
}
if (!arg1) {
error = EPERM;
} else {
error = SYSCTL_IN(req, arg1, sizeof(int));
if (error == 0) {
error = fuse_device_kill(*(int *)arg1, req->p);
}
fuse_kill = -1; /* set it back */
}
return error;
}
#ifdef FUSE4X_ENABLE_MACFUSE_MODE
// some applications (e.g. TrueCrypt) check what macfuse version is installed.
// ideally they should not hard-code such check as macosx has several fuse providers.
SYSCTL_DECL(_macfuse);
SYSCTL_NODE(, OID_AUTO, macfuse, CTLFLAG_RW, 0,
"MacFUSE Sysctl Interface");
SYSCTL_NODE(_macfuse, OID_AUTO, version, CTLFLAG_RW, 0,
"MacFUSE Version Information");
SYSCTL_STRING(_macfuse_version, OID_AUTO, number, CTLFLAG_RD,
"2.2.0", 0, ""); // this is for you TrueCrypt
static struct sysctl_oid *macfuse_sysctl_list[] = {
&sysctl__macfuse_version,
&sysctl__macfuse_version_number,
NULL
};
static thread_t fuse4x_macfuse_mode_thread;
static void fuse4x_macfuse_mode_stop(void)
{
int i;
for (i = 0; macfuse_sysctl_list[i]; i++) {
sysctl_unregister_oid(macfuse_sysctl_list[i]);
}
sysctl_unregister_oid(&sysctl__macfuse);
}
static void fuse4x_macfuse_mode_start(void)
{
int i;
sysctl_register_oid(&sysctl__macfuse);
for (i = 0; macfuse_sysctl_list[i]; i++) {
sysctl_register_oid(macfuse_sysctl_list[i]);
}
}
static void fuse4x_macfuse_mode_handler(__unused void *param, __unused wait_result_t result)
{
if (fuse_macfuse_mode) {
fuse4x_macfuse_mode_start();
} else {
fuse4x_macfuse_mode_stop();
}
thread_terminate(current_thread());
}
int
sysctl_fuse4x_control_macfuse_mode_handler SYSCTL_HANDLER_ARGS
{
int error = 0;
if (arg1) {
error = SYSCTL_OUT(req, arg1, sizeof(int));
} else {
error = SYSCTL_OUT(req, &arg2, sizeof(int));
}
if (error || !req->newptr) {
goto exit;
}
if (!arg1) {
error = EPERM;
goto exit;
} else {
int value;
error = SYSCTL_IN(req, &value, sizeof(value));
if (error) {
goto exit;
}
if (value != 0) {
value = 1;
}
if (value != fuse_macfuse_mode) {
fuse_macfuse_mode = value;
// switch the comaptibility mode
kern_return_t ret = kernel_thread_start(fuse4x_macfuse_mode_handler, NULL, &fuse4x_macfuse_mode_thread);
if (ret != KERN_SUCCESS) {
log("fuse4x: Cannot set macfuse compatibility mode");
error = EPERM;
goto exit;
}
}
}
exit:
return error;
}
#endif
int
sysctl_fuse4x_control_print_vnodes_handler SYSCTL_HANDLER_ARGS
{
int error = 0;
(void)oidp;
if (arg1) {
error = SYSCTL_OUT(req, arg1, sizeof(uint32_t));
} else {
error = SYSCTL_OUT(req, &arg2, sizeof(uint32_t));
}
if (error || !req->newptr) {
return error;
}
if (!arg1) {
error = EPERM;
} else {
error = SYSCTL_IN(req, arg1, sizeof(int));
if (error == 0) {
error = fuse_device_print_vnodes(*(int *)arg1, req->p);
}
fuse_print_vnodes = -1; /* set it back */
}
return error;
}
int
sysctl_fuse4x_tunables_userkernel_bufsize_handler SYSCTL_HANDLER_ARGS
{
int error = 0;
(void)oidp;
if (arg1) {
error = SYSCTL_OUT(req, arg1, sizeof(int));
} else {
error = SYSCTL_OUT(req, &arg2, sizeof(int));
}
if (error || !req->newptr) {
return error;
}
if (!arg1) {
error = EPERM;
} else {
error = SYSCTL_IN(req, arg1, sizeof(uint32_t));
if (error == 0) {
uint32_t incoming = *(uint32_t *)arg1;
incoming = fuse_round_page_32(incoming);
if (incoming > FUSE_MAX_USERKERNEL_BUFSIZE) {
error = E2BIG;
} else if (incoming < FUSE_MIN_USERKERNEL_BUFSIZE) {
error = EINVAL;
} else {
fuse_userkernel_bufsize = incoming;
}
}
}
return error;
}
SYSCTL_PROC(_vfs_generic_fuse4x_control, // our parent
OID_AUTO, // automatically assign object ID
kill, // our name
// type flag/access flag
(CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_ANYBODY),
&fuse_kill, // location of our data
0, // argument passed to our handler
// our handler function
sysctl_fuse4x_control_kill_handler,
"I", // our data type (integer)
"fuse4x Controls: Kill the Given File System");
#ifdef FUSE4X_ENABLE_MACFUSE_MODE
SYSCTL_PROC(_vfs_generic_fuse4x_control, // our parent
OID_AUTO, // automatically assign object ID
macfuse_mode, // our name
// type flag/access flag
(CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_ANYBODY),
&fuse_macfuse_mode, // location of our data
0, // argument passed to our handler
// our handler function
sysctl_fuse4x_control_macfuse_mode_handler,
"I", // our data type (integer)
"fuse4x Controls: Enable Macfuse Compatibility Mode");
#endif
SYSCTL_PROC(_vfs_generic_fuse4x_control, // our parent
OID_AUTO, // automatically assign object ID
print_vnodes, // our name
// type flag/access flag
(CTLTYPE_INT | CTLFLAG_WR),
&fuse_print_vnodes, // location of our data
0, // argument passed to our handler
// our handler function
sysctl_fuse4x_control_print_vnodes_handler,
"I", // our data type (integer)
"fuse4x Controls: Print Vnodes for the Given File System");
/* fuse.counters */
SYSCTL_INT(_vfs_generic_fuse4x_counters, OID_AUTO, filehandle_reuse, CTLFLAG_RD,
&fuse_fh_reuse_count, 0, "");
SYSCTL_INT(_vfs_generic_fuse4x_counters, OID_AUTO, filehandle_upcalls, CTLFLAG_RD,
&fuse_fh_upcall_count, 0, "");
SYSCTL_INT(_vfs_generic_fuse4x_counters, OID_AUTO, lookup_cache_hits, CTLFLAG_RD,
&fuse_lookup_cache_hits, 0, "");
SYSCTL_INT(_vfs_generic_fuse4x_counters, OID_AUTO, lookup_cache_misses, CTLFLAG_RD,
&fuse_lookup_cache_misses, 0, "");
SYSCTL_INT(_vfs_generic_fuse4x_counters, OID_AUTO, lookup_cache_overrides,
CTLFLAG_RD, &fuse_lookup_cache_overrides, 0, "");
SYSCTL_INT(_vfs_generic_fuse4x_counters, OID_AUTO, memory_reallocs, CTLFLAG_RD,
&fuse_realloc_count, 0, "");
/* fuse.resourceusage */
SYSCTL_INT(_vfs_generic_fuse4x_resourceusage, OID_AUTO, filehandles, CTLFLAG_RD,
&fuse_fh_current, 0, "");
SYSCTL_INT(_vfs_generic_fuse4x_resourceusage, OID_AUTO, filehandles_zombies, CTLFLAG_RD,
&fuse_fh_zombies, 0, "");
SYSCTL_INT(_vfs_generic_fuse4x_resourceusage, OID_AUTO, ipc_iovs, CTLFLAG_RD,
&fuse_iov_current, 0, "");
SYSCTL_INT(_vfs_generic_fuse4x_resourceusage, OID_AUTO, ipc_tickets, CTLFLAG_RD,
&fuse_tickets_current, 0, "");
SYSCTL_INT(_vfs_generic_fuse4x_resourceusage, OID_AUTO, mounts, CTLFLAG_RD,
&fuse_mount_count, 0, "");
SYSCTL_INT(_vfs_generic_fuse4x_resourceusage, OID_AUTO, vnodes, CTLFLAG_RD,
&fuse_vnodes_current, 0, "");
#ifdef FUSE4X_COUNT_MEMORY
SYSCTL_INT(_vfs_generic_fuse4x_resourceusage, OID_AUTO, memory_bytes, CTLFLAG_RD,
&fuse_memory_allocated, 0, "");
#endif
/* fuse.tunables */
SYSCTL_INT(_vfs_generic_fuse4x_tunables, OID_AUTO, admin_group, CTLFLAG_RW,
&fuse_admin_group, 0, "");
SYSCTL_INT(_vfs_generic_fuse4x_tunables, OID_AUTO, allow_other, CTLFLAG_RW,
&fuse_allow_other, 0, "");
SYSCTL_INT(_vfs_generic_fuse4x_tunables, OID_AUTO, iov_credit, CTLFLAG_RW,
&fuse_iov_credit, 0, "");
SYSCTL_INT(_vfs_generic_fuse4x_tunables, OID_AUTO, iov_permanent_bufsize, CTLFLAG_RW,
&fuse_iov_permanent_bufsize, 0, "");
SYSCTL_INT(_vfs_generic_fuse4x_tunables, OID_AUTO, max_freetickets, CTLFLAG_RW,
&fuse_max_freetickets, 0, "");
SYSCTL_INT(_vfs_generic_fuse4x_tunables, OID_AUTO, max_tickets, CTLFLAG_RW,
&fuse_max_tickets, 0, "");
SYSCTL_PROC(_vfs_generic_fuse4x_tunables, // our parent
OID_AUTO, // automatically assign object ID
userkernel_bufsize, // our name
(CTLTYPE_INT | CTLFLAG_WR), // type flag/access flag
&fuse_userkernel_bufsize, // location of our data
0, // argument passed to our handler
sysctl_fuse4x_tunables_userkernel_bufsize_handler,
"I", // our data type (integer)
"fuse4x Tunables"); // our description
/* fuse.version */
SYSCTL_INT(_vfs_generic_fuse4x_version, OID_AUTO, api_major, CTLFLAG_RD,
&fuse_api_major, 0, "");
SYSCTL_INT(_vfs_generic_fuse4x_version, OID_AUTO, api_minor, CTLFLAG_RD,
&fuse_api_minor, 0, "");
SYSCTL_STRING(_vfs_generic_fuse4x_version, OID_AUTO, number, CTLFLAG_RD,
FUSE4X_VERSION, 0, "");
SYSCTL_STRING(_vfs_generic_fuse4x_version, OID_AUTO, string, CTLFLAG_RD,
FUSE4X_STRINGIFY(FUSE4X_KEXT_VERSION) ", " FUSE4X_TIMESTAMP, 0, "");
static struct sysctl_oid *fuse_sysctl_list[] =
{
&sysctl__vfs_generic_fuse4x_control,
&sysctl__vfs_generic_fuse4x_counters,
&sysctl__vfs_generic_fuse4x_resourceusage,
&sysctl__vfs_generic_fuse4x_tunables,
&sysctl__vfs_generic_fuse4x_version,
&sysctl__vfs_generic_fuse4x_control_kill,
#ifdef FUSE4X_ENABLE_MACFUSE_MODE
&sysctl__vfs_generic_fuse4x_control_macfuse_mode,
#endif
&sysctl__vfs_generic_fuse4x_control_print_vnodes,
&sysctl__vfs_generic_fuse4x_counters_filehandle_reuse,
&sysctl__vfs_generic_fuse4x_counters_filehandle_upcalls,
&sysctl__vfs_generic_fuse4x_counters_lookup_cache_hits,
&sysctl__vfs_generic_fuse4x_counters_lookup_cache_misses,
&sysctl__vfs_generic_fuse4x_counters_lookup_cache_overrides,
&sysctl__vfs_generic_fuse4x_counters_memory_reallocs,
&sysctl__vfs_generic_fuse4x_resourceusage_filehandles,
&sysctl__vfs_generic_fuse4x_resourceusage_filehandles_zombies,
&sysctl__vfs_generic_fuse4x_resourceusage_ipc_iovs,
&sysctl__vfs_generic_fuse4x_resourceusage_ipc_tickets,
#ifdef FUSE4X_COUNT_MEMORY
&sysctl__vfs_generic_fuse4x_resourceusage_memory_bytes,
#endif
&sysctl__vfs_generic_fuse4x_resourceusage_mounts,
&sysctl__vfs_generic_fuse4x_resourceusage_vnodes,
&sysctl__vfs_generic_fuse4x_tunables_admin_group,
&sysctl__vfs_generic_fuse4x_tunables_allow_other,
&sysctl__vfs_generic_fuse4x_tunables_iov_credit,
&sysctl__vfs_generic_fuse4x_tunables_iov_permanent_bufsize,
&sysctl__vfs_generic_fuse4x_tunables_max_freetickets,
&sysctl__vfs_generic_fuse4x_tunables_max_tickets,
&sysctl__vfs_generic_fuse4x_tunables_userkernel_bufsize,
&sysctl__vfs_generic_fuse4x_version_api_major,
&sysctl__vfs_generic_fuse4x_version_api_minor,
&sysctl__vfs_generic_fuse4x_version_number,
&sysctl__vfs_generic_fuse4x_version_string,
NULL
};
void
fuse_sysctl_start(void)
{
int i;
sysctl_register_oid(&sysctl__vfs_generic_fuse4x);
for (i = 0; fuse_sysctl_list[i]; i++) {
sysctl_register_oid(fuse_sysctl_list[i]);
}
}
void
fuse_sysctl_stop(void)
{
int i;
for (i = 0; fuse_sysctl_list[i]; i++) {
sysctl_unregister_oid(fuse_sysctl_list[i]);
}
sysctl_unregister_oid(&sysctl__vfs_generic_fuse4x);
#ifdef FUSE4X_ENABLE_MACFUSE_MODE
if (fuse_macfuse_mode) {
fuse4x_macfuse_mode_stop();
}
thread_deallocate(fuse4x_macfuse_mode_thread);
#endif
}