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.

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