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.

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