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