weedfs_rename.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package mount
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/hanwen/go-fuse/v2/fs"
  6. "github.com/hanwen/go-fuse/v2/fuse"
  7. "github.com/seaweedfs/seaweedfs/weed/filer"
  8. "github.com/seaweedfs/seaweedfs/weed/glog"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  10. "github.com/seaweedfs/seaweedfs/weed/util"
  11. "io"
  12. "strings"
  13. "syscall"
  14. )
  15. /** Rename a file
  16. *
  17. * If the target exists it should be atomically replaced. If
  18. * the target's inode's lookup count is non-zero, the file
  19. * system is expected to postpone any removal of the inode
  20. * until the lookup count reaches zero (see description of the
  21. * forget function).
  22. *
  23. * If this request is answered with an error code of ENOSYS, this is
  24. * treated as a permanent failure with error code EINVAL, i.e. all
  25. * future bmap requests will fail with EINVAL without being
  26. * send to the filesystem process.
  27. *
  28. * *flags* may be `RENAME_EXCHANGE` or `RENAME_NOREPLACE`. If
  29. * RENAME_NOREPLACE is specified, the filesystem must not
  30. * overwrite *newname* if it exists and return an error
  31. * instead. If `RENAME_EXCHANGE` is specified, the filesystem
  32. * must atomically exchange the two files, i.e. both must
  33. * exist and neither may be deleted.
  34. *
  35. * Valid replies:
  36. * fuse_reply_err
  37. *
  38. * @param req request handle
  39. * @param parent inode number of the old parent directory
  40. * @param name old name
  41. * @param newparent inode number of the new parent directory
  42. * @param newname new name
  43. */
  44. /*
  45. renameat2()
  46. renameat2() has an additional flags argument. A renameat2() call
  47. with a zero flags argument is equivalent to renameat().
  48. The flags argument is a bit mask consisting of zero or more of
  49. the following flags:
  50. RENAME_EXCHANGE
  51. Atomically exchange oldpath and newpath. Both pathnames
  52. must exist but may be of different types (e.g., one could
  53. be a non-empty directory and the other a symbolic link).
  54. RENAME_NOREPLACE
  55. Don't overwrite newpath of the rename. Return an error if
  56. newpath already exists.
  57. RENAME_NOREPLACE can't be employed together with
  58. RENAME_EXCHANGE.
  59. RENAME_NOREPLACE requires support from the underlying
  60. filesystem. Support for various filesystems was added as
  61. follows:
  62. * ext4 (Linux 3.15);
  63. * btrfs, tmpfs, and cifs (Linux 3.17);
  64. * xfs (Linux 4.0);
  65. * Support for many other filesystems was added in Linux
  66. 4.9, including ext2, minix, reiserfs, jfs, vfat, and
  67. bpf.
  68. RENAME_WHITEOUT (since Linux 3.18)
  69. This operation makes sense only for overlay/union
  70. filesystem implementations.
  71. Specifying RENAME_WHITEOUT creates a "whiteout" object at
  72. the source of the rename at the same time as performing
  73. the rename. The whole operation is atomic, so that if the
  74. rename succeeds then the whiteout will also have been
  75. created.
  76. A "whiteout" is an object that has special meaning in
  77. union/overlay filesystem constructs. In these constructs,
  78. multiple layers exist and only the top one is ever
  79. modified. A whiteout on an upper layer will effectively
  80. hide a matching file in the lower layer, making it appear
  81. as if the file didn't exist.
  82. When a file that exists on the lower layer is renamed, the
  83. file is first copied up (if not already on the upper
  84. layer) and then renamed on the upper, read-write layer.
  85. At the same time, the source file needs to be "whiteouted"
  86. (so that the version of the source file in the lower layer
  87. is rendered invisible). The whole operation needs to be
  88. done atomically.
  89. When not part of a union/overlay, the whiteout appears as
  90. a character device with a {0,0} device number. (Note that
  91. other union/overlay implementations may employ different
  92. methods for storing whiteout entries; specifically, BSD
  93. union mount employs a separate inode type, DT_WHT, which,
  94. while supported by some filesystems available in Linux,
  95. such as CODA and XFS, is ignored by the kernel's whiteout
  96. support code, as of Linux 4.19, at least.)
  97. RENAME_WHITEOUT requires the same privileges as creating a
  98. device node (i.e., the CAP_MKNOD capability).
  99. RENAME_WHITEOUT can't be employed together with
  100. RENAME_EXCHANGE.
  101. RENAME_WHITEOUT requires support from the underlying
  102. filesystem. Among the filesystems that support it are
  103. tmpfs (since Linux 3.18), ext4 (since Linux 3.18), XFS
  104. (since Linux 4.1), f2fs (since Linux 4.2), btrfs (since
  105. Linux 4.7), and ubifs (since Linux 4.9).
  106. */
  107. const (
  108. RenameEmptyFlag = 0
  109. RenameNoReplace = 1
  110. RenameExchange = fs.RENAME_EXCHANGE
  111. RenameWhiteout = 3
  112. )
  113. func (wfs *WFS) Rename(cancel <-chan struct{}, in *fuse.RenameIn, oldName string, newName string) (code fuse.Status) {
  114. if wfs.IsOverQuota {
  115. return fuse.Status(syscall.ENOSPC)
  116. }
  117. if s := checkName(newName); s != fuse.OK {
  118. return s
  119. }
  120. switch in.Flags {
  121. case RenameEmptyFlag:
  122. case RenameNoReplace:
  123. case RenameExchange:
  124. case RenameWhiteout:
  125. return fuse.ENOTSUP
  126. default:
  127. return fuse.EINVAL
  128. }
  129. oldDir, code := wfs.inodeToPath.GetPath(in.NodeId)
  130. if code != fuse.OK {
  131. return
  132. }
  133. oldPath := oldDir.Child(oldName)
  134. newDir, code := wfs.inodeToPath.GetPath(in.Newdir)
  135. if code != fuse.OK {
  136. return
  137. }
  138. newPath := newDir.Child(newName)
  139. glog.V(4).Infof("dir Rename %s => %s", oldPath, newPath)
  140. // update remote filer
  141. err := wfs.WithFilerClient(true, func(client filer_pb.SeaweedFilerClient) error {
  142. ctx, cancel := context.WithCancel(context.Background())
  143. defer cancel()
  144. request := &filer_pb.StreamRenameEntryRequest{
  145. OldDirectory: string(oldDir),
  146. OldName: oldName,
  147. NewDirectory: string(newDir),
  148. NewName: newName,
  149. Signatures: []int32{wfs.signature},
  150. }
  151. stream, err := client.StreamRenameEntry(ctx, request)
  152. if err != nil {
  153. code = fuse.EIO
  154. return fmt.Errorf("dir AtomicRenameEntry %s => %s : %v", oldPath, newPath, err)
  155. }
  156. for {
  157. resp, recvErr := stream.Recv()
  158. if recvErr != nil {
  159. if recvErr == io.EOF {
  160. break
  161. } else {
  162. if strings.Contains(recvErr.Error(), "not empty") {
  163. code = fuse.Status(syscall.ENOTEMPTY)
  164. } else if strings.Contains(recvErr.Error(), "not directory") {
  165. code = fuse.ENOTDIR
  166. }
  167. return fmt.Errorf("dir Rename %s => %s receive: %v", oldPath, newPath, recvErr)
  168. }
  169. }
  170. if err = wfs.handleRenameResponse(ctx, resp); err != nil {
  171. glog.V(0).Infof("dir Rename %s => %s : %v", oldPath, newPath, err)
  172. return err
  173. }
  174. }
  175. return nil
  176. })
  177. if err != nil {
  178. glog.V(0).Infof("Link: %v", err)
  179. return
  180. }
  181. return fuse.OK
  182. }
  183. func (wfs *WFS) handleRenameResponse(ctx context.Context, resp *filer_pb.StreamRenameEntryResponse) error {
  184. // comes from filer StreamRenameEntry, can only be create or delete entry
  185. glog.V(4).Infof("dir Rename %+v", resp.EventNotification)
  186. if resp.EventNotification.NewEntry != nil {
  187. // with new entry, the old entry name also exists. This is the first step to create new entry
  188. newEntry := filer.FromPbEntry(resp.EventNotification.NewParentPath, resp.EventNotification.NewEntry)
  189. if err := wfs.metaCache.AtomicUpdateEntryFromFiler(ctx, "", newEntry); err != nil {
  190. return err
  191. }
  192. oldParent, newParent := util.FullPath(resp.Directory), util.FullPath(resp.EventNotification.NewParentPath)
  193. oldName, newName := resp.EventNotification.OldEntry.Name, resp.EventNotification.NewEntry.Name
  194. oldPath := oldParent.Child(oldName)
  195. newPath := newParent.Child(newName)
  196. sourceInode, targetInode := wfs.inodeToPath.MovePath(oldPath, newPath)
  197. if sourceInode != 0 {
  198. fh, foundFh := wfs.fhMap.FindFileHandle(sourceInode)
  199. if foundFh {
  200. if entry := fh.GetEntry(); entry != nil {
  201. entry.Name = newName
  202. }
  203. }
  204. // invalidate attr and data
  205. // wfs.fuseServer.InodeNotify(sourceInode, 0, -1)
  206. }
  207. if targetInode != 0 {
  208. // invalidate attr and data
  209. // wfs.fuseServer.InodeNotify(targetInode, 0, -1)
  210. }
  211. } else if resp.EventNotification.OldEntry != nil {
  212. // without new entry, only old entry name exists. This is the second step to delete old entry
  213. if err := wfs.metaCache.AtomicUpdateEntryFromFiler(ctx, util.NewFullPath(resp.Directory, resp.EventNotification.OldEntry.Name), nil); err != nil {
  214. return err
  215. }
  216. }
  217. return nil
  218. }