forked from daos-stack/daos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfuse.c
1468 lines (1259 loc) · 31.4 KB
/
dfuse.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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* (C) Copyright 2018 Intel Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GOVERNMENT LICENSE RIGHTS-OPEN SOURCE SOFTWARE
* The Government's rights to use, modify, reproduce, release, perform, display,
* or disclose this software are subject to the terms of the Apache License as
* provided in Contract No. B609815.
* Any reproduction of computer software, computer software documentation, or
* portions thereof marked with this legend must also reproduce the markings.
*/
#define D_LOGFAC DD_FAC(dfs)
#include <fuse3/fuse.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <libgen.h>
#include <daos/common.h>
#include "daos_fs.h"
#include "daos_api.h"
struct dfuse_data {
int show_help;
int show_version;
int debug;
int foreground;
int singlethread;
bool destroy;
char *mountpoint;
char *pool;
char *svcl;
char *group;
struct fuse *fuse;
};
static struct dfuse_data dfuse_fs;
static dfs_t *dfs;
#define FUNC_ENTER(fmt, ...) \
do { \
if (dfuse_fs.debug) \
fprintf(stderr, "%s [%d]: "fmt, __func__, \
__LINE__, ##__VA_ARGS__); \
} while (0)
#define UINT64ENCODE(p, n) \
do { \
uint64_t _n = (n); \
size_t _i; \
uint8_t *_p = (uint8_t *)(p); \
for (_i = 0; _i < sizeof(uint64_t); _i++, _n >>= 8) \
*_p++ = (uint8_t)(_n & 0xff); \
for (; _i < 8; _i++) \
*_p++ = 0; \
(p) = (uint8_t *)(p) + 8; \
} while (0)
#define UINT64DECODE(p, n) \
do { \
/* WE DON'T CHECK FOR OVERFLOW! */ \
size_t _i; \
n = 0; \
(p) += 8; \
for (_i = 0; _i < sizeof(uint64_t); _i++) \
n = (n << 8) | *(--p); \
(p) += 8; \
} while (0)
/* Decode a variable-sized buffer */
/* (Assumes that the high bits of the integer will be zero) */
#define DECODE_VAR(p, n, l) \
do { \
size_t _i; \
n = 0; \
(p) += l; \
for (_i = 0; _i < l; _i++) \
n = (n << 8) | *(--p); \
(p) += l; \
} while (0)
/* Decode a variable-sized buffer into a 64-bit unsigned integer */
/* (Assumes that the high bits of the integer will be zero) */
#define UINT64DECODE_VAR(p, n, l) DECODE_VAR(p, n, l)
/* Multiply two 128 bit unsigned integers to yield a 128 bit unsigned integer */
static void
duuid_mult128(uint64_t x_lo, uint64_t x_hi, uint64_t y_lo, uint64_t y_hi,
uint64_t *ans_lo, uint64_t *ans_hi)
{
uint64_t xlyl;
uint64_t xlyh;
uint64_t xhyl;
uint64_t xhyh;
uint64_t temp;
/** First calculate x_lo * y_lo */
/*
* Compute 64 bit results of multiplication of each combination of high
* and low 32 bit sections of x_lo and y_lo.
*/
xlyl = (x_lo & 0xffffffff) * (y_lo & 0xffffffff);
xlyh = (x_lo & 0xffffffff) * (y_lo >> 32);
xhyl = (x_lo >> 32) * (y_lo & 0xffffffff);
xhyh = (x_lo >> 32) * (y_lo >> 32);
/* Calculate lower 32 bits of the answer */
*ans_lo = xlyl & 0xffffffff;
/*
* Calculate second 32 bits of the answer. Use temp to keep a 64 bit
* result of the calculation for these 32 bits, to keep track of
* overflow past these 32 bits.
*/
temp = (xlyl >> 32) + (xlyh & 0xffffffff) + (xhyl & 0xffffffff);
*ans_lo += temp << 32;
/*
* Calculate third 32 bits of the answer, including overflowed result
* from the previous operation.
*/
temp >>= 32;
temp += (xlyh >> 32) + (xhyl >> 32) + (xhyh & 0xffffffff);
*ans_hi = temp & 0xffffffff;
/*
* Calculate highest 32 bits of the answer. No need to keep track of
* overflow because it has overflowed past the end of the 128 bit
* answer.
*/
temp >>= 32;
temp += (xhyh >> 32);
*ans_hi += temp << 32;
/*
* Now add the results from multiplying x_lo * y_hi and x_hi * y_lo. No
* need to consider overflow here, and no need to consider x_hi * y_hi
* because those results would overflow past the end of the 128 bit
* answer.
*/
*ans_hi += (x_lo * y_hi) + (x_hi * y_lo);
} /* end duuid_mult128() */
/* Implementation of the FNV hash algorithm */
static void
duuid_hash128(const char *name, void *hash, uint64_t *hi, uint64_t *lo)
{
const uint8_t *name_p = (const uint8_t *)name;
uint8_t *hash_p = (uint8_t *)hash;
uint64_t name_lo;
uint64_t name_hi;
/* Initialize hash value in accordance with the FNV algorithm */
uint64_t hash_lo = 0x62b821756295c58d;
uint64_t hash_hi = 0x6c62272e07bb0142;
/* Initialize FNV prime number in accordance with the FNV algorithm */
const uint64_t fnv_prime_lo = 0x13b;
const uint64_t fnv_prime_hi = 0x1000000;
size_t name_len_rem = strlen(name);
while (name_len_rem > 0) {
/*
* "Decode" lower 64 bits of this 128 bit section of the name,
* so the numberical value of the integer is the same on both
* little endian and big endian systems.
*/
if (name_len_rem >= 8) {
UINT64DECODE(name_p, name_lo);
name_len_rem -= 8;
} /* end if */
else {
name_lo = 0;
UINT64DECODE_VAR(name_p, name_lo, name_len_rem);
name_len_rem = 0;
} /* end else */
/* "Decode" second 64 bits */
if (name_len_rem > 0) {
if (name_len_rem >= 8) {
UINT64DECODE(name_p, name_hi);
name_len_rem -= 8;
} /* end if */
else {
name_hi = 0;
UINT64DECODE_VAR(name_p, name_hi, name_len_rem);
name_len_rem = 0;
} /* end else */
} /* end if */
else
name_hi = 0;
/*
* FNV algorithm - XOR hash with name then multiply by
* fnv_prime.
*/
hash_lo ^= name_lo;
hash_hi ^= name_hi;
duuid_mult128(hash_lo, hash_hi, fnv_prime_lo, fnv_prime_hi,
&hash_lo, &hash_hi);
} /* end while */
/*
* "Encode" hash integers to char buffer, so the buffer is the same on
* both little endian and big endian systems.
*/
UINT64ENCODE(hash_p, hash_lo);
UINT64ENCODE(hash_p, hash_hi);
if (hi)
*hi = hash_hi;
if (lo)
*lo = hash_lo;
} /* end duuid_hash128() */
static int
error_convert(int error)
{
switch (error) {
case 0:
return 0;
case -DER_NO_PERM:
case -DER_EP_RO:
case -DER_EP_OLD:
return -EPERM;
case -DER_NO_HDL:
case -DER_ENOENT:
case -DER_NONEXIST:
return -ENOENT;
case -DER_INVAL:
case -DER_NOTYPE:
case -DER_NOSCHEMA:
case -DER_NOLOCAL:
case -DER_KEY2BIG:
case -DER_REC2BIG:
case -DER_IO_INVAL:
return -EINVAL;
case -DER_EXIST:
return -EEXIST;
case -DER_UNREACH:
return -ENXIO;
case -DER_NOSPACE:
return -ENOSPC;
case -DER_ALREADY:
return -EALREADY;
case -DER_NOMEM:
return -ENOMEM;
case -DER_TIMEDOUT:
return -ETIMEDOUT;
case -DER_BUSY:
case -DER_EQ_BUSY:
return -EBUSY;
case -DER_AGAIN:
return -EAGAIN;
case -DER_PROTO:
return -EPROTO;
case -DER_IO:
return -EIO;
case -DER_CANCELED:
return -ECANCELED;
case -DER_OVERFLOW:
return -EOVERFLOW;
case -DER_BADPATH:
case -DER_NOTDIR:
return -ENOTDIR;
case -DER_STALE:
return -ESTALE;
case -DER_OOG:
case -DER_HG:
case -DER_UNREG:
case -DER_PMIX:
case -DER_MISC:
case -DER_NOTATTACH:
case -DER_NOREPLY:
return error;
default:
return error;
}
}
static int
parse_filename(const char *path, char **_obj_name, char **_cont_name)
{
char *f1 = NULL;
char *f2 = NULL;
char *fname = NULL;
char *cont_name = NULL;
int rc = 0;
if (path == NULL || _obj_name == NULL || _cont_name == NULL)
return -EINVAL;
if (strcmp(path, "/") == 0) {
*_cont_name = strdup("/");
if (*_cont_name == NULL)
return -ENOMEM;
*_obj_name = NULL;
return 0;
}
f1 = strdup(path);
if (f1 == NULL)
D_GOTO(out, rc = -ENOMEM);
f2 = strdup(path);
if (f2 == NULL)
D_GOTO(out, rc = -ENOMEM);
fname = basename(f1);
cont_name = dirname(f2);
if (cont_name[0] == '.' || cont_name[0] != '/') {
char cwd[1024];
if (getcwd(cwd, 1024) == NULL)
D_GOTO(out, rc = -ENOMEM);
if (strcmp(cont_name, ".") == 0) {
cont_name = strdup(cwd);
if (cont_name == NULL)
D_GOTO(out, rc = -ENOMEM);
} else {
char *new_dir = calloc(strlen(cwd) + strlen(cont_name)
+ 1, sizeof(char));
if (new_dir == NULL)
D_GOTO(out, rc = -ENOMEM);
strcpy(new_dir, cwd);
if (cont_name[0] == '.') {
strcat(new_dir, &cont_name[1]);
} else {
strcat(new_dir, "/");
strcat(new_dir, cont_name);
}
cont_name = new_dir;
}
*_cont_name = cont_name;
} else {
*_cont_name = strdup(cont_name);
if (*_cont_name == NULL)
D_GOTO(out, rc = -ENOMEM);
}
*_obj_name = strdup(fname);
if (*_obj_name == NULL) {
free(*_cont_name);
*_cont_name = NULL;
D_GOTO(out, rc = -ENOMEM);
}
out:
if (f1)
free(f1);
if (f2)
free(f2);
return rc;
}
static int
dfuse_access(const char *path, int mask)
{
char *name = NULL, *dir_name = NULL;
dfs_obj_t *parent = NULL;
mode_t pmode;
int rc;
FUNC_ENTER("path = %s\n", path);
if (path == NULL)
return -ENOENT;
rc = parse_filename(path, &name, &dir_name);
if (rc)
return rc;
if (strcmp(dir_name, "/") != 0) {
rc = dfs_lookup(dfs, dir_name, O_RDONLY, &parent, &pmode);
if (rc) {
fprintf(stderr, "Failed to lookup path %s (%d)\n",
dir_name, rc);
D_GOTO(out, rc);
}
if (!S_ISDIR(pmode)) {
fprintf(stderr, "%s does not resolve to a dir\n",
dir_name);
D_GOTO(out, rc = -DER_INVAL);
}
}
rc = dfs_access(dfs, parent, name, mask);
out:
if (name)
free(name);
if (dir_name)
free(dir_name);
if (parent)
dfs_release(parent);
return error_convert(rc);
}
static int
dfuse_chmod(const char *path, mode_t mode, struct fuse_file_info *fi)
{
char *name = NULL, *dir_name = NULL;
dfs_obj_t *parent = NULL;
mode_t pmode;
int rc;
FUNC_ENTER("path = %s\n", path);
#if 0
/** TODO - maybe add a dfs_fchmod() for this */
if (fi != NULL) {
rc = dfs_chmod(dfs, (dfs_obj_t *)fi->fh, mode);
return error_convert(rc);
}
#endif
if (path == NULL)
return -ENOENT;
rc = parse_filename(path, &name, &dir_name);
if (rc)
return rc;
if (strcmp(dir_name, "/") != 0) {
rc = dfs_lookup(dfs, dir_name, O_RDWR, &parent, &pmode);
if (rc) {
fprintf(stderr, "Failed to lookup path %s (%d)\n",
dir_name, rc);
D_GOTO(out, rc);
}
if (!S_ISDIR(pmode)) {
fprintf(stderr, "%s does not resolve to a dir\n",
dir_name);
D_GOTO(out, rc = -DER_INVAL);
}
}
rc = dfs_chmod(dfs, parent, name, mode);
out:
if (name)
free(name);
if (dir_name)
free(dir_name);
if (parent)
dfs_release(parent);
return error_convert(rc);
}
static int
dfuse_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi)
{
char *name = NULL, *dir_name = NULL;
dfs_obj_t *parent = NULL;
mode_t pmode;
int rc;
FUNC_ENTER("path = %s\n", path);
if (fi != NULL) {
rc = dfs_ostat(dfs, (dfs_obj_t *)fi->fh, stbuf);
return error_convert(rc);
}
if (path == NULL)
return -ENOENT;
rc = parse_filename(path, &name, &dir_name);
if (rc)
return rc;
rc = dfs_lookup(dfs, dir_name, O_RDONLY, &parent, &pmode);
if (rc) {
fprintf(stderr, "Failed path lookup %s (%d)\n", dir_name, rc);
D_GOTO(out, rc);
}
if (!S_ISDIR(pmode)) {
fprintf(stderr, "%s does not resolve to a dir\n", dir_name);
D_GOTO(out, rc = -DER_INVAL);
}
rc = dfs_stat(dfs, parent, name, stbuf);
if (rc)
D_GOTO(out, rc);
out:
if (name)
free(name);
if (dir_name)
free(dir_name);
if (parent)
dfs_release(parent);
return error_convert(rc);
}
static int
dfuse_truncate(const char *path, off_t size, struct fuse_file_info *fi)
{
dfs_obj_t *obj = NULL;
int rc;
FUNC_ENTER("path = %s\n", path);
if (fi != NULL) {
rc = dfs_punch(dfs, (dfs_obj_t *)fi->fh, size, DFS_MAX_FSIZE);
return error_convert(rc);
}
if (path == NULL)
return -ENOENT;
rc = dfs_lookup(dfs, path, O_RDWR, &obj, NULL);
if (rc)
D_GOTO(out, rc);
rc = dfs_punch(dfs, obj, size, DFS_MAX_FSIZE);
if (rc)
D_GOTO(out, rc);
out:
if (obj)
dfs_release(obj);
return error_convert(rc);
}
#define NUM_DIRENTS 10
static int
dfuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi,
enum fuse_readdir_flags flags)
{
dfs_obj_t *obj = NULL;
bool release = false;
daos_anchor_t anchor = {0};
int rc;
(void) offset;
(void) fi;
FUNC_ENTER("path = %s\n", path);
if (fi != NULL) {
obj = (dfs_obj_t *)fi->fh;
} else {
if (path == NULL)
return -ENOENT;
rc = dfs_lookup(dfs, path, O_RDONLY, &obj, NULL);
if (rc) {
fprintf(stderr, "Failed to lookup path %s (%d)\n",
path, rc);
return error_convert(rc);
}
release = true;
}
filler(buf, ".", NULL, 0, 0);
filler(buf, "..", NULL, 0, 0);
while (!daos_anchor_is_eof(&anchor)) {
uint32_t i, nr = NUM_DIRENTS;
struct dirent dirs[NUM_DIRENTS];
rc = dfs_readdir(dfs, obj, &anchor, &nr, dirs);
if (rc) {
fprintf(stderr, "Failed to iterate path %s (%d)\n",
path, rc);
D_GOTO(out, rc);
}
for (i = 0; i < nr; i++)
filler(buf, dirs[i].d_name, NULL, 0, 0);
}
out:
if (release && obj)
dfs_release(obj);
return error_convert(rc);
}
static int
dfuse_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
char *name = NULL, *dir_name = NULL;
dfs_obj_t *obj, *parent = NULL;
mode_t pmode;
int rc;
FUNC_ENTER("path = %s\n", path);
rc = parse_filename(path, &name, &dir_name);
if (rc)
return rc;
D_ASSERT(dir_name);
if (name == NULL)
D_GOTO(out, rc = -EINVAL);
rc = dfs_lookup(dfs, dir_name, O_RDWR, &parent, &pmode);
if (rc) {
fprintf(stderr, "Failed path lookup %s (%d)\n", dir_name, rc);
D_GOTO(out, rc);
}
if (!S_ISDIR(pmode)) {
fprintf(stderr, "%s does not resolve to a dir\n", dir_name);
D_GOTO(out, rc = -DER_INVAL);
}
mode = S_IFREG | mode;
rc = dfs_open(dfs, parent, name, mode, fi->flags, DAOS_OC_LARGE_RW,
NULL, &obj);
if (rc)
D_GOTO(out, rc);
fi->direct_io = 1;
fi->fh = (uint64_t)obj;
out:
if (name)
free(name);
if (dir_name)
free(dir_name);
if (parent)
dfs_release(parent);
return error_convert(rc);
}
static int
dfuse_open(const char *path, struct fuse_file_info *fi)
{
char *name = NULL, *dir_name = NULL;
dfs_obj_t *obj, *parent = NULL;
mode_t pmode;
int rc;
FUNC_ENTER("path = %s\n", path);
rc = parse_filename(path, &name, &dir_name);
if (rc)
return rc;
D_ASSERT(dir_name);
if (name == NULL)
D_GOTO(out, rc = -EINVAL);
rc = dfs_lookup(dfs, dir_name, O_RDWR, &parent, &pmode);
if (rc) {
fprintf(stderr, "Failed path lookup %s (%d)\n", dir_name, rc);
D_GOTO(out, rc);
}
if (!S_ISDIR(pmode)) {
fprintf(stderr, "%s does not resolve to a dir\n", dir_name);
D_GOTO(out, rc = -DER_INVAL);
}
rc = dfs_open(dfs, parent, name, S_IFREG, fi->flags, 0, NULL, &obj);
if (rc)
D_GOTO(out, rc);
fi->direct_io = 1;
fi->fh = (uint64_t)obj;
out:
if (name)
free(name);
if (dir_name)
free(dir_name);
if (parent)
dfs_release(parent);
return error_convert(rc);
}
static int
dfuse_opendir(const char *path, struct fuse_file_info *fi)
{
char *name = NULL, *dir_name = NULL;
dfs_obj_t *obj, *parent = NULL;
mode_t pmode;
bool free_parent = true;
int rc;
FUNC_ENTER("path = %s\n", path);
rc = parse_filename(path, &name, &dir_name);
if (rc)
return rc;
D_ASSERT(dir_name);
rc = dfs_lookup(dfs, dir_name, O_RDONLY, &parent, &pmode);
if (rc) {
fprintf(stderr, "Failed path lookup %s (%d)\n", dir_name, rc);
D_GOTO(out, rc);
}
if (!S_ISDIR(pmode)) {
fprintf(stderr, "%s does not resolve to a dir\n", dir_name);
D_GOTO(out, rc = -DER_INVAL);
}
if (name == NULL) {
fi->fh = (uint64_t)parent;
free_parent = false;
} else {
rc = dfs_open(dfs, parent, name, S_IFDIR, O_RDONLY, 0, NULL,
&obj);
if (rc)
D_GOTO(out, rc);
fi->fh = (uint64_t)obj;
}
out:
if (name)
free(name);
if (dir_name)
free(dir_name);
if (free_parent && parent)
dfs_release(parent);
return error_convert(rc);
}
static int
dfuse_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
dfs_obj_t *obj;
daos_size_t actual;
daos_iov_t iov;
daos_sg_list_t sgl;
int rc;
FUNC_ENTER("path = %s\n", path);
D_ASSERT(fi != NULL);
obj = (dfs_obj_t *)fi->fh;
/** set memory location */
sgl.sg_nr = 1;
sgl.sg_nr_out = 0;
daos_iov_set(&iov, buf, size);
sgl.sg_iovs = &iov;
rc = dfs_read(dfs, obj, sgl, offset, &actual);
if (rc)
return error_convert(rc);
return actual;
}
static int
dfuse_write(const char *path, const char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
dfs_obj_t *obj;
daos_iov_t iov;
daos_sg_list_t sgl;
int rc;
FUNC_ENTER("path = %s\n", path);
D_ASSERT(fi != NULL);
obj = (dfs_obj_t *)fi->fh;
/** set memory location */
sgl.sg_nr = 1;
sgl.sg_nr_out = 0;
daos_iov_set(&iov, (void *)buf, size);
sgl.sg_iovs = &iov;
rc = dfs_write(dfs, obj, sgl, offset);
if (rc)
return error_convert(rc);
return size;
}
static int
dfuse_mkdir(const char *path, mode_t mode)
{
dfs_obj_t *parent = NULL;
mode_t pmode;
char *name = NULL, *dir_name = NULL;
int rc;
FUNC_ENTER("path = %s\n", path);
rc = parse_filename(path, &name, &dir_name);
if (rc)
return rc;
D_ASSERT(dir_name);
if (name == NULL)
D_GOTO(out, rc = -EINVAL);
rc = dfs_lookup(dfs, dir_name, O_RDWR, &parent, &pmode);
if (rc) {
fprintf(stderr, "Failed path lookup %s (%d)\n", dir_name, rc);
D_GOTO(out, rc);
}
if (!S_ISDIR(pmode)) {
fprintf(stderr, "%s does not resolve to a dir\n", dir_name);
D_GOTO(out, rc = -DER_INVAL);
}
rc = dfs_mkdir(dfs, parent, name, mode);
if (rc)
D_GOTO(out, rc);
out:
if (name)
free(name);
if (dir_name)
free(dir_name);
if (parent)
dfs_release(parent);
return error_convert(rc);
}
static int
dfuse_symlink(const char *from, const char *to)
{
dfs_obj_t *parent = NULL;
mode_t pmode;
dfs_obj_t *sym;
char *name = NULL, *dir_name = NULL;
int rc;
FUNC_ENTER("from = %s, to = %s\n", from, to);
rc = parse_filename(to, &name, &dir_name);
if (rc)
return rc;
D_ASSERT(dir_name);
if (name == NULL)
D_GOTO(out, rc = -EINVAL);
rc = dfs_lookup(dfs, dir_name, O_RDWR, &parent, &pmode);
if (rc) {
fprintf(stderr, "Failed path lookup %s (%d)\n", dir_name, rc);
D_GOTO(out, rc);
}
if (!S_ISDIR(pmode)) {
fprintf(stderr, "%s does not resolve to a dir\n", dir_name);
D_GOTO(out, rc = -DER_INVAL);
}
rc = dfs_open(dfs, parent, name, S_IFLNK, O_CREAT, 0, from, &sym);
if (rc)
D_GOTO(out, rc);
dfs_release(sym);
out:
if (name)
free(name);
if (dir_name)
free(dir_name);
if (parent)
dfs_release(parent);
return error_convert(rc);
}
static int
dfuse_readlink(const char *path, char *buf, size_t size)
{
dfs_obj_t *obj = NULL;
mode_t mode;
int rc;
FUNC_ENTER("path = %s\n", path);
rc = dfs_lookup(dfs, path, O_RDONLY, &obj, &mode);
if (rc) {
fprintf(stderr, "Failed path lookup %s (%d)\n", path, rc);
D_GOTO(out, rc);
}
if (!S_ISLNK(mode)) {
fprintf(stderr, "%s does not resolve to a symlink\n", path);
D_GOTO(out, rc = -DER_INVAL);
}
rc = dfs_get_symlink_value(obj, buf, &size);
if (rc)
D_GOTO(out, rc);
out:
if (obj)
dfs_release(obj);
return error_convert(rc);
}
static int
dfuse_unlink(const char *path)
{
dfs_obj_t *parent = NULL;
mode_t pmode;
char *name = NULL, *dir_name = NULL;
int rc;
FUNC_ENTER("path = %s\n", path);
rc = parse_filename(path, &name, &dir_name);
if (rc)
return rc;
D_ASSERT(dir_name);
if (name == NULL)
D_GOTO(out, rc = -EINVAL);
rc = dfs_lookup(dfs, dir_name, O_RDWR, &parent, &pmode);
if (rc) {
fprintf(stderr, "Failed path lookup %s (%d)\n", dir_name, rc);
D_GOTO(out, rc);
}
if (!S_ISDIR(pmode)) {
fprintf(stderr, "%s does not resolve to a dir\n", dir_name);
D_GOTO(out, rc = -DER_INVAL);
}
rc = dfs_remove(dfs, parent, name, false);
if (rc) {
fprintf(stderr, "Failed to remove file %s (%d)\n", name, rc);
D_GOTO(out, rc);
}
out:
if (name)
free(name);
if (dir_name)
free(dir_name);
if (parent)
dfs_release(parent);
return error_convert(rc);
}
static int
dfuse_rmdir(const char *path)
{
dfs_obj_t *parent = NULL;
mode_t pmode;
char *name = NULL, *dir_name = NULL;
int rc;
FUNC_ENTER("path = %s\n", path);
rc = parse_filename(path, &name, &dir_name);
if (rc)
return rc;
D_ASSERT(dir_name);
if (name == NULL)
D_GOTO(out, rc = -EINVAL);
rc = dfs_lookup(dfs, dir_name, O_RDWR, &parent, &pmode);
if (rc) {
fprintf(stderr, "Failed path lookup %s (%d)\n", dir_name, rc);
D_GOTO(out, rc);
}
if (!S_ISDIR(pmode)) {
fprintf(stderr, "%s does not resolve to a dir\n", dir_name);
D_GOTO(out, rc = -DER_INVAL);
}
rc = dfs_remove(dfs, parent, name, false);
if (rc) {
fprintf(stderr, "Failed to remove dir %s (%d)\n", name, rc);
D_GOTO(out, rc);
}
out:
if (name)
free(name);
if (dir_name)
free(dir_name);
if (parent)
dfs_release(parent);
return error_convert(rc);
}
static int
dfuse_release(const char *path, struct fuse_file_info *fi)
{