dir.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. package filesys
  2. import (
  3. "context"
  4. "os"
  5. "strings"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/filer2"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/seaweedfs/fuse"
  11. "github.com/seaweedfs/fuse/fs"
  12. )
  13. type Dir struct {
  14. Path string
  15. wfs *WFS
  16. entry *filer_pb.Entry
  17. }
  18. var _ = fs.Node(&Dir{})
  19. var _ = fs.NodeCreater(&Dir{})
  20. var _ = fs.NodeMkdirer(&Dir{})
  21. var _ = fs.NodeRequestLookuper(&Dir{})
  22. var _ = fs.HandleReadDirAller(&Dir{})
  23. var _ = fs.NodeRemover(&Dir{})
  24. var _ = fs.NodeRenamer(&Dir{})
  25. var _ = fs.NodeSetattrer(&Dir{})
  26. var _ = fs.NodeGetxattrer(&Dir{})
  27. var _ = fs.NodeSetxattrer(&Dir{})
  28. var _ = fs.NodeRemovexattrer(&Dir{})
  29. var _ = fs.NodeListxattrer(&Dir{})
  30. var _ = fs.NodeForgetter(&Dir{})
  31. func (dir *Dir) Attr(ctx context.Context, attr *fuse.Attr) error {
  32. glog.V(3).Infof("dir Attr %s, existing attr: %+v", dir.Path, attr)
  33. // https://github.com/bazil/fuse/issues/196
  34. attr.Valid = time.Second
  35. if dir.Path == dir.wfs.option.FilerMountRootPath {
  36. dir.setRootDirAttributes(attr)
  37. glog.V(3).Infof("root dir Attr %s, attr: %+v", dir.Path, attr)
  38. return nil
  39. }
  40. if err := dir.maybeLoadEntry(ctx); err != nil {
  41. glog.V(3).Infof("dir Attr %s,err: %+v", dir.Path, err)
  42. return err
  43. }
  44. attr.Inode = filer2.FullPath(dir.Path).AsInode()
  45. attr.Mode = os.FileMode(dir.entry.Attributes.FileMode) | os.ModeDir
  46. attr.Mtime = time.Unix(dir.entry.Attributes.Mtime, 0)
  47. attr.Ctime = time.Unix(dir.entry.Attributes.Crtime, 0)
  48. attr.Gid = dir.entry.Attributes.Gid
  49. attr.Uid = dir.entry.Attributes.Uid
  50. glog.V(3).Infof("dir Attr %s, attr: %+v", dir.Path, attr)
  51. return nil
  52. }
  53. func (dir *Dir) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
  54. glog.V(4).Infof("dir Getxattr %s", dir.Path)
  55. if err := dir.maybeLoadEntry(ctx); err != nil {
  56. return err
  57. }
  58. return getxattr(dir.entry, req, resp)
  59. }
  60. func (dir *Dir) setRootDirAttributes(attr *fuse.Attr) {
  61. attr.Inode = 1 // filer2.FullPath(dir.Path).AsInode()
  62. attr.Valid = time.Hour
  63. attr.Uid = dir.wfs.option.MountUid
  64. attr.Gid = dir.wfs.option.MountGid
  65. attr.Mode = dir.wfs.option.MountMode
  66. attr.Crtime = dir.wfs.option.MountCtime
  67. attr.Ctime = dir.wfs.option.MountCtime
  68. attr.Mtime = dir.wfs.option.MountMtime
  69. attr.Atime = dir.wfs.option.MountMtime
  70. attr.BlockSize = 1024 * 1024
  71. }
  72. func (dir *Dir) newFile(name string, entry *filer_pb.Entry) fs.Node {
  73. return dir.wfs.getNode(filer2.NewFullPath(dir.Path, name), func() fs.Node {
  74. return &File{
  75. Name: name,
  76. dir: dir,
  77. wfs: dir.wfs,
  78. entry: entry,
  79. entryViewCache: nil,
  80. }
  81. })
  82. }
  83. func (dir *Dir) newDirectory(fullpath filer2.FullPath, entry *filer_pb.Entry) fs.Node {
  84. return dir.wfs.getNode(fullpath, func() fs.Node {
  85. return &Dir{Path: string(fullpath), wfs: dir.wfs, entry: entry}
  86. })
  87. }
  88. func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest,
  89. resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
  90. request := &filer_pb.CreateEntryRequest{
  91. Directory: dir.Path,
  92. Entry: &filer_pb.Entry{
  93. Name: req.Name,
  94. IsDirectory: req.Mode&os.ModeDir > 0,
  95. Attributes: &filer_pb.FuseAttributes{
  96. Mtime: time.Now().Unix(),
  97. Crtime: time.Now().Unix(),
  98. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  99. Uid: req.Uid,
  100. Gid: req.Gid,
  101. Collection: dir.wfs.option.Collection,
  102. Replication: dir.wfs.option.Replication,
  103. TtlSec: dir.wfs.option.TtlSec,
  104. },
  105. },
  106. OExcl: req.Flags&fuse.OpenExclusive != 0,
  107. }
  108. glog.V(1).Infof("create: %v", req.String())
  109. if err := dir.wfs.WithFilerClient(ctx, func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
  110. if err := filer_pb.CreateEntry(ctx, client, request); err != nil {
  111. if strings.Contains(err.Error(), "EEXIST") {
  112. return fuse.EEXIST
  113. }
  114. return fuse.EIO
  115. }
  116. return nil
  117. }); err != nil {
  118. return nil, nil, err
  119. }
  120. var node fs.Node
  121. if request.Entry.IsDirectory {
  122. node = dir.newDirectory(filer2.NewFullPath(dir.Path, req.Name), request.Entry)
  123. return node, nil, nil
  124. }
  125. node = dir.newFile(req.Name, request.Entry)
  126. file := node.(*File)
  127. file.isOpen++
  128. fh := dir.wfs.AcquireHandle(file, req.Uid, req.Gid)
  129. return file, fh, nil
  130. }
  131. func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
  132. newEntry := &filer_pb.Entry{
  133. Name: req.Name,
  134. IsDirectory: true,
  135. Attributes: &filer_pb.FuseAttributes{
  136. Mtime: time.Now().Unix(),
  137. Crtime: time.Now().Unix(),
  138. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  139. Uid: req.Uid,
  140. Gid: req.Gid,
  141. },
  142. }
  143. err := dir.wfs.WithFilerClient(ctx, func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
  144. request := &filer_pb.CreateEntryRequest{
  145. Directory: dir.Path,
  146. Entry: newEntry,
  147. }
  148. glog.V(1).Infof("mkdir: %v", request)
  149. if err := filer_pb.CreateEntry(ctx, client, request); err != nil {
  150. glog.V(0).Infof("mkdir %s/%s: %v", dir.Path, req.Name, err)
  151. return err
  152. }
  153. return nil
  154. })
  155. if err == nil {
  156. node := dir.newDirectory(filer2.NewFullPath(dir.Path, req.Name), newEntry)
  157. return node, nil
  158. }
  159. return nil, fuse.EIO
  160. }
  161. func (dir *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (node fs.Node, err error) {
  162. glog.V(4).Infof("dir Lookup %s: %s", dir.Path, req.Name)
  163. fullFilePath := filer2.NewFullPath(dir.Path, req.Name)
  164. entry := dir.wfs.cacheGet(fullFilePath)
  165. if entry == nil {
  166. // glog.V(3).Infof("dir Lookup cache miss %s", fullFilePath)
  167. entry, err = filer2.GetEntry(ctx, dir.wfs, fullFilePath)
  168. if err != nil {
  169. glog.V(1).Infof("dir GetEntry %s: %v", fullFilePath, err)
  170. return nil, fuse.ENOENT
  171. }
  172. dir.wfs.cacheSet(fullFilePath, entry, 5*time.Minute)
  173. } else {
  174. glog.V(4).Infof("dir Lookup cache hit %s", fullFilePath)
  175. }
  176. if entry != nil {
  177. if entry.IsDirectory {
  178. node = dir.newDirectory(fullFilePath, entry)
  179. } else {
  180. node = dir.newFile(req.Name, entry)
  181. }
  182. // resp.EntryValid = time.Second
  183. resp.Attr.Inode = fullFilePath.AsInode()
  184. resp.Attr.Valid = time.Second
  185. resp.Attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  186. resp.Attr.Ctime = time.Unix(entry.Attributes.Crtime, 0)
  187. resp.Attr.Mode = os.FileMode(entry.Attributes.FileMode)
  188. resp.Attr.Gid = entry.Attributes.Gid
  189. resp.Attr.Uid = entry.Attributes.Uid
  190. return node, nil
  191. }
  192. glog.V(1).Infof("not found dir GetEntry %s: %v", fullFilePath, err)
  193. return nil, fuse.ENOENT
  194. }
  195. func (dir *Dir) ReadDirAll(ctx context.Context) (ret []fuse.Dirent, err error) {
  196. glog.V(3).Infof("dir ReadDirAll %s", dir.Path)
  197. cacheTtl := 5 * time.Minute
  198. readErr := filer2.ReadDirAllEntries(ctx, dir.wfs, filer2.FullPath(dir.Path), "", func(entry *filer_pb.Entry, isLast bool) {
  199. fullpath := filer2.NewFullPath(dir.Path, entry.Name)
  200. inode := fullpath.AsInode()
  201. if entry.IsDirectory {
  202. dirent := fuse.Dirent{Inode: inode, Name: entry.Name, Type: fuse.DT_Dir}
  203. ret = append(ret, dirent)
  204. } else {
  205. dirent := fuse.Dirent{Inode: inode, Name: entry.Name, Type: fuse.DT_File}
  206. ret = append(ret, dirent)
  207. }
  208. dir.wfs.cacheSet(fullpath, entry, cacheTtl)
  209. })
  210. if readErr != nil {
  211. glog.V(0).Infof("list %s: %v", dir.Path, err)
  212. return ret, fuse.EIO
  213. }
  214. return ret, err
  215. }
  216. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  217. if !req.Dir {
  218. return dir.removeOneFile(ctx, req)
  219. }
  220. return dir.removeFolder(ctx, req)
  221. }
  222. func (dir *Dir) removeOneFile(ctx context.Context, req *fuse.RemoveRequest) error {
  223. filePath := filer2.NewFullPath(dir.Path, req.Name)
  224. entry, err := filer2.GetEntry(ctx, dir.wfs, filePath)
  225. if err != nil {
  226. return err
  227. }
  228. dir.wfs.deleteFileChunks(ctx, entry.Chunks)
  229. dir.wfs.cacheDelete(filePath)
  230. return dir.wfs.WithFilerClient(ctx, func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
  231. request := &filer_pb.DeleteEntryRequest{
  232. Directory: dir.Path,
  233. Name: req.Name,
  234. IsDeleteData: false,
  235. }
  236. glog.V(3).Infof("remove file: %v", request)
  237. _, err := client.DeleteEntry(ctx, request)
  238. if err != nil {
  239. glog.V(3).Infof("not found remove file %s/%s: %v", dir.Path, req.Name, err)
  240. return fuse.ENOENT
  241. }
  242. return nil
  243. })
  244. }
  245. func (dir *Dir) removeFolder(ctx context.Context, req *fuse.RemoveRequest) error {
  246. dir.wfs.cacheDelete(filer2.NewFullPath(dir.Path, req.Name))
  247. return dir.wfs.WithFilerClient(ctx, func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
  248. request := &filer_pb.DeleteEntryRequest{
  249. Directory: dir.Path,
  250. Name: req.Name,
  251. IsDeleteData: true,
  252. }
  253. glog.V(3).Infof("remove directory entry: %v", request)
  254. _, err := client.DeleteEntry(ctx, request)
  255. if err != nil {
  256. glog.V(3).Infof("not found remove %s/%s: %v", dir.Path, req.Name, err)
  257. return fuse.ENOENT
  258. }
  259. return nil
  260. })
  261. }
  262. func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  263. glog.V(3).Infof("%v dir setattr %+v", dir.Path, req)
  264. if err := dir.maybeLoadEntry(ctx); err != nil {
  265. return err
  266. }
  267. if req.Valid.Mode() {
  268. dir.entry.Attributes.FileMode = uint32(req.Mode)
  269. }
  270. if req.Valid.Uid() {
  271. dir.entry.Attributes.Uid = req.Uid
  272. }
  273. if req.Valid.Gid() {
  274. dir.entry.Attributes.Gid = req.Gid
  275. }
  276. if req.Valid.Mtime() {
  277. dir.entry.Attributes.Mtime = req.Mtime.Unix()
  278. }
  279. dir.wfs.cacheDelete(filer2.FullPath(dir.Path))
  280. return dir.saveEntry(ctx)
  281. }
  282. func (dir *Dir) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  283. glog.V(4).Infof("dir Setxattr %s: %s", dir.Path, req.Name)
  284. if err := dir.maybeLoadEntry(ctx); err != nil {
  285. return err
  286. }
  287. if err := setxattr(dir.entry, req); err != nil {
  288. return err
  289. }
  290. dir.wfs.cacheDelete(filer2.FullPath(dir.Path))
  291. return dir.saveEntry(ctx)
  292. }
  293. func (dir *Dir) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  294. glog.V(4).Infof("dir Removexattr %s: %s", dir.Path, req.Name)
  295. if err := dir.maybeLoadEntry(ctx); err != nil {
  296. return err
  297. }
  298. if err := removexattr(dir.entry, req); err != nil {
  299. return err
  300. }
  301. dir.wfs.cacheDelete(filer2.FullPath(dir.Path))
  302. return dir.saveEntry(ctx)
  303. }
  304. func (dir *Dir) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  305. glog.V(4).Infof("dir Listxattr %s", dir.Path)
  306. if err := dir.maybeLoadEntry(ctx); err != nil {
  307. return err
  308. }
  309. if err := listxattr(dir.entry, req, resp); err != nil {
  310. return err
  311. }
  312. return nil
  313. }
  314. func (dir *Dir) Forget() {
  315. glog.V(3).Infof("Forget dir %s", dir.Path)
  316. dir.wfs.forgetNode(filer2.FullPath(dir.Path))
  317. }
  318. func (dir *Dir) maybeLoadEntry(ctx context.Context) error {
  319. if dir.entry == nil {
  320. parentDirPath, name := filer2.FullPath(dir.Path).DirAndName()
  321. entry, err := dir.wfs.maybeLoadEntry(ctx, parentDirPath, name)
  322. if err != nil {
  323. return err
  324. }
  325. dir.entry = entry
  326. }
  327. return nil
  328. }
  329. func (dir *Dir) saveEntry(ctx context.Context) error {
  330. parentDir, name := filer2.FullPath(dir.Path).DirAndName()
  331. return dir.wfs.WithFilerClient(ctx, func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
  332. request := &filer_pb.UpdateEntryRequest{
  333. Directory: parentDir,
  334. Entry: dir.entry,
  335. }
  336. glog.V(1).Infof("save dir entry: %v", request)
  337. _, err := client.UpdateEntry(ctx, request)
  338. if err != nil {
  339. glog.V(0).Infof("UpdateEntry dir %s/%s: %v", parentDir, name, err)
  340. return fuse.EIO
  341. }
  342. return nil
  343. })
  344. }