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.

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