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.

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