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.

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