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 * fragment.c
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun * This file implements code to handle compressed fragments (tail-end packed
13*4882a593Smuzhiyun * datablocks).
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * Regular files contain a fragment index which is mapped to a fragment
16*4882a593Smuzhiyun * location on disk and compressed size using a fragment lookup table.
17*4882a593Smuzhiyun * Like everything in Squashfs this fragment lookup table is itself stored
18*4882a593Smuzhiyun * compressed into metadata blocks. A second index table is used to locate
19*4882a593Smuzhiyun * these. This second index table for speed of access (and because it
20*4882a593Smuzhiyun * is small) is read at mount time and cached in memory.
21*4882a593Smuzhiyun */
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include <linux/fs.h>
24*4882a593Smuzhiyun #include <linux/vfs.h>
25*4882a593Smuzhiyun #include <linux/slab.h>
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include "squashfs_fs.h"
28*4882a593Smuzhiyun #include "squashfs_fs_sb.h"
29*4882a593Smuzhiyun #include "squashfs.h"
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /*
32*4882a593Smuzhiyun * Look-up fragment using the fragment index table. Return the on disk
33*4882a593Smuzhiyun * location of the fragment and its compressed size
34*4882a593Smuzhiyun */
squashfs_frag_lookup(struct super_block * sb,unsigned int fragment,u64 * fragment_block)35*4882a593Smuzhiyun int squashfs_frag_lookup(struct super_block *sb, unsigned int fragment,
36*4882a593Smuzhiyun u64 *fragment_block)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun struct squashfs_sb_info *msblk = sb->s_fs_info;
39*4882a593Smuzhiyun int block, offset, size;
40*4882a593Smuzhiyun struct squashfs_fragment_entry fragment_entry;
41*4882a593Smuzhiyun u64 start_block;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun if (fragment >= msblk->fragments)
44*4882a593Smuzhiyun return -EIO;
45*4882a593Smuzhiyun block = SQUASHFS_FRAGMENT_INDEX(fragment);
46*4882a593Smuzhiyun offset = SQUASHFS_FRAGMENT_INDEX_OFFSET(fragment);
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun start_block = le64_to_cpu(msblk->fragment_index[block]);
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun size = squashfs_read_metadata(sb, &fragment_entry, &start_block,
51*4882a593Smuzhiyun &offset, sizeof(fragment_entry));
52*4882a593Smuzhiyun if (size < 0)
53*4882a593Smuzhiyun return size;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun *fragment_block = le64_to_cpu(fragment_entry.start_block);
56*4882a593Smuzhiyun return squashfs_block_size(fragment_entry.size);
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /*
61*4882a593Smuzhiyun * Read the uncompressed fragment lookup table indexes off disk into memory
62*4882a593Smuzhiyun */
squashfs_read_fragment_index_table(struct super_block * sb,u64 fragment_table_start,u64 next_table,unsigned int fragments)63*4882a593Smuzhiyun __le64 *squashfs_read_fragment_index_table(struct super_block *sb,
64*4882a593Smuzhiyun u64 fragment_table_start, u64 next_table, unsigned int fragments)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun unsigned int length = SQUASHFS_FRAGMENT_INDEX_BYTES(fragments);
67*4882a593Smuzhiyun __le64 *table;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun * Sanity check, length bytes should not extend into the next table -
71*4882a593Smuzhiyun * this check also traps instances where fragment_table_start is
72*4882a593Smuzhiyun * incorrectly larger than the next table start
73*4882a593Smuzhiyun */
74*4882a593Smuzhiyun if (fragment_table_start + length > next_table)
75*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun table = squashfs_read_table(sb, fragment_table_start, length);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /*
80*4882a593Smuzhiyun * table[0] points to the first fragment table metadata block, this
81*4882a593Smuzhiyun * should be less than fragment_table_start
82*4882a593Smuzhiyun */
83*4882a593Smuzhiyun if (!IS_ERR(table) && le64_to_cpu(table[0]) >= fragment_table_start) {
84*4882a593Smuzhiyun kfree(table);
85*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun return table;
89*4882a593Smuzhiyun }
90