diff options
author | Anton Altaparmakov <aia21@cantab.net> | 2005-09-08 16:09:06 -0400 |
---|---|---|
committer | Anton Altaparmakov <aia21@cantab.net> | 2005-09-08 16:09:06 -0400 |
commit | bbf1813fb8ff9d21171bf22e6d1f0e0393601e86 (patch) | |
tree | 2b8474019c1320df290d5df03514eaed56e44cc6 /fs | |
parent | 807c453de7c5487d2e5eece76bafdea8f39d249e (diff) |
NTFS: Fix cluster (de)allocators to work when the runlist is NULL and more
importantly to take a locked runlist rather than them locking it
which leads to lock reversal.
Signed-off-by: Anton Altaparmakov <aia21@cantab.net>
Diffstat (limited to 'fs')
-rw-r--r-- | fs/ntfs/ChangeLog | 3 | ||||
-rw-r--r-- | fs/ntfs/lcnalloc.c | 39 | ||||
-rw-r--r-- | fs/ntfs/lcnalloc.h | 21 | ||||
-rw-r--r-- | fs/ntfs/mft.c | 2 |
4 files changed, 32 insertions, 33 deletions
diff --git a/fs/ntfs/ChangeLog b/fs/ntfs/ChangeLog index b0cc43bd2974..beed90c9f551 100644 --- a/fs/ntfs/ChangeLog +++ b/fs/ntfs/ChangeLog | |||
@@ -70,6 +70,9 @@ ToDo/Notes: | |||
70 | - Add BUG() checks to ntfs_attr_make_non_resident() and ntfs_attr_set() | 70 | - Add BUG() checks to ntfs_attr_make_non_resident() and ntfs_attr_set() |
71 | to ensure that these functions are never called for compressed or | 71 | to ensure that these functions are never called for compressed or |
72 | encrypted attributes. | 72 | encrypted attributes. |
73 | - Fix cluster (de)allocators to work when the runlist is NULL and more | ||
74 | importantly to take a locked runlist rather than them locking it | ||
75 | which leads to lock reversal. | ||
73 | 76 | ||
74 | 2.1.23 - Implement extension of resident files and make writing safe as well as | 77 | 2.1.23 - Implement extension of resident files and make writing safe as well as |
75 | many bug fixes, cleanups, and enhancements... | 78 | many bug fixes, cleanups, and enhancements... |
diff --git a/fs/ntfs/lcnalloc.c b/fs/ntfs/lcnalloc.c index a4bc07616e5d..7b5934290685 100644 --- a/fs/ntfs/lcnalloc.c +++ b/fs/ntfs/lcnalloc.c | |||
@@ -54,6 +54,8 @@ int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol, | |||
54 | int ret = 0; | 54 | int ret = 0; |
55 | 55 | ||
56 | ntfs_debug("Entering."); | 56 | ntfs_debug("Entering."); |
57 | if (!rl) | ||
58 | return 0; | ||
57 | for (; rl->length; rl++) { | 59 | for (; rl->length; rl++) { |
58 | int err; | 60 | int err; |
59 | 61 | ||
@@ -163,17 +165,9 @@ runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, const VCN start_vcn, | |||
163 | BUG_ON(zone < FIRST_ZONE); | 165 | BUG_ON(zone < FIRST_ZONE); |
164 | BUG_ON(zone > LAST_ZONE); | 166 | BUG_ON(zone > LAST_ZONE); |
165 | 167 | ||
166 | /* Return empty runlist if @count == 0 */ | 168 | /* Return NULL if @count is zero. */ |
167 | // FIXME: Do we want to just return NULL instead? (AIA) | 169 | if (!count) |
168 | if (!count) { | 170 | return NULL; |
169 | rl = ntfs_malloc_nofs(PAGE_SIZE); | ||
170 | if (!rl) | ||
171 | return ERR_PTR(-ENOMEM); | ||
172 | rl[0].vcn = start_vcn; | ||
173 | rl[0].lcn = LCN_RL_NOT_MAPPED; | ||
174 | rl[0].length = 0; | ||
175 | return rl; | ||
176 | } | ||
177 | /* Take the lcnbmp lock for writing. */ | 171 | /* Take the lcnbmp lock for writing. */ |
178 | down_write(&vol->lcnbmp_lock); | 172 | down_write(&vol->lcnbmp_lock); |
179 | /* | 173 | /* |
@@ -788,7 +782,8 @@ out: | |||
788 | * @vi: vfs inode whose runlist describes the clusters to free | 782 | * @vi: vfs inode whose runlist describes the clusters to free |
789 | * @start_vcn: vcn in the runlist of @vi at which to start freeing clusters | 783 | * @start_vcn: vcn in the runlist of @vi at which to start freeing clusters |
790 | * @count: number of clusters to free or -1 for all clusters | 784 | * @count: number of clusters to free or -1 for all clusters |
791 | * @is_rollback: if TRUE this is a rollback operation | 785 | * @write_locked: true if the runlist is locked for writing |
786 | * @is_rollback: true if this is a rollback operation | ||
792 | * | 787 | * |
793 | * Free @count clusters starting at the cluster @start_vcn in the runlist | 788 | * Free @count clusters starting at the cluster @start_vcn in the runlist |
794 | * described by the vfs inode @vi. | 789 | * described by the vfs inode @vi. |
@@ -806,17 +801,17 @@ out: | |||
806 | * Return the number of deallocated clusters (not counting sparse ones) on | 801 | * Return the number of deallocated clusters (not counting sparse ones) on |
807 | * success and -errno on error. | 802 | * success and -errno on error. |
808 | * | 803 | * |
809 | * Locking: - The runlist described by @vi must be unlocked on entry and is | 804 | * Locking: - The runlist described by @vi must be locked on entry and is |
810 | * unlocked on return. | 805 | * locked on return. Note if the runlist is locked for reading the |
811 | * - This function takes the runlist lock of @vi for reading and | 806 | * lock may be dropped and reacquired. Note the runlist may be |
812 | * sometimes for writing and sometimes modifies the runlist. | 807 | * modified when needed runlist fragments need to be mapped. |
813 | * - The volume lcn bitmap must be unlocked on entry and is unlocked | 808 | * - The volume lcn bitmap must be unlocked on entry and is unlocked |
814 | * on return. | 809 | * on return. |
815 | * - This function takes the volume lcn bitmap lock for writing and | 810 | * - This function takes the volume lcn bitmap lock for writing and |
816 | * modifies the bitmap contents. | 811 | * modifies the bitmap contents. |
817 | */ | 812 | */ |
818 | s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count, | 813 | s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count, |
819 | const BOOL is_rollback) | 814 | const BOOL write_locked, const BOOL is_rollback) |
820 | { | 815 | { |
821 | s64 delta, to_free, total_freed, real_freed; | 816 | s64 delta, to_free, total_freed, real_freed; |
822 | ntfs_inode *ni; | 817 | ntfs_inode *ni; |
@@ -848,8 +843,7 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count, | |||
848 | 843 | ||
849 | total_freed = real_freed = 0; | 844 | total_freed = real_freed = 0; |
850 | 845 | ||
851 | down_read(&ni->runlist.lock); | 846 | rl = ntfs_attr_find_vcn_nolock(ni, start_vcn, write_locked); |
852 | rl = ntfs_attr_find_vcn_nolock(ni, start_vcn, FALSE); | ||
853 | if (IS_ERR(rl)) { | 847 | if (IS_ERR(rl)) { |
854 | if (!is_rollback) | 848 | if (!is_rollback) |
855 | ntfs_error(vol->sb, "Failed to find first runlist " | 849 | ntfs_error(vol->sb, "Failed to find first runlist " |
@@ -903,7 +897,7 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count, | |||
903 | 897 | ||
904 | /* Attempt to map runlist. */ | 898 | /* Attempt to map runlist. */ |
905 | vcn = rl->vcn; | 899 | vcn = rl->vcn; |
906 | rl = ntfs_attr_find_vcn_nolock(ni, vcn, FALSE); | 900 | rl = ntfs_attr_find_vcn_nolock(ni, vcn, write_locked); |
907 | if (IS_ERR(rl)) { | 901 | if (IS_ERR(rl)) { |
908 | err = PTR_ERR(rl); | 902 | err = PTR_ERR(rl); |
909 | if (!is_rollback) | 903 | if (!is_rollback) |
@@ -950,7 +944,6 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count, | |||
950 | /* Update the total done clusters. */ | 944 | /* Update the total done clusters. */ |
951 | total_freed += to_free; | 945 | total_freed += to_free; |
952 | } | 946 | } |
953 | up_read(&ni->runlist.lock); | ||
954 | if (likely(!is_rollback)) | 947 | if (likely(!is_rollback)) |
955 | up_write(&vol->lcnbmp_lock); | 948 | up_write(&vol->lcnbmp_lock); |
956 | 949 | ||
@@ -960,7 +953,6 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count, | |||
960 | ntfs_debug("Done."); | 953 | ntfs_debug("Done."); |
961 | return real_freed; | 954 | return real_freed; |
962 | err_out: | 955 | err_out: |
963 | up_read(&ni->runlist.lock); | ||
964 | if (is_rollback) | 956 | if (is_rollback) |
965 | return err; | 957 | return err; |
966 | /* If no real clusters were freed, no need to rollback. */ | 958 | /* If no real clusters were freed, no need to rollback. */ |
@@ -973,7 +965,8 @@ err_out: | |||
973 | * If rollback fails, set the volume errors flag, emit an error | 965 | * If rollback fails, set the volume errors flag, emit an error |
974 | * message, and return the error code. | 966 | * message, and return the error code. |
975 | */ | 967 | */ |
976 | delta = __ntfs_cluster_free(vi, start_vcn, total_freed, TRUE); | 968 | delta = __ntfs_cluster_free(vi, start_vcn, total_freed, write_locked, |
969 | TRUE); | ||
977 | if (delta < 0) { | 970 | if (delta < 0) { |
978 | ntfs_error(vol->sb, "Failed to rollback (error %i). Leaving " | 971 | ntfs_error(vol->sb, "Failed to rollback (error %i). Leaving " |
979 | "inconsistent metadata! Unmount and run " | 972 | "inconsistent metadata! Unmount and run " |
diff --git a/fs/ntfs/lcnalloc.h b/fs/ntfs/lcnalloc.h index 4cac1c024af6..e4d7fb98d685 100644 --- a/fs/ntfs/lcnalloc.h +++ b/fs/ntfs/lcnalloc.h | |||
@@ -43,13 +43,14 @@ extern runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, | |||
43 | const NTFS_CLUSTER_ALLOCATION_ZONES zone); | 43 | const NTFS_CLUSTER_ALLOCATION_ZONES zone); |
44 | 44 | ||
45 | extern s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, | 45 | extern s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, |
46 | s64 count, const BOOL is_rollback); | 46 | s64 count, const BOOL write_locked, const BOOL is_rollback); |
47 | 47 | ||
48 | /** | 48 | /** |
49 | * ntfs_cluster_free - free clusters on an ntfs volume | 49 | * ntfs_cluster_free - free clusters on an ntfs volume |
50 | * @vi: vfs inode whose runlist describes the clusters to free | 50 | * @vi: vfs inode whose runlist describes the clusters to free |
51 | * @start_vcn: vcn in the runlist of @vi at which to start freeing clusters | 51 | * @start_vcn: vcn in the runlist of @vi at which to start freeing clusters |
52 | * @count: number of clusters to free or -1 for all clusters | 52 | * @count: number of clusters to free or -1 for all clusters |
53 | * @write_locked: true if the runlist is locked for writing | ||
53 | * | 54 | * |
54 | * Free @count clusters starting at the cluster @start_vcn in the runlist | 55 | * Free @count clusters starting at the cluster @start_vcn in the runlist |
55 | * described by the vfs inode @vi. | 56 | * described by the vfs inode @vi. |
@@ -64,19 +65,19 @@ extern s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, | |||
64 | * Return the number of deallocated clusters (not counting sparse ones) on | 65 | * Return the number of deallocated clusters (not counting sparse ones) on |
65 | * success and -errno on error. | 66 | * success and -errno on error. |
66 | * | 67 | * |
67 | * Locking: - The runlist described by @vi must be unlocked on entry and is | 68 | * Locking: - The runlist described by @vi must be locked on entry and is |
68 | * unlocked on return. | 69 | * locked on return. Note if the runlist is locked for reading the |
69 | * - This function takes the runlist lock of @vi for reading and | 70 | * lock may be dropped and reacquired. Note the runlist may be |
70 | * sometimes for writing and sometimes modifies the runlist. | 71 | * modified when needed runlist fragments need to be mapped. |
71 | * - The volume lcn bitmap must be unlocked on entry and is unlocked | 72 | * - The volume lcn bitmap must be unlocked on entry and is unlocked |
72 | * on return. | 73 | * on return. |
73 | * - This function takes the volume lcn bitmap lock for writing and | 74 | * - This function takes the volume lcn bitmap lock for writing and |
74 | * modifies the bitmap contents. | 75 | * modifies the bitmap contents. |
75 | */ | 76 | */ |
76 | static inline s64 ntfs_cluster_free(struct inode *vi, const VCN start_vcn, | 77 | static inline s64 ntfs_cluster_free(struct inode *vi, const VCN start_vcn, |
77 | s64 count) | 78 | s64 count, const BOOL write_locked) |
78 | { | 79 | { |
79 | return __ntfs_cluster_free(vi, start_vcn, count, FALSE); | 80 | return __ntfs_cluster_free(vi, start_vcn, count, write_locked, FALSE); |
80 | } | 81 | } |
81 | 82 | ||
82 | extern int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol, | 83 | extern int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol, |
@@ -93,8 +94,10 @@ extern int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol, | |||
93 | * | 94 | * |
94 | * Return 0 on success and -errno on error. | 95 | * Return 0 on success and -errno on error. |
95 | * | 96 | * |
96 | * Locking: This function takes the volume lcn bitmap lock for writing and | 97 | * Locking: - This function takes the volume lcn bitmap lock for writing and |
97 | * modifies the bitmap contents. | 98 | * modifies the bitmap contents. |
99 | * - The caller must have locked the runlist @rl for reading or | ||
100 | * writing. | ||
98 | */ | 101 | */ |
99 | static inline int ntfs_cluster_free_from_rl(ntfs_volume *vol, | 102 | static inline int ntfs_cluster_free_from_rl(ntfs_volume *vol, |
100 | const runlist_element *rl) | 103 | const runlist_element *rl) |
diff --git a/fs/ntfs/mft.c b/fs/ntfs/mft.c index e27651db4534..2c32b84385a8 100644 --- a/fs/ntfs/mft.c +++ b/fs/ntfs/mft.c | |||
@@ -1953,7 +1953,7 @@ restore_undo_alloc: | |||
1953 | a = ctx->attr; | 1953 | a = ctx->attr; |
1954 | a->data.non_resident.highest_vcn = cpu_to_sle64(old_last_vcn - 1); | 1954 | a->data.non_resident.highest_vcn = cpu_to_sle64(old_last_vcn - 1); |
1955 | undo_alloc: | 1955 | undo_alloc: |
1956 | if (ntfs_cluster_free(vol->mft_ino, old_last_vcn, -1) < 0) { | 1956 | if (ntfs_cluster_free(vol->mft_ino, old_last_vcn, -1, TRUE) < 0) { |
1957 | ntfs_error(vol->sb, "Failed to free clusters from mft data " | 1957 | ntfs_error(vol->sb, "Failed to free clusters from mft data " |
1958 | "attribute.%s", es); | 1958 | "attribute.%s", es); |
1959 | NVolSetErrors(vol); | 1959 | NVolSetErrors(vol); |