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.

443 lines
11 KiB

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