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.

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