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.

469 lines
12 KiB

7 years ago
5 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
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. "os"
  6. "strings"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/filer2"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "github.com/seaweedfs/fuse"
  13. "github.com/seaweedfs/fuse/fs"
  14. )
  15. type Dir struct {
  16. name string
  17. wfs *WFS
  18. entry *filer_pb.Entry
  19. parent *Dir
  20. }
  21. var _ = fs.Node(&Dir{})
  22. var _ = fs.NodeCreater(&Dir{})
  23. var _ = fs.NodeMkdirer(&Dir{})
  24. var _ = fs.NodeRequestLookuper(&Dir{})
  25. var _ = fs.HandleReadDirAller(&Dir{})
  26. var _ = fs.NodeRemover(&Dir{})
  27. var _ = fs.NodeRenamer(&Dir{})
  28. var _ = fs.NodeSetattrer(&Dir{})
  29. var _ = fs.NodeGetxattrer(&Dir{})
  30. var _ = fs.NodeSetxattrer(&Dir{})
  31. var _ = fs.NodeRemovexattrer(&Dir{})
  32. var _ = fs.NodeListxattrer(&Dir{})
  33. var _ = fs.NodeForgetter(&Dir{})
  34. func (dir *Dir) Attr(ctx context.Context, attr *fuse.Attr) error {
  35. // https://github.com/bazil/fuse/issues/196
  36. attr.Valid = time.Second
  37. if dir.FullPath() == dir.wfs.option.FilerMountRootPath {
  38. dir.setRootDirAttributes(attr)
  39. glog.V(3).Infof("root dir Attr %s, attr: %+v", dir.FullPath(), attr)
  40. return nil
  41. }
  42. if err := dir.maybeLoadEntry(); err != nil {
  43. glog.V(3).Infof("dir Attr %s,err: %+v", dir.FullPath(), err)
  44. return err
  45. }
  46. attr.Inode = util.FullPath(dir.FullPath()).AsInode()
  47. attr.Mode = os.FileMode(dir.entry.Attributes.FileMode) | os.ModeDir
  48. attr.Mtime = time.Unix(dir.entry.Attributes.Mtime, 0)
  49. attr.Crtime = time.Unix(dir.entry.Attributes.Crtime, 0)
  50. attr.Gid = dir.entry.Attributes.Gid
  51. attr.Uid = dir.entry.Attributes.Uid
  52. glog.V(4).Infof("dir Attr %s, attr: %+v", dir.FullPath(), attr)
  53. return nil
  54. }
  55. func (dir *Dir) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
  56. glog.V(4).Infof("dir Getxattr %s", dir.FullPath())
  57. if err := dir.maybeLoadEntry(); err != nil {
  58. return err
  59. }
  60. return getxattr(dir.entry, req, resp)
  61. }
  62. func (dir *Dir) setRootDirAttributes(attr *fuse.Attr) {
  63. attr.Inode = 1 // filer2.FullPath(dir.Path).AsInode()
  64. attr.Valid = time.Hour
  65. attr.Uid = dir.wfs.option.MountUid
  66. attr.Gid = dir.wfs.option.MountGid
  67. attr.Mode = dir.wfs.option.MountMode
  68. attr.Crtime = dir.wfs.option.MountCtime
  69. attr.Ctime = dir.wfs.option.MountCtime
  70. attr.Mtime = dir.wfs.option.MountMtime
  71. attr.Atime = dir.wfs.option.MountMtime
  72. attr.BlockSize = 1024 * 1024
  73. }
  74. func (dir *Dir) newFile(name string, entry *filer_pb.Entry) fs.Node {
  75. return &File{
  76. Name: name,
  77. dir: dir,
  78. wfs: dir.wfs,
  79. entry: entry,
  80. entryViewCache: nil,
  81. }
  82. }
  83. func (dir *Dir) newDirectory(fullpath util.FullPath, entry *filer_pb.Entry) fs.Node {
  84. return &Dir{name: entry.Name, wfs: dir.wfs, entry: entry, parent: dir}
  85. }
  86. func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest,
  87. resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
  88. request := &filer_pb.CreateEntryRequest{
  89. Directory: dir.FullPath(),
  90. Entry: &filer_pb.Entry{
  91. Name: req.Name,
  92. IsDirectory: req.Mode&os.ModeDir > 0,
  93. Attributes: &filer_pb.FuseAttributes{
  94. Mtime: time.Now().Unix(),
  95. Crtime: time.Now().Unix(),
  96. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  97. Uid: req.Uid,
  98. Gid: req.Gid,
  99. Collection: dir.wfs.option.Collection,
  100. Replication: dir.wfs.option.Replication,
  101. TtlSec: dir.wfs.option.TtlSec,
  102. },
  103. },
  104. OExcl: req.Flags&fuse.OpenExclusive != 0,
  105. }
  106. glog.V(1).Infof("create %s/%s: %v", dir.FullPath(), req.Name, req.Flags)
  107. if err := dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  108. if err := filer_pb.CreateEntry(client, request); err != nil {
  109. if strings.Contains(err.Error(), "EEXIST") {
  110. return fuse.EEXIST
  111. }
  112. return fuse.EIO
  113. }
  114. dir.wfs.metaCache.InsertEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
  115. return nil
  116. }); 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), request.Entry)
  122. return node, nil, nil
  123. }
  124. node = dir.newFile(req.Name, request.Entry)
  125. file := node.(*File)
  126. file.isOpen++
  127. fh := dir.wfs.AcquireHandle(file, req.Uid, req.Gid)
  128. return file, fh, nil
  129. }
  130. func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
  131. glog.V(4).Infof("mkdir %s: %s", dir.FullPath(), req.Name)
  132. newEntry := &filer_pb.Entry{
  133. Name: req.Name,
  134. IsDirectory: true,
  135. Attributes: &filer_pb.FuseAttributes{
  136. Mtime: time.Now().Unix(),
  137. Crtime: time.Now().Unix(),
  138. FileMode: uint32(req.Mode &^ dir.wfs.option.Umask),
  139. Uid: req.Uid,
  140. Gid: req.Gid,
  141. },
  142. }
  143. err := dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  144. request := &filer_pb.CreateEntryRequest{
  145. Directory: dir.FullPath(),
  146. Entry: newEntry,
  147. }
  148. glog.V(1).Infof("mkdir: %v", request)
  149. if err := filer_pb.CreateEntry(client, request); err != nil {
  150. glog.V(0).Infof("mkdir %s/%s: %v", dir.FullPath(), req.Name, err)
  151. return err
  152. }
  153. dir.wfs.metaCache.InsertEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
  154. return nil
  155. })
  156. if err == nil {
  157. node := dir.newDirectory(util.NewFullPath(dir.FullPath(), req.Name), newEntry)
  158. return node, nil
  159. }
  160. glog.V(0).Infof("mkdir %s/%s: %v", dir.FullPath(), req.Name, err)
  161. return nil, fuse.EIO
  162. }
  163. func (dir *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (node fs.Node, err error) {
  164. glog.V(4).Infof("dir Lookup %s: %s by %s", dir.FullPath(), req.Name, req.Header.String())
  165. fullFilePath := util.NewFullPath(dir.FullPath(), req.Name)
  166. cachedEntry, cacheErr := dir.wfs.metaCache.FindEntry(context.Background(), fullFilePath)
  167. if cacheErr == filer_pb.ErrNotFound {
  168. return nil, fuse.ENOENT
  169. }
  170. entry := cachedEntry.ToProtoEntry()
  171. if entry == nil {
  172. // glog.V(3).Infof("dir Lookup cache miss %s", fullFilePath)
  173. entry, err = filer_pb.GetEntry(dir.wfs, fullFilePath)
  174. if err != nil {
  175. glog.V(1).Infof("dir GetEntry %s: %v", fullFilePath, err)
  176. return nil, fuse.ENOENT
  177. }
  178. } else {
  179. glog.V(4).Infof("dir Lookup cache hit %s", fullFilePath)
  180. }
  181. if entry != nil {
  182. if entry.IsDirectory {
  183. node = dir.newDirectory(fullFilePath, entry)
  184. } else {
  185. node = dir.newFile(req.Name, entry)
  186. }
  187. // resp.EntryValid = time.Second
  188. resp.Attr.Inode = fullFilePath.AsInode()
  189. resp.Attr.Valid = time.Second
  190. resp.Attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  191. resp.Attr.Crtime = time.Unix(entry.Attributes.Crtime, 0)
  192. resp.Attr.Mode = os.FileMode(entry.Attributes.FileMode)
  193. resp.Attr.Gid = entry.Attributes.Gid
  194. resp.Attr.Uid = entry.Attributes.Uid
  195. return node, nil
  196. }
  197. glog.V(4).Infof("not found dir GetEntry %s: %v", fullFilePath, err)
  198. return nil, fuse.ENOENT
  199. }
  200. func (dir *Dir) ReadDirAll(ctx context.Context) (ret []fuse.Dirent, err error) {
  201. glog.V(3).Infof("dir ReadDirAll %s", dir.FullPath())
  202. processEachEntryFn := func(entry *filer_pb.Entry, isLast bool) error {
  203. fullpath := util.NewFullPath(dir.FullPath(), entry.Name)
  204. inode := fullpath.AsInode()
  205. if entry.IsDirectory {
  206. dirent := fuse.Dirent{Inode: inode, Name: entry.Name, Type: fuse.DT_Dir}
  207. ret = append(ret, dirent)
  208. } else {
  209. dirent := fuse.Dirent{Inode: inode, Name: entry.Name, Type: fuse.DT_File}
  210. ret = append(ret, dirent)
  211. }
  212. return nil
  213. }
  214. listedEntries, listErr := dir.wfs.metaCache.ListDirectoryEntries(context.Background(), util.FullPath(dir.FullPath()), "", false, int(dir.wfs.option.DirListCacheLimit))
  215. if listErr != nil {
  216. glog.Errorf("list meta cache: %v", listErr)
  217. return nil, fuse.EIO
  218. }
  219. for _, cachedEntry := range listedEntries {
  220. processEachEntryFn(cachedEntry.ToProtoEntry(), false)
  221. }
  222. return
  223. }
  224. func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
  225. if !req.Dir {
  226. return dir.removeOneFile(req)
  227. }
  228. return dir.removeFolder(req)
  229. }
  230. func (dir *Dir) removeOneFile(req *fuse.RemoveRequest) error {
  231. filePath := util.NewFullPath(dir.FullPath(), req.Name)
  232. entry, err := filer_pb.GetEntry(dir.wfs, filePath)
  233. if err != nil {
  234. return err
  235. }
  236. if entry == nil {
  237. return nil
  238. }
  239. dir.wfs.deleteFileChunks(entry.Chunks)
  240. dir.wfs.metaCache.DeleteEntry(context.Background(), filePath)
  241. glog.V(3).Infof("remove file: %v", req)
  242. err = filer_pb.Remove(dir.wfs, dir.FullPath(), req.Name, false, false, false)
  243. if err != nil {
  244. glog.V(3).Infof("not found remove file %s/%s: %v", dir.FullPath(), req.Name, err)
  245. return fuse.ENOENT
  246. }
  247. return nil
  248. }
  249. func (dir *Dir) removeFolder(req *fuse.RemoveRequest) error {
  250. t := util.NewFullPath(dir.FullPath(), req.Name)
  251. dir.wfs.metaCache.DeleteEntry(context.Background(), t)
  252. glog.V(3).Infof("remove directory entry: %v", req)
  253. err := filer_pb.Remove(dir.wfs, dir.FullPath(), req.Name, true, false, false)
  254. if err != nil {
  255. glog.V(3).Infof("not found remove %s/%s: %v", dir.FullPath(), req.Name, err)
  256. return fuse.ENOENT
  257. }
  258. return nil
  259. }
  260. func (dir *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  261. glog.V(3).Infof("%v dir setattr %+v", dir.FullPath(), req)
  262. if err := dir.maybeLoadEntry(); err != nil {
  263. return err
  264. }
  265. if req.Valid.Mode() {
  266. dir.entry.Attributes.FileMode = uint32(req.Mode)
  267. }
  268. if req.Valid.Uid() {
  269. dir.entry.Attributes.Uid = req.Uid
  270. }
  271. if req.Valid.Gid() {
  272. dir.entry.Attributes.Gid = req.Gid
  273. }
  274. if req.Valid.Mtime() {
  275. dir.entry.Attributes.Mtime = req.Mtime.Unix()
  276. }
  277. return dir.saveEntry()
  278. }
  279. func (dir *Dir) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  280. glog.V(4).Infof("dir Setxattr %s: %s", dir.FullPath(), req.Name)
  281. if err := dir.maybeLoadEntry(); err != nil {
  282. return err
  283. }
  284. if err := setxattr(dir.entry, req); err != nil {
  285. return err
  286. }
  287. return dir.saveEntry()
  288. }
  289. func (dir *Dir) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  290. glog.V(4).Infof("dir Removexattr %s: %s", dir.FullPath(), req.Name)
  291. if err := dir.maybeLoadEntry(); err != nil {
  292. return err
  293. }
  294. if err := removexattr(dir.entry, req); err != nil {
  295. return err
  296. }
  297. return dir.saveEntry()
  298. }
  299. func (dir *Dir) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  300. glog.V(4).Infof("dir Listxattr %s", dir.FullPath())
  301. if err := dir.maybeLoadEntry(); err != nil {
  302. return err
  303. }
  304. if err := listxattr(dir.entry, req, resp); err != nil {
  305. return err
  306. }
  307. return nil
  308. }
  309. func (dir *Dir) Forget() {
  310. glog.V(3).Infof("Forget dir %s", dir.FullPath())
  311. }
  312. func (dir *Dir) maybeLoadEntry() error {
  313. if dir.entry == nil {
  314. parentDirPath, name := util.FullPath(dir.FullPath()).DirAndName()
  315. entry, err := dir.wfs.maybeLoadEntry(parentDirPath, name)
  316. if err != nil {
  317. return err
  318. }
  319. dir.entry = entry
  320. }
  321. return nil
  322. }
  323. func (dir *Dir) saveEntry() error {
  324. parentDir, name := util.FullPath(dir.FullPath()).DirAndName()
  325. return dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  326. request := &filer_pb.UpdateEntryRequest{
  327. Directory: parentDir,
  328. Entry: dir.entry,
  329. }
  330. glog.V(1).Infof("save dir entry: %v", request)
  331. _, err := client.UpdateEntry(context.Background(), request)
  332. if err != nil {
  333. glog.V(0).Infof("UpdateEntry dir %s/%s: %v", parentDir, name, err)
  334. return fuse.EIO
  335. }
  336. dir.wfs.metaCache.UpdateEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
  337. return nil
  338. })
  339. }
  340. func (dir *Dir) FullPath() string {
  341. var parts []string
  342. for p := dir; p != nil; p = p.parent {
  343. if strings.HasPrefix(p.name, "/") {
  344. if len(p.name) > 1 {
  345. parts = append(parts, p.name[1:])
  346. }
  347. } else {
  348. parts = append(parts, p.name)
  349. }
  350. }
  351. if len(parts) == 0 {
  352. return "/"
  353. }
  354. var buf bytes.Buffer
  355. for i := len(parts) - 1; i >= 0; i-- {
  356. buf.WriteString("/")
  357. buf.WriteString(parts[i])
  358. }
  359. return buf.String()
  360. }