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.

612 lines
16 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
  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) 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. }
  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) fs.Node {
  99. d := dir.wfs.fsNodeCache.EnsureFsNode(fullpath, func() fs.Node {
  100. return &Dir{name: fullpath.Name(), wfs: dir.wfs, 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. if dir.wfs.option.ReadOnly {
  108. return nil, nil, fuse.EPERM
  109. }
  110. request, err := dir.doCreateEntry(req.Name, req.Mode, req.Uid, req.Gid, req.Flags&fuse.OpenExclusive != 0)
  111. if err != nil {
  112. return nil, nil, err
  113. }
  114. var node fs.Node
  115. if request.Entry.IsDirectory {
  116. node = dir.newDirectory(util.NewFullPath(dir.FullPath(), req.Name))
  117. return node, nil, nil
  118. }
  119. node = dir.newFile(req.Name)
  120. file := node.(*File)
  121. fh := dir.wfs.AcquireHandle(file, req.Uid, req.Gid)
  122. return file, fh, nil
  123. }
  124. func (dir *Dir) Mknod(ctx context.Context, req *fuse.MknodRequest) (fs.Node, error) {
  125. if dir.wfs.option.ReadOnly {
  126. return nil, fuse.EPERM
  127. }
  128. _, err := dir.doCreateEntry(req.Name, req.Mode, req.Uid, req.Gid, false)
  129. if err != nil {
  130. return nil, err
  131. }
  132. var node fs.Node
  133. node = dir.newFile(req.Name)
  134. return node, nil
  135. }
  136. func (dir *Dir) doCreateEntry(name string, mode os.FileMode, uid, gid uint32, exclusive bool) (*filer_pb.CreateEntryRequest, error) {
  137. dirFullPath := dir.FullPath()
  138. request := &filer_pb.CreateEntryRequest{
  139. Directory: dirFullPath,
  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: exclusive,
  155. Signatures: []int32{dir.wfs.signature},
  156. }
  157. glog.V(1).Infof("create %s/%s", dirFullPath, 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", dirFullPath, name, err)
  166. return fuse.EIO
  167. }
  168. if err := dir.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry)); err != nil {
  169. glog.Errorf("local InsertEntry dir %s/%s: %v", dirFullPath, name, err)
  170. return fuse.EIO
  171. }
  172. return nil
  173. })
  174. return request, err
  175. }
  176. func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
  177. if dir.wfs.option.ReadOnly {
  178. return nil, fuse.EPERM
  179. }
  180. glog.V(4).Infof("mkdir %s: %s", dir.FullPath(), req.Name)
  181. newEntry := &filer_pb.Entry{
  182. Name: req.Name,
  183. IsDirectory: true,
  184. Attributes: &filer_pb.FuseAttributes{
  185. Mtime: time.Now().Unix(),
  186. Crtime: time.Now().Unix(),
  187. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  188. Uid: req.Uid,
  189. Gid: req.Gid,
  190. },
  191. }
  192. dirFullPath := dir.FullPath()
  193. err := dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  194. dir.wfs.mapPbIdFromLocalToFiler(newEntry)
  195. defer dir.wfs.mapPbIdFromFilerToLocal(newEntry)
  196. request := &filer_pb.CreateEntryRequest{
  197. Directory: dirFullPath,
  198. Entry: newEntry,
  199. Signatures: []int32{dir.wfs.signature},
  200. }
  201. glog.V(1).Infof("mkdir: %v", request)
  202. if err := filer_pb.CreateEntry(client, request); err != nil {
  203. glog.V(0).Infof("mkdir %s/%s: %v", dirFullPath, req.Name, err)
  204. return err
  205. }
  206. if err := dir.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry)); err != nil {
  207. glog.Errorf("local mkdir dir %s/%s: %v", dirFullPath, req.Name, err)
  208. return fuse.EIO
  209. }
  210. return nil
  211. })
  212. if err == nil {
  213. node := dir.newDirectory(util.NewFullPath(dirFullPath, req.Name))
  214. return node, nil
  215. }
  216. glog.V(0).Infof("mkdir %s/%s: %v", dirFullPath, req.Name, err)
  217. return nil, fuse.EIO
  218. }
  219. func (dir *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (node fs.Node, err error) {
  220. dirPath := util.FullPath(dir.FullPath())
  221. glog.V(4).Infof("dir Lookup %s: %s by %s", dirPath, req.Name, req.Header.String())
  222. fullFilePath := dirPath.Child(req.Name)
  223. visitErr := meta_cache.EnsureVisited(dir.wfs.metaCache, dir.wfs, dirPath)
  224. if visitErr != nil {
  225. glog.Errorf("dir Lookup %s: %v", dirPath, visitErr)
  226. return nil, fuse.EIO
  227. }
  228. localEntry, cacheErr := dir.wfs.metaCache.FindEntry(context.Background(), fullFilePath)
  229. if cacheErr == filer_pb.ErrNotFound {
  230. return nil, fuse.ENOENT
  231. }
  232. if localEntry == nil {
  233. // glog.V(3).Infof("dir Lookup cache miss %s", fullFilePath)
  234. entry, err := filer_pb.GetEntry(dir.wfs, fullFilePath)
  235. if err != nil {
  236. glog.V(1).Infof("dir GetEntry %s: %v", fullFilePath, err)
  237. return nil, fuse.ENOENT
  238. }
  239. localEntry = filer.FromPbEntry(string(dirPath), entry)
  240. } else {
  241. glog.V(4).Infof("dir Lookup cache hit %s", fullFilePath)
  242. }
  243. if localEntry != nil {
  244. if localEntry.IsDirectory() {
  245. node = dir.newDirectory(fullFilePath)
  246. } else {
  247. node = dir.newFile(req.Name)
  248. }
  249. // resp.EntryValid = time.Second
  250. // resp.Attr.Inode = fullFilePath.AsInode()
  251. resp.Attr.Valid = time.Second
  252. resp.Attr.Mtime = localEntry.Attr.Mtime
  253. resp.Attr.Crtime = localEntry.Attr.Crtime
  254. resp.Attr.Mode = localEntry.Attr.Mode
  255. resp.Attr.Gid = localEntry.Attr.Gid
  256. resp.Attr.Uid = localEntry.Attr.Uid
  257. if localEntry.HardLinkCounter > 0 {
  258. resp.Attr.Nlink = uint32(localEntry.HardLinkCounter)
  259. }
  260. return node, nil
  261. }
  262. glog.V(4).Infof("not found dir GetEntry %s: %v", fullFilePath, err)
  263. return nil, fuse.ENOENT
  264. }
  265. func (dir *Dir) ReadDirAll(ctx context.Context) (ret []fuse.Dirent, err error) {
  266. dirPath := util.FullPath(dir.FullPath())
  267. glog.V(4).Infof("dir ReadDirAll %s", dirPath)
  268. processEachEntryFn := func(entry *filer.Entry, isLast bool) {
  269. if entry.IsDirectory() {
  270. dirent := fuse.Dirent{Name: entry.Name(), Type: fuse.DT_Dir, Inode: dirPath.Child(entry.Name()).AsInode()}
  271. ret = append(ret, dirent)
  272. } else {
  273. dirent := fuse.Dirent{Name: entry.Name(), Type: findFileType(uint16(entry.Attr.Mode)), Inode: dirPath.Child(entry.Name()).AsInode()}
  274. ret = append(ret, dirent)
  275. }
  276. }
  277. if err = meta_cache.EnsureVisited(dir.wfs.metaCache, dir.wfs, dirPath); err != nil {
  278. glog.Errorf("dir ReadDirAll %s: %v", dirPath, err)
  279. return nil, fuse.EIO
  280. }
  281. listErr := dir.wfs.metaCache.ListDirectoryEntries(context.Background(), dirPath, "", false, int64(math.MaxInt32), func(entry *filer.Entry) bool {
  282. processEachEntryFn(entry, false)
  283. return true
  284. })
  285. if listErr != nil {
  286. glog.Errorf("list meta cache: %v", listErr)
  287. return nil, fuse.EIO
  288. }
  289. return
  290. }
  291. func findFileType(mode uint16) fuse.DirentType {
  292. switch mode & (syscall.S_IFMT & 0xffff) {
  293. case syscall.S_IFSOCK:
  294. return fuse.DT_Socket
  295. case syscall.S_IFLNK:
  296. return fuse.DT_Link
  297. case syscall.S_IFREG:
  298. return fuse.DT_File
  299. case syscall.S_IFBLK:
  300. return fuse.DT_Block
  301. case syscall.S_IFDIR:
  302. return fuse.DT_Dir
  303. case syscall.S_IFCHR:
  304. return fuse.DT_Char
  305. case syscall.S_IFIFO:
  306. return fuse.DT_FIFO
  307. }
  308. return fuse.DT_File
  309. }
  310. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  311. if dir.wfs.option.ReadOnly {
  312. return fuse.EPERM
  313. }
  314. if !req.Dir {
  315. return dir.removeOneFile(req)
  316. }
  317. return dir.removeFolder(req)
  318. }
  319. func (dir *Dir) removeOneFile(req *fuse.RemoveRequest) error {
  320. dirFullPath := dir.FullPath()
  321. filePath := util.NewFullPath(dirFullPath, req.Name)
  322. entry, err := filer_pb.GetEntry(dir.wfs, filePath)
  323. if err != nil {
  324. return err
  325. }
  326. // first, ensure the filer store can correctly delete
  327. glog.V(3).Infof("remove file: %v", req)
  328. isDeleteData := entry != nil && entry.HardLinkCounter <= 1
  329. err = filer_pb.Remove(dir.wfs, dirFullPath, req.Name, isDeleteData, false, false, false, []int32{dir.wfs.signature})
  330. if err != nil {
  331. glog.V(3).Infof("not found remove file %s: %v", filePath, err)
  332. return fuse.ENOENT
  333. }
  334. // then, delete meta cache and fsNode cache
  335. if err = dir.wfs.metaCache.DeleteEntry(context.Background(), filePath); err != nil {
  336. glog.V(3).Infof("local DeleteEntry %s: %v", filePath, err)
  337. return fuse.ESTALE
  338. }
  339. // clear entry inside the file
  340. fsNode := dir.wfs.fsNodeCache.GetFsNode(filePath)
  341. dir.wfs.fsNodeCache.DeleteFsNode(filePath)
  342. if fsNode != nil {
  343. if file, ok := fsNode.(*File); ok {
  344. file.entry = nil
  345. }
  346. }
  347. // remove current file handle if any
  348. dir.wfs.handlesLock.Lock()
  349. defer dir.wfs.handlesLock.Unlock()
  350. inodeId := filePath.AsInode()
  351. delete(dir.wfs.handles, inodeId)
  352. return nil
  353. }
  354. func (dir *Dir) removeFolder(req *fuse.RemoveRequest) error {
  355. dirFullPath := dir.FullPath()
  356. glog.V(3).Infof("remove directory entry: %v", req)
  357. ignoreRecursiveErr := true // ignore recursion error since the OS should manage it
  358. err := filer_pb.Remove(dir.wfs, dirFullPath, req.Name, true, true, ignoreRecursiveErr, false, []int32{dir.wfs.signature})
  359. if err != nil {
  360. glog.V(0).Infof("remove %s/%s: %v", dirFullPath, req.Name, err)
  361. if strings.Contains(err.Error(), "non-empty") {
  362. return fuse.EEXIST
  363. }
  364. return fuse.ENOENT
  365. }
  366. t := util.NewFullPath(dirFullPath, req.Name)
  367. dir.wfs.metaCache.DeleteEntry(context.Background(), t)
  368. dir.wfs.fsNodeCache.DeleteFsNode(t)
  369. return nil
  370. }
  371. func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  372. if dir.wfs.option.ReadOnly {
  373. return fuse.EPERM
  374. }
  375. glog.V(4).Infof("%v dir setattr %+v", dir.FullPath(), req)
  376. entry, err := dir.maybeLoadEntry()
  377. if err != nil {
  378. return err
  379. }
  380. if req.Valid.Mode() {
  381. entry.Attributes.FileMode = uint32(req.Mode)
  382. }
  383. if req.Valid.Uid() {
  384. entry.Attributes.Uid = req.Uid
  385. }
  386. if req.Valid.Gid() {
  387. entry.Attributes.Gid = req.Gid
  388. }
  389. if req.Valid.Mtime() {
  390. entry.Attributes.Mtime = req.Mtime.Unix()
  391. }
  392. return dir.saveEntry(entry)
  393. }
  394. func (dir *Dir) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  395. if dir.wfs.option.ReadOnly {
  396. return fuse.EPERM
  397. }
  398. glog.V(4).Infof("dir Setxattr %s: %s", dir.FullPath(), req.Name)
  399. entry, err := dir.maybeLoadEntry()
  400. if err != nil {
  401. return err
  402. }
  403. if err := setxattr(entry, req); err != nil {
  404. return err
  405. }
  406. return dir.saveEntry(entry)
  407. }
  408. func (dir *Dir) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  409. if dir.wfs.option.ReadOnly {
  410. return fuse.EPERM
  411. }
  412. glog.V(4).Infof("dir Removexattr %s: %s", dir.FullPath(), req.Name)
  413. entry, err := dir.maybeLoadEntry()
  414. if err != nil {
  415. return err
  416. }
  417. if err := removexattr(entry, req); err != nil {
  418. return err
  419. }
  420. return dir.saveEntry(entry)
  421. }
  422. func (dir *Dir) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  423. glog.V(4).Infof("dir Listxattr %s", dir.FullPath())
  424. entry, err := dir.maybeLoadEntry()
  425. if err != nil {
  426. return err
  427. }
  428. if err := listxattr(entry, req, resp); err != nil {
  429. return err
  430. }
  431. return nil
  432. }
  433. func (dir *Dir) Forget() {
  434. glog.V(4).Infof("Forget dir %s", dir.FullPath())
  435. dir.wfs.fsNodeCache.DeleteFsNode(util.FullPath(dir.FullPath()))
  436. }
  437. func (dir *Dir) maybeLoadEntry() (*filer_pb.Entry, error) {
  438. parentDirPath, name := util.FullPath(dir.FullPath()).DirAndName()
  439. return dir.wfs.maybeLoadEntry(parentDirPath, name)
  440. }
  441. func (dir *Dir) saveEntry(entry *filer_pb.Entry) error {
  442. parentDir, name := util.FullPath(dir.FullPath()).DirAndName()
  443. return dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  444. dir.wfs.mapPbIdFromLocalToFiler(entry)
  445. defer dir.wfs.mapPbIdFromFilerToLocal(entry)
  446. request := &filer_pb.UpdateEntryRequest{
  447. Directory: parentDir,
  448. Entry: entry,
  449. Signatures: []int32{dir.wfs.signature},
  450. }
  451. glog.V(1).Infof("save dir entry: %v", request)
  452. _, err := client.UpdateEntry(context.Background(), request)
  453. if err != nil {
  454. glog.Errorf("UpdateEntry dir %s/%s: %v", parentDir, name, err)
  455. return fuse.EIO
  456. }
  457. if err := dir.wfs.metaCache.UpdateEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry)); err != nil {
  458. glog.Errorf("UpdateEntry dir %s/%s: %v", parentDir, name, err)
  459. return fuse.ESTALE
  460. }
  461. return nil
  462. })
  463. }
  464. func (dir *Dir) FullPath() string {
  465. var parts []string
  466. for p := dir; p != nil; p = p.parent {
  467. if strings.HasPrefix(p.name, "/") {
  468. if len(p.name) > 1 {
  469. parts = append(parts, p.name[1:])
  470. }
  471. } else {
  472. parts = append(parts, p.name)
  473. }
  474. }
  475. if len(parts) == 0 {
  476. return "/"
  477. }
  478. var buf bytes.Buffer
  479. for i := len(parts) - 1; i >= 0; i-- {
  480. buf.WriteString("/")
  481. buf.WriteString(parts[i])
  482. }
  483. return buf.String()
  484. }