2 * index.c - NTFS index handling. Originated from the Linux-NTFS project.
4 * Copyright (c) 2004-2005 Anton Altaparmakov
5 * Copyright (c) 2004-2005 Richard Russon
6 * Copyright (c) 2005-2006 Yura Pakhuchiy
7 * Copyright (c) 2005-2008 Szabolcs Szakacsits
8 * Copyright (c) 2007 Jean-Pierre Andre
10 * This program/include file is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program/include file is distributed in the hope that it will be
16 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
17 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program (in the main directory of the NTFS-3G
22 * distribution in the file COPYING); if not, write to the Free Software
23 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
52 * ntfs_index_entry_mark_dirty - mark an index entry dirty
53 * @ictx: ntfs index context describing the index entry
55 * Mark the index entry described by the index entry context @ictx dirty.
57 * If the index entry is in the index root attribute, simply mark the inode
58 * containing the index root attribute dirty. This ensures the mftrecord, and
59 * hence the index root attribute, will be written out to disk later.
61 * If the index entry is in an index block belonging to the index allocation
62 * attribute, set ib_dirty to TRUE, thus index block will be updated during
65 void ntfs_index_entry_mark_dirty(ntfs_index_context
*ictx
)
68 ntfs_inode_mark_dirty(ictx
->actx
->ntfs_ino
);
70 ictx
->ib_dirty
= TRUE
;
73 static s64
ntfs_ib_vcn_to_pos(ntfs_index_context
*icx
, VCN vcn
)
75 return vcn
<< icx
->vcn_size_bits
;
78 static VCN
ntfs_ib_pos_to_vcn(ntfs_index_context
*icx
, s64 pos
)
80 return pos
>> icx
->vcn_size_bits
;
83 static int ntfs_ib_write(ntfs_index_context
*icx
, INDEX_BLOCK
*ib
)
85 s64 ret
, vcn
= sle64_to_cpu(ib
->index_block_vcn
);
87 ntfs_log_trace("vcn: %lld\n", (long long)vcn
);
89 ret
= ntfs_attr_mst_pwrite(icx
->ia_na
, ntfs_ib_vcn_to_pos(icx
, vcn
),
90 1, icx
->block_size
, ib
);
92 ntfs_log_perror("Failed to write index block %lld, inode %llu",
93 (long long)vcn
, (unsigned long long)icx
->ni
->mft_no
);
100 static int ntfs_icx_ib_write(ntfs_index_context
*icx
)
102 if (ntfs_ib_write(icx
, icx
->ib
))
105 icx
->ib_dirty
= FALSE
;
111 * ntfs_index_ctx_get - allocate and initialize a new index context
112 * @ni: ntfs inode with which to initialize the context
113 * @name: name of the which context describes
114 * @name_len: length of the index name
116 * Allocate a new index context, initialize it with @ni and return it.
117 * Return NULL if allocation failed.
119 ntfs_index_context
*ntfs_index_ctx_get(ntfs_inode
*ni
,
120 ntfschar
*name
, u32 name_len
)
122 ntfs_index_context
*icx
;
124 ntfs_log_trace("Entering\n");
130 if (ni
->nr_extents
== -1)
132 icx
= ntfs_calloc(sizeof(ntfs_index_context
));
134 *icx
= (ntfs_index_context
) {
137 .name_len
= name_len
,
142 static void ntfs_index_ctx_free(ntfs_index_context
*icx
)
144 ntfs_log_trace("Entering\n");
150 ntfs_attr_put_search_ctx(icx
->actx
);
152 if (!icx
->is_in_root
) {
154 /* FIXME: Error handling!!! */
155 ntfs_ib_write(icx
, icx
->ib
);
160 ntfs_attr_close(icx
->ia_na
);
164 * ntfs_index_ctx_put - release an index context
165 * @icx: index context to free
167 * Release the index context @icx, releasing all associated resources.
169 void ntfs_index_ctx_put(ntfs_index_context
*icx
)
171 ntfs_index_ctx_free(icx
);
176 * ntfs_index_ctx_reinit - reinitialize an index context
177 * @icx: index context to reinitialize
179 * Reinitialize the index context @icx so it can be used for ntfs_index_lookup.
181 void ntfs_index_ctx_reinit(ntfs_index_context
*icx
)
183 ntfs_log_trace("Entering\n");
185 ntfs_index_ctx_free(icx
);
187 *icx
= (ntfs_index_context
) {
190 .name_len
= icx
->name_len
,
194 static VCN
*ntfs_ie_get_vcn_addr(INDEX_ENTRY
*ie
)
196 return (VCN
*)((u8
*)ie
+ le16_to_cpu(ie
->length
) - sizeof(VCN
));
200 * Get the subnode vcn to which the index entry refers.
202 VCN
ntfs_ie_get_vcn(INDEX_ENTRY
*ie
)
204 return sle64_to_cpup(ntfs_ie_get_vcn_addr(ie
));
207 static INDEX_ENTRY
*ntfs_ie_get_first(INDEX_HEADER
*ih
)
209 return (INDEX_ENTRY
*)((u8
*)ih
+ le32_to_cpu(ih
->entries_offset
));
212 static INDEX_ENTRY
*ntfs_ie_get_next(INDEX_ENTRY
*ie
)
214 return (INDEX_ENTRY
*)((char *)ie
+ le16_to_cpu(ie
->length
));
217 static u8
*ntfs_ie_get_end(INDEX_HEADER
*ih
)
219 /* FIXME: check if it isn't overflowing the index block size */
220 return (u8
*)ih
+ le32_to_cpu(ih
->index_length
);
223 static int ntfs_ie_end(INDEX_ENTRY
*ie
)
225 return ie
->ie_flags
& INDEX_ENTRY_END
|| !ie
->length
;
229 * Find the last entry in the index block
231 static INDEX_ENTRY
*ntfs_ie_get_last(INDEX_ENTRY
*ie
, char *ies_end
)
233 ntfs_log_trace("Entering\n");
235 while ((char *)ie
< ies_end
&& !ntfs_ie_end(ie
))
236 ie
= ntfs_ie_get_next(ie
);
241 static INDEX_ENTRY
*ntfs_ie_get_by_pos(INDEX_HEADER
*ih
, int pos
)
245 ntfs_log_trace("pos: %d\n", pos
);
247 ie
= ntfs_ie_get_first(ih
);
250 ie
= ntfs_ie_get_next(ie
);
255 static INDEX_ENTRY
*ntfs_ie_prev(INDEX_HEADER
*ih
, INDEX_ENTRY
*ie
)
257 INDEX_ENTRY
*ie_prev
= NULL
;
260 ntfs_log_trace("Entering\n");
262 tmp
= ntfs_ie_get_first(ih
);
266 tmp
= ntfs_ie_get_next(tmp
);
272 char *ntfs_ie_filename_get(INDEX_ENTRY
*ie
)
276 fn
= (FILE_NAME_ATTR
*)&ie
->key
;
277 return ntfs_attr_name_get(fn
->file_name
, fn
->file_name_length
);
280 void ntfs_ie_filename_dump(INDEX_ENTRY
*ie
)
284 s
= ntfs_ie_filename_get(ie
);
285 ntfs_log_debug("'%s' ", s
);
286 ntfs_attr_name_free(&s
);
289 void ntfs_ih_filename_dump(INDEX_HEADER
*ih
)
293 ntfs_log_trace("Entering\n");
295 ie
= ntfs_ie_get_first(ih
);
296 while (!ntfs_ie_end(ie
)) {
297 ntfs_ie_filename_dump(ie
);
298 ie
= ntfs_ie_get_next(ie
);
302 static int ntfs_ih_numof_entries(INDEX_HEADER
*ih
)
308 ntfs_log_trace("Entering\n");
310 end
= ntfs_ie_get_end(ih
);
311 ie
= ntfs_ie_get_first(ih
);
312 for (n
= 0; !ntfs_ie_end(ie
) && (u8
*)ie
< end
; n
++)
313 ie
= ntfs_ie_get_next(ie
);
317 static int ntfs_ih_one_entry(INDEX_HEADER
*ih
)
319 return (ntfs_ih_numof_entries(ih
) == 1);
322 static int ntfs_ih_zero_entry(INDEX_HEADER
*ih
)
324 return (ntfs_ih_numof_entries(ih
) == 0);
327 static void ntfs_ie_delete(INDEX_HEADER
*ih
, INDEX_ENTRY
*ie
)
331 ntfs_log_trace("Entering\n");
333 new_size
= le32_to_cpu(ih
->index_length
) - le16_to_cpu(ie
->length
);
334 ih
->index_length
= cpu_to_le32(new_size
);
335 memmove(ie
, (u8
*)ie
+ le16_to_cpu(ie
->length
),
336 new_size
- ((u8
*)ie
- (u8
*)ih
));
339 static void ntfs_ie_set_vcn(INDEX_ENTRY
*ie
, VCN vcn
)
341 *ntfs_ie_get_vcn_addr(ie
) = cpu_to_le64(vcn
);
345 * Insert @ie index entry at @pos entry. Used @ih values should be ok already.
347 static void ntfs_ie_insert(INDEX_HEADER
*ih
, INDEX_ENTRY
*ie
, INDEX_ENTRY
*pos
)
349 int ie_size
= le16_to_cpu(ie
->length
);
351 ntfs_log_trace("Entering\n");
353 ih
->index_length
= cpu_to_le32(le32_to_cpu(ih
->index_length
) + ie_size
);
354 memmove((u8
*)pos
+ ie_size
, pos
,
355 le32_to_cpu(ih
->index_length
) - ((u8
*)pos
- (u8
*)ih
) - ie_size
);
356 memcpy(pos
, ie
, ie_size
);
359 static INDEX_ENTRY
*ntfs_ie_dup(INDEX_ENTRY
*ie
)
363 ntfs_log_trace("Entering\n");
365 dup
= ntfs_malloc(le16_to_cpu(ie
->length
));
367 memcpy(dup
, ie
, le16_to_cpu(ie
->length
));
372 static INDEX_ENTRY
*ntfs_ie_dup_novcn(INDEX_ENTRY
*ie
)
375 int size
= le16_to_cpu(ie
->length
);
377 ntfs_log_trace("Entering\n");
379 if (ie
->ie_flags
& INDEX_ENTRY_NODE
)
382 dup
= ntfs_malloc(size
);
384 memcpy(dup
, ie
, size
);
385 dup
->ie_flags
&= ~INDEX_ENTRY_NODE
;
386 dup
->length
= cpu_to_le16(size
);
391 static int ntfs_ia_check(ntfs_index_context
*icx
, INDEX_BLOCK
*ib
, VCN vcn
)
393 u32 ib_size
= (unsigned)le32_to_cpu(ib
->index
.allocated_size
) + 0x18;
395 ntfs_log_trace("Entering\n");
397 if (!ntfs_is_indx_record(ib
->magic
)) {
399 ntfs_log_error("Corrupt index block signature: vcn %lld inode "
400 "%llu\n", (long long)vcn
,
401 (unsigned long long)icx
->ni
->mft_no
);
405 if (sle64_to_cpu(ib
->index_block_vcn
) != vcn
) {
407 ntfs_log_error("Corrupt index block: VCN (%lld) is different "
408 "from expected VCN (%lld) in inode %llu\n",
409 (long long)sle64_to_cpu(ib
->index_block_vcn
),
411 (unsigned long long)icx
->ni
->mft_no
);
415 if (ib_size
!= icx
->block_size
) {
417 ntfs_log_error("Corrupt index block : VCN (%lld) of inode %llu "
418 "has a size (%u) differing from the index "
419 "specified size (%u)\n", (long long)vcn
,
420 (unsigned long long)icx
->ni
->mft_no
, ib_size
,
427 static INDEX_ROOT
*ntfs_ir_lookup(ntfs_inode
*ni
, ntfschar
*name
,
428 u32 name_len
, ntfs_attr_search_ctx
**ctx
)
431 INDEX_ROOT
*ir
= NULL
;
433 ntfs_log_trace("Entering\n");
435 *ctx
= ntfs_attr_get_search_ctx(ni
, NULL
);
439 if (ntfs_attr_lookup(AT_INDEX_ROOT
, name
, name_len
, CASE_SENSITIVE
,
441 ntfs_log_perror("Failed to lookup $INDEX_ROOT");
446 if (a
->non_resident
) {
448 ntfs_log_perror("Non-resident $INDEX_ROOT detected");
452 ir
= (INDEX_ROOT
*)((char *)a
+ le16_to_cpu(a
->value_offset
));
455 ntfs_attr_put_search_ctx(*ctx
);
461 static INDEX_ROOT
*ntfs_ir_lookup2(ntfs_inode
*ni
, ntfschar
*name
, u32 len
)
463 ntfs_attr_search_ctx
*ctx
;
466 ir
= ntfs_ir_lookup(ni
, name
, len
, &ctx
);
468 ntfs_attr_put_search_ctx(ctx
);
473 * Find a key in the index block.
476 * STATUS_OK with errno set to ESUCCESS if we know for sure that the
477 * entry exists and @ie_out points to this entry.
478 * STATUS_NOT_FOUND with errno set to ENOENT if we know for sure the
479 * entry doesn't exist and @ie_out is the insertion point.
480 * STATUS_KEEP_SEARCHING if we can't answer the above question and
481 * @vcn will contain the node index block.
482 * STATUS_ERROR with errno set if on unexpected error during lookup.
484 static int ntfs_ie_lookup(const void *key
, const int key_len
,
485 ntfs_index_context
*icx
, INDEX_HEADER
*ih
,
486 VCN
*vcn
, INDEX_ENTRY
**ie_out
)
492 ntfs_log_trace("Entering\n");
494 index_end
= ntfs_ie_get_end(ih
);
497 * Loop until we exceed valid memory (corruption case) or until we
498 * reach the last entry.
500 for (ie
= ntfs_ie_get_first(ih
); ; ie
= ntfs_ie_get_next(ie
)) {
502 if ((u8
*)ie
+ sizeof(INDEX_ENTRY_HEADER
) > index_end
||
503 (u8
*)ie
+ le16_to_cpu(ie
->length
) > index_end
) {
505 ntfs_log_error("Index entry out of bounds in inode "
507 (unsigned long long)icx
->ni
->mft_no
);
511 * The last entry cannot contain a key. It can however contain
512 * a pointer to a child node in the B+tree so we just break out.
517 * Not a perfect match, need to do full blown collation so we
518 * know which way in the B+tree we have to go.
521 ntfs_log_error("Collation function not defined\n");
525 rc
= icx
->collate(icx
->ni
->vol
, key
, key_len
,
526 &ie
->key
, le16_to_cpu(ie
->key_length
));
527 if (rc
== NTFS_COLLATION_ERROR
) {
528 ntfs_log_error("Collation error. Perhaps a filename "
529 "contains invalid characters?\n");
534 * If @key collates before the key of the current entry, there
535 * is definitely no such key in this index but we might need to
536 * descend into the B+tree so we just break out of the loop.
544 icx
->parent_pos
[icx
->pindex
] = item
;
551 * We have finished with this index block without success. Check for the
552 * presence of a child node and if not present return with errno ENOENT,
553 * otherwise we will keep searching in another index block.
555 if (!(ie
->ie_flags
& INDEX_ENTRY_NODE
)) {
556 ntfs_log_debug("Index entry wasn't found.\n");
559 return STATUS_NOT_FOUND
;
562 /* Get the starting vcn of the index_block holding the child node. */
563 *vcn
= ntfs_ie_get_vcn(ie
);
566 ntfs_log_perror("Negative vcn in inode %llu",
567 (unsigned long long)icx
->ni
->mft_no
);
571 ntfs_log_trace("Parent entry number %d\n", item
);
572 icx
->parent_pos
[icx
->pindex
] = item
;
574 return STATUS_KEEP_SEARCHING
;
577 static ntfs_attr
*ntfs_ia_open(ntfs_index_context
*icx
, ntfs_inode
*ni
)
581 na
= ntfs_attr_open(ni
, AT_INDEX_ALLOCATION
, icx
->name
, icx
->name_len
);
583 ntfs_log_perror("Failed to open index allocation of inode "
584 "%llu", (unsigned long long)ni
->mft_no
);
591 static int ntfs_ib_read(ntfs_index_context
*icx
, VCN vcn
, INDEX_BLOCK
*dst
)
595 ntfs_log_trace("vcn: %lld\n", (long long)vcn
);
597 pos
= ntfs_ib_vcn_to_pos(icx
, vcn
);
599 ret
= ntfs_attr_mst_pread(icx
->ia_na
, pos
, 1, icx
->block_size
, (u8
*)dst
);
602 ntfs_log_perror("Failed to read index block");
604 ntfs_log_error("Failed to read full index block at "
605 "%lld\n", (long long)pos
);
609 if (ntfs_ia_check(icx
, dst
, vcn
))
615 static int ntfs_icx_parent_inc(ntfs_index_context
*icx
)
618 if (icx
->pindex
>= MAX_PARENT_VCN
) {
620 ntfs_log_perror("Index is over %d level deep", MAX_PARENT_VCN
);
626 static int ntfs_icx_parent_dec(ntfs_index_context
*icx
)
629 if (icx
->pindex
< 0) {
631 ntfs_log_perror("Corrupt index pointer (%d)", icx
->pindex
);
638 * ntfs_index_lookup - find a key in an index and return its index entry
639 * @key: [IN] key for which to search in the index
640 * @key_len: [IN] length of @key in bytes
641 * @icx: [IN/OUT] context describing the index and the returned entry
643 * Before calling ntfs_index_lookup(), @icx must have been obtained from a
644 * call to ntfs_index_ctx_get().
646 * Look for the @key in the index specified by the index lookup context @icx.
647 * ntfs_index_lookup() walks the contents of the index looking for the @key.
649 * If the @key is found in the index, 0 is returned and @icx is setup to
650 * describe the index entry containing the matching @key. @icx->entry is the
651 * index entry and @icx->data and @icx->data_len are the index entry data and
652 * its length in bytes, respectively.
654 * If the @key is not found in the index, -1 is returned, errno = ENOENT and
655 * @icx is setup to describe the index entry whose key collates immediately
656 * after the search @key, i.e. this is the position in the index at which
657 * an index entry with a key of @key would need to be inserted.
659 * If an error occurs return -1, set errno to error code and @icx is left
662 * When finished with the entry and its data, call ntfs_index_ctx_put() to free
663 * the context and other associated resources.
665 * If the index entry was modified, call ntfs_index_entry_mark_dirty() before
666 * the call to ntfs_index_ctx_put() to ensure that the changes are written
669 int ntfs_index_lookup(const void *key
, const int key_len
, ntfs_index_context
*icx
)
672 ntfs_inode
*ni
= icx
->ni
;
675 INDEX_BLOCK
*ib
= NULL
;
678 ntfs_log_trace("Entering\n");
680 if (!key
|| key_len
<= 0) {
682 ntfs_log_perror("key: %p key_len: %d", key
, key_len
);
686 ir
= ntfs_ir_lookup(ni
, icx
->name
, icx
->name_len
, &icx
->actx
);
693 icx
->block_size
= le32_to_cpu(ir
->index_block_size
);
694 if (icx
->block_size
< NTFS_BLOCK_SIZE
) {
696 ntfs_log_perror("Index block size (%d) is smaller than the "
697 "sector size (%d)", icx
->block_size
, NTFS_BLOCK_SIZE
);
701 if (ni
->vol
->cluster_size
<= icx
->block_size
)
702 icx
->vcn_size_bits
= ni
->vol
->cluster_size_bits
;
704 icx
->vcn_size_bits
= NTFS_BLOCK_SIZE_BITS
;
705 /* get the appropriate collation function */
706 icx
->collate
= ntfs_get_collate_function(ir
->collation_rule
);
708 err
= errno
= EOPNOTSUPP
;
709 ntfs_log_perror("Unknown collation rule 0x%x",
710 (unsigned)le32_to_cpu(ir
->collation_rule
));
714 old_vcn
= VCN_INDEX_ROOT_PARENT
;
716 * FIXME: check for both ir and ib that the first index entry is
717 * within the index block.
719 ret
= ntfs_ie_lookup(key
, key_len
, icx
, &ir
->index
, &vcn
, &ie
);
720 if (ret
== STATUS_ERROR
) {
727 if (ret
!= STATUS_KEEP_SEARCHING
) {
728 /* STATUS_OK or STATUS_NOT_FOUND */
730 icx
->is_in_root
= TRUE
;
731 icx
->parent_vcn
[icx
->pindex
] = old_vcn
;
735 /* Child node present, descend into it. */
737 icx
->ia_na
= ntfs_ia_open(icx
, ni
);
741 ib
= ntfs_malloc(icx
->block_size
);
747 descend_into_child_node
:
749 icx
->parent_vcn
[icx
->pindex
] = old_vcn
;
750 if (ntfs_icx_parent_inc(icx
)) {
756 ntfs_log_debug("Descend into node with VCN %lld\n", (long long)vcn
);
758 if (ntfs_ib_read(icx
, vcn
, ib
))
761 ret
= ntfs_ie_lookup(key
, key_len
, icx
, &ib
->index
, &vcn
, &ie
);
762 if (ret
!= STATUS_KEEP_SEARCHING
) {
764 if (ret
== STATUS_ERROR
)
767 /* STATUS_OK or STATUS_NOT_FOUND */
768 icx
->is_in_root
= FALSE
;
770 icx
->parent_vcn
[icx
->pindex
] = vcn
;
774 if ((ib
->index
.ih_flags
& NODE_MASK
) == LEAF_NODE
) {
775 ntfs_log_error("Index entry with child node found in a leaf "
776 "node in inode 0x%llx.\n",
777 (unsigned long long)ni
->mft_no
);
781 goto descend_into_child_node
;
790 icx
->data
= (u8
*)ie
+ offsetof(INDEX_ENTRY
, key
);
791 icx
->data_len
= le16_to_cpu(ie
->key_length
);
792 ntfs_log_trace("Done.\n");
801 static INDEX_BLOCK
*ntfs_ib_alloc(VCN ib_vcn
, u32 ib_size
,
802 INDEX_HEADER_FLAGS node_type
)
805 int ih_size
= sizeof(INDEX_HEADER
);
807 ntfs_log_trace("ib_vcn: %lld ib_size: %u\n", (long long)ib_vcn
, ib_size
);
809 ib
= ntfs_calloc(ib_size
);
813 ib
->magic
= magic_INDX
;
814 ib
->usa_ofs
= cpu_to_le16(sizeof(INDEX_BLOCK
));
815 ib
->usa_count
= cpu_to_le16(ib_size
/ NTFS_BLOCK_SIZE
+ 1);
817 *(u16
*)((char *)ib
+ le16_to_cpu(ib
->usa_ofs
)) = cpu_to_le16(1);
818 ib
->lsn
= cpu_to_le64(0);
820 ib
->index_block_vcn
= cpu_to_sle64(ib_vcn
);
822 ib
->index
.entries_offset
= cpu_to_le32((ih_size
+
823 le16_to_cpu(ib
->usa_count
) * 2 + 7) & ~7);
824 ib
->index
.index_length
= 0;
825 ib
->index
.allocated_size
= cpu_to_le32(ib_size
-
826 (sizeof(INDEX_BLOCK
) - ih_size
));
827 ib
->index
.ih_flags
= node_type
;
833 * Find the median by going through all the entries
835 static INDEX_ENTRY
*ntfs_ie_get_median(INDEX_HEADER
*ih
)
837 INDEX_ENTRY
*ie
, *ie_start
;
841 ntfs_log_trace("Entering\n");
843 ie
= ie_start
= ntfs_ie_get_first(ih
);
844 ie_end
= (u8
*)ntfs_ie_get_end(ih
);
846 while ((u8
*)ie
< ie_end
&& !ntfs_ie_end(ie
)) {
847 ie
= ntfs_ie_get_next(ie
);
851 * NOTE: this could be also the entry at the half of the index block.
855 ntfs_log_trace("Entries: %d median: %d\n", i
, median
);
857 for (i
= 0, ie
= ie_start
; i
<= median
; i
++)
858 ie
= ntfs_ie_get_next(ie
);
863 static s64
ntfs_ibm_vcn_to_pos(ntfs_index_context
*icx
, VCN vcn
)
865 return ntfs_ib_vcn_to_pos(icx
, vcn
) / icx
->block_size
;
868 static s64
ntfs_ibm_pos_to_vcn(ntfs_index_context
*icx
, s64 pos
)
870 return ntfs_ib_pos_to_vcn(icx
, pos
* icx
->block_size
);
873 static int ntfs_ibm_add(ntfs_index_context
*icx
)
877 ntfs_log_trace("Entering\n");
879 if (ntfs_attr_exist(icx
->ni
, AT_BITMAP
, icx
->name
, icx
->name_len
))
882 * AT_BITMAP must be at least 8 bytes.
884 memset(bmp
, 0, sizeof(bmp
));
885 if (ntfs_attr_add(icx
->ni
, AT_BITMAP
, icx
->name
, icx
->name_len
,
887 ntfs_log_perror("Failed to add AT_BITMAP");
894 static int ntfs_ibm_modify(ntfs_index_context
*icx
, VCN vcn
, int set
)
897 s64 pos
= ntfs_ibm_vcn_to_pos(icx
, vcn
);
899 u32 bit
= 1 << (pos
% 8);
901 int ret
= STATUS_ERROR
;
903 ntfs_log_trace("%s vcn: %lld\n", set
? "set" : "clear", (long long)vcn
);
905 na
= ntfs_attr_open(icx
->ni
, AT_BITMAP
, icx
->name
, icx
->name_len
);
907 ntfs_log_perror("Failed to open $BITMAP attribute");
912 if (na
->data_size
< bpos
+ 1) {
913 if (ntfs_attr_truncate(na
, (na
->data_size
+ 8) & ~7)) {
914 ntfs_log_perror("Failed to truncate AT_BITMAP");
920 if (ntfs_attr_pread(na
, bpos
, 1, &byte
) != 1) {
921 ntfs_log_perror("Failed to read $BITMAP");
930 if (ntfs_attr_pwrite(na
, bpos
, 1, &byte
) != 1) {
931 ntfs_log_perror("Failed to write $Bitmap");
942 static int ntfs_ibm_set(ntfs_index_context
*icx
, VCN vcn
)
944 return ntfs_ibm_modify(icx
, vcn
, 1);
947 static int ntfs_ibm_clear(ntfs_index_context
*icx
, VCN vcn
)
949 return ntfs_ibm_modify(icx
, vcn
, 0);
952 static VCN
ntfs_ibm_get_free(ntfs_index_context
*icx
)
958 ntfs_log_trace("Entering\n");
960 bm
= ntfs_attr_readall(icx
->ni
, AT_BITMAP
, icx
->name
, icx
->name_len
,
965 for (byte
= 0; byte
< size
; byte
++) {
970 for (bit
= 0; bit
< 8; bit
++) {
971 if (!(bm
[byte
] & (1 << bit
))) {
972 vcn
= ntfs_ibm_pos_to_vcn(icx
, byte
* 8 + bit
);
978 vcn
= ntfs_ibm_pos_to_vcn(icx
, size
* 8);
980 ntfs_log_trace("allocated vcn: %lld\n", (long long)vcn
);
982 if (ntfs_ibm_set(icx
, vcn
))
989 static INDEX_BLOCK
*ntfs_ir_to_ib(INDEX_ROOT
*ir
, VCN ib_vcn
)
992 INDEX_ENTRY
*ie_last
;
993 char *ies_start
, *ies_end
;
996 ntfs_log_trace("Entering\n");
998 ib
= ntfs_ib_alloc(ib_vcn
, le32_to_cpu(ir
->index_block_size
), LEAF_NODE
);
1002 ies_start
= (char *)ntfs_ie_get_first(&ir
->index
);
1003 ies_end
= (char *)ntfs_ie_get_end(&ir
->index
);
1004 ie_last
= ntfs_ie_get_last((INDEX_ENTRY
*)ies_start
, ies_end
);
1006 * Copy all entries, including the termination entry
1007 * as well, which can never have any data.
1009 i
= (char *)ie_last
- ies_start
+ le16_to_cpu(ie_last
->length
);
1010 memcpy(ntfs_ie_get_first(&ib
->index
), ies_start
, i
);
1012 ib
->index
.ih_flags
= ir
->index
.ih_flags
;
1013 ib
->index
.index_length
= cpu_to_le32(i
+
1014 le32_to_cpu(ib
->index
.entries_offset
));
1018 static void ntfs_ir_nill(INDEX_ROOT
*ir
)
1020 INDEX_ENTRY
*ie_last
;
1021 char *ies_start
, *ies_end
;
1023 ntfs_log_trace("Entering\n");
1025 * TODO: This function could be much simpler.
1027 ies_start
= (char *)ntfs_ie_get_first(&ir
->index
);
1028 ies_end
= (char *)ntfs_ie_get_end(&ir
->index
);
1029 ie_last
= ntfs_ie_get_last((INDEX_ENTRY
*)ies_start
, ies_end
);
1031 * Move the index root termination entry forward
1033 if ((char *)ie_last
> ies_start
) {
1034 memmove(ies_start
, (char *)ie_last
, le16_to_cpu(ie_last
->length
));
1035 ie_last
= (INDEX_ENTRY
*)ies_start
;
1039 static int ntfs_ib_copy_tail(ntfs_index_context
*icx
, INDEX_BLOCK
*src
,
1040 INDEX_ENTRY
*median
, VCN new_vcn
)
1043 INDEX_ENTRY
*ie_head
; /* first entry after the median */
1047 ntfs_log_trace("Entering\n");
1049 dst
= ntfs_ib_alloc(new_vcn
, icx
->block_size
,
1050 src
->index
.ih_flags
& NODE_MASK
);
1052 return STATUS_ERROR
;
1054 ie_head
= ntfs_ie_get_next(median
);
1056 ies_end
= (u8
*)ntfs_ie_get_end(&src
->index
);
1057 tail_size
= ies_end
- (u8
*)ie_head
;
1058 memcpy(ntfs_ie_get_first(&dst
->index
), ie_head
, tail_size
);
1060 dst
->index
.index_length
= cpu_to_le32(tail_size
+
1061 le32_to_cpu(dst
->index
.entries_offset
));
1062 ret
= ntfs_ib_write(icx
, dst
);
1068 static int ntfs_ib_cut_tail(ntfs_index_context
*icx
, INDEX_BLOCK
*ib
,
1071 char *ies_start
, *ies_end
;
1072 INDEX_ENTRY
*ie_last
;
1074 ntfs_log_trace("Entering\n");
1076 ies_start
= (char *)ntfs_ie_get_first(&ib
->index
);
1077 ies_end
= (char *)ntfs_ie_get_end(&ib
->index
);
1079 ie_last
= ntfs_ie_get_last((INDEX_ENTRY
*)ies_start
, ies_end
);
1080 if (ie_last
->ie_flags
& INDEX_ENTRY_NODE
)
1081 ntfs_ie_set_vcn(ie_last
, ntfs_ie_get_vcn(ie
));
1083 memcpy(ie
, ie_last
, le16_to_cpu(ie_last
->length
));
1085 ib
->index
.index_length
= cpu_to_le32(((char *)ie
- ies_start
) +
1086 le16_to_cpu(ie
->length
) + le32_to_cpu(ib
->index
.entries_offset
));
1088 if (ntfs_ib_write(icx
, ib
))
1089 return STATUS_ERROR
;
1094 static int ntfs_ia_add(ntfs_index_context
*icx
)
1096 ntfs_log_trace("Entering\n");
1098 if (ntfs_ibm_add(icx
))
1101 if (!ntfs_attr_exist(icx
->ni
, AT_INDEX_ALLOCATION
, icx
->name
, icx
->name_len
)) {
1103 if (ntfs_attr_add(icx
->ni
, AT_INDEX_ALLOCATION
, icx
->name
,
1104 icx
->name_len
, NULL
, 0)) {
1105 ntfs_log_perror("Failed to add AT_INDEX_ALLOCATION");
1110 icx
->ia_na
= ntfs_ia_open(icx
, icx
->ni
);
1117 static int ntfs_ir_reparent(ntfs_index_context
*icx
)
1119 ntfs_attr_search_ctx
*ctx
= NULL
;
1122 INDEX_BLOCK
*ib
= NULL
;
1125 int ret
= STATUS_ERROR
;
1127 ntfs_log_trace("Entering\n");
1129 ir
= ntfs_ir_lookup2(icx
->ni
, icx
->name
, icx
->name_len
);
1133 if ((ir
->index
.ih_flags
& NODE_MASK
) == SMALL_INDEX
)
1134 if (ntfs_ia_add(icx
))
1137 new_ib_vcn
= ntfs_ibm_get_free(icx
);
1138 if (new_ib_vcn
== -1)
1141 ir
= ntfs_ir_lookup2(icx
->ni
, icx
->name
, icx
->name_len
);
1145 ib
= ntfs_ir_to_ib(ir
, new_ib_vcn
);
1147 ntfs_log_perror("Failed to move index root to index block");
1151 if (ntfs_ib_write(icx
, ib
))
1155 ir
= ntfs_ir_lookup(icx
->ni
, icx
->name
, icx
->name_len
, &ctx
);
1161 ie
= ntfs_ie_get_first(&ir
->index
);
1162 ie
->ie_flags
|= INDEX_ENTRY_NODE
;
1163 ie
->length
= cpu_to_le16(sizeof(INDEX_ENTRY_HEADER
) + sizeof(VCN
));
1165 ir
->index
.ih_flags
= LARGE_INDEX
;
1166 ir
->index
.index_length
= cpu_to_le32(le32_to_cpu(ir
->index
.entries_offset
)
1167 + le16_to_cpu(ie
->length
));
1168 ir
->index
.allocated_size
= ir
->index
.index_length
;
1169 ix_root_size
= sizeof(INDEX_ROOT
) - sizeof(INDEX_HEADER
)
1170 + le32_to_cpu(ir
->index
.allocated_size
);
1171 if (ntfs_resident_attr_value_resize(ctx
->mrec
, ctx
->attr
,
1174 * When there is no space to build a non-resident
1175 * index, we may have to move the root to an extent
1177 if ((errno
== ENOSPC
)
1179 && !ntfs_inode_add_attrlist(icx
->ni
)) {
1180 ntfs_attr_put_search_ctx(ctx
);
1181 ctx
= (ntfs_attr_search_ctx
*)NULL
;
1182 ir
= ntfs_ir_lookup(icx
->ni
, icx
->name
, icx
->name_len
,
1185 && !ntfs_attr_record_move_away(ctx
, ix_root_size
1186 - le32_to_cpu(ctx
->attr
->value_length
))) {
1187 ntfs_attr_put_search_ctx(ctx
);
1188 ctx
= (ntfs_attr_search_ctx
*)NULL
;
1192 /* FIXME: revert index root */
1196 * FIXME: do it earlier if we have enough space in IR (should always),
1197 * so in error case we wouldn't lose the IB.
1199 ntfs_ie_set_vcn(ie
, new_ib_vcn
);
1204 ntfs_attr_put_search_ctx(ctx
);
1208 ntfs_ibm_clear(icx
, new_ib_vcn
);
1213 * ntfs_ir_truncate - Truncate index root attribute
1215 * Returns STATUS_OK, STATUS_RESIDENT_ATTRIBUTE_FILLED_MFT or STATUS_ERROR.
1217 static int ntfs_ir_truncate(ntfs_index_context
*icx
, int data_size
)
1222 ntfs_log_trace("Entering\n");
1224 na
= ntfs_attr_open(icx
->ni
, AT_INDEX_ROOT
, icx
->name
, icx
->name_len
);
1226 ntfs_log_perror("Failed to open INDEX_ROOT");
1227 return STATUS_ERROR
;
1230 * INDEX_ROOT must be resident and its entries can be moved to
1231 * INDEX_BLOCK, so ENOSPC isn't a real error.
1233 ret
= ntfs_attr_truncate(na
, data_size
+ offsetof(INDEX_ROOT
, index
));
1234 if (ret
== STATUS_OK
) {
1236 icx
->ir
= ntfs_ir_lookup2(icx
->ni
, icx
->name
, icx
->name_len
);
1238 return STATUS_ERROR
;
1240 icx
->ir
->index
.allocated_size
= cpu_to_le32(data_size
);
1242 } else if (ret
== STATUS_ERROR
)
1243 ntfs_log_perror("Failed to truncate INDEX_ROOT");
1245 ntfs_attr_close(na
);
1250 * ntfs_ir_make_space - Make more space for the index root attribute
1252 * On success return STATUS_OK or STATUS_KEEP_SEARCHING.
1253 * On error return STATUS_ERROR.
1255 static int ntfs_ir_make_space(ntfs_index_context
*icx
, int data_size
)
1259 ntfs_log_trace("Entering\n");
1261 ret
= ntfs_ir_truncate(icx
, data_size
);
1262 if (ret
== STATUS_RESIDENT_ATTRIBUTE_FILLED_MFT
) {
1264 ret
= ntfs_ir_reparent(icx
);
1265 if (ret
== STATUS_OK
)
1266 ret
= STATUS_KEEP_SEARCHING
;
1268 ntfs_log_perror("Failed to nodify INDEX_ROOT");
1275 * NOTE: 'ie' must be a copy of a real index entry.
1277 static int ntfs_ie_add_vcn(INDEX_ENTRY
**ie
)
1279 INDEX_ENTRY
*p
, *old
= *ie
;
1281 old
->length
= cpu_to_le16(le16_to_cpu(old
->length
) + sizeof(VCN
));
1282 p
= realloc(old
, le16_to_cpu(old
->length
));
1284 return STATUS_ERROR
;
1286 p
->ie_flags
|= INDEX_ENTRY_NODE
;
1292 static int ntfs_ih_insert(INDEX_HEADER
*ih
, INDEX_ENTRY
*orig_ie
, VCN new_vcn
,
1295 INDEX_ENTRY
*ie_node
, *ie
;
1296 int ret
= STATUS_ERROR
;
1299 ntfs_log_trace("Entering\n");
1301 ie
= ntfs_ie_dup(orig_ie
);
1303 return STATUS_ERROR
;
1305 if (!(ie
->ie_flags
& INDEX_ENTRY_NODE
))
1306 if (ntfs_ie_add_vcn(&ie
))
1309 ie_node
= ntfs_ie_get_by_pos(ih
, pos
);
1310 old_vcn
= ntfs_ie_get_vcn(ie_node
);
1311 ntfs_ie_set_vcn(ie_node
, new_vcn
);
1313 ntfs_ie_insert(ih
, ie
, ie_node
);
1314 ntfs_ie_set_vcn(ie_node
, old_vcn
);
1322 static VCN
ntfs_icx_parent_vcn(ntfs_index_context
*icx
)
1324 return icx
->parent_vcn
[icx
->pindex
];
1327 static VCN
ntfs_icx_parent_pos(ntfs_index_context
*icx
)
1329 return icx
->parent_pos
[icx
->pindex
];
1333 static int ntfs_ir_insert_median(ntfs_index_context
*icx
, INDEX_ENTRY
*median
,
1339 ntfs_log_trace("Entering\n");
1341 icx
->ir
= ntfs_ir_lookup2(icx
->ni
, icx
->name
, icx
->name_len
);
1343 return STATUS_ERROR
;
1345 new_size
= le32_to_cpu(icx
->ir
->index
.index_length
) +
1346 le16_to_cpu(median
->length
);
1347 if (!(median
->ie_flags
& INDEX_ENTRY_NODE
))
1348 new_size
+= sizeof(VCN
);
1350 ret
= ntfs_ir_make_space(icx
, new_size
);
1351 if (ret
!= STATUS_OK
)
1354 icx
->ir
= ntfs_ir_lookup2(icx
->ni
, icx
->name
, icx
->name_len
);
1356 return STATUS_ERROR
;
1358 return ntfs_ih_insert(&icx
->ir
->index
, median
, new_vcn
,
1359 ntfs_icx_parent_pos(icx
));
1362 static int ntfs_ib_split(ntfs_index_context
*icx
, INDEX_BLOCK
*ib
);
1365 * On success return STATUS_OK or STATUS_KEEP_SEARCHING.
1366 * On error return STATUS_ERROR.
1368 static int ntfs_ib_insert(ntfs_index_context
*icx
, INDEX_ENTRY
*ie
, VCN new_vcn
)
1371 u32 idx_size
, allocated_size
;
1372 int err
= STATUS_ERROR
;
1375 ntfs_log_trace("Entering\n");
1377 ib
= ntfs_malloc(icx
->block_size
);
1381 old_vcn
= ntfs_icx_parent_vcn(icx
);
1383 if (ntfs_ib_read(icx
, old_vcn
, ib
))
1386 idx_size
= le32_to_cpu(ib
->index
.index_length
);
1387 allocated_size
= le32_to_cpu(ib
->index
.allocated_size
);
1388 /* FIXME: sizeof(VCN) should be included only if ie has no VCN */
1389 if (idx_size
+ le16_to_cpu(ie
->length
) + sizeof(VCN
) > allocated_size
) {
1390 err
= ntfs_ib_split(icx
, ib
);
1391 if (err
== STATUS_OK
)
1392 err
= STATUS_KEEP_SEARCHING
;
1396 if (ntfs_ih_insert(&ib
->index
, ie
, new_vcn
, ntfs_icx_parent_pos(icx
)))
1399 if (ntfs_ib_write(icx
, ib
))
1409 * ntfs_ib_split - Split an index block
1411 * On success return STATUS_OK or STATUS_KEEP_SEARCHING.
1412 * On error return is STATUS_ERROR.
1414 static int ntfs_ib_split(ntfs_index_context
*icx
, INDEX_BLOCK
*ib
)
1416 INDEX_ENTRY
*median
;
1420 ntfs_log_trace("Entering\n");
1422 if (ntfs_icx_parent_dec(icx
))
1423 return STATUS_ERROR
;
1425 median
= ntfs_ie_get_median(&ib
->index
);
1426 new_vcn
= ntfs_ibm_get_free(icx
);
1428 return STATUS_ERROR
;
1430 if (ntfs_ib_copy_tail(icx
, ib
, median
, new_vcn
)) {
1431 ntfs_ibm_clear(icx
, new_vcn
);
1432 return STATUS_ERROR
;
1435 if (ntfs_icx_parent_vcn(icx
) == VCN_INDEX_ROOT_PARENT
)
1436 ret
= ntfs_ir_insert_median(icx
, median
, new_vcn
);
1438 ret
= ntfs_ib_insert(icx
, median
, new_vcn
);
1440 if (ret
!= STATUS_OK
) {
1441 ntfs_ibm_clear(icx
, new_vcn
);
1445 ret
= ntfs_ib_cut_tail(icx
, ib
, median
);
1451 int ntfs_ie_add(ntfs_index_context
*icx
, INDEX_ENTRY
*ie
)
1454 int allocated_size
, new_size
;
1455 int ret
= STATUS_ERROR
;
1458 /* removed by JPA to make function usable for security indexes
1460 fn = ntfs_ie_filename_get(ie);
1461 ntfs_log_trace("file: '%s'\n", fn);
1462 ntfs_attr_name_free(&fn);
1468 if (!ntfs_index_lookup(&ie
->key
, le16_to_cpu(ie
->key_length
), icx
)) {
1470 ntfs_log_perror("Index already have such entry");
1473 if (errno
!= ENOENT
) {
1474 ntfs_log_perror("Failed to find place for new entry");
1478 if (icx
->is_in_root
)
1479 ih
= &icx
->ir
->index
;
1481 ih
= &icx
->ib
->index
;
1483 allocated_size
= le32_to_cpu(ih
->allocated_size
);
1484 new_size
= le32_to_cpu(ih
->index_length
) + le16_to_cpu(ie
->length
);
1486 if (new_size
<= allocated_size
)
1489 ntfs_log_trace("index block sizes: allocated: %d needed: %d\n",
1490 allocated_size
, new_size
);
1492 if (icx
->is_in_root
) {
1493 if (ntfs_ir_make_space(icx
, new_size
) == STATUS_ERROR
)
1496 if (ntfs_ib_split(icx
, icx
->ib
) == STATUS_ERROR
)
1500 ntfs_inode_mark_dirty(icx
->actx
->ntfs_ino
);
1501 ntfs_index_ctx_reinit(icx
);
1504 ntfs_ie_insert(ih
, ie
, icx
->entry
);
1505 ntfs_index_entry_mark_dirty(icx
);
1509 ntfs_log_trace("%s\n", ret
? "Failed" : "Done");
1514 * ntfs_index_add_filename - add filename to directory index
1515 * @ni: ntfs inode describing directory to which index add filename
1516 * @fn: FILE_NAME attribute to add
1517 * @mref: reference of the inode which @fn describes
1519 * Return 0 on success or -1 on error with errno set to the error code.
1521 int ntfs_index_add_filename(ntfs_inode
*ni
, FILE_NAME_ATTR
*fn
, MFT_REF mref
)
1524 ntfs_index_context
*icx
;
1525 int fn_size
, ie_size
, err
, ret
= -1;
1527 ntfs_log_trace("Entering\n");
1530 ntfs_log_error("Invalid arguments.\n");
1535 fn_size
= (fn
->file_name_length
* sizeof(ntfschar
)) +
1536 sizeof(FILE_NAME_ATTR
);
1537 ie_size
= (sizeof(INDEX_ENTRY_HEADER
) + fn_size
+ 7) & ~7;
1539 ie
= ntfs_calloc(ie_size
);
1543 ie
->indexed_file
= cpu_to_le64(mref
);
1544 ie
->length
= cpu_to_le16(ie_size
);
1545 ie
->key_length
= cpu_to_le16(fn_size
);
1546 memcpy(&ie
->key
, fn
, fn_size
);
1548 icx
= ntfs_index_ctx_get(ni
, NTFS_INDEX_I30
, 4);
1552 ret
= ntfs_ie_add(icx
, ie
);
1554 ntfs_index_ctx_put(icx
);
1561 static int ntfs_ih_takeout(ntfs_index_context
*icx
, INDEX_HEADER
*ih
,
1562 INDEX_ENTRY
*ie
, INDEX_BLOCK
*ib
)
1564 INDEX_ENTRY
*ie_roam
;
1565 int ret
= STATUS_ERROR
;
1567 ntfs_log_trace("Entering\n");
1569 ie_roam
= ntfs_ie_dup_novcn(ie
);
1571 return STATUS_ERROR
;
1573 ntfs_ie_delete(ih
, ie
);
1575 if (ntfs_icx_parent_vcn(icx
) == VCN_INDEX_ROOT_PARENT
)
1576 ntfs_inode_mark_dirty(icx
->actx
->ntfs_ino
);
1578 if (ntfs_ib_write(icx
, ib
))
1581 ntfs_index_ctx_reinit(icx
);
1583 ret
= ntfs_ie_add(icx
, ie_roam
);
1590 * Used if an empty index block to be deleted has END entry as the parent
1591 * in the INDEX_ROOT which is the only one there.
1593 static void ntfs_ir_leafify(ntfs_index_context
*icx
, INDEX_HEADER
*ih
)
1597 ntfs_log_trace("Entering\n");
1599 ie
= ntfs_ie_get_first(ih
);
1600 ie
->ie_flags
&= ~INDEX_ENTRY_NODE
;
1601 ie
->length
= cpu_to_le16(le16_to_cpu(ie
->length
) - sizeof(VCN
));
1603 ih
->index_length
= cpu_to_le32(le32_to_cpu(ih
->index_length
) - sizeof(VCN
));
1604 ih
->ih_flags
&= ~LARGE_INDEX
;
1606 /* Not fatal error */
1607 ntfs_ir_truncate(icx
, le32_to_cpu(ih
->index_length
));
1611 * Used if an empty index block to be deleted has END entry as the parent
1612 * in the INDEX_ROOT which is not the only one there.
1614 static int ntfs_ih_reparent_end(ntfs_index_context
*icx
, INDEX_HEADER
*ih
,
1617 INDEX_ENTRY
*ie
, *ie_prev
;
1619 ntfs_log_trace("Entering\n");
1621 ie
= ntfs_ie_get_by_pos(ih
, ntfs_icx_parent_pos(icx
));
1622 ie_prev
= ntfs_ie_prev(ih
, ie
);
1624 ntfs_ie_set_vcn(ie
, ntfs_ie_get_vcn(ie_prev
));
1626 return ntfs_ih_takeout(icx
, ih
, ie_prev
, ib
);
1629 static int ntfs_index_rm_leaf(ntfs_index_context
*icx
)
1631 INDEX_BLOCK
*ib
= NULL
;
1632 INDEX_HEADER
*parent_ih
;
1634 int ret
= STATUS_ERROR
;
1636 ntfs_log_trace("pindex: %d\n", icx
->pindex
);
1638 if (ntfs_icx_parent_dec(icx
))
1639 return STATUS_ERROR
;
1641 if (ntfs_ibm_clear(icx
, icx
->parent_vcn
[icx
->pindex
+ 1]))
1642 return STATUS_ERROR
;
1644 if (ntfs_icx_parent_vcn(icx
) == VCN_INDEX_ROOT_PARENT
)
1645 parent_ih
= &icx
->ir
->index
;
1647 ib
= ntfs_malloc(icx
->block_size
);
1649 return STATUS_ERROR
;
1651 if (ntfs_ib_read(icx
, ntfs_icx_parent_vcn(icx
), ib
))
1654 parent_ih
= &ib
->index
;
1657 ie
= ntfs_ie_get_by_pos(parent_ih
, ntfs_icx_parent_pos(icx
));
1658 if (!ntfs_ie_end(ie
)) {
1659 ret
= ntfs_ih_takeout(icx
, parent_ih
, ie
, ib
);
1663 if (ntfs_ih_zero_entry(parent_ih
)) {
1665 if (ntfs_icx_parent_vcn(icx
) == VCN_INDEX_ROOT_PARENT
) {
1666 ntfs_ir_leafify(icx
, parent_ih
);
1670 ret
= ntfs_index_rm_leaf(icx
);
1674 if (ntfs_ih_reparent_end(icx
, parent_ih
, ib
))
1683 static int ntfs_index_rm_node(ntfs_index_context
*icx
)
1685 int entry_pos
, pindex
;
1687 INDEX_BLOCK
*ib
= NULL
;
1688 INDEX_ENTRY
*ie_succ
, *ie
, *entry
= icx
->entry
;
1691 int delta
, ret
= STATUS_ERROR
;
1693 ntfs_log_trace("Entering\n");
1696 icx
->ia_na
= ntfs_ia_open(icx
, icx
->ni
);
1698 return STATUS_ERROR
;
1701 ib
= ntfs_malloc(icx
->block_size
);
1703 return STATUS_ERROR
;
1705 ie_succ
= ntfs_ie_get_next(icx
->entry
);
1706 entry_pos
= icx
->parent_pos
[icx
->pindex
]++;
1707 pindex
= icx
->pindex
;
1709 vcn
= ntfs_ie_get_vcn(ie_succ
);
1710 if (ntfs_ib_read(icx
, vcn
, ib
))
1713 ie_succ
= ntfs_ie_get_first(&ib
->index
);
1715 if (ntfs_icx_parent_inc(icx
))
1718 icx
->parent_vcn
[icx
->pindex
] = vcn
;
1719 icx
->parent_pos
[icx
->pindex
] = 0;
1721 if ((ib
->index
.ih_flags
& NODE_MASK
) == INDEX_NODE
)
1724 if (ntfs_ih_zero_entry(&ib
->index
)) {
1726 ntfs_log_perror("Empty index block");
1730 ie
= ntfs_ie_dup(ie_succ
);
1734 if (ntfs_ie_add_vcn(&ie
))
1737 ntfs_ie_set_vcn(ie
, ntfs_ie_get_vcn(icx
->entry
));
1739 if (icx
->is_in_root
)
1740 ih
= &icx
->ir
->index
;
1742 ih
= &icx
->ib
->index
;
1744 delta
= le16_to_cpu(ie
->length
) - le16_to_cpu(icx
->entry
->length
);
1745 new_size
= le32_to_cpu(ih
->index_length
) + delta
;
1747 if (icx
->is_in_root
) {
1748 ret
= ntfs_ir_make_space(icx
, new_size
);
1749 if (ret
!= STATUS_OK
)
1752 ih
= &icx
->ir
->index
;
1753 entry
= ntfs_ie_get_by_pos(ih
, entry_pos
);
1755 } else if (new_size
> le32_to_cpu(ih
->allocated_size
)) {
1756 icx
->pindex
= pindex
;
1757 ret
= ntfs_ib_split(icx
, icx
->ib
);
1758 if (ret
== STATUS_OK
)
1759 ret
= STATUS_KEEP_SEARCHING
;
1764 ntfs_ie_delete(ih
, entry
);
1765 ntfs_ie_insert(ih
, ie
, entry
);
1767 if (icx
->is_in_root
) {
1768 if (ntfs_ir_truncate(icx
, new_size
))
1771 if (ntfs_icx_ib_write(icx
))
1774 ntfs_ie_delete(&ib
->index
, ie_succ
);
1776 if (ntfs_ih_zero_entry(&ib
->index
)) {
1777 if (ntfs_index_rm_leaf(icx
))
1780 if (ntfs_ib_write(icx
, ib
))
1792 * ntfs_index_rm - remove entry from the index
1793 * @icx: index context describing entry to delete
1795 * Delete entry described by @icx from the index. Index context is always
1796 * reinitialized after use of this function, so it can be used for index
1797 * lookup once again.
1799 * Return 0 on success or -1 on error with errno set to the error code.
1802 int ntfs_index_rm(ntfs_index_context
*icx
)
1805 int err
, ret
= STATUS_OK
;
1807 ntfs_log_trace("Entering\n");
1809 if (!icx
|| (!icx
->ib
&& !icx
->ir
) || ntfs_ie_end(icx
->entry
)) {
1810 ntfs_log_error("Invalid arguments.\n");
1814 if (icx
->is_in_root
)
1815 ih
= &icx
->ir
->index
;
1817 ih
= &icx
->ib
->index
;
1819 if (icx
->entry
->ie_flags
& INDEX_ENTRY_NODE
) {
1821 ret
= ntfs_index_rm_node(icx
);
1823 } else if (icx
->is_in_root
|| !ntfs_ih_one_entry(ih
)) {
1825 ntfs_ie_delete(ih
, icx
->entry
);
1827 if (icx
->is_in_root
) {
1828 err
= ntfs_ir_truncate(icx
, le32_to_cpu(ih
->index_length
));
1829 if (err
!= STATUS_OK
)
1832 if (ntfs_icx_ib_write(icx
))
1835 if (ntfs_index_rm_leaf(icx
))
1845 int ntfs_index_remove(ntfs_inode
*dir_ni
, ntfs_inode
*ni
,
1846 const void *key
, const int keylen
)
1848 int ret
= STATUS_ERROR
;
1849 ntfs_index_context
*icx
;
1851 icx
= ntfs_index_ctx_get(dir_ni
, NTFS_INDEX_I30
, 4);
1857 if (ntfs_index_lookup(key
, keylen
, icx
))
1860 if ((((FILE_NAME_ATTR
*)icx
->data
)->file_attributes
&
1861 FILE_ATTR_REPARSE_POINT
)
1862 && !ntfs_possible_symlink(ni
)) {
1867 ret
= ntfs_index_rm(icx
);
1868 if (ret
== STATUS_ERROR
)
1870 else if (ret
== STATUS_OK
)
1873 ntfs_inode_mark_dirty(icx
->actx
->ntfs_ino
);
1874 ntfs_index_ctx_reinit(icx
);
1877 ntfs_inode_mark_dirty(icx
->actx
->ntfs_ino
);
1879 ntfs_index_ctx_put(icx
);
1883 ntfs_log_perror("Delete failed");
1888 * ntfs_index_root_get - read the index root of an attribute
1889 * @ni: open ntfs inode in which the ntfs attribute resides
1890 * @attr: attribute for which we want its index root
1892 * This function will read the related index root an ntfs attribute.
1894 * On success a buffer is allocated with the content of the index root
1895 * and which needs to be freed when it's not needed anymore.
1897 * On error NULL is returned with errno set to the error code.
1899 INDEX_ROOT
*ntfs_index_root_get(ntfs_inode
*ni
, ATTR_RECORD
*attr
)
1901 ntfs_attr_search_ctx
*ctx
;
1903 INDEX_ROOT
*root
= NULL
;
1905 name
= (ntfschar
*)((u8
*)attr
+ le16_to_cpu(attr
->name_offset
));
1907 if (!ntfs_ir_lookup(ni
, name
, attr
->name_length
, &ctx
))
1910 root
= ntfs_malloc(sizeof(INDEX_ROOT
));
1914 *root
= *((INDEX_ROOT
*)((u8
*)ctx
->attr
+
1915 le16_to_cpu(ctx
->attr
->value_offset
)));
1917 ntfs_attr_put_search_ctx(ctx
);
1923 * Walk down the index tree (leaf bound)
1924 * until there are no subnode in the first index entry
1925 * returns the entry at the bottom left in subnode
1928 static INDEX_ENTRY
*ntfs_index_walk_down(INDEX_ENTRY
*ie
,
1929 ntfs_index_context
*ictx
)
1936 vcn
= ntfs_ie_get_vcn(entry
);
1937 if (ictx
->is_in_root
) {
1939 /* down from level zero */
1941 ictx
->ir
= (INDEX_ROOT
*)NULL
;
1942 ictx
->ib
= (INDEX_BLOCK
*)ntfs_malloc(ictx
->block_size
);
1944 ictx
->is_in_root
= FALSE
;
1947 /* down from non-zero level */
1951 ictx
->parent_pos
[ictx
->pindex
] = 0;
1952 ictx
->parent_vcn
[ictx
->pindex
] = vcn
;
1953 if (!ntfs_ib_read(ictx
,vcn
,ictx
->ib
)) {
1954 ictx
->entry
= ntfs_ie_get_first(&ictx
->ib
->index
);
1955 entry
= ictx
->entry
;
1957 entry
= (INDEX_ENTRY
*)NULL
;
1958 } while (entry
&& (entry
->ie_flags
& INDEX_ENTRY_NODE
));
1963 * Walk up the index tree (root bound)
1964 * until there is a valid data entry in parent
1965 * returns the parent entry or NULL if no more parent
1968 static INDEX_ENTRY
*ntfs_index_walk_up(INDEX_ENTRY
*ie
,
1969 ntfs_index_context
*ictx
)
1975 if (ictx
->pindex
> 0) {
1978 if (!ictx
->pindex
) {
1980 /* we have reached the root */
1983 ictx
->ib
= (INDEX_BLOCK
*)NULL
;
1984 ictx
->is_in_root
= TRUE
;
1985 /* a new search context is to be allocated */
1988 ictx
->ir
= ntfs_ir_lookup(ictx
->ni
,
1989 ictx
->name
, ictx
->name_len
,
1992 entry
= ntfs_ie_get_by_pos(
1994 ictx
->parent_pos
[ictx
->pindex
]);
1996 entry
= (INDEX_ENTRY
*)NULL
;
1998 /* up into non-root node */
1999 vcn
= ictx
->parent_vcn
[ictx
->pindex
];
2000 if (!ntfs_ib_read(ictx
,vcn
,ictx
->ib
)) {
2001 entry
= ntfs_ie_get_by_pos(
2003 ictx
->parent_pos
[ictx
->pindex
]);
2005 entry
= (INDEX_ENTRY
*)NULL
;
2007 ictx
->entry
= entry
;
2008 } while (entry
&& (ictx
->pindex
> 0)
2009 && (entry
->ie_flags
& INDEX_ENTRY_END
));
2011 entry
= (INDEX_ENTRY
*)NULL
;
2016 * Get next entry in an index according to collating sequence.
2017 * Must be initialized through a ntfs_index_lookup()
2019 * Returns next entry or NULL if none
2023 * +---+---+---+---+---+---+---+---+ n ptrs to subnodes
2024 * | | | 10| 25| 33| | | | n-1 keys in between
2025 * +---+---+---+---+---+---+---+---+ no key in last entry
2027 * | | | +-------------------------------+
2028 * +--------------------------+ | +-----+ |
2031 * +---+---+---+---+---+---+---+---+ | +---+---+---+---+---+---+---+---+
2032 * | 11| 12| 13| 14| 15| 16| 17| | | | 26| 27| 28| 29| 30| 31| 32| |
2033 * +---+---+---+---+---+---+---+---+ | +---+---+---+---+---+---+---+---+
2035 * +-----------------------+ |
2037 * +---+---+---+---+---+---+---+---+
2038 * | 18| 19| 20| 21| 22| 23| 24| |
2039 * +---+---+---+---+---+---+---+---+
2042 INDEX_ENTRY
*ntfs_index_next(INDEX_ENTRY
*ie
, ntfs_index_context
*ictx
)
2048 * lookup() may have returned an invalid node
2049 * when searching for a partial key
2050 * if this happens, walk up
2053 if (ie
->ie_flags
& INDEX_ENTRY_END
)
2054 next
= ntfs_index_walk_up(ie
, ictx
);
2057 * get next entry in same node
2058 * there is always one after any entry with data
2061 next
= (INDEX_ENTRY
*)((char*)ie
+ le16_to_cpu(ie
->length
));
2062 ++ictx
->parent_pos
[ictx
->pindex
];
2063 flags
= next
->ie_flags
;
2065 /* walk down if it has a subnode */
2067 if (flags
& INDEX_ENTRY_NODE
) {
2068 next
= ntfs_index_walk_down(next
,ictx
);
2071 /* walk up it has no subnode, nor data */
2073 if (flags
& INDEX_ENTRY_END
) {
2074 next
= ntfs_index_walk_up(next
, ictx
);
2078 /* return NULL if stuck at end of a block */
2080 if (next
&& (next
->ie_flags
& INDEX_ENTRY_END
))
2081 next
= (INDEX_ENTRY
*)NULL
;