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 ret = strscpy(sbi->volume_name, dsb->volume_name,
321 sizeof(dsb->volume_name));
322 if (ret < 0) { /* -E2BIG */
323 erofs_err(sb, "bad volume name without NIL terminator");
324 ret = -EFSCORRUPTED;
325 goto out;
326 }
327
328 /* parse on-disk compression configurations */
329 ret = z_erofs_parse_cfgs(sb, dsb);
330 if (ret < 0)
331 goto out;
332
333 /* handle multiple devices */
334 ret = erofs_scan_devices(sb, dsb);
335
336 if (erofs_is_fscache_mode(sb))
337 erofs_info(sb, "[deprecated] fscache-based on-demand read feature in use. Use at your own risk!");
338 out:
339 erofs_put_metabuf(&buf);
340 return ret;
341 }
342
erofs_default_options(struct erofs_sb_info * sbi)343 static void erofs_default_options(struct erofs_sb_info *sbi)
344 {
345 #ifdef CONFIG_EROFS_FS_ZIP
346 sbi->opt.cache_strategy = EROFS_ZIP_CACHE_READAROUND;
347 sbi->opt.max_sync_decompress_pages = 3;
348 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_AUTO;
349 #endif
350 #ifdef CONFIG_EROFS_FS_XATTR
351 set_opt(&sbi->opt, XATTR_USER);
352 #endif
353 #ifdef CONFIG_EROFS_FS_POSIX_ACL
354 set_opt(&sbi->opt, POSIX_ACL);
355 #endif
356 }
357
358 enum {
359 Opt_user_xattr, Opt_acl, Opt_cache_strategy, Opt_dax, Opt_dax_enum,
360 Opt_device, Opt_fsid, Opt_domain_id, Opt_directio,
361 Opt_err
362 };
363
364 static const struct constant_table erofs_param_cache_strategy[] = {
365 {"disabled", EROFS_ZIP_CACHE_DISABLED},
366 {"readahead", EROFS_ZIP_CACHE_READAHEAD},
367 {"readaround", EROFS_ZIP_CACHE_READAROUND},
368 {}
369 };
370
371 static const struct constant_table erofs_dax_param_enums[] = {
372 {"always", EROFS_MOUNT_DAX_ALWAYS},
373 {"never", EROFS_MOUNT_DAX_NEVER},
374 {}
375 };
376
377 static const struct fs_parameter_spec erofs_fs_parameters[] = {
378 fsparam_flag_no("user_xattr", Opt_user_xattr),
379 fsparam_flag_no("acl", Opt_acl),
380 fsparam_enum("cache_strategy", Opt_cache_strategy,
381 erofs_param_cache_strategy),
382 fsparam_flag("dax", Opt_dax),
383 fsparam_enum("dax", Opt_dax_enum, erofs_dax_param_enums),
384 fsparam_string("device", Opt_device),
385 fsparam_string("fsid", Opt_fsid),
386 fsparam_string("domain_id", Opt_domain_id),
387 fsparam_flag_no("directio", Opt_directio),
388 {}
389 };
390
erofs_fc_set_dax_mode(struct fs_context * fc,unsigned int mode)391 static bool erofs_fc_set_dax_mode(struct fs_context *fc, unsigned int mode)
392 {
393 #ifdef CONFIG_FS_DAX
394 struct erofs_sb_info *sbi = fc->s_fs_info;
395
396 switch (mode) {
397 case EROFS_MOUNT_DAX_ALWAYS:
398 set_opt(&sbi->opt, DAX_ALWAYS);
399 clear_opt(&sbi->opt, DAX_NEVER);
400 return true;
401 case EROFS_MOUNT_DAX_NEVER:
402 set_opt(&sbi->opt, DAX_NEVER);
403 clear_opt(&sbi->opt, DAX_ALWAYS);
404 return true;
405 default:
406 DBG_BUGON(1);
407 return false;
408 }
409 #else
410 errorfc(fc, "dax options not supported");
411 return false;
412 #endif
413 }
414
erofs_fc_parse_param(struct fs_context * fc,struct fs_parameter * param)415 static int erofs_fc_parse_param(struct fs_context *fc,
416 struct fs_parameter *param)
417 {
418 struct erofs_sb_info *sbi = fc->s_fs_info;
419 struct fs_parse_result result;
420 struct erofs_device_info *dif;
421 int opt, ret;
422
423 opt = fs_parse(fc, erofs_fs_parameters, param, &result);
424 if (opt < 0)
425 return opt;
426
427 switch (opt) {
428 case Opt_user_xattr:
429 #ifdef CONFIG_EROFS_FS_XATTR
430 if (result.boolean)
431 set_opt(&sbi->opt, XATTR_USER);
432 else
433 clear_opt(&sbi->opt, XATTR_USER);
434 #else
435 errorfc(fc, "{,no}user_xattr options not supported");
436 #endif
437 break;
438 case Opt_acl:
439 #ifdef CONFIG_EROFS_FS_POSIX_ACL
440 if (result.boolean)
441 set_opt(&sbi->opt, POSIX_ACL);
442 else
443 clear_opt(&sbi->opt, POSIX_ACL);
444 #else
445 errorfc(fc, "{,no}acl options not supported");
446 #endif
447 break;
448 case Opt_cache_strategy:
449 #ifdef CONFIG_EROFS_FS_ZIP
450 sbi->opt.cache_strategy = result.uint_32;
451 #else
452 errorfc(fc, "compression not supported, cache_strategy ignored");
453 #endif
454 break;
455 case Opt_dax:
456 if (!erofs_fc_set_dax_mode(fc, EROFS_MOUNT_DAX_ALWAYS))
457 return -EINVAL;
458 break;
459 case Opt_dax_enum:
460 if (!erofs_fc_set_dax_mode(fc, result.uint_32))
461 return -EINVAL;
462 break;
463 case Opt_device:
464 dif = kzalloc(sizeof(*dif), GFP_KERNEL);
465 if (!dif)
466 return -ENOMEM;
467 dif->path = kstrdup(param->string, GFP_KERNEL);
468 if (!dif->path) {
469 kfree(dif);
470 return -ENOMEM;
471 }
472 down_write(&sbi->devs->rwsem);
473 ret = idr_alloc(&sbi->devs->tree, dif, 0, 0, GFP_KERNEL);
474 up_write(&sbi->devs->rwsem);
475 if (ret < 0) {
476 kfree(dif->path);
477 kfree(dif);
478 return ret;
479 }
480 ++sbi->devs->extra_devices;
481 break;
482 #ifdef CONFIG_EROFS_FS_ONDEMAND
483 case Opt_fsid:
484 kfree(sbi->fsid);
485 sbi->fsid = kstrdup(param->string, GFP_KERNEL);
486 if (!sbi->fsid)
487 return -ENOMEM;
488 break;
489 case Opt_domain_id:
490 kfree(sbi->domain_id);
491 sbi->domain_id = kstrdup(param->string, GFP_KERNEL);
492 if (!sbi->domain_id)
493 return -ENOMEM;
494 break;
495 #else
496 case Opt_fsid:
497 case Opt_domain_id:
498 errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name);
499 break;
500 #endif
501 case Opt_directio:
502 #ifdef CONFIG_EROFS_FS_BACKED_BY_FILE
503 if (result.boolean)
504 set_opt(&sbi->opt, DIRECT_IO);
505 else
506 clear_opt(&sbi->opt, DIRECT_IO);
507 #else
508 errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name);
509 #endif
510 break;
511 }
512 return 0;
513 }
514
erofs_nfs_get_inode(struct super_block * sb,u64 ino,u32 generation)515 static struct inode *erofs_nfs_get_inode(struct super_block *sb,
516 u64 ino, u32 generation)
517 {
518 return erofs_iget(sb, ino);
519 }
520
erofs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)521 static struct dentry *erofs_fh_to_dentry(struct super_block *sb,
522 struct fid *fid, int fh_len, int fh_type)
523 {
524 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
525 erofs_nfs_get_inode);
526 }
527
erofs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)528 static struct dentry *erofs_fh_to_parent(struct super_block *sb,
529 struct fid *fid, int fh_len, int fh_type)
530 {
531 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
532 erofs_nfs_get_inode);
533 }
534
erofs_get_parent(struct dentry * child)535 static struct dentry *erofs_get_parent(struct dentry *child)
536 {
537 erofs_nid_t nid;
538 unsigned int d_type;
539 int err;
540
541 err = erofs_namei(d_inode(child), &dotdot_name, &nid, &d_type);
542 if (err)
543 return ERR_PTR(err);
544 return d_obtain_alias(erofs_iget(child->d_sb, nid));
545 }
546
547 static const struct export_operations erofs_export_ops = {
548 .encode_fh = generic_encode_ino32_fh,
549 .fh_to_dentry = erofs_fh_to_dentry,
550 .fh_to_parent = erofs_fh_to_parent,
551 .get_parent = erofs_get_parent,
552 };
553
erofs_set_sysfs_name(struct super_block * sb)554 static void erofs_set_sysfs_name(struct super_block *sb)
555 {
556 struct erofs_sb_info *sbi = EROFS_SB(sb);
557
558 if (sbi->domain_id)
559 super_set_sysfs_name_generic(sb, "%s,%s", sbi->domain_id,
560 sbi->fsid);
561 else if (sbi->fsid)
562 super_set_sysfs_name_generic(sb, "%s", sbi->fsid);
563 else if (erofs_is_fileio_mode(sbi))
564 super_set_sysfs_name_generic(sb, "%s",
565 bdi_dev_name(sb->s_bdi));
566 else
567 super_set_sysfs_name_id(sb);
568 }
569
erofs_fc_fill_super(struct super_block * sb,struct fs_context * fc)570 static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
571 {
572 struct inode *inode;
573 struct erofs_sb_info *sbi = EROFS_SB(sb);
574 int err;
575
576 sb->s_magic = EROFS_SUPER_MAGIC;
577 sb->s_flags |= SB_RDONLY | SB_NOATIME;
578 sb->s_maxbytes = MAX_LFS_FILESIZE;
579 sb->s_op = &erofs_sops;
580
581 sbi->blkszbits = PAGE_SHIFT;
582 if (!sb->s_bdev) {
583 sb->s_blocksize = PAGE_SIZE;
584 sb->s_blocksize_bits = PAGE_SHIFT;
585
586 if (erofs_is_fscache_mode(sb)) {
587 err = erofs_fscache_register_fs(sb);
588 if (err)
589 return err;
590 }
591 err = super_setup_bdi(sb);
592 if (err)
593 return err;
594 } else {
595 if (!sb_set_blocksize(sb, PAGE_SIZE)) {
596 errorfc(fc, "failed to set initial blksize");
597 return -EINVAL;
598 }
599
600 sbi->dif0.dax_dev = fs_dax_get_by_bdev(sb->s_bdev,
601 &sbi->dif0.dax_part_off, NULL, NULL);
602 }
603
604 err = erofs_read_superblock(sb);
605 if (err)
606 return err;
607
608 if (sb->s_blocksize_bits != sbi->blkszbits) {
609 if (erofs_is_fscache_mode(sb)) {
610 errorfc(fc, "unsupported blksize for fscache mode");
611 return -EINVAL;
612 }
613
614 if (erofs_is_fileio_mode(sbi)) {
615 sb->s_blocksize = 1 << sbi->blkszbits;
616 sb->s_blocksize_bits = sbi->blkszbits;
617 } else if (!sb_set_blocksize(sb, 1 << sbi->blkszbits)) {
618 errorfc(fc, "failed to set erofs blksize");
619 return -EINVAL;
620 }
621 }
622
623 if (test_opt(&sbi->opt, DAX_ALWAYS)) {
624 if (!sbi->dif0.dax_dev) {
625 errorfc(fc, "DAX unsupported by block device. Turning off DAX.");
626 clear_opt(&sbi->opt, DAX_ALWAYS);
627 } else if (sbi->blkszbits != PAGE_SHIFT) {
628 errorfc(fc, "unsupported blocksize for DAX");
629 clear_opt(&sbi->opt, DAX_ALWAYS);
630 }
631 }
632
633 sb->s_time_gran = 1;
634 sb->s_xattr = erofs_xattr_handlers;
635 sb->s_export_op = &erofs_export_ops;
636
637 if (test_opt(&sbi->opt, POSIX_ACL))
638 sb->s_flags |= SB_POSIXACL;
639 else
640 sb->s_flags &= ~SB_POSIXACL;
641
642 #ifdef CONFIG_EROFS_FS_ZIP
643 xa_init(&sbi->managed_pslots);
644 #endif
645
646 inode = erofs_iget(sb, sbi->root_nid);
647 if (IS_ERR(inode))
648 return PTR_ERR(inode);
649
650 if (!S_ISDIR(inode->i_mode)) {
651 erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
652 sbi->root_nid, inode->i_mode);
653 iput(inode);
654 return -EINVAL;
655 }
656
657 sb->s_root = d_make_root(inode);
658 if (!sb->s_root)
659 return -ENOMEM;
660
661 erofs_shrinker_register(sb);
662 if (erofs_sb_has_fragments(sbi) && sbi->packed_nid) {
663 sbi->packed_inode = erofs_iget(sb, sbi->packed_nid);
664 if (IS_ERR(sbi->packed_inode)) {
665 err = PTR_ERR(sbi->packed_inode);
666 sbi->packed_inode = NULL;
667 return err;
668 }
669 }
670 err = erofs_init_managed_cache(sb);
671 if (err)
672 return err;
673
674 err = erofs_xattr_prefixes_init(sb);
675 if (err)
676 return err;
677
678 erofs_set_sysfs_name(sb);
679 err = erofs_register_sysfs(sb);
680 if (err)
681 return err;
682
683 erofs_info(sb, "mounted with root inode @ nid %llu.", sbi->root_nid);
684 return 0;
685 }
686
erofs_fc_get_tree(struct fs_context * fc)687 static int erofs_fc_get_tree(struct fs_context *fc)
688 {
689 struct erofs_sb_info *sbi = fc->s_fs_info;
690 int ret;
691
692 if (IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid)
693 return get_tree_nodev(fc, erofs_fc_fill_super);
694
695 ret = get_tree_bdev_flags(fc, erofs_fc_fill_super,
696 IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE) ?
697 GET_TREE_BDEV_QUIET_LOOKUP : 0);
698 #ifdef CONFIG_EROFS_FS_BACKED_BY_FILE
699 if (ret == -ENOTBLK) {
700 struct file *file;
701
702 if (!fc->source)
703 return invalf(fc, "No source specified");
704 file = filp_open(fc->source, O_RDONLY | O_LARGEFILE, 0);
705 if (IS_ERR(file))
706 return PTR_ERR(file);
707 sbi->dif0.file = file;
708
709 if (S_ISREG(file_inode(sbi->dif0.file)->i_mode) &&
710 sbi->dif0.file->f_mapping->a_ops->read_folio)
711 return get_tree_nodev(fc, erofs_fc_fill_super);
712 }
713 #endif
714 return ret;
715 }
716
erofs_fc_reconfigure(struct fs_context * fc)717 static int erofs_fc_reconfigure(struct fs_context *fc)
718 {
719 struct super_block *sb = fc->root->d_sb;
720 struct erofs_sb_info *sbi = EROFS_SB(sb);
721 struct erofs_sb_info *new_sbi = fc->s_fs_info;
722
723 DBG_BUGON(!sb_rdonly(sb));
724
725 if (new_sbi->fsid || new_sbi->domain_id)
726 erofs_info(sb, "ignoring reconfiguration for fsid|domain_id.");
727
728 if (test_opt(&new_sbi->opt, POSIX_ACL))
729 fc->sb_flags |= SB_POSIXACL;
730 else
731 fc->sb_flags &= ~SB_POSIXACL;
732
733 sbi->opt = new_sbi->opt;
734
735 fc->sb_flags |= SB_RDONLY;
736 return 0;
737 }
738
erofs_release_device_info(int id,void * ptr,void * data)739 static int erofs_release_device_info(int id, void *ptr, void *data)
740 {
741 struct erofs_device_info *dif = ptr;
742
743 fs_put_dax(dif->dax_dev, NULL);
744 if (dif->file)
745 fput(dif->file);
746 erofs_fscache_unregister_cookie(dif->fscache);
747 dif->fscache = NULL;
748 kfree(dif->path);
749 kfree(dif);
750 return 0;
751 }
752
erofs_free_dev_context(struct erofs_dev_context * devs)753 static void erofs_free_dev_context(struct erofs_dev_context *devs)
754 {
755 if (!devs)
756 return;
757 idr_for_each(&devs->tree, &erofs_release_device_info, NULL);
758 idr_destroy(&devs->tree);
759 kfree(devs);
760 }
761
erofs_sb_free(struct erofs_sb_info * sbi)762 static void erofs_sb_free(struct erofs_sb_info *sbi)
763 {
764 erofs_free_dev_context(sbi->devs);
765 kfree(sbi->fsid);
766 kfree(sbi->domain_id);
767 if (sbi->dif0.file)
768 fput(sbi->dif0.file);
769 kfree(sbi);
770 }
771
erofs_fc_free(struct fs_context * fc)772 static void erofs_fc_free(struct fs_context *fc)
773 {
774 struct erofs_sb_info *sbi = fc->s_fs_info;
775
776 if (sbi) /* free here if an error occurs before transferring to sb */
777 erofs_sb_free(sbi);
778 }
779
780 static const struct fs_context_operations erofs_context_ops = {
781 .parse_param = erofs_fc_parse_param,
782 .get_tree = erofs_fc_get_tree,
783 .reconfigure = erofs_fc_reconfigure,
784 .free = erofs_fc_free,
785 };
786
erofs_init_fs_context(struct fs_context * fc)787 static int erofs_init_fs_context(struct fs_context *fc)
788 {
789 struct erofs_sb_info *sbi;
790
791 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
792 if (!sbi)
793 return -ENOMEM;
794
795 sbi->devs = kzalloc(sizeof(struct erofs_dev_context), GFP_KERNEL);
796 if (!sbi->devs) {
797 kfree(sbi);
798 return -ENOMEM;
799 }
800 fc->s_fs_info = sbi;
801
802 idr_init(&sbi->devs->tree);
803 init_rwsem(&sbi->devs->rwsem);
804 erofs_default_options(sbi);
805 fc->ops = &erofs_context_ops;
806 return 0;
807 }
808
erofs_kill_sb(struct super_block * sb)809 static void erofs_kill_sb(struct super_block *sb)
810 {
811 struct erofs_sb_info *sbi = EROFS_SB(sb);
812
813 if ((IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid) ||
814 sbi->dif0.file)
815 kill_anon_super(sb);
816 else
817 kill_block_super(sb);
818 fs_put_dax(sbi->dif0.dax_dev, NULL);
819 erofs_fscache_unregister_fs(sb);
820 erofs_sb_free(sbi);
821 sb->s_fs_info = NULL;
822 }
823
erofs_put_super(struct super_block * sb)824 static void erofs_put_super(struct super_block *sb)
825 {
826 struct erofs_sb_info *const sbi = EROFS_SB(sb);
827
828 DBG_BUGON(!sbi);
829
830 erofs_unregister_sysfs(sb);
831 erofs_shrinker_unregister(sb);
832 erofs_xattr_prefixes_cleanup(sb);
833 #ifdef CONFIG_EROFS_FS_ZIP
834 iput(sbi->managed_cache);
835 sbi->managed_cache = NULL;
836 #endif
837 iput(sbi->packed_inode);
838 sbi->packed_inode = NULL;
839 erofs_free_dev_context(sbi->devs);
840 sbi->devs = NULL;
841 erofs_fscache_unregister_fs(sb);
842 }
843
844 static struct file_system_type erofs_fs_type = {
845 .owner = THIS_MODULE,
846 .name = "erofs",
847 .init_fs_context = erofs_init_fs_context,
848 .kill_sb = erofs_kill_sb,
849 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
850 };
851 MODULE_ALIAS_FS("erofs");
852
erofs_module_init(void)853 static int __init erofs_module_init(void)
854 {
855 int err;
856
857 erofs_check_ondisk_layout_definitions();
858
859 erofs_inode_cachep = kmem_cache_create("erofs_inode",
860 sizeof(struct erofs_inode), 0,
861 SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
862 erofs_inode_init_once);
863 if (!erofs_inode_cachep)
864 return -ENOMEM;
865
866 err = erofs_init_shrinker();
867 if (err)
868 goto shrinker_err;
869
870 err = z_erofs_init_subsystem();
871 if (err)
872 goto zip_err;
873
874 err = erofs_init_sysfs();
875 if (err)
876 goto sysfs_err;
877
878 err = register_filesystem(&erofs_fs_type);
879 if (err)
880 goto fs_err;
881
882 return 0;
883
884 fs_err:
885 erofs_exit_sysfs();
886 sysfs_err:
887 z_erofs_exit_subsystem();
888 zip_err:
889 erofs_exit_shrinker();
890 shrinker_err:
891 kmem_cache_destroy(erofs_inode_cachep);
892 return err;
893 }
894
erofs_module_exit(void)895 static void __exit erofs_module_exit(void)
896 {
897 unregister_filesystem(&erofs_fs_type);
898
899 /* Ensure all RCU free inodes / pclusters are safe to be destroyed. */
900 rcu_barrier();
901
902 erofs_exit_sysfs();
903 z_erofs_exit_subsystem();
904 erofs_exit_shrinker();
905 kmem_cache_destroy(erofs_inode_cachep);
906 }
907
erofs_statfs(struct dentry * dentry,struct kstatfs * buf)908 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
909 {
910 struct super_block *sb = dentry->d_sb;
911 struct erofs_sb_info *sbi = EROFS_SB(sb);
912
913 buf->f_type = sb->s_magic;
914 buf->f_bsize = sb->s_blocksize;
915 buf->f_blocks = sbi->total_blocks;
916 buf->f_bfree = buf->f_bavail = 0;
917 buf->f_files = ULLONG_MAX;
918 buf->f_ffree = ULLONG_MAX - sbi->inos;
919 buf->f_namelen = EROFS_NAME_LEN;
920
921 if (uuid_is_null(&sb->s_uuid))
922 buf->f_fsid = u64_to_fsid(!sb->s_bdev ? 0 :
923 huge_encode_dev(sb->s_bdev->bd_dev));
924 else
925 buf->f_fsid = uuid_to_fsid(sb->s_uuid.b);
926 return 0;
927 }
928
erofs_show_options(struct seq_file * seq,struct dentry * root)929 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
930 {
931 struct erofs_sb_info *sbi = EROFS_SB(root->d_sb);
932 struct erofs_mount_opts *opt = &sbi->opt;
933
934 if (IS_ENABLED(CONFIG_EROFS_FS_XATTR))
935 seq_puts(seq, test_opt(opt, XATTR_USER) ?
936 ",user_xattr" : ",nouser_xattr");
937 if (IS_ENABLED(CONFIG_EROFS_FS_POSIX_ACL))
938 seq_puts(seq, test_opt(opt, POSIX_ACL) ? ",acl" : ",noacl");
939 if (IS_ENABLED(CONFIG_EROFS_FS_ZIP))
940 seq_printf(seq, ",cache_strategy=%s",
941 erofs_param_cache_strategy[opt->cache_strategy].name);
942 if (test_opt(opt, DAX_ALWAYS))
943 seq_puts(seq, ",dax=always");
944 if (test_opt(opt, DAX_NEVER))
945 seq_puts(seq, ",dax=never");
946 if (erofs_is_fileio_mode(sbi) && test_opt(opt, DIRECT_IO))
947 seq_puts(seq, ",directio");
948 #ifdef CONFIG_EROFS_FS_ONDEMAND
949 if (sbi->fsid)
950 seq_printf(seq, ",fsid=%s", sbi->fsid);
951 if (sbi->domain_id)
952 seq_printf(seq, ",domain_id=%s", sbi->domain_id);
953 #endif
954 return 0;
955 }
956
957 const struct super_operations erofs_sops = {
958 .put_super = erofs_put_super,
959 .alloc_inode = erofs_alloc_inode,
960 .free_inode = erofs_free_inode,
961 .statfs = erofs_statfs,
962 .show_options = erofs_show_options,
963 };
964
965 module_init(erofs_module_init);
966 module_exit(erofs_module_exit);
967
968 MODULE_DESCRIPTION("Enhanced ROM File System");
969 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
970 MODULE_LICENSE("GPL");
971