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.

394 lines
9.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 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. attributes *filer_pb.FuseAttributes
  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. func (dir *Dir) Attr(ctx context.Context, attr *fuse.Attr) error {
  27. // https://github.com/bazil/fuse/issues/196
  28. attr.Valid = time.Second
  29. if dir.Path == dir.wfs.option.FilerMountRootPath {
  30. dir.setRootDirAttributes(attr)
  31. return nil
  32. }
  33. item := dir.wfs.listDirectoryEntriesCache.Get(dir.Path)
  34. if item != nil && !item.Expired() {
  35. entry := item.Value().(*filer_pb.Entry)
  36. attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  37. attr.Ctime = time.Unix(entry.Attributes.Crtime, 0)
  38. attr.Mode = os.FileMode(entry.Attributes.FileMode)
  39. attr.Gid = entry.Attributes.Gid
  40. attr.Uid = entry.Attributes.Uid
  41. return nil
  42. }
  43. entry, err := filer2.GetEntry(ctx, dir.wfs, dir.Path)
  44. if err != nil {
  45. glog.V(2).Infof("read dir %s attr: %v, error: %v", dir.Path, dir.attributes, err)
  46. return err
  47. }
  48. if entry == nil {
  49. return fuse.ENOENT
  50. }
  51. dir.attributes = entry.Attributes
  52. glog.V(2).Infof("dir %s: %v perm: %v", dir.Path, dir.attributes, os.FileMode(dir.attributes.FileMode))
  53. attr.Mode = os.FileMode(dir.attributes.FileMode) | os.ModeDir
  54. attr.Mtime = time.Unix(dir.attributes.Mtime, 0)
  55. attr.Ctime = time.Unix(dir.attributes.Crtime, 0)
  56. attr.Gid = dir.attributes.Gid
  57. attr.Uid = dir.attributes.Uid
  58. return nil
  59. }
  60. func (dir *Dir) setRootDirAttributes(attr *fuse.Attr) {
  61. attr.Uid = dir.wfs.option.MountUid
  62. attr.Gid = dir.wfs.option.MountGid
  63. attr.Mode = dir.wfs.option.MountMode
  64. attr.Crtime = dir.wfs.option.MountCtime
  65. attr.Ctime = dir.wfs.option.MountCtime
  66. attr.Mtime = dir.wfs.option.MountMtime
  67. attr.Atime = dir.wfs.option.MountMtime
  68. }
  69. func (dir *Dir) newFile(name string, entry *filer_pb.Entry) *File {
  70. return &File{
  71. Name: name,
  72. dir: dir,
  73. wfs: dir.wfs,
  74. entry: entry,
  75. entryViewCache: nil,
  76. }
  77. }
  78. func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest,
  79. resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
  80. request := &filer_pb.CreateEntryRequest{
  81. Directory: dir.Path,
  82. Entry: &filer_pb.Entry{
  83. Name: req.Name,
  84. IsDirectory: req.Mode&os.ModeDir > 0,
  85. Attributes: &filer_pb.FuseAttributes{
  86. Mtime: time.Now().Unix(),
  87. Crtime: time.Now().Unix(),
  88. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  89. Uid: req.Uid,
  90. Gid: req.Gid,
  91. Collection: dir.wfs.option.Collection,
  92. Replication: dir.wfs.option.Replication,
  93. TtlSec: dir.wfs.option.TtlSec,
  94. },
  95. },
  96. }
  97. glog.V(1).Infof("create: %v", request)
  98. if request.Entry.IsDirectory {
  99. if err := dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  100. if _, err := client.CreateEntry(ctx, request); err != nil {
  101. glog.V(0).Infof("create %s/%s: %v", dir.Path, req.Name, err)
  102. return fuse.EIO
  103. }
  104. return nil
  105. }); err != nil {
  106. return nil, nil, err
  107. }
  108. }
  109. file := dir.newFile(req.Name, request.Entry)
  110. if !request.Entry.IsDirectory {
  111. file.isOpen = true
  112. }
  113. fh := dir.wfs.AcquireHandle(file, req.Uid, req.Gid)
  114. fh.dirtyMetadata = true
  115. return file, fh, nil
  116. }
  117. func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
  118. err := dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  119. request := &filer_pb.CreateEntryRequest{
  120. Directory: dir.Path,
  121. Entry: &filer_pb.Entry{
  122. Name: req.Name,
  123. IsDirectory: true,
  124. Attributes: &filer_pb.FuseAttributes{
  125. Mtime: time.Now().Unix(),
  126. Crtime: time.Now().Unix(),
  127. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  128. Uid: req.Uid,
  129. Gid: req.Gid,
  130. },
  131. },
  132. }
  133. glog.V(1).Infof("mkdir: %v", request)
  134. if _, err := client.CreateEntry(ctx, request); err != nil {
  135. glog.V(0).Infof("mkdir %s/%s: %v", dir.Path, req.Name, err)
  136. return fuse.EIO
  137. }
  138. return nil
  139. })
  140. if err == nil {
  141. node := &Dir{Path: path.Join(dir.Path, req.Name), wfs: dir.wfs}
  142. return node, nil
  143. }
  144. return nil, err
  145. }
  146. func (dir *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (node fs.Node, err error) {
  147. var entry *filer_pb.Entry
  148. fullFilePath := path.Join(dir.Path, req.Name)
  149. item := dir.wfs.listDirectoryEntriesCache.Get(fullFilePath)
  150. if item != nil && !item.Expired() {
  151. entry = item.Value().(*filer_pb.Entry)
  152. }
  153. if entry == nil {
  154. entry, err = filer2.GetEntry(ctx, dir.wfs, fullFilePath)
  155. if err != nil {
  156. return nil, err
  157. }
  158. }
  159. if entry != nil {
  160. if entry.IsDirectory {
  161. node = &Dir{Path: path.Join(dir.Path, req.Name), wfs: dir.wfs, attributes: entry.Attributes}
  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. err = dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  177. paginationLimit := 1024
  178. remaining := dir.wfs.option.DirListingLimit
  179. lastEntryName := ""
  180. for remaining >= 0 {
  181. request := &filer_pb.ListEntriesRequest{
  182. Directory: dir.Path,
  183. StartFromFileName: lastEntryName,
  184. Limit: uint32(paginationLimit),
  185. }
  186. glog.V(4).Infof("read directory: %v", request)
  187. resp, err := client.ListEntries(ctx, request)
  188. if err != nil {
  189. glog.V(0).Infof("list %s: %v", dir.Path, err)
  190. return fuse.EIO
  191. }
  192. cacheTtl := estimatedCacheTtl(len(resp.Entries))
  193. for _, entry := range resp.Entries {
  194. if entry.IsDirectory {
  195. dirent := fuse.Dirent{Name: entry.Name, Type: fuse.DT_Dir}
  196. ret = append(ret, dirent)
  197. } else {
  198. dirent := fuse.Dirent{Name: entry.Name, Type: fuse.DT_File}
  199. ret = append(ret, dirent)
  200. }
  201. dir.wfs.listDirectoryEntriesCache.Set(path.Join(dir.Path, entry.Name), entry, cacheTtl)
  202. lastEntryName = entry.Name
  203. }
  204. remaining -= len(resp.Entries)
  205. if len(resp.Entries) < paginationLimit {
  206. break
  207. }
  208. }
  209. return nil
  210. })
  211. return ret, err
  212. }
  213. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  214. if !req.Dir {
  215. return dir.removeOneFile(ctx, req)
  216. }
  217. return dir.removeFolder(ctx, req)
  218. }
  219. func (dir *Dir) removeOneFile(ctx context.Context, req *fuse.RemoveRequest) error {
  220. entry, err := filer2.GetEntry(ctx, dir.wfs, path.Join(dir.Path, req.Name))
  221. if err != nil {
  222. return err
  223. }
  224. dir.wfs.deleteFileChunks(ctx, entry.Chunks)
  225. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  226. request := &filer_pb.DeleteEntryRequest{
  227. Directory: dir.Path,
  228. Name: req.Name,
  229. IsDeleteData: false,
  230. }
  231. glog.V(3).Infof("remove file: %v", request)
  232. _, err := client.DeleteEntry(ctx, request)
  233. if err != nil {
  234. glog.V(3).Infof("remove file %s/%s: %v", dir.Path, req.Name, err)
  235. return fuse.ENOENT
  236. }
  237. dir.wfs.listDirectoryEntriesCache.Delete(path.Join(dir.Path, req.Name))
  238. return nil
  239. })
  240. }
  241. func (dir *Dir) removeFolder(ctx context.Context, req *fuse.RemoveRequest) error {
  242. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  243. request := &filer_pb.DeleteEntryRequest{
  244. Directory: dir.Path,
  245. Name: req.Name,
  246. IsDeleteData: true,
  247. }
  248. glog.V(3).Infof("remove directory entry: %v", request)
  249. _, err := client.DeleteEntry(ctx, request)
  250. if err != nil {
  251. glog.V(3).Infof("remove %s/%s: %v", dir.Path, req.Name, err)
  252. return fuse.ENOENT
  253. }
  254. dir.wfs.listDirectoryEntriesCache.Delete(path.Join(dir.Path, req.Name))
  255. return nil
  256. })
  257. }
  258. func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  259. if dir.attributes == nil {
  260. return nil
  261. }
  262. glog.V(3).Infof("%v dir setattr %+v, fh=%d", dir.Path, req, req.Handle)
  263. if req.Valid.Mode() {
  264. dir.attributes.FileMode = uint32(req.Mode)
  265. }
  266. if req.Valid.Uid() {
  267. dir.attributes.Uid = req.Uid
  268. }
  269. if req.Valid.Gid() {
  270. dir.attributes.Gid = req.Gid
  271. }
  272. if req.Valid.Mtime() {
  273. dir.attributes.Mtime = req.Mtime.Unix()
  274. }
  275. parentDir, name := filer2.FullPath(dir.Path).DirAndName()
  276. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  277. request := &filer_pb.UpdateEntryRequest{
  278. Directory: parentDir,
  279. Entry: &filer_pb.Entry{
  280. Name: name,
  281. Attributes: dir.attributes,
  282. },
  283. }
  284. glog.V(1).Infof("set attr directory entry: %v", request)
  285. _, err := client.UpdateEntry(ctx, request)
  286. if err != nil {
  287. glog.V(0).Infof("UpdateEntry %s: %v", dir.Path, err)
  288. return fuse.EIO
  289. }
  290. dir.wfs.listDirectoryEntriesCache.Delete(dir.Path)
  291. return nil
  292. })
  293. }
  294. func estimatedCacheTtl(numEntries int) time.Duration {
  295. if numEntries < 100 {
  296. // 30 ms per entry
  297. return 3 * time.Second
  298. }
  299. if numEntries < 1000 {
  300. // 10 ms per entry
  301. return 10 * time.Second
  302. }
  303. if numEntries < 10000 {
  304. // 10 ms per entry
  305. return 100 * time.Second
  306. }
  307. // 2 ms per entry
  308. return time.Duration(numEntries*2) * time.Millisecond
  309. }