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.

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