-
Notifications
You must be signed in to change notification settings - Fork 7
/
fuse_vnops.c
3373 lines (2744 loc) · 84.6 KB
/
fuse_vnops.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_param.h>
#include "fuse_sysctl.h"
#include "fuse_vnops.h"
#include "compat/tree.h"
#ifdef FUSE4X_ENABLE_BIGLOCK
#include "fuse_biglock_vnops.h"
#endif
#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/param.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 <vfs/vfs_support.h>
#define COM_APPLE_ "com.apple."
/* xattr */
static __inline__
bool
fuse_skip_apple_xattr_mp(mount_t mp, const char *name)
{
return name &&
(fuse_get_mpdata(mp)->dataflags & FSESS_NO_APPLEXATTR) &&
(bcmp(name, COM_APPLE_, sizeof(COM_APPLE_) - 1) == 0);
}
/*
struct vnop_access_args {
struct vnodeop_desc *a_desc;
vnode_t a_vp;
int a_action;
vfs_context_t a_context;
};
*/
FUSE_VNOP_EXPORT
int
fuse_vnop_access(struct vnop_access_args *ap)
{
vnode_t vp = ap->a_vp;
int action = ap->a_action;
vfs_context_t context = ap->a_context;
struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
fuse_trace_printf_vnop();
if (fuse_isdeadfs(vp)) {
if (vnode_isvroot(vp)) {
return 0;
} else {
return ENXIO;
}
}
if (!data->inited) {
if (vnode_isvroot(vp)) {
if (fuse_vfs_context_issuser(context) ||
(fuse_match_cred(data->daemoncred,
vfs_context_ucred(context)) == 0)) {
return 0;
}
}
return EBADF;
}
if (vnode_islnk(vp)) {
return 0;
}
return fuse_internal_access(vp, action, context);
}
/*
struct vnop_blktooff_args {
struct vnodeop_desc *a_desc;
vnode_t a_vp;
daddr64_t a_lblkno;
off_t *a_offset;
};
*/
FUSE_VNOP_EXPORT
int
fuse_vnop_blktooff(struct vnop_blktooff_args *ap)
{
vnode_t vp = ap->a_vp;
daddr64_t lblkno = ap->a_lblkno;
off_t *offsetPtr = ap->a_offset;
struct fuse_data *data;
fuse_trace_printf_vnop();
if (fuse_isdeadfs(vp)) {
return ENXIO;
}
data = fuse_get_mpdata(vnode_mount(vp));
*offsetPtr = lblkno * (off_t)(data->blocksize);
return 0;
}
/*
struct vnop_blockmap_args {
struct vnodeop_desc *a_desc;
vnode_t a_vp;
off_t a_foffset;
size_t a_size;
daddr64_t *a_bpn;
size_t *a_run;
void *a_poff;
int a_flags;
vfs_context_t a_context;
};
*/
FUSE_VNOP_EXPORT
int
fuse_vnop_blockmap(struct vnop_blockmap_args *ap)
{
vnode_t vp = ap->a_vp;
off_t foffset = ap->a_foffset;
size_t size = ap->a_size;
daddr64_t *bpnPtr = ap->a_bpn;
size_t *runPtr = ap->a_run;
int *poffPtr = (int *)ap->a_poff;
/* Ignoring flags and context */
struct fuse_vnode_data *fvdat;
struct fuse_data *data;
off_t contiguousPhysicalBytes;
fuse_trace_printf_vnop();
if (fuse_isdeadfs(vp)) {
return ENXIO;
}
if (vnode_isdir(vp)) {
return ENOTSUP;
}
if (ap->a_bpn == NULL) {
return 0;
}
fvdat = VTOFUD(vp);
data = fuse_get_mpdata(vnode_mount(vp));
/*
* We could assert that:
*
* (foffset % data->blocksize) == 0
* (foffset < fvdat->filesize)
* (size % data->blocksize) == 0)
*/
*bpnPtr = foffset / data->blocksize;
contiguousPhysicalBytes = \
fvdat->filesize - (off_t)(*bpnPtr * data->blocksize);
/* contiguousPhysicalBytes cannot really be negative (could assert) */
if (contiguousPhysicalBytes > (off_t)size) {
contiguousPhysicalBytes = (off_t)size;
}
if (runPtr != NULL) {
*runPtr = (size_t)contiguousPhysicalBytes;
}
if (poffPtr != NULL) {
*poffPtr = 0;
}
return 0;
}
/*
struct vnop_close_args {
struct vnodeop_desc *a_desc;
vnode_t a_vp;
int a_fflag;
vfs_context_t a_context;
};
*/
FUSE_VNOP_EXPORT
int
fuse_vnop_close(struct vnop_close_args *ap)
{
vnode_t vp = ap->a_vp;
int fflag = ap->a_fflag;
vfs_context_t context = ap->a_context;
int err = 0;
int isdir = (vnode_isdir(vp)) ? 1 : 0;
fufh_type_t fufh_type;
struct fuse_data *data;
struct fuse_vnode_data *fvdat = VTOFUD(vp);
struct fuse_filehandle *fufh = NULL;
fuse_trace_printf_vnop();
if (fuse_isdeadfs(vp)) {
return 0;
}
/* vclean() calls VNOP_CLOSE with fflag set to IO_NDELAY. */
if (fflag == IO_NDELAY) {
return 0;
}
if (isdir) {
fufh_type = FUFH_RDONLY;
} else {
fufh_type = fuse_filehandle_xlate_from_fflags(fflag);
}
fufh = &(fvdat->fufh[fufh_type]);
if (!FUFH_IS_VALID(fufh)) {
log("fuse4x: fufh invalid in close [type=%d oc=%d vtype=%d cf=%d]\n",
fufh_type, fufh->open_count, vnode_vtype(vp), fflag);
return 0;
}
if (isdir) {
goto skipdir;
}
/*
* Enforce sync-on-close unless explicitly told not to.
*
* We do this to maintain correct semantics in the not so common case when
* you create a file with O_RDWR but without write permissions--you /are/
* supposed to be able to write to such a file given the descriptor you
* you got from open()/create(). Therefore, if we don't finish all our
* writing before we close this precious writable descriptor, we might
* be doomed.
*/
if (vnode_hasdirtyblks(vp) && !fuse_isnosynconclose(vp)) {
(void)cluster_push(vp, IO_SYNC | IO_CLOSE);
}
data = fuse_get_mpdata(vnode_mount(vp));
if (fuse_implemented(data, FSESS_NOIMPLBIT(FLUSH))) {
struct fuse_dispatcher fdi;
struct fuse_flush_in *ffi;
fuse_dispatcher_init(&fdi, sizeof(*ffi));
fuse_dispatcher_make_vp(&fdi, FUSE_FLUSH, vp, context);
ffi = fdi.indata;
ffi->fh = fufh->fh_id;
ffi->unused = 0;
ffi->padding = 0;
ffi->lock_owner = 0;
err = fuse_dispatcher_wait_answer(&fdi);
if (!err) {
fuse_ticket_drop(fdi.ticket);
} else {
if (err == ENOSYS) {
fuse_clear_implemented(data, FSESS_NOIMPLBIT(FLUSH));
err = 0;
}
}
}
skipdir:
/* This must be done after we have flushed any pending I/O. */
FUFH_USE_DEC(fufh);
if (!FUFH_IS_VALID(fufh)) {
(void)fuse_filehandle_put(vp, context, fufh_type);
}
return err;
}
/*
struct vnop_create_args {
struct vnodeop_desc *a_desc;
vnode_t a_dvp;
vnode_t *a_vpp;
struct componentname *a_cnp;
struct vnode_attr *a_vap;
vfs_context_t a_context;
};
*/
FUSE_VNOP_EXPORT
int
fuse_vnop_create(struct vnop_create_args *ap)
{
vnode_t dvp = ap->a_dvp;
vnode_t *vpp = ap->a_vpp;
struct componentname *cnp = ap->a_cnp;
struct vnode_attr *vap = ap->a_vap;
vfs_context_t context = ap->a_context;
struct fuse_create_in *fci;
struct fuse_mknod_in fmni;
struct fuse_entry_out *feo;
struct fuse_dispatcher fdi;
struct fuse_dispatcher *dispatcher = &fdi;
int err;
bool gone_good_old = false;
struct fuse_data *data = NULL;
mount_t mp = vnode_mount(dvp);
uint64_t parent_nodeid = VTOFUD(dvp)->nodeid;
mode_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
fuse_trace_printf_vnop_novp();
if (fuse_isdeadfs(dvp)) {
return ENXIO;
}
CHECK_BLANKET_DENIAL(dvp, context, EPERM);
if (fuse_skip_apple_double_mp(mp, cnp->cn_nameptr, cnp->cn_namelen)) {
return EPERM;
}
bzero(&fdi, sizeof(fdi));
data = fuse_get_mpdata(mp);
if (!fuse_implemented(data, FSESS_NOIMPLBIT(CREATE)) ||
(vap->va_type != VREG)) {
/* User-space file system does not implement CREATE */
goto good_old;
}
fuse_dispatcher_init(dispatcher, sizeof(*fci) + cnp->cn_namelen + 1);
fuse_dispatcher_make(dispatcher, FUSE_CREATE, mp, parent_nodeid, context);
fci = dispatcher->indata;
fci->mode = mode;
/* XXX: We /always/ creat() like this. Wish we were on Linux. */
fci->flags = O_CREAT | O_RDWR;
memcpy((char *)dispatcher->indata + sizeof(*fci), cnp->cn_nameptr,
cnp->cn_namelen);
((char *)dispatcher->indata)[sizeof(*fci) + cnp->cn_namelen] = '\0';
err = fuse_dispatcher_wait_answer(dispatcher);
if (err == ENOSYS) {
fuse_clear_implemented(data, FSESS_NOIMPLBIT(CREATE));
dispatcher->ticket = NULL;
goto good_old;
} else if (err) {
goto undo;
}
goto bringup;
good_old:
gone_good_old = true;
fmni.mode = mode; /* fvdat->flags; */
fmni.rdev = 0;
fuse_internal_newentry_makerequest(mp, parent_nodeid, cnp,
FUSE_MKNOD, &fmni, sizeof(fmni),
dispatcher, context);
err = fuse_dispatcher_wait_answer(dispatcher);
if (err) {
goto undo;
}
bringup:
feo = dispatcher->answer;
if ((err = fuse_internal_checkentry(feo, VREG))) { // VBLK/VCHR not allowed
fuse_ticket_drop(dispatcher->ticket);
goto undo;
}
err = FSNodeGetOrCreateFileVNodeByID(vpp, false, feo, mp, dvp, context, NULL /* oflags */);
if (err) {
if (gone_good_old) {
fuse_internal_forget_send(mp, context, feo->nodeid, 1, dispatcher);
} else {
struct fuse_release_in *fri;
uint64_t nodeid = feo->nodeid;
uint64_t fh_id = ((struct fuse_open_out *)(feo + 1))->fh;
fuse_dispatcher_init(dispatcher, sizeof(*fri));
fuse_dispatcher_make(dispatcher, FUSE_RELEASE, mp, nodeid, context);
fri = dispatcher->indata;
fri->fh = fh_id;
fri->flags = OFLAGS(mode);
fuse_insert_callback(dispatcher->ticket, fuse_internal_forget_callback);
fuse_insert_message(dispatcher->ticket);
}
return err;
}
dispatcher->answer = gone_good_old ? NULL : feo + 1;
if (!gone_good_old) {
struct fuse_open_out *foo = (struct fuse_open_out *)(feo + 1);
struct fuse_vnode_data *fvdat = VTOFUD(*vpp);
struct fuse_filehandle *fufh = &(fvdat->fufh[FUFH_RDWR]);
fufh->fh_id = foo->fh;
fufh->open_flags = foo->open_flags;
/*
* We're stashing this to be picked up by open. Meanwhile, we set
* the use count to 1 because that's what it is. The use count will
* later transfer to the slot that this handle ends up falling in.
*/
fufh->open_count = 1;
OSIncrementAtomic((SInt32 *)&fuse_fh_current);
}
cache_purge_negatives(dvp);
fuse_ticket_drop(dispatcher->ticket);
return 0;
undo:
return err;
}
/*
struct vnop_exchange_args {
struct vnodeop_desc *a_desc;
vnode_t a_fvp;
vnode_t a_tvp;
int a_options;
vfs_context_t a_context;
};
*/
FUSE_VNOP_EXPORT
int
fuse_vnop_exchange(struct vnop_exchange_args *ap)
{
#ifdef FUSE4X_ENABLE_EXCHANGE
vnode_t fvp = ap->a_fvp;
vnode_t tvp = ap->a_tvp;
int options = ap->a_options;
vfs_context_t context = ap->a_context;
const char *fname = NULL;
const char *tname = NULL;
size_t flen = 0;
size_t tlen = 0;
struct fuse_data *data = fuse_get_mpdata(vnode_mount(fvp));
int err = 0;
fuse_trace_printf_vnop_novp();
if (vnode_mount(fvp) != vnode_mount(tvp)) {
return EXDEV;
}
/* We now know f and t are on the same volume. */
if (!fuse_implemented(data, FSESS_NOIMPLBIT(EXCHANGE))) {
return ENOTSUP;
}
if (fuse_isnovncache(fvp)) {
return ENOTSUP;
}
if (fvp == tvp) {
return EINVAL;
}
if (!vnode_isreg(fvp) || !vnode_isreg(tvp)) {
return EINVAL;
}
if (fuse_isdeadfs(fvp)) {
return ENXIO;
}
fname = vnode_getname(fvp);
if (!fname) {
return EIO;
}
tname = vnode_getname(tvp);
if (!tname) {
vnode_putname(fname);
return EIO;
}
flen = strlen(fname);
tlen = strlen(tname);
if ((flen > 2) && (*fname == '.') && (*(fname + 1) == '_')) {
err = EINVAL;
goto out;
}
if ((tlen > 2) && (*tname == '.') && (*(tname + 1) == '_')) {
err = EINVAL;
goto out;
}
err = fuse_internal_exchange(fvp, fname, flen, tvp, tname, tlen, options,
context);
if (err == ENOSYS) {
fuse_clear_implemented(data, FSESS_NOIMPLBIT(EXCHANGE));
err = ENOTSUP;
}
out:
vnode_putname(fname);
vnode_putname(tname);
return err;
#else /* !FUSE4X_ENABLE_EXCHANGE */
(void)ap;
return ENOTSUP;
#endif /* FUSE4X_ENABLE_EXCHANGE */
}
/*
* Our vnop_fsync roughly corresponds to the FUSE_FSYNC method. The Linux
* version of FUSE also has a FUSE_FLUSH method.
*
* On Linux, fsync() synchronizes a file's complete in-core state with that
* on disk. The call is not supposed to return until the system has completed
* that action or until an error is detected.
*
* Linux also has an fdatasync() call that is similar to fsync() but is not
* required to update the metadata such as access time and modification time.
*/
/*
struct vnop_fsync_args {
struct vnodeop_desc *a_desc;
vnode_t a_vp;
int a_waitfor;
vfs_context_t a_context;
};
*/
FUSE_VNOP_EXPORT
int
fuse_vnop_fsync(struct vnop_fsync_args *ap)
{
vnode_t vp = ap->a_vp;
int waitfor = ap->a_waitfor;
vfs_context_t context = ap->a_context;
struct fuse_dispatcher fdi;
struct fuse_filehandle *fufh;
struct fuse_vnode_data *fvdat = VTOFUD(vp);
int type, err = 0, tmp_err = 0;
(void)waitfor;
fuse_trace_printf_vnop();
if (fuse_isdeadfs(vp)) {
return 0;
}
cluster_push(vp, 0);
/*
* struct timeval tv;
* int wait = (waitfor == MNT_WAIT)
*
* In another world, we could be doing something like:
*
* buf_flushdirtyblks(vp, wait, 0, (char *)"fuse_fsync");
* microtime(&tv);
* ...
*/
/*
* - UBC and vnode are in lock-step.
* - Can call vnode_isinuse().
* - Can call ubc_msync().
*/
mount_t mp = vnode_mount(vp);
if (!fuse_implemented(fuse_get_mpdata(mp), ((vnode_isdir(vp)) ?
FSESS_NOIMPLBIT(FSYNCDIR) : FSESS_NOIMPLBIT(FSYNC)))) {
err = ENOSYS;
goto out;
}
fuse_dispatcher_init(&fdi, 0);
for (type = 0; type < FUFH_MAXTYPE; type++) {
fufh = &(fvdat->fufh[type]);
if (FUFH_IS_VALID(fufh)) {
tmp_err = fuse_internal_fsync(vp, context, fufh, &fdi);
if (tmp_err) {
err = tmp_err;
}
}
}
out:
if ((err == ENOSYS) && !fuse_isnosyncwrites_mp(mp)) {
err = 0;
}
return err;
}
/*
struct vnop_getattr_args {
struct vnodeop_desc *a_desc;
vnode_t a_vp;
struct vnode_attr *a_vap;
vfs_context_t a_context;
};
*/
FUSE_VNOP_EXPORT
int
fuse_vnop_getattr(struct vnop_getattr_args *ap)
{
vnode_t vp = ap->a_vp;
struct vnode_attr *vap = ap->a_vap;
vfs_context_t context = ap->a_context;
int err = 0;
struct timespec uptsp;
struct fuse_dispatcher fdi;
struct fuse_data *data;
data = fuse_get_mpdata(vnode_mount(vp));
fuse_trace_printf_vnop();
if (fuse_isdeadfs(vp)) {
if (vnode_isvroot(vp)) {
goto fake;
} else {
return ENXIO;
}
}
if (!vnode_isvroot(vp) || !fuse_vfs_context_issuser(context)) {
CHECK_BLANKET_DENIAL(vp, context, ENOENT);
}
/* Note that we are not bailing out on a dead file system just yet. */
/* look for cached attributes */
nanouptime(&uptsp);
if (fuse_timespec_cmp(&uptsp, &VTOFUD(vp)->attr_valid, <=)) {
if (vap != VTOVA(vp)) {
fuse_internal_attr_loadvap(vp, vap, context);
}
return 0;
}
if (!data->inited) {
if (!vnode_isvroot(vp)) {
fuse_data_kill(data);
return ENOTCONN;
} else {
goto fake;
}
}
fuse_dispatcher_init(&fdi, sizeof(struct fuse_getattr_in));
fuse_dispatcher_make_vp(&fdi, FUSE_GETATTR, vp, context);
bzero(fdi.indata, sizeof(struct fuse_getattr_in));
if ((err = fuse_dispatcher_wait_answer(&fdi))) {
if ((err == ENOTCONN) && vnode_isvroot(vp)) {
/* see comment at similar place in fuse_statfs() */
goto fake;
}
if (err == ENOENT) {
#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;
}
struct fuse_attr_out *attr_out = fdi.answer;
/* XXX: Could check the sanity/volatility of va_mode here. */
if ((attr_out->attr.mode & S_IFMT) == 0) {
return EIO;
}
cache_attrs(vp, attr_out);
VTOFUD(vp)->c_flag &= ~C_XTIMES_VALID;
fuse_internal_attr_loadvap(vp, vap, context);
/* ATTR_FUDGE_CASE */
if (vnode_isreg(vp) && fuse_isdirectio(vp)) {
/*
* This is for those cases when the file size changed without us
* knowing, and we want to catch up.
*
* For the sake of sanity, we don't want to do it with UBC.
* We also don't want to do it when we have asynchronous writes
* enabled because we might have pending writes on *our* side.
* We're not researching distributed file systems here!
*
*/
VTOFUD(vp)->filesize = attr_out->attr.size;
}
fuse_ticket_drop(fdi.ticket);
if (vnode_vtype(vp) != vap->va_type) {
if ((vnode_vtype(vp) == VNON) && (vap->va_type != VNON)) {
/*
* We should be doing the following:
*
* vp->vtype = vap->v_type
*/
} else {
/*
* STALE vnode, ditch
*
* The vnode has changed its type "behind our back". There's
* nothing really we can do, so let us just force an internal
* revocation.
*/
#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 EIO;
}
}
return 0;
fake:
bzero(vap, sizeof(*vap));
VATTR_RETURN(vap, va_type, vnode_vtype(vp));
VATTR_RETURN(vap, va_uid, kauth_cred_getuid(data->daemoncred));
VATTR_RETURN(vap, va_gid, kauth_cred_getgid(data->daemoncred));
VATTR_RETURN(vap, va_mode, S_IRWXU);
return 0;
}
/*
struct vnop_getxattr_args {
struct vnodeop_desc *a_desc;
vnode_t a_vp;
char *a_name;
uio_t a_uio;
size_t *a_size;
int a_options;
vfs_context_t a_context;
};
*/
FUSE_VNOP_EXPORT
int
fuse_vnop_getxattr(struct vnop_getxattr_args *ap)
{
vnode_t vp = ap->a_vp;
const char *name = ap->a_name;
uio_t uio = ap->a_uio;
vfs_context_t context = ap->a_context;
struct fuse_dispatcher fdi;
struct fuse_getxattr_in *fgxi;
struct fuse_getxattr_out *fgxo;
struct fuse_data *data;
mount_t mp;
int err = 0;
size_t namelen;
fuse_trace_printf_vnop();
if (fuse_isdeadfs(vp)) {
return ENXIO;
}
CHECK_BLANKET_DENIAL(vp, context, ENOENT);
if (name == NULL || name[0] == '\0') {
return EINVAL;
}
mp = vnode_mount(vp);
data = fuse_get_mpdata(mp);
if (fuse_skip_apple_xattr_mp(mp, name)) {
return EPERM;
}
if (data->dataflags & FSESS_AUTO_XATTR) {
return ENOTSUP;
}
if (!fuse_implemented(data, FSESS_NOIMPLBIT(GETXATTR))) {
return ENOTSUP;
}
namelen = strlen(name);
fuse_dispatcher_init(&fdi, sizeof(*fgxi) + namelen + 1);
fuse_dispatcher_make_vp(&fdi, FUSE_GETXATTR, vp, context);
fgxi = fdi.indata;
if (uio) {
fgxi->size = (uint32_t)uio_resid(uio);
} else {
fgxi->size = 0;
}
fgxi->position = (uint32_t)uio_offset(uio);
memcpy((char *)fdi.indata + sizeof(*fgxi), name, namelen);
((char *)fdi.indata)[sizeof(*fgxi) + namelen] = '\0';
if (fgxi->size > FUSE_REASONABLE_XATTRSIZE) {
fdi.ticket->killed = true;
}
err = fuse_dispatcher_wait_answer(&fdi);
if (err) {
if (err == ENOSYS) {
fuse_clear_implemented(data, FSESS_NOIMPLBIT(GETXATTR));
return ENOTSUP;
}
return err;
}
if (uio) {
*ap->a_size = fdi.iosize;
if ((user_ssize_t)fdi.iosize > uio_resid(uio)) {
err = ERANGE;
} else {
err = uiomove((char *)fdi.answer, (int)fdi.iosize, uio);
}
} else {
fgxo = (struct fuse_getxattr_out *)fdi.answer;
*ap->a_size = fgxo->size;
}
fuse_ticket_drop(fdi.ticket);
return err;
}
/*
struct vnop_inactive_args {
struct vnodeop_desc *a_desc;
vnode_t a_vp;
vfs_context_t a_context;
};
*/
FUSE_VNOP_EXPORT
int
fuse_vnop_inactive(struct vnop_inactive_args *ap)
{
vnode_t vp = ap->a_vp;
vfs_context_t context = ap->a_context;
struct fuse_vnode_data *fvdat = VTOFUD(vp);
struct fuse_filehandle *fufh = NULL;
fuse_trace_printf_vnop();
/*
* Cannot do early bail out on a dead file system in this case.
*/
for (int fufh_type = 0; fufh_type < FUFH_MAXTYPE; fufh_type++) {
fufh = &(fvdat->fufh[fufh_type]);
//TOTHINK: should we just check that all fuse_fh are zero??
if (FUFH_IS_VALID(fufh)) {
FUFH_USE_RESET(fufh);
(void)fuse_filehandle_put(vp, context, fufh_type);
}
}
return 0;
}
/*
struct vnop_link_args {
struct vnodeop_desc *a_desc;
vnode_t a_vp;
vnode_t a_tdvp;
struct componentname *a_cnp;
vfs_context_t a_context;
};
*/
FUSE_VNOP_EXPORT
int
fuse_vnop_link(struct vnop_link_args *ap)
{
vnode_t vp = ap->a_vp;
vnode_t tdvp = ap->a_tdvp;
struct componentname *cnp = ap->a_cnp;
vfs_context_t context = ap->a_context;
struct vnode_attr *vap = VTOVA(vp);
struct fuse_dispatcher fdi;
struct fuse_entry_out *feo;
struct fuse_link_in fli;
int err;
fuse_trace_printf_vnop();
if (fuse_isdeadfs(vp)) {
return ENXIO;
}
if (vnode_mount(tdvp) != vnode_mount(vp)) {
return EXDEV;
}
if (vap->va_nlink >= FUSE_LINK_MAX) {
return EMLINK;
}
CHECK_BLANKET_DENIAL(vp, context, EPERM);