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