1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55#include <linux/fs.h>
56#include <linux/vfs.h>
57#include <linux/slab.h>
58#include <linux/string.h>
59#include <linux/dcache.h>
60#include <linux/xattr.h>
61
62#include "squashfs_fs.h"
63#include "squashfs_fs_sb.h"
64#include "squashfs_fs_i.h"
65#include "squashfs.h"
66#include "xattr.h"
67
68
69
70
71
72
73
74
75
76static int get_dir_index_using_name(struct super_block *sb,
77 u64 *next_block, int *next_offset, u64 index_start,
78 int index_offset, int i_count, const char *name,
79 int len)
80{
81 struct squashfs_sb_info *msblk = sb->s_fs_info;
82 int i, length = 0, err;
83 unsigned int size;
84 struct squashfs_dir_index *index;
85 char *str;
86
87 TRACE("Entered get_dir_index_using_name, i_count %d\n", i_count);
88
89 index = kmalloc(sizeof(*index) + SQUASHFS_NAME_LEN * 2 + 2, GFP_KERNEL);
90 if (index == NULL) {
91 ERROR("Failed to allocate squashfs_dir_index\n");
92 goto out;
93 }
94
95 str = &index->name[SQUASHFS_NAME_LEN + 1];
96 strncpy(str, name, len);
97 str[len] = '\0';
98
99 for (i = 0; i < i_count; i++) {
100 err = squashfs_read_metadata(sb, index, &index_start,
101 &index_offset, sizeof(*index));
102 if (err < 0)
103 break;
104
105
106 size = le32_to_cpu(index->size) + 1;
107 if (size > SQUASHFS_NAME_LEN)
108 break;
109
110 err = squashfs_read_metadata(sb, index->name, &index_start,
111 &index_offset, size);
112 if (err < 0)
113 break;
114
115 index->name[size] = '\0';
116
117 if (strcmp(index->name, str) > 0)
118 break;
119
120 length = le32_to_cpu(index->index);
121 *next_block = le32_to_cpu(index->start_block) +
122 msblk->directory_table;
123 }
124
125 *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
126 kfree(index);
127
128out:
129
130
131
132
133
134
135 return length + 3;
136}
137
138
139static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry,
140 unsigned int flags)
141{
142 const unsigned char *name = dentry->d_name.name;
143 int len = dentry->d_name.len;
144 struct inode *inode = NULL;
145 struct squashfs_sb_info *msblk = dir->i_sb->s_fs_info;
146 struct squashfs_dir_header dirh;
147 struct squashfs_dir_entry *dire;
148 u64 block = squashfs_i(dir)->start + msblk->directory_table;
149 int offset = squashfs_i(dir)->offset;
150 int err, length;
151 unsigned int dir_count, size;
152
153 TRACE("Entered squashfs_lookup [%llx:%x]\n", block, offset);
154
155 dire = kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
156 if (dire == NULL) {
157 ERROR("Failed to allocate squashfs_dir_entry\n");
158 return ERR_PTR(-ENOMEM);
159 }
160
161 if (len > SQUASHFS_NAME_LEN) {
162 err = -ENAMETOOLONG;
163 goto failed;
164 }
165
166 length = get_dir_index_using_name(dir->i_sb, &block, &offset,
167 squashfs_i(dir)->dir_idx_start,
168 squashfs_i(dir)->dir_idx_offset,
169 squashfs_i(dir)->dir_idx_cnt, name, len);
170
171 while (length < i_size_read(dir)) {
172
173
174
175 err = squashfs_read_metadata(dir->i_sb, &dirh, &block,
176 &offset, sizeof(dirh));
177 if (err < 0)
178 goto read_failure;
179
180 length += sizeof(dirh);
181
182 dir_count = le32_to_cpu(dirh.count) + 1;
183
184 if (dir_count > SQUASHFS_DIR_COUNT)
185 goto data_error;
186
187 while (dir_count--) {
188
189
190
191 err = squashfs_read_metadata(dir->i_sb, dire, &block,
192 &offset, sizeof(*dire));
193 if (err < 0)
194 goto read_failure;
195
196 size = le16_to_cpu(dire->size) + 1;
197
198
199 if (size > SQUASHFS_NAME_LEN)
200 goto data_error;
201
202 err = squashfs_read_metadata(dir->i_sb, dire->name,
203 &block, &offset, size);
204 if (err < 0)
205 goto read_failure;
206
207 length += sizeof(*dire) + size;
208
209 if (name[0] < dire->name[0])
210 goto exit_lookup;
211
212 if (len == size && !strncmp(name, dire->name, len)) {
213 unsigned int blk, off, ino_num;
214 long long ino;
215 blk = le32_to_cpu(dirh.start_block);
216 off = le16_to_cpu(dire->offset);
217 ino_num = le32_to_cpu(dirh.inode_number) +
218 (short) le16_to_cpu(dire->inode_number);
219 ino = SQUASHFS_MKINODE(blk, off);
220
221 TRACE("calling squashfs_iget for directory "
222 "entry %s, inode %x:%x, %d\n", name,
223 blk, off, ino_num);
224
225 inode = squashfs_iget(dir->i_sb, ino, ino_num);
226 goto exit_lookup;
227 }
228 }
229 }
230
231exit_lookup:
232 kfree(dire);
233 return d_splice_alias(inode, dentry);
234
235data_error:
236 err = -EIO;
237
238read_failure:
239 ERROR("Unable to read directory block [%llx:%x]\n",
240 squashfs_i(dir)->start + msblk->directory_table,
241 squashfs_i(dir)->offset);
242failed:
243 kfree(dire);
244 return ERR_PTR(err);
245}
246
247
248const struct inode_operations squashfs_dir_inode_ops = {
249 .lookup = squashfs_lookup,
250 .getxattr = generic_getxattr,
251 .listxattr = squashfs_listxattr
252};
253