read_pgpriv2.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Read with PG_private_2 [DEPRECATED].
  3. *
  4. * Copyright (C) 2024 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/export.h>
  8. #include <linux/fs.h>
  9. #include <linux/mm.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/slab.h>
  12. #include <linux/task_io_accounting_ops.h>
  13. #include "internal.h"
  14. /*
  15. * [DEPRECATED] Copy a folio to the cache with PG_private_2 set.
  16. */
  17. static void netfs_pgpriv2_copy_folio(struct netfs_io_request *creq, struct folio *folio)
  18. {
  19. struct netfs_io_stream *cache = &creq->io_streams[1];
  20. size_t fsize = folio_size(folio), flen = fsize;
  21. loff_t fpos = folio_pos(folio), i_size;
  22. bool to_eof = false;
  23. _enter("");
  24. /* netfs_perform_write() may shift i_size around the page or from out
  25. * of the page to beyond it, but cannot move i_size into or through the
  26. * page since we have it locked.
  27. */
  28. i_size = i_size_read(creq->inode);
  29. if (fpos >= i_size) {
  30. /* mmap beyond eof. */
  31. _debug("beyond eof");
  32. folio_end_private_2(folio);
  33. return;
  34. }
  35. if (fpos + fsize > creq->i_size)
  36. creq->i_size = i_size;
  37. if (flen > i_size - fpos) {
  38. flen = i_size - fpos;
  39. to_eof = true;
  40. } else if (flen == i_size - fpos) {
  41. to_eof = true;
  42. }
  43. _debug("folio %zx %zx", flen, fsize);
  44. trace_netfs_folio(folio, netfs_folio_trace_store_copy);
  45. /* Attach the folio to the rolling buffer. */
  46. if (rolling_buffer_append(&creq->buffer, folio, 0) < 0) {
  47. clear_bit(NETFS_RREQ_FOLIO_COPY_TO_CACHE, &creq->flags);
  48. return;
  49. }
  50. cache->submit_extendable_to = fsize;
  51. cache->submit_off = 0;
  52. cache->submit_len = flen;
  53. /* Attach the folio to one or more subrequests. For a big folio, we
  54. * could end up with thousands of subrequests if the wsize is small -
  55. * but we might need to wait during the creation of subrequests for
  56. * network resources (eg. SMB credits).
  57. */
  58. do {
  59. ssize_t part;
  60. creq->buffer.iter.iov_offset = cache->submit_off;
  61. atomic64_set(&creq->issued_to, fpos + cache->submit_off);
  62. cache->submit_extendable_to = fsize - cache->submit_off;
  63. part = netfs_advance_write(creq, cache, fpos + cache->submit_off,
  64. cache->submit_len, to_eof);
  65. cache->submit_off += part;
  66. if (part > cache->submit_len)
  67. cache->submit_len = 0;
  68. else
  69. cache->submit_len -= part;
  70. } while (cache->submit_len > 0);
  71. creq->buffer.iter.iov_offset = 0;
  72. rolling_buffer_advance(&creq->buffer, fsize);
  73. atomic64_set(&creq->issued_to, fpos + fsize);
  74. if (flen < fsize)
  75. netfs_issue_write(creq, cache);
  76. }
  77. /*
  78. * [DEPRECATED] Set up copying to the cache.
  79. */
  80. static struct netfs_io_request *netfs_pgpriv2_begin_copy_to_cache(
  81. struct netfs_io_request *rreq, struct folio *folio)
  82. {
  83. struct netfs_io_request *creq;
  84. if (!fscache_resources_valid(&rreq->cache_resources))
  85. goto cancel;
  86. creq = netfs_create_write_req(rreq->mapping, NULL, folio_pos(folio),
  87. NETFS_PGPRIV2_COPY_TO_CACHE);
  88. if (IS_ERR(creq))
  89. goto cancel;
  90. if (!creq->io_streams[1].avail)
  91. goto cancel_put;
  92. __set_bit(NETFS_RREQ_OFFLOAD_COLLECTION, &creq->flags);
  93. trace_netfs_copy2cache(rreq, creq);
  94. trace_netfs_write(creq, netfs_write_trace_copy_to_cache);
  95. netfs_stat(&netfs_n_wh_copy_to_cache);
  96. rreq->copy_to_cache = creq;
  97. return creq;
  98. cancel_put:
  99. netfs_put_failed_request(creq);
  100. cancel:
  101. rreq->copy_to_cache = ERR_PTR(-ENOBUFS);
  102. clear_bit(NETFS_RREQ_FOLIO_COPY_TO_CACHE, &rreq->flags);
  103. return ERR_PTR(-ENOBUFS);
  104. }
  105. /*
  106. * [DEPRECATED] Mark page as requiring copy-to-cache using PG_private_2 and add
  107. * it to the copy write request.
  108. */
  109. void netfs_pgpriv2_copy_to_cache(struct netfs_io_request *rreq, struct folio *folio)
  110. {
  111. struct netfs_io_request *creq = rreq->copy_to_cache;
  112. if (!creq)
  113. creq = netfs_pgpriv2_begin_copy_to_cache(rreq, folio);
  114. if (IS_ERR(creq))
  115. return;
  116. trace_netfs_folio(folio, netfs_folio_trace_copy_to_cache);
  117. folio_start_private_2(folio);
  118. netfs_pgpriv2_copy_folio(creq, folio);
  119. }
  120. /*
  121. * [DEPRECATED] End writing to the cache, flushing out any outstanding writes.
  122. */
  123. void netfs_pgpriv2_end_copy_to_cache(struct netfs_io_request *rreq)
  124. {
  125. struct netfs_io_request *creq = rreq->copy_to_cache;
  126. if (IS_ERR_OR_NULL(creq))
  127. return;
  128. netfs_issue_write(creq, &creq->io_streams[1]);
  129. smp_wmb(); /* Write lists before ALL_QUEUED. */
  130. set_bit(NETFS_RREQ_ALL_QUEUED, &creq->flags);
  131. trace_netfs_rreq(rreq, netfs_rreq_trace_end_copy_to_cache);
  132. if (list_empty_careful(&creq->io_streams[1].subrequests))
  133. netfs_wake_collector(creq);
  134. netfs_put_request(creq, netfs_rreq_trace_put_return);
  135. creq->copy_to_cache = NULL;
  136. }
  137. /*
  138. * [DEPRECATED] Remove the PG_private_2 mark from any folios we've finished
  139. * copying.
  140. */
  141. bool netfs_pgpriv2_unlock_copied_folios(struct netfs_io_request *creq)
  142. {
  143. struct folio_queue *folioq = creq->buffer.tail;
  144. unsigned long long collected_to = creq->collected_to;
  145. unsigned int slot = creq->buffer.first_tail_slot;
  146. bool made_progress = false;
  147. if (slot >= folioq_nr_slots(folioq)) {
  148. folioq = rolling_buffer_delete_spent(&creq->buffer);
  149. slot = 0;
  150. }
  151. for (;;) {
  152. struct folio *folio;
  153. unsigned long long fpos, fend;
  154. size_t fsize, flen;
  155. folio = folioq_folio(folioq, slot);
  156. if (WARN_ONCE(!folio_test_private_2(folio),
  157. "R=%08x: folio %lx is not marked private_2\n",
  158. creq->debug_id, folio->index))
  159. trace_netfs_folio(folio, netfs_folio_trace_not_under_wback);
  160. fpos = folio_pos(folio);
  161. fsize = folio_size(folio);
  162. flen = fsize;
  163. fend = min_t(unsigned long long, fpos + flen, creq->i_size);
  164. trace_netfs_collect_folio(creq, folio, fend, collected_to);
  165. /* Unlock any folio we've transferred all of. */
  166. if (collected_to < fend)
  167. break;
  168. trace_netfs_folio(folio, netfs_folio_trace_end_copy);
  169. folio_end_private_2(folio);
  170. creq->cleaned_to = fpos + fsize;
  171. made_progress = true;
  172. /* Clean up the head folioq. If we clear an entire folioq, then
  173. * we can get rid of it provided it's not also the tail folioq
  174. * being filled by the issuer.
  175. */
  176. folioq_clear(folioq, slot);
  177. slot++;
  178. if (slot >= folioq_nr_slots(folioq)) {
  179. folioq = rolling_buffer_delete_spent(&creq->buffer);
  180. if (!folioq)
  181. goto done;
  182. slot = 0;
  183. }
  184. if (fpos + fsize >= collected_to)
  185. break;
  186. }
  187. creq->buffer.tail = folioq;
  188. done:
  189. creq->buffer.first_tail_slot = slot;
  190. return made_progress;
  191. }