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.

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