misc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Miscellaneous routines.
  3. *
  4. * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/swap.h>
  8. #include "internal.h"
  9. /*
  10. * Make sure there's space in the rolling queue.
  11. */
  12. struct folio_queue *netfs_buffer_make_space(struct netfs_io_request *rreq)
  13. {
  14. struct folio_queue *tail = rreq->buffer_tail, *prev;
  15. unsigned int prev_nr_slots = 0;
  16. if (WARN_ON_ONCE(!rreq->buffer && tail) ||
  17. WARN_ON_ONCE(rreq->buffer && !tail))
  18. return ERR_PTR(-EIO);
  19. prev = tail;
  20. if (prev) {
  21. if (!folioq_full(tail))
  22. return tail;
  23. prev_nr_slots = folioq_nr_slots(tail);
  24. }
  25. tail = kmalloc(sizeof(*tail), GFP_NOFS);
  26. if (!tail)
  27. return ERR_PTR(-ENOMEM);
  28. netfs_stat(&netfs_n_folioq);
  29. folioq_init(tail);
  30. tail->prev = prev;
  31. if (prev)
  32. /* [!] NOTE: After we set prev->next, the consumer is entirely
  33. * at liberty to delete prev.
  34. */
  35. WRITE_ONCE(prev->next, tail);
  36. rreq->buffer_tail = tail;
  37. if (!rreq->buffer) {
  38. rreq->buffer = tail;
  39. iov_iter_folio_queue(&rreq->io_iter, ITER_SOURCE, tail, 0, 0, 0);
  40. } else {
  41. /* Make sure we don't leave the master iterator pointing to a
  42. * block that might get immediately consumed.
  43. */
  44. if (rreq->io_iter.folioq == prev &&
  45. rreq->io_iter.folioq_slot == prev_nr_slots) {
  46. rreq->io_iter.folioq = tail;
  47. rreq->io_iter.folioq_slot = 0;
  48. }
  49. }
  50. rreq->buffer_tail_slot = 0;
  51. return tail;
  52. }
  53. /*
  54. * Append a folio to the rolling queue.
  55. */
  56. int netfs_buffer_append_folio(struct netfs_io_request *rreq, struct folio *folio,
  57. bool needs_put)
  58. {
  59. struct folio_queue *tail;
  60. unsigned int slot, order = folio_order(folio);
  61. tail = netfs_buffer_make_space(rreq);
  62. if (IS_ERR(tail))
  63. return PTR_ERR(tail);
  64. rreq->io_iter.count += PAGE_SIZE << order;
  65. slot = folioq_append(tail, folio);
  66. /* Store the counter after setting the slot. */
  67. smp_store_release(&rreq->buffer_tail_slot, slot);
  68. return 0;
  69. }
  70. /*
  71. * Delete the head of a rolling queue.
  72. */
  73. struct folio_queue *netfs_delete_buffer_head(struct netfs_io_request *wreq)
  74. {
  75. struct folio_queue *head = wreq->buffer, *next = head->next;
  76. if (next)
  77. next->prev = NULL;
  78. netfs_stat_d(&netfs_n_folioq);
  79. kfree(head);
  80. wreq->buffer = next;
  81. return next;
  82. }
  83. /*
  84. * Clear out a rolling queue.
  85. */
  86. void netfs_clear_buffer(struct netfs_io_request *rreq)
  87. {
  88. struct folio_queue *p;
  89. while ((p = rreq->buffer)) {
  90. rreq->buffer = p->next;
  91. for (int slot = 0; slot < folioq_count(p); slot++) {
  92. struct folio *folio = folioq_folio(p, slot);
  93. if (!folio)
  94. continue;
  95. if (folioq_is_marked(p, slot)) {
  96. trace_netfs_folio(folio, netfs_folio_trace_put);
  97. folio_put(folio);
  98. }
  99. }
  100. netfs_stat_d(&netfs_n_folioq);
  101. kfree(p);
  102. }
  103. }
  104. /*
  105. * Reset the subrequest iterator to refer just to the region remaining to be
  106. * read. The iterator may or may not have been advanced by socket ops or
  107. * extraction ops to an extent that may or may not match the amount actually
  108. * read.
  109. */
  110. void netfs_reset_iter(struct netfs_io_subrequest *subreq)
  111. {
  112. struct iov_iter *io_iter = &subreq->io_iter;
  113. size_t remain = subreq->len - subreq->transferred;
  114. if (io_iter->count > remain)
  115. iov_iter_advance(io_iter, io_iter->count - remain);
  116. else if (io_iter->count < remain)
  117. iov_iter_revert(io_iter, remain - io_iter->count);
  118. iov_iter_truncate(&subreq->io_iter, remain);
  119. }
  120. /**
  121. * netfs_dirty_folio - Mark folio dirty and pin a cache object for writeback
  122. * @mapping: The mapping the folio belongs to.
  123. * @folio: The folio being dirtied.
  124. *
  125. * Set the dirty flag on a folio and pin an in-use cache object in memory so
  126. * that writeback can later write to it. This is intended to be called from
  127. * the filesystem's ->dirty_folio() method.
  128. *
  129. * Return: true if the dirty flag was set on the folio, false otherwise.
  130. */
  131. bool netfs_dirty_folio(struct address_space *mapping, struct folio *folio)
  132. {
  133. struct inode *inode = mapping->host;
  134. struct netfs_inode *ictx = netfs_inode(inode);
  135. struct fscache_cookie *cookie = netfs_i_cookie(ictx);
  136. bool need_use = false;
  137. _enter("");
  138. if (!filemap_dirty_folio(mapping, folio))
  139. return false;
  140. if (!fscache_cookie_valid(cookie))
  141. return true;
  142. if (!(inode->i_state & I_PINNING_NETFS_WB)) {
  143. spin_lock(&inode->i_lock);
  144. if (!(inode->i_state & I_PINNING_NETFS_WB)) {
  145. inode->i_state |= I_PINNING_NETFS_WB;
  146. need_use = true;
  147. }
  148. spin_unlock(&inode->i_lock);
  149. if (need_use)
  150. fscache_use_cookie(cookie, true);
  151. }
  152. return true;
  153. }
  154. EXPORT_SYMBOL(netfs_dirty_folio);
  155. /**
  156. * netfs_unpin_writeback - Unpin writeback resources
  157. * @inode: The inode on which the cookie resides
  158. * @wbc: The writeback control
  159. *
  160. * Unpin the writeback resources pinned by netfs_dirty_folio(). This is
  161. * intended to be called as/by the netfs's ->write_inode() method.
  162. */
  163. int netfs_unpin_writeback(struct inode *inode, struct writeback_control *wbc)
  164. {
  165. struct fscache_cookie *cookie = netfs_i_cookie(netfs_inode(inode));
  166. if (wbc->unpinned_netfs_wb)
  167. fscache_unuse_cookie(cookie, NULL, NULL);
  168. return 0;
  169. }
  170. EXPORT_SYMBOL(netfs_unpin_writeback);
  171. /**
  172. * netfs_clear_inode_writeback - Clear writeback resources pinned by an inode
  173. * @inode: The inode to clean up
  174. * @aux: Auxiliary data to apply to the inode
  175. *
  176. * Clear any writeback resources held by an inode when the inode is evicted.
  177. * This must be called before clear_inode() is called.
  178. */
  179. void netfs_clear_inode_writeback(struct inode *inode, const void *aux)
  180. {
  181. struct fscache_cookie *cookie = netfs_i_cookie(netfs_inode(inode));
  182. if (inode->i_state & I_PINNING_NETFS_WB) {
  183. loff_t i_size = i_size_read(inode);
  184. fscache_unuse_cookie(cookie, aux, &i_size);
  185. }
  186. }
  187. EXPORT_SYMBOL(netfs_clear_inode_writeback);
  188. /**
  189. * netfs_invalidate_folio - Invalidate or partially invalidate a folio
  190. * @folio: Folio proposed for release
  191. * @offset: Offset of the invalidated region
  192. * @length: Length of the invalidated region
  193. *
  194. * Invalidate part or all of a folio for a network filesystem. The folio will
  195. * be removed afterwards if the invalidated region covers the entire folio.
  196. */
  197. void netfs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
  198. {
  199. struct netfs_folio *finfo;
  200. struct netfs_inode *ctx = netfs_inode(folio_inode(folio));
  201. size_t flen = folio_size(folio);
  202. _enter("{%lx},%zx,%zx", folio->index, offset, length);
  203. if (offset == 0 && length == flen) {
  204. unsigned long long i_size = i_size_read(&ctx->inode);
  205. unsigned long long fpos = folio_pos(folio), end;
  206. end = umin(fpos + flen, i_size);
  207. if (fpos < i_size && end > ctx->zero_point)
  208. ctx->zero_point = end;
  209. }
  210. folio_wait_private_2(folio); /* [DEPRECATED] */
  211. if (!folio_test_private(folio))
  212. return;
  213. finfo = netfs_folio_info(folio);
  214. if (offset == 0 && length >= flen)
  215. goto erase_completely;
  216. if (finfo) {
  217. /* We have a partially uptodate page from a streaming write. */
  218. unsigned int fstart = finfo->dirty_offset;
  219. unsigned int fend = fstart + finfo->dirty_len;
  220. unsigned int iend = offset + length;
  221. if (offset >= fend)
  222. return;
  223. if (iend <= fstart)
  224. return;
  225. /* The invalidation region overlaps the data. If the region
  226. * covers the start of the data, we either move along the start
  227. * or just erase the data entirely.
  228. */
  229. if (offset <= fstart) {
  230. if (iend >= fend)
  231. goto erase_completely;
  232. /* Move the start of the data. */
  233. finfo->dirty_len = fend - iend;
  234. finfo->dirty_offset = offset;
  235. return;
  236. }
  237. /* Reduce the length of the data if the invalidation region
  238. * covers the tail part.
  239. */
  240. if (iend >= fend) {
  241. finfo->dirty_len = offset - fstart;
  242. return;
  243. }
  244. /* A partial write was split. The caller has already zeroed
  245. * it, so just absorb the hole.
  246. */
  247. }
  248. return;
  249. erase_completely:
  250. netfs_put_group(netfs_folio_group(folio));
  251. folio_detach_private(folio);
  252. folio_clear_uptodate(folio);
  253. kfree(finfo);
  254. return;
  255. }
  256. EXPORT_SYMBOL(netfs_invalidate_folio);
  257. /**
  258. * netfs_release_folio - Try to release a folio
  259. * @folio: Folio proposed for release
  260. * @gfp: Flags qualifying the release
  261. *
  262. * Request release of a folio and clean up its private state if it's not busy.
  263. * Returns true if the folio can now be released, false if not
  264. */
  265. bool netfs_release_folio(struct folio *folio, gfp_t gfp)
  266. {
  267. struct netfs_inode *ctx = netfs_inode(folio_inode(folio));
  268. unsigned long long end;
  269. if (folio_test_dirty(folio))
  270. return false;
  271. end = umin(folio_pos(folio) + folio_size(folio), i_size_read(&ctx->inode));
  272. if (end > ctx->zero_point)
  273. ctx->zero_point = end;
  274. if (folio_test_private(folio))
  275. return false;
  276. if (unlikely(folio_test_private_2(folio))) { /* [DEPRECATED] */
  277. if (current_is_kswapd() || !(gfp & __GFP_FS))
  278. return false;
  279. folio_wait_private_2(folio);
  280. }
  281. fscache_note_page_release(netfs_i_cookie(ctx));
  282. return true;
  283. }
  284. EXPORT_SYMBOL(netfs_release_folio);