Skip to content

Commit 511bea5

Browse files
NTFS: - Change {__,}ntfs_cluster_free() to also take an optional attribute
search context as argument. This allows calling it with the mft record mapped. Update all callers. - Fix potential deadlock in ntfs_mft_data_extend_allocation_nolock() error handling by passing in the active search context when calling ntfs_cluster_free(). Signed-off-by: Anton Altaparmakov <aia21@cantab.net>
1 parent 69b41e3 commit 511bea5

4 files changed

Lines changed: 86 additions & 19 deletions

File tree

‎fs/ntfs/ChangeLog‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ ToDo/Notes:
2424

2525
2.1.25-WIP
2626

27-
- Change ntfs_map_runlist_nolock() and ntfs_attr_find_vcn_nolock() to
28-
also take an optional attribute search context as argument. This
29-
allows calling these functions with the mft record mapped. Update
30-
all callers.
27+
- Change ntfs_map_runlist_nolock(), ntfs_attr_find_vcn_nolock() and
28+
{__,}ntfs_cluster_free() to also take an optional attribute search
29+
context as argument. This allows calling these functions with the
30+
mft record mapped. Update all callers.
31+
- Fix potential deadlock in ntfs_mft_data_extend_allocation_nolock()
32+
error handling by passing in the active search context when calling
33+
ntfs_cluster_free().
3134

3235
2.1.24 - Lots of bug fixes and support more clean journal states.
3336

‎fs/ntfs/lcnalloc.c‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ switch_to_data1_zone: search_zone = 2;
782782
* @ni: ntfs inode whose runlist describes the clusters to free
783783
* @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+
* @ctx: active attribute search context if present or NULL if not
785786
* @is_rollback: true if this is a rollback operation
786787
*
787788
* Free @count clusters starting at the cluster @start_vcn in the runlist
@@ -791,25 +792,53 @@ switch_to_data1_zone: search_zone = 2;
791792
* deallocated. Thus, to completely free all clusters in a runlist, use
792793
* @start_vcn = 0 and @count = -1.
793794
*
795+
* If @ctx is specified, it is an active search context of @ni and its base mft
796+
* record. This is needed when __ntfs_cluster_free() encounters unmapped
797+
* runlist fragments and allows their mapping. If you do not have the mft
798+
* record mapped, you can specify @ctx as NULL and __ntfs_cluster_free() will
799+
* perform the necessary mapping and unmapping.
800+
*
801+
* Note, __ntfs_cluster_free() saves the state of @ctx on entry and restores it
802+
* before returning. Thus, @ctx will be left pointing to the same attribute on
803+
* return as on entry. However, the actual pointers in @ctx may point to
804+
* different memory locations on return, so you must remember to reset any
805+
* cached pointers from the @ctx, i.e. after the call to __ntfs_cluster_free(),
806+
* you will probably want to do:
807+
* m = ctx->mrec;
808+
* a = ctx->attr;
809+
* Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
810+
* you cache ctx->mrec in a variable @m of type MFT_RECORD *.
811+
*
794812
* @is_rollback should always be FALSE, it is for internal use to rollback
795813
* errors. You probably want to use ntfs_cluster_free() instead.
796814
*
797-
* Note, ntfs_cluster_free() does not modify the runlist at all, so the caller
798-
* has to deal with it later.
815+
* Note, __ntfs_cluster_free() does not modify the runlist, so you have to
816+
* remove from the runlist or mark sparse the freed runs later.
799817
*
800818
* Return the number of deallocated clusters (not counting sparse ones) on
801819
* success and -errno on error.
802820
*
821+
* WARNING: If @ctx is supplied, regardless of whether success or failure is
822+
* returned, you need to check IS_ERR(@ctx->mrec) and if TRUE the @ctx
823+
* is no longer valid, i.e. you need to either call
824+
* ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
825+
* In that case PTR_ERR(@ctx->mrec) will give you the error code for
826+
* why the mapping of the old inode failed.
827+
*
803828
* Locking: - The runlist described by @ni must be locked for writing on entry
804829
* and is locked on return. Note the runlist may be modified when
805830
* needed runlist fragments need to be mapped.
806831
* - The volume lcn bitmap must be unlocked on entry and is unlocked
807832
* on return.
808833
* - This function takes the volume lcn bitmap lock for writing and
809834
* modifies the bitmap contents.
835+
* - If @ctx is NULL, the base mft record of @ni must not be mapped on
836+
* entry and it will be left unmapped on return.
837+
* - If @ctx is not NULL, the base mft record must be mapped on entry
838+
* and it will be left mapped on return.
810839
*/
811840
s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn, s64 count,
812-
const BOOL is_rollback)
841+
ntfs_attr_search_ctx *ctx, const BOOL is_rollback)
813842
{
814843
s64 delta, to_free, total_freed, real_freed;
815844
ntfs_volume *vol;
@@ -839,7 +868,7 @@ s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn, s64 count,
839868

840869
total_freed = real_freed = 0;
841870

842-
rl = ntfs_attr_find_vcn_nolock(ni, start_vcn, NULL);
871+
rl = ntfs_attr_find_vcn_nolock(ni, start_vcn, ctx);
843872
if (IS_ERR(rl)) {
844873
if (!is_rollback)
845874
ntfs_error(vol->sb, "Failed to find first runlist "
@@ -893,7 +922,7 @@ s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn, s64 count,
893922

894923
/* Attempt to map runlist. */
895924
vcn = rl->vcn;
896-
rl = ntfs_attr_find_vcn_nolock(ni, vcn, NULL);
925+
rl = ntfs_attr_find_vcn_nolock(ni, vcn, ctx);
897926
if (IS_ERR(rl)) {
898927
err = PTR_ERR(rl);
899928
if (!is_rollback)
@@ -961,7 +990,7 @@ s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn, s64 count,
961990
* If rollback fails, set the volume errors flag, emit an error
962991
* message, and return the error code.
963992
*/
964-
delta = __ntfs_cluster_free(ni, start_vcn, total_freed, TRUE);
993+
delta = __ntfs_cluster_free(ni, start_vcn, total_freed, ctx, TRUE);
965994
if (delta < 0) {
966995
ntfs_error(vol->sb, "Failed to rollback (error %i). Leaving "
967996
"inconsistent metadata! Unmount and run "

‎fs/ntfs/lcnalloc.h‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <linux/fs.h>
2929

30+
#include "attrib.h"
3031
#include "types.h"
3132
#include "inode.h"
3233
#include "runlist.h"
@@ -44,13 +45,14 @@ extern runlist_element *ntfs_cluster_alloc(ntfs_volume *vol,
4445
const NTFS_CLUSTER_ALLOCATION_ZONES zone);
4546

4647
extern s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn,
47-
s64 count, const BOOL is_rollback);
48+
s64 count, ntfs_attr_search_ctx *ctx, const BOOL is_rollback);
4849

4950
/**
5051
* ntfs_cluster_free - free clusters on an ntfs volume
5152
* @ni: ntfs inode whose runlist describes the clusters to free
5253
* @start_vcn: vcn in the runlist of @ni at which to start freeing clusters
5354
* @count: number of clusters to free or -1 for all clusters
55+
* @ctx: active attribute search context if present or NULL if not
5456
*
5557
* Free @count clusters starting at the cluster @start_vcn in the runlist
5658
* described by the ntfs inode @ni.
@@ -59,24 +61,52 @@ extern s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn,
5961
* deallocated. Thus, to completely free all clusters in a runlist, use
6062
* @start_vcn = 0 and @count = -1.
6163
*
62-
* Note, ntfs_cluster_free() does not modify the runlist at all, so the caller
63-
* has to deal with it later.
64+
* If @ctx is specified, it is an active search context of @ni and its base mft
65+
* record. This is needed when ntfs_cluster_free() encounters unmapped runlist
66+
* fragments and allows their mapping. If you do not have the mft record
67+
* mapped, you can specify @ctx as NULL and ntfs_cluster_free() will perform
68+
* the necessary mapping and unmapping.
69+
*
70+
* Note, ntfs_cluster_free() saves the state of @ctx on entry and restores it
71+
* before returning. Thus, @ctx will be left pointing to the same attribute on
72+
* return as on entry. However, the actual pointers in @ctx may point to
73+
* different memory locations on return, so you must remember to reset any
74+
* cached pointers from the @ctx, i.e. after the call to ntfs_cluster_free(),
75+
* you will probably want to do:
76+
* m = ctx->mrec;
77+
* a = ctx->attr;
78+
* Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
79+
* you cache ctx->mrec in a variable @m of type MFT_RECORD *.
80+
*
81+
* Note, ntfs_cluster_free() does not modify the runlist, so you have to remove
82+
* from the runlist or mark sparse the freed runs later.
6483
*
6584
* Return the number of deallocated clusters (not counting sparse ones) on
6685
* success and -errno on error.
6786
*
87+
* WARNING: If @ctx is supplied, regardless of whether success or failure is
88+
* returned, you need to check IS_ERR(@ctx->mrec) and if TRUE the @ctx
89+
* is no longer valid, i.e. you need to either call
90+
* ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
91+
* In that case PTR_ERR(@ctx->mrec) will give you the error code for
92+
* why the mapping of the old inode failed.
93+
*
6894
* Locking: - The runlist described by @ni must be locked for writing on entry
6995
* and is locked on return. Note the runlist may be modified when
7096
* needed runlist fragments need to be mapped.
7197
* - The volume lcn bitmap must be unlocked on entry and is unlocked
7298
* on return.
7399
* - This function takes the volume lcn bitmap lock for writing and
74100
* modifies the bitmap contents.
101+
* - If @ctx is NULL, the base mft record of @ni must not be mapped on
102+
* entry and it will be left unmapped on return.
103+
* - If @ctx is not NULL, the base mft record must be mapped on entry
104+
* and it will be left mapped on return.
75105
*/
76106
static inline s64 ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn,
77-
s64 count)
107+
s64 count, ntfs_attr_search_ctx *ctx)
78108
{
79-
return __ntfs_cluster_free(ni, start_vcn, count, FALSE);
109+
return __ntfs_cluster_free(ni, start_vcn, count, ctx, FALSE);
80110
}
81111

82112
extern int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol,

‎fs/ntfs/mft.c‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,20 +1952,21 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
19521952
NVolSetErrors(vol);
19531953
return ret;
19541954
}
1955-
a = ctx->attr;
1956-
a->data.non_resident.highest_vcn = cpu_to_sle64(old_last_vcn - 1);
1955+
ctx->attr->data.non_resident.highest_vcn =
1956+
cpu_to_sle64(old_last_vcn - 1);
19571957
undo_alloc:
1958-
if (ntfs_cluster_free(mft_ni, old_last_vcn, -1) < 0) {
1958+
if (ntfs_cluster_free(mft_ni, old_last_vcn, -1, ctx) < 0) {
19591959
ntfs_error(vol->sb, "Failed to free clusters from mft data "
19601960
"attribute.%s", es);
19611961
NVolSetErrors(vol);
19621962
}
1963+
a = ctx->attr;
19631964
if (ntfs_rl_truncate_nolock(vol, &mft_ni->runlist, old_last_vcn)) {
19641965
ntfs_error(vol->sb, "Failed to truncate mft data attribute "
19651966
"runlist.%s", es);
19661967
NVolSetErrors(vol);
19671968
}
1968-
if (mp_rebuilt) {
1969+
if (mp_rebuilt && !IS_ERR(ctx->mrec)) {
19691970
if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
19701971
a->data.non_resident.mapping_pairs_offset),
19711972
old_alen - le16_to_cpu(
@@ -1982,6 +1983,10 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
19821983
}
19831984
flush_dcache_mft_record_page(ctx->ntfs_ino);
19841985
mark_mft_record_dirty(ctx->ntfs_ino);
1986+
} else if (IS_ERR(ctx->mrec)) {
1987+
ntfs_error(vol->sb, "Failed to restore attribute search "
1988+
"context.%s", es);
1989+
NVolSetErrors(vol);
19851990
}
19861991
if (ctx)
19871992
ntfs_attr_put_search_ctx(ctx);

0 commit comments

Comments
 (0)