aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ntfs/aops.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ntfs/aops.c')
-rw-r--r--fs/ntfs/aops.c455
1 files changed, 278 insertions, 177 deletions
diff --git a/fs/ntfs/aops.c b/fs/ntfs/aops.c
index 45d56e41ed98..b6cc8cf24626 100644
--- a/fs/ntfs/aops.c
+++ b/fs/ntfs/aops.c
@@ -2,7 +2,7 @@
2 * aops.c - NTFS kernel address space operations and page cache handling. 2 * aops.c - NTFS kernel address space operations and page cache handling.
3 * Part of the Linux-NTFS project. 3 * Part of the Linux-NTFS project.
4 * 4 *
5 * Copyright (c) 2001-2004 Anton Altaparmakov 5 * Copyright (c) 2001-2005 Anton Altaparmakov
6 * Copyright (c) 2002 Richard Russon 6 * Copyright (c) 2002 Richard Russon
7 * 7 *
8 * This program/include file is free software; you can redistribute it and/or 8 * This program/include file is free software; you can redistribute it and/or
@@ -27,6 +27,7 @@
27#include <linux/swap.h> 27#include <linux/swap.h>
28#include <linux/buffer_head.h> 28#include <linux/buffer_head.h>
29#include <linux/writeback.h> 29#include <linux/writeback.h>
30#include <linux/bit_spinlock.h>
30 31
31#include "aops.h" 32#include "aops.h"
32#include "attrib.h" 33#include "attrib.h"
@@ -55,9 +56,8 @@
55 */ 56 */
56static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate) 57static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
57{ 58{
58 static DEFINE_SPINLOCK(page_uptodate_lock);
59 unsigned long flags; 59 unsigned long flags;
60 struct buffer_head *tmp; 60 struct buffer_head *first, *tmp;
61 struct page *page; 61 struct page *page;
62 ntfs_inode *ni; 62 ntfs_inode *ni;
63 int page_uptodate = 1; 63 int page_uptodate = 1;
@@ -66,19 +66,22 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
66 ni = NTFS_I(page->mapping->host); 66 ni = NTFS_I(page->mapping->host);
67 67
68 if (likely(uptodate)) { 68 if (likely(uptodate)) {
69 s64 file_ofs; 69 s64 file_ofs, initialized_size;
70 70
71 set_buffer_uptodate(bh); 71 set_buffer_uptodate(bh);
72 72
73 file_ofs = ((s64)page->index << PAGE_CACHE_SHIFT) + 73 file_ofs = ((s64)page->index << PAGE_CACHE_SHIFT) +
74 bh_offset(bh); 74 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);
75 /* Check for the current buffer head overflowing. */ 78 /* 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) {
77 char *addr; 80 char *addr;
78 int ofs = 0; 81 int ofs = 0;
79 82
80 if (file_ofs < ni->initialized_size) 83 if (file_ofs < initialized_size)
81 ofs = ni->initialized_size - file_ofs; 84 ofs = initialized_size - file_ofs;
82 addr = kmap_atomic(page, KM_BIO_SRC_IRQ); 85 addr = kmap_atomic(page, KM_BIO_SRC_IRQ);
83 memset(addr + bh_offset(bh) + ofs, 0, bh->b_size - ofs); 86 memset(addr + bh_offset(bh) + ofs, 0, bh->b_size - ofs);
84 flush_dcache_page(page); 87 flush_dcache_page(page);
@@ -86,11 +89,13 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
86 } 89 }
87 } else { 90 } else {
88 clear_buffer_uptodate(bh); 91 clear_buffer_uptodate(bh);
92 SetPageError(page);
89 ntfs_error(ni->vol->sb, "Buffer I/O error, logical block %llu.", 93 ntfs_error(ni->vol->sb, "Buffer I/O error, logical block %llu.",
90 (unsigned long long)bh->b_blocknr); 94 (unsigned long long)bh->b_blocknr);
91 SetPageError(page);
92 } 95 }
93 spin_lock_irqsave(&page_uptodate_lock, flags); 96 first = page_buffers(page);
97 local_irq_save(flags);
98 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
94 clear_buffer_async_read(bh); 99 clear_buffer_async_read(bh);
95 unlock_buffer(bh); 100 unlock_buffer(bh);
96 tmp = bh; 101 tmp = bh;
@@ -105,7 +110,8 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
105 } 110 }
106 tmp = tmp->b_this_page; 111 tmp = tmp->b_this_page;
107 } while (tmp != bh); 112 } while (tmp != bh);
108 spin_unlock_irqrestore(&page_uptodate_lock, flags); 113 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
114 local_irq_restore(flags);
109 /* 115 /*
110 * If none of the buffers had errors then we can set the page uptodate, 116 * If none of the buffers had errors then we can set the page uptodate,
111 * but we first have to perform the post read mst fixups, if the 117 * but we first have to perform the post read mst fixups, if the
@@ -132,13 +138,14 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
132 i * rec_size), rec_size); 138 i * rec_size), rec_size);
133 flush_dcache_page(page); 139 flush_dcache_page(page);
134 kunmap_atomic(addr, KM_BIO_SRC_IRQ); 140 kunmap_atomic(addr, KM_BIO_SRC_IRQ);
135 if (likely(!PageError(page) && page_uptodate)) 141 if (likely(page_uptodate && !PageError(page)))
136 SetPageUptodate(page); 142 SetPageUptodate(page);
137 } 143 }
138 unlock_page(page); 144 unlock_page(page);
139 return; 145 return;
140still_busy: 146still_busy:
141 spin_unlock_irqrestore(&page_uptodate_lock, flags); 147 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
148 local_irq_restore(flags);
142 return; 149 return;
143} 150}
144 151
@@ -168,6 +175,7 @@ static int ntfs_read_block(struct page *page)
168 runlist_element *rl; 175 runlist_element *rl;
169 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE]; 176 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
170 sector_t iblock, lblock, zblock; 177 sector_t iblock, lblock, zblock;
178 unsigned long flags;
171 unsigned int blocksize, vcn_ofs; 179 unsigned int blocksize, vcn_ofs;
172 int i, nr; 180 int i, nr;
173 unsigned char blocksize_bits; 181 unsigned char blocksize_bits;
@@ -181,23 +189,28 @@ static int ntfs_read_block(struct page *page)
181 blocksize_bits = VFS_I(ni)->i_blkbits; 189 blocksize_bits = VFS_I(ni)->i_blkbits;
182 blocksize = 1 << blocksize_bits; 190 blocksize = 1 << blocksize_bits;
183 191
184 if (!page_has_buffers(page)) 192 if (!page_has_buffers(page)) {
185 create_empty_buffers(page, blocksize, 0); 193 create_empty_buffers(page, blocksize, 0);
186 bh = head = page_buffers(page); 194 if (unlikely(!page_has_buffers(page))) {
187 if (unlikely(!bh)) { 195 unlock_page(page);
188 unlock_page(page); 196 return -ENOMEM;
189 return -ENOMEM; 197 }
190 } 198 }
199 bh = head = page_buffers(page);
200 BUG_ON(!bh);
191 201
192 iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits); 202 iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
203 read_lock_irqsave(&ni->size_lock, flags);
193 lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits; 204 lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits;
194 zblock = (ni->initialized_size + blocksize - 1) >> blocksize_bits; 205 zblock = (ni->initialized_size + blocksize - 1) >> blocksize_bits;
206 read_unlock_irqrestore(&ni->size_lock, flags);
195 207
196 /* Loop through all the buffers in the page. */ 208 /* Loop through all the buffers in the page. */
197 rl = NULL; 209 rl = NULL;
198 nr = i = 0; 210 nr = i = 0;
199 do { 211 do {
200 u8 *kaddr; 212 u8 *kaddr;
213 int err;
201 214
202 if (unlikely(buffer_uptodate(bh))) 215 if (unlikely(buffer_uptodate(bh)))
203 continue; 216 continue;
@@ -205,6 +218,7 @@ static int ntfs_read_block(struct page *page)
205 arr[nr++] = bh; 218 arr[nr++] = bh;
206 continue; 219 continue;
207 } 220 }
221 err = 0;
208 bh->b_bdev = vol->sb->s_bdev; 222 bh->b_bdev = vol->sb->s_bdev;
209 /* Is the block within the allowed limits? */ 223 /* Is the block within the allowed limits? */
210 if (iblock < lblock) { 224 if (iblock < lblock) {
@@ -246,7 +260,6 @@ lock_retry_remap:
246 goto handle_hole; 260 goto handle_hole;
247 /* If first try and runlist unmapped, map and retry. */ 261 /* If first try and runlist unmapped, map and retry. */
248 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) { 262 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
249 int err;
250 is_retry = TRUE; 263 is_retry = TRUE;
251 /* 264 /*
252 * Attempt to map runlist, dropping lock for 265 * Attempt to map runlist, dropping lock for
@@ -257,19 +270,30 @@ lock_retry_remap:
257 if (likely(!err)) 270 if (likely(!err))
258 goto lock_retry_remap; 271 goto lock_retry_remap;
259 rl = NULL; 272 rl = NULL;
260 lcn = err; 273 } else if (!rl)
274 up_read(&ni->runlist.lock);
275 /*
276 * If buffer is outside the runlist, treat it as a
277 * hole. This can happen due to concurrent truncate
278 * for example.
279 */
280 if (err == -ENOENT || lcn == LCN_ENOENT) {
281 err = 0;
282 goto handle_hole;
261 } 283 }
262 /* Hard error, zero out region. */ 284 /* Hard error, zero out region. */
285 if (!err)
286 err = -EIO;
263 bh->b_blocknr = -1; 287 bh->b_blocknr = -1;
264 SetPageError(page); 288 SetPageError(page);
265 ntfs_error(vol->sb, "Failed to read from inode 0x%lx, " 289 ntfs_error(vol->sb, "Failed to read from inode 0x%lx, "
266 "attribute type 0x%x, vcn 0x%llx, " 290 "attribute type 0x%x, vcn 0x%llx, "
267 "offset 0x%x because its location on " 291 "offset 0x%x because its location on "
268 "disk could not be determined%s " 292 "disk could not be determined%s "
269 "(error code %lli).", ni->mft_no, 293 "(error code %i).", ni->mft_no,
270 ni->type, (unsigned long long)vcn, 294 ni->type, (unsigned long long)vcn,
271 vcn_ofs, is_retry ? " even after " 295 vcn_ofs, is_retry ? " even after "
272 "retrying" : "", (long long)lcn); 296 "retrying" : "", err);
273 } 297 }
274 /* 298 /*
275 * Either iblock was outside lblock limits or 299 * Either iblock was outside lblock limits or
@@ -282,9 +306,10 @@ handle_hole:
282handle_zblock: 306handle_zblock:
283 kaddr = kmap_atomic(page, KM_USER0); 307 kaddr = kmap_atomic(page, KM_USER0);
284 memset(kaddr + i * blocksize, 0, blocksize); 308 memset(kaddr + i * blocksize, 0, blocksize);
285 flush_dcache_page(page);
286 kunmap_atomic(kaddr, KM_USER0); 309 kunmap_atomic(kaddr, KM_USER0);
287 set_buffer_uptodate(bh); 310 flush_dcache_page(page);
311 if (likely(!err))
312 set_buffer_uptodate(bh);
288 } while (i++, iblock++, (bh = bh->b_this_page) != head); 313 } while (i++, iblock++, (bh = bh->b_this_page) != head);
289 314
290 /* Release the lock if we took it. */ 315 /* Release the lock if we took it. */
@@ -341,14 +366,15 @@ handle_zblock:
341 */ 366 */
342static int ntfs_readpage(struct file *file, struct page *page) 367static int ntfs_readpage(struct file *file, struct page *page)
343{ 368{
344 loff_t i_size;
345 ntfs_inode *ni, *base_ni; 369 ntfs_inode *ni, *base_ni;
346 u8 *kaddr; 370 u8 *kaddr;
347 ntfs_attr_search_ctx *ctx; 371 ntfs_attr_search_ctx *ctx;
348 MFT_RECORD *mrec; 372 MFT_RECORD *mrec;
373 unsigned long flags;
349 u32 attr_len; 374 u32 attr_len;
350 int err = 0; 375 int err = 0;
351 376
377retry_readpage:
352 BUG_ON(!PageLocked(page)); 378 BUG_ON(!PageLocked(page));
353 /* 379 /*
354 * This can potentially happen because we clear PageUptodate() during 380 * This can potentially happen because we clear PageUptodate() during
@@ -359,33 +385,40 @@ static int ntfs_readpage(struct file *file, struct page *page)
359 return 0; 385 return 0;
360 } 386 }
361 ni = NTFS_I(page->mapping->host); 387 ni = NTFS_I(page->mapping->host);
362 388 /*
389 * Only $DATA attributes can be encrypted and only unnamed $DATA
390 * attributes can be compressed. Index root can have the flags set but
391 * this means to create compressed/encrypted files, not that the
392 * attribute is compressed/encrypted.
393 */
394 if (ni->type != AT_INDEX_ROOT) {
395 /* If attribute is encrypted, deny access, just like NT4. */
396 if (NInoEncrypted(ni)) {
397 BUG_ON(ni->type != AT_DATA);
398 err = -EACCES;
399 goto err_out;
400 }
401 /* Compressed data streams are handled in compress.c. */
402 if (NInoNonResident(ni) && NInoCompressed(ni)) {
403 BUG_ON(ni->type != AT_DATA);
404 BUG_ON(ni->name_len);
405 return ntfs_read_compressed_block(page);
406 }
407 }
363 /* NInoNonResident() == NInoIndexAllocPresent() */ 408 /* NInoNonResident() == NInoIndexAllocPresent() */
364 if (NInoNonResident(ni)) { 409 if (NInoNonResident(ni)) {
365 /* 410 /* Normal, non-resident data stream. */
366 * Only unnamed $DATA attributes can be compressed or
367 * encrypted.
368 */
369 if (ni->type == AT_DATA && !ni->name_len) {
370 /* If file is encrypted, deny access, just like NT4. */
371 if (NInoEncrypted(ni)) {
372 err = -EACCES;
373 goto err_out;
374 }
375 /* Compressed data streams are handled in compress.c. */
376 if (NInoCompressed(ni))
377 return ntfs_read_compressed_block(page);
378 }
379 /* Normal data stream. */
380 return ntfs_read_block(page); 411 return ntfs_read_block(page);
381 } 412 }
382 /* 413 /*
383 * Attribute is resident, implying it is not compressed or encrypted. 414 * Attribute is resident, implying it is not compressed or encrypted.
384 * This also means the attribute is smaller than an mft record and 415 * This also means the attribute is smaller than an mft record and
385 * hence smaller than a page, so can simply zero out any pages with 416 * hence smaller than a page, so can simply zero out any pages with
386 * index above 0. We can also do this if the file size is 0. 417 * index above 0. Note the attribute can actually be marked compressed
418 * but if it is resident the actual data is not compressed so we are
419 * ok to ignore the compressed flag here.
387 */ 420 */
388 if (unlikely(page->index > 0 || !i_size_read(VFS_I(ni)))) { 421 if (unlikely(page->index > 0)) {
389 kaddr = kmap_atomic(page, KM_USER0); 422 kaddr = kmap_atomic(page, KM_USER0);
390 memset(kaddr, 0, PAGE_CACHE_SIZE); 423 memset(kaddr, 0, PAGE_CACHE_SIZE);
391 flush_dcache_page(page); 424 flush_dcache_page(page);
@@ -402,6 +435,14 @@ static int ntfs_readpage(struct file *file, struct page *page)
402 err = PTR_ERR(mrec); 435 err = PTR_ERR(mrec);
403 goto err_out; 436 goto err_out;
404 } 437 }
438 /*
439 * If a parallel write made the attribute non-resident, drop the mft
440 * record and retry the readpage.
441 */
442 if (unlikely(NInoNonResident(ni))) {
443 unmap_mft_record(base_ni);
444 goto retry_readpage;
445 }
405 ctx = ntfs_attr_get_search_ctx(base_ni, mrec); 446 ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
406 if (unlikely(!ctx)) { 447 if (unlikely(!ctx)) {
407 err = -ENOMEM; 448 err = -ENOMEM;
@@ -412,9 +453,10 @@ static int ntfs_readpage(struct file *file, struct page *page)
412 if (unlikely(err)) 453 if (unlikely(err))
413 goto put_unm_err_out; 454 goto put_unm_err_out;
414 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length); 455 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
415 i_size = i_size_read(VFS_I(ni)); 456 read_lock_irqsave(&ni->size_lock, flags);
416 if (unlikely(attr_len > i_size)) 457 if (unlikely(attr_len > ni->initialized_size))
417 attr_len = i_size; 458 attr_len = ni->initialized_size;
459 read_unlock_irqrestore(&ni->size_lock, flags);
418 kaddr = kmap_atomic(page, KM_USER0); 460 kaddr = kmap_atomic(page, KM_USER0);
419 /* Copy the data to the page. */ 461 /* Copy the data to the page. */
420 memcpy(kaddr, (u8*)ctx->attr + 462 memcpy(kaddr, (u8*)ctx->attr +
@@ -463,12 +505,15 @@ static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
463{ 505{
464 VCN vcn; 506 VCN vcn;
465 LCN lcn; 507 LCN lcn;
508 s64 initialized_size;
509 loff_t i_size;
466 sector_t block, dblock, iblock; 510 sector_t block, dblock, iblock;
467 struct inode *vi; 511 struct inode *vi;
468 ntfs_inode *ni; 512 ntfs_inode *ni;
469 ntfs_volume *vol; 513 ntfs_volume *vol;
470 runlist_element *rl; 514 runlist_element *rl;
471 struct buffer_head *bh, *head; 515 struct buffer_head *bh, *head;
516 unsigned long flags;
472 unsigned int blocksize, vcn_ofs; 517 unsigned int blocksize, vcn_ofs;
473 int err; 518 int err;
474 BOOL need_end_writeback; 519 BOOL need_end_writeback;
@@ -491,30 +536,37 @@ static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
491 BUG_ON(!PageUptodate(page)); 536 BUG_ON(!PageUptodate(page));
492 create_empty_buffers(page, blocksize, 537 create_empty_buffers(page, blocksize,
493 (1 << BH_Uptodate) | (1 << BH_Dirty)); 538 (1 << BH_Uptodate) | (1 << BH_Dirty));
539 if (unlikely(!page_has_buffers(page))) {
540 ntfs_warning(vol->sb, "Error allocating page "
541 "buffers. Redirtying page so we try "
542 "again later.");
543 /*
544 * Put the page back on mapping->dirty_pages, but leave
545 * its buffers' dirty state as-is.
546 */
547 redirty_page_for_writepage(wbc, page);
548 unlock_page(page);
549 return 0;
550 }
494 } 551 }
495 bh = head = page_buffers(page); 552 bh = head = page_buffers(page);
496 if (unlikely(!bh)) { 553 BUG_ON(!bh);
497 ntfs_warning(vol->sb, "Error allocating page buffers. "
498 "Redirtying page so we try again later.");
499 /*
500 * Put the page back on mapping->dirty_pages, but leave its
501 * buffer's dirty state as-is.
502 */
503 redirty_page_for_writepage(wbc, page);
504 unlock_page(page);
505 return 0;
506 }
507 554
508 /* NOTE: Different naming scheme to ntfs_read_block()! */ 555 /* NOTE: Different naming scheme to ntfs_read_block()! */
509 556
510 /* The first block in the page. */ 557 /* The first block in the page. */
511 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits); 558 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
512 559
560 read_lock_irqsave(&ni->size_lock, flags);
561 i_size = i_size_read(vi);
562 initialized_size = ni->initialized_size;
563 read_unlock_irqrestore(&ni->size_lock, flags);
564
513 /* The first out of bounds block for the data size. */ 565 /* The first out of bounds block for the data size. */
514 dblock = (vi->i_size + blocksize - 1) >> blocksize_bits; 566 dblock = (i_size + blocksize - 1) >> blocksize_bits;
515 567
516 /* The last (fully or partially) initialized block. */ 568 /* The last (fully or partially) initialized block. */
517 iblock = ni->initialized_size >> blocksize_bits; 569 iblock = initialized_size >> blocksize_bits;
518 570
519 /* 571 /*
520 * Be very careful. We have no exclusion from __set_page_dirty_buffers 572 * Be very careful. We have no exclusion from __set_page_dirty_buffers
@@ -559,7 +611,7 @@ static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
559 611
560 /* Make sure we have enough initialized size. */ 612 /* Make sure we have enough initialized size. */
561 if (unlikely((block >= iblock) && 613 if (unlikely((block >= iblock) &&
562 (ni->initialized_size < vi->i_size))) { 614 (initialized_size < i_size))) {
563 /* 615 /*
564 * If this page is fully outside initialized size, zero 616 * If this page is fully outside initialized size, zero
565 * out all pages between the current initialized size 617 * out all pages between the current initialized size
@@ -645,6 +697,27 @@ lock_retry_remap:
645 } 697 }
646 /* It is a hole, need to instantiate it. */ 698 /* It is a hole, need to instantiate it. */
647 if (lcn == LCN_HOLE) { 699 if (lcn == LCN_HOLE) {
700 u8 *kaddr;
701 unsigned long *bpos, *bend;
702
703 /* Check if the buffer is zero. */
704 kaddr = kmap_atomic(page, KM_USER0);
705 bpos = (unsigned long *)(kaddr + bh_offset(bh));
706 bend = (unsigned long *)((u8*)bpos + blocksize);
707 do {
708 if (unlikely(*bpos))
709 break;
710 } while (likely(++bpos < bend));
711 kunmap_atomic(kaddr, KM_USER0);
712 if (bpos == bend) {
713 /*
714 * Buffer is zero and sparse, no need to write
715 * it.
716 */
717 bh->b_blocknr = -1;
718 clear_buffer_dirty(bh);
719 continue;
720 }
648 // TODO: Instantiate the hole. 721 // TODO: Instantiate the hole.
649 // clear_buffer_new(bh); 722 // clear_buffer_new(bh);
650 // unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr); 723 // unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
@@ -665,19 +738,37 @@ lock_retry_remap:
665 if (likely(!err)) 738 if (likely(!err))
666 goto lock_retry_remap; 739 goto lock_retry_remap;
667 rl = NULL; 740 rl = NULL;
668 lcn = err; 741 } else if (!rl)
742 up_read(&ni->runlist.lock);
743 /*
744 * If buffer is outside the runlist, truncate has cut it out
745 * of the runlist. Just clean and clear the buffer and set it
746 * uptodate so it can get discarded by the VM.
747 */
748 if (err == -ENOENT || lcn == LCN_ENOENT) {
749 u8 *kaddr;
750
751 bh->b_blocknr = -1;
752 clear_buffer_dirty(bh);
753 kaddr = kmap_atomic(page, KM_USER0);
754 memset(kaddr + bh_offset(bh), 0, blocksize);
755 kunmap_atomic(kaddr, KM_USER0);
756 flush_dcache_page(page);
757 set_buffer_uptodate(bh);
758 err = 0;
759 continue;
669 } 760 }
670 /* Failed to map the buffer, even after retrying. */ 761 /* Failed to map the buffer, even after retrying. */
762 if (!err)
763 err = -EIO;
671 bh->b_blocknr = -1; 764 bh->b_blocknr = -1;
672 ntfs_error(vol->sb, "Failed to write to inode 0x%lx, " 765 ntfs_error(vol->sb, "Failed to write to inode 0x%lx, "
673 "attribute type 0x%x, vcn 0x%llx, offset 0x%x " 766 "attribute type 0x%x, vcn 0x%llx, offset 0x%x "
674 "because its location on disk could not be " 767 "because its location on disk could not be "
675 "determined%s (error code %lli).", ni->mft_no, 768 "determined%s (error code %i).", ni->mft_no,
676 ni->type, (unsigned long long)vcn, 769 ni->type, (unsigned long long)vcn,
677 vcn_ofs, is_retry ? " even after " 770 vcn_ofs, is_retry ? " even after "
678 "retrying" : "", (long long)lcn); 771 "retrying" : "", err);
679 if (!err)
680 err = -EIO;
681 break; 772 break;
682 } while (block++, (bh = bh->b_this_page) != head); 773 } while (block++, (bh = bh->b_this_page) != head);
683 774
@@ -688,7 +779,7 @@ lock_retry_remap:
688 /* For the error case, need to reset bh to the beginning. */ 779 /* For the error case, need to reset bh to the beginning. */
689 bh = head; 780 bh = head;
690 781
691 /* Just an optimization, so ->readpage() isn't called later. */ 782 /* Just an optimization, so ->readpage() is not called later. */
692 if (unlikely(!PageUptodate(page))) { 783 if (unlikely(!PageUptodate(page))) {
693 int uptodate = 1; 784 int uptodate = 1;
694 do { 785 do {
@@ -704,7 +795,6 @@ lock_retry_remap:
704 795
705 /* Setup all mapped, dirty buffers for async write i/o. */ 796 /* Setup all mapped, dirty buffers for async write i/o. */
706 do { 797 do {
707 get_bh(bh);
708 if (buffer_mapped(bh) && buffer_dirty(bh)) { 798 if (buffer_mapped(bh) && buffer_dirty(bh)) {
709 lock_buffer(bh); 799 lock_buffer(bh);
710 if (test_clear_buffer_dirty(bh)) { 800 if (test_clear_buffer_dirty(bh)) {
@@ -742,14 +832,8 @@ lock_retry_remap:
742 832
743 BUG_ON(PageWriteback(page)); 833 BUG_ON(PageWriteback(page));
744 set_page_writeback(page); /* Keeps try_to_free_buffers() away. */ 834 set_page_writeback(page); /* Keeps try_to_free_buffers() away. */
745 unlock_page(page);
746 835
747 /* 836 /* Submit the prepared buffers for i/o. */
748 * Submit the prepared buffers for i/o. Note the page is unlocked,
749 * and the async write i/o completion handler can end_page_writeback()
750 * at any time after the *first* submit_bh(). So the buffers can then
751 * disappear...
752 */
753 need_end_writeback = TRUE; 837 need_end_writeback = TRUE;
754 do { 838 do {
755 struct buffer_head *next = bh->b_this_page; 839 struct buffer_head *next = bh->b_this_page;
@@ -757,9 +841,9 @@ lock_retry_remap:
757 submit_bh(WRITE, bh); 841 submit_bh(WRITE, bh);
758 need_end_writeback = FALSE; 842 need_end_writeback = FALSE;
759 } 843 }
760 put_bh(bh);
761 bh = next; 844 bh = next;
762 } while (bh != head); 845 } while (bh != head);
846 unlock_page(page);
763 847
764 /* If no i/o was started, need to end_page_writeback(). */ 848 /* If no i/o was started, need to end_page_writeback(). */
765 if (unlikely(need_end_writeback)) 849 if (unlikely(need_end_writeback))
@@ -801,17 +885,15 @@ static int ntfs_write_mst_block(struct page *page,
801 ntfs_inode *ni = NTFS_I(vi); 885 ntfs_inode *ni = NTFS_I(vi);
802 ntfs_volume *vol = ni->vol; 886 ntfs_volume *vol = ni->vol;
803 u8 *kaddr; 887 u8 *kaddr;
804 unsigned char bh_size_bits = vi->i_blkbits;
805 unsigned int bh_size = 1 << bh_size_bits;
806 unsigned int rec_size = ni->itype.index.block_size; 888 unsigned int rec_size = ni->itype.index.block_size;
807 ntfs_inode *locked_nis[PAGE_CACHE_SIZE / rec_size]; 889 ntfs_inode *locked_nis[PAGE_CACHE_SIZE / rec_size];
808 struct buffer_head *bh, *head, *tbh, *rec_start_bh; 890 struct buffer_head *bh, *head, *tbh, *rec_start_bh;
809 int max_bhs = PAGE_CACHE_SIZE / bh_size; 891 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
810 struct buffer_head *bhs[max_bhs];
811 runlist_element *rl; 892 runlist_element *rl;
812 int i, nr_locked_nis, nr_recs, nr_bhs, bhs_per_rec, err, err2; 893 int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec, err, err2;
813 unsigned rec_size_bits; 894 unsigned bh_size, rec_size_bits;
814 BOOL sync, is_mft, page_is_dirty, rec_is_dirty; 895 BOOL sync, is_mft, page_is_dirty, rec_is_dirty;
896 unsigned char bh_size_bits;
815 897
816 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index " 898 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
817 "0x%lx.", vi->i_ino, ni->type, page->index); 899 "0x%lx.", vi->i_ino, ni->type, page->index);
@@ -826,13 +908,16 @@ static int ntfs_write_mst_block(struct page *page,
826 */ 908 */
827 BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) || 909 BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) ||
828 (NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION))); 910 (NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION)));
911 bh_size_bits = vi->i_blkbits;
912 bh_size = 1 << bh_size_bits;
913 max_bhs = PAGE_CACHE_SIZE / bh_size;
829 BUG_ON(!max_bhs); 914 BUG_ON(!max_bhs);
915 BUG_ON(max_bhs > MAX_BUF_PER_PAGE);
830 916
831 /* Were we called for sync purposes? */ 917 /* Were we called for sync purposes? */
832 sync = (wbc->sync_mode == WB_SYNC_ALL); 918 sync = (wbc->sync_mode == WB_SYNC_ALL);
833 919
834 /* Make sure we have mapped buffers. */ 920 /* Make sure we have mapped buffers. */
835 BUG_ON(!page_has_buffers(page));
836 bh = head = page_buffers(page); 921 bh = head = page_buffers(page);
837 BUG_ON(!bh); 922 BUG_ON(!bh);
838 923
@@ -846,7 +931,7 @@ static int ntfs_write_mst_block(struct page *page,
846 (PAGE_CACHE_SHIFT - bh_size_bits); 931 (PAGE_CACHE_SHIFT - bh_size_bits);
847 932
848 /* The first out of bounds block for the data size. */ 933 /* The first out of bounds block for the data size. */
849 dblock = (vi->i_size + bh_size - 1) >> bh_size_bits; 934 dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
850 935
851 rl = NULL; 936 rl = NULL;
852 err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0; 937 err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
@@ -858,6 +943,7 @@ static int ntfs_write_mst_block(struct page *page,
858 if (likely(block < rec_block)) { 943 if (likely(block < rec_block)) {
859 if (unlikely(block >= dblock)) { 944 if (unlikely(block >= dblock)) {
860 clear_buffer_dirty(bh); 945 clear_buffer_dirty(bh);
946 set_buffer_uptodate(bh);
861 continue; 947 continue;
862 } 948 }
863 /* 949 /*
@@ -895,6 +981,7 @@ static int ntfs_write_mst_block(struct page *page,
895 LCN lcn; 981 LCN lcn;
896 unsigned int vcn_ofs; 982 unsigned int vcn_ofs;
897 983
984 bh->b_bdev = vol->sb->s_bdev;
898 /* Obtain the vcn and offset of the current block. */ 985 /* Obtain the vcn and offset of the current block. */
899 vcn = (VCN)block << bh_size_bits; 986 vcn = (VCN)block << bh_size_bits;
900 vcn_ofs = vcn & vol->cluster_size_mask; 987 vcn_ofs = vcn & vol->cluster_size_mask;
@@ -938,8 +1025,11 @@ lock_retry_remap:
938 if (err2 == -ENOMEM) 1025 if (err2 == -ENOMEM)
939 page_is_dirty = TRUE; 1026 page_is_dirty = TRUE;
940 lcn = err2; 1027 lcn = err2;
941 } else 1028 } else {
942 err2 = -EIO; 1029 err2 = -EIO;
1030 if (!rl)
1031 up_read(&ni->runlist.lock);
1032 }
943 /* Hard error. Abort writing this record. */ 1033 /* Hard error. Abort writing this record. */
944 if (!err || err == -ENOMEM) 1034 if (!err || err == -ENOMEM)
945 err = err2; 1035 err = err2;
@@ -949,7 +1039,8 @@ lock_retry_remap:
949 "attribute type 0x%x) because " 1039 "attribute type 0x%x) because "
950 "its location on disk could " 1040 "its location on disk could "
951 "not be determined (error " 1041 "not be determined (error "
952 "code %lli).", (s64)block << 1042 "code %lli).",
1043 (long long)block <<
953 bh_size_bits >> 1044 bh_size_bits >>
954 vol->mft_record_size_bits, 1045 vol->mft_record_size_bits,
955 ni->mft_no, ni->type, 1046 ni->mft_no, ni->type,
@@ -1223,19 +1314,17 @@ done:
1223static int ntfs_writepage(struct page *page, struct writeback_control *wbc) 1314static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1224{ 1315{
1225 loff_t i_size; 1316 loff_t i_size;
1226 struct inode *vi; 1317 struct inode *vi = page->mapping->host;
1227 ntfs_inode *ni, *base_ni; 1318 ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
1228 char *kaddr; 1319 char *kaddr;
1229 ntfs_attr_search_ctx *ctx; 1320 ntfs_attr_search_ctx *ctx = NULL;
1230 MFT_RECORD *m; 1321 MFT_RECORD *m = NULL;
1231 u32 attr_len; 1322 u32 attr_len;
1232 int err; 1323 int err;
1233 1324
1325retry_writepage:
1234 BUG_ON(!PageLocked(page)); 1326 BUG_ON(!PageLocked(page));
1235
1236 vi = page->mapping->host;
1237 i_size = i_size_read(vi); 1327 i_size = i_size_read(vi);
1238
1239 /* Is the page fully outside i_size? (truncate in progress) */ 1328 /* Is the page fully outside i_size? (truncate in progress) */
1240 if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >> 1329 if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >>
1241 PAGE_CACHE_SHIFT)) { 1330 PAGE_CACHE_SHIFT)) {
@@ -1248,40 +1337,42 @@ static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1248 ntfs_debug("Write outside i_size - truncated?"); 1337 ntfs_debug("Write outside i_size - truncated?");
1249 return 0; 1338 return 0;
1250 } 1339 }
1251 ni = NTFS_I(vi); 1340 /*
1252 1341 * Only $DATA attributes can be encrypted and only unnamed $DATA
1342 * attributes can be compressed. Index root can have the flags set but
1343 * this means to create compressed/encrypted files, not that the
1344 * attribute is compressed/encrypted.
1345 */
1346 if (ni->type != AT_INDEX_ROOT) {
1347 /* If file is encrypted, deny access, just like NT4. */
1348 if (NInoEncrypted(ni)) {
1349 unlock_page(page);
1350 BUG_ON(ni->type != AT_DATA);
1351 ntfs_debug("Denying write access to encrypted "
1352 "file.");
1353 return -EACCES;
1354 }
1355 /* Compressed data streams are handled in compress.c. */
1356 if (NInoNonResident(ni) && NInoCompressed(ni)) {
1357 BUG_ON(ni->type != AT_DATA);
1358 BUG_ON(ni->name_len);
1359 // TODO: Implement and replace this with
1360 // return ntfs_write_compressed_block(page);
1361 unlock_page(page);
1362 ntfs_error(vi->i_sb, "Writing to compressed files is "
1363 "not supported yet. Sorry.");
1364 return -EOPNOTSUPP;
1365 }
1366 // TODO: Implement and remove this check.
1367 if (NInoNonResident(ni) && NInoSparse(ni)) {
1368 unlock_page(page);
1369 ntfs_error(vi->i_sb, "Writing to sparse files is not "
1370 "supported yet. Sorry.");
1371 return -EOPNOTSUPP;
1372 }
1373 }
1253 /* NInoNonResident() == NInoIndexAllocPresent() */ 1374 /* NInoNonResident() == NInoIndexAllocPresent() */
1254 if (NInoNonResident(ni)) { 1375 if (NInoNonResident(ni)) {
1255 /*
1256 * Only unnamed $DATA attributes can be compressed, encrypted,
1257 * and/or sparse.
1258 */
1259 if (ni->type == AT_DATA && !ni->name_len) {
1260 /* If file is encrypted, deny access, just like NT4. */
1261 if (NInoEncrypted(ni)) {
1262 unlock_page(page);
1263 ntfs_debug("Denying write access to encrypted "
1264 "file.");
1265 return -EACCES;
1266 }
1267 /* Compressed data streams are handled in compress.c. */
1268 if (NInoCompressed(ni)) {
1269 // TODO: Implement and replace this check with
1270 // return ntfs_write_compressed_block(page);
1271 unlock_page(page);
1272 ntfs_error(vi->i_sb, "Writing to compressed "
1273 "files is not supported yet. "
1274 "Sorry.");
1275 return -EOPNOTSUPP;
1276 }
1277 // TODO: Implement and remove this check.
1278 if (NInoSparse(ni)) {
1279 unlock_page(page);
1280 ntfs_error(vi->i_sb, "Writing to sparse files "
1281 "is not supported yet. Sorry.");
1282 return -EOPNOTSUPP;
1283 }
1284 }
1285 /* We have to zero every time due to mmap-at-end-of-file. */ 1376 /* We have to zero every time due to mmap-at-end-of-file. */
1286 if (page->index >= (i_size >> PAGE_CACHE_SHIFT)) { 1377 if (page->index >= (i_size >> PAGE_CACHE_SHIFT)) {
1287 /* The page straddles i_size. */ 1378 /* The page straddles i_size. */
@@ -1294,14 +1385,16 @@ static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1294 /* Handle mst protected attributes. */ 1385 /* Handle mst protected attributes. */
1295 if (NInoMstProtected(ni)) 1386 if (NInoMstProtected(ni))
1296 return ntfs_write_mst_block(page, wbc); 1387 return ntfs_write_mst_block(page, wbc);
1297 /* Normal data stream. */ 1388 /* Normal, non-resident data stream. */
1298 return ntfs_write_block(page, wbc); 1389 return ntfs_write_block(page, wbc);
1299 } 1390 }
1300 /* 1391 /*
1301 * Attribute is resident, implying it is not compressed, encrypted, 1392 * Attribute is resident, implying it is not compressed, encrypted, or
1302 * sparse, or mst protected. This also means the attribute is smaller 1393 * mst protected. This also means the attribute is smaller than an mft
1303 * than an mft record and hence smaller than a page, so can simply 1394 * record and hence smaller than a page, so can simply return error on
1304 * return error on any pages with index above 0. 1395 * any pages with index above 0. Note the attribute can actually be
1396 * marked compressed but if it is resident the actual data is not
1397 * compressed so we are ok to ignore the compressed flag here.
1305 */ 1398 */
1306 BUG_ON(page_has_buffers(page)); 1399 BUG_ON(page_has_buffers(page));
1307 BUG_ON(!PageUptodate(page)); 1400 BUG_ON(!PageUptodate(page));
@@ -1326,6 +1419,14 @@ static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1326 ctx = NULL; 1419 ctx = NULL;
1327 goto err_out; 1420 goto err_out;
1328 } 1421 }
1422 /*
1423 * If a parallel write made the attribute non-resident, drop the mft
1424 * record and retry the writepage.
1425 */
1426 if (unlikely(NInoNonResident(ni))) {
1427 unmap_mft_record(base_ni);
1428 goto retry_writepage;
1429 }
1329 ctx = ntfs_attr_get_search_ctx(base_ni, m); 1430 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1330 if (unlikely(!ctx)) { 1431 if (unlikely(!ctx)) {
1331 err = -ENOMEM; 1432 err = -ENOMEM;
@@ -1342,40 +1443,21 @@ static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1342 BUG_ON(PageWriteback(page)); 1443 BUG_ON(PageWriteback(page));
1343 set_page_writeback(page); 1444 set_page_writeback(page);
1344 unlock_page(page); 1445 unlock_page(page);
1345
1346 /* 1446 /*
1347 * Here, we don't need to zero the out of bounds area everytime because 1447 * Here, we do not need to zero the out of bounds area everytime
1348 * the below memcpy() already takes care of the mmap-at-end-of-file 1448 * because the below memcpy() already takes care of the
1349 * requirements. If the file is converted to a non-resident one, then 1449 * mmap-at-end-of-file requirements. If the file is converted to a
1350 * the code path use is switched to the non-resident one where the 1450 * non-resident one, then the code path use is switched to the
1351 * zeroing happens on each ntfs_writepage() invocation. 1451 * non-resident one where the zeroing happens on each ntfs_writepage()
1352 * 1452 * invocation.
1353 * The above also applies nicely when i_size is decreased.
1354 *
1355 * When i_size is increased, the memory between the old and new i_size
1356 * _must_ be zeroed (or overwritten with new data). Otherwise we will
1357 * expose data to userspace/disk which should never have been exposed.
1358 *
1359 * FIXME: Ensure that i_size increases do the zeroing/overwriting and
1360 * if we cannot guarantee that, then enable the zeroing below. If the
1361 * zeroing below is enabled, we MUST move the unlock_page() from above
1362 * to after the kunmap_atomic(), i.e. just before the
1363 * end_page_writeback().
1364 * UPDATE: ntfs_prepare/commit_write() do the zeroing on i_size
1365 * increases for resident attributes so those are ok.
1366 * TODO: ntfs_truncate(), others?
1367 */ 1453 */
1368
1369 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length); 1454 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
1370 i_size = i_size_read(VFS_I(ni)); 1455 i_size = i_size_read(vi);
1371 kaddr = kmap_atomic(page, KM_USER0);
1372 if (unlikely(attr_len > i_size)) { 1456 if (unlikely(attr_len > i_size)) {
1373 /* Zero out of bounds area in the mft record. */
1374 memset((u8*)ctx->attr + le16_to_cpu(
1375 ctx->attr->data.resident.value_offset) +
1376 i_size, 0, attr_len - i_size);
1377 attr_len = i_size; 1457 attr_len = i_size;
1458 ctx->attr->data.resident.value_length = cpu_to_le32(attr_len);
1378 } 1459 }
1460 kaddr = kmap_atomic(page, KM_USER0);
1379 /* Copy the data from the page to the mft record. */ 1461 /* Copy the data from the page to the mft record. */
1380 memcpy((u8*)ctx->attr + 1462 memcpy((u8*)ctx->attr +
1381 le16_to_cpu(ctx->attr->data.resident.value_offset), 1463 le16_to_cpu(ctx->attr->data.resident.value_offset),
@@ -1405,8 +1487,10 @@ err_out:
1405 err = 0; 1487 err = 0;
1406 } else { 1488 } else {
1407 ntfs_error(vi->i_sb, "Resident attribute write failed with " 1489 ntfs_error(vi->i_sb, "Resident attribute write failed with "
1408 "error %i. Setting page error flag.", err); 1490 "error %i.", err);
1409 SetPageError(page); 1491 SetPageError(page);
1492 NVolSetErrors(ni->vol);
1493 make_bad_inode(vi);
1410 } 1494 }
1411 unlock_page(page); 1495 unlock_page(page);
1412 if (ctx) 1496 if (ctx)
@@ -1425,12 +1509,15 @@ static int ntfs_prepare_nonresident_write(struct page *page,
1425{ 1509{
1426 VCN vcn; 1510 VCN vcn;
1427 LCN lcn; 1511 LCN lcn;
1512 s64 initialized_size;
1513 loff_t i_size;
1428 sector_t block, ablock, iblock; 1514 sector_t block, ablock, iblock;
1429 struct inode *vi; 1515 struct inode *vi;
1430 ntfs_inode *ni; 1516 ntfs_inode *ni;
1431 ntfs_volume *vol; 1517 ntfs_volume *vol;
1432 runlist_element *rl; 1518 runlist_element *rl;
1433 struct buffer_head *bh, *head, *wait[2], **wait_bh = wait; 1519 struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
1520 unsigned long flags;
1434 unsigned int vcn_ofs, block_start, block_end, blocksize; 1521 unsigned int vcn_ofs, block_start, block_end, blocksize;
1435 int err; 1522 int err;
1436 BOOL is_retry; 1523 BOOL is_retry;
@@ -1462,16 +1549,20 @@ static int ntfs_prepare_nonresident_write(struct page *page,
1462 /* The first block in the page. */ 1549 /* The first block in the page. */
1463 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits); 1550 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
1464 1551
1552 read_lock_irqsave(&ni->size_lock, flags);
1465 /* 1553 /*
1466 * The first out of bounds block for the allocated size. No need to 1554 * The first out of bounds block for the allocated size. No need to
1467 * round up as allocated_size is in multiples of cluster size and the 1555 * round up as allocated_size is in multiples of cluster size and the
1468 * minimum cluster size is 512 bytes, which is equal to the smallest 1556 * minimum cluster size is 512 bytes, which is equal to the smallest
1469 * blocksize. 1557 * blocksize.
1470 */ 1558 */
1471 ablock = ni->allocated_size >> blocksize_bits; 1559 ablock = ni->allocated_size >> blocksize_bits;
1560 i_size = i_size_read(vi);
1561 initialized_size = ni->initialized_size;
1562 read_unlock_irqrestore(&ni->size_lock, flags);
1472 1563
1473 /* The last (fully or partially) initialized block. */ 1564 /* The last (fully or partially) initialized block. */
1474 iblock = ni->initialized_size >> blocksize_bits; 1565 iblock = initialized_size >> blocksize_bits;
1475 1566
1476 /* Loop through all the buffers in the page. */ 1567 /* Loop through all the buffers in the page. */
1477 block_start = 0; 1568 block_start = 0;
@@ -1518,7 +1609,7 @@ static int ntfs_prepare_nonresident_write(struct page *page,
1518 * request, i.e. block < ablock is true. 1609 * request, i.e. block < ablock is true.
1519 */ 1610 */
1520 if (unlikely((block >= iblock) && 1611 if (unlikely((block >= iblock) &&
1521 (ni->initialized_size < vi->i_size))) { 1612 (initialized_size < i_size))) {
1522 /* 1613 /*
1523 * If this page is fully outside initialized size, zero 1614 * If this page is fully outside initialized size, zero
1524 * out all pages between the current initialized size 1615 * out all pages between the current initialized size
@@ -1622,6 +1713,8 @@ lock_retry_remap:
1622 "not supported yet. " 1713 "not supported yet. "
1623 "Sorry."); 1714 "Sorry.");
1624 err = -EOPNOTSUPP; 1715 err = -EOPNOTSUPP;
1716 if (!rl)
1717 up_read(&ni->runlist.lock);
1625 goto err_out; 1718 goto err_out;
1626 } else if (!is_retry && 1719 } else if (!is_retry &&
1627 lcn == LCN_RL_NOT_MAPPED) { 1720 lcn == LCN_RL_NOT_MAPPED) {
@@ -1635,26 +1728,25 @@ lock_retry_remap:
1635 if (likely(!err)) 1728 if (likely(!err))
1636 goto lock_retry_remap; 1729 goto lock_retry_remap;
1637 rl = NULL; 1730 rl = NULL;
1638 lcn = err; 1731 } else if (!rl)
1639 } 1732 up_read(&ni->runlist.lock);
1640 /* 1733 /*
1641 * Failed to map the buffer, even after 1734 * Failed to map the buffer, even after
1642 * retrying. 1735 * retrying.
1643 */ 1736 */
1737 if (!err)
1738 err = -EIO;
1644 bh->b_blocknr = -1; 1739 bh->b_blocknr = -1;
1645 ntfs_error(vol->sb, "Failed to write to inode " 1740 ntfs_error(vol->sb, "Failed to write to inode "
1646 "0x%lx, attribute type 0x%x, " 1741 "0x%lx, attribute type 0x%x, "
1647 "vcn 0x%llx, offset 0x%x " 1742 "vcn 0x%llx, offset 0x%x "
1648 "because its location on disk " 1743 "because its location on disk "
1649 "could not be determined%s " 1744 "could not be determined%s "
1650 "(error code %lli).", 1745 "(error code %i).",
1651 ni->mft_no, ni->type, 1746 ni->mft_no, ni->type,
1652 (unsigned long long)vcn, 1747 (unsigned long long)vcn,
1653 vcn_ofs, is_retry ? " even " 1748 vcn_ofs, is_retry ? " even "
1654 "after retrying" : "", 1749 "after retrying" : "", err);
1655 (long long)lcn);
1656 if (!err)
1657 err = -EIO;
1658 goto err_out; 1750 goto err_out;
1659 } 1751 }
1660 /* We now have a successful remap, i.e. lcn >= 0. */ 1752 /* We now have a successful remap, i.e. lcn >= 0. */
@@ -1797,6 +1889,7 @@ static int ntfs_prepare_write(struct file *file, struct page *page,
1797 unsigned from, unsigned to) 1889 unsigned from, unsigned to)
1798{ 1890{
1799 s64 new_size; 1891 s64 new_size;
1892 loff_t i_size;
1800 struct inode *vi = page->mapping->host; 1893 struct inode *vi = page->mapping->host;
1801 ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi); 1894 ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
1802 ntfs_volume *vol = ni->vol; 1895 ntfs_volume *vol = ni->vol;
@@ -1868,14 +1961,8 @@ static int ntfs_prepare_write(struct file *file, struct page *page,
1868 BUG_ON(page_has_buffers(page)); 1961 BUG_ON(page_has_buffers(page));
1869 new_size = ((s64)page->index << PAGE_CACHE_SHIFT) + to; 1962 new_size = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
1870 /* If we do not need to resize the attribute allocation we are done. */ 1963 /* If we do not need to resize the attribute allocation we are done. */
1871 if (new_size <= vi->i_size) 1964 if (new_size <= i_size_read(vi))
1872 goto done; 1965 goto done;
1873
1874 // FIXME: We abort for now as this code is not safe.
1875 ntfs_error(vi->i_sb, "Changing the file size is not supported yet. "
1876 "Sorry.");
1877 return -EOPNOTSUPP;
1878
1879 /* Map, pin, and lock the (base) mft record. */ 1966 /* Map, pin, and lock the (base) mft record. */
1880 if (!NInoAttr(ni)) 1967 if (!NInoAttr(ni))
1881 base_ni = ni; 1968 base_ni = ni;
@@ -1904,7 +1991,15 @@ static int ntfs_prepare_write(struct file *file, struct page *page,
1904 a = ctx->attr; 1991 a = ctx->attr;
1905 /* The total length of the attribute value. */ 1992 /* The total length of the attribute value. */
1906 attr_len = le32_to_cpu(a->data.resident.value_length); 1993 attr_len = le32_to_cpu(a->data.resident.value_length);
1907 BUG_ON(vi->i_size != attr_len); 1994 /* Fix an eventual previous failure of ntfs_commit_write(). */
1995 i_size = i_size_read(vi);
1996 if (unlikely(attr_len > i_size)) {
1997 attr_len = i_size;
1998 a->data.resident.value_length = cpu_to_le32(attr_len);
1999 }
2000 /* If we do not need to resize the attribute allocation we are done. */
2001 if (new_size <= attr_len)
2002 goto done_unm;
1908 /* Check if new size is allowed in $AttrDef. */ 2003 /* Check if new size is allowed in $AttrDef. */
1909 err = ntfs_attr_size_bounds_check(vol, ni->type, new_size); 2004 err = ntfs_attr_size_bounds_check(vol, ni->type, new_size);
1910 if (unlikely(err)) { 2005 if (unlikely(err)) {
@@ -1962,6 +2057,7 @@ static int ntfs_prepare_write(struct file *file, struct page *page,
1962 } 2057 }
1963 flush_dcache_mft_record_page(ctx->ntfs_ino); 2058 flush_dcache_mft_record_page(ctx->ntfs_ino);
1964 mark_mft_record_dirty(ctx->ntfs_ino); 2059 mark_mft_record_dirty(ctx->ntfs_ino);
2060done_unm:
1965 ntfs_attr_put_search_ctx(ctx); 2061 ntfs_attr_put_search_ctx(ctx);
1966 unmap_mft_record(base_ni); 2062 unmap_mft_record(base_ni);
1967 /* 2063 /*
@@ -2047,7 +2143,7 @@ static int ntfs_commit_nonresident_write(struct page *page,
2047 * now we know ntfs_prepare_write() would have failed in the write 2143 * now we know ntfs_prepare_write() would have failed in the write
2048 * exceeds i_size case, so this will never trigger which is fine. 2144 * exceeds i_size case, so this will never trigger which is fine.
2049 */ 2145 */
2050 if (pos > vi->i_size) { 2146 if (pos > i_size_read(vi)) {
2051 ntfs_error(vi->i_sb, "Writing beyond the existing file size is " 2147 ntfs_error(vi->i_sb, "Writing beyond the existing file size is "
2052 "not supported yet. Sorry."); 2148 "not supported yet. Sorry.");
2053 return -EOPNOTSUPP; 2149 return -EOPNOTSUPP;
@@ -2183,9 +2279,13 @@ static int ntfs_commit_write(struct file *file, struct page *page,
2183 } 2279 }
2184 kunmap_atomic(kaddr, KM_USER0); 2280 kunmap_atomic(kaddr, KM_USER0);
2185 /* Update i_size if necessary. */ 2281 /* Update i_size if necessary. */
2186 if (vi->i_size < attr_len) { 2282 if (i_size_read(vi) < attr_len) {
2283 unsigned long flags;
2284
2285 write_lock_irqsave(&ni->size_lock, flags);
2187 ni->allocated_size = ni->initialized_size = attr_len; 2286 ni->allocated_size = ni->initialized_size = attr_len;
2188 i_size_write(vi, attr_len); 2287 i_size_write(vi, attr_len);
2288 write_unlock_irqrestore(&ni->size_lock, flags);
2189 } 2289 }
2190 /* Mark the mft record dirty, so it gets written back. */ 2290 /* Mark the mft record dirty, so it gets written back. */
2191 flush_dcache_mft_record_page(ctx->ntfs_ino); 2291 flush_dcache_mft_record_page(ctx->ntfs_ino);
@@ -2302,6 +2402,7 @@ void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) {
2302 buffers_to_free = bh; 2402 buffers_to_free = bh;
2303 } 2403 }
2304 bh = head = page_buffers(page); 2404 bh = head = page_buffers(page);
2405 BUG_ON(!bh);
2305 do { 2406 do {
2306 bh_ofs = bh_offset(bh); 2407 bh_ofs = bh_offset(bh);
2307 if (bh_ofs + bh_size <= ofs) 2408 if (bh_ofs + bh_size <= ofs)