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.

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