xref: /openbmc/linux/fs/erofs/fscache.c (revision 51b27119196cf2d89d5186c3e476d4ffca9919c7)
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);
122dfb8c3bSJingbo Xu static LIST_HEAD(erofs_domain_cookies_list);
13a9849560SJia Zhu static struct vfsmount *erofs_pseudo_mnt;
148b7adf1dSJia Zhu 
15709fe09eSJingbo Xu struct erofs_fscache_request {
16be62c519SJingbo Xu 	struct erofs_fscache_request *primary;
17709fe09eSJingbo Xu 	struct netfs_cache_resources cache_resources;
18709fe09eSJingbo Xu 	struct address_space	*mapping;	/* The mapping being accessed */
19709fe09eSJingbo Xu 	loff_t			start;		/* Start position */
20709fe09eSJingbo Xu 	size_t			len;		/* Length of the request */
21709fe09eSJingbo Xu 	size_t			submitted;	/* Length of submitted */
22709fe09eSJingbo Xu 	short			error;		/* 0 or error that occurred */
23709fe09eSJingbo Xu 	refcount_t		ref;
24709fe09eSJingbo Xu };
25709fe09eSJingbo Xu 
26709fe09eSJingbo Xu static struct erofs_fscache_request *erofs_fscache_req_alloc(struct address_space *mapping,
27d435d532SXin Yin 					     loff_t start, size_t len)
28d435d532SXin Yin {
29709fe09eSJingbo Xu 	struct erofs_fscache_request *req;
30d435d532SXin Yin 
31709fe09eSJingbo Xu 	req = kzalloc(sizeof(struct erofs_fscache_request), GFP_KERNEL);
32709fe09eSJingbo Xu 	if (!req)
33d435d532SXin Yin 		return ERR_PTR(-ENOMEM);
34d435d532SXin Yin 
35709fe09eSJingbo Xu 	req->mapping = mapping;
36709fe09eSJingbo Xu 	req->start   = start;
37709fe09eSJingbo Xu 	req->len     = len;
38709fe09eSJingbo Xu 	refcount_set(&req->ref, 1);
39709fe09eSJingbo Xu 
40709fe09eSJingbo Xu 	return req;
41d435d532SXin Yin }
42d435d532SXin Yin 
43be62c519SJingbo Xu static struct erofs_fscache_request *erofs_fscache_req_chain(struct erofs_fscache_request *primary,
44be62c519SJingbo Xu 					     size_t len)
45d435d532SXin Yin {
46be62c519SJingbo Xu 	struct erofs_fscache_request *req;
47be62c519SJingbo Xu 
48be62c519SJingbo Xu 	/* use primary request for the first submission */
49be62c519SJingbo Xu 	if (!primary->submitted) {
50be62c519SJingbo Xu 		refcount_inc(&primary->ref);
51be62c519SJingbo Xu 		return primary;
52d435d532SXin Yin 	}
53d435d532SXin Yin 
54be62c519SJingbo Xu 	req = erofs_fscache_req_alloc(primary->mapping,
55be62c519SJingbo Xu 			primary->start + primary->submitted, len);
56be62c519SJingbo Xu 	if (!IS_ERR(req)) {
57be62c519SJingbo Xu 		req->primary = primary;
58be62c519SJingbo Xu 		refcount_inc(&primary->ref);
59be62c519SJingbo Xu 	}
60be62c519SJingbo Xu 	return req;
61be62c519SJingbo Xu }
62be62c519SJingbo Xu 
63709fe09eSJingbo Xu static void erofs_fscache_req_complete(struct erofs_fscache_request *req)
64d435d532SXin Yin {
65d435d532SXin Yin 	struct folio *folio;
66709fe09eSJingbo Xu 	bool failed = req->error;
67709fe09eSJingbo Xu 	pgoff_t start_page = req->start / PAGE_SIZE;
68709fe09eSJingbo Xu 	pgoff_t last_page = ((req->start + req->len) / PAGE_SIZE) - 1;
69d435d532SXin Yin 
70709fe09eSJingbo Xu 	XA_STATE(xas, &req->mapping->i_pages, start_page);
71d435d532SXin Yin 
72d435d532SXin Yin 	rcu_read_lock();
73d435d532SXin Yin 	xas_for_each(&xas, folio, last_page) {
7437020bbbSJingbo Xu 		if (xas_retry(&xas, folio))
7537020bbbSJingbo Xu 			continue;
76709fe09eSJingbo Xu 		if (!failed)
77d435d532SXin Yin 			folio_mark_uptodate(folio);
78d435d532SXin Yin 		folio_unlock(folio);
79d435d532SXin Yin 	}
80d435d532SXin Yin 	rcu_read_unlock();
81d435d532SXin Yin }
82d435d532SXin Yin 
83709fe09eSJingbo Xu static void erofs_fscache_req_put(struct erofs_fscache_request *req)
84d435d532SXin Yin {
85be62c519SJingbo Xu 	if (refcount_dec_and_test(&req->ref)) {
86be62c519SJingbo Xu 		if (req->cache_resources.ops)
87be62c519SJingbo Xu 			req->cache_resources.ops->end_operation(&req->cache_resources);
88be62c519SJingbo Xu 		if (!req->primary)
89709fe09eSJingbo Xu 			erofs_fscache_req_complete(req);
90be62c519SJingbo Xu 		else
91be62c519SJingbo Xu 			erofs_fscache_req_put(req->primary);
92be62c519SJingbo Xu 		kfree(req);
93be62c519SJingbo Xu 	}
94d435d532SXin Yin }
95d435d532SXin Yin 
96709fe09eSJingbo Xu static void erofs_fscache_subreq_complete(void *priv,
97d435d532SXin Yin 		ssize_t transferred_or_error, bool was_async)
98d435d532SXin Yin {
99709fe09eSJingbo Xu 	struct erofs_fscache_request *req = priv;
100d435d532SXin Yin 
101be62c519SJingbo Xu 	if (IS_ERR_VALUE(transferred_or_error)) {
102be62c519SJingbo Xu 		if (req->primary)
103be62c519SJingbo Xu 			req->primary->error = transferred_or_error;
104be62c519SJingbo Xu 		else
105709fe09eSJingbo Xu 			req->error = transferred_or_error;
106be62c519SJingbo Xu 	}
107709fe09eSJingbo Xu 	erofs_fscache_req_put(req);
108d435d532SXin Yin }
109d435d532SXin Yin 
110ec00b5e2SJeffle Xu /*
111709fe09eSJingbo Xu  * Read data from fscache (cookie, pstart, len), and fill the read data into
112709fe09eSJingbo Xu  * page cache described by (req->mapping, lstart, len). @pstart describeis the
113709fe09eSJingbo Xu  * start physical address in the cache file.
114ec00b5e2SJeffle Xu  */
115d435d532SXin Yin static int erofs_fscache_read_folios_async(struct fscache_cookie *cookie,
116709fe09eSJingbo Xu 		struct erofs_fscache_request *req, loff_t pstart, size_t len)
117ec00b5e2SJeffle Xu {
118ec00b5e2SJeffle Xu 	enum netfs_io_source source;
119709fe09eSJingbo Xu 	struct super_block *sb = req->mapping->host->i_sb;
120709fe09eSJingbo Xu 	struct netfs_cache_resources *cres = &req->cache_resources;
121ec00b5e2SJeffle Xu 	struct iov_iter iter;
122709fe09eSJingbo Xu 	loff_t lstart = req->start + req->submitted;
123ec00b5e2SJeffle Xu 	size_t done = 0;
124ec00b5e2SJeffle Xu 	int ret;
125ec00b5e2SJeffle Xu 
126709fe09eSJingbo Xu 	DBG_BUGON(len > req->len - req->submitted);
127d435d532SXin Yin 
128ec00b5e2SJeffle Xu 	ret = fscache_begin_read_operation(cres, cookie);
129ec00b5e2SJeffle Xu 	if (ret)
130709fe09eSJingbo Xu 		return ret;
131ec00b5e2SJeffle Xu 
132ec00b5e2SJeffle Xu 	while (done < len) {
133709fe09eSJingbo Xu 		loff_t sstart = pstart + done;
134709fe09eSJingbo Xu 		size_t slen = len - done;
135709fe09eSJingbo Xu 		unsigned long flags = 1 << NETFS_SREQ_ONDEMAND;
136ec00b5e2SJeffle Xu 
137709fe09eSJingbo Xu 		source = cres->ops->prepare_ondemand_read(cres,
138709fe09eSJingbo Xu 				sstart, &slen, LLONG_MAX, &flags, 0);
139709fe09eSJingbo Xu 		if (WARN_ON(slen == 0))
140ec00b5e2SJeffle Xu 			source = NETFS_INVALID_READ;
141ec00b5e2SJeffle Xu 		if (source != NETFS_READ_FROM_CACHE) {
142709fe09eSJingbo Xu 			erofs_err(sb, "failed to fscache prepare_read (source %d)", source);
143709fe09eSJingbo Xu 			return -EIO;
144ec00b5e2SJeffle Xu 		}
145ec00b5e2SJeffle Xu 
146709fe09eSJingbo Xu 		refcount_inc(&req->ref);
1474a6bff11SLinus Torvalds 		iov_iter_xarray(&iter, ITER_DEST, &req->mapping->i_pages,
148709fe09eSJingbo Xu 				lstart + done, slen);
149d435d532SXin Yin 
150709fe09eSJingbo Xu 		ret = fscache_read(cres, sstart, &iter, NETFS_READ_HOLE_FAIL,
151709fe09eSJingbo Xu 				   erofs_fscache_subreq_complete, req);
152d435d532SXin Yin 		if (ret == -EIOCBQUEUED)
153d435d532SXin Yin 			ret = 0;
154ec00b5e2SJeffle Xu 		if (ret) {
155ec00b5e2SJeffle Xu 			erofs_err(sb, "failed to fscache_read (ret %d)", ret);
156ec00b5e2SJeffle Xu 			return ret;
157ec00b5e2SJeffle Xu 		}
158ec00b5e2SJeffle Xu 
159709fe09eSJingbo Xu 		done += slen;
160709fe09eSJingbo Xu 	}
161709fe09eSJingbo Xu 	DBG_BUGON(done != len);
162709fe09eSJingbo Xu 	return 0;
163709fe09eSJingbo Xu }
164709fe09eSJingbo Xu 
165fdaf9a58SLinus Torvalds static int erofs_fscache_meta_read_folio(struct file *data, struct folio *folio)
1665375e7c8SJeffle Xu {
1675375e7c8SJeffle Xu 	int ret;
168bdfa9014SJingbo Xu 	struct erofs_fscache *ctx = folio_mapping(folio)->host->i_private;
169709fe09eSJingbo Xu 	struct erofs_fscache_request *req;
1705375e7c8SJeffle Xu 
171709fe09eSJingbo Xu 	req = erofs_fscache_req_alloc(folio_mapping(folio),
172709fe09eSJingbo Xu 				folio_pos(folio), folio_size(folio));
173709fe09eSJingbo Xu 	if (IS_ERR(req)) {
1743c265d7dSJeffle Xu 		folio_unlock(folio);
175709fe09eSJingbo Xu 		return PTR_ERR(req);
176709fe09eSJingbo Xu 	}
177709fe09eSJingbo Xu 
178bdfa9014SJingbo Xu 	ret = erofs_fscache_read_folios_async(ctx->cookie, req,
179bdfa9014SJingbo Xu 				folio_pos(folio), folio_size(folio));
180709fe09eSJingbo Xu 	if (ret)
181709fe09eSJingbo Xu 		req->error = ret;
182709fe09eSJingbo Xu 
183709fe09eSJingbo Xu 	erofs_fscache_req_put(req);
1843c265d7dSJeffle Xu 	return ret;
1853c265d7dSJeffle Xu }
1863c265d7dSJeffle Xu 
187be62c519SJingbo Xu static int erofs_fscache_data_read_slice(struct erofs_fscache_request *primary)
188bd735bdaSJeffle Xu {
189be62c519SJingbo Xu 	struct address_space *mapping = primary->mapping;
1901ae9470cSJingbo Xu 	struct inode *inode = mapping->host;
1911ae9470cSJingbo Xu 	struct super_block *sb = inode->i_sb;
192709fe09eSJingbo Xu 	struct erofs_fscache_request *req;
1931ae9470cSJingbo Xu 	struct erofs_map_blocks map;
1941ae9470cSJingbo Xu 	struct erofs_map_dev mdev;
1951ae9470cSJingbo Xu 	struct iov_iter iter;
196be62c519SJingbo Xu 	loff_t pos = primary->start + primary->submitted;
1971ae9470cSJingbo Xu 	size_t count;
1981ae9470cSJingbo Xu 	int ret;
1991ae9470cSJingbo Xu 
2001ae9470cSJingbo Xu 	map.m_la = pos;
2018b58f9f0SJingbo Xu 	ret = erofs_map_blocks(inode, &map);
2021ae9470cSJingbo Xu 	if (ret)
2031ae9470cSJingbo Xu 		return ret;
2041ae9470cSJingbo Xu 
2051ae9470cSJingbo Xu 	if (map.m_flags & EROFS_MAP_META) {
206bd735bdaSJeffle Xu 		struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
207bd735bdaSJeffle Xu 		erofs_blk_t blknr;
2081ae9470cSJingbo Xu 		size_t offset, size;
2091ae9470cSJingbo Xu 		void *src;
210bd735bdaSJeffle Xu 
211bd735bdaSJeffle Xu 		/* For tail packing layout, the offset may be non-zero. */
2123acea5fcSJingbo Xu 		offset = erofs_blkoff(sb, map.m_pa);
2133acea5fcSJingbo Xu 		blknr = erofs_blknr(sb, map.m_pa);
2141ae9470cSJingbo Xu 		size = map.m_llen;
215bd735bdaSJeffle Xu 
216bd735bdaSJeffle Xu 		src = erofs_read_metabuf(&buf, sb, blknr, EROFS_KMAP);
217bd735bdaSJeffle Xu 		if (IS_ERR(src))
218bd735bdaSJeffle Xu 			return PTR_ERR(src);
219bd735bdaSJeffle Xu 
220de4eda9dSAl Viro 		iov_iter_xarray(&iter, ITER_DEST, &mapping->i_pages, pos, PAGE_SIZE);
22175e43355SJingbo Xu 		if (copy_to_iter(src + offset, size, &iter) != size) {
22275e43355SJingbo Xu 			erofs_put_metabuf(&buf);
2231ae9470cSJingbo Xu 			return -EFAULT;
22475e43355SJingbo Xu 		}
2251ae9470cSJingbo Xu 		iov_iter_zero(PAGE_SIZE - size, &iter);
226bd735bdaSJeffle Xu 		erofs_put_metabuf(&buf);
227be62c519SJingbo Xu 		primary->submitted += PAGE_SIZE;
228be62c519SJingbo Xu 		return 0;
229bd735bdaSJeffle Xu 	}
230bd735bdaSJeffle Xu 
231be62c519SJingbo Xu 	count = primary->len - primary->submitted;
2321442b02bSJeffle Xu 	if (!(map.m_flags & EROFS_MAP_MAPPED)) {
233de4eda9dSAl Viro 		iov_iter_xarray(&iter, ITER_DEST, &mapping->i_pages, pos, count);
2341ae9470cSJingbo Xu 		iov_iter_zero(count, &iter);
235be62c519SJingbo Xu 		primary->submitted += count;
236be62c519SJingbo Xu 		return 0;
237bd735bdaSJeffle Xu 	}
238bd735bdaSJeffle Xu 
239be62c519SJingbo Xu 	count = min_t(size_t, map.m_llen - (pos - map.m_la), count);
240e6d9f9baSJingbo Xu 	DBG_BUGON(!count || count % PAGE_SIZE);
241e6d9f9baSJingbo Xu 
2421442b02bSJeffle Xu 	mdev = (struct erofs_map_dev) {
2431442b02bSJeffle Xu 		.m_deviceid = map.m_deviceid,
2441442b02bSJeffle Xu 		.m_pa = map.m_pa,
2451442b02bSJeffle Xu 	};
2461442b02bSJeffle Xu 	ret = erofs_map_dev(sb, &mdev);
2471442b02bSJeffle Xu 	if (ret)
2481442b02bSJeffle Xu 		return ret;
2491ae9470cSJingbo Xu 
250be62c519SJingbo Xu 	req = erofs_fscache_req_chain(primary, count);
251709fe09eSJingbo Xu 	if (IS_ERR(req))
252709fe09eSJingbo Xu 		return PTR_ERR(req);
2531ae9470cSJingbo Xu 
254709fe09eSJingbo Xu 	ret = erofs_fscache_read_folios_async(mdev.m_fscache->cookie,
255709fe09eSJingbo Xu 			req, mdev.m_pa + (pos - map.m_la), count);
256be62c519SJingbo Xu 	erofs_fscache_req_put(req);
257be62c519SJingbo Xu 	primary->submitted += count;
258be62c519SJingbo Xu 	return ret;
259be62c519SJingbo Xu }
260be62c519SJingbo Xu 
261be62c519SJingbo Xu static int erofs_fscache_data_read(struct erofs_fscache_request *req)
262be62c519SJingbo Xu {
263be62c519SJingbo Xu 	int ret;
264be62c519SJingbo Xu 
265be62c519SJingbo Xu 	do {
266be62c519SJingbo Xu 		ret = erofs_fscache_data_read_slice(req);
267709fe09eSJingbo Xu 		if (ret)
268709fe09eSJingbo Xu 			req->error = ret;
269be62c519SJingbo Xu 	} while (!ret && req->submitted < req->len);
270709fe09eSJingbo Xu 
271be62c519SJingbo Xu 	return ret;
2721442b02bSJeffle Xu }
2731442b02bSJeffle Xu 
2741ae9470cSJingbo Xu static int erofs_fscache_read_folio(struct file *file, struct folio *folio)
275c665b394SJeffle Xu {
276be62c519SJingbo Xu 	struct erofs_fscache_request *req;
2771ae9470cSJingbo Xu 	int ret;
2781ae9470cSJingbo Xu 
279be62c519SJingbo Xu 	req = erofs_fscache_req_alloc(folio_mapping(folio),
280be62c519SJingbo Xu 			folio_pos(folio), folio_size(folio));
281be62c519SJingbo Xu 	if (IS_ERR(req)) {
282c665b394SJeffle Xu 		folio_unlock(folio);
283be62c519SJingbo Xu 		return PTR_ERR(req);
284c665b394SJeffle Xu 	}
285be62c519SJingbo Xu 
286be62c519SJingbo Xu 	ret = erofs_fscache_data_read(req);
287be62c519SJingbo Xu 	erofs_fscache_req_put(req);
288be62c519SJingbo Xu 	return ret;
289d435d532SXin Yin }
290c665b394SJeffle Xu 
291c665b394SJeffle Xu static void erofs_fscache_readahead(struct readahead_control *rac)
292c665b394SJeffle Xu {
293be62c519SJingbo Xu 	struct erofs_fscache_request *req;
294c665b394SJeffle Xu 
295c665b394SJeffle Xu 	if (!readahead_count(rac))
296c665b394SJeffle Xu 		return;
297c665b394SJeffle Xu 
298be62c519SJingbo Xu 	req = erofs_fscache_req_alloc(rac->mapping,
299be62c519SJingbo Xu 			readahead_pos(rac), readahead_length(rac));
300be62c519SJingbo Xu 	if (IS_ERR(req))
301c665b394SJeffle Xu 		return;
302c665b394SJeffle Xu 
303be62c519SJingbo Xu 	/* The request completion will drop refs on the folios. */
304be62c519SJingbo Xu 	while (readahead_folio(rac))
305be62c519SJingbo Xu 		;
306be62c519SJingbo Xu 
307be62c519SJingbo Xu 	erofs_fscache_data_read(req);
308be62c519SJingbo Xu 	erofs_fscache_req_put(req);
309c665b394SJeffle Xu }
310c665b394SJeffle Xu 
311c6be2bd0SJeffle Xu static const struct address_space_operations erofs_fscache_meta_aops = {
312fdaf9a58SLinus Torvalds 	.read_folio = erofs_fscache_meta_read_folio,
313c6be2bd0SJeffle Xu };
314c6be2bd0SJeffle Xu 
3151442b02bSJeffle Xu const struct address_space_operations erofs_fscache_access_aops = {
316fdaf9a58SLinus Torvalds 	.read_folio = erofs_fscache_read_folio,
317c665b394SJeffle Xu 	.readahead = erofs_fscache_readahead,
3181442b02bSJeffle Xu };
3191442b02bSJeffle Xu 
3208b7adf1dSJia Zhu static void erofs_fscache_domain_put(struct erofs_domain *domain)
3218b7adf1dSJia Zhu {
3228b7adf1dSJia Zhu 	mutex_lock(&erofs_domain_list_lock);
3238b7adf1dSJia Zhu 	if (refcount_dec_and_test(&domain->ref)) {
3248b7adf1dSJia Zhu 		list_del(&domain->list);
325a9849560SJia Zhu 		if (list_empty(&erofs_domain_list)) {
326a9849560SJia Zhu 			kern_unmount(erofs_pseudo_mnt);
327a9849560SJia Zhu 			erofs_pseudo_mnt = NULL;
328a9849560SJia Zhu 		}
3298b7adf1dSJia Zhu 		fscache_relinquish_volume(domain->volume, NULL, false);
3307032809aSJingbo Xu 		mutex_unlock(&erofs_domain_list_lock);
3318b7adf1dSJia Zhu 		kfree(domain->domain_id);
3328b7adf1dSJia Zhu 		kfree(domain);
3338b7adf1dSJia Zhu 		return;
3348b7adf1dSJia Zhu 	}
3358b7adf1dSJia Zhu 	mutex_unlock(&erofs_domain_list_lock);
3368b7adf1dSJia Zhu }
3378b7adf1dSJia Zhu 
3388b7adf1dSJia Zhu static int erofs_fscache_register_volume(struct super_block *sb)
3398b7adf1dSJia Zhu {
3408b7adf1dSJia Zhu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
34139bfcb81SJingbo Xu 	char *domain_id = sbi->domain_id;
3428b7adf1dSJia Zhu 	struct fscache_volume *volume;
3438b7adf1dSJia Zhu 	char *name;
3448b7adf1dSJia Zhu 	int ret = 0;
3458b7adf1dSJia Zhu 
3468b7adf1dSJia Zhu 	name = kasprintf(GFP_KERNEL, "erofs,%s",
34739bfcb81SJingbo Xu 			 domain_id ? domain_id : sbi->fsid);
3488b7adf1dSJia Zhu 	if (!name)
3498b7adf1dSJia Zhu 		return -ENOMEM;
3508b7adf1dSJia Zhu 
3518b7adf1dSJia Zhu 	volume = fscache_acquire_volume(name, NULL, NULL, 0);
3528b7adf1dSJia Zhu 	if (IS_ERR_OR_NULL(volume)) {
3538b7adf1dSJia Zhu 		erofs_err(sb, "failed to register volume for %s", name);
3548b7adf1dSJia Zhu 		ret = volume ? PTR_ERR(volume) : -EOPNOTSUPP;
3558b7adf1dSJia Zhu 		volume = NULL;
3568b7adf1dSJia Zhu 	}
3578b7adf1dSJia Zhu 
3588b7adf1dSJia Zhu 	sbi->volume = volume;
3598b7adf1dSJia Zhu 	kfree(name);
3608b7adf1dSJia Zhu 	return ret;
3618b7adf1dSJia Zhu }
3628b7adf1dSJia Zhu 
3638b7adf1dSJia Zhu static int erofs_fscache_init_domain(struct super_block *sb)
3648b7adf1dSJia Zhu {
3658b7adf1dSJia Zhu 	int err;
3668b7adf1dSJia Zhu 	struct erofs_domain *domain;
3678b7adf1dSJia Zhu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
3688b7adf1dSJia Zhu 
3698b7adf1dSJia Zhu 	domain = kzalloc(sizeof(struct erofs_domain), GFP_KERNEL);
3708b7adf1dSJia Zhu 	if (!domain)
3718b7adf1dSJia Zhu 		return -ENOMEM;
3728b7adf1dSJia Zhu 
37339bfcb81SJingbo Xu 	domain->domain_id = kstrdup(sbi->domain_id, GFP_KERNEL);
3748b7adf1dSJia Zhu 	if (!domain->domain_id) {
3758b7adf1dSJia Zhu 		kfree(domain);
3768b7adf1dSJia Zhu 		return -ENOMEM;
3778b7adf1dSJia Zhu 	}
3788b7adf1dSJia Zhu 
3798b7adf1dSJia Zhu 	err = erofs_fscache_register_volume(sb);
3808b7adf1dSJia Zhu 	if (err)
3818b7adf1dSJia Zhu 		goto out;
3828b7adf1dSJia Zhu 
383a9849560SJia Zhu 	if (!erofs_pseudo_mnt) {
384*51b27119SAl Viro 		struct vfsmount *mnt = kern_mount(&erofs_fs_type);
385*51b27119SAl Viro 		if (IS_ERR(mnt)) {
386*51b27119SAl Viro 			err = PTR_ERR(mnt);
387a9849560SJia Zhu 			goto out;
388a9849560SJia Zhu 		}
389*51b27119SAl Viro 		erofs_pseudo_mnt = mnt;
390a9849560SJia Zhu 	}
391a9849560SJia Zhu 
3928b7adf1dSJia Zhu 	domain->volume = sbi->volume;
3938b7adf1dSJia Zhu 	refcount_set(&domain->ref, 1);
3948b7adf1dSJia Zhu 	list_add(&domain->list, &erofs_domain_list);
3958b7adf1dSJia Zhu 	sbi->domain = domain;
3968b7adf1dSJia Zhu 	return 0;
3978b7adf1dSJia Zhu out:
3988b7adf1dSJia Zhu 	kfree(domain->domain_id);
3998b7adf1dSJia Zhu 	kfree(domain);
4008b7adf1dSJia Zhu 	return err;
4018b7adf1dSJia Zhu }
4028b7adf1dSJia Zhu 
4038b7adf1dSJia Zhu static int erofs_fscache_register_domain(struct super_block *sb)
4048b7adf1dSJia Zhu {
4058b7adf1dSJia Zhu 	int err;
4068b7adf1dSJia Zhu 	struct erofs_domain *domain;
4078b7adf1dSJia Zhu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
4088b7adf1dSJia Zhu 
4098b7adf1dSJia Zhu 	mutex_lock(&erofs_domain_list_lock);
4108b7adf1dSJia Zhu 	list_for_each_entry(domain, &erofs_domain_list, list) {
41139bfcb81SJingbo Xu 		if (!strcmp(domain->domain_id, sbi->domain_id)) {
4128b7adf1dSJia Zhu 			sbi->domain = domain;
4138b7adf1dSJia Zhu 			sbi->volume = domain->volume;
4148b7adf1dSJia Zhu 			refcount_inc(&domain->ref);
4158b7adf1dSJia Zhu 			mutex_unlock(&erofs_domain_list_lock);
4168b7adf1dSJia Zhu 			return 0;
4178b7adf1dSJia Zhu 		}
4188b7adf1dSJia Zhu 	}
4198b7adf1dSJia Zhu 	err = erofs_fscache_init_domain(sb);
4208b7adf1dSJia Zhu 	mutex_unlock(&erofs_domain_list_lock);
4218b7adf1dSJia Zhu 	return err;
4228b7adf1dSJia Zhu }
4238b7adf1dSJia Zhu 
42461fef989SJingbo Xu static struct erofs_fscache *erofs_fscache_acquire_cookie(struct super_block *sb,
42561fef989SJingbo Xu 						char *name, unsigned int flags)
426c6be2bd0SJeffle Xu {
427c6be2bd0SJeffle Xu 	struct fscache_volume *volume = EROFS_SB(sb)->volume;
428c6be2bd0SJeffle Xu 	struct erofs_fscache *ctx;
429c6be2bd0SJeffle Xu 	struct fscache_cookie *cookie;
43061fef989SJingbo Xu 	struct super_block *isb;
43161fef989SJingbo Xu 	struct inode *inode;
432c6be2bd0SJeffle Xu 	int ret;
433c6be2bd0SJeffle Xu 
434c6be2bd0SJeffle Xu 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
435c6be2bd0SJeffle Xu 	if (!ctx)
436e1de2da0SJia Zhu 		return ERR_PTR(-ENOMEM);
4372dfb8c3bSJingbo Xu 	INIT_LIST_HEAD(&ctx->node);
4382dfb8c3bSJingbo Xu 	refcount_set(&ctx->ref, 1);
439c6be2bd0SJeffle Xu 
440c6be2bd0SJeffle Xu 	cookie = fscache_acquire_cookie(volume, FSCACHE_ADV_WANT_CACHE_SIZE,
441c6be2bd0SJeffle Xu 					name, strlen(name), NULL, 0, 0);
442c6be2bd0SJeffle Xu 	if (!cookie) {
443c6be2bd0SJeffle Xu 		erofs_err(sb, "failed to get cookie for %s", name);
444c6be2bd0SJeffle Xu 		ret = -EINVAL;
445c6be2bd0SJeffle Xu 		goto err;
446c6be2bd0SJeffle Xu 	}
447c6be2bd0SJeffle Xu 	fscache_use_cookie(cookie, false);
448c6be2bd0SJeffle Xu 
44961fef989SJingbo Xu 	/*
45061fef989SJingbo Xu 	 * Allocate anonymous inode in global pseudo mount for shareable blobs,
45161fef989SJingbo Xu 	 * so that they are accessible among erofs fs instances.
45261fef989SJingbo Xu 	 */
45361fef989SJingbo Xu 	isb = flags & EROFS_REG_COOKIE_SHARE ? erofs_pseudo_mnt->mnt_sb : sb;
45461fef989SJingbo Xu 	inode = new_inode(isb);
455b02c602fSJeffle Xu 	if (!inode) {
456b02c602fSJeffle Xu 		erofs_err(sb, "failed to get anon inode for %s", name);
457b02c602fSJeffle Xu 		ret = -ENOMEM;
458b02c602fSJeffle Xu 		goto err_cookie;
459b02c602fSJeffle Xu 	}
460b02c602fSJeffle Xu 
461b02c602fSJeffle Xu 	inode->i_size = OFFSET_MAX;
462b02c602fSJeffle Xu 	inode->i_mapping->a_ops = &erofs_fscache_meta_aops;
463b02c602fSJeffle Xu 	mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
4643acea5fcSJingbo Xu 	inode->i_blkbits = EROFS_SB(sb)->blkszbits;
465bdfa9014SJingbo Xu 	inode->i_private = ctx;
466b02c602fSJeffle Xu 
46761fef989SJingbo Xu 	ctx->cookie = cookie;
468b02c602fSJeffle Xu 	ctx->inode = inode;
469e1de2da0SJia Zhu 	return ctx;
470b02c602fSJeffle Xu 
471b02c602fSJeffle Xu err_cookie:
47261fef989SJingbo Xu 	fscache_unuse_cookie(cookie, NULL, NULL);
47361fef989SJingbo Xu 	fscache_relinquish_cookie(cookie, false);
474b02c602fSJeffle Xu err:
475b02c602fSJeffle Xu 	kfree(ctx);
476e1de2da0SJia Zhu 	return ERR_PTR(ret);
477b02c602fSJeffle Xu }
478b02c602fSJeffle Xu 
4797d419637SJia Zhu static void erofs_fscache_relinquish_cookie(struct erofs_fscache *ctx)
480b02c602fSJeffle Xu {
481b02c602fSJeffle Xu 	fscache_unuse_cookie(ctx->cookie, NULL, NULL);
482b02c602fSJeffle Xu 	fscache_relinquish_cookie(ctx->cookie, false);
4833c265d7dSJeffle Xu 	iput(ctx->inode);
4847d419637SJia Zhu 	kfree(ctx->name);
485b02c602fSJeffle Xu 	kfree(ctx);
486b02c602fSJeffle Xu }
487b02c602fSJeffle Xu 
48861fef989SJingbo Xu static struct erofs_fscache *erofs_domain_init_cookie(struct super_block *sb,
48961fef989SJingbo Xu 						char *name, unsigned int flags)
4907d419637SJia Zhu {
4917d419637SJia Zhu 	struct erofs_fscache *ctx;
4927d419637SJia Zhu 	struct erofs_domain *domain = EROFS_SB(sb)->domain;
4937d419637SJia Zhu 
49427f2a2dcSHou Tao 	ctx = erofs_fscache_acquire_cookie(sb, name, flags);
4957d419637SJia Zhu 	if (IS_ERR(ctx))
4967d419637SJia Zhu 		return ctx;
4977d419637SJia Zhu 
4987d419637SJia Zhu 	ctx->name = kstrdup(name, GFP_KERNEL);
4997d419637SJia Zhu 	if (!ctx->name) {
5007d419637SJia Zhu 		erofs_fscache_relinquish_cookie(ctx);
50161fef989SJingbo Xu 		return ERR_PTR(-ENOMEM);
5027d419637SJia Zhu 	}
5037d419637SJia Zhu 
50461fef989SJingbo Xu 	refcount_inc(&domain->ref);
50561fef989SJingbo Xu 	ctx->domain = domain;
50661fef989SJingbo Xu 	list_add(&ctx->node, &erofs_domain_cookies_list);
50761fef989SJingbo Xu 	return ctx;
50861fef989SJingbo Xu }
50961fef989SJingbo Xu 
51061fef989SJingbo Xu static struct erofs_fscache *erofs_domain_register_cookie(struct super_block *sb,
51161fef989SJingbo Xu 						char *name, unsigned int flags)
5127d419637SJia Zhu {
5137d419637SJia Zhu 	struct erofs_fscache *ctx;
5147d419637SJia Zhu 	struct erofs_domain *domain = EROFS_SB(sb)->domain;
5157d419637SJia Zhu 
51661fef989SJingbo Xu 	flags |= EROFS_REG_COOKIE_SHARE;
5177d419637SJia Zhu 	mutex_lock(&erofs_domain_cookies_lock);
5182dfb8c3bSJingbo Xu 	list_for_each_entry(ctx, &erofs_domain_cookies_list, node) {
5192dfb8c3bSJingbo Xu 		if (ctx->domain != domain || strcmp(ctx->name, name))
5207d419637SJia Zhu 			continue;
52127f2a2dcSHou Tao 		if (!(flags & EROFS_REG_COOKIE_NEED_NOEXIST)) {
5222dfb8c3bSJingbo Xu 			refcount_inc(&ctx->ref);
52327f2a2dcSHou Tao 		} else {
52427f2a2dcSHou Tao 			erofs_err(sb, "%s already exists in domain %s", name,
52527f2a2dcSHou Tao 				  domain->domain_id);
52627f2a2dcSHou Tao 			ctx = ERR_PTR(-EEXIST);
52727f2a2dcSHou Tao 		}
5287d419637SJia Zhu 		mutex_unlock(&erofs_domain_cookies_lock);
5297d419637SJia Zhu 		return ctx;
5307d419637SJia Zhu 	}
53161fef989SJingbo Xu 	ctx = erofs_domain_init_cookie(sb, name, flags);
5327d419637SJia Zhu 	mutex_unlock(&erofs_domain_cookies_lock);
5337d419637SJia Zhu 	return ctx;
5347d419637SJia Zhu }
5357d419637SJia Zhu 
5367d419637SJia Zhu struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
53727f2a2dcSHou Tao 						    char *name,
53827f2a2dcSHou Tao 						    unsigned int flags)
5397d419637SJia Zhu {
54039bfcb81SJingbo Xu 	if (EROFS_SB(sb)->domain_id)
54127f2a2dcSHou Tao 		return erofs_domain_register_cookie(sb, name, flags);
54227f2a2dcSHou Tao 	return erofs_fscache_acquire_cookie(sb, name, flags);
5437d419637SJia Zhu }
5447d419637SJia Zhu 
5457d419637SJia Zhu void erofs_fscache_unregister_cookie(struct erofs_fscache *ctx)
5467d419637SJia Zhu {
5472dfb8c3bSJingbo Xu 	struct erofs_domain *domain = NULL;
5487d419637SJia Zhu 
5497d419637SJia Zhu 	if (!ctx)
5507d419637SJia Zhu 		return;
5512dfb8c3bSJingbo Xu 	if (!ctx->domain)
5522dfb8c3bSJingbo Xu 		return erofs_fscache_relinquish_cookie(ctx);
5537d419637SJia Zhu 
5542dfb8c3bSJingbo Xu 	mutex_lock(&erofs_domain_cookies_lock);
5552dfb8c3bSJingbo Xu 	if (refcount_dec_and_test(&ctx->ref)) {
5562dfb8c3bSJingbo Xu 		domain = ctx->domain;
5572dfb8c3bSJingbo Xu 		list_del(&ctx->node);
5587d419637SJia Zhu 		erofs_fscache_relinquish_cookie(ctx);
5592dfb8c3bSJingbo Xu 	}
5602dfb8c3bSJingbo Xu 	mutex_unlock(&erofs_domain_cookies_lock);
5612dfb8c3bSJingbo Xu 	if (domain)
5627d419637SJia Zhu 		erofs_fscache_domain_put(domain);
5637d419637SJia Zhu }
5647d419637SJia Zhu 
565c6be2bd0SJeffle Xu int erofs_fscache_register_fs(struct super_block *sb)
566c6be2bd0SJeffle Xu {
5678b7adf1dSJia Zhu 	int ret;
568c6be2bd0SJeffle Xu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
569e1de2da0SJia Zhu 	struct erofs_fscache *fscache;
57061fef989SJingbo Xu 	unsigned int flags = 0;
571c6be2bd0SJeffle Xu 
57239bfcb81SJingbo Xu 	if (sbi->domain_id)
5738b7adf1dSJia Zhu 		ret = erofs_fscache_register_domain(sb);
5748b7adf1dSJia Zhu 	else
5758b7adf1dSJia Zhu 		ret = erofs_fscache_register_volume(sb);
5768b7adf1dSJia Zhu 	if (ret)
5778b7adf1dSJia Zhu 		return ret;
578c6be2bd0SJeffle Xu 
57927f2a2dcSHou Tao 	/*
58027f2a2dcSHou Tao 	 * When shared domain is enabled, using NEED_NOEXIST to guarantee
58127f2a2dcSHou Tao 	 * the primary data blob (aka fsid) is unique in the shared domain.
58227f2a2dcSHou Tao 	 *
58327f2a2dcSHou Tao 	 * For non-shared-domain case, fscache_acquire_volume() invoked by
58427f2a2dcSHou Tao 	 * erofs_fscache_register_volume() has already guaranteed
58527f2a2dcSHou Tao 	 * the uniqueness of primary data blob.
58627f2a2dcSHou Tao 	 *
58727f2a2dcSHou Tao 	 * Acquired domain/volume will be relinquished in kill_sb() on error.
58827f2a2dcSHou Tao 	 */
58927f2a2dcSHou Tao 	if (sbi->domain_id)
59027f2a2dcSHou Tao 		flags |= EROFS_REG_COOKIE_NEED_NOEXIST;
59127f2a2dcSHou Tao 	fscache = erofs_fscache_register_cookie(sb, sbi->fsid, flags);
592e1de2da0SJia Zhu 	if (IS_ERR(fscache))
593e1de2da0SJia Zhu 		return PTR_ERR(fscache);
594e1de2da0SJia Zhu 
595e1de2da0SJia Zhu 	sbi->s_fscache = fscache;
596e1de2da0SJia Zhu 	return 0;
597c6be2bd0SJeffle Xu }
598c6be2bd0SJeffle Xu 
599c6be2bd0SJeffle Xu void erofs_fscache_unregister_fs(struct super_block *sb)
600c6be2bd0SJeffle Xu {
601c6be2bd0SJeffle Xu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
602c6be2bd0SJeffle Xu 
603e1de2da0SJia Zhu 	erofs_fscache_unregister_cookie(sbi->s_fscache);
6048b7adf1dSJia Zhu 
6058b7adf1dSJia Zhu 	if (sbi->domain)
6068b7adf1dSJia Zhu 		erofs_fscache_domain_put(sbi->domain);
6078b7adf1dSJia Zhu 	else
608c6be2bd0SJeffle Xu 		fscache_relinquish_volume(sbi->volume, NULL, false);
6098b7adf1dSJia Zhu 
610e1de2da0SJia Zhu 	sbi->s_fscache = NULL;
611c6be2bd0SJeffle Xu 	sbi->volume = NULL;
6128b7adf1dSJia Zhu 	sbi->domain = NULL;
613c6be2bd0SJeffle Xu }
614