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.

576 lines
15 KiB

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