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.

519 lines
14 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
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
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/filer"
  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(4).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. Signatures: []int32{dir.wfs.signature},
  123. }
  124. glog.V(1).Infof("create %s/%s: %v", dir.FullPath(), req.Name, req.Flags)
  125. if err := dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  126. dir.wfs.mapPbIdFromLocalToFiler(request.Entry)
  127. defer dir.wfs.mapPbIdFromFilerToLocal(request.Entry)
  128. if err := filer_pb.CreateEntry(client, request); err != nil {
  129. if strings.Contains(err.Error(), "EEXIST") {
  130. return fuse.EEXIST
  131. }
  132. glog.V(0).Infof("create %s/%s: %v", dir.FullPath(), req.Name, err)
  133. return fuse.EIO
  134. }
  135. dir.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  136. return nil
  137. }); err != nil {
  138. return nil, nil, err
  139. }
  140. var node fs.Node
  141. if request.Entry.IsDirectory {
  142. node = dir.newDirectory(util.NewFullPath(dir.FullPath(), req.Name), request.Entry)
  143. return node, nil, nil
  144. }
  145. node = dir.newFile(req.Name, request.Entry)
  146. file := node.(*File)
  147. fh := dir.wfs.AcquireHandle(file, req.Uid, req.Gid)
  148. return file, fh, nil
  149. }
  150. func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
  151. glog.V(4).Infof("mkdir %s: %s", dir.FullPath(), req.Name)
  152. newEntry := &filer_pb.Entry{
  153. Name: req.Name,
  154. IsDirectory: true,
  155. Attributes: &filer_pb.FuseAttributes{
  156. Mtime: time.Now().Unix(),
  157. Crtime: time.Now().Unix(),
  158. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  159. Uid: req.Uid,
  160. Gid: req.Gid,
  161. },
  162. }
  163. err := dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  164. dir.wfs.mapPbIdFromLocalToFiler(newEntry)
  165. defer dir.wfs.mapPbIdFromFilerToLocal(newEntry)
  166. request := &filer_pb.CreateEntryRequest{
  167. Directory: dir.FullPath(),
  168. Entry: newEntry,
  169. Signatures: []int32{dir.wfs.signature},
  170. }
  171. glog.V(1).Infof("mkdir: %v", request)
  172. if err := filer_pb.CreateEntry(client, request); err != nil {
  173. glog.V(0).Infof("mkdir %s/%s: %v", dir.FullPath(), req.Name, err)
  174. return err
  175. }
  176. dir.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  177. return nil
  178. })
  179. if err == nil {
  180. node := dir.newDirectory(util.NewFullPath(dir.FullPath(), req.Name), newEntry)
  181. return node, nil
  182. }
  183. glog.V(0).Infof("mkdir %s/%s: %v", dir.FullPath(), req.Name, err)
  184. return nil, fuse.EIO
  185. }
  186. func (dir *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (node fs.Node, err error) {
  187. glog.V(4).Infof("dir Lookup %s: %s by %s", dir.FullPath(), req.Name, req.Header.String())
  188. fullFilePath := util.NewFullPath(dir.FullPath(), req.Name)
  189. dirPath := util.FullPath(dir.FullPath())
  190. meta_cache.EnsureVisited(dir.wfs.metaCache, dir.wfs, util.FullPath(dirPath))
  191. cachedEntry, cacheErr := dir.wfs.metaCache.FindEntry(context.Background(), fullFilePath)
  192. if cacheErr == filer_pb.ErrNotFound {
  193. return nil, fuse.ENOENT
  194. }
  195. entry := cachedEntry.ToProtoEntry()
  196. if entry == nil {
  197. // glog.V(3).Infof("dir Lookup cache miss %s", fullFilePath)
  198. entry, err = filer_pb.GetEntry(dir.wfs, fullFilePath)
  199. if err != nil {
  200. glog.V(1).Infof("dir GetEntry %s: %v", fullFilePath, err)
  201. return nil, fuse.ENOENT
  202. }
  203. } else {
  204. glog.V(4).Infof("dir Lookup cache hit %s", fullFilePath)
  205. }
  206. if entry != nil {
  207. if entry.IsDirectory {
  208. node = dir.newDirectory(fullFilePath, entry)
  209. } else {
  210. node = dir.newFile(req.Name, entry)
  211. }
  212. // resp.EntryValid = time.Second
  213. resp.Attr.Inode = fullFilePath.AsInode()
  214. resp.Attr.Valid = time.Second
  215. resp.Attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  216. resp.Attr.Crtime = time.Unix(entry.Attributes.Crtime, 0)
  217. resp.Attr.Mode = os.FileMode(entry.Attributes.FileMode)
  218. resp.Attr.Gid = entry.Attributes.Gid
  219. resp.Attr.Uid = entry.Attributes.Uid
  220. if entry.HardLinkCounter > 0 {
  221. resp.Attr.Nlink = uint32(entry.HardLinkCounter)
  222. }
  223. return node, nil
  224. }
  225. glog.V(4).Infof("not found dir GetEntry %s: %v", fullFilePath, err)
  226. return nil, fuse.ENOENT
  227. }
  228. func (dir *Dir) ReadDirAll(ctx context.Context) (ret []fuse.Dirent, err error) {
  229. glog.V(4).Infof("dir ReadDirAll %s", dir.FullPath())
  230. processEachEntryFn := func(entry *filer_pb.Entry, isLast bool) error {
  231. fullpath := util.NewFullPath(dir.FullPath(), entry.Name)
  232. inode := fullpath.AsInode()
  233. if entry.IsDirectory {
  234. dirent := fuse.Dirent{Inode: inode, Name: entry.Name, Type: fuse.DT_Dir}
  235. ret = append(ret, dirent)
  236. } else {
  237. dirent := fuse.Dirent{Inode: inode, Name: entry.Name, Type: fuse.DT_File}
  238. ret = append(ret, dirent)
  239. }
  240. return nil
  241. }
  242. dirPath := util.FullPath(dir.FullPath())
  243. meta_cache.EnsureVisited(dir.wfs.metaCache, dir.wfs, dirPath)
  244. listedEntries, listErr := dir.wfs.metaCache.ListDirectoryEntries(context.Background(), util.FullPath(dir.FullPath()), "", false, int(math.MaxInt32))
  245. if listErr != nil {
  246. glog.Errorf("list meta cache: %v", listErr)
  247. return nil, fuse.EIO
  248. }
  249. for _, cachedEntry := range listedEntries {
  250. processEachEntryFn(cachedEntry.ToProtoEntry(), false)
  251. }
  252. return
  253. }
  254. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  255. if !req.Dir {
  256. return dir.removeOneFile(req)
  257. }
  258. return dir.removeFolder(req)
  259. }
  260. func (dir *Dir) removeOneFile(req *fuse.RemoveRequest) error {
  261. filePath := util.NewFullPath(dir.FullPath(), req.Name)
  262. entry, err := filer_pb.GetEntry(dir.wfs, filePath)
  263. if err != nil {
  264. return err
  265. }
  266. if entry == nil {
  267. return nil
  268. }
  269. // first, ensure the filer store can correctly delete
  270. glog.V(3).Infof("remove file: %v", req)
  271. isDeleteData := entry.HardLinkCounter <= 1
  272. err = filer_pb.Remove(dir.wfs, dir.FullPath(), req.Name, isDeleteData, false, false, false, []int32{dir.wfs.signature})
  273. if err != nil {
  274. glog.V(3).Infof("not found remove file %s/%s: %v", dir.FullPath(), req.Name, err)
  275. return fuse.ENOENT
  276. }
  277. // then, delete meta cache and fsNode cache
  278. dir.wfs.metaCache.DeleteEntry(context.Background(), filePath)
  279. dir.wfs.fsNodeCache.DeleteFsNode(filePath)
  280. // delete the chunks last
  281. if isDeleteData {
  282. dir.wfs.deleteFileChunks(entry.Chunks)
  283. }
  284. return nil
  285. }
  286. func (dir *Dir) removeFolder(req *fuse.RemoveRequest) error {
  287. glog.V(3).Infof("remove directory entry: %v", req)
  288. ignoreRecursiveErr := true // ignore recursion error since the OS should manage it
  289. err := filer_pb.Remove(dir.wfs, dir.FullPath(), req.Name, true, false, ignoreRecursiveErr, false, []int32{dir.wfs.signature})
  290. if err != nil {
  291. glog.V(0).Infof("remove %s/%s: %v", dir.FullPath(), req.Name, err)
  292. if strings.Contains(err.Error(), "non-empty") {
  293. return fuse.EEXIST
  294. }
  295. return fuse.ENOENT
  296. }
  297. t := util.NewFullPath(dir.FullPath(), req.Name)
  298. dir.wfs.metaCache.DeleteEntry(context.Background(), t)
  299. dir.wfs.fsNodeCache.DeleteFsNode(t)
  300. return nil
  301. }
  302. func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  303. glog.V(4).Infof("%v dir setattr %+v", dir.FullPath(), req)
  304. if err := dir.maybeLoadEntry(); err != nil {
  305. return err
  306. }
  307. if req.Valid.Mode() {
  308. dir.entry.Attributes.FileMode = uint32(req.Mode)
  309. }
  310. if req.Valid.Uid() {
  311. dir.entry.Attributes.Uid = req.Uid
  312. }
  313. if req.Valid.Gid() {
  314. dir.entry.Attributes.Gid = req.Gid
  315. }
  316. if req.Valid.Mtime() {
  317. dir.entry.Attributes.Mtime = req.Mtime.Unix()
  318. }
  319. return dir.saveEntry()
  320. }
  321. func (dir *Dir) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  322. glog.V(4).Infof("dir Setxattr %s: %s", dir.FullPath(), req.Name)
  323. if err := dir.maybeLoadEntry(); err != nil {
  324. return err
  325. }
  326. if err := setxattr(dir.entry, req); err != nil {
  327. return err
  328. }
  329. return dir.saveEntry()
  330. }
  331. func (dir *Dir) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  332. glog.V(4).Infof("dir Removexattr %s: %s", dir.FullPath(), req.Name)
  333. if err := dir.maybeLoadEntry(); err != nil {
  334. return err
  335. }
  336. if err := removexattr(dir.entry, req); err != nil {
  337. return err
  338. }
  339. return dir.saveEntry()
  340. }
  341. func (dir *Dir) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  342. glog.V(4).Infof("dir Listxattr %s", dir.FullPath())
  343. if err := dir.maybeLoadEntry(); err != nil {
  344. return err
  345. }
  346. if err := listxattr(dir.entry, req, resp); err != nil {
  347. return err
  348. }
  349. return nil
  350. }
  351. func (dir *Dir) Forget() {
  352. glog.V(4).Infof("Forget dir %s", dir.FullPath())
  353. dir.wfs.fsNodeCache.DeleteFsNode(util.FullPath(dir.FullPath()))
  354. }
  355. func (dir *Dir) maybeLoadEntry() error {
  356. if dir.entry == nil {
  357. parentDirPath, name := util.FullPath(dir.FullPath()).DirAndName()
  358. entry, err := dir.wfs.maybeLoadEntry(parentDirPath, name)
  359. if err != nil {
  360. return err
  361. }
  362. dir.entry = entry
  363. }
  364. return nil
  365. }
  366. func (dir *Dir) saveEntry() error {
  367. parentDir, name := util.FullPath(dir.FullPath()).DirAndName()
  368. return dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  369. dir.wfs.mapPbIdFromLocalToFiler(dir.entry)
  370. defer dir.wfs.mapPbIdFromFilerToLocal(dir.entry)
  371. request := &filer_pb.UpdateEntryRequest{
  372. Directory: parentDir,
  373. Entry: dir.entry,
  374. Signatures: []int32{dir.wfs.signature},
  375. }
  376. glog.V(1).Infof("save dir entry: %v", request)
  377. _, err := client.UpdateEntry(context.Background(), request)
  378. if err != nil {
  379. glog.Errorf("UpdateEntry dir %s/%s: %v", parentDir, name, err)
  380. return fuse.EIO
  381. }
  382. dir.wfs.metaCache.UpdateEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  383. return nil
  384. })
  385. }
  386. func (dir *Dir) FullPath() string {
  387. var parts []string
  388. for p := dir; p != nil; p = p.parent {
  389. if strings.HasPrefix(p.name, "/") {
  390. if len(p.name) > 1 {
  391. parts = append(parts, p.name[1:])
  392. }
  393. } else {
  394. parts = append(parts, p.name)
  395. }
  396. }
  397. if len(parts) == 0 {
  398. return "/"
  399. }
  400. var buf bytes.Buffer
  401. for i := len(parts) - 1; i >= 0; i-- {
  402. buf.WriteString("/")
  403. buf.WriteString(parts[i])
  404. }
  405. return buf.String()
  406. }