weedfs_file_mkrm.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package mount
  2. import (
  3. "context"
  4. "fmt"
  5. "syscall"
  6. "time"
  7. "github.com/hanwen/go-fuse/v2/fuse"
  8. "github.com/seaweedfs/seaweedfs/weed/filer"
  9. "github.com/seaweedfs/seaweedfs/weed/glog"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  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. 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. SkipCheckParentDirectory: true,
  71. }
  72. glog.V(1).Infof("mknod: %v", request)
  73. if err := filer_pb.CreateEntry(client, request); err != nil {
  74. glog.V(0).Infof("mknod %s: %v", entryFullPath, err)
  75. return err
  76. }
  77. if err := wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry)); err != nil {
  78. return fmt.Errorf("local mknod %s: %v", entryFullPath, err)
  79. }
  80. return nil
  81. })
  82. glog.V(3).Infof("mknod %s: %v", entryFullPath, err)
  83. if err != nil {
  84. return fuse.EIO
  85. }
  86. // this is to increase nlookup counter
  87. inode = wfs.inodeToPath.Lookup(entryFullPath, newEntry.Attributes.Crtime, false, false, inode, true)
  88. wfs.outputPbEntry(out, inode, newEntry)
  89. return fuse.OK
  90. }
  91. /** Remove a file */
  92. func (wfs *WFS) Unlink(cancel <-chan struct{}, header *fuse.InHeader, name string) (code fuse.Status) {
  93. dirFullPath, code := wfs.inodeToPath.GetPath(header.NodeId)
  94. if code != fuse.OK {
  95. if code == fuse.ENOENT {
  96. return fuse.OK
  97. }
  98. return code
  99. }
  100. entryFullPath := dirFullPath.Child(name)
  101. entry, code := wfs.maybeLoadEntry(entryFullPath)
  102. if code != fuse.OK {
  103. if code == fuse.ENOENT {
  104. return fuse.OK
  105. }
  106. return code
  107. }
  108. // first, ensure the filer store can correctly delete
  109. glog.V(3).Infof("remove file: %v", entryFullPath)
  110. isDeleteData := entry != nil && entry.HardLinkCounter <= 1
  111. err := filer_pb.Remove(wfs, string(dirFullPath), name, isDeleteData, false, false, false, []int32{wfs.signature})
  112. if err != nil {
  113. glog.V(0).Infof("remove %s: %v", entryFullPath, err)
  114. return fuse.OK
  115. }
  116. // then, delete meta cache
  117. if err = wfs.metaCache.DeleteEntry(context.Background(), entryFullPath); err != nil {
  118. glog.V(3).Infof("local DeleteEntry %s: %v", entryFullPath, err)
  119. return fuse.EIO
  120. }
  121. wfs.inodeToPath.RemovePath(entryFullPath)
  122. return fuse.OK
  123. }