Skip to content

Commit 715dc63

Browse files
NTFS: Change ntfs_cluster_free() to require a write locked runlist on entry
since we otherwise get into a lock reversal deadlock if a read locked runlist is passed in. In the process also change it to take an ntfs inode instead of a vfs inode as parameter. Signed-off-by: Anton Altaparmakov <aia21@cantab.net>
1 parent 91fbc6e commit 715dc63

5 files changed

Lines changed: 32 additions & 34 deletions

File tree

‎fs/ntfs/ChangeLog‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ ToDo/Notes:
2525
2.1.25-WIP
2626

2727
- Fix sparse warnings that have crept in over time.
28+
- Change ntfs_cluster_free() to require a write locked runlist on entry
29+
since we otherwise get into a lock reversal deadlock if a read locked
30+
runlist is passed in. In the process also change it to take an ntfs
31+
inode instead of a vfs inode as parameter.
2832

2933
2.1.24 - Lots of bug fixes and support more clean journal states.
3034

‎fs/ntfs/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ntfs-objs := aops.o attrib.o collate.o compress.o debug.o dir.o file.o \
66
index.o inode.o mft.o mst.o namei.o runlist.o super.o sysctl.o \
77
unistr.o upcase.o
88

9-
EXTRA_CFLAGS = -DNTFS_VERSION=\"2.1.24\"
9+
EXTRA_CFLAGS = -DNTFS_VERSION=\"2.1.25-WIP\"
1010

1111
ifeq ($(CONFIG_NTFS_DEBUG),y)
1212
EXTRA_CFLAGS += -DDEBUG

‎fs/ntfs/lcnalloc.c‎

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -779,14 +779,13 @@ switch_to_data1_zone: search_zone = 2;
779779

780780
/**
781781
* __ntfs_cluster_free - free clusters on an ntfs volume
782-
* @vi: vfs inode whose runlist describes the clusters to free
783-
* @start_vcn: vcn in the runlist of @vi at which to start freeing clusters
782+
* @ni: ntfs inode whose runlist describes the clusters to free
783+
* @start_vcn: vcn in the runlist of @ni at which to start freeing clusters
784784
* @count: number of clusters to free or -1 for all clusters
785-
* @write_locked: true if the runlist is locked for writing
786785
* @is_rollback: true if this is a rollback operation
787786
*
788787
* Free @count clusters starting at the cluster @start_vcn in the runlist
789-
* described by the vfs inode @vi.
788+
* described by the vfs inode @ni.
790789
*
791790
* If @count is -1, all clusters from @start_vcn to the end of the runlist are
792791
* deallocated. Thus, to completely free all clusters in a runlist, use
@@ -801,31 +800,28 @@ switch_to_data1_zone: search_zone = 2;
801800
* Return the number of deallocated clusters (not counting sparse ones) on
802801
* success and -errno on error.
803802
*
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.
803+
* Locking: - The runlist described by @ni must be locked for writing on entry
804+
* and is locked on return. Note the runlist may be modified when
805+
* needed runlist fragments need to be mapped.
808806
* - The volume lcn bitmap must be unlocked on entry and is unlocked
809807
* on return.
810808
* - This function takes the volume lcn bitmap lock for writing and
811809
* modifies the bitmap contents.
812810
*/
813-
s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
814-
const BOOL write_locked, const BOOL is_rollback)
811+
s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn, s64 count,
812+
const BOOL is_rollback)
815813
{
816814
s64 delta, to_free, total_freed, real_freed;
817-
ntfs_inode *ni;
818815
ntfs_volume *vol;
819816
struct inode *lcnbmp_vi;
820817
runlist_element *rl;
821818
int err;
822819

823-
BUG_ON(!vi);
820+
BUG_ON(!ni);
824821
ntfs_debug("Entering for i_ino 0x%lx, start_vcn 0x%llx, count "
825-
"0x%llx.%s", vi->i_ino, (unsigned long long)start_vcn,
822+
"0x%llx.%s", ni->mft_no, (unsigned long long)start_vcn,
826823
(unsigned long long)count,
827824
is_rollback ? " (rollback)" : "");
828-
ni = NTFS_I(vi);
829825
vol = ni->vol;
830826
lcnbmp_vi = vol->lcnbmp_ino;
831827
BUG_ON(!lcnbmp_vi);
@@ -843,7 +839,7 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
843839

844840
total_freed = real_freed = 0;
845841

846-
rl = ntfs_attr_find_vcn_nolock(ni, start_vcn, write_locked);
842+
rl = ntfs_attr_find_vcn_nolock(ni, start_vcn, TRUE);
847843
if (IS_ERR(rl)) {
848844
if (!is_rollback)
849845
ntfs_error(vol->sb, "Failed to find first runlist "
@@ -897,7 +893,7 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
897893

898894
/* Attempt to map runlist. */
899895
vcn = rl->vcn;
900-
rl = ntfs_attr_find_vcn_nolock(ni, vcn, write_locked);
896+
rl = ntfs_attr_find_vcn_nolock(ni, vcn, TRUE);
901897
if (IS_ERR(rl)) {
902898
err = PTR_ERR(rl);
903899
if (!is_rollback)
@@ -965,8 +961,7 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
965961
* If rollback fails, set the volume errors flag, emit an error
966962
* message, and return the error code.
967963
*/
968-
delta = __ntfs_cluster_free(vi, start_vcn, total_freed, write_locked,
969-
TRUE);
964+
delta = __ntfs_cluster_free(ni, start_vcn, total_freed, TRUE);
970965
if (delta < 0) {
971966
ntfs_error(vol->sb, "Failed to rollback (error %i). Leaving "
972967
"inconsistent metadata! Unmount and run "

‎fs/ntfs/lcnalloc.h‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* lcnalloc.h - Exports for NTFS kernel cluster (de)allocation. Part of the
33
* Linux-NTFS project.
44
*
5-
* Copyright (c) 2004 Anton Altaparmakov
5+
* Copyright (c) 2004-2005 Anton Altaparmakov
66
*
77
* This program/include file is free software; you can redistribute it and/or
88
* modify it under the terms of the GNU General Public License as published
@@ -28,6 +28,7 @@
2828
#include <linux/fs.h>
2929

3030
#include "types.h"
31+
#include "inode.h"
3132
#include "runlist.h"
3233
#include "volume.h"
3334

@@ -42,18 +43,17 @@ extern runlist_element *ntfs_cluster_alloc(ntfs_volume *vol,
4243
const VCN start_vcn, const s64 count, const LCN start_lcn,
4344
const NTFS_CLUSTER_ALLOCATION_ZONES zone);
4445

45-
extern s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn,
46-
s64 count, const BOOL write_locked, const BOOL is_rollback);
46+
extern s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn,
47+
s64 count, const BOOL is_rollback);
4748

4849
/**
4950
* ntfs_cluster_free - free clusters on an ntfs volume
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+
* @ni: ntfs inode whose runlist describes the clusters to free
52+
* @start_vcn: vcn in the runlist of @ni at which to start freeing clusters
5253
* @count: number of clusters to free or -1 for all clusters
53-
* @write_locked: true if the runlist is locked for writing
5454
*
5555
* Free @count clusters starting at the cluster @start_vcn in the runlist
56-
* described by the vfs inode @vi.
56+
* described by the ntfs inode @ni.
5757
*
5858
* If @count is -1, all clusters from @start_vcn to the end of the runlist are
5959
* deallocated. Thus, to completely free all clusters in a runlist, use
@@ -65,19 +65,18 @@ extern s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn,
6565
* Return the number of deallocated clusters (not counting sparse ones) on
6666
* success and -errno on error.
6767
*
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.
68+
* Locking: - The runlist described by @ni must be locked for writing on entry
69+
* and is locked on return. Note the runlist may be modified when
70+
* needed runlist fragments need to be mapped.
7271
* - The volume lcn bitmap must be unlocked on entry and is unlocked
7372
* on return.
7473
* - This function takes the volume lcn bitmap lock for writing and
7574
* modifies the bitmap contents.
7675
*/
77-
static inline s64 ntfs_cluster_free(struct inode *vi, const VCN start_vcn,
78-
s64 count, const BOOL write_locked)
76+
static inline s64 ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn,
77+
s64 count)
7978
{
80-
return __ntfs_cluster_free(vi, start_vcn, count, write_locked, FALSE);
79+
return __ntfs_cluster_free(ni, start_vcn, count, FALSE);
8180
}
8281

8382
extern int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol,

‎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, TRUE) < 0) {
1956+
if (ntfs_cluster_free(mft_ni, old_last_vcn, -1) < 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)