forked from jacobsa/fuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversions.go
1143 lines (994 loc) · 30.7 KB
/
conversions.go
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 2015 Google Inc. All Rights Reserved.
//
// 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.
package fuse
import (
"bytes"
"errors"
"fmt"
"os"
"reflect"
"syscall"
"time"
"unsafe"
"github.com/jacobsa/fuse/fuseops"
"github.com/jacobsa/fuse/internal/buffer"
"github.com/jacobsa/fuse/internal/fusekernel"
)
////////////////////////////////////////////////////////////////////////
// Incoming messages
////////////////////////////////////////////////////////////////////////
// Convert a kernel message to an appropriate op. If the op is unknown, a
// special unexported type will be used.
//
// The caller is responsible for arranging for the message to be destroyed.
func convertInMessage(
config *MountConfig,
inMsg *buffer.InMessage,
outMsg *buffer.OutMessage,
protocol fusekernel.Protocol) (o interface{}, err error) {
switch inMsg.Header().Opcode {
case fusekernel.OpLookup:
buf := inMsg.ConsumeBytes(inMsg.Len())
n := len(buf)
if n == 0 || buf[n-1] != '\x00' {
return nil, errors.New("Corrupt OpLookup")
}
o = &fuseops.LookUpInodeOp{
Parent: fuseops.InodeID(inMsg.Header().Nodeid),
Name: string(buf[:n-1]),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpGetattr:
o = &fuseops.GetInodeAttributesOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpSetattr:
type input fusekernel.SetattrIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
return nil, errors.New("Corrupt OpSetattr")
}
to := &fuseops.SetInodeAttributesOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
o = to
valid := fusekernel.SetattrValid(in.Valid)
if valid&fusekernel.SetattrUid != 0 {
to.Uid = &in.Uid
}
if valid&fusekernel.SetattrGid != 0 {
to.Gid = &in.Gid
}
if valid&fusekernel.SetattrSize != 0 {
to.Size = &in.Size
}
if valid&fusekernel.SetattrMode != 0 {
mode := ConvertFileMode(in.Mode)
to.Mode = &mode
}
if valid&fusekernel.SetattrAtime != 0 {
t := time.Unix(int64(in.Atime), int64(in.AtimeNsec))
to.Atime = &t
}
if valid&fusekernel.SetattrMtime != 0 {
t := time.Unix(int64(in.Mtime), int64(in.MtimeNsec))
to.Mtime = &t
}
if valid.Handle() {
t := fuseops.HandleID(in.Fh)
to.Handle = &t
}
case fusekernel.OpForget:
type input fusekernel.ForgetIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
return nil, errors.New("Corrupt OpForget")
}
o = &fuseops.ForgetInodeOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
N: in.Nlookup,
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpBatchForget:
type input fusekernel.BatchForgetCountIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
return nil, errors.New("Corrupt OpBatchForget")
}
entries := make([]fuseops.BatchForgetEntry, 0, in.Count)
for i := uint32(0); i < in.Count; i++ {
type entry fusekernel.BatchForgetEntryIn
ein := (*entry)(inMsg.Consume(unsafe.Sizeof(entry{})))
if ein == nil {
return nil, errors.New("Corrupt OpBatchForget")
}
entries = append(entries, fuseops.BatchForgetEntry{
Inode: fuseops.InodeID(ein.Inode),
N: ein.Nlookup,
})
}
o = &fuseops.BatchForgetOp{
Entries: entries,
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpMkdir:
in := (*fusekernel.MkdirIn)(inMsg.Consume(fusekernel.MkdirInSize(protocol)))
if in == nil {
return nil, errors.New("Corrupt OpMkdir")
}
name := inMsg.ConsumeBytes(inMsg.Len())
i := bytes.IndexByte(name, '\x00')
if i < 0 {
return nil, errors.New("Corrupt OpMkdir")
}
name = name[:i]
o = &fuseops.MkDirOp{
Parent: fuseops.InodeID(inMsg.Header().Nodeid),
Name: string(name),
// On Linux, vfs_mkdir calls through to the inode with at most
// permissions and sticky bits set (https://tinyurl.com/3djx8498), and
// fuse passes that on directly (https://tinyurl.com/exezw647). In other
// words, the fact that this is a directory is implicit in the fact that
// the opcode is mkdir. But we want the correct mode to go through, so
// ensure that os.ModeDir is set.
Mode: ConvertFileMode(in.Mode) | os.ModeDir,
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpMknod:
in := (*fusekernel.MknodIn)(inMsg.Consume(fusekernel.MknodInSize(protocol)))
if in == nil {
return nil, errors.New("Corrupt OpMknod")
}
name := inMsg.ConsumeBytes(inMsg.Len())
i := bytes.IndexByte(name, '\x00')
if i < 0 {
return nil, errors.New("Corrupt OpMknod")
}
name = name[:i]
o = &fuseops.MkNodeOp{
Parent: fuseops.InodeID(inMsg.Header().Nodeid),
Name: string(name),
Mode: ConvertFileMode(in.Mode),
Rdev: in.Rdev,
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpCreate:
in := (*fusekernel.CreateIn)(inMsg.Consume(fusekernel.CreateInSize(protocol)))
if in == nil {
return nil, errors.New("Corrupt OpCreate")
}
name := inMsg.ConsumeBytes(inMsg.Len())
i := bytes.IndexByte(name, '\x00')
if i < 0 {
return nil, errors.New("Corrupt OpCreate")
}
name = name[:i]
o = &fuseops.CreateFileOp{
Parent: fuseops.InodeID(inMsg.Header().Nodeid),
Name: string(name),
Mode: ConvertFileMode(in.Mode),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpSymlink:
// The message is "newName\0target\0".
names := inMsg.ConsumeBytes(inMsg.Len())
if len(names) == 0 || names[len(names)-1] != 0 {
return nil, errors.New("Corrupt OpSymlink")
}
i := bytes.IndexByte(names, '\x00')
if i < 0 {
return nil, errors.New("Corrupt OpSymlink")
}
newName, target := names[0:i], names[i+1:len(names)-1]
o = &fuseops.CreateSymlinkOp{
Parent: fuseops.InodeID(inMsg.Header().Nodeid),
Name: string(newName),
Target: string(target),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpRename:
type input fusekernel.RenameIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
return nil, errors.New("Corrupt OpRename")
}
names := inMsg.ConsumeBytes(inMsg.Len())
// closed-source macfuse 4.x has broken compatibility with osxfuse 3.x:
// it passes an additional 64-bit field (flags) after RenameIn regardless
// that we don't enable the support for RENAME_SWAP/RENAME_EXCL
// macfuse doesn't want change the behaviour back which is motivated by
// not breaking compatibility the second time, look here for details:
// https://github.com/osxfuse/osxfuse/issues/839
//
// the simplest fix is just to check for the presence of all-zero flags
if len(names) >= 8 &&
names[0] == 0 && names[1] == 0 && names[2] == 0 && names[3] == 0 &&
names[4] == 0 && names[5] == 0 && names[6] == 0 && names[7] == 0 {
names = names[8:]
}
// names should be "old\x00new\x00"
if len(names) < 4 {
return nil, errors.New("Corrupt OpRename")
}
if names[len(names)-1] != '\x00' {
return nil, errors.New("Corrupt OpRename")
}
i := bytes.IndexByte(names, '\x00')
if i < 0 {
return nil, errors.New("Corrupt OpRename")
}
oldName, newName := names[:i], names[i+1:len(names)-1]
o = &fuseops.RenameOp{
OldParent: fuseops.InodeID(inMsg.Header().Nodeid),
OldName: string(oldName),
NewParent: fuseops.InodeID(in.Newdir),
NewName: string(newName),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpUnlink:
buf := inMsg.ConsumeBytes(inMsg.Len())
n := len(buf)
if n == 0 || buf[n-1] != '\x00' {
return nil, errors.New("Corrupt OpUnlink")
}
o = &fuseops.UnlinkOp{
Parent: fuseops.InodeID(inMsg.Header().Nodeid),
Name: string(buf[:n-1]),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpRmdir:
buf := inMsg.ConsumeBytes(inMsg.Len())
n := len(buf)
if n == 0 || buf[n-1] != '\x00' {
return nil, errors.New("Corrupt OpRmdir")
}
o = &fuseops.RmDirOp{
Parent: fuseops.InodeID(inMsg.Header().Nodeid),
Name: string(buf[:n-1]),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpOpen:
type input fusekernel.OpenIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
return nil, errors.New("Corrupt OpOpen")
}
o = &fuseops.OpenFileOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
OpenFlags: fusekernel.OpenFlags(in.Flags),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpOpendir:
o = &fuseops.OpenDirOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpRead:
in := (*fusekernel.ReadIn)(inMsg.Consume(fusekernel.ReadInSize(protocol)))
if in == nil {
return nil, errors.New("Corrupt OpRead")
}
to := &fuseops.ReadFileOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
Handle: fuseops.HandleID(in.Fh),
Offset: int64(in.Offset),
Size: int64(in.Size),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
if !config.UseVectoredRead {
// Use part of the incoming message storage as the read buffer
// For vectored zero-copy reads, don't allocate any buffers
to.Dst = inMsg.GetFree(int(in.Size))
}
o = to
case fusekernel.OpReaddir:
in := (*fusekernel.ReadIn)(inMsg.Consume(fusekernel.ReadInSize(protocol)))
if in == nil {
return nil, errors.New("Corrupt OpReaddir")
}
to := &fuseops.ReadDirOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
Handle: fuseops.HandleID(in.Fh),
Offset: fuseops.DirOffset(in.Offset),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
o = to
readSize := int(in.Size)
p := outMsg.Grow(readSize)
if p == nil {
return nil, fmt.Errorf("Can't grow for %d-byte read", readSize)
}
sh := (*reflect.SliceHeader)(unsafe.Pointer(&to.Dst))
sh.Data = uintptr(p)
sh.Len = readSize
sh.Cap = readSize
case fusekernel.OpRelease:
type input fusekernel.ReleaseIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
return nil, errors.New("Corrupt OpRelease")
}
o = &fuseops.ReleaseFileHandleOp{
Handle: fuseops.HandleID(in.Fh),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpReleasedir:
type input fusekernel.ReleaseIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
return nil, errors.New("Corrupt OpReleasedir")
}
o = &fuseops.ReleaseDirHandleOp{
Handle: fuseops.HandleID(in.Fh),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpWrite:
in := (*fusekernel.WriteIn)(inMsg.Consume(fusekernel.WriteInSize(protocol)))
if in == nil {
return nil, errors.New("Corrupt OpWrite")
}
buf := inMsg.ConsumeBytes(inMsg.Len())
if len(buf) < int(in.Size) {
return nil, errors.New("Corrupt OpWrite")
}
o = &fuseops.WriteFileOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
Handle: fuseops.HandleID(in.Fh),
Data: buf,
Offset: int64(in.Offset),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpFsync, fusekernel.OpFsyncdir:
type input fusekernel.FsyncIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
return nil, errors.New("Corrupt OpFsync/OpFsyncdir")
}
o = &fuseops.SyncFileOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
Handle: fuseops.HandleID(in.Fh),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpSyncFS:
type input fusekernel.SyncFSIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
return nil, errors.New("Corrupt OpSyncFS")
}
o = &fuseops.SyncFSOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
OpContext: fuseops.OpContext{Pid: inMsg.Header().Pid},
}
case fusekernel.OpFlush:
type input fusekernel.FlushIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
return nil, errors.New("Corrupt OpFlush")
}
o = &fuseops.FlushFileOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
Handle: fuseops.HandleID(in.Fh),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpReadlink:
o = &fuseops.ReadSymlinkOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpStatfs:
o = &fuseops.StatFSOp{}
case fusekernel.OpInterrupt:
type input fusekernel.InterruptIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
return nil, errors.New("Corrupt OpInterrupt")
}
o = &interruptOp{
FuseID: in.Unique,
}
case fusekernel.OpInit:
type input fusekernel.InitIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
return nil, errors.New("Corrupt OpInit")
}
o = &initOp{
Kernel: fusekernel.Protocol{in.Major, in.Minor},
MaxReadahead: in.MaxReadahead,
Flags: fusekernel.InitFlags(in.Flags),
}
case fusekernel.OpLink:
type input fusekernel.LinkIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
return nil, errors.New("Corrupt OpLink")
}
name := inMsg.ConsumeBytes(inMsg.Len())
i := bytes.IndexByte(name, '\x00')
if i < 0 {
return nil, errors.New("Corrupt OpLink")
}
name = name[:i]
if len(name) == 0 {
return nil, errors.New("Corrupt OpLink (Name not read)")
}
o = &fuseops.CreateLinkOp{
Parent: fuseops.InodeID(inMsg.Header().Nodeid),
Name: string(name),
Target: fuseops.InodeID(in.Oldnodeid),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpRemovexattr:
buf := inMsg.ConsumeBytes(inMsg.Len())
n := len(buf)
if n == 0 || buf[n-1] != '\x00' {
return nil, errors.New("Corrupt OpRemovexattr")
}
o = &fuseops.RemoveXattrOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
Name: string(buf[:n-1]),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpGetxattr:
type input fusekernel.GetxattrIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
return nil, errors.New("Corrupt OpGetxattr")
}
name := inMsg.ConsumeBytes(inMsg.Len())
i := bytes.IndexByte(name, '\x00')
if i < 0 {
return nil, errors.New("Corrupt OpGetxattr")
}
name = name[:i]
to := &fuseops.GetXattrOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
Name: string(name),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
o = to
readSize := int(in.Size)
if readSize > 0 {
p := outMsg.Grow(readSize)
if p == nil {
return nil, fmt.Errorf("Can't grow for %d-byte read", readSize)
}
sh := (*reflect.SliceHeader)(unsafe.Pointer(&to.Dst))
sh.Data = uintptr(p)
sh.Len = readSize
sh.Cap = readSize
} else {
to.Dst = nil
}
case fusekernel.OpListxattr:
type input fusekernel.ListxattrIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
return nil, errors.New("Corrupt OpListxattr")
}
to := &fuseops.ListXattrOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
o = to
readSize := int(in.Size)
if readSize != 0 {
p := outMsg.Grow(readSize)
if p == nil {
return nil, fmt.Errorf("Can't grow for %d-byte read", readSize)
}
sh := (*reflect.SliceHeader)(unsafe.Pointer(&to.Dst))
sh.Data = uintptr(p)
sh.Len = readSize
sh.Cap = readSize
}
case fusekernel.OpSetxattr:
type input fusekernel.SetxattrIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
return nil, errors.New("Corrupt OpSetxattr")
}
payload := inMsg.ConsumeBytes(inMsg.Len())
// payload should be "name\x00value"
if len(payload) < 3 {
return nil, errors.New("Corrupt OpSetxattr")
}
i := bytes.IndexByte(payload, '\x00')
if i < 0 {
return nil, errors.New("Corrupt OpSetxattr")
}
name, value := payload[:i], payload[i+1:len(payload)]
o = &fuseops.SetXattrOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
Name: string(name),
Value: value,
Flags: in.Flags,
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
case fusekernel.OpFallocate:
type input fusekernel.FallocateIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
return nil, errors.New("Corrupt OpFallocate")
}
o = &fuseops.FallocateOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
Handle: fuseops.HandleID(in.Fh),
Offset: in.Offset,
Length: in.Length,
Mode: in.Mode,
OpContext: fuseops.OpContext{
FuseID: inMsg.Header().Unique,
Pid: inMsg.Header().Pid,
Uid: inMsg.Header().Uid,
},
}
default:
o = &unknownOp{
OpCode: inMsg.Header().Opcode,
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
}
}
return o, nil
}
////////////////////////////////////////////////////////////////////////
// Outgoing messages
////////////////////////////////////////////////////////////////////////
// Fill in the response that should be sent to the kernel, or set noResponse if
// the op requires no response.
func (c *Connection) kernelResponse(
m *buffer.OutMessage,
fuseID uint64,
op interface{},
opErr error) (noResponse bool) {
h := m.OutHeader()
h.Unique = fuseID
// Special case: handle the ops for which the kernel expects no response.
// interruptOp .
switch op.(type) {
case *fuseops.ForgetInodeOp:
return true
case *fuseops.BatchForgetOp:
return true
case *interruptOp:
return true
}
// If the user returned the error, fill in the error field of the outgoing
// message header.
if opErr != nil {
handled := false
if !handled {
m.OutHeader().Error = -int32(syscall.EIO)
if errno, ok := opErr.(syscall.Errno); ok {
m.OutHeader().Error = -int32(errno)
}
// Special case: for some types, convertInMessage grew the message in order
// to obtain a destination buffer. Make sure that we shrink back to just
// the header, because on OS X the kernel otherwise returns EINVAL when we
// attempt to write an error response with a length that extends beyond the
// header.
m.ShrinkTo(buffer.OutMessageHeaderSize)
}
}
// Otherwise, fill in the rest of the response.
if opErr == nil {
c.kernelResponseForOp(m, op)
}
h.Len = uint32(m.Len())
return false
}
// Like kernelResponse, but assumes the user replied with a nil error to the
// op.
func (c *Connection) kernelResponseForOp(
m *buffer.OutMessage,
op interface{}) {
// Create the appropriate output message
switch o := op.(type) {
case *fuseops.LookUpInodeOp:
size := int(fusekernel.EntryOutSize(c.protocol))
out := (*fusekernel.EntryOut)(m.Grow(size))
convertChildInodeEntry(&o.Entry, out)
case *fuseops.GetInodeAttributesOp:
size := int(fusekernel.AttrOutSize(c.protocol))
out := (*fusekernel.AttrOut)(m.Grow(size))
out.AttrValid, out.AttrValidNsec = convertExpirationTime(
o.AttributesExpiration)
convertAttributes(o.Inode, &o.Attributes, &out.Attr)
case *fuseops.SetInodeAttributesOp:
size := int(fusekernel.AttrOutSize(c.protocol))
out := (*fusekernel.AttrOut)(m.Grow(size))
out.AttrValid, out.AttrValidNsec = convertExpirationTime(
o.AttributesExpiration)
convertAttributes(o.Inode, &o.Attributes, &out.Attr)
case *fuseops.MkDirOp:
size := int(fusekernel.EntryOutSize(c.protocol))
out := (*fusekernel.EntryOut)(m.Grow(size))
convertChildInodeEntry(&o.Entry, out)
case *fuseops.MkNodeOp:
size := int(fusekernel.EntryOutSize(c.protocol))
out := (*fusekernel.EntryOut)(m.Grow(size))
convertChildInodeEntry(&o.Entry, out)
case *fuseops.CreateFileOp:
eSize := int(fusekernel.EntryOutSize(c.protocol))
e := (*fusekernel.EntryOut)(m.Grow(eSize))
convertChildInodeEntry(&o.Entry, e)
oo := (*fusekernel.OpenOut)(m.Grow(int(unsafe.Sizeof(fusekernel.OpenOut{}))))
oo.Fh = uint64(o.Handle)
case *fuseops.CreateSymlinkOp:
size := int(fusekernel.EntryOutSize(c.protocol))
out := (*fusekernel.EntryOut)(m.Grow(size))
convertChildInodeEntry(&o.Entry, out)
case *fuseops.CreateLinkOp:
size := int(fusekernel.EntryOutSize(c.protocol))
out := (*fusekernel.EntryOut)(m.Grow(size))
convertChildInodeEntry(&o.Entry, out)
case *fuseops.RenameOp:
// Empty response
case *fuseops.RmDirOp:
// Empty response
case *fuseops.UnlinkOp:
// Empty response
case *fuseops.OpenDirOp:
out := (*fusekernel.OpenOut)(m.Grow(int(unsafe.Sizeof(fusekernel.OpenOut{}))))
out.Fh = uint64(o.Handle)
if o.CacheDir {
out.OpenFlags |= uint32(fusekernel.OpenCacheDir)
}
if o.KeepCache {
out.OpenFlags |= uint32(fusekernel.OpenKeepCache)
}
case *fuseops.ReadDirOp:
// convertInMessage already set up the destination buffer to be at the end
// of the out message. We need only shrink to the right size based on how
// much the user read.
m.ShrinkTo(buffer.OutMessageHeaderSize + o.BytesRead)
case *fuseops.ReleaseDirHandleOp:
// Empty response
case *fuseops.OpenFileOp:
out := (*fusekernel.OpenOut)(m.Grow(int(unsafe.Sizeof(fusekernel.OpenOut{}))))
out.Fh = uint64(o.Handle)
if o.KeepPageCache {
out.OpenFlags |= uint32(fusekernel.OpenKeepCache)
}
if o.UseDirectIO {
out.OpenFlags |= uint32(fusekernel.OpenDirectIO)
}
case *fuseops.ReadFileOp:
if o.Dst != nil {
m.Append(o.Dst)
} else {
m.Append(o.Data...)
}
m.ShrinkTo(buffer.OutMessageHeaderSize + o.BytesRead)
case *fuseops.WriteFileOp:
out := (*fusekernel.WriteOut)(m.Grow(int(unsafe.Sizeof(fusekernel.WriteOut{}))))
out.Size = uint32(len(o.Data))
case *fuseops.SyncFileOp:
// Empty response
case *fuseops.FlushFileOp:
// Empty response
case *fuseops.ReleaseFileHandleOp:
// Empty response
case *fuseops.ReadSymlinkOp:
m.AppendString(o.Target)
case *fuseops.StatFSOp:
out := (*fusekernel.StatfsOut)(m.Grow(int(unsafe.Sizeof(fusekernel.StatfsOut{}))))
out.St.Blocks = o.Blocks
out.St.Bfree = o.BlocksFree
out.St.Bavail = o.BlocksAvailable
out.St.Files = o.Inodes
out.St.Ffree = o.InodesFree
out.St.Namelen = 255
// The posix spec for sys/statvfs.h (https://tinyurl.com/2juj6ah6) defines the
// following fields of statvfs, among others:
//
// f_bsize File system block size.
// f_frsize Fundamental file system block size.
// f_blocks Total number of blocks on file system in units of f_frsize.
//
// It appears as though f_bsize was the only thing supported by most unixes
// originally, but then f_frsize was added when new sorts of file systems
// came about. Quoth The Linux Programming Interface by Michael Kerrisk
// (https://tinyurl.com/5n8mjtws):
//
// For most Linux file systems, the values of f_bsize and f_frsize are
// the same. However, some file systems support the notion of block
// fragments, which can be used to allocate a smaller unit of storage
// at the end of the file if if a full block is not required. This
// avoids the waste of space that would otherwise occur if a full block
// was allocated. On such file systems, f_frsize is the size of a
// fragment, and f_bsize is the size of a whole block. (The notion of
// fragments in UNIX file systems first appeared in the early 1980s
// with the 4.2BSD Fast File System.)
//
// Confusingly, it appears as though osxfuse surfaces fuse_kstatfs::bsize
// as statfs::f_iosize (of advisory use only), and fuse_kstatfs::frsize as
// statfs::f_bsize (which affects free space display in the Finder).
out.St.Bsize = o.IoSize
out.St.Frsize = o.BlockSize
case *fuseops.RemoveXattrOp:
// Empty response
case *fuseops.GetXattrOp:
// convertInMessage already set up the destination buffer to be at the end
// of the out message. We need only shrink to the right size based on how
// much the user read.
if len(o.Dst) == 0 {
writeXattrSize(m, uint32(o.BytesRead))
} else {
m.ShrinkTo(buffer.OutMessageHeaderSize + o.BytesRead)
}
case *fuseops.ListXattrOp:
if len(o.Dst) == 0 {
writeXattrSize(m, uint32(o.BytesRead))
} else {
m.ShrinkTo(buffer.OutMessageHeaderSize + o.BytesRead)
}
case *fuseops.SetXattrOp:
// Empty response
case *fuseops.FallocateOp:
// Empty response
case *fuseops.SyncFSOp:
// Empty response
case *initOp:
out := (*fusekernel.InitOut)(m.Grow(int(unsafe.Sizeof(fusekernel.InitOut{}))))
out.Major = o.Library.Major
out.Minor = o.Library.Minor
out.MaxReadahead = o.MaxReadahead
out.Flags = uint32(o.Flags)
// Default values
out.MaxBackground = 12
out.CongestionThreshold = 9
out.MaxWrite = o.MaxWrite
out.TimeGran = 1
out.MaxPages = o.MaxPages