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.

466 lines
11 KiB

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