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