1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/slab.h>
23
24#include "aops.h"
25#include "collate.h"
26#include "debug.h"
27#include "index.h"
28#include "ntfs.h"
29
30
31
32
33
34
35
36
37
38
39ntfs_index_context *ntfs_index_ctx_get(ntfs_inode *idx_ni)
40{
41 ntfs_index_context *ictx;
42
43 ictx = kmem_cache_alloc(ntfs_index_ctx_cache, GFP_NOFS);
44 if (ictx)
45 *ictx = (ntfs_index_context){ .idx_ni = idx_ni };
46 return ictx;
47}
48
49
50
51
52
53
54
55
56
57void ntfs_index_ctx_put(ntfs_index_context *ictx)
58{
59 if (ictx->entry) {
60 if (ictx->is_in_root) {
61 if (ictx->actx)
62 ntfs_attr_put_search_ctx(ictx->actx);
63 if (ictx->base_ni)
64 unmap_mft_record(ictx->base_ni);
65 } else {
66 struct page *page = ictx->page;
67 if (page) {
68 BUG_ON(!PageLocked(page));
69 unlock_page(page);
70 ntfs_unmap_page(page);
71 }
72 }
73 }
74 kmem_cache_free(ntfs_index_ctx_cache, ictx);
75 return;
76}
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119int ntfs_index_lookup(const void *key, const int key_len,
120 ntfs_index_context *ictx)
121{
122 VCN vcn, old_vcn;
123 ntfs_inode *idx_ni = ictx->idx_ni;
124 ntfs_volume *vol = idx_ni->vol;
125 struct super_block *sb = vol->sb;
126 ntfs_inode *base_ni = idx_ni->ext.base_ntfs_ino;
127 MFT_RECORD *m;
128 INDEX_ROOT *ir;
129 INDEX_ENTRY *ie;
130 INDEX_ALLOCATION *ia;
131 u8 *index_end, *kaddr;
132 ntfs_attr_search_ctx *actx;
133 struct address_space *ia_mapping;
134 struct page *page;
135 int rc, err = 0;
136
137 ntfs_debug("Entering.");
138 BUG_ON(!NInoAttr(idx_ni));
139 BUG_ON(idx_ni->type != AT_INDEX_ALLOCATION);
140 BUG_ON(idx_ni->nr_extents != -1);
141 BUG_ON(!base_ni);
142 BUG_ON(!key);
143 BUG_ON(key_len <= 0);
144 if (!ntfs_is_collation_rule_supported(
145 idx_ni->itype.index.collation_rule)) {
146 ntfs_error(sb, "Index uses unsupported collation rule 0x%x. "
147 "Aborting lookup.", le32_to_cpu(
148 idx_ni->itype.index.collation_rule));
149 return -EOPNOTSUPP;
150 }
151
152 m = map_mft_record(base_ni);
153 if (IS_ERR(m)) {
154 ntfs_error(sb, "map_mft_record() failed with error code %ld.",
155 -PTR_ERR(m));
156 return PTR_ERR(m);
157 }
158 actx = ntfs_attr_get_search_ctx(base_ni, m);
159 if (unlikely(!actx)) {
160 err = -ENOMEM;
161 goto err_out;
162 }
163
164 err = ntfs_attr_lookup(AT_INDEX_ROOT, idx_ni->name, idx_ni->name_len,
165 CASE_SENSITIVE, 0, NULL, 0, actx);
166 if (unlikely(err)) {
167 if (err == -ENOENT) {
168 ntfs_error(sb, "Index root attribute missing in inode "
169 "0x%lx.", idx_ni->mft_no);
170 err = -EIO;
171 }
172 goto err_out;
173 }
174
175 ir = (INDEX_ROOT*)((u8*)actx->attr +
176 le16_to_cpu(actx->attr->data.resident.value_offset));
177 index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
178
179 ie = (INDEX_ENTRY*)((u8*)&ir->index +
180 le32_to_cpu(ir->index.entries_offset));
181
182
183
184
185 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
186
187 if ((u8*)ie < (u8*)actx->mrec || (u8*)ie +
188 sizeof(INDEX_ENTRY_HEADER) > index_end ||
189 (u8*)ie + le16_to_cpu(ie->length) > index_end)
190 goto idx_err_out;
191
192
193
194
195 if (ie->flags & INDEX_ENTRY_END)
196 break;
197
198 if ((u32)sizeof(INDEX_ENTRY_HEADER) +
199 le16_to_cpu(ie->key_length) >
200 le16_to_cpu(ie->data.vi.data_offset) ||
201 (u32)le16_to_cpu(ie->data.vi.data_offset) +
202 le16_to_cpu(ie->data.vi.data_length) >
203 le16_to_cpu(ie->length))
204 goto idx_err_out;
205
206 if ((key_len == le16_to_cpu(ie->key_length)) && !memcmp(key,
207 &ie->key, key_len)) {
208ir_done:
209 ictx->is_in_root = true;
210 ictx->ir = ir;
211 ictx->actx = actx;
212 ictx->base_ni = base_ni;
213 ictx->ia = NULL;
214 ictx->page = NULL;
215done:
216 ictx->entry = ie;
217 ictx->data = (u8*)ie +
218 le16_to_cpu(ie->data.vi.data_offset);
219 ictx->data_len = le16_to_cpu(ie->data.vi.data_length);
220 ntfs_debug("Done.");
221 return err;
222 }
223
224
225
226
227 rc = ntfs_collate(vol, idx_ni->itype.index.collation_rule, key,
228 key_len, &ie->key, le16_to_cpu(ie->key_length));
229
230
231
232
233
234 if (rc == -1)
235 break;
236
237
238
239
240 if (!rc)
241 goto ir_done;
242
243 }
244
245
246
247
248
249 if (!(ie->flags & INDEX_ENTRY_NODE)) {
250 ntfs_debug("Entry not found.");
251 err = -ENOENT;
252 goto ir_done;
253 }
254
255 if (!NInoIndexAllocPresent(idx_ni)) {
256 ntfs_error(sb, "No index allocation attribute but index entry "
257 "requires one. Inode 0x%lx is corrupt or "
258 "driver bug.", idx_ni->mft_no);
259 goto err_out;
260 }
261
262 vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8));
263 ia_mapping = VFS_I(idx_ni)->i_mapping;
264
265
266
267
268 ntfs_attr_put_search_ctx(actx);
269 unmap_mft_record(base_ni);
270 m = NULL;
271 actx = NULL;
272descend_into_child_node:
273
274
275
276
277
278 page = ntfs_map_page(ia_mapping, vcn <<
279 idx_ni->itype.index.vcn_size_bits >> PAGE_SHIFT);
280 if (IS_ERR(page)) {
281 ntfs_error(sb, "Failed to map index page, error %ld.",
282 -PTR_ERR(page));
283 err = PTR_ERR(page);
284 goto err_out;
285 }
286 lock_page(page);
287 kaddr = (u8*)page_address(page);
288fast_descend_into_child_node:
289
290 ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
291 idx_ni->itype.index.vcn_size_bits) & ~PAGE_MASK));
292
293 if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE) {
294 ntfs_error(sb, "Out of bounds check failed. Corrupt inode "
295 "0x%lx or driver bug.", idx_ni->mft_no);
296 goto unm_err_out;
297 }
298
299 if (unlikely(!ntfs_is_indx_record(ia->magic))) {
300 ntfs_error(sb, "Index record with vcn 0x%llx is corrupt. "
301 "Corrupt inode 0x%lx. Run chkdsk.",
302 (long long)vcn, idx_ni->mft_no);
303 goto unm_err_out;
304 }
305 if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
306 ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
307 "different from expected VCN (0x%llx). Inode "
308 "0x%lx is corrupt or driver bug.",
309 (unsigned long long)
310 sle64_to_cpu(ia->index_block_vcn),
311 (unsigned long long)vcn, idx_ni->mft_no);
312 goto unm_err_out;
313 }
314 if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
315 idx_ni->itype.index.block_size) {
316 ntfs_error(sb, "Index buffer (VCN 0x%llx) of inode 0x%lx has "
317 "a size (%u) differing from the index "
318 "specified size (%u). Inode is corrupt or "
319 "driver bug.", (unsigned long long)vcn,
320 idx_ni->mft_no,
321 le32_to_cpu(ia->index.allocated_size) + 0x18,
322 idx_ni->itype.index.block_size);
323 goto unm_err_out;
324 }
325 index_end = (u8*)ia + idx_ni->itype.index.block_size;
326 if (index_end > kaddr + PAGE_SIZE) {
327 ntfs_error(sb, "Index buffer (VCN 0x%llx) of inode 0x%lx "
328 "crosses page boundary. Impossible! Cannot "
329 "access! This is probably a bug in the "
330 "driver.", (unsigned long long)vcn,
331 idx_ni->mft_no);
332 goto unm_err_out;
333 }
334 index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
335 if (index_end > (u8*)ia + idx_ni->itype.index.block_size) {
336 ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of inode "
337 "0x%lx exceeds maximum size.",
338 (unsigned long long)vcn, idx_ni->mft_no);
339 goto unm_err_out;
340 }
341
342 ie = (INDEX_ENTRY*)((u8*)&ia->index +
343 le32_to_cpu(ia->index.entries_offset));
344
345
346
347
348
349 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
350
351 if ((u8*)ie < (u8*)ia || (u8*)ie +
352 sizeof(INDEX_ENTRY_HEADER) > index_end ||
353 (u8*)ie + le16_to_cpu(ie->length) > index_end) {
354 ntfs_error(sb, "Index entry out of bounds in inode "
355 "0x%lx.", idx_ni->mft_no);
356 goto unm_err_out;
357 }
358
359
360
361
362 if (ie->flags & INDEX_ENTRY_END)
363 break;
364
365 if ((u32)sizeof(INDEX_ENTRY_HEADER) +
366 le16_to_cpu(ie->key_length) >
367 le16_to_cpu(ie->data.vi.data_offset) ||
368 (u32)le16_to_cpu(ie->data.vi.data_offset) +
369 le16_to_cpu(ie->data.vi.data_length) >
370 le16_to_cpu(ie->length)) {
371 ntfs_error(sb, "Index entry out of bounds in inode "
372 "0x%lx.", idx_ni->mft_no);
373 goto unm_err_out;
374 }
375
376 if ((key_len == le16_to_cpu(ie->key_length)) && !memcmp(key,
377 &ie->key, key_len)) {
378ia_done:
379 ictx->is_in_root = false;
380 ictx->actx = NULL;
381 ictx->base_ni = NULL;
382 ictx->ia = ia;
383 ictx->page = page;
384 goto done;
385 }
386
387
388
389
390 rc = ntfs_collate(vol, idx_ni->itype.index.collation_rule, key,
391 key_len, &ie->key, le16_to_cpu(ie->key_length));
392
393
394
395
396
397 if (rc == -1)
398 break;
399
400
401
402
403 if (!rc)
404 goto ia_done;
405
406 }
407
408
409
410
411 if (!(ie->flags & INDEX_ENTRY_NODE)) {
412 ntfs_debug("Entry not found.");
413 err = -ENOENT;
414 goto ia_done;
415 }
416 if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
417 ntfs_error(sb, "Index entry with child node found in a leaf "
418 "node in inode 0x%lx.", idx_ni->mft_no);
419 goto unm_err_out;
420 }
421
422 old_vcn = vcn;
423 vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8));
424 if (vcn >= 0) {
425
426
427
428
429 if (old_vcn << vol->cluster_size_bits >>
430 PAGE_SHIFT == vcn <<
431 vol->cluster_size_bits >>
432 PAGE_SHIFT)
433 goto fast_descend_into_child_node;
434 unlock_page(page);
435 ntfs_unmap_page(page);
436 goto descend_into_child_node;
437 }
438 ntfs_error(sb, "Negative child node vcn in inode 0x%lx.",
439 idx_ni->mft_no);
440unm_err_out:
441 unlock_page(page);
442 ntfs_unmap_page(page);
443err_out:
444 if (!err)
445 err = -EIO;
446 if (actx)
447 ntfs_attr_put_search_ctx(actx);
448 if (m)
449 unmap_mft_record(base_ni);
450 return err;
451idx_err_out:
452 ntfs_error(sb, "Corrupt index. Aborting lookup.");
453 goto err_out;
454}
455