168252eb5SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
20aa66619SPhillip Lougher /*
30aa66619SPhillip Lougher * Squashfs - a compressed read only filesystem for Linux
40aa66619SPhillip Lougher *
50aa66619SPhillip Lougher * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
6d7f2ff67SPhillip Lougher * Phillip Lougher <phillip@squashfs.org.uk>
70aa66619SPhillip Lougher *
80aa66619SPhillip Lougher * super.c
90aa66619SPhillip Lougher */
100aa66619SPhillip Lougher
110aa66619SPhillip Lougher /*
120aa66619SPhillip Lougher * This file implements code to read the superblock, read and initialise
130aa66619SPhillip Lougher * in-memory structures at mount time, and all the VFS glue code to register
140aa66619SPhillip Lougher * the filesystem.
150aa66619SPhillip Lougher */
160aa66619SPhillip Lougher
17c811f5f4SFabian Frederick #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18c811f5f4SFabian Frederick
19be9a7b3eSChristoph Hellwig #include <linux/blkdev.h>
200aa66619SPhillip Lougher #include <linux/fs.h>
215a2be128SDavid Howells #include <linux/fs_context.h>
2210dde05bSVincent Whitchurch #include <linux/fs_parser.h>
230aa66619SPhillip Lougher #include <linux/vfs.h>
240aa66619SPhillip Lougher #include <linux/slab.h>
250aa66619SPhillip Lougher #include <linux/mutex.h>
2610dde05bSVincent Whitchurch #include <linux/seq_file.h>
270aa66619SPhillip Lougher #include <linux/pagemap.h>
280aa66619SPhillip Lougher #include <linux/init.h>
290aa66619SPhillip Lougher #include <linux/module.h>
301bcbf313SQinghuang Feng #include <linux/magic.h>
314b5397dcSPhillip Lougher #include <linux/xattr.h>
320aa66619SPhillip Lougher
330aa66619SPhillip Lougher #include "squashfs_fs.h"
340aa66619SPhillip Lougher #include "squashfs_fs_sb.h"
350aa66619SPhillip Lougher #include "squashfs_fs_i.h"
360aa66619SPhillip Lougher #include "squashfs.h"
374c0f0bb2SPhillip Lougher #include "decompressor.h"
3801e5b4e4SPhillip Lougher #include "xattr.h"
390aa66619SPhillip Lougher
400aa66619SPhillip Lougher static struct file_system_type squashfs_fs_type;
41b87221deSAlexey Dobriyan static const struct super_operations squashfs_super_ops;
420aa66619SPhillip Lougher
4310dde05bSVincent Whitchurch enum Opt_errors {
4410dde05bSVincent Whitchurch Opt_errors_continue,
4510dde05bSVincent Whitchurch Opt_errors_panic,
4610dde05bSVincent Whitchurch };
4710dde05bSVincent Whitchurch
4810dde05bSVincent Whitchurch enum squashfs_param {
4910dde05bSVincent Whitchurch Opt_errors,
5080f78409SXiaoming Ni Opt_threads,
5110dde05bSVincent Whitchurch };
5210dde05bSVincent Whitchurch
5310dde05bSVincent Whitchurch struct squashfs_mount_opts {
5410dde05bSVincent Whitchurch enum Opt_errors errors;
5580f78409SXiaoming Ni const struct squashfs_decompressor_thread_ops *thread_ops;
56fb40fe04SXiaoming Ni int thread_num;
5710dde05bSVincent Whitchurch };
5810dde05bSVincent Whitchurch
5910dde05bSVincent Whitchurch static const struct constant_table squashfs_param_errors[] = {
6010dde05bSVincent Whitchurch {"continue", Opt_errors_continue },
6110dde05bSVincent Whitchurch {"panic", Opt_errors_panic },
6210dde05bSVincent Whitchurch {}
6310dde05bSVincent Whitchurch };
6410dde05bSVincent Whitchurch
6510dde05bSVincent Whitchurch static const struct fs_parameter_spec squashfs_fs_parameters[] = {
6610dde05bSVincent Whitchurch fsparam_enum("errors", Opt_errors, squashfs_param_errors),
6780f78409SXiaoming Ni fsparam_string("threads", Opt_threads),
6810dde05bSVincent Whitchurch {}
6910dde05bSVincent Whitchurch };
7010dde05bSVincent Whitchurch
71fb40fe04SXiaoming Ni
squashfs_parse_param_threads_str(const char * str,struct squashfs_mount_opts * opts)72fb40fe04SXiaoming Ni static int squashfs_parse_param_threads_str(const char *str, struct squashfs_mount_opts *opts)
7380f78409SXiaoming Ni {
7480f78409SXiaoming Ni #ifdef CONFIG_SQUASHFS_CHOICE_DECOMP_BY_MOUNT
7580f78409SXiaoming Ni if (strcmp(str, "single") == 0) {
7680f78409SXiaoming Ni opts->thread_ops = &squashfs_decompressor_single;
7780f78409SXiaoming Ni return 0;
7880f78409SXiaoming Ni }
7980f78409SXiaoming Ni if (strcmp(str, "multi") == 0) {
8080f78409SXiaoming Ni opts->thread_ops = &squashfs_decompressor_multi;
8180f78409SXiaoming Ni return 0;
8280f78409SXiaoming Ni }
8380f78409SXiaoming Ni if (strcmp(str, "percpu") == 0) {
8480f78409SXiaoming Ni opts->thread_ops = &squashfs_decompressor_percpu;
8580f78409SXiaoming Ni return 0;
8680f78409SXiaoming Ni }
8780f78409SXiaoming Ni #endif
8880f78409SXiaoming Ni return -EINVAL;
8980f78409SXiaoming Ni }
9080f78409SXiaoming Ni
squashfs_parse_param_threads_num(const char * str,struct squashfs_mount_opts * opts)91fb40fe04SXiaoming Ni static int squashfs_parse_param_threads_num(const char *str, struct squashfs_mount_opts *opts)
92fb40fe04SXiaoming Ni {
93fb40fe04SXiaoming Ni #ifdef CONFIG_SQUASHFS_MOUNT_DECOMP_THREADS
94fb40fe04SXiaoming Ni int ret;
95fb40fe04SXiaoming Ni unsigned long num;
96fb40fe04SXiaoming Ni
97fb40fe04SXiaoming Ni ret = kstrtoul(str, 0, &num);
98fb40fe04SXiaoming Ni if (ret != 0)
99fb40fe04SXiaoming Ni return -EINVAL;
100fb40fe04SXiaoming Ni if (num > 1) {
101fb40fe04SXiaoming Ni opts->thread_ops = &squashfs_decompressor_multi;
102fb40fe04SXiaoming Ni if (num > opts->thread_ops->max_decompressors())
103fb40fe04SXiaoming Ni return -EINVAL;
104fb40fe04SXiaoming Ni opts->thread_num = (int)num;
105fb40fe04SXiaoming Ni return 0;
106fb40fe04SXiaoming Ni }
107fb40fe04SXiaoming Ni #ifdef CONFIG_SQUASHFS_DECOMP_SINGLE
108fb40fe04SXiaoming Ni if (num == 1) {
109fb40fe04SXiaoming Ni opts->thread_ops = &squashfs_decompressor_single;
110fb40fe04SXiaoming Ni opts->thread_num = 1;
111fb40fe04SXiaoming Ni return 0;
112fb40fe04SXiaoming Ni }
113fb40fe04SXiaoming Ni #endif
114fb40fe04SXiaoming Ni #endif /* !CONFIG_SQUASHFS_MOUNT_DECOMP_THREADS */
115fb40fe04SXiaoming Ni return -EINVAL;
116fb40fe04SXiaoming Ni }
117fb40fe04SXiaoming Ni
squashfs_parse_param_threads(const char * str,struct squashfs_mount_opts * opts)118fb40fe04SXiaoming Ni static int squashfs_parse_param_threads(const char *str, struct squashfs_mount_opts *opts)
119fb40fe04SXiaoming Ni {
120fb40fe04SXiaoming Ni int ret = squashfs_parse_param_threads_str(str, opts);
121fb40fe04SXiaoming Ni
122fb40fe04SXiaoming Ni if (ret == 0)
123fb40fe04SXiaoming Ni return ret;
124fb40fe04SXiaoming Ni return squashfs_parse_param_threads_num(str, opts);
125fb40fe04SXiaoming Ni }
126fb40fe04SXiaoming Ni
squashfs_parse_param(struct fs_context * fc,struct fs_parameter * param)12710dde05bSVincent Whitchurch static int squashfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
12810dde05bSVincent Whitchurch {
12910dde05bSVincent Whitchurch struct squashfs_mount_opts *opts = fc->fs_private;
13010dde05bSVincent Whitchurch struct fs_parse_result result;
13110dde05bSVincent Whitchurch int opt;
13210dde05bSVincent Whitchurch
13310dde05bSVincent Whitchurch opt = fs_parse(fc, squashfs_fs_parameters, param, &result);
13410dde05bSVincent Whitchurch if (opt < 0)
13510dde05bSVincent Whitchurch return opt;
13610dde05bSVincent Whitchurch
13710dde05bSVincent Whitchurch switch (opt) {
13810dde05bSVincent Whitchurch case Opt_errors:
13910dde05bSVincent Whitchurch opts->errors = result.uint_32;
14010dde05bSVincent Whitchurch break;
14180f78409SXiaoming Ni case Opt_threads:
14280f78409SXiaoming Ni if (squashfs_parse_param_threads(param->string, opts) != 0)
14380f78409SXiaoming Ni return -EINVAL;
14480f78409SXiaoming Ni break;
14510dde05bSVincent Whitchurch default:
14610dde05bSVincent Whitchurch return -EINVAL;
14710dde05bSVincent Whitchurch }
14810dde05bSVincent Whitchurch
14910dde05bSVincent Whitchurch return 0;
15010dde05bSVincent Whitchurch }
15110dde05bSVincent Whitchurch
supported_squashfs_filesystem(struct fs_context * fc,short major,short minor,short id)1525a2be128SDavid Howells static const struct squashfs_decompressor *supported_squashfs_filesystem(
1535a2be128SDavid Howells struct fs_context *fc,
1545a2be128SDavid Howells short major, short minor, short id)
1550aa66619SPhillip Lougher {
1564c0f0bb2SPhillip Lougher const struct squashfs_decompressor *decompressor;
1574c0f0bb2SPhillip Lougher
1580aa66619SPhillip Lougher if (major < SQUASHFS_MAJOR) {
1595a2be128SDavid Howells errorf(fc, "Major/Minor mismatch, older Squashfs %d.%d "
1605a2be128SDavid Howells "filesystems are unsupported", major, minor);
1614c0f0bb2SPhillip Lougher return NULL;
1620aa66619SPhillip Lougher } else if (major > SQUASHFS_MAJOR || minor > SQUASHFS_MINOR) {
1635a2be128SDavid Howells errorf(fc, "Major/Minor mismatch, trying to mount newer "
1645a2be128SDavid Howells "%d.%d filesystem", major, minor);
1655a2be128SDavid Howells errorf(fc, "Please update your kernel");
1664c0f0bb2SPhillip Lougher return NULL;
1670aa66619SPhillip Lougher }
1680aa66619SPhillip Lougher
1694c0f0bb2SPhillip Lougher decompressor = squashfs_lookup_decompressor(id);
1704c0f0bb2SPhillip Lougher if (!decompressor->supported) {
1715a2be128SDavid Howells errorf(fc, "Filesystem uses \"%s\" compression. This is not supported",
1725a2be128SDavid Howells decompressor->name);
1734c0f0bb2SPhillip Lougher return NULL;
1744c0f0bb2SPhillip Lougher }
1750aa66619SPhillip Lougher
1764c0f0bb2SPhillip Lougher return decompressor;
1770aa66619SPhillip Lougher }
1780aa66619SPhillip Lougher
1790aa66619SPhillip Lougher
squashfs_fill_super(struct super_block * sb,struct fs_context * fc)1805a2be128SDavid Howells static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc)
1810aa66619SPhillip Lougher {
18210dde05bSVincent Whitchurch struct squashfs_mount_opts *opts = fc->fs_private;
1830aa66619SPhillip Lougher struct squashfs_sb_info *msblk;
1840aa66619SPhillip Lougher struct squashfs_super_block *sblk = NULL;
1850aa66619SPhillip Lougher struct inode *root;
1860aa66619SPhillip Lougher long long root_inode;
1870aa66619SPhillip Lougher unsigned short flags;
1880aa66619SPhillip Lougher unsigned int fragments;
18937986f63SPhillip Lougher u64 lookup_table_start, xattr_id_table_start, next_table;
1900aa66619SPhillip Lougher int err;
1910aa66619SPhillip Lougher
1920aa66619SPhillip Lougher TRACE("Entered squashfs_fill_superblock\n");
1930aa66619SPhillip Lougher
1940aa66619SPhillip Lougher sb->s_fs_info = kzalloc(sizeof(*msblk), GFP_KERNEL);
1950aa66619SPhillip Lougher if (sb->s_fs_info == NULL) {
1960aa66619SPhillip Lougher ERROR("Failed to allocate squashfs_sb_info\n");
1970aa66619SPhillip Lougher return -ENOMEM;
1980aa66619SPhillip Lougher }
1990aa66619SPhillip Lougher msblk = sb->s_fs_info;
200c7e8d327SBaokun Li msblk->thread_ops = opts->thread_ops;
2010aa66619SPhillip Lougher
20210dde05bSVincent Whitchurch msblk->panic_on_errors = (opts->errors == Opt_errors_panic);
20310dde05bSVincent Whitchurch
2047657cacfSPhillip Lougher msblk->devblksize = sb_min_blocksize(sb, SQUASHFS_DEVBLK_SIZE);
2050aa66619SPhillip Lougher msblk->devblksize_log2 = ffz(~msblk->devblksize);
2060aa66619SPhillip Lougher
2070aa66619SPhillip Lougher mutex_init(&msblk->meta_index_mutex);
2080aa66619SPhillip Lougher
2090aa66619SPhillip Lougher /*
2100aa66619SPhillip Lougher * msblk->bytes_used is checked in squashfs_read_table to ensure reads
2110aa66619SPhillip Lougher * are not beyond filesystem end. But as we're using
2120aa66619SPhillip Lougher * squashfs_read_table here to read the superblock (including the value
2130aa66619SPhillip Lougher * of bytes_used) we need to set it to an initial sensible dummy value
2140aa66619SPhillip Lougher */
2150aa66619SPhillip Lougher msblk->bytes_used = sizeof(*sblk);
21682de647eSPhillip Lougher sblk = squashfs_read_table(sb, SQUASHFS_START, sizeof(*sblk));
2170aa66619SPhillip Lougher
21882de647eSPhillip Lougher if (IS_ERR(sblk)) {
2195a2be128SDavid Howells errorf(fc, "unable to read squashfs_super_block");
22082de647eSPhillip Lougher err = PTR_ERR(sblk);
22182de647eSPhillip Lougher sblk = NULL;
2220aa66619SPhillip Lougher goto failed_mount;
2230aa66619SPhillip Lougher }
2240aa66619SPhillip Lougher
2254c0f0bb2SPhillip Lougher err = -EINVAL;
2264c0f0bb2SPhillip Lougher
2270aa66619SPhillip Lougher /* Check it is a SQUASHFS superblock */
2280aa66619SPhillip Lougher sb->s_magic = le32_to_cpu(sblk->s_magic);
2290aa66619SPhillip Lougher if (sb->s_magic != SQUASHFS_MAGIC) {
2305a2be128SDavid Howells if (!(fc->sb_flags & SB_SILENT))
2315a2be128SDavid Howells errorf(fc, "Can't find a SQUASHFS superblock on %pg",
232a1c6f057SDmitry Monakhov sb->s_bdev);
2330aa66619SPhillip Lougher goto failed_mount;
2340aa66619SPhillip Lougher }
235c7e8d327SBaokun Li
236fb40fe04SXiaoming Ni if (opts->thread_num == 0) {
237fb40fe04SXiaoming Ni msblk->max_thread_num = msblk->thread_ops->max_decompressors();
238fb40fe04SXiaoming Ni } else {
239fb40fe04SXiaoming Ni msblk->max_thread_num = opts->thread_num;
240fb40fe04SXiaoming Ni }
2410aa66619SPhillip Lougher
2424c0f0bb2SPhillip Lougher /* Check the MAJOR & MINOR versions and lookup compression type */
2434c0f0bb2SPhillip Lougher msblk->decompressor = supported_squashfs_filesystem(
2445a2be128SDavid Howells fc,
2454c0f0bb2SPhillip Lougher le16_to_cpu(sblk->s_major),
2460aa66619SPhillip Lougher le16_to_cpu(sblk->s_minor),
2470aa66619SPhillip Lougher le16_to_cpu(sblk->compression));
2484c0f0bb2SPhillip Lougher if (msblk->decompressor == NULL)
2490aa66619SPhillip Lougher goto failed_mount;
2500aa66619SPhillip Lougher
2510aa66619SPhillip Lougher /* Check the filesystem does not extend beyond the end of the
2520aa66619SPhillip Lougher block device */
2530aa66619SPhillip Lougher msblk->bytes_used = le64_to_cpu(sblk->bytes_used);
254be9a7b3eSChristoph Hellwig if (msblk->bytes_used < 0 ||
255be9a7b3eSChristoph Hellwig msblk->bytes_used > bdev_nr_bytes(sb->s_bdev))
2560aa66619SPhillip Lougher goto failed_mount;
2570aa66619SPhillip Lougher
2580aa66619SPhillip Lougher /* Check block size for sanity */
2590aa66619SPhillip Lougher msblk->block_size = le32_to_cpu(sblk->block_size);
2600aa66619SPhillip Lougher if (msblk->block_size > SQUASHFS_FILE_MAX_SIZE)
2615a2be128SDavid Howells goto insanity;
2620aa66619SPhillip Lougher
263fffb47b8SPhillip Lougher /*
264fffb47b8SPhillip Lougher * Check the system page size is not larger than the filesystem
265fffb47b8SPhillip Lougher * block size (by default 128K). This is currently not supported.
266fffb47b8SPhillip Lougher */
26709cbfeafSKirill A. Shutemov if (PAGE_SIZE > msblk->block_size) {
2685a2be128SDavid Howells errorf(fc, "Page size > filesystem block size (%d). This is "
2695a2be128SDavid Howells "currently not supported!", msblk->block_size);
270fffb47b8SPhillip Lougher goto failed_mount;
271fffb47b8SPhillip Lougher }
272fffb47b8SPhillip Lougher
2734b0180a4SPhillip Lougher /* Check block log for sanity */
2740aa66619SPhillip Lougher msblk->block_log = le16_to_cpu(sblk->block_log);
2750aa66619SPhillip Lougher if (msblk->block_log > SQUASHFS_FILE_MAX_LOG)
2760aa66619SPhillip Lougher goto failed_mount;
2770aa66619SPhillip Lougher
2784b0180a4SPhillip Lougher /* Check that block_size and block_log match */
2794b0180a4SPhillip Lougher if (msblk->block_size != (1 << msblk->block_log))
2805a2be128SDavid Howells goto insanity;
2814b0180a4SPhillip Lougher
2820aa66619SPhillip Lougher /* Check the root inode for sanity */
2830aa66619SPhillip Lougher root_inode = le64_to_cpu(sblk->root_inode);
2840aa66619SPhillip Lougher if (SQUASHFS_INODE_OFFSET(root_inode) > SQUASHFS_METADATA_SIZE)
2855a2be128SDavid Howells goto insanity;
2860aa66619SPhillip Lougher
2870aa66619SPhillip Lougher msblk->inode_table = le64_to_cpu(sblk->inode_table_start);
2880aa66619SPhillip Lougher msblk->directory_table = le64_to_cpu(sblk->directory_table_start);
2890aa66619SPhillip Lougher msblk->inodes = le32_to_cpu(sblk->inodes);
29071755ee5SLinus Torvalds msblk->fragments = le32_to_cpu(sblk->fragments);
291f37aa4c7SPhillip Lougher msblk->ids = le16_to_cpu(sblk->no_ids);
2920aa66619SPhillip Lougher flags = le16_to_cpu(sblk->flags);
2930aa66619SPhillip Lougher
294a1c6f057SDmitry Monakhov TRACE("Found valid superblock on %pg\n", sb->s_bdev);
2950aa66619SPhillip Lougher TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(flags)
2960aa66619SPhillip Lougher ? "un" : "");
2970aa66619SPhillip Lougher TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(flags)
2980aa66619SPhillip Lougher ? "un" : "");
2990aa66619SPhillip Lougher TRACE("Filesystem size %lld bytes\n", msblk->bytes_used);
3000aa66619SPhillip Lougher TRACE("Block size %d\n", msblk->block_size);
3010aa66619SPhillip Lougher TRACE("Number of inodes %d\n", msblk->inodes);
30271755ee5SLinus Torvalds TRACE("Number of fragments %d\n", msblk->fragments);
303f37aa4c7SPhillip Lougher TRACE("Number of ids %d\n", msblk->ids);
3040aa66619SPhillip Lougher TRACE("sblk->inode_table_start %llx\n", msblk->inode_table);
3050aa66619SPhillip Lougher TRACE("sblk->directory_table_start %llx\n", msblk->directory_table);
3060aa66619SPhillip Lougher TRACE("sblk->fragment_table_start %llx\n",
3070aa66619SPhillip Lougher (u64) le64_to_cpu(sblk->fragment_table_start));
3080aa66619SPhillip Lougher TRACE("sblk->id_table_start %llx\n",
3090aa66619SPhillip Lougher (u64) le64_to_cpu(sblk->id_table_start));
3100aa66619SPhillip Lougher
3110aa66619SPhillip Lougher sb->s_maxbytes = MAX_LFS_FILESIZE;
31222b13969SDeepa Dinamani sb->s_time_min = 0;
31322b13969SDeepa Dinamani sb->s_time_max = U32_MAX;
3141751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY;
3150aa66619SPhillip Lougher sb->s_op = &squashfs_super_ops;
3160aa66619SPhillip Lougher
3170aa66619SPhillip Lougher err = -ENOMEM;
3180aa66619SPhillip Lougher
3190aa66619SPhillip Lougher msblk->block_cache = squashfs_cache_init("metadata",
3200aa66619SPhillip Lougher SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
3210aa66619SPhillip Lougher if (msblk->block_cache == NULL)
3220aa66619SPhillip Lougher goto failed_mount;
3230aa66619SPhillip Lougher
3240aa66619SPhillip Lougher /* Allocate read_page block */
3259508c6b9SPhillip Lougher msblk->read_page = squashfs_cache_init("data",
326fb40fe04SXiaoming Ni msblk->max_thread_num, msblk->block_size);
3270aa66619SPhillip Lougher if (msblk->read_page == NULL) {
3285a2be128SDavid Howells errorf(fc, "Failed to allocate read_page block");
3290aa66619SPhillip Lougher goto failed_mount;
3300aa66619SPhillip Lougher }
3310aa66619SPhillip Lougher
332*e994f5b6SVincent Whitchurch if (msblk->devblksize == PAGE_SIZE) {
333*e994f5b6SVincent Whitchurch struct inode *cache = new_inode(sb);
334*e994f5b6SVincent Whitchurch
335*e994f5b6SVincent Whitchurch if (cache == NULL)
336*e994f5b6SVincent Whitchurch goto failed_mount;
337*e994f5b6SVincent Whitchurch
338*e994f5b6SVincent Whitchurch set_nlink(cache, 1);
339*e994f5b6SVincent Whitchurch cache->i_size = OFFSET_MAX;
340*e994f5b6SVincent Whitchurch mapping_set_gfp_mask(cache->i_mapping, GFP_NOFS);
341*e994f5b6SVincent Whitchurch
342*e994f5b6SVincent Whitchurch msblk->cache_mapping = cache->i_mapping;
343*e994f5b6SVincent Whitchurch }
344*e994f5b6SVincent Whitchurch
3459508c6b9SPhillip Lougher msblk->stream = squashfs_decompressor_setup(sb, flags);
346b7fc0ff0SPhillip Lougher if (IS_ERR(msblk->stream)) {
347b7fc0ff0SPhillip Lougher err = PTR_ERR(msblk->stream);
348b7fc0ff0SPhillip Lougher msblk->stream = NULL;
3495a2be128SDavid Howells goto insanity;
350b7fc0ff0SPhillip Lougher }
351b7fc0ff0SPhillip Lougher
35276e002f7SPhillip Lougher /* Handle xattrs */
35376e002f7SPhillip Lougher sb->s_xattr = squashfs_xattr_handlers;
35476e002f7SPhillip Lougher xattr_id_table_start = le64_to_cpu(sblk->xattr_id_table_start);
35537986f63SPhillip Lougher if (xattr_id_table_start == SQUASHFS_INVALID_BLK) {
35637986f63SPhillip Lougher next_table = msblk->bytes_used;
35776e002f7SPhillip Lougher goto allocate_id_index_table;
35837986f63SPhillip Lougher }
35976e002f7SPhillip Lougher
36076e002f7SPhillip Lougher /* Allocate and read xattr id lookup table */
36176e002f7SPhillip Lougher msblk->xattr_id_table = squashfs_read_xattr_id_table(sb,
36276e002f7SPhillip Lougher xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids);
36376e002f7SPhillip Lougher if (IS_ERR(msblk->xattr_id_table)) {
3645a2be128SDavid Howells errorf(fc, "unable to read xattr id index table");
36576e002f7SPhillip Lougher err = PTR_ERR(msblk->xattr_id_table);
36676e002f7SPhillip Lougher msblk->xattr_id_table = NULL;
36776e002f7SPhillip Lougher if (err != -ENOTSUPP)
36876e002f7SPhillip Lougher goto failed_mount;
36976e002f7SPhillip Lougher }
37037986f63SPhillip Lougher next_table = msblk->xattr_table;
37176e002f7SPhillip Lougher
37276e002f7SPhillip Lougher allocate_id_index_table:
3730aa66619SPhillip Lougher /* Allocate and read id index table */
3740aa66619SPhillip Lougher msblk->id_table = squashfs_read_id_index_table(sb,
375f37aa4c7SPhillip Lougher le64_to_cpu(sblk->id_table_start), next_table, msblk->ids);
3760aa66619SPhillip Lougher if (IS_ERR(msblk->id_table)) {
3775a2be128SDavid Howells errorf(fc, "unable to read id index table");
3780aa66619SPhillip Lougher err = PTR_ERR(msblk->id_table);
3790aa66619SPhillip Lougher msblk->id_table = NULL;
3800aa66619SPhillip Lougher goto failed_mount;
3810aa66619SPhillip Lougher }
382d5b72ce1SPhillip Lougher next_table = le64_to_cpu(msblk->id_table[0]);
3830aa66619SPhillip Lougher
38476e002f7SPhillip Lougher /* Handle inode lookup table */
38576e002f7SPhillip Lougher lookup_table_start = le64_to_cpu(sblk->lookup_table_start);
38676e002f7SPhillip Lougher if (lookup_table_start == SQUASHFS_INVALID_BLK)
38776e002f7SPhillip Lougher goto handle_fragments;
38876e002f7SPhillip Lougher
38976e002f7SPhillip Lougher /* Allocate and read inode lookup table */
39076e002f7SPhillip Lougher msblk->inode_lookup_table = squashfs_read_inode_lookup_table(sb,
391ac51a0a7SPhillip Lougher lookup_table_start, next_table, msblk->inodes);
39276e002f7SPhillip Lougher if (IS_ERR(msblk->inode_lookup_table)) {
3935a2be128SDavid Howells errorf(fc, "unable to read inode lookup table");
39476e002f7SPhillip Lougher err = PTR_ERR(msblk->inode_lookup_table);
39576e002f7SPhillip Lougher msblk->inode_lookup_table = NULL;
39676e002f7SPhillip Lougher goto failed_mount;
39776e002f7SPhillip Lougher }
398d5b72ce1SPhillip Lougher next_table = le64_to_cpu(msblk->inode_lookup_table[0]);
39976e002f7SPhillip Lougher
40076e002f7SPhillip Lougher sb->s_export_op = &squashfs_export_ops;
40176e002f7SPhillip Lougher
40276e002f7SPhillip Lougher handle_fragments:
40371755ee5SLinus Torvalds fragments = msblk->fragments;
4040aa66619SPhillip Lougher if (fragments == 0)
4051094a4a6SPhillip Lougher goto check_directory_table;
4060aa66619SPhillip Lougher
4070aa66619SPhillip Lougher msblk->fragment_cache = squashfs_cache_init("fragment",
4080aa66619SPhillip Lougher SQUASHFS_CACHED_FRAGMENTS, msblk->block_size);
4090aa66619SPhillip Lougher if (msblk->fragment_cache == NULL) {
4100aa66619SPhillip Lougher err = -ENOMEM;
4110aa66619SPhillip Lougher goto failed_mount;
4120aa66619SPhillip Lougher }
4130aa66619SPhillip Lougher
4140aa66619SPhillip Lougher /* Allocate and read fragment index table */
4150aa66619SPhillip Lougher msblk->fragment_index = squashfs_read_fragment_index_table(sb,
4161cac63ccSPhillip Lougher le64_to_cpu(sblk->fragment_table_start), next_table, fragments);
4170aa66619SPhillip Lougher if (IS_ERR(msblk->fragment_index)) {
4185a2be128SDavid Howells errorf(fc, "unable to read fragment index table");
4190aa66619SPhillip Lougher err = PTR_ERR(msblk->fragment_index);
4200aa66619SPhillip Lougher msblk->fragment_index = NULL;
4210aa66619SPhillip Lougher goto failed_mount;
4220aa66619SPhillip Lougher }
423d5b72ce1SPhillip Lougher next_table = le64_to_cpu(msblk->fragment_index[0]);
4240aa66619SPhillip Lougher
4251094a4a6SPhillip Lougher check_directory_table:
4261094a4a6SPhillip Lougher /* Sanity check directory_table */
427cc37f75aSPhillip Lougher if (msblk->directory_table > next_table) {
4281094a4a6SPhillip Lougher err = -EINVAL;
4295a2be128SDavid Howells goto insanity;
4301094a4a6SPhillip Lougher }
4311094a4a6SPhillip Lougher
4321094a4a6SPhillip Lougher /* Sanity check inode_table */
4331094a4a6SPhillip Lougher if (msblk->inode_table >= msblk->directory_table) {
4341094a4a6SPhillip Lougher err = -EINVAL;
4355a2be128SDavid Howells goto insanity;
4361094a4a6SPhillip Lougher }
4371094a4a6SPhillip Lougher
4381094a4a6SPhillip Lougher /* allocate root */
4390aa66619SPhillip Lougher root = new_inode(sb);
4400aa66619SPhillip Lougher if (!root) {
4410aa66619SPhillip Lougher err = -ENOMEM;
4420aa66619SPhillip Lougher goto failed_mount;
4430aa66619SPhillip Lougher }
4440aa66619SPhillip Lougher
4450aa66619SPhillip Lougher err = squashfs_read_inode(root, root_inode);
4460aa66619SPhillip Lougher if (err) {
4471cb08e97SPhillip Lougher make_bad_inode(root);
4481cb08e97SPhillip Lougher iput(root);
4490aa66619SPhillip Lougher goto failed_mount;
4500aa66619SPhillip Lougher }
4510aa66619SPhillip Lougher insert_inode_hash(root);
4520aa66619SPhillip Lougher
45348fde701SAl Viro sb->s_root = d_make_root(root);
4540aa66619SPhillip Lougher if (sb->s_root == NULL) {
4550aa66619SPhillip Lougher ERROR("Root inode create failed\n");
4560aa66619SPhillip Lougher err = -ENOMEM;
4570aa66619SPhillip Lougher goto failed_mount;
4580aa66619SPhillip Lougher }
4590aa66619SPhillip Lougher
4600aa66619SPhillip Lougher TRACE("Leaving squashfs_fill_super\n");
4610aa66619SPhillip Lougher kfree(sblk);
4620aa66619SPhillip Lougher return 0;
4630aa66619SPhillip Lougher
4645a2be128SDavid Howells insanity:
4655a2be128SDavid Howells errorf(fc, "squashfs image failed sanity check");
4660aa66619SPhillip Lougher failed_mount:
4670aa66619SPhillip Lougher squashfs_cache_delete(msblk->block_cache);
4680aa66619SPhillip Lougher squashfs_cache_delete(msblk->fragment_cache);
4690aa66619SPhillip Lougher squashfs_cache_delete(msblk->read_page);
470*e994f5b6SVincent Whitchurch if (msblk->cache_mapping)
471*e994f5b6SVincent Whitchurch iput(msblk->cache_mapping->host);
47280f78409SXiaoming Ni msblk->thread_ops->destroy(msblk);
4730aa66619SPhillip Lougher kfree(msblk->inode_lookup_table);
4740aa66619SPhillip Lougher kfree(msblk->fragment_index);
4750aa66619SPhillip Lougher kfree(msblk->id_table);
4764b5397dcSPhillip Lougher kfree(msblk->xattr_id_table);
4770aa66619SPhillip Lougher kfree(sb->s_fs_info);
4780aa66619SPhillip Lougher sb->s_fs_info = NULL;
4790aa66619SPhillip Lougher kfree(sblk);
4800aa66619SPhillip Lougher return err;
4810aa66619SPhillip Lougher }
4820aa66619SPhillip Lougher
squashfs_get_tree(struct fs_context * fc)4835a2be128SDavid Howells static int squashfs_get_tree(struct fs_context *fc)
4845a2be128SDavid Howells {
4855a2be128SDavid Howells return get_tree_bdev(fc, squashfs_fill_super);
4865a2be128SDavid Howells }
4875a2be128SDavid Howells
squashfs_reconfigure(struct fs_context * fc)4885a2be128SDavid Howells static int squashfs_reconfigure(struct fs_context *fc)
4895a2be128SDavid Howells {
49010dde05bSVincent Whitchurch struct super_block *sb = fc->root->d_sb;
49110dde05bSVincent Whitchurch struct squashfs_sb_info *msblk = sb->s_fs_info;
49210dde05bSVincent Whitchurch struct squashfs_mount_opts *opts = fc->fs_private;
49310dde05bSVincent Whitchurch
4945a2be128SDavid Howells sync_filesystem(fc->root->d_sb);
4955a2be128SDavid Howells fc->sb_flags |= SB_RDONLY;
49610dde05bSVincent Whitchurch
49710dde05bSVincent Whitchurch msblk->panic_on_errors = (opts->errors == Opt_errors_panic);
49810dde05bSVincent Whitchurch
4995a2be128SDavid Howells return 0;
5005a2be128SDavid Howells }
5015a2be128SDavid Howells
squashfs_free_fs_context(struct fs_context * fc)50210dde05bSVincent Whitchurch static void squashfs_free_fs_context(struct fs_context *fc)
50310dde05bSVincent Whitchurch {
50410dde05bSVincent Whitchurch kfree(fc->fs_private);
50510dde05bSVincent Whitchurch }
50610dde05bSVincent Whitchurch
5075a2be128SDavid Howells static const struct fs_context_operations squashfs_context_ops = {
5085a2be128SDavid Howells .get_tree = squashfs_get_tree,
50910dde05bSVincent Whitchurch .free = squashfs_free_fs_context,
51010dde05bSVincent Whitchurch .parse_param = squashfs_parse_param,
5115a2be128SDavid Howells .reconfigure = squashfs_reconfigure,
5125a2be128SDavid Howells };
5135a2be128SDavid Howells
squashfs_show_options(struct seq_file * s,struct dentry * root)51410dde05bSVincent Whitchurch static int squashfs_show_options(struct seq_file *s, struct dentry *root)
51510dde05bSVincent Whitchurch {
51610dde05bSVincent Whitchurch struct super_block *sb = root->d_sb;
51710dde05bSVincent Whitchurch struct squashfs_sb_info *msblk = sb->s_fs_info;
51810dde05bSVincent Whitchurch
51910dde05bSVincent Whitchurch if (msblk->panic_on_errors)
52010dde05bSVincent Whitchurch seq_puts(s, ",errors=panic");
52110dde05bSVincent Whitchurch else
52210dde05bSVincent Whitchurch seq_puts(s, ",errors=continue");
52310dde05bSVincent Whitchurch
52480f78409SXiaoming Ni #ifdef CONFIG_SQUASHFS_CHOICE_DECOMP_BY_MOUNT
52580f78409SXiaoming Ni if (msblk->thread_ops == &squashfs_decompressor_single) {
52680f78409SXiaoming Ni seq_puts(s, ",threads=single");
52780f78409SXiaoming Ni return 0;
52880f78409SXiaoming Ni }
52980f78409SXiaoming Ni if (msblk->thread_ops == &squashfs_decompressor_percpu) {
53080f78409SXiaoming Ni seq_puts(s, ",threads=percpu");
53180f78409SXiaoming Ni return 0;
53280f78409SXiaoming Ni }
53380f78409SXiaoming Ni #endif
534fb40fe04SXiaoming Ni #ifdef CONFIG_SQUASHFS_MOUNT_DECOMP_THREADS
535fb40fe04SXiaoming Ni seq_printf(s, ",threads=%d", msblk->max_thread_num);
536fb40fe04SXiaoming Ni #endif
53710dde05bSVincent Whitchurch return 0;
53810dde05bSVincent Whitchurch }
53910dde05bSVincent Whitchurch
squashfs_init_fs_context(struct fs_context * fc)5405a2be128SDavid Howells static int squashfs_init_fs_context(struct fs_context *fc)
5415a2be128SDavid Howells {
54210dde05bSVincent Whitchurch struct squashfs_mount_opts *opts;
54310dde05bSVincent Whitchurch
54410dde05bSVincent Whitchurch opts = kzalloc(sizeof(*opts), GFP_KERNEL);
54510dde05bSVincent Whitchurch if (!opts)
54610dde05bSVincent Whitchurch return -ENOMEM;
54710dde05bSVincent Whitchurch
54880f78409SXiaoming Ni #ifdef CONFIG_SQUASHFS_DECOMP_SINGLE
54980f78409SXiaoming Ni opts->thread_ops = &squashfs_decompressor_single;
55080f78409SXiaoming Ni #elif defined(CONFIG_SQUASHFS_DECOMP_MULTI)
55180f78409SXiaoming Ni opts->thread_ops = &squashfs_decompressor_multi;
55280f78409SXiaoming Ni #elif defined(CONFIG_SQUASHFS_DECOMP_MULTI_PERCPU)
55380f78409SXiaoming Ni opts->thread_ops = &squashfs_decompressor_percpu;
55480f78409SXiaoming Ni #else
55580f78409SXiaoming Ni #error "fail: unknown squashfs decompression thread mode?"
55680f78409SXiaoming Ni #endif
557fb40fe04SXiaoming Ni opts->thread_num = 0;
55810dde05bSVincent Whitchurch fc->fs_private = opts;
5595a2be128SDavid Howells fc->ops = &squashfs_context_ops;
5605a2be128SDavid Howells return 0;
5615a2be128SDavid Howells }
5620aa66619SPhillip Lougher
squashfs_statfs(struct dentry * dentry,struct kstatfs * buf)5630aa66619SPhillip Lougher static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
5640aa66619SPhillip Lougher {
5650aa66619SPhillip Lougher struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
5662fc7f562SColy Li u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
5670aa66619SPhillip Lougher
5680aa66619SPhillip Lougher TRACE("Entered squashfs_statfs\n");
5690aa66619SPhillip Lougher
5700aa66619SPhillip Lougher buf->f_type = SQUASHFS_MAGIC;
5710aa66619SPhillip Lougher buf->f_bsize = msblk->block_size;
5720aa66619SPhillip Lougher buf->f_blocks = ((msblk->bytes_used - 1) >> msblk->block_log) + 1;
5730aa66619SPhillip Lougher buf->f_bfree = buf->f_bavail = 0;
5740aa66619SPhillip Lougher buf->f_files = msblk->inodes;
5750aa66619SPhillip Lougher buf->f_ffree = 0;
5760aa66619SPhillip Lougher buf->f_namelen = SQUASHFS_NAME_LEN;
5776d1349c7SAl Viro buf->f_fsid = u64_to_fsid(id);
5780aa66619SPhillip Lougher
5790aa66619SPhillip Lougher return 0;
5800aa66619SPhillip Lougher }
5810aa66619SPhillip Lougher
5820aa66619SPhillip Lougher
squashfs_put_super(struct super_block * sb)5830aa66619SPhillip Lougher static void squashfs_put_super(struct super_block *sb)
5840aa66619SPhillip Lougher {
5850aa66619SPhillip Lougher if (sb->s_fs_info) {
5860aa66619SPhillip Lougher struct squashfs_sb_info *sbi = sb->s_fs_info;
5870aa66619SPhillip Lougher squashfs_cache_delete(sbi->block_cache);
5880aa66619SPhillip Lougher squashfs_cache_delete(sbi->fragment_cache);
5890aa66619SPhillip Lougher squashfs_cache_delete(sbi->read_page);
590*e994f5b6SVincent Whitchurch if (sbi->cache_mapping)
591*e994f5b6SVincent Whitchurch iput(sbi->cache_mapping->host);
59280f78409SXiaoming Ni sbi->thread_ops->destroy(sbi);
5930aa66619SPhillip Lougher kfree(sbi->id_table);
5940aa66619SPhillip Lougher kfree(sbi->fragment_index);
5950aa66619SPhillip Lougher kfree(sbi->meta_index);
596370ec3d1SPhillip Lougher kfree(sbi->inode_lookup_table);
5974b5397dcSPhillip Lougher kfree(sbi->xattr_id_table);
5980aa66619SPhillip Lougher kfree(sb->s_fs_info);
5990aa66619SPhillip Lougher sb->s_fs_info = NULL;
6000aa66619SPhillip Lougher }
6010aa66619SPhillip Lougher }
6020aa66619SPhillip Lougher
6030aa66619SPhillip Lougher static struct kmem_cache *squashfs_inode_cachep;
6040aa66619SPhillip Lougher
6050aa66619SPhillip Lougher
init_once(void * foo)6060aa66619SPhillip Lougher static void init_once(void *foo)
6070aa66619SPhillip Lougher {
6080aa66619SPhillip Lougher struct squashfs_inode_info *ei = foo;
6090aa66619SPhillip Lougher
6100aa66619SPhillip Lougher inode_init_once(&ei->vfs_inode);
6110aa66619SPhillip Lougher }
6120aa66619SPhillip Lougher
6130aa66619SPhillip Lougher
init_inodecache(void)6140aa66619SPhillip Lougher static int __init init_inodecache(void)
6150aa66619SPhillip Lougher {
6160aa66619SPhillip Lougher squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
6170aa66619SPhillip Lougher sizeof(struct squashfs_inode_info), 0,
6185d097056SVladimir Davydov SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
6195d097056SVladimir Davydov init_once);
6200aa66619SPhillip Lougher
6210aa66619SPhillip Lougher return squashfs_inode_cachep ? 0 : -ENOMEM;
6220aa66619SPhillip Lougher }
6230aa66619SPhillip Lougher
6240aa66619SPhillip Lougher
destroy_inodecache(void)6250aa66619SPhillip Lougher static void destroy_inodecache(void)
6260aa66619SPhillip Lougher {
6278c0a8537SKirill A. Shutemov /*
6288c0a8537SKirill A. Shutemov * Make sure all delayed rcu free inodes are flushed before we
6298c0a8537SKirill A. Shutemov * destroy cache.
6308c0a8537SKirill A. Shutemov */
6318c0a8537SKirill A. Shutemov rcu_barrier();
6320aa66619SPhillip Lougher kmem_cache_destroy(squashfs_inode_cachep);
6330aa66619SPhillip Lougher }
6340aa66619SPhillip Lougher
6350aa66619SPhillip Lougher
init_squashfs_fs(void)6360aa66619SPhillip Lougher static int __init init_squashfs_fs(void)
6370aa66619SPhillip Lougher {
6380aa66619SPhillip Lougher int err = init_inodecache();
6390aa66619SPhillip Lougher
6400aa66619SPhillip Lougher if (err)
6410aa66619SPhillip Lougher return err;
6420aa66619SPhillip Lougher
6430aa66619SPhillip Lougher err = register_filesystem(&squashfs_fs_type);
6440aa66619SPhillip Lougher if (err) {
6450aa66619SPhillip Lougher destroy_inodecache();
6460aa66619SPhillip Lougher return err;
6470aa66619SPhillip Lougher }
6480aa66619SPhillip Lougher
649c811f5f4SFabian Frederick pr_info("version 4.0 (2009/01/31) Phillip Lougher\n");
6500aa66619SPhillip Lougher
6510aa66619SPhillip Lougher return 0;
6520aa66619SPhillip Lougher }
6530aa66619SPhillip Lougher
6540aa66619SPhillip Lougher
exit_squashfs_fs(void)6550aa66619SPhillip Lougher static void __exit exit_squashfs_fs(void)
6560aa66619SPhillip Lougher {
6570aa66619SPhillip Lougher unregister_filesystem(&squashfs_fs_type);
6580aa66619SPhillip Lougher destroy_inodecache();
6590aa66619SPhillip Lougher }
6600aa66619SPhillip Lougher
6610aa66619SPhillip Lougher
squashfs_alloc_inode(struct super_block * sb)6620aa66619SPhillip Lougher static struct inode *squashfs_alloc_inode(struct super_block *sb)
6630aa66619SPhillip Lougher {
6640aa66619SPhillip Lougher struct squashfs_inode_info *ei =
665fd60b288SMuchun Song alloc_inode_sb(sb, squashfs_inode_cachep, GFP_KERNEL);
6660aa66619SPhillip Lougher
6670aa66619SPhillip Lougher return ei ? &ei->vfs_inode : NULL;
6680aa66619SPhillip Lougher }
6690aa66619SPhillip Lougher
6700aa66619SPhillip Lougher
squashfs_free_inode(struct inode * inode)67156b5af19SAl Viro static void squashfs_free_inode(struct inode *inode)
672fa0d7e3dSNick Piggin {
673fa0d7e3dSNick Piggin kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
674fa0d7e3dSNick Piggin }
675fa0d7e3dSNick Piggin
6760aa66619SPhillip Lougher static struct file_system_type squashfs_fs_type = {
6770aa66619SPhillip Lougher .owner = THIS_MODULE,
6780aa66619SPhillip Lougher .name = "squashfs",
6795a2be128SDavid Howells .init_fs_context = squashfs_init_fs_context,
68010dde05bSVincent Whitchurch .parameters = squashfs_fs_parameters,
6810aa66619SPhillip Lougher .kill_sb = kill_block_super,
68242da66acSMichael Weiß .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
6830aa66619SPhillip Lougher };
6843e64fe5bSEric W. Biederman MODULE_ALIAS_FS("squashfs");
6850aa66619SPhillip Lougher
686b87221deSAlexey Dobriyan static const struct super_operations squashfs_super_ops = {
6870aa66619SPhillip Lougher .alloc_inode = squashfs_alloc_inode,
68856b5af19SAl Viro .free_inode = squashfs_free_inode,
6890aa66619SPhillip Lougher .statfs = squashfs_statfs,
6900aa66619SPhillip Lougher .put_super = squashfs_put_super,
69110dde05bSVincent Whitchurch .show_options = squashfs_show_options,
6920aa66619SPhillip Lougher };
6930aa66619SPhillip Lougher
6940aa66619SPhillip Lougher module_init(init_squashfs_fs);
6950aa66619SPhillip Lougher module_exit(exit_squashfs_fs);
6960aa66619SPhillip Lougher MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
697d7f2ff67SPhillip Lougher MODULE_AUTHOR("Phillip Lougher <phillip@squashfs.org.uk>");
6980aa66619SPhillip Lougher MODULE_LICENSE("GPL");
699