forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libsdk.go
1755 lines (1542 loc) · 41.3 KB
/
libsdk.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 2020 The CubeFS Authors.
//
// 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 main
/*
#define _GNU_SOURCE
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
struct cfs_stat_info {
uint64_t ino;
uint64_t size;
uint64_t blocks;
uint64_t atime;
uint64_t mtime;
uint64_t ctime;
uint32_t atime_nsec;
uint32_t mtime_nsec;
uint32_t ctime_nsec;
mode_t mode;
uint32_t nlink;
uint32_t blk_size;
uint32_t uid;
uint32_t gid;
};
struct cfs_summary_info {
int64_t files;
int64_t subdirs;
int64_t fbytes;
};
struct cfs_dirent {
uint64_t ino;
char name[256];
char d_type;
uint32_t nameLen;
};
struct cfs_hdfs_stat_info {
uint64_t size;
uint64_t atime;
uint64_t mtime;
uint32_t atime_nsec;
uint32_t mtime_nsec;
mode_t mode;
};
struct cfs_dirent_info {
struct cfs_hdfs_stat_info stat;
char d_type;
char name[256];
uint32_t nameLen;
};
*/
import "C"
import (
"context"
"fmt"
"io"
syslog "log"
"os"
"path"
gopath "path"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
"unsafe"
"github.com/bits-and-blooms/bitset"
"github.com/cubefs/cubefs/blobstore/api/access"
"github.com/cubefs/cubefs/blobstore/common/trace"
"github.com/cubefs/cubefs/blockcache/bcache"
"github.com/cubefs/cubefs/client/fs"
"github.com/cubefs/cubefs/proto"
"github.com/cubefs/cubefs/sdk/data/blobstore"
"github.com/cubefs/cubefs/sdk/data/stream"
masterSDK "github.com/cubefs/cubefs/sdk/master"
"github.com/cubefs/cubefs/sdk/meta"
"github.com/cubefs/cubefs/util/auditlog"
"github.com/cubefs/cubefs/util/buf"
"github.com/cubefs/cubefs/util/errors"
"github.com/cubefs/cubefs/util/log"
"github.com/cubefs/cubefs/util/stat"
)
const (
defaultBlkSize = uint32(1) << 12
maxFdNum uint = 10240000
MaxSizePutOnce = int64(1) << 23
)
var gClientManager *clientManager
var (
statusOK = C.int(0)
// error status must be minus value
statusEIO = errorToStatus(syscall.EIO)
statusEINVAL = errorToStatus(syscall.EINVAL)
statusEEXIST = errorToStatus(syscall.EEXIST)
statusEBADFD = errorToStatus(syscall.EBADFD)
statusEACCES = errorToStatus(syscall.EACCES)
statusEMFILE = errorToStatus(syscall.EMFILE)
statusENOTDIR = errorToStatus(syscall.ENOTDIR)
statusEISDIR = errorToStatus(syscall.EISDIR)
statusENOSPC = errorToStatus(syscall.ENOSPC)
statusEPERM = errorToStatus(syscall.EPERM)
)
func init() {
gClientManager = &clientManager{
clients: make(map[int64]*client),
}
}
func errorToStatus(err error) C.int {
if err == nil {
return 0
}
if errno, is := err.(syscall.Errno); is {
return -C.int(errno)
}
return -C.int(syscall.EIO)
}
type clientManager struct {
nextClientID int64
clients map[int64]*client
mu sync.RWMutex
}
func newClient() *client {
id := atomic.AddInt64(&gClientManager.nextClientID, 1)
c := &client{
id: id,
fdmap: make(map[uint]*file),
fdset: bitset.New(maxFdNum),
dirChildrenNumLimit: proto.DefaultDirChildrenNumLimit,
cwd: "/",
sc: fs.NewSummaryCache(fs.DefaultSummaryExpiration, fs.MaxSummaryCache),
ic: fs.NewInodeCache(fs.DefaultInodeExpiration, fs.MaxInodeCache),
dc: fs.NewDentryCache(),
}
gClientManager.mu.Lock()
gClientManager.clients[id] = c
gClientManager.mu.Unlock()
return c
}
func getClient(id int64) (c *client, exist bool) {
gClientManager.mu.RLock()
defer gClientManager.mu.RUnlock()
c, exist = gClientManager.clients[id]
return
}
func removeClient(id int64) {
gClientManager.mu.Lock()
defer gClientManager.mu.Unlock()
delete(gClientManager.clients, id)
}
type file struct {
fd uint
ino uint64
pino uint64
flags uint32
mode uint32
// dir only
dirp *dirStream
// rw
fileWriter *blobstore.Writer
fileReader *blobstore.Reader
path string
}
type dirStream struct {
pos int
dirents []proto.Dentry
}
type client struct {
// client id allocated by libsdk
id int64
// mount config
volName string
masterAddr string
followerRead bool
logDir string
logLevel string
ebsEndpoint string
servicePath string
volType int
cacheAction int
ebsBlockSize int
enableBcache bool
readBlockThread int
writeBlockThread int
cacheRuleKey string
cacheThreshold int
enableSummary bool
secretKey string
accessKey string
subDir string
pushAddr string
cluster string
dirChildrenNumLimit uint32
enableAudit bool
// runtime context
cwd string // current working directory
fdmap map[uint]*file
fdset *bitset.BitSet
fdlock sync.RWMutex
// server info
mw *meta.MetaWrapper
ec *stream.ExtentClient
ic *fs.InodeCache
dc *fs.DentryCache
bc *bcache.BcacheClient
ebsc *blobstore.BlobStoreClient
sc *fs.SummaryCache
mu sync.Mutex
}
//export cfs_IsDir
func cfs_IsDir(mode C.mode_t) C.char {
var isDir uint8 = 0
if (mode & C.S_IFMT) == C.S_IFDIR {
isDir = 1
}
return C.char(isDir)
}
//export cfs_IsRegular
func cfs_IsRegular(mode C.mode_t) C.char {
var isRegular uint8 = 0
if (mode & C.S_IFMT) == C.S_IFREG {
isRegular = 1
}
return C.char(isRegular)
}
//export cfs_symlink
func cfs_symlink(id C.int64_t, src_path *C.char, dst_path *C.char) C.int {
c, exist := getClient(int64(id))
if !exist {
return statusEINVAL
}
fullSrcPath := c.absPath(C.GoString(src_path))
fullDstPath := c.absPath(C.GoString(dst_path))
parent_dir := path.Dir(fullDstPath)
filename := path.Base(fullDstPath)
info, err := c.lookupPath(parent_dir)
if err != nil {
return errorToStatus(err)
}
parentIno := info.Inode
info, err = c.mw.Create_ll(parentIno, filename, proto.Mode(os.ModeSymlink|os.ModePerm), 0, 0, []byte(fullSrcPath), fullDstPath, false)
if err != nil {
log.LogErrorf("Symlink: parent(%v) NewName(%v) err(%v)\n", parentIno, filename, err)
return errorToStatus(err)
}
c.ic.Put(info)
log.LogDebugf("Symlink: src_path(%s) dst_path(%s)\n", fullSrcPath, fullDstPath)
return statusOK
}
//export cfs_link
func cfs_link(id C.int64_t, src_path *C.char, dst_path *C.char) C.int {
c, exist := getClient(int64(id))
if !exist {
return statusEINVAL
}
fullSrcPath := c.absPath(C.GoString(src_path))
info, err := c.lookupPath(fullSrcPath)
if err != nil {
return errorToStatus(err)
}
src_ino := info.Inode
if !proto.IsRegular(info.Mode) {
log.LogErrorf("Link: not regular, src_path(%s) src_ino(%v) mode(%v)\n", fullSrcPath, src_ino, proto.OsMode(info.Mode))
return statusEPERM
}
fullDstPath := c.absPath(C.GoString(dst_path))
parent_dir := path.Dir(fullDstPath)
filename := path.Base(fullDstPath)
info, err = c.lookupPath(parent_dir)
if err != nil {
return errorToStatus(err)
}
parentIno := info.Inode
info, err = c.mw.Link(parentIno, filename, src_ino, fullDstPath)
if err != nil {
log.LogErrorf("Link: src_path(%s) src_ino(%v) dst_path(%s) parent(%v) err(%v)\n", fullSrcPath, src_ino, fullDstPath, parentIno, err)
return errorToStatus(err)
}
c.ic.Put(info)
log.LogDebugf("Link: src_path(%s) src_ino(%v) dst_path(%s) dst_ino(%v) parent(%v)\n", fullSrcPath, src_ino, fullDstPath, info.Inode, parentIno)
return statusOK
}
//export cfs_get_dir_lock
func cfs_get_dir_lock(id C.int64_t, path *C.char, lock_id *C.int64_t, valid_time **C.char) C.int {
c, exist := getClient(int64(id))
if !exist {
return statusEINVAL
}
c.mu.Lock()
defer c.mu.Unlock()
log.LogDebugf("cfs_get_dir_lock begin path(%s)\n", c.absPath(C.GoString(path)))
info, err := c.lookupPath(c.absPath(C.GoString(path)))
if err != nil {
return errorToStatus(err)
}
ino := info.Inode
dir_lock, err := c.getDirLock(ino)
if err != nil {
log.LogErrorf("getDirLock failed, path(%s) ino(%v) err(%v)", c.absPath(C.GoString(path)), ino, err)
return errorToStatus(err)
}
if len(dir_lock) == 0 {
log.LogDebugf("dir(%s) is not locked\n", c.absPath(C.GoString(path)))
return errorToStatus(syscall.ENOENT)
}
parts := strings.Split(string(dir_lock), "|")
lockIdStr := parts[0]
lease := parts[1]
lockId, _ := strconv.Atoi(lockIdStr)
*lock_id = C.int64_t(lockId)
*valid_time = C.CString(lease)
log.LogDebugf("cfs_get_dir_lock end path(%s) lock_id(%d) lease(%s)\n", c.absPath(C.GoString(path)), lockId, lease)
return statusOK
}
//export cfs_lock_dir
func cfs_lock_dir(id C.int64_t, path *C.char, lease C.uint64_t, lockId C.int64_t) C.int64_t {
c, exist := getClient(int64(id))
if !exist {
return C.int64_t(statusEINVAL)
}
c.mu.Lock()
defer c.mu.Unlock()
dirpath := c.absPath(C.GoString(path))
log.LogDebugf("cfs_lock_dir path(%s) lease(%d) lockId(%d)\n", dirpath, lease, lockId)
info, err := c.lookupPath(dirpath)
if err != nil {
log.LogErrorf("cfs_lock_dir lookupPath failed, err%v\n", err)
return C.int64_t(errorToStatus(err))
}
ino := info.Inode
retLockId, err := c.lockDir(ino, uint64(lease), int64(lockId))
if err != nil {
log.LogErrorf("cfs_lock_dir failed, dir(%s) ino(%v) err(%v)", dirpath, ino, err)
return C.int64_t(errorToStatus(err))
}
log.LogDebugf("cfs_lock_dir success dir(%s) ino(%v) retLockId(%d)\n", dirpath, ino, retLockId)
return C.int64_t(retLockId)
}
//export cfs_unlock_dir
func cfs_unlock_dir(id C.int64_t, path *C.char) C.int {
c, exist := getClient(int64(id))
if !exist {
return statusEINVAL
}
c.mu.Lock()
defer c.mu.Unlock()
info, err := c.lookupPath(c.absPath(C.GoString(path)))
if err != nil {
return errorToStatus(err)
}
ino := info.Inode
if err = c.unlockDir(ino); err != nil {
log.LogErrorf("unlockDir failed, ino(%v) err(%v)", ino, err)
return errorToStatus(err)
}
return statusOK
}
//export cfs_new_client
func cfs_new_client() C.int64_t {
c := newClient()
// Just skip fd 0, 1, 2, to avoid confusion.
c.fdset.Set(0).Set(1).Set(2)
return C.int64_t(c.id)
}
//export cfs_set_client
func cfs_set_client(id C.int64_t, key, val *C.char) C.int {
c, exist := getClient(int64(id))
if !exist {
return statusEINVAL
}
k := C.GoString(key)
v := C.GoString(val)
switch k {
case "volName":
c.volName = v
case "masterAddr":
c.masterAddr = v
case "followerRead":
if v == "true" {
c.followerRead = true
} else {
c.followerRead = false
}
case "logDir":
c.logDir = v
case "logLevel":
c.logLevel = v
case "enableBcache":
if v == "true" {
c.enableBcache = true
} else {
c.enableBcache = false
}
case "readBlockThread":
rt, err := strconv.Atoi(v)
if err == nil {
c.readBlockThread = rt
}
case "writeBlockThread":
wt, err := strconv.Atoi(v)
if err == nil {
c.writeBlockThread = wt
}
case "enableSummary":
if v == "true" {
c.enableSummary = true
} else {
c.enableSummary = false
}
case "accessKey":
c.accessKey = v
case "secretKey":
c.secretKey = v
case "pushAddr":
c.pushAddr = v
case "enableAudit":
if v == "true" {
c.enableAudit = true
} else {
c.enableAudit = false
}
default:
return statusEINVAL
}
return statusOK
}
//export cfs_start_client
func cfs_start_client(id C.int64_t) C.int {
c, exist := getClient(int64(id))
if !exist {
return statusEINVAL
}
err := c.start()
if err != nil {
syslog.Println(err)
return statusEIO
}
return statusOK
}
//export cfs_close_client
func cfs_close_client(id C.int64_t) {
if c, exist := getClient(int64(id)); exist {
if c.ec != nil {
_ = c.ec.Close()
}
if c.mw != nil {
_ = c.mw.Close()
}
removeClient(int64(id))
}
auditlog.StopAudit()
log.LogFlush()
}
//export cfs_chdir
func cfs_chdir(id C.int64_t, path *C.char) C.int {
c, exist := getClient(int64(id))
if !exist {
return statusEINVAL
}
cwd := c.absPath(C.GoString(path))
dirInfo, err := c.lookupPath(cwd)
if err != nil {
return errorToStatus(err)
}
if !proto.IsDir(dirInfo.Mode) {
return statusENOTDIR
}
c.cwd = cwd
return statusOK
}
//export cfs_getcwd
func cfs_getcwd(id C.int64_t) *C.char {
c, exist := getClient(int64(id)) // client's working directory
if !exist {
return C.CString("")
}
return C.CString(c.cwd)
}
//export cfs_getattr
func cfs_getattr(id C.int64_t, path *C.char, stat *C.struct_cfs_stat_info) C.int {
c, exist := getClient(int64(id))
if !exist {
return statusEINVAL
}
info, err := c.lookupPath(c.absPath(C.GoString(path)))
if err != nil {
return errorToStatus(err)
}
// fill up the stat
stat.ino = C.uint64_t(info.Inode)
stat.size = C.uint64_t(info.Size)
stat.nlink = C.uint32_t(info.Nlink)
stat.blk_size = C.uint32_t(defaultBlkSize)
stat.uid = C.uint32_t(info.Uid)
stat.gid = C.uint32_t(info.Gid)
if info.Size%512 != 0 {
stat.blocks = C.uint64_t(info.Size>>9) + 1
} else {
stat.blocks = C.uint64_t(info.Size >> 9)
}
// fill up the mode
if proto.IsRegular(info.Mode) {
stat.mode = C.uint32_t(C.S_IFREG) | C.uint32_t(info.Mode&0o777)
} else if proto.IsDir(info.Mode) {
stat.mode = C.uint32_t(C.S_IFDIR) | C.uint32_t(info.Mode&0o777)
} else if proto.IsSymlink(info.Mode) {
stat.mode = C.uint32_t(C.S_IFLNK) | C.uint32_t(info.Mode&0o777)
} else {
stat.mode = C.uint32_t(C.S_IFSOCK) | C.uint32_t(info.Mode&0o777)
}
// fill up the time struct
t := info.AccessTime.UnixNano()
stat.atime = C.uint64_t(t / 1e9)
stat.atime_nsec = C.uint32_t(t % 1e9)
t = info.ModifyTime.UnixNano()
stat.mtime = C.uint64_t(t / 1e9)
stat.mtime_nsec = C.uint32_t(t % 1e9)
t = info.CreateTime.UnixNano()
stat.ctime = C.uint64_t(t / 1e9)
stat.ctime_nsec = C.uint32_t(t % 1e9)
return statusOK
}
//export cfs_setattr
func cfs_setattr(id C.int64_t, path *C.char, stat *C.struct_cfs_stat_info, valid C.int) C.int {
c, exist := getClient(int64(id))
if !exist {
return statusEINVAL
}
info, err := c.lookupPath(c.absPath(C.GoString(path)))
if err != nil {
return errorToStatus(err)
}
err = c.setattr(info, uint32(valid), uint32(stat.mode), uint32(stat.uid), uint32(stat.gid), int64(stat.atime), int64(stat.mtime))
if err != nil {
return errorToStatus(err)
}
c.ic.Delete(info.Inode)
return statusOK
}
//export cfs_open
func cfs_open(id C.int64_t, path *C.char, flags C.int, mode C.mode_t) C.int {
c, exist := getClient(int64(id))
if !exist {
return statusEINVAL
}
start := time.Now()
fuseMode := uint32(mode) & uint32(0o777)
fuseFlags := uint32(flags) &^ uint32(0x8000)
accFlags := fuseFlags & uint32(C.O_ACCMODE)
absPath := c.absPath(C.GoString(path))
var info *proto.InodeInfo
var parentIno uint64
/*
* Note that the rwx mode is ignored when using libsdk
*/
if fuseFlags&uint32(C.O_CREAT) != 0 {
if accFlags != uint32(C.O_WRONLY) && accFlags != uint32(C.O_RDWR) {
return statusEACCES
}
dirpath, name := gopath.Split(absPath)
dirInfo, err := c.lookupPath(dirpath)
if err != nil {
return errorToStatus(err)
}
parentIno = dirInfo.Inode
defer func() {
if info != nil {
auditlog.LogClientOp("Create", dirpath, "nil", err, time.Since(start).Microseconds(), info.Inode, 0)
} else {
auditlog.LogClientOp("Create", dirpath, "nil", err, time.Since(start).Microseconds(), 0, 0)
}
}()
newInfo, err := c.create(dirInfo.Inode, name, fuseMode, absPath)
if err != nil {
if err != syscall.EEXIST {
return errorToStatus(err)
}
newInfo, err = c.lookupPath(absPath)
if err != nil {
return errorToStatus(err)
}
}
info = newInfo
} else {
dirpath, _ := gopath.Split(absPath)
dirInfo, err := c.lookupPath(dirpath)
if err != nil {
return errorToStatus(err)
}
parentIno = dirInfo.Inode // parent inode
newInfo, err := c.lookupPath(absPath)
if err != nil {
return errorToStatus(err)
}
info = newInfo
}
var fileCache bool
if c.cacheRuleKey == "" {
fileCache = false
} else {
fileCachePattern := fmt.Sprintf(".*%s.*", c.cacheRuleKey)
fileCache, _ = regexp.MatchString(fileCachePattern, absPath)
}
f := c.allocFD(info.Inode, fuseFlags, fuseMode, fileCache, info.Size, parentIno, absPath)
if f == nil {
return statusEMFILE
}
if proto.IsRegular(info.Mode) {
c.openStream(f)
if fuseFlags&uint32(C.O_TRUNC) != 0 {
if accFlags != uint32(C.O_WRONLY) && accFlags != uint32(C.O_RDWR) {
c.closeStream(f)
c.releaseFD(f.fd)
return statusEACCES
}
if err := c.truncate(f, 0); err != nil {
c.closeStream(f)
c.releaseFD(f.fd)
return statusEIO
}
}
}
return C.int(f.fd)
}
//export cfs_flush
func cfs_flush(id C.int64_t, fd C.int) C.int {
c, exist := getClient(int64(id))
if !exist {
return statusEINVAL
}
f := c.getFile(uint(fd))
if f == nil {
return statusEBADFD
}
err := c.flush(f)
if err != nil {
return statusEIO
}
c.ic.Delete(f.ino)
return statusOK
}
//export cfs_close
func cfs_close(id C.int64_t, fd C.int) {
c, exist := getClient(int64(id))
if !exist {
return
}
f := c.getFile(uint(fd))
if f == nil {
return
}
info := c.ic.Get(f.ino)
if info == nil {
info, _ = c.mw.InodeGet_ll(f.ino)
}
f = c.releaseFD(uint(fd))
// Consistent with cfs open, do close and closeStream only if f is regular file
if f != nil && info != nil && proto.IsRegular(info.Mode) {
c.flush(f)
c.closeStream(f)
}
}
//export cfs_truncate
func cfs_truncate(id C.int64_t, fd C.int, size C.size_t) C.int {
c, exist := getClient(int64(id))
if !exist {
return statusEINVAL
}
f := c.getFile(uint(fd))
if f == nil {
return statusEBADFD
}
if err := c.truncate(f, int(size)); err != nil {
return statusEIO
}
return statusOK
}
//export cfs_write
func cfs_write(id C.int64_t, fd C.int, buf unsafe.Pointer, size C.size_t, off C.off_t) C.ssize_t {
c, exist := getClient(int64(id))
if !exist {
return C.ssize_t(statusEINVAL)
}
f := c.getFile(uint(fd))
if f == nil {
return C.ssize_t(statusEBADFD)
}
accFlags := f.flags & uint32(C.O_ACCMODE)
if accFlags != uint32(C.O_WRONLY) && accFlags != uint32(C.O_RDWR) {
return C.ssize_t(statusEACCES)
}
var buffer []byte
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&buffer))
hdr.Data = uintptr(buf)
hdr.Len = int(size)
hdr.Cap = int(size)
var flags int
var wait bool
if f.flags&uint32(C.O_DIRECT) != 0 || f.flags&uint32(C.O_SYNC) != 0 || f.flags&uint32(C.O_DSYNC) != 0 {
if proto.IsHot(c.volType) {
wait = true
}
}
if f.flags&uint32(C.O_APPEND) != 0 || proto.IsCold(c.volType) {
flags |= proto.FlagsAppend
flags |= proto.FlagsSyncWrite
}
n, err := c.write(f, int(off), buffer, flags)
if err != nil {
if err == syscall.ENOSPC {
return C.ssize_t(statusENOSPC)
}
return C.ssize_t(statusEIO)
}
if wait {
if err = c.flush(f); err != nil {
return C.ssize_t(statusEIO)
}
}
return C.ssize_t(n)
}
//export cfs_read
func cfs_read(id C.int64_t, fd C.int, buf unsafe.Pointer, size C.size_t, off C.off_t) C.ssize_t {
c, exist := getClient(int64(id))
if !exist {
return C.ssize_t(statusEINVAL)
}
f := c.getFile(uint(fd))
if f == nil {
return C.ssize_t(statusEBADFD)
}
accFlags := f.flags & uint32(C.O_ACCMODE)
if accFlags == uint32(C.O_WRONLY) {
return C.ssize_t(statusEACCES)
}
var buffer []byte
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&buffer))
hdr.Data = uintptr(buf)
hdr.Len = int(size)
hdr.Cap = int(size)
n, err := c.read(f, int(off), buffer)
if err != nil {
return C.ssize_t(statusEIO)
}
return C.ssize_t(n)
}
//export cfs_batch_get_inodes
func cfs_batch_get_inodes(id C.int64_t, fd C.int, iids unsafe.Pointer, stats []C.struct_cfs_stat_info, count C.int) (n C.int) {
c, exist := getClient(int64(id))
if !exist {
return statusEINVAL
}
f := c.getFile(uint(fd))
if f == nil {
return statusEBADFD
}
var inodeIDS []uint64
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&inodeIDS))
hdr.Data = uintptr(iids)
hdr.Len = int(count)
hdr.Cap = int(count)
infos := c.mw.BatchInodeGet(inodeIDS)
if len(infos) > int(count) {
return statusEINVAL
}
for i := 0; i < len(infos); i++ {
// fill up the stat
stats[i].ino = C.uint64_t(infos[i].Inode)
stats[i].size = C.uint64_t(infos[i].Size)
stats[i].blocks = C.uint64_t(infos[i].Size >> 9)
stats[i].nlink = C.uint32_t(infos[i].Nlink)
stats[i].blk_size = C.uint32_t(defaultBlkSize)
stats[i].uid = C.uint32_t(infos[i].Uid)
stats[i].gid = C.uint32_t(infos[i].Gid)
// fill up the mode
if proto.IsRegular(infos[i].Mode) {
stats[i].mode = C.uint32_t(C.S_IFREG) | C.uint32_t(infos[i].Mode&0o777)
} else if proto.IsDir(infos[i].Mode) {
stats[i].mode = C.uint32_t(C.S_IFDIR) | C.uint32_t(infos[i].Mode&0o777)
} else if proto.IsSymlink(infos[i].Mode) {
stats[i].mode = C.uint32_t(C.S_IFLNK) | C.uint32_t(infos[i].Mode&0o777)
} else {
stats[i].mode = C.uint32_t(C.S_IFSOCK) | C.uint32_t(infos[i].Mode&0o777)
}
// fill up the time struct
t := infos[i].AccessTime.UnixNano()
stats[i].atime = C.uint64_t(t / 1e9)
stats[i].atime_nsec = C.uint32_t(t % 1e9)
t = infos[i].ModifyTime.UnixNano()
stats[i].mtime = C.uint64_t(t / 1e9)
stats[i].mtime_nsec = C.uint32_t(t % 1e9)
t = infos[i].CreateTime.UnixNano()
stats[i].ctime = C.uint64_t(t / 1e9)
stats[i].ctime_nsec = C.uint32_t(t % 1e9)
}
n = C.int(len(infos))
return
}
//export cfs_refreshsummary
func cfs_refreshsummary(id C.int64_t, path *C.char, goroutine_num C.int) C.int {
c, exist := getClient(int64(id))
if !exist {
return statusEINVAL
}
if !c.enableSummary {
return statusEINVAL
}
info, err := c.lookupPath(c.absPath(C.GoString(path)))
var ino uint64
if err != nil {
ino = proto.RootIno
} else {
ino = info.Inode
}
goroutineNum := int32(goroutine_num)
err = c.mw.RefreshSummary_ll(ino, goroutineNum)
if err != nil {
return errorToStatus(err)
}
return statusOK
}
/*
* Note that readdir is not thread-safe according to the POSIX spec.
*/
//export cfs_readdir
func cfs_readdir(id C.int64_t, fd C.int, dirents []C.struct_cfs_dirent, count C.int) (n C.int) {
c, exist := getClient(int64(id))
if !exist {
return statusEINVAL
}
f := c.getFile(uint(fd))
if f == nil {
return statusEBADFD
}
if f.dirp == nil {
f.dirp = &dirStream{}
dentries, err := c.mw.ReadDir_ll(f.ino)
if err != nil {
return errorToStatus(err)
}
f.dirp.dirents = dentries
}
dirp := f.dirp
for dirp.pos < len(dirp.dirents) && n < count {
// fill up ino
dirents[n].ino = C.uint64_t(dirp.dirents[dirp.pos].Inode)
// fill up d_type
if proto.IsRegular(dirp.dirents[dirp.pos].Type) {
dirents[n].d_type = C.DT_REG