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