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.

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