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.

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