1/** 2 * aops.c - NTFS kernel address space operations and page cache handling. 3 * Part of the Linux-NTFS project. 4 * 5 * Copyright (c) 2001-2007 Anton Altaparmakov 6 * Copyright (c) 2002 Richard Russon 7 * 8 * This program/include file is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License as published 10 * by the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program/include file is distributed in the hope that it will be 14 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program (in the main directory of the Linux-NTFS 20 * distribution in the file COPYING); if not, write to the Free Software 21 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 */ 23 24#include <linux/errno.h> 25#include <linux/fs.h> 26#include <linux/gfp.h> 27#include <linux/mm.h> 28#include <linux/pagemap.h> 29#include <linux/swap.h> 30#include <linux/buffer_head.h> 31#include <linux/writeback.h> 32#include <linux/bit_spinlock.h> 33 34#include "aops.h" 35#include "attrib.h" 36#include "debug.h" 37#include "inode.h" 38#include "mft.h" 39#include "runlist.h" 40#include "types.h" 41#include "ntfs.h" 42 43/** 44 * ntfs_end_buffer_async_read - async io completion for reading attributes 45 * @bh: buffer head on which io is completed 46 * @uptodate: whether @bh is now uptodate or not 47 * 48 * Asynchronous I/O completion handler for reading pages belonging to the 49 * attribute address space of an inode. The inodes can either be files or 50 * directories or they can be fake inodes describing some attribute. 51 * 52 * If NInoMstProtected(), perform the post read mst fixups when all IO on the 53 * page has been completed and mark the page uptodate or set the error bit on 54 * the page. To determine the size of the records that need fixing up, we 55 * cheat a little bit by setting the index_block_size in ntfs_inode to the ntfs 56 * record size, and index_block_size_bits, to the log(base 2) of the ntfs 57 * record size. 58 */ 59static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate) 60{ 61 unsigned long flags; 62 struct buffer_head *first, *tmp; 63 struct page *page; 64 struct inode *vi; 65 ntfs_inode *ni; 66 int page_uptodate = 1; 67 68 page = bh->b_page; 69 vi = page->mapping->host; 70 ni = NTFS_I(vi); 71 72 if (likely(uptodate)) { 73 loff_t i_size; 74 s64 file_ofs, init_size; 75 76 set_buffer_uptodate(bh); 77 78 file_ofs = ((s64)page->index << PAGE_CACHE_SHIFT) + 79 bh_offset(bh); 80 read_lock_irqsave(&ni->size_lock, flags); 81 init_size = ni->initialized_size; 82 i_size = i_size_read(vi); 83 read_unlock_irqrestore(&ni->size_lock, flags); 84 if (unlikely(init_size > i_size)) { 85 /* Race with shrinking truncate. */ 86 init_size = i_size; 87 } 88 /* Check for the current buffer head overflowing. */ 89 if (unlikely(file_ofs + bh->b_size > init_size)) { 90 int ofs; 91 void *kaddr; 92 93 ofs = 0; 94 if (file_ofs < init_size) 95 ofs = init_size - file_ofs; 96 local_irq_save(flags); 97 kaddr = kmap_atomic(page, KM_BIO_SRC_IRQ); 98 memset(kaddr + bh_offset(bh) + ofs, 0, 99 bh->b_size - ofs); 100 flush_dcache_page(page); 101 kunmap_atomic(kaddr, KM_BIO_SRC_IRQ); 102 local_irq_restore(flags); 103 } 104 } else { 105 clear_buffer_uptodate(bh); 106 SetPageError(page); 107 ntfs_error(ni->vol->sb, "Buffer I/O error, logical block " 108 "0x%llx.", (unsigned long long)bh->b_blocknr); 109 } 110 first = page_buffers(page); 111 local_irq_save(flags); 112 bit_spin_lock(BH_Uptodate_Lock, &first->b_state); 113 clear_buffer_async_read(bh); 114 unlock_buffer(bh); 115 tmp = bh; 116 do { 117 if (!buffer_uptodate(tmp)) 118 page_uptodate = 0; 119 if (buffer_async_read(tmp)) { 120 if (likely(buffer_locked(tmp))) 121 goto still_busy; 122 /* Async buffers must be locked. */ 123 BUG(); 124 } 125 tmp = tmp->b_this_page; 126 } while (tmp != bh); 127 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state); 128 local_irq_restore(flags); 129 /* 130 * If none of the buffers had errors then we can set the page uptodate, 131 * but we first have to perform the post read mst fixups, if the 132 * attribute is mst protected, i.e. if NInoMstProteced(ni) is true. 133 * Note we ignore fixup errors as those are detected when 134 * map_mft_record() is called which gives us per record granularity 135 * rather than per page granularity. 136 */ 137 if (!NInoMstProtected(ni)) { 138 if (likely(page_uptodate && !PageError(page))) 139 SetPageUptodate(page); 140 } else { 141 u8 *kaddr; 142 unsigned int i, recs; 143 u32 rec_size; 144 145 rec_size = ni->itype.index.block_size; 146 recs = PAGE_CACHE_SIZE / rec_size; 147 /* Should have been verified before we got here... */ 148 BUG_ON(!recs); 149 local_irq_save(flags); 150 kaddr = kmap_atomic(page, KM_BIO_SRC_IRQ); 151 for (i = 0; i < recs; i++) 152 post_read_mst_fixup((NTFS_RECORD*)(kaddr + 153 i * rec_size), rec_size); 154 kunmap_atomic(kaddr, KM_BIO_SRC_IRQ); 155 local_irq_restore(flags); 156 flush_dcache_page(page); 157 if (likely(page_uptodate && !PageError(page))) 158 SetPageUptodate(page); 159 } 160 unlock_page(page); 161 return; 162still_busy: 163 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state); 164 local_irq_restore(flags); 165 return; 166} 167 168/** 169 * ntfs_read_block - fill a @page of an address space with data 170 * @page: page cache page to fill with data 171 * 172 * Fill the page @page of the address space belonging to the @page->host inode. 173 * We read each buffer asynchronously and when all buffers are read in, our io 174 * completion handler ntfs_end_buffer_read_async(), if required, automatically 175 * applies the mst fixups to the page before finally marking it uptodate and 176 * unlocking it. 177 * 178 * We only enforce allocated_size limit because i_size is checked for in 179 * generic_file_read(). 180 * 181 * Return 0 on success and -errno on error. 182 * 183 * Contains an adapted version of fs/buffer.c::block_read_full_page(). 184 */ 185static int ntfs_read_block(struct page *page) 186{ 187 loff_t i_size; 188 VCN vcn; 189 LCN lcn; 190 s64 init_size; 191 struct inode *vi; 192 ntfs_inode *ni; 193 ntfs_volume *vol; 194 runlist_element *rl; 195 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE]; 196 sector_t iblock, lblock, zblock; 197 unsigned long flags; 198 unsigned int blocksize, vcn_ofs; 199 int i, nr; 200 unsigned char blocksize_bits; 201 202 vi = page->mapping->host; 203 ni = NTFS_I(vi); 204 vol = ni->vol; 205 206 /* $MFT/$DATA must have its complete runlist in memory at all times. */ 207 BUG_ON(!ni->runlist.rl && !ni->mft_no && !NInoAttr(ni)); 208 209 blocksize = vol->sb->s_blocksize; 210 blocksize_bits = vol->sb->s_blocksize_bits; 211 212 if (!page_has_buffers(page)) { 213 create_empty_buffers(page, blocksize, 0); 214 if (unlikely(!page_has_buffers(page))) { 215 unlock_page(page); 216 return -ENOMEM; 217 } 218 } 219 bh = head = page_buffers(page); 220 BUG_ON(!bh); 221 222 /* 223 * We may be racing with truncate. To avoid some of the problems we 224 * now take a snapshot of the various sizes and use those for the whole 225 * of the function. In case of an extending truncate it just means we 226 * may leave some buffers unmapped which are now allocated. This is 227 * not a problem since these buffers will just get mapped when a write 228 * occurs. In case of a shrinking truncate, we will detect this later 229 * on due to the runlist being incomplete and if the page is being 230 * fully truncated, truncate will throw it away as soon as we unlock 231 * it so no need to worry what we do with it. 232 */ 233 iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits); 234 read_lock_irqsave(&ni->size_lock, flags); 235 lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits; 236 init_size = ni->initialized_size; 237 i_size = i_size_read(vi); 238 read_unlock_irqrestore(&ni->size_lock, flags); 239 if (unlikely(init_size > i_size)) { 240 /* Race with shrinking truncate. */ 241 init_size = i_size; 242 } 243 zblock = (init_size + blocksize - 1) >> blocksize_bits; 244 245 /* Loop through all the buffers in the page. */ 246 rl = NULL; 247 nr = i = 0; 248 do { 249 int err = 0; 250 251 if (unlikely(buffer_uptodate(bh))) 252 continue; 253 if (unlikely(buffer_mapped(bh))) { 254 arr[nr++] = bh; 255 continue; 256 } 257 bh->b_bdev = vol->sb->s_bdev; 258 /* Is the block within the allowed limits? */ 259 if (iblock < lblock) { 260 bool is_retry = false; 261 262 /* Convert iblock into corresponding vcn and offset. */ 263 vcn = (VCN)iblock << blocksize_bits >> 264 vol->cluster_size_bits; 265 vcn_ofs = ((VCN)iblock << blocksize_bits) & 266 vol->cluster_size_mask; 267 if (!rl) { 268lock_retry_remap: 269 down_read(&ni->runlist.lock); 270 rl = ni->runlist.rl; 271 } 272 if (likely(rl != NULL)) { 273 /* Seek to element containing target vcn. */ 274 while (rl->length && rl[1].vcn <= vcn) 275 rl++; 276 lcn = ntfs_rl_vcn_to_lcn(rl, vcn); 277 } else 278 lcn = LCN_RL_NOT_MAPPED; 279 /* Successful remap. */ 280 if (lcn >= 0) { 281 /* Setup buffer head to correct block. */ 282 bh->b_blocknr = ((lcn << vol->cluster_size_bits) 283 + vcn_ofs) >> blocksize_bits; 284 set_buffer_mapped(bh); 285 /* Only read initialized data blocks. */ 286 if (iblock < zblock) { 287 arr[nr++] = bh; 288 continue; 289 } 290 /* Fully non-initialized data block, zero it. */ 291 goto handle_zblock; 292 } 293 /* It is a hole, need to zero it. */ 294 if (lcn == LCN_HOLE) 295 goto handle_hole; 296 /* If first try and runlist unmapped, map and retry. */ 297 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) { 298 is_retry = true; 299 /* 300 * Attempt to map runlist, dropping lock for 301 * the duration. 302 */ 303 up_read(&ni->runlist.lock); 304 err = ntfs_map_runlist(ni, vcn); 305 if (likely(!err)) 306 goto lock_retry_remap; 307 rl = NULL; 308 } else if (!rl) 309 up_read(&ni->runlist.lock); 310 /* 311 * If buffer is outside the runlist, treat it as a 312 * hole. This can happen due to concurrent truncate 313 * for example. 314 */ 315 if (err == -ENOENT || lcn == LCN_ENOENT) { 316 err = 0; 317 goto handle_hole; 318 } 319 /* Hard error, zero out region. */ 320 if (!err) 321 err = -EIO; 322 bh->b_blocknr = -1; 323 SetPageError(page); 324 ntfs_error(vol->sb, "Failed to read from inode 0x%lx, " 325 "attribute type 0x%x, vcn 0x%llx, " 326 "offset 0x%x because its location on " 327 "disk could not be determined%s " 328 "(error code %i).", ni->mft_no, 329 ni->type, (unsigned long long)vcn, 330 vcn_ofs, is_retry ? " even after " 331 "retrying" : "", err); 332 } 333 /* 334 * Either iblock was outside lblock limits or 335 * ntfs_rl_vcn_to_lcn() returned error. Just zero that portion 336 * of the page and set the buffer uptodate. 337 */ 338handle_hole: 339 bh->b_blocknr = -1UL; 340 clear_buffer_mapped(bh); 341handle_zblock: 342 zero_user(page, i * blocksize, blocksize); 343 if (likely(!err)) 344 set_buffer_uptodate(bh); 345 } while (i++, iblock++, (bh = bh->b_this_page) != head); 346 347 /* Release the lock if we took it. */ 348 if (rl) 349 up_read(&ni->runlist.lock); 350 351 /* Check we have at least one buffer ready for i/o. */ 352 if (nr) { 353 struct buffer_head *tbh; 354 355 /* Lock the buffers. */ 356 for (i = 0; i < nr; i++) { 357 tbh = arr[i]; 358 lock_buffer(tbh); 359 tbh->b_end_io = ntfs_end_buffer_async_read; 360 set_buffer_async_read(tbh); 361 } 362 /* Finally, start i/o on the buffers. */ 363 for (i = 0; i < nr; i++) { 364 tbh = arr[i]; 365 if (likely(!buffer_uptodate(tbh))) 366 submit_bh(READ, tbh); 367 else 368 ntfs_end_buffer_async_read(tbh, 1); 369 } 370 return 0; 371 } 372 /* No i/o was scheduled on any of the buffers. */ 373 if (likely(!PageError(page))) 374 SetPageUptodate(page); 375 else /* Signal synchronous i/o error. */ 376 nr = -EIO; 377 unlock_page(page); 378 return nr; 379} 380 381/** 382 * ntfs_readpage - fill a @page of a @file with data from the device 383 * @file: open file to which the page @page belongs or NULL 384 * @page: page cache page to fill with data 385 * 386 * For non-resident attributes, ntfs_readpage() fills the @page of the open 387 * file @file by calling the ntfs version of the generic block_read_full_page() 388 * function, ntfs_read_block(), which in turn creates and reads in the buffers 389 * associated with the page asynchronously. 390 * 391 * For resident attributes, OTOH, ntfs_readpage() fills @page by copying the 392 * data from the mft record (which at this stage is most likely in memory) and 393 * fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as 394 * even if the mft record is not cached at this point in time, we need to wait 395 * for it to be read in before we can do the copy. 396 * 397 * Return 0 on success and -errno on error. 398 */ 399static int ntfs_readpage(struct file *file, struct page *page) 400{ 401 loff_t i_size; 402 struct inode *vi; 403 ntfs_inode *ni, *base_ni; 404 u8 *addr; 405 ntfs_attr_search_ctx *ctx; 406 MFT_RECORD *mrec; 407 unsigned long flags; 408 u32 attr_len; 409 int err = 0; 410 411retry_readpage: 412 BUG_ON(!PageLocked(page)); 413 vi = page->mapping->host; 414 i_size = i_size_read(vi); 415 /* Is the page fully outside i_size? (truncate in progress) */ 416 if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >> 417 PAGE_CACHE_SHIFT)) { 418 zero_user(page, 0, PAGE_CACHE_SIZE); 419 ntfs_debug("Read outside i_size - truncated?"); 420 goto done; 421 } 422 /* 423 * This can potentially happen because we clear PageUptodate() during 424 * ntfs_writepage() of MstProtected() attributes. 425 */ 426 if (PageUptodate(page)) { 427 unlock_page(page); 428 return 0; 429 } 430 ni = NTFS_I(vi); 431 /* 432 * Only $DATA attributes can be encrypted and only unnamed $DATA 433 * attributes can be compressed. Index root can have the flags set but 434 * this means to create compressed/encrypted files, not that the 435 * attribute is compressed/encrypted. Note we need to check for 436 * AT_INDEX_ALLOCATION since this is the type of both directory and 437 * index inodes. 438 */ 439 if (ni->type != AT_INDEX_ALLOCATION) { 440 /* If attribute is encrypted, deny access, just like NT4. */ 441 if (NInoEncrypted(ni)) { 442 BUG_ON(ni->type != AT_DATA); 443 err = -EACCES; 444 goto err_out; 445 } 446 /* Compressed data streams are handled in compress.c. */ 447 if (NInoNonResident(ni) && NInoCompressed(ni)) { 448 BUG_ON(ni->type != AT_DATA); 449 BUG_ON(ni->name_len); 450 return ntfs_read_compressed_block(page); 451 } 452 } 453 /* NInoNonResident() == NInoIndexAllocPresent() */ 454 if (NInoNonResident(ni)) { 455 /* Normal, non-resident data stream. */ 456 return ntfs_read_block(page); 457 } 458 /* 459 * Attribute is resident, implying it is not compressed or encrypted. 460 * This also means the attribute is smaller than an mft record and 461 * hence smaller than a page, so can simply zero out any pages with 462 * index above 0. Note the attribute can actually be marked compressed 463 * but if it is resident the actual data is not compressed so we are 464 * ok to ignore the compressed flag here. 465 */ 466 if (unlikely(page->index > 0)) { 467 zero_user(page, 0, PAGE_CACHE_SIZE); 468 goto done; 469 } 470 if (!NInoAttr(ni)) 471 base_ni = ni; 472 else 473 base_ni = ni->ext.base_ntfs_ino; 474 /* Map, pin, and lock the mft record. */ 475 mrec = map_mft_record(base_ni); 476 if (IS_ERR(mrec)) { 477 err = PTR_ERR(mrec); 478 goto err_out; 479 } 480 /* 481 * If a parallel write made the attribute non-resident, drop the mft 482 * record and retry the readpage. 483 */ 484 if (unlikely(NInoNonResident(ni))) { 485 unmap_mft_record(base_ni); 486 goto retry_readpage; 487 } 488 ctx = ntfs_attr_get_search_ctx(base_ni, mrec); 489 if (unlikely(!ctx)) { 490 err = -ENOMEM; 491 goto unm_err_out; 492 } 493 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 494 CASE_SENSITIVE, 0, NULL, 0, ctx); 495 if (unlikely(err)) 496 goto put_unm_err_out; 497 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length); 498 read_lock_irqsave(&ni->size_lock, flags); 499 if (unlikely(attr_len > ni->initialized_size)) 500 attr_len = ni->initialized_size; 501 i_size = i_size_read(vi); 502 read_unlock_irqrestore(&ni->size_lock, flags); 503 if (unlikely(attr_len > i_size)) { 504 /* Race with shrinking truncate. */ 505 attr_len = i_size; 506 } 507 addr = kmap_atomic(page, KM_USER0); 508 /* Copy the data to the page. */ 509 memcpy(addr, (u8*)ctx->attr + 510 le16_to_cpu(ctx->attr->data.resident.value_offset), 511 attr_len); 512 /* Zero the remainder of the page. */ 513 memset(addr + attr_len, 0, PAGE_CACHE_SIZE - attr_len); 514 flush_dcache_page(page); 515 kunmap_atomic(addr, KM_USER0); 516put_unm_err_out: 517 ntfs_attr_put_search_ctx(ctx); 518unm_err_out: 519 unmap_mft_record(base_ni); 520done: 521 SetPageUptodate(page); 522err_out: 523 unlock_page(page); 524 return err; 525} 526 527#ifdef NTFS_RW 528 529/** 530 * ntfs_write_block - write a @page to the backing store 531 * @page: page cache page to write out 532 * @wbc: writeback control structure 533 * 534 * This function is for writing pages belonging to non-resident, non-mst 535 * protected attributes to their backing store. 536 * 537 * For a page with buffers, map and write the dirty buffers asynchronously 538 * under page writeback. For a page without buffers, create buffers for the 539 * page, then proceed as above. 540 * 541 * If a page doesn't have buffers the page dirty state is definitive. If a page 542 * does have buffers, the page dirty state is just a hint, and the buffer dirty 543 * state is definitive. (A hint which has rules: dirty buffers against a clean 544 * page is illegal. Other combinations are legal and need to be handled. In 545 * particular a dirty page containing clean buffers for example.) 546 * 547 * Return 0 on success and -errno on error. 548 * 549 * Based on ntfs_read_block() and __block_write_full_page(). 550 */ 551static int ntfs_write_block(struct page *page, struct writeback_control *wbc) 552{ 553 VCN vcn; 554 LCN lcn; 555 s64 initialized_size; 556 loff_t i_size; 557 sector_t block, dblock, iblock; 558 struct inode *vi; 559 ntfs_inode *ni; 560 ntfs_volume *vol; 561 runlist_element *rl; 562 struct buffer_head *bh, *head; 563 unsigned long flags; 564 unsigned int blocksize, vcn_ofs; 565 int err; 566 bool need_end_writeback; 567 unsigned char blocksize_bits; 568 569 vi = page->mapping->host; 570 ni = NTFS_I(vi); 571 vol = ni->vol; 572 573 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index " 574 "0x%lx.", ni->mft_no, ni->type, page->index); 575 576 BUG_ON(!NInoNonResident(ni)); 577 BUG_ON(NInoMstProtected(ni)); 578 blocksize = vol->sb->s_blocksize; 579 blocksize_bits = vol->sb->s_blocksize_bits; 580 if (!page_has_buffers(page)) { 581 BUG_ON(!PageUptodate(page)); 582 create_empty_buffers(page, blocksize, 583 (1 << BH_Uptodate) | (1 << BH_Dirty)); 584 if (unlikely(!page_has_buffers(page))) { 585 ntfs_warning(vol->sb, "Error allocating page " 586 "buffers. Redirtying page so we try " 587 "again later."); 588 /* 589 * Put the page back on mapping->dirty_pages, but leave 590 * its buffers' dirty state as-is. 591 */ 592 redirty_page_for_writepage(wbc, page); 593 unlock_page(page); 594 return 0; 595 } 596 } 597 bh = head = page_buffers(page); 598 BUG_ON(!bh); 599 600 /* NOTE: Different naming scheme to ntfs_read_block()! */ 601 602 /* The first block in the page. */ 603 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits); 604 605 read_lock_irqsave(&ni->size_lock, flags); 606 i_size = i_size_read(vi); 607 initialized_size = ni->initialized_size; 608 read_unlock_irqrestore(&ni->size_lock, flags); 609 610 /* The first out of bounds block for the data size. */ 611 dblock = (i_size + blocksize - 1) >> blocksize_bits; 612 613 /* The last (fully or partially) initialized block. */ 614 iblock = initialized_size >> blocksize_bits; 615 616 /* 617 * Be very careful. We have no exclusion from __set_page_dirty_buffers 618 * here, and the (potentially unmapped) buffers may become dirty at 619 * any time. If a buffer becomes dirty here after we've inspected it 620 * then we just miss that fact, and the page stays dirty. 621 * 622 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers; 623 * handle that here by just cleaning them. 624 */ 625 626 /* 627 * Loop through all the buffers in the page, mapping all the dirty 628 * buffers to disk addresses and handling any aliases from the 629 * underlying block device's mapping. 630 */ 631 rl = NULL; 632 err = 0; 633 do { 634 bool is_retry = false; 635 636 if (unlikely(block >= dblock)) { 637 clear_buffer_dirty(bh); 638 set_buffer_uptodate(bh); 639 continue; 640 } 641 642 /* Clean buffers are not written out, so no need to map them. */ 643 if (!buffer_dirty(bh)) 644 continue; 645 646 /* Make sure we have enough initialized size. */ 647 if (unlikely((block >= iblock) && 648 (initialized_size < i_size))) { 649 /* 650 * If this page is fully outside initialized size, zero 651 * out all pages between the current initialized size 652 * and the current page. Just use ntfs_readpage() to do 653 * the zeroing transparently. 654 */ 655 if (block > iblock) { 656 // TODO: 657 // For each page do: 658 // - read_cache_page() 659 // Again for each page do: 660 // - wait_on_page_locked() 661 // - Check (PageUptodate(page) && 662 // !PageError(page)) 663 // Update initialized size in the attribute and 664 // in the inode. 665 // Again, for each page do: 666 // __set_page_dirty_buffers(); 667 // page_cache_release() 668 // We don't need to wait on the writes. 669 // Update iblock. 670 } 671 if (!PageUptodate(page)) { 672 // TODO: 673 // Zero any non-uptodate buffers up to i_size. 674 // Set them uptodate and dirty. 675 } 676 // TODO: 677 // Update initialized size in the attribute and in the 678 // inode (up to i_size). 679 // Update iblock. 680 // size changes to happen in one go. 681 ntfs_error(vol->sb, "Writing beyond initialized size " 682 "is not supported yet. Sorry."); 683 err = -EOPNOTSUPP; 684 break; 685 // Do NOT set_buffer_new() BUT DO clear buffer range 686 // outside write request range. 687 // set_buffer_uptodate() on complete buffers as well as 688 // set_buffer_dirty(). 689 } 690 691 /* No need to map buffers that are already mapped. */ 692 if (buffer_mapped(bh)) 693 continue; 694 695 /* Unmapped, dirty buffer. Need to map it. */ 696 bh->b_bdev = vol->sb->s_bdev; 697 698 /* Convert block into corresponding vcn and offset. */ 699 vcn = (VCN)block << blocksize_bits; 700 vcn_ofs = vcn & vol->cluster_size_mask; 701 vcn >>= vol->cluster_size_bits; 702 if (!rl) { 703lock_retry_remap: 704 down_read(&ni->runlist.lock); 705 rl = ni->runlist.rl; 706 } 707 if (likely(rl != NULL)) { 708 /* Seek to element containing target vcn. */ 709 while (rl->length && rl[1].vcn <= vcn) 710 rl++; 711 lcn = ntfs_rl_vcn_to_lcn(rl, vcn); 712 } else 713 lcn = LCN_RL_NOT_MAPPED; 714 /* Successful remap. */ 715 if (lcn >= 0) { 716 /* Setup buffer head to point to correct block. */ 717 bh->b_blocknr = ((lcn << vol->cluster_size_bits) + 718 vcn_ofs) >> blocksize_bits; 719 set_buffer_mapped(bh); 720 continue; 721 } 722 /* It is a hole, need to instantiate it. */ 723 if (lcn == LCN_HOLE) { 724 u8 *kaddr; 725 unsigned long *bpos, *bend; 726 727 /* Check if the buffer is zero. */ 728 kaddr = kmap_atomic(page, KM_USER0); 729 bpos = (unsigned long *)(kaddr + bh_offset(bh)); 730 bend = (unsigned long *)((u8*)bpos + blocksize); 731 do { 732 if (unlikely(*bpos)) 733 break; 734 } while (likely(++bpos < bend)); 735 kunmap_atomic(kaddr, KM_USER0); 736 if (bpos == bend) { 737 /* 738 * Buffer is zero and sparse, no need to write 739 * it. 740 */ 741 bh->b_blocknr = -1; 742 clear_buffer_dirty(bh); 743 continue; 744 } 745 // TODO: Instantiate the hole. 746 // clear_buffer_new(bh); 747 // unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr); 748 ntfs_error(vol->sb, "Writing into sparse regions is " 749 "not supported yet. Sorry."); 750 err = -EOPNOTSUPP; 751 break; 752 } 753 /* If first try and runlist unmapped, map and retry. */ 754 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) { 755 is_retry = true; 756 /* 757 * Attempt to map runlist, dropping lock for 758 * the duration. 759 */ 760 up_read(&ni->runlist.lock); 761 err = ntfs_map_runlist(ni, vcn); 762 if (likely(!err)) 763 goto lock_retry_remap; 764 rl = NULL; 765 } else if (!rl) 766 up_read(&ni->runlist.lock); 767 /* 768 * If buffer is outside the runlist, truncate has cut it out 769 * of the runlist. Just clean and clear the buffer and set it 770 * uptodate so it can get discarded by the VM. 771 */ 772 if (err == -ENOENT || lcn == LCN_ENOENT) { 773 bh->b_blocknr = -1; 774 clear_buffer_dirty(bh); 775 zero_user(page, bh_offset(bh), blocksize); 776 set_buffer_uptodate(bh); 777 err = 0; 778 continue; 779 } 780 /* Failed to map the buffer, even after retrying. */ 781 if (!err) 782 err = -EIO; 783 bh->b_blocknr = -1; 784 ntfs_error(vol->sb, "Failed to write to inode 0x%lx, " 785 "attribute type 0x%x, vcn 0x%llx, offset 0x%x " 786 "because its location on disk could not be " 787 "determined%s (error code %i).", ni->mft_no, 788 ni->type, (unsigned long long)vcn, 789 vcn_ofs, is_retry ? " even after " 790 "retrying" : "", err); 791 break; 792 } while (block++, (bh = bh->b_this_page) != head); 793 794 /* Release the lock if we took it. */ 795 if (rl) 796 up_read(&ni->runlist.lock); 797 798 /* For the error case, need to reset bh to the beginning. */ 799 bh = head; 800 801 /* Just an optimization, so ->readpage() is not called later. */ 802 if (unlikely(!PageUptodate(page))) { 803 int uptodate = 1; 804 do { 805 if (!buffer_uptodate(bh)) { 806 uptodate = 0; 807 bh = head; 808 break; 809 } 810 } while ((bh = bh->b_this_page) != head); 811 if (uptodate) 812 SetPageUptodate(page); 813 } 814 815 /* Setup all mapped, dirty buffers for async write i/o. */ 816 do { 817 if (buffer_mapped(bh) && buffer_dirty(bh)) { 818 lock_buffer(bh); 819 if (test_clear_buffer_dirty(bh)) { 820 BUG_ON(!buffer_uptodate(bh)); 821 mark_buffer_async_write(bh); 822 } else 823 unlock_buffer(bh); 824 } else if (unlikely(err)) { 825 /* 826 * For the error case. The buffer may have been set 827 * dirty during attachment to a dirty page. 828 */ 829 if (err != -ENOMEM) 830 clear_buffer_dirty(bh); 831 } 832 } while ((bh = bh->b_this_page) != head); 833 834 if (unlikely(err)) { 835 // TODO: Remove the -EOPNOTSUPP check later on... 836 if (unlikely(err == -EOPNOTSUPP)) 837 err = 0; 838 else if (err == -ENOMEM) { 839 ntfs_warning(vol->sb, "Error allocating memory. " 840 "Redirtying page so we try again " 841 "later."); 842 /* 843 * Put the page back on mapping->dirty_pages, but 844 * leave its buffer's dirty state as-is. 845 */ 846 redirty_page_for_writepage(wbc, page); 847 err = 0; 848 } else 849 SetPageError(page); 850 } 851 852 BUG_ON(PageWriteback(page)); 853 set_page_writeback(page); /* Keeps try_to_free_buffers() away. */ 854 855 /* Submit the prepared buffers for i/o. */ 856 need_end_writeback = true; 857 do { 858 struct buffer_head *next = bh->b_this_page; 859 if (buffer_async_write(bh)) { 860 submit_bh(WRITE, bh); 861 need_end_writeback = false; 862 } 863 bh = next; 864 } while (bh != head); 865 unlock_page(page); 866 867 /* If no i/o was started, need to end_page_writeback(). */ 868 if (unlikely(need_end_writeback)) 869 end_page_writeback(page); 870 871 ntfs_debug("Done."); 872 return err; 873} 874 875/** 876 * ntfs_write_mst_block - write a @page to the backing store 877 * @page: page cache page to write out 878 * @wbc: writeback control structure 879 * 880 * This function is for writing pages belonging to non-resident, mst protected 881 * attributes to their backing store. The only supported attributes are index 882 * allocation and $MFT/$DATA. Both directory inodes and index inodes are 883 * supported for the index allocation case. 884 * 885 * The page must remain locked for the duration of the write because we apply 886 * the mst fixups, write, and then undo the fixups, so if we were to unlock the 887 * page before undoing the fixups, any other user of the page will see the 888 * page contents as corrupt. 889 * 890 * We clear the page uptodate flag for the duration of the function to ensure 891 * exclusion for the $MFT/$DATA case against someone mapping an mft record we 892 * are about to apply the mst fixups to. 893 * 894 * Return 0 on success and -errno on error. 895 * 896 * Based on ntfs_write_block(), ntfs_mft_writepage(), and 897 * write_mft_record_nolock(). 898 */ 899static int ntfs_write_mst_block(struct page *page, 900 struct writeback_control *wbc) 901{ 902 sector_t block, dblock, rec_block; 903 struct inode *vi = page->mapping->host; 904 ntfs_inode *ni = NTFS_I(vi); 905 ntfs_volume *vol = ni->vol; 906 u8 *kaddr; 907 unsigned int rec_size = ni->itype.index.block_size; 908 ntfs_inode *locked_nis[PAGE_CACHE_SIZE / rec_size]; 909 struct buffer_head *bh, *head, *tbh, *rec_start_bh; 910 struct buffer_head *bhs[MAX_BUF_PER_PAGE]; 911 runlist_element *rl; 912 int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec, err, err2; 913 unsigned bh_size, rec_size_bits; 914 bool sync, is_mft, page_is_dirty, rec_is_dirty; 915 unsigned char bh_size_bits; 916 917 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index " 918 "0x%lx.", vi->i_ino, ni->type, page->index); 919 BUG_ON(!NInoNonResident(ni)); 920 BUG_ON(!NInoMstProtected(ni)); 921 is_mft = (S_ISREG(vi->i_mode) && !vi->i_ino); 922 /* 923 * NOTE: ntfs_write_mst_block() would be called for $MFTMirr if a page 924 * in its page cache were to be marked dirty. However this should 925 * never happen with the current driver and considering we do not 926 * handle this case here we do want to BUG(), at least for now. 927 */ 928 BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) || 929 (NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION))); 930 bh_size = vol->sb->s_blocksize; 931 bh_size_bits = vol->sb->s_blocksize_bits; 932 max_bhs = PAGE_CACHE_SIZE / bh_size; 933 BUG_ON(!max_bhs); 934 BUG_ON(max_bhs > MAX_BUF_PER_PAGE); 935 936 /* Were we called for sync purposes? */ 937 sync = (wbc->sync_mode == WB_SYNC_ALL); 938 939 /* Make sure we have mapped buffers. */ 940 bh = head = page_buffers(page); 941 BUG_ON(!bh); 942 943 rec_size_bits = ni->itype.index.block_size_bits; 944 BUG_ON(!(PAGE_CACHE_SIZE >> rec_size_bits)); 945 bhs_per_rec = rec_size >> bh_size_bits; 946 BUG_ON(!bhs_per_rec); 947 948 /* The first block in the page. */ 949 rec_block = block = (sector_t)page->index << 950 (PAGE_CACHE_SHIFT - bh_size_bits); 951 952 /* The first out of bounds block for the data size. */ 953 dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits; 954 955 rl = NULL; 956 err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0; 957 page_is_dirty = rec_is_dirty = false; 958 rec_start_bh = NULL; 959 do { 960 bool is_retry = false; 961 962 if (likely(block < rec_block)) { 963 if (unlikely(block >= dblock)) { 964 clear_buffer_dirty(bh); 965 set_buffer_uptodate(bh); 966 continue; 967 } 968 /* 969 * This block is not the first one in the record. We 970 * ignore the buffer's dirty state because we could 971 * have raced with a parallel mark_ntfs_record_dirty(). 972 */ 973 if (!rec_is_dirty) 974 continue; 975 if (unlikely(err2)) { 976 if (err2 != -ENOMEM) 977 clear_buffer_dirty(bh); 978 continue; 979 } 980 } else /* if (block == rec_block) */ { 981 BUG_ON(block > rec_block); 982 /* This block is the first one in the record. */ 983 rec_block += bhs_per_rec; 984 err2 = 0; 985 if (unlikely(block >= dblock)) { 986 clear_buffer_dirty(bh); 987 continue; 988 } 989 if (!buffer_dirty(bh)) { 990 /* Clean records are not written out. */ 991 rec_is_dirty = false; 992 continue; 993 } 994 rec_is_dirty = true; 995 rec_start_bh = bh; 996 } 997 /* Need to map the buffer if it is not mapped already. */ 998 if (unlikely(!buffer_mapped(bh))) { 999 VCN vcn; 1000 LCN lcn; 1001 unsigned int vcn_ofs; 1002 1003 bh->b_bdev = vol->sb->s_bdev; 1004 /* Obtain the vcn and offset of the current block. */ 1005 vcn = (VCN)block << bh_size_bits; 1006 vcn_ofs = vcn & vol->cluster_size_mask; 1007 vcn >>= vol->cluster_size_bits; 1008 if (!rl) { 1009lock_retry_remap: 1010 down_read(&ni->runlist.lock); 1011 rl = ni->runlist.rl; 1012 } 1013 if (likely(rl != NULL)) { 1014 /* Seek to element containing target vcn. */ 1015 while (rl->length && rl[1].vcn <= vcn) 1016 rl++; 1017 lcn = ntfs_rl_vcn_to_lcn(rl, vcn); 1018 } else 1019 lcn = LCN_RL_NOT_MAPPED; 1020 /* Successful remap. */ 1021 if (likely(lcn >= 0)) { 1022 /* Setup buffer head to correct block. */ 1023 bh->b_blocknr = ((lcn << 1024 vol->cluster_size_bits) + 1025 vcn_ofs) >> bh_size_bits; 1026 set_buffer_mapped(bh); 1027 } else { 1028 /* 1029 * Remap failed. Retry to map the runlist once 1030 * unless we are working on $MFT which always 1031 * has the whole of its runlist in memory. 1032 */ 1033 if (!is_mft && !is_retry && 1034 lcn == LCN_RL_NOT_MAPPED) { 1035 is_retry = true; 1036 /* 1037 * Attempt to map runlist, dropping 1038 * lock for the duration. 1039 */ 1040 up_read(&ni->runlist.lock); 1041 err2 = ntfs_map_runlist(ni, vcn); 1042 if (likely(!err2)) 1043 goto lock_retry_remap; 1044 if (err2 == -ENOMEM) 1045 page_is_dirty = true; 1046 lcn = err2; 1047 } else { 1048 err2 = -EIO; 1049 if (!rl) 1050 up_read(&ni->runlist.lock); 1051 } 1052 /* Hard error. Abort writing this record. */ 1053 if (!err || err == -ENOMEM) 1054 err = err2; 1055 bh->b_blocknr = -1; 1056 ntfs_error(vol->sb, "Cannot write ntfs record " 1057 "0x%llx (inode 0x%lx, " 1058 "attribute type 0x%x) because " 1059 "its location on disk could " 1060 "not be determined (error " 1061 "code %lli).", 1062 (long long)block << 1063 bh_size_bits >> 1064 vol->mft_record_size_bits, 1065 ni->mft_no, ni->type, 1066 (long long)lcn); 1067 /* 1068 * If this is not the first buffer, remove the 1069 * buffers in this record from the list of 1070 * buffers to write and clear their dirty bit 1071 * if not error -ENOMEM. 1072 */ 1073 if (rec_start_bh != bh) { 1074 while (bhs[--nr_bhs] != rec_start_bh) 1075 ; 1076 if (err2 != -ENOMEM) { 1077 do { 1078 clear_buffer_dirty( 1079 rec_start_bh); 1080 } while ((rec_start_bh = 1081 rec_start_bh-> 1082 b_this_page) != 1083 bh); 1084 } 1085 } 1086 continue; 1087 } 1088 } 1089 BUG_ON(!buffer_uptodate(bh)); 1090 BUG_ON(nr_bhs >= max_bhs); 1091 bhs[nr_bhs++] = bh; 1092 } while (block++, (bh = bh->b_this_page) != head); 1093 if (unlikely(rl)) 1094 up_read(&ni->runlist.lock); 1095 /* If there were no dirty buffers, we are done. */ 1096 if (!nr_bhs) 1097 goto done; 1098 /* Map the page so we can access its contents. */ 1099 kaddr = kmap(page); 1100 /* Clear the page uptodate flag whilst the mst fixups are applied. */ 1101 BUG_ON(!PageUptodate(page)); 1102 ClearPageUptodate(page); 1103 for (i = 0; i < nr_bhs; i++) { 1104 unsigned int ofs; 1105 1106 /* Skip buffers which are not at the beginning of records. */ 1107 if (i % bhs_per_rec) 1108 continue; 1109 tbh = bhs[i]; 1110 ofs = bh_offset(tbh); 1111 if (is_mft) { 1112 ntfs_inode *tni; 1113 unsigned long mft_no; 1114 1115 /* Get the mft record number. */ 1116 mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs) 1117 >> rec_size_bits; 1118 /* Check whether to write this mft record. */ 1119 tni = NULL; 1120 if (!ntfs_may_write_mft_record(vol, mft_no, 1121 (MFT_RECORD*)(kaddr + ofs), &tni)) { 1122 /* 1123 * The record should not be written. This 1124 * means we need to redirty the page before 1125 * returning. 1126 */ 1127 page_is_dirty = true; 1128 /* 1129 * Remove the buffers in this mft record from 1130 * the list of buffers to write. 1131 */ 1132 do { 1133 bhs[i] = NULL; 1134 } while (++i % bhs_per_rec); 1135 continue; 1136 } 1137 /* 1138 * The record should be written. If a locked ntfs 1139 * inode was returned, add it to the array of locked 1140 * ntfs inodes. 1141 */ 1142 if (tni) 1143 locked_nis[nr_locked_nis++] = tni; 1144 } 1145 /* Apply the mst protection fixups. */ 1146 err2 = pre_write_mst_fixup((NTFS_RECORD*)(kaddr + ofs), 1147 rec_size); 1148 if (unlikely(err2)) { 1149 if (!err || err == -ENOMEM) 1150 err = -EIO; 1151 ntfs_error(vol->sb, "Failed to apply mst fixups " 1152 "(inode 0x%lx, attribute type 0x%x, " 1153 "page index 0x%lx, page offset 0x%x)!" 1154 " Unmount and run chkdsk.", vi->i_ino, 1155 ni->type, page->index, ofs); 1156 /* 1157 * Mark all the buffers in this record clean as we do 1158 * not want to write corrupt data to disk. 1159 */ 1160 do { 1161 clear_buffer_dirty(bhs[i]); 1162 bhs[i] = NULL; 1163 } while (++i % bhs_per_rec); 1164 continue; 1165 } 1166 nr_recs++; 1167 } 1168 /* If no records are to be written out, we are done. */ 1169 if (!nr_recs) 1170 goto unm_done; 1171 flush_dcache_page(page); 1172 /* Lock buffers and start synchronous write i/o on them. */ 1173 for (i = 0; i < nr_bhs; i++) { 1174 tbh = bhs[i]; 1175 if (!tbh) 1176 continue; 1177 if (!trylock_buffer(tbh)) 1178 BUG(); 1179 /* The buffer dirty state is now irrelevant, just clean it. */ 1180 clear_buffer_dirty(tbh); 1181 BUG_ON(!buffer_uptodate(tbh)); 1182 BUG_ON(!buffer_mapped(tbh)); 1183 get_bh(tbh); 1184 tbh->b_end_io = end_buffer_write_sync; 1185 submit_bh(WRITE, tbh); 1186 } 1187 /* Synchronize the mft mirror now if not @sync. */ 1188 if (is_mft && !sync) 1189 goto do_mirror; 1190do_wait: 1191 /* Wait on i/o completion of buffers. */ 1192 for (i = 0; i < nr_bhs; i++) { 1193 tbh = bhs[i]; 1194 if (!tbh) 1195 continue; 1196 wait_on_buffer(tbh); 1197 if (unlikely(!buffer_uptodate(tbh))) { 1198 ntfs_error(vol->sb, "I/O error while writing ntfs " 1199 "record buffer (inode 0x%lx, " 1200 "attribute type 0x%x, page index " 1201 "0x%lx, page offset 0x%lx)! Unmount " 1202 "and run chkdsk.", vi->i_ino, ni->type, 1203 page->index, bh_offset(tbh)); 1204 if (!err || err == -ENOMEM) 1205 err = -EIO; 1206 /* 1207 * Set the buffer uptodate so the page and buffer 1208 * states do not become out of sync. 1209 */ 1210 set_buffer_uptodate(tbh); 1211 } 1212 } 1213 /* If @sync, now synchronize the mft mirror. */ 1214 if (is_mft && sync) { 1215do_mirror: 1216 for (i = 0; i < nr_bhs; i++) { 1217 unsigned long mft_no; 1218 unsigned int ofs; 1219 1220 /* 1221 * Skip buffers which are not at the beginning of 1222 * records. 1223 */ 1224 if (i % bhs_per_rec) 1225 continue; 1226 tbh = bhs[i]; 1227 /* Skip removed buffers (and hence records). */ 1228 if (!tbh) 1229 continue; 1230 ofs = bh_offset(tbh); 1231 /* Get the mft record number. */ 1232 mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs) 1233 >> rec_size_bits; 1234 if (mft_no < vol->mftmirr_size) 1235 ntfs_sync_mft_mirror(vol, mft_no, 1236 (MFT_RECORD*)(kaddr + ofs), 1237 sync); 1238 } 1239 if (!sync) 1240 goto do_wait; 1241 } 1242 /* Remove the mst protection fixups again. */ 1243 for (i = 0; i < nr_bhs; i++) { 1244 if (!(i % bhs_per_rec)) { 1245 tbh = bhs[i]; 1246 if (!tbh) 1247 continue; 1248 post_write_mst_fixup((NTFS_RECORD*)(kaddr + 1249 bh_offset(tbh))); 1250 } 1251 } 1252 flush_dcache_page(page); 1253unm_done: 1254 /* Unlock any locked inodes. */ 1255 while (nr_locked_nis-- > 0) { 1256 ntfs_inode *tni, *base_tni; 1257 1258 tni = locked_nis[nr_locked_nis]; 1259 /* Get the base inode. */ 1260 mutex_lock(&tni->extent_lock); 1261 if (tni->nr_extents >= 0) 1262 base_tni = tni; 1263 else { 1264 base_tni = tni->ext.base_ntfs_ino; 1265 BUG_ON(!base_tni); 1266 } 1267 mutex_unlock(&tni->extent_lock); 1268 ntfs_debug("Unlocking %s inode 0x%lx.", 1269 tni == base_tni ? "base" : "extent", 1270 tni->mft_no); 1271 mutex_unlock(&tni->mrec_lock); 1272 atomic_dec(&tni->count); 1273 iput(VFS_I(base_tni)); 1274 } 1275 SetPageUptodate(page); 1276 kunmap(page); 1277done: 1278 if (unlikely(err && err != -ENOMEM)) { 1279 /* 1280 * Set page error if there is only one ntfs record in the page. 1281 * Otherwise we would loose per-record granularity. 1282 */ 1283 if (ni->itype.index.block_size == PAGE_CACHE_SIZE) 1284 SetPageError(page); 1285 NVolSetErrors(vol); 1286 } 1287 if (page_is_dirty) { 1288 ntfs_debug("Page still contains one or more dirty ntfs " 1289 "records. Redirtying the page starting at " 1290 "record 0x%lx.", page->index << 1291 (PAGE_CACHE_SHIFT - rec_size_bits)); 1292 redirty_page_for_writepage(wbc, page); 1293 unlock_page(page); 1294 } else { 1295 /* 1296 * Keep the VM happy. This must be done otherwise the 1297 * radix-tree tag PAGECACHE_TAG_DIRTY remains set even though 1298 * the page is clean. 1299 */ 1300 BUG_ON(PageWriteback(page)); 1301 set_page_writeback(page); 1302 unlock_page(page); 1303 end_page_writeback(page); 1304 } 1305 if (likely(!err)) 1306 ntfs_debug("Done."); 1307 return err; 1308} 1309 1310/** 1311 * ntfs_writepage - write a @page to the backing store 1312 * @page: page cache page to write out 1313 * @wbc: writeback control structure 1314 * 1315 * This is called from the VM when it wants to have a dirty ntfs page cache 1316 * page cleaned. The VM has already locked the page and marked it clean. 1317 * 1318 * For non-resident attributes, ntfs_writepage() writes the @page by calling 1319 * the ntfs version of the generic block_write_full_page() function, 1320 * ntfs_write_block(), which in turn if necessary creates and writes the 1321 * buffers associated with the page asynchronously. 1322 * 1323 * For resident attributes, OTOH, ntfs_writepage() writes the @page by copying 1324 * the data to the mft record (which at this stage is most likely in memory). 1325 * The mft record is then marked dirty and written out asynchronously via the 1326 * vfs inode dirty code path for the inode the mft record belongs to or via the 1327 * vm page dirty code path for the page the mft record is in. 1328 * 1329 * Based on ntfs_readpage() and fs/buffer.c::block_write_full_page(). 1330 * 1331 * Return 0 on success and -errno on error. 1332 */ 1333static int ntfs_writepage(struct page *page, struct writeback_control *wbc) 1334{ 1335 loff_t i_size; 1336 struct inode *vi = page->mapping->host; 1337 ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi); 1338 char *addr; 1339 ntfs_attr_search_ctx *ctx = NULL; 1340 MFT_RECORD *m = NULL; 1341 u32 attr_len; 1342 int err; 1343 1344retry_writepage: 1345 BUG_ON(!PageLocked(page)); 1346 i_size = i_size_read(vi); 1347 /* Is the page fully outside i_size? (truncate in progress) */ 1348 if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >> 1349 PAGE_CACHE_SHIFT)) { 1350 /* 1351 * The page may have dirty, unmapped buffers. Make them 1352 * freeable here, so the page does not leak. 1353 */ 1354 block_invalidatepage(page, 0); 1355 unlock_page(page); 1356 ntfs_debug("Write outside i_size - truncated?"); 1357 return 0; 1358 } 1359 /* 1360 * Only $DATA attributes can be encrypted and only unnamed $DATA 1361 * attributes can be compressed. Index root can have the flags set but 1362 * this means to create compressed/encrypted files, not that the 1363 * attribute is compressed/encrypted. Note we need to check for 1364 * AT_INDEX_ALLOCATION since this is the type of both directory and 1365 * index inodes. 1366 */ 1367 if (ni->type != AT_INDEX_ALLOCATION) { 1368 /* If file is encrypted, deny access, just like NT4. */ 1369 if (NInoEncrypted(ni)) { 1370 unlock_page(page); 1371 BUG_ON(ni->type != AT_DATA); 1372 ntfs_debug("Denying write access to encrypted file."); 1373 return -EACCES; 1374 } 1375 /* Compressed data streams are handled in compress.c. */ 1376 if (NInoNonResident(ni) && NInoCompressed(ni)) { 1377 BUG_ON(ni->type != AT_DATA); 1378 BUG_ON(ni->name_len); 1379 // TODO: Implement and replace this with 1380 // return ntfs_write_compressed_block(page); 1381 unlock_page(page); 1382 ntfs_error(vi->i_sb, "Writing to compressed files is " 1383 "not supported yet. Sorry."); 1384 return -EOPNOTSUPP; 1385 } 1386 // TODO: Implement and remove this check. 1387 if (NInoNonResident(ni) && NInoSparse(ni)) { 1388 unlock_page(page); 1389 ntfs_error(vi->i_sb, "Writing to sparse files is not " 1390 "supported yet. Sorry."); 1391 return -EOPNOTSUPP; 1392 } 1393 } 1394 /* NInoNonResident() == NInoIndexAllocPresent() */ 1395 if (NInoNonResident(ni)) { 1396 /* We have to zero every time due to mmap-at-end-of-file. */ 1397 if (page->index >= (i_size >> PAGE_CACHE_SHIFT)) { 1398 /* The page straddles i_size. */ 1399 unsigned int ofs = i_size & ~PAGE_CACHE_MASK; 1400 zero_user_segment(page, ofs, PAGE_CACHE_SIZE); 1401 } 1402 /* Handle mst protected attributes. */ 1403 if (NInoMstProtected(ni)) 1404 return ntfs_write_mst_block(page, wbc); 1405 /* Normal, non-resident data stream. */ 1406 return ntfs_write_block(page, wbc); 1407 } 1408 /* 1409 * Attribute is resident, implying it is not compressed, encrypted, or 1410 * mst protected. This also means the attribute is smaller than an mft 1411 * record and hence smaller than a page, so can simply return error on 1412 * any pages with index above 0. Note the attribute can actually be 1413 * marked compressed but if it is resident the actual data is not 1414 * compressed so we are ok to ignore the compressed flag here. 1415 */ 1416 BUG_ON(page_has_buffers(page)); 1417 BUG_ON(!PageUptodate(page)); 1418 if (unlikely(page->index > 0)) { 1419 ntfs_error(vi->i_sb, "BUG()! page->index (0x%lx) > 0. " 1420 "Aborting write.", page->index); 1421 BUG_ON(PageWriteback(page)); 1422 set_page_writeback(page); 1423 unlock_page(page); 1424 end_page_writeback(page); 1425 return -EIO; 1426 } 1427 if (!NInoAttr(ni)) 1428 base_ni = ni; 1429 else 1430 base_ni = ni->ext.base_ntfs_ino; 1431 /* Map, pin, and lock the mft record. */ 1432 m = map_mft_record(base_ni); 1433 if (IS_ERR(m)) { 1434 err = PTR_ERR(m); 1435 m = NULL; 1436 ctx = NULL; 1437 goto err_out; 1438 } 1439 /* 1440 * If a parallel write made the attribute non-resident, drop the mft 1441 * record and retry the writepage. 1442 */ 1443 if (unlikely(NInoNonResident(ni))) { 1444 unmap_mft_record(base_ni); 1445 goto retry_writepage; 1446 } 1447 ctx = ntfs_attr_get_search_ctx(base_ni, m); 1448 if (unlikely(!ctx)) { 1449 err = -ENOMEM; 1450 goto err_out; 1451 } 1452 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 1453 CASE_SENSITIVE, 0, NULL, 0, ctx); 1454 if (unlikely(err)) 1455 goto err_out; 1456 /* 1457 * Keep the VM happy. This must be done otherwise the radix-tree tag 1458 * PAGECACHE_TAG_DIRTY remains set even though the page is clean. 1459 */ 1460 BUG_ON(PageWriteback(page)); 1461 set_page_writeback(page); 1462 unlock_page(page); 1463 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length); 1464 i_size = i_size_read(vi); 1465 if (unlikely(attr_len > i_size)) { 1466 /* Race with shrinking truncate or a failed truncate. */ 1467 attr_len = i_size; 1468 /* 1469 * If the truncate failed, fix it up now. If a concurrent 1470 * truncate, we do its job, so it does not have to do anything. 1471 */ 1472 err = ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr, 1473 attr_len); 1474 /* Shrinking cannot fail. */ 1475 BUG_ON(err); 1476 } 1477 addr = kmap_atomic(page, KM_USER0); 1478 /* Copy the data from the page to the mft record. */ 1479 memcpy((u8*)ctx->attr + 1480 le16_to_cpu(ctx->attr->data.resident.value_offset), 1481 addr, attr_len); 1482 /* Zero out of bounds area in the page cache page. */ 1483 memset(addr + attr_len, 0, PAGE_CACHE_SIZE - attr_len); 1484 kunmap_atomic(addr, KM_USER0); 1485 flush_dcache_page(page); 1486 flush_dcache_mft_record_page(ctx->ntfs_ino); 1487 /* We are done with the page. */ 1488 end_page_writeback(page); 1489 /* Finally, mark the mft record dirty, so it gets written back. */ 1490 mark_mft_record_dirty(ctx->ntfs_ino); 1491 ntfs_attr_put_search_ctx(ctx); 1492 unmap_mft_record(base_ni); 1493 return 0; 1494err_out: 1495 if (err == -ENOMEM) { 1496 ntfs_warning(vi->i_sb, "Error allocating memory. Redirtying " 1497 "page so we try again later."); 1498 /* 1499 * Put the page back on mapping->dirty_pages, but leave its 1500 * buffers' dirty state as-is. 1501 */ 1502 redirty_page_for_writepage(wbc, page); 1503 err = 0; 1504 } else { 1505 ntfs_error(vi->i_sb, "Resident attribute write failed with " 1506 "error %i.", err); 1507 SetPageError(page); 1508 NVolSetErrors(ni->vol); 1509 } 1510 unlock_page(page); 1511 if (ctx) 1512 ntfs_attr_put_search_ctx(ctx); 1513 if (m) 1514 unmap_mft_record(base_ni); 1515 return err; 1516} 1517 1518#endif /* NTFS_RW */ 1519 1520/** 1521 * ntfs_aops - general address space operations for inodes and attributes 1522 */ 1523const struct address_space_operations ntfs_aops = { 1524 .readpage = ntfs_readpage, /* Fill page with data. */ 1525 .sync_page = block_sync_page, /* Currently, just unplugs the 1526 disk request queue. */ 1527#ifdef NTFS_RW 1528 .writepage = ntfs_writepage, /* Write dirty page to disk. */ 1529#endif /* NTFS_RW */ 1530 .migratepage = buffer_migrate_page, /* Move a page cache page from 1531 one physical page to an 1532 other. */ 1533 .error_remove_page = generic_error_remove_page, 1534}; 1535 1536/** 1537 * ntfs_mst_aops - general address space operations for mst protecteed inodes 1538 * and attributes 1539 */ 1540const struct address_space_operations ntfs_mst_aops = { 1541 .readpage = ntfs_readpage, /* Fill page with data. */ 1542 .sync_page = block_sync_page, /* Currently, just unplugs the 1543 disk request queue. */ 1544#ifdef NTFS_RW 1545 .writepage = ntfs_writepage, /* Write dirty page to disk. */ 1546 .set_page_dirty = __set_page_dirty_nobuffers, /* Set the page dirty 1547 without touching the buffers 1548 belonging to the page. */ 1549#endif /* NTFS_RW */ 1550 .migratepage = buffer_migrate_page, /* Move a page cache page from 1551 one physical page to an 1552 other. */ 1553 .error_remove_page = generic_error_remove_page, 1554}; 1555 1556#ifdef NTFS_RW 1557 1558/** 1559 * mark_ntfs_record_dirty - mark an ntfs record dirty 1560 * @page: page containing the ntfs record to mark dirty 1561 * @ofs: byte offset within @page at which the ntfs record begins 1562 * 1563 * Set the buffers and the page in which the ntfs record is located dirty. 1564 * 1565 * The latter also marks the vfs inode the ntfs record belongs to dirty 1566 * (I_DIRTY_PAGES only). 1567 * 1568 * If the page does not have buffers, we create them and set them uptodate. 1569 * The page may not be locked which is why we need to handle the buffers under 1570 * the mapping->private_lock. Once the buffers are marked dirty we no longer 1571 * need the lock since try_to_free_buffers() does not free dirty buffers. 1572 */ 1573void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) { 1574 struct address_space *mapping = page->mapping; 1575 ntfs_inode *ni = NTFS_I(mapping->host); 1576 struct buffer_head *bh, *head, *buffers_to_free = NULL; 1577 unsigned int end, bh_size, bh_ofs; 1578 1579 BUG_ON(!PageUptodate(page)); 1580 end = ofs + ni->itype.index.block_size; 1581 bh_size = VFS_I(ni)->i_sb->s_blocksize; 1582 spin_lock(&mapping->private_lock); 1583 if (unlikely(!page_has_buffers(page))) { 1584 spin_unlock(&mapping->private_lock); 1585 bh = head = alloc_page_buffers(page, bh_size, 1); 1586 spin_lock(&mapping->private_lock); 1587 if (likely(!page_has_buffers(page))) { 1588 struct buffer_head *tail; 1589 1590 do { 1591 set_buffer_uptodate(bh); 1592 tail = bh; 1593 bh = bh->b_this_page; 1594 } while (bh); 1595 tail->b_this_page = head; 1596 attach_page_buffers(page, head); 1597 } else 1598 buffers_to_free = bh; 1599 } 1600 bh = head = page_buffers(page); 1601 BUG_ON(!bh); 1602 do { 1603 bh_ofs = bh_offset(bh); 1604 if (bh_ofs + bh_size <= ofs) 1605 continue; 1606 if (unlikely(bh_ofs >= end)) 1607 break; 1608 set_buffer_dirty(bh); 1609 } while ((bh = bh->b_this_page) != head); 1610 spin_unlock(&mapping->private_lock); 1611 __set_page_dirty_nobuffers(page); 1612 if (unlikely(buffers_to_free)) { 1613 do { 1614 bh = buffers_to_free->b_this_page; 1615 free_buffer_head(buffers_to_free); 1616 buffers_to_free = bh; 1617 } while (buffers_to_free); 1618 } 1619} 1620 1621#endif /* NTFS_RW */ 1622