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.

391 lines
9.4 KiB

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