| .. | .. | 
|---|
|  | 1 | +// SPDX-License-Identifier: GPL-2.0-or-later | 
|---|
| 1 | 2 | /* | 
|---|
| 2 | 3 | * Squashfs - a compressed read only filesystem for Linux | 
|---|
| 3 | 4 | * | 
|---|
| 4 | 5 | * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008 | 
|---|
| 5 | 6 | * Phillip Lougher <phillip@squashfs.org.uk> | 
|---|
| 6 |  | - * | 
|---|
| 7 |  | - * This program is free software; you can redistribute it and/or | 
|---|
| 8 |  | - * modify it under the terms of the GNU General Public License | 
|---|
| 9 |  | - * as published by the Free Software Foundation; either version 2, | 
|---|
| 10 |  | - * or (at your option) any later version. | 
|---|
| 11 |  | - * | 
|---|
| 12 |  | - * This program is distributed in the hope that it will be useful, | 
|---|
| 13 |  | - * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 14 |  | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 15 |  | - * GNU General Public License for more details. | 
|---|
| 16 |  | - * | 
|---|
| 17 |  | - * You should have received a copy of the GNU General Public License | 
|---|
| 18 |  | - * along with this program; if not, write to the Free Software | 
|---|
| 19 |  | - * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
|---|
| 20 | 7 | * | 
|---|
| 21 | 8 | * super.c | 
|---|
| 22 | 9 | */ | 
|---|
| .. | .. | 
|---|
| 30 | 17 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | 
|---|
| 31 | 18 |  | 
|---|
| 32 | 19 | #include <linux/fs.h> | 
|---|
|  | 20 | +#include <linux/fs_context.h> | 
|---|
| 33 | 21 | #include <linux/vfs.h> | 
|---|
| 34 | 22 | #include <linux/slab.h> | 
|---|
| 35 | 23 | #include <linux/mutex.h> | 
|---|
| .. | .. | 
|---|
| 49 | 37 | static struct file_system_type squashfs_fs_type; | 
|---|
| 50 | 38 | static const struct super_operations squashfs_super_ops; | 
|---|
| 51 | 39 |  | 
|---|
| 52 |  | -static const struct squashfs_decompressor *supported_squashfs_filesystem(short | 
|---|
| 53 |  | -	major, short minor, short id) | 
|---|
|  | 40 | +static const struct squashfs_decompressor *supported_squashfs_filesystem( | 
|---|
|  | 41 | +	struct fs_context *fc, | 
|---|
|  | 42 | +	short major, short minor, short id) | 
|---|
| 54 | 43 | { | 
|---|
| 55 | 44 | const struct squashfs_decompressor *decompressor; | 
|---|
| 56 | 45 |  | 
|---|
| 57 | 46 | if (major < SQUASHFS_MAJOR) { | 
|---|
| 58 |  | -		ERROR("Major/Minor mismatch, older Squashfs %d.%d " | 
|---|
| 59 |  | -			"filesystems are unsupported\n", major, minor); | 
|---|
|  | 47 | +		errorf(fc, "Major/Minor mismatch, older Squashfs %d.%d " | 
|---|
|  | 48 | +		       "filesystems are unsupported", major, minor); | 
|---|
| 60 | 49 | return NULL; | 
|---|
| 61 | 50 | } else if (major > SQUASHFS_MAJOR || minor > SQUASHFS_MINOR) { | 
|---|
| 62 |  | -		ERROR("Major/Minor mismatch, trying to mount newer " | 
|---|
| 63 |  | -			"%d.%d filesystem\n", major, minor); | 
|---|
| 64 |  | -		ERROR("Please update your kernel\n"); | 
|---|
|  | 51 | +		errorf(fc, "Major/Minor mismatch, trying to mount newer " | 
|---|
|  | 52 | +		       "%d.%d filesystem", major, minor); | 
|---|
|  | 53 | +		errorf(fc, "Please update your kernel"); | 
|---|
| 65 | 54 | return NULL; | 
|---|
| 66 | 55 | } | 
|---|
| 67 | 56 |  | 
|---|
| 68 | 57 | decompressor = squashfs_lookup_decompressor(id); | 
|---|
| 69 | 58 | if (!decompressor->supported) { | 
|---|
| 70 |  | -		ERROR("Filesystem uses \"%s\" compression. This is not " | 
|---|
| 71 |  | -			"supported\n", decompressor->name); | 
|---|
|  | 59 | +		errorf(fc, "Filesystem uses \"%s\" compression. This is not supported", | 
|---|
|  | 60 | +		       decompressor->name); | 
|---|
| 72 | 61 | return NULL; | 
|---|
| 73 | 62 | } | 
|---|
| 74 | 63 |  | 
|---|
| .. | .. | 
|---|
| 76 | 65 | } | 
|---|
| 77 | 66 |  | 
|---|
| 78 | 67 |  | 
|---|
| 79 |  | -static int squashfs_fill_super(struct super_block *sb, void *data, int silent) | 
|---|
|  | 68 | +static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc) | 
|---|
| 80 | 69 | { | 
|---|
| 81 | 70 | struct squashfs_sb_info *msblk; | 
|---|
| 82 | 71 | struct squashfs_super_block *sblk = NULL; | 
|---|
| .. | .. | 
|---|
| 111 | 100 | sblk = squashfs_read_table(sb, SQUASHFS_START, sizeof(*sblk)); | 
|---|
| 112 | 101 |  | 
|---|
| 113 | 102 | if (IS_ERR(sblk)) { | 
|---|
| 114 |  | -		ERROR("unable to read squashfs_super_block\n"); | 
|---|
|  | 103 | +		errorf(fc, "unable to read squashfs_super_block"); | 
|---|
| 115 | 104 | err = PTR_ERR(sblk); | 
|---|
| 116 | 105 | sblk = NULL; | 
|---|
| 117 | 106 | goto failed_mount; | 
|---|
| .. | .. | 
|---|
| 122 | 111 | /* Check it is a SQUASHFS superblock */ | 
|---|
| 123 | 112 | sb->s_magic = le32_to_cpu(sblk->s_magic); | 
|---|
| 124 | 113 | if (sb->s_magic != SQUASHFS_MAGIC) { | 
|---|
| 125 |  | -		if (!silent) | 
|---|
| 126 |  | -			ERROR("Can't find a SQUASHFS superblock on %pg\n", | 
|---|
| 127 |  | -						sb->s_bdev); | 
|---|
|  | 114 | +		if (!(fc->sb_flags & SB_SILENT)) | 
|---|
|  | 115 | +			errorf(fc, "Can't find a SQUASHFS superblock on %pg", | 
|---|
|  | 116 | +			       sb->s_bdev); | 
|---|
| 128 | 117 | goto failed_mount; | 
|---|
| 129 | 118 | } | 
|---|
| 130 | 119 |  | 
|---|
| 131 | 120 | /* Check the MAJOR & MINOR versions and lookup compression type */ | 
|---|
| 132 | 121 | msblk->decompressor = supported_squashfs_filesystem( | 
|---|
|  | 122 | +			fc, | 
|---|
| 133 | 123 | le16_to_cpu(sblk->s_major), | 
|---|
| 134 | 124 | le16_to_cpu(sblk->s_minor), | 
|---|
| 135 | 125 | le16_to_cpu(sblk->compression)); | 
|---|
| .. | .. | 
|---|
| 146 | 136 | /* Check block size for sanity */ | 
|---|
| 147 | 137 | msblk->block_size = le32_to_cpu(sblk->block_size); | 
|---|
| 148 | 138 | if (msblk->block_size > SQUASHFS_FILE_MAX_SIZE) | 
|---|
| 149 |  | -		goto failed_mount; | 
|---|
|  | 139 | +		goto insanity; | 
|---|
| 150 | 140 |  | 
|---|
| 151 | 141 | /* | 
|---|
| 152 | 142 | * Check the system page size is not larger than the filesystem | 
|---|
| 153 | 143 | * block size (by default 128K).  This is currently not supported. | 
|---|
| 154 | 144 | */ | 
|---|
| 155 | 145 | if (PAGE_SIZE > msblk->block_size) { | 
|---|
| 156 |  | -		ERROR("Page size > filesystem block size (%d).  This is " | 
|---|
| 157 |  | -			"currently not supported!\n", msblk->block_size); | 
|---|
|  | 146 | +		errorf(fc, "Page size > filesystem block size (%d).  This is " | 
|---|
|  | 147 | +		       "currently not supported!", msblk->block_size); | 
|---|
| 158 | 148 | goto failed_mount; | 
|---|
| 159 | 149 | } | 
|---|
| 160 | 150 |  | 
|---|
| .. | .. | 
|---|
| 165 | 155 |  | 
|---|
| 166 | 156 | /* Check that block_size and block_log match */ | 
|---|
| 167 | 157 | if (msblk->block_size != (1 << msblk->block_log)) | 
|---|
| 168 |  | -		goto failed_mount; | 
|---|
|  | 158 | +		goto insanity; | 
|---|
| 169 | 159 |  | 
|---|
| 170 | 160 | /* Check the root inode for sanity */ | 
|---|
| 171 | 161 | root_inode = le64_to_cpu(sblk->root_inode); | 
|---|
| 172 | 162 | if (SQUASHFS_INODE_OFFSET(root_inode) > SQUASHFS_METADATA_SIZE) | 
|---|
| 173 |  | -		goto failed_mount; | 
|---|
|  | 163 | +		goto insanity; | 
|---|
| 174 | 164 |  | 
|---|
| 175 | 165 | msblk->inode_table = le64_to_cpu(sblk->inode_table_start); | 
|---|
| 176 | 166 | msblk->directory_table = le64_to_cpu(sblk->directory_table_start); | 
|---|
| .. | .. | 
|---|
| 197 | 187 | (u64) le64_to_cpu(sblk->id_table_start)); | 
|---|
| 198 | 188 |  | 
|---|
| 199 | 189 | sb->s_maxbytes = MAX_LFS_FILESIZE; | 
|---|
|  | 190 | +	sb->s_time_min = 0; | 
|---|
|  | 191 | +	sb->s_time_max = U32_MAX; | 
|---|
| 200 | 192 | sb->s_flags |= SB_RDONLY; | 
|---|
| 201 | 193 | sb->s_op = &squashfs_super_ops; | 
|---|
| 202 | 194 |  | 
|---|
| .. | .. | 
|---|
| 211 | 203 | msblk->read_page = squashfs_cache_init("data", | 
|---|
| 212 | 204 | squashfs_max_decompressors(), msblk->block_size); | 
|---|
| 213 | 205 | if (msblk->read_page == NULL) { | 
|---|
| 214 |  | -		ERROR("Failed to allocate read_page block\n"); | 
|---|
|  | 206 | +		errorf(fc, "Failed to allocate read_page block"); | 
|---|
| 215 | 207 | goto failed_mount; | 
|---|
| 216 | 208 | } | 
|---|
| 217 | 209 |  | 
|---|
| .. | .. | 
|---|
| 219 | 211 | if (IS_ERR(msblk->stream)) { | 
|---|
| 220 | 212 | err = PTR_ERR(msblk->stream); | 
|---|
| 221 | 213 | msblk->stream = NULL; | 
|---|
| 222 |  | -		goto failed_mount; | 
|---|
|  | 214 | +		goto insanity; | 
|---|
| 223 | 215 | } | 
|---|
| 224 | 216 |  | 
|---|
| 225 | 217 | /* Handle xattrs */ | 
|---|
| .. | .. | 
|---|
| 234 | 226 | msblk->xattr_id_table = squashfs_read_xattr_id_table(sb, | 
|---|
| 235 | 227 | xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids); | 
|---|
| 236 | 228 | if (IS_ERR(msblk->xattr_id_table)) { | 
|---|
| 237 |  | -		ERROR("unable to read xattr id index table\n"); | 
|---|
|  | 229 | +		errorf(fc, "unable to read xattr id index table"); | 
|---|
| 238 | 230 | err = PTR_ERR(msblk->xattr_id_table); | 
|---|
| 239 | 231 | msblk->xattr_id_table = NULL; | 
|---|
| 240 | 232 | if (err != -ENOTSUPP) | 
|---|
| .. | .. | 
|---|
| 247 | 239 | msblk->id_table = squashfs_read_id_index_table(sb, | 
|---|
| 248 | 240 | le64_to_cpu(sblk->id_table_start), next_table, msblk->ids); | 
|---|
| 249 | 241 | if (IS_ERR(msblk->id_table)) { | 
|---|
| 250 |  | -		ERROR("unable to read id index table\n"); | 
|---|
|  | 242 | +		errorf(fc, "unable to read id index table"); | 
|---|
| 251 | 243 | err = PTR_ERR(msblk->id_table); | 
|---|
| 252 | 244 | msblk->id_table = NULL; | 
|---|
| 253 | 245 | goto failed_mount; | 
|---|
| .. | .. | 
|---|
| 263 | 255 | msblk->inode_lookup_table = squashfs_read_inode_lookup_table(sb, | 
|---|
| 264 | 256 | lookup_table_start, next_table, msblk->inodes); | 
|---|
| 265 | 257 | if (IS_ERR(msblk->inode_lookup_table)) { | 
|---|
| 266 |  | -		ERROR("unable to read inode lookup table\n"); | 
|---|
|  | 258 | +		errorf(fc, "unable to read inode lookup table"); | 
|---|
| 267 | 259 | err = PTR_ERR(msblk->inode_lookup_table); | 
|---|
| 268 | 260 | msblk->inode_lookup_table = NULL; | 
|---|
| 269 | 261 | goto failed_mount; | 
|---|
| .. | .. | 
|---|
| 288 | 280 | msblk->fragment_index = squashfs_read_fragment_index_table(sb, | 
|---|
| 289 | 281 | le64_to_cpu(sblk->fragment_table_start), next_table, fragments); | 
|---|
| 290 | 282 | if (IS_ERR(msblk->fragment_index)) { | 
|---|
| 291 |  | -		ERROR("unable to read fragment index table\n"); | 
|---|
|  | 283 | +		errorf(fc, "unable to read fragment index table"); | 
|---|
| 292 | 284 | err = PTR_ERR(msblk->fragment_index); | 
|---|
| 293 | 285 | msblk->fragment_index = NULL; | 
|---|
| 294 | 286 | goto failed_mount; | 
|---|
| .. | .. | 
|---|
| 299 | 291 | /* Sanity check directory_table */ | 
|---|
| 300 | 292 | if (msblk->directory_table > next_table) { | 
|---|
| 301 | 293 | err = -EINVAL; | 
|---|
| 302 |  | -		goto failed_mount; | 
|---|
|  | 294 | +		goto insanity; | 
|---|
| 303 | 295 | } | 
|---|
| 304 | 296 |  | 
|---|
| 305 | 297 | /* Sanity check inode_table */ | 
|---|
| 306 | 298 | if (msblk->inode_table >= msblk->directory_table) { | 
|---|
| 307 | 299 | err = -EINVAL; | 
|---|
| 308 |  | -		goto failed_mount; | 
|---|
|  | 300 | +		goto insanity; | 
|---|
| 309 | 301 | } | 
|---|
| 310 | 302 |  | 
|---|
| 311 | 303 | /* allocate root */ | 
|---|
| .. | .. | 
|---|
| 334 | 326 | kfree(sblk); | 
|---|
| 335 | 327 | return 0; | 
|---|
| 336 | 328 |  | 
|---|
|  | 329 | +insanity: | 
|---|
|  | 330 | +	errorf(fc, "squashfs image failed sanity check"); | 
|---|
| 337 | 331 | failed_mount: | 
|---|
| 338 | 332 | squashfs_cache_delete(msblk->block_cache); | 
|---|
| 339 | 333 | squashfs_cache_delete(msblk->fragment_cache); | 
|---|
| .. | .. | 
|---|
| 349 | 343 | return err; | 
|---|
| 350 | 344 | } | 
|---|
| 351 | 345 |  | 
|---|
|  | 346 | +static int squashfs_get_tree(struct fs_context *fc) | 
|---|
|  | 347 | +{ | 
|---|
|  | 348 | +	return get_tree_bdev(fc, squashfs_fill_super); | 
|---|
|  | 349 | +} | 
|---|
|  | 350 | + | 
|---|
|  | 351 | +static int squashfs_reconfigure(struct fs_context *fc) | 
|---|
|  | 352 | +{ | 
|---|
|  | 353 | +	sync_filesystem(fc->root->d_sb); | 
|---|
|  | 354 | +	fc->sb_flags |= SB_RDONLY; | 
|---|
|  | 355 | +	return 0; | 
|---|
|  | 356 | +} | 
|---|
|  | 357 | + | 
|---|
|  | 358 | +static const struct fs_context_operations squashfs_context_ops = { | 
|---|
|  | 359 | +	.get_tree	= squashfs_get_tree, | 
|---|
|  | 360 | +	.reconfigure	= squashfs_reconfigure, | 
|---|
|  | 361 | +}; | 
|---|
|  | 362 | + | 
|---|
|  | 363 | +static int squashfs_init_fs_context(struct fs_context *fc) | 
|---|
|  | 364 | +{ | 
|---|
|  | 365 | +	fc->ops = &squashfs_context_ops; | 
|---|
|  | 366 | +	return 0; | 
|---|
|  | 367 | +} | 
|---|
| 352 | 368 |  | 
|---|
| 353 | 369 | static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf) | 
|---|
| 354 | 370 | { | 
|---|
| .. | .. | 
|---|
| 364 | 380 | buf->f_files = msblk->inodes; | 
|---|
| 365 | 381 | buf->f_ffree = 0; | 
|---|
| 366 | 382 | buf->f_namelen = SQUASHFS_NAME_LEN; | 
|---|
| 367 |  | -	buf->f_fsid.val[0] = (u32)id; | 
|---|
| 368 |  | -	buf->f_fsid.val[1] = (u32)(id >> 32); | 
|---|
|  | 383 | +	buf->f_fsid = u64_to_fsid(id); | 
|---|
| 369 | 384 |  | 
|---|
| 370 |  | -	return 0; | 
|---|
| 371 |  | -} | 
|---|
| 372 |  | - | 
|---|
| 373 |  | - | 
|---|
| 374 |  | -static int squashfs_remount(struct super_block *sb, int *flags, char *data) | 
|---|
| 375 |  | -{ | 
|---|
| 376 |  | -	sync_filesystem(sb); | 
|---|
| 377 |  | -	*flags |= SB_RDONLY; | 
|---|
| 378 | 385 | return 0; | 
|---|
| 379 | 386 | } | 
|---|
| 380 | 387 |  | 
|---|
| .. | .. | 
|---|
| 396 | 403 | sb->s_fs_info = NULL; | 
|---|
| 397 | 404 | } | 
|---|
| 398 | 405 | } | 
|---|
| 399 |  | - | 
|---|
| 400 |  | - | 
|---|
| 401 |  | -static struct dentry *squashfs_mount(struct file_system_type *fs_type, | 
|---|
| 402 |  | -				int flags, const char *dev_name, void *data) | 
|---|
| 403 |  | -{ | 
|---|
| 404 |  | -	return mount_bdev(fs_type, flags, dev_name, data, squashfs_fill_super); | 
|---|
| 405 |  | -} | 
|---|
| 406 |  | - | 
|---|
| 407 | 406 |  | 
|---|
| 408 | 407 | static struct kmem_cache *squashfs_inode_cachep; | 
|---|
| 409 | 408 |  | 
|---|
| .. | .. | 
|---|
| 473 | 472 | } | 
|---|
| 474 | 473 |  | 
|---|
| 475 | 474 |  | 
|---|
| 476 |  | -static void squashfs_i_callback(struct rcu_head *head) | 
|---|
|  | 475 | +static void squashfs_free_inode(struct inode *inode) | 
|---|
| 477 | 476 | { | 
|---|
| 478 |  | -	struct inode *inode = container_of(head, struct inode, i_rcu); | 
|---|
| 479 | 477 | kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode)); | 
|---|
| 480 | 478 | } | 
|---|
| 481 |  | - | 
|---|
| 482 |  | -static void squashfs_destroy_inode(struct inode *inode) | 
|---|
| 483 |  | -{ | 
|---|
| 484 |  | -	call_rcu(&inode->i_rcu, squashfs_i_callback); | 
|---|
| 485 |  | -} | 
|---|
| 486 |  | - | 
|---|
| 487 | 479 |  | 
|---|
| 488 | 480 | static struct file_system_type squashfs_fs_type = { | 
|---|
| 489 | 481 | .owner = THIS_MODULE, | 
|---|
| 490 | 482 | .name = "squashfs", | 
|---|
| 491 |  | -	.mount = squashfs_mount, | 
|---|
|  | 483 | +	.init_fs_context = squashfs_init_fs_context, | 
|---|
| 492 | 484 | .kill_sb = kill_block_super, | 
|---|
| 493 | 485 | .fs_flags = FS_REQUIRES_DEV | 
|---|
| 494 | 486 | }; | 
|---|
| .. | .. | 
|---|
| 496 | 488 |  | 
|---|
| 497 | 489 | static const struct super_operations squashfs_super_ops = { | 
|---|
| 498 | 490 | .alloc_inode = squashfs_alloc_inode, | 
|---|
| 499 |  | -	.destroy_inode = squashfs_destroy_inode, | 
|---|
|  | 491 | +	.free_inode = squashfs_free_inode, | 
|---|
| 500 | 492 | .statfs = squashfs_statfs, | 
|---|
| 501 | 493 | .put_super = squashfs_put_super, | 
|---|
| 502 |  | -	.remount_fs = squashfs_remount | 
|---|
| 503 | 494 | }; | 
|---|
| 504 | 495 |  | 
|---|
| 505 | 496 | module_init(init_squashfs_fs); | 
|---|
| .. | .. | 
|---|
| 507 | 498 | MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem"); | 
|---|
| 508 | 499 | MODULE_AUTHOR("Phillip Lougher <phillip@squashfs.org.uk>"); | 
|---|
| 509 | 500 | MODULE_LICENSE("GPL"); | 
|---|
|  | 501 | +MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY); | 
|---|