dir.go 11 KB

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