netfs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* Network filesystem support services.
  3. *
  4. * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. *
  7. * See:
  8. *
  9. * Documentation/filesystems/netfs_library.rst
  10. *
  11. * for a description of the network filesystem interface declared here.
  12. */
  13. #ifndef _LINUX_NETFS_H
  14. #define _LINUX_NETFS_H
  15. #include <linux/workqueue.h>
  16. #include <linux/fs.h>
  17. #include <linux/pagemap.h>
  18. enum netfs_sreq_ref_trace;
  19. /*
  20. * Overload PG_private_2 to give us PG_fscache - this is used to indicate that
  21. * a page is currently backed by a local disk cache
  22. */
  23. #define folio_test_fscache(folio) folio_test_private_2(folio)
  24. #define PageFsCache(page) PagePrivate2((page))
  25. #define SetPageFsCache(page) SetPagePrivate2((page))
  26. #define ClearPageFsCache(page) ClearPagePrivate2((page))
  27. #define TestSetPageFsCache(page) TestSetPagePrivate2((page))
  28. #define TestClearPageFsCache(page) TestClearPagePrivate2((page))
  29. /**
  30. * folio_start_fscache - Start an fscache write on a folio.
  31. * @folio: The folio.
  32. *
  33. * Call this function before writing a folio to a local cache. Starting a
  34. * second write before the first one finishes is not allowed.
  35. */
  36. static inline void folio_start_fscache(struct folio *folio)
  37. {
  38. VM_BUG_ON_FOLIO(folio_test_private_2(folio), folio);
  39. folio_get(folio);
  40. folio_set_private_2(folio);
  41. }
  42. /**
  43. * folio_end_fscache - End an fscache write on a folio.
  44. * @folio: The folio.
  45. *
  46. * Call this function after the folio has been written to the local cache.
  47. * This will wake any sleepers waiting on this folio.
  48. */
  49. static inline void folio_end_fscache(struct folio *folio)
  50. {
  51. folio_end_private_2(folio);
  52. }
  53. /**
  54. * folio_wait_fscache - Wait for an fscache write on this folio to end.
  55. * @folio: The folio.
  56. *
  57. * If this folio is currently being written to a local cache, wait for
  58. * the write to finish. Another write may start after this one finishes,
  59. * unless the caller holds the folio lock.
  60. */
  61. static inline void folio_wait_fscache(struct folio *folio)
  62. {
  63. folio_wait_private_2(folio);
  64. }
  65. /**
  66. * folio_wait_fscache_killable - Wait for an fscache write on this folio to end.
  67. * @folio: The folio.
  68. *
  69. * If this folio is currently being written to a local cache, wait
  70. * for the write to finish or for a fatal signal to be received.
  71. * Another write may start after this one finishes, unless the caller
  72. * holds the folio lock.
  73. *
  74. * Return:
  75. * - 0 if successful.
  76. * - -EINTR if a fatal signal was encountered.
  77. */
  78. static inline int folio_wait_fscache_killable(struct folio *folio)
  79. {
  80. return folio_wait_private_2_killable(folio);
  81. }
  82. static inline void set_page_fscache(struct page *page)
  83. {
  84. folio_start_fscache(page_folio(page));
  85. }
  86. static inline void end_page_fscache(struct page *page)
  87. {
  88. folio_end_private_2(page_folio(page));
  89. }
  90. static inline void wait_on_page_fscache(struct page *page)
  91. {
  92. folio_wait_private_2(page_folio(page));
  93. }
  94. static inline int wait_on_page_fscache_killable(struct page *page)
  95. {
  96. return folio_wait_private_2_killable(page_folio(page));
  97. }
  98. enum netfs_io_source {
  99. NETFS_FILL_WITH_ZEROES,
  100. NETFS_DOWNLOAD_FROM_SERVER,
  101. NETFS_READ_FROM_CACHE,
  102. NETFS_INVALID_READ,
  103. } __mode(byte);
  104. typedef void (*netfs_io_terminated_t)(void *priv, ssize_t transferred_or_error,
  105. bool was_async);
  106. /*
  107. * Per-inode context. This wraps the VFS inode.
  108. */
  109. struct netfs_inode {
  110. struct inode inode; /* The VFS inode */
  111. const struct netfs_request_ops *ops;
  112. #if IS_ENABLED(CONFIG_FSCACHE)
  113. struct fscache_cookie *cache;
  114. #endif
  115. loff_t remote_i_size; /* Size of the remote file */
  116. };
  117. /*
  118. * Resources required to do operations on a cache.
  119. */
  120. struct netfs_cache_resources {
  121. const struct netfs_cache_ops *ops;
  122. void *cache_priv;
  123. void *cache_priv2;
  124. unsigned int debug_id; /* Cookie debug ID */
  125. unsigned int inval_counter; /* object->inval_counter at begin_op */
  126. };
  127. /*
  128. * Descriptor for a single component subrequest.
  129. */
  130. struct netfs_io_subrequest {
  131. struct netfs_io_request *rreq; /* Supervising I/O request */
  132. struct list_head rreq_link; /* Link in rreq->subrequests */
  133. loff_t start; /* Where to start the I/O */
  134. size_t len; /* Size of the I/O */
  135. size_t transferred; /* Amount of data transferred */
  136. refcount_t ref;
  137. short error; /* 0 or error that occurred */
  138. unsigned short debug_index; /* Index in list (for debugging output) */
  139. enum netfs_io_source source; /* Where to read from/write to */
  140. unsigned long flags;
  141. #define NETFS_SREQ_COPY_TO_CACHE 0 /* Set if should copy the data to the cache */
  142. #define NETFS_SREQ_CLEAR_TAIL 1 /* Set if the rest of the read should be cleared */
  143. #define NETFS_SREQ_SHORT_IO 2 /* Set if the I/O was short */
  144. #define NETFS_SREQ_SEEK_DATA_READ 3 /* Set if ->read() should SEEK_DATA first */
  145. #define NETFS_SREQ_NO_PROGRESS 4 /* Set if we didn't manage to read any data */
  146. #define NETFS_SREQ_ONDEMAND 5 /* Set if it's from on-demand read mode */
  147. };
  148. enum netfs_io_origin {
  149. NETFS_READAHEAD, /* This read was triggered by readahead */
  150. NETFS_READPAGE, /* This read is a synchronous read */
  151. NETFS_READ_FOR_WRITE, /* This read is to prepare a write */
  152. } __mode(byte);
  153. /*
  154. * Descriptor for an I/O helper request. This is used to make multiple I/O
  155. * operations to a variety of data stores and then stitch the result together.
  156. */
  157. struct netfs_io_request {
  158. struct work_struct work;
  159. struct inode *inode; /* The file being accessed */
  160. struct address_space *mapping; /* The mapping being accessed */
  161. struct netfs_cache_resources cache_resources;
  162. struct list_head subrequests; /* Contributory I/O operations */
  163. void *netfs_priv; /* Private data for the netfs */
  164. unsigned int debug_id;
  165. atomic_t nr_outstanding; /* Number of ops in progress */
  166. atomic_t nr_copy_ops; /* Number of copy-to-cache ops in progress */
  167. size_t submitted; /* Amount submitted for I/O so far */
  168. size_t len; /* Length of the request */
  169. short error; /* 0 or error that occurred */
  170. enum netfs_io_origin origin; /* Origin of the request */
  171. loff_t i_size; /* Size of the file */
  172. loff_t start; /* Start position */
  173. pgoff_t no_unlock_folio; /* Don't unlock this folio after read */
  174. refcount_t ref;
  175. unsigned long flags;
  176. #define NETFS_RREQ_INCOMPLETE_IO 0 /* Some ioreqs terminated short or with error */
  177. #define NETFS_RREQ_COPY_TO_CACHE 1 /* Need to write to the cache */
  178. #define NETFS_RREQ_NO_UNLOCK_FOLIO 2 /* Don't unlock no_unlock_folio on completion */
  179. #define NETFS_RREQ_DONT_UNLOCK_FOLIOS 3 /* Don't unlock the folios on completion */
  180. #define NETFS_RREQ_FAILED 4 /* The request failed */
  181. #define NETFS_RREQ_IN_PROGRESS 5 /* Unlocked when the request completes */
  182. const struct netfs_request_ops *netfs_ops;
  183. };
  184. /*
  185. * Operations the network filesystem can/must provide to the helpers.
  186. */
  187. struct netfs_request_ops {
  188. int (*init_request)(struct netfs_io_request *rreq, struct file *file);
  189. void (*free_request)(struct netfs_io_request *rreq);
  190. int (*begin_cache_operation)(struct netfs_io_request *rreq);
  191. void (*expand_readahead)(struct netfs_io_request *rreq);
  192. bool (*clamp_length)(struct netfs_io_subrequest *subreq);
  193. void (*issue_read)(struct netfs_io_subrequest *subreq);
  194. bool (*is_still_valid)(struct netfs_io_request *rreq);
  195. int (*check_write_begin)(struct file *file, loff_t pos, unsigned len,
  196. struct folio **foliop, void **_fsdata);
  197. void (*done)(struct netfs_io_request *rreq);
  198. };
  199. /*
  200. * How to handle reading from a hole.
  201. */
  202. enum netfs_read_from_hole {
  203. NETFS_READ_HOLE_IGNORE,
  204. NETFS_READ_HOLE_CLEAR,
  205. NETFS_READ_HOLE_FAIL,
  206. };
  207. /*
  208. * Table of operations for access to a cache. This is obtained by
  209. * rreq->ops->begin_cache_operation().
  210. */
  211. struct netfs_cache_ops {
  212. /* End an operation */
  213. void (*end_operation)(struct netfs_cache_resources *cres);
  214. /* Read data from the cache */
  215. int (*read)(struct netfs_cache_resources *cres,
  216. loff_t start_pos,
  217. struct iov_iter *iter,
  218. enum netfs_read_from_hole read_hole,
  219. netfs_io_terminated_t term_func,
  220. void *term_func_priv);
  221. /* Write data to the cache */
  222. int (*write)(struct netfs_cache_resources *cres,
  223. loff_t start_pos,
  224. struct iov_iter *iter,
  225. netfs_io_terminated_t term_func,
  226. void *term_func_priv);
  227. /* Expand readahead request */
  228. void (*expand_readahead)(struct netfs_cache_resources *cres,
  229. loff_t *_start, size_t *_len, loff_t i_size);
  230. /* Prepare a read operation, shortening it to a cached/uncached
  231. * boundary as appropriate.
  232. */
  233. enum netfs_io_source (*prepare_read)(struct netfs_io_subrequest *subreq,
  234. loff_t i_size);
  235. /* Prepare a write operation, working out what part of the write we can
  236. * actually do.
  237. */
  238. int (*prepare_write)(struct netfs_cache_resources *cres,
  239. loff_t *_start, size_t *_len, loff_t i_size,
  240. bool no_space_allocated_yet);
  241. /* Query the occupancy of the cache in a region, returning where the
  242. * next chunk of data starts and how long it is.
  243. */
  244. int (*query_occupancy)(struct netfs_cache_resources *cres,
  245. loff_t start, size_t len, size_t granularity,
  246. loff_t *_data_start, size_t *_data_len);
  247. };
  248. struct readahead_control;
  249. void netfs_readahead(struct readahead_control *);
  250. int netfs_read_folio(struct file *, struct folio *);
  251. int netfs_write_begin(struct netfs_inode *, struct file *,
  252. struct address_space *, loff_t pos, unsigned int len,
  253. struct folio **, void **fsdata);
  254. void netfs_subreq_terminated(struct netfs_io_subrequest *, ssize_t, bool);
  255. void netfs_get_subrequest(struct netfs_io_subrequest *subreq,
  256. enum netfs_sreq_ref_trace what);
  257. void netfs_put_subrequest(struct netfs_io_subrequest *subreq,
  258. bool was_async, enum netfs_sreq_ref_trace what);
  259. void netfs_stats_show(struct seq_file *);
  260. /**
  261. * netfs_inode - Get the netfs inode context from the inode
  262. * @inode: The inode to query
  263. *
  264. * Get the netfs lib inode context from the network filesystem's inode. The
  265. * context struct is expected to directly follow on from the VFS inode struct.
  266. */
  267. static inline struct netfs_inode *netfs_inode(struct inode *inode)
  268. {
  269. return container_of(inode, struct netfs_inode, inode);
  270. }
  271. /**
  272. * netfs_inode_init - Initialise a netfslib inode context
  273. * @ctx: The netfs inode to initialise
  274. * @ops: The netfs's operations list
  275. *
  276. * Initialise the netfs library context struct. This is expected to follow on
  277. * directly from the VFS inode struct.
  278. */
  279. static inline void netfs_inode_init(struct netfs_inode *ctx,
  280. const struct netfs_request_ops *ops)
  281. {
  282. ctx->ops = ops;
  283. ctx->remote_i_size = i_size_read(&ctx->inode);
  284. #if IS_ENABLED(CONFIG_FSCACHE)
  285. ctx->cache = NULL;
  286. #endif
  287. }
  288. /**
  289. * netfs_resize_file - Note that a file got resized
  290. * @ctx: The netfs inode being resized
  291. * @new_i_size: The new file size
  292. *
  293. * Inform the netfs lib that a file got resized so that it can adjust its state.
  294. */
  295. static inline void netfs_resize_file(struct netfs_inode *ctx, loff_t new_i_size)
  296. {
  297. ctx->remote_i_size = new_i_size;
  298. }
  299. /**
  300. * netfs_i_cookie - Get the cache cookie from the inode
  301. * @ctx: The netfs inode to query
  302. *
  303. * Get the caching cookie (if enabled) from the network filesystem's inode.
  304. */
  305. static inline struct fscache_cookie *netfs_i_cookie(struct netfs_inode *ctx)
  306. {
  307. #if IS_ENABLED(CONFIG_FSCACHE)
  308. return ctx->cache;
  309. #else
  310. return NULL;
  311. #endif
  312. }
  313. #endif /* _LINUX_NETFS_H */