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.

379 lines
9.1 KiB

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