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.

361 lines
8.7 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. request := &filer_pb.ListEntriesRequest{
  192. Directory: dir.Path,
  193. Limit: uint32(dir.wfs.option.DirListingLimit),
  194. }
  195. glog.V(4).Infof("read directory: %v", request)
  196. resp, err := client.ListEntries(ctx, request)
  197. if err != nil {
  198. glog.V(0).Infof("list %s: %v", dir.Path, err)
  199. return fuse.EIO
  200. }
  201. cacheTtl := estimatedCacheTtl(len(resp.Entries))
  202. for _, entry := range resp.Entries {
  203. if entry.IsDirectory {
  204. dirent := fuse.Dirent{Name: entry.Name, Type: fuse.DT_Dir}
  205. ret = append(ret, dirent)
  206. } else {
  207. dirent := fuse.Dirent{Name: entry.Name, Type: fuse.DT_File}
  208. ret = append(ret, dirent)
  209. }
  210. dir.wfs.listDirectoryEntriesCache.Set(path.Join(dir.Path, entry.Name), entry, cacheTtl)
  211. }
  212. return nil
  213. })
  214. return ret, err
  215. }
  216. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  217. return dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  218. request := &filer_pb.DeleteEntryRequest{
  219. Directory: dir.Path,
  220. Name: req.Name,
  221. IsDirectory: req.Dir,
  222. IsDeleteData: true,
  223. }
  224. glog.V(1).Infof("remove directory entry: %v", request)
  225. _, err := client.DeleteEntry(ctx, request)
  226. if err != nil {
  227. glog.V(0).Infof("remove %s/%s: %v", dir.Path, req.Name, err)
  228. return fuse.EIO
  229. }
  230. dir.wfs.listDirectoryEntriesCache.Delete(path.Join(dir.Path, req.Name))
  231. return nil
  232. })
  233. }
  234. func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  235. glog.V(3).Infof("%v dir setattr %+v, fh=%d", dir.Path, req, req.Handle)
  236. if req.Valid.Mode() {
  237. dir.attributes.FileMode = uint32(req.Mode)
  238. }
  239. if req.Valid.Uid() {
  240. dir.attributes.Uid = req.Uid
  241. }
  242. if req.Valid.Gid() {
  243. dir.attributes.Gid = req.Gid
  244. }
  245. if req.Valid.Mtime() {
  246. dir.attributes.Mtime = req.Mtime.Unix()
  247. }
  248. parentDir, name := filer2.FullPath(dir.Path).DirAndName()
  249. return dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  250. request := &filer_pb.UpdateEntryRequest{
  251. Directory: parentDir,
  252. Entry: &filer_pb.Entry{
  253. Name: name,
  254. Attributes: dir.attributes,
  255. },
  256. }
  257. glog.V(1).Infof("set attr directory entry: %v", request)
  258. _, err := client.UpdateEntry(ctx, request)
  259. if err != nil {
  260. glog.V(0).Infof("UpdateEntry %s: %v", dir.Path, err)
  261. return fuse.EIO
  262. }
  263. dir.wfs.listDirectoryEntriesCache.Delete(dir.Path)
  264. return nil
  265. })
  266. }
  267. func estimatedCacheTtl(numEntries int) time.Duration {
  268. if numEntries < 100 {
  269. // 30 ms per entry
  270. return 3 * time.Second
  271. }
  272. if numEntries < 1000 {
  273. // 10 ms per entry
  274. return 10 * time.Second
  275. }
  276. if numEntries < 10000 {
  277. // 10 ms per entry
  278. return 100 * time.Second
  279. }
  280. if numEntries < 100000 {
  281. // 10 ms per entry
  282. return 1000 * time.Second
  283. }
  284. // 2 ms per entry
  285. return time.Duration(numEntries*2) * time.Millisecond
  286. }