xref: /openbmc/linux/fs/squashfs/super.c (revision 3848636b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Squashfs - a compressed read only filesystem for Linux
4  *
5  * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
6  * Phillip Lougher <phillip@squashfs.org.uk>
7  *
8  * super.c
9  */
10 
11 /*
12  * This file implements code to read the superblock, read and initialise
13  * in-memory structures at mount time, and all the VFS glue code to register
14  * the filesystem.
15  */
16 
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #include <linux/blkdev.h>
20 #include <linux/fs.h>
21 #include <linux/fs_context.h>
22 #include <linux/fs_parser.h>
23 #include <linux/vfs.h>
24 #include <linux/slab.h>
25 #include <linux/mutex.h>
26 #include <linux/seq_file.h>
27 #include <linux/pagemap.h>
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/magic.h>
31 #include <linux/xattr.h>
32 #include <linux/backing-dev.h>
33 
34 #include "squashfs_fs.h"
35 #include "squashfs_fs_sb.h"
36 #include "squashfs_fs_i.h"
37 #include "squashfs.h"
38 #include "decompressor.h"
39 #include "xattr.h"
40 
41 static struct file_system_type squashfs_fs_type;
42 static const struct super_operations squashfs_super_ops;
43 
44 enum Opt_errors {
45 	Opt_errors_continue,
46 	Opt_errors_panic,
47 };
48 
49 enum squashfs_param {
50 	Opt_errors,
51 };
52 
53 struct squashfs_mount_opts {
54 	enum Opt_errors errors;
55 };
56 
57 static const struct constant_table squashfs_param_errors[] = {
58 	{"continue",   Opt_errors_continue },
59 	{"panic",      Opt_errors_panic },
60 	{}
61 };
62 
63 static const struct fs_parameter_spec squashfs_fs_parameters[] = {
64 	fsparam_enum("errors", Opt_errors, squashfs_param_errors),
65 	{}
66 };
67 
68 static int squashfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
69 {
70 	struct squashfs_mount_opts *opts = fc->fs_private;
71 	struct fs_parse_result result;
72 	int opt;
73 
74 	opt = fs_parse(fc, squashfs_fs_parameters, param, &result);
75 	if (opt < 0)
76 		return opt;
77 
78 	switch (opt) {
79 	case Opt_errors:
80 		opts->errors = result.uint_32;
81 		break;
82 	default:
83 		return -EINVAL;
84 	}
85 
86 	return 0;
87 }
88 
89 static const struct squashfs_decompressor *supported_squashfs_filesystem(
90 	struct fs_context *fc,
91 	short major, short minor, short id)
92 {
93 	const struct squashfs_decompressor *decompressor;
94 
95 	if (major < SQUASHFS_MAJOR) {
96 		errorf(fc, "Major/Minor mismatch, older Squashfs %d.%d "
97 		       "filesystems are unsupported", major, minor);
98 		return NULL;
99 	} else if (major > SQUASHFS_MAJOR || minor > SQUASHFS_MINOR) {
100 		errorf(fc, "Major/Minor mismatch, trying to mount newer "
101 		       "%d.%d filesystem", major, minor);
102 		errorf(fc, "Please update your kernel");
103 		return NULL;
104 	}
105 
106 	decompressor = squashfs_lookup_decompressor(id);
107 	if (!decompressor->supported) {
108 		errorf(fc, "Filesystem uses \"%s\" compression. This is not supported",
109 		       decompressor->name);
110 		return NULL;
111 	}
112 
113 	return decompressor;
114 }
115 
116 static int squashfs_bdi_init(struct super_block *sb)
117 {
118 	int err;
119 	unsigned int major = MAJOR(sb->s_dev);
120 	unsigned int minor = MINOR(sb->s_dev);
121 
122 	bdi_put(sb->s_bdi);
123 	sb->s_bdi = &noop_backing_dev_info;
124 
125 	err = super_setup_bdi_name(sb, "squashfs_%u_%u", major, minor);
126 	if (err)
127 		return err;
128 
129 	sb->s_bdi->ra_pages = 0;
130 	sb->s_bdi->io_pages = 0;
131 
132 	return 0;
133 }
134 
135 static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc)
136 {
137 	struct squashfs_mount_opts *opts = fc->fs_private;
138 	struct squashfs_sb_info *msblk;
139 	struct squashfs_super_block *sblk = NULL;
140 	struct inode *root;
141 	long long root_inode;
142 	unsigned short flags;
143 	unsigned int fragments;
144 	u64 lookup_table_start, xattr_id_table_start, next_table;
145 	int err;
146 
147 	TRACE("Entered squashfs_fill_superblock\n");
148 
149 	/*
150 	 * squashfs provides 'backing_dev_info' in order to disable read-ahead. For
151 	 * squashfs, I/O is not deferred, it is done immediately in read_folio,
152 	 * which means the user would always have to wait their own I/O. So the effect
153 	 * of readahead is very weak for squashfs. squashfs_bdi_init will set
154 	 * sb->s_bdi->ra_pages and sb->s_bdi->io_pages to 0 and close readahead for
155 	 * squashfs.
156 	 */
157 	err = squashfs_bdi_init(sb);
158 	if (err) {
159 		errorf(fc, "squashfs init bdi failed");
160 		return err;
161 	}
162 
163 	sb->s_fs_info = kzalloc(sizeof(*msblk), GFP_KERNEL);
164 	if (sb->s_fs_info == NULL) {
165 		ERROR("Failed to allocate squashfs_sb_info\n");
166 		return -ENOMEM;
167 	}
168 	msblk = sb->s_fs_info;
169 
170 	msblk->panic_on_errors = (opts->errors == Opt_errors_panic);
171 
172 	msblk->devblksize = sb_min_blocksize(sb, SQUASHFS_DEVBLK_SIZE);
173 	msblk->devblksize_log2 = ffz(~msblk->devblksize);
174 
175 	mutex_init(&msblk->meta_index_mutex);
176 
177 	/*
178 	 * msblk->bytes_used is checked in squashfs_read_table to ensure reads
179 	 * are not beyond filesystem end.  But as we're using
180 	 * squashfs_read_table here to read the superblock (including the value
181 	 * of bytes_used) we need to set it to an initial sensible dummy value
182 	 */
183 	msblk->bytes_used = sizeof(*sblk);
184 	sblk = squashfs_read_table(sb, SQUASHFS_START, sizeof(*sblk));
185 
186 	if (IS_ERR(sblk)) {
187 		errorf(fc, "unable to read squashfs_super_block");
188 		err = PTR_ERR(sblk);
189 		sblk = NULL;
190 		goto failed_mount;
191 	}
192 
193 	err = -EINVAL;
194 
195 	/* Check it is a SQUASHFS superblock */
196 	sb->s_magic = le32_to_cpu(sblk->s_magic);
197 	if (sb->s_magic != SQUASHFS_MAGIC) {
198 		if (!(fc->sb_flags & SB_SILENT))
199 			errorf(fc, "Can't find a SQUASHFS superblock on %pg",
200 			       sb->s_bdev);
201 		goto failed_mount;
202 	}
203 
204 	/* Check the MAJOR & MINOR versions and lookup compression type */
205 	msblk->decompressor = supported_squashfs_filesystem(
206 			fc,
207 			le16_to_cpu(sblk->s_major),
208 			le16_to_cpu(sblk->s_minor),
209 			le16_to_cpu(sblk->compression));
210 	if (msblk->decompressor == NULL)
211 		goto failed_mount;
212 
213 	/* Check the filesystem does not extend beyond the end of the
214 	   block device */
215 	msblk->bytes_used = le64_to_cpu(sblk->bytes_used);
216 	if (msblk->bytes_used < 0 ||
217 	    msblk->bytes_used > bdev_nr_bytes(sb->s_bdev))
218 		goto failed_mount;
219 
220 	/* Check block size for sanity */
221 	msblk->block_size = le32_to_cpu(sblk->block_size);
222 	if (msblk->block_size > SQUASHFS_FILE_MAX_SIZE)
223 		goto insanity;
224 
225 	/*
226 	 * Check the system page size is not larger than the filesystem
227 	 * block size (by default 128K).  This is currently not supported.
228 	 */
229 	if (PAGE_SIZE > msblk->block_size) {
230 		errorf(fc, "Page size > filesystem block size (%d).  This is "
231 		       "currently not supported!", msblk->block_size);
232 		goto failed_mount;
233 	}
234 
235 	/* Check block log for sanity */
236 	msblk->block_log = le16_to_cpu(sblk->block_log);
237 	if (msblk->block_log > SQUASHFS_FILE_MAX_LOG)
238 		goto failed_mount;
239 
240 	/* Check that block_size and block_log match */
241 	if (msblk->block_size != (1 << msblk->block_log))
242 		goto insanity;
243 
244 	/* Check the root inode for sanity */
245 	root_inode = le64_to_cpu(sblk->root_inode);
246 	if (SQUASHFS_INODE_OFFSET(root_inode) > SQUASHFS_METADATA_SIZE)
247 		goto insanity;
248 
249 	msblk->inode_table = le64_to_cpu(sblk->inode_table_start);
250 	msblk->directory_table = le64_to_cpu(sblk->directory_table_start);
251 	msblk->inodes = le32_to_cpu(sblk->inodes);
252 	msblk->fragments = le32_to_cpu(sblk->fragments);
253 	msblk->ids = le16_to_cpu(sblk->no_ids);
254 	flags = le16_to_cpu(sblk->flags);
255 
256 	TRACE("Found valid superblock on %pg\n", sb->s_bdev);
257 	TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(flags)
258 				? "un" : "");
259 	TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(flags)
260 				? "un" : "");
261 	TRACE("Filesystem size %lld bytes\n", msblk->bytes_used);
262 	TRACE("Block size %d\n", msblk->block_size);
263 	TRACE("Number of inodes %d\n", msblk->inodes);
264 	TRACE("Number of fragments %d\n", msblk->fragments);
265 	TRACE("Number of ids %d\n", msblk->ids);
266 	TRACE("sblk->inode_table_start %llx\n", msblk->inode_table);
267 	TRACE("sblk->directory_table_start %llx\n", msblk->directory_table);
268 	TRACE("sblk->fragment_table_start %llx\n",
269 		(u64) le64_to_cpu(sblk->fragment_table_start));
270 	TRACE("sblk->id_table_start %llx\n",
271 		(u64) le64_to_cpu(sblk->id_table_start));
272 
273 	sb->s_maxbytes = MAX_LFS_FILESIZE;
274 	sb->s_time_min = 0;
275 	sb->s_time_max = U32_MAX;
276 	sb->s_flags |= SB_RDONLY;
277 	sb->s_op = &squashfs_super_ops;
278 
279 	err = -ENOMEM;
280 
281 	msblk->block_cache = squashfs_cache_init("metadata",
282 			SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
283 	if (msblk->block_cache == NULL)
284 		goto failed_mount;
285 
286 	/* Allocate read_page block */
287 	msblk->read_page = squashfs_cache_init("data",
288 		squashfs_max_decompressors(), msblk->block_size);
289 	if (msblk->read_page == NULL) {
290 		errorf(fc, "Failed to allocate read_page block");
291 		goto failed_mount;
292 	}
293 
294 	msblk->stream = squashfs_decompressor_setup(sb, flags);
295 	if (IS_ERR(msblk->stream)) {
296 		err = PTR_ERR(msblk->stream);
297 		msblk->stream = NULL;
298 		goto insanity;
299 	}
300 
301 	/* Handle xattrs */
302 	sb->s_xattr = squashfs_xattr_handlers;
303 	xattr_id_table_start = le64_to_cpu(sblk->xattr_id_table_start);
304 	if (xattr_id_table_start == SQUASHFS_INVALID_BLK) {
305 		next_table = msblk->bytes_used;
306 		goto allocate_id_index_table;
307 	}
308 
309 	/* Allocate and read xattr id lookup table */
310 	msblk->xattr_id_table = squashfs_read_xattr_id_table(sb,
311 		xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids);
312 	if (IS_ERR(msblk->xattr_id_table)) {
313 		errorf(fc, "unable to read xattr id index table");
314 		err = PTR_ERR(msblk->xattr_id_table);
315 		msblk->xattr_id_table = NULL;
316 		if (err != -ENOTSUPP)
317 			goto failed_mount;
318 	}
319 	next_table = msblk->xattr_table;
320 
321 allocate_id_index_table:
322 	/* Allocate and read id index table */
323 	msblk->id_table = squashfs_read_id_index_table(sb,
324 		le64_to_cpu(sblk->id_table_start), next_table, msblk->ids);
325 	if (IS_ERR(msblk->id_table)) {
326 		errorf(fc, "unable to read id index table");
327 		err = PTR_ERR(msblk->id_table);
328 		msblk->id_table = NULL;
329 		goto failed_mount;
330 	}
331 	next_table = le64_to_cpu(msblk->id_table[0]);
332 
333 	/* Handle inode lookup table */
334 	lookup_table_start = le64_to_cpu(sblk->lookup_table_start);
335 	if (lookup_table_start == SQUASHFS_INVALID_BLK)
336 		goto handle_fragments;
337 
338 	/* Allocate and read inode lookup table */
339 	msblk->inode_lookup_table = squashfs_read_inode_lookup_table(sb,
340 		lookup_table_start, next_table, msblk->inodes);
341 	if (IS_ERR(msblk->inode_lookup_table)) {
342 		errorf(fc, "unable to read inode lookup table");
343 		err = PTR_ERR(msblk->inode_lookup_table);
344 		msblk->inode_lookup_table = NULL;
345 		goto failed_mount;
346 	}
347 	next_table = le64_to_cpu(msblk->inode_lookup_table[0]);
348 
349 	sb->s_export_op = &squashfs_export_ops;
350 
351 handle_fragments:
352 	fragments = msblk->fragments;
353 	if (fragments == 0)
354 		goto check_directory_table;
355 
356 	msblk->fragment_cache = squashfs_cache_init("fragment",
357 		SQUASHFS_CACHED_FRAGMENTS, msblk->block_size);
358 	if (msblk->fragment_cache == NULL) {
359 		err = -ENOMEM;
360 		goto failed_mount;
361 	}
362 
363 	/* Allocate and read fragment index table */
364 	msblk->fragment_index = squashfs_read_fragment_index_table(sb,
365 		le64_to_cpu(sblk->fragment_table_start), next_table, fragments);
366 	if (IS_ERR(msblk->fragment_index)) {
367 		errorf(fc, "unable to read fragment index table");
368 		err = PTR_ERR(msblk->fragment_index);
369 		msblk->fragment_index = NULL;
370 		goto failed_mount;
371 	}
372 	next_table = le64_to_cpu(msblk->fragment_index[0]);
373 
374 check_directory_table:
375 	/* Sanity check directory_table */
376 	if (msblk->directory_table > next_table) {
377 		err = -EINVAL;
378 		goto insanity;
379 	}
380 
381 	/* Sanity check inode_table */
382 	if (msblk->inode_table >= msblk->directory_table) {
383 		err = -EINVAL;
384 		goto insanity;
385 	}
386 
387 	/* allocate root */
388 	root = new_inode(sb);
389 	if (!root) {
390 		err = -ENOMEM;
391 		goto failed_mount;
392 	}
393 
394 	err = squashfs_read_inode(root, root_inode);
395 	if (err) {
396 		make_bad_inode(root);
397 		iput(root);
398 		goto failed_mount;
399 	}
400 	insert_inode_hash(root);
401 
402 	sb->s_root = d_make_root(root);
403 	if (sb->s_root == NULL) {
404 		ERROR("Root inode create failed\n");
405 		err = -ENOMEM;
406 		goto failed_mount;
407 	}
408 
409 	TRACE("Leaving squashfs_fill_super\n");
410 	kfree(sblk);
411 	return 0;
412 
413 insanity:
414 	errorf(fc, "squashfs image failed sanity check");
415 failed_mount:
416 	squashfs_cache_delete(msblk->block_cache);
417 	squashfs_cache_delete(msblk->fragment_cache);
418 	squashfs_cache_delete(msblk->read_page);
419 	squashfs_decompressor_destroy(msblk);
420 	kfree(msblk->inode_lookup_table);
421 	kfree(msblk->fragment_index);
422 	kfree(msblk->id_table);
423 	kfree(msblk->xattr_id_table);
424 	kfree(sb->s_fs_info);
425 	sb->s_fs_info = NULL;
426 	kfree(sblk);
427 	return err;
428 }
429 
430 static int squashfs_get_tree(struct fs_context *fc)
431 {
432 	return get_tree_bdev(fc, squashfs_fill_super);
433 }
434 
435 static int squashfs_reconfigure(struct fs_context *fc)
436 {
437 	struct super_block *sb = fc->root->d_sb;
438 	struct squashfs_sb_info *msblk = sb->s_fs_info;
439 	struct squashfs_mount_opts *opts = fc->fs_private;
440 
441 	sync_filesystem(fc->root->d_sb);
442 	fc->sb_flags |= SB_RDONLY;
443 
444 	msblk->panic_on_errors = (opts->errors == Opt_errors_panic);
445 
446 	return 0;
447 }
448 
449 static void squashfs_free_fs_context(struct fs_context *fc)
450 {
451 	kfree(fc->fs_private);
452 }
453 
454 static const struct fs_context_operations squashfs_context_ops = {
455 	.get_tree	= squashfs_get_tree,
456 	.free		= squashfs_free_fs_context,
457 	.parse_param	= squashfs_parse_param,
458 	.reconfigure	= squashfs_reconfigure,
459 };
460 
461 static int squashfs_show_options(struct seq_file *s, struct dentry *root)
462 {
463 	struct super_block *sb = root->d_sb;
464 	struct squashfs_sb_info *msblk = sb->s_fs_info;
465 
466 	if (msblk->panic_on_errors)
467 		seq_puts(s, ",errors=panic");
468 	else
469 		seq_puts(s, ",errors=continue");
470 
471 	return 0;
472 }
473 
474 static int squashfs_init_fs_context(struct fs_context *fc)
475 {
476 	struct squashfs_mount_opts *opts;
477 
478 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
479 	if (!opts)
480 		return -ENOMEM;
481 
482 	fc->fs_private = opts;
483 	fc->ops = &squashfs_context_ops;
484 	return 0;
485 }
486 
487 static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
488 {
489 	struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
490 	u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
491 
492 	TRACE("Entered squashfs_statfs\n");
493 
494 	buf->f_type = SQUASHFS_MAGIC;
495 	buf->f_bsize = msblk->block_size;
496 	buf->f_blocks = ((msblk->bytes_used - 1) >> msblk->block_log) + 1;
497 	buf->f_bfree = buf->f_bavail = 0;
498 	buf->f_files = msblk->inodes;
499 	buf->f_ffree = 0;
500 	buf->f_namelen = SQUASHFS_NAME_LEN;
501 	buf->f_fsid = u64_to_fsid(id);
502 
503 	return 0;
504 }
505 
506 
507 static void squashfs_put_super(struct super_block *sb)
508 {
509 	if (sb->s_fs_info) {
510 		struct squashfs_sb_info *sbi = sb->s_fs_info;
511 		squashfs_cache_delete(sbi->block_cache);
512 		squashfs_cache_delete(sbi->fragment_cache);
513 		squashfs_cache_delete(sbi->read_page);
514 		squashfs_decompressor_destroy(sbi);
515 		kfree(sbi->id_table);
516 		kfree(sbi->fragment_index);
517 		kfree(sbi->meta_index);
518 		kfree(sbi->inode_lookup_table);
519 		kfree(sbi->xattr_id_table);
520 		kfree(sb->s_fs_info);
521 		sb->s_fs_info = NULL;
522 	}
523 }
524 
525 static struct kmem_cache *squashfs_inode_cachep;
526 
527 
528 static void init_once(void *foo)
529 {
530 	struct squashfs_inode_info *ei = foo;
531 
532 	inode_init_once(&ei->vfs_inode);
533 }
534 
535 
536 static int __init init_inodecache(void)
537 {
538 	squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
539 		sizeof(struct squashfs_inode_info), 0,
540 		SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
541 		init_once);
542 
543 	return squashfs_inode_cachep ? 0 : -ENOMEM;
544 }
545 
546 
547 static void destroy_inodecache(void)
548 {
549 	/*
550 	 * Make sure all delayed rcu free inodes are flushed before we
551 	 * destroy cache.
552 	 */
553 	rcu_barrier();
554 	kmem_cache_destroy(squashfs_inode_cachep);
555 }
556 
557 
558 static int __init init_squashfs_fs(void)
559 {
560 	int err = init_inodecache();
561 
562 	if (err)
563 		return err;
564 
565 	err = register_filesystem(&squashfs_fs_type);
566 	if (err) {
567 		destroy_inodecache();
568 		return err;
569 	}
570 
571 	pr_info("version 4.0 (2009/01/31) Phillip Lougher\n");
572 
573 	return 0;
574 }
575 
576 
577 static void __exit exit_squashfs_fs(void)
578 {
579 	unregister_filesystem(&squashfs_fs_type);
580 	destroy_inodecache();
581 }
582 
583 
584 static struct inode *squashfs_alloc_inode(struct super_block *sb)
585 {
586 	struct squashfs_inode_info *ei =
587 		alloc_inode_sb(sb, squashfs_inode_cachep, GFP_KERNEL);
588 
589 	return ei ? &ei->vfs_inode : NULL;
590 }
591 
592 
593 static void squashfs_free_inode(struct inode *inode)
594 {
595 	kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
596 }
597 
598 static struct file_system_type squashfs_fs_type = {
599 	.owner = THIS_MODULE,
600 	.name = "squashfs",
601 	.init_fs_context = squashfs_init_fs_context,
602 	.parameters = squashfs_fs_parameters,
603 	.kill_sb = kill_block_super,
604 	.fs_flags = FS_REQUIRES_DEV
605 };
606 MODULE_ALIAS_FS("squashfs");
607 
608 static const struct super_operations squashfs_super_ops = {
609 	.alloc_inode = squashfs_alloc_inode,
610 	.free_inode = squashfs_free_inode,
611 	.statfs = squashfs_statfs,
612 	.put_super = squashfs_put_super,
613 	.show_options = squashfs_show_options,
614 };
615 
616 module_init(init_squashfs_fs);
617 module_exit(exit_squashfs_fs);
618 MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
619 MODULE_AUTHOR("Phillip Lougher <phillip@squashfs.org.uk>");
620 MODULE_LICENSE("GPL");
621