xref: /openbmc/linux/fs/fscache/io.c (revision 08276bda)
1d64f4554SDavid Howells // SPDX-License-Identifier: GPL-2.0-or-later
2d64f4554SDavid Howells /* Cache data I/O routines
3d64f4554SDavid Howells  *
4d64f4554SDavid Howells  * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5d64f4554SDavid Howells  * Written by David Howells (dhowells@redhat.com)
6d64f4554SDavid Howells  */
7d64f4554SDavid Howells #define FSCACHE_DEBUG_LEVEL OPERATION
8d64f4554SDavid Howells #include <linux/fscache-cache.h>
9d64f4554SDavid Howells #include <linux/uio.h>
10d64f4554SDavid Howells #include <linux/bvec.h>
11d64f4554SDavid Howells #include <linux/slab.h>
12d64f4554SDavid Howells #include <linux/uio.h>
13d64f4554SDavid Howells #include "internal.h"
14d64f4554SDavid Howells 
15d64f4554SDavid Howells /**
16d64f4554SDavid Howells  * fscache_wait_for_operation - Wait for an object become accessible
17d64f4554SDavid Howells  * @cres: The cache resources for the operation being performed
18d64f4554SDavid Howells  * @want_state: The minimum state the object must be at
19d64f4554SDavid Howells  *
20d64f4554SDavid Howells  * See if the target cache object is at the specified minimum state of
21d64f4554SDavid Howells  * accessibility yet, and if not, wait for it.
22d64f4554SDavid Howells  */
23d64f4554SDavid Howells bool fscache_wait_for_operation(struct netfs_cache_resources *cres,
24d64f4554SDavid Howells 				enum fscache_want_state want_state)
25d64f4554SDavid Howells {
26d64f4554SDavid Howells 	struct fscache_cookie *cookie = fscache_cres_cookie(cres);
27d64f4554SDavid Howells 	enum fscache_cookie_state state;
28d64f4554SDavid Howells 
29d64f4554SDavid Howells again:
30d64f4554SDavid Howells 	if (!fscache_cache_is_live(cookie->volume->cache)) {
31d64f4554SDavid Howells 		_leave(" [broken]");
32d64f4554SDavid Howells 		return false;
33d64f4554SDavid Howells 	}
34d64f4554SDavid Howells 
35d64f4554SDavid Howells 	state = fscache_cookie_state(cookie);
36d64f4554SDavid Howells 	_enter("c=%08x{%u},%x", cookie->debug_id, state, want_state);
37d64f4554SDavid Howells 
38d64f4554SDavid Howells 	switch (state) {
39d64f4554SDavid Howells 	case FSCACHE_COOKIE_STATE_CREATING:
40d64f4554SDavid Howells 	case FSCACHE_COOKIE_STATE_INVALIDATING:
41d64f4554SDavid Howells 		if (want_state == FSCACHE_WANT_PARAMS)
42d64f4554SDavid Howells 			goto ready; /* There can be no content */
43d64f4554SDavid Howells 		fallthrough;
44d64f4554SDavid Howells 	case FSCACHE_COOKIE_STATE_LOOKING_UP:
45d64f4554SDavid Howells 	case FSCACHE_COOKIE_STATE_LRU_DISCARDING:
46d64f4554SDavid Howells 		wait_var_event(&cookie->state,
47d64f4554SDavid Howells 			       fscache_cookie_state(cookie) != state);
48d64f4554SDavid Howells 		goto again;
49d64f4554SDavid Howells 
50d64f4554SDavid Howells 	case FSCACHE_COOKIE_STATE_ACTIVE:
51d64f4554SDavid Howells 		goto ready;
52d64f4554SDavid Howells 	case FSCACHE_COOKIE_STATE_DROPPED:
53d64f4554SDavid Howells 	case FSCACHE_COOKIE_STATE_RELINQUISHING:
54d64f4554SDavid Howells 	default:
55d64f4554SDavid Howells 		_leave(" [not live]");
56d64f4554SDavid Howells 		return false;
57d64f4554SDavid Howells 	}
58d64f4554SDavid Howells 
59d64f4554SDavid Howells ready:
60d64f4554SDavid Howells 	if (!cres->cache_priv2)
61d64f4554SDavid Howells 		return cookie->volume->cache->ops->begin_operation(cres, want_state);
62d64f4554SDavid Howells 	return true;
63d64f4554SDavid Howells }
64d64f4554SDavid Howells EXPORT_SYMBOL(fscache_wait_for_operation);
65d64f4554SDavid Howells 
66d64f4554SDavid Howells /*
67d64f4554SDavid Howells  * Begin an I/O operation on the cache, waiting till we reach the right state.
68d64f4554SDavid Howells  *
69d64f4554SDavid Howells  * Attaches the resources required to the operation resources record.
70d64f4554SDavid Howells  */
71d64f4554SDavid Howells static int fscache_begin_operation(struct netfs_cache_resources *cres,
72d64f4554SDavid Howells 				   struct fscache_cookie *cookie,
73d64f4554SDavid Howells 				   enum fscache_want_state want_state,
74d64f4554SDavid Howells 				   enum fscache_access_trace why)
75d64f4554SDavid Howells {
76d64f4554SDavid Howells 	enum fscache_cookie_state state;
77d64f4554SDavid Howells 	long timeo;
78d64f4554SDavid Howells 	bool once_only = false;
79d64f4554SDavid Howells 
80d64f4554SDavid Howells 	cres->ops		= NULL;
81d64f4554SDavid Howells 	cres->cache_priv	= cookie;
82d64f4554SDavid Howells 	cres->cache_priv2	= NULL;
83d64f4554SDavid Howells 	cres->debug_id		= cookie->debug_id;
84d64f4554SDavid Howells 	cres->inval_counter	= cookie->inval_counter;
85d64f4554SDavid Howells 
86d64f4554SDavid Howells 	if (!fscache_begin_cookie_access(cookie, why))
87d64f4554SDavid Howells 		return -ENOBUFS;
88d64f4554SDavid Howells 
89d64f4554SDavid Howells again:
90d64f4554SDavid Howells 	spin_lock(&cookie->lock);
91d64f4554SDavid Howells 
92d64f4554SDavid Howells 	state = fscache_cookie_state(cookie);
93d64f4554SDavid Howells 	_enter("c=%08x{%u},%x", cookie->debug_id, state, want_state);
94d64f4554SDavid Howells 
95d64f4554SDavid Howells 	switch (state) {
96d64f4554SDavid Howells 	case FSCACHE_COOKIE_STATE_LOOKING_UP:
97d64f4554SDavid Howells 	case FSCACHE_COOKIE_STATE_LRU_DISCARDING:
98d64f4554SDavid Howells 	case FSCACHE_COOKIE_STATE_INVALIDATING:
99d64f4554SDavid Howells 		goto wait_for_file_wrangling;
100d64f4554SDavid Howells 	case FSCACHE_COOKIE_STATE_CREATING:
101d64f4554SDavid Howells 		if (want_state == FSCACHE_WANT_PARAMS)
102d64f4554SDavid Howells 			goto ready; /* There can be no content */
103d64f4554SDavid Howells 		goto wait_for_file_wrangling;
104d64f4554SDavid Howells 	case FSCACHE_COOKIE_STATE_ACTIVE:
105d64f4554SDavid Howells 		goto ready;
106d64f4554SDavid Howells 	case FSCACHE_COOKIE_STATE_DROPPED:
107d64f4554SDavid Howells 	case FSCACHE_COOKIE_STATE_RELINQUISHING:
108d64f4554SDavid Howells 		WARN(1, "Can't use cookie in state %u\n", cookie->state);
109d64f4554SDavid Howells 		goto not_live;
110d64f4554SDavid Howells 	default:
111d64f4554SDavid Howells 		goto not_live;
112d64f4554SDavid Howells 	}
113d64f4554SDavid Howells 
114d64f4554SDavid Howells ready:
115d64f4554SDavid Howells 	spin_unlock(&cookie->lock);
116d64f4554SDavid Howells 	if (!cookie->volume->cache->ops->begin_operation(cres, want_state))
117d64f4554SDavid Howells 		goto failed;
118d64f4554SDavid Howells 	return 0;
119d64f4554SDavid Howells 
120d64f4554SDavid Howells wait_for_file_wrangling:
121d64f4554SDavid Howells 	spin_unlock(&cookie->lock);
122d64f4554SDavid Howells 	trace_fscache_access(cookie->debug_id, refcount_read(&cookie->ref),
123d64f4554SDavid Howells 			     atomic_read(&cookie->n_accesses),
124d64f4554SDavid Howells 			     fscache_access_io_wait);
125d64f4554SDavid Howells 	timeo = wait_var_event_timeout(&cookie->state,
126d64f4554SDavid Howells 				       fscache_cookie_state(cookie) != state, 20 * HZ);
127d64f4554SDavid Howells 	if (timeo <= 1 && !once_only) {
128d64f4554SDavid Howells 		pr_warn("%s: cookie state change wait timed out: cookie->state=%u state=%u",
129d64f4554SDavid Howells 			__func__, fscache_cookie_state(cookie), state);
130d64f4554SDavid Howells 		fscache_print_cookie(cookie, 'O');
131d64f4554SDavid Howells 		once_only = true;
132d64f4554SDavid Howells 	}
133d64f4554SDavid Howells 	goto again;
134d64f4554SDavid Howells 
135d64f4554SDavid Howells not_live:
136d64f4554SDavid Howells 	spin_unlock(&cookie->lock);
137d64f4554SDavid Howells failed:
138d64f4554SDavid Howells 	cres->cache_priv = NULL;
139d64f4554SDavid Howells 	cres->ops = NULL;
140d64f4554SDavid Howells 	fscache_end_cookie_access(cookie, fscache_access_io_not_live);
141d64f4554SDavid Howells 	_leave(" = -ENOBUFS");
142d64f4554SDavid Howells 	return -ENOBUFS;
143d64f4554SDavid Howells }
144d64f4554SDavid Howells 
145d64f4554SDavid Howells int __fscache_begin_read_operation(struct netfs_cache_resources *cres,
146d64f4554SDavid Howells 				   struct fscache_cookie *cookie)
147d64f4554SDavid Howells {
148d64f4554SDavid Howells 	return fscache_begin_operation(cres, cookie, FSCACHE_WANT_PARAMS,
149d64f4554SDavid Howells 				       fscache_access_io_read);
150d64f4554SDavid Howells }
151d64f4554SDavid Howells EXPORT_SYMBOL(__fscache_begin_read_operation);
152b6e16652SDavid Howells 
153*08276bdaSDavid Howells /**
154*08276bdaSDavid Howells  * fscache_set_page_dirty - Mark page dirty and pin a cache object for writeback
155*08276bdaSDavid Howells  * @page: The page being dirtied
156*08276bdaSDavid Howells  * @cookie: The cookie referring to the cache object
157*08276bdaSDavid Howells  *
158*08276bdaSDavid Howells  * Set the dirty flag on a page and pin an in-use cache object in memory when
159*08276bdaSDavid Howells  * dirtying a page so that writeback can later write to it.  This is intended
160*08276bdaSDavid Howells  * to be called from the filesystem's ->set_page_dirty() method.
161*08276bdaSDavid Howells  *
162*08276bdaSDavid Howells  *  Returns 1 if PG_dirty was set on the page, 0 otherwise.
163*08276bdaSDavid Howells  */
164*08276bdaSDavid Howells int fscache_set_page_dirty(struct page *page, struct fscache_cookie *cookie)
165*08276bdaSDavid Howells {
166*08276bdaSDavid Howells 	struct inode *inode = page->mapping->host;
167*08276bdaSDavid Howells 	bool need_use = false;
168*08276bdaSDavid Howells 
169*08276bdaSDavid Howells 	_enter("");
170*08276bdaSDavid Howells 
171*08276bdaSDavid Howells 	if (!__set_page_dirty_nobuffers(page))
172*08276bdaSDavid Howells 		return 0;
173*08276bdaSDavid Howells 	if (!fscache_cookie_valid(cookie))
174*08276bdaSDavid Howells 		return 1;
175*08276bdaSDavid Howells 
176*08276bdaSDavid Howells 	if (!(inode->i_state & I_PINNING_FSCACHE_WB)) {
177*08276bdaSDavid Howells 		spin_lock(&inode->i_lock);
178*08276bdaSDavid Howells 		if (!(inode->i_state & I_PINNING_FSCACHE_WB)) {
179*08276bdaSDavid Howells 			inode->i_state |= I_PINNING_FSCACHE_WB;
180*08276bdaSDavid Howells 			need_use = true;
181*08276bdaSDavid Howells 		}
182*08276bdaSDavid Howells 		spin_unlock(&inode->i_lock);
183*08276bdaSDavid Howells 
184*08276bdaSDavid Howells 		if (need_use)
185*08276bdaSDavid Howells 			fscache_use_cookie(cookie, true);
186*08276bdaSDavid Howells 	}
187*08276bdaSDavid Howells 	return 1;
188*08276bdaSDavid Howells }
189*08276bdaSDavid Howells EXPORT_SYMBOL(fscache_set_page_dirty);
190*08276bdaSDavid Howells 
191b6e16652SDavid Howells struct fscache_write_request {
192b6e16652SDavid Howells 	struct netfs_cache_resources cache_resources;
193b6e16652SDavid Howells 	struct address_space	*mapping;
194b6e16652SDavid Howells 	loff_t			start;
195b6e16652SDavid Howells 	size_t			len;
196b6e16652SDavid Howells 	bool			set_bits;
197b6e16652SDavid Howells 	netfs_io_terminated_t	term_func;
198b6e16652SDavid Howells 	void			*term_func_priv;
199b6e16652SDavid Howells };
200b6e16652SDavid Howells 
201b6e16652SDavid Howells void __fscache_clear_page_bits(struct address_space *mapping,
202b6e16652SDavid Howells 			       loff_t start, size_t len)
203b6e16652SDavid Howells {
204b6e16652SDavid Howells 	pgoff_t first = start / PAGE_SIZE;
205b6e16652SDavid Howells 	pgoff_t last = (start + len - 1) / PAGE_SIZE;
206b6e16652SDavid Howells 	struct page *page;
207b6e16652SDavid Howells 
208b6e16652SDavid Howells 	if (len) {
209b6e16652SDavid Howells 		XA_STATE(xas, &mapping->i_pages, first);
210b6e16652SDavid Howells 
211b6e16652SDavid Howells 		rcu_read_lock();
212b6e16652SDavid Howells 		xas_for_each(&xas, page, last) {
213b6e16652SDavid Howells 			end_page_fscache(page);
214b6e16652SDavid Howells 		}
215b6e16652SDavid Howells 		rcu_read_unlock();
216b6e16652SDavid Howells 	}
217b6e16652SDavid Howells }
218b6e16652SDavid Howells EXPORT_SYMBOL(__fscache_clear_page_bits);
219b6e16652SDavid Howells 
220b6e16652SDavid Howells /*
221b6e16652SDavid Howells  * Deal with the completion of writing the data to the cache.
222b6e16652SDavid Howells  */
223b6e16652SDavid Howells static void fscache_wreq_done(void *priv, ssize_t transferred_or_error,
224b6e16652SDavid Howells 			      bool was_async)
225b6e16652SDavid Howells {
226b6e16652SDavid Howells 	struct fscache_write_request *wreq = priv;
227b6e16652SDavid Howells 
228b6e16652SDavid Howells 	fscache_clear_page_bits(fscache_cres_cookie(&wreq->cache_resources),
229b6e16652SDavid Howells 				wreq->mapping, wreq->start, wreq->len,
230b6e16652SDavid Howells 				wreq->set_bits);
231b6e16652SDavid Howells 
232b6e16652SDavid Howells 	if (wreq->term_func)
233b6e16652SDavid Howells 		wreq->term_func(wreq->term_func_priv, transferred_or_error,
234b6e16652SDavid Howells 				was_async);
235b6e16652SDavid Howells 	fscache_end_operation(&wreq->cache_resources);
236b6e16652SDavid Howells 	kfree(wreq);
237b6e16652SDavid Howells }
238b6e16652SDavid Howells 
239b6e16652SDavid Howells void __fscache_write_to_cache(struct fscache_cookie *cookie,
240b6e16652SDavid Howells 			      struct address_space *mapping,
241b6e16652SDavid Howells 			      loff_t start, size_t len, loff_t i_size,
242b6e16652SDavid Howells 			      netfs_io_terminated_t term_func,
243b6e16652SDavid Howells 			      void *term_func_priv,
244b6e16652SDavid Howells 			      bool cond)
245b6e16652SDavid Howells {
246b6e16652SDavid Howells 	struct fscache_write_request *wreq;
247b6e16652SDavid Howells 	struct netfs_cache_resources *cres;
248b6e16652SDavid Howells 	struct iov_iter iter;
249b6e16652SDavid Howells 	int ret = -ENOBUFS;
250b6e16652SDavid Howells 
251b6e16652SDavid Howells 	if (len == 0)
252b6e16652SDavid Howells 		goto abandon;
253b6e16652SDavid Howells 
254b6e16652SDavid Howells 	_enter("%llx,%zx", start, len);
255b6e16652SDavid Howells 
256b6e16652SDavid Howells 	wreq = kzalloc(sizeof(struct fscache_write_request), GFP_NOFS);
257b6e16652SDavid Howells 	if (!wreq)
258b6e16652SDavid Howells 		goto abandon;
259b6e16652SDavid Howells 	wreq->mapping		= mapping;
260b6e16652SDavid Howells 	wreq->start		= start;
261b6e16652SDavid Howells 	wreq->len		= len;
262b6e16652SDavid Howells 	wreq->set_bits		= cond;
263b6e16652SDavid Howells 	wreq->term_func		= term_func;
264b6e16652SDavid Howells 	wreq->term_func_priv	= term_func_priv;
265b6e16652SDavid Howells 
266b6e16652SDavid Howells 	cres = &wreq->cache_resources;
267b6e16652SDavid Howells 	if (fscache_begin_operation(cres, cookie, FSCACHE_WANT_WRITE,
268b6e16652SDavid Howells 				    fscache_access_io_write) < 0)
269b6e16652SDavid Howells 		goto abandon_free;
270b6e16652SDavid Howells 
271b6e16652SDavid Howells 	ret = cres->ops->prepare_write(cres, &start, &len, i_size, false);
272b6e16652SDavid Howells 	if (ret < 0)
273b6e16652SDavid Howells 		goto abandon_end;
274b6e16652SDavid Howells 
275b6e16652SDavid Howells 	/* TODO: Consider clearing page bits now for space the write isn't
276b6e16652SDavid Howells 	 * covering.  This is more complicated than it appears when THPs are
277b6e16652SDavid Howells 	 * taken into account.
278b6e16652SDavid Howells 	 */
279b6e16652SDavid Howells 
280b6e16652SDavid Howells 	iov_iter_xarray(&iter, WRITE, &mapping->i_pages, start, len);
281b6e16652SDavid Howells 	fscache_write(cres, start, &iter, fscache_wreq_done, wreq);
282b6e16652SDavid Howells 	return;
283b6e16652SDavid Howells 
284b6e16652SDavid Howells abandon_end:
285b6e16652SDavid Howells 	return fscache_wreq_done(wreq, ret, false);
286b6e16652SDavid Howells abandon_free:
287b6e16652SDavid Howells 	kfree(wreq);
288b6e16652SDavid Howells abandon:
289b6e16652SDavid Howells 	fscache_clear_page_bits(cookie, mapping, start, len, cond);
290b6e16652SDavid Howells 	if (term_func)
291b6e16652SDavid Howells 		term_func(term_func_priv, ret, false);
292b6e16652SDavid Howells }
293b6e16652SDavid Howells EXPORT_SYMBOL(__fscache_write_to_cache);
294