-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuse_media.c
241 lines (184 loc) · 7.3 KB
/
fuse_media.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
/*
* fuse_media eBPF program
*
* Copyright (C) 2021 Google
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version
* 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <bpf_helpers.h>
#include <stdint.h>
#define __KERNEL__
#include <fuse_kernel.h>
#define bpf_printk(fmt, ...) \
({ \
char ____fmt[] = fmt; \
bpf_trace_printk(____fmt, sizeof(____fmt), ##__VA_ARGS__); \
})
DEFINE_BPF_PROG("fuse/media", AID_ROOT, AID_MEDIA_RW, fuse_media)
(struct fuse_args* fa) {
switch (fa->opcode) {
case FUSE_LOOKUP | FUSE_PREFILTER: {
const char* name = fa->in_args[0].value;
bpf_printk("LOOKUP: %lx %s", fa->nodeid, name);
if (fa->nodeid == 1)
return FUSE_BPF_USER_FILTER | FUSE_BPF_BACKING;
else
return FUSE_BPF_BACKING;
}
/* FUSE_FORGET */
case FUSE_GETATTR | FUSE_PREFILTER: {
const struct fuse_getattr_in* fgi = fa->in_args[0].value;
bpf_printk("GETATTR: %d", fgi->fh);
return FUSE_BPF_BACKING;
}
case FUSE_SETATTR | FUSE_PREFILTER: {
const struct fuse_setattr_in* fsi = fa->in_args[0].value;
bpf_printk("SETATTR: %d", fsi->fh);
return FUSE_BPF_BACKING;
}
/* FUSE_READLINK */
/* FUSE_SYMLINK */
case FUSE_MKNOD | FUSE_PREFILTER: {
const struct fuse_mknod_in* fmi = fa->in_args[0].value;
const char* name = fa->in_args[1].value;
bpf_printk("MKNOD: %s %x %x", name, fmi->rdev | fmi->mode, fmi->umask);
return FUSE_BPF_BACKING;
}
case FUSE_MKDIR | FUSE_PREFILTER: {
const struct fuse_mkdir_in* fmi = fa->in_args[0].value;
const char* name = fa->in_args[1].value;
bpf_printk("MKDIR: %s %x %x", name, fmi->mode, fmi->umask);
return FUSE_BPF_BACKING;
}
case FUSE_UNLINK | FUSE_PREFILTER: {
const char* name = fa->in_args[0].value;
bpf_printk("UNLINK: %s", name);
return FUSE_BPF_BACKING;
}
case FUSE_RMDIR | FUSE_PREFILTER: {
const char* name = fa->in_args[0].value;
bpf_printk("RMDIR: %s", name);
return FUSE_BPF_BACKING;
}
case FUSE_RENAME | FUSE_PREFILTER: {
const char* name_old = fa->in_args[1].value;
const char* name_new = fa->in_args[2].value;
bpf_printk("RENAME: %s to %s", name_old, name_new);
return FUSE_BPF_BACKING;
}
case FUSE_LINK | FUSE_PREFILTER: {
const struct fuse_link_in* fli = fa->in_args[0].value;
const char* dst_name = fa->in_args[1].value;
bpf_printk("LINK: %d %s", fli->oldnodeid, dst_name);
return FUSE_BPF_BACKING;
}
case FUSE_OPEN | FUSE_PREFILTER: {
bpf_printk("OPEN: %d", fa->nodeid);
return FUSE_BPF_BACKING;
}
case FUSE_READ | FUSE_PREFILTER: {
const struct fuse_read_in* fri = fa->in_args[0].value;
bpf_printk("READ: fh: %lu, offset %lu, size %lu", fri->fh, fri->offset, fri->size);
return FUSE_BPF_BACKING;
}
case FUSE_WRITE | FUSE_PREFILTER: {
const struct fuse_write_in* fwi = fa->in_args[0].value;
bpf_printk("WRITE: fh: %lu, offset %lu, size %lu", fwi->fh, fwi->offset, fwi->size);
return FUSE_BPF_BACKING;
}
/* FUSE_STATFS */
case FUSE_RELEASE | FUSE_PREFILTER: {
const struct fuse_release_in* fri = fa->in_args[0].value;
bpf_printk("RELEASE: %d", fri->fh);
return FUSE_BPF_BACKING;
}
/* FUSE_FSYNC */
case FUSE_SETXATTR | FUSE_PREFILTER: {
const char* name = fa->in_args[1].value;
bpf_printk("SETXATTR: %d %s", fa->nodeid, name);
return FUSE_BPF_BACKING;
}
case FUSE_GETXATTR | FUSE_PREFILTER: {
const char* name = fa->in_args[1].value;
bpf_printk("GETXATTR: %d %s", fa->nodeid, name);
return FUSE_BPF_BACKING;
}
case FUSE_LISTXATTR | FUSE_PREFILTER: {
const char* name = fa->in_args[1].value;
bpf_printk("LISTXATTR: %d %s", fa->nodeid, name);
return FUSE_BPF_BACKING;
}
/* FUSE_REMOVEXATTR */
case FUSE_FLUSH | FUSE_PREFILTER: {
const struct fuse_flush_in* ffi = fa->in_args[0].value;
bpf_printk("FLUSH: %d", ffi->fh);
return FUSE_BPF_BACKING;
}
/* FUSE_INIT */
case FUSE_OPENDIR | FUSE_PREFILTER: {
bpf_printk("OPENDIR: %d", fa->nodeid);
return FUSE_BPF_BACKING;
}
case FUSE_READDIR | FUSE_PREFILTER: {
const struct fuse_read_in* fri = fa->in_args[0].value;
bpf_printk("READDIR: fh: %lu", fri->fh, fri->offset);
return FUSE_BPF_BACKING;
}
case FUSE_RELEASEDIR | FUSE_PREFILTER: {
const struct fuse_release_in* fri = fa->in_args[0].value;
bpf_printk("RELEASEDIR: %d", fri->fh);
return FUSE_BPF_BACKING;
}
/* FUSE_FSYNCDIR */
/* FUSE_GETLK */
/* FUSE_SETLK */
/* FUSE_SETLKW */
case FUSE_ACCESS | FUSE_PREFILTER: {
bpf_printk("ACCESS: %d", fa->nodeid);
return FUSE_BPF_BACKING;
}
case FUSE_CREATE | FUSE_PREFILTER: {
bpf_printk("CREATE: %s", fa->in_args[1].value);
return FUSE_BPF_BACKING;
}
/* FUSE_INTERRUPT */
/* FUSE_BMAP */
/* FUSE_DESTROY */
/* FUSE_IOCTL */
/* FUSE_POLL */
/* FUSE_NOTIFY_REPLY */
/* FUSE_BATCH_FORGET */
case FUSE_FALLOCATE | FUSE_PREFILTER: {
const struct fuse_fallocate_in* ffa = fa->in_args[0].value;
bpf_printk("FALLOCATE: %d %lu", ffa->fh, ffa->length);
return FUSE_BPF_BACKING;
}
/* FUSE_READDIRPLUS */
/* FUSE_RENAME2 */
/* FUSE_LSEEK */
/* FUSE_COPY_FILE_RANGE */
/* CUSE_INIT */
case FUSE_CANONICAL_PATH | FUSE_PREFILTER: {
bpf_printk("CANONICAL_PATH: %d", fa->nodeid);
return FUSE_BPF_BACKING;
}
default:
if (fa->opcode & FUSE_PREFILTER)
bpf_printk("Prefilter *** UNKNOWN *** opcode: %d", fa->opcode & FUSE_OPCODE_FILTER);
else if (fa->opcode & FUSE_POSTFILTER)
bpf_printk("Postfilter *** UNKNOWN *** opcode: %d",
fa->opcode & FUSE_OPCODE_FILTER);
else
bpf_printk("*** UNKNOWN *** opcode: %d", fa->opcode);
return FUSE_BPF_BACKING;
}
}
LICENSE("GPL");