aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ntfs/super.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ntfs/super.c')
-rw-r--r--fs/ntfs/super.c197
1 files changed, 132 insertions, 65 deletions
diff --git a/fs/ntfs/super.c b/fs/ntfs/super.c
index c3a3f1a8310b..368a8ec10668 100644
--- a/fs/ntfs/super.c
+++ b/fs/ntfs/super.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project. 2 * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project.
3 * 3 *
4 * Copyright (c) 2001-2005 Anton Altaparmakov 4 * Copyright (c) 2001-2006 Anton Altaparmakov
5 * Copyright (c) 2001,2002 Richard Russon 5 * Copyright (c) 2001,2002 Richard Russon
6 * 6 *
7 * This program/include file is free software; you can redistribute it and/or 7 * This program/include file is free software; you can redistribute it and/or
@@ -22,6 +22,7 @@
22 22
23#include <linux/stddef.h> 23#include <linux/stddef.h>
24#include <linux/init.h> 24#include <linux/init.h>
25#include <linux/slab.h>
25#include <linux/string.h> 26#include <linux/string.h>
26#include <linux/spinlock.h> 27#include <linux/spinlock.h>
27#include <linux/blkdev.h> /* For bdev_hardsect_size(). */ 28#include <linux/blkdev.h> /* For bdev_hardsect_size(). */
@@ -471,9 +472,16 @@ static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
471 ntfs_error(sb, "Volume is dirty and read-only%s", es); 472 ntfs_error(sb, "Volume is dirty and read-only%s", es);
472 return -EROFS; 473 return -EROFS;
473 } 474 }
475 if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
476 ntfs_error(sb, "Volume has been modified by chkdsk "
477 "and is read-only%s", es);
478 return -EROFS;
479 }
474 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) { 480 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
475 ntfs_error(sb, "Volume has unsupported flags set and " 481 ntfs_error(sb, "Volume has unsupported flags set "
476 "is read-only%s", es); 482 "(0x%x) and is read-only%s",
483 (unsigned)le16_to_cpu(vol->vol_flags),
484 es);
477 return -EROFS; 485 return -EROFS;
478 } 486 }
479 if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) { 487 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,
641{ 649{
642 const char *read_err_str = "Unable to read %s boot sector."; 650 const char *read_err_str = "Unable to read %s boot sector.";
643 struct buffer_head *bh_primary, *bh_backup; 651 struct buffer_head *bh_primary, *bh_backup;
644 long nr_blocks = NTFS_SB(sb)->nr_blocks; 652 sector_t nr_blocks = NTFS_SB(sb)->nr_blocks;
645 653
646 /* Try to read primary boot sector. */ 654 /* Try to read primary boot sector. */
647 if ((bh_primary = sb_bread(sb, 0))) { 655 if ((bh_primary = sb_bread(sb, 0))) {
@@ -688,13 +696,18 @@ hotfix_primary_boot_sector:
688 /* 696 /*
689 * If we managed to read sector zero and the volume is not 697 * If we managed to read sector zero and the volume is not
690 * read-only, copy the found, valid backup boot sector to the 698 * read-only, copy the found, valid backup boot sector to the
691 * primary boot sector. 699 * primary boot sector. Note we only copy the actual boot
700 * sector structure, not the actual whole device sector as that
701 * may be bigger and would potentially damage the $Boot system
702 * file (FIXME: Would be nice to know if the backup boot sector
703 * on a large sector device contains the whole boot loader or
704 * just the first 512 bytes).
692 */ 705 */
693 if (!(sb->s_flags & MS_RDONLY)) { 706 if (!(sb->s_flags & MS_RDONLY)) {
694 ntfs_warning(sb, "Hot-fix: Recovering invalid primary " 707 ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
695 "boot sector from backup copy."); 708 "boot sector from backup copy.");
696 memcpy(bh_primary->b_data, bh_backup->b_data, 709 memcpy(bh_primary->b_data, bh_backup->b_data,
697 sb->s_blocksize); 710 NTFS_BLOCK_SIZE);
698 mark_buffer_dirty(bh_primary); 711 mark_buffer_dirty(bh_primary);
699 sync_dirty_buffer(bh_primary); 712 sync_dirty_buffer(bh_primary);
700 if (buffer_uptodate(bh_primary)) { 713 if (buffer_uptodate(bh_primary)) {
@@ -733,9 +746,13 @@ static BOOL parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
733 vol->sector_size); 746 vol->sector_size);
734 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits, 747 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
735 vol->sector_size_bits); 748 vol->sector_size_bits);
736 if (vol->sector_size != vol->sb->s_blocksize) 749 if (vol->sector_size < vol->sb->s_blocksize) {
737 ntfs_warning(vol->sb, "The boot sector indicates a sector size " 750 ntfs_error(vol->sb, "Sector size (%i) is smaller than the "
738 "different from the device sector size."); 751 "device block size (%lu). This is not "
752 "supported. Sorry.", vol->sector_size,
753 vol->sb->s_blocksize);
754 return FALSE;
755 }
739 ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster); 756 ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
740 sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1; 757 sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
741 ntfs_debug("sectors_per_cluster_bits = 0x%x", 758 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)
748 ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size, 765 ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
749 vol->cluster_size); 766 vol->cluster_size);
750 ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask); 767 ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
751 ntfs_debug("vol->cluster_size_bits = %i (0x%x)", 768 ntfs_debug("vol->cluster_size_bits = %i", vol->cluster_size_bits);
752 vol->cluster_size_bits, vol->cluster_size_bits); 769 if (vol->cluster_size < vol->sector_size) {
753 if (vol->sector_size > vol->cluster_size) { 770 ntfs_error(vol->sb, "Cluster size (%i) is smaller than the "
754 ntfs_error(vol->sb, "Sector sizes above the cluster size are " 771 "sector size (%i). This is not supported. "
755 "not supported. Sorry."); 772 "Sorry.", vol->cluster_size, vol->sector_size);
756 return FALSE;
757 }
758 if (vol->sb->s_blocksize > vol->cluster_size) {
759 ntfs_error(vol->sb, "Cluster sizes smaller than the device "
760 "sector size are not supported. Sorry.");
761 return FALSE; 773 return FALSE;
762 } 774 }
763 clusters_per_mft_record = b->clusters_per_mft_record; 775 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)
786 * we store $MFT/$DATA, the table of mft records in the page cache. 798 * we store $MFT/$DATA, the table of mft records in the page cache.
787 */ 799 */
788 if (vol->mft_record_size > PAGE_CACHE_SIZE) { 800 if (vol->mft_record_size > PAGE_CACHE_SIZE) {
789 ntfs_error(vol->sb, "Mft record size %i (0x%x) exceeds the " 801 ntfs_error(vol->sb, "Mft record size (%i) exceeds the "
790 "page cache size on your system %lu (0x%lx). " 802 "PAGE_CACHE_SIZE on your system (%lu). "
791 "This is not supported. Sorry.", 803 "This is not supported. Sorry.",
792 vol->mft_record_size, vol->mft_record_size, 804 vol->mft_record_size, PAGE_CACHE_SIZE);
793 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE); 805 return FALSE;
806 }
807 /* We cannot support mft record sizes below the sector size. */
808 if (vol->mft_record_size < vol->sector_size) {
809 ntfs_error(vol->sb, "Mft record size (%i) is smaller than the "
810 "sector size (%i). This is not supported. "
811 "Sorry.", vol->mft_record_size,
812 vol->sector_size);
794 return FALSE; 813 return FALSE;
795 } 814 }
796 clusters_per_index_record = b->clusters_per_index_record; 815 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)
816 ntfs_debug("vol->index_record_size_bits = %i (0x%x)", 835 ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
817 vol->index_record_size_bits, 836 vol->index_record_size_bits,
818 vol->index_record_size_bits); 837 vol->index_record_size_bits);
838 /* We cannot support index record sizes below the sector size. */
839 if (vol->index_record_size < vol->sector_size) {
840 ntfs_error(vol->sb, "Index record size (%i) is smaller than "
841 "the sector size (%i). This is not "
842 "supported. Sorry.", vol->index_record_size,
843 vol->sector_size);
844 return FALSE;
845 }
819 /* 846 /*
820 * Get the size of the volume in clusters and check for 64-bit-ness. 847 * Get the size of the volume in clusters and check for 64-bit-ness.
821 * Windows currently only uses 32 bits to save the clusters so we do 848 * 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)
845 } 872 }
846 ll = sle64_to_cpu(b->mft_lcn); 873 ll = sle64_to_cpu(b->mft_lcn);
847 if (ll >= vol->nr_clusters) { 874 if (ll >= vol->nr_clusters) {
848 ntfs_error(vol->sb, "MFT LCN is beyond end of volume. Weird."); 875 ntfs_error(vol->sb, "MFT LCN (%lli, 0x%llx) is beyond end of "
876 "volume. Weird.", (unsigned long long)ll,
877 (unsigned long long)ll);
849 return FALSE; 878 return FALSE;
850 } 879 }
851 vol->mft_lcn = ll; 880 vol->mft_lcn = ll;
852 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn); 881 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
853 ll = sle64_to_cpu(b->mftmirr_lcn); 882 ll = sle64_to_cpu(b->mftmirr_lcn);
854 if (ll >= vol->nr_clusters) { 883 if (ll >= vol->nr_clusters) {
855 ntfs_error(vol->sb, "MFTMirr LCN is beyond end of volume. " 884 ntfs_error(vol->sb, "MFTMirr LCN (%lli, 0x%llx) is beyond end "
856 "Weird."); 885 "of volume. Weird.", (unsigned long long)ll,
886 (unsigned long long)ll);
857 return FALSE; 887 return FALSE;
858 } 888 }
859 vol->mftmirr_lcn = ll; 889 vol->mftmirr_lcn = ll;
@@ -1822,11 +1852,24 @@ get_ctx_vol_failed:
1822 /* Make sure that no unsupported volume flags are set. */ 1852 /* Make sure that no unsupported volume flags are set. */
1823 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) { 1853 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
1824 static const char *es1a = "Volume is dirty"; 1854 static const char *es1a = "Volume is dirty";
1825 static const char *es1b = "Volume has unsupported flags set"; 1855 static const char *es1b = "Volume has been modified by chkdsk";
1826 static const char *es2 = ". Run chkdsk and mount in Windows."; 1856 static const char *es1c = "Volume has unsupported flags set";
1827 const char *es1; 1857 static const char *es2a = ". Run chkdsk and mount in Windows.";
1828 1858 static const char *es2b = ". Mount in Windows.";
1829 es1 = vol->vol_flags & VOLUME_IS_DIRTY ? es1a : es1b; 1859 const char *es1, *es2;
1860
1861 es2 = es2a;
1862 if (vol->vol_flags & VOLUME_IS_DIRTY)
1863 es1 = es1a;
1864 else if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
1865 es1 = es1b;
1866 es2 = es2b;
1867 } else {
1868 es1 = es1c;
1869 ntfs_warning(sb, "Unsupported volume flags 0x%x "
1870 "encountered.",
1871 (unsigned)le16_to_cpu(vol->vol_flags));
1872 }
1830 /* If a read-write mount, convert it to a read-only mount. */ 1873 /* If a read-write mount, convert it to a read-only mount. */
1831 if (!(sb->s_flags & MS_RDONLY)) { 1874 if (!(sb->s_flags & MS_RDONLY)) {
1832 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO | 1875 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
@@ -2685,7 +2728,7 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2685 ntfs_volume *vol; 2728 ntfs_volume *vol;
2686 struct buffer_head *bh; 2729 struct buffer_head *bh;
2687 struct inode *tmp_ino; 2730 struct inode *tmp_ino;
2688 int result; 2731 int blocksize, result;
2689 2732
2690 ntfs_debug("Entering."); 2733 ntfs_debug("Entering.");
2691#ifndef NTFS_RW 2734#ifndef NTFS_RW
@@ -2724,60 +2767,85 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2724 if (!parse_options(vol, (char*)opt)) 2767 if (!parse_options(vol, (char*)opt))
2725 goto err_out_now; 2768 goto err_out_now;
2726 2769
2770 /* We support sector sizes up to the PAGE_CACHE_SIZE. */
2771 if (bdev_hardsect_size(sb->s_bdev) > PAGE_CACHE_SIZE) {
2772 if (!silent)
2773 ntfs_error(sb, "Device has unsupported sector size "
2774 "(%i). The maximum supported sector "
2775 "size on this architecture is %lu "
2776 "bytes.",
2777 bdev_hardsect_size(sb->s_bdev),
2778 PAGE_CACHE_SIZE);
2779 goto err_out_now;
2780 }
2727 /* 2781 /*
2728 * TODO: Fail safety check. In the future we should really be able to 2782 * Setup the device access block size to NTFS_BLOCK_SIZE or the hard
2729 * cope with this being the case, but for now just bail out. 2783 * sector size, whichever is bigger.
2730 */ 2784 */
2731 if (bdev_hardsect_size(sb->s_bdev) > NTFS_BLOCK_SIZE) { 2785 blocksize = sb_min_blocksize(sb, NTFS_BLOCK_SIZE);
2786 if (blocksize < NTFS_BLOCK_SIZE) {
2732 if (!silent) 2787 if (!silent)
2733 ntfs_error(sb, "Device has unsupported hardsect_size."); 2788 ntfs_error(sb, "Unable to set device block size.");
2734 goto err_out_now; 2789 goto err_out_now;
2735 } 2790 }
2736 2791 BUG_ON(blocksize != sb->s_blocksize);
2737 /* Setup the device access block size to NTFS_BLOCK_SIZE. */ 2792 ntfs_debug("Set device block size to %i bytes (block size bits %i).",
2738 if (sb_set_blocksize(sb, NTFS_BLOCK_SIZE) != NTFS_BLOCK_SIZE) { 2793 blocksize, sb->s_blocksize_bits);
2794 /* Determine the size of the device in units of block_size bytes. */
2795 if (!i_size_read(sb->s_bdev->bd_inode)) {
2739 if (!silent) 2796 if (!silent)
2740 ntfs_error(sb, "Unable to set block size."); 2797 ntfs_error(sb, "Unable to determine device size.");
2741 goto err_out_now; 2798 goto err_out_now;
2742 } 2799 }
2743
2744 /* Get the size of the device in units of NTFS_BLOCK_SIZE bytes. */
2745 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >> 2800 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2746 NTFS_BLOCK_SIZE_BITS; 2801 sb->s_blocksize_bits;
2747
2748 /* Read the boot sector and return unlocked buffer head to it. */ 2802 /* Read the boot sector and return unlocked buffer head to it. */
2749 if (!(bh = read_ntfs_boot_sector(sb, silent))) { 2803 if (!(bh = read_ntfs_boot_sector(sb, silent))) {
2750 if (!silent) 2804 if (!silent)
2751 ntfs_error(sb, "Not an NTFS volume."); 2805 ntfs_error(sb, "Not an NTFS volume.");
2752 goto err_out_now; 2806 goto err_out_now;
2753 } 2807 }
2754
2755 /* 2808 /*
2756 * Extract the data from the boot sector and setup the ntfs super block 2809 * Extract the data from the boot sector and setup the ntfs volume
2757 * using it. 2810 * using it.
2758 */ 2811 */
2759 result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data); 2812 result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
2760
2761 /* Initialize the cluster and mft allocators. */
2762 ntfs_setup_allocators(vol);
2763
2764 brelse(bh); 2813 brelse(bh);
2765
2766 if (!result) { 2814 if (!result) {
2767 if (!silent) 2815 if (!silent)
2768 ntfs_error(sb, "Unsupported NTFS filesystem."); 2816 ntfs_error(sb, "Unsupported NTFS filesystem.");
2769 goto err_out_now; 2817 goto err_out_now;
2770 } 2818 }
2771
2772 /* 2819 /*
2773 * TODO: When we start coping with sector sizes different from 2820 * If the boot sector indicates a sector size bigger than the current
2774 * NTFS_BLOCK_SIZE, we now probably need to set the blocksize of the 2821 * device block size, switch the device block size to the sector size.
2775 * device (probably to NTFS_BLOCK_SIZE). 2822 * TODO: It may be possible to support this case even when the set
2823 * below fails, we would just be breaking up the i/o for each sector
2824 * into multiple blocks for i/o purposes but otherwise it should just
2825 * work. However it is safer to leave disabled until someone hits this
2826 * error message and then we can get them to try it without the setting
2827 * so we know for sure that it works.
2776 */ 2828 */
2777 2829 if (vol->sector_size > blocksize) {
2830 blocksize = sb_set_blocksize(sb, vol->sector_size);
2831 if (blocksize != vol->sector_size) {
2832 if (!silent)
2833 ntfs_error(sb, "Unable to set device block "
2834 "size to sector size (%i).",
2835 vol->sector_size);
2836 goto err_out_now;
2837 }
2838 BUG_ON(blocksize != sb->s_blocksize);
2839 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2840 sb->s_blocksize_bits;
2841 ntfs_debug("Changed device block size to %i bytes (block size "
2842 "bits %i) to match volume sector size.",
2843 blocksize, sb->s_blocksize_bits);
2844 }
2845 /* Initialize the cluster and mft allocators. */
2846 ntfs_setup_allocators(vol);
2778 /* Setup remaining fields in the super block. */ 2847 /* Setup remaining fields in the super block. */
2779 sb->s_magic = NTFS_SB_MAGIC; 2848 sb->s_magic = NTFS_SB_MAGIC;
2780
2781 /* 2849 /*
2782 * Ntfs allows 63 bits for the file size, i.e. correct would be: 2850 * Ntfs allows 63 bits for the file size, i.e. correct would be:
2783 * sb->s_maxbytes = ~0ULL >> 1; 2851 * sb->s_maxbytes = ~0ULL >> 1;
@@ -2787,9 +2855,8 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2787 * without overflowing the index or to 2^63 - 1, whichever is smaller. 2855 * without overflowing the index or to 2^63 - 1, whichever is smaller.
2788 */ 2856 */
2789 sb->s_maxbytes = MAX_LFS_FILESIZE; 2857 sb->s_maxbytes = MAX_LFS_FILESIZE;
2790 2858 /* Ntfs measures time in 100ns intervals. */
2791 sb->s_time_gran = 100; 2859 sb->s_time_gran = 100;
2792
2793 /* 2860 /*
2794 * Now load the metadata required for the page cache and our address 2861 * Now load the metadata required for the page cache and our address
2795 * space operations to function. We do this by setting up a specialised 2862 * space operations to function. We do this by setting up a specialised
@@ -2987,14 +3054,14 @@ err_out_now:
2987 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN 3054 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
2988 * (255) Unicode characters + a terminating NULL Unicode character. 3055 * (255) Unicode characters + a terminating NULL Unicode character.
2989 */ 3056 */
2990kmem_cache_t *ntfs_name_cache; 3057struct kmem_cache *ntfs_name_cache;
2991 3058
2992/* Slab caches for efficient allocation/deallocation of inodes. */ 3059/* Slab caches for efficient allocation/deallocation of inodes. */
2993kmem_cache_t *ntfs_inode_cache; 3060struct kmem_cache *ntfs_inode_cache;
2994kmem_cache_t *ntfs_big_inode_cache; 3061struct kmem_cache *ntfs_big_inode_cache;
2995 3062
2996/* Init once constructor for the inode slab cache. */ 3063/* Init once constructor for the inode slab cache. */
2997static void ntfs_big_inode_init_once(void *foo, kmem_cache_t *cachep, 3064static void ntfs_big_inode_init_once(void *foo, struct kmem_cache *cachep,
2998 unsigned long flags) 3065 unsigned long flags)
2999{ 3066{
3000 ntfs_inode *ni = (ntfs_inode *)foo; 3067 ntfs_inode *ni = (ntfs_inode *)foo;
@@ -3008,8 +3075,8 @@ static void ntfs_big_inode_init_once(void *foo, kmem_cache_t *cachep,
3008 * Slab caches to optimize allocations and deallocations of attribute search 3075 * Slab caches to optimize allocations and deallocations of attribute search
3009 * contexts and index contexts, respectively. 3076 * contexts and index contexts, respectively.
3010 */ 3077 */
3011kmem_cache_t *ntfs_attr_ctx_cache; 3078struct kmem_cache *ntfs_attr_ctx_cache;
3012kmem_cache_t *ntfs_index_ctx_cache; 3079struct kmem_cache *ntfs_index_ctx_cache;
3013 3080
3014/* Driver wide semaphore. */ 3081/* Driver wide semaphore. */
3015DECLARE_MUTEX(ntfs_lock); 3082DECLARE_MUTEX(ntfs_lock);