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.

147 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 s := checkName(name); s != fuse.OK {
  35. return s
  36. }
  37. dirFullPath, code := wfs.inodeToPath.GetPath(in.NodeId)
  38. if code != fuse.OK {
  39. return
  40. }
  41. entryFullPath := dirFullPath.Child(name)
  42. fileMode := toOsFileMode(in.Mode)
  43. inode := wfs.inodeToPath.AllocateInode(entryFullPath, fileMode)
  44. newEntry := &filer_pb.Entry{
  45. Name: name,
  46. IsDirectory: false,
  47. Attributes: &filer_pb.FuseAttributes{
  48. Mtime: time.Now().Unix(),
  49. Crtime: time.Now().Unix(),
  50. FileMode: uint32(fileMode),
  51. Uid: in.Uid,
  52. Gid: in.Gid,
  53. Collection: wfs.option.Collection,
  54. Replication: wfs.option.Replication,
  55. TtlSec: wfs.option.TtlSec,
  56. Rdev: in.Rdev,
  57. Inode: inode,
  58. },
  59. }
  60. err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  61. wfs.mapPbIdFromLocalToFiler(newEntry)
  62. defer wfs.mapPbIdFromFilerToLocal(newEntry)
  63. request := &filer_pb.CreateEntryRequest{
  64. Directory: string(dirFullPath),
  65. Entry: newEntry,
  66. Signatures: []int32{wfs.signature},
  67. }
  68. glog.V(1).Infof("mknod: %v", request)
  69. if err := filer_pb.CreateEntry(client, request); err != nil {
  70. glog.V(0).Infof("mknod %s: %v", entryFullPath, err)
  71. return err
  72. }
  73. if err := wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry)); err != nil {
  74. return fmt.Errorf("local mknod %s: %v", entryFullPath, err)
  75. }
  76. return nil
  77. })
  78. glog.V(3).Infof("mknod %s: %v", entryFullPath, err)
  79. if err != nil {
  80. return fuse.EIO
  81. }
  82. // this is to increase nlookup counter
  83. inode = wfs.inodeToPath.Lookup(entryFullPath, fileMode, false, inode, true)
  84. wfs.outputPbEntry(out, inode, newEntry)
  85. return fuse.OK
  86. }
  87. /** Remove a file */
  88. func (wfs *WFS) Unlink(cancel <-chan struct{}, header *fuse.InHeader, name string) (code fuse.Status) {
  89. dirFullPath, code := wfs.inodeToPath.GetPath(header.NodeId)
  90. if code != fuse.OK {
  91. if code == fuse.ENOENT {
  92. return fuse.OK
  93. }
  94. return code
  95. }
  96. entryFullPath := dirFullPath.Child(name)
  97. entry, code := wfs.maybeLoadEntry(entryFullPath)
  98. if code != fuse.OK {
  99. if code == fuse.ENOENT {
  100. return fuse.OK
  101. }
  102. return code
  103. }
  104. // first, ensure the filer store can correctly delete
  105. glog.V(3).Infof("remove file: %v", entryFullPath)
  106. isDeleteData := entry != nil && entry.HardLinkCounter <= 1
  107. err := filer_pb.Remove(wfs, string(dirFullPath), name, isDeleteData, false, false, false, []int32{wfs.signature})
  108. if err != nil {
  109. glog.V(0).Infof("remove %s: %v", entryFullPath, err)
  110. return fuse.OK
  111. }
  112. // then, delete meta cache
  113. if err = wfs.metaCache.DeleteEntry(context.Background(), entryFullPath); err != nil {
  114. glog.V(3).Infof("local DeleteEntry %s: %v", entryFullPath, err)
  115. return fuse.EIO
  116. }
  117. wfs.metaCache.DeleteEntry(context.Background(), entryFullPath)
  118. wfs.inodeToPath.RemovePath(entryFullPath)
  119. return fuse.OK
  120. }