[PATCH] Really ignore kmem_cache_destroy return value
[safe/jmp/linux-2.6] / fs / ntfs / super.c
index e9c0d80..6b2712f 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project.
  *
- * Copyright (c) 2001-2005 Anton Altaparmakov
+ * Copyright (c) 2001-2006 Anton Altaparmakov
  * Copyright (c) 2001,2002 Richard Russon
  *
  * This program/include file is free software; you can redistribute it and/or
@@ -22,6 +22,7 @@
 
 #include <linux/stddef.h>
 #include <linux/init.h>
+#include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/spinlock.h>
 #include <linux/blkdev.h>      /* For bdev_hardsect_size(). */
@@ -471,9 +472,16 @@ static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
                        ntfs_error(sb, "Volume is dirty and read-only%s", es);
                        return -EROFS;
                }
+               if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
+                       ntfs_error(sb, "Volume has been modified by chkdsk "
+                                       "and is read-only%s", es);
+                       return -EROFS;
+               }
                if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
-                       ntfs_error(sb, "Volume has unsupported flags set and "
-                                       "is read-only%s", es);
+                       ntfs_error(sb, "Volume has unsupported flags set "
+                                       "(0x%x) and is read-only%s",
+                                       (unsigned)le16_to_cpu(vol->vol_flags),
+                                       es);
                        return -EROFS;
                }
                if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
@@ -641,7 +649,7 @@ static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb,
 {
        const char *read_err_str = "Unable to read %s boot sector.";
        struct buffer_head *bh_primary, *bh_backup;
-       long nr_blocks = NTFS_SB(sb)->nr_blocks;
+       sector_t nr_blocks = NTFS_SB(sb)->nr_blocks;
 
        /* Try to read primary boot sector. */
        if ((bh_primary = sb_bread(sb, 0))) {
@@ -688,13 +696,18 @@ hotfix_primary_boot_sector:
                /*
                 * If we managed to read sector zero and the volume is not
                 * read-only, copy the found, valid backup boot sector to the
-                * primary boot sector.
+                * primary boot sector.  Note we only copy the actual boot
+                * sector structure, not the actual whole device sector as that
+                * may be bigger and would potentially damage the $Boot system
+                * file (FIXME: Would be nice to know if the backup boot sector
+                * on a large sector device contains the whole boot loader or
+                * just the first 512 bytes).
                 */
                if (!(sb->s_flags & MS_RDONLY)) {
                        ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
                                        "boot sector from backup copy.");
                        memcpy(bh_primary->b_data, bh_backup->b_data,
-                                       sb->s_blocksize);
+                                       NTFS_BLOCK_SIZE);
                        mark_buffer_dirty(bh_primary);
                        sync_dirty_buffer(bh_primary);
                        if (buffer_uptodate(bh_primary)) {
@@ -733,9 +746,13 @@ static BOOL parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
                        vol->sector_size);
        ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
                        vol->sector_size_bits);
-       if (vol->sector_size != vol->sb->s_blocksize)
-               ntfs_warning(vol->sb, "The boot sector indicates a sector size "
-                               "different from the device sector size.");
+       if (vol->sector_size < vol->sb->s_blocksize) {
+               ntfs_error(vol->sb, "Sector size (%i) is smaller than the "
+                               "device block size (%lu).  This is not "
+                               "supported.  Sorry.", vol->sector_size,
+                               vol->sb->s_blocksize);
+               return FALSE;
+       }
        ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
        sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
        ntfs_debug("sectors_per_cluster_bits = 0x%x",
@@ -748,16 +765,11 @@ static BOOL parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
        ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
                        vol->cluster_size);
        ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
-       ntfs_debug("vol->cluster_size_bits = %i (0x%x)",
-                       vol->cluster_size_bits, vol->cluster_size_bits);
-       if (vol->sector_size > vol->cluster_size) {
-               ntfs_error(vol->sb, "Sector sizes above the cluster size are "
-                               "not supported.  Sorry.");
-               return FALSE;
-       }
-       if (vol->sb->s_blocksize > vol->cluster_size) {
-               ntfs_error(vol->sb, "Cluster sizes smaller than the device "
-                               "sector size are not supported.  Sorry.");
+       ntfs_debug("vol->cluster_size_bits = %i", vol->cluster_size_bits);
+       if (vol->cluster_size < vol->sector_size) {
+               ntfs_error(vol->sb, "Cluster size (%i) is smaller than the "
+                               "sector size (%i).  This is not supported.  "
+                               "Sorry.", vol->cluster_size, vol->sector_size);
                return FALSE;
        }
        clusters_per_mft_record = b->clusters_per_mft_record;
@@ -786,11 +798,18 @@ static BOOL parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
         * we store $MFT/$DATA, the table of mft records in the page cache.
         */
        if (vol->mft_record_size > PAGE_CACHE_SIZE) {
-               ntfs_error(vol->sb, "Mft record size %i (0x%x) exceeds the "
-                               "page cache size on your system %lu (0x%lx).  "
+               ntfs_error(vol->sb, "Mft record size (%i) exceeds the "
+                               "PAGE_CACHE_SIZE on your system (%lu).  "
                                "This is not supported.  Sorry.",
-                               vol->mft_record_size, vol->mft_record_size,
-                               PAGE_CACHE_SIZE, PAGE_CACHE_SIZE);
+                               vol->mft_record_size, PAGE_CACHE_SIZE);
+               return FALSE;
+       }
+       /* We cannot support mft record sizes below the sector size. */
+       if (vol->mft_record_size < vol->sector_size) {
+               ntfs_error(vol->sb, "Mft record size (%i) is smaller than the "
+                               "sector size (%i).  This is not supported.  "
+                               "Sorry.", vol->mft_record_size,
+                               vol->sector_size);
                return FALSE;
        }
        clusters_per_index_record = b->clusters_per_index_record;
@@ -816,6 +835,14 @@ static BOOL parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
        ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
                        vol->index_record_size_bits,
                        vol->index_record_size_bits);
+       /* We cannot support index record sizes below the sector size. */
+       if (vol->index_record_size < vol->sector_size) {
+               ntfs_error(vol->sb, "Index record size (%i) is smaller than "
+                               "the sector size (%i).  This is not "
+                               "supported.  Sorry.", vol->index_record_size,
+                               vol->sector_size);
+               return FALSE;
+       }
        /*
         * Get the size of the volume in clusters and check for 64-bit-ness.
         * Windows currently only uses 32 bits to save the clusters so we do
@@ -845,15 +872,18 @@ static BOOL parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
        }
        ll = sle64_to_cpu(b->mft_lcn);
        if (ll >= vol->nr_clusters) {
-               ntfs_error(vol->sb, "MFT LCN is beyond end of volume.  Weird.");
+               ntfs_error(vol->sb, "MFT LCN (%lli, 0x%llx) is beyond end of "
+                               "volume.  Weird.", (unsigned long long)ll,
+                               (unsigned long long)ll);
                return FALSE;
        }
        vol->mft_lcn = ll;
        ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
        ll = sle64_to_cpu(b->mftmirr_lcn);
        if (ll >= vol->nr_clusters) {
-               ntfs_error(vol->sb, "MFTMirr LCN is beyond end of volume.  "
-                               "Weird.");
+               ntfs_error(vol->sb, "MFTMirr LCN (%lli, 0x%llx) is beyond end "
+                               "of volume.  Weird.", (unsigned long long)ll,
+                               (unsigned long long)ll);
                return FALSE;
        }
        vol->mftmirr_lcn = ll;
@@ -1069,26 +1099,38 @@ static BOOL check_mft_mirror(ntfs_volume *vol)
                        kmirr = page_address(mirr_page);
                        ++index;
                }
-               /* Make sure the record is ok. */
-               if (ntfs_is_baad_recordp((le32*)kmft)) {
-                       ntfs_error(sb, "Incomplete multi sector transfer "
-                                       "detected in mft record %i.", i);
+               /* Do not check the record if it is not in use. */
+               if (((MFT_RECORD*)kmft)->flags & MFT_RECORD_IN_USE) {
+                       /* Make sure the record is ok. */
+                       if (ntfs_is_baad_recordp((le32*)kmft)) {
+                               ntfs_error(sb, "Incomplete multi sector "
+                                               "transfer detected in mft "
+                                               "record %i.", i);
 mm_unmap_out:
-                       ntfs_unmap_page(mirr_page);
+                               ntfs_unmap_page(mirr_page);
 mft_unmap_out:
-                       ntfs_unmap_page(mft_page);
-                       return FALSE;
+                               ntfs_unmap_page(mft_page);
+                               return FALSE;
+                       }
                }
-               if (ntfs_is_baad_recordp((le32*)kmirr)) {
-                       ntfs_error(sb, "Incomplete multi sector transfer "
-                                       "detected in mft mirror record %i.", i);
-                       goto mm_unmap_out;
+               /* Do not check the mirror record if it is not in use. */
+               if (((MFT_RECORD*)kmirr)->flags & MFT_RECORD_IN_USE) {
+                       if (ntfs_is_baad_recordp((le32*)kmirr)) {
+                               ntfs_error(sb, "Incomplete multi sector "
+                                               "transfer detected in mft "
+                                               "mirror record %i.", i);
+                               goto mm_unmap_out;
+                       }
                }
                /* Get the amount of data in the current record. */
                bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use);
-               if (!bytes || bytes > vol->mft_record_size) {
+               if (bytes < sizeof(MFT_RECORD_OLD) ||
+                               bytes > vol->mft_record_size ||
+                               ntfs_is_baad_recordp((le32*)kmft)) {
                        bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use);
-                       if (!bytes || bytes > vol->mft_record_size)
+                       if (bytes < sizeof(MFT_RECORD_OLD) ||
+                                       bytes > vol->mft_record_size ||
+                                       ntfs_is_baad_recordp((le32*)kmirr))
                                bytes = vol->mft_record_size;
                }
                /* Compare the two records. */
@@ -1635,11 +1677,11 @@ read_partial_upcase_page:
        ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
                        i_size, 64 * 1024 * sizeof(ntfschar));
        iput(ino);
-       down(&ntfs_lock);
+       mutex_lock(&ntfs_lock);
        if (!default_upcase) {
                ntfs_debug("Using volume specified $UpCase since default is "
                                "not present.");
-               up(&ntfs_lock);
+               mutex_unlock(&ntfs_lock);
                return TRUE;
        }
        max = default_upcase_len;
@@ -1653,12 +1695,12 @@ read_partial_upcase_page:
                vol->upcase = default_upcase;
                vol->upcase_len = max;
                ntfs_nr_upcase_users++;
-               up(&ntfs_lock);
+               mutex_unlock(&ntfs_lock);
                ntfs_debug("Volume specified $UpCase matches default. Using "
                                "default.");
                return TRUE;
        }
-       up(&ntfs_lock);
+       mutex_unlock(&ntfs_lock);
        ntfs_debug("Using volume specified $UpCase since it does not match "
                        "the default.");
        return TRUE;
@@ -1667,21 +1709,29 @@ iput_upcase_failed:
        ntfs_free(vol->upcase);
        vol->upcase = NULL;
 upcase_failed:
-       down(&ntfs_lock);
+       mutex_lock(&ntfs_lock);
        if (default_upcase) {
                vol->upcase = default_upcase;
                vol->upcase_len = default_upcase_len;
                ntfs_nr_upcase_users++;
-               up(&ntfs_lock);
+               mutex_unlock(&ntfs_lock);
                ntfs_error(sb, "Failed to load $UpCase from the volume. Using "
                                "default.");
                return TRUE;
        }
-       up(&ntfs_lock);
+       mutex_unlock(&ntfs_lock);
        ntfs_error(sb, "Failed to initialize upcase table.");
        return FALSE;
 }
 
+/*
+ * The lcn and mft bitmap inodes are NTFS-internal inodes with
+ * their own special locking rules:
+ */
+static struct lock_class_key
+       lcnbmp_runlist_lock_key, lcnbmp_mrec_lock_key,
+       mftbmp_runlist_lock_key, mftbmp_mrec_lock_key;
+
 /**
  * load_system_files - open the system files using normal functions
  * @vol:       ntfs super block describing device whose system files to load
@@ -1738,6 +1788,10 @@ static BOOL load_system_files(ntfs_volume *vol)
                ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
                goto iput_mirr_err_out;
        }
+       lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->runlist.lock,
+                          &mftbmp_runlist_lock_key);
+       lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->mrec_lock,
+                          &mftbmp_mrec_lock_key);
        /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
        if (!load_and_init_upcase(vol))
                goto iput_mftbmp_err_out;
@@ -1760,6 +1814,11 @@ static BOOL load_system_files(ntfs_volume *vol)
                        iput(vol->lcnbmp_ino);
                goto bitmap_failed;
        }
+       lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->runlist.lock,
+                          &lcnbmp_runlist_lock_key);
+       lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->mrec_lock,
+                          &lcnbmp_mrec_lock_key);
+
        NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
        if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
                iput(vol->lcnbmp_ino);
@@ -1822,11 +1881,24 @@ get_ctx_vol_failed:
        /* Make sure that no unsupported volume flags are set. */
        if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
                static const char *es1a = "Volume is dirty";
-               static const char *es1b = "Volume has unsupported flags set";
-               static const char *es2 = ".  Run chkdsk and mount in Windows.";
-               const char *es1;
-               
-               es1 = vol->vol_flags & VOLUME_IS_DIRTY ? es1a : es1b;
+               static const char *es1b = "Volume has been modified by chkdsk";
+               static const char *es1c = "Volume has unsupported flags set";
+               static const char *es2a = ".  Run chkdsk and mount in Windows.";
+               static const char *es2b = ".  Mount in Windows.";
+               const char *es1, *es2;
+
+               es2 = es2a;
+               if (vol->vol_flags & VOLUME_IS_DIRTY)
+                       es1 = es1a;
+               else if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
+                       es1 = es1b;
+                       es2 = es2b;
+               } else {
+                       es1 = es1c;
+                       ntfs_warning(sb, "Unsupported volume flags 0x%x "
+                                       "encountered.",
+                                       (unsigned)le16_to_cpu(vol->vol_flags));
+               }
                /* If a read-write mount, convert it to a read-only mount. */
                if (!(sb->s_flags & MS_RDONLY)) {
                        if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
@@ -2140,12 +2212,12 @@ iput_attrdef_err_out:
 iput_upcase_err_out:
 #endif /* NTFS_RW */
        vol->upcase_len = 0;
-       down(&ntfs_lock);
+       mutex_lock(&ntfs_lock);
        if (vol->upcase == default_upcase) {
                ntfs_nr_upcase_users--;
                vol->upcase = NULL;
        }
-       up(&ntfs_lock);
+       mutex_unlock(&ntfs_lock);
        if (vol->upcase) {
                ntfs_free(vol->upcase);
                vol->upcase = NULL;
@@ -2350,7 +2422,7 @@ static void ntfs_put_super(struct super_block *sb)
         * Destroy the global default upcase table if necessary.  Also decrease
         * the number of upcase users if we are a user.
         */
-       down(&ntfs_lock);
+       mutex_lock(&ntfs_lock);
        if (vol->upcase == default_upcase) {
                ntfs_nr_upcase_users--;
                vol->upcase = NULL;
@@ -2361,7 +2433,7 @@ static void ntfs_put_super(struct super_block *sb)
        }
        if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
                free_compression_buffers();
-       up(&ntfs_lock);
+       mutex_unlock(&ntfs_lock);
        if (vol->upcase) {
                ntfs_free(vol->upcase);
                vol->upcase = NULL;
@@ -2546,10 +2618,10 @@ static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
 
 /**
  * ntfs_statfs - return information about mounted NTFS volume
- * @sb:                super block of mounted volume
+ * @dentry:    dentry from mounted volume
  * @sfs:       statfs structure in which to return the information
  *
- * Return information about the mounted NTFS volume @sb in the statfs structure
+ * Return information about the mounted NTFS volume @dentry in the statfs structure
  * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
  * called). We interpret the values to be correct of the moment in time at
  * which we are called. Most values are variable otherwise and this isn't just
@@ -2562,8 +2634,9 @@ static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
  *
  * Return 0 on success or -errno on error.
  */
-static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs)
+static int ntfs_statfs(struct dentry *dentry, struct kstatfs *sfs)
 {
+       struct super_block *sb = dentry->d_sb;
        s64 size;
        ntfs_volume *vol = NTFS_SB(sb);
        ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
@@ -2685,8 +2758,19 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
        ntfs_volume *vol;
        struct buffer_head *bh;
        struct inode *tmp_ino;
-       int result;
+       int blocksize, result;
 
+       /*
+        * We do a pretty difficult piece of bootstrap by reading the
+        * MFT (and other metadata) from disk into memory. We'll only
+        * release this metadata during umount, so the locking patterns
+        * observed during bootstrap do not count. So turn off the
+        * observation of locking patterns (strictly for this context
+        * only) while mounting NTFS. [The validator is still active
+        * otherwise, even for this context: it will for example record
+        * lock class registrations.]
+        */
+       lockdep_off();
        ntfs_debug("Entering.");
 #ifndef NTFS_RW
        sb->s_flags |= MS_RDONLY;
@@ -2698,6 +2782,7 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
                if (!silent)
                        ntfs_error(sb, "Allocation of NTFS volume structure "
                                        "failed. Aborting mount...");
+               lockdep_on();
                return -ENOMEM;
        }
        /* Initialize ntfs_volume structure. */
@@ -2724,60 +2809,85 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
        if (!parse_options(vol, (char*)opt))
                goto err_out_now;
 
+       /* We support sector sizes up to the PAGE_CACHE_SIZE. */
+       if (bdev_hardsect_size(sb->s_bdev) > PAGE_CACHE_SIZE) {
+               if (!silent)
+                       ntfs_error(sb, "Device has unsupported sector size "
+                                       "(%i).  The maximum supported sector "
+                                       "size on this architecture is %lu "
+                                       "bytes.",
+                                       bdev_hardsect_size(sb->s_bdev),
+                                       PAGE_CACHE_SIZE);
+               goto err_out_now;
+       }
        /*
-        * TODO: Fail safety check. In the future we should really be able to
-        * cope with this being the case, but for now just bail out.
+        * Setup the device access block size to NTFS_BLOCK_SIZE or the hard
+        * sector size, whichever is bigger.
         */
-       if (bdev_hardsect_size(sb->s_bdev) > NTFS_BLOCK_SIZE) {
+       blocksize = sb_min_blocksize(sb, NTFS_BLOCK_SIZE);
+       if (blocksize < NTFS_BLOCK_SIZE) {
                if (!silent)
-                       ntfs_error(sb, "Device has unsupported hardsect_size.");
+                       ntfs_error(sb, "Unable to set device block size.");
                goto err_out_now;
        }
-
-       /* Setup the device access block size to NTFS_BLOCK_SIZE. */
-       if (sb_set_blocksize(sb, NTFS_BLOCK_SIZE) != NTFS_BLOCK_SIZE) {
+       BUG_ON(blocksize != sb->s_blocksize);
+       ntfs_debug("Set device block size to %i bytes (block size bits %i).",
+                       blocksize, sb->s_blocksize_bits);
+       /* Determine the size of the device in units of block_size bytes. */
+       if (!i_size_read(sb->s_bdev->bd_inode)) {
                if (!silent)
-                       ntfs_error(sb, "Unable to set block size.");
+                       ntfs_error(sb, "Unable to determine device size.");
                goto err_out_now;
        }
-
-       /* Get the size of the device in units of NTFS_BLOCK_SIZE bytes. */
        vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
-                       NTFS_BLOCK_SIZE_BITS;
-
+                       sb->s_blocksize_bits;
        /* Read the boot sector and return unlocked buffer head to it. */
        if (!(bh = read_ntfs_boot_sector(sb, silent))) {
                if (!silent)
                        ntfs_error(sb, "Not an NTFS volume.");
                goto err_out_now;
        }
-
        /*
-        * Extract the data from the boot sector and setup the ntfs super block
+        * Extract the data from the boot sector and setup the ntfs volume
         * using it.
         */
        result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
-
-       /* Initialize the cluster and mft allocators. */
-       ntfs_setup_allocators(vol);
-
        brelse(bh);
-
        if (!result) {
                if (!silent)
                        ntfs_error(sb, "Unsupported NTFS filesystem.");
                goto err_out_now;
        }
-
        /*
-        * TODO: When we start coping with sector sizes different from
-        * NTFS_BLOCK_SIZE, we now probably need to set the blocksize of the
-        * device (probably to NTFS_BLOCK_SIZE).
+        * If the boot sector indicates a sector size bigger than the current
+        * device block size, switch the device block size to the sector size.
+        * TODO: It may be possible to support this case even when the set
+        * below fails, we would just be breaking up the i/o for each sector
+        * into multiple blocks for i/o purposes but otherwise it should just
+        * work.  However it is safer to leave disabled until someone hits this
+        * error message and then we can get them to try it without the setting
+        * so we know for sure that it works.
         */
-
+       if (vol->sector_size > blocksize) {
+               blocksize = sb_set_blocksize(sb, vol->sector_size);
+               if (blocksize != vol->sector_size) {
+                       if (!silent)
+                               ntfs_error(sb, "Unable to set device block "
+                                               "size to sector size (%i).",
+                                               vol->sector_size);
+                       goto err_out_now;
+               }
+               BUG_ON(blocksize != sb->s_blocksize);
+               vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
+                               sb->s_blocksize_bits;
+               ntfs_debug("Changed device block size to %i bytes (block size "
+                               "bits %i) to match volume sector size.",
+                               blocksize, sb->s_blocksize_bits);
+       }
+       /* Initialize the cluster and mft allocators. */
+       ntfs_setup_allocators(vol);
        /* Setup remaining fields in the super block. */
        sb->s_magic = NTFS_SB_MAGIC;
-
        /*
         * Ntfs allows 63 bits for the file size, i.e. correct would be:
         *      sb->s_maxbytes = ~0ULL >> 1;
@@ -2787,9 +2897,8 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
         * without overflowing the index or to 2^63 - 1, whichever is smaller.
         */
        sb->s_maxbytes = MAX_LFS_FILESIZE;
-
+       /* Ntfs measures time in 100ns intervals. */
        sb->s_time_gran = 100;
-
        /*
         * Now load the metadata required for the page cache and our address
         * space operations to function. We do this by setting up a specialised
@@ -2811,7 +2920,7 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
                        ntfs_error(sb, "Failed to load essential metadata.");
                goto iput_tmp_ino_err_out_now;
        }
-       down(&ntfs_lock);
+       mutex_lock(&ntfs_lock);
        /*
         * The current mount is a compression user if the cluster size is
         * less than or equal 4kiB.
@@ -2822,7 +2931,7 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
                        ntfs_error(NULL, "Failed to allocate buffers "
                                        "for compression engine.");
                        ntfs_nr_compression_users--;
-                       up(&ntfs_lock);
+                       mutex_unlock(&ntfs_lock);
                        goto iput_tmp_ino_err_out_now;
                }
        }
@@ -2834,7 +2943,7 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
        if (!default_upcase)
                default_upcase = generate_default_upcase();
        ntfs_nr_upcase_users++;
-       up(&ntfs_lock);
+       mutex_unlock(&ntfs_lock);
        /*
         * From now on, ignore @silent parameter. If we fail below this line,
         * it will be due to a corrupt fs or a system error, so we report it.
@@ -2852,14 +2961,15 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
                atomic_inc(&vol->root_ino->i_count);
                ntfs_debug("Exiting, status successful.");
                /* Release the default upcase if it has no users. */
-               down(&ntfs_lock);
+               mutex_lock(&ntfs_lock);
                if (!--ntfs_nr_upcase_users && default_upcase) {
                        ntfs_free(default_upcase);
                        default_upcase = NULL;
                }
-               up(&ntfs_lock);
+               mutex_unlock(&ntfs_lock);
                sb->s_export_op = &ntfs_export_ops;
                lock_kernel();
+               lockdep_on();
                return 0;
        }
        ntfs_error(sb, "Failed to allocate root directory.");
@@ -2925,12 +3035,12 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
                vol->attrdef = NULL;
        }
        vol->upcase_len = 0;
-       down(&ntfs_lock);
+       mutex_lock(&ntfs_lock);
        if (vol->upcase == default_upcase) {
                ntfs_nr_upcase_users--;
                vol->upcase = NULL;
        }
-       up(&ntfs_lock);
+       mutex_unlock(&ntfs_lock);
        if (vol->upcase) {
                ntfs_free(vol->upcase);
                vol->upcase = NULL;
@@ -2945,14 +3055,14 @@ unl_upcase_iput_tmp_ino_err_out_now:
         * Decrease the number of upcase users and destroy the global default
         * upcase table if necessary.
         */
-       down(&ntfs_lock);
+       mutex_lock(&ntfs_lock);
        if (!--ntfs_nr_upcase_users && default_upcase) {
                ntfs_free(default_upcase);
                default_upcase = NULL;
        }
        if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
                free_compression_buffers();
-       up(&ntfs_lock);
+       mutex_unlock(&ntfs_lock);
 iput_tmp_ino_err_out_now:
        iput(tmp_ino);
        if (vol->mft_ino && vol->mft_ino != tmp_ino)
@@ -2979,6 +3089,7 @@ err_out_now:
        sb->s_fs_info = NULL;
        kfree(vol);
        ntfs_debug("Failed, returning -EINVAL.");
+       lockdep_on();
        return -EINVAL;
 }
 
@@ -3011,13 +3122,14 @@ static void ntfs_big_inode_init_once(void *foo, struct kmem_cache *cachep,
 struct kmem_cache *ntfs_attr_ctx_cache;
 struct kmem_cache *ntfs_index_ctx_cache;
 
-/* Driver wide semaphore. */
-DECLARE_MUTEX(ntfs_lock);
+/* Driver wide mutex. */
+DEFINE_MUTEX(ntfs_lock);
 
-static struct super_block *ntfs_get_sb(struct file_system_type *fs_type,
-       int flags, const char *dev_name, void *data)
+static int ntfs_get_sb(struct file_system_type *fs_type,
+       int flags, const char *dev_name, void *data, struct vfsmount *mnt)
 {
-       return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super);
+       return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super,
+                          mnt);
 }
 
 static struct file_system_type ntfs_fs_type = {
@@ -3084,7 +3196,7 @@ static int __init init_ntfs_fs(void)
 
        ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name,
                        sizeof(ntfs_inode), 0,
-                       SLAB_RECLAIM_ACCOUNT, NULL, NULL);
+                       SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL, NULL);
        if (!ntfs_inode_cache) {
                printk(KERN_CRIT "NTFS: Failed to create %s!\n",
                                ntfs_inode_cache_name);
@@ -3093,7 +3205,7 @@ static int __init init_ntfs_fs(void)
 
        ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name,
                        sizeof(big_ntfs_inode), 0,
-                       SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
+                       SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
                        ntfs_big_inode_init_once, NULL);
        if (!ntfs_big_inode_cache) {
                printk(KERN_CRIT "NTFS: Failed to create %s!\n",
@@ -3136,38 +3248,20 @@ ictx_err_out:
 
 static void __exit exit_ntfs_fs(void)
 {
-       int err = 0;
-
        ntfs_debug("Unregistering NTFS driver.");
 
        unregister_filesystem(&ntfs_fs_type);
-
-       if (kmem_cache_destroy(ntfs_big_inode_cache) && (err = 1))
-               printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
-                               ntfs_big_inode_cache_name);
-       if (kmem_cache_destroy(ntfs_inode_cache) && (err = 1))
-               printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
-                               ntfs_inode_cache_name);
-       if (kmem_cache_destroy(ntfs_name_cache) && (err = 1))
-               printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
-                               ntfs_name_cache_name);
-       if (kmem_cache_destroy(ntfs_attr_ctx_cache) && (err = 1))
-               printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
-                               ntfs_attr_ctx_cache_name);
-       if (kmem_cache_destroy(ntfs_index_ctx_cache) && (err = 1))
-               printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
-                               ntfs_index_ctx_cache_name);
-       if (err)
-               printk(KERN_CRIT "NTFS: This causes memory to leak! There is "
-                               "probably a BUG in the driver! Please report "
-                               "you saw this message to "
-                               "linux-ntfs-dev@lists.sourceforge.net\n");
+       kmem_cache_destroy(ntfs_big_inode_cache);
+       kmem_cache_destroy(ntfs_inode_cache);
+       kmem_cache_destroy(ntfs_name_cache);
+       kmem_cache_destroy(ntfs_attr_ctx_cache);
+       kmem_cache_destroy(ntfs_index_ctx_cache);
        /* Unregister the ntfs sysctls. */
        ntfs_sysctl(0);
 }
 
 MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
-MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2005 Anton Altaparmakov");
+MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2006 Anton Altaparmakov");
 MODULE_VERSION(NTFS_VERSION);
 MODULE_LICENSE("GPL");
 #ifdef DEBUG