blob: b1b556dbce12df87dc5b9a9b3228919809f0c145 [file] [log] [blame]
Thomas Gleixner68252eb2019-05-20 19:08:00 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Phillip Lougher0aa66612009-01-05 08:46:25 +00002/*
3 * Squashfs - a compressed read only filesystem for Linux
4 *
5 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
Phillip Lougherd7f2ff62011-05-26 10:39:56 +01006 * Phillip Lougher <phillip@squashfs.org.uk>
Phillip Lougher0aa66612009-01-05 08:46:25 +00007 *
Phillip Lougher0aa66612009-01-05 08:46:25 +00008 * 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
Fabian Frederickc811f5f2014-08-06 16:03:52 -070017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Christoph Hellwigbe9a7b32021-10-18 12:11:23 +020019#include <linux/blkdev.h>
Phillip Lougher0aa66612009-01-05 08:46:25 +000020#include <linux/fs.h>
David Howells5a2be122019-03-25 16:38:32 +000021#include <linux/fs_context.h>
Vincent Whitchurch10dde052021-06-28 19:33:55 -070022#include <linux/fs_parser.h>
Phillip Lougher0aa66612009-01-05 08:46:25 +000023#include <linux/vfs.h>
24#include <linux/slab.h>
25#include <linux/mutex.h>
Vincent Whitchurch10dde052021-06-28 19:33:55 -070026#include <linux/seq_file.h>
Phillip Lougher0aa66612009-01-05 08:46:25 +000027#include <linux/pagemap.h>
28#include <linux/init.h>
29#include <linux/module.h>
Qinghuang Feng1bcbf312009-01-15 13:51:03 -080030#include <linux/magic.h>
Phillip Lougher4b5397d2010-05-14 20:48:47 +010031#include <linux/xattr.h>
Zheng Liang9eec1d82022-01-14 14:03:31 -080032#include <linux/backing-dev.h>
Phillip Lougher0aa66612009-01-05 08:46:25 +000033
34#include "squashfs_fs.h"
35#include "squashfs_fs_sb.h"
36#include "squashfs_fs_i.h"
37#include "squashfs.h"
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010038#include "decompressor.h"
Phillip Lougher01e5b4e2010-05-17 19:39:02 +010039#include "xattr.h"
Phillip Lougher0aa66612009-01-05 08:46:25 +000040
41static struct file_system_type squashfs_fs_type;
Alexey Dobriyanb87221d2009-09-21 17:01:09 -070042static const struct super_operations squashfs_super_ops;
Phillip Lougher0aa66612009-01-05 08:46:25 +000043
Vincent Whitchurch10dde052021-06-28 19:33:55 -070044enum Opt_errors {
45 Opt_errors_continue,
46 Opt_errors_panic,
47};
48
49enum squashfs_param {
50 Opt_errors,
51};
52
53struct squashfs_mount_opts {
54 enum Opt_errors errors;
55};
56
57static const struct constant_table squashfs_param_errors[] = {
58 {"continue", Opt_errors_continue },
59 {"panic", Opt_errors_panic },
60 {}
61};
62
63static const struct fs_parameter_spec squashfs_fs_parameters[] = {
64 fsparam_enum("errors", Opt_errors, squashfs_param_errors),
65 {}
66};
67
68static 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
David Howells5a2be122019-03-25 16:38:32 +000089static const struct squashfs_decompressor *supported_squashfs_filesystem(
90 struct fs_context *fc,
91 short major, short minor, short id)
Phillip Lougher0aa66612009-01-05 08:46:25 +000092{
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010093 const struct squashfs_decompressor *decompressor;
94
Phillip Lougher0aa66612009-01-05 08:46:25 +000095 if (major < SQUASHFS_MAJOR) {
David Howells5a2be122019-03-25 16:38:32 +000096 errorf(fc, "Major/Minor mismatch, older Squashfs %d.%d "
97 "filesystems are unsupported", major, minor);
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010098 return NULL;
Phillip Lougher0aa66612009-01-05 08:46:25 +000099 } else if (major > SQUASHFS_MAJOR || minor > SQUASHFS_MINOR) {
David Howells5a2be122019-03-25 16:38:32 +0000100 errorf(fc, "Major/Minor mismatch, trying to mount newer "
101 "%d.%d filesystem", major, minor);
102 errorf(fc, "Please update your kernel");
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100103 return NULL;
Phillip Lougher0aa66612009-01-05 08:46:25 +0000104 }
105
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100106 decompressor = squashfs_lookup_decompressor(id);
107 if (!decompressor->supported) {
David Howells5a2be122019-03-25 16:38:32 +0000108 errorf(fc, "Filesystem uses \"%s\" compression. This is not supported",
109 decompressor->name);
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100110 return NULL;
111 }
Phillip Lougher0aa66612009-01-05 08:46:25 +0000112
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100113 return decompressor;
Phillip Lougher0aa66612009-01-05 08:46:25 +0000114}
115
Zheng Liang9eec1d82022-01-14 14:03:31 -0800116static 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}
Phillip Lougher0aa66612009-01-05 08:46:25 +0000134
David Howells5a2be122019-03-25 16:38:32 +0000135static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc)
Phillip Lougher0aa66612009-01-05 08:46:25 +0000136{
Vincent Whitchurch10dde052021-06-28 19:33:55 -0700137 struct squashfs_mount_opts *opts = fc->fs_private;
Phillip Lougher0aa66612009-01-05 08:46:25 +0000138 struct squashfs_sb_info *msblk;
139 struct squashfs_super_block *sblk = NULL;
Phillip Lougher0aa66612009-01-05 08:46:25 +0000140 struct inode *root;
141 long long root_inode;
142 unsigned short flags;
143 unsigned int fragments;
Phillip Lougher37986f62011-05-24 04:05:22 +0100144 u64 lookup_table_start, xattr_id_table_start, next_table;
Phillip Lougher0aa66612009-01-05 08:46:25 +0000145 int err;
146
147 TRACE("Entered squashfs_fill_superblock\n");
148
Zheng Liang9eec1d82022-01-14 14:03:31 -0800149 /*
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 readpage,
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
Phillip Lougher0aa66612009-01-05 08:46:25 +0000163 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
Vincent Whitchurch10dde052021-06-28 19:33:55 -0700170 msblk->panic_on_errors = (opts->errors == Opt_errors_panic);
171
Phillip Lougher7657cac2011-10-22 01:34:48 +0100172 msblk->devblksize = sb_min_blocksize(sb, SQUASHFS_DEVBLK_SIZE);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000173 msblk->devblksize_log2 = ffz(~msblk->devblksize);
174
Phillip Lougher0aa66612009-01-05 08:46:25 +0000175 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);
Phillip Lougher82de6472011-05-20 02:26:43 +0100184 sblk = squashfs_read_table(sb, SQUASHFS_START, sizeof(*sblk));
Phillip Lougher0aa66612009-01-05 08:46:25 +0000185
Phillip Lougher82de6472011-05-20 02:26:43 +0100186 if (IS_ERR(sblk)) {
David Howells5a2be122019-03-25 16:38:32 +0000187 errorf(fc, "unable to read squashfs_super_block");
Phillip Lougher82de6472011-05-20 02:26:43 +0100188 err = PTR_ERR(sblk);
189 sblk = NULL;
Phillip Lougher0aa66612009-01-05 08:46:25 +0000190 goto failed_mount;
191 }
192
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100193 err = -EINVAL;
194
Phillip Lougher0aa66612009-01-05 08:46:25 +0000195 /* Check it is a SQUASHFS superblock */
196 sb->s_magic = le32_to_cpu(sblk->s_magic);
197 if (sb->s_magic != SQUASHFS_MAGIC) {
David Howells5a2be122019-03-25 16:38:32 +0000198 if (!(fc->sb_flags & SB_SILENT))
199 errorf(fc, "Can't find a SQUASHFS superblock on %pg",
200 sb->s_bdev);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000201 goto failed_mount;
202 }
203
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100204 /* Check the MAJOR & MINOR versions and lookup compression type */
205 msblk->decompressor = supported_squashfs_filesystem(
David Howells5a2be122019-03-25 16:38:32 +0000206 fc,
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100207 le16_to_cpu(sblk->s_major),
Phillip Lougher0aa66612009-01-05 08:46:25 +0000208 le16_to_cpu(sblk->s_minor),
209 le16_to_cpu(sblk->compression));
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100210 if (msblk->decompressor == NULL)
Phillip Lougher0aa66612009-01-05 08:46:25 +0000211 goto failed_mount;
212
Phillip Lougher0aa66612009-01-05 08:46:25 +0000213 /* Check the filesystem does not extend beyond the end of the
214 block device */
215 msblk->bytes_used = le64_to_cpu(sblk->bytes_used);
Christoph Hellwigbe9a7b32021-10-18 12:11:23 +0200216 if (msblk->bytes_used < 0 ||
217 msblk->bytes_used > bdev_nr_bytes(sb->s_bdev))
Phillip Lougher0aa66612009-01-05 08:46:25 +0000218 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)
David Howells5a2be122019-03-25 16:38:32 +0000223 goto insanity;
Phillip Lougher0aa66612009-01-05 08:46:25 +0000224
Phillip Lougherfffb47b2009-05-13 02:59:26 +0100225 /*
226 * Check the system page size is not larger than the filesystem
227 * block size (by default 128K). This is currently not supported.
228 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300229 if (PAGE_SIZE > msblk->block_size) {
David Howells5a2be122019-03-25 16:38:32 +0000230 errorf(fc, "Page size > filesystem block size (%d). This is "
231 "currently not supported!", msblk->block_size);
Phillip Lougherfffb47b2009-05-13 02:59:26 +0100232 goto failed_mount;
233 }
234
Phillip Lougher4b0180a2012-03-09 03:02:59 +0000235 /* Check block log for sanity */
Phillip Lougher0aa66612009-01-05 08:46:25 +0000236 msblk->block_log = le16_to_cpu(sblk->block_log);
237 if (msblk->block_log > SQUASHFS_FILE_MAX_LOG)
238 goto failed_mount;
239
Phillip Lougher4b0180a2012-03-09 03:02:59 +0000240 /* Check that block_size and block_log match */
241 if (msblk->block_size != (1 << msblk->block_log))
David Howells5a2be122019-03-25 16:38:32 +0000242 goto insanity;
Phillip Lougher4b0180a2012-03-09 03:02:59 +0000243
Phillip Lougher0aa66612009-01-05 08:46:25 +0000244 /* 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)
David Howells5a2be122019-03-25 16:38:32 +0000247 goto insanity;
Phillip Lougher0aa66612009-01-05 08:46:25 +0000248
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);
Linus Torvalds71755ee2018-08-02 08:43:35 -0700252 msblk->fragments = le32_to_cpu(sblk->fragments);
Phillip Lougherf37aa4c2021-02-09 13:41:53 -0800253 msblk->ids = le16_to_cpu(sblk->no_ids);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000254 flags = le16_to_cpu(sblk->flags);
255
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +0400256 TRACE("Found valid superblock on %pg\n", sb->s_bdev);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000257 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);
Linus Torvalds71755ee2018-08-02 08:43:35 -0700264 TRACE("Number of fragments %d\n", msblk->fragments);
Phillip Lougherf37aa4c2021-02-09 13:41:53 -0800265 TRACE("Number of ids %d\n", msblk->ids);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000266 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;
Deepa Dinamani22b13962019-07-30 08:22:29 -0700274 sb->s_time_min = 0;
275 sb->s_time_max = U32_MAX;
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800276 sb->s_flags |= SB_RDONLY;
Phillip Lougher0aa66612009-01-05 08:46:25 +0000277 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 */
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000287 msblk->read_page = squashfs_cache_init("data",
288 squashfs_max_decompressors(), msblk->block_size);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000289 if (msblk->read_page == NULL) {
David Howells5a2be122019-03-25 16:38:32 +0000290 errorf(fc, "Failed to allocate read_page block");
Phillip Lougher0aa66612009-01-05 08:46:25 +0000291 goto failed_mount;
292 }
293
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000294 msblk->stream = squashfs_decompressor_setup(sb, flags);
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +0000295 if (IS_ERR(msblk->stream)) {
296 err = PTR_ERR(msblk->stream);
297 msblk->stream = NULL;
David Howells5a2be122019-03-25 16:38:32 +0000298 goto insanity;
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +0000299 }
300
Phillip Lougher76e002f2011-05-24 02:57:05 +0100301 /* Handle xattrs */
302 sb->s_xattr = squashfs_xattr_handlers;
303 xattr_id_table_start = le64_to_cpu(sblk->xattr_id_table_start);
Phillip Lougher37986f62011-05-24 04:05:22 +0100304 if (xattr_id_table_start == SQUASHFS_INVALID_BLK) {
305 next_table = msblk->bytes_used;
Phillip Lougher76e002f2011-05-24 02:57:05 +0100306 goto allocate_id_index_table;
Phillip Lougher37986f62011-05-24 04:05:22 +0100307 }
Phillip Lougher76e002f2011-05-24 02:57:05 +0100308
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)) {
David Howells5a2be122019-03-25 16:38:32 +0000313 errorf(fc, "unable to read xattr id index table");
Phillip Lougher76e002f2011-05-24 02:57:05 +0100314 err = PTR_ERR(msblk->xattr_id_table);
315 msblk->xattr_id_table = NULL;
316 if (err != -ENOTSUPP)
317 goto failed_mount;
318 }
Phillip Lougher37986f62011-05-24 04:05:22 +0100319 next_table = msblk->xattr_table;
Phillip Lougher76e002f2011-05-24 02:57:05 +0100320
321allocate_id_index_table:
Phillip Lougher0aa66612009-01-05 08:46:25 +0000322 /* Allocate and read id index table */
323 msblk->id_table = squashfs_read_id_index_table(sb,
Phillip Lougherf37aa4c2021-02-09 13:41:53 -0800324 le64_to_cpu(sblk->id_table_start), next_table, msblk->ids);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000325 if (IS_ERR(msblk->id_table)) {
David Howells5a2be122019-03-25 16:38:32 +0000326 errorf(fc, "unable to read id index table");
Phillip Lougher0aa66612009-01-05 08:46:25 +0000327 err = PTR_ERR(msblk->id_table);
328 msblk->id_table = NULL;
329 goto failed_mount;
330 }
Phillip Lougherd5b72ce2011-05-29 00:38:46 +0100331 next_table = le64_to_cpu(msblk->id_table[0]);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000332
Phillip Lougher76e002f2011-05-24 02:57:05 +0100333 /* 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,
Phillip Lougherac51a0a2011-05-24 04:15:21 +0100340 lookup_table_start, next_table, msblk->inodes);
Phillip Lougher76e002f2011-05-24 02:57:05 +0100341 if (IS_ERR(msblk->inode_lookup_table)) {
David Howells5a2be122019-03-25 16:38:32 +0000342 errorf(fc, "unable to read inode lookup table");
Phillip Lougher76e002f2011-05-24 02:57:05 +0100343 err = PTR_ERR(msblk->inode_lookup_table);
344 msblk->inode_lookup_table = NULL;
345 goto failed_mount;
346 }
Phillip Lougherd5b72ce2011-05-29 00:38:46 +0100347 next_table = le64_to_cpu(msblk->inode_lookup_table[0]);
Phillip Lougher76e002f2011-05-24 02:57:05 +0100348
349 sb->s_export_op = &squashfs_export_ops;
350
351handle_fragments:
Linus Torvalds71755ee2018-08-02 08:43:35 -0700352 fragments = msblk->fragments;
Phillip Lougher0aa66612009-01-05 08:46:25 +0000353 if (fragments == 0)
Phillip Lougher1094a4a2011-05-24 04:45:33 +0100354 goto check_directory_table;
Phillip Lougher0aa66612009-01-05 08:46:25 +0000355
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,
Phillip Lougher1cac63c2011-05-24 04:33:34 +0100365 le64_to_cpu(sblk->fragment_table_start), next_table, fragments);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000366 if (IS_ERR(msblk->fragment_index)) {
David Howells5a2be122019-03-25 16:38:32 +0000367 errorf(fc, "unable to read fragment index table");
Phillip Lougher0aa66612009-01-05 08:46:25 +0000368 err = PTR_ERR(msblk->fragment_index);
369 msblk->fragment_index = NULL;
370 goto failed_mount;
371 }
Phillip Lougherd5b72ce2011-05-29 00:38:46 +0100372 next_table = le64_to_cpu(msblk->fragment_index[0]);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000373
Phillip Lougher1094a4a2011-05-24 04:45:33 +0100374check_directory_table:
375 /* Sanity check directory_table */
Phillip Loughercc37f752012-01-02 17:47:14 +0000376 if (msblk->directory_table > next_table) {
Phillip Lougher1094a4a2011-05-24 04:45:33 +0100377 err = -EINVAL;
David Howells5a2be122019-03-25 16:38:32 +0000378 goto insanity;
Phillip Lougher1094a4a2011-05-24 04:45:33 +0100379 }
380
381 /* Sanity check inode_table */
382 if (msblk->inode_table >= msblk->directory_table) {
383 err = -EINVAL;
David Howells5a2be122019-03-25 16:38:32 +0000384 goto insanity;
Phillip Lougher1094a4a2011-05-24 04:45:33 +0100385 }
386
387 /* allocate root */
Phillip Lougher0aa66612009-01-05 08:46:25 +0000388 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) {
Phillip Lougher1cb08e92010-04-16 01:01:36 +0100396 make_bad_inode(root);
397 iput(root);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000398 goto failed_mount;
399 }
400 insert_inode_hash(root);
401
Al Viro48fde702012-01-08 22:15:13 -0500402 sb->s_root = d_make_root(root);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000403 if (sb->s_root == NULL) {
404 ERROR("Root inode create failed\n");
405 err = -ENOMEM;
Phillip Lougher0aa66612009-01-05 08:46:25 +0000406 goto failed_mount;
407 }
408
409 TRACE("Leaving squashfs_fill_super\n");
410 kfree(sblk);
411 return 0;
412
David Howells5a2be122019-03-25 16:38:32 +0000413insanity:
414 errorf(fc, "squashfs image failed sanity check");
Phillip Lougher0aa66612009-01-05 08:46:25 +0000415failed_mount:
416 squashfs_cache_delete(msblk->block_cache);
417 squashfs_cache_delete(msblk->fragment_cache);
418 squashfs_cache_delete(msblk->read_page);
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000419 squashfs_decompressor_destroy(msblk);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000420 kfree(msblk->inode_lookup_table);
421 kfree(msblk->fragment_index);
422 kfree(msblk->id_table);
Phillip Lougher4b5397d2010-05-14 20:48:47 +0100423 kfree(msblk->xattr_id_table);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000424 kfree(sb->s_fs_info);
425 sb->s_fs_info = NULL;
426 kfree(sblk);
427 return err;
Phillip Lougher0aa66612009-01-05 08:46:25 +0000428}
429
David Howells5a2be122019-03-25 16:38:32 +0000430static int squashfs_get_tree(struct fs_context *fc)
431{
432 return get_tree_bdev(fc, squashfs_fill_super);
433}
434
435static int squashfs_reconfigure(struct fs_context *fc)
436{
Vincent Whitchurch10dde052021-06-28 19:33:55 -0700437 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
David Howells5a2be122019-03-25 16:38:32 +0000441 sync_filesystem(fc->root->d_sb);
442 fc->sb_flags |= SB_RDONLY;
Vincent Whitchurch10dde052021-06-28 19:33:55 -0700443
444 msblk->panic_on_errors = (opts->errors == Opt_errors_panic);
445
David Howells5a2be122019-03-25 16:38:32 +0000446 return 0;
447}
448
Vincent Whitchurch10dde052021-06-28 19:33:55 -0700449static void squashfs_free_fs_context(struct fs_context *fc)
450{
451 kfree(fc->fs_private);
452}
453
David Howells5a2be122019-03-25 16:38:32 +0000454static const struct fs_context_operations squashfs_context_ops = {
455 .get_tree = squashfs_get_tree,
Vincent Whitchurch10dde052021-06-28 19:33:55 -0700456 .free = squashfs_free_fs_context,
457 .parse_param = squashfs_parse_param,
David Howells5a2be122019-03-25 16:38:32 +0000458 .reconfigure = squashfs_reconfigure,
459};
460
Vincent Whitchurch10dde052021-06-28 19:33:55 -0700461static 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
David Howells5a2be122019-03-25 16:38:32 +0000474static int squashfs_init_fs_context(struct fs_context *fc)
475{
Vincent Whitchurch10dde052021-06-28 19:33:55 -0700476 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;
David Howells5a2be122019-03-25 16:38:32 +0000483 fc->ops = &squashfs_context_ops;
484 return 0;
485}
Phillip Lougher0aa66612009-01-05 08:46:25 +0000486
487static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
488{
489 struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
Coly Li2fc7f562009-04-02 16:59:42 -0700490 u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000491
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;
Al Viro6d1349c2020-09-18 16:45:50 -0400501 buf->f_fsid = u64_to_fsid(id);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000502
503 return 0;
504}
505
506
Phillip Lougher0aa66612009-01-05 08:46:25 +0000507static 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);
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000514 squashfs_decompressor_destroy(sbi);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000515 kfree(sbi->id_table);
516 kfree(sbi->fragment_index);
517 kfree(sbi->meta_index);
Phillip Lougher370ec3d2010-04-23 00:24:22 +0100518 kfree(sbi->inode_lookup_table);
Phillip Lougher4b5397d2010-05-14 20:48:47 +0100519 kfree(sbi->xattr_id_table);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000520 kfree(sb->s_fs_info);
521 sb->s_fs_info = NULL;
522 }
523}
524
Phillip Lougher0aa66612009-01-05 08:46:25 +0000525static struct kmem_cache *squashfs_inode_cachep;
526
527
528static void init_once(void *foo)
529{
530 struct squashfs_inode_info *ei = foo;
531
532 inode_init_once(&ei->vfs_inode);
533}
534
535
536static int __init init_inodecache(void)
537{
538 squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
539 sizeof(struct squashfs_inode_info), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -0800540 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
541 init_once);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000542
543 return squashfs_inode_cachep ? 0 : -ENOMEM;
544}
545
546
547static void destroy_inodecache(void)
548{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000549 /*
550 * Make sure all delayed rcu free inodes are flushed before we
551 * destroy cache.
552 */
553 rcu_barrier();
Phillip Lougher0aa66612009-01-05 08:46:25 +0000554 kmem_cache_destroy(squashfs_inode_cachep);
555}
556
557
558static 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
Fabian Frederickc811f5f2014-08-06 16:03:52 -0700571 pr_info("version 4.0 (2009/01/31) Phillip Lougher\n");
Phillip Lougher0aa66612009-01-05 08:46:25 +0000572
573 return 0;
574}
575
576
577static void __exit exit_squashfs_fs(void)
578{
579 unregister_filesystem(&squashfs_fs_type);
580 destroy_inodecache();
581}
582
583
584static struct inode *squashfs_alloc_inode(struct super_block *sb)
585{
586 struct squashfs_inode_info *ei =
587 kmem_cache_alloc(squashfs_inode_cachep, GFP_KERNEL);
588
589 return ei ? &ei->vfs_inode : NULL;
590}
591
592
Al Viro56b5af12019-04-15 22:22:40 -0400593static void squashfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100594{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100595 kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
596}
597
Phillip Lougher0aa66612009-01-05 08:46:25 +0000598static struct file_system_type squashfs_fs_type = {
599 .owner = THIS_MODULE,
600 .name = "squashfs",
David Howells5a2be122019-03-25 16:38:32 +0000601 .init_fs_context = squashfs_init_fs_context,
Vincent Whitchurch10dde052021-06-28 19:33:55 -0700602 .parameters = squashfs_fs_parameters,
Phillip Lougher0aa66612009-01-05 08:46:25 +0000603 .kill_sb = kill_block_super,
604 .fs_flags = FS_REQUIRES_DEV
605};
Eric W. Biederman3e64fe52013-03-11 07:05:42 -0700606MODULE_ALIAS_FS("squashfs");
Phillip Lougher0aa66612009-01-05 08:46:25 +0000607
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700608static const struct super_operations squashfs_super_ops = {
Phillip Lougher0aa66612009-01-05 08:46:25 +0000609 .alloc_inode = squashfs_alloc_inode,
Al Viro56b5af12019-04-15 22:22:40 -0400610 .free_inode = squashfs_free_inode,
Phillip Lougher0aa66612009-01-05 08:46:25 +0000611 .statfs = squashfs_statfs,
612 .put_super = squashfs_put_super,
Vincent Whitchurch10dde052021-06-28 19:33:55 -0700613 .show_options = squashfs_show_options,
Phillip Lougher0aa66612009-01-05 08:46:25 +0000614};
615
616module_init(init_squashfs_fs);
617module_exit(exit_squashfs_fs);
618MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
Phillip Lougherd7f2ff62011-05-26 10:39:56 +0100619MODULE_AUTHOR("Phillip Lougher <phillip@squashfs.org.uk>");
Phillip Lougher0aa66612009-01-05 08:46:25 +0000620MODULE_LICENSE("GPL");