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