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.

445 lines
11 KiB

7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "os"
  5. "strings"
  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. entry *filer_pb.Entry
  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. var _ = fs.NodeGetxattrer(&Dir{})
  27. var _ = fs.NodeSetxattrer(&Dir{})
  28. var _ = fs.NodeRemovexattrer(&Dir{})
  29. var _ = fs.NodeListxattrer(&Dir{})
  30. var _ = fs.NodeForgetter(&Dir{})
  31. func (dir *Dir) Attr(ctx context.Context, attr *fuse.Attr) error {
  32. glog.V(3).Infof("dir Attr %s, existing attr: %+v", dir.Path, attr)
  33. // https://github.com/bazil/fuse/issues/196
  34. attr.Valid = time.Second
  35. if dir.Path == dir.wfs.option.FilerMountRootPath {
  36. dir.setRootDirAttributes(attr)
  37. glog.V(3).Infof("root dir Attr %s, attr: %+v", dir.Path, attr)
  38. return nil
  39. }
  40. if err := dir.maybeLoadEntry(ctx); err != nil {
  41. glog.V(3).Infof("dir Attr %s,err: %+v", dir.Path, err)
  42. return err
  43. }
  44. attr.Inode = filer2.FullPath(dir.Path).AsInode()
  45. attr.Mode = os.FileMode(dir.entry.Attributes.FileMode) | os.ModeDir
  46. attr.Mtime = time.Unix(dir.entry.Attributes.Mtime, 0)
  47. attr.Ctime = time.Unix(dir.entry.Attributes.Crtime, 0)
  48. attr.Gid = dir.entry.Attributes.Gid
  49. attr.Uid = dir.entry.Attributes.Uid
  50. glog.V(3).Infof("dir Attr %s, attr: %+v", dir.Path, attr)
  51. return nil
  52. }
  53. func (dir *Dir) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
  54. glog.V(4).Infof("dir Getxattr %s", dir.Path)
  55. if err := dir.maybeLoadEntry(ctx); err != nil {
  56. return err
  57. }
  58. return getxattr(dir.entry, req, resp)
  59. }
  60. func (dir *Dir) setRootDirAttributes(attr *fuse.Attr) {
  61. attr.Inode = 1 // filer2.FullPath(dir.Path).AsInode()
  62. attr.Valid = time.Hour
  63. attr.Uid = dir.wfs.option.MountUid
  64. attr.Gid = dir.wfs.option.MountGid
  65. attr.Mode = dir.wfs.option.MountMode
  66. attr.Crtime = dir.wfs.option.MountCtime
  67. attr.Ctime = dir.wfs.option.MountCtime
  68. attr.Mtime = dir.wfs.option.MountMtime
  69. attr.Atime = dir.wfs.option.MountMtime
  70. attr.BlockSize = 1024 * 1024
  71. }
  72. func (dir *Dir) newFile(name string, entry *filer_pb.Entry) fs.Node {
  73. return dir.wfs.getNode(filer2.NewFullPath(dir.Path, name), func() fs.Node {
  74. return &File{
  75. Name: name,
  76. dir: dir,
  77. wfs: dir.wfs,
  78. entry: entry,
  79. entryViewCache: nil,
  80. }
  81. })
  82. }
  83. func (dir *Dir) newDirectory(fullpath filer2.FullPath, entry *filer_pb.Entry) fs.Node {
  84. return dir.wfs.getNode(fullpath, func() fs.Node {
  85. return &Dir{Path: string(fullpath), wfs: dir.wfs, entry: entry}
  86. })
  87. }
  88. func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest,
  89. resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
  90. request := &filer_pb.CreateEntryRequest{
  91. Directory: dir.Path,
  92. Entry: &filer_pb.Entry{
  93. Name: req.Name,
  94. IsDirectory: req.Mode&os.ModeDir > 0,
  95. Attributes: &filer_pb.FuseAttributes{
  96. Mtime: time.Now().Unix(),
  97. Crtime: time.Now().Unix(),
  98. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  99. Uid: req.Uid,
  100. Gid: req.Gid,
  101. Collection: dir.wfs.option.Collection,
  102. Replication: dir.wfs.option.Replication,
  103. TtlSec: dir.wfs.option.TtlSec,
  104. },
  105. },
  106. OExcl: req.Flags&fuse.OpenExclusive != 0,
  107. }
  108. glog.V(1).Infof("create: %v", req.String())
  109. if err := dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  110. if _, err := client.CreateEntry(ctx, request); err != nil {
  111. glog.V(0).Infof("create %s/%s: %v", dir.Path, req.Name, err)
  112. if strings.Contains(err.Error(), "EEXIST") {
  113. return fuse.EEXIST
  114. }
  115. return fuse.EIO
  116. }
  117. return nil
  118. }); err != nil {
  119. return nil, nil, err
  120. }
  121. node := dir.newFile(req.Name, request.Entry)
  122. if request.Entry.IsDirectory {
  123. return node, nil, nil
  124. }
  125. file := node.(*File)
  126. file.isOpen++
  127. fh := dir.wfs.AcquireHandle(file, req.Uid, req.Gid)
  128. return file, fh, nil
  129. }
  130. func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
  131. err := dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  132. request := &filer_pb.CreateEntryRequest{
  133. Directory: dir.Path,
  134. Entry: &filer_pb.Entry{
  135. Name: req.Name,
  136. IsDirectory: true,
  137. Attributes: &filer_pb.FuseAttributes{
  138. Mtime: time.Now().Unix(),
  139. Crtime: time.Now().Unix(),
  140. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  141. Uid: req.Uid,
  142. Gid: req.Gid,
  143. },
  144. },
  145. }
  146. glog.V(1).Infof("mkdir: %v", request)
  147. if _, err := client.CreateEntry(ctx, request); err != nil {
  148. glog.V(0).Infof("mkdir %s/%s: %v", dir.Path, req.Name, err)
  149. return err
  150. }
  151. return nil
  152. })
  153. if err == nil {
  154. node := dir.newDirectory(filer2.NewFullPath(dir.Path, req.Name), nil)
  155. return node, nil
  156. }
  157. return nil, fuse.EIO
  158. }
  159. func (dir *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (node fs.Node, err error) {
  160. glog.V(4).Infof("dir Lookup %s: %s", dir.Path, req.Name)
  161. fullFilePath := filer2.NewFullPath(dir.Path, req.Name)
  162. entry := dir.wfs.cacheGet(fullFilePath)
  163. if entry == nil {
  164. glog.V(3).Infof("dir Lookup cache miss %s", fullFilePath)
  165. entry, err = filer2.GetEntry(ctx, dir.wfs, fullFilePath)
  166. if err != nil {
  167. glog.V(1).Infof("dir GetEntry %s: %v", fullFilePath, err)
  168. return nil, fuse.ENOENT
  169. }
  170. dir.wfs.cacheSet(fullFilePath, entry, 5*time.Minute)
  171. } else {
  172. glog.V(4).Infof("dir Lookup cache hit %s", fullFilePath)
  173. }
  174. if entry != nil {
  175. if entry.IsDirectory {
  176. node = dir.newDirectory(fullFilePath, entry)
  177. } else {
  178. node = dir.newFile(req.Name, entry)
  179. }
  180. // resp.EntryValid = time.Second
  181. resp.Attr.Inode = fullFilePath.AsInode()
  182. resp.Attr.Valid = time.Second
  183. resp.Attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  184. resp.Attr.Ctime = time.Unix(entry.Attributes.Crtime, 0)
  185. resp.Attr.Mode = os.FileMode(entry.Attributes.FileMode)
  186. resp.Attr.Gid = entry.Attributes.Gid
  187. resp.Attr.Uid = entry.Attributes.Uid
  188. return node, nil
  189. }
  190. glog.V(1).Infof("not found dir GetEntry %s: %v", fullFilePath, err)
  191. return nil, fuse.ENOENT
  192. }
  193. func (dir *Dir) ReadDirAll(ctx context.Context) (ret []fuse.Dirent, err error) {
  194. glog.V(3).Infof("dir ReadDirAll %s", dir.Path)
  195. cacheTtl := 5 * time.Minute
  196. readErr := filer2.ReadDirAllEntries(ctx, dir.wfs, filer2.FullPath(dir.Path), "", func(entry *filer_pb.Entry, isLast bool) {
  197. fullpath := filer2.NewFullPath(dir.Path, entry.Name)
  198. inode := fullpath.AsInode()
  199. if entry.IsDirectory {
  200. dirent := fuse.Dirent{Inode: inode, Name: entry.Name, Type: fuse.DT_Dir}
  201. ret = append(ret, dirent)
  202. } else {
  203. dirent := fuse.Dirent{Inode: inode, Name: entry.Name, Type: fuse.DT_File}
  204. ret = append(ret, dirent)
  205. }
  206. dir.wfs.cacheSet(fullpath, entry, cacheTtl)
  207. })
  208. if readErr != nil {
  209. glog.V(0).Infof("list %s: %v", dir.Path, err)
  210. return ret, fuse.EIO
  211. }
  212. return ret, err
  213. }
  214. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  215. if !req.Dir {
  216. return dir.removeOneFile(ctx, req)
  217. }
  218. return dir.removeFolder(ctx, req)
  219. }
  220. func (dir *Dir) removeOneFile(ctx context.Context, req *fuse.RemoveRequest) error {
  221. filePath := filer2.NewFullPath(dir.Path, req.Name)
  222. entry, err := filer2.GetEntry(ctx, dir.wfs, filePath)
  223. if err != nil {
  224. return err
  225. }
  226. dir.wfs.deleteFileChunks(ctx, entry.Chunks)
  227. dir.wfs.cacheDelete(filePath)
  228. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  229. request := &filer_pb.DeleteEntryRequest{
  230. Directory: dir.Path,
  231. Name: req.Name,
  232. IsDeleteData: false,
  233. }
  234. glog.V(3).Infof("remove file: %v", request)
  235. _, err := client.DeleteEntry(ctx, request)
  236. if err != nil {
  237. glog.V(3).Infof("not found remove file %s/%s: %v", dir.Path, req.Name, err)
  238. return fuse.ENOENT
  239. }
  240. return nil
  241. })
  242. }
  243. func (dir *Dir) removeFolder(ctx context.Context, req *fuse.RemoveRequest) error {
  244. dir.wfs.cacheDelete(filer2.NewFullPath(dir.Path, req.Name))
  245. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  246. request := &filer_pb.DeleteEntryRequest{
  247. Directory: dir.Path,
  248. Name: req.Name,
  249. IsDeleteData: true,
  250. }
  251. glog.V(3).Infof("remove directory entry: %v", request)
  252. _, err := client.DeleteEntry(ctx, request)
  253. if err != nil {
  254. glog.V(3).Infof("not found remove %s/%s: %v", dir.Path, req.Name, err)
  255. return fuse.ENOENT
  256. }
  257. return nil
  258. })
  259. }
  260. func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  261. glog.V(3).Infof("%v dir setattr %+v", dir.Path, req)
  262. if err := dir.maybeLoadEntry(ctx); err != nil {
  263. return err
  264. }
  265. if req.Valid.Mode() {
  266. dir.entry.Attributes.FileMode = uint32(req.Mode)
  267. }
  268. if req.Valid.Uid() {
  269. dir.entry.Attributes.Uid = req.Uid
  270. }
  271. if req.Valid.Gid() {
  272. dir.entry.Attributes.Gid = req.Gid
  273. }
  274. if req.Valid.Mtime() {
  275. dir.entry.Attributes.Mtime = req.Mtime.Unix()
  276. }
  277. dir.wfs.cacheDelete(filer2.FullPath(dir.Path))
  278. return dir.saveEntry(ctx)
  279. }
  280. func (dir *Dir) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  281. glog.V(4).Infof("dir Setxattr %s: %s", dir.Path, req.Name)
  282. if err := dir.maybeLoadEntry(ctx); err != nil {
  283. return err
  284. }
  285. if err := setxattr(dir.entry, req); err != nil {
  286. return err
  287. }
  288. dir.wfs.cacheDelete(filer2.FullPath(dir.Path))
  289. return dir.saveEntry(ctx)
  290. }
  291. func (dir *Dir) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  292. glog.V(4).Infof("dir Removexattr %s: %s", dir.Path, req.Name)
  293. if err := dir.maybeLoadEntry(ctx); err != nil {
  294. return err
  295. }
  296. if err := removexattr(dir.entry, req); err != nil {
  297. return err
  298. }
  299. dir.wfs.cacheDelete(filer2.FullPath(dir.Path))
  300. return dir.saveEntry(ctx)
  301. }
  302. func (dir *Dir) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  303. glog.V(4).Infof("dir Listxattr %s", dir.Path)
  304. if err := dir.maybeLoadEntry(ctx); err != nil {
  305. return err
  306. }
  307. if err := listxattr(dir.entry, req, resp); err != nil {
  308. return err
  309. }
  310. return nil
  311. }
  312. func (dir *Dir) Forget() {
  313. glog.V(3).Infof("Forget dir %s", dir.Path)
  314. dir.wfs.forgetNode(filer2.FullPath(dir.Path))
  315. }
  316. func (dir *Dir) maybeLoadEntry(ctx context.Context) error {
  317. if dir.entry == nil {
  318. parentDirPath, name := filer2.FullPath(dir.Path).DirAndName()
  319. entry, err := dir.wfs.maybeLoadEntry(ctx, parentDirPath, name)
  320. if err != nil {
  321. return err
  322. }
  323. dir.entry = entry
  324. }
  325. return nil
  326. }
  327. func (dir *Dir) saveEntry(ctx context.Context) error {
  328. parentDir, name := filer2.FullPath(dir.Path).DirAndName()
  329. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  330. request := &filer_pb.UpdateEntryRequest{
  331. Directory: parentDir,
  332. Entry: dir.entry,
  333. }
  334. glog.V(1).Infof("save dir entry: %v", request)
  335. _, err := client.UpdateEntry(ctx, request)
  336. if err != nil {
  337. glog.V(0).Infof("UpdateEntry dir %s/%s: %v", parentDir, name, err)
  338. return fuse.EIO
  339. }
  340. return nil
  341. })
  342. }