This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / fs / ntfs / inode.c
index 31840ba..873c25d 100644 (file)
 #include <linux/quotaops.h>
 #include <linux/mount.h>
 
-#include "aops.h"
+#include "ntfs.h"
 #include "dir.h"
-#include "debug.h"
 #include "inode.h"
 #include "attrib.h"
-#include "malloc.h"
-#include "mft.h"
 #include "time.h"
-#include "ntfs.h"
 
 /**
  * ntfs_test_inode - compare two (possibly fake) inodes for equality
@@ -352,7 +348,7 @@ static inline ntfs_inode *ntfs_alloc_extent_inode(void)
        return NULL;
 }
 
-static void ntfs_destroy_extent_inode(ntfs_inode *ni)
+void ntfs_destroy_extent_inode(ntfs_inode *ni)
 {
        ntfs_debug("Entering.");
        BUG_ON(ni->page);
@@ -373,20 +369,20 @@ static void ntfs_destroy_extent_inode(ntfs_inode *ni)
  *
  * Return zero on success and -ENOMEM on error.
  */
-void __ntfs_init_inode(struct super_block *sb, ntfs_inode *ni)
+static void __ntfs_init_inode(struct super_block *sb, ntfs_inode *ni)
 {
        ntfs_debug("Entering.");
        ni->initialized_size = ni->allocated_size = 0;
        ni->seq_no = 0;
        atomic_set(&ni->count, 1);
        ni->vol = NTFS_SB(sb);
-       ntfs_init_runlist(&ni->runlist);
+       init_runlist(&ni->runlist);
        init_MUTEX(&ni->mrec_lock);
        ni->page = NULL;
        ni->page_ofs = 0;
        ni->attr_list_size = 0;
        ni->attr_list = NULL;
-       ntfs_init_runlist(&ni->attr_list_rl);
+       init_runlist(&ni->attr_list_rl);
        ni->itype.index.bmp_ino = NULL;
        ni->itype.index.block_size = 0;
        ni->itype.index.vcn_size = 0;
@@ -396,6 +392,17 @@ void __ntfs_init_inode(struct super_block *sb, ntfs_inode *ni)
        init_MUTEX(&ni->extent_lock);
        ni->nr_extents = 0;
        ni->ext.base_ntfs_ino = NULL;
+       return;
+}
+
+static inline void ntfs_init_big_inode(struct inode *vi)
+{
+       ntfs_inode *ni = NTFS_I(vi);
+
+       ntfs_debug("Entering.");
+       __ntfs_init_inode(vi->i_sb, ni);
+       ni->mft_no = vi->i_ino;
+       return;
 }
 
 inline ntfs_inode *ntfs_new_extent_inode(struct super_block *sb,
@@ -564,11 +571,13 @@ static int ntfs_read_locked_inode(struct inode *vi)
        }
 
        if (!(m->flags & MFT_RECORD_IN_USE)) {
-               ntfs_error(vi->i_sb, "Inode is not in use!");
+               ntfs_error(vi->i_sb, "Inode is not in use! You should "
+                               "run chkdsk.");
                goto unm_err_out;
        }
        if (m->base_mft_record) {
-               ntfs_error(vi->i_sb, "Inode is an extent inode!");
+               ntfs_error(vi->i_sb, "Inode is an extent inode! You should "
+                               "run chkdsk.");
                goto unm_err_out;
        }
 
@@ -592,26 +601,14 @@ static int ntfs_read_locked_inode(struct inode *vi)
         * Also if not a directory, it could be something else, rather than
         * a regular file. But again, will do for now.
         */
-       /* Everyone gets all permissions. */
-       vi->i_mode |= S_IRWXUGO;
-       /* If read-only, noone gets write permissions. */
-       if (IS_RDONLY(vi))
-               vi->i_mode &= ~S_IWUGO;
        if (m->flags & MFT_RECORD_IS_DIRECTORY) {
                vi->i_mode |= S_IFDIR;
-               /*
-                * Apply the directory permissions mask set in the mount
-                * options.
-                */
-               vi->i_mode &= ~vol->dmask;
                /* Things break without this kludge! */
                if (vi->i_nlink > 1)
                        vi->i_nlink = 1;
-       } else {
+       } else
                vi->i_mode |= S_IFREG;
-               /* Apply the file permissions mask set in the mount options. */
-               vi->i_mode &= ~vol->fmask;
-       }
+
        /*
         * Find the standard information attribute in the mft record. At this
         * stage we haven't setup the attribute list stuff yet, so this could
@@ -665,7 +662,7 @@ static int ntfs_read_locked_inode(struct inode *vi)
        if (err) {
                if (unlikely(err != -ENOENT)) {
                        ntfs_error(vi->i_sb, "Failed to lookup attribute list "
-                                       "attribute.");
+                                       "attribute. You should run chkdsk.");
                        goto unm_err_out;
                }
        } else /* if (!err) */ {
@@ -677,7 +674,9 @@ static int ntfs_read_locked_inode(struct inode *vi)
                                ctx->attr->flags & ATTR_COMPRESSION_MASK ||
                                ctx->attr->flags & ATTR_IS_SPARSE) {
                        ntfs_error(vi->i_sb, "Attribute list attribute is "
-                                       "compressed/encrypted/sparse.");
+                                       "compressed/encrypted/sparse. Not "
+                                       "allowed. Corrupt inode. You should "
+                                       "run chkdsk.");
                        goto unm_err_out;
                }
                /* Now allocate memory for the attribute list. */
@@ -693,20 +692,25 @@ static int ntfs_read_locked_inode(struct inode *vi)
                        NInoSetAttrListNonResident(ni);
                        if (ctx->attr->data.non_resident.lowest_vcn) {
                                ntfs_error(vi->i_sb, "Attribute list has non "
-                                               "zero lowest_vcn.");
+                                               "zero lowest_vcn. Inode is "
+                                               "corrupt. You should run "
+                                               "chkdsk.");
                                goto unm_err_out;
                        }
                        /*
                         * Setup the runlist. No need for locking as we have
                         * exclusive access to the inode at this time.
                         */
-                       ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol,
+                       ni->attr_list_rl.rl = decompress_mapping_pairs(vol,
                                        ctx->attr, NULL);
                        if (IS_ERR(ni->attr_list_rl.rl)) {
                                err = PTR_ERR(ni->attr_list_rl.rl);
                                ni->attr_list_rl.rl = NULL;
                                ntfs_error(vi->i_sb, "Mapping pairs "
-                                               "decompression failed.");
+                                               "decompression failed with "
+                                               "error code %i. Corrupt "
+                                               "attribute list in inode.",
+                                               -err);
                                goto unm_err_out;
                        }
                        /* Now load the attribute list. */
@@ -761,18 +765,9 @@ skip_attr_list_load:
                        goto unm_err_out;
                }
                /* Set up the state. */
-               if (unlikely(ctx->attr->non_resident)) {
-                       ntfs_error(vol->sb, "$INDEX_ROOT attribute is not "
-                                       "resident.");
-                       goto unm_err_out;
-               }
-               /* Ensure the attribute name is placed before the value. */
-               if (unlikely(ctx->attr->name_length &&
-                               (le16_to_cpu(ctx->attr->name_offset) >=
-                               le16_to_cpu(ctx->attr->data.resident.
-                               value_offset)))) {
-                       ntfs_error(vol->sb, "$INDEX_ROOT attribute name is "
-                                       "placed after the attribute value.");
+               if (ctx->attr->non_resident) {
+                       ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is "
+                                       "not resident. Not allowed.");
                        goto unm_err_out;
                }
                /*
@@ -786,7 +781,8 @@ skip_attr_list_load:
                if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
                        if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
                                ntfs_error(vi->i_sb, "Found encrypted and "
-                                               "compressed attribute.");
+                                               "compressed attribute. Not "
+                                               "allowed.");
                                goto unm_err_out;
                        }
                        NInoSetEncrypted(ni);
@@ -810,12 +806,12 @@ skip_attr_list_load:
                }
                if (ir->type != AT_FILE_NAME) {
                        ntfs_error(vi->i_sb, "Indexed attribute is not "
-                                       "$FILE_NAME.");
+                                       "$FILE_NAME. Not allowed.");
                        goto unm_err_out;
                }
                if (ir->collation_rule != COLLATION_FILE_NAME) {
                        ntfs_error(vi->i_sb, "Index collation rule is not "
-                                       "COLLATION_FILE_NAME.");
+                                       "COLLATION_FILE_NAME. Not allowed.");
                        goto unm_err_out;
                }
                ni->itype.index.collation_rule = ir->collation_rule;
@@ -830,7 +826,7 @@ skip_attr_list_load:
                if (ni->itype.index.block_size > PAGE_CACHE_SIZE) {
                        ntfs_error(vi->i_sb, "Index block size (%u) > "
                                        "PAGE_CACHE_SIZE (%ld) is not "
-                                       "supported.  Sorry.",
+                                       "supported. Sorry.",
                                        ni->itype.index.block_size,
                                        PAGE_CACHE_SIZE);
                        err = -EOPNOTSUPP;
@@ -839,7 +835,7 @@ skip_attr_list_load:
                if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) {
                        ntfs_error(vi->i_sb, "Index block size (%u) < "
                                        "NTFS_BLOCK_SIZE (%i) is not "
-                                       "supported.  Sorry.",
+                                       "supported. Sorry.",
                                        ni->itype.index.block_size,
                                        NTFS_BLOCK_SIZE);
                        err = -EOPNOTSUPP;
@@ -882,7 +878,8 @@ skip_attr_list_load:
                        if (err == -ENOENT)
                                ntfs_error(vi->i_sb, "$INDEX_ALLOCATION "
                                                "attribute is not present but "
-                                               "$INDEX_ROOT indicated it is.");
+                                               "$INDEX_ROOT indicated it "
+                                               "is.");
                        else
                                ntfs_error(vi->i_sb, "Failed to lookup "
                                                "$INDEX_ALLOCATION "
@@ -894,19 +891,6 @@ skip_attr_list_load:
                                        "is resident.");
                        goto unm_err_out;
                }
-               /*
-                * Ensure the attribute name is placed before the mapping pairs
-                * array.
-                */
-               if (unlikely(ctx->attr->name_length &&
-                               (le16_to_cpu(ctx->attr->name_offset) >=
-                               le16_to_cpu(ctx->attr->data.non_resident.
-                               mapping_pairs_offset)))) {
-                       ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name "
-                                       "is placed after the mapping pairs "
-                                       "array.");
-                       goto unm_err_out;
-               }
                if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
                        ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
                                        "is encrypted.");
@@ -925,7 +909,8 @@ skip_attr_list_load:
                if (ctx->attr->data.non_resident.lowest_vcn) {
                        ntfs_error(vi->i_sb, "First extent of "
                                        "$INDEX_ALLOCATION attribute has non "
-                                       "zero lowest_vcn.");
+                                       "zero lowest_vcn. Inode is corrupt. "
+                                       "You should run chkdsk.");
                        goto unm_err_out;
                }
                vi->i_size = sle64_to_cpu(
@@ -966,9 +951,20 @@ skip_attr_list_load:
                        goto unm_err_out;
                }
 skip_large_dir_stuff:
+               /* Everyone gets read and scan permissions. */
+               vi->i_mode |= S_IRUGO | S_IXUGO;
+               /* If not read-only, set write permissions. */
+               if (!IS_RDONLY(vi))
+                       vi->i_mode |= S_IWUGO;
+               /*
+                * Apply the directory permissions mask set in the mount
+                * options.
+                */
+               vi->i_mode &= ~vol->dmask;
                /* Setup the operations for this inode. */
                vi->i_op = &ntfs_dir_inode_ops;
                vi->i_fop = &ntfs_dir_ops;
+               vi->i_mapping->a_ops = &ntfs_mst_aops;
        } else {
                /* It is a file. */
                ntfs_attr_reinit_search_ctx(ctx);
@@ -1007,7 +1003,8 @@ skip_large_dir_stuff:
                                goto no_data_attr_special_case;
                        // FIXME: File is corrupt! Hot-fix with empty data
                        // attribute if recovery option is set.
-                       ntfs_error(vi->i_sb, "$DATA attribute is missing.");
+                       ntfs_error(vi->i_sb, "$DATA attribute is "
+                                       "missing.");
                        goto unm_err_out;
                }
                /* Setup the state. */
@@ -1037,8 +1034,10 @@ skip_large_dir_stuff:
                                                compression_unit != 4) {
                                        ntfs_error(vi->i_sb, "Found "
                                                "nonstandard compression unit "
-                                               "(%u instead of 4).  Cannot "
-                                               "handle this.",
+                                               "(%u instead of 4). Cannot "
+                                               "handle this. This might "
+                                               "indicate corruption so you "
+                                               "should run chkdsk.",
                                                ctx->attr->data.non_resident.
                                                compression_unit);
                                        err = -EOPNOTSUPP;
@@ -1064,7 +1063,8 @@ skip_large_dir_stuff:
                        if (ctx->attr->data.non_resident.lowest_vcn) {
                                ntfs_error(vi->i_sb, "First extent of $DATA "
                                                "attribute has non zero "
-                                               "lowest_vcn.");
+                                               "lowest_vcn. Inode is corrupt. "
+                                               "You should run chkdsk.");
                                goto unm_err_out;
                        }
                        /* Setup all the sizes. */
@@ -1098,14 +1098,18 @@ no_data_attr_special_case:
                unmap_mft_record(ni);
                m = NULL;
                ctx = NULL;
+               /* Everyone gets all permissions. */
+               vi->i_mode |= S_IRWXUGO;
+               /* If read-only, noone gets write permissions. */
+               if (IS_RDONLY(vi))
+                       vi->i_mode &= ~S_IWUGO;
+               /* Apply the file permissions mask set in the mount options. */
+               vi->i_mode &= ~vol->fmask;
                /* Setup the operations for this inode. */
                vi->i_op = &ntfs_file_inode_ops;
                vi->i_fop = &ntfs_file_ops;
-       }
-       if (NInoMstProtected(ni))
-               vi->i_mapping->a_ops = &ntfs_mst_aops;
-       else
                vi->i_mapping->a_ops = &ntfs_aops;
+       }
        /*
         * The number of 512-byte blocks used on disk (for stat). This is in so
         * far inaccurate as it doesn't account for any named streams or other
@@ -1133,11 +1137,9 @@ unm_err_out:
        if (m)
                unmap_mft_record(ni);
 err_out:
-       ntfs_error(vol->sb, "Failed with error code %i.  Marking corrupt "
-                       "inode 0x%lx as bad.  Run chkdsk.", err, vi->i_ino);
+       ntfs_error(vi->i_sb, "Failed with error code %i. Marking inode 0x%lx "
+                       "as bad.", -err, vi->i_ino);
        make_bad_inode(vi);
-       if (err != -EOPNOTSUPP && err != -ENOMEM)
-               NVolSetErrors(vol);
        return err;
 }
 
@@ -1208,21 +1210,15 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
                goto unm_err_out;
 
        if (!ctx->attr->non_resident) {
-               /* Ensure the attribute name is placed before the value. */
-               if (unlikely(ctx->attr->name_length &&
-                               (le16_to_cpu(ctx->attr->name_offset) >=
-                               le16_to_cpu(ctx->attr->data.resident.
-                               value_offset)))) {
-                       ntfs_error(vol->sb, "Attribute name is placed after "
-                                       "the attribute value.");
-                       goto unm_err_out;
-               }
                if (NInoMstProtected(ni) || ctx->attr->flags) {
                        ntfs_error(vi->i_sb, "Found mst protected attribute "
                                        "or attribute with non-zero flags but "
-                                       "the attribute is resident.  Please "
-                                       "report you saw this message to "
-                                       "linux-ntfs-dev@lists.sourceforge.net");
+                                       "the attribute is resident (mft_no "
+                                       "0x%lx, type 0x%x, name_len %i). "
+                                       "Please report you saw this message "
+                                       "to linux-ntfs-dev@lists."
+                                       "sourceforge.net",
+                                       vi->i_ino, ni->type, ni->name_len);
                        goto unm_err_out;
                }
                /*
@@ -1233,63 +1229,60 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
                        le32_to_cpu(ctx->attr->data.resident.value_length);
        } else {
                NInoSetNonResident(ni);
-               /*
-                * Ensure the attribute name is placed before the mapping pairs
-                * array.
-                */
-               if (unlikely(ctx->attr->name_length &&
-                               (le16_to_cpu(ctx->attr->name_offset) >=
-                               le16_to_cpu(ctx->attr->data.non_resident.
-                               mapping_pairs_offset)))) {
-                       ntfs_error(vol->sb, "Attribute name is placed after "
-                                       "the mapping pairs array.");
-                       goto unm_err_out;
-               }
                if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
                        if (NInoMstProtected(ni)) {
                                ntfs_error(vi->i_sb, "Found mst protected "
                                                "attribute but the attribute "
-                                               "is compressed.  Please report "
-                                               "you saw this message to "
-                                               "linux-ntfs-dev@lists."
-                                               "sourceforge.net");
+                                               "is compressed (mft_no 0x%lx, "
+                                               "type 0x%x, name_len %i). "
+                                               "Please report you saw this "
+                                               "message to linux-ntfs-dev@"
+                                               "lists.sourceforge.net",
+                                               vi->i_ino, ni->type,
+                                               ni->name_len);
                                goto unm_err_out;
                        }
                        NInoSetCompressed(ni);
                        if ((ni->type != AT_DATA) || (ni->type == AT_DATA &&
                                        ni->name_len)) {
-                               ntfs_error(vi->i_sb, "Found compressed "
-                                               "non-data or named data "
-                                               "attribute.  Please report "
+                               ntfs_error(vi->i_sb, "Found compressed non-"
+                                               "data or named data attribute "
+                                               "(mft_no 0x%lx, type 0x%x, "
+                                               "name_len %i). Please report "
                                                "you saw this message to "
                                                "linux-ntfs-dev@lists."
-                                               "sourceforge.net");
+                                               "sourceforge.net",
+                                               vi->i_ino, ni->type,
+                                               ni->name_len);
                                goto unm_err_out;
                        }
                        if (vol->cluster_size > 4096) {
-                               ntfs_error(vi->i_sb, "Found compressed "
-                                               "attribute but compression is "
-                                               "disabled due to cluster size "
-                                               "(%i) > 4kiB.",
-                                               vol->cluster_size);
+                               ntfs_error(vi->i_sb, "Found "
+                                       "compressed attribute but "
+                                       "compression is disabled due "
+                                       "to cluster size (%i) > 4kiB.",
+                                       vol->cluster_size);
                                goto unm_err_out;
                        }
                        if ((ctx->attr->flags & ATTR_COMPRESSION_MASK)
                                        != ATTR_IS_COMPRESSED) {
                                ntfs_error(vi->i_sb, "Found unknown "
-                                               "compression method.");
+                                               "compression method or "
+                                               "corrupt file.");
                                goto unm_err_out;
                        }
                        ni->itype.compressed.block_clusters = 1U <<
                                        ctx->attr->data.non_resident.
                                        compression_unit;
-                       if (ctx->attr->data.non_resident.compression_unit !=
-                                       4) {
-                               ntfs_error(vi->i_sb, "Found nonstandard "
-                                               "compression unit (%u instead "
-                                               "of 4).  Cannot handle this.",
-                                               ctx->attr->data.non_resident.
-                                               compression_unit);
+                       if (ctx->attr->data.non_resident.compression_unit != 4) {
+                               ntfs_error(vi->i_sb, "Found "
+                                       "nonstandard compression unit "
+                                       "(%u instead of 4). Cannot "
+                                       "handle this. This might "
+                                       "indicate corruption so you "
+                                       "should run chkdsk.",
+                                       ctx->attr->data.non_resident.
+                                       compression_unit);
                                err = -EOPNOTSUPP;
                                goto unm_err_out;
                        }
@@ -1309,10 +1302,13 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
                        if (NInoMstProtected(ni)) {
                                ntfs_error(vi->i_sb, "Found mst protected "
                                                "attribute but the attribute "
-                                               "is encrypted.  Please report "
-                                               "you saw this message to "
-                                               "linux-ntfs-dev@lists."
-                                               "sourceforge.net");
+                                               "is encrypted (mft_no 0x%lx, "
+                                               "type 0x%x, name_len %i). "
+                                               "Please report you saw this "
+                                               "message to linux-ntfs-dev@"
+                                               "lists.sourceforge.net",
+                                               vi->i_ino, ni->type,
+                                               ni->name_len);
                                goto unm_err_out;
                        }
                        NInoSetEncrypted(ni);
@@ -1321,17 +1317,21 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
                        if (NInoMstProtected(ni)) {
                                ntfs_error(vi->i_sb, "Found mst protected "
                                                "attribute but the attribute "
-                                               "is sparse.  Please report "
-                                               "you saw this message to "
-                                               "linux-ntfs-dev@lists."
-                                               "sourceforge.net");
+                                               "is sparse (mft_no 0x%lx, "
+                                               "type 0x%x, name_len %i). "
+                                               "Please report you saw this "
+                                               "message to linux-ntfs-dev@"
+                                               "lists.sourceforge.net",
+                                               vi->i_ino, ni->type,
+                                               ni->name_len);
                                goto unm_err_out;
                        }
                        NInoSetSparse(ni);
                }
                if (ctx->attr->data.non_resident.lowest_vcn) {
                        ntfs_error(vi->i_sb, "First extent of attribute has "
-                                       "non-zero lowest_vcn.");
+                                       "non-zero lowest_vcn. Inode is "
+                                       "corrupt. You should run chkdsk.");
                        goto unm_err_out;
                }
                /* Setup all the sizes. */
@@ -1382,15 +1382,10 @@ unm_err_out:
                ntfs_attr_put_search_ctx(ctx);
        unmap_mft_record(base_ni);
 err_out:
-       ntfs_error(vol->sb, "Failed with error code %i while reading attribute "
-                       "inode (mft_no 0x%lx, type 0x%x, name_len %i).  "
-                       "Marking corrupt inode and base inode 0x%lx as bad.  "
-                       "Run chkdsk.", err, vi->i_ino, ni->type, ni->name_len,
-                       base_vi->i_ino);
+       ntfs_error(vi->i_sb, "Failed with error code %i while reading "
+                       "attribute inode (mft_no 0x%lx, type 0x%x, name_len "
+                       "%i.", -err, vi->i_ino, ni->type, ni->name_len);
        make_bad_inode(vi);
-       make_bad_inode(base_vi);
-       if (err != -ENOMEM)
-               NVolSetErrors(vol);
        return err;
 }
 
@@ -1475,24 +1470,16 @@ static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
                goto unm_err_out;
        }
        /* Set up the state. */
-       if (unlikely(ctx->attr->non_resident)) {
-               ntfs_error(vol->sb, "$INDEX_ROOT attribute is not resident.");
-               goto unm_err_out;
-       }
-       /* Ensure the attribute name is placed before the value. */
-       if (unlikely(ctx->attr->name_length &&
-                       (le16_to_cpu(ctx->attr->name_offset) >=
-                       le16_to_cpu(ctx->attr->data.resident.
-                       value_offset)))) {
-               ntfs_error(vol->sb, "$INDEX_ROOT attribute name is placed "
-                               "after the attribute value.");
+       if (ctx->attr->non_resident) {
+               ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is not resident.  "
+                               "Not allowed.");
                goto unm_err_out;
        }
        /* Compressed/encrypted/sparse index root is not allowed. */
        if (ctx->attr->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_ENCRYPTED |
                        ATTR_IS_SPARSE)) {
                ntfs_error(vi->i_sb, "Found compressed/encrypted/sparse index "
-                               "root attribute.");
+                               "root attribute.  Not allowed.");
                goto unm_err_out;
        }
        ir = (INDEX_ROOT*)((u8*)ctx->attr +
@@ -1508,8 +1495,8 @@ static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
                goto unm_err_out;
        }
        if (ir->type) {
-               ntfs_error(vi->i_sb, "Index type is not 0 (type is 0x%x).",
-                               le32_to_cpu(ir->type));
+               ntfs_error(vi->i_sb, "Index type is not 0 (type is 0x%x).  "
+                               "Not allowed.", le32_to_cpu(ir->type));
                goto unm_err_out;
        }
        ni->itype.index.collation_rule = ir->collation_rule;
@@ -1575,16 +1562,6 @@ static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
                                "resident.");
                goto unm_err_out;
        }
-       /*
-        * Ensure the attribute name is placed before the mapping pairs array.
-        */
-       if (unlikely(ctx->attr->name_length && (le16_to_cpu(
-                       ctx->attr->name_offset) >= le16_to_cpu(
-                       ctx->attr->data.non_resident.mapping_pairs_offset)))) {
-               ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name is "
-                               "placed after the mapping pairs array.");
-               goto unm_err_out;
-       }
        if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
                ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
                                "encrypted.");
@@ -1601,7 +1578,8 @@ static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
        }
        if (ctx->attr->data.non_resident.lowest_vcn) {
                ntfs_error(vi->i_sb, "First extent of $INDEX_ALLOCATION "
-                               "attribute has non zero lowest_vcn.");
+                               "attribute has non zero lowest_vcn.  Inode is "
+                               "corrupt. You should run chkdsk.");
                goto unm_err_out;
        }
        vi->i_size = sle64_to_cpu(ctx->attr->data.non_resident.data_size);
@@ -1627,16 +1605,16 @@ static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
        bni = NTFS_I(bvi);
        if (NInoCompressed(bni) || NInoEncrypted(bni) ||
                        NInoSparse(bni)) {
-               ntfs_error(vi->i_sb, "$BITMAP attribute is compressed and/or "
-                               "encrypted and/or sparse.");
+               ntfs_error(vi->i_sb, "$BITMAP attribute is compressed "
+                               "and/or encrypted and/or sparse.");
                goto iput_unm_err_out;
        }
        /* Consistency check bitmap size vs. index allocation size. */
        if ((bvi->i_size << 3) < (vi->i_size >>
                        ni->itype.index.block_size_bits)) {
-               ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) for "
-                               "index allocation (0x%llx).", bvi->i_size << 3,
-                               vi->i_size);
+               ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) "
+                               "for index allocation (0x%llx).",
+                               bvi->i_size << 3, vi->i_size);
                goto iput_unm_err_out;
        }
        ni->itype.index.bmp_ino = bvi;
@@ -1669,11 +1647,9 @@ unm_err_out:
                unmap_mft_record(base_ni);
 err_out:
        ntfs_error(vi->i_sb, "Failed with error code %i while reading index "
-                       "inode (mft_no 0x%lx, name_len %i.", err, vi->i_ino,
+                       "inode (mft_no 0x%lx, name_len %i.", -err, vi->i_ino,
                        ni->name_len);
        make_bad_inode(vi);
-       if (err != -EOPNOTSUPP && err != -ENOMEM)
-               NVolSetErrors(vol);
        return err;
 }
 
@@ -1696,8 +1672,8 @@ err_out:
  *
  * We solve these problems by starting with the $DATA attribute before anything
  * else and iterating using ntfs_attr_lookup($DATA) over all extents.  As each
- * extent is found, we ntfs_mapping_pairs_decompress() including the implied
- * ntfs_runlists_merge().  Each step of the iteration necessarily provides
+ * extent is found, we decompress_mapping_pairs() including the implied
+ * ntfs_merge_runlists().  Each step of the iteration necessarily provides
  * sufficient information for the next step to complete.
  *
  * This should work but there are two possible pit falls (see inline comments
@@ -1786,7 +1762,7 @@ int ntfs_read_inode_mount(struct inode *vi)
        vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
 
        /* Provides readpage() and sync_page() for map_mft_record(). */
-       vi->i_mapping->a_ops = &ntfs_mst_aops;
+       vi->i_mapping->a_ops = &ntfs_mft_aops;
 
        ctx = ntfs_attr_get_search_ctx(ni, m);
        if (!ctx) {
@@ -1834,7 +1810,7 @@ int ntfs_read_inode_mount(struct inode *vi)
                                goto put_err_out;
                        }
                        /* Setup the runlist. */
-                       ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol,
+                       ni->attr_list_rl.rl = decompress_mapping_pairs(vol,
                                        ctx->attr, NULL);
                        if (IS_ERR(ni->attr_list_rl.rl)) {
                                err = PTR_ERR(ni->attr_list_rl.rl);
@@ -1966,11 +1942,11 @@ int ntfs_read_inode_mount(struct inode *vi)
                 * as we have exclusive access to the inode at this time and we
                 * are a mount in progress task, too.
                 */
-               nrl = ntfs_mapping_pairs_decompress(vol, attr, ni->runlist.rl);
+               nrl = decompress_mapping_pairs(vol, attr, ni->runlist.rl);
                if (IS_ERR(nrl)) {
-                       ntfs_error(sb, "ntfs_mapping_pairs_decompress() "
-                                       "failed with error code %ld.  $MFT is "
-                                       "corrupt.", PTR_ERR(nrl));
+                       ntfs_error(sb, "decompress_mapping_pairs() failed with "
+                                       "error code %ld. $MFT is corrupt.",
+                                       PTR_ERR(nrl));
                        goto put_err_out;
                }
                ni->runlist.rl = nrl;
@@ -2048,6 +2024,8 @@ int ntfs_read_inode_mount(struct inode *vi)
                        /* No VFS initiated operations allowed for $MFT. */
                        vi->i_op = &ntfs_empty_inode_ops;
                        vi->i_fop = &ntfs_empty_file_ops;
+                       /* Put back our special address space operations. */
+                       vi->i_mapping->a_ops = &ntfs_mft_aops;
                }
 
                /* Get the lowest vcn for the next extent. */
@@ -2113,27 +2091,40 @@ err_out:
  * dropped, we need to put the attribute inode for the directory index bitmap,
  * if it is present, otherwise the directory inode would remain pinned for
  * ever.
+ *
+ * If the inode @vi is an index inode with only one reference which is being
+ * dropped, we need to put the attribute inode for the index bitmap, if it is
+ * present, otherwise the index inode would disappear and the attribute inode
+ * for the index bitmap would no longer be referenced from anywhere and thus it
+ * would remain pinned for ever.
  */
 void ntfs_put_inode(struct inode *vi)
 {
-       if (S_ISDIR(vi->i_mode) && atomic_read(&vi->i_count) == 2) {
-               ntfs_inode *ni = NTFS_I(vi);
-               if (NInoIndexAllocPresent(ni)) {
-                       struct inode *bvi = NULL;
-                       down(&vi->i_sem);
-                       if (atomic_read(&vi->i_count) == 2) {
-                               bvi = ni->itype.index.bmp_ino;
-                               if (bvi)
-                                       ni->itype.index.bmp_ino = NULL;
+       ntfs_inode *ni;
+
+       if (S_ISDIR(vi->i_mode)) {
+               if (atomic_read(&vi->i_count) == 2) {
+                       ni = NTFS_I(vi);
+                       if (NInoIndexAllocPresent(ni) &&
+                                       ni->itype.index.bmp_ino) {
+                               iput(ni->itype.index.bmp_ino);
+                               ni->itype.index.bmp_ino = NULL;
                        }
-                       up(&vi->i_sem);
-                       if (bvi)
-                               iput(bvi);
                }
+               return;
+       }
+       if (atomic_read(&vi->i_count) != 1)
+               return;
+       ni = NTFS_I(vi);
+       if (NInoAttr(ni) && (ni->type == AT_INDEX_ALLOCATION) &&
+                       NInoIndexAllocPresent(ni) && ni->itype.index.bmp_ino) {
+               iput(ni->itype.index.bmp_ino);
+               ni->itype.index.bmp_ino = NULL;
        }
+       return;
 }
 
-static void __ntfs_clear_inode(ntfs_inode *ni)
+void __ntfs_clear_inode(ntfs_inode *ni)
 {
        /* Free all alocated memory. */
        down_write(&ni->runlist.lock);
@@ -2198,18 +2189,6 @@ void ntfs_clear_big_inode(struct inode *vi)
 {
        ntfs_inode *ni = NTFS_I(vi);
 
-       /*
-        * If the inode @vi is an index inode we need to put the attribute
-        * inode for the index bitmap, if it is present, otherwise the index
-        * inode would disappear and the attribute inode for the index bitmap
-        * would no longer be referenced from anywhere and thus it would remain
-        * pinned for ever.
-        */
-       if (NInoAttr(ni) && (ni->type == AT_INDEX_ALLOCATION) &&
-                       NInoIndexAllocPresent(ni) && ni->itype.index.bmp_ino) {
-               iput(ni->itype.index.bmp_ino);
-               ni->itype.index.bmp_ino = NULL;
-       }
 #ifdef NTFS_RW
        if (NInoDirty(ni)) {
                BOOL was_bad = (is_bad_inode(vi));
@@ -2289,7 +2268,7 @@ int ntfs_show_options(struct seq_file *sf, struct vfsmount *mnt)
  * ntfs_truncate - called when the i_size of an ntfs inode is changed
  * @vi:                inode for which the i_size was changed
  *
- * We do not support i_size changes yet.
+ * We don't support i_size changes yet.
  *
  * The kernel guarantees that @vi is a regular file (S_ISREG() is true) and
  * that the change is allowed.
@@ -2297,93 +2276,66 @@ int ntfs_show_options(struct seq_file *sf, struct vfsmount *mnt)
  * This implies for us that @vi is a file inode rather than a directory, index,
  * or attribute inode as well as that @vi is a base inode.
  *
- * Returns 0 on success or -errno on error.
- *
  * Called with ->i_sem held.  In all but one case ->i_alloc_sem is held for
  * writing.  The only case where ->i_alloc_sem is not held is
  * mm/filemap.c::generic_file_buffered_write() where vmtruncate() is called
  * with the current i_size as the offset which means that it is a noop as far
  * as ntfs_truncate() is concerned.
  */
-int ntfs_truncate(struct inode *vi)
+void ntfs_truncate(struct inode *vi)
 {
        ntfs_inode *ni = NTFS_I(vi);
-       ntfs_volume *vol = ni->vol;
        ntfs_attr_search_ctx *ctx;
        MFT_RECORD *m;
-       const char *te = "  Leaving file length out of sync with i_size.";
        int err;
 
-       ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
-       BUG_ON(NInoAttr(ni));
-       BUG_ON(ni->nr_extents < 0);
        m = map_mft_record(ni);
        if (IS_ERR(m)) {
-               err = PTR_ERR(m);
                ntfs_error(vi->i_sb, "Failed to map mft record for inode 0x%lx "
-                               "(error code %d).%s", vi->i_ino, err, te);
-               ctx = NULL;
-               m = NULL;
-               goto err_out;
+                               "(error code %ld).", vi->i_ino, PTR_ERR(m));
+               if (PTR_ERR(m) != ENOMEM)
+                       make_bad_inode(vi);
+               return;
        }
        ctx = ntfs_attr_get_search_ctx(ni, m);
        if (unlikely(!ctx)) {
-               ntfs_error(vi->i_sb, "Failed to allocate a search context for "
-                               "inode 0x%lx (not enough memory).%s",
-                               vi->i_ino, te);
-               err = -ENOMEM;
-               goto err_out;
+               ntfs_error(vi->i_sb, "Failed to allocate a search context: "
+                               "Not enough memory");
+               // FIXME: We can't report an error code upstream.  So what do
+               // we do?!?  make_bad_inode() seems a bit harsh...
+               unmap_mft_record(ni);
+               return;
        }
        err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
                        CASE_SENSITIVE, 0, NULL, 0, ctx);
        if (unlikely(err)) {
-               if (err == -ENOENT)
+               if (err == -ENOENT) {
                        ntfs_error(vi->i_sb, "Open attribute is missing from "
                                        "mft record.  Inode 0x%lx is corrupt.  "
                                        "Run chkdsk.", vi->i_ino);
-               else
+                       make_bad_inode(vi);
+               } else {
                        ntfs_error(vi->i_sb, "Failed to lookup attribute in "
                                        "inode 0x%lx (error code %d).",
                                        vi->i_ino, err);
-               goto err_out;
+                       // FIXME: We can't report an error code upstream.  So
+                       // what do we do?!?  make_bad_inode() seems a bit
+                       // harsh...
+               }
+               goto out;
        }
        /* If the size has not changed there is nothing to do. */
        if (ntfs_attr_size(ctx->attr) == i_size_read(vi))
-               goto done;
+               goto out;
        // TODO: Implement the truncate...
        ntfs_error(vi->i_sb, "Inode size has changed but this is not "
                        "implemented yet.  Resetting inode size to old value. "
                        " This is most likely a bug in the ntfs driver!");
        i_size_write(vi, ntfs_attr_size(ctx->attr)); 
-done:
+out:
        ntfs_attr_put_search_ctx(ctx);
        unmap_mft_record(ni);
-       NInoClearTruncateFailed(ni);
-       ntfs_debug("Done.");
-       return 0;
-err_out:
-       if (err != -ENOMEM) {
-               NVolSetErrors(vol);
-               make_bad_inode(vi);
-       }
-       if (ctx)
-               ntfs_attr_put_search_ctx(ctx);
-       if (m)
-               unmap_mft_record(ni);
-       NInoSetTruncateFailed(ni);
-       return err;
-}
-
-/**
- * ntfs_truncate_vfs - wrapper for ntfs_truncate() that has no return value
- * @vi:                inode for which the i_size was changed
- *
- * Wrapper for ntfs_truncate() that has no return value.
- *
- * See ntfs_truncate() description above for details.
- */
-void ntfs_truncate_vfs(struct inode *vi) {
-       ntfs_truncate(vi);
+       return;
 }
 
 /**
@@ -2548,16 +2500,9 @@ int ntfs_write_inode(struct inode *vi, int sync)
         * dirty, since we are going to write this mft record below in any case
         * and the base mft record may actually not have been modified so it
         * might not need to be written out.
-        * NOTE: It is not a problem when the inode for $MFT itself is being
-        * written out as mark_ntfs_record_dirty() will only set I_DIRTY_PAGES
-        * on the $MFT inode and hence ntfs_write_inode() will not be
-        * re-invoked because of it which in turn is ok since the dirtied mft
-        * record will be cleaned and written out to disk below, i.e. before
-        * this function returns.
         */
        if (modified && !NInoTestSetDirty(ctx->ntfs_ino))
-               mark_ntfs_record_dirty(ctx->ntfs_ino->page,
-                               ctx->ntfs_ino->page_ofs);
+               __set_page_dirty_nobuffers(ctx->ntfs_ino->page);
        ntfs_attr_put_search_ctx(ctx);
        /* Now the access times are updated, write the base mft record. */
        if (NInoDirty(ni))
@@ -2608,7 +2553,6 @@ err_out:
                ntfs_error(vi->i_sb, "Failed (error code %i):  Marking inode "
                                "as bad.  You should run chkdsk.", -err);
                make_bad_inode(vi);
-               NVolSetErrors(ni->vol);
        }
        return err;
 }