fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / ntfs / index.c
index b19a488..2194eff 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * index.c - NTFS kernel index handling.  Part of the Linux-NTFS project.
  *
- * Copyright (c) 2004 Anton Altaparmakov
+ * Copyright (c) 2004-2005 Anton Altaparmakov
  *
  * This program/include file is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as published
  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-#include "ntfs.h"
+#include "aops.h"
 #include "collate.h"
+#include "debug.h"
 #include "index.h"
+#include "ntfs.h"
 
 /**
  * ntfs_index_ctx_get - allocate and initialize a new index context
  * Allocate a new index context, initialize it with @idx_ni and return it.
  * Return NULL if allocation failed.
  *
- * Locking:  Caller must hold i_sem on the index inode.
+ * Locking:  Caller must hold i_mutex on the index inode.
  */
 ntfs_index_context *ntfs_index_ctx_get(ntfs_inode *idx_ni)
 {
        ntfs_index_context *ictx;
 
-       ictx = kmem_cache_alloc(ntfs_index_ctx_cache, SLAB_NOFS);
-       if (ictx) {
-               ictx->idx_ni = idx_ni;
-               ictx->entry = NULL;
-               ictx->data = NULL;
-               ictx->data_len = 0;
-               ictx->is_in_root = 0;
-               ictx->ir = NULL;
-               ictx->actx = NULL;
-               ictx->base_ni = NULL;
-               ictx->ia = NULL;
-               ictx->page = NULL;
-       }
+       ictx = kmem_cache_alloc(ntfs_index_ctx_cache, GFP_NOFS);
+       if (ictx)
+               *ictx = (ntfs_index_context){ .idx_ni = idx_ni };
        return ictx;
 }
 
@@ -58,7 +50,7 @@ ntfs_index_context *ntfs_index_ctx_get(ntfs_inode *idx_ni)
  *
  * Release the index context @ictx, releasing all associated resources.
  *
- * Locking:  Caller must hold i_sem on the index inode.
+ * Locking:  Caller must hold i_mutex on the index inode.
  */
 void ntfs_index_ctx_put(ntfs_index_context *ictx)
 {
@@ -114,7 +106,7 @@ void ntfs_index_ctx_put(ntfs_index_context *ictx)
  * or ntfs_index_entry_write() before the call to ntfs_index_ctx_put() to
  * ensure that the changes are written to disk.
  *
- * Locking:  - Caller must hold i_sem on the index inode.
+ * Locking:  - Caller must hold i_mutex on the index inode.
  *          - Each page cache page in the index allocation mapping must be
  *            locked whilst being accessed otherwise we may find a corrupt
  *            page due to it being under ->writepage at the moment which
@@ -212,7 +204,8 @@ int ntfs_index_lookup(const void *key, const int key_len,
                if ((key_len == le16_to_cpu(ie->key_length)) && !memcmp(key,
                                &ie->key, key_len)) {
 ir_done:
-                       ictx->is_in_root = TRUE;
+                       ictx->is_in_root = true;
+                       ictx->ir = ir;
                        ictx->actx = actx;
                        ictx->base_ni = base_ni;
                        ictx->ia = NULL;
@@ -261,7 +254,6 @@ done:
                ntfs_error(sb, "No index allocation attribute but index entry "
                                "requires one.  Inode 0x%lx is corrupt or "
                                "driver bug.", idx_ni->mft_no);
-               err = -EIO;
                goto err_out;
        }
        /* Get the starting vcn of the index_block holding the child node. */
@@ -299,7 +291,13 @@ fast_descend_into_child_node:
        if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE) {
                ntfs_error(sb, "Out of bounds check failed.  Corrupt inode "
                                "0x%lx or driver bug.", idx_ni->mft_no);
-               err = -EIO;
+               goto unm_err_out;
+       }
+       /* Catch multi sector transfer fixup errors. */
+       if (unlikely(!ntfs_is_indx_record(ia->magic))) {
+               ntfs_error(sb, "Index record with vcn 0x%llx is corrupt.  "
+                               "Corrupt inode 0x%lx.  Run chkdsk.",
+                               (long long)vcn, idx_ni->mft_no);
                goto unm_err_out;
        }
        if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
@@ -309,7 +307,6 @@ fast_descend_into_child_node:
                                (unsigned long long)
                                sle64_to_cpu(ia->index_block_vcn),
                                (unsigned long long)vcn, idx_ni->mft_no);
-               err = -EIO;
                goto unm_err_out;
        }
        if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
@@ -321,7 +318,6 @@ fast_descend_into_child_node:
                                idx_ni->mft_no,
                                le32_to_cpu(ia->index.allocated_size) + 0x18,
                                idx_ni->itype.index.block_size);
-               err = -EIO;
                goto unm_err_out;
        }
        index_end = (u8*)ia + idx_ni->itype.index.block_size;
@@ -331,7 +327,6 @@ fast_descend_into_child_node:
                                "access!  This is probably a bug in the "
                                "driver.", (unsigned long long)vcn,
                                idx_ni->mft_no);
-               err = -EIO;
                goto unm_err_out;
        }
        index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
@@ -339,7 +334,6 @@ fast_descend_into_child_node:
                ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of inode "
                                "0x%lx exceeds maximum size.",
                                (unsigned long long)vcn, idx_ni->mft_no);
-               err = -EIO;
                goto unm_err_out;
        }
        /* The first index entry. */
@@ -357,11 +351,10 @@ fast_descend_into_child_node:
                                (u8*)ie + le16_to_cpu(ie->length) > index_end) {
                        ntfs_error(sb, "Index entry out of bounds in inode "
                                        "0x%lx.", idx_ni->mft_no);
-                       err = -EIO;
                        goto unm_err_out;
                }
                /*
-                * The last entry cannot contain a ket.  It can however contain
+                * The last entry cannot contain a key.  It can however contain
                 * a pointer to a child node in the B+tree so we just break out.
                 */
                if (ie->flags & INDEX_ENTRY_END)
@@ -375,14 +368,13 @@ fast_descend_into_child_node:
                                le16_to_cpu(ie->length)) {
                        ntfs_error(sb, "Index entry out of bounds in inode "
                                        "0x%lx.", idx_ni->mft_no);
-                       err = -EIO;
                        goto unm_err_out;
                }
                /* If the keys match perfectly, we setup @ictx and return 0. */
                if ((key_len == le16_to_cpu(ie->key_length)) && !memcmp(key,
                                &ie->key, key_len)) {
 ia_done:
-                       ictx->is_in_root = FALSE;
+                       ictx->is_in_root = false;
                        ictx->actx = NULL;
                        ictx->base_ni = NULL;
                        ictx->ia = ia;
@@ -422,7 +414,6 @@ ia_done:
        if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
                ntfs_error(sb, "Index entry with child node found in a leaf "
                                "node in inode 0x%lx.", idx_ni->mft_no);
-               err = -EIO;
                goto unm_err_out;
        }
        /* Child node present, descend into it. */
@@ -444,11 +435,12 @@ ia_done:
        }
        ntfs_error(sb, "Negative child node vcn in inode 0x%lx.",
                        idx_ni->mft_no);
-       err = -EIO;
 unm_err_out:
        unlock_page(page);
        ntfs_unmap_page(page);
 err_out:
+       if (!err)
+               err = -EIO;
        if (actx)
                ntfs_attr_put_search_ctx(actx);
        if (m)
@@ -456,61 +448,5 @@ err_out:
        return err;
 idx_err_out:
        ntfs_error(sb, "Corrupt index.  Aborting lookup.");
-       err = -EIO;
        goto err_out;
 }
-
-#ifdef NTFS_RW
-
-/**
- * __ntfs_index_entry_mark_dirty - mark an index allocation entry dirty
- * @ictx:      ntfs index context describing the index entry
- *
- * NOTE: You want to use fs/ntfs/index.h::ntfs_index_entry_mark_dirty() instead!
- * 
- * Mark the index allocation entry described by the index entry context @ictx
- * dirty.
- *
- * The index entry must be in an index block belonging to the index allocation
- * attribute.  Mark the buffers belonging to the index record as well as the
- * page cache page the index block is in dirty.  This automatically marks the
- * VFS inode of the ntfs index inode to which the index entry belongs dirty,
- * too (I_DIRTY_PAGES) and this in turn ensures the page buffers, and hence the
- * dirty index block, will be written out to disk later.
- */
-void __ntfs_index_entry_mark_dirty(ntfs_index_context *ictx)
-{
-       ntfs_inode *ni;
-       struct page *page;
-       struct buffer_head *bh, *head;
-       unsigned int rec_start, rec_end, bh_size, bh_start, bh_end;
-
-       BUG_ON(ictx->is_in_root);
-       ni = ictx->idx_ni;
-       page = ictx->page;
-       BUG_ON(!page_has_buffers(page));
-       /*
-        * If the index block is the same size as the page cache page, set all
-        * the buffers in the page, as well as the page itself, dirty.
-        */
-       if (ni->itype.index.block_size == PAGE_CACHE_SIZE) {
-               __set_page_dirty_buffers(page);
-               return;
-       }
-       /* Set only the buffers in which the index block is located dirty. */
-       rec_start = (unsigned int)((u8*)ictx->ia - (u8*)page_address(page));
-       rec_end = rec_start + ni->itype.index.block_size;
-       bh_size = ni->vol->sb->s_blocksize;
-       bh_start = 0;
-       bh = head = page_buffers(page);
-       do {
-               bh_end = bh_start + bh_size;
-               if ((bh_start >= rec_start) && (bh_end <= rec_end))
-                       set_buffer_dirty(bh);
-               bh_start = bh_end;
-       } while ((bh = bh->b_this_page) != head);
-       /* Finally, set the page itself dirty, too. */
-       __set_page_dirty_nobuffers(page);
-}
-
-#endif /* NTFS_RW */