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