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.

601 lines
15 KiB

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