dir.go 13 KB

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