Skip to content

Commit 07a4e2d

Browse files
NTFS: Use i_size_{read,write}() in fs/ntfs/{aops.c,mft.c} and protect
access to the i_size and other size fields using the size_lock. Signed-off-by: Anton Altaparmakov <aia21@cantab.net>
1 parent f50f3ac commit 07a4e2d

3 files changed

Lines changed: 156 additions & 59 deletions

File tree

‎fs/ntfs/ChangeLog‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ ToDo/Notes:
4949
- Minor optimization to fs/ntfs/super.c::ntfs_statfs() and its helpers.
5050
- Use i_size_read() in fs/ntfs/inode.c once and then use the cached
5151
value afterwards when reading the size of the bitmap inode.
52+
- Use i_size_{read,write}() in fs/ntfs/{aops.c,mft.c} and protect
53+
access to the i_size and other size fields using the size_lock.
5254

5355
2.1.22 - Many bug and race fixes and error handling improvements.
5456

‎fs/ntfs/aops.c‎

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,22 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
6666
ni = NTFS_I(page->mapping->host);
6767

6868
if (likely(uptodate)) {
69-
s64 file_ofs;
69+
s64 file_ofs, initialized_size;
7070

7171
set_buffer_uptodate(bh);
7272

7373
file_ofs = ((s64)page->index << PAGE_CACHE_SHIFT) +
7474
bh_offset(bh);
75+
read_lock_irqsave(&ni->size_lock, flags);
76+
initialized_size = ni->initialized_size;
77+
read_unlock_irqrestore(&ni->size_lock, flags);
7578
/* Check for the current buffer head overflowing. */
76-
if (file_ofs + bh->b_size > ni->initialized_size) {
79+
if (file_ofs + bh->b_size > initialized_size) {
7780
char *addr;
7881
int ofs = 0;
7982

80-
if (file_ofs < ni->initialized_size)
81-
ofs = ni->initialized_size - file_ofs;
83+
if (file_ofs < initialized_size)
84+
ofs = initialized_size - file_ofs;
8285
addr = kmap_atomic(page, KM_BIO_SRC_IRQ);
8386
memset(addr + bh_offset(bh) + ofs, 0, bh->b_size - ofs);
8487
flush_dcache_page(page);
@@ -168,6 +171,7 @@ static int ntfs_read_block(struct page *page)
168171
runlist_element *rl;
169172
struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
170173
sector_t iblock, lblock, zblock;
174+
unsigned long flags;
171175
unsigned int blocksize, vcn_ofs;
172176
int i, nr;
173177
unsigned char blocksize_bits;
@@ -190,8 +194,10 @@ static int ntfs_read_block(struct page *page)
190194
}
191195

192196
iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
197+
read_lock_irqsave(&ni->size_lock, flags);
193198
lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits;
194199
zblock = (ni->initialized_size + blocksize - 1) >> blocksize_bits;
200+
read_unlock_irqrestore(&ni->size_lock, flags);
195201

196202
/* Loop through all the buffers in the page. */
197203
rl = NULL;
@@ -463,12 +469,15 @@ static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
463469
{
464470
VCN vcn;
465471
LCN lcn;
472+
s64 initialized_size;
473+
loff_t i_size;
466474
sector_t block, dblock, iblock;
467475
struct inode *vi;
468476
ntfs_inode *ni;
469477
ntfs_volume *vol;
470478
runlist_element *rl;
471479
struct buffer_head *bh, *head;
480+
unsigned long flags;
472481
unsigned int blocksize, vcn_ofs;
473482
int err;
474483
BOOL need_end_writeback;
@@ -510,11 +519,16 @@ static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
510519
/* The first block in the page. */
511520
block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
512521

522+
read_lock_irqsave(&ni->size_lock, flags);
523+
i_size = i_size_read(vi);
524+
initialized_size = ni->initialized_size;
525+
read_unlock_irqrestore(&ni->size_lock, flags);
526+
513527
/* The first out of bounds block for the data size. */
514-
dblock = (vi->i_size + blocksize - 1) >> blocksize_bits;
528+
dblock = (i_size + blocksize - 1) >> blocksize_bits;
515529

516530
/* The last (fully or partially) initialized block. */
517-
iblock = ni->initialized_size >> blocksize_bits;
531+
iblock = initialized_size >> blocksize_bits;
518532

519533
/*
520534
* Be very careful. We have no exclusion from __set_page_dirty_buffers
@@ -559,7 +573,7 @@ static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
559573

560574
/* Make sure we have enough initialized size. */
561575
if (unlikely((block >= iblock) &&
562-
(ni->initialized_size < vi->i_size))) {
576+
(initialized_size < i_size))) {
563577
/*
564578
* If this page is fully outside initialized size, zero
565579
* out all pages between the current initialized size
@@ -846,7 +860,7 @@ static int ntfs_write_mst_block(struct page *page,
846860
(PAGE_CACHE_SHIFT - bh_size_bits);
847861

848862
/* The first out of bounds block for the data size. */
849-
dblock = (vi->i_size + bh_size - 1) >> bh_size_bits;
863+
dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
850864

851865
rl = NULL;
852866
err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
@@ -1367,7 +1381,7 @@ static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
13671381
*/
13681382

13691383
attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
1370-
i_size = i_size_read(VFS_I(ni));
1384+
i_size = i_size_read(vi);
13711385
kaddr = kmap_atomic(page, KM_USER0);
13721386
if (unlikely(attr_len > i_size)) {
13731387
/* Zero out of bounds area in the mft record. */
@@ -1425,12 +1439,15 @@ static int ntfs_prepare_nonresident_write(struct page *page,
14251439
{
14261440
VCN vcn;
14271441
LCN lcn;
1442+
s64 initialized_size;
1443+
loff_t i_size;
14281444
sector_t block, ablock, iblock;
14291445
struct inode *vi;
14301446
ntfs_inode *ni;
14311447
ntfs_volume *vol;
14321448
runlist_element *rl;
14331449
struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
1450+
unsigned long flags;
14341451
unsigned int vcn_ofs, block_start, block_end, blocksize;
14351452
int err;
14361453
BOOL is_retry;
@@ -1462,6 +1479,7 @@ static int ntfs_prepare_nonresident_write(struct page *page,
14621479
/* The first block in the page. */
14631480
block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
14641481

1482+
read_lock_irqsave(&ni->size_lock, flags);
14651483
/*
14661484
* The first out of bounds block for the allocated size. No need to
14671485
* round up as allocated_size is in multiples of cluster size and the
@@ -1470,8 +1488,12 @@ static int ntfs_prepare_nonresident_write(struct page *page,
14701488
*/
14711489
ablock = ni->allocated_size >> blocksize_bits;
14721490

1491+
i_size = i_size_read(vi);
1492+
initialized_size = ni->initialized_size;
1493+
read_unlock_irqrestore(&ni->size_lock, flags);
1494+
14731495
/* The last (fully or partially) initialized block. */
1474-
iblock = ni->initialized_size >> blocksize_bits;
1496+
iblock = initialized_size >> blocksize_bits;
14751497

14761498
/* Loop through all the buffers in the page. */
14771499
block_start = 0;
@@ -1518,7 +1540,7 @@ static int ntfs_prepare_nonresident_write(struct page *page,
15181540
* request, i.e. block < ablock is true.
15191541
*/
15201542
if (unlikely((block >= iblock) &&
1521-
(ni->initialized_size < vi->i_size))) {
1543+
(initialized_size < i_size))) {
15221544
/*
15231545
* If this page is fully outside initialized size, zero
15241546
* out all pages between the current initialized size
@@ -1868,7 +1890,7 @@ static int ntfs_prepare_write(struct file *file, struct page *page,
18681890
BUG_ON(page_has_buffers(page));
18691891
new_size = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
18701892
/* If we do not need to resize the attribute allocation we are done. */
1871-
if (new_size <= vi->i_size)
1893+
if (new_size <= i_size_read(vi))
18721894
goto done;
18731895

18741896
// FIXME: We abort for now as this code is not safe.
@@ -1904,7 +1926,7 @@ static int ntfs_prepare_write(struct file *file, struct page *page,
19041926
a = ctx->attr;
19051927
/* The total length of the attribute value. */
19061928
attr_len = le32_to_cpu(a->data.resident.value_length);
1907-
BUG_ON(vi->i_size != attr_len);
1929+
BUG_ON(i_size_read(vi) != attr_len);
19081930
/* Check if new size is allowed in $AttrDef. */
19091931
err = ntfs_attr_size_bounds_check(vol, ni->type, new_size);
19101932
if (unlikely(err)) {
@@ -2047,7 +2069,7 @@ static int ntfs_commit_nonresident_write(struct page *page,
20472069
* now we know ntfs_prepare_write() would have failed in the write
20482070
* exceeds i_size case, so this will never trigger which is fine.
20492071
*/
2050-
if (pos > vi->i_size) {
2072+
if (pos > i_size_read(vi)) {
20512073
ntfs_error(vi->i_sb, "Writing beyond the existing file size is "
20522074
"not supported yet. Sorry.");
20532075
return -EOPNOTSUPP;
@@ -2183,9 +2205,13 @@ static int ntfs_commit_write(struct file *file, struct page *page,
21832205
}
21842206
kunmap_atomic(kaddr, KM_USER0);
21852207
/* Update i_size if necessary. */
2186-
if (vi->i_size < attr_len) {
2208+
if (i_size_read(vi) < attr_len) {
2209+
unsigned long flags;
2210+
2211+
write_lock_irqsave(&ni->size_lock, flags);
21872212
ni->allocated_size = ni->initialized_size = attr_len;
21882213
i_size_write(vi, attr_len);
2214+
write_unlock_irqrestore(&ni->size_lock, flags);
21892215
}
21902216
/* Mark the mft record dirty, so it gets written back. */
21912217
flush_dcache_mft_record_page(ctx->ntfs_ino);

0 commit comments

Comments
 (0)