/src/barebox/fs/squashfs/namei.c
Line | Count | Source |
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 | | * namei.c |
18 | | */ |
19 | | |
20 | | /* |
21 | | * This file implements code to do filename lookup in directories. |
22 | | * |
23 | | * Like inodes, directories are packed into compressed metadata blocks, stored |
24 | | * in a directory table. Directories are accessed using the start address of |
25 | | * the metablock containing the directory and the offset into the |
26 | | * decompressed block (<block, offset>). |
27 | | * |
28 | | * Directories are organised in a slightly complex way, and are not simply |
29 | | * a list of file names. The organisation takes advantage of the |
30 | | * fact that (in most cases) the inodes of the files will be in the same |
31 | | * compressed metadata block, and therefore, can share the start block. |
32 | | * Directories are therefore organised in a two level list, a directory |
33 | | * header containing the shared start block value, and a sequence of directory |
34 | | * entries, each of which share the shared start block. A new directory header |
35 | | * is written once/if the inode start block changes. The directory |
36 | | * header/directory entry list is repeated as many times as necessary. |
37 | | * |
38 | | * Directories are sorted, and can contain a directory index to speed up |
39 | | * file lookup. Directory indexes store one entry per metablock, each entry |
40 | | * storing the index/filename mapping to the first directory header |
41 | | * in each metadata block. Directories are sorted in alphabetical order, |
42 | | * and at lookup the index is scanned linearly looking for the first filename |
43 | | * alphabetically larger than the filename being looked up. At this point the |
44 | | * location of the metadata block the filename is in has been found. |
45 | | * The general idea of the index is ensure only one metadata block needs to be |
46 | | * decompressed to do a lookup irrespective of the length of the directory. |
47 | | * This scheme has the advantage that it doesn't require extra memory overhead |
48 | | * and doesn't require much extra storage on disk. |
49 | | */ |
50 | | |
51 | | #include <common.h> |
52 | | #include <linux/fs.h> |
53 | | #include <malloc.h> |
54 | | #include <linux/string.h> |
55 | | #include <linux/dcache.h> |
56 | | |
57 | | #include "squashfs_fs.h" |
58 | | #include "squashfs_fs_sb.h" |
59 | | #include "squashfs_fs_i.h" |
60 | | #include "squashfs.h" |
61 | | |
62 | | /* |
63 | | * Lookup name in the directory index, returning the location of the metadata |
64 | | * block containing it, and the directory index this represents. |
65 | | * |
66 | | * If we get an error reading the index then return the part of the index |
67 | | * (if any) we have managed to read - the index isn't essential, just |
68 | | * quicker. |
69 | | */ |
70 | | static int get_dir_index_using_name(struct super_block *sb, |
71 | | u64 *next_block, int *next_offset, u64 index_start, |
72 | | int index_offset, int i_count, const char *name, |
73 | | int len) |
74 | 153k | { |
75 | 153k | struct squashfs_sb_info *msblk = sb->s_fs_info; |
76 | 153k | int i, length = 0, err; |
77 | 153k | unsigned int size; |
78 | 153k | struct squashfs_dir_index *index; |
79 | 153k | char *str; |
80 | | |
81 | 153k | TRACE("Entered get_dir_index_using_name, i_count %d\n", i_count); |
82 | | |
83 | 153k | index = kmalloc(sizeof(*index) + SQUASHFS_NAME_LEN * 2 + 2, GFP_KERNEL); |
84 | 153k | if (index == NULL) { |
85 | 0 | ERROR("Failed to allocate squashfs_dir_index\n"); |
86 | 0 | goto out; |
87 | 0 | } |
88 | | |
89 | 153k | str = &index->name[SQUASHFS_NAME_LEN + 1]; |
90 | 153k | strncpy(str, name, len); |
91 | 153k | str[len] = '\0'; |
92 | | |
93 | 157k | for (i = 0; i < i_count; i++) { |
94 | 10.0k | err = squashfs_read_metadata(sb, index, &index_start, |
95 | 10.0k | &index_offset, sizeof(*index)); |
96 | 10.0k | if (err < 0) |
97 | 491 | break; |
98 | | |
99 | | |
100 | 9.52k | size = le32_to_cpu(index->size) + 1; |
101 | 9.52k | if (size > SQUASHFS_NAME_LEN) |
102 | 2.33k | break; |
103 | | |
104 | 7.19k | err = squashfs_read_metadata(sb, index->name, &index_start, |
105 | 7.19k | &index_offset, size); |
106 | 7.19k | if (err < 0) |
107 | 302 | break; |
108 | | |
109 | 6.88k | index->name[size] = '\0'; |
110 | | |
111 | 6.88k | if (strcmp(index->name, str) > 0) |
112 | 2.18k | break; |
113 | | |
114 | 4.70k | length = le32_to_cpu(index->index); |
115 | 4.70k | *next_block = le32_to_cpu(index->start_block) + |
116 | 4.70k | msblk->directory_table; |
117 | 4.70k | } |
118 | | |
119 | 153k | *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE; |
120 | 153k | kfree(index); |
121 | | |
122 | 153k | out: |
123 | | /* |
124 | | * Return index (f_pos) of the looked up metadata block. Translate |
125 | | * from internal f_pos to external f_pos which is offset by 3 because |
126 | | * we invent "." and ".." entries which are not actually stored in the |
127 | | * directory. |
128 | | */ |
129 | 153k | return length + 3; |
130 | 153k | } |
131 | | |
132 | | |
133 | | static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry, |
134 | | unsigned int flags) |
135 | 153k | { |
136 | 153k | const unsigned char *name = dentry->d_name.name; |
137 | 153k | int len = dentry->d_name.len; |
138 | 153k | struct inode *inode = NULL; |
139 | 153k | struct squashfs_sb_info *msblk = dir->i_sb->s_fs_info; |
140 | 153k | struct squashfs_dir_header dirh; |
141 | 153k | struct squashfs_dir_entry *dire; |
142 | 153k | u64 block = squashfs_i(dir)->start + msblk->directory_table; |
143 | 153k | int offset = squashfs_i(dir)->offset; |
144 | 153k | int err, length; |
145 | 153k | unsigned int dir_count, size; |
146 | | |
147 | 153k | TRACE("Entered squashfs_lookup [%llx:%x]\n", block, offset); |
148 | 153k | dire = kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL); |
149 | 153k | if (dire == NULL) { |
150 | 0 | ERROR("Failed to allocate squashfs_dir_entry\n"); |
151 | 0 | return ERR_PTR(-ENOMEM); |
152 | 0 | } |
153 | | |
154 | 153k | if (len > SQUASHFS_NAME_LEN) { |
155 | 0 | err = -ENAMETOOLONG; |
156 | 0 | goto failed; |
157 | 0 | } |
158 | | |
159 | 153k | length = get_dir_index_using_name(dir->i_sb, &block, &offset, |
160 | 153k | squashfs_i(dir)->dir_idx_start, |
161 | 153k | squashfs_i(dir)->dir_idx_offset, |
162 | 153k | squashfs_i(dir)->dir_idx_cnt, name, len); |
163 | | |
164 | 429k | while (length < i_size_read(dir)) { |
165 | | /* |
166 | | * Read directory header. |
167 | | */ |
168 | 429k | err = squashfs_read_metadata(dir->i_sb, &dirh, &block, |
169 | 429k | &offset, sizeof(dirh)); |
170 | 429k | if (err < 0) |
171 | 1.00k | goto read_failure; |
172 | | |
173 | 428k | length += sizeof(dirh); |
174 | | |
175 | 428k | dir_count = le32_to_cpu(dirh.count) + 1; |
176 | | |
177 | 428k | if (dir_count > SQUASHFS_DIR_COUNT) |
178 | 444 | goto data_error; |
179 | | |
180 | 772k | while (dir_count--) { |
181 | | /* |
182 | | * Read directory entry. |
183 | | */ |
184 | 496k | err = squashfs_read_metadata(dir->i_sb, dire, &block, |
185 | 496k | &offset, sizeof(*dire)); |
186 | 496k | if (err < 0) |
187 | 110 | goto read_failure; |
188 | | |
189 | 496k | size = le16_to_cpu(dire->size) + 1; |
190 | | |
191 | | /* size should never be larger than SQUASHFS_NAME_LEN */ |
192 | 496k | if (size > SQUASHFS_NAME_LEN) |
193 | 404 | goto data_error; |
194 | | |
195 | 495k | err = squashfs_read_metadata(dir->i_sb, dire->name, |
196 | 495k | &block, &offset, size); |
197 | 495k | if (err < 0) |
198 | 335 | goto read_failure; |
199 | | |
200 | 495k | length += sizeof(*dire) + size; |
201 | 495k | dire->name[len] = '\0'; |
202 | 495k | if (name[0] < dire->name[0]) |
203 | 25.2k | goto exit_lookup; |
204 | | |
205 | 470k | if (len == size && !strncmp(name, dire->name, len)) { |
206 | 125k | unsigned int blk, off, ino_num; |
207 | 125k | long long ino; |
208 | 125k | blk = le32_to_cpu(dirh.start_block); |
209 | 125k | off = le16_to_cpu(dire->offset); |
210 | 125k | ino_num = le32_to_cpu(dirh.inode_number) + |
211 | 125k | (short) le16_to_cpu(dire->inode_number); |
212 | 125k | ino = SQUASHFS_MKINODE(blk, off); |
213 | | |
214 | 125k | TRACE("calling squashfs_iget for directory " |
215 | 125k | "entry %s, inode %x:%x, %d\n", name, |
216 | 125k | blk, off, ino_num); |
217 | | |
218 | 125k | inode = squashfs_iget(dir->i_sb, ino, ino_num); |
219 | 125k | goto exit_lookup; |
220 | 125k | } |
221 | 470k | } |
222 | 427k | } |
223 | | |
224 | 150k | exit_lookup: |
225 | 150k | kfree(dire); |
226 | 150k | if (IS_ERR(inode)) |
227 | 19.3k | return ERR_CAST(inode); |
228 | 131k | d_add(dentry, inode); |
229 | 131k | return NULL; |
230 | | |
231 | 848 | data_error: |
232 | 848 | err = -EIO; |
233 | | |
234 | 2.29k | read_failure: |
235 | 2.29k | ERROR("Unable to read directory block [%llx:%x]\n", |
236 | 2.29k | squashfs_i(dir)->start + msblk->directory_table, |
237 | 2.29k | squashfs_i(dir)->offset); |
238 | 2.29k | failed: |
239 | 2.29k | kfree(dire); |
240 | 2.29k | return ERR_PTR(err); |
241 | 2.29k | } |
242 | | |
243 | | const struct inode_operations squashfs_dir_inode_ops = { |
244 | | .lookup = squashfs_lookup, |
245 | | }; |