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.

487 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
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/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. "github.com/seaweedfs/fuse"
  12. "github.com/seaweedfs/fuse/fs"
  13. )
  14. type Dir struct {
  15. name string
  16. wfs *WFS
  17. entry *filer_pb.Entry
  18. parent *Dir
  19. }
  20. var _ = fs.Node(&Dir{})
  21. var _ = fs.NodeCreater(&Dir{})
  22. var _ = fs.NodeMkdirer(&Dir{})
  23. var _ = fs.NodeRequestLookuper(&Dir{})
  24. var _ = fs.HandleReadDirAller(&Dir{})
  25. var _ = fs.NodeRemover(&Dir{})
  26. var _ = fs.NodeRenamer(&Dir{})
  27. var _ = fs.NodeSetattrer(&Dir{})
  28. var _ = fs.NodeGetxattrer(&Dir{})
  29. var _ = fs.NodeSetxattrer(&Dir{})
  30. var _ = fs.NodeRemovexattrer(&Dir{})
  31. var _ = fs.NodeListxattrer(&Dir{})
  32. var _ = fs.NodeForgetter(&Dir{})
  33. func (dir *Dir) Attr(ctx context.Context, attr *fuse.Attr) error {
  34. // https://github.com/bazil/fuse/issues/196
  35. attr.Valid = time.Second
  36. if dir.FullPath() == dir.wfs.option.FilerMountRootPath {
  37. dir.setRootDirAttributes(attr)
  38. glog.V(3).Infof("root dir Attr %s, attr: %+v", dir.FullPath(), attr)
  39. return nil
  40. }
  41. if err := dir.maybeLoadEntry(); err != nil {
  42. glog.V(3).Infof("dir Attr %s,err: %+v", dir.FullPath(), err)
  43. return err
  44. }
  45. attr.Inode = util.FullPath(dir.FullPath()).AsInode()
  46. attr.Mode = os.FileMode(dir.entry.Attributes.FileMode) | os.ModeDir
  47. attr.Mtime = time.Unix(dir.entry.Attributes.Mtime, 0)
  48. attr.Crtime = time.Unix(dir.entry.Attributes.Crtime, 0)
  49. attr.Gid = dir.entry.Attributes.Gid
  50. attr.Uid = dir.entry.Attributes.Uid
  51. glog.V(4).Infof("dir Attr %s, attr: %+v", dir.FullPath(), attr)
  52. return nil
  53. }
  54. func (dir *Dir) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
  55. glog.V(4).Infof("dir Getxattr %s", dir.FullPath())
  56. if err := dir.maybeLoadEntry(); err != nil {
  57. return err
  58. }
  59. return getxattr(dir.entry, req, resp)
  60. }
  61. func (dir *Dir) setRootDirAttributes(attr *fuse.Attr) {
  62. attr.Inode = 1 // filer2.FullPath(dir.Path).AsInode()
  63. attr.Valid = time.Hour
  64. attr.Uid = dir.wfs.option.MountUid
  65. attr.Gid = dir.wfs.option.MountGid
  66. attr.Mode = dir.wfs.option.MountMode
  67. attr.Crtime = dir.wfs.option.MountCtime
  68. attr.Ctime = dir.wfs.option.MountCtime
  69. attr.Mtime = dir.wfs.option.MountMtime
  70. attr.Atime = dir.wfs.option.MountMtime
  71. attr.BlockSize = 1024 * 1024
  72. }
  73. func (dir *Dir) newFile(name string, entry *filer_pb.Entry) fs.Node {
  74. return dir.wfs.fsNodeCache.EnsureFsNode(util.NewFullPath(dir.FullPath(), name), func() fs.Node {
  75. return &File{
  76. Name: name,
  77. dir: dir,
  78. wfs: dir.wfs,
  79. entry: entry,
  80. entryViewCache: nil,
  81. }
  82. })
  83. }
  84. func (dir *Dir) newDirectory(fullpath util.FullPath, entry *filer_pb.Entry) fs.Node {
  85. return dir.wfs.fsNodeCache.EnsureFsNode(fullpath, func() fs.Node {
  86. return &Dir{name: entry.Name, wfs: dir.wfs, entry: entry, parent: dir}
  87. })
  88. }
  89. func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest,
  90. resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
  91. request := &filer_pb.CreateEntryRequest{
  92. Directory: dir.FullPath(),
  93. Entry: &filer_pb.Entry{
  94. Name: req.Name,
  95. IsDirectory: req.Mode&os.ModeDir > 0,
  96. Attributes: &filer_pb.FuseAttributes{
  97. Mtime: time.Now().Unix(),
  98. Crtime: time.Now().Unix(),
  99. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  100. Uid: req.Uid,
  101. Gid: req.Gid,
  102. Collection: dir.wfs.option.Collection,
  103. Replication: dir.wfs.option.Replication,
  104. TtlSec: dir.wfs.option.TtlSec,
  105. },
  106. },
  107. OExcl: req.Flags&fuse.OpenExclusive != 0,
  108. }
  109. glog.V(1).Infof("create %s/%s: %v", dir.FullPath(), req.Name, req.Flags)
  110. if err := dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  111. if err := filer_pb.CreateEntry(client, request); err != nil {
  112. if strings.Contains(err.Error(), "EEXIST") {
  113. return fuse.EEXIST
  114. }
  115. return fuse.EIO
  116. }
  117. return nil
  118. }); err != nil {
  119. return nil, nil, err
  120. }
  121. var node fs.Node
  122. if request.Entry.IsDirectory {
  123. node = dir.newDirectory(util.NewFullPath(dir.FullPath(), req.Name), request.Entry)
  124. return node, nil, nil
  125. }
  126. node = dir.newFile(req.Name, request.Entry)
  127. file := node.(*File)
  128. file.isOpen++
  129. fh := dir.wfs.AcquireHandle(file, req.Uid, req.Gid)
  130. return file, fh, nil
  131. }
  132. func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
  133. glog.V(4).Infof("mkdir %s: %s", dir.FullPath(), req.Name)
  134. newEntry := &filer_pb.Entry{
  135. Name: req.Name,
  136. IsDirectory: true,
  137. Attributes: &filer_pb.FuseAttributes{
  138. Mtime: time.Now().Unix(),
  139. Crtime: time.Now().Unix(),
  140. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  141. Uid: req.Uid,
  142. Gid: req.Gid,
  143. },
  144. }
  145. err := dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  146. request := &filer_pb.CreateEntryRequest{
  147. Directory: dir.FullPath(),
  148. Entry: newEntry,
  149. }
  150. glog.V(1).Infof("mkdir: %v", request)
  151. if err := filer_pb.CreateEntry(client, request); err != nil {
  152. glog.V(0).Infof("mkdir %s/%s: %v", dir.FullPath(), req.Name, err)
  153. return err
  154. }
  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. fullFilePath := util.NewFullPath(dir.FullPath(), req.Name)
  167. entry := dir.wfs.cacheGet(fullFilePath)
  168. if dir.wfs.option.AsyncMetaDataCaching {
  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. }
  175. if entry == nil {
  176. // glog.V(3).Infof("dir Lookup cache miss %s", fullFilePath)
  177. entry, err = filer_pb.GetEntry(dir.wfs, fullFilePath)
  178. if err != nil {
  179. glog.V(1).Infof("dir GetEntry %s: %v", fullFilePath, err)
  180. return nil, fuse.ENOENT
  181. }
  182. dir.wfs.cacheSet(fullFilePath, entry, 5*time.Minute)
  183. } else {
  184. glog.V(4).Infof("dir Lookup cache hit %s", fullFilePath)
  185. }
  186. if entry != nil {
  187. if entry.IsDirectory {
  188. node = dir.newDirectory(fullFilePath, entry)
  189. } else {
  190. node = dir.newFile(req.Name, entry)
  191. }
  192. // resp.EntryValid = time.Second
  193. resp.Attr.Inode = fullFilePath.AsInode()
  194. resp.Attr.Valid = time.Second
  195. resp.Attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  196. resp.Attr.Crtime = time.Unix(entry.Attributes.Crtime, 0)
  197. resp.Attr.Mode = os.FileMode(entry.Attributes.FileMode)
  198. resp.Attr.Gid = entry.Attributes.Gid
  199. resp.Attr.Uid = entry.Attributes.Uid
  200. return node, nil
  201. }
  202. glog.V(4).Infof("not found dir GetEntry %s: %v", fullFilePath, err)
  203. return nil, fuse.ENOENT
  204. }
  205. func (dir *Dir) ReadDirAll(ctx context.Context) (ret []fuse.Dirent, err error) {
  206. glog.V(3).Infof("dir ReadDirAll %s", dir.FullPath())
  207. cacheTtl := 5 * time.Minute
  208. processEachEntryFn := func(entry *filer_pb.Entry, isLast bool) {
  209. fullpath := util.NewFullPath(dir.FullPath(), entry.Name)
  210. inode := fullpath.AsInode()
  211. if entry.IsDirectory {
  212. dirent := fuse.Dirent{Inode: inode, Name: entry.Name, Type: fuse.DT_Dir}
  213. ret = append(ret, dirent)
  214. } else {
  215. dirent := fuse.Dirent{Inode: inode, Name: entry.Name, Type: fuse.DT_File}
  216. ret = append(ret, dirent)
  217. }
  218. dir.wfs.cacheSet(fullpath, entry, cacheTtl)
  219. }
  220. if dir.wfs.option.AsyncMetaDataCaching {
  221. listedEntries, listErr := dir.wfs.metaCache.ListDirectoryEntries(context.Background(), util.FullPath(dir.FullPath()), "", false, int(dir.wfs.option.DirListCacheLimit))
  222. if listErr != nil {
  223. glog.Errorf("list meta cache: %v", listErr)
  224. return nil, fuse.EIO
  225. }
  226. for _, cachedEntry := range listedEntries {
  227. processEachEntryFn(cachedEntry.ToProtoEntry(), false)
  228. }
  229. return
  230. }
  231. readErr := filer_pb.ReadDirAllEntries(dir.wfs, util.FullPath(dir.FullPath()), "", processEachEntryFn)
  232. if readErr != nil {
  233. glog.V(0).Infof("list %s: %v", dir.FullPath(), err)
  234. return ret, fuse.EIO
  235. }
  236. return ret, err
  237. }
  238. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  239. if !req.Dir {
  240. return dir.removeOneFile(req)
  241. }
  242. return dir.removeFolder(req)
  243. }
  244. func (dir *Dir) removeOneFile(req *fuse.RemoveRequest) error {
  245. filePath := util.NewFullPath(dir.FullPath(), req.Name)
  246. entry, err := filer_pb.GetEntry(dir.wfs, filePath)
  247. if err != nil {
  248. return err
  249. }
  250. if entry == nil {
  251. return nil
  252. }
  253. dir.wfs.deleteFileChunks(entry.Chunks)
  254. dir.wfs.cacheDelete(filePath)
  255. dir.wfs.fsNodeCache.DeleteFsNode(filePath)
  256. glog.V(3).Infof("remove file: %v", req)
  257. err = filer_pb.Remove(dir.wfs, dir.FullPath(), req.Name, false, false, false)
  258. if err != nil {
  259. glog.V(3).Infof("not found remove file %s/%s: %v", dir.FullPath(), req.Name, err)
  260. return fuse.ENOENT
  261. }
  262. return nil
  263. }
  264. func (dir *Dir) removeFolder(req *fuse.RemoveRequest) error {
  265. t := util.NewFullPath(dir.FullPath(), req.Name)
  266. dir.wfs.cacheDelete(t)
  267. dir.wfs.fsNodeCache.DeleteFsNode(t)
  268. glog.V(3).Infof("remove directory entry: %v", req)
  269. err := filer_pb.Remove(dir.wfs, dir.FullPath(), req.Name, true, false, false)
  270. if err != nil {
  271. glog.V(3).Infof("not found remove %s/%s: %v", dir.FullPath(), req.Name, err)
  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. dir.wfs.cacheDelete(util.FullPath(dir.FullPath()))
  294. return dir.saveEntry()
  295. }
  296. func (dir *Dir) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  297. glog.V(4).Infof("dir Setxattr %s: %s", dir.FullPath(), req.Name)
  298. if err := dir.maybeLoadEntry(); err != nil {
  299. return err
  300. }
  301. if err := setxattr(dir.entry, req); err != nil {
  302. return err
  303. }
  304. dir.wfs.cacheDelete(util.FullPath(dir.FullPath()))
  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. dir.wfs.cacheDelete(util.FullPath(dir.FullPath()))
  316. return dir.saveEntry()
  317. }
  318. func (dir *Dir) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  319. glog.V(4).Infof("dir Listxattr %s", dir.FullPath())
  320. if err := dir.maybeLoadEntry(); err != nil {
  321. return err
  322. }
  323. if err := listxattr(dir.entry, req, resp); err != nil {
  324. return err
  325. }
  326. return nil
  327. }
  328. func (dir *Dir) Forget() {
  329. glog.V(3).Infof("Forget dir %s", dir.FullPath())
  330. dir.wfs.fsNodeCache.DeleteFsNode(util.FullPath(dir.FullPath()))
  331. }
  332. func (dir *Dir) maybeLoadEntry() error {
  333. if dir.entry == nil {
  334. parentDirPath, name := util.FullPath(dir.FullPath()).DirAndName()
  335. entry, err := dir.wfs.maybeLoadEntry(parentDirPath, name)
  336. if err != nil {
  337. return err
  338. }
  339. dir.entry = entry
  340. }
  341. return nil
  342. }
  343. func (dir *Dir) saveEntry() error {
  344. parentDir, name := util.FullPath(dir.FullPath()).DirAndName()
  345. return dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  346. request := &filer_pb.UpdateEntryRequest{
  347. Directory: parentDir,
  348. Entry: dir.entry,
  349. }
  350. glog.V(1).Infof("save dir entry: %v", request)
  351. _, err := client.UpdateEntry(context.Background(), request)
  352. if err != nil {
  353. glog.V(0).Infof("UpdateEntry dir %s/%s: %v", parentDir, name, err)
  354. return fuse.EIO
  355. }
  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. }