netfs_library.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =================================
  3. Network Filesystem Helper Library
  4. =================================
  5. .. Contents:
  6. - Overview.
  7. - Per-inode context.
  8. - Inode context helper functions.
  9. - Buffered read helpers.
  10. - Read helper functions.
  11. - Read helper structures.
  12. - Read helper operations.
  13. - Read helper procedure.
  14. - Read helper cache API.
  15. Overview
  16. ========
  17. The network filesystem helper library is a set of functions designed to aid a
  18. network filesystem in implementing VM/VFS operations. For the moment, that
  19. just includes turning various VM buffered read operations into requests to read
  20. from the server. The helper library, however, can also interpose other
  21. services, such as local caching or local data encryption.
  22. Note that the library module doesn't link against local caching directly, so
  23. access must be provided by the netfs.
  24. Per-Inode Context
  25. =================
  26. The network filesystem helper library needs a place to store a bit of state for
  27. its use on each netfs inode it is helping to manage. To this end, a context
  28. structure is defined::
  29. struct netfs_inode {
  30. struct inode inode;
  31. const struct netfs_request_ops *ops;
  32. struct fscache_cookie *cache;
  33. };
  34. A network filesystem that wants to use netfs lib must place one of these in its
  35. inode wrapper struct instead of the VFS ``struct inode``. This can be done in
  36. a way similar to the following::
  37. struct my_inode {
  38. struct netfs_inode netfs; /* Netfslib context and vfs inode */
  39. ...
  40. };
  41. This allows netfslib to find its state by using ``container_of()`` from the
  42. inode pointer, thereby allowing the netfslib helper functions to be pointed to
  43. directly by the VFS/VM operation tables.
  44. The structure contains the following fields:
  45. * ``inode``
  46. The VFS inode structure.
  47. * ``ops``
  48. The set of operations provided by the network filesystem to netfslib.
  49. * ``cache``
  50. Local caching cookie, or NULL if no caching is enabled. This field does not
  51. exist if fscache is disabled.
  52. Inode Context Helper Functions
  53. ------------------------------
  54. To help deal with the per-inode context, a number helper functions are
  55. provided. Firstly, a function to perform basic initialisation on a context and
  56. set the operations table pointer::
  57. void netfs_inode_init(struct netfs_inode *ctx,
  58. const struct netfs_request_ops *ops);
  59. then a function to cast from the VFS inode structure to the netfs context::
  60. struct netfs_inode *netfs_node(struct inode *inode);
  61. and finally, a function to get the cache cookie pointer from the context
  62. attached to an inode (or NULL if fscache is disabled)::
  63. struct fscache_cookie *netfs_i_cookie(struct netfs_inode *ctx);
  64. Buffered Read Helpers
  65. =====================
  66. The library provides a set of read helpers that handle the ->read_folio(),
  67. ->readahead() and much of the ->write_begin() VM operations and translate them
  68. into a common call framework.
  69. The following services are provided:
  70. * Handle folios that span multiple pages.
  71. * Insulate the netfs from VM interface changes.
  72. * Allow the netfs to arbitrarily split reads up into pieces, even ones that
  73. don't match folio sizes or folio alignments and that may cross folios.
  74. * Allow the netfs to expand a readahead request in both directions to meet its
  75. needs.
  76. * Allow the netfs to partially fulfil a read, which will then be resubmitted.
  77. * Handle local caching, allowing cached data and server-read data to be
  78. interleaved for a single request.
  79. * Handle clearing of bufferage that isn't on the server.
  80. * Handle retrying of reads that failed, switching reads from the cache to the
  81. server as necessary.
  82. * In the future, this is a place that other services can be performed, such as
  83. local encryption of data to be stored remotely or in the cache.
  84. From the network filesystem, the helpers require a table of operations. This
  85. includes a mandatory method to issue a read operation along with a number of
  86. optional methods.
  87. Read Helper Functions
  88. ---------------------
  89. Three read helpers are provided::
  90. void netfs_readahead(struct readahead_control *ractl);
  91. int netfs_read_folio(struct file *file,
  92. struct folio *folio);
  93. int netfs_write_begin(struct netfs_inode *ctx,
  94. struct file *file,
  95. struct address_space *mapping,
  96. loff_t pos,
  97. unsigned int len,
  98. struct folio **_folio,
  99. void **_fsdata);
  100. Each corresponds to a VM address space operation. These operations use the
  101. state in the per-inode context.
  102. For ->readahead() and ->read_folio(), the network filesystem just point directly
  103. at the corresponding read helper; whereas for ->write_begin(), it may be a
  104. little more complicated as the network filesystem might want to flush
  105. conflicting writes or track dirty data and needs to put the acquired folio if
  106. an error occurs after calling the helper.
  107. The helpers manage the read request, calling back into the network filesystem
  108. through the supplied table of operations. Waits will be performed as
  109. necessary before returning for helpers that are meant to be synchronous.
  110. If an error occurs, the ->free_request() will be called to clean up the
  111. netfs_io_request struct allocated. If some parts of the request are in
  112. progress when an error occurs, the request will get partially completed if
  113. sufficient data is read.
  114. Additionally, there is::
  115. * void netfs_subreq_terminated(struct netfs_io_subrequest *subreq,
  116. ssize_t transferred_or_error,
  117. bool was_async);
  118. which should be called to complete a read subrequest. This is given the number
  119. of bytes transferred or a negative error code, plus a flag indicating whether
  120. the operation was asynchronous (ie. whether the follow-on processing can be
  121. done in the current context, given this may involve sleeping).
  122. Read Helper Structures
  123. ----------------------
  124. The read helpers make use of a couple of structures to maintain the state of
  125. the read. The first is a structure that manages a read request as a whole::
  126. struct netfs_io_request {
  127. struct inode *inode;
  128. struct address_space *mapping;
  129. struct netfs_cache_resources cache_resources;
  130. void *netfs_priv;
  131. loff_t start;
  132. size_t len;
  133. loff_t i_size;
  134. const struct netfs_request_ops *netfs_ops;
  135. unsigned int debug_id;
  136. ...
  137. };
  138. The above fields are the ones the netfs can use. They are:
  139. * ``inode``
  140. * ``mapping``
  141. The inode and the address space of the file being read from. The mapping
  142. may or may not point to inode->i_data.
  143. * ``cache_resources``
  144. Resources for the local cache to use, if present.
  145. * ``netfs_priv``
  146. The network filesystem's private data. The value for this can be passed in
  147. to the helper functions or set during the request.
  148. * ``start``
  149. * ``len``
  150. The file position of the start of the read request and the length. These
  151. may be altered by the ->expand_readahead() op.
  152. * ``i_size``
  153. The size of the file at the start of the request.
  154. * ``netfs_ops``
  155. A pointer to the operation table. The value for this is passed into the
  156. helper functions.
  157. * ``debug_id``
  158. A number allocated to this operation that can be displayed in trace lines
  159. for reference.
  160. The second structure is used to manage individual slices of the overall read
  161. request::
  162. struct netfs_io_subrequest {
  163. struct netfs_io_request *rreq;
  164. loff_t start;
  165. size_t len;
  166. size_t transferred;
  167. unsigned long flags;
  168. unsigned short debug_index;
  169. ...
  170. };
  171. Each subrequest is expected to access a single source, though the helpers will
  172. handle falling back from one source type to another. The members are:
  173. * ``rreq``
  174. A pointer to the read request.
  175. * ``start``
  176. * ``len``
  177. The file position of the start of this slice of the read request and the
  178. length.
  179. * ``transferred``
  180. The amount of data transferred so far of the length of this slice. The
  181. network filesystem or cache should start the operation this far into the
  182. slice. If a short read occurs, the helpers will call again, having updated
  183. this to reflect the amount read so far.
  184. * ``flags``
  185. Flags pertaining to the read. There are two of interest to the filesystem
  186. or cache:
  187. * ``NETFS_SREQ_CLEAR_TAIL``
  188. This can be set to indicate that the remainder of the slice, from
  189. transferred to len, should be cleared.
  190. * ``NETFS_SREQ_SEEK_DATA_READ``
  191. This is a hint to the cache that it might want to try skipping ahead to
  192. the next data (ie. using SEEK_DATA).
  193. * ``debug_index``
  194. A number allocated to this slice that can be displayed in trace lines for
  195. reference.
  196. Read Helper Operations
  197. ----------------------
  198. The network filesystem must provide the read helpers with a table of operations
  199. through which it can issue requests and negotiate::
  200. struct netfs_request_ops {
  201. void (*init_request)(struct netfs_io_request *rreq, struct file *file);
  202. void (*free_request)(struct netfs_io_request *rreq);
  203. void (*expand_readahead)(struct netfs_io_request *rreq);
  204. bool (*clamp_length)(struct netfs_io_subrequest *subreq);
  205. void (*issue_read)(struct netfs_io_subrequest *subreq);
  206. bool (*is_still_valid)(struct netfs_io_request *rreq);
  207. int (*check_write_begin)(struct file *file, loff_t pos, unsigned len,
  208. struct folio **foliop, void **_fsdata);
  209. void (*done)(struct netfs_io_request *rreq);
  210. };
  211. The operations are as follows:
  212. * ``init_request()``
  213. [Optional] This is called to initialise the request structure. It is given
  214. the file for reference.
  215. * ``free_request()``
  216. [Optional] This is called as the request is being deallocated so that the
  217. filesystem can clean up any state it has attached there.
  218. * ``expand_readahead()``
  219. [Optional] This is called to allow the filesystem to expand the size of a
  220. readahead read request. The filesystem gets to expand the request in both
  221. directions, though it's not permitted to reduce it as the numbers may
  222. represent an allocation already made. If local caching is enabled, it gets
  223. to expand the request first.
  224. Expansion is communicated by changing ->start and ->len in the request
  225. structure. Note that if any change is made, ->len must be increased by at
  226. least as much as ->start is reduced.
  227. * ``clamp_length()``
  228. [Optional] This is called to allow the filesystem to reduce the size of a
  229. subrequest. The filesystem can use this, for example, to chop up a request
  230. that has to be split across multiple servers or to put multiple reads in
  231. flight.
  232. This should return 0 on success and an error code on error.
  233. * ``issue_read()``
  234. [Required] The helpers use this to dispatch a subrequest to the server for
  235. reading. In the subrequest, ->start, ->len and ->transferred indicate what
  236. data should be read from the server.
  237. There is no return value; the netfs_subreq_terminated() function should be
  238. called to indicate whether or not the operation succeeded and how much data
  239. it transferred. The filesystem also should not deal with setting folios
  240. uptodate, unlocking them or dropping their refs - the helpers need to deal
  241. with this as they have to coordinate with copying to the local cache.
  242. Note that the helpers have the folios locked, but not pinned. It is
  243. possible to use the ITER_XARRAY iov iterator to refer to the range of the
  244. inode that is being operated upon without the need to allocate large bvec
  245. tables.
  246. * ``is_still_valid()``
  247. [Optional] This is called to find out if the data just read from the local
  248. cache is still valid. It should return true if it is still valid and false
  249. if not. If it's not still valid, it will be reread from the server.
  250. * ``check_write_begin()``
  251. [Optional] This is called from the netfs_write_begin() helper once it has
  252. allocated/grabbed the folio to be modified to allow the filesystem to flush
  253. conflicting state before allowing it to be modified.
  254. It may unlock and discard the folio it was given and set the caller's folio
  255. pointer to NULL. It should return 0 if everything is now fine (``*foliop``
  256. left set) or the op should be retried (``*foliop`` cleared) and any other
  257. error code to abort the operation.
  258. * ``done``
  259. [Optional] This is called after the folios in the request have all been
  260. unlocked (and marked uptodate if applicable).
  261. Read Helper Procedure
  262. ---------------------
  263. The read helpers work by the following general procedure:
  264. * Set up the request.
  265. * For readahead, allow the local cache and then the network filesystem to
  266. propose expansions to the read request. This is then proposed to the VM.
  267. If the VM cannot fully perform the expansion, a partially expanded read will
  268. be performed, though this may not get written to the cache in its entirety.
  269. * Loop around slicing chunks off of the request to form subrequests:
  270. * If a local cache is present, it gets to do the slicing, otherwise the
  271. helpers just try to generate maximal slices.
  272. * The network filesystem gets to clamp the size of each slice if it is to be
  273. the source. This allows rsize and chunking to be implemented.
  274. * The helpers issue a read from the cache or a read from the server or just
  275. clears the slice as appropriate.
  276. * The next slice begins at the end of the last one.
  277. * As slices finish being read, they terminate.
  278. * When all the subrequests have terminated, the subrequests are assessed and
  279. any that are short or have failed are reissued:
  280. * Failed cache requests are issued against the server instead.
  281. * Failed server requests just fail.
  282. * Short reads against either source will be reissued against that source
  283. provided they have transferred some more data:
  284. * The cache may need to skip holes that it can't do DIO from.
  285. * If NETFS_SREQ_CLEAR_TAIL was set, a short read will be cleared to the
  286. end of the slice instead of reissuing.
  287. * Once the data is read, the folios that have been fully read/cleared:
  288. * Will be marked uptodate.
  289. * If a cache is present, will be marked with PG_fscache.
  290. * Unlocked
  291. * Any folios that need writing to the cache will then have DIO writes issued.
  292. * Synchronous operations will wait for reading to be complete.
  293. * Writes to the cache will proceed asynchronously and the folios will have the
  294. PG_fscache mark removed when that completes.
  295. * The request structures will be cleaned up when everything has completed.
  296. Read Helper Cache API
  297. ---------------------
  298. When implementing a local cache to be used by the read helpers, two things are
  299. required: some way for the network filesystem to initialise the caching for a
  300. read request and a table of operations for the helpers to call.
  301. To begin a cache operation on an fscache object, the following function is
  302. called::
  303. int fscache_begin_read_operation(struct netfs_io_request *rreq,
  304. struct fscache_cookie *cookie);
  305. passing in the request pointer and the cookie corresponding to the file. This
  306. fills in the cache resources mentioned below.
  307. The netfs_io_request object contains a place for the cache to hang its
  308. state::
  309. struct netfs_cache_resources {
  310. const struct netfs_cache_ops *ops;
  311. void *cache_priv;
  312. void *cache_priv2;
  313. };
  314. This contains an operations table pointer and two private pointers. The
  315. operation table looks like the following::
  316. struct netfs_cache_ops {
  317. void (*end_operation)(struct netfs_cache_resources *cres);
  318. void (*expand_readahead)(struct netfs_cache_resources *cres,
  319. loff_t *_start, size_t *_len, loff_t i_size);
  320. enum netfs_io_source (*prepare_read)(struct netfs_io_subrequest *subreq,
  321. loff_t i_size);
  322. int (*read)(struct netfs_cache_resources *cres,
  323. loff_t start_pos,
  324. struct iov_iter *iter,
  325. bool seek_data,
  326. netfs_io_terminated_t term_func,
  327. void *term_func_priv);
  328. int (*prepare_write)(struct netfs_cache_resources *cres,
  329. loff_t *_start, size_t *_len, loff_t i_size,
  330. bool no_space_allocated_yet);
  331. int (*write)(struct netfs_cache_resources *cres,
  332. loff_t start_pos,
  333. struct iov_iter *iter,
  334. netfs_io_terminated_t term_func,
  335. void *term_func_priv);
  336. int (*query_occupancy)(struct netfs_cache_resources *cres,
  337. loff_t start, size_t len, size_t granularity,
  338. loff_t *_data_start, size_t *_data_len);
  339. };
  340. With a termination handler function pointer::
  341. typedef void (*netfs_io_terminated_t)(void *priv,
  342. ssize_t transferred_or_error,
  343. bool was_async);
  344. The methods defined in the table are:
  345. * ``end_operation()``
  346. [Required] Called to clean up the resources at the end of the read request.
  347. * ``expand_readahead()``
  348. [Optional] Called at the beginning of a netfs_readahead() operation to allow
  349. the cache to expand a request in either direction. This allows the cache to
  350. size the request appropriately for the cache granularity.
  351. The function is passed poiners to the start and length in its parameters,
  352. plus the size of the file for reference, and adjusts the start and length
  353. appropriately. It should return one of:
  354. * ``NETFS_FILL_WITH_ZEROES``
  355. * ``NETFS_DOWNLOAD_FROM_SERVER``
  356. * ``NETFS_READ_FROM_CACHE``
  357. * ``NETFS_INVALID_READ``
  358. to indicate whether the slice should just be cleared or whether it should be
  359. downloaded from the server or read from the cache - or whether slicing
  360. should be given up at the current point.
  361. * ``prepare_read()``
  362. [Required] Called to configure the next slice of a request. ->start and
  363. ->len in the subrequest indicate where and how big the next slice can be;
  364. the cache gets to reduce the length to match its granularity requirements.
  365. * ``read()``
  366. [Required] Called to read from the cache. The start file offset is given
  367. along with an iterator to read to, which gives the length also. It can be
  368. given a hint requesting that it seek forward from that start position for
  369. data.
  370. Also provided is a pointer to a termination handler function and private
  371. data to pass to that function. The termination function should be called
  372. with the number of bytes transferred or an error code, plus a flag
  373. indicating whether the termination is definitely happening in the caller's
  374. context.
  375. * ``prepare_write()``
  376. [Required] Called to prepare a write to the cache to take place. This
  377. involves checking to see whether the cache has sufficient space to honour
  378. the write. ``*_start`` and ``*_len`` indicate the region to be written; the
  379. region can be shrunk or it can be expanded to a page boundary either way as
  380. necessary to align for direct I/O. i_size holds the size of the object and
  381. is provided for reference. no_space_allocated_yet is set to true if the
  382. caller is certain that no data has been written to that region - for example
  383. if it tried to do a read from there already.
  384. * ``write()``
  385. [Required] Called to write to the cache. The start file offset is given
  386. along with an iterator to write from, which gives the length also.
  387. Also provided is a pointer to a termination handler function and private
  388. data to pass to that function. The termination function should be called
  389. with the number of bytes transferred or an error code, plus a flag
  390. indicating whether the termination is definitely happening in the caller's
  391. context.
  392. * ``query_occupancy()``
  393. [Required] Called to find out where the next piece of data is within a
  394. particular region of the cache. The start and length of the region to be
  395. queried are passed in, along with the granularity to which the answer needs
  396. to be aligned. The function passes back the start and length of the data,
  397. if any, available within that region. Note that there may be a hole at the
  398. front.
  399. It returns 0 if some data was found, -ENODATA if there was no usable data
  400. within the region or -ENOBUFS if there is no caching on this file.
  401. Note that these methods are passed a pointer to the cache resource structure,
  402. not the read request structure as they could be used in other situations where
  403. there isn't a read request structure as well, such as writing dirty data to the
  404. cache.
  405. API Function Reference
  406. ======================
  407. .. kernel-doc:: include/linux/netfs.h
  408. .. kernel-doc:: fs/netfs/buffered_read.c