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.

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