-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuse.cc
623 lines (551 loc) · 17.2 KB
/
fuse.cc
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
* receive request from fuse and call methods of chfs_client
*
* started life as low-level example in the fuse distribution. we
* have to use low-level interface in order to get i-numbers. the
* high-level interface only gives us complete paths.
*/
#include <fuse_lowlevel.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "lang/verify.h"
#include "chfs_client.h"
int myid;
chfs_client *chfs;
int id() {
return myid;
}
//
// A file/directory's attributes are a set of information
// including owner, permissions, size, &c. The information is
// much the same as that returned by the stat() system call.
// The kernel needs attributes in many situations, and some
// fuse functions (such as lookup) need to return attributes
// as well as other information, so getattr() gets called a lot.
//
// ChFS fakes most of the attributes. It does provide more or
// less correct values for the access/modify/change times
// (atime, mtime, and ctime), and correct values for file sizes.
//
chfs_client::status
getattr(chfs_client::inum inum, struct stat &st)
{
chfs_client::status ret;
bzero(&st, sizeof(st));
st.st_ino = inum;
printf("getattr %016llx %d\n", inum, chfs->isfile(inum));
if(chfs->isfile(inum)){
chfs_client::fileinfo info;
ret = chfs->getfile(inum, info);
if(ret != chfs_client::OK)
return ret;
st.st_mode = S_IFREG | 0666;
st.st_nlink = 1;
st.st_atime = info.atime;
st.st_mtime = info.mtime;
st.st_ctime = info.ctime;
st.st_size = info.size;
printf(" getattr -> %llu\n", info.size);
} else if (chfs->isdir(inum)) {
chfs_client::dirinfo info;
ret = chfs->getdir(inum, info);
if(ret != chfs_client::OK)
return ret;
st.st_mode = S_IFDIR | 0777;
st.st_nlink = 2;
st.st_atime = info.atime;
st.st_mtime = info.mtime;
st.st_ctime = info.ctime;
printf(" getattr -> %lu %lu %lu\n", info.atime, info.mtime, info.ctime);
} else {
chfs_client::fileinfo info;
ret = chfs->getfile(inum, info);
if(ret != chfs_client::OK)
return ret;
st.st_mode = S_IFLNK | 0777;
st.st_nlink = 1;
st.st_atime = info.atime;
st.st_mtime = info.mtime;
st.st_ctime = info.ctime;
st.st_size = info.size;
printf(" getattr -> link %llu\n", info.size);
}
return chfs_client::OK;
}
//
// This is a typical fuse operation handler; you'll be writing
// a bunch of handlers like it.
//
// A handler takes some arguments
// and supplies either a success or failure response. It provides
// an error response by calling either fuse_reply_err(req, errno), and
// a normal response by calling ruse_reply_xxx(req, ...). The req
// argument serves to link up this response with the original
// request; just pass the same @req that was passed into the handler.
//
// The @ino argument indicates the file or directory FUSE wants
// you to operate on. It's a 32-bit FUSE identifier; just assign
// it to a chfs_client::inum to get a 64-bit ChFS inum.
//
void
fuseserver_getattr(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi)
{
struct stat st;
chfs_client::inum inum = ino; // req->in.h.nodeid;
chfs_client::status ret;
ret = getattr(inum, st);
if(ret != chfs_client::OK){
fuse_reply_err(req, ENOENT);
return;
}
fuse_reply_attr(req, &st, 0);
}
//
// Set the attributes of a file. Often used as part of overwriting
// a file, to set the file length to zero.
//
// to_set is a bitmap indicating which attributes to set. You only
// have to implement the FUSE_SET_ATTR_SIZE bit, which indicates
// that the size of the file should be changed. The new size is
// in attr->st_size. If the new size is bigger than the current
// file size, fill the new bytes with '\0'.
//
// On success, call fuse_reply_attr, passing the file's new
// attributes (from a call to getattr()).
//
void
fuseserver_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
int to_set, struct fuse_file_info *fi)
{
printf("fuseserver_setattr 0x%x\n", to_set);
if (FUSE_SET_ATTR_SIZE & to_set) {
printf(" fuseserver_setattr set size to %zu\n", attr->st_size);
struct stat st;
#if 1
// Change the above line to "#if 1", and your code goes here
// Note: fill st using getattr before fuse_reply_attr
if (to_set & FUSE_SET_ATTR_SIZE) {
chfs->setattr(ino, attr->st_size);
}
getattr(ino, st);
fuse_reply_attr(req, &st, 0);
#else
fuse_reply_err(req, ENOSYS);
#endif
} else {
fuse_reply_err(req, ENOSYS);
}
}
//
// Read up to @size bytes starting at byte offset @off in file @ino.
//
// Pass the number of bytes actually read to fuse_reply_buf.
// If there are fewer than @size bytes to read between @off and the
// end of the file, read just that many bytes. If @off is greater
// than or equal to the size of the file, read zero bytes.
//
// Ignore @fi.
// @req identifies this request, and is used only to send a
// response back to fuse with fuse_reply_buf or fuse_reply_err.
//
void
fuseserver_read(fuse_req_t req, fuse_ino_t ino, size_t size,
off_t off, struct fuse_file_info *fi)
{
#if 1
// Change the above "#if 0" to "#if 1", and your code goes here
std::string buf;
int r;
if ((r = chfs->read(ino, size, off, buf)) == chfs_client::OK) {
fuse_reply_buf(req, buf.data(), buf.size());
} else {
fuse_reply_err(req, ENOENT);
}
#else
fuse_reply_err(req, ENOSYS);
#endif
}
//
// Write @size bytes from @buf to file @ino, starting
// at byte offset @off in the file.
//
// If @off + @size is greater than the current size of the
// file, the write should cause the file to grow. If @off is
// beyond the end of the file, fill the gap with null bytes.
//
// Set the file's mtime to the current time.
//
// Ignore @fi.
//
// @req identifies this request, and is used only to send a
// response back to fuse with fuse_reply_write or fuse_reply_err.
//
void
fuseserver_write(fuse_req_t req, fuse_ino_t ino,
const char *buf, size_t size, off_t off,
struct fuse_file_info *fi)
{
#if 1
// Change the above line to "#if 1", and your code goes here
int r;
if ((r = chfs->write(ino, size, off, buf, size)) == chfs_client::OK) {
fuse_reply_write(req, size);
} else {
fuse_reply_err(req, ENOENT);
}
#else
fuse_reply_err(req, ENOSYS);
#endif
}
//
// Create file @name in directory @parent.
//
// - @mode specifies the create mode of the file. Ignore it - you do not
// have to implement file mode.
// - If a file named @name already exists in @parent, return EXIST.
// - Pick an ino (with type of chfs_client::inum) for file @name.
// Make sure ino indicates a file, not a directory!
// - Create an empty extent for ino.
// - Add a <name, ino> entry into @parent.
// - Change the parent's mtime and ctime to the current time/date
// (this may fall naturally out of your extent server code).
// - On success, store the inum of newly created file into @e->ino,
// and the new file's attribute into @e->attr. Get the file's
// attributes with getattr().
//
// @return chfs_client::OK on success, and EXIST if @name already exists.
//
chfs_client::status
fuseserver_createhelper(fuse_ino_t parent, const char *name,
mode_t mode, struct fuse_entry_param *e, int type)
{
int ret;
// In chfs, timeouts are always set to 0.0, and generations are always set to 0
e->attr_timeout = 0.0;
e->entry_timeout = 0.0;
e->generation = 0;
chfs_client::inum inum;
if ( type == extent_protocol::T_FILE )
ret = chfs->create(parent, name, mode, inum);
else
ret = chfs->mkdir(parent, name, mode, inum);
if (ret != chfs_client::OK)
return ret;
e->ino = inum;
ret = getattr(inum, e->attr);
return ret;
}
void
fuseserver_create(fuse_req_t req, fuse_ino_t parent, const char *name,
mode_t mode, struct fuse_file_info *fi)
{
struct fuse_entry_param e;
chfs_client::status ret;
if( (ret = fuseserver_createhelper( parent, name, mode, &e, extent_protocol::T_FILE)) == chfs_client::OK ) {
fuse_reply_create(req, &e, fi);
printf("OK: create returns.\n");
} else {
if (ret == chfs_client::EXIST) {
fuse_reply_err(req, EEXIST);
} else {
fuse_reply_err(req, ENOENT);
}
}
}
void fuseserver_mknod( fuse_req_t req, fuse_ino_t parent,
const char *name, mode_t mode, dev_t rdev ) {
struct fuse_entry_param e;
chfs_client::status ret;
if( (ret = fuseserver_createhelper( parent, name, mode, &e, extent_protocol::T_FILE)) == chfs_client::OK ) {
fuse_reply_entry(req, &e);
} else {
if (ret == chfs_client::EXIST) {
fuse_reply_err(req, EEXIST);
}else{
fuse_reply_err(req, ENOENT);
}
}
}
//
// Look up file or directory @name in the directory @parent. If @name is
// found, set e.attr (using getattr) and e.ino to the attribute and inum of
// the file.
//
void
fuseserver_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
{
struct fuse_entry_param e;
// In chfs, timeouts are always set to 0.0, and generations are always set to 0
e.attr_timeout = 0.0;
e.entry_timeout = 0.0;
e.generation = 0;
bool found = false;
chfs_client::inum ino;
chfs->lookup(parent, name, found, ino);
if (found) {
e.ino = ino;
getattr(ino, e.attr);
fuse_reply_entry(req, &e);
} else {
fuse_reply_err(req, ENOENT);
}
}
struct dirbuf {
char *p;
size_t size;
};
void dirbuf_add(struct dirbuf *b, const char *name, fuse_ino_t ino)
{
struct stat stbuf;
size_t oldsize = b->size;
b->size += fuse_dirent_size(strlen(name));
b->p = (char *) realloc(b->p, b->size);
memset(&stbuf, 0, sizeof(stbuf));
stbuf.st_ino = ino;
fuse_add_dirent(b->p + oldsize, name, &stbuf, b->size);
}
#define min(x, y) ((x) < (y) ? (x) : (y))
int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
off_t off, size_t maxsize)
{
if ((size_t)off < bufsize)
return fuse_reply_buf(req, buf + off, min(bufsize - off, maxsize));
else
return fuse_reply_buf(req, NULL, 0);
}
//
// Retrieve all the file names / i-numbers pairs
// in directory @ino. Send the reply using reply_buf_limited.
//
// You can ignore @size and @off (except that you must pass
// them to reply_buf_limited).
//
// Call dirbuf_add(&b, name, inum) for each entry in the directory.
//
void
fuseserver_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
off_t off, struct fuse_file_info *fi)
{
chfs_client::inum inum = ino; // req->in.h.nodeid;
struct dirbuf b;
printf("fuseserver_readdir\n");
if(!chfs->isdir(inum)){
fuse_reply_err(req, ENOTDIR);
return;
}
memset(&b, 0, sizeof(b));
std::list<chfs_client::dirent> entries;
chfs->readdir(inum, entries);
for (std::list<chfs_client::dirent>::iterator it = entries.begin(); it != entries.end(); ++it) {
dirbuf_add(&b, it->name.c_str(), (fuse_ino_t) it->inum);
}
reply_buf_limited(req, b.p, b.size, off, size);
free(b.p);
}
void
fuseserver_open(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi)
{
fuse_reply_open(req, fi);
}
//
// Create a new directory with name @name in parent directory @parent.
// Leave new directory's inum in e.ino and attributes in e.attr.
//
// The new directory should be empty (no . or ..).
//
// If a file/directory named @name already exists, indicate error EEXIST.
//
// Ignore mode.
//
void
fuseserver_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name,
mode_t mode)
{
struct fuse_entry_param e;
// In chfs, timeouts are always set to 0.0, and generations are always set to 0
e.attr_timeout = 0.0;
e.entry_timeout = 0.0;
e.generation = 0;
// Suppress compiler warning of unused e.
(void) e;
#if 1
// Change the above line to "#if 1", and your code goes here
chfs_client::status r;
if ((r = fuseserver_createhelper(parent, name, mode, &e, extent_protocol::T_DIR)) == chfs_client::OK) {
fuse_reply_entry(req, &e);
} else {
if (r == chfs_client::EXIST) {
fuse_reply_err(req, EEXIST);
} else {
fuse_reply_err(req, ENOENT);
}
}
#else
fuse_reply_err(req, ENOSYS);
#endif
}
void
fuseserver_symlink(fuse_req_t req, const char *link, fuse_ino_t parent, const char *name)
{
chfs_client::inum id;
chfs_client::status stat;
struct fuse_entry_param e;
stat = chfs->link(parent, name, link, id);
if (stat != chfs_client::OK) {
if (stat == chfs_client::EXIST) {
fuse_reply_err(req, EEXIST);
} else {
fuse_reply_err(req, ENOENT);
}
return;
}
e.ino = id;
e.attr_timeout = 0.0;
e.entry_timeout = 0.0;
e.generation = 0;
if (getattr(id, e.attr) != chfs_client::OK) {
fuse_reply_err(req, ENOENT);
return;
}
fuse_reply_entry(req, &e);
}
void
fuseserver_readlink(fuse_req_t req, fuse_ino_t ino)
{
std::string buf;
if (chfs->readlink(ino, buf) != chfs_client::OK) {
fuse_reply_err(req, ENOENT);
return;
}
fuse_reply_readlink(req, buf.c_str());
}
//
// Remove the file named @name from directory @parent.
// Free the file's extent.
// If the file doesn't exist, indicate error ENOENT.
//
// Do *not* allow unlinking of a directory.
//
void
fuseserver_unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
{
int r;
if ((r = chfs->unlink(parent, name)) == chfs_client::OK) {
fuse_reply_err(req, 0);
} else {
if (r == chfs_client::NOENT) {
fuse_reply_err(req, ENOENT);
} else {
fuse_reply_err(req, ENOTEMPTY);
}
}
}
void
fuseserver_statfs(fuse_req_t req)
{
struct statvfs buf;
printf("statfs\n");
memset(&buf, 0, sizeof(buf));
buf.f_namemax = 255;
buf.f_bsize = 512;
fuse_reply_statfs(req, &buf);
}
struct fuse_lowlevel_ops fuseserver_oper;
int
main(int argc, char *argv[])
{
char *mountpoint = 0;
int err = -1;
int fd;
setvbuf(stdout, NULL, _IONBF, 0);
#if 1
if(argc != 3){
fprintf(stderr, "Usage: chfs_client <mountpoint> <port-extent-server>\n");
exit(1);
}
#else
if(argc != 2){
fprintf(stderr, "Usage: chfs_client <mountpoint>\n");
exit(1);
}
#endif
mountpoint = argv[1];
srandom(getpid());
myid = random();
chfs = new chfs_client(argv[2]);
// chfs = new chfs_client();
fuseserver_oper.getattr = fuseserver_getattr;
fuseserver_oper.statfs = fuseserver_statfs;
fuseserver_oper.readdir = fuseserver_readdir;
fuseserver_oper.lookup = fuseserver_lookup;
fuseserver_oper.create = fuseserver_create;
fuseserver_oper.mknod = fuseserver_mknod;
fuseserver_oper.open = fuseserver_open;
fuseserver_oper.read = fuseserver_read;
fuseserver_oper.write = fuseserver_write;
fuseserver_oper.setattr = fuseserver_setattr;
fuseserver_oper.unlink = fuseserver_unlink;
fuseserver_oper.mkdir = fuseserver_mkdir;
fuseserver_oper.symlink = fuseserver_symlink;
fuseserver_oper.readlink = fuseserver_readlink;
/** Your code here for Lab.
* you may want to add
* routines here to implement symbolic link,
* rmdir, etc.
* */
const char *fuse_argv[20];
int fuse_argc = 0;
fuse_argv[fuse_argc++] = argv[0];
#ifdef __APPLE__
fuse_argv[fuse_argc++] = "-o";
fuse_argv[fuse_argc++] = "nolocalcaches"; // no dir entry caching
fuse_argv[fuse_argc++] = "-o";
fuse_argv[fuse_argc++] = "daemon_timeout=86400";
#endif
// everyone can play, why not?
//fuse_argv[fuse_argc++] = "-o";
//fuse_argv[fuse_argc++] = "allow_other";
fuse_argv[fuse_argc++] = mountpoint;
fuse_argv[fuse_argc++] = "-d";
fuse_args args = FUSE_ARGS_INIT( fuse_argc, (char **) fuse_argv );
int foreground;
int res = fuse_parse_cmdline( &args, &mountpoint, 0 /*multithreaded*/,
&foreground );
if( res == -1 ) {
fprintf(stderr, "fuse_parse_cmdline failed\n");
return 0;
}
args.allocated = 0;
fd = fuse_mount(mountpoint, &args);
if(fd == -1){
fprintf(stderr, "fuse_mount failed\n");
exit(1);
}
struct fuse_session *se;
se = fuse_lowlevel_new(&args, &fuseserver_oper, sizeof(fuseserver_oper),
NULL);
if(se == 0){
fprintf(stderr, "fuse_lowlevel_new failed\n");
exit(1);
}
struct fuse_chan *ch = fuse_kern_chan_new(fd);
if (ch == NULL) {
fprintf(stderr, "fuse_kern_chan_new failed\n");
exit(1);
}
fuse_session_add_chan(se, ch);
// err = fuse_session_loop_mt(se); // FK: wheelfs does this; why?
err = fuse_session_loop(se);
fuse_session_destroy(se);
close(fd);
fuse_unmount(mountpoint);
return err ? 1 : 0;
}