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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
|
/* $OpenBSD: fuse_ops.c,v 1.26 2016/09/07 17:53:35 natano Exp $ */
/*
* Copyright (c) 2013 Sylvestre Gallon <ccna.syl@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "fuse_private.h"
#include "debug.h"
#define CHECK_OPT(opname) DPRINTF("Opcode:\t%s\n", #opname); \
DPRINTF("Inode:\t%llu\n", \
(unsigned long long)fbuf->fb_ino); \
if (!f->op.opname) { \
fbuf->fb_err = -ENOSYS; \
return (0); \
}
static int
update_attr(struct fuse *f, struct stat *attr, const char *realname,
struct fuse_vnode *vn)
{
int ret;
memset(attr, 0, sizeof(struct stat));
ret = f->op.getattr(realname, attr);
if (attr->st_blksize == 0)
attr->st_blksize = 512;
if (attr->st_blocks == 0)
attr->st_blocks = 4;
attr->st_ino = vn->ino;
if (f->conf.set_mode)
attr->st_mode = (attr->st_mode & S_IFMT) | (0777 & ~f->conf.umask);
if (f->conf.set_uid)
attr->st_uid = f->conf.uid;
if (f->conf.set_gid)
attr->st_gid = f->conf.gid;
return (ret);
}
static int
ifuse_ops_init(struct fuse *f)
{
struct fuse_conn_info fci;
DPRINTF("Opcode:\tinit\n");
if (f->op.init) {
bzero(&fci, sizeof fci);
fci.proto_minor = FUSE_MINOR_VERSION;
fci.proto_major = FUSE_MAJOR_VERSION;
f->op.init(&fci);
}
return (0);
}
static int
ifuse_ops_getattr(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_vnode *vn;
char *realname;
DPRINTF("Opcode:\tgetattr\n");
DPRINTF("Inode:\t%llu\n", (unsigned long long)fbuf->fb_ino);
memset(&fbuf->fb_attr, 0, sizeof(struct stat));
vn = tree_get(&f->vnode_tree, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
return (0);
}
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
return (0);
}
fbuf->fb_err = update_attr(f, &fbuf->fb_attr, realname, vn);
free(realname);
return (0);
}
static int
ifuse_ops_access(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_vnode *vn;
char *realname;
CHECK_OPT(access);
vn = tree_get(&f->vnode_tree, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
return (0);
}
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
return (0);
}
fbuf->fb_err = f->op.access(realname, fbuf->fb_io_mode);
free(realname);
return (0);
}
static int
ifuse_ops_open(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_file_info ffi;
struct fuse_vnode *vn;
char *realname;
CHECK_OPT(open);
bzero(&ffi, sizeof(ffi));
ffi.flags = fbuf->fb_io_flags;
vn = tree_get(&f->vnode_tree, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
return (0);
}
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
return (0);
}
fbuf->fb_err = f->op.open(realname, &ffi);
free(realname);
if (!fbuf->fb_err)
fbuf->fb_io_fd = ffi.fh;
return (0);
}
static int
ifuse_ops_opendir(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_file_info ffi;
struct fuse_vnode *vn;
char *realname;
DPRINTF("Opcode:\topendir\n");
DPRINTF("Inode:\t%llu\n", (unsigned long long)fbuf->fb_ino);
memset(&ffi, 0, sizeof(ffi));
ffi.flags = fbuf->fb_io_flags;
vn = tree_get(&f->vnode_tree, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
return (0);
}
if (f->op.opendir) {
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
return (0);
}
fbuf->fb_err = f->op.opendir(realname, &ffi);
free(realname);
}
if (!fbuf->fb_err) {
fbuf->fb_io_fd = ffi.fh;
vn->fd = calloc(1, sizeof(*vn->fd));
if (vn->fd == NULL) {
fbuf->fb_err = -errno;
return (0);
}
vn->fd->filled = 0;
vn->fd->size = 0;
vn->fd->start = 0;
}
return (0);
}
#define GENERIC_DIRSIZ(NLEN) \
((sizeof (struct dirent) - (MAXNAMLEN+1)) + ((NLEN+1 + 7) &~ 7))
static int
ifuse_fill_readdir(void *dh, const char *name, const struct stat *stbuf,
off_t off)
{
struct fuse_dirhandle *fd = dh;
struct fusebuf *fbuf;
struct dirent *dir;
uint32_t namelen;
uint32_t len;
fbuf = fd->buf;
namelen = strnlen(name, MAXNAMLEN);
len = GENERIC_DIRSIZ(namelen);
if (fd->full || (fbuf->fb_len + len > fd->size)) {
fd->full = 1;
return (0);
}
if (fd->start != 0 && fd->idx < fd->start) {
fd->idx += len;
return (0);
}
dir = (struct dirent *) &fbuf->fb_dat[fbuf->fb_len];
if (off)
fd->filled = 0;
if (stbuf) {
dir->d_fileno = stbuf->st_ino;
dir->d_type = IFTODT(stbuf->st_mode);
} else {
dir->d_fileno = 0xffffffff;
dir->d_type = DT_UNKNOWN;
}
dir->d_reclen = len;
dir->d_off = off + len; /* XXX */
strlcpy(dir->d_name, name, sizeof(dir->d_name));
dir->d_namlen = strlen(dir->d_name);
fbuf->fb_len += len;
fd->start += len;
fd->idx += len;
return (0);
}
static int
ifuse_fill_getdir(fuse_dirh_t fd, const char *name, int type, ino_t ino)
{
struct stat st;
bzero(&st, sizeof(st));
st.st_mode = type << 12;
if (ino == 0)
st.st_ino = 0xffffffff;
else
st.st_ino = ino;
return (fd->filler(fd, name, &st, 0));
}
static int
ifuse_ops_readdir(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_file_info ffi;
struct fuse_vnode *vn;
char *realname;
uint64_t offset;
uint32_t size;
uint32_t startsave;
DPRINTF("Opcode:\treaddir\n");
DPRINTF("Inode:\t%llu\n", (unsigned long long)fbuf->fb_ino);
DPRINTF("Offset:\t%llu\n", fbuf->fb_io_off);
DPRINTF("Size:\t%lu\n", fbuf->fb_io_len);
bzero(&ffi, sizeof(ffi));
ffi.fh = fbuf->fb_io_fd;
offset = fbuf->fb_io_off;
size = fbuf->fb_io_len;
startsave = 0;
fbuf->fb_dat = calloc(1, size);
if (fbuf->fb_dat == NULL) {
fbuf->fb_err = -errno;
return (0);
}
vn = tree_get(&f->vnode_tree, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
if (!vn->fd->filled) {
vn->fd->filler = ifuse_fill_readdir;
vn->fd->buf = fbuf;
vn->fd->filled = 0;
vn->fd->full = 0;
vn->fd->size = size;
vn->fd->off = offset;
vn->fd->idx = 0;
startsave = vn->fd->start;
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
if (f->op.readdir)
fbuf->fb_err = f->op.readdir(realname, vn->fd,
ifuse_fill_readdir, offset, &ffi);
else if (f->op.getdir)
fbuf->fb_err = f->op.getdir(realname, vn->fd,
ifuse_fill_getdir);
else
fbuf->fb_err = -ENOSYS;
free(realname);
}
if (!vn->fd->full && vn->fd->start == startsave)
vn->fd->filled = 1;
if (fbuf->fb_err) {
fbuf->fb_len = 0;
vn->fd->filled = 1;
}
if (fbuf->fb_len == 0)
free(fbuf->fb_dat);
return (0);
}
static int
ifuse_ops_releasedir(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_file_info ffi;
struct fuse_vnode *vn;
char *realname;
DPRINTF("Opcode:\treleasedir\n");
DPRINTF("Inode:\t%llu\n", (unsigned long long)fbuf->fb_ino);
bzero(&ffi, sizeof(ffi));
ffi.fh = fbuf->fb_io_fd;
ffi.fh_old = ffi.fh;
ffi.flags = fbuf->fb_io_flags;
vn = tree_get(&f->vnode_tree, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
return (0);
}
if (f->op.releasedir) {
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
return (0);
}
fbuf->fb_err = f->op.releasedir(realname, &ffi);
free(realname);
}
if (!fbuf->fb_err)
free(vn->fd);
return (0);
}
static int
ifuse_ops_release(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_file_info ffi;
struct fuse_vnode *vn;
char *realname;
CHECK_OPT(release);
bzero(&ffi, sizeof(ffi));
ffi.fh = fbuf->fb_io_fd;
ffi.fh_old = ffi.fh;
ffi.flags = fbuf->fb_io_flags;
vn = tree_get(&f->vnode_tree, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
return (0);
}
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
return (0);
}
fbuf->fb_err = f->op.release(realname, &ffi);
free(realname);
return (0);
}
static int
ifuse_ops_lookup(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_vnode *vn;
char *realname;
DPRINTF("Opcode:\tlookup\n");
DPRINTF("Inode:\t%llu\n", (unsigned long long)fbuf->fb_ino);
DPRINTF("For file %s\n", fbuf->fb_dat);
if (strcmp((const char *)fbuf->fb_dat, "..") == 0) {
vn = tree_get(&f->vnode_tree, fbuf->fb_ino);
if (vn == NULL || vn->parent == NULL) {
fbuf->fb_err = -ENOENT;
return (0);
}
vn = vn->parent;
if (vn->ino != FUSE_ROOT_INO)
ref_vn(vn);
} else {
vn = get_vn_by_name_and_parent(f, fbuf->fb_dat, fbuf->fb_ino);
if (vn == NULL) {
vn = alloc_vn(f, (const char *)fbuf->fb_dat, -1,
fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
set_vn(f, vn); /*XXX*/
} else if (vn->ino != FUSE_ROOT_INO)
ref_vn(vn);
}
DPRINTF("new ino %llu\n", (unsigned long long)vn->ino);
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
fbuf->fb_err = update_attr(f, &fbuf->fb_attr, realname, vn);
free(fbuf->fb_dat);
free(realname);
return (0);
}
static int
ifuse_ops_read(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_file_info ffi;
struct fuse_vnode *vn;
char *realname;
uint64_t offset;
uint32_t size;
int ret;
CHECK_OPT(read);
bzero(&ffi, sizeof(ffi));
ffi.fh = fbuf->fb_io_fd;
size = fbuf->fb_io_len;
offset = fbuf->fb_io_off;
fbuf->fb_dat = malloc(size);
if (fbuf->fb_dat == NULL) {
fbuf->fb_err = -errno;
return (0);
}
vn = tree_get(&f->vnode_tree, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
ret = f->op.read(realname, (char *)fbuf->fb_dat, size, offset, &ffi);
free(realname);
if (ret >= 0)
fbuf->fb_len = ret;
else
fbuf->fb_err = ret;
if (fbuf->fb_len == 0)
free(fbuf->fb_dat);
return (0);
}
static int
ifuse_ops_write(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_file_info ffi;
struct fuse_vnode *vn;
char *realname;
uint64_t offset;
uint32_t size;
int ret;
CHECK_OPT(write);
bzero(&ffi, sizeof(ffi));
ffi.fh = fbuf->fb_io_fd;
ffi.fh_old = ffi.fh;
ffi.writepage = fbuf->fb_io_flags & 1;
size = fbuf->fb_io_len;
offset = fbuf->fb_io_off;
vn = tree_get(&f->vnode_tree, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
ret = f->op.write(realname, (char *)fbuf->fb_dat, size, offset, &ffi);
free(realname);
free(fbuf->fb_dat);
if (ret >= 0)
fbuf->fb_io_len = ret;
else
fbuf->fb_err = ret;
return (0);
}
static int
ifuse_ops_create(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_file_info ffi;
struct fuse_vnode *vn;
uint32_t mode;
char *realname;
DPRINTF("Opcode:\tcreate\n");
DPRINTF("Inode:\t%llu\n", (unsigned long long)fbuf->fb_ino);
bzero(&ffi, sizeof(ffi));
ffi.flags = fbuf->fb_io_flags;
mode = fbuf->fb_io_mode;
vn = get_vn_by_name_and_parent(f, fbuf->fb_dat, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
free(fbuf->fb_dat);
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
return (0);
}
if (f->op.create)
fbuf->fb_err = f->op.create(realname, mode, &ffi);
else if (f->op.mknod)
fbuf->fb_err = f->op.mknod(realname, S_IFREG | mode, 0);
else
fbuf->fb_err = -ENOSYS;
if (!fbuf->fb_err) {
fbuf->fb_err = update_attr(f, &fbuf->fb_attr, realname, vn);
fbuf->fb_ino = fbuf->fb_attr.st_ino;
fbuf->fb_io_mode = fbuf->fb_attr.st_mode;
}
free(realname);
return (0);
}
static int
ifuse_ops_mkdir(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_vnode *vn;
char *realname;
uint32_t mode;
CHECK_OPT(mkdir);
mode = fbuf->fb_io_mode;
vn = get_vn_by_name_and_parent(f, fbuf->fb_dat, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
free(fbuf->fb_dat);
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
return (0);
}
fbuf->fb_err = f->op.mkdir(realname, mode);
if (!fbuf->fb_err) {
fbuf->fb_err = update_attr(f, &fbuf->fb_attr, realname, vn);
fbuf->fb_io_mode = fbuf->fb_attr.st_mode;
fbuf->fb_ino = vn->ino;
}
free(realname);
return (0);
}
static int
ifuse_ops_rmdir(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_vnode *vn;
char *realname;
CHECK_OPT(rmdir);
vn = get_vn_by_name_and_parent(f, fbuf->fb_dat, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
free(fbuf->fb_dat);
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
return (0);
}
fbuf->fb_err = f->op.rmdir(realname);
free(realname);
return (0);
}
static int
ifuse_ops_readlink(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_vnode *vn;
char *realname;
char name[PATH_MAX + 1];
int len, ret;
DPRINTF("Opcode:\treadlink\n");
DPRINTF("Inode:\t%llu\n", (unsigned long long)fbuf->fb_ino);
vn = tree_get(&f->vnode_tree, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
return (0);
}
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
return (0);
}
if (f->op.readlink)
ret = f->op.readlink(realname, name, sizeof(name));
else
ret = -ENOSYS;
free(realname);
fbuf->fb_err = ret;
if (!ret) {
len = strnlen(name, PATH_MAX);
fbuf->fb_len = len;
fbuf->fb_dat = malloc(fbuf->fb_len);
if (fbuf->fb_dat == NULL) {
fbuf->fb_err = -errno;
return (0);
}
memcpy(fbuf->fb_dat, name, len);
} else
fbuf->fb_len = 0;
return (0);
}
static int
ifuse_ops_unlink(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_vnode *vn;
char *realname;
CHECK_OPT(unlink);
vn = get_vn_by_name_and_parent(f, fbuf->fb_dat, fbuf->fb_ino);
if (vn == NULL) {
free(fbuf->fb_dat);
fbuf->fb_err = -errno;
return (0);
}
free(fbuf->fb_dat);
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
return (0);
}
fbuf->fb_err = f->op.unlink(realname);
free(realname);
return (0);
}
static int
ifuse_ops_statfs(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_vnode *vn;
char *realname;
bzero(&fbuf->fb_stat, sizeof(fbuf->fb_stat));
CHECK_OPT(statfs);
vn = tree_get(&f->vnode_tree, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
return (0);
}
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
return (0);
}
fbuf->fb_err = f->op.statfs(realname, &fbuf->fb_stat);
free(realname);
return (0);
}
static int
ifuse_ops_link(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_vnode *vn;
char *realname;
char *realname_ln;
ino_t oldnodeid;
CHECK_OPT(link);
oldnodeid = fbuf->fb_io_ino;
vn = get_vn_by_name_and_parent(f, fbuf->fb_dat, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
free(fbuf->fb_dat);
realname = build_realname(f, oldnodeid);
if (realname == NULL) {
fbuf->fb_err = -errno;
return (0);
}
realname_ln = build_realname(f, vn->ino);
if (realname_ln == NULL) {
fbuf->fb_err = -errno;
free(realname);
return (0);
}
fbuf->fb_err = f->op.link(realname, realname_ln);
free(realname);
free(realname_ln);
return (0);
}
static int
ifuse_ops_setattr(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_vnode *vn;
struct timespec ts[2];
struct utimbuf tbuf;
struct fb_io *io;
char *realname;
uid_t uid;
gid_t gid;
DPRINTF("Opcode:\tsetattr\n");
DPRINTF("Inode:\t%llu\n", (unsigned long long)fbuf->fb_ino);
vn = tree_get(&f->vnode_tree, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
io = fbtod(fbuf, struct fb_io *);
if (io->fi_flags & FUSE_FATTR_MODE) {
if (f->op.chmod)
fbuf->fb_err = f->op.chmod(realname,
fbuf->fb_attr.st_mode);
else
fbuf->fb_err = -ENOSYS;
}
if (!fbuf->fb_err && (io->fi_flags & FUSE_FATTR_UID ||
io->fi_flags & FUSE_FATTR_GID) ) {
uid = (io->fi_flags & FUSE_FATTR_UID) ?
fbuf->fb_attr.st_uid : (gid_t)-1;
gid = (io->fi_flags & FUSE_FATTR_GID) ?
fbuf->fb_attr.st_gid : (uid_t)-1;
if (f->op.chown)
fbuf->fb_err = f->op.chown(realname, uid, gid);
else
fbuf->fb_err = -ENOSYS;
}
if (!fbuf->fb_err && ( io->fi_flags & FUSE_FATTR_MTIME ||
io->fi_flags & FUSE_FATTR_ATIME)) {
ts[0] = fbuf->fb_attr.st_atim;
ts[1] = fbuf->fb_attr.st_mtim;
tbuf.actime = ts[0].tv_sec;
tbuf.modtime = ts[1].tv_sec;
if (f->op.utimens)
fbuf->fb_err = f->op.utimens(realname, ts);
else if (f->op.utime)
fbuf->fb_err = f->op.utime(realname, &tbuf);
else
fbuf->fb_err = -ENOSYS;
}
if (!fbuf->fb_err && (io->fi_flags & FUSE_FATTR_SIZE)) {
if (f->op.truncate)
fbuf->fb_err = f->op.truncate(realname,
fbuf->fb_attr.st_size);
else
fbuf->fb_err = -ENOSYS;
}
memset(&fbuf->fb_attr, 0, sizeof(struct stat));
if (!fbuf->fb_err)
fbuf->fb_err = update_attr(f, &fbuf->fb_attr, realname, vn);
free(realname);
free(fbuf->fb_dat);
return (0);
}
static int
ifuse_ops_symlink(unused struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_vnode *vn;
char *realname;
int len;
CHECK_OPT(symlink);
vn = get_vn_by_name_and_parent(f, fbuf->fb_dat, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
len = strlen((char *)fbuf->fb_dat);
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
/* fuse invert the symlink params */
fbuf->fb_err = f->op.symlink((const char *)&fbuf->fb_dat[len + 1],
realname);
fbuf->fb_ino = vn->ino;
free(fbuf->fb_dat);
free(realname);
return (0);
}
static int
ifuse_ops_rename(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_vnode *vnt;
struct fuse_vnode *vnf;
char *realnamef;
char *realnamet;
int len;
CHECK_OPT(rename);
len = strlen((char *)fbuf->fb_dat);
vnf = get_vn_by_name_and_parent(f, fbuf->fb_dat, fbuf->fb_ino);
if (vnf == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
vnt = get_vn_by_name_and_parent(f, &fbuf->fb_dat[len + 1],
fbuf->fb_io_ino);
if (vnt == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
free(fbuf->fb_dat);
realnamef = build_realname(f, vnf->ino);
if (realnamef == NULL) {
fbuf->fb_err = -errno;
return (0);
}
realnamet = build_realname(f, vnt->ino);
if (realnamet == NULL) {
fbuf->fb_err = -errno;
free(realnamef);
return (0);
}
fbuf->fb_err = f->op.rename(realnamef, realnamet);
free(realnamef);
free(realnamet);
return (0);
}
static int
ifuse_ops_destroy(struct fuse *f)
{
struct fuse_context *ctx;
DPRINTF("Opcode:\tdestroy\n");
if (f->op.destroy) {
ctx = fuse_get_context();
f->op.destroy((ctx)?ctx->private_data:NULL);
}
f->fc->dead = 1;
return (0);
}
static int
ifuse_ops_reclaim(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_vnode *vn;
vn = tree_get(&f->vnode_tree, fbuf->fb_ino);
if (vn != NULL)
unref_vn(f, vn);
return (0);
}
static int
ifuse_ops_mknod(struct fuse *f, struct fusebuf *fbuf)
{
struct fuse_vnode *vn;
char *realname;
uint32_t mode;
dev_t dev;
CHECK_OPT(mknod);
mode = fbuf->fb_io_mode;
dev = fbuf->fb_io_rdev;
vn = get_vn_by_name_and_parent(f, fbuf->fb_dat, fbuf->fb_ino);
if (vn == NULL) {
fbuf->fb_err = -errno;
free(fbuf->fb_dat);
return (0);
}
free(fbuf->fb_dat);
realname = build_realname(f, vn->ino);
if (realname == NULL) {
fbuf->fb_err = -errno;
return (0);
}
fbuf->fb_err = f->op.mknod(realname, mode, dev);
if (!fbuf->fb_err) {
fbuf->fb_err = update_attr(f, &fbuf->fb_attr, realname, vn);
fbuf->fb_io_mode = fbuf->fb_attr.st_mode;
fbuf->fb_ino = fbuf->fb_attr.st_ino;
}
free(realname);
return (0);
}
int
ifuse_exec_opcode(struct fuse *f, struct fusebuf *fbuf)
{
int ret = 0;
fbuf->fb_len = 0;
fbuf->fb_err = 0;
switch (fbuf->fb_type) {
case FBT_LOOKUP:
ret = ifuse_ops_lookup(f, fbuf);
break;
case FBT_GETATTR:
ret = ifuse_ops_getattr(f, fbuf);
break;
case FBT_SETATTR:
ret = ifuse_ops_setattr(f, fbuf);
break;
case FBT_READLINK:
ret = ifuse_ops_readlink(f, fbuf);
break;
case FBT_MKDIR:
ret = ifuse_ops_mkdir(f, fbuf);
break;
case FBT_UNLINK:
ret = ifuse_ops_unlink(f, fbuf);
break;
case FBT_RMDIR:
ret = ifuse_ops_rmdir(f, fbuf);
break;
case FBT_LINK:
ret = ifuse_ops_link(f, fbuf);
break;
case FBT_OPEN:
ret = ifuse_ops_open(f, fbuf);
break;
case FBT_READ:
ret = ifuse_ops_read(f, fbuf);
break;
case FBT_WRITE:
ret = ifuse_ops_write(f, fbuf);
break;
case FBT_STATFS:
ret = ifuse_ops_statfs(f, fbuf);
break;
case FBT_RELEASE:
ret = ifuse_ops_release(f, fbuf);
break;
case FBT_INIT:
ret = ifuse_ops_init(f);
break;
case FBT_OPENDIR:
ret = ifuse_ops_opendir(f, fbuf);
break;
case FBT_READDIR:
ret = ifuse_ops_readdir(f, fbuf);
break;
case FBT_RELEASEDIR:
ret = ifuse_ops_releasedir(f, fbuf);
break;
case FBT_ACCESS:
ret = ifuse_ops_access(f, fbuf);
break;
case FBT_CREATE:
ret = ifuse_ops_create(f, fbuf);
break;
case FBT_SYMLINK:
ret = ifuse_ops_symlink(f, fbuf);
break;
case FBT_RENAME:
ret = ifuse_ops_rename(f, fbuf);
break;
case FBT_DESTROY:
ret = ifuse_ops_destroy(f);
break;
case FBT_RECLAIM:
ret = ifuse_ops_reclaim(f, fbuf);
break;
case FBT_MKNOD:
ret = ifuse_ops_mknod(f, fbuf);
break;
default:
DPRINTF("Opcode:\t%i not supported\n", fbuf->fb_type);
DPRINTF("Inode:\t%llu\n", (unsigned long long)fbuf->fb_ino);
fbuf->fb_err = -ENOSYS;
fbuf->fb_len = 0;
}
DPRINTF("\n");
/* fuse api use negative errno */
fbuf->fb_err = -fbuf->fb_err;
return (ret);
}
|