Gao Xiang | 29b24f6 | 2019-07-31 23:57:31 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 2 | /* |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 3 | * Copyright (C) 2017-2018 HUAWEI, Inc. |
Alexander A. Klimov | 592e7cd | 2020-07-13 15:09:44 +0200 | [diff] [blame] | 4 | * https://www.huawei.com/ |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 5 | * Created by Gao Xiang <gaoxiang25@huawei.com> |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 6 | */ |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/buffer_head.h> |
| 9 | #include <linux/statfs.h> |
| 10 | #include <linux/parser.h> |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 11 | #include <linux/seq_file.h> |
Pratik Shinde | b858a48 | 2019-11-04 10:49:37 +0800 | [diff] [blame] | 12 | #include <linux/crc32c.h> |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 13 | #include <linux/fs_context.h> |
| 14 | #include <linux/fs_parser.h> |
Gao Xiang | 6af7b48 | 2019-01-14 19:40:25 +0800 | [diff] [blame] | 15 | #include "xattr.h" |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 16 | |
Chao Yu | 13f06f4 | 2018-07-26 20:21:55 +0800 | [diff] [blame] | 17 | #define CREATE_TRACE_POINTS |
| 18 | #include <trace/events/erofs.h> |
| 19 | |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 20 | static struct kmem_cache *erofs_inode_cachep __read_mostly; |
| 21 | |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 22 | void _erofs_err(struct super_block *sb, const char *function, |
| 23 | const char *fmt, ...) |
| 24 | { |
| 25 | struct va_format vaf; |
| 26 | va_list args; |
| 27 | |
| 28 | va_start(args, fmt); |
| 29 | |
| 30 | vaf.fmt = fmt; |
| 31 | vaf.va = &args; |
| 32 | |
| 33 | pr_err("(device %s): %s: %pV", sb->s_id, function, &vaf); |
| 34 | va_end(args); |
| 35 | } |
| 36 | |
| 37 | void _erofs_info(struct super_block *sb, const char *function, |
| 38 | const char *fmt, ...) |
| 39 | { |
| 40 | struct va_format vaf; |
| 41 | va_list args; |
| 42 | |
| 43 | va_start(args, fmt); |
| 44 | |
| 45 | vaf.fmt = fmt; |
| 46 | vaf.va = &args; |
| 47 | |
| 48 | pr_info("(device %s): %pV", sb->s_id, &vaf); |
| 49 | va_end(args); |
| 50 | } |
| 51 | |
Pratik Shinde | b858a48 | 2019-11-04 10:49:37 +0800 | [diff] [blame] | 52 | static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata) |
| 53 | { |
| 54 | struct erofs_super_block *dsb; |
| 55 | u32 expected_crc, crc; |
| 56 | |
| 57 | dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET, |
| 58 | EROFS_BLKSIZ - EROFS_SUPER_OFFSET, GFP_KERNEL); |
| 59 | if (!dsb) |
| 60 | return -ENOMEM; |
| 61 | |
| 62 | expected_crc = le32_to_cpu(dsb->checksum); |
| 63 | dsb->checksum = 0; |
| 64 | /* to allow for x86 boot sectors and other oddities. */ |
| 65 | crc = crc32c(~0, dsb, EROFS_BLKSIZ - EROFS_SUPER_OFFSET); |
| 66 | kfree(dsb); |
| 67 | |
| 68 | if (crc != expected_crc) { |
| 69 | erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected", |
| 70 | crc, expected_crc); |
| 71 | return -EBADMSG; |
| 72 | } |
| 73 | return 0; |
| 74 | } |
| 75 | |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 76 | static void erofs_inode_init_once(void *ptr) |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 77 | { |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 78 | struct erofs_inode *vi = ptr; |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 79 | |
| 80 | inode_init_once(&vi->vfs_inode); |
| 81 | } |
| 82 | |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 83 | static struct inode *erofs_alloc_inode(struct super_block *sb) |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 84 | { |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 85 | struct erofs_inode *vi = |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 86 | kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL); |
| 87 | |
Vatsala Narang | e2ff9f1 | 2019-03-21 05:36:13 +0530 | [diff] [blame] | 88 | if (!vi) |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 89 | return NULL; |
| 90 | |
| 91 | /* zero out everything except vfs_inode */ |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 92 | memset(vi, 0, offsetof(struct erofs_inode, vfs_inode)); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 93 | return &vi->vfs_inode; |
| 94 | } |
| 95 | |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 96 | static void erofs_free_inode(struct inode *inode) |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 97 | { |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 98 | struct erofs_inode *vi = EROFS_I(inode); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 99 | |
Gao Xiang | a2c75c8 | 2019-09-04 10:08:59 +0800 | [diff] [blame] | 100 | /* be careful of RCU symlink path */ |
| 101 | if (inode->i_op == &erofs_fast_symlink_iops) |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 102 | kfree(inode->i_link); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 103 | kfree(vi->xattr_shared_xattrs); |
| 104 | |
| 105 | kmem_cache_free(erofs_inode_cachep, vi); |
| 106 | } |
| 107 | |
Gao Xiang | 5efe513 | 2019-06-13 16:35:41 +0800 | [diff] [blame] | 108 | static bool check_layout_compatibility(struct super_block *sb, |
Gao Xiang | 0259f20 | 2019-09-04 10:09:00 +0800 | [diff] [blame] | 109 | struct erofs_super_block *dsb) |
Gao Xiang | 5efe513 | 2019-06-13 16:35:41 +0800 | [diff] [blame] | 110 | { |
Gao Xiang | 0259f20 | 2019-09-04 10:09:00 +0800 | [diff] [blame] | 111 | const unsigned int feature = le32_to_cpu(dsb->feature_incompat); |
Gao Xiang | 5efe513 | 2019-06-13 16:35:41 +0800 | [diff] [blame] | 112 | |
Gao Xiang | 426a930 | 2019-09-04 10:08:53 +0800 | [diff] [blame] | 113 | EROFS_SB(sb)->feature_incompat = feature; |
Gao Xiang | 5efe513 | 2019-06-13 16:35:41 +0800 | [diff] [blame] | 114 | |
| 115 | /* check if current kernel meets all mandatory requirements */ |
Gao Xiang | 426a930 | 2019-09-04 10:08:53 +0800 | [diff] [blame] | 116 | if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 117 | erofs_err(sb, |
| 118 | "unidentified incompatible feature %x, please upgrade kernel version", |
| 119 | feature & ~EROFS_ALL_FEATURE_INCOMPAT); |
Gao Xiang | 5efe513 | 2019-06-13 16:35:41 +0800 | [diff] [blame] | 120 | return false; |
| 121 | } |
| 122 | return true; |
| 123 | } |
| 124 | |
Gao Xiang | 1437371 | 2021-03-29 18:00:12 +0800 | [diff] [blame] | 125 | #ifdef CONFIG_EROFS_FS_ZIP |
| 126 | /* read variable-sized metadata, offset will be aligned by 4-byte */ |
| 127 | static void *erofs_read_metadata(struct super_block *sb, struct page **pagep, |
| 128 | erofs_off_t *offset, int *lengthp) |
| 129 | { |
| 130 | struct page *page = *pagep; |
| 131 | u8 *buffer, *ptr; |
| 132 | int len, i, cnt; |
| 133 | erofs_blk_t blk; |
| 134 | |
| 135 | *offset = round_up(*offset, 4); |
| 136 | blk = erofs_blknr(*offset); |
| 137 | |
| 138 | if (!page || page->index != blk) { |
| 139 | if (page) { |
| 140 | unlock_page(page); |
| 141 | put_page(page); |
| 142 | } |
| 143 | page = erofs_get_meta_page(sb, blk); |
| 144 | if (IS_ERR(page)) |
| 145 | goto err_nullpage; |
| 146 | } |
| 147 | |
| 148 | ptr = kmap(page); |
| 149 | len = le16_to_cpu(*(__le16 *)&ptr[erofs_blkoff(*offset)]); |
| 150 | if (!len) |
| 151 | len = U16_MAX + 1; |
| 152 | buffer = kmalloc(len, GFP_KERNEL); |
| 153 | if (!buffer) { |
| 154 | buffer = ERR_PTR(-ENOMEM); |
| 155 | goto out; |
| 156 | } |
| 157 | *offset += sizeof(__le16); |
| 158 | *lengthp = len; |
| 159 | |
| 160 | for (i = 0; i < len; i += cnt) { |
| 161 | cnt = min(EROFS_BLKSIZ - (int)erofs_blkoff(*offset), len - i); |
| 162 | blk = erofs_blknr(*offset); |
| 163 | |
| 164 | if (!page || page->index != blk) { |
| 165 | if (page) { |
| 166 | kunmap(page); |
| 167 | unlock_page(page); |
| 168 | put_page(page); |
| 169 | } |
| 170 | page = erofs_get_meta_page(sb, blk); |
| 171 | if (IS_ERR(page)) { |
| 172 | kfree(buffer); |
| 173 | goto err_nullpage; |
| 174 | } |
| 175 | ptr = kmap(page); |
| 176 | } |
| 177 | memcpy(buffer + i, ptr + erofs_blkoff(*offset), cnt); |
| 178 | *offset += cnt; |
| 179 | } |
| 180 | out: |
| 181 | kunmap(page); |
| 182 | *pagep = page; |
| 183 | return buffer; |
| 184 | err_nullpage: |
| 185 | *pagep = NULL; |
| 186 | return page; |
| 187 | } |
| 188 | |
| 189 | static int erofs_load_compr_cfgs(struct super_block *sb, |
| 190 | struct erofs_super_block *dsb) |
| 191 | { |
| 192 | struct erofs_sb_info *sbi; |
| 193 | struct page *page; |
| 194 | unsigned int algs, alg; |
| 195 | erofs_off_t offset; |
| 196 | int size, ret; |
| 197 | |
| 198 | sbi = EROFS_SB(sb); |
| 199 | sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs); |
| 200 | |
| 201 | if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) { |
| 202 | erofs_err(sb, "try to load compressed fs with unsupported algorithms %x", |
| 203 | sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS); |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | |
| 207 | offset = EROFS_SUPER_OFFSET + sbi->sb_size; |
| 208 | page = NULL; |
| 209 | alg = 0; |
| 210 | ret = 0; |
| 211 | |
| 212 | for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) { |
| 213 | void *data; |
| 214 | |
| 215 | if (!(algs & 1)) |
| 216 | continue; |
| 217 | |
| 218 | data = erofs_read_metadata(sb, &page, &offset, &size); |
| 219 | if (IS_ERR(data)) { |
| 220 | ret = PTR_ERR(data); |
| 221 | goto err; |
| 222 | } |
| 223 | |
| 224 | switch (alg) { |
| 225 | case Z_EROFS_COMPRESSION_LZ4: |
| 226 | ret = z_erofs_load_lz4_config(sb, dsb, data, size); |
| 227 | break; |
| 228 | default: |
| 229 | DBG_BUGON(1); |
| 230 | ret = -EFAULT; |
| 231 | } |
| 232 | kfree(data); |
| 233 | if (ret) |
| 234 | goto err; |
| 235 | } |
| 236 | err: |
| 237 | if (page) { |
| 238 | unlock_page(page); |
| 239 | put_page(page); |
| 240 | } |
| 241 | return ret; |
| 242 | } |
| 243 | #else |
| 244 | static int erofs_load_compr_cfgs(struct super_block *sb, |
| 245 | struct erofs_super_block *dsb) |
| 246 | { |
| 247 | if (dsb->u1.available_compr_algs) { |
| 248 | erofs_err(sb, "try to load compressed fs when compression is disabled"); |
| 249 | return -EINVAL; |
| 250 | } |
| 251 | return 0; |
| 252 | } |
| 253 | #endif |
| 254 | |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 255 | static int erofs_read_superblock(struct super_block *sb) |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 256 | { |
| 257 | struct erofs_sb_info *sbi; |
Gao Xiang | fe7c242 | 2019-09-04 10:09:10 +0800 | [diff] [blame] | 258 | struct page *page; |
Gao Xiang | 0259f20 | 2019-09-04 10:09:00 +0800 | [diff] [blame] | 259 | struct erofs_super_block *dsb; |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 260 | unsigned int blkszbits; |
Gao Xiang | fe7c242 | 2019-09-04 10:09:10 +0800 | [diff] [blame] | 261 | void *data; |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 262 | int ret; |
| 263 | |
Gao Xiang | fe7c242 | 2019-09-04 10:09:10 +0800 | [diff] [blame] | 264 | page = read_mapping_page(sb->s_bdev->bd_inode->i_mapping, 0, NULL); |
Wei Yongjun | 517d6b9 | 2019-09-18 08:30:33 +0000 | [diff] [blame] | 265 | if (IS_ERR(page)) { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 266 | erofs_err(sb, "cannot read erofs superblock"); |
Wei Yongjun | 517d6b9 | 2019-09-18 08:30:33 +0000 | [diff] [blame] | 267 | return PTR_ERR(page); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | sbi = EROFS_SB(sb); |
Gao Xiang | fe7c242 | 2019-09-04 10:09:10 +0800 | [diff] [blame] | 271 | |
Pratik Shinde | b858a48 | 2019-11-04 10:49:37 +0800 | [diff] [blame] | 272 | data = kmap(page); |
Gao Xiang | fe7c242 | 2019-09-04 10:09:10 +0800 | [diff] [blame] | 273 | dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 274 | |
| 275 | ret = -EINVAL; |
Gao Xiang | 0259f20 | 2019-09-04 10:09:00 +0800 | [diff] [blame] | 276 | if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 277 | erofs_err(sb, "cannot find valid erofs superblock"); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 278 | goto out; |
| 279 | } |
| 280 | |
Pratik Shinde | b858a48 | 2019-11-04 10:49:37 +0800 | [diff] [blame] | 281 | sbi->feature_compat = le32_to_cpu(dsb->feature_compat); |
Gao Xiang | de06a6a | 2021-03-29 09:23:05 +0800 | [diff] [blame] | 282 | if (erofs_sb_has_sb_chksum(sbi)) { |
Pratik Shinde | b858a48 | 2019-11-04 10:49:37 +0800 | [diff] [blame] | 283 | ret = erofs_superblock_csum_verify(sb, data); |
| 284 | if (ret) |
| 285 | goto out; |
| 286 | } |
| 287 | |
Gao Xiang | 0259f20 | 2019-09-04 10:09:00 +0800 | [diff] [blame] | 288 | blkszbits = dsb->blkszbits; |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 289 | /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */ |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 290 | if (blkszbits != LOG_BLOCK_SIZE) { |
Gao Xiang | bde5452 | 2021-01-20 09:30:16 +0800 | [diff] [blame] | 291 | erofs_err(sb, "blkszbits %u isn't supported on this platform", |
| 292 | blkszbits); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 293 | goto out; |
| 294 | } |
| 295 | |
Gao Xiang | 0259f20 | 2019-09-04 10:09:00 +0800 | [diff] [blame] | 296 | if (!check_layout_compatibility(sb, dsb)) |
Gao Xiang | 5efe513 | 2019-06-13 16:35:41 +0800 | [diff] [blame] | 297 | goto out; |
| 298 | |
Gao Xiang | 1437371 | 2021-03-29 18:00:12 +0800 | [diff] [blame] | 299 | sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE; |
| 300 | if (sbi->sb_size > EROFS_BLKSIZ) { |
| 301 | erofs_err(sb, "invalid sb_extslots %u (more than a fs block)", |
| 302 | sbi->sb_size); |
| 303 | goto out; |
| 304 | } |
Gao Xiang | 0259f20 | 2019-09-04 10:09:00 +0800 | [diff] [blame] | 305 | sbi->blocks = le32_to_cpu(dsb->blocks); |
| 306 | sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 307 | #ifdef CONFIG_EROFS_FS_XATTR |
Gao Xiang | 0259f20 | 2019-09-04 10:09:00 +0800 | [diff] [blame] | 308 | sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 309 | #endif |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 310 | sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact)); |
Gao Xiang | 0259f20 | 2019-09-04 10:09:00 +0800 | [diff] [blame] | 311 | sbi->root_nid = le16_to_cpu(dsb->root_nid); |
| 312 | sbi->inos = le64_to_cpu(dsb->inos); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 313 | |
Gao Xiang | 0259f20 | 2019-09-04 10:09:00 +0800 | [diff] [blame] | 314 | sbi->build_time = le64_to_cpu(dsb->build_time); |
| 315 | sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 316 | |
Gao Xiang | 0259f20 | 2019-09-04 10:09:00 +0800 | [diff] [blame] | 317 | memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid)); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 318 | |
Gao Xiang | 0259f20 | 2019-09-04 10:09:00 +0800 | [diff] [blame] | 319 | ret = strscpy(sbi->volume_name, dsb->volume_name, |
| 320 | sizeof(dsb->volume_name)); |
Gao Xiang | a64d949 | 2019-08-18 18:28:24 +0800 | [diff] [blame] | 321 | if (ret < 0) { /* -E2BIG */ |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 322 | erofs_err(sb, "bad volume name without NIL terminator"); |
Gao Xiang | a64d949 | 2019-08-18 18:28:24 +0800 | [diff] [blame] | 323 | ret = -EFSCORRUPTED; |
| 324 | goto out; |
| 325 | } |
Huang Jianan | 5d50538 | 2021-03-29 09:23:06 +0800 | [diff] [blame] | 326 | |
| 327 | /* parse on-disk compression configurations */ |
Gao Xiang | 1437371 | 2021-03-29 18:00:12 +0800 | [diff] [blame] | 328 | if (erofs_sb_has_compr_cfgs(sbi)) |
| 329 | ret = erofs_load_compr_cfgs(sb, dsb); |
| 330 | else |
| 331 | ret = z_erofs_load_lz4_config(sb, dsb, NULL, 0); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 332 | out: |
Pratik Shinde | b858a48 | 2019-11-04 10:49:37 +0800 | [diff] [blame] | 333 | kunmap(page); |
Gao Xiang | fe7c242 | 2019-09-04 10:09:10 +0800 | [diff] [blame] | 334 | put_page(page); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 335 | return ret; |
| 336 | } |
| 337 | |
Gao Xiang | 4279f3f | 2019-07-31 23:57:49 +0800 | [diff] [blame] | 338 | /* set up default EROFS parameters */ |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 339 | static void erofs_default_options(struct erofs_fs_context *ctx) |
Gao Xiang | 4279f3f | 2019-07-31 23:57:49 +0800 | [diff] [blame] | 340 | { |
| 341 | #ifdef CONFIG_EROFS_FS_ZIP |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 342 | ctx->cache_strategy = EROFS_ZIP_CACHE_READAROUND; |
| 343 | ctx->max_sync_decompress_pages = 3; |
Huang Jianan | 30048cd | 2021-03-17 11:54:48 +0800 | [diff] [blame] | 344 | ctx->readahead_sync_decompress = false; |
Gao Xiang | 4279f3f | 2019-07-31 23:57:49 +0800 | [diff] [blame] | 345 | #endif |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 346 | #ifdef CONFIG_EROFS_FS_XATTR |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 347 | set_opt(ctx, XATTR_USER); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 348 | #endif |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 349 | #ifdef CONFIG_EROFS_FS_POSIX_ACL |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 350 | set_opt(ctx, POSIX_ACL); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 351 | #endif |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | enum { |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 355 | Opt_user_xattr, |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 356 | Opt_acl, |
Gao Xiang | 4279f3f | 2019-07-31 23:57:49 +0800 | [diff] [blame] | 357 | Opt_cache_strategy, |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 358 | Opt_err |
| 359 | }; |
| 360 | |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 361 | static const struct constant_table erofs_param_cache_strategy[] = { |
| 362 | {"disabled", EROFS_ZIP_CACHE_DISABLED}, |
| 363 | {"readahead", EROFS_ZIP_CACHE_READAHEAD}, |
| 364 | {"readaround", EROFS_ZIP_CACHE_READAROUND}, |
| 365 | {} |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 366 | }; |
| 367 | |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 368 | static const struct fs_parameter_spec erofs_fs_parameters[] = { |
| 369 | fsparam_flag_no("user_xattr", Opt_user_xattr), |
| 370 | fsparam_flag_no("acl", Opt_acl), |
| 371 | fsparam_enum("cache_strategy", Opt_cache_strategy, |
| 372 | erofs_param_cache_strategy), |
| 373 | {} |
| 374 | }; |
| 375 | |
| 376 | static int erofs_fc_parse_param(struct fs_context *fc, |
| 377 | struct fs_parameter *param) |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 378 | { |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 379 | struct erofs_fs_context *ctx __maybe_unused = fc->fs_private; |
| 380 | struct fs_parse_result result; |
| 381 | int opt; |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 382 | |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 383 | opt = fs_parse(fc, erofs_fs_parameters, param, &result); |
| 384 | if (opt < 0) |
| 385 | return opt; |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 386 | |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 387 | switch (opt) { |
| 388 | case Opt_user_xattr: |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 389 | #ifdef CONFIG_EROFS_FS_XATTR |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 390 | if (result.boolean) |
| 391 | set_opt(ctx, XATTR_USER); |
| 392 | else |
| 393 | clear_opt(ctx, XATTR_USER); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 394 | #else |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 395 | errorfc(fc, "{,no}user_xattr options not supported"); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 396 | #endif |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 397 | break; |
| 398 | case Opt_acl: |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 399 | #ifdef CONFIG_EROFS_FS_POSIX_ACL |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 400 | if (result.boolean) |
| 401 | set_opt(ctx, POSIX_ACL); |
| 402 | else |
| 403 | clear_opt(ctx, POSIX_ACL); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 404 | #else |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 405 | errorfc(fc, "{,no}acl options not supported"); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 406 | #endif |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 407 | break; |
| 408 | case Opt_cache_strategy: |
| 409 | #ifdef CONFIG_EROFS_FS_ZIP |
| 410 | ctx->cache_strategy = result.uint_32; |
| 411 | #else |
| 412 | errorfc(fc, "compression not supported, cache_strategy ignored"); |
| 413 | #endif |
| 414 | break; |
| 415 | default: |
| 416 | return -ENOPARAM; |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 417 | } |
| 418 | return 0; |
| 419 | } |
| 420 | |
Gao Xiang | 4279f3f | 2019-07-31 23:57:49 +0800 | [diff] [blame] | 421 | #ifdef CONFIG_EROFS_FS_ZIP |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 422 | static const struct address_space_operations managed_cache_aops; |
| 423 | |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 424 | static int erofs_managed_cache_releasepage(struct page *page, gfp_t gfp_mask) |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 425 | { |
| 426 | int ret = 1; /* 0 - busy */ |
| 427 | struct address_space *const mapping = page->mapping; |
| 428 | |
Gao Xiang | 8b987bc | 2018-12-05 21:23:13 +0800 | [diff] [blame] | 429 | DBG_BUGON(!PageLocked(page)); |
| 430 | DBG_BUGON(mapping->a_ops != &managed_cache_aops); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 431 | |
| 432 | if (PagePrivate(page)) |
Gao Xiang | 47e541a | 2018-07-29 13:34:58 +0800 | [diff] [blame] | 433 | ret = erofs_try_to_free_cached_page(mapping, page); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 434 | |
| 435 | return ret; |
| 436 | } |
| 437 | |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 438 | static void erofs_managed_cache_invalidatepage(struct page *page, |
| 439 | unsigned int offset, |
| 440 | unsigned int length) |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 441 | { |
| 442 | const unsigned int stop = length + offset; |
| 443 | |
Gao Xiang | 8b987bc | 2018-12-05 21:23:13 +0800 | [diff] [blame] | 444 | DBG_BUGON(!PageLocked(page)); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 445 | |
Gao Xiang | 8b987bc | 2018-12-05 21:23:13 +0800 | [diff] [blame] | 446 | /* Check for potential overflow in debug mode */ |
| 447 | DBG_BUGON(stop > PAGE_SIZE || stop < length); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 448 | |
| 449 | if (offset == 0 && stop == PAGE_SIZE) |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 450 | while (!erofs_managed_cache_releasepage(page, GFP_NOFS)) |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 451 | cond_resched(); |
| 452 | } |
| 453 | |
| 454 | static const struct address_space_operations managed_cache_aops = { |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 455 | .releasepage = erofs_managed_cache_releasepage, |
| 456 | .invalidatepage = erofs_managed_cache_invalidatepage, |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 457 | }; |
| 458 | |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 459 | static int erofs_init_managed_cache(struct super_block *sb) |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 460 | { |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 461 | struct erofs_sb_info *const sbi = EROFS_SB(sb); |
| 462 | struct inode *const inode = new_inode(sb); |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 463 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 464 | if (!inode) |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 465 | return -ENOMEM; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 466 | |
| 467 | set_nlink(inode, 1); |
| 468 | inode->i_size = OFFSET_MAX; |
| 469 | |
| 470 | inode->i_mapping->a_ops = &managed_cache_aops; |
| 471 | mapping_set_gfp_mask(inode->i_mapping, |
Gao Xiang | 8494c29 | 2019-07-31 23:57:42 +0800 | [diff] [blame] | 472 | GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE); |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 473 | sbi->managed_cache = inode; |
| 474 | return 0; |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 475 | } |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 476 | #else |
| 477 | static int erofs_init_managed_cache(struct super_block *sb) { return 0; } |
Gao Xiang | 105d4ad | 2018-07-26 20:22:07 +0800 | [diff] [blame] | 478 | #endif |
| 479 | |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 480 | static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 481 | { |
| 482 | struct inode *inode; |
| 483 | struct erofs_sb_info *sbi; |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 484 | struct erofs_fs_context *ctx = fc->fs_private; |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 485 | int err; |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 486 | |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 487 | sb->s_magic = EROFS_SUPER_MAGIC; |
| 488 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 489 | if (!sb_set_blocksize(sb, EROFS_BLKSIZ)) { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 490 | erofs_err(sb, "failed to set erofs blksize"); |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 491 | return -EINVAL; |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 492 | } |
| 493 | |
Shobhit Kukreti | a9f69bd | 2019-06-26 22:31:18 -0700 | [diff] [blame] | 494 | sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 495 | if (!sbi) |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 496 | return -ENOMEM; |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 497 | |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 498 | sb->s_fs_info = sbi; |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 499 | err = erofs_read_superblock(sb); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 500 | if (err) |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 501 | return err; |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 502 | |
Gao Xiang | 5f0abea | 2018-09-06 17:01:47 +0800 | [diff] [blame] | 503 | sb->s_flags |= SB_RDONLY | SB_NOATIME; |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 504 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
| 505 | sb->s_time_gran = 1; |
| 506 | |
| 507 | sb->s_op = &erofs_sops; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 508 | sb->s_xattr = erofs_xattr_handlers; |
Chengguang Xu | e7cda1e | 2020-05-26 17:03:43 +0800 | [diff] [blame] | 509 | |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 510 | if (test_opt(ctx, POSIX_ACL)) |
Gao Xiang | 516c115c | 2019-01-29 16:35:20 +0800 | [diff] [blame] | 511 | sb->s_flags |= SB_POSIXACL; |
| 512 | else |
| 513 | sb->s_flags &= ~SB_POSIXACL; |
| 514 | |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 515 | sbi->ctx = *ctx; |
| 516 | |
Gao Xiang | e7e9a30 | 2018-07-26 20:22:05 +0800 | [diff] [blame] | 517 | #ifdef CONFIG_EROFS_FS_ZIP |
Gao Xiang | 64094a0 | 2020-02-20 10:46:42 +0800 | [diff] [blame] | 518 | xa_init(&sbi->managed_pslots); |
Gao Xiang | e7e9a30 | 2018-07-26 20:22:05 +0800 | [diff] [blame] | 519 | #endif |
| 520 | |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 521 | /* get the root inode */ |
| 522 | inode = erofs_iget(sb, ROOT_NID(sbi), true); |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 523 | if (IS_ERR(inode)) |
| 524 | return PTR_ERR(inode); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 525 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 526 | if (!S_ISDIR(inode->i_mode)) { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 527 | erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)", |
| 528 | ROOT_NID(sbi), inode->i_mode); |
Chengguang Xu | 94832d9 | 2019-01-23 14:12:25 +0800 | [diff] [blame] | 529 | iput(inode); |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 530 | return -EINVAL; |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | sb->s_root = d_make_root(inode); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 534 | if (!sb->s_root) |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 535 | return -ENOMEM; |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 536 | |
Gao Xiang | 22fe04a | 2019-07-31 23:57:39 +0800 | [diff] [blame] | 537 | erofs_shrinker_register(sb); |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 538 | /* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */ |
| 539 | err = erofs_init_managed_cache(sb); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 540 | if (err) |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 541 | return err; |
Gao Xiang | 2497ee4 | 2018-07-26 20:22:03 +0800 | [diff] [blame] | 542 | |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 543 | erofs_info(sb, "mounted with root inode @ nid %llu.", ROOT_NID(sbi)); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 544 | return 0; |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 545 | } |
| 546 | |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 547 | static int erofs_fc_get_tree(struct fs_context *fc) |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 548 | { |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 549 | return get_tree_bdev(fc, erofs_fc_fill_super); |
| 550 | } |
| 551 | |
| 552 | static int erofs_fc_reconfigure(struct fs_context *fc) |
| 553 | { |
| 554 | struct super_block *sb = fc->root->d_sb; |
| 555 | struct erofs_sb_info *sbi = EROFS_SB(sb); |
| 556 | struct erofs_fs_context *ctx = fc->fs_private; |
| 557 | |
| 558 | DBG_BUGON(!sb_rdonly(sb)); |
| 559 | |
| 560 | if (test_opt(ctx, POSIX_ACL)) |
| 561 | fc->sb_flags |= SB_POSIXACL; |
| 562 | else |
| 563 | fc->sb_flags &= ~SB_POSIXACL; |
| 564 | |
| 565 | sbi->ctx = *ctx; |
| 566 | |
| 567 | fc->sb_flags |= SB_RDONLY; |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | static void erofs_fc_free(struct fs_context *fc) |
| 572 | { |
| 573 | kfree(fc->fs_private); |
| 574 | } |
| 575 | |
| 576 | static const struct fs_context_operations erofs_context_ops = { |
| 577 | .parse_param = erofs_fc_parse_param, |
| 578 | .get_tree = erofs_fc_get_tree, |
| 579 | .reconfigure = erofs_fc_reconfigure, |
| 580 | .free = erofs_fc_free, |
| 581 | }; |
| 582 | |
| 583 | static int erofs_init_fs_context(struct fs_context *fc) |
| 584 | { |
| 585 | fc->fs_private = kzalloc(sizeof(struct erofs_fs_context), GFP_KERNEL); |
| 586 | if (!fc->fs_private) |
| 587 | return -ENOMEM; |
| 588 | |
| 589 | /* set default mount options */ |
| 590 | erofs_default_options(fc->fs_private); |
| 591 | |
| 592 | fc->ops = &erofs_context_ops; |
| 593 | |
| 594 | return 0; |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 595 | } |
| 596 | |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 597 | /* |
| 598 | * could be triggered after deactivate_locked_super() |
| 599 | * is called, thus including umount and failed to initialize. |
| 600 | */ |
| 601 | static void erofs_kill_sb(struct super_block *sb) |
| 602 | { |
| 603 | struct erofs_sb_info *sbi; |
| 604 | |
| 605 | WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC); |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 606 | |
| 607 | kill_block_super(sb); |
| 608 | |
| 609 | sbi = EROFS_SB(sb); |
| 610 | if (!sbi) |
| 611 | return; |
| 612 | kfree(sbi); |
| 613 | sb->s_fs_info = NULL; |
| 614 | } |
| 615 | |
| 616 | /* called when ->s_root is non-NULL */ |
| 617 | static void erofs_put_super(struct super_block *sb) |
| 618 | { |
| 619 | struct erofs_sb_info *const sbi = EROFS_SB(sb); |
| 620 | |
| 621 | DBG_BUGON(!sbi); |
| 622 | |
| 623 | erofs_shrinker_unregister(sb); |
Gao Xiang | 4279f3f | 2019-07-31 23:57:49 +0800 | [diff] [blame] | 624 | #ifdef CONFIG_EROFS_FS_ZIP |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 625 | iput(sbi->managed_cache); |
| 626 | sbi->managed_cache = NULL; |
| 627 | #endif |
| 628 | } |
| 629 | |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 630 | static struct file_system_type erofs_fs_type = { |
| 631 | .owner = THIS_MODULE, |
| 632 | .name = "erofs", |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 633 | .init_fs_context = erofs_init_fs_context, |
Gao Xiang | 8f7acda | 2019-07-31 23:57:41 +0800 | [diff] [blame] | 634 | .kill_sb = erofs_kill_sb, |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 635 | .fs_flags = FS_REQUIRES_DEV, |
| 636 | }; |
| 637 | MODULE_ALIAS_FS("erofs"); |
| 638 | |
| 639 | static int __init erofs_module_init(void) |
| 640 | { |
| 641 | int err; |
| 642 | |
| 643 | erofs_check_ondisk_layout_definitions(); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 644 | |
Gao Xiang | 1c2dfbf | 2019-09-04 10:08:55 +0800 | [diff] [blame] | 645 | erofs_inode_cachep = kmem_cache_create("erofs_inode", |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 646 | sizeof(struct erofs_inode), 0, |
Gao Xiang | 1c2dfbf | 2019-09-04 10:08:55 +0800 | [diff] [blame] | 647 | SLAB_RECLAIM_ACCOUNT, |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 648 | erofs_inode_init_once); |
Gao Xiang | 1c2dfbf | 2019-09-04 10:08:55 +0800 | [diff] [blame] | 649 | if (!erofs_inode_cachep) { |
| 650 | err = -ENOMEM; |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 651 | goto icache_err; |
Gao Xiang | 1c2dfbf | 2019-09-04 10:08:55 +0800 | [diff] [blame] | 652 | } |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 653 | |
Gao Xiang | 22fe04a | 2019-07-31 23:57:39 +0800 | [diff] [blame] | 654 | err = erofs_init_shrinker(); |
Gao Xiang | a158131 | 2018-07-26 20:22:04 +0800 | [diff] [blame] | 655 | if (err) |
| 656 | goto shrinker_err; |
| 657 | |
Gao Xiang | 5248873 | 2021-04-10 03:06:30 +0800 | [diff] [blame] | 658 | erofs_pcpubuf_init(); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 659 | err = z_erofs_init_zip_subsystem(); |
| 660 | if (err) |
| 661 | goto zip_err; |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 662 | |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 663 | err = register_filesystem(&erofs_fs_type); |
| 664 | if (err) |
| 665 | goto fs_err; |
| 666 | |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 667 | return 0; |
| 668 | |
| 669 | fs_err: |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 670 | z_erofs_exit_zip_subsystem(); |
| 671 | zip_err: |
Gao Xiang | 22fe04a | 2019-07-31 23:57:39 +0800 | [diff] [blame] | 672 | erofs_exit_shrinker(); |
Gao Xiang | a158131 | 2018-07-26 20:22:04 +0800 | [diff] [blame] | 673 | shrinker_err: |
Gao Xiang | 1c2dfbf | 2019-09-04 10:08:55 +0800 | [diff] [blame] | 674 | kmem_cache_destroy(erofs_inode_cachep); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 675 | icache_err: |
| 676 | return err; |
| 677 | } |
| 678 | |
| 679 | static void __exit erofs_module_exit(void) |
| 680 | { |
| 681 | unregister_filesystem(&erofs_fs_type); |
Gao Xiang | 3883a79 | 2018-07-26 20:22:06 +0800 | [diff] [blame] | 682 | z_erofs_exit_zip_subsystem(); |
Gao Xiang | 22fe04a | 2019-07-31 23:57:39 +0800 | [diff] [blame] | 683 | erofs_exit_shrinker(); |
Gao Xiang | 1c2dfbf | 2019-09-04 10:08:55 +0800 | [diff] [blame] | 684 | |
| 685 | /* Ensure all RCU free inodes are safe before cache is destroyed. */ |
| 686 | rcu_barrier(); |
| 687 | kmem_cache_destroy(erofs_inode_cachep); |
Gao Xiang | 5248873 | 2021-04-10 03:06:30 +0800 | [diff] [blame] | 688 | erofs_pcpubuf_exit(); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* get filesystem statistics */ |
| 692 | static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 693 | { |
| 694 | struct super_block *sb = dentry->d_sb; |
| 695 | struct erofs_sb_info *sbi = EROFS_SB(sb); |
| 696 | u64 id = huge_encode_dev(sb->s_bdev->bd_dev); |
| 697 | |
| 698 | buf->f_type = sb->s_magic; |
| 699 | buf->f_bsize = EROFS_BLKSIZ; |
| 700 | buf->f_blocks = sbi->blocks; |
| 701 | buf->f_bfree = buf->f_bavail = 0; |
| 702 | |
| 703 | buf->f_files = ULLONG_MAX; |
| 704 | buf->f_ffree = ULLONG_MAX - sbi->inos; |
| 705 | |
| 706 | buf->f_namelen = EROFS_NAME_LEN; |
| 707 | |
Al Viro | 6d1349c | 2020-09-18 16:45:50 -0400 | [diff] [blame] | 708 | buf->f_fsid = u64_to_fsid(id); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | static int erofs_show_options(struct seq_file *seq, struct dentry *root) |
| 713 | { |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 714 | struct erofs_sb_info *sbi __maybe_unused = EROFS_SB(root->d_sb); |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 715 | struct erofs_fs_context *ctx __maybe_unused = &sbi->ctx; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 716 | |
| 717 | #ifdef CONFIG_EROFS_FS_XATTR |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 718 | if (test_opt(ctx, XATTR_USER)) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 719 | seq_puts(seq, ",user_xattr"); |
| 720 | else |
| 721 | seq_puts(seq, ",nouser_xattr"); |
| 722 | #endif |
| 723 | #ifdef CONFIG_EROFS_FS_POSIX_ACL |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 724 | if (test_opt(ctx, POSIX_ACL)) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 725 | seq_puts(seq, ",acl"); |
| 726 | else |
| 727 | seq_puts(seq, ",noacl"); |
| 728 | #endif |
Gao Xiang | 4279f3f | 2019-07-31 23:57:49 +0800 | [diff] [blame] | 729 | #ifdef CONFIG_EROFS_FS_ZIP |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 730 | if (ctx->cache_strategy == EROFS_ZIP_CACHE_DISABLED) |
Gao Xiang | 4279f3f | 2019-07-31 23:57:49 +0800 | [diff] [blame] | 731 | seq_puts(seq, ",cache_strategy=disabled"); |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 732 | else if (ctx->cache_strategy == EROFS_ZIP_CACHE_READAHEAD) |
Gao Xiang | 4279f3f | 2019-07-31 23:57:49 +0800 | [diff] [blame] | 733 | seq_puts(seq, ",cache_strategy=readahead"); |
Chao Yu | f57a3fe | 2020-05-29 18:48:36 +0800 | [diff] [blame] | 734 | else if (ctx->cache_strategy == EROFS_ZIP_CACHE_READAROUND) |
Gao Xiang | 4279f3f | 2019-07-31 23:57:49 +0800 | [diff] [blame] | 735 | seq_puts(seq, ",cache_strategy=readaround"); |
Gao Xiang | 4279f3f | 2019-07-31 23:57:49 +0800 | [diff] [blame] | 736 | #endif |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 737 | return 0; |
| 738 | } |
| 739 | |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 740 | const struct super_operations erofs_sops = { |
| 741 | .put_super = erofs_put_super, |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 742 | .alloc_inode = erofs_alloc_inode, |
| 743 | .free_inode = erofs_free_inode, |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 744 | .statfs = erofs_statfs, |
| 745 | .show_options = erofs_show_options, |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 746 | }; |
| 747 | |
| 748 | module_init(erofs_module_init); |
| 749 | module_exit(erofs_module_exit); |
| 750 | |
| 751 | MODULE_DESCRIPTION("Enhanced ROM File System"); |
Gao Xiang | bc33d9f | 2019-07-31 23:57:51 +0800 | [diff] [blame] | 752 | MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc."); |
Gao Xiang | ba2b77a | 2018-07-26 20:21:46 +0800 | [diff] [blame] | 753 | MODULE_LICENSE("GPL"); |
| 754 | |