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.

486 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("remove %s/%s: %v", dir.FullPath(), req.Name, err)
  269. if strings.Contains(err.Error(), "non-empty"){
  270. return fuse.EEXIST
  271. }
  272. return fuse.ENOENT
  273. }
  274. return nil
  275. }
  276. func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  277. glog.V(3).Infof("%v dir setattr %+v", dir.FullPath(), req)
  278. if err := dir.maybeLoadEntry(); err != nil {
  279. return err
  280. }
  281. if req.Valid.Mode() {
  282. dir.entry.Attributes.FileMode = uint32(req.Mode)
  283. }
  284. if req.Valid.Uid() {
  285. dir.entry.Attributes.Uid = req.Uid
  286. }
  287. if req.Valid.Gid() {
  288. dir.entry.Attributes.Gid = req.Gid
  289. }
  290. if req.Valid.Mtime() {
  291. dir.entry.Attributes.Mtime = req.Mtime.Unix()
  292. }
  293. return dir.saveEntry()
  294. }
  295. func (dir *Dir) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  296. glog.V(4).Infof("dir Setxattr %s: %s", dir.FullPath(), req.Name)
  297. if err := dir.maybeLoadEntry(); err != nil {
  298. return err
  299. }
  300. if err := setxattr(dir.entry, req); err != nil {
  301. return err
  302. }
  303. return dir.saveEntry()
  304. }
  305. func (dir *Dir) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  306. glog.V(4).Infof("dir Removexattr %s: %s", dir.FullPath(), req.Name)
  307. if err := dir.maybeLoadEntry(); err != nil {
  308. return err
  309. }
  310. if err := removexattr(dir.entry, req); err != nil {
  311. return err
  312. }
  313. return dir.saveEntry()
  314. }
  315. func (dir *Dir) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  316. glog.V(4).Infof("dir Listxattr %s", dir.FullPath())
  317. if err := dir.maybeLoadEntry(); err != nil {
  318. return err
  319. }
  320. if err := listxattr(dir.entry, req, resp); err != nil {
  321. return err
  322. }
  323. return nil
  324. }
  325. func (dir *Dir) Forget() {
  326. glog.V(3).Infof("Forget dir %s", dir.FullPath())
  327. }
  328. func (dir *Dir) maybeLoadEntry() error {
  329. if dir.entry == nil {
  330. parentDirPath, name := util.FullPath(dir.FullPath()).DirAndName()
  331. entry, err := dir.wfs.maybeLoadEntry(parentDirPath, name)
  332. if err != nil {
  333. return err
  334. }
  335. dir.entry = entry
  336. }
  337. return nil
  338. }
  339. func (dir *Dir) saveEntry() error {
  340. parentDir, name := util.FullPath(dir.FullPath()).DirAndName()
  341. return dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  342. request := &filer_pb.UpdateEntryRequest{
  343. Directory: parentDir,
  344. Entry: dir.entry,
  345. }
  346. glog.V(1).Infof("save dir entry: %v", request)
  347. _, err := client.UpdateEntry(context.Background(), request)
  348. if err != nil {
  349. glog.V(0).Infof("UpdateEntry dir %s/%s: %v", parentDir, name, err)
  350. return fuse.EIO
  351. }
  352. dir.wfs.metaCache.UpdateEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
  353. return nil
  354. })
  355. }
  356. func (dir *Dir) FullPath() string {
  357. var parts []string
  358. for p := dir; p != nil; p = p.parent {
  359. if strings.HasPrefix(p.name, "/") {
  360. if len(p.name) > 1 {
  361. parts = append(parts, p.name[1:])
  362. }
  363. } else {
  364. parts = append(parts, p.name)
  365. }
  366. }
  367. if len(parts) == 0 {
  368. return "/"
  369. }
  370. var buf bytes.Buffer
  371. for i := len(parts) - 1; i >= 0; i-- {
  372. buf.WriteString("/")
  373. buf.WriteString(parts[i])
  374. }
  375. return buf.String()
  376. }