2 * aops.c - NTFS kernel address space operations and page cache handling.
3 * Part of the Linux-NTFS project.
5 * Copyright (c) 2001-2005 Anton Altaparmakov
6 * Copyright (c) 2002 Richard Russon
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.
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.
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
24 #include <linux/errno.h>
26 #include <linux/pagemap.h>
27 #include <linux/swap.h>
28 #include <linux/buffer_head.h>
29 #include <linux/writeback.h>
30 #include <linux/bit_spinlock.h>
42 * ntfs_end_buffer_async_read - async io completion for reading attributes
43 * @bh: buffer head on which io is completed
44 * @uptodate: whether @bh is now uptodate or not
46 * Asynchronous I/O completion handler for reading pages belonging to the
47 * attribute address space of an inode. The inodes can either be files or
48 * directories or they can be fake inodes describing some attribute.
50 * If NInoMstProtected(), perform the post read mst fixups when all IO on the
51 * page has been completed and mark the page uptodate or set the error bit on
52 * the page. To determine the size of the records that need fixing up, we
53 * cheat a little bit by setting the index_block_size in ntfs_inode to the ntfs
54 * record size, and index_block_size_bits, to the log(base 2) of the ntfs
57 static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
60 struct buffer_head *first, *tmp;
64 int page_uptodate = 1;
67 vi = page->mapping->host;
70 if (likely(uptodate)) {
72 s64 file_ofs, init_size;
74 set_buffer_uptodate(bh);
76 file_ofs = ((s64)page->index << PAGE_CACHE_SHIFT) +
78 read_lock_irqsave(&ni->size_lock, flags);
79 init_size = ni->initialized_size;
80 i_size = i_size_read(vi);
81 read_unlock_irqrestore(&ni->size_lock, flags);
82 if (unlikely(init_size > i_size)) {
83 /* Race with shrinking truncate. */
86 /* Check for the current buffer head overflowing. */
87 if (unlikely(file_ofs + bh->b_size > init_size)) {
92 if (file_ofs < init_size)
93 ofs = init_size - file_ofs;
94 kaddr = kmap_atomic(page, KM_BIO_SRC_IRQ);
95 memset(kaddr + bh_offset(bh) + ofs, 0,
97 kunmap_atomic(kaddr, KM_BIO_SRC_IRQ);
98 flush_dcache_page(page);
101 clear_buffer_uptodate(bh);
103 ntfs_error(ni->vol->sb, "Buffer I/O error, logical block "
104 "0x%llx.", (unsigned long long)bh->b_blocknr);
106 first = page_buffers(page);
107 local_irq_save(flags);
108 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
109 clear_buffer_async_read(bh);
113 if (!buffer_uptodate(tmp))
115 if (buffer_async_read(tmp)) {
116 if (likely(buffer_locked(tmp)))
118 /* Async buffers must be locked. */
121 tmp = tmp->b_this_page;
123 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
124 local_irq_restore(flags);
126 * If none of the buffers had errors then we can set the page uptodate,
127 * but we first have to perform the post read mst fixups, if the
128 * attribute is mst protected, i.e. if NInoMstProteced(ni) is true.
129 * Note we ignore fixup errors as those are detected when
130 * map_mft_record() is called which gives us per record granularity
131 * rather than per page granularity.
133 if (!NInoMstProtected(ni)) {
134 if (likely(page_uptodate && !PageError(page)))
135 SetPageUptodate(page);
138 unsigned int i, recs;
141 rec_size = ni->itype.index.block_size;
142 recs = PAGE_CACHE_SIZE / rec_size;
143 /* Should have been verified before we got here... */
145 kaddr = kmap_atomic(page, KM_BIO_SRC_IRQ);
146 for (i = 0; i < recs; i++)
147 post_read_mst_fixup((NTFS_RECORD*)(kaddr +
148 i * rec_size), rec_size);
149 kunmap_atomic(kaddr, KM_BIO_SRC_IRQ);
150 flush_dcache_page(page);
151 if (likely(page_uptodate && !PageError(page)))
152 SetPageUptodate(page);
157 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
158 local_irq_restore(flags);
163 * ntfs_read_block - fill a @page of an address space with data
164 * @page: page cache page to fill with data
166 * Fill the page @page of the address space belonging to the @page->host inode.
167 * We read each buffer asynchronously and when all buffers are read in, our io
168 * completion handler ntfs_end_buffer_read_async(), if required, automatically
169 * applies the mst fixups to the page before finally marking it uptodate and
172 * We only enforce allocated_size limit because i_size is checked for in
173 * generic_file_read().
175 * Return 0 on success and -errno on error.
177 * Contains an adapted version of fs/buffer.c::block_read_full_page().
179 static int ntfs_read_block(struct page *page)
189 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
190 sector_t iblock, lblock, zblock;
192 unsigned int blocksize, vcn_ofs;
194 unsigned char blocksize_bits;
196 vi = page->mapping->host;
200 /* $MFT/$DATA must have its complete runlist in memory at all times. */
201 BUG_ON(!ni->runlist.rl && !ni->mft_no && !NInoAttr(ni));
203 blocksize_bits = VFS_I(ni)->i_blkbits;
204 blocksize = 1 << blocksize_bits;
206 if (!page_has_buffers(page)) {
207 create_empty_buffers(page, blocksize, 0);
208 if (unlikely(!page_has_buffers(page))) {
213 bh = head = page_buffers(page);
217 * We may be racing with truncate. To avoid some of the problems we
218 * now take a snapshot of the various sizes and use those for the whole
219 * of the function. In case of an extending truncate it just means we
220 * may leave some buffers unmapped which are now allocated. This is
221 * not a problem since these buffers will just get mapped when a write
222 * occurs. In case of a shrinking truncate, we will detect this later
223 * on due to the runlist being incomplete and if the page is being
224 * fully truncated, truncate will throw it away as soon as we unlock
225 * it so no need to worry what we do with it.
227 iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
228 read_lock_irqsave(&ni->size_lock, flags);
229 lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits;
230 init_size = ni->initialized_size;
231 i_size = i_size_read(vi);
232 read_unlock_irqrestore(&ni->size_lock, flags);
233 if (unlikely(init_size > i_size)) {
234 /* Race with shrinking truncate. */
237 zblock = (init_size + blocksize - 1) >> blocksize_bits;
239 /* Loop through all the buffers in the page. */
246 if (unlikely(buffer_uptodate(bh)))
248 if (unlikely(buffer_mapped(bh))) {
253 bh->b_bdev = vol->sb->s_bdev;
254 /* Is the block within the allowed limits? */
255 if (iblock < lblock) {
256 BOOL is_retry = FALSE;
258 /* Convert iblock into corresponding vcn and offset. */
259 vcn = (VCN)iblock << blocksize_bits >>
260 vol->cluster_size_bits;
261 vcn_ofs = ((VCN)iblock << blocksize_bits) &
262 vol->cluster_size_mask;
265 down_read(&ni->runlist.lock);
268 if (likely(rl != NULL)) {
269 /* Seek to element containing target vcn. */
270 while (rl->length && rl[1].vcn <= vcn)
272 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
274 lcn = LCN_RL_NOT_MAPPED;
275 /* Successful remap. */
277 /* Setup buffer head to correct block. */
278 bh->b_blocknr = ((lcn << vol->cluster_size_bits)
279 + vcn_ofs) >> blocksize_bits;
280 set_buffer_mapped(bh);
281 /* Only read initialized data blocks. */
282 if (iblock < zblock) {
286 /* Fully non-initialized data block, zero it. */
289 /* It is a hole, need to zero it. */
292 /* If first try and runlist unmapped, map and retry. */
293 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
296 * Attempt to map runlist, dropping lock for
299 up_read(&ni->runlist.lock);
300 err = ntfs_map_runlist(ni, vcn);
302 goto lock_retry_remap;
305 up_read(&ni->runlist.lock);
307 * If buffer is outside the runlist, treat it as a
308 * hole. This can happen due to concurrent truncate
311 if (err == -ENOENT || lcn == LCN_ENOENT) {
315 /* Hard error, zero out region. */
320 ntfs_error(vol->sb, "Failed to read from inode 0x%lx, "
321 "attribute type 0x%x, vcn 0x%llx, "
322 "offset 0x%x because its location on "
323 "disk could not be determined%s "
324 "(error code %i).", ni->mft_no,
325 ni->type, (unsigned long long)vcn,
326 vcn_ofs, is_retry ? " even after "
327 "retrying" : "", err);
330 * Either iblock was outside lblock limits or
331 * ntfs_rl_vcn_to_lcn() returned error. Just zero that portion
332 * of the page and set the buffer uptodate.
335 bh->b_blocknr = -1UL;
336 clear_buffer_mapped(bh);
338 kaddr = kmap_atomic(page, KM_USER0);
339 memset(kaddr + i * blocksize, 0, blocksize);
340 kunmap_atomic(kaddr, KM_USER0);
341 flush_dcache_page(page);
343 set_buffer_uptodate(bh);
344 } while (i++, iblock++, (bh = bh->b_this_page) != head);
346 /* Release the lock if we took it. */
348 up_read(&ni->runlist.lock);
350 /* Check we have at least one buffer ready for i/o. */
352 struct buffer_head *tbh;
354 /* Lock the buffers. */
355 for (i = 0; i < nr; i++) {
358 tbh->b_end_io = ntfs_end_buffer_async_read;
359 set_buffer_async_read(tbh);
361 /* Finally, start i/o on the buffers. */
362 for (i = 0; i < nr; i++) {
364 if (likely(!buffer_uptodate(tbh)))
365 submit_bh(READ, tbh);
367 ntfs_end_buffer_async_read(tbh, 1);
371 /* No i/o was scheduled on any of the buffers. */
372 if (likely(!PageError(page)))
373 SetPageUptodate(page);
374 else /* Signal synchronous i/o error. */
381 * ntfs_readpage - fill a @page of a @file with data from the device
382 * @file: open file to which the page @page belongs or NULL
383 * @page: page cache page to fill with data
385 * For non-resident attributes, ntfs_readpage() fills the @page of the open
386 * file @file by calling the ntfs version of the generic block_read_full_page()
387 * function, ntfs_read_block(), which in turn creates and reads in the buffers
388 * associated with the page asynchronously.
390 * For resident attributes, OTOH, ntfs_readpage() fills @page by copying the
391 * data from the mft record (which at this stage is most likely in memory) and
392 * fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as
393 * even if the mft record is not cached at this point in time, we need to wait
394 * for it to be read in before we can do the copy.
396 * Return 0 on success and -errno on error.
398 static int ntfs_readpage(struct file *file, struct page *page)
402 ntfs_inode *ni, *base_ni;
404 ntfs_attr_search_ctx *ctx;
411 BUG_ON(!PageLocked(page));
413 * This can potentially happen because we clear PageUptodate() during
414 * ntfs_writepage() of MstProtected() attributes.
416 if (PageUptodate(page)) {
420 vi = page->mapping->host;
423 * Only $DATA attributes can be encrypted and only unnamed $DATA
424 * attributes can be compressed. Index root can have the flags set but
425 * this means to create compressed/encrypted files, not that the
426 * attribute is compressed/encrypted. Note we need to check for
427 * AT_INDEX_ALLOCATION since this is the type of both directory and
430 if (ni->type != AT_INDEX_ALLOCATION) {
431 /* If attribute is encrypted, deny access, just like NT4. */
432 if (NInoEncrypted(ni)) {
433 BUG_ON(ni->type != AT_DATA);
437 /* Compressed data streams are handled in compress.c. */
438 if (NInoNonResident(ni) && NInoCompressed(ni)) {
439 BUG_ON(ni->type != AT_DATA);
440 BUG_ON(ni->name_len);
441 return ntfs_read_compressed_block(page);
444 /* NInoNonResident() == NInoIndexAllocPresent() */
445 if (NInoNonResident(ni)) {
446 /* Normal, non-resident data stream. */
447 return ntfs_read_block(page);
450 * Attribute is resident, implying it is not compressed or encrypted.
451 * This also means the attribute is smaller than an mft record and
452 * hence smaller than a page, so can simply zero out any pages with
453 * index above 0. Note the attribute can actually be marked compressed
454 * but if it is resident the actual data is not compressed so we are
455 * ok to ignore the compressed flag here.
457 if (unlikely(page->index > 0)) {
458 kaddr = kmap_atomic(page, KM_USER0);
459 memset(kaddr, 0, PAGE_CACHE_SIZE);
460 flush_dcache_page(page);
461 kunmap_atomic(kaddr, KM_USER0);
467 base_ni = ni->ext.base_ntfs_ino;
468 /* Map, pin, and lock the mft record. */
469 mrec = map_mft_record(base_ni);
475 * If a parallel write made the attribute non-resident, drop the mft
476 * record and retry the readpage.
478 if (unlikely(NInoNonResident(ni))) {
479 unmap_mft_record(base_ni);
482 ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
483 if (unlikely(!ctx)) {
487 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
488 CASE_SENSITIVE, 0, NULL, 0, ctx);
490 goto put_unm_err_out;
491 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
492 read_lock_irqsave(&ni->size_lock, flags);
493 if (unlikely(attr_len > ni->initialized_size))
494 attr_len = ni->initialized_size;
495 i_size = i_size_read(vi);
496 read_unlock_irqrestore(&ni->size_lock, flags);
497 if (unlikely(attr_len > i_size)) {
498 /* Race with shrinking truncate. */
501 kaddr = kmap_atomic(page, KM_USER0);
502 /* Copy the data to the page. */
503 memcpy(kaddr, (u8*)ctx->attr +
504 le16_to_cpu(ctx->attr->data.resident.value_offset),
506 /* Zero the remainder of the page. */
507 memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
508 flush_dcache_page(page);
509 kunmap_atomic(kaddr, KM_USER0);
511 ntfs_attr_put_search_ctx(ctx);
513 unmap_mft_record(base_ni);
515 SetPageUptodate(page);
524 * ntfs_write_block - write a @page to the backing store
525 * @page: page cache page to write out
526 * @wbc: writeback control structure
528 * This function is for writing pages belonging to non-resident, non-mst
529 * protected attributes to their backing store.
531 * For a page with buffers, map and write the dirty buffers asynchronously
532 * under page writeback. For a page without buffers, create buffers for the
533 * page, then proceed as above.
535 * If a page doesn't have buffers the page dirty state is definitive. If a page
536 * does have buffers, the page dirty state is just a hint, and the buffer dirty
537 * state is definitive. (A hint which has rules: dirty buffers against a clean
538 * page is illegal. Other combinations are legal and need to be handled. In
539 * particular a dirty page containing clean buffers for example.)
541 * Return 0 on success and -errno on error.
543 * Based on ntfs_read_block() and __block_write_full_page().
545 static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
549 s64 initialized_size;
551 sector_t block, dblock, iblock;
556 struct buffer_head *bh, *head;
558 unsigned int blocksize, vcn_ofs;
560 BOOL need_end_writeback;
561 unsigned char blocksize_bits;
563 vi = page->mapping->host;
567 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
568 "0x%lx.", ni->mft_no, ni->type, page->index);
570 BUG_ON(!NInoNonResident(ni));
571 BUG_ON(NInoMstProtected(ni));
573 blocksize_bits = vi->i_blkbits;
574 blocksize = 1 << blocksize_bits;
576 if (!page_has_buffers(page)) {
577 BUG_ON(!PageUptodate(page));
578 create_empty_buffers(page, blocksize,
579 (1 << BH_Uptodate) | (1 << BH_Dirty));
580 if (unlikely(!page_has_buffers(page))) {
581 ntfs_warning(vol->sb, "Error allocating page "
582 "buffers. Redirtying page so we try "
585 * Put the page back on mapping->dirty_pages, but leave
586 * its buffers' dirty state as-is.
588 redirty_page_for_writepage(wbc, page);
593 bh = head = page_buffers(page);
596 /* NOTE: Different naming scheme to ntfs_read_block()! */
598 /* The first block in the page. */
599 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
601 read_lock_irqsave(&ni->size_lock, flags);
602 i_size = i_size_read(vi);
603 initialized_size = ni->initialized_size;
604 read_unlock_irqrestore(&ni->size_lock, flags);
606 /* The first out of bounds block for the data size. */
607 dblock = (i_size + blocksize - 1) >> blocksize_bits;
609 /* The last (fully or partially) initialized block. */
610 iblock = initialized_size >> blocksize_bits;
613 * Be very careful. We have no exclusion from __set_page_dirty_buffers
614 * here, and the (potentially unmapped) buffers may become dirty at
615 * any time. If a buffer becomes dirty here after we've inspected it
616 * then we just miss that fact, and the page stays dirty.
618 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
619 * handle that here by just cleaning them.
623 * Loop through all the buffers in the page, mapping all the dirty
624 * buffers to disk addresses and handling any aliases from the
625 * underlying block device's mapping.
630 BOOL is_retry = FALSE;
632 if (unlikely(block >= dblock)) {
634 * Mapped buffers outside i_size will occur, because
635 * this page can be outside i_size when there is a
636 * truncate in progress. The contents of such buffers
637 * were zeroed by ntfs_writepage().
639 * FIXME: What about the small race window where
640 * ntfs_writepage() has not done any clearing because
641 * the page was within i_size but before we get here,
642 * vmtruncate() modifies i_size?
644 clear_buffer_dirty(bh);
645 set_buffer_uptodate(bh);
649 /* Clean buffers are not written out, so no need to map them. */
650 if (!buffer_dirty(bh))
653 /* Make sure we have enough initialized size. */
654 if (unlikely((block >= iblock) &&
655 (initialized_size < i_size))) {
657 * If this page is fully outside initialized size, zero
658 * out all pages between the current initialized size
659 * and the current page. Just use ntfs_readpage() to do
660 * the zeroing transparently.
662 if (block > iblock) {
665 // - read_cache_page()
666 // Again for each page do:
667 // - wait_on_page_locked()
668 // - Check (PageUptodate(page) &&
670 // Update initialized size in the attribute and
672 // Again, for each page do:
673 // __set_page_dirty_buffers();
674 // page_cache_release()
675 // We don't need to wait on the writes.
679 * The current page straddles initialized size. Zero
680 * all non-uptodate buffers and set them uptodate (and
681 * dirty?). Note, there aren't any non-uptodate buffers
682 * if the page is uptodate.
683 * FIXME: For an uptodate page, the buffers may need to
684 * be written out because they were not initialized on
687 if (!PageUptodate(page)) {
689 // Zero any non-uptodate buffers up to i_size.
690 // Set them uptodate and dirty.
693 // Update initialized size in the attribute and in the
694 // inode (up to i_size).
696 // FIXME: This is inefficient. Try to batch the two
697 // size changes to happen in one go.
698 ntfs_error(vol->sb, "Writing beyond initialized size "
699 "is not supported yet. Sorry.");
702 // Do NOT set_buffer_new() BUT DO clear buffer range
703 // outside write request range.
704 // set_buffer_uptodate() on complete buffers as well as
705 // set_buffer_dirty().
708 /* No need to map buffers that are already mapped. */
709 if (buffer_mapped(bh))
712 /* Unmapped, dirty buffer. Need to map it. */
713 bh->b_bdev = vol->sb->s_bdev;
715 /* Convert block into corresponding vcn and offset. */
716 vcn = (VCN)block << blocksize_bits;
717 vcn_ofs = vcn & vol->cluster_size_mask;
718 vcn >>= vol->cluster_size_bits;
721 down_read(&ni->runlist.lock);
724 if (likely(rl != NULL)) {
725 /* Seek to element containing target vcn. */
726 while (rl->length && rl[1].vcn <= vcn)
728 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
730 lcn = LCN_RL_NOT_MAPPED;
731 /* Successful remap. */
733 /* Setup buffer head to point to correct block. */
734 bh->b_blocknr = ((lcn << vol->cluster_size_bits) +
735 vcn_ofs) >> blocksize_bits;
736 set_buffer_mapped(bh);
739 /* It is a hole, need to instantiate it. */
740 if (lcn == LCN_HOLE) {
742 unsigned long *bpos, *bend;
744 /* Check if the buffer is zero. */
745 kaddr = kmap_atomic(page, KM_USER0);
746 bpos = (unsigned long *)(kaddr + bh_offset(bh));
747 bend = (unsigned long *)((u8*)bpos + blocksize);
751 } while (likely(++bpos < bend));
752 kunmap_atomic(kaddr, KM_USER0);
755 * Buffer is zero and sparse, no need to write
759 clear_buffer_dirty(bh);
762 // TODO: Instantiate the hole.
763 // clear_buffer_new(bh);
764 // unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
765 ntfs_error(vol->sb, "Writing into sparse regions is "
766 "not supported yet. Sorry.");
770 /* If first try and runlist unmapped, map and retry. */
771 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
774 * Attempt to map runlist, dropping lock for
777 up_read(&ni->runlist.lock);
778 err = ntfs_map_runlist(ni, vcn);
780 goto lock_retry_remap;
783 up_read(&ni->runlist.lock);
785 * If buffer is outside the runlist, truncate has cut it out
786 * of the runlist. Just clean and clear the buffer and set it
787 * uptodate so it can get discarded by the VM.
789 if (err == -ENOENT || lcn == LCN_ENOENT) {
793 clear_buffer_dirty(bh);
794 kaddr = kmap_atomic(page, KM_USER0);
795 memset(kaddr + bh_offset(bh), 0, blocksize);
796 kunmap_atomic(kaddr, KM_USER0);
797 flush_dcache_page(page);
798 set_buffer_uptodate(bh);
802 /* Failed to map the buffer, even after retrying. */
806 ntfs_error(vol->sb, "Failed to write to inode 0x%lx, "
807 "attribute type 0x%x, vcn 0x%llx, offset 0x%x "
808 "because its location on disk could not be "
809 "determined%s (error code %i).", ni->mft_no,
810 ni->type, (unsigned long long)vcn,
811 vcn_ofs, is_retry ? " even after "
812 "retrying" : "", err);
814 } while (block++, (bh = bh->b_this_page) != head);
816 /* Release the lock if we took it. */
818 up_read(&ni->runlist.lock);
820 /* For the error case, need to reset bh to the beginning. */
823 /* Just an optimization, so ->readpage() is not called later. */
824 if (unlikely(!PageUptodate(page))) {
827 if (!buffer_uptodate(bh)) {
832 } while ((bh = bh->b_this_page) != head);
834 SetPageUptodate(page);
837 /* Setup all mapped, dirty buffers for async write i/o. */
839 if (buffer_mapped(bh) && buffer_dirty(bh)) {
841 if (test_clear_buffer_dirty(bh)) {
842 BUG_ON(!buffer_uptodate(bh));
843 mark_buffer_async_write(bh);
846 } else if (unlikely(err)) {
848 * For the error case. The buffer may have been set
849 * dirty during attachment to a dirty page.
852 clear_buffer_dirty(bh);
854 } while ((bh = bh->b_this_page) != head);
857 // TODO: Remove the -EOPNOTSUPP check later on...
858 if (unlikely(err == -EOPNOTSUPP))
860 else if (err == -ENOMEM) {
861 ntfs_warning(vol->sb, "Error allocating memory. "
862 "Redirtying page so we try again "
865 * Put the page back on mapping->dirty_pages, but
866 * leave its buffer's dirty state as-is.
868 redirty_page_for_writepage(wbc, page);
874 BUG_ON(PageWriteback(page));
875 set_page_writeback(page); /* Keeps try_to_free_buffers() away. */
877 /* Submit the prepared buffers for i/o. */
878 need_end_writeback = TRUE;
880 struct buffer_head *next = bh->b_this_page;
881 if (buffer_async_write(bh)) {
882 submit_bh(WRITE, bh);
883 need_end_writeback = FALSE;
886 } while (bh != head);
889 /* If no i/o was started, need to end_page_writeback(). */
890 if (unlikely(need_end_writeback))
891 end_page_writeback(page);
898 * ntfs_write_mst_block - write a @page to the backing store
899 * @page: page cache page to write out
900 * @wbc: writeback control structure
902 * This function is for writing pages belonging to non-resident, mst protected
903 * attributes to their backing store. The only supported attributes are index
904 * allocation and $MFT/$DATA. Both directory inodes and index inodes are
905 * supported for the index allocation case.
907 * The page must remain locked for the duration of the write because we apply
908 * the mst fixups, write, and then undo the fixups, so if we were to unlock the
909 * page before undoing the fixups, any other user of the page will see the
910 * page contents as corrupt.
912 * We clear the page uptodate flag for the duration of the function to ensure
913 * exclusion for the $MFT/$DATA case against someone mapping an mft record we
914 * are about to apply the mst fixups to.
916 * Return 0 on success and -errno on error.
918 * Based on ntfs_write_block(), ntfs_mft_writepage(), and
919 * write_mft_record_nolock().
921 static int ntfs_write_mst_block(struct page *page,
922 struct writeback_control *wbc)
924 sector_t block, dblock, rec_block;
925 struct inode *vi = page->mapping->host;
926 ntfs_inode *ni = NTFS_I(vi);
927 ntfs_volume *vol = ni->vol;
929 unsigned int rec_size = ni->itype.index.block_size;
930 ntfs_inode *locked_nis[PAGE_CACHE_SIZE / rec_size];
931 struct buffer_head *bh, *head, *tbh, *rec_start_bh;
932 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
934 int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec, err, err2;
935 unsigned bh_size, rec_size_bits;
936 BOOL sync, is_mft, page_is_dirty, rec_is_dirty;
937 unsigned char bh_size_bits;
939 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
940 "0x%lx.", vi->i_ino, ni->type, page->index);
941 BUG_ON(!NInoNonResident(ni));
942 BUG_ON(!NInoMstProtected(ni));
943 is_mft = (S_ISREG(vi->i_mode) && !vi->i_ino);
945 * NOTE: ntfs_write_mst_block() would be called for $MFTMirr if a page
946 * in its page cache were to be marked dirty. However this should
947 * never happen with the current driver and considering we do not
948 * handle this case here we do want to BUG(), at least for now.
950 BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) ||
951 (NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION)));
952 bh_size_bits = vi->i_blkbits;
953 bh_size = 1 << bh_size_bits;
954 max_bhs = PAGE_CACHE_SIZE / bh_size;
956 BUG_ON(max_bhs > MAX_BUF_PER_PAGE);
958 /* Were we called for sync purposes? */
959 sync = (wbc->sync_mode == WB_SYNC_ALL);
961 /* Make sure we have mapped buffers. */
962 bh = head = page_buffers(page);
965 rec_size_bits = ni->itype.index.block_size_bits;
966 BUG_ON(!(PAGE_CACHE_SIZE >> rec_size_bits));
967 bhs_per_rec = rec_size >> bh_size_bits;
968 BUG_ON(!bhs_per_rec);
970 /* The first block in the page. */
971 rec_block = block = (sector_t)page->index <<
972 (PAGE_CACHE_SHIFT - bh_size_bits);
974 /* The first out of bounds block for the data size. */
975 dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
978 err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
979 page_is_dirty = rec_is_dirty = FALSE;
982 BOOL is_retry = FALSE;
984 if (likely(block < rec_block)) {
985 if (unlikely(block >= dblock)) {
986 clear_buffer_dirty(bh);
987 set_buffer_uptodate(bh);
991 * This block is not the first one in the record. We
992 * ignore the buffer's dirty state because we could
993 * have raced with a parallel mark_ntfs_record_dirty().
997 if (unlikely(err2)) {
999 clear_buffer_dirty(bh);
1002 } else /* if (block == rec_block) */ {
1003 BUG_ON(block > rec_block);
1004 /* This block is the first one in the record. */
1005 rec_block += bhs_per_rec;
1007 if (unlikely(block >= dblock)) {
1008 clear_buffer_dirty(bh);
1011 if (!buffer_dirty(bh)) {
1012 /* Clean records are not written out. */
1013 rec_is_dirty = FALSE;
1016 rec_is_dirty = TRUE;
1019 /* Need to map the buffer if it is not mapped already. */
1020 if (unlikely(!buffer_mapped(bh))) {
1023 unsigned int vcn_ofs;
1025 bh->b_bdev = vol->sb->s_bdev;
1026 /* Obtain the vcn and offset of the current block. */
1027 vcn = (VCN)block << bh_size_bits;
1028 vcn_ofs = vcn & vol->cluster_size_mask;
1029 vcn >>= vol->cluster_size_bits;
1032 down_read(&ni->runlist.lock);
1033 rl = ni->runlist.rl;
1035 if (likely(rl != NULL)) {
1036 /* Seek to element containing target vcn. */
1037 while (rl->length && rl[1].vcn <= vcn)
1039 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
1041 lcn = LCN_RL_NOT_MAPPED;
1042 /* Successful remap. */
1043 if (likely(lcn >= 0)) {
1044 /* Setup buffer head to correct block. */
1045 bh->b_blocknr = ((lcn <<
1046 vol->cluster_size_bits) +
1047 vcn_ofs) >> bh_size_bits;
1048 set_buffer_mapped(bh);
1051 * Remap failed. Retry to map the runlist once
1052 * unless we are working on $MFT which always
1053 * has the whole of its runlist in memory.
1055 if (!is_mft && !is_retry &&
1056 lcn == LCN_RL_NOT_MAPPED) {
1059 * Attempt to map runlist, dropping
1060 * lock for the duration.
1062 up_read(&ni->runlist.lock);
1063 err2 = ntfs_map_runlist(ni, vcn);
1065 goto lock_retry_remap;
1066 if (err2 == -ENOMEM)
1067 page_is_dirty = TRUE;
1072 up_read(&ni->runlist.lock);
1074 /* Hard error. Abort writing this record. */
1075 if (!err || err == -ENOMEM)
1078 ntfs_error(vol->sb, "Cannot write ntfs record "
1079 "0x%llx (inode 0x%lx, "
1080 "attribute type 0x%x) because "
1081 "its location on disk could "
1082 "not be determined (error "
1086 vol->mft_record_size_bits,
1087 ni->mft_no, ni->type,
1090 * If this is not the first buffer, remove the
1091 * buffers in this record from the list of
1092 * buffers to write and clear their dirty bit
1093 * if not error -ENOMEM.
1095 if (rec_start_bh != bh) {
1096 while (bhs[--nr_bhs] != rec_start_bh)
1098 if (err2 != -ENOMEM) {
1102 } while ((rec_start_bh =
1111 BUG_ON(!buffer_uptodate(bh));
1112 BUG_ON(nr_bhs >= max_bhs);
1114 } while (block++, (bh = bh->b_this_page) != head);
1116 up_read(&ni->runlist.lock);
1117 /* If there were no dirty buffers, we are done. */
1120 /* Map the page so we can access its contents. */
1122 /* Clear the page uptodate flag whilst the mst fixups are applied. */
1123 BUG_ON(!PageUptodate(page));
1124 ClearPageUptodate(page);
1125 for (i = 0; i < nr_bhs; i++) {
1128 /* Skip buffers which are not at the beginning of records. */
1129 if (i % bhs_per_rec)
1132 ofs = bh_offset(tbh);
1135 unsigned long mft_no;
1137 /* Get the mft record number. */
1138 mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
1140 /* Check whether to write this mft record. */
1142 if (!ntfs_may_write_mft_record(vol, mft_no,
1143 (MFT_RECORD*)(kaddr + ofs), &tni)) {
1145 * The record should not be written. This
1146 * means we need to redirty the page before
1149 page_is_dirty = TRUE;
1151 * Remove the buffers in this mft record from
1152 * the list of buffers to write.
1156 } while (++i % bhs_per_rec);
1160 * The record should be written. If a locked ntfs
1161 * inode was returned, add it to the array of locked
1165 locked_nis[nr_locked_nis++] = tni;
1167 /* Apply the mst protection fixups. */
1168 err2 = pre_write_mst_fixup((NTFS_RECORD*)(kaddr + ofs),
1170 if (unlikely(err2)) {
1171 if (!err || err == -ENOMEM)
1173 ntfs_error(vol->sb, "Failed to apply mst fixups "
1174 "(inode 0x%lx, attribute type 0x%x, "
1175 "page index 0x%lx, page offset 0x%x)!"
1176 " Unmount and run chkdsk.", vi->i_ino,
1177 ni->type, page->index, ofs);
1179 * Mark all the buffers in this record clean as we do
1180 * not want to write corrupt data to disk.
1183 clear_buffer_dirty(bhs[i]);
1185 } while (++i % bhs_per_rec);
1190 /* If no records are to be written out, we are done. */
1193 flush_dcache_page(page);
1194 /* Lock buffers and start synchronous write i/o on them. */
1195 for (i = 0; i < nr_bhs; i++) {
1199 if (unlikely(test_set_buffer_locked(tbh)))
1201 /* The buffer dirty state is now irrelevant, just clean it. */
1202 clear_buffer_dirty(tbh);
1203 BUG_ON(!buffer_uptodate(tbh));
1204 BUG_ON(!buffer_mapped(tbh));
1206 tbh->b_end_io = end_buffer_write_sync;
1207 submit_bh(WRITE, tbh);
1209 /* Synchronize the mft mirror now if not @sync. */
1210 if (is_mft && !sync)
1213 /* Wait on i/o completion of buffers. */
1214 for (i = 0; i < nr_bhs; i++) {
1218 wait_on_buffer(tbh);
1219 if (unlikely(!buffer_uptodate(tbh))) {
1220 ntfs_error(vol->sb, "I/O error while writing ntfs "
1221 "record buffer (inode 0x%lx, "
1222 "attribute type 0x%x, page index "
1223 "0x%lx, page offset 0x%lx)! Unmount "
1224 "and run chkdsk.", vi->i_ino, ni->type,
1225 page->index, bh_offset(tbh));
1226 if (!err || err == -ENOMEM)
1229 * Set the buffer uptodate so the page and buffer
1230 * states do not become out of sync.
1232 set_buffer_uptodate(tbh);
1235 /* If @sync, now synchronize the mft mirror. */
1236 if (is_mft && sync) {
1238 for (i = 0; i < nr_bhs; i++) {
1239 unsigned long mft_no;
1243 * Skip buffers which are not at the beginning of
1246 if (i % bhs_per_rec)
1249 /* Skip removed buffers (and hence records). */
1252 ofs = bh_offset(tbh);
1253 /* Get the mft record number. */
1254 mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
1256 if (mft_no < vol->mftmirr_size)
1257 ntfs_sync_mft_mirror(vol, mft_no,
1258 (MFT_RECORD*)(kaddr + ofs),
1264 /* Remove the mst protection fixups again. */
1265 for (i = 0; i < nr_bhs; i++) {
1266 if (!(i % bhs_per_rec)) {
1270 post_write_mst_fixup((NTFS_RECORD*)(kaddr +
1274 flush_dcache_page(page);
1276 /* Unlock any locked inodes. */
1277 while (nr_locked_nis-- > 0) {
1278 ntfs_inode *tni, *base_tni;
1280 tni = locked_nis[nr_locked_nis];
1281 /* Get the base inode. */
1282 down(&tni->extent_lock);
1283 if (tni->nr_extents >= 0)
1286 base_tni = tni->ext.base_ntfs_ino;
1289 up(&tni->extent_lock);
1290 ntfs_debug("Unlocking %s inode 0x%lx.",
1291 tni == base_tni ? "base" : "extent",
1293 up(&tni->mrec_lock);
1294 atomic_dec(&tni->count);
1295 iput(VFS_I(base_tni));
1297 SetPageUptodate(page);
1300 if (unlikely(err && err != -ENOMEM)) {
1302 * Set page error if there is only one ntfs record in the page.
1303 * Otherwise we would loose per-record granularity.
1305 if (ni->itype.index.block_size == PAGE_CACHE_SIZE)
1309 if (page_is_dirty) {
1310 ntfs_debug("Page still contains one or more dirty ntfs "
1311 "records. Redirtying the page starting at "
1312 "record 0x%lx.", page->index <<
1313 (PAGE_CACHE_SHIFT - rec_size_bits));
1314 redirty_page_for_writepage(wbc, page);
1318 * Keep the VM happy. This must be done otherwise the
1319 * radix-tree tag PAGECACHE_TAG_DIRTY remains set even though
1320 * the page is clean.
1322 BUG_ON(PageWriteback(page));
1323 set_page_writeback(page);
1325 end_page_writeback(page);
1328 ntfs_debug("Done.");
1333 * ntfs_writepage - write a @page to the backing store
1334 * @page: page cache page to write out
1335 * @wbc: writeback control structure
1337 * This is called from the VM when it wants to have a dirty ntfs page cache
1338 * page cleaned. The VM has already locked the page and marked it clean.
1340 * For non-resident attributes, ntfs_writepage() writes the @page by calling
1341 * the ntfs version of the generic block_write_full_page() function,
1342 * ntfs_write_block(), which in turn if necessary creates and writes the
1343 * buffers associated with the page asynchronously.
1345 * For resident attributes, OTOH, ntfs_writepage() writes the @page by copying
1346 * the data to the mft record (which at this stage is most likely in memory).
1347 * The mft record is then marked dirty and written out asynchronously via the
1348 * vfs inode dirty code path for the inode the mft record belongs to or via the
1349 * vm page dirty code path for the page the mft record is in.
1351 * Based on ntfs_readpage() and fs/buffer.c::block_write_full_page().
1353 * Return 0 on success and -errno on error.
1355 static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1358 struct inode *vi = page->mapping->host;
1359 ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
1361 ntfs_attr_search_ctx *ctx = NULL;
1362 MFT_RECORD *m = NULL;
1367 BUG_ON(!PageLocked(page));
1368 i_size = i_size_read(vi);
1369 /* Is the page fully outside i_size? (truncate in progress) */
1370 if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >>
1371 PAGE_CACHE_SHIFT)) {
1373 * The page may have dirty, unmapped buffers. Make them
1374 * freeable here, so the page does not leak.
1376 block_invalidatepage(page, 0);
1378 ntfs_debug("Write outside i_size - truncated?");
1382 * Only $DATA attributes can be encrypted and only unnamed $DATA
1383 * attributes can be compressed. Index root can have the flags set but
1384 * this means to create compressed/encrypted files, not that the
1385 * attribute is compressed/encrypted. Note we need to check for
1386 * AT_INDEX_ALLOCATION since this is the type of both directory and
1389 if (ni->type != AT_INDEX_ALLOCATION) {
1390 /* If file is encrypted, deny access, just like NT4. */
1391 if (NInoEncrypted(ni)) {
1393 BUG_ON(ni->type != AT_DATA);
1394 ntfs_debug("Denying write access to encrypted "
1398 /* Compressed data streams are handled in compress.c. */
1399 if (NInoNonResident(ni) && NInoCompressed(ni)) {
1400 BUG_ON(ni->type != AT_DATA);
1401 BUG_ON(ni->name_len);
1402 // TODO: Implement and replace this with
1403 // return ntfs_write_compressed_block(page);
1405 ntfs_error(vi->i_sb, "Writing to compressed files is "
1406 "not supported yet. Sorry.");
1409 // TODO: Implement and remove this check.
1410 if (NInoNonResident(ni) && NInoSparse(ni)) {
1412 ntfs_error(vi->i_sb, "Writing to sparse files is not "
1413 "supported yet. Sorry.");
1417 /* NInoNonResident() == NInoIndexAllocPresent() */
1418 if (NInoNonResident(ni)) {
1419 /* We have to zero every time due to mmap-at-end-of-file. */
1420 if (page->index >= (i_size >> PAGE_CACHE_SHIFT)) {
1421 /* The page straddles i_size. */
1422 unsigned int ofs = i_size & ~PAGE_CACHE_MASK;
1423 kaddr = kmap_atomic(page, KM_USER0);
1424 memset(kaddr + ofs, 0, PAGE_CACHE_SIZE - ofs);
1425 kunmap_atomic(kaddr, KM_USER0);
1426 flush_dcache_page(page);
1428 /* Handle mst protected attributes. */
1429 if (NInoMstProtected(ni))
1430 return ntfs_write_mst_block(page, wbc);
1431 /* Normal, non-resident data stream. */
1432 return ntfs_write_block(page, wbc);
1435 * Attribute is resident, implying it is not compressed, encrypted, or
1436 * mst protected. This also means the attribute is smaller than an mft
1437 * record and hence smaller than a page, so can simply return error on
1438 * any pages with index above 0. Note the attribute can actually be
1439 * marked compressed but if it is resident the actual data is not
1440 * compressed so we are ok to ignore the compressed flag here.
1442 BUG_ON(page_has_buffers(page));
1443 BUG_ON(!PageUptodate(page));
1444 if (unlikely(page->index > 0)) {
1445 ntfs_error(vi->i_sb, "BUG()! page->index (0x%lx) > 0. "
1446 "Aborting write.", page->index);
1447 BUG_ON(PageWriteback(page));
1448 set_page_writeback(page);
1450 end_page_writeback(page);
1456 base_ni = ni->ext.base_ntfs_ino;
1457 /* Map, pin, and lock the mft record. */
1458 m = map_mft_record(base_ni);
1466 * If a parallel write made the attribute non-resident, drop the mft
1467 * record and retry the writepage.
1469 if (unlikely(NInoNonResident(ni))) {
1470 unmap_mft_record(base_ni);
1471 goto retry_writepage;
1473 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1474 if (unlikely(!ctx)) {
1478 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1479 CASE_SENSITIVE, 0, NULL, 0, ctx);
1483 * Keep the VM happy. This must be done otherwise the radix-tree tag
1484 * PAGECACHE_TAG_DIRTY remains set even though the page is clean.
1486 BUG_ON(PageWriteback(page));
1487 set_page_writeback(page);
1489 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
1490 i_size = i_size_read(vi);
1491 if (unlikely(attr_len > i_size)) {
1492 /* Race with shrinking truncate or a failed truncate. */
1495 * If the truncate failed, fix it up now. If a concurrent
1496 * truncate, we do its job, so it does not have to do anything.
1498 err = ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr,
1500 /* Shrinking cannot fail. */
1503 kaddr = kmap_atomic(page, KM_USER0);
1504 /* Copy the data from the page to the mft record. */
1505 memcpy((u8*)ctx->attr +
1506 le16_to_cpu(ctx->attr->data.resident.value_offset),
1508 /* Zero out of bounds area in the page cache page. */
1509 memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
1510 kunmap_atomic(kaddr, KM_USER0);
1511 flush_dcache_mft_record_page(ctx->ntfs_ino);
1512 flush_dcache_page(page);
1513 /* We are done with the page. */
1514 end_page_writeback(page);
1515 /* Finally, mark the mft record dirty, so it gets written back. */
1516 mark_mft_record_dirty(ctx->ntfs_ino);
1517 ntfs_attr_put_search_ctx(ctx);
1518 unmap_mft_record(base_ni);
1521 if (err == -ENOMEM) {
1522 ntfs_warning(vi->i_sb, "Error allocating memory. Redirtying "
1523 "page so we try again later.");
1525 * Put the page back on mapping->dirty_pages, but leave its
1526 * buffers' dirty state as-is.
1528 redirty_page_for_writepage(wbc, page);
1531 ntfs_error(vi->i_sb, "Resident attribute write failed with "
1534 NVolSetErrors(ni->vol);
1539 ntfs_attr_put_search_ctx(ctx);
1541 unmap_mft_record(base_ni);
1546 * ntfs_prepare_nonresident_write -
1549 static int ntfs_prepare_nonresident_write(struct page *page,
1550 unsigned from, unsigned to)
1554 s64 initialized_size;
1556 sector_t block, ablock, iblock;
1560 runlist_element *rl;
1561 struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
1562 unsigned long flags;
1563 unsigned int vcn_ofs, block_start, block_end, blocksize;
1566 unsigned char blocksize_bits;
1568 vi = page->mapping->host;
1572 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
1573 "0x%lx, from = %u, to = %u.", ni->mft_no, ni->type,
1574 page->index, from, to);
1576 BUG_ON(!NInoNonResident(ni));
1578 blocksize_bits = vi->i_blkbits;
1579 blocksize = 1 << blocksize_bits;
1582 * create_empty_buffers() will create uptodate/dirty buffers if the
1583 * page is uptodate/dirty.
1585 if (!page_has_buffers(page))
1586 create_empty_buffers(page, blocksize, 0);
1587 bh = head = page_buffers(page);
1591 /* The first block in the page. */
1592 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
1594 read_lock_irqsave(&ni->size_lock, flags);
1596 * The first out of bounds block for the allocated size. No need to
1597 * round up as allocated_size is in multiples of cluster size and the
1598 * minimum cluster size is 512 bytes, which is equal to the smallest
1601 ablock = ni->allocated_size >> blocksize_bits;
1602 i_size = i_size_read(vi);
1603 initialized_size = ni->initialized_size;
1604 read_unlock_irqrestore(&ni->size_lock, flags);
1606 /* The last (fully or partially) initialized block. */
1607 iblock = initialized_size >> blocksize_bits;
1609 /* Loop through all the buffers in the page. */
1614 block_end = block_start + blocksize;
1616 * If buffer @bh is outside the write, just mark it uptodate
1617 * if the page is uptodate and continue with the next buffer.
1619 if (block_end <= from || block_start >= to) {
1620 if (PageUptodate(page)) {
1621 if (!buffer_uptodate(bh))
1622 set_buffer_uptodate(bh);
1627 * @bh is at least partially being written to.
1628 * Make sure it is not marked as new.
1630 //if (buffer_new(bh))
1631 // clear_buffer_new(bh);
1633 if (block >= ablock) {
1634 // TODO: block is above allocated_size, need to
1635 // allocate it. Best done in one go to accommodate not
1636 // only block but all above blocks up to and including:
1637 // ((page->index << PAGE_CACHE_SHIFT) + to + blocksize
1638 // - 1) >> blobksize_bits. Obviously will need to round
1639 // up to next cluster boundary, too. This should be
1640 // done with a helper function, so it can be reused.
1641 ntfs_error(vol->sb, "Writing beyond allocated size "
1642 "is not supported yet. Sorry.");
1645 // Need to update ablock.
1646 // Need to set_buffer_new() on all block bhs that are
1650 * Now we have enough allocated size to fulfill the whole
1651 * request, i.e. block < ablock is true.
1653 if (unlikely((block >= iblock) &&
1654 (initialized_size < i_size))) {
1656 * If this page is fully outside initialized size, zero
1657 * out all pages between the current initialized size
1658 * and the current page. Just use ntfs_readpage() to do
1659 * the zeroing transparently.
1661 if (block > iblock) {
1663 // For each page do:
1664 // - read_cache_page()
1665 // Again for each page do:
1666 // - wait_on_page_locked()
1667 // - Check (PageUptodate(page) &&
1668 // !PageError(page))
1669 // Update initialized size in the attribute and
1671 // Again, for each page do:
1672 // __set_page_dirty_buffers();
1673 // page_cache_release()
1674 // We don't need to wait on the writes.
1678 * The current page straddles initialized size. Zero
1679 * all non-uptodate buffers and set them uptodate (and
1680 * dirty?). Note, there aren't any non-uptodate buffers
1681 * if the page is uptodate.
1682 * FIXME: For an uptodate page, the buffers may need to
1683 * be written out because they were not initialized on
1686 if (!PageUptodate(page)) {
1688 // Zero any non-uptodate buffers up to i_size.
1689 // Set them uptodate and dirty.
1692 // Update initialized size in the attribute and in the
1693 // inode (up to i_size).
1695 // FIXME: This is inefficient. Try to batch the two
1696 // size changes to happen in one go.
1697 ntfs_error(vol->sb, "Writing beyond initialized size "
1698 "is not supported yet. Sorry.");
1701 // Do NOT set_buffer_new() BUT DO clear buffer range
1702 // outside write request range.
1703 // set_buffer_uptodate() on complete buffers as well as
1704 // set_buffer_dirty().
1707 /* Need to map unmapped buffers. */
1708 if (!buffer_mapped(bh)) {
1709 /* Unmapped buffer. Need to map it. */
1710 bh->b_bdev = vol->sb->s_bdev;
1712 /* Convert block into corresponding vcn and offset. */
1713 vcn = (VCN)block << blocksize_bits >>
1714 vol->cluster_size_bits;
1715 vcn_ofs = ((VCN)block << blocksize_bits) &
1716 vol->cluster_size_mask;
1721 down_read(&ni->runlist.lock);
1722 rl = ni->runlist.rl;
1724 if (likely(rl != NULL)) {
1725 /* Seek to element containing target vcn. */
1726 while (rl->length && rl[1].vcn <= vcn)
1728 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
1730 lcn = LCN_RL_NOT_MAPPED;
1731 if (unlikely(lcn < 0)) {
1733 * We extended the attribute allocation above.
1734 * If we hit an ENOENT here it means that the
1735 * allocation was insufficient which is a bug.
1737 BUG_ON(lcn == LCN_ENOENT);
1739 /* It is a hole, need to instantiate it. */
1740 if (lcn == LCN_HOLE) {
1741 // TODO: Instantiate the hole.
1742 // clear_buffer_new(bh);
1743 // unmap_underlying_metadata(bh->b_bdev,
1745 // For non-uptodate buffers, need to
1746 // zero out the region outside the
1747 // request in this bh or all bhs,
1748 // depending on what we implemented
1750 // Need to flush_dcache_page().
1751 // Or could use set_buffer_new()
1753 ntfs_error(vol->sb, "Writing into "
1754 "sparse regions is "
1755 "not supported yet. "
1759 up_read(&ni->runlist.lock);
1761 } else if (!is_retry &&
1762 lcn == LCN_RL_NOT_MAPPED) {
1765 * Attempt to map runlist, dropping
1766 * lock for the duration.
1768 up_read(&ni->runlist.lock);
1769 err = ntfs_map_runlist(ni, vcn);
1771 goto lock_retry_remap;
1774 up_read(&ni->runlist.lock);
1776 * Failed to map the buffer, even after
1782 ntfs_error(vol->sb, "Failed to write to inode "
1783 "0x%lx, attribute type 0x%x, "
1784 "vcn 0x%llx, offset 0x%x "
1785 "because its location on disk "
1786 "could not be determined%s "
1788 ni->mft_no, ni->type,
1789 (unsigned long long)vcn,
1790 vcn_ofs, is_retry ? " even "
1791 "after retrying" : "", err);
1794 /* We now have a successful remap, i.e. lcn >= 0. */
1796 /* Setup buffer head to correct block. */
1797 bh->b_blocknr = ((lcn << vol->cluster_size_bits)
1798 + vcn_ofs) >> blocksize_bits;
1799 set_buffer_mapped(bh);
1801 // FIXME: Something analogous to this is needed for
1802 // each newly allocated block, i.e. BH_New.
1803 // FIXME: Might need to take this out of the
1804 // if (!buffer_mapped(bh)) {}, depending on how we
1805 // implement things during the allocated_size and
1806 // initialized_size extension code above.
1807 if (buffer_new(bh)) {
1808 clear_buffer_new(bh);
1809 unmap_underlying_metadata(bh->b_bdev,
1811 if (PageUptodate(page)) {
1812 set_buffer_uptodate(bh);
1816 * Page is _not_ uptodate, zero surrounding
1817 * region. NOTE: This is how we decide if to
1820 if (block_end > to || block_start < from) {
1823 kaddr = kmap_atomic(page, KM_USER0);
1825 memset(kaddr + to, 0,
1827 if (block_start < from)
1828 memset(kaddr + block_start, 0,
1831 flush_dcache_page(page);
1832 kunmap_atomic(kaddr, KM_USER0);
1837 /* @bh is mapped, set it uptodate if the page is uptodate. */
1838 if (PageUptodate(page)) {
1839 if (!buffer_uptodate(bh))
1840 set_buffer_uptodate(bh);
1844 * The page is not uptodate. The buffer is mapped. If it is not
1845 * uptodate, and it is only partially being written to, we need
1846 * to read the buffer in before the write, i.e. right now.
1848 if (!buffer_uptodate(bh) &&
1849 (block_start < from || block_end > to)) {
1850 ll_rw_block(READ, 1, &bh);
1853 } while (block++, block_start = block_end,
1854 (bh = bh->b_this_page) != head);
1856 /* Release the lock if we took it. */
1858 up_read(&ni->runlist.lock);
1862 /* If we issued read requests, let them complete. */
1863 while (wait_bh > wait) {
1864 wait_on_buffer(*--wait_bh);
1865 if (!buffer_uptodate(*wait_bh))
1869 ntfs_debug("Done.");
1873 * Zero out any newly allocated blocks to avoid exposing stale data.
1874 * If BH_New is set, we know that the block was newly allocated in the
1876 * FIXME: What about initialized_size increments? Have we done all the
1877 * required zeroing above? If not this error handling is broken, and
1878 * in particular the if (block_end <= from) check is completely bogus.
1884 block_end = block_start + blocksize;
1885 if (block_end <= from)
1887 if (block_start >= to)
1889 if (buffer_new(bh)) {
1892 clear_buffer_new(bh);
1893 kaddr = kmap_atomic(page, KM_USER0);
1894 memset(kaddr + block_start, 0, bh->b_size);
1895 kunmap_atomic(kaddr, KM_USER0);
1896 set_buffer_uptodate(bh);
1897 mark_buffer_dirty(bh);
1900 } while (block_start = block_end, (bh = bh->b_this_page) != head);
1902 flush_dcache_page(page);
1904 up_read(&ni->runlist.lock);
1909 * ntfs_prepare_write - prepare a page for receiving data
1911 * This is called from generic_file_write() with i_sem held on the inode
1912 * (@page->mapping->host). The @page is locked but not kmap()ped. The source
1913 * data has not yet been copied into the @page.
1915 * Need to extend the attribute/fill in holes if necessary, create blocks and
1916 * make partially overwritten blocks uptodate,
1918 * i_size is not to be modified yet.
1920 * Return 0 on success or -errno on error.
1922 * Should be using block_prepare_write() [support for sparse files] or
1923 * cont_prepare_write() [no support for sparse files]. Cannot do that due to
1924 * ntfs specifics but can look at them for implementation guidance.
1926 * Note: In the range, @from is inclusive and @to is exclusive, i.e. @from is
1927 * the first byte in the page that will be written to and @to is the first byte
1928 * after the last byte that will be written to.
1930 static int ntfs_prepare_write(struct file *file, struct page *page,
1931 unsigned from, unsigned to)
1935 struct inode *vi = page->mapping->host;
1936 ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
1937 ntfs_volume *vol = ni->vol;
1938 ntfs_attr_search_ctx *ctx = NULL;
1939 MFT_RECORD *m = NULL;
1945 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
1946 "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
1947 page->index, from, to);
1948 BUG_ON(!PageLocked(page));
1949 BUG_ON(from > PAGE_CACHE_SIZE);
1950 BUG_ON(to > PAGE_CACHE_SIZE);
1952 BUG_ON(NInoMstProtected(ni));
1954 * If a previous ntfs_truncate() failed, repeat it and abort if it
1957 if (unlikely(NInoTruncateFailed(ni))) {
1958 down_write(&vi->i_alloc_sem);
1959 err = ntfs_truncate(vi);
1960 up_write(&vi->i_alloc_sem);
1961 if (err || NInoTruncateFailed(ni)) {
1967 /* If the attribute is not resident, deal with it elsewhere. */
1968 if (NInoNonResident(ni)) {
1970 * Only unnamed $DATA attributes can be compressed, encrypted,
1973 if (ni->type == AT_DATA && !ni->name_len) {
1974 /* If file is encrypted, deny access, just like NT4. */
1975 if (NInoEncrypted(ni)) {
1976 ntfs_debug("Denying write access to encrypted "
1980 /* Compressed data streams are handled in compress.c. */
1981 if (NInoCompressed(ni)) {
1982 // TODO: Implement and replace this check with
1983 // return ntfs_write_compressed_block(page);
1984 ntfs_error(vi->i_sb, "Writing to compressed "
1985 "files is not supported yet. "
1989 // TODO: Implement and remove this check.
1990 if (NInoSparse(ni)) {
1991 ntfs_error(vi->i_sb, "Writing to sparse files "
1992 "is not supported yet. Sorry.");
1996 /* Normal data stream. */
1997 return ntfs_prepare_nonresident_write(page, from, to);
2000 * Attribute is resident, implying it is not compressed, encrypted, or
2003 BUG_ON(page_has_buffers(page));
2004 new_size = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
2005 /* If we do not need to resize the attribute allocation we are done. */
2006 if (new_size <= i_size_read(vi))
2008 /* Map, pin, and lock the (base) mft record. */
2012 base_ni = ni->ext.base_ntfs_ino;
2013 m = map_mft_record(base_ni);
2020 ctx = ntfs_attr_get_search_ctx(base_ni, m);
2021 if (unlikely(!ctx)) {
2025 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2026 CASE_SENSITIVE, 0, NULL, 0, ctx);
2027 if (unlikely(err)) {
2034 /* The total length of the attribute value. */
2035 attr_len = le32_to_cpu(a->data.resident.value_length);
2036 /* Fix an eventual previous failure of ntfs_commit_write(). */
2037 i_size = i_size_read(vi);
2038 if (unlikely(attr_len > i_size)) {
2040 a->data.resident.value_length = cpu_to_le32(attr_len);
2042 /* If we do not need to resize the attribute allocation we are done. */
2043 if (new_size <= attr_len)
2045 /* Check if new size is allowed in $AttrDef. */
2046 err = ntfs_attr_size_bounds_check(vol, ni->type, new_size);
2047 if (unlikely(err)) {
2048 if (err == -ERANGE) {
2049 ntfs_error(vol->sb, "Write would cause the inode "
2050 "0x%lx to exceed the maximum size for "
2051 "its attribute type (0x%x). Aborting "
2052 "write.", vi->i_ino,
2053 le32_to_cpu(ni->type));
2055 ntfs_error(vol->sb, "Inode 0x%lx has unknown "
2056 "attribute type 0x%x. Aborting "
2057 "write.", vi->i_ino,
2058 le32_to_cpu(ni->type));
2064 * Extend the attribute record to be able to store the new attribute
2067 if (new_size >= vol->mft_record_size || ntfs_attr_record_resize(m, a,
2068 le16_to_cpu(a->data.resident.value_offset) +
2070 /* Not enough space in the mft record. */
2071 ntfs_error(vol->sb, "Not enough space in the mft record for "
2072 "the resized attribute value. This is not "
2073 "supported yet. Aborting write.");
2078 * We have enough space in the mft record to fit the write. This
2079 * implies the attribute is smaller than the mft record and hence the
2080 * attribute must be in a single page and hence page->index must be 0.
2082 BUG_ON(page->index);
2084 * If the beginning of the write is past the old size, enlarge the
2085 * attribute value up to the beginning of the write and fill it with
2088 if (from > attr_len) {
2089 memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
2090 attr_len, 0, from - attr_len);
2091 a->data.resident.value_length = cpu_to_le32(from);
2092 /* Zero the corresponding area in the page as well. */
2093 if (PageUptodate(page)) {
2094 kaddr = kmap_atomic(page, KM_USER0);
2095 memset(kaddr + attr_len, 0, from - attr_len);
2096 kunmap_atomic(kaddr, KM_USER0);
2097 flush_dcache_page(page);
2100 flush_dcache_mft_record_page(ctx->ntfs_ino);
2101 mark_mft_record_dirty(ctx->ntfs_ino);
2103 ntfs_attr_put_search_ctx(ctx);
2104 unmap_mft_record(base_ni);
2106 * Because resident attributes are handled by memcpy() to/from the
2107 * corresponding MFT record, and because this form of i/o is byte
2108 * aligned rather than block aligned, there is no need to bring the
2109 * page uptodate here as in the non-resident case where we need to
2110 * bring the buffers straddled by the write uptodate before
2111 * generic_file_write() does the copying from userspace.
2113 * We thus defer the uptodate bringing of the page region outside the
2114 * region written to to ntfs_commit_write(), which makes the code
2115 * simpler and saves one atomic kmap which is good.
2118 ntfs_debug("Done.");
2122 ntfs_warning(vi->i_sb, "Error allocating memory required to "
2123 "prepare the write.");
2125 ntfs_error(vi->i_sb, "Resident attribute prepare write failed "
2126 "with error %i.", err);
2132 ntfs_attr_put_search_ctx(ctx);
2134 unmap_mft_record(base_ni);
2139 * ntfs_commit_nonresident_write -
2142 static int ntfs_commit_nonresident_write(struct page *page,
2143 unsigned from, unsigned to)
2145 s64 pos = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
2146 struct inode *vi = page->mapping->host;
2147 struct buffer_head *bh, *head;
2148 unsigned int block_start, block_end, blocksize;
2151 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
2152 "0x%lx, from = %u, to = %u.", vi->i_ino,
2153 NTFS_I(vi)->type, page->index, from, to);
2154 blocksize = 1 << vi->i_blkbits;
2156 // FIXME: We need a whole slew of special cases in here for compressed
2157 // files for example...
2158 // For now, we know ntfs_prepare_write() would have failed so we can't
2159 // get here in any of the cases which we have to special case, so we
2160 // are just a ripped off, unrolled generic_commit_write().
2162 bh = head = page_buffers(page);
2166 block_end = block_start + blocksize;
2167 if (block_end <= from || block_start >= to) {
2168 if (!buffer_uptodate(bh))
2171 set_buffer_uptodate(bh);
2172 mark_buffer_dirty(bh);
2174 } while (block_start = block_end, (bh = bh->b_this_page) != head);
2176 * If this is a partial write which happened to make all buffers
2177 * uptodate then we can optimize away a bogus ->readpage() for the next
2178 * read(). Here we 'discover' whether the page went uptodate as a
2179 * result of this (potentially partial) write.
2182 SetPageUptodate(page);
2184 * Not convinced about this at all. See disparity comment above. For
2185 * now we know ntfs_prepare_write() would have failed in the write
2186 * exceeds i_size case, so this will never trigger which is fine.
2188 if (pos > i_size_read(vi)) {
2189 ntfs_error(vi->i_sb, "Writing beyond the existing file size is "
2190 "not supported yet. Sorry.");
2192 // vi->i_size = pos;
2193 // mark_inode_dirty(vi);
2195 ntfs_debug("Done.");
2200 * ntfs_commit_write - commit the received data
2202 * This is called from generic_file_write() with i_sem held on the inode
2203 * (@page->mapping->host). The @page is locked but not kmap()ped. The source
2204 * data has already been copied into the @page. ntfs_prepare_write() has been
2205 * called before the data copied and it returned success so we can take the
2206 * results of various BUG checks and some error handling for granted.
2208 * Need to mark modified blocks dirty so they get written out later when
2209 * ntfs_writepage() is invoked by the VM.
2211 * Return 0 on success or -errno on error.
2213 * Should be using generic_commit_write(). This marks buffers uptodate and
2214 * dirty, sets the page uptodate if all buffers in the page are uptodate, and
2215 * updates i_size if the end of io is beyond i_size. In that case, it also
2216 * marks the inode dirty.
2218 * Cannot use generic_commit_write() due to ntfs specialities but can look at
2219 * it for implementation guidance.
2221 * If things have gone as outlined in ntfs_prepare_write(), then we do not
2222 * need to do any page content modifications here at all, except in the write
2223 * to resident attribute case, where we need to do the uptodate bringing here
2224 * which we combine with the copying into the mft record which means we save
2227 static int ntfs_commit_write(struct file *file, struct page *page,
2228 unsigned from, unsigned to)
2230 struct inode *vi = page->mapping->host;
2231 ntfs_inode *base_ni, *ni = NTFS_I(vi);
2232 char *kaddr, *kattr;
2233 ntfs_attr_search_ctx *ctx;
2239 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
2240 "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
2241 page->index, from, to);
2242 /* If the attribute is not resident, deal with it elsewhere. */
2243 if (NInoNonResident(ni)) {
2244 /* Only unnamed $DATA attributes can be compressed/encrypted. */
2245 if (ni->type == AT_DATA && !ni->name_len) {
2246 /* Encrypted files need separate handling. */
2247 if (NInoEncrypted(ni)) {
2248 // We never get here at present!
2251 /* Compressed data streams are handled in compress.c. */
2252 if (NInoCompressed(ni)) {
2253 // TODO: Implement this!
2254 // return ntfs_write_compressed_block(page);
2255 // We never get here at present!
2259 /* Normal data stream. */
2260 return ntfs_commit_nonresident_write(page, from, to);
2263 * Attribute is resident, implying it is not compressed, encrypted, or
2269 base_ni = ni->ext.base_ntfs_ino;
2270 /* Map, pin, and lock the mft record. */
2271 m = map_mft_record(base_ni);
2278 ctx = ntfs_attr_get_search_ctx(base_ni, m);
2279 if (unlikely(!ctx)) {
2283 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2284 CASE_SENSITIVE, 0, NULL, 0, ctx);
2285 if (unlikely(err)) {
2291 /* The total length of the attribute value. */
2292 attr_len = le32_to_cpu(a->data.resident.value_length);
2293 BUG_ON(from > attr_len);
2294 kattr = (u8*)a + le16_to_cpu(a->data.resident.value_offset);
2295 kaddr = kmap_atomic(page, KM_USER0);
2296 /* Copy the received data from the page to the mft record. */
2297 memcpy(kattr + from, kaddr + from, to - from);
2298 /* Update the attribute length if necessary. */
2299 if (to > attr_len) {
2301 a->data.resident.value_length = cpu_to_le32(attr_len);
2304 * If the page is not uptodate, bring the out of bounds area(s)
2305 * uptodate by copying data from the mft record to the page.
2307 if (!PageUptodate(page)) {
2309 memcpy(kaddr, kattr, from);
2311 memcpy(kaddr + to, kattr + to, attr_len - to);
2312 /* Zero the region outside the end of the attribute value. */
2313 if (attr_len < PAGE_CACHE_SIZE)
2314 memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
2316 * The probability of not having done any of the above is
2317 * extremely small, so we just flush unconditionally.
2319 flush_dcache_page(page);
2320 SetPageUptodate(page);
2322 kunmap_atomic(kaddr, KM_USER0);
2323 /* Update i_size if necessary. */
2324 if (i_size_read(vi) < attr_len) {
2325 unsigned long flags;
2327 write_lock_irqsave(&ni->size_lock, flags);
2328 ni->allocated_size = ni->initialized_size = attr_len;
2329 i_size_write(vi, attr_len);
2330 write_unlock_irqrestore(&ni->size_lock, flags);
2332 /* Mark the mft record dirty, so it gets written back. */
2333 flush_dcache_mft_record_page(ctx->ntfs_ino);
2334 mark_mft_record_dirty(ctx->ntfs_ino);
2335 ntfs_attr_put_search_ctx(ctx);
2336 unmap_mft_record(base_ni);
2337 ntfs_debug("Done.");
2340 if (err == -ENOMEM) {
2341 ntfs_warning(vi->i_sb, "Error allocating memory required to "
2342 "commit the write.");
2343 if (PageUptodate(page)) {
2344 ntfs_warning(vi->i_sb, "Page is uptodate, setting "
2345 "dirty so the write will be retried "
2346 "later on by the VM.");
2348 * Put the page on mapping->dirty_pages, but leave its
2349 * buffers' dirty state as-is.
2351 __set_page_dirty_nobuffers(page);
2354 ntfs_error(vi->i_sb, "Page is not uptodate. Written "
2355 "data has been lost.");
2357 ntfs_error(vi->i_sb, "Resident attribute commit write failed "
2358 "with error %i.", err);
2359 NVolSetErrors(ni->vol);
2363 ntfs_attr_put_search_ctx(ctx);
2365 unmap_mft_record(base_ni);
2369 #endif /* NTFS_RW */
2372 * ntfs_aops - general address space operations for inodes and attributes
2374 struct address_space_operations ntfs_aops = {
2375 .readpage = ntfs_readpage, /* Fill page with data. */
2376 .sync_page = block_sync_page, /* Currently, just unplugs the
2377 disk request queue. */
2379 .writepage = ntfs_writepage, /* Write dirty page to disk. */
2380 .prepare_write = ntfs_prepare_write, /* Prepare page and buffers
2381 ready to receive data. */
2382 .commit_write = ntfs_commit_write, /* Commit received data. */
2383 #endif /* NTFS_RW */
2387 * ntfs_mst_aops - general address space operations for mst protecteed inodes
2390 struct address_space_operations ntfs_mst_aops = {
2391 .readpage = ntfs_readpage, /* Fill page with data. */
2392 .sync_page = block_sync_page, /* Currently, just unplugs the
2393 disk request queue. */
2395 .writepage = ntfs_writepage, /* Write dirty page to disk. */
2396 .set_page_dirty = __set_page_dirty_nobuffers, /* Set the page dirty
2397 without touching the buffers
2398 belonging to the page. */
2399 #endif /* NTFS_RW */
2405 * mark_ntfs_record_dirty - mark an ntfs record dirty
2406 * @page: page containing the ntfs record to mark dirty
2407 * @ofs: byte offset within @page at which the ntfs record begins
2409 * Set the buffers and the page in which the ntfs record is located dirty.
2411 * The latter also marks the vfs inode the ntfs record belongs to dirty
2412 * (I_DIRTY_PAGES only).
2414 * If the page does not have buffers, we create them and set them uptodate.
2415 * The page may not be locked which is why we need to handle the buffers under
2416 * the mapping->private_lock. Once the buffers are marked dirty we no longer
2417 * need the lock since try_to_free_buffers() does not free dirty buffers.
2419 void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) {
2420 struct address_space *mapping = page->mapping;
2421 ntfs_inode *ni = NTFS_I(mapping->host);
2422 struct buffer_head *bh, *head, *buffers_to_free = NULL;
2423 unsigned int end, bh_size, bh_ofs;
2425 BUG_ON(!PageUptodate(page));
2426 end = ofs + ni->itype.index.block_size;
2427 bh_size = 1 << VFS_I(ni)->i_blkbits;
2428 spin_lock(&mapping->private_lock);
2429 if (unlikely(!page_has_buffers(page))) {
2430 spin_unlock(&mapping->private_lock);
2431 bh = head = alloc_page_buffers(page, bh_size, 1);
2432 spin_lock(&mapping->private_lock);
2433 if (likely(!page_has_buffers(page))) {
2434 struct buffer_head *tail;
2437 set_buffer_uptodate(bh);
2439 bh = bh->b_this_page;
2441 tail->b_this_page = head;
2442 attach_page_buffers(page, head);
2444 buffers_to_free = bh;
2446 bh = head = page_buffers(page);
2449 bh_ofs = bh_offset(bh);
2450 if (bh_ofs + bh_size <= ofs)
2452 if (unlikely(bh_ofs >= end))
2454 set_buffer_dirty(bh);
2455 } while ((bh = bh->b_this_page) != head);
2456 spin_unlock(&mapping->private_lock);
2457 __set_page_dirty_nobuffers(page);
2458 if (unlikely(buffers_to_free)) {
2460 bh = buffers_to_free->b_this_page;
2461 free_buffer_head(buffers_to_free);
2462 buffers_to_free = bh;
2463 } while (buffers_to_free);
2467 #endif /* NTFS_RW */