xref: /openbmc/linux/fs/erofs/fscache.c (revision df202b452fe6c6d6f1351bad485e2367ef1e644e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2022, Alibaba Cloud
4  */
5 #include <linux/fscache.h>
6 #include "internal.h"
7 
8 static struct netfs_io_request *erofs_fscache_alloc_request(struct address_space *mapping,
9 					     loff_t start, size_t len)
10 {
11 	struct netfs_io_request *rreq;
12 
13 	rreq = kzalloc(sizeof(struct netfs_io_request), GFP_KERNEL);
14 	if (!rreq)
15 		return ERR_PTR(-ENOMEM);
16 
17 	rreq->start	= start;
18 	rreq->len	= len;
19 	rreq->mapping	= mapping;
20 	INIT_LIST_HEAD(&rreq->subrequests);
21 	refcount_set(&rreq->ref, 1);
22 	return rreq;
23 }
24 
25 static void erofs_fscache_put_request(struct netfs_io_request *rreq)
26 {
27 	if (!refcount_dec_and_test(&rreq->ref))
28 		return;
29 	if (rreq->cache_resources.ops)
30 		rreq->cache_resources.ops->end_operation(&rreq->cache_resources);
31 	kfree(rreq);
32 }
33 
34 static void erofs_fscache_put_subrequest(struct netfs_io_subrequest *subreq)
35 {
36 	if (!refcount_dec_and_test(&subreq->ref))
37 		return;
38 	erofs_fscache_put_request(subreq->rreq);
39 	kfree(subreq);
40 }
41 
42 static void erofs_fscache_clear_subrequests(struct netfs_io_request *rreq)
43 {
44 	struct netfs_io_subrequest *subreq;
45 
46 	while (!list_empty(&rreq->subrequests)) {
47 		subreq = list_first_entry(&rreq->subrequests,
48 				struct netfs_io_subrequest, rreq_link);
49 		list_del(&subreq->rreq_link);
50 		erofs_fscache_put_subrequest(subreq);
51 	}
52 }
53 
54 static void erofs_fscache_rreq_unlock_folios(struct netfs_io_request *rreq)
55 {
56 	struct netfs_io_subrequest *subreq;
57 	struct folio *folio;
58 	unsigned int iopos = 0;
59 	pgoff_t start_page = rreq->start / PAGE_SIZE;
60 	pgoff_t last_page = ((rreq->start + rreq->len) / PAGE_SIZE) - 1;
61 	bool subreq_failed = false;
62 
63 	XA_STATE(xas, &rreq->mapping->i_pages, start_page);
64 
65 	subreq = list_first_entry(&rreq->subrequests,
66 				  struct netfs_io_subrequest, rreq_link);
67 	subreq_failed = (subreq->error < 0);
68 
69 	rcu_read_lock();
70 	xas_for_each(&xas, folio, last_page) {
71 		unsigned int pgpos =
72 			(folio_index(folio) - start_page) * PAGE_SIZE;
73 		unsigned int pgend = pgpos + folio_size(folio);
74 		bool pg_failed = false;
75 
76 		for (;;) {
77 			if (!subreq) {
78 				pg_failed = true;
79 				break;
80 			}
81 
82 			pg_failed |= subreq_failed;
83 			if (pgend < iopos + subreq->len)
84 				break;
85 
86 			iopos += subreq->len;
87 			if (!list_is_last(&subreq->rreq_link,
88 					  &rreq->subrequests)) {
89 				subreq = list_next_entry(subreq, rreq_link);
90 				subreq_failed = (subreq->error < 0);
91 			} else {
92 				subreq = NULL;
93 				subreq_failed = false;
94 			}
95 			if (pgend == iopos)
96 				break;
97 		}
98 
99 		if (!pg_failed)
100 			folio_mark_uptodate(folio);
101 
102 		folio_unlock(folio);
103 	}
104 	rcu_read_unlock();
105 }
106 
107 static void erofs_fscache_rreq_complete(struct netfs_io_request *rreq)
108 {
109 	erofs_fscache_rreq_unlock_folios(rreq);
110 	erofs_fscache_clear_subrequests(rreq);
111 	erofs_fscache_put_request(rreq);
112 }
113 
114 static void erofc_fscache_subreq_complete(void *priv,
115 		ssize_t transferred_or_error, bool was_async)
116 {
117 	struct netfs_io_subrequest *subreq = priv;
118 	struct netfs_io_request *rreq = subreq->rreq;
119 
120 	if (IS_ERR_VALUE(transferred_or_error))
121 		subreq->error = transferred_or_error;
122 
123 	if (atomic_dec_and_test(&rreq->nr_outstanding))
124 		erofs_fscache_rreq_complete(rreq);
125 
126 	erofs_fscache_put_subrequest(subreq);
127 }
128 
129 /*
130  * Read data from fscache and fill the read data into page cache described by
131  * @rreq, which shall be both aligned with PAGE_SIZE. @pstart describes
132  * the start physical address in the cache file.
133  */
134 static int erofs_fscache_read_folios_async(struct fscache_cookie *cookie,
135 				struct netfs_io_request *rreq, loff_t pstart)
136 {
137 	enum netfs_io_source source;
138 	struct super_block *sb = rreq->mapping->host->i_sb;
139 	struct netfs_io_subrequest *subreq;
140 	struct netfs_cache_resources *cres = &rreq->cache_resources;
141 	struct iov_iter iter;
142 	loff_t start = rreq->start;
143 	size_t len = rreq->len;
144 	size_t done = 0;
145 	int ret;
146 
147 	atomic_set(&rreq->nr_outstanding, 1);
148 
149 	ret = fscache_begin_read_operation(cres, cookie);
150 	if (ret)
151 		goto out;
152 
153 	while (done < len) {
154 		subreq = kzalloc(sizeof(struct netfs_io_subrequest),
155 				 GFP_KERNEL);
156 		if (subreq) {
157 			INIT_LIST_HEAD(&subreq->rreq_link);
158 			refcount_set(&subreq->ref, 2);
159 			subreq->rreq = rreq;
160 			refcount_inc(&rreq->ref);
161 		} else {
162 			ret = -ENOMEM;
163 			goto out;
164 		}
165 
166 		subreq->start = pstart + done;
167 		subreq->len	=  len - done;
168 		subreq->flags = 1 << NETFS_SREQ_ONDEMAND;
169 
170 		list_add_tail(&subreq->rreq_link, &rreq->subrequests);
171 
172 		source = cres->ops->prepare_read(subreq, LLONG_MAX);
173 		if (WARN_ON(subreq->len == 0))
174 			source = NETFS_INVALID_READ;
175 		if (source != NETFS_READ_FROM_CACHE) {
176 			erofs_err(sb, "failed to fscache prepare_read (source %d)",
177 				  source);
178 			ret = -EIO;
179 			subreq->error = ret;
180 			erofs_fscache_put_subrequest(subreq);
181 			goto out;
182 		}
183 
184 		atomic_inc(&rreq->nr_outstanding);
185 
186 		iov_iter_xarray(&iter, READ, &rreq->mapping->i_pages,
187 				start + done, subreq->len);
188 
189 		ret = fscache_read(cres, subreq->start, &iter,
190 				   NETFS_READ_HOLE_FAIL,
191 				   erofc_fscache_subreq_complete, subreq);
192 		if (ret == -EIOCBQUEUED)
193 			ret = 0;
194 		if (ret) {
195 			erofs_err(sb, "failed to fscache_read (ret %d)", ret);
196 			goto out;
197 		}
198 
199 		done += subreq->len;
200 	}
201 out:
202 	if (atomic_dec_and_test(&rreq->nr_outstanding))
203 		erofs_fscache_rreq_complete(rreq);
204 
205 	return ret;
206 }
207 
208 static int erofs_fscache_meta_read_folio(struct file *data, struct folio *folio)
209 {
210 	int ret;
211 	struct super_block *sb = folio_mapping(folio)->host->i_sb;
212 	struct netfs_io_request *rreq;
213 	struct erofs_map_dev mdev = {
214 		.m_deviceid = 0,
215 		.m_pa = folio_pos(folio),
216 	};
217 
218 	ret = erofs_map_dev(sb, &mdev);
219 	if (ret)
220 		goto out;
221 
222 	rreq = erofs_fscache_alloc_request(folio_mapping(folio),
223 				folio_pos(folio), folio_size(folio));
224 	if (IS_ERR(rreq))
225 		goto out;
226 
227 	return erofs_fscache_read_folios_async(mdev.m_fscache->cookie,
228 				rreq, mdev.m_pa);
229 out:
230 	folio_unlock(folio);
231 	return ret;
232 }
233 
234 static int erofs_fscache_read_folio_inline(struct folio *folio,
235 					 struct erofs_map_blocks *map)
236 {
237 	struct super_block *sb = folio_mapping(folio)->host->i_sb;
238 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
239 	erofs_blk_t blknr;
240 	size_t offset, len;
241 	void *src, *dst;
242 
243 	/* For tail packing layout, the offset may be non-zero. */
244 	offset = erofs_blkoff(map->m_pa);
245 	blknr = erofs_blknr(map->m_pa);
246 	len = map->m_llen;
247 
248 	src = erofs_read_metabuf(&buf, sb, blknr, EROFS_KMAP);
249 	if (IS_ERR(src))
250 		return PTR_ERR(src);
251 
252 	dst = kmap_local_folio(folio, 0);
253 	memcpy(dst, src + offset, len);
254 	memset(dst + len, 0, PAGE_SIZE - len);
255 	kunmap_local(dst);
256 
257 	erofs_put_metabuf(&buf);
258 	return 0;
259 }
260 
261 static int erofs_fscache_read_folio(struct file *file, struct folio *folio)
262 {
263 	struct inode *inode = folio_mapping(folio)->host;
264 	struct super_block *sb = inode->i_sb;
265 	struct erofs_map_blocks map;
266 	struct erofs_map_dev mdev;
267 	struct netfs_io_request *rreq;
268 	erofs_off_t pos;
269 	loff_t pstart;
270 	int ret;
271 
272 	DBG_BUGON(folio_size(folio) != EROFS_BLKSIZ);
273 
274 	pos = folio_pos(folio);
275 	map.m_la = pos;
276 
277 	ret = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW);
278 	if (ret)
279 		goto out_unlock;
280 
281 	if (!(map.m_flags & EROFS_MAP_MAPPED)) {
282 		folio_zero_range(folio, 0, folio_size(folio));
283 		goto out_uptodate;
284 	}
285 
286 	if (map.m_flags & EROFS_MAP_META) {
287 		ret = erofs_fscache_read_folio_inline(folio, &map);
288 		goto out_uptodate;
289 	}
290 
291 	mdev = (struct erofs_map_dev) {
292 		.m_deviceid = map.m_deviceid,
293 		.m_pa = map.m_pa,
294 	};
295 
296 	ret = erofs_map_dev(sb, &mdev);
297 	if (ret)
298 		goto out_unlock;
299 
300 
301 	rreq = erofs_fscache_alloc_request(folio_mapping(folio),
302 				folio_pos(folio), folio_size(folio));
303 	if (IS_ERR(rreq))
304 		goto out_unlock;
305 
306 	pstart = mdev.m_pa + (pos - map.m_la);
307 	return erofs_fscache_read_folios_async(mdev.m_fscache->cookie,
308 				rreq, pstart);
309 
310 out_uptodate:
311 	if (!ret)
312 		folio_mark_uptodate(folio);
313 out_unlock:
314 	folio_unlock(folio);
315 	return ret;
316 }
317 
318 static void erofs_fscache_advance_folios(struct readahead_control *rac,
319 					 size_t len, bool unlock)
320 {
321 	while (len) {
322 		struct folio *folio = readahead_folio(rac);
323 		len -= folio_size(folio);
324 		if (unlock) {
325 			folio_mark_uptodate(folio);
326 			folio_unlock(folio);
327 		}
328 	}
329 }
330 
331 static void erofs_fscache_readahead(struct readahead_control *rac)
332 {
333 	struct inode *inode = rac->mapping->host;
334 	struct super_block *sb = inode->i_sb;
335 	size_t len, count, done = 0;
336 	erofs_off_t pos;
337 	loff_t start, offset;
338 	int ret;
339 
340 	if (!readahead_count(rac))
341 		return;
342 
343 	start = readahead_pos(rac);
344 	len = readahead_length(rac);
345 
346 	do {
347 		struct erofs_map_blocks map;
348 		struct erofs_map_dev mdev;
349 		struct netfs_io_request *rreq;
350 
351 		pos = start + done;
352 		map.m_la = pos;
353 
354 		ret = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW);
355 		if (ret)
356 			return;
357 
358 		offset = start + done;
359 		count = min_t(size_t, map.m_llen - (pos - map.m_la),
360 			      len - done);
361 
362 		if (!(map.m_flags & EROFS_MAP_MAPPED)) {
363 			struct iov_iter iter;
364 
365 			iov_iter_xarray(&iter, READ, &rac->mapping->i_pages,
366 					offset, count);
367 			iov_iter_zero(count, &iter);
368 
369 			erofs_fscache_advance_folios(rac, count, true);
370 			ret = count;
371 			continue;
372 		}
373 
374 		if (map.m_flags & EROFS_MAP_META) {
375 			struct folio *folio = readahead_folio(rac);
376 
377 			ret = erofs_fscache_read_folio_inline(folio, &map);
378 			if (!ret) {
379 				folio_mark_uptodate(folio);
380 				ret = folio_size(folio);
381 			}
382 
383 			folio_unlock(folio);
384 			continue;
385 		}
386 
387 		mdev = (struct erofs_map_dev) {
388 			.m_deviceid = map.m_deviceid,
389 			.m_pa = map.m_pa,
390 		};
391 		ret = erofs_map_dev(sb, &mdev);
392 		if (ret)
393 			return;
394 
395 		rreq = erofs_fscache_alloc_request(rac->mapping, offset, count);
396 		if (IS_ERR(rreq))
397 			return;
398 		/*
399 		 * Drop the ref of folios here. Unlock them in
400 		 * rreq_unlock_folios() when rreq complete.
401 		 */
402 		erofs_fscache_advance_folios(rac, count, false);
403 		ret = erofs_fscache_read_folios_async(mdev.m_fscache->cookie,
404 					rreq, mdev.m_pa + (pos - map.m_la));
405 		if (!ret)
406 			ret = count;
407 	} while (ret > 0 && ((done += ret) < len));
408 }
409 
410 static const struct address_space_operations erofs_fscache_meta_aops = {
411 	.read_folio = erofs_fscache_meta_read_folio,
412 };
413 
414 const struct address_space_operations erofs_fscache_access_aops = {
415 	.read_folio = erofs_fscache_read_folio,
416 	.readahead = erofs_fscache_readahead,
417 };
418 
419 int erofs_fscache_register_cookie(struct super_block *sb,
420 				  struct erofs_fscache **fscache,
421 				  char *name, bool need_inode)
422 {
423 	struct fscache_volume *volume = EROFS_SB(sb)->volume;
424 	struct erofs_fscache *ctx;
425 	struct fscache_cookie *cookie;
426 	int ret;
427 
428 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
429 	if (!ctx)
430 		return -ENOMEM;
431 
432 	cookie = fscache_acquire_cookie(volume, FSCACHE_ADV_WANT_CACHE_SIZE,
433 					name, strlen(name), NULL, 0, 0);
434 	if (!cookie) {
435 		erofs_err(sb, "failed to get cookie for %s", name);
436 		ret = -EINVAL;
437 		goto err;
438 	}
439 
440 	fscache_use_cookie(cookie, false);
441 	ctx->cookie = cookie;
442 
443 	if (need_inode) {
444 		struct inode *const inode = new_inode(sb);
445 
446 		if (!inode) {
447 			erofs_err(sb, "failed to get anon inode for %s", name);
448 			ret = -ENOMEM;
449 			goto err_cookie;
450 		}
451 
452 		set_nlink(inode, 1);
453 		inode->i_size = OFFSET_MAX;
454 		inode->i_mapping->a_ops = &erofs_fscache_meta_aops;
455 		mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
456 
457 		ctx->inode = inode;
458 	}
459 
460 	*fscache = ctx;
461 	return 0;
462 
463 err_cookie:
464 	fscache_unuse_cookie(ctx->cookie, NULL, NULL);
465 	fscache_relinquish_cookie(ctx->cookie, false);
466 	ctx->cookie = NULL;
467 err:
468 	kfree(ctx);
469 	return ret;
470 }
471 
472 void erofs_fscache_unregister_cookie(struct erofs_fscache **fscache)
473 {
474 	struct erofs_fscache *ctx = *fscache;
475 
476 	if (!ctx)
477 		return;
478 
479 	fscache_unuse_cookie(ctx->cookie, NULL, NULL);
480 	fscache_relinquish_cookie(ctx->cookie, false);
481 	ctx->cookie = NULL;
482 
483 	iput(ctx->inode);
484 	ctx->inode = NULL;
485 
486 	kfree(ctx);
487 	*fscache = NULL;
488 }
489 
490 int erofs_fscache_register_fs(struct super_block *sb)
491 {
492 	struct erofs_sb_info *sbi = EROFS_SB(sb);
493 	struct fscache_volume *volume;
494 	char *name;
495 	int ret = 0;
496 
497 	name = kasprintf(GFP_KERNEL, "erofs,%s", sbi->opt.fsid);
498 	if (!name)
499 		return -ENOMEM;
500 
501 	volume = fscache_acquire_volume(name, NULL, NULL, 0);
502 	if (IS_ERR_OR_NULL(volume)) {
503 		erofs_err(sb, "failed to register volume for %s", name);
504 		ret = volume ? PTR_ERR(volume) : -EOPNOTSUPP;
505 		volume = NULL;
506 	}
507 
508 	sbi->volume = volume;
509 	kfree(name);
510 	return ret;
511 }
512 
513 void erofs_fscache_unregister_fs(struct super_block *sb)
514 {
515 	struct erofs_sb_info *sbi = EROFS_SB(sb);
516 
517 	fscache_relinquish_volume(sbi->volume, NULL, false);
518 	sbi->volume = NULL;
519 }
520