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.

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