^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) * export.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 make Squashfs filesystems exportable (NFS etc.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * The export code uses an inode lookup table to map inode numbers passed in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * filehandles to an inode location on disk. This table is stored compressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * into metadata blocks. A second index table is used to locate these. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * second index table for speed of access (and because it is small) is read at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * mount time and cached in memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * The inode lookup table is used only by the export code, inode disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * locations are directly encoded in directories, enabling direct access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * without an intermediate lookup for all operations except the export ops.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/vfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/dcache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/exportfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include "squashfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include "squashfs_fs_sb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include "squashfs_fs_i.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include "squashfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * Look-up inode number (ino) in table, returning the inode location.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static long long squashfs_inode_lookup(struct super_block *sb, int ino_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct squashfs_sb_info *msblk = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int blk = SQUASHFS_LOOKUP_BLOCK(ino_num - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int offset = SQUASHFS_LOOKUP_BLOCK_OFFSET(ino_num - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) __le64 ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) TRACE("Entered squashfs_inode_lookup, inode_number = %d\n", ino_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (ino_num == 0 || (ino_num - 1) >= msblk->inodes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) start = le64_to_cpu(msblk->inode_lookup_table[blk]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) err = squashfs_read_metadata(sb, &ino, &start, &offset, sizeof(ino));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) TRACE("squashfs_inode_lookup, inode = 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) (u64) le64_to_cpu(ino));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return le64_to_cpu(ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static struct dentry *squashfs_export_iget(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) unsigned int ino_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) long long ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct dentry *dentry = ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) TRACE("Entered squashfs_export_iget\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) ino = squashfs_inode_lookup(sb, ino_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (ino >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) dentry = d_obtain_alias(squashfs_iget(sb, ino, ino_num));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static struct dentry *squashfs_fh_to_dentry(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct fid *fid, int fh_len, int fh_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if ((fh_type != FILEID_INO32_GEN && fh_type != FILEID_INO32_GEN_PARENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) || fh_len < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return squashfs_export_iget(sb, fid->i32.ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static struct dentry *squashfs_fh_to_parent(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct fid *fid, int fh_len, int fh_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (fh_type != FILEID_INO32_GEN_PARENT || fh_len < 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return squashfs_export_iget(sb, fid->i32.parent_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static struct dentry *squashfs_get_parent(struct dentry *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct inode *inode = d_inode(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) unsigned int parent_ino = squashfs_i(inode)->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return squashfs_export_iget(inode->i_sb, parent_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * Read uncompressed inode lookup table indexes off disk into memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) __le64 *squashfs_read_inode_lookup_table(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) u64 lookup_table_start, u64 next_table, unsigned int inodes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) unsigned int length = SQUASHFS_LOOKUP_BLOCK_BYTES(inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) unsigned int indexes = SQUASHFS_LOOKUP_BLOCKS(inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) __le64 *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) u64 start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) TRACE("In read_inode_lookup_table, length %d\n", length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /* Sanity check values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /* there should always be at least one inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (inodes == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * The computed size of the lookup table (length bytes) should exactly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * match the table start and end points
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (length != (next_table - lookup_table_start))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) table = squashfs_read_table(sb, lookup_table_start, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (IS_ERR(table))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * table0], table[1], ... table[indexes - 1] store the locations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * of the compressed inode lookup blocks. Each entry should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * less than the next (i.e. table[0] < table[1]), and the difference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * between them should be SQUASHFS_METADATA_SIZE or less.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * table[indexes - 1] should be less than lookup_table_start, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * again the difference should be SQUASHFS_METADATA_SIZE or less
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) for (n = 0; n < (indexes - 1); n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) start = le64_to_cpu(table[n]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) end = le64_to_cpu(table[n + 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (start >= end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) || (end - start) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) kfree(table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) start = le64_to_cpu(table[indexes - 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (start >= lookup_table_start ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) (lookup_table_start - start) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) kfree(table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) const struct export_operations squashfs_export_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .fh_to_dentry = squashfs_fh_to_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .fh_to_parent = squashfs_fh_to_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .get_parent = squashfs_get_parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) };