xref: /openbmc/linux/fs/erofs/fscache.c (revision 4a6bff1187409f2c2ba1b17234541d314f0680fc)
1c6be2bd0SJeffle Xu // SPDX-License-Identifier: GPL-2.0-or-later
2c6be2bd0SJeffle Xu /*
3c6be2bd0SJeffle Xu  * Copyright (C) 2022, Alibaba Cloud
48b7adf1dSJia Zhu  * Copyright (C) 2022, Bytedance Inc. All rights reserved.
5c6be2bd0SJeffle Xu  */
6c6be2bd0SJeffle Xu #include <linux/fscache.h>
7c6be2bd0SJeffle Xu #include "internal.h"
8c6be2bd0SJeffle Xu 
98b7adf1dSJia Zhu static DEFINE_MUTEX(erofs_domain_list_lock);
107d419637SJia Zhu static DEFINE_MUTEX(erofs_domain_cookies_lock);
118b7adf1dSJia Zhu static LIST_HEAD(erofs_domain_list);
12a9849560SJia Zhu static struct vfsmount *erofs_pseudo_mnt;
138b7adf1dSJia Zhu 
14709fe09eSJingbo Xu struct erofs_fscache_request {
15be62c519SJingbo Xu 	struct erofs_fscache_request *primary;
16709fe09eSJingbo Xu 	struct netfs_cache_resources cache_resources;
17709fe09eSJingbo Xu 	struct address_space	*mapping;	/* The mapping being accessed */
18709fe09eSJingbo Xu 	loff_t			start;		/* Start position */
19709fe09eSJingbo Xu 	size_t			len;		/* Length of the request */
20709fe09eSJingbo Xu 	size_t			submitted;	/* Length of submitted */
21709fe09eSJingbo Xu 	short			error;		/* 0 or error that occurred */
22709fe09eSJingbo Xu 	refcount_t		ref;
23709fe09eSJingbo Xu };
24709fe09eSJingbo Xu 
25709fe09eSJingbo Xu static struct erofs_fscache_request *erofs_fscache_req_alloc(struct address_space *mapping,
26d435d532SXin Yin 					     loff_t start, size_t len)
27d435d532SXin Yin {
28709fe09eSJingbo Xu 	struct erofs_fscache_request *req;
29d435d532SXin Yin 
30709fe09eSJingbo Xu 	req = kzalloc(sizeof(struct erofs_fscache_request), GFP_KERNEL);
31709fe09eSJingbo Xu 	if (!req)
32d435d532SXin Yin 		return ERR_PTR(-ENOMEM);
33d435d532SXin Yin 
34709fe09eSJingbo Xu 	req->mapping = mapping;
35709fe09eSJingbo Xu 	req->start   = start;
36709fe09eSJingbo Xu 	req->len     = len;
37709fe09eSJingbo Xu 	refcount_set(&req->ref, 1);
38709fe09eSJingbo Xu 
39709fe09eSJingbo Xu 	return req;
40d435d532SXin Yin }
41d435d532SXin Yin 
42be62c519SJingbo Xu static struct erofs_fscache_request *erofs_fscache_req_chain(struct erofs_fscache_request *primary,
43be62c519SJingbo Xu 					     size_t len)
44d435d532SXin Yin {
45be62c519SJingbo Xu 	struct erofs_fscache_request *req;
46be62c519SJingbo Xu 
47be62c519SJingbo Xu 	/* use primary request for the first submission */
48be62c519SJingbo Xu 	if (!primary->submitted) {
49be62c519SJingbo Xu 		refcount_inc(&primary->ref);
50be62c519SJingbo Xu 		return primary;
51d435d532SXin Yin 	}
52d435d532SXin Yin 
53be62c519SJingbo Xu 	req = erofs_fscache_req_alloc(primary->mapping,
54be62c519SJingbo Xu 			primary->start + primary->submitted, len);
55be62c519SJingbo Xu 	if (!IS_ERR(req)) {
56be62c519SJingbo Xu 		req->primary = primary;
57be62c519SJingbo Xu 		refcount_inc(&primary->ref);
58be62c519SJingbo Xu 	}
59be62c519SJingbo Xu 	return req;
60be62c519SJingbo Xu }
61be62c519SJingbo Xu 
62709fe09eSJingbo Xu static void erofs_fscache_req_complete(struct erofs_fscache_request *req)
63d435d532SXin Yin {
64d435d532SXin Yin 	struct folio *folio;
65709fe09eSJingbo Xu 	bool failed = req->error;
66709fe09eSJingbo Xu 	pgoff_t start_page = req->start / PAGE_SIZE;
67709fe09eSJingbo Xu 	pgoff_t last_page = ((req->start + req->len) / PAGE_SIZE) - 1;
68d435d532SXin Yin 
69709fe09eSJingbo Xu 	XA_STATE(xas, &req->mapping->i_pages, start_page);
70d435d532SXin Yin 
71d435d532SXin Yin 	rcu_read_lock();
72d435d532SXin Yin 	xas_for_each(&xas, folio, last_page) {
7337020bbbSJingbo Xu 		if (xas_retry(&xas, folio))
7437020bbbSJingbo Xu 			continue;
75709fe09eSJingbo Xu 		if (!failed)
76d435d532SXin Yin 			folio_mark_uptodate(folio);
77d435d532SXin Yin 		folio_unlock(folio);
78d435d532SXin Yin 	}
79d435d532SXin Yin 	rcu_read_unlock();
80d435d532SXin Yin }
81d435d532SXin Yin 
82709fe09eSJingbo Xu static void erofs_fscache_req_put(struct erofs_fscache_request *req)
83d435d532SXin Yin {
84be62c519SJingbo Xu 	if (refcount_dec_and_test(&req->ref)) {
85be62c519SJingbo Xu 		if (req->cache_resources.ops)
86be62c519SJingbo Xu 			req->cache_resources.ops->end_operation(&req->cache_resources);
87be62c519SJingbo Xu 		if (!req->primary)
88709fe09eSJingbo Xu 			erofs_fscache_req_complete(req);
89be62c519SJingbo Xu 		else
90be62c519SJingbo Xu 			erofs_fscache_req_put(req->primary);
91be62c519SJingbo Xu 		kfree(req);
92be62c519SJingbo Xu 	}
93d435d532SXin Yin }
94d435d532SXin Yin 
95709fe09eSJingbo Xu static void erofs_fscache_subreq_complete(void *priv,
96d435d532SXin Yin 		ssize_t transferred_or_error, bool was_async)
97d435d532SXin Yin {
98709fe09eSJingbo Xu 	struct erofs_fscache_request *req = priv;
99d435d532SXin Yin 
100be62c519SJingbo Xu 	if (IS_ERR_VALUE(transferred_or_error)) {
101be62c519SJingbo Xu 		if (req->primary)
102be62c519SJingbo Xu 			req->primary->error = transferred_or_error;
103be62c519SJingbo Xu 		else
104709fe09eSJingbo Xu 			req->error = transferred_or_error;
105be62c519SJingbo Xu 	}
106709fe09eSJingbo Xu 	erofs_fscache_req_put(req);
107d435d532SXin Yin }
108d435d532SXin Yin 
109ec00b5e2SJeffle Xu /*
110709fe09eSJingbo Xu  * Read data from fscache (cookie, pstart, len), and fill the read data into
111709fe09eSJingbo Xu  * page cache described by (req->mapping, lstart, len). @pstart describeis the
112709fe09eSJingbo Xu  * start physical address in the cache file.
113ec00b5e2SJeffle Xu  */
114d435d532SXin Yin static int erofs_fscache_read_folios_async(struct fscache_cookie *cookie,
115709fe09eSJingbo Xu 		struct erofs_fscache_request *req, loff_t pstart, size_t len)
116ec00b5e2SJeffle Xu {
117ec00b5e2SJeffle Xu 	enum netfs_io_source source;
118709fe09eSJingbo Xu 	struct super_block *sb = req->mapping->host->i_sb;
119709fe09eSJingbo Xu 	struct netfs_cache_resources *cres = &req->cache_resources;
120ec00b5e2SJeffle Xu 	struct iov_iter iter;
121709fe09eSJingbo Xu 	loff_t lstart = req->start + req->submitted;
122ec00b5e2SJeffle Xu 	size_t done = 0;
123ec00b5e2SJeffle Xu 	int ret;
124ec00b5e2SJeffle Xu 
125709fe09eSJingbo Xu 	DBG_BUGON(len > req->len - req->submitted);
126d435d532SXin Yin 
127ec00b5e2SJeffle Xu 	ret = fscache_begin_read_operation(cres, cookie);
128ec00b5e2SJeffle Xu 	if (ret)
129709fe09eSJingbo Xu 		return ret;
130ec00b5e2SJeffle Xu 
131ec00b5e2SJeffle Xu 	while (done < len) {
132709fe09eSJingbo Xu 		loff_t sstart = pstart + done;
133709fe09eSJingbo Xu 		size_t slen = len - done;
134709fe09eSJingbo Xu 		unsigned long flags = 1 << NETFS_SREQ_ONDEMAND;
135ec00b5e2SJeffle Xu 
136709fe09eSJingbo Xu 		source = cres->ops->prepare_ondemand_read(cres,
137709fe09eSJingbo Xu 				sstart, &slen, LLONG_MAX, &flags, 0);
138709fe09eSJingbo Xu 		if (WARN_ON(slen == 0))
139ec00b5e2SJeffle Xu 			source = NETFS_INVALID_READ;
140ec00b5e2SJeffle Xu 		if (source != NETFS_READ_FROM_CACHE) {
141709fe09eSJingbo Xu 			erofs_err(sb, "failed to fscache prepare_read (source %d)", source);
142709fe09eSJingbo Xu 			return -EIO;
143ec00b5e2SJeffle Xu 		}
144ec00b5e2SJeffle Xu 
145709fe09eSJingbo Xu 		refcount_inc(&req->ref);
146*4a6bff11SLinus Torvalds 		iov_iter_xarray(&iter, ITER_DEST, &req->mapping->i_pages,
147709fe09eSJingbo Xu 				lstart + done, slen);
148d435d532SXin Yin 
149709fe09eSJingbo Xu 		ret = fscache_read(cres, sstart, &iter, NETFS_READ_HOLE_FAIL,
150709fe09eSJingbo Xu 				   erofs_fscache_subreq_complete, req);
151d435d532SXin Yin 		if (ret == -EIOCBQUEUED)
152d435d532SXin Yin 			ret = 0;
153ec00b5e2SJeffle Xu 		if (ret) {
154ec00b5e2SJeffle Xu 			erofs_err(sb, "failed to fscache_read (ret %d)", ret);
155ec00b5e2SJeffle Xu 			return ret;
156ec00b5e2SJeffle Xu 		}
157ec00b5e2SJeffle Xu 
158709fe09eSJingbo Xu 		done += slen;
159709fe09eSJingbo Xu 	}
160709fe09eSJingbo Xu 	DBG_BUGON(done != len);
161709fe09eSJingbo Xu 	return 0;
162709fe09eSJingbo Xu }
163709fe09eSJingbo Xu 
164fdaf9a58SLinus Torvalds static int erofs_fscache_meta_read_folio(struct file *data, struct folio *folio)
1655375e7c8SJeffle Xu {
1665375e7c8SJeffle Xu 	int ret;
1675375e7c8SJeffle Xu 	struct super_block *sb = folio_mapping(folio)->host->i_sb;
168709fe09eSJingbo Xu 	struct erofs_fscache_request *req;
1695375e7c8SJeffle Xu 	struct erofs_map_dev mdev = {
1705375e7c8SJeffle Xu 		.m_deviceid = 0,
1715375e7c8SJeffle Xu 		.m_pa = folio_pos(folio),
1725375e7c8SJeffle Xu 	};
1735375e7c8SJeffle Xu 
1745375e7c8SJeffle Xu 	ret = erofs_map_dev(sb, &mdev);
175709fe09eSJingbo Xu 	if (ret) {
1765375e7c8SJeffle Xu 		folio_unlock(folio);
1775375e7c8SJeffle Xu 		return ret;
1785375e7c8SJeffle Xu 	}
1795375e7c8SJeffle Xu 
180709fe09eSJingbo Xu 	req = erofs_fscache_req_alloc(folio_mapping(folio),
181709fe09eSJingbo Xu 				folio_pos(folio), folio_size(folio));
182709fe09eSJingbo Xu 	if (IS_ERR(req)) {
1833c265d7dSJeffle Xu 		folio_unlock(folio);
184709fe09eSJingbo Xu 		return PTR_ERR(req);
185709fe09eSJingbo Xu 	}
186709fe09eSJingbo Xu 
187709fe09eSJingbo Xu 	ret = erofs_fscache_read_folios_async(mdev.m_fscache->cookie,
188709fe09eSJingbo Xu 				req, mdev.m_pa, folio_size(folio));
189709fe09eSJingbo Xu 	if (ret)
190709fe09eSJingbo Xu 		req->error = ret;
191709fe09eSJingbo Xu 
192709fe09eSJingbo Xu 	erofs_fscache_req_put(req);
1933c265d7dSJeffle Xu 	return ret;
1943c265d7dSJeffle Xu }
1953c265d7dSJeffle Xu 
196be62c519SJingbo Xu static int erofs_fscache_data_read_slice(struct erofs_fscache_request *primary)
197bd735bdaSJeffle Xu {
198be62c519SJingbo Xu 	struct address_space *mapping = primary->mapping;
1991ae9470cSJingbo Xu 	struct inode *inode = mapping->host;
2001ae9470cSJingbo Xu 	struct super_block *sb = inode->i_sb;
201709fe09eSJingbo Xu 	struct erofs_fscache_request *req;
2021ae9470cSJingbo Xu 	struct erofs_map_blocks map;
2031ae9470cSJingbo Xu 	struct erofs_map_dev mdev;
2041ae9470cSJingbo Xu 	struct iov_iter iter;
205be62c519SJingbo Xu 	loff_t pos = primary->start + primary->submitted;
2061ae9470cSJingbo Xu 	size_t count;
2071ae9470cSJingbo Xu 	int ret;
2081ae9470cSJingbo Xu 
2091ae9470cSJingbo Xu 	map.m_la = pos;
2101ae9470cSJingbo Xu 	ret = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW);
2111ae9470cSJingbo Xu 	if (ret)
2121ae9470cSJingbo Xu 		return ret;
2131ae9470cSJingbo Xu 
2141ae9470cSJingbo Xu 	if (map.m_flags & EROFS_MAP_META) {
215bd735bdaSJeffle Xu 		struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
216bd735bdaSJeffle Xu 		erofs_blk_t blknr;
2171ae9470cSJingbo Xu 		size_t offset, size;
2181ae9470cSJingbo Xu 		void *src;
219bd735bdaSJeffle Xu 
220bd735bdaSJeffle Xu 		/* For tail packing layout, the offset may be non-zero. */
2211ae9470cSJingbo Xu 		offset = erofs_blkoff(map.m_pa);
2221ae9470cSJingbo Xu 		blknr = erofs_blknr(map.m_pa);
2231ae9470cSJingbo Xu 		size = map.m_llen;
224bd735bdaSJeffle Xu 
225bd735bdaSJeffle Xu 		src = erofs_read_metabuf(&buf, sb, blknr, EROFS_KMAP);
226bd735bdaSJeffle Xu 		if (IS_ERR(src))
227bd735bdaSJeffle Xu 			return PTR_ERR(src);
228bd735bdaSJeffle Xu 
229de4eda9dSAl Viro 		iov_iter_xarray(&iter, ITER_DEST, &mapping->i_pages, pos, PAGE_SIZE);
23075e43355SJingbo Xu 		if (copy_to_iter(src + offset, size, &iter) != size) {
23175e43355SJingbo Xu 			erofs_put_metabuf(&buf);
2321ae9470cSJingbo Xu 			return -EFAULT;
23375e43355SJingbo Xu 		}
2341ae9470cSJingbo Xu 		iov_iter_zero(PAGE_SIZE - size, &iter);
235bd735bdaSJeffle Xu 		erofs_put_metabuf(&buf);
236be62c519SJingbo Xu 		primary->submitted += PAGE_SIZE;
237be62c519SJingbo Xu 		return 0;
238bd735bdaSJeffle Xu 	}
239bd735bdaSJeffle Xu 
240be62c519SJingbo Xu 	count = primary->len - primary->submitted;
2411442b02bSJeffle Xu 	if (!(map.m_flags & EROFS_MAP_MAPPED)) {
242de4eda9dSAl Viro 		iov_iter_xarray(&iter, ITER_DEST, &mapping->i_pages, pos, count);
2431ae9470cSJingbo Xu 		iov_iter_zero(count, &iter);
244be62c519SJingbo Xu 		primary->submitted += count;
245be62c519SJingbo Xu 		return 0;
246bd735bdaSJeffle Xu 	}
247bd735bdaSJeffle Xu 
248be62c519SJingbo Xu 	count = min_t(size_t, map.m_llen - (pos - map.m_la), count);
249e6d9f9baSJingbo Xu 	DBG_BUGON(!count || count % PAGE_SIZE);
250e6d9f9baSJingbo Xu 
2511442b02bSJeffle Xu 	mdev = (struct erofs_map_dev) {
2521442b02bSJeffle Xu 		.m_deviceid = map.m_deviceid,
2531442b02bSJeffle Xu 		.m_pa = map.m_pa,
2541442b02bSJeffle Xu 	};
2551442b02bSJeffle Xu 	ret = erofs_map_dev(sb, &mdev);
2561442b02bSJeffle Xu 	if (ret)
2571442b02bSJeffle Xu 		return ret;
2581ae9470cSJingbo Xu 
259be62c519SJingbo Xu 	req = erofs_fscache_req_chain(primary, count);
260709fe09eSJingbo Xu 	if (IS_ERR(req))
261709fe09eSJingbo Xu 		return PTR_ERR(req);
2621ae9470cSJingbo Xu 
263709fe09eSJingbo Xu 	ret = erofs_fscache_read_folios_async(mdev.m_fscache->cookie,
264709fe09eSJingbo Xu 			req, mdev.m_pa + (pos - map.m_la), count);
265be62c519SJingbo Xu 	erofs_fscache_req_put(req);
266be62c519SJingbo Xu 	primary->submitted += count;
267be62c519SJingbo Xu 	return ret;
268be62c519SJingbo Xu }
269be62c519SJingbo Xu 
270be62c519SJingbo Xu static int erofs_fscache_data_read(struct erofs_fscache_request *req)
271be62c519SJingbo Xu {
272be62c519SJingbo Xu 	int ret;
273be62c519SJingbo Xu 
274be62c519SJingbo Xu 	do {
275be62c519SJingbo Xu 		ret = erofs_fscache_data_read_slice(req);
276709fe09eSJingbo Xu 		if (ret)
277709fe09eSJingbo Xu 			req->error = ret;
278be62c519SJingbo Xu 	} while (!ret && req->submitted < req->len);
279709fe09eSJingbo Xu 
280be62c519SJingbo Xu 	return ret;
2811442b02bSJeffle Xu }
2821442b02bSJeffle Xu 
2831ae9470cSJingbo Xu static int erofs_fscache_read_folio(struct file *file, struct folio *folio)
284c665b394SJeffle Xu {
285be62c519SJingbo Xu 	struct erofs_fscache_request *req;
2861ae9470cSJingbo Xu 	int ret;
2871ae9470cSJingbo Xu 
288be62c519SJingbo Xu 	req = erofs_fscache_req_alloc(folio_mapping(folio),
289be62c519SJingbo Xu 			folio_pos(folio), folio_size(folio));
290be62c519SJingbo Xu 	if (IS_ERR(req)) {
291c665b394SJeffle Xu 		folio_unlock(folio);
292be62c519SJingbo Xu 		return PTR_ERR(req);
293c665b394SJeffle Xu 	}
294be62c519SJingbo Xu 
295be62c519SJingbo Xu 	ret = erofs_fscache_data_read(req);
296be62c519SJingbo Xu 	erofs_fscache_req_put(req);
297be62c519SJingbo Xu 	return ret;
298d435d532SXin Yin }
299c665b394SJeffle Xu 
300c665b394SJeffle Xu static void erofs_fscache_readahead(struct readahead_control *rac)
301c665b394SJeffle Xu {
302be62c519SJingbo Xu 	struct erofs_fscache_request *req;
303c665b394SJeffle Xu 
304c665b394SJeffle Xu 	if (!readahead_count(rac))
305c665b394SJeffle Xu 		return;
306c665b394SJeffle Xu 
307be62c519SJingbo Xu 	req = erofs_fscache_req_alloc(rac->mapping,
308be62c519SJingbo Xu 			readahead_pos(rac), readahead_length(rac));
309be62c519SJingbo Xu 	if (IS_ERR(req))
310c665b394SJeffle Xu 		return;
311c665b394SJeffle Xu 
312be62c519SJingbo Xu 	/* The request completion will drop refs on the folios. */
313be62c519SJingbo Xu 	while (readahead_folio(rac))
314be62c519SJingbo Xu 		;
315be62c519SJingbo Xu 
316be62c519SJingbo Xu 	erofs_fscache_data_read(req);
317be62c519SJingbo Xu 	erofs_fscache_req_put(req);
318c665b394SJeffle Xu }
319c665b394SJeffle Xu 
320c6be2bd0SJeffle Xu static const struct address_space_operations erofs_fscache_meta_aops = {
321fdaf9a58SLinus Torvalds 	.read_folio = erofs_fscache_meta_read_folio,
322c6be2bd0SJeffle Xu };
323c6be2bd0SJeffle Xu 
3241442b02bSJeffle Xu const struct address_space_operations erofs_fscache_access_aops = {
325fdaf9a58SLinus Torvalds 	.read_folio = erofs_fscache_read_folio,
326c665b394SJeffle Xu 	.readahead = erofs_fscache_readahead,
3271442b02bSJeffle Xu };
3281442b02bSJeffle Xu 
3298b7adf1dSJia Zhu static void erofs_fscache_domain_put(struct erofs_domain *domain)
3308b7adf1dSJia Zhu {
3318b7adf1dSJia Zhu 	if (!domain)
3328b7adf1dSJia Zhu 		return;
3338b7adf1dSJia Zhu 	mutex_lock(&erofs_domain_list_lock);
3348b7adf1dSJia Zhu 	if (refcount_dec_and_test(&domain->ref)) {
3358b7adf1dSJia Zhu 		list_del(&domain->list);
336a9849560SJia Zhu 		if (list_empty(&erofs_domain_list)) {
337a9849560SJia Zhu 			kern_unmount(erofs_pseudo_mnt);
338a9849560SJia Zhu 			erofs_pseudo_mnt = NULL;
339a9849560SJia Zhu 		}
3408b7adf1dSJia Zhu 		mutex_unlock(&erofs_domain_list_lock);
3418b7adf1dSJia Zhu 		fscache_relinquish_volume(domain->volume, NULL, false);
3428b7adf1dSJia Zhu 		kfree(domain->domain_id);
3438b7adf1dSJia Zhu 		kfree(domain);
3448b7adf1dSJia Zhu 		return;
3458b7adf1dSJia Zhu 	}
3468b7adf1dSJia Zhu 	mutex_unlock(&erofs_domain_list_lock);
3478b7adf1dSJia Zhu }
3488b7adf1dSJia Zhu 
3498b7adf1dSJia Zhu static int erofs_fscache_register_volume(struct super_block *sb)
3508b7adf1dSJia Zhu {
3518b7adf1dSJia Zhu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
35239bfcb81SJingbo Xu 	char *domain_id = sbi->domain_id;
3538b7adf1dSJia Zhu 	struct fscache_volume *volume;
3548b7adf1dSJia Zhu 	char *name;
3558b7adf1dSJia Zhu 	int ret = 0;
3568b7adf1dSJia Zhu 
3578b7adf1dSJia Zhu 	name = kasprintf(GFP_KERNEL, "erofs,%s",
35839bfcb81SJingbo Xu 			 domain_id ? domain_id : sbi->fsid);
3598b7adf1dSJia Zhu 	if (!name)
3608b7adf1dSJia Zhu 		return -ENOMEM;
3618b7adf1dSJia Zhu 
3628b7adf1dSJia Zhu 	volume = fscache_acquire_volume(name, NULL, NULL, 0);
3638b7adf1dSJia Zhu 	if (IS_ERR_OR_NULL(volume)) {
3648b7adf1dSJia Zhu 		erofs_err(sb, "failed to register volume for %s", name);
3658b7adf1dSJia Zhu 		ret = volume ? PTR_ERR(volume) : -EOPNOTSUPP;
3668b7adf1dSJia Zhu 		volume = NULL;
3678b7adf1dSJia Zhu 	}
3688b7adf1dSJia Zhu 
3698b7adf1dSJia Zhu 	sbi->volume = volume;
3708b7adf1dSJia Zhu 	kfree(name);
3718b7adf1dSJia Zhu 	return ret;
3728b7adf1dSJia Zhu }
3738b7adf1dSJia Zhu 
3748b7adf1dSJia Zhu static int erofs_fscache_init_domain(struct super_block *sb)
3758b7adf1dSJia Zhu {
3768b7adf1dSJia Zhu 	int err;
3778b7adf1dSJia Zhu 	struct erofs_domain *domain;
3788b7adf1dSJia Zhu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
3798b7adf1dSJia Zhu 
3808b7adf1dSJia Zhu 	domain = kzalloc(sizeof(struct erofs_domain), GFP_KERNEL);
3818b7adf1dSJia Zhu 	if (!domain)
3828b7adf1dSJia Zhu 		return -ENOMEM;
3838b7adf1dSJia Zhu 
38439bfcb81SJingbo Xu 	domain->domain_id = kstrdup(sbi->domain_id, GFP_KERNEL);
3858b7adf1dSJia Zhu 	if (!domain->domain_id) {
3868b7adf1dSJia Zhu 		kfree(domain);
3878b7adf1dSJia Zhu 		return -ENOMEM;
3888b7adf1dSJia Zhu 	}
3898b7adf1dSJia Zhu 
3908b7adf1dSJia Zhu 	err = erofs_fscache_register_volume(sb);
3918b7adf1dSJia Zhu 	if (err)
3928b7adf1dSJia Zhu 		goto out;
3938b7adf1dSJia Zhu 
394a9849560SJia Zhu 	if (!erofs_pseudo_mnt) {
395a9849560SJia Zhu 		erofs_pseudo_mnt = kern_mount(&erofs_fs_type);
396a9849560SJia Zhu 		if (IS_ERR(erofs_pseudo_mnt)) {
397a9849560SJia Zhu 			err = PTR_ERR(erofs_pseudo_mnt);
398a9849560SJia Zhu 			goto out;
399a9849560SJia Zhu 		}
400a9849560SJia Zhu 	}
401a9849560SJia Zhu 
4028b7adf1dSJia Zhu 	domain->volume = sbi->volume;
4038b7adf1dSJia Zhu 	refcount_set(&domain->ref, 1);
4048b7adf1dSJia Zhu 	list_add(&domain->list, &erofs_domain_list);
4058b7adf1dSJia Zhu 	sbi->domain = domain;
4068b7adf1dSJia Zhu 	return 0;
4078b7adf1dSJia Zhu out:
4088b7adf1dSJia Zhu 	kfree(domain->domain_id);
4098b7adf1dSJia Zhu 	kfree(domain);
4108b7adf1dSJia Zhu 	return err;
4118b7adf1dSJia Zhu }
4128b7adf1dSJia Zhu 
4138b7adf1dSJia Zhu static int erofs_fscache_register_domain(struct super_block *sb)
4148b7adf1dSJia Zhu {
4158b7adf1dSJia Zhu 	int err;
4168b7adf1dSJia Zhu 	struct erofs_domain *domain;
4178b7adf1dSJia Zhu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
4188b7adf1dSJia Zhu 
4198b7adf1dSJia Zhu 	mutex_lock(&erofs_domain_list_lock);
4208b7adf1dSJia Zhu 	list_for_each_entry(domain, &erofs_domain_list, list) {
42139bfcb81SJingbo Xu 		if (!strcmp(domain->domain_id, sbi->domain_id)) {
4228b7adf1dSJia Zhu 			sbi->domain = domain;
4238b7adf1dSJia Zhu 			sbi->volume = domain->volume;
4248b7adf1dSJia Zhu 			refcount_inc(&domain->ref);
4258b7adf1dSJia Zhu 			mutex_unlock(&erofs_domain_list_lock);
4268b7adf1dSJia Zhu 			return 0;
4278b7adf1dSJia Zhu 		}
4288b7adf1dSJia Zhu 	}
4298b7adf1dSJia Zhu 	err = erofs_fscache_init_domain(sb);
4308b7adf1dSJia Zhu 	mutex_unlock(&erofs_domain_list_lock);
4318b7adf1dSJia Zhu 	return err;
4328b7adf1dSJia Zhu }
4338b7adf1dSJia Zhu 
4347d419637SJia Zhu static
4357d419637SJia Zhu struct erofs_fscache *erofs_fscache_acquire_cookie(struct super_block *sb,
43627f2a2dcSHou Tao 						   char *name,
43727f2a2dcSHou Tao 						   unsigned int flags)
438c6be2bd0SJeffle Xu {
439c6be2bd0SJeffle Xu 	struct fscache_volume *volume = EROFS_SB(sb)->volume;
440c6be2bd0SJeffle Xu 	struct erofs_fscache *ctx;
441c6be2bd0SJeffle Xu 	struct fscache_cookie *cookie;
442c6be2bd0SJeffle Xu 	int ret;
443c6be2bd0SJeffle Xu 
444c6be2bd0SJeffle Xu 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
445c6be2bd0SJeffle Xu 	if (!ctx)
446e1de2da0SJia Zhu 		return ERR_PTR(-ENOMEM);
447c6be2bd0SJeffle Xu 
448c6be2bd0SJeffle Xu 	cookie = fscache_acquire_cookie(volume, FSCACHE_ADV_WANT_CACHE_SIZE,
449c6be2bd0SJeffle Xu 					name, strlen(name), NULL, 0, 0);
450c6be2bd0SJeffle Xu 	if (!cookie) {
451c6be2bd0SJeffle Xu 		erofs_err(sb, "failed to get cookie for %s", name);
452c6be2bd0SJeffle Xu 		ret = -EINVAL;
453c6be2bd0SJeffle Xu 		goto err;
454c6be2bd0SJeffle Xu 	}
455c6be2bd0SJeffle Xu 
456c6be2bd0SJeffle Xu 	fscache_use_cookie(cookie, false);
457c6be2bd0SJeffle Xu 	ctx->cookie = cookie;
458c6be2bd0SJeffle Xu 
45927f2a2dcSHou Tao 	if (flags & EROFS_REG_COOKIE_NEED_INODE) {
460c6be2bd0SJeffle Xu 		struct inode *const inode = new_inode(sb);
461c6be2bd0SJeffle Xu 
462b02c602fSJeffle Xu 		if (!inode) {
463b02c602fSJeffle Xu 			erofs_err(sb, "failed to get anon inode for %s", name);
464b02c602fSJeffle Xu 			ret = -ENOMEM;
465b02c602fSJeffle Xu 			goto err_cookie;
466b02c602fSJeffle Xu 		}
467b02c602fSJeffle Xu 
468b02c602fSJeffle Xu 		set_nlink(inode, 1);
469b02c602fSJeffle Xu 		inode->i_size = OFFSET_MAX;
470b02c602fSJeffle Xu 		inode->i_mapping->a_ops = &erofs_fscache_meta_aops;
471b02c602fSJeffle Xu 		mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
472b02c602fSJeffle Xu 
473b02c602fSJeffle Xu 		ctx->inode = inode;
474b02c602fSJeffle Xu 	}
475b02c602fSJeffle Xu 
476e1de2da0SJia Zhu 	return ctx;
477b02c602fSJeffle Xu 
478b02c602fSJeffle Xu err_cookie:
479b02c602fSJeffle Xu 	fscache_unuse_cookie(ctx->cookie, NULL, NULL);
480b02c602fSJeffle Xu 	fscache_relinquish_cookie(ctx->cookie, false);
481b02c602fSJeffle Xu err:
482b02c602fSJeffle Xu 	kfree(ctx);
483e1de2da0SJia Zhu 	return ERR_PTR(ret);
484b02c602fSJeffle Xu }
485b02c602fSJeffle Xu 
4867d419637SJia Zhu static void erofs_fscache_relinquish_cookie(struct erofs_fscache *ctx)
487b02c602fSJeffle Xu {
488b02c602fSJeffle Xu 	fscache_unuse_cookie(ctx->cookie, NULL, NULL);
489b02c602fSJeffle Xu 	fscache_relinquish_cookie(ctx->cookie, false);
4903c265d7dSJeffle Xu 	iput(ctx->inode);
4917d419637SJia Zhu 	kfree(ctx->name);
492b02c602fSJeffle Xu 	kfree(ctx);
493b02c602fSJeffle Xu }
494b02c602fSJeffle Xu 
4957d419637SJia Zhu static
4967d419637SJia Zhu struct erofs_fscache *erofs_fscache_domain_init_cookie(struct super_block *sb,
49727f2a2dcSHou Tao 						       char *name,
49827f2a2dcSHou Tao 						       unsigned int flags)
4997d419637SJia Zhu {
5007d419637SJia Zhu 	int err;
5017d419637SJia Zhu 	struct inode *inode;
5027d419637SJia Zhu 	struct erofs_fscache *ctx;
5037d419637SJia Zhu 	struct erofs_domain *domain = EROFS_SB(sb)->domain;
5047d419637SJia Zhu 
50527f2a2dcSHou Tao 	ctx = erofs_fscache_acquire_cookie(sb, name, flags);
5067d419637SJia Zhu 	if (IS_ERR(ctx))
5077d419637SJia Zhu 		return ctx;
5087d419637SJia Zhu 
5097d419637SJia Zhu 	ctx->name = kstrdup(name, GFP_KERNEL);
5107d419637SJia Zhu 	if (!ctx->name) {
5117d419637SJia Zhu 		err = -ENOMEM;
5127d419637SJia Zhu 		goto out;
5137d419637SJia Zhu 	}
5147d419637SJia Zhu 
5157d419637SJia Zhu 	inode = new_inode(erofs_pseudo_mnt->mnt_sb);
5167d419637SJia Zhu 	if (!inode) {
5177d419637SJia Zhu 		err = -ENOMEM;
5187d419637SJia Zhu 		goto out;
5197d419637SJia Zhu 	}
5207d419637SJia Zhu 
5217d419637SJia Zhu 	ctx->domain = domain;
5227d419637SJia Zhu 	ctx->anon_inode = inode;
5237d419637SJia Zhu 	inode->i_private = ctx;
5247d419637SJia Zhu 	refcount_inc(&domain->ref);
5257d419637SJia Zhu 	return ctx;
5267d419637SJia Zhu out:
5277d419637SJia Zhu 	erofs_fscache_relinquish_cookie(ctx);
5287d419637SJia Zhu 	return ERR_PTR(err);
5297d419637SJia Zhu }
5307d419637SJia Zhu 
5317d419637SJia Zhu static
5327d419637SJia Zhu struct erofs_fscache *erofs_domain_register_cookie(struct super_block *sb,
53327f2a2dcSHou Tao 						   char *name,
53427f2a2dcSHou Tao 						   unsigned int flags)
5357d419637SJia Zhu {
5367d419637SJia Zhu 	struct inode *inode;
5377d419637SJia Zhu 	struct erofs_fscache *ctx;
5387d419637SJia Zhu 	struct erofs_domain *domain = EROFS_SB(sb)->domain;
5397d419637SJia Zhu 	struct super_block *psb = erofs_pseudo_mnt->mnt_sb;
5407d419637SJia Zhu 
5417d419637SJia Zhu 	mutex_lock(&erofs_domain_cookies_lock);
542ce4b8156SDawei Li 	spin_lock(&psb->s_inode_list_lock);
5437d419637SJia Zhu 	list_for_each_entry(inode, &psb->s_inodes, i_sb_list) {
5447d419637SJia Zhu 		ctx = inode->i_private;
5457d419637SJia Zhu 		if (!ctx || ctx->domain != domain || strcmp(ctx->name, name))
5467d419637SJia Zhu 			continue;
54727f2a2dcSHou Tao 		if (!(flags & EROFS_REG_COOKIE_NEED_NOEXIST)) {
5487d419637SJia Zhu 			igrab(inode);
54927f2a2dcSHou Tao 		} else {
55027f2a2dcSHou Tao 			erofs_err(sb, "%s already exists in domain %s", name,
55127f2a2dcSHou Tao 				  domain->domain_id);
55227f2a2dcSHou Tao 			ctx = ERR_PTR(-EEXIST);
55327f2a2dcSHou Tao 		}
554ce4b8156SDawei Li 		spin_unlock(&psb->s_inode_list_lock);
5557d419637SJia Zhu 		mutex_unlock(&erofs_domain_cookies_lock);
5567d419637SJia Zhu 		return ctx;
5577d419637SJia Zhu 	}
558ce4b8156SDawei Li 	spin_unlock(&psb->s_inode_list_lock);
55927f2a2dcSHou Tao 	ctx = erofs_fscache_domain_init_cookie(sb, name, flags);
5607d419637SJia Zhu 	mutex_unlock(&erofs_domain_cookies_lock);
5617d419637SJia Zhu 	return ctx;
5627d419637SJia Zhu }
5637d419637SJia Zhu 
5647d419637SJia Zhu struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
56527f2a2dcSHou Tao 						    char *name,
56627f2a2dcSHou Tao 						    unsigned int flags)
5677d419637SJia Zhu {
56839bfcb81SJingbo Xu 	if (EROFS_SB(sb)->domain_id)
56927f2a2dcSHou Tao 		return erofs_domain_register_cookie(sb, name, flags);
57027f2a2dcSHou Tao 	return erofs_fscache_acquire_cookie(sb, name, flags);
5717d419637SJia Zhu }
5727d419637SJia Zhu 
5737d419637SJia Zhu void erofs_fscache_unregister_cookie(struct erofs_fscache *ctx)
5747d419637SJia Zhu {
5757d419637SJia Zhu 	bool drop;
5767d419637SJia Zhu 	struct erofs_domain *domain;
5777d419637SJia Zhu 
5787d419637SJia Zhu 	if (!ctx)
5797d419637SJia Zhu 		return;
5807d419637SJia Zhu 	domain = ctx->domain;
5817d419637SJia Zhu 	if (domain) {
5827d419637SJia Zhu 		mutex_lock(&erofs_domain_cookies_lock);
5837d419637SJia Zhu 		drop = atomic_read(&ctx->anon_inode->i_count) == 1;
5847d419637SJia Zhu 		iput(ctx->anon_inode);
5857d419637SJia Zhu 		mutex_unlock(&erofs_domain_cookies_lock);
5867d419637SJia Zhu 		if (!drop)
5877d419637SJia Zhu 			return;
5887d419637SJia Zhu 	}
5897d419637SJia Zhu 
5907d419637SJia Zhu 	erofs_fscache_relinquish_cookie(ctx);
5917d419637SJia Zhu 	erofs_fscache_domain_put(domain);
5927d419637SJia Zhu }
5937d419637SJia Zhu 
594c6be2bd0SJeffle Xu int erofs_fscache_register_fs(struct super_block *sb)
595c6be2bd0SJeffle Xu {
5968b7adf1dSJia Zhu 	int ret;
597c6be2bd0SJeffle Xu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
598e1de2da0SJia Zhu 	struct erofs_fscache *fscache;
59927f2a2dcSHou Tao 	unsigned int flags;
600c6be2bd0SJeffle Xu 
60139bfcb81SJingbo Xu 	if (sbi->domain_id)
6028b7adf1dSJia Zhu 		ret = erofs_fscache_register_domain(sb);
6038b7adf1dSJia Zhu 	else
6048b7adf1dSJia Zhu 		ret = erofs_fscache_register_volume(sb);
6058b7adf1dSJia Zhu 	if (ret)
6068b7adf1dSJia Zhu 		return ret;
607c6be2bd0SJeffle Xu 
60827f2a2dcSHou Tao 	/*
60927f2a2dcSHou Tao 	 * When shared domain is enabled, using NEED_NOEXIST to guarantee
61027f2a2dcSHou Tao 	 * the primary data blob (aka fsid) is unique in the shared domain.
61127f2a2dcSHou Tao 	 *
61227f2a2dcSHou Tao 	 * For non-shared-domain case, fscache_acquire_volume() invoked by
61327f2a2dcSHou Tao 	 * erofs_fscache_register_volume() has already guaranteed
61427f2a2dcSHou Tao 	 * the uniqueness of primary data blob.
61527f2a2dcSHou Tao 	 *
61627f2a2dcSHou Tao 	 * Acquired domain/volume will be relinquished in kill_sb() on error.
61727f2a2dcSHou Tao 	 */
61827f2a2dcSHou Tao 	flags = EROFS_REG_COOKIE_NEED_INODE;
61927f2a2dcSHou Tao 	if (sbi->domain_id)
62027f2a2dcSHou Tao 		flags |= EROFS_REG_COOKIE_NEED_NOEXIST;
62127f2a2dcSHou Tao 	fscache = erofs_fscache_register_cookie(sb, sbi->fsid, flags);
622e1de2da0SJia Zhu 	if (IS_ERR(fscache))
623e1de2da0SJia Zhu 		return PTR_ERR(fscache);
624e1de2da0SJia Zhu 
625e1de2da0SJia Zhu 	sbi->s_fscache = fscache;
626e1de2da0SJia Zhu 	return 0;
627c6be2bd0SJeffle Xu }
628c6be2bd0SJeffle Xu 
629c6be2bd0SJeffle Xu void erofs_fscache_unregister_fs(struct super_block *sb)
630c6be2bd0SJeffle Xu {
631c6be2bd0SJeffle Xu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
632c6be2bd0SJeffle Xu 
633e1de2da0SJia Zhu 	erofs_fscache_unregister_cookie(sbi->s_fscache);
6348b7adf1dSJia Zhu 
6358b7adf1dSJia Zhu 	if (sbi->domain)
6368b7adf1dSJia Zhu 		erofs_fscache_domain_put(sbi->domain);
6378b7adf1dSJia Zhu 	else
638c6be2bd0SJeffle Xu 		fscache_relinquish_volume(sbi->volume, NULL, false);
6398b7adf1dSJia Zhu 
640e1de2da0SJia Zhu 	sbi->s_fscache = NULL;
641c6be2bd0SJeffle Xu 	sbi->volume = NULL;
6428b7adf1dSJia Zhu 	sbi->domain = NULL;
643c6be2bd0SJeffle Xu }
644