/
disk.c
781 lines (654 loc) · 20.4 KB
/
disk.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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
#include <assert.h>
#include <gc/gc.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <utime.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include "types.h"
#include "9p.h"
#include "list.h"
#include "util.h"
#include "config.h"
#include "worker.h"
#include "lru.h"
#include "disk.h"
#include "dir.h"
Lru *objectdir_lru;
Lru *openfile_lru;
u64 disk_next_available;
static inline char *make_filename(u64 oid, u32 mode, char *name, char *group) {
char *filename = GC_MALLOC_ATOMIC(OBJECT_FILENAME_LENGTH + 1);
assert(filename != NULL);
sprintf(filename, "%0*x %8x %*.*s %*.*s",
(BITS_PER_DIR_OBJECTS + 3) / 4,
(unsigned) (oid & ((1 << BITS_PER_DIR_OBJECTS) - 1)),
mode,
MAX_UID_LENGTH, MAX_UID_LENGTH, name,
MAX_GID_LENGTH, MAX_GID_LENGTH, group);
return filename;
}
static inline int parse_filename(char *filename, unsigned int *id, u32 *mode,
char **name, char **group)
{
char eos;
*name = GC_MALLOC_ATOMIC(MAX_UID_LENGTH + 1);
assert(name != NULL);
*group = GC_MALLOC_ATOMIC(MAX_GID_LENGTH + 1);
assert(group != NULL);
if (sscanf(filename, "%x %x %8s %8s%c",
id, mode, *name, *group, &eos) != 4 ||
name[0] == 0 || group[0] == 0)
{
return -1;
}
return 0;
}
static inline u64 disk_dir_findstart(u64 oid) {
return (oid >> BITS_PER_DIR_OBJECTS) << BITS_PER_DIR_OBJECTS;
}
static int resurrect_objectdir(Objectdir *dir) {
return dir->lock != NULL;
}
static void close_objectdir(Objectdir *dir) {
assert(dir->lock == NULL);
/* no cleanup is required */
dir->start = ~0LL;
dir->dirname = NULL;
dir->filenames = NULL;
}
static int resurrect_openfile(Openfile *file) {
return file->lock != NULL;
}
static void close_openfile(Openfile *file) {
assert(file->lock == NULL);
close(file->fd);
file->fd = -1;
}
void disk_state_init_storage(void) {
objectdir_lru = lru_new(
OBJECTDIR_CACHE_SIZE_STORAGE,
(Hashfunc) u64_hash,
(Cmpfunc) u64_cmp,
(int (*)(void *)) resurrect_objectdir,
(void (*)(void *)) close_objectdir);
openfile_lru = lru_new(
FD_CACHE_SIZE_STORAGE,
(Hashfunc) u64_hash,
(Cmpfunc) u64_cmp,
(int (*)(void *)) resurrect_openfile,
(void (*)(void *)) close_openfile);
disk_next_available = disk_find_next_available();
}
void disk_state_init_envoy(void) {
objectdir_lru = lru_new(
OBJECTDIR_CACHE_SIZE_ENVOY,
(Hashfunc) u64_hash,
(Cmpfunc) u64_cmp,
(int (*)(void *)) resurrect_objectdir,
(void (*)(void *)) close_objectdir);
openfile_lru = lru_new(
FD_CACHE_SIZE_ENVOY,
(Hashfunc) u64_hash,
(Cmpfunc) u64_cmp,
(int (*)(void *)) resurrect_openfile,
(void (*)(void *)) close_openfile);
disk_next_available = disk_find_next_available();
}
Openfile *disk_add_openfile(u64 oid, int fd) {
Openfile *file;
u64 *key;
assert(fd >= 0);
file = GC_NEW(Openfile);
assert(file != NULL);
key = GC_NEW_ATOMIC(u64);
assert(key != NULL);
file->lock = NULL;
file->fd = fd;
*key = oid;
lru_add(openfile_lru, key, file);
return file;
}
/* return a list of directories for all the bits (except the end) */
List *oid_to_path(u64 oid) {
List *path = NULL;
int bitsleft = OID_BITS;
char buff[16];
while (bitsleft > 0) {
u64 part;
int chunkpart =
((bitsleft - BITS_PER_DIR_OBJECTS) / BITS_PER_DIR_DIRS) *
BITS_PER_DIR_DIRS;
int bits;
/* three cases:
* 1) odd bits at the beginning
* 2) odd bits at the end
* 3) regular sized chunks in the middle */
if (bitsleft > chunkpart + BITS_PER_DIR_OBJECTS)
bits = bitsleft - (chunkpart + BITS_PER_DIR_OBJECTS);
else if (bitsleft == BITS_PER_DIR_OBJECTS)
break;
else
bits = BITS_PER_DIR_DIRS;
bitsleft -= bits;
part = oid >> bitsleft;
oid &= (1LL << bitsleft) - 1LL;
sprintf(buff, "%0*llx", (bits + 3) / 4, part);
path = cons(stringcopy(buff), path);
}
return reverse(path);
}
u64 path_to_oid(List *path) {
u64 oid = 0LL;
int bitsleft = OID_BITS;
while (bitsleft > BITS_PER_DIR_OBJECTS) {
int part;
char *elt = car(path);
int chunkpart =
((bitsleft - BITS_PER_DIR_OBJECTS) / BITS_PER_DIR_DIRS) *
BITS_PER_DIR_DIRS;
int bits;
assert(sscanf(elt, "%x", &part) == 1);
path = cdr(path);
/* two cases:
* 1) odd bits at the beginning
* 2) regular sized chunks in the middle */
if (bitsleft > chunkpart + BITS_PER_DIR_OBJECTS)
bits = bitsleft - (chunkpart + BITS_PER_DIR_OBJECTS);
else
bits = BITS_PER_DIR_DIRS;
bitsleft -= bits;
oid |= ((u64) part) << bitsleft;
}
return oid;
}
u64 disk_find_next_available(void) {
char *dir = objectroot;
List *minpath_full = oid_to_path(0LL);
List *minpath_lst = minpath_full;
List *maxpath_full = oid_to_path(0xFFFFFFFFFFFFFFFFLL);
List *maxpath_lst = maxpath_full;
List *lastpath = NULL;
while (!null(maxpath_lst)) {
/* scan the current directory */
char *maxpath = car(maxpath_lst);
char *high = NULL;
DIR *dd = opendir(dir);
struct dirent *ent;
assert(dd != NULL);
while ((ent = readdir(dd)) != NULL) {
int n;
char c;
/* test this filename:
* 1) is it the right length?
* 2) is it a valid hex number?
* 3) is it within the acceptable range?
* 4) does it beat our previous high value? */
if (strlen(ent->d_name) != strlen(maxpath) ||
sscanf(ent->d_name, "%x%c", &n, &c) != 1 ||
strcmp(ent->d_name, maxpath) > 0 ||
(high != NULL && strcmp(ent->d_name, high) <= 0))
{
continue;
}
high = stringcopy(ent->d_name);
}
closedir(dd);
if (high == NULL) {
while (!null(minpath_lst)) {
lastpath = cons(car(minpath_lst), lastpath);
minpath_lst = cdr(minpath_lst);
}
return path_to_oid(reverse(lastpath));
} else {
maxpath_lst = cdr(maxpath_lst);
minpath_lst = cdr(minpath_lst);
lastpath = cons(high, lastpath);
dir = concatname(dir, high);
}
}
return path_to_oid(reverse(lastpath)) + (1 << BITS_PER_DIR_OBJECTS);
}
int disk_reserve_block(u64 *oid, u32 *count) {
char *dir = objectroot;
struct stat info;
List *path;
*oid = disk_next_available;
*count = 1 << BITS_PER_DIR_OBJECTS;
disk_next_available += (u64) *count;
/* just in case 64 bits isn't enough... */
if (disk_next_available < *oid)
return -1;
path = oid_to_path(*oid);
while (!null(path)) {
dir = concatname(dir, car(path));
path = cdr(path);
if (lstat(dir, &info) < 0) {
if (mkdir(dir, OBJECT_DIR_MODE) < 0)
return -1;
} else if (!S_ISDIR(info.st_mode)) {
return -1;
}
}
return 0;
}
/* Read a directory of objects. The argument should be a blank object that
* is already indexed in the LRU and reserved. */
static void read_objectdir(Objectdir *dir) {
int i;
const int size = 1 << BITS_PER_DIR_OBJECTS;
char *dirname;
DIR *dd;
struct dirent *ent;
List *path;
dirname = objectroot;
path = oid_to_path(dir->start);
for (path = oid_to_path(dir->start); !null(path); path = cdr(path))
dirname = concatname(dirname, (char *) car(path));
dir->dirname = dirname;
dir->filenames = GC_MALLOC(sizeof(char *) * size);
assert(dir->filenames != NULL);
for (i = 0; i < size; i++)
dir->filenames[i] = NULL;
dd = opendir(dirname);
if (dd == NULL)
return;
while ((ent = readdir(dd)) != NULL) {
unsigned int id, mode;
char *name, *group;
if (parse_filename(ent->d_name, &id, &mode, &name, &group) == 0 &&
id < size)
{
dir->filenames[id] = stringcopy(ent->d_name);
}
}
closedir(dd);
}
/* reserves the resulting object */
Objectdir *objectdir_lookup(Worker *worker, u64 start) {
Objectdir *dir;
assert(start == disk_dir_findstart(start));
dir = lru_get(objectdir_lru, &start);
if (dir == NULL) {
/* create a new blank entry and reserve it */
u64 *key = GC_NEW_ATOMIC(u64);
assert(key != NULL);
*key = start;
dir = GC_NEW(Objectdir);
assert(dir != NULL);
dir->lock = NULL;
dir->start = start;
dir->dirname = NULL;
dir->filenames = NULL;
lru_add(objectdir_lru, key, dir);
reserve(worker, LOCK_DIRECTORY, dir);
read_objectdir(dir);
} else {
reserve(worker, LOCK_DIRECTORY, dir);
}
return dir;
}
struct p9stat *disk_stat(Worker *worker, u64 oid) {
/* get filename */
u64 start = disk_dir_findstart(oid);
struct objectdir *dir;
char *filename;
struct stat info;
struct p9stat *result;
unsigned int id, mode;
char *name, *group;
if ( /* gather all the info we need */
(dir = objectdir_lookup(worker, start)) == NULL ||
(filename = dir->filenames[oid - start]) == NULL ||
parse_filename(filename, &id, &mode, &name, &group) < 0 ||
id != oid - start ||
lstat(concatname(dir->dirname, filename), &info) < 0)
{
return NULL;
}
result = GC_NEW(struct p9stat);
assert(result != NULL);
/* convert everything to a 9p stat record */
result->type = 0;
result->dev = 0;
result->qid = makeqid(mode, info.st_mtime, info.st_size, oid);
result->mode = mode;
result->atime = info.st_atime;
result->mtime = info.st_mtime;
result->length = info.st_size;
result->name = NULL;
result->uid = stringcopy(name);
result->gid = stringcopy(group);
result->muid = result->uid;
result->extension = NULL;
/* for special files, read the contents of the file into extension */
if (mode & (DMSYMLINK | DMDEVICE | DMNAMEDPIPE | DMSOCKET) &&
info.st_size > 0 && info.st_size < MAX_EXTENSION_LENGTH)
{
int i, size;
Openfile *file = disk_get_openfile(worker, oid);
result->extension = GC_MALLOC_ATOMIC(info.st_size + 1);
assert(result->extension != NULL);
if (lseek(file->fd, 0, SEEK_SET) < 0)
return NULL;
{
int failure = 0;
unlock();
for (size = 0; size < info.st_size; ) {
i = read(file->fd, result->extension + size,
info.st_size - size);
if (i <= 0) {
failure = 1;
break;
}
size += i;
}
lock();
if (failure)
return NULL;
}
result->extension[size] = 0;
worker_cleanup_remove(worker, LOCK_OPENFILE, file);
file->lock = NULL;
}
result->n_uid = user_to_uid(name);
result->n_gid = group_to_gid(group);
result->n_muid = user_to_uid(name);
return result;
}
int disk_wstat(Worker *worker, u64 oid, struct p9stat *info) {
u64 start = disk_dir_findstart(oid);
struct objectdir *dir = NULL;
char *filename, *newfilename;
char *pathname, *newpathname;
unsigned int id;
u32 mode;
char *name, *group;
if ( /* get the current info for the object */
(dir = objectdir_lookup(worker, start)) == NULL ||
(filename = dir->filenames[oid - start]) == NULL ||
parse_filename(filename, &id, &mode, &name, &group) < 0 ||
id != oid - start)
{
return -1;
}
pathname = concatname(dir->dirname, filename);
/* make the requested changes */
/* mtime */
if (info->mtime != ~(u32) 0) {
struct utimbuf buf;
buf.actime = info->mtime;
buf.modtime = info->mtime;
if (utime(pathname, &buf) < 0)
return -1;
}
/* mode */
if (info->mode != ~(u32) 0)
mode = info->mode;
/* gid */
if (!emptystring(info->gid))
group = info->gid;
/* uid */
if (!emptystring(info->uid))
name = info->uid;
/* extension */
if (!emptystring(info->extension) &&
(mode & (DMSYMLINK | DMDEVICE | DMNAMEDPIPE | DMSOCKET)))
{
Openfile *file = disk_get_openfile(worker, oid);
int len = strlen(info->extension);
int failure = 0;
unlock();
if (ftruncate(file->fd, 0) < 0 ||
lseek(file->fd, 0, SEEK_SET) < 0 ||
write(file->fd, info->extension, len) != len)
{
failure = 1;
}
lock();
if (failure)
return -1;
} else if (info->length != ~(u64) 0) {
/* truncate */
struct stat finfo;
if (lstat(pathname, &finfo) < 0)
return -1;
if (info->length != finfo.st_size) {
int failure = 0;
unlock();
if (truncate(pathname, info->length) < 0)
failure = 1;
lock();
if (failure)
return -1;
}
}
newfilename = make_filename(oid, mode, name, group);
/* see if there were any filename changes */
if (strcmp(filename, newfilename)) {
newpathname = concatname(dir->dirname, newfilename);
if (rename(pathname, newpathname) < 0)
return -1;
dir->filenames[oid - start] = newfilename;
}
return 0;
}
int disk_create(Worker *worker, u64 oid, u32 mode, u32 ctime, char *uid,
char *gid, char *extension)
{
u64 start = disk_dir_findstart(oid);
struct objectdir *dir;
struct utimbuf buf;
char *filename;
char *pathname;
int fd;
int length = 0;
if ( /* find the place for this object and make sure it is new */
(dir = objectdir_lookup(worker, start)) == NULL ||
(filename = dir->filenames[oid - start]) != NULL)
{
return -1;
}
/* create the file and set filename-encoded stats */
filename = make_filename(oid, mode, uid, gid);
pathname = concatname(dir->dirname, filename);
fd = open(pathname, O_CREAT | O_RDWR | O_TRUNC, OBJECT_MODE);
/* create failed */
if (fd < 0) {
/* was it because the directory doesn't exist? */
if (errno != ENOENT) {
return -1;
} else {
/* create the directories and try again */
List *path = oid_to_path(oid);
char *prefix = objectroot;
struct stat info;
while (!null(path)) {
prefix = concatname(prefix, car(path));
path = cdr(path);
if (lstat(prefix, &info) < 0) {
if (mkdir(prefix, OBJECT_DIR_MODE) < 0)
return -1;
} else if (!S_ISDIR(info.st_mode)) {
return -1;
}
}
/* try again */
if ((fd = open(pathname, O_CREAT | O_RDWR | O_TRUNC,
OBJECT_MODE)) < 0)
{
return -1;
}
}
}
dir->filenames[oid - start] = filename;
disk_add_openfile(oid, fd);
/* store metadata for special file types as file contents */
if (!emptystring(extension) &&
(mode & (DMSYMLINK | DMDEVICE | DMNAMEDPIPE | DMSOCKET)) &&
strlen(extension) !=
write(fd, extension, length = strlen(extension)))
{
return -1;
}
/* set the times */
buf.actime = ctime;
buf.modtime = ctime;
if (utime(pathname, &buf) < 0)
return -1;
return length;
}
Openfile *disk_get_openfile(Worker *worker, u64 oid) {
Openfile *file;
/* first check the cache */
file = lru_get(openfile_lru, &oid);
/* open the file if necessary */
if (file == NULL) {
u64 start = disk_dir_findstart(oid);
struct objectdir *dir;
char *filename;
int fd;
if ( /* find the pathname and open the file */
(dir = objectdir_lookup(worker, start)) == NULL ||
(filename = dir->filenames[oid - start]) == NULL ||
(fd = open(concatname(dir->dirname, filename), O_RDWR )) < 0)
{
return NULL;
}
file = disk_add_openfile(oid, fd);
}
assert(file->fd >= 0);
reserve(worker, LOCK_OPENFILE, file);
return file;
}
int disk_set_times(Worker *worker, u64 oid, struct utimbuf *buf) {
u64 start = disk_dir_findstart(oid);
struct objectdir *dir;
char *filename;
if ((dir = objectdir_lookup(worker, start)) == NULL ||
(filename = dir->filenames[oid - start]) == NULL ||
utime(concatname(dir->dirname, filename), buf) < 0)
{
return -1;
}
return 0;
}
int disk_clone(Worker *worker, u64 oldoid, u64 newoid) {
struct p9stat *info = disk_stat(worker, oldoid);
Openfile *new_fd, *old_fd;
void *buff = GC_MALLOC_ATOMIC(CLONE_BUFFER_SIZE);
int count;
struct utimbuf buf;
assert(buff != NULL);
if (disk_create(worker, newoid, info->mode, info->mtime, info->uid,
info->gid, info->extension) < 0)
{
return -1;
}
/* zero byte file? */
if (info->length == 0LL)
return 0;
/* open both files and position at the start of the files */
if ((old_fd = disk_get_openfile(worker, oldoid)) == NULL)
return -1;
if (lseek(old_fd->fd, 0, SEEK_SET) < 0 ||
((new_fd = disk_get_openfile(worker, newoid)) == NULL))
{
return -1;
}
if (lseek(new_fd->fd, 0, SEEK_SET) < 0)
return -1;
/* copy the file a chunk at a time */
{
int failure = 0;
unlock();
for (;;) {
count = read(old_fd->fd, buff, CLONE_BUFFER_SIZE);
if (count == 0)
break;
if (count < 0) {
failure = 1;
break;
}
if ((info->mode & DMDIR) != 0) {
/* it's a directory, so set all the CoW flags */
u32 i;
for (i = 0; i < count; i += BLOCK_SIZE) {
u32 size = min(BLOCK_SIZE, count - i);
dir_clone(size, buff);
}
}
if (write(new_fd->fd, buff, count) != count) {
failure = 1;
break;
}
}
lock();
if (failure)
return -1;
}
buf.actime = info->atime;
buf.modtime = info->mtime;
return disk_set_times(worker, newoid, &buf);
}
int disk_delete(Worker *worker, u64 oid) {
u64 start = disk_dir_findstart(oid);
struct objectdir *dir;
char *filename;
if ( (dir = objectdir_lookup(worker, start)) == NULL ||
(filename = dir->filenames[oid - start]) == NULL)
{
return -ENOENT;
}
lru_remove(openfile_lru, &oid);
dir->filenames[oid - start] = NULL;
if (unlink(concatname(dir->dirname, filename)) < 0)
return -errno;
return 0;
}
int disk_write(Worker *worker, u64 oid, u32 time, u64 offset, u32 count,
u8 *data)
{
Openfile *file;
struct utimbuf buf;
int len;
file = disk_get_openfile(worker, oid);
if (file == NULL)
return -ENOENT;
if (lseek(file->fd, offset, SEEK_SET) < 0)
return -errno;
unlock();
len = write(file->fd, data, count);
lock();
if (len < 0)
return -errno;
assert(count == (u32) len);
/* set the mtime */
buf.actime = time;
buf.modtime = time;
if (disk_set_times(worker, oid, &buf) < 0)
return -errno;
return len;
}
int disk_read(Worker *worker, u64 oid, u32 time, u64 offset, u32 count,
u8 *data)
{
Openfile *file;
int len;
file = disk_get_openfile(worker, oid);
if (file == NULL)
return -ENOENT;
if (lseek(file->fd, offset, SEEK_SET) < 0)
return -errno;
unlock();
len = read(file->fd, data, count);
lock();
if (len < 0)
return -errno;
/* set the atime */
return len;
}