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