Skip to content

Commit b6ad6c5

Browse files
NTFS: - Split ntfs_map_runlist() into ntfs_map_runlist() and a non-locking
helper ntfs_map_runlist_nolock() which is used by ntfs_map_runlist(). This allows us to map runlist fragments with the runlist lock already held without having to drop and reacquire it around the call. Adapt all callers. - Change ntfs_find_vcn() to ntfs_find_vcn_nolock() which takes a locked runlist. This allows us to find runlist elements with the runlist lock already held without having to drop and reacquire it around the call. Adapt all callers. Signed-off-by: Anton Altaparmakov <aia21@cantab.net>
1 parent 1a0df15 commit b6ad6c5

6 files changed

Lines changed: 119 additions & 113 deletions

File tree

‎fs/ntfs/ChangeLog‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ ToDo/Notes:
6363
- Fix a bug in fs/ntfs/runlist.c::ntfs_mapping_pairs_decompress() in
6464
the creation of the unmapped runlist element for the base attribute
6565
extent.
66+
- Split ntfs_map_runlist() into ntfs_map_runlist() and a non-locking
67+
helper ntfs_map_runlist_nolock() which is used by ntfs_map_runlist().
68+
This allows us to map runlist fragments with the runlist lock already
69+
held without having to drop and reacquire it around the call. Adapt
70+
all callers.
71+
- Change ntfs_find_vcn() to ntfs_find_vcn_nolock() which takes a locked
72+
runlist. This allows us to find runlist elements with the runlist
73+
lock already held without having to drop and reacquire it around the
74+
call. Adapt all callers.
6675

6776
2.1.22 - Many bug and race fixes and error handling improvements.
6877

‎fs/ntfs/aops.c‎

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* aops.c - NTFS kernel address space operations and page cache handling.
33
* Part of the Linux-NTFS project.
44
*
5-
* Copyright (c) 2001-2004 Anton Altaparmakov
5+
* Copyright (c) 2001-2005 Anton Altaparmakov
66
* Copyright (c) 2002 Richard Russon
77
*
88
* This program/include file is free software; you can redistribute it and/or
@@ -135,7 +135,7 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
135135
i * rec_size), rec_size);
136136
flush_dcache_page(page);
137137
kunmap_atomic(addr, KM_BIO_SRC_IRQ);
138-
if (likely(!PageError(page) && page_uptodate))
138+
if (likely(page_uptodate && !PageError(page)))
139139
SetPageUptodate(page);
140140
}
141141
unlock_page(page);
@@ -347,11 +347,11 @@ static int ntfs_read_block(struct page *page)
347347
*/
348348
static int ntfs_readpage(struct file *file, struct page *page)
349349
{
350-
loff_t i_size;
351350
ntfs_inode *ni, *base_ni;
352351
u8 *kaddr;
353352
ntfs_attr_search_ctx *ctx;
354353
MFT_RECORD *mrec;
354+
unsigned long flags;
355355
u32 attr_len;
356356
int err = 0;
357357

@@ -389,9 +389,9 @@ static int ntfs_readpage(struct file *file, struct page *page)
389389
* Attribute is resident, implying it is not compressed or encrypted.
390390
* This also means the attribute is smaller than an mft record and
391391
* hence smaller than a page, so can simply zero out any pages with
392-
* index above 0. We can also do this if the file size is 0.
392+
* index above 0.
393393
*/
394-
if (unlikely(page->index > 0 || !i_size_read(VFS_I(ni)))) {
394+
if (unlikely(page->index > 0)) {
395395
kaddr = kmap_atomic(page, KM_USER0);
396396
memset(kaddr, 0, PAGE_CACHE_SIZE);
397397
flush_dcache_page(page);
@@ -418,9 +418,10 @@ static int ntfs_readpage(struct file *file, struct page *page)
418418
if (unlikely(err))
419419
goto put_unm_err_out;
420420
attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
421-
i_size = i_size_read(VFS_I(ni));
422-
if (unlikely(attr_len > i_size))
423-
attr_len = i_size;
421+
read_lock_irqsave(&ni->size_lock, flags);
422+
if (unlikely(attr_len > ni->initialized_size))
423+
attr_len = ni->initialized_size;
424+
read_unlock_irqrestore(&ni->size_lock, flags);
424425
kaddr = kmap_atomic(page, KM_USER0);
425426
/* Copy the data to the page. */
426427
memcpy(kaddr, (u8*)ctx->attr +
@@ -1247,20 +1248,6 @@ static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
12471248
int err;
12481249

12491250
BUG_ON(!PageLocked(page));
1250-
/*
1251-
* If a previous ntfs_truncate() failed, repeat it and abort if it
1252-
* fails again.
1253-
*/
1254-
if (unlikely(NInoTruncateFailed(ni))) {
1255-
down_write(&vi->i_alloc_sem);
1256-
err = ntfs_truncate(vi);
1257-
up_write(&vi->i_alloc_sem);
1258-
if (err || NInoTruncateFailed(ni)) {
1259-
if (!err)
1260-
err = -EIO;
1261-
goto err_out;
1262-
}
1263-
}
12641251
i_size = i_size_read(vi);
12651252
/* Is the page fully outside i_size? (truncate in progress) */
12661253
if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >>
@@ -1490,13 +1477,12 @@ static int ntfs_prepare_nonresident_write(struct page *page,
14901477

14911478
read_lock_irqsave(&ni->size_lock, flags);
14921479
/*
1493-
* The first out of bounds block for the allocated size. No need to
1480+
* The first out of bounds block for the allocated size. No need to
14941481
* round up as allocated_size is in multiples of cluster size and the
14951482
* minimum cluster size is 512 bytes, which is equal to the smallest
14961483
* blocksize.
14971484
*/
14981485
ablock = ni->allocated_size >> blocksize_bits;
1499-
15001486
i_size = i_size_read(vi);
15011487
initialized_size = ni->initialized_size;
15021488
read_unlock_irqrestore(&ni->size_lock, flags);

‎fs/ntfs/attrib.c‎

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* attrib.c - NTFS attribute operations. Part of the Linux-NTFS project.
33
*
4-
* Copyright (c) 2001-2004 Anton Altaparmakov
4+
* Copyright (c) 2001-2005 Anton Altaparmakov
55
* Copyright (c) 2002 Richard Russon
66
*
77
* This program/include file is free software; you can redistribute it and/or
@@ -30,32 +30,31 @@
3030
#include "types.h"
3131

3232
/**
33-
* ntfs_map_runlist - map (a part of) a runlist of an ntfs inode
33+
* ntfs_map_runlist_nolock - map (a part of) a runlist of an ntfs inode
3434
* @ni: ntfs inode for which to map (part of) a runlist
3535
* @vcn: map runlist part containing this vcn
3636
*
3737
* Map the part of a runlist containing the @vcn of the ntfs inode @ni.
3838
*
3939
* Return 0 on success and -errno on error.
4040
*
41-
* Locking: - The runlist must be unlocked on entry and is unlocked on return.
42-
* - This function takes the lock for writing and modifies the runlist.
41+
* Locking: - The runlist must be locked for writing.
42+
* - This function modifies the runlist.
4343
*/
44-
int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
44+
int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn)
4545
{
4646
ntfs_inode *base_ni;
47-
ntfs_attr_search_ctx *ctx;
4847
MFT_RECORD *mrec;
48+
ntfs_attr_search_ctx *ctx;
49+
runlist_element *rl;
4950
int err = 0;
5051

5152
ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
5253
(unsigned long long)vcn);
53-
5454
if (!NInoAttr(ni))
5555
base_ni = ni;
5656
else
5757
base_ni = ni->ext.base_ntfs_ino;
58-
5958
mrec = map_mft_record(base_ni);
6059
if (IS_ERR(mrec))
6160
return PTR_ERR(mrec);
@@ -66,43 +65,60 @@ int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
6665
}
6766
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
6867
CASE_SENSITIVE, vcn, NULL, 0, ctx);
69-
if (unlikely(err))
70-
goto put_err_out;
71-
72-
down_write(&ni->runlist.lock);
73-
/* Make sure someone else didn't do the work while we were sleeping. */
74-
if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <=
75-
LCN_RL_NOT_MAPPED)) {
76-
runlist_element *rl;
77-
68+
if (likely(!err)) {
7869
rl = ntfs_mapping_pairs_decompress(ni->vol, ctx->attr,
7970
ni->runlist.rl);
8071
if (IS_ERR(rl))
8172
err = PTR_ERR(rl);
8273
else
8374
ni->runlist.rl = rl;
8475
}
85-
up_write(&ni->runlist.lock);
86-
87-
put_err_out:
8876
ntfs_attr_put_search_ctx(ctx);
8977
err_out:
9078
unmap_mft_record(base_ni);
9179
return err;
9280
}
9381

9482
/**
95-
* ntfs_find_vcn - find a vcn in the runlist described by an ntfs inode
96-
* @ni: ntfs inode describing the runlist to search
97-
* @vcn: vcn to find
98-
* @need_write: if false, lock for reading and if true, lock for writing
83+
* ntfs_map_runlist - map (a part of) a runlist of an ntfs inode
84+
* @ni: ntfs inode for which to map (part of) a runlist
85+
* @vcn: map runlist part containing this vcn
86+
*
87+
* Map the part of a runlist containing the @vcn of the ntfs inode @ni.
88+
*
89+
* Return 0 on success and -errno on error.
90+
*
91+
* Locking: - The runlist must be unlocked on entry and is unlocked on return.
92+
* - This function takes the runlist lock for writing and modifies the
93+
* runlist.
94+
*/
95+
int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
96+
{
97+
int err = 0;
98+
99+
down_write(&ni->runlist.lock);
100+
/* Make sure someone else didn't do the work while we were sleeping. */
101+
if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <=
102+
LCN_RL_NOT_MAPPED))
103+
err = ntfs_map_runlist_nolock(ni, vcn);
104+
up_write(&ni->runlist.lock);
105+
return err;
106+
}
107+
108+
/**
109+
* ntfs_find_vcn_nolock - find a vcn in the runlist described by an ntfs inode
110+
* @ni: ntfs inode describing the runlist to search
111+
* @vcn: vcn to find
112+
* @write_locked: true if the runlist is locked for writing
99113
*
100114
* Find the virtual cluster number @vcn in the runlist described by the ntfs
101115
* inode @ni and return the address of the runlist element containing the @vcn.
102-
* The runlist is left locked and the caller has to unlock it. If @need_write
103-
* is true, the runlist is locked for writing and if @need_write is false, the
104-
* runlist is locked for reading. In the error case, the runlist is not left
105-
* locked.
116+
* The runlist is left locked and the caller has to unlock it. In the error
117+
* case, the runlist is left in the same locking state as on entry.
118+
*
119+
* Note if @write_locked is FALSE the lock may be dropped inside the function
120+
* so you cannot rely on the runlist still being the same when this function
121+
* returns.
106122
*
107123
* Note you need to distinguish between the lcn of the returned runlist element
108124
* being >= 0 and LCN_HOLE. In the later case you have to return zeroes on
@@ -124,28 +140,24 @@ int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
124140
* true, it is locked for writing. Otherwise is is locked for
125141
* reading.
126142
*/
127-
runlist_element *ntfs_find_vcn(ntfs_inode *ni, const VCN vcn,
128-
const BOOL need_write)
143+
runlist_element *ntfs_find_vcn_nolock(ntfs_inode *ni, const VCN vcn,
144+
const BOOL write_locked)
129145
{
130146
runlist_element *rl;
131147
int err = 0;
132148
BOOL is_retry = FALSE;
133149

134-
ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, lock for %sing.",
150+
ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
135151
ni->mft_no, (unsigned long long)vcn,
136-
!need_write ? "read" : "writ");
152+
write_locked ? "write" : "read");
137153
BUG_ON(!ni);
138154
BUG_ON(!NInoNonResident(ni));
139155
BUG_ON(vcn < 0);
140-
lock_retry_remap:
141-
if (!need_write)
142-
down_read(&ni->runlist.lock);
143-
else
144-
down_write(&ni->runlist.lock);
156+
retry_remap:
145157
rl = ni->runlist.rl;
146158
if (likely(rl && vcn >= rl[0].vcn)) {
147159
while (likely(rl->length)) {
148-
if (likely(vcn < rl[1].vcn)) {
160+
if (unlikely(vcn < rl[1].vcn)) {
149161
if (likely(rl->lcn >= LCN_HOLE)) {
150162
ntfs_debug("Done.");
151163
return rl;
@@ -161,19 +173,23 @@ runlist_element *ntfs_find_vcn(ntfs_inode *ni, const VCN vcn,
161173
err = -EIO;
162174
}
163175
}
164-
if (!need_write)
165-
up_read(&ni->runlist.lock);
166-
else
167-
up_write(&ni->runlist.lock);
168176
if (!err && !is_retry) {
169177
/*
170178
* The @vcn is in an unmapped region, map the runlist and
171179
* retry.
172180
*/
173-
err = ntfs_map_runlist(ni, vcn);
181+
if (!write_locked) {
182+
up_read(&ni->runlist.lock);
183+
down_write(&ni->runlist.lock);
184+
}
185+
err = ntfs_map_runlist_nolock(ni, vcn);
186+
if (!write_locked) {
187+
up_write(&ni->runlist.lock);
188+
down_read(&ni->runlist.lock);
189+
}
174190
if (likely(!err)) {
175191
is_retry = TRUE;
176-
goto lock_retry_remap;
192+
goto retry_remap;
177193
}
178194
/*
179195
* -EINVAL and -ENOENT coming from a failed mapping attempt are
@@ -184,7 +200,8 @@ runlist_element *ntfs_find_vcn(ntfs_inode *ni, const VCN vcn,
184200
err = -EIO;
185201
} else if (!err)
186202
err = -EIO;
187-
ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
203+
if (err != -ENOENT)
204+
ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
188205
return ERR_PTR(err);
189206
}
190207

‎fs/ntfs/attrib.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ typedef struct {
6060
ATTR_RECORD *base_attr;
6161
} ntfs_attr_search_ctx;
6262

63+
extern int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn);
6364
extern int ntfs_map_runlist(ntfs_inode *ni, VCN vcn);
6465

65-
extern runlist_element *ntfs_find_vcn(ntfs_inode *ni, const VCN vcn,
66-
const BOOL need_write);
66+
extern runlist_element *ntfs_find_vcn_nolock(ntfs_inode *ni, const VCN vcn,
67+
const BOOL write_locked);
6768

6869
int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name,
6970
const u32 name_len, const IGNORE_CASE_BOOL ic,

0 commit comments

Comments
 (0)