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.

511 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) error {
  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. return nil
  227. }
  228. if dir.wfs.option.AsyncMetaDataCaching {
  229. listedEntries, listErr := dir.wfs.metaCache.ListDirectoryEntries(context.Background(), util.FullPath(dir.FullPath()), "", false, int(dir.wfs.option.DirListCacheLimit))
  230. if listErr != nil {
  231. glog.Errorf("list meta cache: %v", listErr)
  232. return nil, fuse.EIO
  233. }
  234. for _, cachedEntry := range listedEntries {
  235. processEachEntryFn(cachedEntry.ToProtoEntry(), false)
  236. }
  237. return
  238. }
  239. readErr := filer_pb.ReadDirAllEntries(dir.wfs, util.FullPath(dir.FullPath()), "", processEachEntryFn)
  240. if readErr != nil {
  241. glog.V(0).Infof("list %s: %v", dir.FullPath(), err)
  242. return ret, fuse.EIO
  243. }
  244. return ret, err
  245. }
  246. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  247. if !req.Dir {
  248. return dir.removeOneFile(req)
  249. }
  250. return dir.removeFolder(req)
  251. }
  252. func (dir *Dir) removeOneFile(req *fuse.RemoveRequest) error {
  253. filePath := util.NewFullPath(dir.FullPath(), req.Name)
  254. entry, err := filer_pb.GetEntry(dir.wfs, filePath)
  255. if err != nil {
  256. return err
  257. }
  258. if entry == nil {
  259. return nil
  260. }
  261. dir.wfs.deleteFileChunks(entry.Chunks)
  262. dir.wfs.cacheDelete(filePath)
  263. dir.wfs.fsNodeCache.DeleteFsNode(filePath)
  264. if dir.wfs.option.AsyncMetaDataCaching {
  265. dir.wfs.metaCache.DeleteEntry(context.Background(), filePath)
  266. }
  267. glog.V(3).Infof("remove file: %v", req)
  268. err = filer_pb.Remove(dir.wfs, dir.FullPath(), req.Name, false, false, false)
  269. if err != nil {
  270. glog.V(3).Infof("not found remove file %s/%s: %v", dir.FullPath(), req.Name, err)
  271. return fuse.ENOENT
  272. }
  273. return nil
  274. }
  275. func (dir *Dir) removeFolder(req *fuse.RemoveRequest) error {
  276. t := util.NewFullPath(dir.FullPath(), req.Name)
  277. dir.wfs.cacheDelete(t)
  278. dir.wfs.fsNodeCache.DeleteFsNode(t)
  279. if dir.wfs.option.AsyncMetaDataCaching {
  280. dir.wfs.metaCache.DeleteEntry(context.Background(), t)
  281. }
  282. glog.V(3).Infof("remove directory entry: %v", req)
  283. err := filer_pb.Remove(dir.wfs, dir.FullPath(), req.Name, true, false, false)
  284. if err != nil {
  285. glog.V(3).Infof("not found remove %s/%s: %v", dir.FullPath(), req.Name, err)
  286. return fuse.ENOENT
  287. }
  288. return nil
  289. }
  290. func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  291. glog.V(3).Infof("%v dir setattr %+v", dir.FullPath(), req)
  292. if err := dir.maybeLoadEntry(); err != nil {
  293. return err
  294. }
  295. if req.Valid.Mode() {
  296. dir.entry.Attributes.FileMode = uint32(req.Mode)
  297. }
  298. if req.Valid.Uid() {
  299. dir.entry.Attributes.Uid = req.Uid
  300. }
  301. if req.Valid.Gid() {
  302. dir.entry.Attributes.Gid = req.Gid
  303. }
  304. if req.Valid.Mtime() {
  305. dir.entry.Attributes.Mtime = req.Mtime.Unix()
  306. }
  307. dir.wfs.cacheDelete(util.FullPath(dir.FullPath()))
  308. return dir.saveEntry()
  309. }
  310. func (dir *Dir) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  311. glog.V(4).Infof("dir Setxattr %s: %s", dir.FullPath(), req.Name)
  312. if err := dir.maybeLoadEntry(); err != nil {
  313. return err
  314. }
  315. if err := setxattr(dir.entry, req); err != nil {
  316. return err
  317. }
  318. dir.wfs.cacheDelete(util.FullPath(dir.FullPath()))
  319. return dir.saveEntry()
  320. }
  321. func (dir *Dir) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  322. glog.V(4).Infof("dir Removexattr %s: %s", dir.FullPath(), req.Name)
  323. if err := dir.maybeLoadEntry(); err != nil {
  324. return err
  325. }
  326. if err := removexattr(dir.entry, req); err != nil {
  327. return err
  328. }
  329. dir.wfs.cacheDelete(util.FullPath(dir.FullPath()))
  330. return dir.saveEntry()
  331. }
  332. func (dir *Dir) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  333. glog.V(4).Infof("dir Listxattr %s", dir.FullPath())
  334. if err := dir.maybeLoadEntry(); err != nil {
  335. return err
  336. }
  337. if err := listxattr(dir.entry, req, resp); err != nil {
  338. return err
  339. }
  340. return nil
  341. }
  342. func (dir *Dir) Forget() {
  343. glog.V(3).Infof("Forget dir %s", dir.FullPath())
  344. dir.wfs.fsNodeCache.DeleteFsNode(util.FullPath(dir.FullPath()))
  345. }
  346. func (dir *Dir) maybeLoadEntry() error {
  347. if dir.entry == nil {
  348. parentDirPath, name := util.FullPath(dir.FullPath()).DirAndName()
  349. entry, err := dir.wfs.maybeLoadEntry(parentDirPath, name)
  350. if err != nil {
  351. return err
  352. }
  353. dir.entry = entry
  354. }
  355. return nil
  356. }
  357. func (dir *Dir) saveEntry() error {
  358. parentDir, name := util.FullPath(dir.FullPath()).DirAndName()
  359. return dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  360. request := &filer_pb.UpdateEntryRequest{
  361. Directory: parentDir,
  362. Entry: dir.entry,
  363. }
  364. glog.V(1).Infof("save dir entry: %v", request)
  365. _, err := client.UpdateEntry(context.Background(), request)
  366. if err != nil {
  367. glog.V(0).Infof("UpdateEntry dir %s/%s: %v", parentDir, name, err)
  368. return fuse.EIO
  369. }
  370. if dir.wfs.option.AsyncMetaDataCaching {
  371. dir.wfs.metaCache.UpdateEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
  372. }
  373. return nil
  374. })
  375. }
  376. func (dir *Dir) FullPath() string {
  377. var parts []string
  378. for p := dir; p != nil; p = p.parent {
  379. if strings.HasPrefix(p.name, "/") {
  380. if len(p.name) > 1 {
  381. parts = append(parts, p.name[1:])
  382. }
  383. } else {
  384. parts = append(parts, p.name)
  385. }
  386. }
  387. if len(parts) == 0 {
  388. return "/"
  389. }
  390. var buf bytes.Buffer
  391. for i := len(parts) - 1; i >= 0; i-- {
  392. buf.WriteString("/")
  393. buf.WriteString(parts[i])
  394. }
  395. return buf.String()
  396. }