dir.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. package filesys
  2. import (
  3. "context"
  4. "math"
  5. "os"
  6. "strings"
  7. "syscall"
  8. "time"
  9. "github.com/seaweedfs/fuse"
  10. "github.com/seaweedfs/fuse/fs"
  11. "github.com/chrislusf/seaweedfs/weed/filer"
  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.Entry
  21. parent *Dir
  22. }
  23. var _ = fs.Node(&Dir{})
  24. var _ = fs.NodeCreater(&Dir{})
  25. var _ = fs.NodeMknoder(&Dir{})
  26. var _ = fs.NodeMkdirer(&Dir{})
  27. var _ = fs.NodeFsyncer(&Dir{})
  28. var _ = fs.NodeRequestLookuper(&Dir{})
  29. var _ = fs.HandleReadDirAller(&Dir{})
  30. var _ = fs.NodeRemover(&Dir{})
  31. var _ = fs.NodeRenamer(&Dir{})
  32. var _ = fs.NodeSetattrer(&Dir{})
  33. var _ = fs.NodeGetxattrer(&Dir{})
  34. var _ = fs.NodeSetxattrer(&Dir{})
  35. var _ = fs.NodeRemovexattrer(&Dir{})
  36. var _ = fs.NodeListxattrer(&Dir{})
  37. var _ = fs.NodeForgetter(&Dir{})
  38. func (dir *Dir) Attr(ctx context.Context, attr *fuse.Attr) error {
  39. // https://github.com/bazil/fuse/issues/196
  40. attr.Valid = time.Second
  41. if dir.FullPath() == dir.wfs.option.FilerMountRootPath {
  42. dir.setRootDirAttributes(attr)
  43. glog.V(3).Infof("root dir Attr %s, attr: %+v", dir.FullPath(), attr)
  44. return nil
  45. }
  46. if err := dir.maybeLoadEntry(); err != nil {
  47. glog.V(3).Infof("dir Attr %s,err: %+v", dir.FullPath(), err)
  48. return err
  49. }
  50. // attr.Inode = util.FullPath(dir.FullPath()).AsInode()
  51. attr.Mode = dir.entry.Attr.Mode | os.ModeDir
  52. attr.Mtime = dir.entry.Attr.Mtime
  53. attr.Crtime = dir.entry.Attr.Crtime
  54. attr.Gid = dir.entry.Attr.Gid
  55. attr.Uid = dir.entry.Attr.Uid
  56. glog.V(4).Infof("dir Attr %s, attr: %+v", dir.FullPath(), attr)
  57. return nil
  58. }
  59. func (dir *Dir) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
  60. glog.V(4).Infof("dir Getxattr %s", dir.FullPath())
  61. if err := dir.maybeLoadEntry(); err != nil {
  62. return err
  63. }
  64. return getxattr(dir.entry, req, resp)
  65. }
  66. func (dir *Dir) setRootDirAttributes(attr *fuse.Attr) {
  67. // attr.Inode = 1 // filer2.FullPath(dir.Path).AsInode()
  68. attr.Valid = time.Second
  69. attr.Uid = dir.wfs.option.MountUid
  70. attr.Gid = dir.wfs.option.MountGid
  71. attr.Mode = dir.wfs.option.MountMode
  72. attr.Crtime = dir.wfs.option.MountCtime
  73. attr.Ctime = dir.wfs.option.MountCtime
  74. attr.Mtime = dir.wfs.option.MountMtime
  75. attr.Atime = dir.wfs.option.MountMtime
  76. attr.BlockSize = blockSize
  77. }
  78. func (dir *Dir) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
  79. // fsync works at OS level
  80. // write the file chunks to the filerGrpcAddress
  81. glog.V(3).Infof("dir %s fsync %+v", dir.FullPath(), req)
  82. return nil
  83. }
  84. func (dir *Dir) newFile(name string, entry *filer_pb.Entry) fs.Node {
  85. dirPath := dir.FullPath()
  86. f := dir.wfs.fsNodeCache.EnsureFsNode(util.NewFullPath(dirPath, name), func() fs.Node {
  87. return &File{
  88. Name: name,
  89. dir: dir,
  90. wfs: dir.wfs,
  91. entry: filer.FromPbEntry(dirPath, entry),
  92. entryViewCache: nil,
  93. }
  94. })
  95. f.(*File).dir = dir // in case dir node was created later
  96. return f
  97. }
  98. func (dir *Dir) newDirectory(fullpath util.FullPath, entry *filer_pb.Entry) fs.Node {
  99. d := dir.wfs.fsNodeCache.EnsureFsNode(fullpath, func() fs.Node {
  100. return &Dir{name: entry.Name, wfs: dir.wfs, entry: filer.FromPbEntry(dir.FullPath(), entry), parent: dir}
  101. })
  102. d.(*Dir).parent = dir // in case dir node was created later
  103. return d
  104. }
  105. func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest,
  106. resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
  107. request, err := dir.doCreateEntry(req.Name, req.Mode, req.Uid, req.Gid, req.Flags&fuse.OpenExclusive != 0)
  108. if err != nil {
  109. return nil, nil, err
  110. }
  111. var node fs.Node
  112. if request.Entry.IsDirectory {
  113. node = dir.newDirectory(util.NewFullPath(dir.FullPath(), req.Name), request.Entry)
  114. return node, nil, nil
  115. }
  116. node = dir.newFile(req.Name, request.Entry)
  117. file := node.(*File)
  118. fh := dir.wfs.AcquireHandle(file, req.Uid, req.Gid)
  119. return file, fh, nil
  120. }
  121. func (dir *Dir) Mknod(ctx context.Context, req *fuse.MknodRequest) (fs.Node, error) {
  122. request, err := dir.doCreateEntry(req.Name, req.Mode, req.Uid, req.Gid, false)
  123. if err != nil {
  124. return nil, err
  125. }
  126. var node fs.Node
  127. node = dir.newFile(req.Name, request.Entry)
  128. return node, nil
  129. }
  130. func (dir *Dir) doCreateEntry(name string, mode os.FileMode, uid, gid uint32, exlusive bool) (*filer_pb.CreateEntryRequest, error) {
  131. request := &filer_pb.CreateEntryRequest{
  132. Directory: dir.FullPath(),
  133. Entry: &filer_pb.Entry{
  134. Name: name,
  135. IsDirectory: mode&os.ModeDir > 0,
  136. Attributes: &filer_pb.FuseAttributes{
  137. Mtime: time.Now().Unix(),
  138. Crtime: time.Now().Unix(),
  139. FileMode: uint32(mode &^ dir.wfs.option.Umask),
  140. Uid: uid,
  141. Gid: gid,
  142. Collection: dir.wfs.option.Collection,
  143. Replication: dir.wfs.option.Replication,
  144. TtlSec: dir.wfs.option.TtlSec,
  145. },
  146. },
  147. OExcl: exlusive,
  148. Signatures: []int32{dir.wfs.signature},
  149. }
  150. glog.V(1).Infof("create %s/%s", dir.FullPath(), name)
  151. err := dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  152. dir.wfs.mapPbIdFromLocalToFiler(request.Entry)
  153. defer dir.wfs.mapPbIdFromFilerToLocal(request.Entry)
  154. if err := filer_pb.CreateEntry(client, request); err != nil {
  155. if strings.Contains(err.Error(), "EEXIST") {
  156. return fuse.EEXIST
  157. }
  158. glog.V(0).Infof("create %s/%s: %v", dir.FullPath(), name, err)
  159. return fuse.EIO
  160. }
  161. dir.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  162. return nil
  163. })
  164. return request, err
  165. }
  166. func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
  167. glog.V(4).Infof("mkdir %s: %s", dir.FullPath(), req.Name)
  168. newEntry := &filer_pb.Entry{
  169. Name: req.Name,
  170. IsDirectory: true,
  171. Attributes: &filer_pb.FuseAttributes{
  172. Mtime: time.Now().Unix(),
  173. Crtime: time.Now().Unix(),
  174. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  175. Uid: req.Uid,
  176. Gid: req.Gid,
  177. },
  178. }
  179. err := dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  180. dir.wfs.mapPbIdFromLocalToFiler(newEntry)
  181. defer dir.wfs.mapPbIdFromFilerToLocal(newEntry)
  182. request := &filer_pb.CreateEntryRequest{
  183. Directory: dir.FullPath(),
  184. Entry: newEntry,
  185. Signatures: []int32{dir.wfs.signature},
  186. }
  187. glog.V(1).Infof("mkdir: %v", request)
  188. if err := filer_pb.CreateEntry(client, request); err != nil {
  189. glog.V(0).Infof("mkdir %s/%s: %v", dir.FullPath(), req.Name, err)
  190. return err
  191. }
  192. dir.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  193. return nil
  194. })
  195. if err == nil {
  196. node := dir.newDirectory(util.NewFullPath(dir.FullPath(), req.Name), newEntry)
  197. return node, nil
  198. }
  199. glog.V(0).Infof("mkdir %s/%s: %v", dir.FullPath(), req.Name, err)
  200. return nil, fuse.EIO
  201. }
  202. func (dir *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (node fs.Node, err error) {
  203. dirPath := util.FullPath(dir.FullPath())
  204. glog.V(4).Infof("dir Lookup %s: %s by %s", dirPath, req.Name, req.Header.String())
  205. fullFilePath := dirPath.Child(req.Name)
  206. visitErr := meta_cache.EnsureVisited(dir.wfs.metaCache, dir.wfs, dirPath)
  207. if visitErr != nil {
  208. glog.Errorf("dir Lookup %s: %v", dirPath, visitErr)
  209. return nil, fuse.EIO
  210. }
  211. cachedEntry, cacheErr := dir.wfs.metaCache.FindEntry(context.Background(), fullFilePath)
  212. if cacheErr == filer_pb.ErrNotFound {
  213. return nil, fuse.ENOENT
  214. }
  215. entry := cachedEntry.ToProtoEntry()
  216. if entry == nil {
  217. // glog.V(3).Infof("dir Lookup cache miss %s", fullFilePath)
  218. entry, err = filer_pb.GetEntry(dir.wfs, fullFilePath)
  219. if err != nil {
  220. glog.V(1).Infof("dir GetEntry %s: %v", fullFilePath, err)
  221. return nil, fuse.ENOENT
  222. }
  223. } else {
  224. glog.V(4).Infof("dir Lookup cache hit %s", fullFilePath)
  225. }
  226. if entry != nil {
  227. if entry.IsDirectory {
  228. node = dir.newDirectory(fullFilePath, entry)
  229. } else {
  230. node = dir.newFile(req.Name, entry)
  231. }
  232. // resp.EntryValid = time.Second
  233. // resp.Attr.Inode = fullFilePath.AsInode()
  234. resp.Attr.Valid = time.Second
  235. resp.Attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  236. resp.Attr.Crtime = time.Unix(entry.Attributes.Crtime, 0)
  237. resp.Attr.Mode = os.FileMode(entry.Attributes.FileMode)
  238. resp.Attr.Gid = entry.Attributes.Gid
  239. resp.Attr.Uid = entry.Attributes.Uid
  240. if entry.HardLinkCounter > 0 {
  241. resp.Attr.Nlink = uint32(entry.HardLinkCounter)
  242. }
  243. return node, nil
  244. }
  245. glog.V(4).Infof("not found dir GetEntry %s: %v", fullFilePath, err)
  246. return nil, fuse.ENOENT
  247. }
  248. func (dir *Dir) ReadDirAll(ctx context.Context) (ret []fuse.Dirent, err error) {
  249. dirPath := util.FullPath(dir.FullPath())
  250. glog.V(4).Infof("dir ReadDirAll %s", dirPath)
  251. processEachEntryFn := func(entry *filer_pb.Entry, isLast bool) error {
  252. if entry.IsDirectory {
  253. dirent := fuse.Dirent{Name: entry.Name, Type: fuse.DT_Dir}
  254. ret = append(ret, dirent)
  255. } else {
  256. dirent := fuse.Dirent{Name: entry.Name, Type: findFileType(uint16(entry.Attributes.FileMode))}
  257. ret = append(ret, dirent)
  258. }
  259. return nil
  260. }
  261. if err = meta_cache.EnsureVisited(dir.wfs.metaCache, dir.wfs, dirPath); err != nil {
  262. glog.Errorf("dir ReadDirAll %s: %v", dirPath, err)
  263. return nil, fuse.EIO
  264. }
  265. listErr := dir.wfs.metaCache.ListDirectoryEntries(context.Background(), dirPath, "", false, int64(math.MaxInt32), func(entry *filer.Entry) bool {
  266. processEachEntryFn(entry.ToProtoEntry(), false)
  267. return true
  268. })
  269. if listErr != nil {
  270. glog.Errorf("list meta cache: %v", listErr)
  271. return nil, fuse.EIO
  272. }
  273. return
  274. }
  275. func findFileType(mode uint16) fuse.DirentType {
  276. switch mode & (syscall.S_IFMT & 0xffff) {
  277. case syscall.S_IFSOCK:
  278. return fuse.DT_Socket
  279. case syscall.S_IFLNK:
  280. return fuse.DT_Link
  281. case syscall.S_IFREG:
  282. return fuse.DT_File
  283. case syscall.S_IFBLK:
  284. return fuse.DT_Block
  285. case syscall.S_IFDIR:
  286. return fuse.DT_Dir
  287. case syscall.S_IFCHR:
  288. return fuse.DT_Char
  289. case syscall.S_IFIFO:
  290. return fuse.DT_FIFO
  291. }
  292. return fuse.DT_File
  293. }
  294. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  295. if !req.Dir {
  296. return dir.removeOneFile(req)
  297. }
  298. return dir.removeFolder(req)
  299. }
  300. func (dir *Dir) removeOneFile(req *fuse.RemoveRequest) error {
  301. filePath := util.NewFullPath(dir.FullPath(), req.Name)
  302. entry, err := filer_pb.GetEntry(dir.wfs, filePath)
  303. if err != nil {
  304. return err
  305. }
  306. if entry == nil {
  307. return nil
  308. }
  309. // first, ensure the filer store can correctly delete
  310. glog.V(3).Infof("remove file: %v", req)
  311. isDeleteData := entry.HardLinkCounter <= 1
  312. err = filer_pb.Remove(dir.wfs, dir.FullPath(), req.Name, isDeleteData, false, false, false, []int32{dir.wfs.signature})
  313. if err != nil {
  314. glog.V(3).Infof("not found remove file %s/%s: %v", dir.FullPath(), req.Name, err)
  315. return fuse.ENOENT
  316. }
  317. // then, delete meta cache and fsNode cache
  318. dir.wfs.metaCache.DeleteEntry(context.Background(), filePath)
  319. // clear entry inside the file
  320. fsNode := dir.wfs.fsNodeCache.GetFsNode(filePath)
  321. dir.wfs.fsNodeCache.DeleteFsNode(filePath)
  322. if fsNode != nil {
  323. if file, ok := fsNode.(*File); ok {
  324. file.clearEntry()
  325. }
  326. }
  327. // remove current file handle if any
  328. dir.wfs.handlesLock.Lock()
  329. defer dir.wfs.handlesLock.Unlock()
  330. inodeId := util.NewFullPath(dir.FullPath(), req.Name).AsInode()
  331. delete(dir.wfs.handles, inodeId)
  332. return nil
  333. }
  334. func (dir *Dir) removeFolder(req *fuse.RemoveRequest) error {
  335. glog.V(3).Infof("remove directory entry: %v", req)
  336. ignoreRecursiveErr := true // ignore recursion error since the OS should manage it
  337. err := filer_pb.Remove(dir.wfs, dir.FullPath(), req.Name, true, false, ignoreRecursiveErr, false, []int32{dir.wfs.signature})
  338. if err != nil {
  339. glog.V(0).Infof("remove %s/%s: %v", dir.FullPath(), req.Name, err)
  340. if strings.Contains(err.Error(), "non-empty") {
  341. return fuse.EEXIST
  342. }
  343. return fuse.ENOENT
  344. }
  345. t := util.NewFullPath(dir.FullPath(), req.Name)
  346. dir.wfs.metaCache.DeleteEntry(context.Background(), t)
  347. dir.wfs.fsNodeCache.DeleteFsNode(t)
  348. return nil
  349. }
  350. func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  351. glog.V(4).Infof("%v dir setattr %+v", dir.FullPath(), req)
  352. if err := dir.maybeLoadEntry(); err != nil {
  353. return err
  354. }
  355. if req.Valid.Mode() {
  356. dir.entry.Attr.Mode = req.Mode
  357. }
  358. if req.Valid.Uid() {
  359. dir.entry.Attr.Uid = req.Uid
  360. }
  361. if req.Valid.Gid() {
  362. dir.entry.Attr.Gid = req.Gid
  363. }
  364. if req.Valid.Mtime() {
  365. dir.entry.Attr.Mtime = req.Mtime
  366. }
  367. return dir.saveEntry()
  368. }
  369. func (dir *Dir) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  370. glog.V(4).Infof("dir Setxattr %s: %s", dir.FullPath(), req.Name)
  371. if err := dir.maybeLoadEntry(); err != nil {
  372. return err
  373. }
  374. if err := setxattr(dir.entry, req); err != nil {
  375. return err
  376. }
  377. return dir.saveEntry()
  378. }
  379. func (dir *Dir) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  380. glog.V(4).Infof("dir Removexattr %s: %s", dir.FullPath(), req.Name)
  381. if err := dir.maybeLoadEntry(); err != nil {
  382. return err
  383. }
  384. if err := removexattr(dir.entry, req); err != nil {
  385. return err
  386. }
  387. return dir.saveEntry()
  388. }
  389. func (dir *Dir) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  390. glog.V(4).Infof("dir Listxattr %s", dir.FullPath())
  391. if err := dir.maybeLoadEntry(); err != nil {
  392. return err
  393. }
  394. if err := listxattr(dir.entry, req, resp); err != nil {
  395. return err
  396. }
  397. return nil
  398. }
  399. func (dir *Dir) Forget() {
  400. glog.V(4).Infof("Forget dir %s", dir.FullPath())
  401. dir.wfs.fsNodeCache.DeleteFsNode(util.FullPath(dir.FullPath()))
  402. }
  403. func (dir *Dir) maybeLoadEntry() error {
  404. if dir.entry == nil {
  405. parentDirPath, name := util.FullPath(dir.FullPath()).DirAndName()
  406. entry, err := dir.wfs.maybeLoadEntry(parentDirPath, name)
  407. if err != nil {
  408. return err
  409. }
  410. dir.entry = entry
  411. }
  412. return nil
  413. }
  414. func (dir *Dir) saveEntry() error {
  415. parentDir, name := util.FullPath(dir.FullPath()).DirAndName()
  416. return dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  417. pbEntry := dir.entry.ToProtoEntry()
  418. dir.wfs.mapPbIdFromLocalToFiler(pbEntry)
  419. defer dir.wfs.mapPbIdFromFilerToLocal(pbEntry)
  420. request := &filer_pb.UpdateEntryRequest{
  421. Directory: parentDir,
  422. Entry: pbEntry,
  423. Signatures: []int32{dir.wfs.signature},
  424. }
  425. glog.V(1).Infof("save dir entry: %v", request)
  426. _, err := client.UpdateEntry(context.Background(), request)
  427. if err != nil {
  428. glog.Errorf("UpdateEntry dir %s/%s: %v", parentDir, name, err)
  429. return fuse.EIO
  430. }
  431. dir.wfs.metaCache.UpdateEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  432. return nil
  433. })
  434. }
  435. func (dir *Dir) FullPath() string {
  436. return string(dir.entry.FullPath)
  437. }