-
Notifications
You must be signed in to change notification settings - Fork 7
/
fuse_main.c
172 lines (137 loc) · 3.84 KB
/
fuse_main.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
/*
* Copyright (C) 2006-2008 Google. All Rights Reserved.
* Copyright (C) 2010 Tuxera. All Rights Reserved.
* Copyright (C) 2011 Anatol Pomozov. All Rights Reserved.
*/
#include "fuse.h"
#include "fuse_device.h"
#include "fuse_ipc.h"
#include "fuse_locking.h"
#include "fuse_node.h"
#include "fuse_sysctl.h"
#include <fuse_mount.h>
#include <libkern/libkern.h>
#include <libkern/OSMalloc.h>
#include <libkern/locks.h>
#include <mach/mach_types.h>
#include <miscfs/devfs/devfs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
OSMallocTag fuse_malloc_tag = NULL;
extern struct vfs_fsentry fuse_vfs_entry;
extern vfstable_t fuse_vfs_table_ref;
kern_return_t fusefs_start(kmod_info_t *ki, void *d);
kern_return_t fusefs_stop(kmod_info_t *ki, void *d);
static void
fini_stuff(void)
{
if (fuse_device_mutex) {
lck_mtx_free(fuse_device_mutex, fuse_lock_group);
fuse_device_mutex = NULL;
}
#ifdef FUSE4X_SERIALIZE_LOGGING
if (fuse_log_lock) {
lck_mtx_free(fuse_log_lock, fuse_lock_group);
fuse_log_lock = NULL;
}
#endif /* FUSE4X_SERIALIZE_LOGGING */
if (fuse_lock_group) {
lck_grp_free(fuse_lock_group);
fuse_lock_group = NULL;
}
if (fuse_malloc_tag) {
OSMalloc_Tagfree(fuse_malloc_tag);
fuse_malloc_tag = NULL;
}
if (fuse_lock_attr) {
lck_attr_free(fuse_lock_attr);
fuse_lock_attr = NULL;
}
if (fuse_group_attr) {
lck_grp_attr_free(fuse_group_attr);
fuse_group_attr = NULL;
}
}
static kern_return_t
init_stuff(void)
{
kern_return_t ret = KERN_SUCCESS;
fuse_malloc_tag = OSMalloc_Tagalloc(FUSE4X_BUNDLE_IDENTIFIER,
OSMT_DEFAULT);
if (fuse_malloc_tag == NULL) {
ret = KERN_FAILURE;
}
fuse_lock_attr = lck_attr_alloc_init();
fuse_group_attr = lck_grp_attr_alloc_init();
lck_attr_setdebug(fuse_lock_attr);
if (ret == KERN_SUCCESS) {
fuse_lock_group = lck_grp_alloc_init(FUSE4X_BUNDLE_IDENTIFIER,
fuse_group_attr);
if (fuse_lock_group == NULL) {
ret = KERN_FAILURE;
}
}
if (ret == KERN_SUCCESS) {
fuse_device_mutex = lck_mtx_alloc_init(fuse_lock_group, fuse_lock_attr);
if (fuse_device_mutex == NULL) {
ret = ENOMEM;
}
}
#ifdef FUSE4X_SERIALIZE_LOGGING
if (ret == KERN_SUCCESS) {
fuse_log_lock = lck_mtx_alloc_init(fuse_lock_group, fuse_lock_attr);
if (fuse_log_lock == NULL) {
ret = ENOMEM;
}
}
#endif /* FUSE4X_SERIALIZE_LOGGING */
if (ret != KERN_SUCCESS) {
fini_stuff();
}
return ret;
}
kern_return_t
fusefs_start(__unused kmod_info_t *ki, __unused void *d)
{
int ret;
ret = init_stuff();
if (ret != KERN_SUCCESS) {
return KERN_FAILURE;
}
ret = vfs_fsadd(&fuse_vfs_entry, &fuse_vfs_table_ref);
if (ret != 0) {
fuse_vfs_table_ref = NULL;
goto error;
}
ret = fuse_devices_start();
if (ret != KERN_SUCCESS) {
goto error;
}
fuse_sysctl_start();
log("fuse4x: starting (version %s, %s)\n", FUSE4X_STRINGIFY(FUSE4X_KEXT_VERSION), FUSE4X_TIMESTAMP);
return KERN_SUCCESS;
error:
if (fuse_vfs_table_ref) {
(void)vfs_fsremove(fuse_vfs_table_ref);
}
fini_stuff();
return KERN_FAILURE;
}
kern_return_t
fusefs_stop(__unused kmod_info_t *ki, __unused void *d)
{
int ret;
ret = fuse_devices_stop();
if (ret != KERN_SUCCESS) {
return KERN_FAILURE;
}
ret = vfs_fsremove(fuse_vfs_table_ref);
if (ret != KERN_SUCCESS) {
return KERN_FAILURE;
}
fini_stuff();
fuse_sysctl_stop();
IOLog("fuse4x: stopping (version %s, %s)\n", FUSE4X_STRINGIFY(FUSE4X_KEXT_VERSION), FUSE4X_TIMESTAMP);
return KERN_SUCCESS;
}