-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathyav_dirimpl_fuse.pyx
531 lines (383 loc) · 14.1 KB
/
yav_dirimpl_fuse.pyx
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
import sys
import time
import traceback
from xv6inode import create_fuse_inode
from llfuse cimport *
from diskimpl cimport Block, Stat, Concat32
from dirinode cimport DirImpl
from libc.stdint cimport uint64_t, int64_t
from libc.stdlib cimport malloc, calloc, free, abort, realloc
from libc.string cimport memcpy, memset, strlen, strcmp
from posix.fcntl cimport S_IFREG, S_IFDIR, S_IFLNK
from posix.types cimport ino_t
from libc.errno cimport ENOENT, ENOTDIR, EISDIR, EACCES, ENOMEM, ENAMETOOLONG, EFBIG
cdef DirImpl inode_obj = None
cdef uint64_t NUM_BLOCKS = 522
cdef uint64_t MAX_NAME_LENGTH = 120
cdef void update_block(Block b, size_t off, const char *buf, size_t size) nogil:
cdef char *bbuf = <char*>b.buf
off = off % 4096
cdef size_t i = 0
for i in range(size):
bbuf[off + i] = buf[i]
#################
cdef void gc():
for oidx in range(inode_obj._orphans.size()):
ino = inode_obj._orphans.index(oidx)
for i in range(inode_obj.get_iattr(ino).bsize + 1, -1, -1):
# print 'gc-ing orphan', i, oidx, ino, inode_obj.get_iattr(ino).bsize
inode_obj.gc1(oidx, i)
inode_obj.gc2(oidx)
inode_obj.gc3()
cdef int mkstat(fuse_ino_t ino, struct_stat *stbuf):
cdef Stat attr = inode_obj.get_iattr(ino)
# print 'mkstat ino={}, attr={}'.format(ino, attr)
if attr.mode == 0:
return -1
stbuf.st_ino = ino
if attr.mode & S_IFDIR != 0:
stbuf.st_nlink = 2
else:
stbuf.st_nlink = 1
stbuf.st_mode = attr.mode
stbuf.st_size = attr.fsize
if stbuf.st_size >= (10 * 4096):
stbuf.st_size -= 4096
stbuf.st_mtime = attr.mtime
return 0
cdef void ll_getattr(fuse_req_t req, fuse_ino_t ino, fuse_file_info *fi):
# print "getattr ino={}".format(ino)
cdef struct_stat stbuf
memset(&stbuf, 0, sizeof(stbuf))
if mkstat(ino, &stbuf) == -1:
fuse_reply_err(req, ENOENT)
else:
fuse_reply_attr(req, &stbuf, 1.0)
cdef void ll_setattr(fuse_req_t req, fuse_ino_t ino, struct_stat *attr,
int to_set, fuse_file_info *fi):
# print "setattr ino={}, st_mode={}, st_size={}, st_mtime={}, oldattr={}".format(ino,
# attr[0].st_mode,
# attr[0].st_size,
# attr[0].st_mtime,
# iattr)
cdef Stat iattr = inode_obj.get_iattr(ino)
cdef uint64_t off, startoff, endoff, oldsize, newsize
if to_set & FUSE_SET_ATTR_SIZE:
oldsize = iattr.fsize
newsize = attr[0].st_size
if newsize >= 4096 * 10:
newsize += 4096
if oldsize > newsize:
inode_obj.truncate(ino, newsize)
iattr = inode_obj.get_iattr(ino)
if to_set & FUSE_SET_ATTR_MTIME:
iattr.mtime = attr[0].st_mtime
if to_set & FUSE_SET_ATTR_MODE:
iattr.mode = attr[0].st_mode
inode_obj.set_iattr(ino, iattr)
cdef struct_stat stbuf
memset(&stbuf, 0, sizeof(stbuf))
mkstat(ino, &stbuf)
fuse_reply_attr(req, &stbuf, 1.0)
cdef void ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name):
# print "lookup parent={}, name={}".format(parent, name)
cdef size_t namelen = strlen(name)
if namelen > MAX_NAME_LENGTH:
fuse_reply_err(req, ENOENT)
return
cdef fuse_entry_param e
memset(&e, 0, sizeof(e))
cdef uint64_t[15] pname
memset(&pname, 0, sizeof(uint64_t) * 15)
memcpy(&pname, name, namelen)
t = inode_obj.lookup(parent, pname)
if t is None:
fuse_reply_err(req, ENOENT)
return
e.ino = t
e.attr_timeout = 1.0
e.entry_timeout = 1.0
mkstat(e.ino, &e.attr)
fuse_reply_entry(req, &e)
cdef struct dirbuf:
char *p;
size_t size
cdef void dirbuf_add(fuse_req_t req, dirbuf *b, const char *name, fuse_ino_t ino):
cdef struct_stat stbuf
cdef size_t oldsize = b.size
b.size += fuse_add_direntry(req, NULL, 0, name, NULL, 0)
cdef char *newp = <char*>realloc(<void*>b.p, b.size)
if newp == NULL:
print "memory error, could not realloc buffer"
abort()
b.p = newp
memset(&stbuf, 0, sizeof(stbuf))
stbuf.st_ino = ino
fuse_add_direntry(req, b.p + oldsize, b.size - oldsize, name, &stbuf, b.size)
cdef int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize, off_t off, size_t maxsize):
if off < bufsize:
return fuse_reply_buf(req, buf + off, min(bufsize - off, maxsize))
else:
fuse_reply_buf(req, NULL, 0)
cdef void ll_readdir(fuse_req_t req, fuse_ino_t ino,
size_t size, off_t off, fuse_file_info* fi):
# print "readdir ino={} size={} off={}".format(ino, size, off)
cdef dirbuf b
cdef Block block
cdef uint64_t i, ioff, boff, fileino
cdef uint64_t[16] name
cdef bint mapped
memset(&name, 0, sizeof(uint64_t) * 16)
boff = 0
iattr = inode_obj.get_iattr(ino)
if ino != 1 and iattr.mode & S_IFDIR == 0:
fuse_reply_err(req, ENOTDIR)
else:
memset(&b, 0, sizeof(b))
dirbuf_add(req, &b, ".", ino)
dirbuf_add(req, &b, "..", ino)
for ioff in range(522):
if ioff == 10:
continue
is_mapped = inode_obj._inode.is_mapped(Concat32(ino, ioff))
if not is_mapped:
break
block = inode_obj.read(ino, ioff)
for i in range(0, 512, 16):
fileino = block.get(i)
if fileino != 0:
memcpy(&name, (<char*>block.buf) + (i + 1) * 8, MAX_NAME_LENGTH)
dirbuf_add(req, &b, <char*>&name, fileino)
if b.size > size + off:
break
reply_buf_limited(req, b.p, b.size, off, size)
free(b.p)
cdef void ll_open(fuse_req_t req, fuse_ino_t ino, fuse_file_info *fi):
iattr = inode_obj.get_iattr(ino)
# print "open ino={} attr={}".format(ino, iattr)
if iattr.mode & S_IFDIR != 0:
fuse_reply_err(req, EISDIR)
# TODO: Check permission?
# else ...:
# fuse_reply_err(req, EACCES)
else:
fuse_reply_open(req, fi)
cdef void ll_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, fuse_file_info *fi):
print 'read ino={}, size={}, off={}'.format(ino, size, off)
cdef Stat iattr = inode_obj.get_iattr(ino)
cdef uint64_t bufsize, fsize = iattr.fsize
if fsize / 4096 == off / 4096:
bufsize = fsize % 4096
else:
bufsize = 4096
bufsize = min(bufsize, size)
cdef uint64_t boff = off / 4096
if boff >= 10:
boff += 1
cdef Block block = inode_obj.read(ino, boff)
reply_buf_limited(req, <char*>block.buf, bufsize, 0, bufsize)
cdef (uint64_t, uint64_t) mknod(fuse_ino_t parent, const char *name, mode_t mode, dev_t rdev):
cdef size_t namelen = strlen(name)
if namelen > MAX_NAME_LENGTH:
return (0, ENAMETOOLONG)
cdef uint64_t[15] pname
memset(&pname, 0, sizeof(uint64_t) * 15)
memcpy(&pname, name, namelen)
return inode_obj.mknod(parent, pname, mode, int(time.time()))
cdef void ll_mknod(fuse_req_t req, fuse_ino_t parent, const char *name,
mode_t mode, dev_t rdev):
# print 'mknod parent={}, name={}, mode={}, rdev={}'.format(parent, name, mode, rdev)
v = mknod(parent, name, mode, rdev)
cdef int64_t ino, err
ino = v[0]
err = v[1]
if err != 0:
fuse_reply_err(req, err)
return
cdef fuse_entry_param e
memset(&e, 0, sizeof(e))
e.ino = ino
e.attr_timeout = 1.0
e.entry_timeout = 1.0
mkstat(e.ino, &e.attr)
fuse_reply_entry(req, &e)
cdef void ll_rename(fuse_req_t req, fuse_ino_t oldparent, const char *oldname,
fuse_ino_t newparent, const char *newname):
# print 'rename oldparent={}, oldname={}, newparent={}, newname={}'.format(
# oldparent, oldname, newparent, newname)
cdef size_t oldnamelen = strlen(oldname)
cdef size_t newnamelen = strlen(newname)
if oldnamelen > MAX_NAME_LENGTH:
fuse_reply_err(req, ENOENT)
return
if newnamelen > MAX_NAME_LENGTH:
fuse_reply_err(req, ENAMETOOLONG)
return
cdef uint64_t[15] poldname
cdef uint64_t[15] pnewname
memset(&poldname, 0, sizeof(uint64_t) * 15)
memcpy(&poldname, oldname, oldnamelen)
memset(&pnewname, 0, sizeof(uint64_t) * 15)
memcpy(&pnewname, newname, newnamelen)
ret = inode_obj.rename(oldparent, poldname, newparent, pnewname)
fuse_reply_err(req, ret)
cdef void ll_write(fuse_req_t req, fuse_ino_t ino, const char *buf,
size_t size, off_t off, fuse_file_info *fi):
# print "write off={}, size={}".format(off, size)
# print (off % 4096) + size
cdef uint64_t boff = off / 4096;
if boff >= 10:
boff += 1
cdef Block b = inode_obj.read(ino, boff)
update_block(b, off, buf, size)
if boff >= NUM_BLOCKS:
fuse_reply_err(req, EFBIG)
return
cdef size_t written = inode_obj.write(ino, boff, b, off % 4096 + size)
if written == 0:
fuse_reply_write(req, 0)
return
else:
fuse_reply_write(req, size)
return
cdef void ll_mkdir(fuse_req_t req, fuse_ino_t parent,
const char *name, mode_t mode):
# print 'mkdir parent={}, name={}, mode={}'.format(parent, name, parent)
ll_mknod(req, parent, name, S_IFDIR | mode, 0)
cdef void ll_create(fuse_req_t req, fuse_ino_t parent,
const char *name, mode_t mode, fuse_file_info *fi):
# print 'create parent={}, name={}, mode={}'.format(parent, name, parent)
cdef (uint64_t, uint64_t) res = mknod(parent, name, mode, 0)
if res[1] != 0:
fuse_reply_err(req, res[1])
return
cdef fuse_entry_param e
memset(&e, 0, sizeof(e))
e.ino = res[0]
e.attr_timeout = 1.0
e.entry_timeout = 1.0
mkstat(e.ino, &e.attr)
fuse_reply_create(req, &e, fi)
cdef void ll_unlink(fuse_req_t req, fuse_ino_t parent, const char *name):
# print 'unlink parent={}, name={}'.format(parent, name)
cdef size_t namelen = strlen(name)
if namelen > MAX_NAME_LENGTH:
fuse_reply_err(req, ENOENT)
return
cdef uint64_t[15] pname
memset(&pname, 0, sizeof(uint64_t) * 15)
memcpy(&pname, name, namelen)
ret = inode_obj.unlink(parent, pname)
fuse_reply_err(req, 0)
cdef void ll_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name):
# print 'rmdir parent={}, name={}'.format(parent, name)
cdef size_t namelen = strlen(name)
if namelen > MAX_NAME_LENGTH:
fuse_reply_err(req, ENOENT)
return
cdef uint64_t[15] pname
memset(&pname, 0, sizeof(uint64_t) * 15)
memcpy(&pname, name, namelen)
cdef (uint64_t, uint64_t) res = inode_obj.rmdir(parent, pname)
fuse_reply_err(req, res[1])
cdef void ll_forget(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup):
print 'forget ino={}, nlookup={}'.format(ino, nlookup)
inode_obj.forget(ino)
gc()
fuse_reply_none(req)
cdef void ll_symlink(fuse_req_t req, const char *link, fuse_ino_t parent,
const char *name):
# print 'symlink link={}, parent={}, name={}'.format(link, parent, name)
v = mknod(parent, name, 0777 | S_IFLNK, 0)
cdef uint64_t ino = v[0]
cdef uint64_t err = v[1]
if err != 0:
fuse_reply_err(req, err)
return
# inode_obj.bunmap(ino, 0)
cdef Block b = inode_obj.read(ino, 0)
update_block(b, 0, link, strlen(link))
inode_obj.write(ino, 0, b, strlen(link))
cdef fuse_entry_param e
memset(&e, 0, sizeof(e))
e.ino = ino
e.attr_timeout = 1.0
e.entry_timeout = 1.0
mkstat(e.ino, &e.attr)
fuse_reply_entry(req, &e)
cdef void ll_readlink(fuse_req_t req, fuse_ino_t ino):
# print 'readlink ino={}'.format(ino)
cdef Block b = inode_obj.read(ino, 0)
fuse_reply_readlink(req, <char*>b.buf)
cdef void ll_fsync(fuse_req_t req, fuse_ino_t ino,
int datasync, fuse_file_info *fi):
# print "fsync ino={} datasync={}".format(ino, datasync)
inode_obj.fsync()
fuse_reply_err(req, 0)
cdef void ll_fsyncdir(fuse_req_t req, fuse_ino_t ino,
int datasync, fuse_file_info *fi):
# print "fsyncdir ino={} datasync={}".format(ino, datasync)
inode_obj.fsync()
fuse_reply_err(req, 0)
def main():
global inode_obj
if '--' in sys.argv:
fargs = sys.argv[sys.argv.index('--') + 1:]
sys.argv = sys.argv[:sys.argv.index('--')]
else:
fargs = []
inode_obj = create_fuse_inode(fargs)
cdef int argc = len(sys.argv)
cdef char** argv = <char**>malloc(argc * sizeof(char**))
if argv == NULL:
print "Malloc: Memory error. Could not allocate"
abort()
for i in range(argc):
argv[i] = sys.argv[i]
cmain(argc, argv)
cdef void ll_init(void *userdata, fuse_conn_info *conn):
pass
cdef int cmain(int argc, char **argv):
cdef fuse_args args
args.argc = argc
args.argv = argv
args.allocated = 0
cdef fuse_chan *ch
cdef char* mountpoint = NULL
cdef int err = -1
cdef fuse_session *se
cdef fuse_lowlevel_ops ops
memset(<void*>&ops, 0, sizeof(ops))
# ops.init = &ll_init
ops.lookup = &ll_lookup
ops.getattr = &ll_getattr
ops.setattr = &ll_setattr
ops.readdir = &ll_readdir
ops.open = &ll_open
ops.read = &ll_read
ops.write = &ll_write
ops.mknod = &ll_mknod
ops.create = &ll_create
ops.mkdir = &ll_mkdir
ops.rename = &ll_rename
ops.unlink = &ll_unlink
ops.rmdir = &ll_rmdir
ops.forget = &ll_forget
ops.fsync = &ll_fsync
ops.fsyncdir = &ll_fsyncdir
ops.symlink = &ll_symlink
ops.readlink = &ll_readlink
if fuse_parse_cmdline(&args, &mountpoint, NULL, NULL) != -1:
ch = fuse_mount(mountpoint, &args)
if ch != NULL:
se = fuse_lowlevel_new(&args, &ops, sizeof(ops), NULL)
if se != NULL:
if fuse_set_signal_handlers(se) != -1:
fuse_session_add_chan(se, ch)
err = fuse_session_loop(se)
fuse_remove_signal_handlers(se)
fuse_session_remove_chan(ch)
fuse_session_destroy(se)
fuse_unmount(mountpoint, ch)
fuse_opt_free_args(&args)
return err