forked from fuse4x/kext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuse_file.h
174 lines (146 loc) · 3.81 KB
/
fuse_file.h
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
/*
* Copyright (C) 2006-2008 Google. All Rights Reserved.
* Amit Singh <singh@>
*/
#ifndef _FUSE_FILE_H_
#define _FUSE_FILE_H_
#include <sys/fcntl.h>
#include <sys/kauth.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/vnode.h>
typedef enum fufh_type {
FUFH_INVALID = -1,
FUFH_RDONLY = 0,
FUFH_WRONLY = 1,
FUFH_RDWR = 2,
FUFH_MAXTYPE = 3,
} fufh_type_t;
struct fuse_filehandle {
uint64_t fh_id;
int32_t open_count;
int32_t open_flags;
int32_t fuse_open_flags;
int32_t aux_count;
};
typedef struct fuse_filehandle * fuse_filehandle_t;
#define FUFH_IS_VALID(f) ((f)->open_count > 0)
#define FUFH_USE_INC(f) ((f)->open_count++)
#define FUFH_USE_DEC(f) ((f)->open_count--)
#define FUFH_USE_RESET(f) ((f)->open_count = 0)
#define FUFH_AUX_INC(f) ((f)->aux_count++)
static __inline__
fufh_type_t
fuse_filehandle_xlate_from_mmap(int fflags)
{
if (fflags & PROT_WRITE) {
if (fflags & (PROT_READ | PROT_EXEC)) {
return FUFH_RDWR;
} else {
return FUFH_WRONLY;
}
} else if (fflags & (PROT_READ | PROT_EXEC)) {
return FUFH_RDONLY;
} else {
log("fuse4x: mmap being attempted with no region accessibility\n");
return FUFH_INVALID;
}
}
static __inline__
fufh_type_t
fuse_filehandle_xlate_from_fflags(int fflags)
{
if ((fflags & FREAD) && (fflags & FWRITE)) {
return FUFH_RDWR;
} else if (fflags & (FWRITE)) {
return FUFH_WRONLY;
} else if (fflags & (FREAD)) {
return FUFH_RDONLY;
} else {
/*
* Looks like there might be a code path in Apple's
* IOHDIXController/AppleDiskImagesFileBackingStore
* that calls vnode_open() with a 0 fmode argument.
* Translate 0 to FREAD, which is most likely what
* that kext intends to do anyway. Lets hope the
* calls to VNOP_OPEN and VNOP_CLOSE do match up
* even with this fudging.
*/
if (fflags == 0) {
return FUFH_RDONLY;
} else {
panic("fuse4x: What kind of a flag is this (%x)?", fflags);
}
}
return FUFH_INVALID;
}
static __inline__
int
fuse_filehandle_xlate_to_oflags(fufh_type_t type)
{
int oflags = -1;
switch (type) {
case FUFH_RDONLY:
oflags = O_RDONLY;
break;
case FUFH_WRONLY:
oflags = O_WRONLY;
break;
case FUFH_RDWR:
oflags = O_RDWR;
break;
default:
break;
}
return oflags;
}
/*
* 0 return => can proceed
*/
static __inline__
int
fuse_filehandle_preflight_status(vnode_t vp, vnode_t dvp, vfs_context_t context,
fufh_type_t fufh_type)
{
vfs_context_t icontext = context;
kauth_action_t action = 0;
mount_t mp = vnode_mount(vp);
int err = 0;
if (vfs_authopaque(mp) || !vfs_issynchronous(mp) || !vnode_isreg(vp)) {
goto out;
}
#if M_FUSE4X_ENABLE_UNSUPPORTED
if (!icontext) {
icontext = vfs_context_current();
}
#endif /* M_FUSE4X_ENABLE_UNSUPPORTED */
if (!icontext) {
goto out;
}
switch (fufh_type) {
case FUFH_RDONLY:
action |= KAUTH_VNODE_READ_DATA;
break;
case FUFH_WRONLY:
action |= KAUTH_VNODE_WRITE_DATA;
break;
case FUFH_RDWR:
action |= (KAUTH_VNODE_READ_DATA | KAUTH_VNODE_WRITE_DATA);
break;
default:
err = EINVAL;
break;
}
if (!err) {
err = vnode_authorize(vp, dvp, action, icontext);
}
out:
return err;
}
int fuse_filehandle_get(vnode_t vp, vfs_context_t context,
fufh_type_t fufh_type, int mode);
int fuse_filehandle_put(vnode_t vp, vfs_context_t context,
fufh_type_t fufh_type);
#endif /* _FUSE_FILE_H_ */