168252eb5SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
212260140SPhillip Lougher /*
312260140SPhillip Lougher * Squashfs - a compressed read only filesystem for Linux
412260140SPhillip Lougher *
512260140SPhillip Lougher * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
6d7f2ff67SPhillip Lougher * Phillip Lougher <phillip@squashfs.org.uk>
712260140SPhillip Lougher *
812260140SPhillip Lougher * export.c
912260140SPhillip Lougher */
1012260140SPhillip Lougher
1112260140SPhillip Lougher /*
1212260140SPhillip Lougher * This file implements code to make Squashfs filesystems exportable (NFS etc.)
1312260140SPhillip Lougher *
1412260140SPhillip Lougher * The export code uses an inode lookup table to map inode numbers passed in
1512260140SPhillip Lougher * filehandles to an inode location on disk. This table is stored compressed
1612260140SPhillip Lougher * into metadata blocks. A second index table is used to locate these. This
1712260140SPhillip Lougher * second index table for speed of access (and because it is small) is read at
1812260140SPhillip Lougher * mount time and cached in memory.
1912260140SPhillip Lougher *
2012260140SPhillip Lougher * The inode lookup table is used only by the export code, inode disk
2112260140SPhillip Lougher * locations are directly encoded in directories, enabling direct access
2212260140SPhillip Lougher * without an intermediate lookup for all operations except the export ops.
2312260140SPhillip Lougher */
2412260140SPhillip Lougher
2512260140SPhillip Lougher #include <linux/fs.h>
2612260140SPhillip Lougher #include <linux/vfs.h>
2712260140SPhillip Lougher #include <linux/dcache.h>
2812260140SPhillip Lougher #include <linux/exportfs.h>
2923516dc7SPekka Enberg #include <linux/slab.h>
3012260140SPhillip Lougher
3112260140SPhillip Lougher #include "squashfs_fs.h"
3212260140SPhillip Lougher #include "squashfs_fs_sb.h"
3312260140SPhillip Lougher #include "squashfs_fs_i.h"
3412260140SPhillip Lougher #include "squashfs.h"
3512260140SPhillip Lougher
3612260140SPhillip Lougher /*
3712260140SPhillip Lougher * Look-up inode number (ino) in table, returning the inode location.
3812260140SPhillip Lougher */
squashfs_inode_lookup(struct super_block * sb,int ino_num)3912260140SPhillip Lougher static long long squashfs_inode_lookup(struct super_block *sb, int ino_num)
4012260140SPhillip Lougher {
4112260140SPhillip Lougher struct squashfs_sb_info *msblk = sb->s_fs_info;
4212260140SPhillip Lougher int blk = SQUASHFS_LOOKUP_BLOCK(ino_num - 1);
4312260140SPhillip Lougher int offset = SQUASHFS_LOOKUP_BLOCK_OFFSET(ino_num - 1);
44eabac19eSPhillip Lougher u64 start;
4512260140SPhillip Lougher __le64 ino;
4612260140SPhillip Lougher int err;
4712260140SPhillip Lougher
4812260140SPhillip Lougher TRACE("Entered squashfs_inode_lookup, inode_number = %d\n", ino_num);
4912260140SPhillip Lougher
50eabac19eSPhillip Lougher if (ino_num == 0 || (ino_num - 1) >= msblk->inodes)
51eabac19eSPhillip Lougher return -EINVAL;
52eabac19eSPhillip Lougher
53eabac19eSPhillip Lougher start = le64_to_cpu(msblk->inode_lookup_table[blk]);
54eabac19eSPhillip Lougher
5512260140SPhillip Lougher err = squashfs_read_metadata(sb, &ino, &start, &offset, sizeof(ino));
5612260140SPhillip Lougher if (err < 0)
5712260140SPhillip Lougher return err;
5812260140SPhillip Lougher
5912260140SPhillip Lougher TRACE("squashfs_inode_lookup, inode = 0x%llx\n",
6012260140SPhillip Lougher (u64) le64_to_cpu(ino));
6112260140SPhillip Lougher
6212260140SPhillip Lougher return le64_to_cpu(ino);
6312260140SPhillip Lougher }
6412260140SPhillip Lougher
6512260140SPhillip Lougher
squashfs_export_iget(struct super_block * sb,unsigned int ino_num)6612260140SPhillip Lougher static struct dentry *squashfs_export_iget(struct super_block *sb,
6712260140SPhillip Lougher unsigned int ino_num)
6812260140SPhillip Lougher {
6912260140SPhillip Lougher long long ino;
7012260140SPhillip Lougher struct dentry *dentry = ERR_PTR(-ENOENT);
7112260140SPhillip Lougher
7212260140SPhillip Lougher TRACE("Entered squashfs_export_iget\n");
7312260140SPhillip Lougher
7412260140SPhillip Lougher ino = squashfs_inode_lookup(sb, ino_num);
7512260140SPhillip Lougher if (ino >= 0)
7612260140SPhillip Lougher dentry = d_obtain_alias(squashfs_iget(sb, ino, ino_num));
7712260140SPhillip Lougher
7812260140SPhillip Lougher return dentry;
7912260140SPhillip Lougher }
8012260140SPhillip Lougher
8112260140SPhillip Lougher
squashfs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)8212260140SPhillip Lougher static struct dentry *squashfs_fh_to_dentry(struct super_block *sb,
8312260140SPhillip Lougher struct fid *fid, int fh_len, int fh_type)
8412260140SPhillip Lougher {
8512260140SPhillip Lougher if ((fh_type != FILEID_INO32_GEN && fh_type != FILEID_INO32_GEN_PARENT)
8612260140SPhillip Lougher || fh_len < 2)
8712260140SPhillip Lougher return NULL;
8812260140SPhillip Lougher
8912260140SPhillip Lougher return squashfs_export_iget(sb, fid->i32.ino);
9012260140SPhillip Lougher }
9112260140SPhillip Lougher
9212260140SPhillip Lougher
squashfs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)9312260140SPhillip Lougher static struct dentry *squashfs_fh_to_parent(struct super_block *sb,
9412260140SPhillip Lougher struct fid *fid, int fh_len, int fh_type)
9512260140SPhillip Lougher {
9612260140SPhillip Lougher if (fh_type != FILEID_INO32_GEN_PARENT || fh_len < 4)
9712260140SPhillip Lougher return NULL;
9812260140SPhillip Lougher
9912260140SPhillip Lougher return squashfs_export_iget(sb, fid->i32.parent_ino);
10012260140SPhillip Lougher }
10112260140SPhillip Lougher
10212260140SPhillip Lougher
squashfs_get_parent(struct dentry * child)10312260140SPhillip Lougher static struct dentry *squashfs_get_parent(struct dentry *child)
10412260140SPhillip Lougher {
1052b0143b5SDavid Howells struct inode *inode = d_inode(child);
10612260140SPhillip Lougher unsigned int parent_ino = squashfs_i(inode)->parent;
10712260140SPhillip Lougher
10812260140SPhillip Lougher return squashfs_export_iget(inode->i_sb, parent_ino);
10912260140SPhillip Lougher }
11012260140SPhillip Lougher
11112260140SPhillip Lougher
11212260140SPhillip Lougher /*
11312260140SPhillip Lougher * Read uncompressed inode lookup table indexes off disk into memory
11412260140SPhillip Lougher */
squashfs_read_inode_lookup_table(struct super_block * sb,u64 lookup_table_start,u64 next_table,unsigned int inodes)11512260140SPhillip Lougher __le64 *squashfs_read_inode_lookup_table(struct super_block *sb,
116ac51a0a7SPhillip Lougher u64 lookup_table_start, u64 next_table, unsigned int inodes)
11712260140SPhillip Lougher {
11812260140SPhillip Lougher unsigned int length = SQUASHFS_LOOKUP_BLOCK_BYTES(inodes);
119eabac19eSPhillip Lougher unsigned int indexes = SQUASHFS_LOOKUP_BLOCKS(inodes);
120eabac19eSPhillip Lougher int n;
121ac51a0a7SPhillip Lougher __le64 *table;
122eabac19eSPhillip Lougher u64 start, end;
12312260140SPhillip Lougher
12412260140SPhillip Lougher TRACE("In read_inode_lookup_table, length %d\n", length);
12512260140SPhillip Lougher
126ac51a0a7SPhillip Lougher /* Sanity check values */
127ac51a0a7SPhillip Lougher
128ac51a0a7SPhillip Lougher /* there should always be at least one inode */
129ac51a0a7SPhillip Lougher if (inodes == 0)
130ac51a0a7SPhillip Lougher return ERR_PTR(-EINVAL);
131ac51a0a7SPhillip Lougher
132eabac19eSPhillip Lougher /*
133eabac19eSPhillip Lougher * The computed size of the lookup table (length bytes) should exactly
134eabac19eSPhillip Lougher * match the table start and end points
135ac51a0a7SPhillip Lougher */
136eabac19eSPhillip Lougher if (length != (next_table - lookup_table_start))
137ac51a0a7SPhillip Lougher return ERR_PTR(-EINVAL);
138ac51a0a7SPhillip Lougher
139ac51a0a7SPhillip Lougher table = squashfs_read_table(sb, lookup_table_start, length);
140eabac19eSPhillip Lougher if (IS_ERR(table))
141eabac19eSPhillip Lougher return table;
142ac51a0a7SPhillip Lougher
143ac51a0a7SPhillip Lougher /*
144eabac19eSPhillip Lougher * table0], table[1], ... table[indexes - 1] store the locations
145eabac19eSPhillip Lougher * of the compressed inode lookup blocks. Each entry should be
146eabac19eSPhillip Lougher * less than the next (i.e. table[0] < table[1]), and the difference
147eabac19eSPhillip Lougher * between them should be SQUASHFS_METADATA_SIZE or less.
148eabac19eSPhillip Lougher * table[indexes - 1] should be less than lookup_table_start, and
149eabac19eSPhillip Lougher * again the difference should be SQUASHFS_METADATA_SIZE or less
150ac51a0a7SPhillip Lougher */
151eabac19eSPhillip Lougher for (n = 0; n < (indexes - 1); n++) {
152eabac19eSPhillip Lougher start = le64_to_cpu(table[n]);
153eabac19eSPhillip Lougher end = le64_to_cpu(table[n + 1]);
154eabac19eSPhillip Lougher
155*c1b20283SSean Nyekjaer if (start >= end
156*c1b20283SSean Nyekjaer || (end - start) >
157*c1b20283SSean Nyekjaer (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
158eabac19eSPhillip Lougher kfree(table);
159eabac19eSPhillip Lougher return ERR_PTR(-EINVAL);
160eabac19eSPhillip Lougher }
161eabac19eSPhillip Lougher }
162eabac19eSPhillip Lougher
163eabac19eSPhillip Lougher start = le64_to_cpu(table[indexes - 1]);
164*c1b20283SSean Nyekjaer if (start >= lookup_table_start ||
165*c1b20283SSean Nyekjaer (lookup_table_start - start) >
166*c1b20283SSean Nyekjaer (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
167ac51a0a7SPhillip Lougher kfree(table);
168ac51a0a7SPhillip Lougher return ERR_PTR(-EINVAL);
169ac51a0a7SPhillip Lougher }
170ac51a0a7SPhillip Lougher
171ac51a0a7SPhillip Lougher return table;
17212260140SPhillip Lougher }
17312260140SPhillip Lougher
17412260140SPhillip Lougher
17512260140SPhillip Lougher const struct export_operations squashfs_export_ops = {
17612260140SPhillip Lougher .fh_to_dentry = squashfs_fh_to_dentry,
17712260140SPhillip Lougher .fh_to_parent = squashfs_fh_to_parent,
17812260140SPhillip Lougher .get_parent = squashfs_get_parent
17912260140SPhillip Lougher };
180