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.

421 lines
10 KiB

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