2 * index.c - NTFS kernel index handling. Part of the Linux-NTFS project.
4 * Copyright (c) 2004 Anton Altaparmakov
6 * This program/include file is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program/include file is distributed in the hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program (in the main directory of the Linux-NTFS
18 * distribution in the file COPYING); if not, write to the Free Software
19 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * ntfs_index_ctx_get - allocate and initialize a new index context
30 * @idx_ni: ntfs index inode with which to initialize the context
32 * Allocate a new index context, initialize it with @idx_ni and return it.
33 * Return NULL if allocation failed.
35 * Locking: Caller must hold i_sem on the index inode.
37 ntfs_index_context
*ntfs_index_ctx_get(ntfs_inode
*idx_ni
)
39 ntfs_index_context
*ictx
;
41 ictx
= kmem_cache_alloc(ntfs_index_ctx_cache
, SLAB_NOFS
);
43 ictx
->idx_ni
= idx_ni
;
58 * ntfs_index_ctx_put - release an index context
59 * @ictx: index context to free
61 * Release the index context @ictx, releasing all associated resources.
63 * Locking: Caller must hold i_sem on the index inode.
65 void ntfs_index_ctx_put(ntfs_index_context
*ictx
)
68 if (ictx
->is_in_root
) {
70 ntfs_attr_put_search_ctx(ictx
->actx
);
72 unmap_mft_record(ictx
->base_ni
);
74 struct page
*page
= ictx
->page
;
76 BUG_ON(!PageLocked(page
));
78 ntfs_unmap_page(page
);
82 kmem_cache_free(ntfs_index_ctx_cache
, ictx
);
87 * ntfs_index_lookup - find a key in an index and return its index entry
88 * @key: [IN] key for which to search in the index
89 * @key_len: [IN] length of @key in bytes
90 * @ictx: [IN/OUT] context describing the index and the returned entry
92 * Before calling ntfs_index_lookup(), @ictx must have been obtained from a
93 * call to ntfs_index_ctx_get().
95 * Look for the @key in the index specified by the index lookup context @ictx.
96 * ntfs_index_lookup() walks the contents of the index looking for the @key.
98 * If the @key is found in the index, 0 is returned and @ictx is setup to
99 * describe the index entry containing the matching @key. @ictx->entry is the
100 * index entry and @ictx->data and @ictx->data_len are the index entry data and
101 * its length in bytes, respectively.
103 * If the @key is not found in the index, -ENOENT is returned and @ictx is
104 * setup to describe the index entry whose key collates immediately after the
105 * search @key, i.e. this is the position in the index at which an index entry
106 * with a key of @key would need to be inserted.
108 * If an error occurs return the negative error code and @ictx is left
111 * When finished with the entry and its data, call ntfs_index_ctx_put() to free
112 * the context and other associated resources.
114 * If the index entry was modified, call flush_dcache_index_entry_page()
115 * immediately after the modification and either ntfs_index_entry_mark_dirty()
116 * or ntfs_index_entry_write() before the call to ntfs_index_ctx_put() to
117 * ensure that the changes are written to disk.
119 * Locking: - Caller must hold i_sem on the index inode.
120 * - Each page cache page in the index allocation mapping must be
121 * locked whilst being accessed otherwise we may find a corrupt
122 * page due to it being under ->writepage at the moment which
123 * applies the mst protection fixups before writing out and then
124 * removes them again after the write is complete after which it
127 int ntfs_index_lookup(const void *key
, const int key_len
,
128 ntfs_index_context
*ictx
)
131 ntfs_inode
*idx_ni
= ictx
->idx_ni
;
132 ntfs_volume
*vol
= idx_ni
->vol
;
133 struct super_block
*sb
= vol
->sb
;
134 ntfs_inode
*base_ni
= idx_ni
->ext
.base_ntfs_ino
;
138 INDEX_ALLOCATION
*ia
;
139 u8
*index_end
, *kaddr
;
140 ntfs_attr_search_ctx
*actx
;
141 struct address_space
*ia_mapping
;
145 ntfs_debug("Entering.");
146 BUG_ON(!NInoAttr(idx_ni
));
147 BUG_ON(idx_ni
->type
!= AT_INDEX_ALLOCATION
);
148 BUG_ON(idx_ni
->nr_extents
!= -1);
151 BUG_ON(key_len
<= 0);
152 if (!ntfs_is_collation_rule_supported(
153 idx_ni
->itype
.index
.collation_rule
)) {
154 ntfs_error(sb
, "Index uses unsupported collation rule 0x%x. "
155 "Aborting lookup.", le32_to_cpu(
156 idx_ni
->itype
.index
.collation_rule
));
159 /* Get hold of the mft record for the index inode. */
160 m
= map_mft_record(base_ni
);
162 ntfs_error(sb
, "map_mft_record() failed with error code %ld.",
166 actx
= ntfs_attr_get_search_ctx(base_ni
, m
);
167 if (unlikely(!actx
)) {
171 /* Find the index root attribute in the mft record. */
172 err
= ntfs_attr_lookup(AT_INDEX_ROOT
, idx_ni
->name
, idx_ni
->name_len
,
173 CASE_SENSITIVE
, 0, NULL
, 0, actx
);
175 if (err
== -ENOENT
) {
176 ntfs_error(sb
, "Index root attribute missing in inode "
177 "0x%lx.", idx_ni
->mft_no
);
182 /* Get to the index root value (it has been verified in read_inode). */
183 ir
= (INDEX_ROOT
*)((u8
*)actx
->attr
+
184 le16_to_cpu(actx
->attr
->data
.resident
.value_offset
));
185 index_end
= (u8
*)&ir
->index
+ le32_to_cpu(ir
->index
.index_length
);
186 /* The first index entry. */
187 ie
= (INDEX_ENTRY
*)((u8
*)&ir
->index
+
188 le32_to_cpu(ir
->index
.entries_offset
));
190 * Loop until we exceed valid memory (corruption case) or until we
191 * reach the last entry.
193 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
195 if ((u8
*)ie
< (u8
*)actx
->mrec
|| (u8
*)ie
+
196 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
197 (u8
*)ie
+ le16_to_cpu(ie
->length
) > index_end
)
200 * The last entry cannot contain a key. It can however contain
201 * a pointer to a child node in the B+tree so we just break out.
203 if (ie
->flags
& INDEX_ENTRY_END
)
205 /* Further bounds checks. */
206 if ((u32
)sizeof(INDEX_ENTRY_HEADER
) +
207 le16_to_cpu(ie
->key_length
) >
208 le16_to_cpu(ie
->data
.vi
.data_offset
) ||
209 (u32
)le16_to_cpu(ie
->data
.vi
.data_offset
) +
210 le16_to_cpu(ie
->data
.vi
.data_length
) >
211 le16_to_cpu(ie
->length
))
213 /* If the keys match perfectly, we setup @ictx and return 0. */
214 if ((key_len
== le16_to_cpu(ie
->key_length
)) && !memcmp(key
,
215 &ie
->key
, key_len
)) {
217 ictx
->is_in_root
= TRUE
;
219 ictx
->base_ni
= base_ni
;
224 ictx
->data
= (u8
*)ie
+
225 le16_to_cpu(ie
->data
.vi
.data_offset
);
226 ictx
->data_len
= le16_to_cpu(ie
->data
.vi
.data_length
);
231 * Not a perfect match, need to do full blown collation so we
232 * know which way in the B+tree we have to go.
234 rc
= ntfs_collate(vol
, idx_ni
->itype
.index
.collation_rule
, key
,
235 key_len
, &ie
->key
, le16_to_cpu(ie
->key_length
));
237 * If @key collates before the key of the current entry, there
238 * is definitely no such key in this index but we might need to
239 * descend into the B+tree so we just break out of the loop.
244 * A match should never happen as the memcmp() call should have
245 * cought it, but we still treat it correctly.
249 /* The keys are not equal, continue the search. */
252 * We have finished with this index without success. Check for the
253 * presence of a child node and if not present setup @ictx and return
256 if (!(ie
->flags
& INDEX_ENTRY_NODE
)) {
257 ntfs_debug("Entry not found.");
260 } /* Child node present, descend into it. */
261 /* Consistency check: Verify that an index allocation exists. */
262 if (!NInoIndexAllocPresent(idx_ni
)) {
263 ntfs_error(sb
, "No index allocation attribute but index entry "
264 "requires one. Inode 0x%lx is corrupt or "
265 "driver bug.", idx_ni
->mft_no
);
268 /* Get the starting vcn of the index_block holding the child node. */
269 vcn
= sle64_to_cpup((sle64
*)((u8
*)ie
+ le16_to_cpu(ie
->length
) - 8));
270 ia_mapping
= VFS_I(idx_ni
)->i_mapping
;
272 * We are done with the index root and the mft record. Release them,
273 * otherwise we deadlock with ntfs_map_page().
275 ntfs_attr_put_search_ctx(actx
);
276 unmap_mft_record(base_ni
);
279 descend_into_child_node
:
281 * Convert vcn to index into the index allocation attribute in units
282 * of PAGE_CACHE_SIZE and map the page cache page, reading it from
285 page
= ntfs_map_page(ia_mapping
, vcn
<<
286 idx_ni
->itype
.index
.vcn_size_bits
>> PAGE_CACHE_SHIFT
);
288 ntfs_error(sb
, "Failed to map index page, error %ld.",
294 kaddr
= (u8
*)page_address(page
);
295 fast_descend_into_child_node
:
296 /* Get to the index allocation block. */
297 ia
= (INDEX_ALLOCATION
*)(kaddr
+ ((vcn
<<
298 idx_ni
->itype
.index
.vcn_size_bits
) & ~PAGE_CACHE_MASK
));
300 if ((u8
*)ia
< kaddr
|| (u8
*)ia
> kaddr
+ PAGE_CACHE_SIZE
) {
301 ntfs_error(sb
, "Out of bounds check failed. Corrupt inode "
302 "0x%lx or driver bug.", idx_ni
->mft_no
);
305 /* Catch multi sector transfer fixup errors. */
306 if (unlikely(!ntfs_is_indx_record(ia
->magic
))) {
307 ntfs_error(sb
, "Index record with vcn 0x%llx is corrupt. "
308 "Corrupt inode 0x%lx. Run chkdsk.",
309 (long long)vcn
, idx_ni
->mft_no
);
312 if (sle64_to_cpu(ia
->index_block_vcn
) != vcn
) {
313 ntfs_error(sb
, "Actual VCN (0x%llx) of index buffer is "
314 "different from expected VCN (0x%llx). Inode "
315 "0x%lx is corrupt or driver bug.",
317 sle64_to_cpu(ia
->index_block_vcn
),
318 (unsigned long long)vcn
, idx_ni
->mft_no
);
321 if (le32_to_cpu(ia
->index
.allocated_size
) + 0x18 !=
322 idx_ni
->itype
.index
.block_size
) {
323 ntfs_error(sb
, "Index buffer (VCN 0x%llx) of inode 0x%lx has "
324 "a size (%u) differing from the index "
325 "specified size (%u). Inode is corrupt or "
326 "driver bug.", (unsigned long long)vcn
,
328 le32_to_cpu(ia
->index
.allocated_size
) + 0x18,
329 idx_ni
->itype
.index
.block_size
);
332 index_end
= (u8
*)ia
+ idx_ni
->itype
.index
.block_size
;
333 if (index_end
> kaddr
+ PAGE_CACHE_SIZE
) {
334 ntfs_error(sb
, "Index buffer (VCN 0x%llx) of inode 0x%lx "
335 "crosses page boundary. Impossible! Cannot "
336 "access! This is probably a bug in the "
337 "driver.", (unsigned long long)vcn
,
341 index_end
= (u8
*)&ia
->index
+ le32_to_cpu(ia
->index
.index_length
);
342 if (index_end
> (u8
*)ia
+ idx_ni
->itype
.index
.block_size
) {
343 ntfs_error(sb
, "Size of index buffer (VCN 0x%llx) of inode "
344 "0x%lx exceeds maximum size.",
345 (unsigned long long)vcn
, idx_ni
->mft_no
);
348 /* The first index entry. */
349 ie
= (INDEX_ENTRY
*)((u8
*)&ia
->index
+
350 le32_to_cpu(ia
->index
.entries_offset
));
352 * Iterate similar to above big loop but applied to index buffer, thus
353 * loop until we exceed valid memory (corruption case) or until we
354 * reach the last entry.
356 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
358 if ((u8
*)ie
< (u8
*)ia
|| (u8
*)ie
+
359 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
360 (u8
*)ie
+ le16_to_cpu(ie
->length
) > index_end
) {
361 ntfs_error(sb
, "Index entry out of bounds in inode "
362 "0x%lx.", idx_ni
->mft_no
);
366 * The last entry cannot contain a key. It can however contain
367 * a pointer to a child node in the B+tree so we just break out.
369 if (ie
->flags
& INDEX_ENTRY_END
)
371 /* Further bounds checks. */
372 if ((u32
)sizeof(INDEX_ENTRY_HEADER
) +
373 le16_to_cpu(ie
->key_length
) >
374 le16_to_cpu(ie
->data
.vi
.data_offset
) ||
375 (u32
)le16_to_cpu(ie
->data
.vi
.data_offset
) +
376 le16_to_cpu(ie
->data
.vi
.data_length
) >
377 le16_to_cpu(ie
->length
)) {
378 ntfs_error(sb
, "Index entry out of bounds in inode "
379 "0x%lx.", idx_ni
->mft_no
);
382 /* If the keys match perfectly, we setup @ictx and return 0. */
383 if ((key_len
== le16_to_cpu(ie
->key_length
)) && !memcmp(key
,
384 &ie
->key
, key_len
)) {
386 ictx
->is_in_root
= FALSE
;
388 ictx
->base_ni
= NULL
;
394 * Not a perfect match, need to do full blown collation so we
395 * know which way in the B+tree we have to go.
397 rc
= ntfs_collate(vol
, idx_ni
->itype
.index
.collation_rule
, key
,
398 key_len
, &ie
->key
, le16_to_cpu(ie
->key_length
));
400 * If @key collates before the key of the current entry, there
401 * is definitely no such key in this index but we might need to
402 * descend into the B+tree so we just break out of the loop.
407 * A match should never happen as the memcmp() call should have
408 * cought it, but we still treat it correctly.
412 /* The keys are not equal, continue the search. */
415 * We have finished with this index buffer without success. Check for
416 * the presence of a child node and if not present return -ENOENT.
418 if (!(ie
->flags
& INDEX_ENTRY_NODE
)) {
419 ntfs_debug("Entry not found.");
423 if ((ia
->index
.flags
& NODE_MASK
) == LEAF_NODE
) {
424 ntfs_error(sb
, "Index entry with child node found in a leaf "
425 "node in inode 0x%lx.", idx_ni
->mft_no
);
428 /* Child node present, descend into it. */
430 vcn
= sle64_to_cpup((sle64
*)((u8
*)ie
+ le16_to_cpu(ie
->length
) - 8));
433 * If vcn is in the same page cache page as old_vcn we recycle
436 if (old_vcn
<< vol
->cluster_size_bits
>>
437 PAGE_CACHE_SHIFT
== vcn
<<
438 vol
->cluster_size_bits
>>
440 goto fast_descend_into_child_node
;
442 ntfs_unmap_page(page
);
443 goto descend_into_child_node
;
445 ntfs_error(sb
, "Negative child node vcn in inode 0x%lx.",
449 ntfs_unmap_page(page
);
454 ntfs_attr_put_search_ctx(actx
);
456 unmap_mft_record(base_ni
);
459 ntfs_error(sb
, "Corrupt index. Aborting lookup.");