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.

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