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.

447 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
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. newEntry := &filer_pb.Entry{
  132. Name: req.Name,
  133. IsDirectory: true,
  134. Attributes: &filer_pb.FuseAttributes{
  135. Mtime: time.Now().Unix(),
  136. Crtime: time.Now().Unix(),
  137. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  138. Uid: req.Uid,
  139. Gid: req.Gid,
  140. },
  141. }
  142. err := dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  143. request := &filer_pb.CreateEntryRequest{
  144. Directory: dir.Path,
  145. Entry: newEntry,
  146. }
  147. glog.V(1).Infof("mkdir: %v", request)
  148. if _, err := client.CreateEntry(ctx, request); err != nil {
  149. glog.V(0).Infof("mkdir %s/%s: %v", dir.Path, req.Name, err)
  150. return err
  151. }
  152. return nil
  153. })
  154. if err == nil {
  155. node := dir.newDirectory(filer2.NewFullPath(dir.Path, req.Name), newEntry)
  156. return node, nil
  157. }
  158. return nil, fuse.EIO
  159. }
  160. func (dir *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (node fs.Node, err error) {
  161. glog.V(4).Infof("dir Lookup %s: %s", dir.Path, req.Name)
  162. fullFilePath := filer2.NewFullPath(dir.Path, req.Name)
  163. entry := dir.wfs.cacheGet(fullFilePath)
  164. if entry == nil {
  165. // glog.V(3).Infof("dir Lookup cache miss %s", fullFilePath)
  166. entry, err = filer2.GetEntry(ctx, dir.wfs, fullFilePath)
  167. if err != nil {
  168. glog.V(1).Infof("dir GetEntry %s: %v", fullFilePath, err)
  169. return nil, fuse.ENOENT
  170. }
  171. dir.wfs.cacheSet(fullFilePath, entry, 5*time.Minute)
  172. } else {
  173. glog.V(4).Infof("dir Lookup cache hit %s", fullFilePath)
  174. }
  175. if entry != nil {
  176. if entry.IsDirectory {
  177. node = dir.newDirectory(fullFilePath, entry)
  178. } else {
  179. node = dir.newFile(req.Name, entry)
  180. }
  181. // resp.EntryValid = time.Second
  182. resp.Attr.Inode = fullFilePath.AsInode()
  183. resp.Attr.Valid = time.Second
  184. resp.Attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  185. resp.Attr.Ctime = time.Unix(entry.Attributes.Crtime, 0)
  186. resp.Attr.Mode = os.FileMode(entry.Attributes.FileMode)
  187. resp.Attr.Gid = entry.Attributes.Gid
  188. resp.Attr.Uid = entry.Attributes.Uid
  189. return node, nil
  190. }
  191. glog.V(1).Infof("not found dir GetEntry %s: %v", fullFilePath, err)
  192. return nil, fuse.ENOENT
  193. }
  194. func (dir *Dir) ReadDirAll(ctx context.Context) (ret []fuse.Dirent, err error) {
  195. glog.V(3).Infof("dir ReadDirAll %s", dir.Path)
  196. cacheTtl := 5 * time.Minute
  197. readErr := filer2.ReadDirAllEntries(ctx, dir.wfs, filer2.FullPath(dir.Path), "", func(entry *filer_pb.Entry, isLast bool) {
  198. fullpath := filer2.NewFullPath(dir.Path, entry.Name)
  199. inode := fullpath.AsInode()
  200. if entry.IsDirectory {
  201. dirent := fuse.Dirent{Inode: inode, Name: entry.Name, Type: fuse.DT_Dir}
  202. ret = append(ret, dirent)
  203. } else {
  204. dirent := fuse.Dirent{Inode: inode, Name: entry.Name, Type: fuse.DT_File}
  205. ret = append(ret, dirent)
  206. }
  207. dir.wfs.cacheSet(fullpath, entry, cacheTtl)
  208. })
  209. if readErr != nil {
  210. glog.V(0).Infof("list %s: %v", dir.Path, err)
  211. return ret, fuse.EIO
  212. }
  213. return ret, err
  214. }
  215. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  216. if !req.Dir {
  217. return dir.removeOneFile(ctx, req)
  218. }
  219. return dir.removeFolder(ctx, req)
  220. }
  221. func (dir *Dir) removeOneFile(ctx context.Context, req *fuse.RemoveRequest) error {
  222. filePath := filer2.NewFullPath(dir.Path, req.Name)
  223. entry, err := filer2.GetEntry(ctx, dir.wfs, filePath)
  224. if err != nil {
  225. return err
  226. }
  227. dir.wfs.deleteFileChunks(ctx, entry.Chunks)
  228. dir.wfs.cacheDelete(filePath)
  229. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  230. request := &filer_pb.DeleteEntryRequest{
  231. Directory: dir.Path,
  232. Name: req.Name,
  233. IsDeleteData: false,
  234. }
  235. glog.V(3).Infof("remove file: %v", request)
  236. _, err := client.DeleteEntry(ctx, request)
  237. if err != nil {
  238. glog.V(3).Infof("not found remove file %s/%s: %v", dir.Path, req.Name, err)
  239. return fuse.ENOENT
  240. }
  241. return nil
  242. })
  243. }
  244. func (dir *Dir) removeFolder(ctx context.Context, req *fuse.RemoveRequest) error {
  245. dir.wfs.cacheDelete(filer2.NewFullPath(dir.Path, req.Name))
  246. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  247. request := &filer_pb.DeleteEntryRequest{
  248. Directory: dir.Path,
  249. Name: req.Name,
  250. IsDeleteData: true,
  251. }
  252. glog.V(3).Infof("remove directory entry: %v", request)
  253. _, err := client.DeleteEntry(ctx, request)
  254. if err != nil {
  255. glog.V(3).Infof("not found remove %s/%s: %v", dir.Path, req.Name, err)
  256. return fuse.ENOENT
  257. }
  258. return nil
  259. })
  260. }
  261. func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  262. glog.V(3).Infof("%v dir setattr %+v", dir.Path, req)
  263. if err := dir.maybeLoadEntry(ctx); err != nil {
  264. return err
  265. }
  266. if req.Valid.Mode() {
  267. dir.entry.Attributes.FileMode = uint32(req.Mode)
  268. }
  269. if req.Valid.Uid() {
  270. dir.entry.Attributes.Uid = req.Uid
  271. }
  272. if req.Valid.Gid() {
  273. dir.entry.Attributes.Gid = req.Gid
  274. }
  275. if req.Valid.Mtime() {
  276. dir.entry.Attributes.Mtime = req.Mtime.Unix()
  277. }
  278. dir.wfs.cacheDelete(filer2.FullPath(dir.Path))
  279. return dir.saveEntry(ctx)
  280. }
  281. func (dir *Dir) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  282. glog.V(4).Infof("dir Setxattr %s: %s", dir.Path, req.Name)
  283. if err := dir.maybeLoadEntry(ctx); err != nil {
  284. return err
  285. }
  286. if err := setxattr(dir.entry, req); err != nil {
  287. return err
  288. }
  289. dir.wfs.cacheDelete(filer2.FullPath(dir.Path))
  290. return dir.saveEntry(ctx)
  291. }
  292. func (dir *Dir) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  293. glog.V(4).Infof("dir Removexattr %s: %s", dir.Path, req.Name)
  294. if err := dir.maybeLoadEntry(ctx); err != nil {
  295. return err
  296. }
  297. if err := removexattr(dir.entry, req); err != nil {
  298. return err
  299. }
  300. dir.wfs.cacheDelete(filer2.FullPath(dir.Path))
  301. return dir.saveEntry(ctx)
  302. }
  303. func (dir *Dir) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  304. glog.V(4).Infof("dir Listxattr %s", dir.Path)
  305. if err := dir.maybeLoadEntry(ctx); err != nil {
  306. return err
  307. }
  308. if err := listxattr(dir.entry, req, resp); err != nil {
  309. return err
  310. }
  311. return nil
  312. }
  313. func (dir *Dir) Forget() {
  314. glog.V(3).Infof("Forget dir %s", dir.Path)
  315. dir.wfs.forgetNode(filer2.FullPath(dir.Path))
  316. }
  317. func (dir *Dir) maybeLoadEntry(ctx context.Context) error {
  318. if dir.entry == nil {
  319. parentDirPath, name := filer2.FullPath(dir.Path).DirAndName()
  320. entry, err := dir.wfs.maybeLoadEntry(ctx, parentDirPath, name)
  321. if err != nil {
  322. return err
  323. }
  324. dir.entry = entry
  325. }
  326. return nil
  327. }
  328. func (dir *Dir) saveEntry(ctx context.Context) error {
  329. parentDir, name := filer2.FullPath(dir.Path).DirAndName()
  330. return dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  331. request := &filer_pb.UpdateEntryRequest{
  332. Directory: parentDir,
  333. Entry: dir.entry,
  334. }
  335. glog.V(1).Infof("save dir entry: %v", request)
  336. _, err := client.UpdateEntry(ctx, request)
  337. if err != nil {
  338. glog.V(0).Infof("UpdateEntry dir %s/%s: %v", parentDir, name, err)
  339. return fuse.EIO
  340. }
  341. return nil
  342. })
  343. }