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