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 folio_end_private_2(folio);
58 clear_bit(NETFS_RREQ_FOLIO_COPY_TO_CACHE, &creq->flags);
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 clear_bit(NETFS_RREQ_FOLIO_COPY_TO_CACHE, &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.
132 */
netfs_pgpriv2_copy_to_cache(struct netfs_io_request * rreq,struct folio * folio)133 void netfs_pgpriv2_copy_to_cache(struct netfs_io_request *rreq, struct folio *folio)
134 {
135 struct netfs_io_request *creq = rreq->copy_to_cache;
136
137 if (!creq)
138 creq = netfs_pgpriv2_begin_copy_to_cache(rreq, folio);
139 if (IS_ERR(creq))
140 return;
141
142 trace_netfs_folio(folio, netfs_folio_trace_copy_to_cache);
143 folio_start_private_2(folio);
144 netfs_pgpriv2_copy_folio(creq, folio);
145 }
146
147 /*
148 * [DEPRECATED] End writing to the cache, flushing out any outstanding writes.
149 */
netfs_pgpriv2_end_copy_to_cache(struct netfs_io_request * rreq)150 void netfs_pgpriv2_end_copy_to_cache(struct netfs_io_request *rreq)
151 {
152 struct netfs_io_request *creq = rreq->copy_to_cache;
153
154 if (IS_ERR_OR_NULL(creq))
155 return;
156
157 netfs_issue_write(creq, &creq->io_streams[1]);
158 smp_wmb(); /* Write lists before ALL_QUEUED. */
159 set_bit(NETFS_RREQ_ALL_QUEUED, &creq->flags);
160 trace_netfs_rreq(rreq, netfs_rreq_trace_end_copy_to_cache);
161 if (list_empty_careful(&creq->io_streams[1].subrequests))
162 netfs_wake_collector(creq);
163
164 netfs_put_request(creq, netfs_rreq_trace_put_return);
165 creq->copy_to_cache = NULL;
166 }
167
168 /*
169 * [DEPRECATED] Remove the PG_private_2 mark from any folios we've finished
170 * copying.
171 */
netfs_pgpriv2_unlock_copied_folios(struct netfs_io_request * creq)172 bool netfs_pgpriv2_unlock_copied_folios(struct netfs_io_request *creq)
173 {
174 struct folio_queue *folioq = creq->buffer.tail;
175 unsigned long long collected_to = creq->collected_to;
176 unsigned int slot = creq->buffer.first_tail_slot;
177 bool made_progress = false;
178
179 if (slot >= folioq_nr_slots(folioq)) {
180 folioq = rolling_buffer_delete_spent(&creq->buffer);
181 slot = 0;
182 }
183
184 for (;;) {
185 struct folio *folio;
186 unsigned long long fpos, fend;
187 size_t fsize, flen;
188
189 folio = folioq_folio(folioq, slot);
190 if (WARN_ONCE(!folio_test_private_2(folio),
191 "R=%08x: folio %lx is not marked private_2\n",
192 creq->debug_id, folio->index))
193 trace_netfs_folio(folio, netfs_folio_trace_not_under_wback);
194
195 fpos = folio_pos(folio);
196 fsize = folio_size(folio);
197 flen = fsize;
198
199 fend = min_t(unsigned long long, fpos + flen, creq->i_size);
200
201 trace_netfs_collect_folio(creq, folio, fend, collected_to);
202
203 /* Unlock any folio we've transferred all of. */
204 if (collected_to < fend)
205 break;
206
207 trace_netfs_folio(folio, netfs_folio_trace_end_copy);
208 folio_end_private_2(folio);
209 creq->cleaned_to = fpos + fsize;
210 made_progress = true;
211
212 /* Clean up the head folioq. If we clear an entire folioq, then
213 * we can get rid of it provided it's not also the tail folioq
214 * being filled by the issuer.
215 */
216 folioq_clear(folioq, slot);
217 slot++;
218 if (slot >= folioq_nr_slots(folioq)) {
219 folioq = rolling_buffer_delete_spent(&creq->buffer);
220 if (!folioq)
221 goto done;
222 slot = 0;
223 }
224
225 if (fpos + fsize >= collected_to)
226 break;
227 }
228
229 creq->buffer.tail = folioq;
230 done:
231 creq->buffer.first_tail_slot = slot;
232 return made_progress;
233 }
234