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.

374 lines
8.9 KiB

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