xref: /openbmc/linux/fs/erofs/super.c (revision b217945eab7a0c37738cf37b89fdcc5b17d64c40)
147e4937aSGao Xiang // SPDX-License-Identifier: GPL-2.0-only
247e4937aSGao Xiang /*
347e4937aSGao Xiang  * Copyright (C) 2017-2018 HUAWEI, Inc.
4592e7cd0SAlexander A. Klimov  *             https://www.huawei.com/
52b5379f7SGao Xiang  * Copyright (C) 2021, Alibaba Cloud
647e4937aSGao Xiang  */
747e4937aSGao Xiang #include <linux/module.h>
847e4937aSGao Xiang #include <linux/statfs.h>
947e4937aSGao Xiang #include <linux/parser.h>
1047e4937aSGao Xiang #include <linux/seq_file.h>
11b858a484SPratik Shinde #include <linux/crc32c.h>
12f57a3fe4SChao Yu #include <linux/fs_context.h>
13f57a3fe4SChao Yu #include <linux/fs_parser.h>
1406252e9cSGao Xiang #include <linux/dax.h>
153e917cc3SHongnan Li #include <linux/exportfs.h>
1647e4937aSGao Xiang #include "xattr.h"
1747e4937aSGao Xiang 
1847e4937aSGao Xiang #define CREATE_TRACE_POINTS
1947e4937aSGao Xiang #include <trace/events/erofs.h>
2047e4937aSGao Xiang 
2147e4937aSGao Xiang static struct kmem_cache *erofs_inode_cachep __read_mostly;
2247e4937aSGao Xiang 
_erofs_err(struct super_block * sb,const char * func,const char * fmt,...)23428f27ccSFerry Meng void _erofs_err(struct super_block *sb, const char *func, const char *fmt, ...)
244f761fa2SGao Xiang {
254f761fa2SGao Xiang 	struct va_format vaf;
264f761fa2SGao Xiang 	va_list args;
274f761fa2SGao Xiang 
284f761fa2SGao Xiang 	va_start(args, fmt);
294f761fa2SGao Xiang 
304f761fa2SGao Xiang 	vaf.fmt = fmt;
314f761fa2SGao Xiang 	vaf.va = &args;
324f761fa2SGao Xiang 
33428f27ccSFerry Meng 	pr_err("(device %s): %s: %pV", sb->s_id, func, &vaf);
344f761fa2SGao Xiang 	va_end(args);
354f761fa2SGao Xiang }
364f761fa2SGao Xiang 
_erofs_info(struct super_block * sb,const char * func,const char * fmt,...)37428f27ccSFerry Meng void _erofs_info(struct super_block *sb, const char *func, const char *fmt, ...)
384f761fa2SGao Xiang {
394f761fa2SGao Xiang 	struct va_format vaf;
404f761fa2SGao Xiang 	va_list args;
414f761fa2SGao Xiang 
424f761fa2SGao Xiang 	va_start(args, fmt);
434f761fa2SGao Xiang 
444f761fa2SGao Xiang 	vaf.fmt = fmt;
454f761fa2SGao Xiang 	vaf.va = &args;
464f761fa2SGao Xiang 
474f761fa2SGao Xiang 	pr_info("(device %s): %pV", sb->s_id, &vaf);
484f761fa2SGao Xiang 	va_end(args);
494f761fa2SGao Xiang }
504f761fa2SGao Xiang 
erofs_superblock_csum_verify(struct super_block * sb,void * sbdata)51b858a484SPratik Shinde static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata)
52b858a484SPratik Shinde {
533acea5fcSJingbo Xu 	size_t len = 1 << EROFS_SB(sb)->blkszbits;
54b858a484SPratik Shinde 	struct erofs_super_block *dsb;
55b858a484SPratik Shinde 	u32 expected_crc, crc;
56b858a484SPratik Shinde 
573acea5fcSJingbo Xu 	if (len > EROFS_SUPER_OFFSET)
583acea5fcSJingbo Xu 		len -= EROFS_SUPER_OFFSET;
593acea5fcSJingbo Xu 
603acea5fcSJingbo Xu 	dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET, len, GFP_KERNEL);
61b858a484SPratik Shinde 	if (!dsb)
62b858a484SPratik Shinde 		return -ENOMEM;
63b858a484SPratik Shinde 
64b858a484SPratik Shinde 	expected_crc = le32_to_cpu(dsb->checksum);
65b858a484SPratik Shinde 	dsb->checksum = 0;
66b858a484SPratik Shinde 	/* to allow for x86 boot sectors and other oddities. */
673acea5fcSJingbo Xu 	crc = crc32c(~0, dsb, len);
68b858a484SPratik Shinde 	kfree(dsb);
69b858a484SPratik Shinde 
70b858a484SPratik Shinde 	if (crc != expected_crc) {
71b858a484SPratik Shinde 		erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected",
72b858a484SPratik Shinde 			  crc, expected_crc);
73b858a484SPratik Shinde 		return -EBADMSG;
74b858a484SPratik Shinde 	}
75b858a484SPratik Shinde 	return 0;
76b858a484SPratik Shinde }
77b858a484SPratik Shinde 
erofs_inode_init_once(void * ptr)7899634bf3SGao Xiang static void erofs_inode_init_once(void *ptr)
7947e4937aSGao Xiang {
80a5876e24SGao Xiang 	struct erofs_inode *vi = ptr;
8147e4937aSGao Xiang 
8247e4937aSGao Xiang 	inode_init_once(&vi->vfs_inode);
8347e4937aSGao Xiang }
8447e4937aSGao Xiang 
erofs_alloc_inode(struct super_block * sb)8599634bf3SGao Xiang static struct inode *erofs_alloc_inode(struct super_block *sb)
8647e4937aSGao Xiang {
87a5876e24SGao Xiang 	struct erofs_inode *vi =
88fd60b288SMuchun Song 		alloc_inode_sb(sb, erofs_inode_cachep, GFP_KERNEL);
8947e4937aSGao Xiang 
9047e4937aSGao Xiang 	if (!vi)
9147e4937aSGao Xiang 		return NULL;
9247e4937aSGao Xiang 
9347e4937aSGao Xiang 	/* zero out everything except vfs_inode */
94a5876e24SGao Xiang 	memset(vi, 0, offsetof(struct erofs_inode, vfs_inode));
9547e4937aSGao Xiang 	return &vi->vfs_inode;
9647e4937aSGao Xiang }
9747e4937aSGao Xiang 
erofs_free_inode(struct inode * inode)9899634bf3SGao Xiang static void erofs_free_inode(struct inode *inode)
9947e4937aSGao Xiang {
100a5876e24SGao Xiang 	struct erofs_inode *vi = EROFS_I(inode);
10147e4937aSGao Xiang 
102a2c75c81SGao Xiang 	if (inode->i_op == &erofs_fast_symlink_iops)
10347e4937aSGao Xiang 		kfree(inode->i_link);
10447e4937aSGao Xiang 	kfree(vi->xattr_shared_xattrs);
10547e4937aSGao Xiang 	kmem_cache_free(erofs_inode_cachep, vi);
10647e4937aSGao Xiang }
10747e4937aSGao Xiang 
check_layout_compatibility(struct super_block * sb,struct erofs_super_block * dsb)10847e4937aSGao Xiang static bool check_layout_compatibility(struct super_block *sb,
1090259f209SGao Xiang 				       struct erofs_super_block *dsb)
11047e4937aSGao Xiang {
1110259f209SGao Xiang 	const unsigned int feature = le32_to_cpu(dsb->feature_incompat);
11247e4937aSGao Xiang 
113426a9308SGao Xiang 	EROFS_SB(sb)->feature_incompat = feature;
11447e4937aSGao Xiang 
11547e4937aSGao Xiang 	/* check if current kernel meets all mandatory requirements */
116426a9308SGao Xiang 	if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) {
117428f27ccSFerry Meng 		erofs_err(sb, "unidentified incompatible feature %x, please upgrade kernel",
118426a9308SGao Xiang 			   feature & ~EROFS_ALL_FEATURE_INCOMPAT);
11947e4937aSGao Xiang 		return false;
12047e4937aSGao Xiang 	}
12147e4937aSGao Xiang 	return true;
12247e4937aSGao Xiang }
12347e4937aSGao Xiang 
12414373711SGao Xiang /* read variable-sized metadata, offset will be aligned by 4-byte */
erofs_read_metadata(struct super_block * sb,struct erofs_buf * buf,erofs_off_t * offset,int * lengthp)1259e382914SJingbo Xu void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
12614373711SGao Xiang 			  erofs_off_t *offset, int *lengthp)
12714373711SGao Xiang {
12814373711SGao Xiang 	u8 *buffer, *ptr;
12914373711SGao Xiang 	int len, i, cnt;
13014373711SGao Xiang 
13114373711SGao Xiang 	*offset = round_up(*offset, 4);
132eb2c5e41SGao Xiang 	ptr = erofs_bread(buf, erofs_blknr(sb, *offset), EROFS_KMAP);
1332b5379f7SGao Xiang 	if (IS_ERR(ptr))
1342b5379f7SGao Xiang 		return ptr;
13514373711SGao Xiang 
1363acea5fcSJingbo Xu 	len = le16_to_cpu(*(__le16 *)&ptr[erofs_blkoff(sb, *offset)]);
13714373711SGao Xiang 	if (!len)
13814373711SGao Xiang 		len = U16_MAX + 1;
13914373711SGao Xiang 	buffer = kmalloc(len, GFP_KERNEL);
1402b5379f7SGao Xiang 	if (!buffer)
1412b5379f7SGao Xiang 		return ERR_PTR(-ENOMEM);
14214373711SGao Xiang 	*offset += sizeof(__le16);
14314373711SGao Xiang 	*lengthp = len;
14414373711SGao Xiang 
14514373711SGao Xiang 	for (i = 0; i < len; i += cnt) {
1463acea5fcSJingbo Xu 		cnt = min_t(int, sb->s_blocksize - erofs_blkoff(sb, *offset),
1473acea5fcSJingbo Xu 			    len - i);
148eb2c5e41SGao Xiang 		ptr = erofs_bread(buf, erofs_blknr(sb, *offset), EROFS_KMAP);
1492b5379f7SGao Xiang 		if (IS_ERR(ptr)) {
15014373711SGao Xiang 			kfree(buffer);
1512b5379f7SGao Xiang 			return ptr;
15214373711SGao Xiang 		}
1533acea5fcSJingbo Xu 		memcpy(buffer + i, ptr + erofs_blkoff(sb, *offset), cnt);
15414373711SGao Xiang 		*offset += cnt;
15514373711SGao Xiang 	}
15614373711SGao Xiang 	return buffer;
15714373711SGao Xiang }
15814373711SGao Xiang 
159586814edSGao Xiang #ifndef CONFIG_EROFS_FS_ZIP
z_erofs_parse_cfgs(struct super_block * sb,struct erofs_super_block * dsb)160586814edSGao Xiang static int z_erofs_parse_cfgs(struct super_block *sb,
16114373711SGao Xiang 			      struct erofs_super_block *dsb)
16214373711SGao Xiang {
163586814edSGao Xiang 	if (!dsb->u1.available_compr_algs)
16414373711SGao Xiang 		return 0;
165586814edSGao Xiang 
166586814edSGao Xiang 	erofs_err(sb, "compression disabled, unable to mount compressed EROFS");
167586814edSGao Xiang 	return -EOPNOTSUPP;
16814373711SGao Xiang }
16914373711SGao Xiang #endif
17014373711SGao Xiang 
erofs_init_device(struct erofs_buf * buf,struct super_block * sb,struct erofs_device_info * dif,erofs_off_t * pos)171ba73eaddSJeffle Xu static int erofs_init_device(struct erofs_buf *buf, struct super_block *sb,
172ba73eaddSJeffle Xu 			     struct erofs_device_info *dif, erofs_off_t *pos)
173ba73eaddSJeffle Xu {
174ba73eaddSJeffle Xu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
175e1de2da0SJia Zhu 	struct erofs_fscache *fscache;
176ba73eaddSJeffle Xu 	struct erofs_deviceslot *dis;
177613139a0SJan Kara 	struct bdev_handle *bdev_handle;
178ba73eaddSJeffle Xu 	void *ptr;
179ba73eaddSJeffle Xu 
1803acea5fcSJingbo Xu 	ptr = erofs_read_metabuf(buf, sb, erofs_blknr(sb, *pos), EROFS_KMAP);
181ba73eaddSJeffle Xu 	if (IS_ERR(ptr))
182ba73eaddSJeffle Xu 		return PTR_ERR(ptr);
1833acea5fcSJingbo Xu 	dis = ptr + erofs_blkoff(sb, *pos);
184ba73eaddSJeffle Xu 
185f939aeeaSJingbo Xu 	if (!sbi->devs->flatdev && !dif->path) {
186ba73eaddSJeffle Xu 		if (!dis->tag[0]) {
187ba73eaddSJeffle Xu 			erofs_err(sb, "empty device tag @ pos %llu", *pos);
188ba73eaddSJeffle Xu 			return -EINVAL;
189ba73eaddSJeffle Xu 		}
190ba73eaddSJeffle Xu 		dif->path = kmemdup_nul(dis->tag, sizeof(dis->tag), GFP_KERNEL);
191ba73eaddSJeffle Xu 		if (!dif->path)
192ba73eaddSJeffle Xu 			return -ENOMEM;
193ba73eaddSJeffle Xu 	}
194ba73eaddSJeffle Xu 
195ba73eaddSJeffle Xu 	if (erofs_is_fscache_mode(sb)) {
19627f2a2dcSHou Tao 		fscache = erofs_fscache_register_cookie(sb, dif->path, 0);
197e1de2da0SJia Zhu 		if (IS_ERR(fscache))
198e1de2da0SJia Zhu 			return PTR_ERR(fscache);
199e1de2da0SJia Zhu 		dif->fscache = fscache;
2008b465fecSJia Zhu 	} else if (!sbi->devs->flatdev) {
201613139a0SJan Kara 		bdev_handle = bdev_open_by_path(dif->path, BLK_OPEN_READ,
202613139a0SJan Kara 						sb->s_type, NULL);
203613139a0SJan Kara 		if (IS_ERR(bdev_handle))
204613139a0SJan Kara 			return PTR_ERR(bdev_handle);
205613139a0SJan Kara 		dif->bdev_handle = bdev_handle;
206613139a0SJan Kara 		dif->dax_dev = fs_dax_get_by_bdev(bdev_handle->bdev,
207613139a0SJan Kara 				&dif->dax_part_off, NULL, NULL);
208ba73eaddSJeffle Xu 	}
209ba73eaddSJeffle Xu 
210ba73eaddSJeffle Xu 	dif->blocks = le32_to_cpu(dis->blocks);
211ba73eaddSJeffle Xu 	dif->mapped_blkaddr = le32_to_cpu(dis->mapped_blkaddr);
212ba73eaddSJeffle Xu 	sbi->total_blocks += dif->blocks;
213ba73eaddSJeffle Xu 	*pos += EROFS_DEVT_SLOT_SIZE;
214ba73eaddSJeffle Xu 	return 0;
215ba73eaddSJeffle Xu }
216ba73eaddSJeffle Xu 
erofs_scan_devices(struct super_block * sb,struct erofs_super_block * dsb)217ba73eaddSJeffle Xu static int erofs_scan_devices(struct super_block *sb,
218dfeab2e9SGao Xiang 			      struct erofs_super_block *dsb)
219dfeab2e9SGao Xiang {
220dfeab2e9SGao Xiang 	struct erofs_sb_info *sbi = EROFS_SB(sb);
221dfeab2e9SGao Xiang 	unsigned int ondisk_extradevs;
222dfeab2e9SGao Xiang 	erofs_off_t pos;
2232b5379f7SGao Xiang 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
224dfeab2e9SGao Xiang 	struct erofs_device_info *dif;
225dfeab2e9SGao Xiang 	int id, err = 0;
226dfeab2e9SGao Xiang 
227dfeab2e9SGao Xiang 	sbi->total_blocks = sbi->primarydevice_blocks;
228dfeab2e9SGao Xiang 	if (!erofs_sb_has_device_table(sbi))
229dfeab2e9SGao Xiang 		ondisk_extradevs = 0;
230dfeab2e9SGao Xiang 	else
231dfeab2e9SGao Xiang 		ondisk_extradevs = le16_to_cpu(dsb->extra_devices);
232dfeab2e9SGao Xiang 
233ba73eaddSJeffle Xu 	if (sbi->devs->extra_devices &&
234ba73eaddSJeffle Xu 	    ondisk_extradevs != sbi->devs->extra_devices) {
235dfeab2e9SGao Xiang 		erofs_err(sb, "extra devices don't match (ondisk %u, given %u)",
236dfeab2e9SGao Xiang 			  ondisk_extradevs, sbi->devs->extra_devices);
237dfeab2e9SGao Xiang 		return -EINVAL;
238dfeab2e9SGao Xiang 	}
239dfeab2e9SGao Xiang 	if (!ondisk_extradevs)
240dfeab2e9SGao Xiang 		return 0;
241dfeab2e9SGao Xiang 
2428b465fecSJia Zhu 	if (!sbi->devs->extra_devices && !erofs_is_fscache_mode(sb))
2438b465fecSJia Zhu 		sbi->devs->flatdev = true;
2448b465fecSJia Zhu 
245dfeab2e9SGao Xiang 	sbi->device_id_mask = roundup_pow_of_two(ondisk_extradevs + 1) - 1;
246dfeab2e9SGao Xiang 	pos = le16_to_cpu(dsb->devt_slotoff) * EROFS_DEVT_SLOT_SIZE;
247dfeab2e9SGao Xiang 	down_read(&sbi->devs->rwsem);
248ba73eaddSJeffle Xu 	if (sbi->devs->extra_devices) {
249dfeab2e9SGao Xiang 		idr_for_each_entry(&sbi->devs->tree, dif, id) {
250ba73eaddSJeffle Xu 			err = erofs_init_device(&buf, sb, dif, &pos);
251955b478eSJeffle Xu 			if (err)
252955b478eSJeffle Xu 				break;
253ba73eaddSJeffle Xu 		}
254955b478eSJeffle Xu 	} else {
255ba73eaddSJeffle Xu 		for (id = 0; id < ondisk_extradevs; id++) {
256ba73eaddSJeffle Xu 			dif = kzalloc(sizeof(*dif), GFP_KERNEL);
257ba73eaddSJeffle Xu 			if (!dif) {
258ba73eaddSJeffle Xu 				err = -ENOMEM;
2592b5379f7SGao Xiang 				break;
260dfeab2e9SGao Xiang 			}
26193b856bbSJeffle Xu 
262ba73eaddSJeffle Xu 			err = idr_alloc(&sbi->devs->tree, dif, 0, 0, GFP_KERNEL);
263ba73eaddSJeffle Xu 			if (err < 0) {
264ba73eaddSJeffle Xu 				kfree(dif);
265ba73eaddSJeffle Xu 				break;
266ba73eaddSJeffle Xu 			}
267ba73eaddSJeffle Xu 			++sbi->devs->extra_devices;
268ba73eaddSJeffle Xu 
269ba73eaddSJeffle Xu 			err = erofs_init_device(&buf, sb, dif, &pos);
270ba73eaddSJeffle Xu 			if (err)
271ba73eaddSJeffle Xu 				break;
272ba73eaddSJeffle Xu 		}
273dfeab2e9SGao Xiang 	}
274dfeab2e9SGao Xiang 	up_read(&sbi->devs->rwsem);
2752b5379f7SGao Xiang 	erofs_put_metabuf(&buf);
276dfeab2e9SGao Xiang 	return err;
277dfeab2e9SGao Xiang }
278dfeab2e9SGao Xiang 
erofs_read_superblock(struct super_block * sb)27999634bf3SGao Xiang static int erofs_read_superblock(struct super_block *sb)
28047e4937aSGao Xiang {
28147e4937aSGao Xiang 	struct erofs_sb_info *sbi;
282ed6e0401SJeffle Xu 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
2830259f209SGao Xiang 	struct erofs_super_block *dsb;
284fe7c2423SGao Xiang 	void *data;
28547e4937aSGao Xiang 	int ret;
28647e4937aSGao Xiang 
287ed6e0401SJeffle Xu 	data = erofs_read_metabuf(&buf, sb, 0, EROFS_KMAP);
288ed6e0401SJeffle Xu 	if (IS_ERR(data)) {
2894f761fa2SGao Xiang 		erofs_err(sb, "cannot read erofs superblock");
290ed6e0401SJeffle Xu 		return PTR_ERR(data);
29147e4937aSGao Xiang 	}
29247e4937aSGao Xiang 
29347e4937aSGao Xiang 	sbi = EROFS_SB(sb);
294fe7c2423SGao Xiang 	dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
29547e4937aSGao Xiang 
29647e4937aSGao Xiang 	ret = -EINVAL;
2970259f209SGao Xiang 	if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
2984f761fa2SGao Xiang 		erofs_err(sb, "cannot find valid erofs superblock");
29947e4937aSGao Xiang 		goto out;
30047e4937aSGao Xiang 	}
30147e4937aSGao Xiang 
302d3c4bdccSJingbo Xu 	sbi->blkszbits  = dsb->blkszbits;
303d3c4bdccSJingbo Xu 	if (sbi->blkszbits < 9 || sbi->blkszbits > PAGE_SHIFT) {
304d3c4bdccSJingbo Xu 		erofs_err(sb, "blkszbits %u isn't supported", sbi->blkszbits);
305d3c4bdccSJingbo Xu 		goto out;
306d3c4bdccSJingbo Xu 	}
307d3c4bdccSJingbo Xu 	if (dsb->dirblkbits) {
308d3c4bdccSJingbo Xu 		erofs_err(sb, "dirblkbits %u isn't supported", dsb->dirblkbits);
309d3c4bdccSJingbo Xu 		goto out;
310d3c4bdccSJingbo Xu 	}
311d3c4bdccSJingbo Xu 
312b858a484SPratik Shinde 	sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
313de06a6a3SGao Xiang 	if (erofs_sb_has_sb_chksum(sbi)) {
314b858a484SPratik Shinde 		ret = erofs_superblock_csum_verify(sb, data);
315b858a484SPratik Shinde 		if (ret)
316b858a484SPratik Shinde 			goto out;
317b858a484SPratik Shinde 	}
318b858a484SPratik Shinde 
3190508c1adSWei Yongjun 	ret = -EINVAL;
3200259f209SGao Xiang 	if (!check_layout_compatibility(sb, dsb))
32147e4937aSGao Xiang 		goto out;
32247e4937aSGao Xiang 
32314373711SGao Xiang 	sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE;
324d3c4bdccSJingbo Xu 	if (sbi->sb_size > PAGE_SIZE - EROFS_SUPER_OFFSET) {
32514373711SGao Xiang 		erofs_err(sb, "invalid sb_extslots %u (more than a fs block)",
32614373711SGao Xiang 			  sbi->sb_size);
32714373711SGao Xiang 		goto out;
32814373711SGao Xiang 	}
329dfeab2e9SGao Xiang 	sbi->primarydevice_blocks = le32_to_cpu(dsb->blocks);
3300259f209SGao Xiang 	sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
33147e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_XATTR
3320259f209SGao Xiang 	sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
3336a318ccdSJingbo Xu 	sbi->xattr_prefix_start = le32_to_cpu(dsb->xattr_prefix_start);
3346a318ccdSJingbo Xu 	sbi->xattr_prefix_count = dsb->xattr_prefix_count;
335fd73a439SJingbo Xu 	sbi->xattr_filter_reserved = dsb->xattr_filter_reserved;
33647e4937aSGao Xiang #endif
3378a765682SGao Xiang 	sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
3380259f209SGao Xiang 	sbi->root_nid = le16_to_cpu(dsb->root_nid);
339cb9bce79SJingbo Xu 	sbi->packed_nid = le64_to_cpu(dsb->packed_nid);
3400259f209SGao Xiang 	sbi->inos = le64_to_cpu(dsb->inos);
34147e4937aSGao Xiang 
3420259f209SGao Xiang 	sbi->build_time = le64_to_cpu(dsb->build_time);
3430259f209SGao Xiang 	sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
34447e4937aSGao Xiang 
3450259f209SGao Xiang 	memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid));
34647e4937aSGao Xiang 
3470259f209SGao Xiang 	ret = strscpy(sbi->volume_name, dsb->volume_name,
3480259f209SGao Xiang 		      sizeof(dsb->volume_name));
34947e4937aSGao Xiang 	if (ret < 0) {	/* -E2BIG */
3504f761fa2SGao Xiang 		erofs_err(sb, "bad volume name without NIL terminator");
35147e4937aSGao Xiang 		ret = -EFSCORRUPTED;
35247e4937aSGao Xiang 		goto out;
35347e4937aSGao Xiang 	}
3545d50538fSHuang Jianan 
3555d50538fSHuang Jianan 	/* parse on-disk compression configurations */
356586814edSGao Xiang 	ret = z_erofs_parse_cfgs(sb, dsb);
357dfeab2e9SGao Xiang 	if (ret < 0)
358dfeab2e9SGao Xiang 		goto out;
359dfeab2e9SGao Xiang 
360dfeab2e9SGao Xiang 	/* handle multiple devices */
361ba73eaddSJeffle Xu 	ret = erofs_scan_devices(sb, dsb);
362ab92184fSYue Hu 
3639c0cc9c7SJeffle Xu 	if (erofs_is_fscache_mode(sb))
3649c0cc9c7SJeffle Xu 		erofs_info(sb, "EXPERIMENTAL fscache-based on-demand read feature in use. Use at your own risk!");
36547e4937aSGao Xiang out:
366ed6e0401SJeffle Xu 	erofs_put_metabuf(&buf);
36747e4937aSGao Xiang 	return ret;
36847e4937aSGao Xiang }
36947e4937aSGao Xiang 
erofs_default_options(struct erofs_sb_info * sbi)3703924179bSBaokun Li static void erofs_default_options(struct erofs_sb_info *sbi)
37147e4937aSGao Xiang {
37247e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP
3733924179bSBaokun Li 	sbi->opt.cache_strategy = EROFS_ZIP_CACHE_READAROUND;
3743924179bSBaokun Li 	sbi->opt.max_sync_decompress_pages = 3;
3753924179bSBaokun Li 	sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_AUTO;
37647e4937aSGao Xiang #endif
37747e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_XATTR
3783924179bSBaokun Li 	set_opt(&sbi->opt, XATTR_USER);
37947e4937aSGao Xiang #endif
38047e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_POSIX_ACL
3813924179bSBaokun Li 	set_opt(&sbi->opt, POSIX_ACL);
38247e4937aSGao Xiang #endif
38347e4937aSGao Xiang }
38447e4937aSGao Xiang 
38547e4937aSGao Xiang enum {
38647e4937aSGao Xiang 	Opt_user_xattr,
38747e4937aSGao Xiang 	Opt_acl,
38847e4937aSGao Xiang 	Opt_cache_strategy,
38906252e9cSGao Xiang 	Opt_dax,
39006252e9cSGao Xiang 	Opt_dax_enum,
391dfeab2e9SGao Xiang 	Opt_device,
3929c0cc9c7SJeffle Xu 	Opt_fsid,
3932ef16441SJia Zhu 	Opt_domain_id,
39447e4937aSGao Xiang 	Opt_err
39547e4937aSGao Xiang };
39647e4937aSGao Xiang 
397f57a3fe4SChao Yu static const struct constant_table erofs_param_cache_strategy[] = {
398f57a3fe4SChao Yu 	{"disabled",	EROFS_ZIP_CACHE_DISABLED},
399f57a3fe4SChao Yu 	{"readahead",	EROFS_ZIP_CACHE_READAHEAD},
400f57a3fe4SChao Yu 	{"readaround",	EROFS_ZIP_CACHE_READAROUND},
401f57a3fe4SChao Yu 	{}
40247e4937aSGao Xiang };
40347e4937aSGao Xiang 
40406252e9cSGao Xiang static const struct constant_table erofs_dax_param_enums[] = {
40506252e9cSGao Xiang 	{"always",	EROFS_MOUNT_DAX_ALWAYS},
40606252e9cSGao Xiang 	{"never",	EROFS_MOUNT_DAX_NEVER},
40706252e9cSGao Xiang 	{}
40806252e9cSGao Xiang };
40906252e9cSGao Xiang 
410f57a3fe4SChao Yu static const struct fs_parameter_spec erofs_fs_parameters[] = {
411f57a3fe4SChao Yu 	fsparam_flag_no("user_xattr",	Opt_user_xattr),
412f57a3fe4SChao Yu 	fsparam_flag_no("acl",		Opt_acl),
413f57a3fe4SChao Yu 	fsparam_enum("cache_strategy",	Opt_cache_strategy,
414f57a3fe4SChao Yu 		     erofs_param_cache_strategy),
41506252e9cSGao Xiang 	fsparam_flag("dax",             Opt_dax),
41606252e9cSGao Xiang 	fsparam_enum("dax",		Opt_dax_enum, erofs_dax_param_enums),
417dfeab2e9SGao Xiang 	fsparam_string("device",	Opt_device),
4189c0cc9c7SJeffle Xu 	fsparam_string("fsid",		Opt_fsid),
4192ef16441SJia Zhu 	fsparam_string("domain_id",	Opt_domain_id),
420f57a3fe4SChao Yu 	{}
421f57a3fe4SChao Yu };
422f57a3fe4SChao Yu 
erofs_fc_set_dax_mode(struct fs_context * fc,unsigned int mode)42306252e9cSGao Xiang static bool erofs_fc_set_dax_mode(struct fs_context *fc, unsigned int mode)
42406252e9cSGao Xiang {
42506252e9cSGao Xiang #ifdef CONFIG_FS_DAX
4263924179bSBaokun Li 	struct erofs_sb_info *sbi = fc->s_fs_info;
42706252e9cSGao Xiang 
42806252e9cSGao Xiang 	switch (mode) {
42906252e9cSGao Xiang 	case EROFS_MOUNT_DAX_ALWAYS:
43006252e9cSGao Xiang 		warnfc(fc, "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
4313924179bSBaokun Li 		set_opt(&sbi->opt, DAX_ALWAYS);
4323924179bSBaokun Li 		clear_opt(&sbi->opt, DAX_NEVER);
43306252e9cSGao Xiang 		return true;
43406252e9cSGao Xiang 	case EROFS_MOUNT_DAX_NEVER:
4353924179bSBaokun Li 		set_opt(&sbi->opt, DAX_NEVER);
4363924179bSBaokun Li 		clear_opt(&sbi->opt, DAX_ALWAYS);
43706252e9cSGao Xiang 		return true;
43806252e9cSGao Xiang 	default:
43906252e9cSGao Xiang 		DBG_BUGON(1);
44006252e9cSGao Xiang 		return false;
44106252e9cSGao Xiang 	}
44206252e9cSGao Xiang #else
44306252e9cSGao Xiang 	errorfc(fc, "dax options not supported");
44406252e9cSGao Xiang 	return false;
44506252e9cSGao Xiang #endif
44606252e9cSGao Xiang }
44706252e9cSGao Xiang 
erofs_fc_parse_param(struct fs_context * fc,struct fs_parameter * param)448f57a3fe4SChao Yu static int erofs_fc_parse_param(struct fs_context *fc,
449f57a3fe4SChao Yu 				struct fs_parameter *param)
45047e4937aSGao Xiang {
4513924179bSBaokun Li 	struct erofs_sb_info *sbi = fc->s_fs_info;
452f57a3fe4SChao Yu 	struct fs_parse_result result;
453dfeab2e9SGao Xiang 	struct erofs_device_info *dif;
454dfeab2e9SGao Xiang 	int opt, ret;
45547e4937aSGao Xiang 
456f57a3fe4SChao Yu 	opt = fs_parse(fc, erofs_fs_parameters, param, &result);
457f57a3fe4SChao Yu 	if (opt < 0)
458f57a3fe4SChao Yu 		return opt;
45947e4937aSGao Xiang 
460f57a3fe4SChao Yu 	switch (opt) {
461f57a3fe4SChao Yu 	case Opt_user_xattr:
46247e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_XATTR
463f57a3fe4SChao Yu 		if (result.boolean)
4643924179bSBaokun Li 			set_opt(&sbi->opt, XATTR_USER);
465f57a3fe4SChao Yu 		else
4663924179bSBaokun Li 			clear_opt(&sbi->opt, XATTR_USER);
46747e4937aSGao Xiang #else
468f57a3fe4SChao Yu 		errorfc(fc, "{,no}user_xattr options not supported");
46947e4937aSGao Xiang #endif
470f57a3fe4SChao Yu 		break;
471f57a3fe4SChao Yu 	case Opt_acl:
47247e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_POSIX_ACL
473f57a3fe4SChao Yu 		if (result.boolean)
4743924179bSBaokun Li 			set_opt(&sbi->opt, POSIX_ACL);
475f57a3fe4SChao Yu 		else
4763924179bSBaokun Li 			clear_opt(&sbi->opt, POSIX_ACL);
47747e4937aSGao Xiang #else
478f57a3fe4SChao Yu 		errorfc(fc, "{,no}acl options not supported");
47947e4937aSGao Xiang #endif
480f57a3fe4SChao Yu 		break;
48147e4937aSGao Xiang 	case Opt_cache_strategy:
482f57a3fe4SChao Yu #ifdef CONFIG_EROFS_FS_ZIP
4833924179bSBaokun Li 		sbi->opt.cache_strategy = result.uint_32;
484f57a3fe4SChao Yu #else
485f57a3fe4SChao Yu 		errorfc(fc, "compression not supported, cache_strategy ignored");
486f57a3fe4SChao Yu #endif
48747e4937aSGao Xiang 		break;
48806252e9cSGao Xiang 	case Opt_dax:
48906252e9cSGao Xiang 		if (!erofs_fc_set_dax_mode(fc, EROFS_MOUNT_DAX_ALWAYS))
49006252e9cSGao Xiang 			return -EINVAL;
49106252e9cSGao Xiang 		break;
49206252e9cSGao Xiang 	case Opt_dax_enum:
49306252e9cSGao Xiang 		if (!erofs_fc_set_dax_mode(fc, result.uint_32))
49406252e9cSGao Xiang 			return -EINVAL;
49506252e9cSGao Xiang 		break;
496dfeab2e9SGao Xiang 	case Opt_device:
497dfeab2e9SGao Xiang 		dif = kzalloc(sizeof(*dif), GFP_KERNEL);
498dfeab2e9SGao Xiang 		if (!dif)
499dfeab2e9SGao Xiang 			return -ENOMEM;
500dfeab2e9SGao Xiang 		dif->path = kstrdup(param->string, GFP_KERNEL);
501dfeab2e9SGao Xiang 		if (!dif->path) {
502dfeab2e9SGao Xiang 			kfree(dif);
503dfeab2e9SGao Xiang 			return -ENOMEM;
504dfeab2e9SGao Xiang 		}
5053924179bSBaokun Li 		down_write(&sbi->devs->rwsem);
5063924179bSBaokun Li 		ret = idr_alloc(&sbi->devs->tree, dif, 0, 0, GFP_KERNEL);
5073924179bSBaokun Li 		up_write(&sbi->devs->rwsem);
508dfeab2e9SGao Xiang 		if (ret < 0) {
509dfeab2e9SGao Xiang 			kfree(dif->path);
510dfeab2e9SGao Xiang 			kfree(dif);
511dfeab2e9SGao Xiang 			return ret;
512dfeab2e9SGao Xiang 		}
5133924179bSBaokun Li 		++sbi->devs->extra_devices;
514dfeab2e9SGao Xiang 		break;
5159c0cc9c7SJeffle Xu #ifdef CONFIG_EROFS_FS_ONDEMAND
516e02ac3e7SJingbo Xu 	case Opt_fsid:
5173924179bSBaokun Li 		kfree(sbi->fsid);
5183924179bSBaokun Li 		sbi->fsid = kstrdup(param->string, GFP_KERNEL);
5193924179bSBaokun Li 		if (!sbi->fsid)
5209c0cc9c7SJeffle Xu 			return -ENOMEM;
5219c0cc9c7SJeffle Xu 		break;
5222ef16441SJia Zhu 	case Opt_domain_id:
5233924179bSBaokun Li 		kfree(sbi->domain_id);
5243924179bSBaokun Li 		sbi->domain_id = kstrdup(param->string, GFP_KERNEL);
5253924179bSBaokun Li 		if (!sbi->domain_id)
5262ef16441SJia Zhu 			return -ENOMEM;
5272ef16441SJia Zhu 		break;
528e02ac3e7SJingbo Xu #else
529e02ac3e7SJingbo Xu 	case Opt_fsid:
530e02ac3e7SJingbo Xu 	case Opt_domain_id:
531e02ac3e7SJingbo Xu 		errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name);
532e02ac3e7SJingbo Xu 		break;
533e02ac3e7SJingbo Xu #endif
53447e4937aSGao Xiang 	default:
535f57a3fe4SChao Yu 		return -ENOPARAM;
53647e4937aSGao Xiang 	}
53747e4937aSGao Xiang 	return 0;
53847e4937aSGao Xiang }
53947e4937aSGao Xiang 
erofs_nfs_get_inode(struct super_block * sb,u64 ino,u32 generation)5403e917cc3SHongnan Li static struct inode *erofs_nfs_get_inode(struct super_block *sb,
5413e917cc3SHongnan Li 					 u64 ino, u32 generation)
5423e917cc3SHongnan Li {
543312fe643SGao Xiang 	return erofs_iget(sb, ino);
5443e917cc3SHongnan Li }
5453e917cc3SHongnan Li 
erofs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)5463e917cc3SHongnan Li static struct dentry *erofs_fh_to_dentry(struct super_block *sb,
5473e917cc3SHongnan Li 		struct fid *fid, int fh_len, int fh_type)
5483e917cc3SHongnan Li {
5493e917cc3SHongnan Li 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
5503e917cc3SHongnan Li 				    erofs_nfs_get_inode);
5513e917cc3SHongnan Li }
5523e917cc3SHongnan Li 
erofs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)5533e917cc3SHongnan Li static struct dentry *erofs_fh_to_parent(struct super_block *sb,
5543e917cc3SHongnan Li 		struct fid *fid, int fh_len, int fh_type)
5553e917cc3SHongnan Li {
5563e917cc3SHongnan Li 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
5573e917cc3SHongnan Li 				    erofs_nfs_get_inode);
5583e917cc3SHongnan Li }
5593e917cc3SHongnan Li 
erofs_get_parent(struct dentry * child)5603e917cc3SHongnan Li static struct dentry *erofs_get_parent(struct dentry *child)
5613e917cc3SHongnan Li {
5623e917cc3SHongnan Li 	erofs_nid_t nid;
5633e917cc3SHongnan Li 	unsigned int d_type;
5643e917cc3SHongnan Li 	int err;
5653e917cc3SHongnan Li 
5663e917cc3SHongnan Li 	err = erofs_namei(d_inode(child), &dotdot_name, &nid, &d_type);
5673e917cc3SHongnan Li 	if (err)
5683e917cc3SHongnan Li 		return ERR_PTR(err);
569312fe643SGao Xiang 	return d_obtain_alias(erofs_iget(child->d_sb, nid));
5703e917cc3SHongnan Li }
5713e917cc3SHongnan Li 
5723e917cc3SHongnan Li static const struct export_operations erofs_export_ops = {
5733e917cc3SHongnan Li 	.fh_to_dentry = erofs_fh_to_dentry,
5743e917cc3SHongnan Li 	.fh_to_parent = erofs_fh_to_parent,
5753e917cc3SHongnan Li 	.get_parent = erofs_get_parent,
5763e917cc3SHongnan Li };
5773e917cc3SHongnan Li 
erofs_fc_fill_super(struct super_block * sb,struct fs_context * fc)578f57a3fe4SChao Yu static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
57947e4937aSGao Xiang {
58047e4937aSGao Xiang 	struct inode *inode;
5813924179bSBaokun Li 	struct erofs_sb_info *sbi = EROFS_SB(sb);
58247e4937aSGao Xiang 	int err;
58347e4937aSGao Xiang 
58447e4937aSGao Xiang 	sb->s_magic = EROFS_SUPER_MAGIC;
58537c90c5fSJeffle Xu 	sb->s_flags |= SB_RDONLY | SB_NOATIME;
58637c90c5fSJeffle Xu 	sb->s_maxbytes = MAX_LFS_FILESIZE;
58737c90c5fSJeffle Xu 	sb->s_op = &erofs_sops;
58847e4937aSGao Xiang 
5893acea5fcSJingbo Xu 	sbi->blkszbits = PAGE_SHIFT;
59093b856bbSJeffle Xu 	if (erofs_is_fscache_mode(sb)) {
591d3c4bdccSJingbo Xu 		sb->s_blocksize = PAGE_SIZE;
592d3c4bdccSJingbo Xu 		sb->s_blocksize_bits = PAGE_SHIFT;
593c6be2bd0SJeffle Xu 
594c6be2bd0SJeffle Xu 		err = erofs_fscache_register_fs(sb);
595c6be2bd0SJeffle Xu 		if (err)
596c6be2bd0SJeffle Xu 			return err;
59737c90c5fSJeffle Xu 
598c665b394SJeffle Xu 		err = super_setup_bdi(sb);
599c665b394SJeffle Xu 		if (err)
600c665b394SJeffle Xu 			return err;
60193b856bbSJeffle Xu 	} else {
602d3c4bdccSJingbo Xu 		if (!sb_set_blocksize(sb, PAGE_SIZE)) {
603d3c4bdccSJingbo Xu 			errorfc(fc, "failed to set initial blksize");
60493b856bbSJeffle Xu 			return -EINVAL;
60593b856bbSJeffle Xu 		}
60693b856bbSJeffle Xu 
60793b856bbSJeffle Xu 		sbi->dax_dev = fs_dax_get_by_bdev(sb->s_bdev,
6088012b866SShiyang Ruan 						  &sbi->dax_part_off,
6098012b866SShiyang Ruan 						  NULL, NULL);
61093b856bbSJeffle Xu 	}
61193b856bbSJeffle Xu 
61299634bf3SGao Xiang 	err = erofs_read_superblock(sb);
61347e4937aSGao Xiang 	if (err)
61447e4937aSGao Xiang 		return err;
61547e4937aSGao Xiang 
616d3c4bdccSJingbo Xu 	if (sb->s_blocksize_bits != sbi->blkszbits) {
617d3c4bdccSJingbo Xu 		if (erofs_is_fscache_mode(sb)) {
618d3c4bdccSJingbo Xu 			errorfc(fc, "unsupported blksize for fscache mode");
619d3c4bdccSJingbo Xu 			return -EINVAL;
620d3c4bdccSJingbo Xu 		}
621d3c4bdccSJingbo Xu 		if (!sb_set_blocksize(sb, 1 << sbi->blkszbits)) {
622d3c4bdccSJingbo Xu 			errorfc(fc, "failed to set erofs blksize");
623d3c4bdccSJingbo Xu 			return -EINVAL;
624d3c4bdccSJingbo Xu 		}
625d3c4bdccSJingbo Xu 	}
6267b0800d0SChristoph Hellwig 
627d3c4bdccSJingbo Xu 	if (test_opt(&sbi->opt, DAX_ALWAYS)) {
6287b0800d0SChristoph Hellwig 		if (!sbi->dax_dev) {
62906252e9cSGao Xiang 			errorfc(fc, "DAX unsupported by block device. Turning off DAX.");
630e6242465SGao Xiang 			clear_opt(&sbi->opt, DAX_ALWAYS);
631d3c4bdccSJingbo Xu 		} else if (sbi->blkszbits != PAGE_SHIFT) {
632d3c4bdccSJingbo Xu 			errorfc(fc, "unsupported blocksize for DAX");
633d3c4bdccSJingbo Xu 			clear_opt(&sbi->opt, DAX_ALWAYS);
63406252e9cSGao Xiang 		}
6357b0800d0SChristoph Hellwig 	}
63647e4937aSGao Xiang 
63737c90c5fSJeffle Xu 	sb->s_time_gran = 1;
63847e4937aSGao Xiang 	sb->s_xattr = erofs_xattr_handlers;
6393e917cc3SHongnan Li 	sb->s_export_op = &erofs_export_ops;
640e7cda1eeSChengguang Xu 
641e6242465SGao Xiang 	if (test_opt(&sbi->opt, POSIX_ACL))
64247e4937aSGao Xiang 		sb->s_flags |= SB_POSIXACL;
64347e4937aSGao Xiang 	else
64447e4937aSGao Xiang 		sb->s_flags &= ~SB_POSIXACL;
64547e4937aSGao Xiang 
64647e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP
64764094a04SGao Xiang 	xa_init(&sbi->managed_pslots);
64847e4937aSGao Xiang #endif
64947e4937aSGao Xiang 
650312fe643SGao Xiang 	inode = erofs_iget(sb, ROOT_NID(sbi));
65147e4937aSGao Xiang 	if (IS_ERR(inode))
65247e4937aSGao Xiang 		return PTR_ERR(inode);
65347e4937aSGao Xiang 
6548d8a09b0SGao Xiang 	if (!S_ISDIR(inode->i_mode)) {
6554f761fa2SGao Xiang 		erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
65647e4937aSGao Xiang 			  ROOT_NID(sbi), inode->i_mode);
65747e4937aSGao Xiang 		iput(inode);
65847e4937aSGao Xiang 		return -EINVAL;
65947e4937aSGao Xiang 	}
66047e4937aSGao Xiang 
66147e4937aSGao Xiang 	sb->s_root = d_make_root(inode);
6628d8a09b0SGao Xiang 	if (!sb->s_root)
66347e4937aSGao Xiang 		return -ENOMEM;
66447e4937aSGao Xiang 
66547e4937aSGao Xiang 	erofs_shrinker_register(sb);
666cb9bce79SJingbo Xu 	if (erofs_sb_has_fragments(sbi) && sbi->packed_nid) {
667cb9bce79SJingbo Xu 		sbi->packed_inode = erofs_iget(sb, sbi->packed_nid);
668cb9bce79SJingbo Xu 		if (IS_ERR(sbi->packed_inode)) {
669cb9bce79SJingbo Xu 			err = PTR_ERR(sbi->packed_inode);
670cb9bce79SJingbo Xu 			sbi->packed_inode = NULL;
671cb9bce79SJingbo Xu 			return err;
672cb9bce79SJingbo Xu 		}
673cb9bce79SJingbo Xu 	}
67447e4937aSGao Xiang 	err = erofs_init_managed_cache(sb);
6758d8a09b0SGao Xiang 	if (err)
67647e4937aSGao Xiang 		return err;
67747e4937aSGao Xiang 
6786a318ccdSJingbo Xu 	err = erofs_xattr_prefixes_init(sb);
6796a318ccdSJingbo Xu 	if (err)
6806a318ccdSJingbo Xu 		return err;
6816a318ccdSJingbo Xu 
682168e9a76SHuang Jianan 	err = erofs_register_sysfs(sb);
683168e9a76SHuang Jianan 	if (err)
684168e9a76SHuang Jianan 		return err;
685168e9a76SHuang Jianan 
686f57a3fe4SChao Yu 	erofs_info(sb, "mounted with root inode @ nid %llu.", ROOT_NID(sbi));
68747e4937aSGao Xiang 	return 0;
68847e4937aSGao Xiang }
68947e4937aSGao Xiang 
erofs_fc_get_tree(struct fs_context * fc)690f57a3fe4SChao Yu static int erofs_fc_get_tree(struct fs_context *fc)
69147e4937aSGao Xiang {
6923924179bSBaokun Li 	struct erofs_sb_info *sbi = fc->s_fs_info;
6939c0cc9c7SJeffle Xu 
6943924179bSBaokun Li 	if (IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid)
6959c0cc9c7SJeffle Xu 		return get_tree_nodev(fc, erofs_fc_fill_super);
6969c0cc9c7SJeffle Xu 
697f57a3fe4SChao Yu 	return get_tree_bdev(fc, erofs_fc_fill_super);
698f57a3fe4SChao Yu }
699f57a3fe4SChao Yu 
erofs_fc_reconfigure(struct fs_context * fc)700f57a3fe4SChao Yu static int erofs_fc_reconfigure(struct fs_context *fc)
701f57a3fe4SChao Yu {
702f57a3fe4SChao Yu 	struct super_block *sb = fc->root->d_sb;
703f57a3fe4SChao Yu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
7043924179bSBaokun Li 	struct erofs_sb_info *new_sbi = fc->s_fs_info;
705f57a3fe4SChao Yu 
706f57a3fe4SChao Yu 	DBG_BUGON(!sb_rdonly(sb));
707f57a3fe4SChao Yu 
7083924179bSBaokun Li 	if (new_sbi->fsid || new_sbi->domain_id)
70939bfcb81SJingbo Xu 		erofs_info(sb, "ignoring reconfiguration for fsid|domain_id.");
71039bfcb81SJingbo Xu 
7113924179bSBaokun Li 	if (test_opt(&new_sbi->opt, POSIX_ACL))
712f57a3fe4SChao Yu 		fc->sb_flags |= SB_POSIXACL;
713f57a3fe4SChao Yu 	else
714f57a3fe4SChao Yu 		fc->sb_flags &= ~SB_POSIXACL;
715f57a3fe4SChao Yu 
7163924179bSBaokun Li 	sbi->opt = new_sbi->opt;
717f57a3fe4SChao Yu 
718f57a3fe4SChao Yu 	fc->sb_flags |= SB_RDONLY;
719f57a3fe4SChao Yu 	return 0;
720f57a3fe4SChao Yu }
721f57a3fe4SChao Yu 
erofs_release_device_info(int id,void * ptr,void * data)722dfeab2e9SGao Xiang static int erofs_release_device_info(int id, void *ptr, void *data)
723dfeab2e9SGao Xiang {
724dfeab2e9SGao Xiang 	struct erofs_device_info *dif = ptr;
725dfeab2e9SGao Xiang 
7268012b866SShiyang Ruan 	fs_put_dax(dif->dax_dev, NULL);
727613139a0SJan Kara 	if (dif->bdev_handle)
728613139a0SJan Kara 		bdev_release(dif->bdev_handle);
729e1de2da0SJia Zhu 	erofs_fscache_unregister_cookie(dif->fscache);
730e1de2da0SJia Zhu 	dif->fscache = NULL;
731dfeab2e9SGao Xiang 	kfree(dif->path);
732dfeab2e9SGao Xiang 	kfree(dif);
733dfeab2e9SGao Xiang 	return 0;
734dfeab2e9SGao Xiang }
735dfeab2e9SGao Xiang 
erofs_free_dev_context(struct erofs_dev_context * devs)736dfeab2e9SGao Xiang static void erofs_free_dev_context(struct erofs_dev_context *devs)
737dfeab2e9SGao Xiang {
738dfeab2e9SGao Xiang 	if (!devs)
739dfeab2e9SGao Xiang 		return;
740dfeab2e9SGao Xiang 	idr_for_each(&devs->tree, &erofs_release_device_info, NULL);
741dfeab2e9SGao Xiang 	idr_destroy(&devs->tree);
742dfeab2e9SGao Xiang 	kfree(devs);
743dfeab2e9SGao Xiang }
744dfeab2e9SGao Xiang 
erofs_fc_free(struct fs_context * fc)745f57a3fe4SChao Yu static void erofs_fc_free(struct fs_context *fc)
746f57a3fe4SChao Yu {
7473924179bSBaokun Li 	struct erofs_sb_info *sbi = fc->s_fs_info;
748dfeab2e9SGao Xiang 
7493924179bSBaokun Li 	if (!sbi)
7503924179bSBaokun Li 		return;
7513924179bSBaokun Li 
7523924179bSBaokun Li 	erofs_free_dev_context(sbi->devs);
7533924179bSBaokun Li 	kfree(sbi->fsid);
7543924179bSBaokun Li 	kfree(sbi->domain_id);
7553924179bSBaokun Li 	kfree(sbi);
756f57a3fe4SChao Yu }
757f57a3fe4SChao Yu 
758f57a3fe4SChao Yu static const struct fs_context_operations erofs_context_ops = {
759f57a3fe4SChao Yu 	.parse_param	= erofs_fc_parse_param,
760f57a3fe4SChao Yu 	.get_tree       = erofs_fc_get_tree,
761f57a3fe4SChao Yu 	.reconfigure    = erofs_fc_reconfigure,
762f57a3fe4SChao Yu 	.free		= erofs_fc_free,
763f57a3fe4SChao Yu };
764f57a3fe4SChao Yu 
erofs_init_fs_context(struct fs_context * fc)765f57a3fe4SChao Yu static int erofs_init_fs_context(struct fs_context *fc)
766f57a3fe4SChao Yu {
7673924179bSBaokun Li 	struct erofs_sb_info *sbi;
768dfeab2e9SGao Xiang 
7693924179bSBaokun Li 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
7703924179bSBaokun Li 	if (!sbi)
771f57a3fe4SChao Yu 		return -ENOMEM;
7723924179bSBaokun Li 
7733924179bSBaokun Li 	sbi->devs = kzalloc(sizeof(struct erofs_dev_context), GFP_KERNEL);
7743924179bSBaokun Li 	if (!sbi->devs) {
7753924179bSBaokun Li 		kfree(sbi);
776dfeab2e9SGao Xiang 		return -ENOMEM;
777dfeab2e9SGao Xiang 	}
7783924179bSBaokun Li 	fc->s_fs_info = sbi;
779f57a3fe4SChao Yu 
7803924179bSBaokun Li 	idr_init(&sbi->devs->tree);
7813924179bSBaokun Li 	init_rwsem(&sbi->devs->rwsem);
7823924179bSBaokun Li 	erofs_default_options(sbi);
783f57a3fe4SChao Yu 	fc->ops = &erofs_context_ops;
784f57a3fe4SChao Yu 	return 0;
78547e4937aSGao Xiang }
78647e4937aSGao Xiang 
erofs_kill_sb(struct super_block * sb)78747e4937aSGao Xiang static void erofs_kill_sb(struct super_block *sb)
78847e4937aSGao Xiang {
789*f9b877a7SChristian Brauner 	struct erofs_sb_info *sbi = EROFS_SB(sb);
79047e4937aSGao Xiang 
791*f9b877a7SChristian Brauner 	if (IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid)
7921015c101SJia Zhu 		kill_anon_super(sb);
7939c0cc9c7SJeffle Xu 	else
79447e4937aSGao Xiang 		kill_block_super(sb);
79547e4937aSGao Xiang 
796dfeab2e9SGao Xiang 	erofs_free_dev_context(sbi->devs);
7978012b866SShiyang Ruan 	fs_put_dax(sbi->dax_dev, NULL);
798c6be2bd0SJeffle Xu 	erofs_fscache_unregister_fs(sb);
79939bfcb81SJingbo Xu 	kfree(sbi->fsid);
80039bfcb81SJingbo Xu 	kfree(sbi->domain_id);
80147e4937aSGao Xiang 	kfree(sbi);
80247e4937aSGao Xiang 	sb->s_fs_info = NULL;
80347e4937aSGao Xiang }
80447e4937aSGao Xiang 
erofs_put_super(struct super_block * sb)80547e4937aSGao Xiang static void erofs_put_super(struct super_block *sb)
80647e4937aSGao Xiang {
80747e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
80847e4937aSGao Xiang 
80947e4937aSGao Xiang 	DBG_BUGON(!sbi);
81047e4937aSGao Xiang 
811168e9a76SHuang Jianan 	erofs_unregister_sysfs(sb);
81247e4937aSGao Xiang 	erofs_shrinker_unregister(sb);
8136a318ccdSJingbo Xu 	erofs_xattr_prefixes_cleanup(sb);
81447e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP
81547e4937aSGao Xiang 	iput(sbi->managed_cache);
81647e4937aSGao Xiang 	sbi->managed_cache = NULL;
817a97a218bSJingbo Xu #endif
818b15b2e30SYue Hu 	iput(sbi->packed_inode);
819b15b2e30SYue Hu 	sbi->packed_inode = NULL;
82061fef989SJingbo Xu 	erofs_free_dev_context(sbi->devs);
82161fef989SJingbo Xu 	sbi->devs = NULL;
822e1de2da0SJia Zhu 	erofs_fscache_unregister_fs(sb);
82347e4937aSGao Xiang }
82447e4937aSGao Xiang 
825145cd3e3SBaokun Li static struct file_system_type erofs_fs_type = {
82647e4937aSGao Xiang 	.owner          = THIS_MODULE,
82747e4937aSGao Xiang 	.name           = "erofs",
828f57a3fe4SChao Yu 	.init_fs_context = erofs_init_fs_context,
82947e4937aSGao Xiang 	.kill_sb        = erofs_kill_sb,
8306c459b78SChao Yu 	.fs_flags       = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
83147e4937aSGao Xiang };
83247e4937aSGao Xiang MODULE_ALIAS_FS("erofs");
83347e4937aSGao Xiang 
erofs_module_init(void)83447e4937aSGao Xiang static int __init erofs_module_init(void)
83547e4937aSGao Xiang {
83647e4937aSGao Xiang 	int err;
83747e4937aSGao Xiang 
83847e4937aSGao Xiang 	erofs_check_ondisk_layout_definitions();
83947e4937aSGao Xiang 
8401c2dfbf9SGao Xiang 	erofs_inode_cachep = kmem_cache_create("erofs_inode",
841a5876e24SGao Xiang 			sizeof(struct erofs_inode), 0,
8428ec9a252SFerry Meng 			SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
84399634bf3SGao Xiang 			erofs_inode_init_once);
84419905955SYangtao Li 	if (!erofs_inode_cachep)
84519905955SYangtao Li 		return -ENOMEM;
84647e4937aSGao Xiang 
84747e4937aSGao Xiang 	err = erofs_init_shrinker();
84847e4937aSGao Xiang 	if (err)
84947e4937aSGao Xiang 		goto shrinker_err;
85047e4937aSGao Xiang 
851622ceaddSGao Xiang 	err = z_erofs_lzma_init();
852622ceaddSGao Xiang 	if (err)
853622ceaddSGao Xiang 		goto lzma_err;
854622ceaddSGao Xiang 
855ffa09b3bSGao Xiang 	err = z_erofs_deflate_init();
856ffa09b3bSGao Xiang 	if (err)
857ffa09b3bSGao Xiang 		goto deflate_err;
858ffa09b3bSGao Xiang 
85952488734SGao Xiang 	erofs_pcpubuf_init();
86047e4937aSGao Xiang 	err = z_erofs_init_zip_subsystem();
86147e4937aSGao Xiang 	if (err)
86247e4937aSGao Xiang 		goto zip_err;
86347e4937aSGao Xiang 
864168e9a76SHuang Jianan 	err = erofs_init_sysfs();
865168e9a76SHuang Jianan 	if (err)
866168e9a76SHuang Jianan 		goto sysfs_err;
867168e9a76SHuang Jianan 
86847e4937aSGao Xiang 	err = register_filesystem(&erofs_fs_type);
86947e4937aSGao Xiang 	if (err)
87047e4937aSGao Xiang 		goto fs_err;
87147e4937aSGao Xiang 
87247e4937aSGao Xiang 	return 0;
87347e4937aSGao Xiang 
87447e4937aSGao Xiang fs_err:
875168e9a76SHuang Jianan 	erofs_exit_sysfs();
876168e9a76SHuang Jianan sysfs_err:
87747e4937aSGao Xiang 	z_erofs_exit_zip_subsystem();
87847e4937aSGao Xiang zip_err:
879ffa09b3bSGao Xiang 	z_erofs_deflate_exit();
880ffa09b3bSGao Xiang deflate_err:
881622ceaddSGao Xiang 	z_erofs_lzma_exit();
882622ceaddSGao Xiang lzma_err:
88347e4937aSGao Xiang 	erofs_exit_shrinker();
88447e4937aSGao Xiang shrinker_err:
8851c2dfbf9SGao Xiang 	kmem_cache_destroy(erofs_inode_cachep);
88647e4937aSGao Xiang 	return err;
88747e4937aSGao Xiang }
88847e4937aSGao Xiang 
erofs_module_exit(void)88947e4937aSGao Xiang static void __exit erofs_module_exit(void)
89047e4937aSGao Xiang {
89147e4937aSGao Xiang 	unregister_filesystem(&erofs_fs_type);
8921c2dfbf9SGao Xiang 
893622ceaddSGao Xiang 	/* Ensure all RCU free inodes / pclusters are safe to be destroyed. */
8941c2dfbf9SGao Xiang 	rcu_barrier();
895622ceaddSGao Xiang 
896168e9a76SHuang Jianan 	erofs_exit_sysfs();
897622ceaddSGao Xiang 	z_erofs_exit_zip_subsystem();
898ffa09b3bSGao Xiang 	z_erofs_deflate_exit();
899622ceaddSGao Xiang 	z_erofs_lzma_exit();
900622ceaddSGao Xiang 	erofs_exit_shrinker();
9011c2dfbf9SGao Xiang 	kmem_cache_destroy(erofs_inode_cachep);
90252488734SGao Xiang 	erofs_pcpubuf_exit();
90347e4937aSGao Xiang }
90447e4937aSGao Xiang 
erofs_statfs(struct dentry * dentry,struct kstatfs * buf)90547e4937aSGao Xiang static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
90647e4937aSGao Xiang {
90747e4937aSGao Xiang 	struct super_block *sb = dentry->d_sb;
90847e4937aSGao Xiang 	struct erofs_sb_info *sbi = EROFS_SB(sb);
90993b856bbSJeffle Xu 	u64 id = 0;
91093b856bbSJeffle Xu 
91193b856bbSJeffle Xu 	if (!erofs_is_fscache_mode(sb))
91293b856bbSJeffle Xu 		id = huge_encode_dev(sb->s_bdev->bd_dev);
91347e4937aSGao Xiang 
91447e4937aSGao Xiang 	buf->f_type = sb->s_magic;
9153acea5fcSJingbo Xu 	buf->f_bsize = sb->s_blocksize;
916dfeab2e9SGao Xiang 	buf->f_blocks = sbi->total_blocks;
91747e4937aSGao Xiang 	buf->f_bfree = buf->f_bavail = 0;
91847e4937aSGao Xiang 
91947e4937aSGao Xiang 	buf->f_files = ULLONG_MAX;
92047e4937aSGao Xiang 	buf->f_ffree = ULLONG_MAX - sbi->inos;
92147e4937aSGao Xiang 
92247e4937aSGao Xiang 	buf->f_namelen = EROFS_NAME_LEN;
92347e4937aSGao Xiang 
9246d1349c7SAl Viro 	buf->f_fsid    = u64_to_fsid(id);
92547e4937aSGao Xiang 	return 0;
92647e4937aSGao Xiang }
92747e4937aSGao Xiang 
erofs_show_options(struct seq_file * seq,struct dentry * root)92847e4937aSGao Xiang static int erofs_show_options(struct seq_file *seq, struct dentry *root)
92947e4937aSGao Xiang {
93006252e9cSGao Xiang 	struct erofs_sb_info *sbi = EROFS_SB(root->d_sb);
931e6242465SGao Xiang 	struct erofs_mount_opts *opt = &sbi->opt;
93247e4937aSGao Xiang 
93347e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_XATTR
934e6242465SGao Xiang 	if (test_opt(opt, XATTR_USER))
93547e4937aSGao Xiang 		seq_puts(seq, ",user_xattr");
93647e4937aSGao Xiang 	else
93747e4937aSGao Xiang 		seq_puts(seq, ",nouser_xattr");
93847e4937aSGao Xiang #endif
93947e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_POSIX_ACL
940e6242465SGao Xiang 	if (test_opt(opt, POSIX_ACL))
94147e4937aSGao Xiang 		seq_puts(seq, ",acl");
94247e4937aSGao Xiang 	else
94347e4937aSGao Xiang 		seq_puts(seq, ",noacl");
94447e4937aSGao Xiang #endif
94547e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP
946e6242465SGao Xiang 	if (opt->cache_strategy == EROFS_ZIP_CACHE_DISABLED)
94747e4937aSGao Xiang 		seq_puts(seq, ",cache_strategy=disabled");
948e6242465SGao Xiang 	else if (opt->cache_strategy == EROFS_ZIP_CACHE_READAHEAD)
94947e4937aSGao Xiang 		seq_puts(seq, ",cache_strategy=readahead");
950e6242465SGao Xiang 	else if (opt->cache_strategy == EROFS_ZIP_CACHE_READAROUND)
95147e4937aSGao Xiang 		seq_puts(seq, ",cache_strategy=readaround");
95247e4937aSGao Xiang #endif
953e6242465SGao Xiang 	if (test_opt(opt, DAX_ALWAYS))
95406252e9cSGao Xiang 		seq_puts(seq, ",dax=always");
955e6242465SGao Xiang 	if (test_opt(opt, DAX_NEVER))
95606252e9cSGao Xiang 		seq_puts(seq, ",dax=never");
9579c0cc9c7SJeffle Xu #ifdef CONFIG_EROFS_FS_ONDEMAND
95839bfcb81SJingbo Xu 	if (sbi->fsid)
95939bfcb81SJingbo Xu 		seq_printf(seq, ",fsid=%s", sbi->fsid);
96039bfcb81SJingbo Xu 	if (sbi->domain_id)
96139bfcb81SJingbo Xu 		seq_printf(seq, ",domain_id=%s", sbi->domain_id);
9629c0cc9c7SJeffle Xu #endif
96347e4937aSGao Xiang 	return 0;
96447e4937aSGao Xiang }
96547e4937aSGao Xiang 
96647e4937aSGao Xiang const struct super_operations erofs_sops = {
96747e4937aSGao Xiang 	.put_super = erofs_put_super,
96899634bf3SGao Xiang 	.alloc_inode = erofs_alloc_inode,
96999634bf3SGao Xiang 	.free_inode = erofs_free_inode,
97047e4937aSGao Xiang 	.statfs = erofs_statfs,
97147e4937aSGao Xiang 	.show_options = erofs_show_options,
97247e4937aSGao Xiang };
97347e4937aSGao Xiang 
97447e4937aSGao Xiang module_init(erofs_module_init);
97547e4937aSGao Xiang module_exit(erofs_module_exit);
97647e4937aSGao Xiang 
97747e4937aSGao Xiang MODULE_DESCRIPTION("Enhanced ROM File System");
97847e4937aSGao Xiang MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
97947e4937aSGao Xiang MODULE_LICENSE("GPL");
980