xref: /linux/fs/netfs/misc.c (revision a0aab7d7c8605f53ea77dfaafec1dcc0bfb1f232)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Miscellaneous routines.
3  *
4  * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #include <linux/swap.h>
9 #include "internal.h"
10 
11 /**
12  * netfs_dirty_folio - Mark folio dirty and pin a cache object for writeback
13  * @mapping: The mapping the folio belongs to.
14  * @folio: The folio being dirtied.
15  *
16  * Set the dirty flag on a folio and pin an in-use cache object in memory so
17  * that writeback can later write to it.  This is intended to be called from
18  * the filesystem's ->dirty_folio() method.
19  *
20  * Return: true if the dirty flag was set on the folio, false otherwise.
21  */
22 bool netfs_dirty_folio(struct address_space *mapping, struct folio *folio)
23 {
24 	struct inode *inode = mapping->host;
25 	struct netfs_inode *ictx = netfs_inode(inode);
26 	struct fscache_cookie *cookie = netfs_i_cookie(ictx);
27 	bool need_use = false;
28 
29 	_enter("");
30 
31 	if (!filemap_dirty_folio(mapping, folio))
32 		return false;
33 	if (!fscache_cookie_valid(cookie))
34 		return true;
35 
36 	if (!(inode->i_state & I_PINNING_NETFS_WB)) {
37 		spin_lock(&inode->i_lock);
38 		if (!(inode->i_state & I_PINNING_NETFS_WB)) {
39 			inode->i_state |= I_PINNING_NETFS_WB;
40 			need_use = true;
41 		}
42 		spin_unlock(&inode->i_lock);
43 
44 		if (need_use)
45 			fscache_use_cookie(cookie, true);
46 	}
47 	return true;
48 }
49 EXPORT_SYMBOL(netfs_dirty_folio);
50 
51 /**
52  * netfs_unpin_writeback - Unpin writeback resources
53  * @inode: The inode on which the cookie resides
54  * @wbc: The writeback control
55  *
56  * Unpin the writeback resources pinned by netfs_dirty_folio().  This is
57  * intended to be called as/by the netfs's ->write_inode() method.
58  */
59 int netfs_unpin_writeback(struct inode *inode, struct writeback_control *wbc)
60 {
61 	struct fscache_cookie *cookie = netfs_i_cookie(netfs_inode(inode));
62 
63 	if (wbc->unpinned_netfs_wb)
64 		fscache_unuse_cookie(cookie, NULL, NULL);
65 	return 0;
66 }
67 EXPORT_SYMBOL(netfs_unpin_writeback);
68 
69 /**
70  * netfs_clear_inode_writeback - Clear writeback resources pinned by an inode
71  * @inode: The inode to clean up
72  * @aux: Auxiliary data to apply to the inode
73  *
74  * Clear any writeback resources held by an inode when the inode is evicted.
75  * This must be called before clear_inode() is called.
76  */
77 void netfs_clear_inode_writeback(struct inode *inode, const void *aux)
78 {
79 	struct fscache_cookie *cookie = netfs_i_cookie(netfs_inode(inode));
80 
81 	if (inode->i_state & I_PINNING_NETFS_WB) {
82 		loff_t i_size = i_size_read(inode);
83 		fscache_unuse_cookie(cookie, aux, &i_size);
84 	}
85 }
86 EXPORT_SYMBOL(netfs_clear_inode_writeback);
87 
88 /**
89  * netfs_invalidate_folio - Invalidate or partially invalidate a folio
90  * @folio: Folio proposed for release
91  * @offset: Offset of the invalidated region
92  * @length: Length of the invalidated region
93  *
94  * Invalidate part or all of a folio for a network filesystem.  The folio will
95  * be removed afterwards if the invalidated region covers the entire folio.
96  */
97 void netfs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
98 {
99 	struct netfs_folio *finfo;
100 	size_t flen = folio_size(folio);
101 
102 	_enter("{%lx},%zx,%zx", folio->index, offset, length);
103 
104 	if (!folio_test_private(folio))
105 		return;
106 
107 	finfo = netfs_folio_info(folio);
108 
109 	if (offset == 0 && length >= flen)
110 		goto erase_completely;
111 
112 	if (finfo) {
113 		/* We have a partially uptodate page from a streaming write. */
114 		unsigned int fstart = finfo->dirty_offset;
115 		unsigned int fend = fstart + finfo->dirty_len;
116 		unsigned int end = offset + length;
117 
118 		if (offset >= fend)
119 			return;
120 		if (end <= fstart)
121 			return;
122 		if (offset <= fstart && end >= fend)
123 			goto erase_completely;
124 		if (offset <= fstart && end > fstart)
125 			goto reduce_len;
126 		if (offset > fstart && end >= fend)
127 			goto move_start;
128 		/* A partial write was split.  The caller has already zeroed
129 		 * it, so just absorb the hole.
130 		 */
131 	}
132 	return;
133 
134 erase_completely:
135 	netfs_put_group(netfs_folio_group(folio));
136 	folio_detach_private(folio);
137 	folio_clear_uptodate(folio);
138 	kfree(finfo);
139 	return;
140 reduce_len:
141 	finfo->dirty_len = offset + length - finfo->dirty_offset;
142 	return;
143 move_start:
144 	finfo->dirty_len -= offset - finfo->dirty_offset;
145 	finfo->dirty_offset = offset;
146 }
147 EXPORT_SYMBOL(netfs_invalidate_folio);
148 
149 /**
150  * netfs_release_folio - Try to release a folio
151  * @folio: Folio proposed for release
152  * @gfp: Flags qualifying the release
153  *
154  * Request release of a folio and clean up its private state if it's not busy.
155  * Returns true if the folio can now be released, false if not
156  */
157 bool netfs_release_folio(struct folio *folio, gfp_t gfp)
158 {
159 	struct netfs_inode *ctx = netfs_inode(folio_inode(folio));
160 	unsigned long long end;
161 
162 	end = folio_pos(folio) + folio_size(folio);
163 	if (end > ctx->zero_point)
164 		ctx->zero_point = end;
165 
166 	if (folio_test_private(folio))
167 		return false;
168 	fscache_note_page_release(netfs_i_cookie(ctx));
169 	return true;
170 }
171 EXPORT_SYMBOL(netfs_release_folio);
172