Skip to content

Commit bbf1813

Browse files
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>
1 parent 807c453 commit bbf1813

4 files changed

Lines changed: 32 additions & 33 deletions

File tree

‎fs/ntfs/ChangeLog‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ ToDo/Notes:
7070
- Add BUG() checks to ntfs_attr_make_non_resident() and ntfs_attr_set()
7171
to ensure that these functions are never called for compressed or
7272
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.
7376

7477
2.1.23 - Implement extension of resident files and make writing safe as well as
7578
many bug fixes, cleanups, and enhancements...

‎fs/ntfs/lcnalloc.c‎

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol,
5454
int ret = 0;
5555

5656
ntfs_debug("Entering.");
57+
if (!rl)
58+
return 0;
5759
for (; rl->length; rl++) {
5860
int err;
5961

@@ -163,17 +165,9 @@ runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, const VCN start_vcn,
163165
BUG_ON(zone < FIRST_ZONE);
164166
BUG_ON(zone > LAST_ZONE);
165167

166-
/* Return empty runlist if @count == 0 */
167-
// FIXME: Do we want to just return NULL instead? (AIA)
168-
if (!count) {
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-
}
168+
/* Return NULL if @count is zero. */
169+
if (!count)
170+
return NULL;
177171
/* Take the lcnbmp lock for writing. */
178172
down_write(&vol->lcnbmp_lock);
179173
/*
@@ -788,7 +782,8 @@ switch_to_data1_zone: search_zone = 2;
788782
* @vi: vfs inode whose runlist describes the clusters to free
789783
* @start_vcn: vcn in the runlist of @vi at which to start freeing clusters
790784
* @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
792787
*
793788
* Free @count clusters starting at the cluster @start_vcn in the runlist
794789
* described by the vfs inode @vi.
@@ -806,17 +801,17 @@ switch_to_data1_zone: search_zone = 2;
806801
* Return the number of deallocated clusters (not counting sparse ones) on
807802
* success and -errno on error.
808803
*
809-
* Locking: - The runlist described by @vi must be unlocked on entry and is
810-
* unlocked on return.
811-
* - This function takes the runlist lock of @vi for reading and
812-
* sometimes for writing and sometimes modifies the runlist.
804+
* Locking: - The runlist described by @vi must be locked on entry and is
805+
* locked on return. Note if the runlist is locked for reading the
806+
* lock may be dropped and reacquired. Note the runlist may be
807+
* modified when needed runlist fragments need to be mapped.
813808
* - The volume lcn bitmap must be unlocked on entry and is unlocked
814809
* on return.
815810
* - This function takes the volume lcn bitmap lock for writing and
816811
* modifies the bitmap contents.
817812
*/
818813
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)
820815
{
821816
s64 delta, to_free, total_freed, real_freed;
822817
ntfs_inode *ni;
@@ -848,8 +843,7 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
848843

849844
total_freed = real_freed = 0;
850845

851-
down_read(&ni->runlist.lock);
852-
rl = ntfs_attr_find_vcn_nolock(ni, start_vcn, FALSE);
846+
rl = ntfs_attr_find_vcn_nolock(ni, start_vcn, write_locked);
853847
if (IS_ERR(rl)) {
854848
if (!is_rollback)
855849
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,
903897

904898
/* Attempt to map runlist. */
905899
vcn = rl->vcn;
906-
rl = ntfs_attr_find_vcn_nolock(ni, vcn, FALSE);
900+
rl = ntfs_attr_find_vcn_nolock(ni, vcn, write_locked);
907901
if (IS_ERR(rl)) {
908902
err = PTR_ERR(rl);
909903
if (!is_rollback)
@@ -950,7 +944,6 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
950944
/* Update the total done clusters. */
951945
total_freed += to_free;
952946
}
953-
up_read(&ni->runlist.lock);
954947
if (likely(!is_rollback))
955948
up_write(&vol->lcnbmp_lock);
956949

@@ -960,7 +953,6 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
960953
ntfs_debug("Done.");
961954
return real_freed;
962955
err_out:
963-
up_read(&ni->runlist.lock);
964956
if (is_rollback)
965957
return err;
966958
/* If no real clusters were freed, no need to rollback. */
@@ -973,7 +965,8 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
973965
* If rollback fails, set the volume errors flag, emit an error
974966
* message, and return the error code.
975967
*/
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);
977970
if (delta < 0) {
978971
ntfs_error(vol->sb, "Failed to rollback (error %i). Leaving "
979972
"inconsistent metadata! Unmount and run "

‎fs/ntfs/lcnalloc.h‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ extern runlist_element *ntfs_cluster_alloc(ntfs_volume *vol,
4343
const NTFS_CLUSTER_ALLOCATION_ZONES zone);
4444

4545
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);
4747

4848
/**
4949
* ntfs_cluster_free - free clusters on an ntfs volume
5050
* @vi: vfs inode whose runlist describes the clusters to free
5151
* @start_vcn: vcn in the runlist of @vi at which to start freeing clusters
5252
* @count: number of clusters to free or -1 for all clusters
53+
* @write_locked: true if the runlist is locked for writing
5354
*
5455
* Free @count clusters starting at the cluster @start_vcn in the runlist
5556
* described by the vfs inode @vi.
@@ -64,19 +65,19 @@ extern s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn,
6465
* Return the number of deallocated clusters (not counting sparse ones) on
6566
* success and -errno on error.
6667
*
67-
* Locking: - The runlist described by @vi must be unlocked on entry and is
68-
* unlocked on return.
69-
* - This function takes the runlist lock of @vi for reading and
70-
* sometimes for writing and sometimes modifies the runlist.
68+
* Locking: - The runlist described by @vi must be locked on entry and is
69+
* locked on return. Note if the runlist is locked for reading the
70+
* lock may be dropped and reacquired. Note the runlist may be
71+
* modified when needed runlist fragments need to be mapped.
7172
* - The volume lcn bitmap must be unlocked on entry and is unlocked
7273
* on return.
7374
* - This function takes the volume lcn bitmap lock for writing and
7475
* modifies the bitmap contents.
7576
*/
7677
static inline s64 ntfs_cluster_free(struct inode *vi, const VCN start_vcn,
77-
s64 count)
78+
s64 count, const BOOL write_locked)
7879
{
79-
return __ntfs_cluster_free(vi, start_vcn, count, FALSE);
80+
return __ntfs_cluster_free(vi, start_vcn, count, write_locked, FALSE);
8081
}
8182

8283
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,
9394
*
9495
* Return 0 on success and -errno on error.
9596
*
96-
* Locking: This function takes the volume lcn bitmap lock for writing and
97-
* modifies the bitmap contents.
97+
* Locking: - This function takes the volume lcn bitmap lock for writing and
98+
* modifies the bitmap contents.
99+
* - The caller must have locked the runlist @rl for reading or
100+
* writing.
98101
*/
99102
static inline int ntfs_cluster_free_from_rl(ntfs_volume *vol,
100103
const runlist_element *rl)

‎fs/ntfs/mft.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1953,7 +1953,7 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
19531953
a = ctx->attr;
19541954
a->data.non_resident.highest_vcn = cpu_to_sle64(old_last_vcn - 1);
19551955
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) {
19571957
ntfs_error(vol->sb, "Failed to free clusters from mft data "
19581958
"attribute.%s", es);
19591959
NVolSetErrors(vol);

0 commit comments

Comments
 (0)