1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Miscellaneous routines.
4 * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved.
8 #include <linux/swap.h>
12 * Make sure there's space in the rolling queue.
14 struct folio_queue *netfs_buffer_make_space(struct netfs_io_request *rreq)
16 struct folio_queue *tail = rreq->buffer_tail, *prev;
17 unsigned int prev_nr_slots = 0;
19 if (WARN_ON_ONCE(!rreq->buffer && tail) ||
20 WARN_ON_ONCE(rreq->buffer && !tail))
25 if (!folioq_full(tail))
27 prev_nr_slots = folioq_nr_slots(tail);
30 tail = kmalloc(sizeof(*tail), GFP_NOFS);
32 return ERR_PTR(-ENOMEM);
33 netfs_stat(&netfs_n_folioq);
37 /* [!] NOTE: After we set prev->next, the consumer is entirely
38 * at liberty to delete prev.
40 WRITE_ONCE(prev->next, tail);
42 rreq->buffer_tail = tail;
45 iov_iter_folio_queue(&rreq->io_iter, ITER_SOURCE, tail, 0, 0, 0);
47 /* Make sure we don't leave the master iterator pointing to a
48 * block that might get immediately consumed.
50 if (rreq->io_iter.folioq == prev &&
51 rreq->io_iter.folioq_slot == prev_nr_slots) {
52 rreq->io_iter.folioq = tail;
53 rreq->io_iter.folioq_slot = 0;
56 rreq->buffer_tail_slot = 0;
61 * Append a folio to the rolling queue.
63 int netfs_buffer_append_folio(struct netfs_io_request *rreq, struct folio *folio,
66 struct folio_queue *tail;
67 unsigned int slot, order = folio_order(folio);
69 tail = netfs_buffer_make_space(rreq);
73 rreq->io_iter.count += PAGE_SIZE << order;
75 slot = folioq_append(tail, folio);
76 /* Store the counter after setting the slot. */
77 smp_store_release(&rreq->buffer_tail_slot, slot);
82 * Delete the head of a rolling queue.
84 struct folio_queue *netfs_delete_buffer_head(struct netfs_io_request *wreq)
86 struct folio_queue *head = wreq->buffer, *next = head->next;
90 netfs_stat_d(&netfs_n_folioq);
97 * Clear out a rolling queue.
99 void netfs_clear_buffer(struct netfs_io_request *rreq)
101 struct folio_queue *p;
103 while ((p = rreq->buffer)) {
104 rreq->buffer = p->next;
105 for (int slot = 0; slot < folioq_count(p); slot++) {
106 struct folio *folio = folioq_folio(p, slot);
109 if (folioq_is_marked(p, slot)) {
110 trace_netfs_folio(folio, netfs_folio_trace_put);
114 netfs_stat_d(&netfs_n_folioq);
120 * Reset the subrequest iterator to refer just to the region remaining to be
121 * read. The iterator may or may not have been advanced by socket ops or
122 * extraction ops to an extent that may or may not match the amount actually
125 void netfs_reset_iter(struct netfs_io_subrequest *subreq)
127 struct iov_iter *io_iter = &subreq->io_iter;
128 size_t remain = subreq->len - subreq->transferred;
130 if (io_iter->count > remain)
131 iov_iter_advance(io_iter, io_iter->count - remain);
132 else if (io_iter->count < remain)
133 iov_iter_revert(io_iter, remain - io_iter->count);
134 iov_iter_truncate(&subreq->io_iter, remain);
138 * netfs_dirty_folio - Mark folio dirty and pin a cache object for writeback
139 * @mapping: The mapping the folio belongs to.
140 * @folio: The folio being dirtied.
142 * Set the dirty flag on a folio and pin an in-use cache object in memory so
143 * that writeback can later write to it. This is intended to be called from
144 * the filesystem's ->dirty_folio() method.
146 * Return: true if the dirty flag was set on the folio, false otherwise.
148 bool netfs_dirty_folio(struct address_space *mapping, struct folio *folio)
150 struct inode *inode = mapping->host;
151 struct netfs_inode *ictx = netfs_inode(inode);
152 struct fscache_cookie *cookie = netfs_i_cookie(ictx);
153 bool need_use = false;
157 if (!filemap_dirty_folio(mapping, folio))
159 if (!fscache_cookie_valid(cookie))
162 if (!(inode->i_state & I_PINNING_NETFS_WB)) {
163 spin_lock(&inode->i_lock);
164 if (!(inode->i_state & I_PINNING_NETFS_WB)) {
165 inode->i_state |= I_PINNING_NETFS_WB;
168 spin_unlock(&inode->i_lock);
171 fscache_use_cookie(cookie, true);
175 EXPORT_SYMBOL(netfs_dirty_folio);
178 * netfs_unpin_writeback - Unpin writeback resources
179 * @inode: The inode on which the cookie resides
180 * @wbc: The writeback control
182 * Unpin the writeback resources pinned by netfs_dirty_folio(). This is
183 * intended to be called as/by the netfs's ->write_inode() method.
185 int netfs_unpin_writeback(struct inode *inode, struct writeback_control *wbc)
187 struct fscache_cookie *cookie = netfs_i_cookie(netfs_inode(inode));
189 if (wbc->unpinned_netfs_wb)
190 fscache_unuse_cookie(cookie, NULL, NULL);
193 EXPORT_SYMBOL(netfs_unpin_writeback);
196 * netfs_clear_inode_writeback - Clear writeback resources pinned by an inode
197 * @inode: The inode to clean up
198 * @aux: Auxiliary data to apply to the inode
200 * Clear any writeback resources held by an inode when the inode is evicted.
201 * This must be called before clear_inode() is called.
203 void netfs_clear_inode_writeback(struct inode *inode, const void *aux)
205 struct fscache_cookie *cookie = netfs_i_cookie(netfs_inode(inode));
207 if (inode->i_state & I_PINNING_NETFS_WB) {
208 loff_t i_size = i_size_read(inode);
209 fscache_unuse_cookie(cookie, aux, &i_size);
212 EXPORT_SYMBOL(netfs_clear_inode_writeback);
215 * netfs_invalidate_folio - Invalidate or partially invalidate a folio
216 * @folio: Folio proposed for release
217 * @offset: Offset of the invalidated region
218 * @length: Length of the invalidated region
220 * Invalidate part or all of a folio for a network filesystem. The folio will
221 * be removed afterwards if the invalidated region covers the entire folio.
223 void netfs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
225 struct netfs_folio *finfo;
226 struct netfs_inode *ctx = netfs_inode(folio_inode(folio));
227 size_t flen = folio_size(folio);
229 _enter("{%lx},%zx,%zx", folio->index, offset, length);
231 if (offset == 0 && length == flen) {
232 unsigned long long i_size = i_size_read(&ctx->inode);
233 unsigned long long fpos = folio_pos(folio), end;
235 end = umin(fpos + flen, i_size);
236 if (fpos < i_size && end > ctx->zero_point)
237 ctx->zero_point = end;
240 folio_wait_private_2(folio); /* [DEPRECATED] */
242 if (!folio_test_private(folio))
245 finfo = netfs_folio_info(folio);
247 if (offset == 0 && length >= flen)
248 goto erase_completely;
251 /* We have a partially uptodate page from a streaming write. */
252 unsigned int fstart = finfo->dirty_offset;
253 unsigned int fend = fstart + finfo->dirty_len;
254 unsigned int iend = offset + length;
261 /* The invalidation region overlaps the data. If the region
262 * covers the start of the data, we either move along the start
263 * or just erase the data entirely.
265 if (offset <= fstart) {
267 goto erase_completely;
268 /* Move the start of the data. */
269 finfo->dirty_len = fend - iend;
270 finfo->dirty_offset = offset;
274 /* Reduce the length of the data if the invalidation region
275 * covers the tail part.
278 finfo->dirty_len = offset - fstart;
282 /* A partial write was split. The caller has already zeroed
283 * it, so just absorb the hole.
289 netfs_put_group(netfs_folio_group(folio));
290 folio_detach_private(folio);
291 folio_clear_uptodate(folio);
295 EXPORT_SYMBOL(netfs_invalidate_folio);
298 * netfs_release_folio - Try to release a folio
299 * @folio: Folio proposed for release
300 * @gfp: Flags qualifying the release
302 * Request release of a folio and clean up its private state if it's not busy.
303 * Returns true if the folio can now be released, false if not
305 bool netfs_release_folio(struct folio *folio, gfp_t gfp)
307 struct netfs_inode *ctx = netfs_inode(folio_inode(folio));
308 unsigned long long end;
310 if (folio_test_dirty(folio))
313 end = umin(folio_pos(folio) + folio_size(folio), i_size_read(&ctx->inode));
314 if (end > ctx->zero_point)
315 ctx->zero_point = end;
317 if (folio_test_private(folio))
319 if (unlikely(folio_test_private_2(folio))) { /* [DEPRECATED] */
320 if (current_is_kswapd() || !(gfp & __GFP_FS))
322 folio_wait_private_2(folio);
324 fscache_note_page_release(netfs_i_cookie(ctx));
327 EXPORT_SYMBOL(netfs_release_folio);