You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
3.9 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package mount
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/hanwen/go-fuse/v2/fuse"
  9. "time"
  10. )
  11. /**
  12. * Create and open a file
  13. *
  14. * If the file does not exist, first create it with the specified
  15. * mode, and then open it.
  16. *
  17. * If this method is not implemented or under Linux kernel
  18. * versions earlier than 2.6.15, the mknod() and open() methods
  19. * will be called instead.
  20. */
  21. func (wfs *WFS) Create(cancel <-chan struct{}, in *fuse.CreateIn, name string, out *fuse.CreateOut) (code fuse.Status) {
  22. // if implemented, need to use
  23. // inode := wfs.inodeToPath.Lookup(entryFullPath)
  24. // to ensure nlookup counter
  25. return fuse.ENOSYS
  26. }
  27. /** Create a file node
  28. *
  29. * This is called for creation of all non-directory, non-symlink
  30. * nodes. If the filesystem defines a create() method, then for
  31. * regular files that will be called instead.
  32. */
  33. func (wfs *WFS) Mknod(cancel <-chan struct{}, in *fuse.MknodIn, name string, out *fuse.EntryOut) (code fuse.Status) {
  34. if wfs.IsOverQuota {
  35. return fuse.EPERM
  36. }
  37. if s := checkName(name); s != fuse.OK {
  38. return s
  39. }
  40. dirFullPath, code := wfs.inodeToPath.GetPath(in.NodeId)
  41. if code != fuse.OK {
  42. return
  43. }
  44. entryFullPath := dirFullPath.Child(name)
  45. fileMode := toOsFileMode(in.Mode)
  46. inode := wfs.inodeToPath.AllocateInode(entryFullPath, fileMode)
  47. newEntry := &filer_pb.Entry{
  48. Name: name,
  49. IsDirectory: false,
  50. Attributes: &filer_pb.FuseAttributes{
  51. Mtime: time.Now().Unix(),
  52. Crtime: time.Now().Unix(),
  53. FileMode: uint32(fileMode),
  54. Uid: in.Uid,
  55. Gid: in.Gid,
  56. Collection: wfs.option.Collection,
  57. Replication: wfs.option.Replication,
  58. TtlSec: wfs.option.TtlSec,
  59. Rdev: in.Rdev,
  60. Inode: inode,
  61. },
  62. }
  63. err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  64. wfs.mapPbIdFromLocalToFiler(newEntry)
  65. defer wfs.mapPbIdFromFilerToLocal(newEntry)
  66. request := &filer_pb.CreateEntryRequest{
  67. Directory: string(dirFullPath),
  68. Entry: newEntry,
  69. Signatures: []int32{wfs.signature},
  70. }
  71. glog.V(1).Infof("mknod: %v", request)
  72. if err := filer_pb.CreateEntry(client, request); err != nil {
  73. glog.V(0).Infof("mknod %s: %v", entryFullPath, err)
  74. return err
  75. }
  76. if err := wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry)); err != nil {
  77. return fmt.Errorf("local mknod %s: %v", entryFullPath, err)
  78. }
  79. return nil
  80. })
  81. glog.V(3).Infof("mknod %s: %v", entryFullPath, err)
  82. if err != nil {
  83. return fuse.EIO
  84. }
  85. // this is to increase nlookup counter
  86. inode = wfs.inodeToPath.Lookup(entryFullPath, fileMode, false, inode, true)
  87. wfs.outputPbEntry(out, inode, newEntry)
  88. return fuse.OK
  89. }
  90. /** Remove a file */
  91. func (wfs *WFS) Unlink(cancel <-chan struct{}, header *fuse.InHeader, name string) (code fuse.Status) {
  92. dirFullPath, code := wfs.inodeToPath.GetPath(header.NodeId)
  93. if code != fuse.OK {
  94. if code == fuse.ENOENT {
  95. return fuse.OK
  96. }
  97. return code
  98. }
  99. entryFullPath := dirFullPath.Child(name)
  100. entry, code := wfs.maybeLoadEntry(entryFullPath)
  101. if code != fuse.OK {
  102. if code == fuse.ENOENT {
  103. return fuse.OK
  104. }
  105. return code
  106. }
  107. // first, ensure the filer store can correctly delete
  108. glog.V(3).Infof("remove file: %v", entryFullPath)
  109. isDeleteData := entry != nil && entry.HardLinkCounter <= 1
  110. err := filer_pb.Remove(wfs, string(dirFullPath), name, isDeleteData, false, false, false, []int32{wfs.signature})
  111. if err != nil {
  112. glog.V(0).Infof("remove %s: %v", entryFullPath, err)
  113. return fuse.OK
  114. }
  115. // then, delete meta cache
  116. if err = wfs.metaCache.DeleteEntry(context.Background(), entryFullPath); err != nil {
  117. glog.V(3).Infof("local DeleteEntry %s: %v", entryFullPath, err)
  118. return fuse.EIO
  119. }
  120. wfs.metaCache.DeleteEntry(context.Background(), entryFullPath)
  121. wfs.inodeToPath.RemovePath(entryFullPath)
  122. return fuse.OK
  123. }