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 * Attach a folio to the buffer and maybe set marks on it to say that we need
13 * to put the folio later and twiddle the pagecache flags.
15 int netfs_xa_store_and_mark(struct xarray *xa, unsigned long index,
16 struct folio *folio, unsigned int flags,
19 XA_STATE_ORDER(xas, xa, index, folio_order(folio));
24 xas_store(&xas, folio);
28 if (!xas_nomem(&xas, gfp_mask))
29 return xas_error(&xas);
33 if (flags & NETFS_FLAG_PUT_MARK)
34 xas_set_mark(&xas, NETFS_BUF_PUT_MARK);
35 if (flags & NETFS_FLAG_PAGECACHE_MARK)
36 xas_set_mark(&xas, NETFS_BUF_PAGECACHE_MARK);
38 return xas_error(&xas);
42 * Create the specified range of folios in the buffer attached to the read
43 * request. The folios are marked with NETFS_BUF_PUT_MARK so that we know that
44 * these need freeing later.
46 int netfs_add_folios_to_buffer(struct xarray *buffer,
47 struct address_space *mapping,
48 pgoff_t index, pgoff_t to, gfp_t gfp_mask)
53 if (to + 1 == index) /* Page range is inclusive */
57 /* TODO: Figure out what order folio can be allocated here */
58 folio = filemap_alloc_folio(readahead_gfp_mask(mapping), 0);
62 ret = netfs_xa_store_and_mark(buffer, index, folio,
63 NETFS_FLAG_PUT_MARK, gfp_mask);
69 index += folio_nr_pages(folio);
70 } while (index <= to && index != 0);
76 * Clear an xarray buffer, putting a ref on the folios that have
77 * NETFS_BUF_PUT_MARK set.
79 void netfs_clear_buffer(struct xarray *buffer)
82 XA_STATE(xas, buffer, 0);
85 xas_for_each_marked(&xas, folio, ULONG_MAX, NETFS_BUF_PUT_MARK) {
93 * netfs_dirty_folio - Mark folio dirty and pin a cache object for writeback
94 * @mapping: The mapping the folio belongs to.
95 * @folio: The folio being dirtied.
97 * Set the dirty flag on a folio and pin an in-use cache object in memory so
98 * that writeback can later write to it. This is intended to be called from
99 * the filesystem's ->dirty_folio() method.
101 * Return: true if the dirty flag was set on the folio, false otherwise.
103 bool netfs_dirty_folio(struct address_space *mapping, struct folio *folio)
105 struct inode *inode = mapping->host;
106 struct netfs_inode *ictx = netfs_inode(inode);
107 struct fscache_cookie *cookie = netfs_i_cookie(ictx);
108 bool need_use = false;
112 if (!filemap_dirty_folio(mapping, folio))
114 if (!fscache_cookie_valid(cookie))
117 if (!(inode->i_state & I_PINNING_NETFS_WB)) {
118 spin_lock(&inode->i_lock);
119 if (!(inode->i_state & I_PINNING_NETFS_WB)) {
120 inode->i_state |= I_PINNING_NETFS_WB;
123 spin_unlock(&inode->i_lock);
126 fscache_use_cookie(cookie, true);
130 EXPORT_SYMBOL(netfs_dirty_folio);
133 * netfs_unpin_writeback - Unpin writeback resources
134 * @inode: The inode on which the cookie resides
135 * @wbc: The writeback control
137 * Unpin the writeback resources pinned by netfs_dirty_folio(). This is
138 * intended to be called as/by the netfs's ->write_inode() method.
140 int netfs_unpin_writeback(struct inode *inode, struct writeback_control *wbc)
142 struct fscache_cookie *cookie = netfs_i_cookie(netfs_inode(inode));
144 if (wbc->unpinned_netfs_wb)
145 fscache_unuse_cookie(cookie, NULL, NULL);
148 EXPORT_SYMBOL(netfs_unpin_writeback);
151 * netfs_clear_inode_writeback - Clear writeback resources pinned by an inode
152 * @inode: The inode to clean up
153 * @aux: Auxiliary data to apply to the inode
155 * Clear any writeback resources held by an inode when the inode is evicted.
156 * This must be called before clear_inode() is called.
158 void netfs_clear_inode_writeback(struct inode *inode, const void *aux)
160 struct fscache_cookie *cookie = netfs_i_cookie(netfs_inode(inode));
162 if (inode->i_state & I_PINNING_NETFS_WB) {
163 loff_t i_size = i_size_read(inode);
164 fscache_unuse_cookie(cookie, aux, &i_size);
167 EXPORT_SYMBOL(netfs_clear_inode_writeback);
170 * netfs_invalidate_folio - Invalidate or partially invalidate a folio
171 * @folio: Folio proposed for release
172 * @offset: Offset of the invalidated region
173 * @length: Length of the invalidated region
175 * Invalidate part or all of a folio for a network filesystem. The folio will
176 * be removed afterwards if the invalidated region covers the entire folio.
178 void netfs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
180 struct netfs_folio *finfo = NULL;
181 size_t flen = folio_size(folio);
183 _enter("{%lx},%zx,%zx", folio->index, offset, length);
185 folio_wait_fscache(folio);
187 if (!folio_test_private(folio))
190 finfo = netfs_folio_info(folio);
192 if (offset == 0 && length >= flen)
193 goto erase_completely;
196 /* We have a partially uptodate page from a streaming write. */
197 unsigned int fstart = finfo->dirty_offset;
198 unsigned int fend = fstart + finfo->dirty_len;
199 unsigned int end = offset + length;
205 if (offset <= fstart && end >= fend)
206 goto erase_completely;
207 if (offset <= fstart && end > fstart)
209 if (offset > fstart && end >= fend)
211 /* A partial write was split. The caller has already zeroed
212 * it, so just absorb the hole.
218 netfs_put_group(netfs_folio_group(folio));
219 folio_detach_private(folio);
220 folio_clear_uptodate(folio);
224 finfo->dirty_len = offset + length - finfo->dirty_offset;
227 finfo->dirty_len -= offset - finfo->dirty_offset;
228 finfo->dirty_offset = offset;
230 EXPORT_SYMBOL(netfs_invalidate_folio);
233 * netfs_release_folio - Try to release a folio
234 * @folio: Folio proposed for release
235 * @gfp: Flags qualifying the release
237 * Request release of a folio and clean up its private state if it's not busy.
238 * Returns true if the folio can now be released, false if not
240 bool netfs_release_folio(struct folio *folio, gfp_t gfp)
242 struct netfs_inode *ctx = netfs_inode(folio_inode(folio));
243 unsigned long long end;
245 end = folio_pos(folio) + folio_size(folio);
246 if (end > ctx->zero_point)
247 ctx->zero_point = end;
249 if (folio_test_private(folio))
251 if (folio_test_fscache(folio)) {
252 if (current_is_kswapd() || !(gfp & __GFP_FS))
254 folio_wait_fscache(folio);
257 fscache_note_page_release(netfs_i_cookie(ctx));
260 EXPORT_SYMBOL(netfs_release_folio);