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.

600 lines
15 KiB

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