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.

495 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. dir.wfs.metaCache.InsertEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
  120. return nil
  121. }); err != nil {
  122. return nil, nil, err
  123. }
  124. var node fs.Node
  125. if request.Entry.IsDirectory {
  126. node = dir.newDirectory(util.NewFullPath(dir.FullPath(), req.Name), request.Entry)
  127. return node, nil, nil
  128. }
  129. node = dir.newFile(req.Name, request.Entry)
  130. file := node.(*File)
  131. file.isOpen++
  132. fh := dir.wfs.AcquireHandle(file, req.Uid, req.Gid)
  133. return file, fh, nil
  134. }
  135. func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
  136. glog.V(4).Infof("mkdir %s: %s", dir.FullPath(), req.Name)
  137. newEntry := &filer_pb.Entry{
  138. Name: req.Name,
  139. IsDirectory: true,
  140. Attributes: &filer_pb.FuseAttributes{
  141. Mtime: time.Now().Unix(),
  142. Crtime: time.Now().Unix(),
  143. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  144. Uid: req.Uid,
  145. Gid: req.Gid,
  146. },
  147. }
  148. err := dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  149. request := &filer_pb.CreateEntryRequest{
  150. Directory: dir.FullPath(),
  151. Entry: newEntry,
  152. }
  153. glog.V(1).Infof("mkdir: %v", request)
  154. if err := filer_pb.CreateEntry(client, request); err != nil {
  155. glog.V(0).Infof("mkdir %s/%s: %v", dir.FullPath(), req.Name, err)
  156. return err
  157. }
  158. dir.wfs.metaCache.InsertEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
  159. return nil
  160. })
  161. if err == nil {
  162. node := dir.newDirectory(util.NewFullPath(dir.FullPath(), req.Name), newEntry)
  163. return node, nil
  164. }
  165. glog.V(0).Infof("mkdir %s/%s: %v", dir.FullPath(), req.Name, err)
  166. return nil, fuse.EIO
  167. }
  168. func (dir *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (node fs.Node, err error) {
  169. glog.V(4).Infof("dir Lookup %s: %s by %s", dir.FullPath(), req.Name, req.Header.String())
  170. fullFilePath := util.NewFullPath(dir.FullPath(), req.Name)
  171. entry := dir.wfs.cacheGet(fullFilePath)
  172. dirPath := util.FullPath(dir.FullPath())
  173. meta_cache.EnsureVisited(dir.wfs.metaCache, dir.wfs, util.FullPath(dirPath))
  174. cachedEntry, cacheErr := dir.wfs.metaCache.FindEntry(context.Background(), fullFilePath)
  175. if cacheErr == filer_pb.ErrNotFound {
  176. return nil, fuse.ENOENT
  177. }
  178. entry = cachedEntry.ToProtoEntry()
  179. if entry == nil {
  180. // glog.V(3).Infof("dir Lookup cache miss %s", fullFilePath)
  181. entry, err = filer_pb.GetEntry(dir.wfs, fullFilePath)
  182. if err != nil {
  183. glog.V(1).Infof("dir GetEntry %s: %v", fullFilePath, err)
  184. return nil, fuse.ENOENT
  185. }
  186. dir.wfs.cacheSet(fullFilePath, entry, 5*time.Minute)
  187. } else {
  188. glog.V(4).Infof("dir Lookup cache hit %s", fullFilePath)
  189. }
  190. if entry != nil {
  191. if entry.IsDirectory {
  192. node = dir.newDirectory(fullFilePath, entry)
  193. } else {
  194. node = dir.newFile(req.Name, entry)
  195. }
  196. // resp.EntryValid = time.Second
  197. resp.Attr.Inode = fullFilePath.AsInode()
  198. resp.Attr.Valid = time.Second
  199. resp.Attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  200. resp.Attr.Crtime = time.Unix(entry.Attributes.Crtime, 0)
  201. resp.Attr.Mode = os.FileMode(entry.Attributes.FileMode)
  202. resp.Attr.Gid = entry.Attributes.Gid
  203. resp.Attr.Uid = entry.Attributes.Uid
  204. return node, nil
  205. }
  206. glog.V(4).Infof("not found dir GetEntry %s: %v", fullFilePath, err)
  207. return nil, fuse.ENOENT
  208. }
  209. func (dir *Dir) ReadDirAll(ctx context.Context) (ret []fuse.Dirent, err error) {
  210. glog.V(3).Infof("dir ReadDirAll %s", dir.FullPath())
  211. cacheTtl := 5 * time.Minute
  212. processEachEntryFn := func(entry *filer_pb.Entry, isLast bool) error {
  213. fullpath := util.NewFullPath(dir.FullPath(), entry.Name)
  214. inode := fullpath.AsInode()
  215. if entry.IsDirectory {
  216. dirent := fuse.Dirent{Inode: inode, Name: entry.Name, Type: fuse.DT_Dir}
  217. ret = append(ret, dirent)
  218. } else {
  219. dirent := fuse.Dirent{Inode: inode, Name: entry.Name, Type: fuse.DT_File}
  220. ret = append(ret, dirent)
  221. }
  222. dir.wfs.cacheSet(fullpath, entry, cacheTtl)
  223. return nil
  224. }
  225. dirPath := util.FullPath(dir.FullPath())
  226. meta_cache.EnsureVisited(dir.wfs.metaCache, dir.wfs, dirPath)
  227. listedEntries, listErr := dir.wfs.metaCache.ListDirectoryEntries(context.Background(), util.FullPath(dir.FullPath()), "", false, int(dir.wfs.option.DirListCacheLimit))
  228. if listErr != nil {
  229. glog.Errorf("list meta cache: %v", listErr)
  230. return nil, fuse.EIO
  231. }
  232. for _, cachedEntry := range listedEntries {
  233. processEachEntryFn(cachedEntry.ToProtoEntry(), false)
  234. }
  235. return
  236. }
  237. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  238. if !req.Dir {
  239. return dir.removeOneFile(req)
  240. }
  241. return dir.removeFolder(req)
  242. }
  243. func (dir *Dir) removeOneFile(req *fuse.RemoveRequest) error {
  244. filePath := util.NewFullPath(dir.FullPath(), req.Name)
  245. entry, err := filer_pb.GetEntry(dir.wfs, filePath)
  246. if err != nil {
  247. return err
  248. }
  249. if entry == nil {
  250. return nil
  251. }
  252. dir.wfs.deleteFileChunks(entry.Chunks)
  253. dir.wfs.cacheDelete(filePath)
  254. dir.wfs.fsNodeCache.DeleteFsNode(filePath)
  255. dir.wfs.metaCache.DeleteEntry(context.Background(), filePath)
  256. glog.V(3).Infof("remove file: %v", req)
  257. err = filer_pb.Remove(dir.wfs, dir.FullPath(), req.Name, false, false, false)
  258. if err != nil {
  259. glog.V(3).Infof("not found remove file %s/%s: %v", dir.FullPath(), req.Name, err)
  260. return fuse.ENOENT
  261. }
  262. return nil
  263. }
  264. func (dir *Dir) removeFolder(req *fuse.RemoveRequest) error {
  265. t := util.NewFullPath(dir.FullPath(), req.Name)
  266. dir.wfs.cacheDelete(t)
  267. dir.wfs.fsNodeCache.DeleteFsNode(t)
  268. dir.wfs.metaCache.DeleteEntry(context.Background(), t)
  269. glog.V(3).Infof("remove directory entry: %v", req)
  270. err := filer_pb.Remove(dir.wfs, dir.FullPath(), req.Name, true, false, false)
  271. if err != nil {
  272. glog.V(3).Infof("not found remove %s/%s: %v", dir.FullPath(), req.Name, err)
  273. return fuse.ENOENT
  274. }
  275. return nil
  276. }
  277. func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  278. glog.V(3).Infof("%v dir setattr %+v", dir.FullPath(), req)
  279. if err := dir.maybeLoadEntry(); err != nil {
  280. return err
  281. }
  282. if req.Valid.Mode() {
  283. dir.entry.Attributes.FileMode = uint32(req.Mode)
  284. }
  285. if req.Valid.Uid() {
  286. dir.entry.Attributes.Uid = req.Uid
  287. }
  288. if req.Valid.Gid() {
  289. dir.entry.Attributes.Gid = req.Gid
  290. }
  291. if req.Valid.Mtime() {
  292. dir.entry.Attributes.Mtime = req.Mtime.Unix()
  293. }
  294. dir.wfs.cacheDelete(util.FullPath(dir.FullPath()))
  295. return dir.saveEntry()
  296. }
  297. func (dir *Dir) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  298. glog.V(4).Infof("dir Setxattr %s: %s", dir.FullPath(), req.Name)
  299. if err := dir.maybeLoadEntry(); err != nil {
  300. return err
  301. }
  302. if err := setxattr(dir.entry, req); err != nil {
  303. return err
  304. }
  305. dir.wfs.cacheDelete(util.FullPath(dir.FullPath()))
  306. return dir.saveEntry()
  307. }
  308. func (dir *Dir) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  309. glog.V(4).Infof("dir Removexattr %s: %s", dir.FullPath(), req.Name)
  310. if err := dir.maybeLoadEntry(); err != nil {
  311. return err
  312. }
  313. if err := removexattr(dir.entry, req); err != nil {
  314. return err
  315. }
  316. dir.wfs.cacheDelete(util.FullPath(dir.FullPath()))
  317. return dir.saveEntry()
  318. }
  319. func (dir *Dir) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  320. glog.V(4).Infof("dir Listxattr %s", dir.FullPath())
  321. if err := dir.maybeLoadEntry(); err != nil {
  322. return err
  323. }
  324. if err := listxattr(dir.entry, req, resp); err != nil {
  325. return err
  326. }
  327. return nil
  328. }
  329. func (dir *Dir) Forget() {
  330. glog.V(3).Infof("Forget dir %s", dir.FullPath())
  331. dir.wfs.fsNodeCache.DeleteFsNode(util.FullPath(dir.FullPath()))
  332. }
  333. func (dir *Dir) maybeLoadEntry() error {
  334. if dir.entry == nil {
  335. parentDirPath, name := util.FullPath(dir.FullPath()).DirAndName()
  336. entry, err := dir.wfs.maybeLoadEntry(parentDirPath, name)
  337. if err != nil {
  338. return err
  339. }
  340. dir.entry = entry
  341. }
  342. return nil
  343. }
  344. func (dir *Dir) saveEntry() error {
  345. parentDir, name := util.FullPath(dir.FullPath()).DirAndName()
  346. return dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  347. request := &filer_pb.UpdateEntryRequest{
  348. Directory: parentDir,
  349. Entry: dir.entry,
  350. }
  351. glog.V(1).Infof("save dir entry: %v", request)
  352. _, err := client.UpdateEntry(context.Background(), request)
  353. if err != nil {
  354. glog.V(0).Infof("UpdateEntry dir %s/%s: %v", parentDir, name, err)
  355. return fuse.EIO
  356. }
  357. dir.wfs.metaCache.UpdateEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
  358. return nil
  359. })
  360. }
  361. func (dir *Dir) FullPath() string {
  362. var parts []string
  363. for p := dir; p != nil; p = p.parent {
  364. if strings.HasPrefix(p.name, "/") {
  365. if len(p.name) > 1 {
  366. parts = append(parts, p.name[1:])
  367. }
  368. } else {
  369. parts = append(parts, p.name)
  370. }
  371. }
  372. if len(parts) == 0 {
  373. return "/"
  374. }
  375. var buf bytes.Buffer
  376. for i := len(parts) - 1; i >= 0; i-- {
  377. buf.WriteString("/")
  378. buf.WriteString(parts[i])
  379. }
  380. return buf.String()
  381. }