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 msblk->devblksize_log2 = ffz(~msblk->devblksize);
122
123 mutex_init(&msblk->meta_index_mutex);
124
125 /*
126 * msblk->bytes_used is checked in squashfs_read_table to ensure reads
127 * are not beyond filesystem end. But as we're using
128 * squashfs_read_table here to read the superblock (including the value
129 * of bytes_used) we need to set it to an initial sensible dummy value
130 */
131 msblk->bytes_used = sizeof(*sblk);
132 sblk = squashfs_read_table(sb, SQUASHFS_START, sizeof(*sblk));
133
134 if (IS_ERR(sblk)) {
135 errorf(fc, "unable to read squashfs_super_block");
136 err = PTR_ERR(sblk);
137 sblk = NULL;
138 goto failed_mount;
139 }
140
141 err = -EINVAL;
142
143 /* Check it is a SQUASHFS superblock */
144 sb->s_magic = le32_to_cpu(sblk->s_magic);
145 if (sb->s_magic != SQUASHFS_MAGIC) {
146 if (!(fc->sb_flags & SB_SILENT))
147 errorf(fc, "Can't find a SQUASHFS superblock on %pg",
148 sb->s_bdev);
149 goto failed_mount;
150 }
151
152 /* Check the MAJOR & MINOR versions and lookup compression type */
153 msblk->decompressor = supported_squashfs_filesystem(
154 fc,
155 le16_to_cpu(sblk->s_major),
156 le16_to_cpu(sblk->s_minor),
157 le16_to_cpu(sblk->compression));
158 if (msblk->decompressor == NULL)
159 goto failed_mount;
160
161 /* Check the filesystem does not extend beyond the end of the
162 block device */
163 msblk->bytes_used = le64_to_cpu(sblk->bytes_used);
164 if (msblk->bytes_used < 0 || msblk->bytes_used >
165 i_size_read(sb->s_bdev->bd_inode))
166 goto failed_mount;
167
168 /* Check block size for sanity */
169 msblk->block_size = le32_to_cpu(sblk->block_size);
170 if (msblk->block_size > SQUASHFS_FILE_MAX_SIZE)
171 goto insanity;
172
173 /*
174 * Check the system page size is not larger than the filesystem
175 * block size (by default 128K). This is currently not supported.
176 */
177 if (PAGE_SIZE > msblk->block_size) {
178 errorf(fc, "Page size > filesystem block size (%d). This is "
179 "currently not supported!", msblk->block_size);
180 goto failed_mount;
181 }
182
183 /* Check block log for sanity */
184 msblk->block_log = le16_to_cpu(sblk->block_log);
185 if (msblk->block_log > SQUASHFS_FILE_MAX_LOG)
186 goto failed_mount;
187
188 /* Check that block_size and block_log match */
189 if (msblk->block_size != (1 << msblk->block_log))
190 goto insanity;
191
192 /* Check the root inode for sanity */
193 root_inode = le64_to_cpu(sblk->root_inode);
194 if (SQUASHFS_INODE_OFFSET(root_inode) > SQUASHFS_METADATA_SIZE)
195 goto insanity;
196
197 msblk->inode_table = le64_to_cpu(sblk->inode_table_start);
198 msblk->directory_table = le64_to_cpu(sblk->directory_table_start);
199 msblk->inodes = le32_to_cpu(sblk->inodes);
200 msblk->fragments = le32_to_cpu(sblk->fragments);
201 msblk->ids = le16_to_cpu(sblk->no_ids);
202 flags = le16_to_cpu(sblk->flags);
203
204 TRACE("Found valid superblock on %pg\n", sb->s_bdev);
205 TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(flags)
206 ? "un" : "");
207 TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(flags)
208 ? "un" : "");
209 TRACE("Filesystem size %lld bytes\n", msblk->bytes_used);
210 TRACE("Block size %d\n", msblk->block_size);
211 TRACE("Number of inodes %d\n", msblk->inodes);
212 TRACE("Number of fragments %d\n", msblk->fragments);
213 TRACE("Number of ids %d\n", msblk->ids);
214 TRACE("sblk->inode_table_start %llx\n", msblk->inode_table);
215 TRACE("sblk->directory_table_start %llx\n", msblk->directory_table);
216 TRACE("sblk->fragment_table_start %llx\n",
217 (u64) le64_to_cpu(sblk->fragment_table_start));
218 TRACE("sblk->id_table_start %llx\n",
219 (u64) le64_to_cpu(sblk->id_table_start));
220
221 sb->s_maxbytes = MAX_LFS_FILESIZE;
222 sb->s_time_min = 0;
223 sb->s_time_max = U32_MAX;
224 sb->s_flags |= SB_RDONLY;
225 sb->s_op = &squashfs_super_ops;
226
227 err = -ENOMEM;
228
229 msblk->block_cache = squashfs_cache_init("metadata",
230 SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
231 if (msblk->block_cache == NULL)
232 goto failed_mount;
233
234 /* Allocate read_page block */
235 msblk->read_page = squashfs_cache_init("data",
236 squashfs_max_decompressors(), msblk->block_size);
237 if (msblk->read_page == NULL) {
238 errorf(fc, "Failed to allocate read_page block");
239 goto failed_mount;
240 }
241
242 msblk->stream = squashfs_decompressor_setup(sb, flags);
243 if (IS_ERR(msblk->stream)) {
244 err = PTR_ERR(msblk->stream);
245 msblk->stream = NULL;
246 goto insanity;
247 }
248
249 /* Handle xattrs */
250 sb->s_xattr = squashfs_xattr_handlers;
251 xattr_id_table_start = le64_to_cpu(sblk->xattr_id_table_start);
252 if (xattr_id_table_start == SQUASHFS_INVALID_BLK) {
253 next_table = msblk->bytes_used;
254 goto allocate_id_index_table;
255 }
256
257 /* Allocate and read xattr id lookup table */
258 msblk->xattr_id_table = squashfs_read_xattr_id_table(sb,
259 xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids);
260 if (IS_ERR(msblk->xattr_id_table)) {
261 errorf(fc, "unable to read xattr id index table");
262 err = PTR_ERR(msblk->xattr_id_table);
263 msblk->xattr_id_table = NULL;
264 if (err != -ENOTSUPP)
265 goto failed_mount;
266 }
267 next_table = msblk->xattr_table;
268
269 allocate_id_index_table:
270 /* Allocate and read id index table */
271 msblk->id_table = squashfs_read_id_index_table(sb,
272 le64_to_cpu(sblk->id_table_start), next_table, msblk->ids);
273 if (IS_ERR(msblk->id_table)) {
274 errorf(fc, "unable to read id index table");
275 err = PTR_ERR(msblk->id_table);
276 msblk->id_table = NULL;
277 goto failed_mount;
278 }
279 next_table = le64_to_cpu(msblk->id_table[0]);
280
281 /* Handle inode lookup table */
282 lookup_table_start = le64_to_cpu(sblk->lookup_table_start);
283 if (lookup_table_start == SQUASHFS_INVALID_BLK)
284 goto handle_fragments;
285
286 /* Allocate and read inode lookup table */
287 msblk->inode_lookup_table = squashfs_read_inode_lookup_table(sb,
288 lookup_table_start, next_table, msblk->inodes);
289 if (IS_ERR(msblk->inode_lookup_table)) {
290 errorf(fc, "unable to read inode lookup table");
291 err = PTR_ERR(msblk->inode_lookup_table);
292 msblk->inode_lookup_table = NULL;
293 goto failed_mount;
294 }
295 next_table = le64_to_cpu(msblk->inode_lookup_table[0]);
296
297 sb->s_export_op = &squashfs_export_ops;
298
299 handle_fragments:
300 fragments = msblk->fragments;
301 if (fragments == 0)
302 goto check_directory_table;
303
304 msblk->fragment_cache = squashfs_cache_init("fragment",
305 SQUASHFS_CACHED_FRAGMENTS, msblk->block_size);
306 if (msblk->fragment_cache == NULL) {
307 err = -ENOMEM;
308 goto failed_mount;
309 }
310
311 /* Allocate and read fragment index table */
312 msblk->fragment_index = squashfs_read_fragment_index_table(sb,
313 le64_to_cpu(sblk->fragment_table_start), next_table, fragments);
314 if (IS_ERR(msblk->fragment_index)) {
315 errorf(fc, "unable to read fragment index table");
316 err = PTR_ERR(msblk->fragment_index);
317 msblk->fragment_index = NULL;
318 goto failed_mount;
319 }
320 next_table = le64_to_cpu(msblk->fragment_index[0]);
321
322 check_directory_table:
323 /* Sanity check directory_table */
324 if (msblk->directory_table > next_table) {
325 err = -EINVAL;
326 goto insanity;
327 }
328
329 /* Sanity check inode_table */
330 if (msblk->inode_table >= msblk->directory_table) {
331 err = -EINVAL;
332 goto insanity;
333 }
334
335 /* allocate root */
336 root = new_inode(sb);
337 if (!root) {
338 err = -ENOMEM;
339 goto failed_mount;
340 }
341
342 err = squashfs_read_inode(root, root_inode);
343 if (err) {
344 make_bad_inode(root);
345 iput(root);
346 goto failed_mount;
347 }
348 insert_inode_hash(root);
349
350 sb->s_root = d_make_root(root);
351 if (sb->s_root == NULL) {
352 ERROR("Root inode create failed\n");
353 err = -ENOMEM;
354 goto failed_mount;
355 }
356
357 TRACE("Leaving squashfs_fill_super\n");
358 kfree(sblk);
359 return 0;
360
361 insanity:
362 errorf(fc, "squashfs image failed sanity check");
363 failed_mount:
364 squashfs_cache_delete(msblk->block_cache);
365 squashfs_cache_delete(msblk->fragment_cache);
366 squashfs_cache_delete(msblk->read_page);
367 squashfs_decompressor_destroy(msblk);
368 kfree(msblk->inode_lookup_table);
369 kfree(msblk->fragment_index);
370 kfree(msblk->id_table);
371 kfree(msblk->xattr_id_table);
372 kfree(sb->s_fs_info);
373 sb->s_fs_info = NULL;
374 kfree(sblk);
375 return err;
376 }
377
squashfs_get_tree(struct fs_context * fc)378 static int squashfs_get_tree(struct fs_context *fc)
379 {
380 return get_tree_bdev(fc, squashfs_fill_super);
381 }
382
squashfs_reconfigure(struct fs_context * fc)383 static int squashfs_reconfigure(struct fs_context *fc)
384 {
385 sync_filesystem(fc->root->d_sb);
386 fc->sb_flags |= SB_RDONLY;
387 return 0;
388 }
389
390 static const struct fs_context_operations squashfs_context_ops = {
391 .get_tree = squashfs_get_tree,
392 .reconfigure = squashfs_reconfigure,
393 };
394
squashfs_init_fs_context(struct fs_context * fc)395 static int squashfs_init_fs_context(struct fs_context *fc)
396 {
397 fc->ops = &squashfs_context_ops;
398 return 0;
399 }
400
squashfs_statfs(struct dentry * dentry,struct kstatfs * buf)401 static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
402 {
403 struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
404 u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
405
406 TRACE("Entered squashfs_statfs\n");
407
408 buf->f_type = SQUASHFS_MAGIC;
409 buf->f_bsize = msblk->block_size;
410 buf->f_blocks = ((msblk->bytes_used - 1) >> msblk->block_log) + 1;
411 buf->f_bfree = buf->f_bavail = 0;
412 buf->f_files = msblk->inodes;
413 buf->f_ffree = 0;
414 buf->f_namelen = SQUASHFS_NAME_LEN;
415 buf->f_fsid = u64_to_fsid(id);
416
417 return 0;
418 }
419
420
squashfs_put_super(struct super_block * sb)421 static void squashfs_put_super(struct super_block *sb)
422 {
423 if (sb->s_fs_info) {
424 struct squashfs_sb_info *sbi = sb->s_fs_info;
425 squashfs_cache_delete(sbi->block_cache);
426 squashfs_cache_delete(sbi->fragment_cache);
427 squashfs_cache_delete(sbi->read_page);
428 squashfs_decompressor_destroy(sbi);
429 kfree(sbi->id_table);
430 kfree(sbi->fragment_index);
431 kfree(sbi->meta_index);
432 kfree(sbi->inode_lookup_table);
433 kfree(sbi->xattr_id_table);
434 kfree(sb->s_fs_info);
435 sb->s_fs_info = NULL;
436 }
437 }
438
439 static struct kmem_cache *squashfs_inode_cachep;
440
441
init_once(void * foo)442 static void init_once(void *foo)
443 {
444 struct squashfs_inode_info *ei = foo;
445
446 inode_init_once(&ei->vfs_inode);
447 }
448
449
init_inodecache(void)450 static int __init init_inodecache(void)
451 {
452 squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
453 sizeof(struct squashfs_inode_info), 0,
454 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
455 init_once);
456
457 return squashfs_inode_cachep ? 0 : -ENOMEM;
458 }
459
460
destroy_inodecache(void)461 static void destroy_inodecache(void)
462 {
463 /*
464 * Make sure all delayed rcu free inodes are flushed before we
465 * destroy cache.
466 */
467 rcu_barrier();
468 kmem_cache_destroy(squashfs_inode_cachep);
469 }
470
471
init_squashfs_fs(void)472 static int __init init_squashfs_fs(void)
473 {
474 int err = init_inodecache();
475
476 if (err)
477 return err;
478
479 err = register_filesystem(&squashfs_fs_type);
480 if (err) {
481 destroy_inodecache();
482 return err;
483 }
484
485 pr_info("version 4.0 (2009/01/31) Phillip Lougher\n");
486
487 return 0;
488 }
489
490
exit_squashfs_fs(void)491 static void __exit exit_squashfs_fs(void)
492 {
493 unregister_filesystem(&squashfs_fs_type);
494 destroy_inodecache();
495 }
496
497
squashfs_alloc_inode(struct super_block * sb)498 static struct inode *squashfs_alloc_inode(struct super_block *sb)
499 {
500 struct squashfs_inode_info *ei =
501 kmem_cache_alloc(squashfs_inode_cachep, GFP_KERNEL);
502
503 return ei ? &ei->vfs_inode : NULL;
504 }
505
506
squashfs_free_inode(struct inode * inode)507 static void squashfs_free_inode(struct inode *inode)
508 {
509 kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
510 }
511
512 static struct file_system_type squashfs_fs_type = {
513 .owner = THIS_MODULE,
514 .name = "squashfs",
515 .init_fs_context = squashfs_init_fs_context,
516 .kill_sb = kill_block_super,
517 .fs_flags = FS_REQUIRES_DEV
518 };
519 MODULE_ALIAS_FS("squashfs");
520
521 static const struct super_operations squashfs_super_ops = {
522 .alloc_inode = squashfs_alloc_inode,
523 .free_inode = squashfs_free_inode,
524 .statfs = squashfs_statfs,
525 .put_super = squashfs_put_super,
526 };
527
528 module_init(init_squashfs_fs);
529 module_exit(exit_squashfs_fs);
530 MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
531 MODULE_AUTHOR("Phillip Lougher <phillip@squashfs.org.uk>");
532 MODULE_LICENSE("GPL");
533