blob: 915eefe0d7e2c51f65851933ff0b2067da70e88c [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001// SPDX-License-Identifier: GPL-2.0-only
Gao Xiangba2b77a2018-07-26 20:21:46 +08002/*
Gao Xiangba2b77a2018-07-26 20:21:46 +08003 * Copyright (C) 2017-2018 HUAWEI, Inc.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02004 * https://www.huawei.com/
Gao Xiang2b5379f2022-01-02 16:13:17 +08005 * Copyright (C) 2021, Alibaba Cloud
Gao Xiangba2b77a2018-07-26 20:21:46 +08006 */
7#include <linux/module.h>
8#include <linux/buffer_head.h>
9#include <linux/statfs.h>
10#include <linux/parser.h>
Gao Xiangb17500a2018-07-26 20:21:52 +080011#include <linux/seq_file.h>
Pratik Shindeb858a482019-11-04 10:49:37 +080012#include <linux/crc32c.h>
Chao Yuf57a3fe2020-05-29 18:48:36 +080013#include <linux/fs_context.h>
14#include <linux/fs_parser.h>
Gao Xiang06252e92021-08-05 08:36:00 +080015#include <linux/dax.h>
Gao Xiang6af7b482019-01-14 19:40:25 +080016#include "xattr.h"
Gao Xiangba2b77a2018-07-26 20:21:46 +080017
Chao Yu13f06f42018-07-26 20:21:55 +080018#define CREATE_TRACE_POINTS
19#include <trace/events/erofs.h>
20
Gao Xiangba2b77a2018-07-26 20:21:46 +080021static struct kmem_cache *erofs_inode_cachep __read_mostly;
22
Gao Xiang4f761fa2019-09-04 10:09:09 +080023void _erofs_err(struct super_block *sb, const char *function,
24 const char *fmt, ...)
25{
26 struct va_format vaf;
27 va_list args;
28
29 va_start(args, fmt);
30
31 vaf.fmt = fmt;
32 vaf.va = &args;
33
34 pr_err("(device %s): %s: %pV", sb->s_id, function, &vaf);
35 va_end(args);
36}
37
38void _erofs_info(struct super_block *sb, const char *function,
39 const char *fmt, ...)
40{
41 struct va_format vaf;
42 va_list args;
43
44 va_start(args, fmt);
45
46 vaf.fmt = fmt;
47 vaf.va = &args;
48
49 pr_info("(device %s): %pV", sb->s_id, &vaf);
50 va_end(args);
51}
52
Pratik Shindeb858a482019-11-04 10:49:37 +080053static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata)
54{
55 struct erofs_super_block *dsb;
56 u32 expected_crc, crc;
57
58 dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET,
59 EROFS_BLKSIZ - EROFS_SUPER_OFFSET, GFP_KERNEL);
60 if (!dsb)
61 return -ENOMEM;
62
63 expected_crc = le32_to_cpu(dsb->checksum);
64 dsb->checksum = 0;
65 /* to allow for x86 boot sectors and other oddities. */
66 crc = crc32c(~0, dsb, EROFS_BLKSIZ - EROFS_SUPER_OFFSET);
67 kfree(dsb);
68
69 if (crc != expected_crc) {
70 erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected",
71 crc, expected_crc);
72 return -EBADMSG;
73 }
74 return 0;
75}
76
Gao Xiang99634bf2019-09-04 10:09:05 +080077static void erofs_inode_init_once(void *ptr)
Gao Xiangba2b77a2018-07-26 20:21:46 +080078{
Gao Xianga5876e22019-09-04 10:08:56 +080079 struct erofs_inode *vi = ptr;
Gao Xiangba2b77a2018-07-26 20:21:46 +080080
81 inode_init_once(&vi->vfs_inode);
82}
83
Gao Xiang99634bf2019-09-04 10:09:05 +080084static struct inode *erofs_alloc_inode(struct super_block *sb)
Gao Xiangba2b77a2018-07-26 20:21:46 +080085{
Gao Xianga5876e22019-09-04 10:08:56 +080086 struct erofs_inode *vi =
Gao Xiangba2b77a2018-07-26 20:21:46 +080087 kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL);
88
Vatsala Narange2ff9f12019-03-21 05:36:13 +053089 if (!vi)
Gao Xiangba2b77a2018-07-26 20:21:46 +080090 return NULL;
91
92 /* zero out everything except vfs_inode */
Gao Xianga5876e22019-09-04 10:08:56 +080093 memset(vi, 0, offsetof(struct erofs_inode, vfs_inode));
Gao Xiangba2b77a2018-07-26 20:21:46 +080094 return &vi->vfs_inode;
95}
96
Gao Xiang99634bf2019-09-04 10:09:05 +080097static void erofs_free_inode(struct inode *inode)
Gao Xiangba2b77a2018-07-26 20:21:46 +080098{
Gao Xianga5876e22019-09-04 10:08:56 +080099 struct erofs_inode *vi = EROFS_I(inode);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800100
Gao Xianga2c75c82019-09-04 10:08:59 +0800101 /* be careful of RCU symlink path */
102 if (inode->i_op == &erofs_fast_symlink_iops)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800103 kfree(inode->i_link);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800104 kfree(vi->xattr_shared_xattrs);
105
106 kmem_cache_free(erofs_inode_cachep, vi);
107}
108
Gao Xiang5efe5132019-06-13 16:35:41 +0800109static bool check_layout_compatibility(struct super_block *sb,
Gao Xiang0259f202019-09-04 10:09:00 +0800110 struct erofs_super_block *dsb)
Gao Xiang5efe5132019-06-13 16:35:41 +0800111{
Gao Xiang0259f202019-09-04 10:09:00 +0800112 const unsigned int feature = le32_to_cpu(dsb->feature_incompat);
Gao Xiang5efe5132019-06-13 16:35:41 +0800113
Gao Xiang426a9302019-09-04 10:08:53 +0800114 EROFS_SB(sb)->feature_incompat = feature;
Gao Xiang5efe5132019-06-13 16:35:41 +0800115
116 /* check if current kernel meets all mandatory requirements */
Gao Xiang426a9302019-09-04 10:08:53 +0800117 if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800118 erofs_err(sb,
119 "unidentified incompatible feature %x, please upgrade kernel version",
120 feature & ~EROFS_ALL_FEATURE_INCOMPAT);
Gao Xiang5efe5132019-06-13 16:35:41 +0800121 return false;
122 }
123 return true;
124}
125
Gao Xiang14373712021-03-29 18:00:12 +0800126#ifdef CONFIG_EROFS_FS_ZIP
127/* read variable-sized metadata, offset will be aligned by 4-byte */
Gao Xiang2b5379f2022-01-02 16:13:17 +0800128static void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
Gao Xiang14373712021-03-29 18:00:12 +0800129 erofs_off_t *offset, int *lengthp)
130{
Gao Xiang14373712021-03-29 18:00:12 +0800131 u8 *buffer, *ptr;
132 int len, i, cnt;
Gao Xiang14373712021-03-29 18:00:12 +0800133
134 *offset = round_up(*offset, 4);
Gao Xiang2b5379f2022-01-02 16:13:17 +0800135 ptr = erofs_read_metabuf(buf, sb, erofs_blknr(*offset), EROFS_KMAP);
136 if (IS_ERR(ptr))
137 return ptr;
Gao Xiang14373712021-03-29 18:00:12 +0800138
Gao Xiang14373712021-03-29 18:00:12 +0800139 len = le16_to_cpu(*(__le16 *)&ptr[erofs_blkoff(*offset)]);
140 if (!len)
141 len = U16_MAX + 1;
142 buffer = kmalloc(len, GFP_KERNEL);
Gao Xiang2b5379f2022-01-02 16:13:17 +0800143 if (!buffer)
144 return ERR_PTR(-ENOMEM);
Gao Xiang14373712021-03-29 18:00:12 +0800145 *offset += sizeof(__le16);
146 *lengthp = len;
147
148 for (i = 0; i < len; i += cnt) {
149 cnt = min(EROFS_BLKSIZ - (int)erofs_blkoff(*offset), len - i);
Gao Xiang2b5379f2022-01-02 16:13:17 +0800150 ptr = erofs_read_metabuf(buf, sb, erofs_blknr(*offset),
151 EROFS_KMAP);
152 if (IS_ERR(ptr)) {
153 kfree(buffer);
154 return ptr;
Gao Xiang14373712021-03-29 18:00:12 +0800155 }
156 memcpy(buffer + i, ptr + erofs_blkoff(*offset), cnt);
157 *offset += cnt;
158 }
Gao Xiang14373712021-03-29 18:00:12 +0800159 return buffer;
Gao Xiang14373712021-03-29 18:00:12 +0800160}
161
162static int erofs_load_compr_cfgs(struct super_block *sb,
163 struct erofs_super_block *dsb)
164{
Gao Xiang2b5379f2022-01-02 16:13:17 +0800165 struct erofs_sb_info *sbi = EROFS_SB(sb);
166 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
Gao Xiang14373712021-03-29 18:00:12 +0800167 unsigned int algs, alg;
168 erofs_off_t offset;
Gao Xiang2b5379f2022-01-02 16:13:17 +0800169 int size, ret = 0;
Gao Xiang14373712021-03-29 18:00:12 +0800170
Gao Xiang14373712021-03-29 18:00:12 +0800171 sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs);
Gao Xiang14373712021-03-29 18:00:12 +0800172 if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) {
173 erofs_err(sb, "try to load compressed fs with unsupported algorithms %x",
174 sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS);
175 return -EINVAL;
176 }
177
178 offset = EROFS_SUPER_OFFSET + sbi->sb_size;
Gao Xiang14373712021-03-29 18:00:12 +0800179 alg = 0;
Gao Xiang14373712021-03-29 18:00:12 +0800180 for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) {
181 void *data;
182
183 if (!(algs & 1))
184 continue;
185
Gao Xiang2b5379f2022-01-02 16:13:17 +0800186 data = erofs_read_metadata(sb, &buf, &offset, &size);
Gao Xiang14373712021-03-29 18:00:12 +0800187 if (IS_ERR(data)) {
188 ret = PTR_ERR(data);
Gao Xiang2b5379f2022-01-02 16:13:17 +0800189 break;
Gao Xiang14373712021-03-29 18:00:12 +0800190 }
191
192 switch (alg) {
193 case Z_EROFS_COMPRESSION_LZ4:
194 ret = z_erofs_load_lz4_config(sb, dsb, data, size);
195 break;
Gao Xiang622cead2021-10-11 05:31:45 +0800196 case Z_EROFS_COMPRESSION_LZMA:
197 ret = z_erofs_load_lzma_config(sb, dsb, data, size);
198 break;
Gao Xiang14373712021-03-29 18:00:12 +0800199 default:
200 DBG_BUGON(1);
201 ret = -EFAULT;
202 }
203 kfree(data);
204 if (ret)
Gao Xiang2b5379f2022-01-02 16:13:17 +0800205 break;
Gao Xiang14373712021-03-29 18:00:12 +0800206 }
Gao Xiang2b5379f2022-01-02 16:13:17 +0800207 erofs_put_metabuf(&buf);
Gao Xiang14373712021-03-29 18:00:12 +0800208 return ret;
209}
210#else
211static int erofs_load_compr_cfgs(struct super_block *sb,
212 struct erofs_super_block *dsb)
213{
214 if (dsb->u1.available_compr_algs) {
215 erofs_err(sb, "try to load compressed fs when compression is disabled");
216 return -EINVAL;
217 }
218 return 0;
219}
220#endif
221
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800222static int erofs_init_devices(struct super_block *sb,
223 struct erofs_super_block *dsb)
224{
225 struct erofs_sb_info *sbi = EROFS_SB(sb);
226 unsigned int ondisk_extradevs;
227 erofs_off_t pos;
Gao Xiang2b5379f2022-01-02 16:13:17 +0800228 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800229 struct erofs_device_info *dif;
230 struct erofs_deviceslot *dis;
231 void *ptr;
232 int id, err = 0;
233
234 sbi->total_blocks = sbi->primarydevice_blocks;
235 if (!erofs_sb_has_device_table(sbi))
236 ondisk_extradevs = 0;
237 else
238 ondisk_extradevs = le16_to_cpu(dsb->extra_devices);
239
240 if (ondisk_extradevs != sbi->devs->extra_devices) {
241 erofs_err(sb, "extra devices don't match (ondisk %u, given %u)",
242 ondisk_extradevs, sbi->devs->extra_devices);
243 return -EINVAL;
244 }
245 if (!ondisk_extradevs)
246 return 0;
247
248 sbi->device_id_mask = roundup_pow_of_two(ondisk_extradevs + 1) - 1;
249 pos = le16_to_cpu(dsb->devt_slotoff) * EROFS_DEVT_SLOT_SIZE;
250 down_read(&sbi->devs->rwsem);
251 idr_for_each_entry(&sbi->devs->tree, dif, id) {
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800252 struct block_device *bdev;
253
Gao Xiang2b5379f2022-01-02 16:13:17 +0800254 ptr = erofs_read_metabuf(&buf, sb, erofs_blknr(pos),
255 EROFS_KMAP);
256 if (IS_ERR(ptr)) {
257 err = PTR_ERR(ptr);
258 break;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800259 }
260 dis = ptr + erofs_blkoff(pos);
261
262 bdev = blkdev_get_by_path(dif->path,
263 FMODE_READ | FMODE_EXCL,
264 sb->s_type);
265 if (IS_ERR(bdev)) {
266 err = PTR_ERR(bdev);
Gao Xiang2b5379f2022-01-02 16:13:17 +0800267 break;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800268 }
269 dif->bdev = bdev;
Christoph Hellwigcd913c72021-11-29 11:21:59 +0100270 dif->dax_dev = fs_dax_get_by_bdev(bdev, &dif->dax_part_off);
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800271 dif->blocks = le32_to_cpu(dis->blocks);
272 dif->mapped_blkaddr = le32_to_cpu(dis->mapped_blkaddr);
273 sbi->total_blocks += dif->blocks;
274 pos += EROFS_DEVT_SLOT_SIZE;
275 }
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800276 up_read(&sbi->devs->rwsem);
Gao Xiang2b5379f2022-01-02 16:13:17 +0800277 erofs_put_metabuf(&buf);
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800278 return err;
279}
280
Gao Xiang99634bf2019-09-04 10:09:05 +0800281static int erofs_read_superblock(struct super_block *sb)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800282{
283 struct erofs_sb_info *sbi;
Gao Xiangfe7c2422019-09-04 10:09:10 +0800284 struct page *page;
Gao Xiang0259f202019-09-04 10:09:00 +0800285 struct erofs_super_block *dsb;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200286 unsigned int blkszbits;
Gao Xiangfe7c2422019-09-04 10:09:10 +0800287 void *data;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800288 int ret;
289
Gao Xiangfe7c2422019-09-04 10:09:10 +0800290 page = read_mapping_page(sb->s_bdev->bd_inode->i_mapping, 0, NULL);
Wei Yongjun517d6b92019-09-18 08:30:33 +0000291 if (IS_ERR(page)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800292 erofs_err(sb, "cannot read erofs superblock");
Wei Yongjun517d6b92019-09-18 08:30:33 +0000293 return PTR_ERR(page);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800294 }
295
296 sbi = EROFS_SB(sb);
Gao Xiangfe7c2422019-09-04 10:09:10 +0800297
Pratik Shindeb858a482019-11-04 10:49:37 +0800298 data = kmap(page);
Gao Xiangfe7c2422019-09-04 10:09:10 +0800299 dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800300
301 ret = -EINVAL;
Gao Xiang0259f202019-09-04 10:09:00 +0800302 if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800303 erofs_err(sb, "cannot find valid erofs superblock");
Gao Xiangba2b77a2018-07-26 20:21:46 +0800304 goto out;
305 }
306
Pratik Shindeb858a482019-11-04 10:49:37 +0800307 sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
Gao Xiangde06a6a2021-03-29 09:23:05 +0800308 if (erofs_sb_has_sb_chksum(sbi)) {
Pratik Shindeb858a482019-11-04 10:49:37 +0800309 ret = erofs_superblock_csum_verify(sb, data);
310 if (ret)
311 goto out;
312 }
313
Wei Yongjun0508c1a2021-05-19 14:16:57 +0000314 ret = -EINVAL;
Gao Xiang0259f202019-09-04 10:09:00 +0800315 blkszbits = dsb->blkszbits;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800316 /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800317 if (blkszbits != LOG_BLOCK_SIZE) {
Gao Xiangbde54522021-01-20 09:30:16 +0800318 erofs_err(sb, "blkszbits %u isn't supported on this platform",
319 blkszbits);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800320 goto out;
321 }
322
Gao Xiang0259f202019-09-04 10:09:00 +0800323 if (!check_layout_compatibility(sb, dsb))
Gao Xiang5efe5132019-06-13 16:35:41 +0800324 goto out;
325
Gao Xiang14373712021-03-29 18:00:12 +0800326 sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE;
327 if (sbi->sb_size > EROFS_BLKSIZ) {
328 erofs_err(sb, "invalid sb_extslots %u (more than a fs block)",
329 sbi->sb_size);
330 goto out;
331 }
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800332 sbi->primarydevice_blocks = le32_to_cpu(dsb->blocks);
Gao Xiang0259f202019-09-04 10:09:00 +0800333 sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
Gao Xiangb17500a2018-07-26 20:21:52 +0800334#ifdef CONFIG_EROFS_FS_XATTR
Gao Xiang0259f202019-09-04 10:09:00 +0800335 sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
Gao Xiangb17500a2018-07-26 20:21:52 +0800336#endif
Gao Xiang8a765682019-09-04 10:08:54 +0800337 sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
Gao Xiang0259f202019-09-04 10:09:00 +0800338 sbi->root_nid = le16_to_cpu(dsb->root_nid);
339 sbi->inos = le64_to_cpu(dsb->inos);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800340
Gao Xiang0259f202019-09-04 10:09:00 +0800341 sbi->build_time = le64_to_cpu(dsb->build_time);
342 sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800343
Gao Xiang0259f202019-09-04 10:09:00 +0800344 memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid));
Gao Xiangba2b77a2018-07-26 20:21:46 +0800345
Gao Xiang0259f202019-09-04 10:09:00 +0800346 ret = strscpy(sbi->volume_name, dsb->volume_name,
347 sizeof(dsb->volume_name));
Gao Xianga64d9492019-08-18 18:28:24 +0800348 if (ret < 0) { /* -E2BIG */
Gao Xiang4f761fa2019-09-04 10:09:09 +0800349 erofs_err(sb, "bad volume name without NIL terminator");
Gao Xianga64d9492019-08-18 18:28:24 +0800350 ret = -EFSCORRUPTED;
351 goto out;
352 }
Huang Jianan5d505382021-03-29 09:23:06 +0800353
354 /* parse on-disk compression configurations */
Gao Xiang14373712021-03-29 18:00:12 +0800355 if (erofs_sb_has_compr_cfgs(sbi))
356 ret = erofs_load_compr_cfgs(sb, dsb);
357 else
358 ret = z_erofs_load_lz4_config(sb, dsb, NULL, 0);
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800359 if (ret < 0)
360 goto out;
361
362 /* handle multiple devices */
363 ret = erofs_init_devices(sb, dsb);
Yue Huab921842021-12-28 13:46:04 +0800364
365 if (erofs_sb_has_ztailpacking(sbi))
366 erofs_info(sb, "EXPERIMENTAL compressed inline data feature in use. Use at your own risk!");
Gao Xiangba2b77a2018-07-26 20:21:46 +0800367out:
Pratik Shindeb858a482019-11-04 10:49:37 +0800368 kunmap(page);
Gao Xiangfe7c2422019-09-04 10:09:10 +0800369 put_page(page);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800370 return ret;
371}
372
Gao Xiang4279f3f2019-07-31 23:57:49 +0800373/* set up default EROFS parameters */
Chao Yuf57a3fe2020-05-29 18:48:36 +0800374static void erofs_default_options(struct erofs_fs_context *ctx)
Gao Xiang4279f3f2019-07-31 23:57:49 +0800375{
376#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiange6242462021-10-07 15:02:23 +0800377 ctx->opt.cache_strategy = EROFS_ZIP_CACHE_READAROUND;
378 ctx->opt.max_sync_decompress_pages = 3;
Huang Jianan40452ff2021-12-06 22:35:52 +0800379 ctx->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_AUTO;
Gao Xiang4279f3f2019-07-31 23:57:49 +0800380#endif
Gao Xiangb17500a2018-07-26 20:21:52 +0800381#ifdef CONFIG_EROFS_FS_XATTR
Gao Xiange6242462021-10-07 15:02:23 +0800382 set_opt(&ctx->opt, XATTR_USER);
Gao Xiangb17500a2018-07-26 20:21:52 +0800383#endif
Gao Xiangb17500a2018-07-26 20:21:52 +0800384#ifdef CONFIG_EROFS_FS_POSIX_ACL
Gao Xiange6242462021-10-07 15:02:23 +0800385 set_opt(&ctx->opt, POSIX_ACL);
Gao Xiangb17500a2018-07-26 20:21:52 +0800386#endif
Gao Xiangba2b77a2018-07-26 20:21:46 +0800387}
388
389enum {
Gao Xiangb17500a2018-07-26 20:21:52 +0800390 Opt_user_xattr,
Gao Xiangb17500a2018-07-26 20:21:52 +0800391 Opt_acl,
Gao Xiang4279f3f2019-07-31 23:57:49 +0800392 Opt_cache_strategy,
Gao Xiang06252e92021-08-05 08:36:00 +0800393 Opt_dax,
394 Opt_dax_enum,
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800395 Opt_device,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800396 Opt_err
397};
398
Chao Yuf57a3fe2020-05-29 18:48:36 +0800399static const struct constant_table erofs_param_cache_strategy[] = {
400 {"disabled", EROFS_ZIP_CACHE_DISABLED},
401 {"readahead", EROFS_ZIP_CACHE_READAHEAD},
402 {"readaround", EROFS_ZIP_CACHE_READAROUND},
403 {}
Gao Xiangba2b77a2018-07-26 20:21:46 +0800404};
405
Gao Xiang06252e92021-08-05 08:36:00 +0800406static const struct constant_table erofs_dax_param_enums[] = {
407 {"always", EROFS_MOUNT_DAX_ALWAYS},
408 {"never", EROFS_MOUNT_DAX_NEVER},
409 {}
410};
411
Chao Yuf57a3fe2020-05-29 18:48:36 +0800412static const struct fs_parameter_spec erofs_fs_parameters[] = {
413 fsparam_flag_no("user_xattr", Opt_user_xattr),
414 fsparam_flag_no("acl", Opt_acl),
415 fsparam_enum("cache_strategy", Opt_cache_strategy,
416 erofs_param_cache_strategy),
Gao Xiang06252e92021-08-05 08:36:00 +0800417 fsparam_flag("dax", Opt_dax),
418 fsparam_enum("dax", Opt_dax_enum, erofs_dax_param_enums),
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800419 fsparam_string("device", Opt_device),
Chao Yuf57a3fe2020-05-29 18:48:36 +0800420 {}
421};
422
Gao Xiang06252e92021-08-05 08:36:00 +0800423static bool erofs_fc_set_dax_mode(struct fs_context *fc, unsigned int mode)
424{
425#ifdef CONFIG_FS_DAX
426 struct erofs_fs_context *ctx = fc->fs_private;
427
428 switch (mode) {
429 case EROFS_MOUNT_DAX_ALWAYS:
430 warnfc(fc, "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
Gao Xiange6242462021-10-07 15:02:23 +0800431 set_opt(&ctx->opt, DAX_ALWAYS);
432 clear_opt(&ctx->opt, DAX_NEVER);
Gao Xiang06252e92021-08-05 08:36:00 +0800433 return true;
434 case EROFS_MOUNT_DAX_NEVER:
Gao Xiange6242462021-10-07 15:02:23 +0800435 set_opt(&ctx->opt, DAX_NEVER);
436 clear_opt(&ctx->opt, DAX_ALWAYS);
Gao Xiang06252e92021-08-05 08:36:00 +0800437 return true;
438 default:
439 DBG_BUGON(1);
440 return false;
441 }
442#else
443 errorfc(fc, "dax options not supported");
444 return false;
445#endif
446}
447
Chao Yuf57a3fe2020-05-29 18:48:36 +0800448static int erofs_fc_parse_param(struct fs_context *fc,
449 struct fs_parameter *param)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800450{
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800451 struct erofs_fs_context *ctx = fc->fs_private;
Chao Yuf57a3fe2020-05-29 18:48:36 +0800452 struct fs_parse_result result;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800453 struct erofs_device_info *dif;
454 int opt, ret;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800455
Chao Yuf57a3fe2020-05-29 18:48:36 +0800456 opt = fs_parse(fc, erofs_fs_parameters, param, &result);
457 if (opt < 0)
458 return opt;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800459
Chao Yuf57a3fe2020-05-29 18:48:36 +0800460 switch (opt) {
461 case Opt_user_xattr:
Gao Xiangb17500a2018-07-26 20:21:52 +0800462#ifdef CONFIG_EROFS_FS_XATTR
Chao Yuf57a3fe2020-05-29 18:48:36 +0800463 if (result.boolean)
Gao Xiange6242462021-10-07 15:02:23 +0800464 set_opt(&ctx->opt, XATTR_USER);
Chao Yuf57a3fe2020-05-29 18:48:36 +0800465 else
Gao Xiange6242462021-10-07 15:02:23 +0800466 clear_opt(&ctx->opt, XATTR_USER);
Gao Xiangb17500a2018-07-26 20:21:52 +0800467#else
Chao Yuf57a3fe2020-05-29 18:48:36 +0800468 errorfc(fc, "{,no}user_xattr options not supported");
Gao Xiangb17500a2018-07-26 20:21:52 +0800469#endif
Chao Yuf57a3fe2020-05-29 18:48:36 +0800470 break;
471 case Opt_acl:
Gao Xiangb17500a2018-07-26 20:21:52 +0800472#ifdef CONFIG_EROFS_FS_POSIX_ACL
Chao Yuf57a3fe2020-05-29 18:48:36 +0800473 if (result.boolean)
Gao Xiange6242462021-10-07 15:02:23 +0800474 set_opt(&ctx->opt, POSIX_ACL);
Chao Yuf57a3fe2020-05-29 18:48:36 +0800475 else
Gao Xiange6242462021-10-07 15:02:23 +0800476 clear_opt(&ctx->opt, POSIX_ACL);
Gao Xiangb17500a2018-07-26 20:21:52 +0800477#else
Chao Yuf57a3fe2020-05-29 18:48:36 +0800478 errorfc(fc, "{,no}acl options not supported");
Gao Xiangb17500a2018-07-26 20:21:52 +0800479#endif
Chao Yuf57a3fe2020-05-29 18:48:36 +0800480 break;
481 case Opt_cache_strategy:
482#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiange6242462021-10-07 15:02:23 +0800483 ctx->opt.cache_strategy = result.uint_32;
Chao Yuf57a3fe2020-05-29 18:48:36 +0800484#else
485 errorfc(fc, "compression not supported, cache_strategy ignored");
486#endif
487 break;
Gao Xiang06252e92021-08-05 08:36:00 +0800488 case Opt_dax:
489 if (!erofs_fc_set_dax_mode(fc, EROFS_MOUNT_DAX_ALWAYS))
490 return -EINVAL;
491 break;
492 case Opt_dax_enum:
493 if (!erofs_fc_set_dax_mode(fc, result.uint_32))
494 return -EINVAL;
495 break;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800496 case Opt_device:
497 dif = kzalloc(sizeof(*dif), GFP_KERNEL);
498 if (!dif)
499 return -ENOMEM;
500 dif->path = kstrdup(param->string, GFP_KERNEL);
501 if (!dif->path) {
502 kfree(dif);
503 return -ENOMEM;
504 }
505 down_write(&ctx->devs->rwsem);
506 ret = idr_alloc(&ctx->devs->tree, dif, 0, 0, GFP_KERNEL);
507 up_write(&ctx->devs->rwsem);
508 if (ret < 0) {
509 kfree(dif->path);
510 kfree(dif);
511 return ret;
512 }
513 ++ctx->devs->extra_devices;
514 break;
Chao Yuf57a3fe2020-05-29 18:48:36 +0800515 default:
516 return -ENOPARAM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800517 }
518 return 0;
519}
520
Gao Xiang4279f3f2019-07-31 23:57:49 +0800521#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang105d4ad2018-07-26 20:22:07 +0800522static const struct address_space_operations managed_cache_aops;
523
Gao Xiang99634bf2019-09-04 10:09:05 +0800524static int erofs_managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800525{
526 int ret = 1; /* 0 - busy */
527 struct address_space *const mapping = page->mapping;
528
Gao Xiang8b987bc2018-12-05 21:23:13 +0800529 DBG_BUGON(!PageLocked(page));
530 DBG_BUGON(mapping->a_ops != &managed_cache_aops);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800531
532 if (PagePrivate(page))
Yue Hud252ff32021-08-10 15:24:16 +0800533 ret = erofs_try_to_free_cached_page(page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800534
535 return ret;
536}
537
Gao Xiang99634bf2019-09-04 10:09:05 +0800538static void erofs_managed_cache_invalidatepage(struct page *page,
539 unsigned int offset,
540 unsigned int length)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800541{
542 const unsigned int stop = length + offset;
543
Gao Xiang8b987bc2018-12-05 21:23:13 +0800544 DBG_BUGON(!PageLocked(page));
Gao Xiang105d4ad2018-07-26 20:22:07 +0800545
Gao Xiang8b987bc2018-12-05 21:23:13 +0800546 /* Check for potential overflow in debug mode */
547 DBG_BUGON(stop > PAGE_SIZE || stop < length);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800548
549 if (offset == 0 && stop == PAGE_SIZE)
Gao Xiang99634bf2019-09-04 10:09:05 +0800550 while (!erofs_managed_cache_releasepage(page, GFP_NOFS))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800551 cond_resched();
552}
553
554static const struct address_space_operations managed_cache_aops = {
Gao Xiang99634bf2019-09-04 10:09:05 +0800555 .releasepage = erofs_managed_cache_releasepage,
556 .invalidatepage = erofs_managed_cache_invalidatepage,
Gao Xiang105d4ad2018-07-26 20:22:07 +0800557};
558
Gao Xiang8f7acda2019-07-31 23:57:41 +0800559static int erofs_init_managed_cache(struct super_block *sb)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800560{
Gao Xiang8f7acda2019-07-31 23:57:41 +0800561 struct erofs_sb_info *const sbi = EROFS_SB(sb);
562 struct inode *const inode = new_inode(sb);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800563
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800564 if (!inode)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800565 return -ENOMEM;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800566
567 set_nlink(inode, 1);
568 inode->i_size = OFFSET_MAX;
569
570 inode->i_mapping->a_ops = &managed_cache_aops;
571 mapping_set_gfp_mask(inode->i_mapping,
Gao Xiang8494c292019-07-31 23:57:42 +0800572 GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800573 sbi->managed_cache = inode;
574 return 0;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800575}
Gao Xiang8f7acda2019-07-31 23:57:41 +0800576#else
577static int erofs_init_managed_cache(struct super_block *sb) { return 0; }
Gao Xiang105d4ad2018-07-26 20:22:07 +0800578#endif
579
Chao Yuf57a3fe2020-05-29 18:48:36 +0800580static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800581{
582 struct inode *inode;
583 struct erofs_sb_info *sbi;
Chao Yuf57a3fe2020-05-29 18:48:36 +0800584 struct erofs_fs_context *ctx = fc->fs_private;
Gao Xiang8f7acda2019-07-31 23:57:41 +0800585 int err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800586
Gao Xiang8f7acda2019-07-31 23:57:41 +0800587 sb->s_magic = EROFS_SUPER_MAGIC;
588
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800589 if (!sb_set_blocksize(sb, EROFS_BLKSIZ)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800590 erofs_err(sb, "failed to set erofs blksize");
Gao Xiang8f7acda2019-07-31 23:57:41 +0800591 return -EINVAL;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800592 }
593
Shobhit Kukretia9f69bd2019-06-26 22:31:18 -0700594 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800595 if (!sbi)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800596 return -ENOMEM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800597
Gao Xiang8f7acda2019-07-31 23:57:41 +0800598 sb->s_fs_info = sbi;
Gao Xiange6242462021-10-07 15:02:23 +0800599 sbi->opt = ctx->opt;
Christoph Hellwigcd913c72021-11-29 11:21:59 +0100600 sbi->dax_dev = fs_dax_get_by_bdev(sb->s_bdev, &sbi->dax_part_off);
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800601 sbi->devs = ctx->devs;
602 ctx->devs = NULL;
603
Gao Xiang99634bf2019-09-04 10:09:05 +0800604 err = erofs_read_superblock(sb);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800605 if (err)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800606 return err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800607
Christoph Hellwig7b0800d2021-11-29 11:21:42 +0100608 if (test_opt(&sbi->opt, DAX_ALWAYS)) {
609 BUILD_BUG_ON(EROFS_BLKSIZ != PAGE_SIZE);
610
611 if (!sbi->dax_dev) {
612 errorfc(fc, "DAX unsupported by block device. Turning off DAX.");
613 clear_opt(&sbi->opt, DAX_ALWAYS);
614 }
Gao Xiang06252e92021-08-05 08:36:00 +0800615 }
Gao Xiang5f0abea2018-09-06 17:01:47 +0800616 sb->s_flags |= SB_RDONLY | SB_NOATIME;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800617 sb->s_maxbytes = MAX_LFS_FILESIZE;
618 sb->s_time_gran = 1;
619
620 sb->s_op = &erofs_sops;
Gao Xiangb17500a2018-07-26 20:21:52 +0800621 sb->s_xattr = erofs_xattr_handlers;
Chengguang Xue7cda1e2020-05-26 17:03:43 +0800622
Gao Xiange6242462021-10-07 15:02:23 +0800623 if (test_opt(&sbi->opt, POSIX_ACL))
Gao Xiang516c115c2019-01-29 16:35:20 +0800624 sb->s_flags |= SB_POSIXACL;
625 else
626 sb->s_flags &= ~SB_POSIXACL;
627
Gao Xiange7e9a302018-07-26 20:22:05 +0800628#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang64094a02020-02-20 10:46:42 +0800629 xa_init(&sbi->managed_pslots);
Gao Xiange7e9a302018-07-26 20:22:05 +0800630#endif
631
Gao Xiangba2b77a2018-07-26 20:21:46 +0800632 /* get the root inode */
633 inode = erofs_iget(sb, ROOT_NID(sbi), true);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800634 if (IS_ERR(inode))
635 return PTR_ERR(inode);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800636
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800637 if (!S_ISDIR(inode->i_mode)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800638 erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
639 ROOT_NID(sbi), inode->i_mode);
Chengguang Xu94832d92019-01-23 14:12:25 +0800640 iput(inode);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800641 return -EINVAL;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800642 }
643
644 sb->s_root = d_make_root(inode);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800645 if (!sb->s_root)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800646 return -ENOMEM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800647
Gao Xiang22fe04a2019-07-31 23:57:39 +0800648 erofs_shrinker_register(sb);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800649 /* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
650 err = erofs_init_managed_cache(sb);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800651 if (err)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800652 return err;
Gao Xiang2497ee42018-07-26 20:22:03 +0800653
Huang Jianan168e9a72021-12-01 22:54:36 +0800654 err = erofs_register_sysfs(sb);
655 if (err)
656 return err;
657
Chao Yuf57a3fe2020-05-29 18:48:36 +0800658 erofs_info(sb, "mounted with root inode @ nid %llu.", ROOT_NID(sbi));
Gao Xiangba2b77a2018-07-26 20:21:46 +0800659 return 0;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800660}
661
Chao Yuf57a3fe2020-05-29 18:48:36 +0800662static int erofs_fc_get_tree(struct fs_context *fc)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800663{
Chao Yuf57a3fe2020-05-29 18:48:36 +0800664 return get_tree_bdev(fc, erofs_fc_fill_super);
665}
666
667static int erofs_fc_reconfigure(struct fs_context *fc)
668{
669 struct super_block *sb = fc->root->d_sb;
670 struct erofs_sb_info *sbi = EROFS_SB(sb);
671 struct erofs_fs_context *ctx = fc->fs_private;
672
673 DBG_BUGON(!sb_rdonly(sb));
674
Gao Xiange6242462021-10-07 15:02:23 +0800675 if (test_opt(&ctx->opt, POSIX_ACL))
Chao Yuf57a3fe2020-05-29 18:48:36 +0800676 fc->sb_flags |= SB_POSIXACL;
677 else
678 fc->sb_flags &= ~SB_POSIXACL;
679
Gao Xiange6242462021-10-07 15:02:23 +0800680 sbi->opt = ctx->opt;
Chao Yuf57a3fe2020-05-29 18:48:36 +0800681
682 fc->sb_flags |= SB_RDONLY;
683 return 0;
684}
685
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800686static int erofs_release_device_info(int id, void *ptr, void *data)
687{
688 struct erofs_device_info *dif = ptr;
689
690 fs_put_dax(dif->dax_dev);
691 if (dif->bdev)
692 blkdev_put(dif->bdev, FMODE_READ | FMODE_EXCL);
693 kfree(dif->path);
694 kfree(dif);
695 return 0;
696}
697
698static void erofs_free_dev_context(struct erofs_dev_context *devs)
699{
700 if (!devs)
701 return;
702 idr_for_each(&devs->tree, &erofs_release_device_info, NULL);
703 idr_destroy(&devs->tree);
704 kfree(devs);
705}
706
Chao Yuf57a3fe2020-05-29 18:48:36 +0800707static void erofs_fc_free(struct fs_context *fc)
708{
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800709 struct erofs_fs_context *ctx = fc->fs_private;
710
711 erofs_free_dev_context(ctx->devs);
712 kfree(ctx);
Chao Yuf57a3fe2020-05-29 18:48:36 +0800713}
714
715static const struct fs_context_operations erofs_context_ops = {
716 .parse_param = erofs_fc_parse_param,
717 .get_tree = erofs_fc_get_tree,
718 .reconfigure = erofs_fc_reconfigure,
719 .free = erofs_fc_free,
720};
721
722static int erofs_init_fs_context(struct fs_context *fc)
723{
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800724 struct erofs_fs_context *ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
725
726 if (!ctx)
Chao Yuf57a3fe2020-05-29 18:48:36 +0800727 return -ENOMEM;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800728 ctx->devs = kzalloc(sizeof(struct erofs_dev_context), GFP_KERNEL);
729 if (!ctx->devs) {
730 kfree(ctx);
731 return -ENOMEM;
732 }
733 fc->fs_private = ctx;
Chao Yuf57a3fe2020-05-29 18:48:36 +0800734
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800735 idr_init(&ctx->devs->tree);
736 init_rwsem(&ctx->devs->rwsem);
737 erofs_default_options(ctx);
Chao Yuf57a3fe2020-05-29 18:48:36 +0800738 fc->ops = &erofs_context_ops;
Chao Yuf57a3fe2020-05-29 18:48:36 +0800739 return 0;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800740}
741
Gao Xiang8f7acda2019-07-31 23:57:41 +0800742/*
743 * could be triggered after deactivate_locked_super()
744 * is called, thus including umount and failed to initialize.
745 */
746static void erofs_kill_sb(struct super_block *sb)
747{
748 struct erofs_sb_info *sbi;
749
750 WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800751
752 kill_block_super(sb);
753
754 sbi = EROFS_SB(sb);
755 if (!sbi)
756 return;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800757
758 erofs_free_dev_context(sbi->devs);
Gao Xiang06252e92021-08-05 08:36:00 +0800759 fs_put_dax(sbi->dax_dev);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800760 kfree(sbi);
761 sb->s_fs_info = NULL;
762}
763
764/* called when ->s_root is non-NULL */
765static void erofs_put_super(struct super_block *sb)
766{
767 struct erofs_sb_info *const sbi = EROFS_SB(sb);
768
769 DBG_BUGON(!sbi);
770
Huang Jianan168e9a72021-12-01 22:54:36 +0800771 erofs_unregister_sysfs(sb);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800772 erofs_shrinker_unregister(sb);
Gao Xiang4279f3f2019-07-31 23:57:49 +0800773#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang8f7acda2019-07-31 23:57:41 +0800774 iput(sbi->managed_cache);
775 sbi->managed_cache = NULL;
776#endif
777}
778
Gao Xiangba2b77a2018-07-26 20:21:46 +0800779static struct file_system_type erofs_fs_type = {
780 .owner = THIS_MODULE,
781 .name = "erofs",
Chao Yuf57a3fe2020-05-29 18:48:36 +0800782 .init_fs_context = erofs_init_fs_context,
Gao Xiang8f7acda2019-07-31 23:57:41 +0800783 .kill_sb = erofs_kill_sb,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800784 .fs_flags = FS_REQUIRES_DEV,
785};
786MODULE_ALIAS_FS("erofs");
787
788static int __init erofs_module_init(void)
789{
790 int err;
791
792 erofs_check_ondisk_layout_definitions();
Gao Xiangba2b77a2018-07-26 20:21:46 +0800793
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800794 erofs_inode_cachep = kmem_cache_create("erofs_inode",
Gao Xianga5876e22019-09-04 10:08:56 +0800795 sizeof(struct erofs_inode), 0,
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800796 SLAB_RECLAIM_ACCOUNT,
Gao Xiang99634bf2019-09-04 10:09:05 +0800797 erofs_inode_init_once);
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800798 if (!erofs_inode_cachep) {
799 err = -ENOMEM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800800 goto icache_err;
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800801 }
Gao Xiangba2b77a2018-07-26 20:21:46 +0800802
Gao Xiang22fe04a2019-07-31 23:57:39 +0800803 err = erofs_init_shrinker();
Gao Xianga1581312018-07-26 20:22:04 +0800804 if (err)
805 goto shrinker_err;
806
Gao Xiang622cead2021-10-11 05:31:45 +0800807 err = z_erofs_lzma_init();
808 if (err)
809 goto lzma_err;
810
Gao Xiang52488732021-04-10 03:06:30 +0800811 erofs_pcpubuf_init();
Gao Xiang3883a792018-07-26 20:22:06 +0800812 err = z_erofs_init_zip_subsystem();
813 if (err)
814 goto zip_err;
Gao Xiang3883a792018-07-26 20:22:06 +0800815
Huang Jianan168e9a72021-12-01 22:54:36 +0800816 err = erofs_init_sysfs();
817 if (err)
818 goto sysfs_err;
819
Gao Xiangba2b77a2018-07-26 20:21:46 +0800820 err = register_filesystem(&erofs_fs_type);
821 if (err)
822 goto fs_err;
823
Gao Xiangba2b77a2018-07-26 20:21:46 +0800824 return 0;
825
826fs_err:
Huang Jianan168e9a72021-12-01 22:54:36 +0800827 erofs_exit_sysfs();
828sysfs_err:
Gao Xiang3883a792018-07-26 20:22:06 +0800829 z_erofs_exit_zip_subsystem();
830zip_err:
Gao Xiang622cead2021-10-11 05:31:45 +0800831 z_erofs_lzma_exit();
832lzma_err:
Gao Xiang22fe04a2019-07-31 23:57:39 +0800833 erofs_exit_shrinker();
Gao Xianga1581312018-07-26 20:22:04 +0800834shrinker_err:
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800835 kmem_cache_destroy(erofs_inode_cachep);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800836icache_err:
837 return err;
838}
839
840static void __exit erofs_module_exit(void)
841{
842 unregister_filesystem(&erofs_fs_type);
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800843
Gao Xiang622cead2021-10-11 05:31:45 +0800844 /* Ensure all RCU free inodes / pclusters are safe to be destroyed. */
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800845 rcu_barrier();
Gao Xiang622cead2021-10-11 05:31:45 +0800846
Huang Jianan168e9a72021-12-01 22:54:36 +0800847 erofs_exit_sysfs();
Gao Xiang622cead2021-10-11 05:31:45 +0800848 z_erofs_exit_zip_subsystem();
849 z_erofs_lzma_exit();
850 erofs_exit_shrinker();
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800851 kmem_cache_destroy(erofs_inode_cachep);
Gao Xiang52488732021-04-10 03:06:30 +0800852 erofs_pcpubuf_exit();
Gao Xiangba2b77a2018-07-26 20:21:46 +0800853}
854
855/* get filesystem statistics */
856static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
857{
858 struct super_block *sb = dentry->d_sb;
859 struct erofs_sb_info *sbi = EROFS_SB(sb);
860 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
861
862 buf->f_type = sb->s_magic;
863 buf->f_bsize = EROFS_BLKSIZ;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800864 buf->f_blocks = sbi->total_blocks;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800865 buf->f_bfree = buf->f_bavail = 0;
866
867 buf->f_files = ULLONG_MAX;
868 buf->f_ffree = ULLONG_MAX - sbi->inos;
869
870 buf->f_namelen = EROFS_NAME_LEN;
871
Al Viro6d1349c2020-09-18 16:45:50 -0400872 buf->f_fsid = u64_to_fsid(id);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800873 return 0;
874}
875
876static int erofs_show_options(struct seq_file *seq, struct dentry *root)
877{
Gao Xiang06252e92021-08-05 08:36:00 +0800878 struct erofs_sb_info *sbi = EROFS_SB(root->d_sb);
Gao Xiange6242462021-10-07 15:02:23 +0800879 struct erofs_mount_opts *opt = &sbi->opt;
Gao Xiangb17500a2018-07-26 20:21:52 +0800880
881#ifdef CONFIG_EROFS_FS_XATTR
Gao Xiange6242462021-10-07 15:02:23 +0800882 if (test_opt(opt, XATTR_USER))
Gao Xiangb17500a2018-07-26 20:21:52 +0800883 seq_puts(seq, ",user_xattr");
884 else
885 seq_puts(seq, ",nouser_xattr");
886#endif
887#ifdef CONFIG_EROFS_FS_POSIX_ACL
Gao Xiange6242462021-10-07 15:02:23 +0800888 if (test_opt(opt, POSIX_ACL))
Gao Xiangb17500a2018-07-26 20:21:52 +0800889 seq_puts(seq, ",acl");
890 else
891 seq_puts(seq, ",noacl");
892#endif
Gao Xiang4279f3f2019-07-31 23:57:49 +0800893#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiange6242462021-10-07 15:02:23 +0800894 if (opt->cache_strategy == EROFS_ZIP_CACHE_DISABLED)
Gao Xiang4279f3f2019-07-31 23:57:49 +0800895 seq_puts(seq, ",cache_strategy=disabled");
Gao Xiange6242462021-10-07 15:02:23 +0800896 else if (opt->cache_strategy == EROFS_ZIP_CACHE_READAHEAD)
Gao Xiang4279f3f2019-07-31 23:57:49 +0800897 seq_puts(seq, ",cache_strategy=readahead");
Gao Xiange6242462021-10-07 15:02:23 +0800898 else if (opt->cache_strategy == EROFS_ZIP_CACHE_READAROUND)
Gao Xiang4279f3f2019-07-31 23:57:49 +0800899 seq_puts(seq, ",cache_strategy=readaround");
Gao Xiang4279f3f2019-07-31 23:57:49 +0800900#endif
Gao Xiange6242462021-10-07 15:02:23 +0800901 if (test_opt(opt, DAX_ALWAYS))
Gao Xiang06252e92021-08-05 08:36:00 +0800902 seq_puts(seq, ",dax=always");
Gao Xiange6242462021-10-07 15:02:23 +0800903 if (test_opt(opt, DAX_NEVER))
Gao Xiang06252e92021-08-05 08:36:00 +0800904 seq_puts(seq, ",dax=never");
Gao Xiangba2b77a2018-07-26 20:21:46 +0800905 return 0;
906}
907
Gao Xiangba2b77a2018-07-26 20:21:46 +0800908const struct super_operations erofs_sops = {
909 .put_super = erofs_put_super,
Gao Xiang99634bf2019-09-04 10:09:05 +0800910 .alloc_inode = erofs_alloc_inode,
911 .free_inode = erofs_free_inode,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800912 .statfs = erofs_statfs,
913 .show_options = erofs_show_options,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800914};
915
916module_init(erofs_module_init);
917module_exit(erofs_module_exit);
918
919MODULE_DESCRIPTION("Enhanced ROM File System");
Gao Xiangbc33d9f2019-07-31 23:57:51 +0800920MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
Gao Xiangba2b77a2018-07-26 20:21:46 +0800921MODULE_LICENSE("GPL");