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