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.

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