caching_fs.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cachingfs
  15. import (
  16. "crypto/rand"
  17. "fmt"
  18. "io"
  19. "os"
  20. "time"
  21. "golang.org/x/net/context"
  22. "github.com/jacobsa/fuse"
  23. "github.com/jacobsa/fuse/fuseops"
  24. "github.com/jacobsa/fuse/fuseutil"
  25. "github.com/jacobsa/syncutil"
  26. )
  27. const (
  28. // Sizes of the files according to the file system.
  29. FooSize = 123
  30. BarSize = 456
  31. )
  32. // A file system with a fixed structure that looks like this:
  33. //
  34. // foo
  35. // dir/
  36. // bar
  37. //
  38. // The file system is configured with durations that specify how long to allow
  39. // inode entries and attributes to be cached, used when responding to fuse
  40. // requests. It also exposes methods for renumbering inodes and updating mtimes
  41. // that are useful in testing that these durations are honored.
  42. //
  43. // Each file responds to reads with random contents. SetKeepCache can be used
  44. // to control whether the response to OpenFileOp tells the kernel to keep the
  45. // file's data in the page cache or not.
  46. type CachingFS interface {
  47. fuseutil.FileSystem
  48. // Return the current inode ID of the file/directory with the given name.
  49. FooID() fuseops.InodeID
  50. DirID() fuseops.InodeID
  51. BarID() fuseops.InodeID
  52. // Cause the inode IDs to change to values that have never before been used.
  53. RenumberInodes()
  54. // Cause further queries for the attributes of inodes to use the supplied
  55. // time as the inode's mtime.
  56. SetMtime(mtime time.Time)
  57. // Instruct the file system whether or not to reply to OpenFileOp with
  58. // FOPEN_KEEP_CACHE set.
  59. SetKeepCache(keep bool)
  60. }
  61. // Create a file system that issues cacheable responses according to the
  62. // following rules:
  63. //
  64. // * LookUpInodeResponse.Entry.EntryExpiration is set according to
  65. // lookupEntryTimeout.
  66. //
  67. // * GetInodeAttributesResponse.AttributesExpiration is set according to
  68. // getattrTimeout.
  69. //
  70. // * Nothing else is marked cacheable. (In particular, the attributes
  71. // returned by LookUpInode are not cacheable.)
  72. //
  73. func NewCachingFS(
  74. lookupEntryTimeout time.Duration,
  75. getattrTimeout time.Duration) (fs CachingFS, err error) {
  76. roundUp := func(n fuseops.InodeID) fuseops.InodeID {
  77. return numInodes * ((n + numInodes - 1) / numInodes)
  78. }
  79. cfs := &cachingFS{
  80. lookupEntryTimeout: lookupEntryTimeout,
  81. getattrTimeout: getattrTimeout,
  82. baseID: roundUp(fuseops.RootInodeID + 1),
  83. mtime: time.Now(),
  84. }
  85. cfs.mu = syncutil.NewInvariantMutex(cfs.checkInvariants)
  86. fs = cfs
  87. return
  88. }
  89. const (
  90. // Inode IDs are issued such that "foo" always receives an ID that is
  91. // congruent to fooOffset modulo numInodes, etc.
  92. fooOffset = iota
  93. dirOffset
  94. barOffset
  95. numInodes
  96. )
  97. type cachingFS struct {
  98. fuseutil.NotImplementedFileSystem
  99. /////////////////////////
  100. // Constant data
  101. /////////////////////////
  102. lookupEntryTimeout time.Duration
  103. getattrTimeout time.Duration
  104. /////////////////////////
  105. // Mutable state
  106. /////////////////////////
  107. mu syncutil.InvariantMutex
  108. // GUARDED_BY(mu)
  109. keepPageCache bool
  110. // The current ID of the lowest numbered non-root inode.
  111. //
  112. // INVARIANT: baseID > fuseops.RootInodeID
  113. // INVARIANT: baseID % numInodes == 0
  114. //
  115. // GUARDED_BY(mu)
  116. baseID fuseops.InodeID
  117. // GUARDED_BY(mu)
  118. mtime time.Time
  119. }
  120. ////////////////////////////////////////////////////////////////////////
  121. // Helpers
  122. ////////////////////////////////////////////////////////////////////////
  123. func (fs *cachingFS) checkInvariants() {
  124. // INVARIANT: baseID > fuseops.RootInodeID
  125. // INVARIANT: baseID % numInodes == 0
  126. if fs.baseID <= fuseops.RootInodeID || fs.baseID%numInodes != 0 {
  127. panic(fmt.Sprintf("Bad baseID: %v", fs.baseID))
  128. }
  129. }
  130. // LOCKS_REQUIRED(fs.mu)
  131. func (fs *cachingFS) fooID() fuseops.InodeID {
  132. return fs.baseID + fooOffset
  133. }
  134. // LOCKS_REQUIRED(fs.mu)
  135. func (fs *cachingFS) dirID() fuseops.InodeID {
  136. return fs.baseID + dirOffset
  137. }
  138. // LOCKS_REQUIRED(fs.mu)
  139. func (fs *cachingFS) barID() fuseops.InodeID {
  140. return fs.baseID + barOffset
  141. }
  142. // LOCKS_REQUIRED(fs.mu)
  143. func (fs *cachingFS) rootAttrs() fuseops.InodeAttributes {
  144. return fuseops.InodeAttributes{
  145. Mode: os.ModeDir | 0777,
  146. Mtime: fs.mtime,
  147. }
  148. }
  149. // LOCKS_REQUIRED(fs.mu)
  150. func (fs *cachingFS) fooAttrs() fuseops.InodeAttributes {
  151. return fuseops.InodeAttributes{
  152. Nlink: 1,
  153. Size: FooSize,
  154. Mode: 0777,
  155. Mtime: fs.mtime,
  156. }
  157. }
  158. // LOCKS_REQUIRED(fs.mu)
  159. func (fs *cachingFS) dirAttrs() fuseops.InodeAttributes {
  160. return fuseops.InodeAttributes{
  161. Nlink: 1,
  162. Mode: os.ModeDir | 0777,
  163. Mtime: fs.mtime,
  164. }
  165. }
  166. // LOCKS_REQUIRED(fs.mu)
  167. func (fs *cachingFS) barAttrs() fuseops.InodeAttributes {
  168. return fuseops.InodeAttributes{
  169. Nlink: 1,
  170. Size: BarSize,
  171. Mode: 0777,
  172. Mtime: fs.mtime,
  173. }
  174. }
  175. ////////////////////////////////////////////////////////////////////////
  176. // Public interface
  177. ////////////////////////////////////////////////////////////////////////
  178. // LOCKS_EXCLUDED(fs.mu)
  179. func (fs *cachingFS) FooID() fuseops.InodeID {
  180. fs.mu.Lock()
  181. defer fs.mu.Unlock()
  182. return fs.fooID()
  183. }
  184. // LOCKS_EXCLUDED(fs.mu)
  185. func (fs *cachingFS) DirID() fuseops.InodeID {
  186. fs.mu.Lock()
  187. defer fs.mu.Unlock()
  188. return fs.dirID()
  189. }
  190. // LOCKS_EXCLUDED(fs.mu)
  191. func (fs *cachingFS) BarID() fuseops.InodeID {
  192. fs.mu.Lock()
  193. defer fs.mu.Unlock()
  194. return fs.barID()
  195. }
  196. // LOCKS_EXCLUDED(fs.mu)
  197. func (fs *cachingFS) RenumberInodes() {
  198. fs.mu.Lock()
  199. defer fs.mu.Unlock()
  200. fs.baseID += numInodes
  201. }
  202. // LOCKS_EXCLUDED(fs.mu)
  203. func (fs *cachingFS) SetMtime(mtime time.Time) {
  204. fs.mu.Lock()
  205. defer fs.mu.Unlock()
  206. fs.mtime = mtime
  207. }
  208. // LOCKS_EXCLUDED(fs.mu)
  209. func (fs *cachingFS) SetKeepCache(keep bool) {
  210. fs.mu.Lock()
  211. defer fs.mu.Unlock()
  212. fs.keepPageCache = keep
  213. }
  214. ////////////////////////////////////////////////////////////////////////
  215. // FileSystem methods
  216. ////////////////////////////////////////////////////////////////////////
  217. func (fs *cachingFS) StatFS(
  218. ctx context.Context,
  219. op *fuseops.StatFSOp) (err error) {
  220. return
  221. }
  222. // LOCKS_EXCLUDED(fs.mu)
  223. func (fs *cachingFS) LookUpInode(
  224. ctx context.Context,
  225. op *fuseops.LookUpInodeOp) (err error) {
  226. fs.mu.Lock()
  227. defer fs.mu.Unlock()
  228. // Find the ID and attributes.
  229. var id fuseops.InodeID
  230. var attrs fuseops.InodeAttributes
  231. switch op.Name {
  232. case "foo":
  233. // Parent must be the root.
  234. if op.Parent != fuseops.RootInodeID {
  235. err = fuse.ENOENT
  236. return
  237. }
  238. id = fs.fooID()
  239. attrs = fs.fooAttrs()
  240. case "dir":
  241. // Parent must be the root.
  242. if op.Parent != fuseops.RootInodeID {
  243. err = fuse.ENOENT
  244. return
  245. }
  246. id = fs.dirID()
  247. attrs = fs.dirAttrs()
  248. case "bar":
  249. // Parent must be dir.
  250. if op.Parent == fuseops.RootInodeID || op.Parent%numInodes != dirOffset {
  251. err = fuse.ENOENT
  252. return
  253. }
  254. id = fs.barID()
  255. attrs = fs.barAttrs()
  256. default:
  257. err = fuse.ENOENT
  258. return
  259. }
  260. // Fill in the response.
  261. op.Entry.Child = id
  262. op.Entry.Attributes = attrs
  263. op.Entry.EntryExpiration = time.Now().Add(fs.lookupEntryTimeout)
  264. return
  265. }
  266. // LOCKS_EXCLUDED(fs.mu)
  267. func (fs *cachingFS) GetInodeAttributes(
  268. ctx context.Context,
  269. op *fuseops.GetInodeAttributesOp) (err error) {
  270. fs.mu.Lock()
  271. defer fs.mu.Unlock()
  272. // Figure out which inode the request is for.
  273. var attrs fuseops.InodeAttributes
  274. switch {
  275. case op.Inode == fuseops.RootInodeID:
  276. attrs = fs.rootAttrs()
  277. case op.Inode%numInodes == fooOffset:
  278. attrs = fs.fooAttrs()
  279. case op.Inode%numInodes == dirOffset:
  280. attrs = fs.dirAttrs()
  281. case op.Inode%numInodes == barOffset:
  282. attrs = fs.barAttrs()
  283. }
  284. // Fill in the response.
  285. op.Attributes = attrs
  286. op.AttributesExpiration = time.Now().Add(fs.getattrTimeout)
  287. return
  288. }
  289. func (fs *cachingFS) OpenDir(
  290. ctx context.Context,
  291. op *fuseops.OpenDirOp) (err error) {
  292. return
  293. }
  294. func (fs *cachingFS) OpenFile(
  295. ctx context.Context,
  296. op *fuseops.OpenFileOp) (err error) {
  297. fs.mu.Lock()
  298. defer fs.mu.Unlock()
  299. op.KeepPageCache = fs.keepPageCache
  300. return
  301. }
  302. func (fs *cachingFS) ReadFile(
  303. ctx context.Context,
  304. op *fuseops.ReadFileOp) (err error) {
  305. op.BytesRead, err = io.ReadFull(rand.Reader, op.Dst)
  306. return
  307. }