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.

483 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. "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(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. return &File{
  85. Name: name,
  86. dir: dir,
  87. wfs: dir.wfs,
  88. entry: entry,
  89. entryViewCache: nil,
  90. }
  91. }
  92. func (dir *Dir) newDirectory(fullpath util.FullPath, entry *filer_pb.Entry) fs.Node {
  93. return &Dir{name: entry.Name, wfs: dir.wfs, entry: entry, parent: dir}
  94. }
  95. func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest,
  96. resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
  97. request := &filer_pb.CreateEntryRequest{
  98. Directory: dir.FullPath(),
  99. Entry: &filer_pb.Entry{
  100. Name: req.Name,
  101. IsDirectory: req.Mode&os.ModeDir > 0,
  102. Attributes: &filer_pb.FuseAttributes{
  103. Mtime: time.Now().Unix(),
  104. Crtime: time.Now().Unix(),
  105. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  106. Uid: req.Uid,
  107. Gid: req.Gid,
  108. Collection: dir.wfs.option.Collection,
  109. Replication: dir.wfs.option.Replication,
  110. TtlSec: dir.wfs.option.TtlSec,
  111. },
  112. },
  113. OExcl: req.Flags&fuse.OpenExclusive != 0,
  114. }
  115. glog.V(1).Infof("create %s/%s: %v", dir.FullPath(), req.Name, req.Flags)
  116. if err := dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  117. if err := filer_pb.CreateEntry(client, request); err != nil {
  118. if strings.Contains(err.Error(), "EEXIST") {
  119. return fuse.EEXIST
  120. }
  121. return fuse.EIO
  122. }
  123. dir.wfs.metaCache.InsertEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
  124. return nil
  125. }); err != nil {
  126. return nil, nil, err
  127. }
  128. var node fs.Node
  129. if request.Entry.IsDirectory {
  130. node = dir.newDirectory(util.NewFullPath(dir.FullPath(), req.Name), request.Entry)
  131. return node, nil, nil
  132. }
  133. node = dir.newFile(req.Name, request.Entry)
  134. file := node.(*File)
  135. file.isOpen++
  136. fh := dir.wfs.AcquireHandle(file, req.Uid, req.Gid)
  137. return file, fh, nil
  138. }
  139. func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
  140. glog.V(4).Infof("mkdir %s: %s", dir.FullPath(), req.Name)
  141. newEntry := &filer_pb.Entry{
  142. Name: req.Name,
  143. IsDirectory: true,
  144. Attributes: &filer_pb.FuseAttributes{
  145. Mtime: time.Now().Unix(),
  146. Crtime: time.Now().Unix(),
  147. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  148. Uid: req.Uid,
  149. Gid: req.Gid,
  150. },
  151. }
  152. err := dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  153. request := &filer_pb.CreateEntryRequest{
  154. Directory: dir.FullPath(),
  155. Entry: newEntry,
  156. }
  157. glog.V(1).Infof("mkdir: %v", request)
  158. if err := filer_pb.CreateEntry(client, request); err != nil {
  159. glog.V(0).Infof("mkdir %s/%s: %v", dir.FullPath(), req.Name, err)
  160. return err
  161. }
  162. dir.wfs.metaCache.InsertEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
  163. return nil
  164. })
  165. if err == nil {
  166. node := dir.newDirectory(util.NewFullPath(dir.FullPath(), req.Name), newEntry)
  167. return node, nil
  168. }
  169. glog.V(0).Infof("mkdir %s/%s: %v", dir.FullPath(), req.Name, err)
  170. return nil, fuse.EIO
  171. }
  172. func (dir *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (node fs.Node, err error) {
  173. glog.V(4).Infof("dir Lookup %s: %s by %s", dir.FullPath(), req.Name, req.Header.String())
  174. fullFilePath := util.NewFullPath(dir.FullPath(), req.Name)
  175. dirPath := util.FullPath(dir.FullPath())
  176. meta_cache.EnsureVisited(dir.wfs.metaCache, dir.wfs, util.FullPath(dirPath))
  177. cachedEntry, cacheErr := dir.wfs.metaCache.FindEntry(context.Background(), fullFilePath)
  178. if cacheErr == filer_pb.ErrNotFound {
  179. return nil, fuse.ENOENT
  180. }
  181. entry := cachedEntry.ToProtoEntry()
  182. if entry == nil {
  183. // glog.V(3).Infof("dir Lookup cache miss %s", fullFilePath)
  184. entry, err = filer_pb.GetEntry(dir.wfs, fullFilePath)
  185. if err != nil {
  186. glog.V(1).Infof("dir GetEntry %s: %v", fullFilePath, err)
  187. return nil, fuse.ENOENT
  188. }
  189. } else {
  190. glog.V(4).Infof("dir Lookup cache hit %s", fullFilePath)
  191. }
  192. if entry != nil {
  193. if entry.IsDirectory {
  194. node = dir.newDirectory(fullFilePath, entry)
  195. } else {
  196. node = dir.newFile(req.Name, entry)
  197. }
  198. // resp.EntryValid = time.Second
  199. resp.Attr.Inode = fullFilePath.AsInode()
  200. resp.Attr.Valid = time.Second
  201. resp.Attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  202. resp.Attr.Crtime = time.Unix(entry.Attributes.Crtime, 0)
  203. resp.Attr.Mode = os.FileMode(entry.Attributes.FileMode)
  204. resp.Attr.Gid = entry.Attributes.Gid
  205. resp.Attr.Uid = entry.Attributes.Uid
  206. return node, nil
  207. }
  208. glog.V(4).Infof("not found dir GetEntry %s: %v", fullFilePath, err)
  209. return nil, fuse.ENOENT
  210. }
  211. func (dir *Dir) ReadDirAll(ctx context.Context) (ret []fuse.Dirent, err error) {
  212. glog.V(3).Infof("dir ReadDirAll %s", dir.FullPath())
  213. processEachEntryFn := func(entry *filer_pb.Entry, isLast bool) error {
  214. fullpath := util.NewFullPath(dir.FullPath(), entry.Name)
  215. inode := fullpath.AsInode()
  216. if entry.IsDirectory {
  217. dirent := fuse.Dirent{Inode: inode, Name: entry.Name, Type: fuse.DT_Dir}
  218. ret = append(ret, dirent)
  219. } else {
  220. dirent := fuse.Dirent{Inode: inode, Name: entry.Name, Type: fuse.DT_File}
  221. ret = append(ret, dirent)
  222. }
  223. return nil
  224. }
  225. dirPath := util.FullPath(dir.FullPath())
  226. meta_cache.EnsureVisited(dir.wfs.metaCache, dir.wfs, dirPath)
  227. listedEntries, listErr := dir.wfs.metaCache.ListDirectoryEntries(context.Background(), util.FullPath(dir.FullPath()), "", false, int(math.MaxInt32))
  228. if listErr != nil {
  229. glog.Errorf("list meta cache: %v", listErr)
  230. return nil, fuse.EIO
  231. }
  232. for _, cachedEntry := range listedEntries {
  233. processEachEntryFn(cachedEntry.ToProtoEntry(), false)
  234. }
  235. return
  236. }
  237. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  238. if !req.Dir {
  239. return dir.removeOneFile(req)
  240. }
  241. return dir.removeFolder(req)
  242. }
  243. func (dir *Dir) removeOneFile(req *fuse.RemoveRequest) error {
  244. filePath := util.NewFullPath(dir.FullPath(), req.Name)
  245. entry, err := filer_pb.GetEntry(dir.wfs, filePath)
  246. if err != nil {
  247. return err
  248. }
  249. if entry == nil {
  250. return nil
  251. }
  252. dir.wfs.deleteFileChunks(entry.Chunks)
  253. dir.wfs.metaCache.DeleteEntry(context.Background(), filePath)
  254. glog.V(3).Infof("remove file: %v", req)
  255. err = filer_pb.Remove(dir.wfs, dir.FullPath(), req.Name, false, false, false, false)
  256. if err != nil {
  257. glog.V(3).Infof("not found remove file %s/%s: %v", dir.FullPath(), req.Name, err)
  258. return fuse.ENOENT
  259. }
  260. return nil
  261. }
  262. func (dir *Dir) removeFolder(req *fuse.RemoveRequest) error {
  263. t := util.NewFullPath(dir.FullPath(), req.Name)
  264. dir.wfs.metaCache.DeleteEntry(context.Background(), t)
  265. glog.V(3).Infof("remove directory entry: %v", req)
  266. err := filer_pb.Remove(dir.wfs, dir.FullPath(), req.Name, true, false, false, false)
  267. if err != nil {
  268. glog.V(3).Infof("not found remove %s/%s: %v", dir.FullPath(), req.Name, err)
  269. return fuse.ENOENT
  270. }
  271. return nil
  272. }
  273. func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  274. glog.V(3).Infof("%v dir setattr %+v", dir.FullPath(), req)
  275. if err := dir.maybeLoadEntry(); err != nil {
  276. return err
  277. }
  278. if req.Valid.Mode() {
  279. dir.entry.Attributes.FileMode = uint32(req.Mode)
  280. }
  281. if req.Valid.Uid() {
  282. dir.entry.Attributes.Uid = req.Uid
  283. }
  284. if req.Valid.Gid() {
  285. dir.entry.Attributes.Gid = req.Gid
  286. }
  287. if req.Valid.Mtime() {
  288. dir.entry.Attributes.Mtime = req.Mtime.Unix()
  289. }
  290. return dir.saveEntry()
  291. }
  292. func (dir *Dir) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  293. glog.V(4).Infof("dir Setxattr %s: %s", dir.FullPath(), req.Name)
  294. if err := dir.maybeLoadEntry(); err != nil {
  295. return err
  296. }
  297. if err := setxattr(dir.entry, req); err != nil {
  298. return err
  299. }
  300. return dir.saveEntry()
  301. }
  302. func (dir *Dir) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  303. glog.V(4).Infof("dir Removexattr %s: %s", dir.FullPath(), req.Name)
  304. if err := dir.maybeLoadEntry(); err != nil {
  305. return err
  306. }
  307. if err := removexattr(dir.entry, req); err != nil {
  308. return err
  309. }
  310. return dir.saveEntry()
  311. }
  312. func (dir *Dir) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  313. glog.V(4).Infof("dir Listxattr %s", dir.FullPath())
  314. if err := dir.maybeLoadEntry(); err != nil {
  315. return err
  316. }
  317. if err := listxattr(dir.entry, req, resp); err != nil {
  318. return err
  319. }
  320. return nil
  321. }
  322. func (dir *Dir) Forget() {
  323. glog.V(3).Infof("Forget dir %s", dir.FullPath())
  324. }
  325. func (dir *Dir) maybeLoadEntry() error {
  326. if dir.entry == nil {
  327. parentDirPath, name := util.FullPath(dir.FullPath()).DirAndName()
  328. entry, err := dir.wfs.maybeLoadEntry(parentDirPath, name)
  329. if err != nil {
  330. return err
  331. }
  332. dir.entry = entry
  333. }
  334. return nil
  335. }
  336. func (dir *Dir) saveEntry() error {
  337. parentDir, name := util.FullPath(dir.FullPath()).DirAndName()
  338. return dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  339. request := &filer_pb.UpdateEntryRequest{
  340. Directory: parentDir,
  341. Entry: dir.entry,
  342. }
  343. glog.V(1).Infof("save dir entry: %v", request)
  344. _, err := client.UpdateEntry(context.Background(), request)
  345. if err != nil {
  346. glog.V(0).Infof("UpdateEntry dir %s/%s: %v", parentDir, name, err)
  347. return fuse.EIO
  348. }
  349. dir.wfs.metaCache.UpdateEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
  350. return nil
  351. })
  352. }
  353. func (dir *Dir) FullPath() string {
  354. var parts []string
  355. for p := dir; p != nil; p = p.parent {
  356. if strings.HasPrefix(p.name, "/") {
  357. if len(p.name) > 1 {
  358. parts = append(parts, p.name[1:])
  359. }
  360. } else {
  361. parts = append(parts, p.name)
  362. }
  363. }
  364. if len(parts) == 0 {
  365. return "/"
  366. }
  367. var buf bytes.Buffer
  368. for i := len(parts) - 1; i >= 0; i-- {
  369. buf.WriteString("/")
  370. buf.WriteString(parts[i])
  371. }
  372. return buf.String()
  373. }