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.

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