| 1 | |
| 2 | |
| 3 | *
|
| 4 | * Copyright (C) 2024 Red Hat, Inc. All Rights Reserved.
|
| 5 | * Written by David Howells ([email protected])
|
| 6 |
|
| 7 |
|
| 8 | #include <linux/fs.h>
|
| 9 | #include <linux/mm.h>
|
| 10 | #include <linux/pagemap.h>
|
| 11 | #include <linux/slab.h>
|
| 12 | #include "internal.h"
|
| 13 |
|
| 14 | |
| 15 | * Perform retries on the streams that need it.
|
| 16 |
|
| 17 | static void netfs_retry_write_stream(struct netfs_io_request *wreq,
|
| 18 | struct netfs_io_stream *stream)
|
| 19 | {
|
| 20 | struct list_head *next;
|
| 21 |
|
| 22 | _enter("R=%x[%x:]", wreq->debug_id, stream->stream_nr);
|
| 23 |
|
| 24 | if (list_empty(&stream->subrequests))
|
| 25 | return;
|
| 26 |
|
| 27 | if (stream->source == NETFS_UPLOAD_TO_SERVER &&
|
| 28 | wreq->netfs_ops->retry_request)
|
| 29 | wreq->netfs_ops->retry_request(wreq, stream);
|
| 30 |
|
| 31 | if (unlikely(stream->failed))
|
| 32 | return;
|
| 33 |
|
| 34 |
|
| 35 | if (!stream->prepare_write) {
|
| 36 | struct netfs_io_subrequest *subreq;
|
| 37 |
|
| 38 | list_for_each_entry(subreq, &stream->subrequests, rreq_link) {
|
| 39 | if (test_bit(NETFS_SREQ_FAILED, &subreq->flags))
|
| 40 | break;
|
| 41 | if (__test_and_clear_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags)) {
|
| 42 | struct iov_iter source;
|
| 43 |
|
| 44 | netfs_reset_iter(subreq);
|
| 45 | source = subreq->io_iter;
|
| 46 | netfs_get_subrequest(subreq, netfs_sreq_trace_get_resubmit);
|
| 47 | netfs_reissue_write(stream, subreq, &source);
|
| 48 | }
|
| 49 | }
|
| 50 | return;
|
| 51 | }
|
| 52 |
|
| 53 | next = stream->subrequests.next;
|
| 54 |
|
| 55 | do {
|
| 56 | struct netfs_io_subrequest *subreq = NULL, *from, *to, *tmp;
|
| 57 | struct iov_iter source;
|
| 58 | uoff_t start, len;
|
| 59 | size_t part;
|
| 60 | bool boundary = false;
|
| 61 |
|
| 62 | |
| 63 | * data that we then rejig (cifs, for example, needs the wsize
|
| 64 | * renegotiating) and reissue.
|
| 65 |
|
| 66 | from = list_entry(next, struct netfs_io_subrequest, rreq_link);
|
| 67 | to = from;
|
| 68 | start = from->start + from->transferred;
|
| 69 | len = from->len - from->transferred;
|
| 70 |
|
| 71 | if (test_bit(NETFS_SREQ_FAILED, &from->flags) ||
|
| 72 | !test_bit(NETFS_SREQ_NEED_RETRY, &from->flags))
|
| 73 | return;
|
| 74 |
|
| 75 | for (;;) {
|
| 76 |
|
| 77 | next = smp_load_acquire(&next->next);
|
| 78 | if (next == &stream->subrequests)
|
| 79 | break;
|
| 80 |
|
| 81 | subreq = list_entry(next, struct netfs_io_subrequest, rreq_link);
|
| 82 | if (subreq->start + subreq->transferred != start + len ||
|
| 83 | test_bit(NETFS_SREQ_BOUNDARY, &subreq->flags) ||
|
| 84 | !test_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags))
|
| 85 | break;
|
| 86 | to = subreq;
|
| 87 | len += to->len;
|
| 88 | }
|
| 89 |
|
| 90 | |
| 91 | * subreq gets a subset of a single overall contiguous buffer.
|
| 92 |
|
| 93 | netfs_reset_iter(from);
|
| 94 | source = from->io_iter;
|
| 95 | source.count = len;
|
| 96 |
|
| 97 |
|
| 98 | subreq = from;
|
| 99 | list_for_each_entry_from(subreq, &stream->subrequests, rreq_link) {
|
| 100 | if (!len)
|
| 101 | break;
|
| 102 |
|
| 103 | subreq->start = start;
|
| 104 | subreq->len = len;
|
| 105 | __clear_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags);
|
| 106 | trace_netfs_sreq(subreq, netfs_sreq_trace_retry);
|
| 107 |
|
| 108 |
|
| 109 | stream->sreq_max_len = len;
|
| 110 | stream->prepare_write(subreq);
|
| 111 |
|
| 112 | part = umin(len, stream->sreq_max_len);
|
| 113 | if (unlikely(stream->sreq_max_segs))
|
| 114 | part = netfs_limit_iter(&source, 0, part, stream->sreq_max_segs);
|
| 115 | subreq->len = part;
|
| 116 | subreq->transferred = 0;
|
| 117 | len -= part;
|
| 118 | start += part;
|
| 119 | if (len && subreq == to &&
|
| 120 | __test_and_clear_bit(NETFS_SREQ_BOUNDARY, &to->flags))
|
| 121 | boundary = true;
|
| 122 |
|
| 123 | netfs_get_subrequest(subreq, netfs_sreq_trace_get_resubmit);
|
| 124 | netfs_reissue_write(stream, subreq, &source);
|
| 125 | if (subreq == to)
|
| 126 | break;
|
| 127 | }
|
| 128 |
|
| 129 | |
| 130 | * excess; if we used the same number, then we're done.
|
| 131 |
|
| 132 | if (!len) {
|
| 133 | if (subreq == to)
|
| 134 | continue;
|
| 135 | list_for_each_entry_safe_from(subreq, tmp,
|
| 136 | &stream->subrequests, rreq_link) {
|
| 137 | trace_netfs_sreq(subreq, netfs_sreq_trace_discard);
|
| 138 | spin_lock(&wreq->lock);
|
| 139 | list_del(&subreq->rreq_link);
|
| 140 | spin_unlock(&wreq->lock);
|
| 141 | netfs_put_subrequest(subreq, netfs_sreq_trace_put_done);
|
| 142 | if (subreq == to)
|
| 143 | break;
|
| 144 | }
|
| 145 | continue;
|
| 146 | }
|
| 147 |
|
| 148 | |
| 149 | * and insert them after.
|
| 150 |
|
| 151 | do {
|
| 152 | subreq = netfs_alloc_subrequest(wreq, stream->source);
|
| 153 | subreq->start = start;
|
| 154 | subreq->stream_nr = to->stream_nr;
|
| 155 | subreq->retry_count = 1;
|
| 156 |
|
| 157 | trace_netfs_sreq_ref(wreq->debug_id, subreq->debug_index,
|
| 158 | refcount_read(&subreq->ref),
|
| 159 | netfs_sreq_trace_new);
|
| 160 | trace_netfs_sreq(subreq, netfs_sreq_trace_split);
|
| 161 |
|
| 162 | spin_lock(&wreq->lock);
|
| 163 | list_add(&subreq->rreq_link, &to->rreq_link);
|
| 164 | spin_unlock(&wreq->lock);
|
| 165 | to = subreq;
|
| 166 | trace_netfs_sreq(subreq, netfs_sreq_trace_retry);
|
| 167 |
|
| 168 | stream->sreq_max_len = len;
|
| 169 | stream->sreq_max_segs = INT_MAX;
|
| 170 | switch (stream->source) {
|
| 171 | case NETFS_UPLOAD_TO_SERVER:
|
| 172 | netfs_stat(&netfs_n_wh_upload);
|
| 173 | stream->sreq_max_len = umin(len, wreq->wsize);
|
| 174 | break;
|
| 175 | case NETFS_WRITE_TO_CACHE:
|
| 176 | netfs_stat(&netfs_n_wh_write);
|
| 177 | break;
|
| 178 | default:
|
| 179 | WARN_ON_ONCE(1);
|
| 180 | }
|
| 181 |
|
| 182 | stream->prepare_write(subreq);
|
| 183 |
|
| 184 | part = umin(len, stream->sreq_max_len);
|
| 185 | subreq->len = subreq->transferred + part;
|
| 186 | len -= part;
|
| 187 | start += part;
|
| 188 | if (!len && boundary) {
|
| 189 | __set_bit(NETFS_SREQ_BOUNDARY, &to->flags);
|
| 190 | boundary = false;
|
| 191 | }
|
| 192 |
|
| 193 | netfs_reissue_write(stream, subreq, &source);
|
| 194 | if (!len)
|
| 195 | break;
|
| 196 |
|
| 197 | } while (len);
|
| 198 |
|
| 199 | } while (!list_is_head(next, &stream->subrequests));
|
| 200 | }
|
| 201 |
|
| 202 | |
| 203 | * Perform retries on the streams that need it. If we're doing content
|
| 204 | * encryption and the server copy changed due to a third-party write, we may
|
| 205 | * need to do an RMW cycle and also rewrite the data to the cache.
|
| 206 |
|
| 207 | void netfs_retry_writes(struct netfs_io_request *wreq)
|
| 208 | {
|
| 209 | struct netfs_io_stream *stream;
|
| 210 | int s;
|
| 211 |
|
| 212 | netfs_stat(&netfs_n_wh_retry_write_req);
|
| 213 | trace_netfs_rreq(wreq, netfs_rreq_trace_retry_begin);
|
| 214 |
|
| 215 | |
| 216 | * we may need to renegotiate the I/O sizes.
|
| 217 |
|
| 218 | set_bit(NETFS_RREQ_RETRYING, &wreq->flags);
|
| 219 | for (s = 0; s < NR_IO_STREAMS; s++) {
|
| 220 | stream = &wreq->io_streams[s];
|
| 221 | if (stream->active)
|
| 222 | netfs_wait_for_in_progress_stream(wreq, stream);
|
| 223 | }
|
| 224 | clear_bit(NETFS_RREQ_RETRYING, &wreq->flags);
|
| 225 |
|
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 |
|
| 231 | for (s = 0; s < NR_IO_STREAMS; s++) {
|
| 232 | stream = &wreq->io_streams[s];
|
| 233 | if (stream->need_retry) {
|
| 234 | stream->need_retry = false;
|
| 235 | netfs_retry_write_stream(wreq, stream);
|
| 236 | }
|
| 237 | }
|
| 238 |
|
| 239 | trace_netfs_rreq(wreq, netfs_rreq_trace_retry_end);
|
| 240 | }
|