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.

517 lines
13 KiB

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