-
Notifications
You must be signed in to change notification settings - Fork 7
/
memfs.c
661 lines (504 loc) · 14 KB
/
memfs.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
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
#define FUSE_USE_VERSION 26
#include <fuse.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/time.h>
#include <libgen.h>
#include "node.h"
#include "dir.h"
#define BLOCKSIZE 4096
#define O_WRITE(flags) ((flags) & (O_RDWR | O_WRONLY))
#define O_READ(flags) (((flags) & (O_RDWR | O_RDONLY)) | !O_WRITE(flags))
#define U_ATIME (1 << 0)
#define U_CTIME (1 << 1)
#define U_MTIME (1 << 2)
struct filesystem {
struct node *root;
};
struct filehandle {
struct node *node;
int o_flags;
};
struct filesystem the_fs;
//
// Utility functions
//
char * safe_dirname(const char *msg) {
char *buf = strdup(msg);
char *dir = dirname(buf);
char *res = strdup(dir);
free(buf);
return res;
}
char * safe_basename(const char *msg) {
char *buf = strdup(msg);
char *nam = basename(buf);
char *res = strdup(nam);
free(buf);
return res;
}
int getnodebypath(const char *path, struct filesystem *fs, struct node **node) {
return getnoderelativeto(path, fs->root, node);
}
static void update_times(struct node *node, int which) {
time_t now = time(0);
if(which & U_ATIME) node->vstat.st_atime = now;
if(which & U_CTIME) node->vstat.st_ctime = now;
if(which & U_MTIME) node->vstat.st_mtime = now;
}
static int initstat(struct node *node, mode_t mode) {
struct stat *stbuf = &node->vstat;
memset(stbuf, 0, sizeof(struct stat));
stbuf->st_mode = mode;
stbuf->st_nlink = 0;
update_times(node, U_ATIME | U_MTIME | U_CTIME);
return 1;
}
static int createentry(const char *path, mode_t mode, struct node **node) {
char *dirpath = safe_dirname(path);
// Find parent node
struct node *dir;
int ret = getnodebypath(dirpath, &the_fs, &dir);
free(dirpath);
if(!ret) {
return -errno;
}
// Create new node
*node = malloc(sizeof(struct node));
if(!*node) {
return -ENOMEM;
}
(*node)->fd_count = 0;
(*node)->delete_on_close = 0;
// Initialize stats
if(!initstat(*node, mode)) {
free(*node);
return -errno;
}
struct fuse_context *ctx = fuse_get_context();
(*node)->vstat.st_uid = ctx->uid;
(*node)->vstat.st_gid = ctx->gid;
// Add to parent directory
if(!dir_add_alloc(dir, safe_basename(path), *node, 0)) {
free(*node);
return -errno;
}
return 0;
}
//
// Filesystem entry points
//
static int memfs_getattr(const char *path, struct stat *stbuf) {
struct node *node;
if(!getnodebypath(path, &the_fs, &node)) {
return -errno;
}
stbuf->st_mode = node->vstat.st_mode;
stbuf->st_nlink = node->vstat.st_nlink;
stbuf->st_size = node->vstat.st_size;
stbuf->st_blocks = node->vstat.st_blocks;
stbuf->st_uid = node->vstat.st_uid;
stbuf->st_gid = node->vstat.st_gid;
stbuf->st_mtime = node->vstat.st_mtime;
stbuf->st_atime = node->vstat.st_atime;
stbuf->st_ctime = node->vstat.st_ctime;
// Directories contain the implicit hardlink '.'
if(S_ISDIR(node->vstat.st_mode)) {
stbuf->st_nlink++;
}
return 0;
}
static int memfs_readlink(const char *path, char *buf, size_t size) {
struct node *node;
if(!getnodebypath(path, &the_fs, &node)) {
return -errno;
}
if(!S_ISLNK(node->vstat.st_mode)) {
return -ENOLINK;
}
// Fuse breaks compatibility with other readlink() implementations as we cannot use the return
// value to indicate how many bytes were written. Instead, we need to null-terminate the string,
// unless the buffer is not large enough to hold the path. In that case, fuse will null-terminate
// the string before passing it on.
if(node->vstat.st_size > size) {
memcpy(buf, node->data, size);
} else {
strcpy(buf, node->data);
}
return 0;
}
static int memfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) {
struct node *dir;
if(!getnodebypath(path, &the_fs, &dir)) {
return -errno;
}
if(!S_ISDIR(dir->vstat.st_mode)) {
return -ENOTDIR;
}
filler(buf, ".", &dir->vstat, 0);
if(dir == the_fs.root) {
filler(buf, "..", NULL, 0);
} else {
char *parent_path = safe_dirname(path);
struct node *parent;
getnodebypath(parent_path, &the_fs, &parent);
free(parent_path);
filler(buf, "..", &parent->vstat, 0);
}
struct direntry *entry = dir->data;
while(entry != NULL) {
if(filler(buf, entry->name, &entry->node->vstat, 0))
break;
entry = entry->next;
}
return 0;
}
static int memfs_mknod(const char *path, mode_t mode, dev_t rdev) {
struct node *node;
int res = createentry(path, mode, &node);
if(res) return res;
if(S_ISREG(mode)) {
node->data = NULL;
node->vstat.st_blocks = 0;
} else {
return -ENOSYS;
}
return 0;
}
static int memfs_mkdir(const char *path, mode_t mode) {
struct node *node;
int res = createentry(path, S_IFDIR | mode, &node);
if(res) return res;
// No entries
node->data = NULL;
return 0;
}
static int memfs_unlink(const char *path) {
char *dirpath, *name;
struct node *dir, *node;
// Find inode
if(!getnodebypath(path, &the_fs, &node)) {
return -errno;
}
if(S_ISDIR(node->vstat.st_mode)) {
return -EISDIR;
}
dirpath = safe_dirname(path);
// Find parent inode
if(!getnodebypath(dirpath, &the_fs, &dir)) {
free(dirpath);
return -errno;
}
free(dirpath);
name = safe_basename(path);
// Find directory entry in parent
if(!dir_remove(dir, name)) {
free(name);
return -errno;
}
free(name);
// If the link count is zero, delete the associated data
if(node->vstat.st_nlink == 0) {
if(node->fd_count == 0) {
// No open file descriptors, we can safely delete the node
if(node->data) free(node->data);
free(node);
} else {
// There are open file descriptors, schedule deletion
node->delete_on_close = 1;
}
}
return 0;
}
static int memfs_rmdir(const char *path) {
char *dirpath, *name;
struct node *dir, *node;
// Find inode
if(!getnodebypath(path, &the_fs, &node)) {
return -errno;
}
if(!S_ISDIR(node->vstat.st_mode)) {
return -ENOTDIR;
}
// Check if directory is empty
if(node->data != NULL) {
return -ENOTEMPTY;
}
dirpath = safe_dirname(path);
// Find parent inode
if(!getnodebypath(dirpath, &the_fs, &dir)) {
free(dirpath);
return -errno;
}
free(dirpath);
name = safe_basename(path);
// Find directory entry in parent
if(!dir_remove(dir, name)) {
free(name);
return -errno;
}
free(name);
free(node);
return 0;
}
static int memfs_symlink(const char *from, const char *to) {
struct node *node;
int res = createentry(to, S_IFLNK | 0766, &node);
if(res) return res;
node->data = strdup(from);
node->vstat.st_size = strlen(from);
return 0;
}
// TODO: Adapt to description: https://linux.die.net/man/2/rename
static int memfs_rename(const char *from, const char *to) {
char *fromdir, *fromnam, *todir, *tonam;
struct node *node, *fromdirnode, *todirnode;
if(!getnodebypath(from, &the_fs, &node)) {
return -errno;
}
fromdir = safe_dirname(from);
if(!getnodebypath(fromdir, &the_fs, &fromdirnode)) {
free(fromdir);
return -errno;
}
free(fromdir);
todir = safe_dirname(to);
if(!getnodebypath(todir, &the_fs, &todirnode)) {
free(todir);
return -errno;
}
free(todir);
tonam = safe_basename(to);
// TODO: When replacing, perform the same things as when unlinking
if(!dir_add_alloc(todirnode, tonam, node, 1)) {
free(tonam);
return -errno;
}
free(tonam);
fromnam = safe_basename(from);
if(!dir_remove(fromdirnode, fromnam)) {
free(fromnam);
return -errno;
}
free(fromnam);
return 0;
}
static int memfs_link(const char *from, const char *to) {
char *todir, *tonam;
struct node *node, *todirnode;
if(!getnodebypath(from, &the_fs, &node)) {
return -errno;
}
todir = safe_dirname(to);
if(!getnodebypath(todir, &the_fs, &todirnode)) {
free(todir);
return -errno;
}
free(todir);
tonam = safe_basename(to);
if(!dir_add_alloc(todirnode, tonam, node, 0)) {
free(tonam);
return -errno;
}
free(tonam);
return 0;
}
static int memfs_chmod(const char *path, mode_t mode) {
struct node *node;
if(!getnodebypath(path, &the_fs, &node)) {
return -errno;
}
mode_t mask = S_ISUID | S_ISGID | S_ISVTX |
S_IRUSR | S_IWUSR | S_IXUSR |
S_IRGRP | S_IWGRP | S_IXGRP |
S_IROTH | S_IWOTH | S_IXOTH;
node->vstat.st_mode = (node->vstat.st_mode & ~mask) | (mode & mask);
update_times(node, U_CTIME);
return 0;
}
static int memfs_chown(const char *path, uid_t uid, gid_t gid) {
struct node *node;
if(!getnodebypath(path, &the_fs, &node)) {
return -errno;
}
node->vstat.st_uid = uid;
node->vstat.st_gid = gid;
update_times(node, U_CTIME);
return 0;
}
static int memfs_utimens(const char *path, const struct timespec ts[2]) {
struct node *node;
if(!getnodebypath(path, &the_fs, &node)) {
return -errno;
}
node->vstat.st_atime = ts[0].tv_sec;
node->vstat.st_mtime = ts[1].tv_sec;
return 0;
}
static int memfs_truncate(const char *path, off_t size) {
struct node *node;
if(!getnodebypath(path, &the_fs, &node)) {
return -errno;
}
// Calculate new block count
blkcnt_t newblkcnt = (size + BLOCKSIZE - 1) / BLOCKSIZE;
blkcnt_t oldblkcnt = node->vstat.st_blocks;
if(oldblkcnt < newblkcnt) {
// Allocate additional memory
void *newdata = malloc(newblkcnt * BLOCKSIZE);
if(!newdata) {
return -ENOMEM;
}
memcpy(newdata, node->data, node->vstat.st_size);
free(node->data);
node->data = newdata;
} else if(oldblkcnt > newblkcnt) {
// Allocate new memory so we can free the unnecessarily large memory
void *newdata = malloc(newblkcnt * BLOCKSIZE);
if(!newdata) {
return -ENOMEM;
}
memcpy(newdata, node->data, size);
free(node->data);
node->data = newdata;
}
// Fill additional memory with zeroes
if(node->vstat.st_size < size) {
memset(node->data + node->vstat.st_size, 0, node->vstat.st_size - size);
}
// Update file size
node->vstat.st_size = size;
node->vstat.st_blocks = newblkcnt;
return 0;
}
static int memfs_open(const char *path, struct fuse_file_info *fi) {
struct node *node;
if(!getnodebypath(path, &the_fs, &node)) {
return -errno;
}
if(!S_ISREG(node->vstat.st_mode)) {
if(S_ISDIR(node->vstat.st_mode)) {
return -EISDIR;
}
}
// Update file timestamps
update_times(node, U_ATIME);
// The "file handle" is a pointer to a struct we use to keep track of the inode and the
// flags passed to open().
struct filehandle *fh = malloc(sizeof(struct filehandle));
fh->node = node;
fh->o_flags = fi->flags;
fi->fh = (uint64_t) fh;
node->fd_count++;
return 0;
}
static int memfs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
struct filehandle *fh = (struct filehandle *) fi->fh;
// Check whether the file was opened for reading
if(!O_READ(fh->o_flags)) {
return -EACCES;
}
struct node *node = fh->node;
off_t filesize = node->vstat.st_size;
// Check whether offset is at or beyond the end of file
if(offset >= filesize) {
return 0;
}
// Calculate number of bytes to copy
size_t avail = filesize - offset;
size_t n = (size < avail) ? size : avail;
// Copy file contents
memcpy(buf, node->data + offset, n);
update_times(node, U_ATIME);
return n;
}
static int memfs_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
struct filehandle *fh = (struct filehandle *) fi->fh;
// Check whether the file was opened for writing
if(!O_WRITE(fh->o_flags)) {
return -EACCES;
}
struct node *node = fh->node;
// Calculate number of required blocks
blkcnt_t req_blocks = (offset + size + BLOCKSIZE - 1) / BLOCKSIZE;
if(node->vstat.st_blocks < req_blocks) {
// Allocate more memory
void *newdata = malloc(req_blocks * BLOCKSIZE);
if(!newdata) {
return -ENOMEM;
}
// Copy old contents
if(node->data != NULL) {
memcpy(newdata, node->data, node->vstat.st_size);
free(node->data);
}
// Update allocation information
node->data = newdata;
node->vstat.st_blocks = req_blocks;
}
// Write to file buffer
memcpy(((char *) node->data) + offset, buf, size);
// Update file size if necessary
off_t minsize = offset + size;
if(minsize > node->vstat.st_size) {
node->vstat.st_size = minsize;
}
update_times(node, U_CTIME | U_MTIME);
return size;
}
static int memfs_release(const char *path, struct fuse_file_info *fi) {
struct filehandle *fh = (struct filehandle *) fi->fh;
// If the file was deleted but we could not free it due to open file descriptors,
// free the node and its data after all file descriptors have been closed.
if(--fh->node->fd_count == 0) {
if(fh->node->delete_on_close) {
if(fh->node->data) free(fh->node->data);
free(fh->node);
}
}
// Free "file handle"
free(fh);
return 0;
}
static struct fuse_operations memfs_oper = {
.getattr = memfs_getattr,
.readlink = memfs_readlink,
.readdir = memfs_readdir,
.mknod = memfs_mknod,
.mkdir = memfs_mkdir,
.symlink = memfs_symlink,
.unlink = memfs_unlink,
.rmdir = memfs_rmdir,
.rename = memfs_rename,
.link = memfs_link,
.chmod = memfs_chmod,
.chown = memfs_chown,
.truncate = memfs_truncate,
.utimens = memfs_utimens,
.open = memfs_open,
.read = memfs_read,
.write = memfs_write,
.release = memfs_release
};
//
// Application entry point
//
int main(int argc, char *argv[]) {
// Initialize root directory
struct node *root = malloc(sizeof(struct node));
memset(root, 0, sizeof(struct node));
initstat(root, S_IFDIR | 0755);
root->vstat.st_uid = getuid();
root->vstat.st_gid = getgid();
// No entries
root->data = NULL;
// Set root directory of filesystem
the_fs.root = root;
umask(0);
return fuse_main(argc, argv, &memfs_oper, NULL);
}