-
Notifications
You must be signed in to change notification settings - Fork 7
/
fuse_internal.c
1531 lines (1231 loc) · 40.1 KB
/
fuse_internal.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
/*
* Copyright (C) 2006-2008 Google. All Rights Reserved.
* Copyright (C) 2010 Tuxera. All Rights Reserved.
* Copyright (C) 2011 Anatol Pomozov. All Rights Reserved.
*/
#include "fuse.h"
#include "fuse_file.h"
#include "fuse_internal.h"
#include "fuse_ipc.h"
#include "fuse_locking.h"
#include "fuse_node.h"
#include "fuse_file.h"
#include "fuse_sysctl.h"
#include <AvailabilityMacros.h>
#include <kern/assert.h>
#include <libkern/libkern.h>
#include <libkern/OSMalloc.h>
#include <libkern/locks.h>
#include <mach/mach_types.h>
#include <sys/dirent.h>
#include <sys/disk.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/kernel_types.h>
#include <sys/mount.h>
#include <sys/proc.h>
#include <sys/stat.h>
#include <sys/ubc.h>
#include <sys/unistd.h>
#include <sys/vnode.h>
#include <sys/vnode_if.h>
#include <sys/xattr.h>
#include <sys/buf.h>
#include <sys/namei.h>
#include <sys/mman.h>
#include <sys/param.h>
#ifdef FUSE4X_ENABLE_BIGLOCK
#include "fuse_biglock_vnops.h"
#endif
#ifdef FUSE4X_ENABLE_EXCHANGE
# include "compat/exchange.h"
#endif
/* access */
__private_extern__
int
fuse_internal_access(vnode_t vp,
int action,
vfs_context_t context)
{
int err = 0;
uint32_t mask = 0;
int dataflags;
mount_t mp;
struct fuse_dispatcher fdi;
struct fuse_access_in *fai;
struct fuse_data *data;
fuse_trace_printf_func();
mp = vnode_mount(vp);
data = fuse_get_mpdata(mp);
dataflags = data->dataflags;
/* Allow for now; let checks be handled inline later. */
if (fuse_isdeferpermissions_mp(mp)) {
return 0;
}
/*
* (action & KAUTH_VNODE_GENERIC_WRITE_BITS) on a read-only file system
* would have been handled by higher layers.
*/
if (!fuse_implemented(data, FSESS_NOIMPLBIT(ACCESS))) {
return ENOTSUP;
}
if (!vnode_isvroot(vp)) {
CHECK_BLANKET_DENIAL(vp, context, EPERM);
}
if (vnode_isdir(vp)) {
if (action & (KAUTH_VNODE_LIST_DIRECTORY |
KAUTH_VNODE_READ_EXTATTRIBUTES)) {
mask |= R_OK;
}
if (action & (KAUTH_VNODE_ADD_FILE |
KAUTH_VNODE_ADD_SUBDIRECTORY |
KAUTH_VNODE_DELETE_CHILD)) {
mask |= W_OK;
}
if (action & KAUTH_VNODE_SEARCH) {
mask |= X_OK;
}
} else {
if (action & (KAUTH_VNODE_READ_DATA | KAUTH_VNODE_READ_EXTATTRIBUTES)) {
mask |= R_OK;
}
if (action & (KAUTH_VNODE_WRITE_DATA | KAUTH_VNODE_APPEND_DATA)) {
mask |= W_OK;
}
if (action & KAUTH_VNODE_EXECUTE) {
mask |= X_OK;
}
}
if (action & (KAUTH_VNODE_WRITE_ATTRIBUTES |
KAUTH_VNODE_WRITE_EXTATTRIBUTES |
KAUTH_VNODE_WRITE_SECURITY)) {
mask |= W_OK;
}
bzero(&fdi, sizeof(fdi));
fuse_dispatcher_init(&fdi, sizeof(*fai));
fuse_dispatcher_make_vp(&fdi, FUSE_ACCESS, vp, context);
fai = fdi.indata;
fai->mask = F_OK;
fai->mask |= mask;
if (!(err = fuse_dispatcher_wait_answer(&fdi))) {
fuse_ticket_drop(fdi.ticket);
}
if (err == ENOSYS) {
/*
* Make sure we don't come in here again.
*/
vfs_clearauthopaque(mp);
fuse_clear_implemented(data, FSESS_NOIMPLBIT(ACCESS));
err = ENOTSUP;
}
if (err == ENOENT) {
const char *vname = vnode_getname(vp);
log("fuse4x: disappearing vnode %p (name=%s type=%d action=%x)\n",
vp, (vname) ? vname : "?", vnode_vtype(vp), action);
if (vname) {
vnode_putname(vname);
}
#ifdef FUSE4X_ENABLE_BIGLOCK
fuse_biglock_unlock(data->biglock);
#endif
fuse_vncache_purge(vp);
#ifdef FUSE4X_ENABLE_BIGLOCK
fuse_biglock_lock(data->biglock);
#endif
}
return err;
}
#ifdef FUSE4X_ENABLE_EXCHANGE
/* exchange */
__private_extern__
int
fuse_internal_exchange(vnode_t fvp,
const char *fname,
size_t flen,
vnode_t tvp,
const char *tname,
size_t tlen,
int options,
vfs_context_t context)
{
struct fuse_dispatcher fdi;
struct fuse_exchange_in *fei;
struct fuse_vnode_data *ffud = VTOFUD(fvp);
struct fuse_vnode_data *tfud = VTOFUD(tvp);
vnode_t fdvp = ffud->parentvp;
vnode_t tdvp = tfud->parentvp;
int err = 0;
fuse_dispatcher_init(&fdi, sizeof(*fei) + flen + tlen + 2);
fuse_dispatcher_make_vp(&fdi, FUSE_EXCHANGE, fvp, context);
fei = fdi.indata;
fei->olddir = VTOI(fdvp);
fei->newdir = VTOI(tdvp);
fei->options = (uint64_t)options;
memcpy((char *)fdi.indata + sizeof(*fei), fname, flen);
((char *)fdi.indata)[sizeof(*fei) + flen] = '\0';
memcpy((char *)fdi.indata + sizeof(*fei) + flen + 1, tname, tlen);
((char *)fdi.indata)[sizeof(*fei) + flen + tlen + 1] = '\0';
ubc_msync(fvp, (off_t)0, (off_t)ffud->filesize, NULL,
UBC_PUSHALL | UBC_INVALIDATE | UBC_SYNC);
ubc_msync(tvp, (off_t)0, (off_t)tfud->filesize, NULL,
UBC_PUSHALL | UBC_INVALIDATE | UBC_SYNC);
if (!(err = fuse_dispatcher_wait_answer(&fdi))) {
fuse_ticket_drop(fdi.ticket);
}
if (err == 0) {
if (fdvp) {
fuse_invalidate_attr(fdvp);
}
if (tdvp && (tdvp != fdvp)) {
fuse_invalidate_attr(tdvp);
}
fuse_invalidate_attr(fvp);
fuse_invalidate_attr(tvp);
cache_purge(fvp);
cache_purge(tvp);
/* Swap sizes */
off_t tmpfilesize = ffud->filesize;
ffud->filesize = tfud->filesize;
tfud->filesize = tmpfilesize;
ubc_setsize(fvp, (off_t)ffud->filesize);
ubc_setsize(tvp, (off_t)tfud->filesize);
fuse_compat_exchange(fvp, tvp);
/*
* Another approach (will need additional kernel support to work):
*
vnode_t tmpvp = ffud->vp;
ffud->vp = tfud->vp;
tfud->vp = tmpvp;
vnode_t tmpparentvp = ffud->parentvp;
ffud->parentvp = tfud->parentvp;
tfud->parentvp = tmpparentvp;
off_t tmpfilesize = ffud->filesize;
ffud->filesize = tfud->filesize;
tfud->filesize = tmpfilesize;
struct fuse_vnode_data tmpfud;
memcpy(&tmpfud, ffud, sizeof(struct fuse_vnode_data));
memcpy(ffud, tfud, sizeof(struct fuse_vnode_data));
memcpy(tfud, &tmpfud, sizeof(struct fuse_vnode_data));
HNodeExchangeFromFSNode(ffud, tfud);
*
*/
}
return err;
}
#endif /* FUSE4X_ENABLE_EXCHANGE */
/* fsync */
__private_extern__
int
fuse_internal_fsync_callback(struct fuse_ticket *ticket, __unused uio_t uio)
{
fuse_trace_printf_func();
if (ticket->aw_ohead.error == ENOSYS) {
if (fuse_ticket_opcode(ticket) == FUSE_FSYNC) {
fuse_clear_implemented(ticket->data, FSESS_NOIMPLBIT(FSYNC));
} else if (fuse_ticket_opcode(ticket) == FUSE_FSYNCDIR) {
fuse_clear_implemented(ticket->data, FSESS_NOIMPLBIT(FSYNCDIR));
} else {
log("fuse4x: unexpected opcode in sync handling\n");
}
}
fuse_ticket_drop(ticket);
return 0;
}
__private_extern__
int
fuse_internal_fsync(vnode_t vp,
vfs_context_t context,
struct fuse_filehandle *fufh,
void *param)
{
int err = 0;
int op = FUSE_FSYNC;
struct fuse_fsync_in *ffsi;
struct fuse_dispatcher *dispatcher = param;
const bool wait_for_completion = true;
fuse_trace_printf_func();
dispatcher->iosize = sizeof(*ffsi);
dispatcher->ticket = NULL;
if (vnode_isdir(vp)) {
op = FUSE_FSYNCDIR;
}
fuse_dispatcher_make_vp(dispatcher, op, vp, context);
ffsi = dispatcher->indata;
ffsi->fh = fufh->fh_id;
ffsi->fsync_flags = 1; /* datasync */
if (wait_for_completion) {
if ((err = fuse_dispatcher_wait_answer(dispatcher))) {
if (err == ENOSYS) {
if (op == FUSE_FSYNC) {
fuse_clear_implemented(dispatcher->ticket->data,
FSESS_NOIMPLBIT(FSYNC));
} else if (op == FUSE_FSYNCDIR) {
fuse_clear_implemented(dispatcher->ticket->data,
FSESS_NOIMPLBIT(FSYNCDIR));
}
}
goto out;
} else {
fuse_ticket_drop(dispatcher->ticket);
}
} else {
fuse_insert_callback(dispatcher->ticket, fuse_internal_fsync_callback);
fuse_insert_message(dispatcher->ticket);
}
out:
return err;
}
/* getattr sidekicks */
__private_extern__
int
fuse_internal_loadxtimes(vnode_t vp, struct vnode_attr *out_vap,
vfs_context_t context)
{
struct vnode_attr *in_vap = VTOVA(vp);
struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
struct fuse_dispatcher fdi;
struct fuse_getxtimes_out *fgxo = NULL;
int isvroot = vnode_isvroot(vp);
struct timespec t = { 0, 0 };
const struct timespec kZeroTime = { 0, 0 };
int err = 0;
if (!(data->dataflags & FSESS_XTIMES)) {
/* We don't return anything. */
goto out;
}
if (VTOFUD(vp)->c_flag & C_XTIMES_VALID) {
VATTR_RETURN(out_vap, va_backup_time, in_vap->va_backup_time);
VATTR_RETURN(out_vap, va_create_time, in_vap->va_create_time);
goto out;
}
if (!fuse_implemented(data, FSESS_NOIMPLBIT(GETXTIMES))) {
goto fake;
}
if (fuse_isdeadfs(vp) && isvroot) {
goto fake;
}
if (!data->inited && isvroot) {
goto fake;
}
err = fuse_dispatcher_simple_putget_vp(&fdi, FUSE_GETXTIMES, vp, context);
if (err) {
/* We don't ever treat this as a hard error. */
err = 0;
goto fake;
}
fgxo = (struct fuse_getxtimes_out *)fdi.answer;
t.tv_sec = (time_t)fgxo->bkuptime; /* XXX: truncation */
t.tv_nsec = fgxo->bkuptimensec;
VATTR_RETURN(in_vap, va_backup_time, t);
VATTR_RETURN(out_vap, va_backup_time, t);
t.tv_sec = (time_t)fgxo->crtime; /* XXX: truncation */
t.tv_nsec = fgxo->crtimensec;
VATTR_RETURN(in_vap, va_create_time, t);
VATTR_RETURN(out_vap, va_create_time, t);
fuse_ticket_drop(fdi.ticket);
VTOFUD(vp)->c_flag |= C_XTIMES_VALID;
goto out;
fake:
VATTR_RETURN(out_vap, va_backup_time, kZeroTime);
VATTR_RETURN(out_vap, va_create_time, kZeroTime);
out:
return err;
}
/* setattr sidekicks */
__private_extern__
int
fuse_internal_attr_vat2fsai(mount_t mp,
vnode_t vp,
struct vnode_attr *vap,
struct fuse_setattr_in *fsai,
uint64_t *newsize)
{
/*
* XXX: Locking
*
* We need to worry about the file size changing in setattr(). If the call
* is indeed altering the size, then:
*
* lock_exclusive(truncatelock)
* lock(nodelock)
* set the new size
* unlock(nodelock)
* adjust ubc
* lock(nodelock)
* do cleanup
* unlock(nodelock)
* unlock(truncatelock)
* ...
*/
int sizechanged = 0;
uid_t nuid;
gid_t ngid;
fsai->valid = 0;
if (newsize) {
*newsize = 0;
}
nuid = VATTR_IS_ACTIVE(vap, va_uid) ? vap->va_uid : (uid_t)VNOVAL;
if (nuid != (uid_t)VNOVAL) {
fsai->uid = nuid;
fsai->valid |= FATTR_UID;
}
VATTR_SET_SUPPORTED(vap, va_uid);
ngid = VATTR_IS_ACTIVE(vap, va_gid) ? vap->va_gid : (gid_t)VNOVAL;
if (ngid != (gid_t)VNOVAL) {
fsai->gid = ngid;
fsai->valid |= FATTR_GID;
}
VATTR_SET_SUPPORTED(vap, va_gid);
if (VATTR_IS_ACTIVE(vap, va_data_size)) {
// Truncate to a new value.
fsai->size = vap->va_data_size;
sizechanged = 1;
if (newsize) {
*newsize = vap->va_data_size;
}
fsai->valid |= FATTR_SIZE;
if (vp) {
struct fuse_filehandle *fufh = NULL;
struct fuse_vnode_data *fvdat = VTOFUD(vp);
fufh = &(fvdat->fufh[FUFH_WRONLY]);
if (!FUFH_IS_VALID(fufh)) {
fufh = &(fvdat->fufh[FUFH_RDWR]);
if (!FUFH_IS_VALID(fufh)) {
fufh = NULL;
}
}
if (fufh) {
fsai->fh = fufh->fh_id;
fsai->valid |= FATTR_FH;
}
}
}
VATTR_SET_SUPPORTED(vap, va_data_size);
/*
* Possible timestamps:
*
* Mac OS X Linux FUSE API
*
* va_access_time last access time atime atime
* va_backup_time last backup time - -
* va_change_time last metadata change time ctime* -
* va_create_time creation time - -
* va_modify_time last data modification time mtime mtime
*
*/
if (VATTR_IS_ACTIVE(vap, va_access_time)) {
fsai->atime = vap->va_access_time.tv_sec;
/* XXX: truncation */
fsai->atimensec = (uint32_t)vap->va_access_time.tv_nsec;
fsai->valid |= FATTR_ATIME;
}
VATTR_SET_SUPPORTED(vap, va_access_time);
if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
fsai->mtime = vap->va_modify_time.tv_sec;
/* XXX: truncation */
fsai->mtimensec = (uint32_t)vap->va_modify_time.tv_nsec;
fsai->valid |= FATTR_MTIME;
}
VATTR_SET_SUPPORTED(vap, va_modify_time);
if (VATTR_IS_ACTIVE(vap, va_backup_time) && fuse_isxtimes_mp(mp)) {
fsai->bkuptime = vap->va_backup_time.tv_sec;
/* XXX: truncation */
fsai->bkuptimensec = (uint32_t)vap->va_backup_time.tv_nsec;
fsai->valid |= FATTR_BKUPTIME;
VATTR_SET_SUPPORTED(vap, va_backup_time);
}
if (VATTR_IS_ACTIVE(vap, va_change_time)) {
if (fuse_isxtimes_mp(mp)) {
fsai->chgtime = vap->va_change_time.tv_sec;
/* XXX: truncation */
fsai->chgtimensec = (uint32_t)vap->va_change_time.tv_nsec;
fsai->valid |= FATTR_CHGTIME;
VATTR_SET_SUPPORTED(vap, va_change_time);
}
}
if (VATTR_IS_ACTIVE(vap, va_create_time) && fuse_isxtimes_mp(mp)) {
fsai->crtime = vap->va_create_time.tv_sec;
/* XXX: truncation */
fsai->crtimensec = (uint32_t)vap->va_create_time.tv_nsec;
fsai->valid |= FATTR_CRTIME;
VATTR_SET_SUPPORTED(vap, va_create_time);
}
if (VATTR_IS_ACTIVE(vap, va_mode)) {
fsai->mode = vap->va_mode & ALLPERMS;
fsai->mode |= VTTOIF(vnode_vtype(vp));
fsai->valid |= FATTR_MODE;
}
VATTR_SET_SUPPORTED(vap, va_mode);
if (VATTR_IS_ACTIVE(vap, va_flags)) {
fsai->flags = vap->va_flags;
fsai->valid |= FATTR_FLAGS;
}
VATTR_SET_SUPPORTED(vap, va_flags);
/*
* We /are/ OK with va_acl, va_guuid, and va_uuuid passing through here.
*/
return sizechanged;
}
/* readdir */
__private_extern__
int
fuse_internal_readdir(vnode_t vp,
uio_t uio,
vfs_context_t context,
struct fuse_filehandle *fufh,
struct fuse_iov *cookediov,
int *numdirent)
{
int err = 0;
struct fuse_dispatcher fdi;
struct fuse_read_in *fri;
struct fuse_data *data;
if (uio_resid(uio) == 0) {
return 0;
}
fuse_dispatcher_init(&fdi, 0);
/* Note that we DO NOT have a UIO_SYSSPACE here (so no need for p2p I/O). */
while (uio_resid(uio) > 0) {
fdi.iosize = sizeof(*fri);
fuse_dispatcher_make_vp(&fdi, FUSE_READDIR, vp, context);
fri = fdi.indata;
fri->fh = fufh->fh_id;
fri->offset = uio_offset(uio);
data = fuse_get_mpdata(vnode_mount(vp));
fri->size = (typeof(fri->size))min((size_t)uio_resid(uio), data->iosize);
if ((err = fuse_dispatcher_wait_answer(&fdi))) {
goto out;
}
if ((err = fuse_internal_readdir_processdata(vp,
uio,
fri->size,
fdi.answer,
fdi.iosize,
cookediov,
numdirent))) {
break;
}
}
/* done: */
fuse_ticket_drop(fdi.ticket);
out:
return ((err == -1) ? 0 : err);
}
__private_extern__
int
fuse_internal_readdir_processdata(vnode_t vp,
uio_t uio,
__unused size_t reqsize,
void *buf,
size_t bufsize,
struct fuse_iov *cookediov,
int *numdirent)
{
int err = 0;
int cou = 0;
int n = 0;
size_t bytesavail;
size_t freclen;
struct dirent *de;
struct fuse_dirent *fudge;
if (bufsize < FUSE_NAME_OFFSET) {
return -1;
}
for (;;) {
if (bufsize < FUSE_NAME_OFFSET) {
err = -1;
break;
}
fudge = (struct fuse_dirent *)buf;
freclen = FUSE_DIRENT_SIZE(fudge);
cou++;
if (bufsize < freclen) {
err = ((cou == 1) ? -1 : 0);
break;
}
/*
* if (isbzero(buf, FUSE_NAME_OFFSET)) {
* // zero-pad incomplete buffer
* ...
* err = -1;
* break;
* }
*/
if (!fudge->namelen) {
err = EINVAL;
break;
}
if (fudge->namelen > FUSE_MAXNAMLEN) {
err = EIO;
break;
}
bytesavail = (sizeof(struct dirent) - (FUSE_MAXNAMLEN + 1)) +
((fudge->namelen + 1 + 3) & ~3);
if (bytesavail > (size_t)uio_resid(uio)) {
err = -1;
break;
}
fiov_refresh(cookediov);
fiov_adjust(cookediov, bytesavail);
de = (struct dirent *)cookediov->base;
#ifdef _DARWIN_FEATURE_64_BIT_INODE
de->d_ino = fudge->ino;
#else
de->d_ino = (ino_t)fudge->ino; /* XXX: truncation */
#endif /* _DARWIN_FEATURE_64_BIT_INODE */
de->d_reclen = bytesavail;
de->d_type = fudge->type;
de->d_namlen = fudge->namelen;
/* Filter out any ._* files if the mount is configured as such. */
if (fuse_skip_apple_double_mp(vnode_mount(vp),
fudge->name, fudge->namelen)) {
de->d_ino = 0;
de->d_type = DT_WHT;
}
memcpy((char *)cookediov->base +
sizeof(struct dirent) - FUSE_MAXNAMLEN - 1,
(char *)buf + FUSE_NAME_OFFSET, fudge->namelen);
((char *)cookediov->base)[bytesavail] = '\0';
err = uiomove(cookediov->base, (int)cookediov->len, uio);
if (err) {
break;
}
n++;
buf = (char *)buf + freclen;
bufsize -= freclen;
uio_setoffset(uio, fudge->off);
}
if (!err && numdirent) {
*numdirent = n;
}
return err;
}
/* remove */
static int
fuse_internal_remove_callback(vnode_t vp, void *cargs)
{
struct vnode_attr *vap;
uint64_t target_nlink;
vap = VTOVA(vp);
target_nlink = *(uint64_t *)cargs;
/* somewhat lame "heuristics", but you got better ideas? */
if ((vap->va_nlink == target_nlink) && vnode_isreg(vp)) {
fuse_invalidate_attr(vp);
}
return VNODE_RETURNED;
}
__private_extern__
int
fuse_internal_remove(vnode_t dvp,
vnode_t vp,
struct componentname *cnp,
enum fuse_opcode op,
vfs_context_t context)
{
struct fuse_dispatcher fdi;
struct vnode_attr *vap = VTOVA(vp);
int need_invalidate = 0;
uint64_t target_nlink = 0;
mount_t mp = vnode_mount(vp);
int err = 0;
fuse_dispatcher_init(&fdi, cnp->cn_namelen + 1);
fuse_dispatcher_make_vp(&fdi, op, dvp, context);
memcpy(fdi.indata, cnp->cn_nameptr, cnp->cn_namelen);
((char *)fdi.indata)[cnp->cn_namelen] = '\0';
if ((vap->va_nlink > 1) && vnode_isreg(vp)) {
need_invalidate = 1;
target_nlink = vap->va_nlink;
}
if (!(err = fuse_dispatcher_wait_answer(&fdi))) {
fuse_ticket_drop(fdi.ticket);
}
fuse_invalidate_attr(dvp);
fuse_invalidate_attr(vp);
/*
* XXX: M_FUSE4X_INVALIDATE_CACHED_VATTRS_UPON_UNLINK
*
* Consider the case where vap->va_nlink > 1 for the entity being
* removed. In our world, other in-memory vnodes that share a link
* count each with this one may not know right way that this one just
* got deleted. We should let them know, say, through a vnode_iterate()
* here and a callback that does fuse_invalidate_attr(vp) on each
* relevant vnode.
*/
if (need_invalidate && !err) {
if (!vfs_busy(mp, LK_NOWAIT)) {
#ifdef FUSE4X_ENABLE_BIGLOCK
struct fuse_data *data = fuse_get_mpdata(mp);
fuse_biglock_unlock(data->biglock);
#endif
vnode_iterate(mp, 0, fuse_internal_remove_callback,
(void *)&target_nlink);
#ifdef FUSE4X_ENABLE_BIGLOCK
fuse_biglock_lock(data->biglock);
#endif
vfs_unbusy(mp);
} else {
log("fuse4x: skipping link count fixup upon remove\n");
}
}
return err;
}
/* rename */
__private_extern__
int
fuse_internal_rename(vnode_t fdvp,
__unused vnode_t fvp,
struct componentname *fcnp,
vnode_t tdvp,
__unused vnode_t tvp,
struct componentname *tcnp,
vfs_context_t context)
{
struct fuse_dispatcher fdi;
struct fuse_rename_in *fri;
int err = 0;
fuse_dispatcher_init(&fdi, sizeof(*fri) + fcnp->cn_namelen + tcnp->cn_namelen + 2);
fuse_dispatcher_make_vp(&fdi, FUSE_RENAME, fdvp, context);
fri = fdi.indata;
fri->newdir = VTOI(tdvp);
memcpy((char *)fdi.indata + sizeof(*fri), fcnp->cn_nameptr,
fcnp->cn_namelen);
((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen] = '\0';
memcpy((char *)fdi.indata + sizeof(*fri) + fcnp->cn_namelen + 1,
tcnp->cn_nameptr, tcnp->cn_namelen);
((char *)fdi.indata)[sizeof(*fri) + fcnp->cn_namelen +
tcnp->cn_namelen + 1] = '\0';
if (!(err = fuse_dispatcher_wait_answer(&fdi))) {
fuse_ticket_drop(fdi.ticket);
}
if (err == 0) {
fuse_invalidate_attr(fdvp);
if (tdvp != fdvp) {
fuse_invalidate_attr(tdvp);
}
}
return err;
}
/* strategy */
__private_extern__
int
fuse_internal_strategy(vnode_t vp, buf_t bp)
{
size_t biosize;
size_t chunksize;
size_t respsize;
bool mapped = false;
int mode;
int op;
int vtype = vnode_vtype(vp);
int err = 0;
caddr_t bufdat;
off_t left;
off_t offset;
int32_t bflags = buf_flags(bp);
fufh_type_t fufh_type;
struct fuse_dispatcher fdi;
struct fuse_data *data;
struct fuse_vnode_data *fvdat = VTOFUD(vp);
struct fuse_filehandle *fufh = NULL;
mount_t mp = vnode_mount(vp);
data = fuse_get_mpdata(mp);
biosize = data->blocksize;
if (!(vtype == VREG || vtype == VDIR)) {
return ENOTSUP;
}
if (bflags & B_READ) {
mode = FREAD;
fufh_type = FUFH_RDONLY; /* FUFH_RDWR will also do */
} else {
mode = FWRITE;
fufh_type = FUFH_WRONLY; /* FUFH_RDWR will also do */
}
fufh = &(fvdat->fufh[fufh_type]);
if (!FUFH_IS_VALID(fufh)) {
fufh_type = FUFH_RDWR;
fufh = &(fvdat->fufh[fufh_type]);
if (!FUFH_IS_VALID(fufh)) {
fufh = NULL;
} else {
/* We've successfully fallen back to FUFH_RDWR. */
}
}
if (!fufh) {
if (mode == FREAD) {
fufh_type = FUFH_RDONLY;
} else {
fufh_type = FUFH_RDWR;
}
/*
* Lets NOT do the filehandle preflight check here.
*/
err = fuse_filehandle_get(vp, NULL, fufh_type, 0 /* mode */);
if (!err) {
fufh = &(fvdat->fufh[fufh_type]);
/* We've created a NEW fufh of type fufh_type. open_count is 1. */
}
} else { /* good fufh */
OSIncrementAtomic((SInt32 *)&fuse_fh_reuse_count);
/* We're using an existing fufh of type fufh_type. */
}
if (err) {
/* A more typical error case. */
if ((err == ENOTCONN) || fuse_isdeadfs(vp)) {
buf_seterror(bp, EIO);
buf_biodone(bp);
return EIO;
}
log("fuse4x: strategy failed to get fh "
"(vtype=%d, fufh_type=%d, err=%d)\n", vtype, fufh_type, err);
if (!vfs_issynchronous(mp)) {
log("fuse4x: asynchronous write failed!\n");
}
buf_seterror(bp, EIO);
buf_biodone(bp);
return EIO;
}
if (!fufh) {
panic("fuse4x: tried everything but still no fufh");
/* NOTREACHED */
}
#define B_INVAL 0x00040000 /* Does not contain valid info. */
#define B_ERROR 0x00080000 /* I/O error occurred. */
if (bflags & B_INVAL) {
log("fuse4x: buffer does not contain valid information\n");
}
if (bflags & B_ERROR) {
log("fuse4x: an I/O error has occured\n");
}
if (buf_count(bp) == 0) {
return 0;
}
fuse_dispatcher_init(&fdi, 0);
if (mode == FREAD) {
struct fuse_read_in *fri;
buf_setresid(bp, buf_count(bp));
offset = (off_t)((off_t)buf_blkno(bp) * biosize);
if (offset >= fvdat->filesize) {
/* Trying to read at/after EOF? */
if (offset != fvdat->filesize) {
/* Trying to read after EOF? */