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.

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