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.

412 lines
9.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
5 years ago
6 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. dir.wfs.listDirectoryEntriesCache.Set(fullFilePath, entry, 5*time.Minute)
  156. } else {
  157. glog.V(4).Infof("dir Lookup cache hit %s", fullFilePath)
  158. }
  159. if entry != nil {
  160. if entry.IsDirectory {
  161. node = &Dir{Path: path.Join(dir.Path, req.Name), wfs: dir.wfs, entry: entry}
  162. } else {
  163. node = dir.newFile(req.Name, entry)
  164. }
  165. resp.EntryValid = time.Duration(0)
  166. resp.Attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  167. resp.Attr.Ctime = time.Unix(entry.Attributes.Crtime, 0)
  168. resp.Attr.Mode = os.FileMode(entry.Attributes.FileMode)
  169. resp.Attr.Gid = entry.Attributes.Gid
  170. resp.Attr.Uid = entry.Attributes.Uid
  171. return node, nil
  172. }
  173. return nil, fuse.ENOENT
  174. }
  175. func (dir *Dir) ReadDirAll(ctx context.Context) (ret []fuse.Dirent, err error) {
  176. glog.V(3).Infof("dir ReadDirAll %s", dir.Path)
  177. cacheTtl := 5 * time.Minute
  178. readErr := filer2.ReadDirAllEntries(ctx, dir.wfs, dir.Path, "", func(entry *filer_pb.Entry, isLast bool) {
  179. if entry.IsDirectory {
  180. dirent := fuse.Dirent{Name: entry.Name, Type: fuse.DT_Dir}
  181. ret = append(ret, dirent)
  182. } else {
  183. dirent := fuse.Dirent{Name: entry.Name, Type: fuse.DT_File}
  184. ret = append(ret, dirent)
  185. }
  186. dir.wfs.listDirectoryEntriesCache.Set(path.Join(dir.Path, entry.Name), entry, cacheTtl)
  187. })
  188. if readErr != nil {
  189. glog.V(0).Infof("list %s: %v", dir.Path, err)
  190. return ret, fuse.EIO
  191. }
  192. return ret, err
  193. }
  194. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  195. if !req.Dir {
  196. return dir.removeOneFile(ctx, req)
  197. }
  198. return dir.removeFolder(ctx, req)
  199. }
  200. func (dir *Dir) removeOneFile(ctx context.Context, req *fuse.RemoveRequest) error {
  201. entry, err := filer2.GetEntry(ctx, dir.wfs, path.Join(dir.Path, req.Name))
  202. if err != nil {
  203. return err
  204. }
  205. dir.wfs.deleteFileChunks(ctx, entry.Chunks)
  206. dir.wfs.listDirectoryEntriesCache.Delete(path.Join(dir.Path, req.Name))
  207. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  208. request := &filer_pb.DeleteEntryRequest{
  209. Directory: dir.Path,
  210. Name: req.Name,
  211. IsDeleteData: false,
  212. }
  213. glog.V(3).Infof("remove file: %v", request)
  214. _, err := client.DeleteEntry(ctx, request)
  215. if err != nil {
  216. glog.V(3).Infof("remove file %s/%s: %v", dir.Path, req.Name, err)
  217. return fuse.ENOENT
  218. }
  219. return nil
  220. })
  221. }
  222. func (dir *Dir) removeFolder(ctx context.Context, req *fuse.RemoveRequest) error {
  223. dir.wfs.listDirectoryEntriesCache.Delete(path.Join(dir.Path, req.Name))
  224. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  225. request := &filer_pb.DeleteEntryRequest{
  226. Directory: dir.Path,
  227. Name: req.Name,
  228. IsDeleteData: true,
  229. }
  230. glog.V(3).Infof("remove directory entry: %v", request)
  231. _, err := client.DeleteEntry(ctx, request)
  232. if err != nil {
  233. glog.V(3).Infof("remove %s/%s: %v", dir.Path, req.Name, err)
  234. return fuse.ENOENT
  235. }
  236. return nil
  237. })
  238. }
  239. func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  240. if err := dir.maybeLoadEntry(ctx); err != nil {
  241. return err
  242. }
  243. glog.V(3).Infof("%v dir setattr %+v, fh=%d", dir.Path, req, req.Handle)
  244. if req.Valid.Mode() {
  245. dir.entry.Attributes.FileMode = uint32(req.Mode)
  246. }
  247. if req.Valid.Uid() {
  248. dir.entry.Attributes.Uid = req.Uid
  249. }
  250. if req.Valid.Gid() {
  251. dir.entry.Attributes.Gid = req.Gid
  252. }
  253. if req.Valid.Mtime() {
  254. dir.entry.Attributes.Mtime = req.Mtime.Unix()
  255. }
  256. dir.wfs.listDirectoryEntriesCache.Delete(dir.Path)
  257. return dir.saveEntry(ctx)
  258. }
  259. func (dir *Dir) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  260. glog.V(4).Infof("dir Setxattr %s: %s", dir.Path, req.Name)
  261. if err := dir.maybeLoadEntry(ctx); err != nil {
  262. return err
  263. }
  264. if err := setxattr(dir.entry, req); err != nil {
  265. return err
  266. }
  267. return dir.saveEntry(ctx)
  268. }
  269. func (dir *Dir) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  270. glog.V(4).Infof("dir Removexattr %s: %s", dir.Path, req.Name)
  271. if err := dir.maybeLoadEntry(ctx); err != nil {
  272. return err
  273. }
  274. if err := removexattr(dir.entry, req); err != nil {
  275. return err
  276. }
  277. return dir.saveEntry(ctx)
  278. }
  279. func (dir *Dir) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  280. glog.V(4).Infof("dir Listxattr %s", dir.Path)
  281. if err := dir.maybeLoadEntry(ctx); err != nil {
  282. return err
  283. }
  284. if err := listxattr(dir.entry, req, resp); err != nil {
  285. return err
  286. }
  287. return dir.saveEntry(ctx)
  288. }
  289. func (dir *Dir) maybeLoadEntry(ctx context.Context) error {
  290. if dir.entry == nil {
  291. parentDirPath, name := filer2.FullPath(dir.Path).DirAndName()
  292. entry, err := dir.wfs.maybeLoadEntry(ctx, parentDirPath, name)
  293. dir.entry = entry
  294. if err != nil {
  295. return err
  296. }
  297. }
  298. return nil
  299. }
  300. func (dir *Dir) saveEntry(ctx context.Context) error {
  301. parentDir, name := filer2.FullPath(dir.Path).DirAndName()
  302. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  303. request := &filer_pb.UpdateEntryRequest{
  304. Directory: parentDir,
  305. Entry: dir.entry,
  306. }
  307. glog.V(1).Infof("save dir entry: %v", request)
  308. _, err := client.UpdateEntry(ctx, request)
  309. if err != nil {
  310. glog.V(0).Infof("UpdateEntry dir %s/%s: %v", parentDir, name, err)
  311. return fuse.EIO
  312. }
  313. return nil
  314. })
  315. }