1 /*
2 * Squashfs - a compressed read only filesystem for Linux
3 *
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 * Phillip Lougher <phillip@squashfs.org.uk>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 * export.c
22 */
23
24 /*
25 * This file implements code to make Squashfs filesystems exportable (NFS etc.)
26 *
27 * The export code uses an inode lookup table to map inode numbers passed in
28 * filehandles to an inode location on disk. This table is stored compressed
29 * into metadata blocks. A second index table is used to locate these. This
30 * second index table for speed of access (and because it is small) is read at
31 * mount time and cached in memory.
32 *
33 * The inode lookup table is used only by the export code, inode disk
34 * locations are directly encoded in directories, enabling direct access
35 * without an intermediate lookup for all operations except the export ops.
36 */
37
38 #include <linux/fs.h>
39 #include <linux/vfs.h>
40 #include <linux/dcache.h>
41 #include <linux/exportfs.h>
42 #include <linux/slab.h>
43
44 #include "squashfs_fs.h"
45 #include "squashfs_fs_sb.h"
46 #include "squashfs_fs_i.h"
47 #include "squashfs.h"
48
49 /*
50 * Look-up inode number (ino) in table, returning the inode location.
51 */
squashfs_inode_lookup(struct super_block * sb,int ino_num)52 static long long squashfs_inode_lookup(struct super_block *sb, int ino_num)
53 {
54 struct squashfs_sb_info *msblk = sb->s_fs_info;
55 int blk = SQUASHFS_LOOKUP_BLOCK(ino_num - 1);
56 int offset = SQUASHFS_LOOKUP_BLOCK_OFFSET(ino_num - 1);
57 u64 start;
58 __le64 ino;
59 int err;
60
61 TRACE("Entered squashfs_inode_lookup, inode_number = %d\n", ino_num);
62
63 if (ino_num == 0 || (ino_num - 1) >= msblk->inodes)
64 return -EINVAL;
65
66 start = le64_to_cpu(msblk->inode_lookup_table[blk]);
67
68 err = squashfs_read_metadata(sb, &ino, &start, &offset, sizeof(ino));
69 if (err < 0)
70 return err;
71
72 TRACE("squashfs_inode_lookup, inode = 0x%llx\n",
73 (u64) le64_to_cpu(ino));
74
75 return le64_to_cpu(ino);
76 }
77
78
squashfs_export_iget(struct super_block * sb,unsigned int ino_num)79 static struct dentry *squashfs_export_iget(struct super_block *sb,
80 unsigned int ino_num)
81 {
82 long long ino;
83 struct dentry *dentry = ERR_PTR(-ENOENT);
84
85 TRACE("Entered squashfs_export_iget\n");
86
87 ino = squashfs_inode_lookup(sb, ino_num);
88 if (ino >= 0)
89 dentry = d_obtain_alias(squashfs_iget(sb, ino, ino_num));
90
91 return dentry;
92 }
93
94
squashfs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)95 static struct dentry *squashfs_fh_to_dentry(struct super_block *sb,
96 struct fid *fid, int fh_len, int fh_type)
97 {
98 if ((fh_type != FILEID_INO32_GEN && fh_type != FILEID_INO32_GEN_PARENT)
99 || fh_len < 2)
100 return NULL;
101
102 return squashfs_export_iget(sb, fid->i32.ino);
103 }
104
105
squashfs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)106 static struct dentry *squashfs_fh_to_parent(struct super_block *sb,
107 struct fid *fid, int fh_len, int fh_type)
108 {
109 if (fh_type != FILEID_INO32_GEN_PARENT || fh_len < 4)
110 return NULL;
111
112 return squashfs_export_iget(sb, fid->i32.parent_ino);
113 }
114
115
squashfs_get_parent(struct dentry * child)116 static struct dentry *squashfs_get_parent(struct dentry *child)
117 {
118 struct inode *inode = d_inode(child);
119 unsigned int parent_ino = squashfs_i(inode)->parent;
120
121 return squashfs_export_iget(inode->i_sb, parent_ino);
122 }
123
124
125 /*
126 * Read uncompressed inode lookup table indexes off disk into memory
127 */
squashfs_read_inode_lookup_table(struct super_block * sb,u64 lookup_table_start,u64 next_table,unsigned int inodes)128 __le64 *squashfs_read_inode_lookup_table(struct super_block *sb,
129 u64 lookup_table_start, u64 next_table, unsigned int inodes)
130 {
131 unsigned int length = SQUASHFS_LOOKUP_BLOCK_BYTES(inodes);
132 unsigned int indexes = SQUASHFS_LOOKUP_BLOCKS(inodes);
133 int n;
134 __le64 *table;
135 u64 start, end;
136
137 TRACE("In read_inode_lookup_table, length %d\n", length);
138
139 /* Sanity check values */
140
141 /* there should always be at least one inode */
142 if (inodes == 0)
143 return ERR_PTR(-EINVAL);
144
145 /*
146 * The computed size of the lookup table (length bytes) should exactly
147 * match the table start and end points
148 */
149 if (length != (next_table - lookup_table_start))
150 return ERR_PTR(-EINVAL);
151
152 table = squashfs_read_table(sb, lookup_table_start, length);
153 if (IS_ERR(table))
154 return table;
155
156 /*
157 * table0], table[1], ... table[indexes - 1] store the locations
158 * of the compressed inode lookup blocks. Each entry should be
159 * less than the next (i.e. table[0] < table[1]), and the difference
160 * between them should be SQUASHFS_METADATA_SIZE or less.
161 * table[indexes - 1] should be less than lookup_table_start, and
162 * again the difference should be SQUASHFS_METADATA_SIZE or less
163 */
164 for (n = 0; n < (indexes - 1); n++) {
165 start = le64_to_cpu(table[n]);
166 end = le64_to_cpu(table[n + 1]);
167
168 if (start >= end
169 || (end - start) >
170 (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
171 kfree(table);
172 return ERR_PTR(-EINVAL);
173 }
174 }
175
176 start = le64_to_cpu(table[indexes - 1]);
177 if (start >= lookup_table_start ||
178 (lookup_table_start - start) >
179 (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
180 kfree(table);
181 return ERR_PTR(-EINVAL);
182 }
183
184 return table;
185 }
186
187
188 const struct export_operations squashfs_export_ops = {
189 .fh_to_dentry = squashfs_fh_to_dentry,
190 .fh_to_parent = squashfs_fh_to_parent,
191 .get_parent = squashfs_get_parent
192 };
193