1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Copyright (C) 2021, Alibaba Cloud
6 */
7 #include <linux/statfs.h>
8 #include <linux/seq_file.h>
9 #include <linux/crc32c.h>
10 #include <linux/fs_context.h>
11 #include <linux/fs_parser.h>
12 #include <linux/exportfs.h>
13 #include <linux/backing-dev.h>
14 #include "xattr.h"
15
16 #define CREATE_TRACE_POINTS
17 #include <trace/events/erofs.h>
18
19 static struct kmem_cache *erofs_inode_cachep __read_mostly;
20
_erofs_printk(struct super_block * sb,const char * fmt,...)21 void _erofs_printk(struct super_block *sb, const char *fmt, ...)
22 {
23 struct va_format vaf;
24 va_list args;
25 int level;
26
27 va_start(args, fmt);
28
29 level = printk_get_level(fmt);
30 vaf.fmt = printk_skip_level(fmt);
31 vaf.va = &args;
32 if (sb)
33 printk("%c%cerofs (device %s): %pV",
34 KERN_SOH_ASCII, level, sb->s_id, &vaf);
35 else
36 printk("%c%cerofs: %pV", KERN_SOH_ASCII, level, &vaf);
37 va_end(args);
38 }
39
erofs_superblock_csum_verify(struct super_block * sb,void * sbdata)40 static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata)
41 {
42 struct erofs_super_block *dsb = sbdata + EROFS_SUPER_OFFSET;
43 u32 len = 1 << EROFS_SB(sb)->blkszbits, crc;
44
45 if (len > EROFS_SUPER_OFFSET)
46 len -= EROFS_SUPER_OFFSET;
47 len -= offsetof(struct erofs_super_block, checksum) +
48 sizeof(dsb->checksum);
49
50 /* skip .magic(pre-verified) and .checksum(0) fields */
51 crc = crc32c(0x5045B54A, (&dsb->checksum) + 1, len);
52 if (crc == le32_to_cpu(dsb->checksum))
53 return 0;
54 erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected",
55 crc, le32_to_cpu(dsb->checksum));
56 return -EBADMSG;
57 }
58
erofs_inode_init_once(void * ptr)59 static void erofs_inode_init_once(void *ptr)
60 {
61 struct erofs_inode *vi = ptr;
62
63 inode_init_once(&vi->vfs_inode);
64 }
65
erofs_alloc_inode(struct super_block * sb)66 static struct inode *erofs_alloc_inode(struct super_block *sb)
67 {
68 struct erofs_inode *vi =
69 alloc_inode_sb(sb, erofs_inode_cachep, GFP_KERNEL);
70
71 if (!vi)
72 return NULL;
73
74 /* zero out everything except vfs_inode */
75 memset(vi, 0, offsetof(struct erofs_inode, vfs_inode));
76 return &vi->vfs_inode;
77 }
78
erofs_free_inode(struct inode * inode)79 static void erofs_free_inode(struct inode *inode)
80 {
81 struct erofs_inode *vi = EROFS_I(inode);
82
83 if (inode->i_op == &erofs_fast_symlink_iops)
84 kfree(inode->i_link);
85 kfree(vi->xattr_shared_xattrs);
86 kmem_cache_free(erofs_inode_cachep, vi);
87 }
88
89 /* 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)90 void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
91 erofs_off_t *offset, int *lengthp)
92 {
93 u8 *buffer, *ptr;
94 int len, i, cnt;
95
96 *offset = round_up(*offset, 4);
97 ptr = erofs_bread(buf, *offset, EROFS_KMAP);
98 if (IS_ERR(ptr))
99 return ptr;
100
101 len = le16_to_cpu(*(__le16 *)ptr);
102 if (!len)
103 len = U16_MAX + 1;
104 buffer = kmalloc(len, GFP_KERNEL);
105 if (!buffer)
106 return ERR_PTR(-ENOMEM);
107 *offset += sizeof(__le16);
108 *lengthp = len;
109
110 for (i = 0; i < len; i += cnt) {
111 cnt = min_t(int, sb->s_blocksize - erofs_blkoff(sb, *offset),
112 len - i);
113 ptr = erofs_bread(buf, *offset, EROFS_KMAP);
114 if (IS_ERR(ptr)) {
115 kfree(buffer);
116 return ptr;
117 }
118 memcpy(buffer + i, ptr, cnt);
119 *offset += cnt;
120 }
121 return buffer;
122 }
123
124 #ifndef CONFIG_EROFS_FS_ZIP
z_erofs_parse_cfgs(struct super_block * sb,struct erofs_super_block * dsb)125 static int z_erofs_parse_cfgs(struct super_block *sb,
126 struct erofs_super_block *dsb)
127 {
128 if (!dsb->u1.available_compr_algs)
129 return 0;
130
131 erofs_err(sb, "compression disabled, unable to mount compressed EROFS");
132 return -EOPNOTSUPP;
133 }
134 #endif
135
erofs_init_device(struct erofs_buf * buf,struct super_block * sb,struct erofs_device_info * dif,erofs_off_t * pos)136 static int erofs_init_device(struct erofs_buf *buf, struct super_block *sb,
137 struct erofs_device_info *dif, erofs_off_t *pos)
138 {
139 struct erofs_sb_info *sbi = EROFS_SB(sb);
140 struct erofs_fscache *fscache;
141 struct erofs_deviceslot *dis;
142 struct file *file;
143
144 dis = erofs_read_metabuf(buf, sb, *pos, EROFS_KMAP);
145 if (IS_ERR(dis))
146 return PTR_ERR(dis);
147
148 if (!sbi->devs->flatdev && !dif->path) {
149 if (!dis->tag[0]) {
150 erofs_err(sb, "empty device tag @ pos %llu", *pos);
151 return -EINVAL;
152 }
153 dif->path = kmemdup_nul(dis->tag, sizeof(dis->tag), GFP_KERNEL);
154 if (!dif->path)
155 return -ENOMEM;
156 }
157
158 if (erofs_is_fscache_mode(sb)) {
159 fscache = erofs_fscache_register_cookie(sb, dif->path, 0);
160 if (IS_ERR(fscache))
161 return PTR_ERR(fscache);
162 dif->fscache = fscache;
163 } else if (!sbi->devs->flatdev) {
164 file = erofs_is_fileio_mode(sbi) ?
165 filp_open(dif->path, O_RDONLY | O_LARGEFILE, 0) :
166 bdev_file_open_by_path(dif->path,
167 BLK_OPEN_READ, sb->s_type, NULL);
168 if (IS_ERR(file))
169 return PTR_ERR(file);
170
171 if (!erofs_is_fileio_mode(sbi)) {
172 dif->dax_dev = fs_dax_get_by_bdev(file_bdev(file),
173 &dif->dax_part_off, NULL, NULL);
174 } else if (!S_ISREG(file_inode(file)->i_mode)) {
175 fput(file);
176 return -EINVAL;
177 }
178 dif->file = file;
179 }
180
181 dif->blocks = le32_to_cpu(dis->blocks);
182 dif->mapped_blkaddr = le32_to_cpu(dis->mapped_blkaddr);
183 sbi->total_blocks += dif->blocks;
184 *pos += EROFS_DEVT_SLOT_SIZE;
185 return 0;
186 }
187
erofs_scan_devices(struct super_block * sb,struct erofs_super_block * dsb)188 static int erofs_scan_devices(struct super_block *sb,
189 struct erofs_super_block *dsb)
190 {
191 struct erofs_sb_info *sbi = EROFS_SB(sb);
192 unsigned int ondisk_extradevs;
193 erofs_off_t pos;
194 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
195 struct erofs_device_info *dif;
196 int id, err = 0;
197
198 sbi->total_blocks = sbi->dif0.blocks;
199 if (!erofs_sb_has_device_table(sbi))
200 ondisk_extradevs = 0;
201 else
202 ondisk_extradevs = le16_to_cpu(dsb->extra_devices);
203
204 if (sbi->devs->extra_devices &&
205 ondisk_extradevs != sbi->devs->extra_devices) {
206 erofs_err(sb, "extra devices don't match (ondisk %u, given %u)",
207 ondisk_extradevs, sbi->devs->extra_devices);
208 return -EINVAL;
209 }
210 if (!ondisk_extradevs)
211 return 0;
212
213 if (!sbi->devs->extra_devices && !erofs_is_fscache_mode(sb))
214 sbi->devs->flatdev = true;
215
216 sbi->device_id_mask = roundup_pow_of_two(ondisk_extradevs + 1) - 1;
217 pos = le16_to_cpu(dsb->devt_slotoff) * EROFS_DEVT_SLOT_SIZE;
218 down_read(&sbi->devs->rwsem);
219 if (sbi->devs->extra_devices) {
220 idr_for_each_entry(&sbi->devs->tree, dif, id) {
221 err = erofs_init_device(&buf, sb, dif, &pos);
222 if (err)
223 break;
224 }
225 } else {
226 for (id = 0; id < ondisk_extradevs; id++) {
227 dif = kzalloc(sizeof(*dif), GFP_KERNEL);
228 if (!dif) {
229 err = -ENOMEM;
230 break;
231 }
232
233 err = idr_alloc(&sbi->devs->tree, dif, 0, 0, GFP_KERNEL);
234 if (err < 0) {
235 kfree(dif);
236 break;
237 }
238 ++sbi->devs->extra_devices;
239
240 err = erofs_init_device(&buf, sb, dif, &pos);
241 if (err)
242 break;
243 }
244 }
245 up_read(&sbi->devs->rwsem);
246 erofs_put_metabuf(&buf);
247 return err;
248 }
249
erofs_read_superblock(struct super_block * sb)250 static int erofs_read_superblock(struct super_block *sb)
251 {
252 struct erofs_sb_info *sbi = EROFS_SB(sb);
253 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
254 struct erofs_super_block *dsb;
255 void *data;
256 int ret;
257
258 data = erofs_read_metabuf(&buf, sb, 0, EROFS_KMAP);
259 if (IS_ERR(data)) {
260 erofs_err(sb, "cannot read erofs superblock");
261 return PTR_ERR(data);
262 }
263
264 dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
265 ret = -EINVAL;
266 if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
267 erofs_err(sb, "cannot find valid erofs superblock");
268 goto out;
269 }
270
271 sbi->blkszbits = dsb->blkszbits;
272 if (sbi->blkszbits < 9 || sbi->blkszbits > PAGE_SHIFT) {
273 erofs_err(sb, "blkszbits %u isn't supported", sbi->blkszbits);
274 goto out;
275 }
276 if (dsb->dirblkbits) {
277 erofs_err(sb, "dirblkbits %u isn't supported", dsb->dirblkbits);
278 goto out;
279 }
280
281 sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
282 if (erofs_sb_has_sb_chksum(sbi)) {
283 ret = erofs_superblock_csum_verify(sb, data);
284 if (ret)
285 goto out;
286 }
287
288 ret = -EINVAL;
289 sbi->feature_incompat = le32_to_cpu(dsb->feature_incompat);
290 if (sbi->feature_incompat & ~EROFS_ALL_FEATURE_INCOMPAT) {
291 erofs_err(sb, "unidentified incompatible feature %x, please upgrade kernel",
292 sbi->feature_incompat & ~EROFS_ALL_FEATURE_INCOMPAT);
293 goto out;
294 }
295
296 sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE;
297 if (sbi->sb_size > PAGE_SIZE - EROFS_SUPER_OFFSET) {
298 erofs_err(sb, "invalid sb_extslots %u (more than a fs block)",
299 sbi->sb_size);
300 goto out;
301 }
302 sbi->dif0.blocks = le32_to_cpu(dsb->blocks);
303 sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
304 #ifdef CONFIG_EROFS_FS_XATTR
305 sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
306 sbi->xattr_prefix_start = le32_to_cpu(dsb->xattr_prefix_start);
307 sbi->xattr_prefix_count = dsb->xattr_prefix_count;
308 sbi->xattr_filter_reserved = dsb->xattr_filter_reserved;
309 #endif
310 sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
311 sbi->root_nid = le16_to_cpu(dsb->root_nid);
312 sbi->packed_nid = le64_to_cpu(dsb->packed_nid);
313 sbi->inos = le64_to_cpu(dsb->inos);
314
315 sbi->build_time = le64_to_cpu(dsb->build_time);
316 sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
317
318 super_set_uuid(sb, (void *)dsb->uuid, sizeof(dsb->uuid));
319
320 /* parse on-disk compression configurations */
321 ret = z_erofs_parse_cfgs(sb, dsb);
322 if (ret < 0)
323 goto out;
324
325 /* handle multiple devices */
326 ret = erofs_scan_devices(sb, dsb);
327
328 if (erofs_is_fscache_mode(sb))
329 erofs_info(sb, "[deprecated] fscache-based on-demand read feature in use. Use at your own risk!");
330 out:
331 erofs_put_metabuf(&buf);
332 return ret;
333 }
334
erofs_default_options(struct erofs_sb_info * sbi)335 static void erofs_default_options(struct erofs_sb_info *sbi)
336 {
337 #ifdef CONFIG_EROFS_FS_ZIP
338 sbi->opt.cache_strategy = EROFS_ZIP_CACHE_READAROUND;
339 sbi->opt.max_sync_decompress_pages = 3;
340 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_AUTO;
341 #endif
342 #ifdef CONFIG_EROFS_FS_XATTR
343 set_opt(&sbi->opt, XATTR_USER);
344 #endif
345 #ifdef CONFIG_EROFS_FS_POSIX_ACL
346 set_opt(&sbi->opt, POSIX_ACL);
347 #endif
348 }
349
350 enum {
351 Opt_user_xattr, Opt_acl, Opt_cache_strategy, Opt_dax, Opt_dax_enum,
352 Opt_device, Opt_fsid, Opt_domain_id, Opt_directio,
353 Opt_err
354 };
355
356 static const struct constant_table erofs_param_cache_strategy[] = {
357 {"disabled", EROFS_ZIP_CACHE_DISABLED},
358 {"readahead", EROFS_ZIP_CACHE_READAHEAD},
359 {"readaround", EROFS_ZIP_CACHE_READAROUND},
360 {}
361 };
362
363 static const struct constant_table erofs_dax_param_enums[] = {
364 {"always", EROFS_MOUNT_DAX_ALWAYS},
365 {"never", EROFS_MOUNT_DAX_NEVER},
366 {}
367 };
368
369 static const struct fs_parameter_spec erofs_fs_parameters[] = {
370 fsparam_flag_no("user_xattr", Opt_user_xattr),
371 fsparam_flag_no("acl", Opt_acl),
372 fsparam_enum("cache_strategy", Opt_cache_strategy,
373 erofs_param_cache_strategy),
374 fsparam_flag("dax", Opt_dax),
375 fsparam_enum("dax", Opt_dax_enum, erofs_dax_param_enums),
376 fsparam_string("device", Opt_device),
377 fsparam_string("fsid", Opt_fsid),
378 fsparam_string("domain_id", Opt_domain_id),
379 fsparam_flag_no("directio", Opt_directio),
380 {}
381 };
382
erofs_fc_set_dax_mode(struct fs_context * fc,unsigned int mode)383 static bool erofs_fc_set_dax_mode(struct fs_context *fc, unsigned int mode)
384 {
385 #ifdef CONFIG_FS_DAX
386 struct erofs_sb_info *sbi = fc->s_fs_info;
387
388 switch (mode) {
389 case EROFS_MOUNT_DAX_ALWAYS:
390 set_opt(&sbi->opt, DAX_ALWAYS);
391 clear_opt(&sbi->opt, DAX_NEVER);
392 return true;
393 case EROFS_MOUNT_DAX_NEVER:
394 set_opt(&sbi->opt, DAX_NEVER);
395 clear_opt(&sbi->opt, DAX_ALWAYS);
396 return true;
397 default:
398 DBG_BUGON(1);
399 return false;
400 }
401 #else
402 errorfc(fc, "dax options not supported");
403 return false;
404 #endif
405 }
406
erofs_fc_parse_param(struct fs_context * fc,struct fs_parameter * param)407 static int erofs_fc_parse_param(struct fs_context *fc,
408 struct fs_parameter *param)
409 {
410 struct erofs_sb_info *sbi = fc->s_fs_info;
411 struct fs_parse_result result;
412 struct erofs_device_info *dif;
413 int opt, ret;
414
415 opt = fs_parse(fc, erofs_fs_parameters, param, &result);
416 if (opt < 0)
417 return opt;
418
419 switch (opt) {
420 case Opt_user_xattr:
421 #ifdef CONFIG_EROFS_FS_XATTR
422 if (result.boolean)
423 set_opt(&sbi->opt, XATTR_USER);
424 else
425 clear_opt(&sbi->opt, XATTR_USER);
426 #else
427 errorfc(fc, "{,no}user_xattr options not supported");
428 #endif
429 break;
430 case Opt_acl:
431 #ifdef CONFIG_EROFS_FS_POSIX_ACL
432 if (result.boolean)
433 set_opt(&sbi->opt, POSIX_ACL);
434 else
435 clear_opt(&sbi->opt, POSIX_ACL);
436 #else
437 errorfc(fc, "{,no}acl options not supported");
438 #endif
439 break;
440 case Opt_cache_strategy:
441 #ifdef CONFIG_EROFS_FS_ZIP
442 sbi->opt.cache_strategy = result.uint_32;
443 #else
444 errorfc(fc, "compression not supported, cache_strategy ignored");
445 #endif
446 break;
447 case Opt_dax:
448 if (!erofs_fc_set_dax_mode(fc, EROFS_MOUNT_DAX_ALWAYS))
449 return -EINVAL;
450 break;
451 case Opt_dax_enum:
452 if (!erofs_fc_set_dax_mode(fc, result.uint_32))
453 return -EINVAL;
454 break;
455 case Opt_device:
456 dif = kzalloc(sizeof(*dif), GFP_KERNEL);
457 if (!dif)
458 return -ENOMEM;
459 dif->path = kstrdup(param->string, GFP_KERNEL);
460 if (!dif->path) {
461 kfree(dif);
462 return -ENOMEM;
463 }
464 down_write(&sbi->devs->rwsem);
465 ret = idr_alloc(&sbi->devs->tree, dif, 0, 0, GFP_KERNEL);
466 up_write(&sbi->devs->rwsem);
467 if (ret < 0) {
468 kfree(dif->path);
469 kfree(dif);
470 return ret;
471 }
472 ++sbi->devs->extra_devices;
473 break;
474 #ifdef CONFIG_EROFS_FS_ONDEMAND
475 case Opt_fsid:
476 kfree(sbi->fsid);
477 sbi->fsid = kstrdup(param->string, GFP_KERNEL);
478 if (!sbi->fsid)
479 return -ENOMEM;
480 break;
481 case Opt_domain_id:
482 kfree(sbi->domain_id);
483 sbi->domain_id = kstrdup(param->string, GFP_KERNEL);
484 if (!sbi->domain_id)
485 return -ENOMEM;
486 break;
487 #else
488 case Opt_fsid:
489 case Opt_domain_id:
490 errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name);
491 break;
492 #endif
493 case Opt_directio:
494 #ifdef CONFIG_EROFS_FS_BACKED_BY_FILE
495 if (result.boolean)
496 set_opt(&sbi->opt, DIRECT_IO);
497 else
498 clear_opt(&sbi->opt, DIRECT_IO);
499 #else
500 errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name);
501 #endif
502 break;
503 }
504 return 0;
505 }
506
erofs_nfs_get_inode(struct super_block * sb,u64 ino,u32 generation)507 static struct inode *erofs_nfs_get_inode(struct super_block *sb,
508 u64 ino, u32 generation)
509 {
510 return erofs_iget(sb, ino);
511 }
512
erofs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)513 static struct dentry *erofs_fh_to_dentry(struct super_block *sb,
514 struct fid *fid, int fh_len, int fh_type)
515 {
516 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
517 erofs_nfs_get_inode);
518 }
519
erofs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)520 static struct dentry *erofs_fh_to_parent(struct super_block *sb,
521 struct fid *fid, int fh_len, int fh_type)
522 {
523 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
524 erofs_nfs_get_inode);
525 }
526
erofs_get_parent(struct dentry * child)527 static struct dentry *erofs_get_parent(struct dentry *child)
528 {
529 erofs_nid_t nid;
530 unsigned int d_type;
531 int err;
532
533 err = erofs_namei(d_inode(child), &dotdot_name, &nid, &d_type);
534 if (err)
535 return ERR_PTR(err);
536 return d_obtain_alias(erofs_iget(child->d_sb, nid));
537 }
538
539 static const struct export_operations erofs_export_ops = {
540 .encode_fh = generic_encode_ino32_fh,
541 .fh_to_dentry = erofs_fh_to_dentry,
542 .fh_to_parent = erofs_fh_to_parent,
543 .get_parent = erofs_get_parent,
544 };
545
erofs_set_sysfs_name(struct super_block * sb)546 static void erofs_set_sysfs_name(struct super_block *sb)
547 {
548 struct erofs_sb_info *sbi = EROFS_SB(sb);
549
550 if (sbi->domain_id)
551 super_set_sysfs_name_generic(sb, "%s,%s", sbi->domain_id,
552 sbi->fsid);
553 else if (sbi->fsid)
554 super_set_sysfs_name_generic(sb, "%s", sbi->fsid);
555 else if (erofs_is_fileio_mode(sbi))
556 super_set_sysfs_name_generic(sb, "%s",
557 bdi_dev_name(sb->s_bdi));
558 else
559 super_set_sysfs_name_id(sb);
560 }
561
erofs_fc_fill_super(struct super_block * sb,struct fs_context * fc)562 static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
563 {
564 struct inode *inode;
565 struct erofs_sb_info *sbi = EROFS_SB(sb);
566 int err;
567
568 sb->s_magic = EROFS_SUPER_MAGIC;
569 sb->s_flags |= SB_RDONLY | SB_NOATIME;
570 sb->s_maxbytes = MAX_LFS_FILESIZE;
571 sb->s_op = &erofs_sops;
572
573 sbi->blkszbits = PAGE_SHIFT;
574 if (!sb->s_bdev) {
575 sb->s_blocksize = PAGE_SIZE;
576 sb->s_blocksize_bits = PAGE_SHIFT;
577
578 if (erofs_is_fscache_mode(sb)) {
579 err = erofs_fscache_register_fs(sb);
580 if (err)
581 return err;
582 }
583 err = super_setup_bdi(sb);
584 if (err)
585 return err;
586 } else {
587 if (!sb_set_blocksize(sb, PAGE_SIZE)) {
588 errorfc(fc, "failed to set initial blksize");
589 return -EINVAL;
590 }
591
592 sbi->dif0.dax_dev = fs_dax_get_by_bdev(sb->s_bdev,
593 &sbi->dif0.dax_part_off, NULL, NULL);
594 }
595
596 err = erofs_read_superblock(sb);
597 if (err)
598 return err;
599
600 if (sb->s_blocksize_bits != sbi->blkszbits) {
601 if (erofs_is_fscache_mode(sb)) {
602 errorfc(fc, "unsupported blksize for fscache mode");
603 return -EINVAL;
604 }
605
606 if (erofs_is_fileio_mode(sbi)) {
607 sb->s_blocksize = 1 << sbi->blkszbits;
608 sb->s_blocksize_bits = sbi->blkszbits;
609 } else if (!sb_set_blocksize(sb, 1 << sbi->blkszbits)) {
610 errorfc(fc, "failed to set erofs blksize");
611 return -EINVAL;
612 }
613 }
614
615 if (test_opt(&sbi->opt, DAX_ALWAYS)) {
616 if (!sbi->dif0.dax_dev) {
617 errorfc(fc, "DAX unsupported by block device. Turning off DAX.");
618 clear_opt(&sbi->opt, DAX_ALWAYS);
619 } else if (sbi->blkszbits != PAGE_SHIFT) {
620 errorfc(fc, "unsupported blocksize for DAX");
621 clear_opt(&sbi->opt, DAX_ALWAYS);
622 }
623 }
624
625 sb->s_time_gran = 1;
626 sb->s_xattr = erofs_xattr_handlers;
627 sb->s_export_op = &erofs_export_ops;
628
629 if (test_opt(&sbi->opt, POSIX_ACL))
630 sb->s_flags |= SB_POSIXACL;
631 else
632 sb->s_flags &= ~SB_POSIXACL;
633
634 #ifdef CONFIG_EROFS_FS_ZIP
635 xa_init(&sbi->managed_pslots);
636 #endif
637
638 inode = erofs_iget(sb, sbi->root_nid);
639 if (IS_ERR(inode))
640 return PTR_ERR(inode);
641
642 if (!S_ISDIR(inode->i_mode)) {
643 erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
644 sbi->root_nid, inode->i_mode);
645 iput(inode);
646 return -EINVAL;
647 }
648
649 sb->s_root = d_make_root(inode);
650 if (!sb->s_root)
651 return -ENOMEM;
652
653 erofs_shrinker_register(sb);
654 if (erofs_sb_has_fragments(sbi) && sbi->packed_nid) {
655 sbi->packed_inode = erofs_iget(sb, sbi->packed_nid);
656 if (IS_ERR(sbi->packed_inode)) {
657 err = PTR_ERR(sbi->packed_inode);
658 sbi->packed_inode = NULL;
659 return err;
660 }
661 }
662 err = erofs_init_managed_cache(sb);
663 if (err)
664 return err;
665
666 err = erofs_xattr_prefixes_init(sb);
667 if (err)
668 return err;
669
670 erofs_set_sysfs_name(sb);
671 err = erofs_register_sysfs(sb);
672 if (err)
673 return err;
674
675 erofs_info(sb, "mounted with root inode @ nid %llu.", sbi->root_nid);
676 return 0;
677 }
678
erofs_fc_get_tree(struct fs_context * fc)679 static int erofs_fc_get_tree(struct fs_context *fc)
680 {
681 struct erofs_sb_info *sbi = fc->s_fs_info;
682 int ret;
683
684 if (IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid)
685 return get_tree_nodev(fc, erofs_fc_fill_super);
686
687 ret = get_tree_bdev_flags(fc, erofs_fc_fill_super,
688 IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE) ?
689 GET_TREE_BDEV_QUIET_LOOKUP : 0);
690 #ifdef CONFIG_EROFS_FS_BACKED_BY_FILE
691 if (ret == -ENOTBLK) {
692 struct file *file;
693
694 if (!fc->source)
695 return invalf(fc, "No source specified");
696 file = filp_open(fc->source, O_RDONLY | O_LARGEFILE, 0);
697 if (IS_ERR(file))
698 return PTR_ERR(file);
699 sbi->dif0.file = file;
700
701 if (S_ISREG(file_inode(sbi->dif0.file)->i_mode) &&
702 sbi->dif0.file->f_mapping->a_ops->read_folio)
703 return get_tree_nodev(fc, erofs_fc_fill_super);
704 }
705 #endif
706 return ret;
707 }
708
erofs_fc_reconfigure(struct fs_context * fc)709 static int erofs_fc_reconfigure(struct fs_context *fc)
710 {
711 struct super_block *sb = fc->root->d_sb;
712 struct erofs_sb_info *sbi = EROFS_SB(sb);
713 struct erofs_sb_info *new_sbi = fc->s_fs_info;
714
715 DBG_BUGON(!sb_rdonly(sb));
716
717 if (new_sbi->fsid || new_sbi->domain_id)
718 erofs_info(sb, "ignoring reconfiguration for fsid|domain_id.");
719
720 if (test_opt(&new_sbi->opt, POSIX_ACL))
721 fc->sb_flags |= SB_POSIXACL;
722 else
723 fc->sb_flags &= ~SB_POSIXACL;
724
725 sbi->opt = new_sbi->opt;
726
727 fc->sb_flags |= SB_RDONLY;
728 return 0;
729 }
730
erofs_release_device_info(int id,void * ptr,void * data)731 static int erofs_release_device_info(int id, void *ptr, void *data)
732 {
733 struct erofs_device_info *dif = ptr;
734
735 fs_put_dax(dif->dax_dev, NULL);
736 if (dif->file)
737 fput(dif->file);
738 erofs_fscache_unregister_cookie(dif->fscache);
739 dif->fscache = NULL;
740 kfree(dif->path);
741 kfree(dif);
742 return 0;
743 }
744
erofs_free_dev_context(struct erofs_dev_context * devs)745 static void erofs_free_dev_context(struct erofs_dev_context *devs)
746 {
747 if (!devs)
748 return;
749 idr_for_each(&devs->tree, &erofs_release_device_info, NULL);
750 idr_destroy(&devs->tree);
751 kfree(devs);
752 }
753
erofs_sb_free(struct erofs_sb_info * sbi)754 static void erofs_sb_free(struct erofs_sb_info *sbi)
755 {
756 erofs_free_dev_context(sbi->devs);
757 kfree(sbi->fsid);
758 kfree(sbi->domain_id);
759 if (sbi->dif0.file)
760 fput(sbi->dif0.file);
761 kfree(sbi);
762 }
763
erofs_fc_free(struct fs_context * fc)764 static void erofs_fc_free(struct fs_context *fc)
765 {
766 struct erofs_sb_info *sbi = fc->s_fs_info;
767
768 if (sbi) /* free here if an error occurs before transferring to sb */
769 erofs_sb_free(sbi);
770 }
771
772 static const struct fs_context_operations erofs_context_ops = {
773 .parse_param = erofs_fc_parse_param,
774 .get_tree = erofs_fc_get_tree,
775 .reconfigure = erofs_fc_reconfigure,
776 .free = erofs_fc_free,
777 };
778
erofs_init_fs_context(struct fs_context * fc)779 static int erofs_init_fs_context(struct fs_context *fc)
780 {
781 struct erofs_sb_info *sbi;
782
783 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
784 if (!sbi)
785 return -ENOMEM;
786
787 sbi->devs = kzalloc(sizeof(struct erofs_dev_context), GFP_KERNEL);
788 if (!sbi->devs) {
789 kfree(sbi);
790 return -ENOMEM;
791 }
792 fc->s_fs_info = sbi;
793
794 idr_init(&sbi->devs->tree);
795 init_rwsem(&sbi->devs->rwsem);
796 erofs_default_options(sbi);
797 fc->ops = &erofs_context_ops;
798 return 0;
799 }
800
erofs_kill_sb(struct super_block * sb)801 static void erofs_kill_sb(struct super_block *sb)
802 {
803 struct erofs_sb_info *sbi = EROFS_SB(sb);
804
805 if ((IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid) ||
806 sbi->dif0.file)
807 kill_anon_super(sb);
808 else
809 kill_block_super(sb);
810 fs_put_dax(sbi->dif0.dax_dev, NULL);
811 erofs_fscache_unregister_fs(sb);
812 erofs_sb_free(sbi);
813 sb->s_fs_info = NULL;
814 }
815
erofs_put_super(struct super_block * sb)816 static void erofs_put_super(struct super_block *sb)
817 {
818 struct erofs_sb_info *const sbi = EROFS_SB(sb);
819
820 DBG_BUGON(!sbi);
821
822 erofs_unregister_sysfs(sb);
823 erofs_shrinker_unregister(sb);
824 erofs_xattr_prefixes_cleanup(sb);
825 #ifdef CONFIG_EROFS_FS_ZIP
826 iput(sbi->managed_cache);
827 sbi->managed_cache = NULL;
828 #endif
829 iput(sbi->packed_inode);
830 sbi->packed_inode = NULL;
831 erofs_free_dev_context(sbi->devs);
832 sbi->devs = NULL;
833 erofs_fscache_unregister_fs(sb);
834 }
835
836 static struct file_system_type erofs_fs_type = {
837 .owner = THIS_MODULE,
838 .name = "erofs",
839 .init_fs_context = erofs_init_fs_context,
840 .kill_sb = erofs_kill_sb,
841 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
842 };
843 MODULE_ALIAS_FS("erofs");
844
erofs_module_init(void)845 static int __init erofs_module_init(void)
846 {
847 int err;
848
849 erofs_check_ondisk_layout_definitions();
850
851 erofs_inode_cachep = kmem_cache_create("erofs_inode",
852 sizeof(struct erofs_inode), 0,
853 SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
854 erofs_inode_init_once);
855 if (!erofs_inode_cachep)
856 return -ENOMEM;
857
858 err = erofs_init_shrinker();
859 if (err)
860 goto shrinker_err;
861
862 err = z_erofs_init_subsystem();
863 if (err)
864 goto zip_err;
865
866 err = erofs_init_sysfs();
867 if (err)
868 goto sysfs_err;
869
870 err = register_filesystem(&erofs_fs_type);
871 if (err)
872 goto fs_err;
873
874 return 0;
875
876 fs_err:
877 erofs_exit_sysfs();
878 sysfs_err:
879 z_erofs_exit_subsystem();
880 zip_err:
881 erofs_exit_shrinker();
882 shrinker_err:
883 kmem_cache_destroy(erofs_inode_cachep);
884 return err;
885 }
886
erofs_module_exit(void)887 static void __exit erofs_module_exit(void)
888 {
889 unregister_filesystem(&erofs_fs_type);
890
891 /* Ensure all RCU free inodes / pclusters are safe to be destroyed. */
892 rcu_barrier();
893
894 erofs_exit_sysfs();
895 z_erofs_exit_subsystem();
896 erofs_exit_shrinker();
897 kmem_cache_destroy(erofs_inode_cachep);
898 }
899
erofs_statfs(struct dentry * dentry,struct kstatfs * buf)900 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
901 {
902 struct super_block *sb = dentry->d_sb;
903 struct erofs_sb_info *sbi = EROFS_SB(sb);
904
905 buf->f_type = sb->s_magic;
906 buf->f_bsize = sb->s_blocksize;
907 buf->f_blocks = sbi->total_blocks;
908 buf->f_bfree = buf->f_bavail = 0;
909 buf->f_files = ULLONG_MAX;
910 buf->f_ffree = ULLONG_MAX - sbi->inos;
911 buf->f_namelen = EROFS_NAME_LEN;
912
913 if (uuid_is_null(&sb->s_uuid))
914 buf->f_fsid = u64_to_fsid(!sb->s_bdev ? 0 :
915 huge_encode_dev(sb->s_bdev->bd_dev));
916 else
917 buf->f_fsid = uuid_to_fsid(sb->s_uuid.b);
918 return 0;
919 }
920
erofs_show_options(struct seq_file * seq,struct dentry * root)921 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
922 {
923 struct erofs_sb_info *sbi = EROFS_SB(root->d_sb);
924 struct erofs_mount_opts *opt = &sbi->opt;
925
926 if (IS_ENABLED(CONFIG_EROFS_FS_XATTR))
927 seq_puts(seq, test_opt(opt, XATTR_USER) ?
928 ",user_xattr" : ",nouser_xattr");
929 if (IS_ENABLED(CONFIG_EROFS_FS_POSIX_ACL))
930 seq_puts(seq, test_opt(opt, POSIX_ACL) ? ",acl" : ",noacl");
931 if (IS_ENABLED(CONFIG_EROFS_FS_ZIP))
932 seq_printf(seq, ",cache_strategy=%s",
933 erofs_param_cache_strategy[opt->cache_strategy].name);
934 if (test_opt(opt, DAX_ALWAYS))
935 seq_puts(seq, ",dax=always");
936 if (test_opt(opt, DAX_NEVER))
937 seq_puts(seq, ",dax=never");
938 if (erofs_is_fileio_mode(sbi) && test_opt(opt, DIRECT_IO))
939 seq_puts(seq, ",directio");
940 #ifdef CONFIG_EROFS_FS_ONDEMAND
941 if (sbi->fsid)
942 seq_printf(seq, ",fsid=%s", sbi->fsid);
943 if (sbi->domain_id)
944 seq_printf(seq, ",domain_id=%s", sbi->domain_id);
945 #endif
946 return 0;
947 }
948
949 const struct super_operations erofs_sops = {
950 .put_super = erofs_put_super,
951 .alloc_inode = erofs_alloc_inode,
952 .free_inode = erofs_free_inode,
953 .statfs = erofs_statfs,
954 .show_options = erofs_show_options,
955 };
956
957 module_init(erofs_module_init);
958 module_exit(erofs_module_exit);
959
960 MODULE_DESCRIPTION("Enhanced ROM File System");
961 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
962 MODULE_LICENSE("GPL");
963