diff options
author | Phillip Lougher <phillip@lougher.demon.co.uk> | 2009-01-05 03:46:23 -0500 |
---|---|---|
committer | Phillip Lougher <phillip@lougher.demon.co.uk> | 2009-01-05 03:46:23 -0500 |
commit | c88da2c979369e6bf8d2c0c80fad2f90c35e64ce (patch) | |
tree | 56b94069134e65b3788fc24e6c7489a4389edf29 /fs | |
parent | 6545b246a2c815a8fcd07d58240effb6ec3481b1 (diff) |
Squashfs: directory lookup operations
Signed-off-by: Phillip Lougher <phillip@lougher.demon.co.uk>
Diffstat (limited to 'fs')
-rw-r--r-- | fs/squashfs/namei.c | 242 |
1 files changed, 242 insertions, 0 deletions
diff --git a/fs/squashfs/namei.c b/fs/squashfs/namei.c new file mode 100644 index 000000000000..9e398653b22b --- /dev/null +++ b/fs/squashfs/namei.c | |||
@@ -0,0 +1,242 @@ | |||
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@lougher.demon.co.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 | * namei.c | ||
22 | */ | ||
23 | |||
24 | /* | ||
25 | * This file implements code to do filename lookup in directories. | ||
26 | * | ||
27 | * Like inodes, directories are packed into compressed metadata blocks, stored | ||
28 | * in a directory table. Directories are accessed using the start address of | ||
29 | * the metablock containing the directory and the offset into the | ||
30 | * decompressed block (<block, offset>). | ||
31 | * | ||
32 | * Directories are organised in a slightly complex way, and are not simply | ||
33 | * a list of file names. The organisation takes advantage of the | ||
34 | * fact that (in most cases) the inodes of the files will be in the same | ||
35 | * compressed metadata block, and therefore, can share the start block. | ||
36 | * Directories are therefore organised in a two level list, a directory | ||
37 | * header containing the shared start block value, and a sequence of directory | ||
38 | * entries, each of which share the shared start block. A new directory header | ||
39 | * is written once/if the inode start block changes. The directory | ||
40 | * header/directory entry list is repeated as many times as necessary. | ||
41 | * | ||
42 | * Directories are sorted, and can contain a directory index to speed up | ||
43 | * file lookup. Directory indexes store one entry per metablock, each entry | ||
44 | * storing the index/filename mapping to the first directory header | ||
45 | * in each metadata block. Directories are sorted in alphabetical order, | ||
46 | * and at lookup the index is scanned linearly looking for the first filename | ||
47 | * alphabetically larger than the filename being looked up. At this point the | ||
48 | * location of the metadata block the filename is in has been found. | ||
49 | * The general idea of the index is ensure only one metadata block needs to be | ||
50 | * decompressed to do a lookup irrespective of the length of the directory. | ||
51 | * This scheme has the advantage that it doesn't require extra memory overhead | ||
52 | * and doesn't require much extra storage on disk. | ||
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/zlib.h> | ||
61 | |||
62 | #include "squashfs_fs.h" | ||
63 | #include "squashfs_fs_sb.h" | ||
64 | #include "squashfs_fs_i.h" | ||
65 | #include "squashfs.h" | ||
66 | |||
67 | /* | ||
68 | * Lookup name in the directory index, returning the location of the metadata | ||
69 | * block containing it, and the directory index this represents. | ||
70 | * | ||
71 | * If we get an error reading the index then return the part of the index | ||
72 | * (if any) we have managed to read - the index isn't essential, just | ||
73 | * quicker. | ||
74 | */ | ||
75 | static int get_dir_index_using_name(struct super_block *sb, | ||
76 | u64 *next_block, int *next_offset, u64 index_start, | ||
77 | int index_offset, int i_count, const char *name, | ||
78 | int len) | ||
79 | { | ||
80 | struct squashfs_sb_info *msblk = sb->s_fs_info; | ||
81 | int i, size, length = 0, err; | ||
82 | struct squashfs_dir_index *index; | ||
83 | char *str; | ||
84 | |||
85 | TRACE("Entered get_dir_index_using_name, i_count %d\n", i_count); | ||
86 | |||
87 | index = kmalloc(sizeof(*index) + SQUASHFS_NAME_LEN * 2 + 2, GFP_KERNEL); | ||
88 | if (index == NULL) { | ||
89 | ERROR("Failed to allocate squashfs_dir_index\n"); | ||
90 | goto out; | ||
91 | } | ||
92 | |||
93 | str = &index->name[SQUASHFS_NAME_LEN + 1]; | ||
94 | strncpy(str, name, len); | ||
95 | str[len] = '\0'; | ||
96 | |||
97 | for (i = 0; i < i_count; i++) { | ||
98 | err = squashfs_read_metadata(sb, index, &index_start, | ||
99 | &index_offset, sizeof(*index)); | ||
100 | if (err < 0) | ||
101 | break; | ||
102 | |||
103 | |||
104 | size = le32_to_cpu(index->size) + 1; | ||
105 | |||
106 | err = squashfs_read_metadata(sb, index->name, &index_start, | ||
107 | &index_offset, size); | ||
108 | if (err < 0) | ||
109 | break; | ||
110 | |||
111 | index->name[size] = '\0'; | ||
112 | |||
113 | if (strcmp(index->name, str) > 0) | ||
114 | break; | ||
115 | |||
116 | length = le32_to_cpu(index->index); | ||
117 | *next_block = le32_to_cpu(index->start_block) + | ||
118 | msblk->directory_table; | ||
119 | } | ||
120 | |||
121 | *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE; | ||
122 | kfree(index); | ||
123 | |||
124 | out: | ||
125 | /* | ||
126 | * Return index (f_pos) of the looked up metadata block. Translate | ||
127 | * from internal f_pos to external f_pos which is offset by 3 because | ||
128 | * we invent "." and ".." entries which are not actually stored in the | ||
129 | * directory. | ||
130 | */ | ||
131 | return length + 3; | ||
132 | } | ||
133 | |||
134 | |||
135 | static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry, | ||
136 | struct nameidata *nd) | ||
137 | { | ||
138 | const unsigned char *name = dentry->d_name.name; | ||
139 | int len = dentry->d_name.len; | ||
140 | struct inode *inode = NULL; | ||
141 | struct squashfs_sb_info *msblk = dir->i_sb->s_fs_info; | ||
142 | struct squashfs_dir_header dirh; | ||
143 | struct squashfs_dir_entry *dire; | ||
144 | u64 block = squashfs_i(dir)->start + msblk->directory_table; | ||
145 | int offset = squashfs_i(dir)->offset; | ||
146 | int err, length = 0, dir_count, size; | ||
147 | |||
148 | TRACE("Entered squashfs_lookup [%llx:%x]\n", block, offset); | ||
149 | |||
150 | dire = kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL); | ||
151 | if (dire == NULL) { | ||
152 | ERROR("Failed to allocate squashfs_dir_entry\n"); | ||
153 | return ERR_PTR(-ENOMEM); | ||
154 | } | ||
155 | |||
156 | if (len > SQUASHFS_NAME_LEN) { | ||
157 | err = -ENAMETOOLONG; | ||
158 | goto failed; | ||
159 | } | ||
160 | |||
161 | length = get_dir_index_using_name(dir->i_sb, &block, &offset, | ||
162 | squashfs_i(dir)->dir_idx_start, | ||
163 | squashfs_i(dir)->dir_idx_offset, | ||
164 | squashfs_i(dir)->dir_idx_cnt, name, len); | ||
165 | |||
166 | while (length < i_size_read(dir)) { | ||
167 | /* | ||
168 | * Read directory header. | ||
169 | */ | ||
170 | err = squashfs_read_metadata(dir->i_sb, &dirh, &block, | ||
171 | &offset, sizeof(dirh)); | ||
172 | if (err < 0) | ||
173 | goto read_failure; | ||
174 | |||
175 | length += sizeof(dirh); | ||
176 | |||
177 | dir_count = le32_to_cpu(dirh.count) + 1; | ||
178 | while (dir_count--) { | ||
179 | /* | ||
180 | * Read directory entry. | ||
181 | */ | ||
182 | err = squashfs_read_metadata(dir->i_sb, dire, &block, | ||
183 | &offset, sizeof(*dire)); | ||
184 | if (err < 0) | ||
185 | goto read_failure; | ||
186 | |||
187 | size = le16_to_cpu(dire->size) + 1; | ||
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 | if (IS_ERR(inode)) { | ||
214 | err = PTR_ERR(inode); | ||
215 | goto failed; | ||
216 | } | ||
217 | |||
218 | goto exit_lookup; | ||
219 | } | ||
220 | } | ||
221 | } | ||
222 | |||
223 | exit_lookup: | ||
224 | kfree(dire); | ||
225 | if (inode) | ||
226 | return d_splice_alias(inode, dentry); | ||
227 | d_add(dentry, inode); | ||
228 | return ERR_PTR(0); | ||
229 | |||
230 | read_failure: | ||
231 | ERROR("Unable to read directory block [%llx:%x]\n", | ||
232 | squashfs_i(dir)->start + msblk->directory_table, | ||
233 | squashfs_i(dir)->offset); | ||
234 | failed: | ||
235 | kfree(dire); | ||
236 | return ERR_PTR(err); | ||
237 | } | ||
238 | |||
239 | |||
240 | const struct inode_operations squashfs_dir_inode_ops = { | ||
241 | .lookup = squashfs_lookup | ||
242 | }; | ||