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