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 */ 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 uoff_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 */ 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 */ 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 */ 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 netfs_all_subreqs_queued(creq); 162 trace_netfs_rreq(rreq, netfs_rreq_trace_end_copy_to_cache); 163 if (list_empty_careful(&creq->io_streams[1].subrequests)) 164 netfs_wake_collector(creq); 165 166 netfs_put_request(creq, netfs_rreq_trace_put_return); 167 creq->copy_to_cache = NULL; 168 } 169 170 /* 171 * [DEPRECATED] Remove the PG_private_2 mark from any folios we've finished 172 * copying. 173 */ 174 bool netfs_pgpriv2_unlock_copied_folios(struct netfs_io_request *creq) 175 { 176 struct folio_queue *folioq = creq->buffer.tail; 177 unsigned int slot = creq->buffer.first_tail_slot; 178 uoff_t collected_to = creq->collected_to; 179 bool made_progress = false; 180 181 if (slot >= folioq_nr_slots(folioq)) { 182 folioq = rolling_buffer_delete_spent(&creq->buffer); 183 slot = 0; 184 } 185 186 for (;;) { 187 struct folio *folio; 188 uoff_t fpos, fend; 189 size_t fsize, flen; 190 191 folio = folioq_folio(folioq, slot); 192 if (WARN_ONCE(!folio_test_private_2(folio), 193 "R=%08x: folio %lx is not marked private_2\n", 194 creq->debug_id, folio->index)) 195 trace_netfs_folio(folio, netfs_folio_trace_not_under_wback); 196 197 fpos = folio_pos(folio); 198 fsize = folio_size(folio); 199 flen = fsize; 200 201 fend = min_t(uoff_t, fpos + flen, creq->i_size); 202 203 trace_netfs_collect_folio(creq, folio); 204 205 /* Unlock any folio we've transferred all of. */ 206 if (collected_to < fend) 207 break; 208 209 trace_netfs_folio(folio, netfs_folio_trace_end_copy); 210 folio_end_private_2(folio); 211 creq->cleaned_to = fpos + fsize; 212 made_progress = true; 213 214 /* Clean up the head folioq. If we clear an entire folioq, then 215 * we can get rid of it provided it's not also the tail folioq 216 * being filled by the issuer. 217 */ 218 folioq_clear(folioq, slot); 219 slot++; 220 if (slot >= folioq_nr_slots(folioq)) { 221 folioq = rolling_buffer_delete_spent(&creq->buffer); 222 if (!folioq) 223 goto done; 224 slot = 0; 225 } 226 227 if (fpos + fsize >= collected_to) 228 break; 229 } 230 231 creq->buffer.tail = folioq; 232 done: 233 creq->buffer.first_tail_slot = slot; 234 return made_progress; 235 } 236