write_retry.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Network filesystem write retrying.
  3. *
  4. * Copyright (C) 2024 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/fs.h>
  8. #include <linux/mm.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/slab.h>
  11. #include "internal.h"
  12. /*
  13. * Perform retries on the streams that need it.
  14. */
  15. static void netfs_retry_write_stream(struct netfs_io_request *wreq,
  16. struct netfs_io_stream *stream)
  17. {
  18. struct list_head *next;
  19. _enter("R=%x[%x:]", wreq->debug_id, stream->stream_nr);
  20. if (list_empty(&stream->subrequests))
  21. return;
  22. if (stream->source == NETFS_UPLOAD_TO_SERVER &&
  23. wreq->netfs_ops->retry_request)
  24. wreq->netfs_ops->retry_request(wreq, stream);
  25. if (unlikely(stream->failed))
  26. return;
  27. /* If there's no renegotiation to do, just resend each failed subreq. */
  28. if (!stream->prepare_write) {
  29. struct netfs_io_subrequest *subreq;
  30. list_for_each_entry(subreq, &stream->subrequests, rreq_link) {
  31. if (test_bit(NETFS_SREQ_FAILED, &subreq->flags))
  32. break;
  33. if (__test_and_clear_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags)) {
  34. struct iov_iter source;
  35. netfs_reset_iter(subreq);
  36. source = subreq->io_iter;
  37. netfs_get_subrequest(subreq, netfs_sreq_trace_get_resubmit);
  38. netfs_reissue_write(stream, subreq, &source);
  39. }
  40. }
  41. return;
  42. }
  43. next = stream->subrequests.next;
  44. do {
  45. struct netfs_io_subrequest *subreq = NULL, *from, *to, *tmp;
  46. struct iov_iter source;
  47. unsigned long long start, len;
  48. size_t part;
  49. bool boundary = false;
  50. /* Go through the stream and find the next span of contiguous
  51. * data that we then rejig (cifs, for example, needs the wsize
  52. * renegotiating) and reissue.
  53. */
  54. from = list_entry(next, struct netfs_io_subrequest, rreq_link);
  55. to = from;
  56. start = from->start + from->transferred;
  57. len = from->len - from->transferred;
  58. if (test_bit(NETFS_SREQ_FAILED, &from->flags) ||
  59. !test_bit(NETFS_SREQ_NEED_RETRY, &from->flags))
  60. return;
  61. list_for_each_continue(next, &stream->subrequests) {
  62. subreq = list_entry(next, struct netfs_io_subrequest, rreq_link);
  63. if (subreq->start + subreq->transferred != start + len ||
  64. test_bit(NETFS_SREQ_BOUNDARY, &subreq->flags) ||
  65. !test_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags))
  66. break;
  67. to = subreq;
  68. len += to->len;
  69. }
  70. /* Determine the set of buffers we're going to use. Each
  71. * subreq gets a subset of a single overall contiguous buffer.
  72. */
  73. netfs_reset_iter(from);
  74. source = from->io_iter;
  75. source.count = len;
  76. /* Work through the sublist. */
  77. subreq = from;
  78. list_for_each_entry_from(subreq, &stream->subrequests, rreq_link) {
  79. if (!len)
  80. break;
  81. subreq->start = start;
  82. subreq->len = len;
  83. __clear_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags);
  84. trace_netfs_sreq(subreq, netfs_sreq_trace_retry);
  85. /* Renegotiate max_len (wsize) */
  86. stream->sreq_max_len = len;
  87. stream->prepare_write(subreq);
  88. part = umin(len, stream->sreq_max_len);
  89. if (unlikely(stream->sreq_max_segs))
  90. part = netfs_limit_iter(&source, 0, part, stream->sreq_max_segs);
  91. subreq->len = part;
  92. subreq->transferred = 0;
  93. len -= part;
  94. start += part;
  95. if (len && subreq == to &&
  96. __test_and_clear_bit(NETFS_SREQ_BOUNDARY, &to->flags))
  97. boundary = true;
  98. netfs_get_subrequest(subreq, netfs_sreq_trace_get_resubmit);
  99. netfs_reissue_write(stream, subreq, &source);
  100. if (subreq == to)
  101. break;
  102. }
  103. /* If we managed to use fewer subreqs, we can discard the
  104. * excess; if we used the same number, then we're done.
  105. */
  106. if (!len) {
  107. if (subreq == to)
  108. continue;
  109. list_for_each_entry_safe_from(subreq, tmp,
  110. &stream->subrequests, rreq_link) {
  111. trace_netfs_sreq(subreq, netfs_sreq_trace_discard);
  112. list_del(&subreq->rreq_link);
  113. netfs_put_subrequest(subreq, netfs_sreq_trace_put_done);
  114. if (subreq == to)
  115. break;
  116. }
  117. continue;
  118. }
  119. /* We ran out of subrequests, so we need to allocate some more
  120. * and insert them after.
  121. */
  122. do {
  123. subreq = netfs_alloc_subrequest(wreq);
  124. subreq->source = to->source;
  125. subreq->start = start;
  126. subreq->stream_nr = to->stream_nr;
  127. subreq->retry_count = 1;
  128. trace_netfs_sreq_ref(wreq->debug_id, subreq->debug_index,
  129. refcount_read(&subreq->ref),
  130. netfs_sreq_trace_new);
  131. trace_netfs_sreq(subreq, netfs_sreq_trace_split);
  132. list_add(&subreq->rreq_link, &to->rreq_link);
  133. to = list_next_entry(to, rreq_link);
  134. trace_netfs_sreq(subreq, netfs_sreq_trace_retry);
  135. stream->sreq_max_len = len;
  136. stream->sreq_max_segs = INT_MAX;
  137. switch (stream->source) {
  138. case NETFS_UPLOAD_TO_SERVER:
  139. netfs_stat(&netfs_n_wh_upload);
  140. stream->sreq_max_len = umin(len, wreq->wsize);
  141. break;
  142. case NETFS_WRITE_TO_CACHE:
  143. netfs_stat(&netfs_n_wh_write);
  144. break;
  145. default:
  146. WARN_ON_ONCE(1);
  147. }
  148. stream->prepare_write(subreq);
  149. part = umin(len, stream->sreq_max_len);
  150. subreq->len = subreq->transferred + part;
  151. len -= part;
  152. start += part;
  153. if (!len && boundary) {
  154. __set_bit(NETFS_SREQ_BOUNDARY, &to->flags);
  155. boundary = false;
  156. }
  157. netfs_reissue_write(stream, subreq, &source);
  158. if (!len)
  159. break;
  160. } while (len);
  161. } while (!list_is_head(next, &stream->subrequests));
  162. }
  163. /*
  164. * Perform retries on the streams that need it. If we're doing content
  165. * encryption and the server copy changed due to a third-party write, we may
  166. * need to do an RMW cycle and also rewrite the data to the cache.
  167. */
  168. void netfs_retry_writes(struct netfs_io_request *wreq)
  169. {
  170. struct netfs_io_stream *stream;
  171. int s;
  172. netfs_stat(&netfs_n_wh_retry_write_req);
  173. /* Wait for all outstanding I/O to quiesce before performing retries as
  174. * we may need to renegotiate the I/O sizes.
  175. */
  176. set_bit(NETFS_RREQ_RETRYING, &wreq->flags);
  177. for (s = 0; s < NR_IO_STREAMS; s++) {
  178. stream = &wreq->io_streams[s];
  179. if (stream->active)
  180. netfs_wait_for_in_progress_stream(wreq, stream);
  181. }
  182. clear_bit(NETFS_RREQ_RETRYING, &wreq->flags);
  183. // TODO: Enc: Fetch changed partial pages
  184. // TODO: Enc: Reencrypt content if needed.
  185. // TODO: Enc: Wind back transferred point.
  186. // TODO: Enc: Mark cache pages for retry.
  187. for (s = 0; s < NR_IO_STREAMS; s++) {
  188. stream = &wreq->io_streams[s];
  189. if (stream->need_retry) {
  190. stream->need_retry = false;
  191. netfs_retry_write_stream(wreq, stream);
  192. }
  193. }
  194. }