1N/A/**
1N/A * index.c - NTFS index handling. Part of the Linux-NTFS project.
1N/A *
1N/A * Copyright (c) 2004-2005 Anton Altaparmakov
1N/A * Copyright (c) 2004-2005 Richard Russon
1N/A * Copyright (c) 2005-2007 Yura Pakhuchiy
1N/A * Copyright (c) 2005-2006 Szabolcs Szakacsits
1N/A *
1N/A * This program/include file is free software; you can redistribute it and/or
1N/A * modify it under the terms of the GNU General Public License as published
1N/A * by the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program/include file is distributed in the hope that it will be
1N/A * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
1N/A * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program (in the main directory of the Linux-NTFS
1N/A * distribution in the file COPYING); if not, write to the Free Software
1N/A * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1N/A */
1N/A
1N/A#ifdef HAVE_CONFIG_H
1N/A#include "config.h"
1N/A#endif
1N/A
1N/A#ifdef HAVE_STDLIB_H
1N/A#include <stdlib.h>
1N/A#endif
1N/A#ifdef HAVE_STRING_H
1N/A#include <string.h>
1N/A#endif
1N/A#ifdef HAVE_ERRNO_H
1N/A#include <errno.h>
1N/A#endif
1N/A
1N/A#include "compat.h"
1N/A#include "attrib.h"
1N/A#include "collate.h"
1N/A#include "debug.h"
1N/A#include "index.h"
1N/A#include "mst.h"
1N/A#include "dir.h"
1N/A#include "logging.h"
1N/A#include "bitmap.h"
1N/A#include "support.h"
1N/A
1N/A/**
1N/A * ntfs_index_entry_mark_dirty - mark an index entry dirty
1N/A * @ictx: ntfs index context describing the index entry
1N/A *
1N/A * Mark the index entry described by the index entry context @ictx dirty.
1N/A *
1N/A * If the index entry is in the index root attribute, simply mark the inode
1N/A * containing the index root attribute dirty. This ensures the mftrecord, and
1N/A * hence the index root attribute, will be written out to disk later.
1N/A *
1N/A * If the index entry is in an index block belonging to the index allocation
1N/A * attribute, set ib_dirty to TRUE, thus index block will be updated during
1N/A * ntfs_index_ctx_put.
1N/A */
1N/Avoid ntfs_index_entry_mark_dirty(ntfs_index_context *ictx)
1N/A{
1N/A if (ictx->is_in_root)
1N/A ntfs_inode_mark_dirty(ictx->actx->ntfs_ino);
1N/A else
1N/A ictx->ib_dirty = TRUE;
1N/A}
1N/A
1N/Astatic s64 ntfs_ib_vcn_to_pos(ntfs_index_context *icx, VCN vcn)
1N/A{
1N/A return vcn << icx->vcn_size_bits;
1N/A}
1N/A
1N/Astatic VCN ntfs_ib_pos_to_vcn(ntfs_index_context *icx, s64 pos)
1N/A{
1N/A return pos >> icx->vcn_size_bits;
1N/A}
1N/A
1N/Astatic int ntfs_ib_write(ntfs_index_context *icx, VCN vcn, void *buf)
1N/A{
1N/A s64 ret;
1N/A
1N/A ntfs_log_trace("vcn: %lld\n", vcn);
1N/A
1N/A ret = ntfs_attr_mst_pwrite(icx->ia_na, ntfs_ib_vcn_to_pos(icx, vcn),
1N/A 1, icx->block_size, buf);
1N/A if (ret != 1) {
1N/A ntfs_log_perror("Failed to write index block %lld of inode "
1N/A "%llu", (long long)vcn,
1N/A (unsigned long long)icx->ni->mft_no);
1N/A return STATUS_ERROR;
1N/A }
1N/A return STATUS_OK;
1N/A}
1N/A
1N/Astatic int ntfs_icx_ib_write(ntfs_index_context *icx)
1N/A{
1N/A if (ntfs_ib_write(icx, icx->ib_vcn, icx->ib))
1N/A return STATUS_ERROR;
1N/A
1N/A icx->ib_dirty = FALSE;
1N/A
1N/A return STATUS_OK;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_index_ctx_get - allocate and initialize a new index context
1N/A * @ni: ntfs inode with which to initialize the context
1N/A * @name: name of the which context describes
1N/A * @name_len: length of the index name
1N/A *
1N/A * Allocate a new index context, initialize it with @ni and return it.
1N/A * Return NULL if allocation failed.
1N/A */
1N/Antfs_index_context *ntfs_index_ctx_get(ntfs_inode *ni,
1N/A ntfschar *name, u32 name_len)
1N/A{
1N/A ntfs_index_context *icx;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A if (!ni) {
1N/A errno = EINVAL;
1N/A return NULL;
1N/A }
1N/A if (ni->nr_extents == -1)
1N/A ni = ni->u.base_ni;
1N/A icx = ntfs_calloc(sizeof(ntfs_index_context));
1N/A if (icx)
1N/A *icx = (ntfs_index_context) {
1N/A .ni = ni,
1N/A .name = name,
1N/A .name_len = name_len,
1N/A };
1N/A return icx;
1N/A}
1N/A
1N/Astatic void ntfs_index_ctx_free(ntfs_index_context *icx)
1N/A{
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A if (!icx->entry)
1N/A return;
1N/A
1N/A if (icx->actx)
1N/A ntfs_attr_put_search_ctx(icx->actx);
1N/A
1N/A if (icx->is_in_root) {
1N/A if (icx->ia_na)
1N/A ntfs_attr_close(icx->ia_na);
1N/A return;
1N/A }
1N/A
1N/A if (icx->ib_dirty) {
1N/A /* FIXME: Error handling!!! */
1N/A ntfs_ib_write(icx, icx->ib_vcn, icx->ib);
1N/A }
1N/A
1N/A free(icx->ib);
1N/A ntfs_attr_close(icx->ia_na);
1N/A}
1N/A
1N/A/**
1N/A * ntfs_index_ctx_put - release an index context
1N/A * @icx: index context to free
1N/A *
1N/A * Release the index context @icx, releasing all associated resources.
1N/A */
1N/Avoid ntfs_index_ctx_put(ntfs_index_context *icx)
1N/A{
1N/A ntfs_index_ctx_free(icx);
1N/A free(icx);
1N/A}
1N/A
1N/A/**
1N/A * ntfs_index_ctx_reinit - reinitialize an index context
1N/A * @icx: index context to reinitialize
1N/A *
1N/A * Reinitialize the index context @icx so it can be used for ntfs_index_lookup.
1N/A */
1N/Avoid ntfs_index_ctx_reinit(ntfs_index_context *icx)
1N/A{
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A ntfs_index_ctx_free(icx);
1N/A
1N/A *icx = (ntfs_index_context) {
1N/A .ni = icx->ni,
1N/A .name = icx->name,
1N/A .name_len = icx->name_len,
1N/A };
1N/A}
1N/A
1N/Astatic leVCN *ntfs_ie_get_vcn_addr(INDEX_ENTRY *ie)
1N/A{
1N/A return (leVCN *)((u8 *)ie + le16_to_cpu(ie->length) - sizeof(VCN));
1N/A}
1N/A
1N/A/**
1N/A * Get the subnode vcn to which the index entry refers.
1N/A */
1N/AVCN ntfs_ie_get_vcn(INDEX_ENTRY *ie)
1N/A{
1N/A return sle64_to_cpup(ntfs_ie_get_vcn_addr(ie));
1N/A}
1N/A
1N/Astatic INDEX_ENTRY *ntfs_ie_get_first(INDEX_HEADER *ih)
1N/A{
1N/A return (INDEX_ENTRY *)((u8 *)ih + le32_to_cpu(ih->entries_offset));
1N/A}
1N/A
1N/Astatic INDEX_ENTRY *ntfs_ie_get_next(INDEX_ENTRY *ie)
1N/A{
1N/A return (INDEX_ENTRY *)((char *)ie + le16_to_cpu(ie->length));
1N/A}
1N/A
1N/Astatic u8 *ntfs_ie_get_end(INDEX_HEADER *ih)
1N/A{
1N/A /* FIXME: check if it isn't overflowing the index block size */
1N/A return (u8 *)ih + le32_to_cpu(ih->index_length);
1N/A}
1N/A
1N/Astatic int ntfs_ie_end(INDEX_ENTRY *ie)
1N/A{
1N/A return (ie->flags & INDEX_ENTRY_END) ? 1 : 0;
1N/A}
1N/A
1N/A/**
1N/A * Find the last entry in the index block
1N/A */
1N/Astatic INDEX_ENTRY *ntfs_ie_get_last(INDEX_ENTRY *ie, char *ies_end)
1N/A{
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A while ((char *)ie < ies_end && !ntfs_ie_end(ie))
1N/A ie = ntfs_ie_get_next(ie);
1N/A return ie;
1N/A}
1N/A
1N/Astatic INDEX_ENTRY *ntfs_ie_get_by_pos(INDEX_HEADER *ih, int pos)
1N/A{
1N/A INDEX_ENTRY *ie;
1N/A
1N/A ntfs_log_trace("pos: %d\n", pos);
1N/A
1N/A ie = ntfs_ie_get_first(ih);
1N/A
1N/A while (pos-- > 0)
1N/A ie = ntfs_ie_get_next(ie);
1N/A return ie;
1N/A}
1N/A
1N/Astatic INDEX_ENTRY *ntfs_ie_prev(INDEX_HEADER *ih, INDEX_ENTRY *ie)
1N/A{
1N/A INDEX_ENTRY *ie_prev, *tmp;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A ie_prev = NULL;
1N/A tmp = ntfs_ie_get_first(ih);
1N/A
1N/A while (tmp != ie) {
1N/A ie_prev = tmp;
1N/A tmp = ntfs_ie_get_next(tmp);
1N/A }
1N/A return ie_prev;
1N/A}
1N/A
1N/Achar *ntfs_ie_filename_get(INDEX_ENTRY *ie)
1N/A{
1N/A FILE_NAME_ATTR *fn;
1N/A char *name = NULL;
1N/A int name_len;
1N/A
1N/A fn = (FILE_NAME_ATTR *)&ie->key;
1N/A name_len = ntfs_ucstombs(fn->file_name, fn->file_name_length, &name, 0);
1N/A if (name_len < 0) {
1N/A ntfs_log_perror("ntfs_ucstombs");
1N/A return NULL;
1N/A } else if (name_len > 0)
1N/A return name;
1N/A free(name);
1N/A return NULL;
1N/A}
1N/A
1N/Avoid ntfs_ie_filename_dump(INDEX_ENTRY *ie)
1N/A{
1N/A char *s;
1N/A
1N/A s = ntfs_ie_filename_get(ie);
1N/A ntfs_log_debug("'%s' ", s);
1N/A free(s);
1N/A}
1N/A
1N/Avoid ntfs_ih_filename_dump(INDEX_HEADER *ih)
1N/A{
1N/A INDEX_ENTRY *ie;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A ie = ntfs_ie_get_first(ih);
1N/A while (!ntfs_ie_end(ie)) {
1N/A ntfs_ie_filename_dump(ie);
1N/A ie = ntfs_ie_get_next(ie);
1N/A }
1N/A}
1N/A
1N/Astatic int ntfs_ih_numof_entries(INDEX_HEADER *ih)
1N/A{
1N/A int n;
1N/A INDEX_ENTRY *ie;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A ie = ntfs_ie_get_first(ih);
1N/A for (n = 0; !ntfs_ie_end(ie); n++)
1N/A ie = ntfs_ie_get_next(ie);
1N/A return n;
1N/A}
1N/A
1N/Astatic int ntfs_ih_one_entry(INDEX_HEADER *ih)
1N/A{
1N/A return (ntfs_ih_numof_entries(ih) == 1);
1N/A}
1N/A
1N/Astatic int ntfs_ih_zero_entry(INDEX_HEADER *ih)
1N/A{
1N/A return (ntfs_ih_numof_entries(ih) == 0);
1N/A}
1N/A
1N/Astatic void ntfs_ie_delete(INDEX_HEADER *ih, INDEX_ENTRY *ie)
1N/A{
1N/A u32 new_size;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A new_size = le32_to_cpu(ih->index_length) - le16_to_cpu(ie->length);
1N/A ih->index_length = cpu_to_le32(new_size);
1N/A memmove(ie, (u8 *)ie + le16_to_cpu(ie->length),
1N/A new_size - ((u8 *)ie - (u8 *)ih));
1N/A}
1N/A
1N/Astatic void ntfs_ie_set_vcn(INDEX_ENTRY *ie, VCN vcn)
1N/A{
1N/A *ntfs_ie_get_vcn_addr(ie) = cpu_to_sle64(vcn);
1N/A}
1N/A
1N/A/**
1N/A * Insert @ie index entry at @pos entry. Used @ih values should be ok already.
1N/A */
1N/Astatic void ntfs_ie_insert(INDEX_HEADER *ih, INDEX_ENTRY *ie, INDEX_ENTRY *pos)
1N/A{
1N/A int ie_size = le16_to_cpu(ie->length);
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A ih->index_length = cpu_to_le32(le32_to_cpu(ih->index_length) + ie_size);
1N/A memmove((u8 *)pos + ie_size, pos,
1N/A le32_to_cpu(ih->index_length) - ((u8 *)pos - (u8 *)ih) -
1N/A ie_size);
1N/A memcpy(pos, ie, ie_size);
1N/A}
1N/A
1N/Astatic INDEX_ENTRY *ntfs_ie_dup(INDEX_ENTRY *ie)
1N/A{
1N/A INDEX_ENTRY *dup;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A dup = ntfs_malloc(le16_to_cpu(ie->length));
1N/A if (dup)
1N/A memcpy(dup, ie, le16_to_cpu(ie->length));
1N/A return dup;
1N/A}
1N/A
1N/Astatic INDEX_ENTRY *ntfs_ie_dup_novcn(INDEX_ENTRY *ie)
1N/A{
1N/A INDEX_ENTRY *dup;
1N/A int size = le16_to_cpu(ie->length);
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A if (ie->flags & INDEX_ENTRY_NODE)
1N/A size -= sizeof(VCN);
1N/A
1N/A dup = ntfs_malloc(size);
1N/A if (dup) {
1N/A memcpy(dup, ie, size);
1N/A dup->flags &= ~INDEX_ENTRY_NODE;
1N/A dup->length = cpu_to_le16(size);
1N/A }
1N/A return dup;
1N/A}
1N/A
1N/Astatic int ntfs_ia_check(ntfs_index_context *icx, INDEX_BLOCK *ib, VCN vcn)
1N/A{
1N/A u32 ib_size = (unsigned)le32_to_cpu(ib->index.allocated_size) + 0x18;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A if (!ntfs_is_indx_record(ib->magic)) {
1N/A
1N/A ntfs_log_error("Corrupt index block signature: vcn %lld inode "
1N/A "%llu\n", (long long)vcn,
1N/A (unsigned long long)icx->ni->mft_no);
1N/A return -1;
1N/A }
1N/A
1N/A if (sle64_to_cpu(ib->index_block_vcn) != vcn) {
1N/A
1N/A ntfs_log_error("Corrupt index block: VCN (%lld) is different "
1N/A "from expected VCN (%lld) in inode %llu\n",
1N/A (long long)sle64_to_cpu(ib->index_block_vcn),
1N/A (long long)vcn,
1N/A (unsigned long long)icx->ni->mft_no);
1N/A return -1;
1N/A }
1N/A
1N/A if (ib_size != icx->block_size) {
1N/A
1N/A ntfs_log_error("Corrupt index block : VCN (%lld) of inode %llu "
1N/A "has a size (%u) differing from the index "
1N/A "specified size (%u)\n", (long long)vcn,
1N/A icx->ni->mft_no, (unsigned)ib_size,
1N/A (unsigned)icx->block_size);
1N/A return -1;
1N/A }
1N/A return 0;
1N/A}
1N/A
1N/Astatic INDEX_ROOT *ntfs_ir_lookup(ntfs_inode *ni, ntfschar *name,
1N/A u32 name_len, ntfs_attr_search_ctx **ctx)
1N/A{
1N/A ATTR_RECORD *a;
1N/A INDEX_ROOT *ir = NULL;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A *ctx = ntfs_attr_get_search_ctx(ni, NULL);
1N/A if (!*ctx) {
1N/A ntfs_log_perror("Failed to get $INDEX_ROOT search context");
1N/A return NULL;
1N/A }
1N/A
1N/A if (ntfs_attr_lookup(AT_INDEX_ROOT, name, name_len, CASE_SENSITIVE,
1N/A 0, NULL, 0, *ctx)) {
1N/A ntfs_log_perror("Failed to lookup $INDEX_ROOT");
1N/A goto err_out;
1N/A }
1N/A
1N/A a = (*ctx)->attr;
1N/A if (a->non_resident) {
1N/A errno = EINVAL;
1N/A ntfs_log_perror("Non-resident $INDEX_ROOT detected");
1N/A goto err_out;
1N/A }
1N/A
1N/A ir = (INDEX_ROOT *)((char *)a + le16_to_cpu(a->u.res.value_offset));
1N/Aerr_out:
1N/A if (!ir)
1N/A ntfs_attr_put_search_ctx(*ctx);
1N/A return ir;
1N/A}
1N/A
1N/A/**
1N/A * Find a key in the index block.
1N/A *
1N/A * Return values:
1N/A * STATUS_OK with errno set to ESUCCESS if we know for sure that the
1N/A * entry exists and @ie_out points to this entry.
1N/A * STATUS_NOT_FOUND with errno set to ENOENT if we know for sure the
1N/A * entry doesn't exist and @ie_out is the insertion point.
1N/A * STATUS_KEEP_SEARCHING if we can't answer the above question and
1N/A * @vcn will contain the node index block.
1N/A * STATUS_ERROR with errno set if on unexpected error during lookup.
1N/A */
1N/Astatic int ntfs_ie_lookup(const void *key, const int key_len,
1N/A ntfs_index_context *icx, INDEX_HEADER *ih,
1N/A VCN *vcn, INDEX_ENTRY **ie_out)
1N/A{
1N/A INDEX_ENTRY *ie;
1N/A u8 *index_end;
1N/A int rc, item = 0;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A index_end = ntfs_ie_get_end(ih);
1N/A
1N/A /*
1N/A * Loop until we exceed valid memory (corruption case) or until we
1N/A * reach the last entry.
1N/A */
1N/A for (ie = ntfs_ie_get_first(ih); ; ie = ntfs_ie_get_next(ie)) {
1N/A /* Bounds checks. */
1N/A if ((u8 *)ie + sizeof(INDEX_ENTRY_HEADER) > index_end ||
1N/A (u8 *)ie + le16_to_cpu(ie->length) > index_end) {
1N/A errno = ERANGE;
1N/A ntfs_log_error("Index entry out of bounds in inode "
1N/A "%llu.\n",
1N/A (unsigned long long)icx->ni->mft_no);
1N/A return STATUS_ERROR;
1N/A }
1N/A /*
1N/A * The last entry cannot contain a key. It can however contain
1N/A * a pointer to a child node in the B+tree so we just break out.
1N/A */
1N/A if (ntfs_ie_end(ie))
1N/A break;
1N/A /*
1N/A * Not a perfect match, need to do full blown collation so we
1N/A * know which way in the B+tree we have to go.
1N/A */
1N/A rc = ntfs_collate(icx->ni->vol, icx->cr, key, key_len, &ie->key,
1N/A le16_to_cpu(ie->key_length));
1N/A if (rc == NTFS_COLLATION_ERROR) {
1N/A ntfs_log_error("Collation error. Perhaps a filename "
1N/A "contains invalid characters?\n");
1N/A errno = ERANGE;
1N/A return STATUS_ERROR;
1N/A }
1N/A /*
1N/A * If @key collates before the key of the current entry, there
1N/A * is definitely no such key in this index but we might need to
1N/A * descend into the B+tree so we just break out of the loop.
1N/A */
1N/A if (rc == -1)
1N/A break;
1N/A
1N/A if (!rc) {
1N/A *ie_out = ie;
1N/A errno = 0;
1N/A icx->parent_pos[icx->pindex] = item;
1N/A return STATUS_OK;
1N/A }
1N/A
1N/A item++;
1N/A }
1N/A /*
1N/A * We have finished with this index block without success. Check for the
1N/A * presence of a child node and if not present return with errno ENOENT,
1N/A * otherwise we will keep searching in another index block.
1N/A */
1N/A if (!(ie->flags & INDEX_ENTRY_NODE)) {
1N/A ntfs_log_debug("Index entry wasn't found.\n");
1N/A *ie_out = ie;
1N/A errno = ENOENT;
1N/A return STATUS_NOT_FOUND;
1N/A }
1N/A
1N/A /* Get the starting vcn of the index_block holding the child node. */
1N/A *vcn = ntfs_ie_get_vcn(ie);
1N/A if (*vcn < 0) {
1N/A errno = EINVAL;
1N/A ntfs_log_perror("Negative vcn in inode %llu\n",
1N/A icx->ni->mft_no);
1N/A return STATUS_ERROR;
1N/A }
1N/A
1N/A ntfs_log_trace("Parent entry number %d\n", item);
1N/A icx->parent_pos[icx->pindex] = item;
1N/A return STATUS_KEEP_SEARCHING;
1N/A}
1N/A
1N/Astatic ntfs_attr *ntfs_ia_open(ntfs_index_context *icx, ntfs_inode *ni)
1N/A{
1N/A ntfs_attr *na;
1N/A
1N/A na = ntfs_attr_open(ni, AT_INDEX_ALLOCATION, icx->name, icx->name_len);
1N/A if (!na) {
1N/A ntfs_log_perror("Failed to open index allocation of inode "
1N/A "%llu", (unsigned long long)ni->mft_no);
1N/A return NULL;
1N/A }
1N/A return na;
1N/A}
1N/A
1N/Astatic int ntfs_ib_read(ntfs_index_context *icx, VCN vcn, INDEX_BLOCK *dst)
1N/A{
1N/A s64 pos, ret;
1N/A
1N/A ntfs_log_trace("vcn: %lld\n", vcn);
1N/A
1N/A pos = ntfs_ib_vcn_to_pos(icx, vcn);
1N/A
1N/A ret = ntfs_attr_mst_pread(icx->ia_na, pos, 1, icx->block_size,
1N/A (u8 *)dst);
1N/A if (ret != 1) {
1N/A if (ret == -1)
1N/A ntfs_log_perror("Failed to read index block");
1N/A else
1N/A ntfs_log_error("Failed to read full index block at "
1N/A "%lld\n", (long long)pos);
1N/A return -1;
1N/A }
1N/A
1N/A if (ntfs_ia_check(icx, dst, vcn))
1N/A return -1;
1N/A return 0;
1N/A}
1N/A
1N/Astatic int ntfs_icx_parent_inc(ntfs_index_context *icx)
1N/A{
1N/A icx->pindex++;
1N/A if (icx->pindex >= MAX_PARENT_VCN) {
1N/A errno = EOPNOTSUPP;
1N/A ntfs_log_perror("Index is over %d level deep", MAX_PARENT_VCN);
1N/A return STATUS_ERROR;
1N/A }
1N/A return STATUS_OK;
1N/A}
1N/A
1N/Astatic int ntfs_icx_parent_dec(ntfs_index_context *icx)
1N/A{
1N/A icx->pindex--;
1N/A if (icx->pindex < 0) {
1N/A errno = EINVAL;
1N/A ntfs_log_perror("Corrupt index pointer (%d)", icx->pindex);
1N/A return STATUS_ERROR;
1N/A }
1N/A return STATUS_OK;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_index_lookup - find a key in an index and return its index entry
1N/A * @key: [IN] key for which to search in the index
1N/A * @key_len: [IN] length of @key in bytes
1N/A * @icx: [IN/OUT] context describing the index and the returned entry
1N/A *
1N/A * Before calling ntfs_index_lookup(), @icx must have been obtained from a
1N/A * call to ntfs_index_ctx_get().
1N/A *
1N/A * Look for the @key in the index specified by the index lookup context @icx.
1N/A * ntfs_index_lookup() walks the contents of the index looking for the @key.
1N/A *
1N/A * If the @key is found in the index, 0 is returned and @icx is setup to
1N/A * describe the index entry containing the matching @key. @icx->entry is the
1N/A * index entry and @icx->data and @icx->data_len are the index entry data and
1N/A * its length in bytes, respectively.
1N/A *
1N/A * If the @key is not found in the index, -1 is returned, errno = ENOENT and
1N/A * @icx is setup to describe the index entry whose key collates immediately
1N/A * after the search @key, i.e. this is the position in the index at which
1N/A * an index entry with a key of @key would need to be inserted.
1N/A *
1N/A * If an error occurs return -1, set errno to error code and @icx is left
1N/A * untouched.
1N/A *
1N/A * When finished with the entry and its data, call ntfs_index_ctx_put() to free
1N/A * the context and other associated resources.
1N/A *
1N/A * If the index entry was modified, call ntfs_index_entry_mark_dirty() before
1N/A * the call to ntfs_index_ctx_put() to ensure that the changes are written
1N/A * to disk.
1N/A */
1N/Aint ntfs_index_lookup(const void *key, const int key_len,
1N/A ntfs_index_context *icx)
1N/A{
1N/A VCN old_vcn, vcn;
1N/A ntfs_inode *ni = icx->ni;
1N/A INDEX_ROOT *ir;
1N/A INDEX_ENTRY *ie;
1N/A INDEX_BLOCK *ib = NULL;
1N/A ntfs_attr_search_ctx *actx;
1N/A int ret, err = 0;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A if (!key || key_len <= 0) {
1N/A errno = EINVAL;
1N/A ntfs_log_perror("key: %p key_len: %d", key, key_len);
1N/A return -1;
1N/A }
1N/A
1N/A ir = ntfs_ir_lookup(ni, icx->name, icx->name_len, &actx);
1N/A if (!ir) {
1N/A if (errno == ENOENT)
1N/A errno = EIO;
1N/A return -1;
1N/A }
1N/A
1N/A icx->block_size = le32_to_cpu(ir->index_block_size);
1N/A if (icx->block_size < NTFS_BLOCK_SIZE) {
1N/A errno = EINVAL;
1N/A ntfs_log_perror("Index block size (%u) is smaller than the "
1N/A "sector size (%d)", (unsigned)icx->block_size,
1N/A NTFS_BLOCK_SIZE);
1N/A return -1;
1N/A }
1N/A
1N/A if (ni->vol->cluster_size <= icx->block_size)
1N/A icx->vcn_size_bits = ni->vol->cluster_size_bits;
1N/A else
1N/A icx->vcn_size_bits = ni->vol->sector_size_bits;
1N/A
1N/A icx->cr = ir->collation_rule;
1N/A if (!ntfs_is_collation_rule_supported(icx->cr)) {
1N/A err = errno = EOPNOTSUPP;
1N/A ntfs_log_perror("Unknown collation rule 0x%x",
1N/A (unsigned)le32_to_cpu(icx->cr));
1N/A goto err_out;
1N/A }
1N/A
1N/A old_vcn = VCN_INDEX_ROOT_PARENT;
1N/A /*
1N/A * FIXME: check for both ir and ib that the first index entry is
1N/A * within the index block.
1N/A */
1N/A ret = ntfs_ie_lookup(key, key_len, icx, &ir->index, &vcn, &ie);
1N/A if (ret == STATUS_ERROR) {
1N/A err = errno;
1N/A goto err_out;
1N/A }
1N/A
1N/A icx->actx = actx;
1N/A icx->ir = ir;
1N/A
1N/A if (ret != STATUS_KEEP_SEARCHING) {
1N/A /* STATUS_OK or STATUS_NOT_FOUND */
1N/A err = errno;
1N/A icx->is_in_root = TRUE;
1N/A icx->parent_vcn[icx->pindex] = old_vcn;
1N/A goto done;
1N/A }
1N/A
1N/A /* Child node present, descend into it. */
1N/A icx->ia_na = ntfs_ia_open(icx, ni);
1N/A if (!icx->ia_na)
1N/A goto err_out;
1N/A
1N/A ib = ntfs_malloc(icx->block_size);
1N/A if (!ib) {
1N/A err = errno;
1N/A goto err_out;
1N/A }
1N/A
1N/Adescend_into_child_node:
1N/A icx->parent_vcn[icx->pindex] = old_vcn;
1N/A if (ntfs_icx_parent_inc(icx)) {
1N/A err = errno;
1N/A goto err_out;
1N/A }
1N/A old_vcn = vcn;
1N/A
1N/A ntfs_log_debug("Descend into node with VCN %lld.\n", vcn);
1N/A
1N/A if (ntfs_ib_read(icx, vcn, ib))
1N/A goto err_out;
1N/A
1N/A ret = ntfs_ie_lookup(key, key_len, icx, &ib->index, &vcn, &ie);
1N/A if (ret != STATUS_KEEP_SEARCHING) {
1N/A err = errno;
1N/A if (ret == STATUS_ERROR)
1N/A goto err_out;
1N/A
1N/A /* STATUS_OK or STATUS_NOT_FOUND */
1N/A icx->is_in_root = FALSE;
1N/A icx->ib = ib;
1N/A icx->parent_vcn[icx->pindex] = icx->ib_vcn = vcn;
1N/A goto done;
1N/A }
1N/A
1N/A if ((ib->index.flags & NODE_MASK) == LEAF_NODE) {
1N/A ntfs_log_error("Index entry with child node found in a leaf "
1N/A "node in inode 0x%llx.\n",
1N/A (unsigned long long)ni->mft_no);
1N/A goto err_out;
1N/A }
1N/A
1N/A goto descend_into_child_node;
1N/Aerr_out:
1N/A if (icx->ia_na) {
1N/A ntfs_attr_close(icx->ia_na);
1N/A icx->ia_na = NULL;
1N/A }
1N/A free(ib);
1N/A if (!err)
1N/A err = EIO;
1N/A if (actx)
1N/A ntfs_attr_put_search_ctx(actx);
1N/A errno = err;
1N/A return -1;
1N/Adone:
1N/A icx->entry = ie;
1N/A icx->data = (u8 *)ie + offsetof(INDEX_ENTRY, key);
1N/A icx->data_len = le16_to_cpu(ie->key_length);
1N/A icx->max_depth = icx->pindex;
1N/A ntfs_log_trace("Done.\n");
1N/A if (err) {
1N/A errno = err;
1N/A return -1;
1N/A }
1N/A return 0;
1N/A}
1N/A
1N/Astatic INDEX_BLOCK *ntfs_ib_alloc(VCN ib_vcn, u32 ib_size,
1N/A INDEX_HEADER_FLAGS node_type)
1N/A{
1N/A INDEX_BLOCK *ib;
1N/A int ih_size = sizeof(INDEX_HEADER);
1N/A
1N/A ntfs_log_trace("Entering ib_vcn = %lld ib_size = %u\n", ib_vcn,
1N/A ib_size);
1N/A
1N/A ib = ntfs_calloc(ib_size);
1N/A if (!ib)
1N/A return NULL;
1N/A
1N/A ib->magic = magic_INDX;
1N/A ib->usa_ofs = cpu_to_le16(sizeof(INDEX_BLOCK));
1N/A ib->usa_count = cpu_to_le16(ib_size / NTFS_BLOCK_SIZE + 1);
1N/A /* Set USN to 1 */
1N/A *(le16 *)((char *)ib + le16_to_cpu(ib->usa_ofs)) = cpu_to_le16(1);
1N/A ib->lsn = 0;
1N/A
1N/A ib->index_block_vcn = cpu_to_sle64(ib_vcn);
1N/A
1N/A ib->index.entries_offset = cpu_to_le32((ih_size +
1N/A le16_to_cpu(ib->usa_count) * 2 + 7) & ~7);
1N/A ib->index.index_length = 0;
1N/A ib->index.allocated_size = cpu_to_le32(ib_size -
1N/A (sizeof(INDEX_BLOCK) - ih_size));
1N/A ib->index.flags = node_type;
1N/A return ib;
1N/A}
1N/A
1N/A/**
1N/A * Find the median by going through all the entries
1N/A */
1N/Astatic INDEX_ENTRY *ntfs_ie_get_median(INDEX_HEADER *ih)
1N/A{
1N/A INDEX_ENTRY *ie, *ie_start;
1N/A u8 *ie_end;
1N/A int i = 0, median;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A ie = ie_start = ntfs_ie_get_first(ih);
1N/A ie_end = (u8 *)ntfs_ie_get_end(ih);
1N/A
1N/A while ((u8 *)ie < ie_end && !ntfs_ie_end(ie)) {
1N/A ie = ntfs_ie_get_next(ie);
1N/A i++;
1N/A }
1N/A /*
1N/A * NOTE: this could be also the entry at the half of the index block.
1N/A */
1N/A median = i / 2 - 1;
1N/A
1N/A ntfs_log_trace("Entries: %d median: %d\n", i, median);
1N/A
1N/A for (i = 0, ie = ie_start; i <= median; i++)
1N/A ie = ntfs_ie_get_next(ie);
1N/A
1N/A return ie;
1N/A}
1N/A
1N/Astatic s64 ntfs_ibm_vcn_to_pos(ntfs_index_context *icx, VCN vcn)
1N/A{
1N/A return ntfs_ib_vcn_to_pos(icx, vcn) / icx->block_size;
1N/A}
1N/A
1N/Astatic s64 ntfs_ibm_pos_to_vcn(ntfs_index_context *icx, s64 pos)
1N/A{
1N/A return ntfs_ib_pos_to_vcn(icx, pos * icx->block_size);
1N/A}
1N/A
1N/Astatic int ntfs_ibm_add(ntfs_index_context *icx)
1N/A{
1N/A u8 bmp[8];
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A if (ntfs_attr_exist(icx->ni, AT_BITMAP, icx->name, icx->name_len))
1N/A return STATUS_OK;
1N/A /*
1N/A * AT_BITMAP must be at least 8 bytes.
1N/A */
1N/A memset(bmp, 0, sizeof(bmp));
1N/A if (ntfs_attr_add(icx->ni, AT_BITMAP, icx->name, icx->name_len,
1N/A bmp, sizeof(bmp))) {
1N/A ntfs_log_perror("Failed to add AT_BITMAP");
1N/A return STATUS_ERROR;
1N/A }
1N/A return STATUS_OK;
1N/A}
1N/A
1N/Astatic int ntfs_ibm_modify(ntfs_index_context *icx, VCN vcn, int set)
1N/A{
1N/A u8 byte;
1N/A s64 pos = ntfs_ibm_vcn_to_pos(icx, vcn);
1N/A u32 bpos = pos / 8;
1N/A u32 bit = 1 << (pos % 8);
1N/A ntfs_attr *na;
1N/A int ret = STATUS_ERROR;
1N/A
1N/A ntfs_log_trace("%s vcn: %lld\n", set ? "set" : "clear", vcn);
1N/A
1N/A na = ntfs_attr_open(icx->ni, AT_BITMAP, icx->name, icx->name_len);
1N/A if (!na) {
1N/A ntfs_log_perror("Failed to open $BITMAP attribute");
1N/A return -1;
1N/A }
1N/A
1N/A if (set) {
1N/A if (na->data_size < bpos + 1) {
1N/A if (ntfs_attr_truncate(na, (na->data_size + 8) & ~7)) {
1N/A ntfs_log_perror("Failed to truncate AT_BITMAP");
1N/A goto err_na;
1N/A }
1N/A }
1N/A }
1N/A
1N/A if (ntfs_attr_pread(na, bpos, 1, &byte) != 1) {
1N/A ntfs_log_perror("Failed to read $BITMAP");
1N/A goto err_na;
1N/A }
1N/A
1N/A if (set)
1N/A byte |= bit;
1N/A else
1N/A byte &= ~bit;
1N/A
1N/A if (ntfs_attr_pwrite(na, bpos, 1, &byte) != 1) {
1N/A ntfs_log_perror("Failed to write $Bitmap");
1N/A goto err_na;
1N/A }
1N/A
1N/A ret = STATUS_OK;
1N/Aerr_na:
1N/A ntfs_attr_close(na);
1N/A return ret;
1N/A}
1N/A
1N/A
1N/Astatic int ntfs_ibm_set(ntfs_index_context *icx, VCN vcn)
1N/A{
1N/A return ntfs_ibm_modify(icx, vcn, 1);
1N/A}
1N/A
1N/Astatic int ntfs_ibm_clear(ntfs_index_context *icx, VCN vcn)
1N/A{
1N/A return ntfs_ibm_modify(icx, vcn, 0);
1N/A}
1N/A
1N/Astatic VCN ntfs_ibm_get_free(ntfs_index_context *icx)
1N/A{
1N/A u8 *bm;
1N/A int bit;
1N/A s64 vcn, byte, size;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A bm = ntfs_attr_readall(icx->ni, AT_BITMAP, icx->name, icx->name_len,
1N/A &size);
1N/A if (!bm)
1N/A return (VCN)-1;
1N/A
1N/A for (byte = 0; byte < size; byte++) {
1N/A
1N/A if (bm[byte] == 255)
1N/A continue;
1N/A
1N/A for (bit = 0; bit < 8; bit++) {
1N/A if (!(bm[byte] & (1 << bit))) {
1N/A vcn = ntfs_ibm_pos_to_vcn(icx, byte * 8 + bit);
1N/A goto out;
1N/A }
1N/A }
1N/A }
1N/A
1N/A vcn = ntfs_ibm_pos_to_vcn(icx, size * 8);
1N/Aout:
1N/A ntfs_log_trace("allocated vcn: %lld\n", vcn);
1N/A
1N/A if (ntfs_ibm_set(icx, vcn))
1N/A vcn = (VCN)-1;
1N/A
1N/A free(bm);
1N/A return vcn;
1N/A}
1N/A
1N/Astatic INDEX_BLOCK *ntfs_ir_to_ib(INDEX_ROOT *ir, VCN ib_vcn)
1N/A{
1N/A INDEX_BLOCK *ib;
1N/A INDEX_ENTRY *ie_last;
1N/A char *ies_start, *ies_end;
1N/A int i;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A if (!(ib = ntfs_ib_alloc(ib_vcn, le32_to_cpu(ir->index_block_size),
1N/A LEAF_NODE)))
1N/A return NULL;
1N/A
1N/A ies_start = (char *)ntfs_ie_get_first(&ir->index);
1N/A ies_end = (char *)ntfs_ie_get_end(&ir->index);
1N/A
1N/A ie_last = ntfs_ie_get_last((INDEX_ENTRY *)ies_start, ies_end);
1N/A /*
1N/A * Copy all entries, including the termination entry
1N/A * as well, which can never have any data.
1N/A */
1N/A i = (char *)ie_last - ies_start + le16_to_cpu(ie_last->length);
1N/A memcpy(ntfs_ie_get_first(&ib->index), ies_start, i);
1N/A
1N/A ib->index.flags = ir->index.flags;
1N/A ib->index.index_length = cpu_to_le32(i +
1N/A le32_to_cpu(ib->index.entries_offset));
1N/A return ib;
1N/A}
1N/A
1N/Astatic void ntfs_ir_nill(INDEX_ROOT *ir)
1N/A{
1N/A INDEX_ENTRY *ie_last;
1N/A char *ies_start, *ies_end;
1N/A
1N/A ntfs_log_trace("Entering\n");
1N/A /* TODO: This function could be much simpler. */
1N/A ies_start = (char *)ntfs_ie_get_first(&ir->index);
1N/A ies_end = (char *)ntfs_ie_get_end(&ir->index);
1N/A ie_last = ntfs_ie_get_last((INDEX_ENTRY *)ies_start, ies_end);
1N/A /* Move the index root termination entry forward. */
1N/A if ((char *)ie_last > ies_start) {
1N/A memmove(ies_start, (char *)ie_last, le16_to_cpu(
1N/A ie_last->length));
1N/A ie_last = (INDEX_ENTRY *)ies_start;
1N/A }
1N/A}
1N/A
1N/Astatic int ntfs_ib_copy_tail(ntfs_index_context *icx, INDEX_BLOCK *src,
1N/A INDEX_ENTRY *median, VCN new_vcn)
1N/A{
1N/A u8 *ies_end;
1N/A INDEX_ENTRY *ie_head; /* first entry after the median */
1N/A int tail_size, ret;
1N/A INDEX_BLOCK *dst;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A dst = ntfs_ib_alloc(new_vcn, icx->block_size,
1N/A src->index.flags & NODE_MASK);
1N/A if (!dst)
1N/A return STATUS_ERROR;
1N/A
1N/A ie_head = ntfs_ie_get_next(median);
1N/A
1N/A ies_end = (u8 *)ntfs_ie_get_end(&src->index);
1N/A tail_size = ies_end - (u8 *)ie_head;
1N/A memcpy(ntfs_ie_get_first(&dst->index), ie_head, tail_size);
1N/A
1N/A dst->index.index_length = cpu_to_le32(tail_size +
1N/A le32_to_cpu(dst->index.entries_offset));
1N/A
1N/A ret = ntfs_ib_write(icx, new_vcn, dst);
1N/A
1N/A free(dst);
1N/A return ret;
1N/A}
1N/A
1N/Astatic int ntfs_ib_cut_tail(ntfs_index_context *icx, INDEX_BLOCK *src,
1N/A INDEX_ENTRY *ie)
1N/A{
1N/A char *ies_start, *ies_end;
1N/A INDEX_ENTRY *ie_last;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A ies_start = (char *)ntfs_ie_get_first(&src->index);
1N/A ies_end = (char *)ntfs_ie_get_end(&src->index);
1N/A
1N/A ie_last = ntfs_ie_get_last((INDEX_ENTRY *)ies_start, ies_end);
1N/A if (ie_last->flags & INDEX_ENTRY_NODE)
1N/A ntfs_ie_set_vcn(ie_last, ntfs_ie_get_vcn(ie));
1N/A
1N/A memcpy(ie, ie_last, le16_to_cpu(ie_last->length));
1N/A
1N/A src->index.index_length = cpu_to_le32(((char *)ie - ies_start) +
1N/A le16_to_cpu(ie->length) +
1N/A le32_to_cpu(src->index.entries_offset));
1N/A
1N/A if (ntfs_ib_write(icx, icx->parent_vcn[icx->pindex + 1], src))
1N/A return STATUS_ERROR;
1N/A
1N/A return STATUS_OK;
1N/A}
1N/A
1N/Astatic int ntfs_ia_add(ntfs_index_context *icx)
1N/A{
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A if (ntfs_ibm_add(icx))
1N/A return -1;
1N/A
1N/A if (!ntfs_attr_exist(icx->ni, AT_INDEX_ALLOCATION, icx->name,
1N/A icx->name_len)) {
1N/A if (ntfs_attr_add(icx->ni, AT_INDEX_ALLOCATION, icx->name,
1N/A icx->name_len, NULL, 0)) {
1N/A ntfs_log_perror("Failed to add AT_INDEX_ALLOCATION");
1N/A return -1;
1N/A }
1N/A }
1N/A
1N/A icx->ia_na = ntfs_ia_open(icx, icx->ni);
1N/A if (!icx->ia_na)
1N/A return -1;
1N/A return 0;
1N/A}
1N/A
1N/Astatic INDEX_ROOT *ntfs_ir_lookup2(ntfs_inode *ni, ntfschar *name, u32 len)
1N/A{
1N/A ntfs_attr_search_ctx *ctx;
1N/A INDEX_ROOT *ir;
1N/A
1N/A ir = ntfs_ir_lookup(ni, name, len, &ctx);
1N/A if (ir)
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A return ir;
1N/A}
1N/A
1N/Astatic int ntfs_ir_reparent(ntfs_index_context *icx)
1N/A{
1N/A ntfs_attr_search_ctx *ctx;
1N/A INDEX_ROOT *ir;
1N/A INDEX_ENTRY *ie;
1N/A INDEX_BLOCK *ib = NULL;
1N/A VCN new_ib_vcn;
1N/A int ret = STATUS_ERROR;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A if (!(icx->ia_na))
1N/A if (ntfs_ia_add(icx))
1N/A return -1;
1N/A
1N/A ir = ntfs_ir_lookup(icx->ni, icx->name, icx->name_len, &ctx);
1N/A if (!ir)
1N/A return -1;
1N/A
1N/A new_ib_vcn = ntfs_ibm_get_free(icx);
1N/A if (new_ib_vcn == -1)
1N/A goto err_out;
1N/A
1N/A ib = ntfs_ir_to_ib(ir, new_ib_vcn);
1N/A if (ib == NULL) {
1N/A ntfs_log_perror("Failed to move index root to index block");
1N/A goto clear_bmp;
1N/A }
1N/A
1N/A if (ntfs_ib_write(icx, new_ib_vcn, ib))
1N/A goto clear_bmp;
1N/A
1N/A ntfs_ir_nill(ir);
1N/A
1N/A ie = ntfs_ie_get_first(&ir->index);
1N/A ie->flags |= INDEX_ENTRY_NODE;
1N/A ie->length = cpu_to_le16(sizeof(INDEX_ENTRY_HEADER) + sizeof(VCN));
1N/A ntfs_ie_set_vcn(ie, new_ib_vcn);
1N/A
1N/A ir->index.flags = LARGE_INDEX;
1N/A ir->index.index_length = cpu_to_le32(le32_to_cpu(
1N/A ir->index.entries_offset) + le16_to_cpu(ie->length));
1N/A ir->index.allocated_size = ir->index.index_length;
1N/A
1N/A if (ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr,
1N/A sizeof(INDEX_ROOT) - sizeof(INDEX_HEADER) +
1N/A le32_to_cpu(ir->index.allocated_size)))
1N/A /* FIXME: revert bitmap, index root */
1N/A goto err_out;
1N/A ntfs_inode_mark_dirty(ctx->ntfs_ino);
1N/A
1N/A ret = STATUS_OK;
1N/Aerr_out:
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A free(ib);
1N/A return ret;
1N/Aclear_bmp:
1N/A ntfs_ibm_clear(icx, new_ib_vcn);
1N/A goto err_out;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_ir_truncate - Truncate index root attribute
1N/A *
1N/A * Returns STATUS_OK, STATUS_RESIDENT_ATTRIBUTE_FILLED_MFT or STATUS_ERROR.
1N/A */
1N/Astatic int ntfs_ir_truncate(ntfs_index_context *icx, int data_size)
1N/A{
1N/A ntfs_attr *na;
1N/A int ret;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A na = ntfs_attr_open(icx->ni, AT_INDEX_ROOT, icx->name, icx->name_len);
1N/A if (!na) {
1N/A ntfs_log_perror("Failed to open INDEX_ROOT");
1N/A return STATUS_ERROR;
1N/A }
1N/A /*
1N/A * INDEX_ROOT must be resident and its entries can be moved to
1N/A * INDEX_BLOCK, so ENOSPC isn't a real error.
1N/A */
1N/A ret = ntfs_attr_truncate(na, data_size + offsetof(INDEX_ROOT, index));
1N/A if (ret == STATUS_OK) {
1N/A icx->ir = ntfs_ir_lookup2(icx->ni, icx->name, icx->name_len);
1N/A if (!icx->ir)
1N/A return STATUS_ERROR;
1N/A
1N/A icx->ir->index.allocated_size = cpu_to_le32(data_size);
1N/A } else {
1N/A if (errno != ENOSPC && errno != EOVERFLOW)
1N/A ntfs_log_trace("Failed to truncate INDEX_ROOT");
1N/A if (errno == EOVERFLOW)
1N/A ret = STATUS_RESIDENT_ATTRIBUTE_FILLED_MFT;
1N/A }
1N/A
1N/A ntfs_attr_close(na);
1N/A return ret;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_ir_make_space - Make more space for the index root attribute
1N/A *
1N/A * On success return STATUS_OK or STATUS_KEEP_SEARCHING.
1N/A * On error return STATUS_ERROR.
1N/A */
1N/Astatic int ntfs_ir_make_space(ntfs_index_context *icx, int data_size)
1N/A{
1N/A int ret;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A ret = ntfs_ir_truncate(icx, data_size);
1N/A if (ret == STATUS_RESIDENT_ATTRIBUTE_FILLED_MFT) {
1N/A ret = ntfs_ir_reparent(icx);
1N/A if (ret == STATUS_OK)
1N/A ret = STATUS_KEEP_SEARCHING;
1N/A else
1N/A ntfs_log_perror("Failed to nodify INDEX_ROOT");
1N/A }
1N/A return ret;
1N/A}
1N/A
1N/A/*
1N/A * NOTE: 'ie' must be a copy of a real index entry.
1N/A */
1N/Astatic int ntfs_ie_add_vcn(INDEX_ENTRY **ie)
1N/A{
1N/A INDEX_ENTRY *p, *old = *ie;
1N/A
1N/A old->length = cpu_to_le16(le16_to_cpu(old->length) + sizeof(VCN));
1N/A p = realloc(old, le16_to_cpu(old->length));
1N/A if (!p)
1N/A return STATUS_ERROR;
1N/A
1N/A p->flags |= INDEX_ENTRY_NODE;
1N/A *ie = p;
1N/A return STATUS_OK;
1N/A}
1N/A
1N/Astatic int ntfs_ih_insert(INDEX_HEADER *ih, INDEX_ENTRY *orig_ie, VCN new_vcn,
1N/A int pos)
1N/A{
1N/A INDEX_ENTRY *ie_node, *ie;
1N/A int ret = STATUS_ERROR;
1N/A VCN old_vcn;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A ie = ntfs_ie_dup(orig_ie);
1N/A if (!ie)
1N/A return STATUS_ERROR;
1N/A
1N/A if (!(ie->flags & INDEX_ENTRY_NODE))
1N/A if (ntfs_ie_add_vcn(&ie))
1N/A goto out;
1N/A
1N/A ie_node = ntfs_ie_get_by_pos(ih, pos);
1N/A old_vcn = ntfs_ie_get_vcn(ie_node);
1N/A ntfs_ie_set_vcn(ie_node, new_vcn);
1N/A
1N/A ntfs_ie_insert(ih, ie, ie_node);
1N/A ntfs_ie_set_vcn(ie_node, old_vcn);
1N/A ret = STATUS_OK;
1N/Aout:
1N/A free(ie);
1N/A return ret;
1N/A}
1N/A
1N/Astatic VCN ntfs_icx_parent_vcn(ntfs_index_context *icx)
1N/A{
1N/A return icx->parent_vcn[icx->pindex];
1N/A}
1N/A
1N/Astatic VCN ntfs_icx_parent_pos(ntfs_index_context *icx)
1N/A{
1N/A return icx->parent_pos[icx->pindex];
1N/A}
1N/A
1N/Astatic int ntfs_ir_insert_median(ntfs_index_context *icx, INDEX_ENTRY *median,
1N/A VCN new_vcn)
1N/A{
1N/A u32 new_size;
1N/A int ret;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A icx->ir = ntfs_ir_lookup2(icx->ni, icx->name, icx->name_len);
1N/A if (!icx->ir)
1N/A return STATUS_ERROR;
1N/A
1N/A new_size = le32_to_cpu(icx->ir->index.index_length) +
1N/A le16_to_cpu(median->length);
1N/A if (!(median->flags & INDEX_ENTRY_NODE))
1N/A new_size += sizeof(VCN);
1N/A
1N/A ret = ntfs_ir_make_space(icx, new_size);
1N/A if (ret != STATUS_OK)
1N/A return ret;
1N/A
1N/A icx->ir = ntfs_ir_lookup2(icx->ni, icx->name, icx->name_len);
1N/A if (!icx->ir)
1N/A return STATUS_ERROR;
1N/A
1N/A return ntfs_ih_insert(&icx->ir->index, median, new_vcn,
1N/A ntfs_icx_parent_pos(icx));
1N/A}
1N/A
1N/Astatic int ntfs_ib_split(ntfs_index_context *icx, INDEX_BLOCK *ib);
1N/A
1N/A/**
1N/A * ntfs_ib_insert - insert an index block to an index context.
1N/A *
1N/A * On success return STATUS_OK or STATUS_KEEP_SEARCHING.
1N/A * On error return STATUS_ERROR.
1N/A */
1N/Astatic int ntfs_ib_insert(ntfs_index_context *icx, INDEX_ENTRY *ie, VCN new_vcn)
1N/A{
1N/A INDEX_BLOCK *ib;
1N/A u32 idx_size, allocated_size;
1N/A int err = STATUS_ERROR;
1N/A VCN old_vcn;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A ib = ntfs_malloc(icx->block_size);
1N/A if (!ib)
1N/A return -1;
1N/A
1N/A old_vcn = ntfs_icx_parent_vcn(icx);
1N/A
1N/A if (ntfs_ib_read(icx, old_vcn, ib))
1N/A goto err_out;
1N/A
1N/A idx_size = le32_to_cpu(ib->index.index_length);
1N/A allocated_size = le32_to_cpu(ib->index.allocated_size);
1N/A /* FIXME: sizeof(VCN) should be included only if ie has no VCN */
1N/A if (idx_size + le16_to_cpu(ie->length) + sizeof(VCN) > allocated_size) {
1N/A err = ntfs_ib_split(icx, ib);
1N/A if (err == STATUS_OK)
1N/A err = STATUS_KEEP_SEARCHING;
1N/A goto err_out;
1N/A }
1N/A
1N/A if (ntfs_ih_insert(&ib->index, ie, new_vcn, ntfs_icx_parent_pos(icx)))
1N/A goto err_out;
1N/A
1N/A if (ntfs_ib_write(icx, old_vcn, ib))
1N/A goto err_out;
1N/A
1N/A err = STATUS_OK;
1N/Aerr_out:
1N/A free(ib);
1N/A return err;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_ib_split - Split index allocation attribute
1N/A *
1N/A * On success return STATUS_OK or STATUS_KEEP_SEARCHING.
1N/A * On error return is STATUS_ERROR.
1N/A */
1N/Astatic int ntfs_ib_split(ntfs_index_context *icx, INDEX_BLOCK *ib)
1N/A{
1N/A INDEX_ENTRY *median;
1N/A VCN new_vcn;
1N/A int ret;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A if (ntfs_icx_parent_dec(icx))
1N/A return STATUS_ERROR;
1N/A
1N/A median = ntfs_ie_get_median(&ib->index);
1N/A new_vcn = ntfs_ibm_get_free(icx);
1N/A if (new_vcn == -1)
1N/A return STATUS_ERROR;
1N/A
1N/A if (ntfs_ib_copy_tail(icx, ib, median, new_vcn)) {
1N/A ntfs_ibm_clear(icx, new_vcn);
1N/A return STATUS_ERROR;
1N/A }
1N/A
1N/A if (ntfs_icx_parent_vcn(icx) == VCN_INDEX_ROOT_PARENT)
1N/A ret = ntfs_ir_insert_median(icx, median, new_vcn);
1N/A else
1N/A ret = ntfs_ib_insert(icx, median, new_vcn);
1N/A
1N/A ntfs_inode_mark_dirty(icx->actx->ntfs_ino);
1N/A
1N/A if (ret != STATUS_OK) {
1N/A ntfs_ibm_clear(icx, new_vcn);
1N/A return ret;
1N/A }
1N/A
1N/A ret = ntfs_ib_cut_tail(icx, ib, median);
1N/A return ret;
1N/A}
1N/A
1N/Astatic int ntfs_ie_add(ntfs_index_context *icx, INDEX_ENTRY *ie)
1N/A{
1N/A INDEX_HEADER *ih;
1N/A int allocated_size, new_size;
1N/A int ret = STATUS_ERROR;
1N/A
1N/A#ifdef DEBUG
1N/A char *fn;
1N/A fn = ntfs_ie_filename_get(ie);
1N/A ntfs_log_trace("file: '%s'\n", fn);
1N/A free(fn);
1N/A#endif
1N/A
1N/A while (1) {
1N/A if (!ntfs_index_lookup(&ie->key, le16_to_cpu(ie->key_length),
1N/A icx)) {
1N/A errno = EEXIST;
1N/A ntfs_log_error("Index already have such entry.\n");
1N/A goto err_out;
1N/A }
1N/A if (errno != ENOENT) {
1N/A ntfs_log_perror("Failed to find place for new entry");
1N/A goto err_out;
1N/A }
1N/A
1N/A if (icx->is_in_root)
1N/A ih = &icx->ir->index;
1N/A else
1N/A ih = &icx->ib->index;
1N/A
1N/A allocated_size = le32_to_cpu(ih->allocated_size);
1N/A new_size = le32_to_cpu(ih->index_length) +
1N/A le16_to_cpu(ie->length);
1N/A
1N/A if (new_size <= allocated_size)
1N/A break;
1N/A
1N/A ntfs_log_trace("index block sizes: allocated: %d needed: %d\n",
1N/A allocated_size, new_size);
1N/A
1N/A if (icx->is_in_root) {
1N/A if (ntfs_ir_make_space(icx, new_size) == STATUS_ERROR)
1N/A goto err_out;
1N/A } else {
1N/A if (ntfs_ib_split(icx, icx->ib) == STATUS_ERROR)
1N/A goto err_out;
1N/A }
1N/A ntfs_inode_mark_dirty(icx->actx->ntfs_ino);
1N/A ntfs_index_ctx_reinit(icx);
1N/A }
1N/A
1N/A ntfs_ie_insert(ih, ie, icx->entry);
1N/A ntfs_index_entry_mark_dirty(icx);
1N/A
1N/A ret = STATUS_OK;
1N/Aerr_out:
1N/A ntfs_log_trace("%s\n", ret ? "Failed" : "Done");
1N/A return ret;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_index_add_filename - add filename to directory index
1N/A * @ni: ntfs inode describing directory to which index add filename
1N/A * @fn: FILE_NAME attribute to add
1N/A * @mref: reference of the inode which @fn describes
1N/A *
1N/A * Return 0 on success or -1 on error with errno set to the error code.
1N/A */
1N/Aint ntfs_index_add_filename(ntfs_inode *ni, FILE_NAME_ATTR *fn, MFT_REF mref)
1N/A{
1N/A INDEX_ENTRY *ie;
1N/A ntfs_index_context *icx;
1N/A int fn_size, ie_size, ret = -1, err;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A if (!ni || !fn) {
1N/A ntfs_log_error("Invalid arguments.\n");
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A
1N/A fn_size = (fn->file_name_length * sizeof(ntfschar)) +
1N/A sizeof(FILE_NAME_ATTR);
1N/A ie_size = (sizeof(INDEX_ENTRY_HEADER) + fn_size + 7) & ~7;
1N/A
1N/A ie = ntfs_calloc(ie_size);
1N/A if (!ie)
1N/A return -1;
1N/A
1N/A ie->u.indexed_file = cpu_to_le64(mref);
1N/A ie->length = cpu_to_le16(ie_size);
1N/A ie->key_length = cpu_to_le16(fn_size);
1N/A memcpy(&ie->key, fn, fn_size);
1N/A
1N/A icx = ntfs_index_ctx_get(ni, NTFS_INDEX_I30, 4);
1N/A if (!icx)
1N/A goto out;
1N/A
1N/A err = errno;
1N/A ret = ntfs_ie_add(icx, ie);
1N/A errno = err;
1N/A
1N/A ntfs_index_ctx_put(icx);
1N/Aout:
1N/A free(ie);
1N/A return ret;
1N/A}
1N/A
1N/Astatic int ntfs_ih_takeout(ntfs_index_context *icx, INDEX_HEADER *ih,
1N/A INDEX_ENTRY *ie, INDEX_BLOCK *ib)
1N/A{
1N/A INDEX_ENTRY *ie_roam;
1N/A int ret = STATUS_ERROR;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A ie_roam = ntfs_ie_dup_novcn(ie);
1N/A if (!ie_roam)
1N/A return STATUS_ERROR;
1N/A
1N/A ntfs_ie_delete(ih, ie);
1N/A
1N/A if (ntfs_icx_parent_vcn(icx) == VCN_INDEX_ROOT_PARENT)
1N/A ntfs_inode_mark_dirty(icx->actx->ntfs_ino);
1N/A else
1N/A if (ntfs_ib_write(icx, ntfs_icx_parent_vcn(icx), ib))
1N/A goto out;
1N/A
1N/A ntfs_index_ctx_reinit(icx);
1N/A
1N/A ret = ntfs_ie_add(icx, ie_roam);
1N/Aout:
1N/A free(ie_roam);
1N/A return ret;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_ir_leafify -
1N/A *
1N/A * Used if an empty index block to be deleted has END entry as the parent
1N/A * in the INDEX_ROOT which is the only one there.
1N/A */
1N/Astatic void ntfs_ir_leafify(ntfs_index_context *icx, INDEX_HEADER *ih)
1N/A{
1N/A INDEX_ENTRY *ie;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A ie = ntfs_ie_get_first(ih);
1N/A ie->flags &= ~INDEX_ENTRY_NODE;
1N/A ie->length = cpu_to_le16(le16_to_cpu(ie->length) - sizeof(VCN));
1N/A
1N/A ih->index_length = cpu_to_le32(le32_to_cpu(ih->index_length) -
1N/A sizeof(VCN));
1N/A ih->flags &= ~LARGE_INDEX;
1N/A
1N/A /* Not fatal error */
1N/A ntfs_ir_truncate(icx, le32_to_cpu(ih->index_length));
1N/A
1N/A ntfs_inode_mark_dirty(icx->actx->ntfs_ino);
1N/A ntfs_index_ctx_reinit(icx);
1N/A}
1N/A
1N/A/**
1N/A * ntfs_ih_reparent_end -
1N/A *
1N/A * Used if an empty index block to be deleted has END entry as the parent
1N/A * in the INDEX_ROOT which is not the only one there.
1N/A */
1N/Astatic int ntfs_ih_reparent_end(ntfs_index_context *icx, INDEX_HEADER *ih,
1N/A INDEX_BLOCK *ib)
1N/A{
1N/A INDEX_ENTRY *ie, *ie_prev;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A ie = ntfs_ie_get_by_pos(ih, ntfs_icx_parent_pos(icx));
1N/A ie_prev = ntfs_ie_prev(ih, ie);
1N/A
1N/A ntfs_ie_set_vcn(ie, ntfs_ie_get_vcn(ie_prev));
1N/A return ntfs_ih_takeout(icx, ih, ie_prev, ib);
1N/A}
1N/A
1N/Astatic int ntfs_index_rm_leaf(ntfs_index_context *icx)
1N/A{
1N/A INDEX_BLOCK *ib = NULL;
1N/A INDEX_HEADER *parent_ih;
1N/A INDEX_ENTRY *ie;
1N/A int ret = STATUS_ERROR;
1N/A
1N/A ntfs_log_trace("pindex: %d\n", icx->pindex);
1N/A
1N/A if (ntfs_icx_parent_dec(icx))
1N/A return STATUS_ERROR;
1N/A
1N/A if (ntfs_ibm_clear(icx, icx->parent_vcn[icx->pindex + 1]))
1N/A return STATUS_ERROR;
1N/A
1N/A if (ntfs_icx_parent_vcn(icx) == VCN_INDEX_ROOT_PARENT)
1N/A parent_ih = &icx->ir->index;
1N/A else {
1N/A ib = ntfs_malloc(icx->block_size);
1N/A if (!ib)
1N/A return STATUS_ERROR;
1N/A
1N/A if (ntfs_ib_read(icx, ntfs_icx_parent_vcn(icx), ib))
1N/A goto out;
1N/A
1N/A parent_ih = &ib->index;
1N/A }
1N/A
1N/A ie = ntfs_ie_get_by_pos(parent_ih, ntfs_icx_parent_pos(icx));
1N/A if (!ntfs_ie_end(ie)) {
1N/A ret = ntfs_ih_takeout(icx, parent_ih, ie, ib);
1N/A goto out;
1N/A }
1N/A
1N/A if (ntfs_ih_zero_entry(parent_ih)) {
1N/A
1N/A if (ntfs_icx_parent_vcn(icx) == VCN_INDEX_ROOT_PARENT) {
1N/A ntfs_ir_leafify(icx, parent_ih);
1N/A goto ok;
1N/A }
1N/A
1N/A ret = ntfs_index_rm_leaf(icx);
1N/A goto out;
1N/A }
1N/A
1N/A if (ntfs_ih_reparent_end(icx, parent_ih, ib))
1N/A goto out;
1N/Aok:
1N/A ret = STATUS_OK;
1N/Aout:
1N/A free(ib);
1N/A return ret;
1N/A}
1N/A
1N/Astatic int ntfs_index_rm_node(ntfs_index_context *icx)
1N/A{
1N/A int entry_pos;
1N/A VCN vcn;
1N/A INDEX_BLOCK *ib = NULL;
1N/A INDEX_ENTRY *ie_succ, *ie, *entry = icx->entry;
1N/A INDEX_HEADER *ih;
1N/A u32 new_size;
1N/A int delta, ret = STATUS_ERROR;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A if (!icx->ia_na) {
1N/A icx->ia_na = ntfs_ia_open(icx, icx->ni);
1N/A if (!icx->ia_na)
1N/A return STATUS_ERROR;
1N/A }
1N/A
1N/A ib = ntfs_malloc(icx->block_size);
1N/A if (!ib)
1N/A return STATUS_ERROR;
1N/A
1N/A ie_succ = ntfs_ie_get_next(icx->entry);
1N/A entry_pos = icx->parent_pos[icx->pindex]++;
1N/Adescend:
1N/A vcn = ntfs_ie_get_vcn(ie_succ);
1N/A if (ntfs_ib_read(icx, vcn, ib))
1N/A goto out;
1N/A
1N/A ie_succ = ntfs_ie_get_first(&ib->index);
1N/A
1N/A if (ntfs_icx_parent_inc(icx))
1N/A goto out;
1N/A
1N/A icx->parent_vcn[icx->pindex] = vcn;
1N/A icx->parent_pos[icx->pindex] = 0;
1N/A
1N/A if ((ib->index.flags & NODE_MASK) == INDEX_NODE)
1N/A goto descend;
1N/A
1N/A if (ntfs_ih_zero_entry(&ib->index)) {
1N/A errno = EOPNOTSUPP;
1N/A ntfs_log_perror("Failed to find any entry in an index block. "
1N/A "Please run chkdsk.");
1N/A goto out;
1N/A }
1N/A
1N/A ie = ntfs_ie_dup(ie_succ);
1N/A if (!ie)
1N/A goto out;
1N/A
1N/A if (ntfs_ie_add_vcn(&ie))
1N/A goto out2;
1N/A
1N/A ntfs_ie_set_vcn(ie, ntfs_ie_get_vcn(icx->entry));
1N/A
1N/A if (icx->is_in_root)
1N/A ih = &icx->ir->index;
1N/A else
1N/A ih = &icx->ib->index;
1N/A
1N/A delta = le16_to_cpu(ie->length) - le16_to_cpu(icx->entry->length);
1N/A new_size = le32_to_cpu(ih->index_length) + delta;
1N/A if (delta > 0) {
1N/A if (icx->is_in_root) {
1N/A if (ntfs_ir_truncate(icx, new_size)) {
1N/A errno = EOPNOTSUPP;
1N/A ntfs_log_perror("Denied to truncate INDEX_ROOT"
1N/A " during entry removal");
1N/A goto out2;
1N/A }
1N/A ih = &icx->ir->index;
1N/A entry = ntfs_ie_get_by_pos(ih, entry_pos);
1N/A } else if (new_size > le32_to_cpu(ih->allocated_size)) {
1N/A errno = EOPNOTSUPP;
1N/A ntfs_log_perror("Denied to split INDEX_BLOCK during "
1N/A "entry removal");
1N/A goto out2;
1N/A }
1N/A }
1N/A
1N/A ntfs_ie_delete(ih, entry);
1N/A ntfs_ie_insert(ih, ie, entry);
1N/A
1N/A if (icx->is_in_root) {
1N/A if (ntfs_ir_truncate(icx, new_size))
1N/A goto out2;
1N/A ntfs_inode_mark_dirty(icx->actx->ntfs_ino);
1N/A } else
1N/A if (ntfs_icx_ib_write(icx))
1N/A goto out2;
1N/A
1N/A ntfs_ie_delete(&ib->index, ie_succ);
1N/A
1N/A if (ntfs_ih_zero_entry(&ib->index)) {
1N/A if (ntfs_index_rm_leaf(icx))
1N/A goto out2;
1N/A } else
1N/A if (ntfs_ib_write(icx, vcn, ib))
1N/A goto out2;
1N/A
1N/A ret = STATUS_OK;
1N/Aout2:
1N/A free(ie);
1N/Aout:
1N/A free(ib);
1N/A return ret;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_index_rm - remove entry from the index
1N/A * @icx: index context describing entry to delete
1N/A *
1N/A * Delete entry described by @icx from the index. Index context is always
1N/A * reinitialized after use of this function, so it can be used for index
1N/A * lookup once again.
1N/A *
1N/A * Return 0 on success or -1 on error with errno set to the error code.
1N/A */
1N/Aint ntfs_index_rm(ntfs_index_context *icx)
1N/A{
1N/A INDEX_HEADER *ih;
1N/A int err;
1N/A
1N/A ntfs_log_trace("Entering.\n");
1N/A
1N/A if (!icx || (!icx->ib && !icx->ir) || ntfs_ie_end(icx->entry)) {
1N/A ntfs_log_error("Invalid arguments.\n");
1N/A errno = EINVAL;
1N/A goto err_out;
1N/A }
1N/A if (icx->is_in_root)
1N/A ih = &icx->ir->index;
1N/A else
1N/A ih = &icx->ib->index;
1N/A
1N/A if (icx->entry->flags & INDEX_ENTRY_NODE) {
1N/A
1N/A if (ntfs_index_rm_node(icx))
1N/A goto err_out;
1N/A
1N/A } else if (icx->is_in_root || !ntfs_ih_one_entry(ih)) {
1N/A
1N/A ntfs_ie_delete(ih, icx->entry);
1N/A
1N/A if (icx->is_in_root) {
1N/A err = ntfs_ir_truncate(icx,
1N/A le32_to_cpu(ih->index_length));
1N/A if (err != STATUS_OK)
1N/A goto err_out;
1N/A } else
1N/A if (ntfs_icx_ib_write(icx))
1N/A goto err_out;
1N/A } else {
1N/A if (ntfs_index_rm_leaf(icx))
1N/A goto err_out;
1N/A }
1N/A
1N/A ntfs_index_ctx_reinit(icx);
1N/A ntfs_log_trace("Done.\n");
1N/A return 0;
1N/Aerr_out:
1N/A err = errno;
1N/A ntfs_index_ctx_reinit(icx);
1N/A errno = err;
1N/A ntfs_log_trace("Failed.\n");
1N/A return -1;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_index_root_get - read the index root of an attribute
1N/A * @ni: open ntfs inode in which the ntfs attribute resides
1N/A * @attr: attribute for which we want its index root
1N/A *
1N/A * This function will read the related index root an ntfs attribute.
1N/A *
1N/A * On success a buffer is allocated with the content of the index root
1N/A * and which needs to be freed when it's not needed anymore.
1N/A *
1N/A * On error NULL is returned with errno set to the error code.
1N/A */
1N/AINDEX_ROOT *ntfs_index_root_get(ntfs_inode *ni, ATTR_RECORD *attr)
1N/A{
1N/A ntfs_attr_search_ctx *ctx;
1N/A ntfschar *name;
1N/A INDEX_ROOT *root = NULL;
1N/A
1N/A name = (ntfschar *)((u8 *)attr + le16_to_cpu(attr->name_offset));
1N/A
1N/A if (!ntfs_ir_lookup(ni, name, attr->name_length, &ctx))
1N/A return NULL;
1N/A
1N/A root = ntfs_malloc(sizeof(INDEX_ROOT));
1N/A if (!root)
1N/A goto out;
1N/A
1N/A *root = *((INDEX_ROOT *)((u8 *)ctx->attr +
1N/A le16_to_cpu(ctx->attr->u.res.value_offset)));
1N/Aout:
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A return root;
1N/A}
1N/A