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;
63 int page_uptodate = 1;
66 ni = NTFS_I(page->mapping->host);
68 if (likely(uptodate)) {
69 s64 file_ofs, initialized_size;
71 set_buffer_uptodate(bh);
73 file_ofs = ((s64)page->index << PAGE_CACHE_SHIFT) +
75 read_lock_irqsave(&ni->size_lock, flags);
76 initialized_size = ni->initialized_size;
77 read_unlock_irqrestore(&ni->size_lock, flags);
78 /* Check for the current buffer head overflowing. */
79 if (file_ofs + bh->b_size > initialized_size) {
83 if (file_ofs < initialized_size)
84 ofs = initialized_size - file_ofs;
85 addr = kmap_atomic(page, KM_BIO_SRC_IRQ);
86 memset(addr + bh_offset(bh) + ofs, 0, bh->b_size - ofs);
87 flush_dcache_page(page);
88 kunmap_atomic(addr, KM_BIO_SRC_IRQ);
91 clear_buffer_uptodate(bh);
93 ntfs_error(ni->vol->sb, "Buffer I/O error, logical block %llu.",
94 (unsigned long long)bh->b_blocknr);
96 first = page_buffers(page);
97 local_irq_save(flags);
98 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
99 clear_buffer_async_read(bh);
103 if (!buffer_uptodate(tmp))
105 if (buffer_async_read(tmp)) {
106 if (likely(buffer_locked(tmp)))
108 /* Async buffers must be locked. */
111 tmp = tmp->b_this_page;
113 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
114 local_irq_restore(flags);
116 * If none of the buffers had errors then we can set the page uptodate,
117 * but we first have to perform the post read mst fixups, if the
118 * attribute is mst protected, i.e. if NInoMstProteced(ni) is true.
119 * Note we ignore fixup errors as those are detected when
120 * map_mft_record() is called which gives us per record granularity
121 * rather than per page granularity.
123 if (!NInoMstProtected(ni)) {
124 if (likely(page_uptodate && !PageError(page)))
125 SetPageUptodate(page);
128 unsigned int i, recs;
131 rec_size = ni->itype.index.block_size;
132 recs = PAGE_CACHE_SIZE / rec_size;
133 /* Should have been verified before we got here... */
135 addr = kmap_atomic(page, KM_BIO_SRC_IRQ);
136 for (i = 0; i < recs; i++)
137 post_read_mst_fixup((NTFS_RECORD*)(addr +
138 i * rec_size), rec_size);
139 flush_dcache_page(page);
140 kunmap_atomic(addr, KM_BIO_SRC_IRQ);
141 if (likely(page_uptodate && !PageError(page)))
142 SetPageUptodate(page);
147 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
148 local_irq_restore(flags);
153 * ntfs_read_block - fill a @page of an address space with data
154 * @page: page cache page to fill with data
156 * Fill the page @page of the address space belonging to the @page->host inode.
157 * We read each buffer asynchronously and when all buffers are read in, our io
158 * completion handler ntfs_end_buffer_read_async(), if required, automatically
159 * applies the mst fixups to the page before finally marking it uptodate and
162 * We only enforce allocated_size limit because i_size is checked for in
163 * generic_file_read().
165 * Return 0 on success and -errno on error.
167 * Contains an adapted version of fs/buffer.c::block_read_full_page().
169 static int ntfs_read_block(struct page *page)
176 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
177 sector_t iblock, lblock, zblock;
179 unsigned int blocksize, vcn_ofs;
181 unsigned char blocksize_bits;
183 ni = NTFS_I(page->mapping->host);
186 /* $MFT/$DATA must have its complete runlist in memory at all times. */
187 BUG_ON(!ni->runlist.rl && !ni->mft_no && !NInoAttr(ni));
189 blocksize_bits = VFS_I(ni)->i_blkbits;
190 blocksize = 1 << blocksize_bits;
192 if (!page_has_buffers(page)) {
193 create_empty_buffers(page, blocksize, 0);
194 if (unlikely(!page_has_buffers(page))) {
199 bh = head = page_buffers(page);
202 iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
203 read_lock_irqsave(&ni->size_lock, flags);
204 lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits;
205 zblock = (ni->initialized_size + blocksize - 1) >> blocksize_bits;
206 read_unlock_irqrestore(&ni->size_lock, flags);
208 /* Loop through all the buffers in the page. */
215 if (unlikely(buffer_uptodate(bh)))
217 if (unlikely(buffer_mapped(bh))) {
222 bh->b_bdev = vol->sb->s_bdev;
223 /* Is the block within the allowed limits? */
224 if (iblock < lblock) {
225 BOOL is_retry = FALSE;
227 /* Convert iblock into corresponding vcn and offset. */
228 vcn = (VCN)iblock << blocksize_bits >>
229 vol->cluster_size_bits;
230 vcn_ofs = ((VCN)iblock << blocksize_bits) &
231 vol->cluster_size_mask;
234 down_read(&ni->runlist.lock);
237 if (likely(rl != NULL)) {
238 /* Seek to element containing target vcn. */
239 while (rl->length && rl[1].vcn <= vcn)
241 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
243 lcn = LCN_RL_NOT_MAPPED;
244 /* Successful remap. */
246 /* Setup buffer head to correct block. */
247 bh->b_blocknr = ((lcn << vol->cluster_size_bits)
248 + vcn_ofs) >> blocksize_bits;
249 set_buffer_mapped(bh);
250 /* Only read initialized data blocks. */
251 if (iblock < zblock) {
255 /* Fully non-initialized data block, zero it. */
258 /* It is a hole, need to zero it. */
261 /* If first try and runlist unmapped, map and retry. */
262 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
265 * Attempt to map runlist, dropping lock for
268 up_read(&ni->runlist.lock);
269 err = ntfs_map_runlist(ni, vcn);
271 goto lock_retry_remap;
274 up_read(&ni->runlist.lock);
276 * If buffer is outside the runlist, treat it as a
277 * hole. This can happen due to concurrent truncate
280 if (err == -ENOENT || lcn == LCN_ENOENT) {
284 /* Hard error, zero out region. */
289 ntfs_error(vol->sb, "Failed to read from inode 0x%lx, "
290 "attribute type 0x%x, vcn 0x%llx, "
291 "offset 0x%x because its location on "
292 "disk could not be determined%s "
293 "(error code %i).", ni->mft_no,
294 ni->type, (unsigned long long)vcn,
295 vcn_ofs, is_retry ? " even after "
296 "retrying" : "", err);
299 * Either iblock was outside lblock limits or
300 * ntfs_rl_vcn_to_lcn() returned error. Just zero that portion
301 * of the page and set the buffer uptodate.
304 bh->b_blocknr = -1UL;
305 clear_buffer_mapped(bh);
307 kaddr = kmap_atomic(page, KM_USER0);
308 memset(kaddr + i * blocksize, 0, blocksize);
309 kunmap_atomic(kaddr, KM_USER0);
310 flush_dcache_page(page);
312 set_buffer_uptodate(bh);
313 } while (i++, iblock++, (bh = bh->b_this_page) != head);
315 /* Release the lock if we took it. */
317 up_read(&ni->runlist.lock);
319 /* Check we have at least one buffer ready for i/o. */
321 struct buffer_head *tbh;
323 /* Lock the buffers. */
324 for (i = 0; i < nr; i++) {
327 tbh->b_end_io = ntfs_end_buffer_async_read;
328 set_buffer_async_read(tbh);
330 /* Finally, start i/o on the buffers. */
331 for (i = 0; i < nr; i++) {
333 if (likely(!buffer_uptodate(tbh)))
334 submit_bh(READ, tbh);
336 ntfs_end_buffer_async_read(tbh, 1);
340 /* No i/o was scheduled on any of the buffers. */
341 if (likely(!PageError(page)))
342 SetPageUptodate(page);
343 else /* Signal synchronous i/o error. */
350 * ntfs_readpage - fill a @page of a @file with data from the device
351 * @file: open file to which the page @page belongs or NULL
352 * @page: page cache page to fill with data
354 * For non-resident attributes, ntfs_readpage() fills the @page of the open
355 * file @file by calling the ntfs version of the generic block_read_full_page()
356 * function, ntfs_read_block(), which in turn creates and reads in the buffers
357 * associated with the page asynchronously.
359 * For resident attributes, OTOH, ntfs_readpage() fills @page by copying the
360 * data from the mft record (which at this stage is most likely in memory) and
361 * fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as
362 * even if the mft record is not cached at this point in time, we need to wait
363 * for it to be read in before we can do the copy.
365 * Return 0 on success and -errno on error.
367 static int ntfs_readpage(struct file *file, struct page *page)
369 ntfs_inode *ni, *base_ni;
371 ntfs_attr_search_ctx *ctx;
378 BUG_ON(!PageLocked(page));
380 * This can potentially happen because we clear PageUptodate() during
381 * ntfs_writepage() of MstProtected() attributes.
383 if (PageUptodate(page)) {
387 ni = NTFS_I(page->mapping->host);
389 * Only $DATA attributes can be encrypted and only unnamed $DATA
390 * attributes can be compressed. Index root can have the flags set but
391 * this means to create compressed/encrypted files, not that the
392 * attribute is compressed/encrypted. Note we need to check for
393 * AT_INDEX_ALLOCATION since this is the type of both directory and
396 if (ni->type != AT_INDEX_ALLOCATION) {
397 /* If attribute is encrypted, deny access, just like NT4. */
398 if (NInoEncrypted(ni)) {
399 BUG_ON(ni->type != AT_DATA);
403 /* Compressed data streams are handled in compress.c. */
404 if (NInoNonResident(ni) && NInoCompressed(ni)) {
405 BUG_ON(ni->type != AT_DATA);
406 BUG_ON(ni->name_len);
407 return ntfs_read_compressed_block(page);
410 /* NInoNonResident() == NInoIndexAllocPresent() */
411 if (NInoNonResident(ni)) {
412 /* Normal, non-resident data stream. */
413 return ntfs_read_block(page);
416 * Attribute is resident, implying it is not compressed or encrypted.
417 * This also means the attribute is smaller than an mft record and
418 * hence smaller than a page, so can simply zero out any pages with
419 * index above 0. Note the attribute can actually be marked compressed
420 * but if it is resident the actual data is not compressed so we are
421 * ok to ignore the compressed flag here.
423 if (unlikely(page->index > 0)) {
424 kaddr = kmap_atomic(page, KM_USER0);
425 memset(kaddr, 0, PAGE_CACHE_SIZE);
426 flush_dcache_page(page);
427 kunmap_atomic(kaddr, KM_USER0);
433 base_ni = ni->ext.base_ntfs_ino;
434 /* Map, pin, and lock the mft record. */
435 mrec = map_mft_record(base_ni);
441 * If a parallel write made the attribute non-resident, drop the mft
442 * record and retry the readpage.
444 if (unlikely(NInoNonResident(ni))) {
445 unmap_mft_record(base_ni);
448 ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
449 if (unlikely(!ctx)) {
453 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
454 CASE_SENSITIVE, 0, NULL, 0, ctx);
456 goto put_unm_err_out;
457 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
458 read_lock_irqsave(&ni->size_lock, flags);
459 if (unlikely(attr_len > ni->initialized_size))
460 attr_len = ni->initialized_size;
461 read_unlock_irqrestore(&ni->size_lock, flags);
462 kaddr = kmap_atomic(page, KM_USER0);
463 /* Copy the data to the page. */
464 memcpy(kaddr, (u8*)ctx->attr +
465 le16_to_cpu(ctx->attr->data.resident.value_offset),
467 /* Zero the remainder of the page. */
468 memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
469 flush_dcache_page(page);
470 kunmap_atomic(kaddr, KM_USER0);
472 ntfs_attr_put_search_ctx(ctx);
474 unmap_mft_record(base_ni);
476 SetPageUptodate(page);
485 * ntfs_write_block - write a @page to the backing store
486 * @page: page cache page to write out
487 * @wbc: writeback control structure
489 * This function is for writing pages belonging to non-resident, non-mst
490 * protected attributes to their backing store.
492 * For a page with buffers, map and write the dirty buffers asynchronously
493 * under page writeback. For a page without buffers, create buffers for the
494 * page, then proceed as above.
496 * If a page doesn't have buffers the page dirty state is definitive. If a page
497 * does have buffers, the page dirty state is just a hint, and the buffer dirty
498 * state is definitive. (A hint which has rules: dirty buffers against a clean
499 * page is illegal. Other combinations are legal and need to be handled. In
500 * particular a dirty page containing clean buffers for example.)
502 * Return 0 on success and -errno on error.
504 * Based on ntfs_read_block() and __block_write_full_page().
506 static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
510 s64 initialized_size;
512 sector_t block, dblock, iblock;
517 struct buffer_head *bh, *head;
519 unsigned int blocksize, vcn_ofs;
521 BOOL need_end_writeback;
522 unsigned char blocksize_bits;
524 vi = page->mapping->host;
528 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
529 "0x%lx.", ni->mft_no, ni->type, page->index);
531 BUG_ON(!NInoNonResident(ni));
532 BUG_ON(NInoMstProtected(ni));
534 blocksize_bits = vi->i_blkbits;
535 blocksize = 1 << blocksize_bits;
537 if (!page_has_buffers(page)) {
538 BUG_ON(!PageUptodate(page));
539 create_empty_buffers(page, blocksize,
540 (1 << BH_Uptodate) | (1 << BH_Dirty));
541 if (unlikely(!page_has_buffers(page))) {
542 ntfs_warning(vol->sb, "Error allocating page "
543 "buffers. Redirtying page so we try "
546 * Put the page back on mapping->dirty_pages, but leave
547 * its buffers' dirty state as-is.
549 redirty_page_for_writepage(wbc, page);
554 bh = head = page_buffers(page);
557 /* NOTE: Different naming scheme to ntfs_read_block()! */
559 /* The first block in the page. */
560 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
562 read_lock_irqsave(&ni->size_lock, flags);
563 i_size = i_size_read(vi);
564 initialized_size = ni->initialized_size;
565 read_unlock_irqrestore(&ni->size_lock, flags);
567 /* The first out of bounds block for the data size. */
568 dblock = (i_size + blocksize - 1) >> blocksize_bits;
570 /* The last (fully or partially) initialized block. */
571 iblock = initialized_size >> blocksize_bits;
574 * Be very careful. We have no exclusion from __set_page_dirty_buffers
575 * here, and the (potentially unmapped) buffers may become dirty at
576 * any time. If a buffer becomes dirty here after we've inspected it
577 * then we just miss that fact, and the page stays dirty.
579 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
580 * handle that here by just cleaning them.
584 * Loop through all the buffers in the page, mapping all the dirty
585 * buffers to disk addresses and handling any aliases from the
586 * underlying block device's mapping.
591 BOOL is_retry = FALSE;
593 if (unlikely(block >= dblock)) {
595 * Mapped buffers outside i_size will occur, because
596 * this page can be outside i_size when there is a
597 * truncate in progress. The contents of such buffers
598 * were zeroed by ntfs_writepage().
600 * FIXME: What about the small race window where
601 * ntfs_writepage() has not done any clearing because
602 * the page was within i_size but before we get here,
603 * vmtruncate() modifies i_size?
605 clear_buffer_dirty(bh);
606 set_buffer_uptodate(bh);
610 /* Clean buffers are not written out, so no need to map them. */
611 if (!buffer_dirty(bh))
614 /* Make sure we have enough initialized size. */
615 if (unlikely((block >= iblock) &&
616 (initialized_size < i_size))) {
618 * If this page is fully outside initialized size, zero
619 * out all pages between the current initialized size
620 * and the current page. Just use ntfs_readpage() to do
621 * the zeroing transparently.
623 if (block > iblock) {
626 // - read_cache_page()
627 // Again for each page do:
628 // - wait_on_page_locked()
629 // - Check (PageUptodate(page) &&
631 // Update initialized size in the attribute and
633 // Again, for each page do:
634 // __set_page_dirty_buffers();
635 // page_cache_release()
636 // We don't need to wait on the writes.
640 * The current page straddles initialized size. Zero
641 * all non-uptodate buffers and set them uptodate (and
642 * dirty?). Note, there aren't any non-uptodate buffers
643 * if the page is uptodate.
644 * FIXME: For an uptodate page, the buffers may need to
645 * be written out because they were not initialized on
648 if (!PageUptodate(page)) {
650 // Zero any non-uptodate buffers up to i_size.
651 // Set them uptodate and dirty.
654 // Update initialized size in the attribute and in the
655 // inode (up to i_size).
657 // FIXME: This is inefficient. Try to batch the two
658 // size changes to happen in one go.
659 ntfs_error(vol->sb, "Writing beyond initialized size "
660 "is not supported yet. Sorry.");
663 // Do NOT set_buffer_new() BUT DO clear buffer range
664 // outside write request range.
665 // set_buffer_uptodate() on complete buffers as well as
666 // set_buffer_dirty().
669 /* No need to map buffers that are already mapped. */
670 if (buffer_mapped(bh))
673 /* Unmapped, dirty buffer. Need to map it. */
674 bh->b_bdev = vol->sb->s_bdev;
676 /* Convert block into corresponding vcn and offset. */
677 vcn = (VCN)block << blocksize_bits;
678 vcn_ofs = vcn & vol->cluster_size_mask;
679 vcn >>= vol->cluster_size_bits;
682 down_read(&ni->runlist.lock);
685 if (likely(rl != NULL)) {
686 /* Seek to element containing target vcn. */
687 while (rl->length && rl[1].vcn <= vcn)
689 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
691 lcn = LCN_RL_NOT_MAPPED;
692 /* Successful remap. */
694 /* Setup buffer head to point to correct block. */
695 bh->b_blocknr = ((lcn << vol->cluster_size_bits) +
696 vcn_ofs) >> blocksize_bits;
697 set_buffer_mapped(bh);
700 /* It is a hole, need to instantiate it. */
701 if (lcn == LCN_HOLE) {
703 unsigned long *bpos, *bend;
705 /* Check if the buffer is zero. */
706 kaddr = kmap_atomic(page, KM_USER0);
707 bpos = (unsigned long *)(kaddr + bh_offset(bh));
708 bend = (unsigned long *)((u8*)bpos + blocksize);
712 } while (likely(++bpos < bend));
713 kunmap_atomic(kaddr, KM_USER0);
716 * Buffer is zero and sparse, no need to write
720 clear_buffer_dirty(bh);
723 // TODO: Instantiate the hole.
724 // clear_buffer_new(bh);
725 // unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
726 ntfs_error(vol->sb, "Writing into sparse regions is "
727 "not supported yet. Sorry.");
731 /* If first try and runlist unmapped, map and retry. */
732 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
735 * Attempt to map runlist, dropping lock for
738 up_read(&ni->runlist.lock);
739 err = ntfs_map_runlist(ni, vcn);
741 goto lock_retry_remap;
744 up_read(&ni->runlist.lock);
746 * If buffer is outside the runlist, truncate has cut it out
747 * of the runlist. Just clean and clear the buffer and set it
748 * uptodate so it can get discarded by the VM.
750 if (err == -ENOENT || lcn == LCN_ENOENT) {
754 clear_buffer_dirty(bh);
755 kaddr = kmap_atomic(page, KM_USER0);
756 memset(kaddr + bh_offset(bh), 0, blocksize);
757 kunmap_atomic(kaddr, KM_USER0);
758 flush_dcache_page(page);
759 set_buffer_uptodate(bh);
763 /* Failed to map the buffer, even after retrying. */
767 ntfs_error(vol->sb, "Failed to write to inode 0x%lx, "
768 "attribute type 0x%x, vcn 0x%llx, offset 0x%x "
769 "because its location on disk could not be "
770 "determined%s (error code %i).", ni->mft_no,
771 ni->type, (unsigned long long)vcn,
772 vcn_ofs, is_retry ? " even after "
773 "retrying" : "", err);
775 } while (block++, (bh = bh->b_this_page) != head);
777 /* Release the lock if we took it. */
779 up_read(&ni->runlist.lock);
781 /* For the error case, need to reset bh to the beginning. */
784 /* Just an optimization, so ->readpage() is not called later. */
785 if (unlikely(!PageUptodate(page))) {
788 if (!buffer_uptodate(bh)) {
793 } while ((bh = bh->b_this_page) != head);
795 SetPageUptodate(page);
798 /* Setup all mapped, dirty buffers for async write i/o. */
800 if (buffer_mapped(bh) && buffer_dirty(bh)) {
802 if (test_clear_buffer_dirty(bh)) {
803 BUG_ON(!buffer_uptodate(bh));
804 mark_buffer_async_write(bh);
807 } else if (unlikely(err)) {
809 * For the error case. The buffer may have been set
810 * dirty during attachment to a dirty page.
813 clear_buffer_dirty(bh);
815 } while ((bh = bh->b_this_page) != head);
818 // TODO: Remove the -EOPNOTSUPP check later on...
819 if (unlikely(err == -EOPNOTSUPP))
821 else if (err == -ENOMEM) {
822 ntfs_warning(vol->sb, "Error allocating memory. "
823 "Redirtying page so we try again "
826 * Put the page back on mapping->dirty_pages, but
827 * leave its buffer's dirty state as-is.
829 redirty_page_for_writepage(wbc, page);
835 BUG_ON(PageWriteback(page));
836 set_page_writeback(page); /* Keeps try_to_free_buffers() away. */
838 /* Submit the prepared buffers for i/o. */
839 need_end_writeback = TRUE;
841 struct buffer_head *next = bh->b_this_page;
842 if (buffer_async_write(bh)) {
843 submit_bh(WRITE, bh);
844 need_end_writeback = FALSE;
847 } while (bh != head);
850 /* If no i/o was started, need to end_page_writeback(). */
851 if (unlikely(need_end_writeback))
852 end_page_writeback(page);
859 * ntfs_write_mst_block - write a @page to the backing store
860 * @page: page cache page to write out
861 * @wbc: writeback control structure
863 * This function is for writing pages belonging to non-resident, mst protected
864 * attributes to their backing store. The only supported attributes are index
865 * allocation and $MFT/$DATA. Both directory inodes and index inodes are
866 * supported for the index allocation case.
868 * The page must remain locked for the duration of the write because we apply
869 * the mst fixups, write, and then undo the fixups, so if we were to unlock the
870 * page before undoing the fixups, any other user of the page will see the
871 * page contents as corrupt.
873 * We clear the page uptodate flag for the duration of the function to ensure
874 * exclusion for the $MFT/$DATA case against someone mapping an mft record we
875 * are about to apply the mst fixups to.
877 * Return 0 on success and -errno on error.
879 * Based on ntfs_write_block(), ntfs_mft_writepage(), and
880 * write_mft_record_nolock().
882 static int ntfs_write_mst_block(struct page *page,
883 struct writeback_control *wbc)
885 sector_t block, dblock, rec_block;
886 struct inode *vi = page->mapping->host;
887 ntfs_inode *ni = NTFS_I(vi);
888 ntfs_volume *vol = ni->vol;
890 unsigned int rec_size = ni->itype.index.block_size;
891 ntfs_inode *locked_nis[PAGE_CACHE_SIZE / rec_size];
892 struct buffer_head *bh, *head, *tbh, *rec_start_bh;
893 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
895 int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec, err, err2;
896 unsigned bh_size, rec_size_bits;
897 BOOL sync, is_mft, page_is_dirty, rec_is_dirty;
898 unsigned char bh_size_bits;
900 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
901 "0x%lx.", vi->i_ino, ni->type, page->index);
902 BUG_ON(!NInoNonResident(ni));
903 BUG_ON(!NInoMstProtected(ni));
904 is_mft = (S_ISREG(vi->i_mode) && !vi->i_ino);
906 * NOTE: ntfs_write_mst_block() would be called for $MFTMirr if a page
907 * in its page cache were to be marked dirty. However this should
908 * never happen with the current driver and considering we do not
909 * handle this case here we do want to BUG(), at least for now.
911 BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) ||
912 (NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION)));
913 bh_size_bits = vi->i_blkbits;
914 bh_size = 1 << bh_size_bits;
915 max_bhs = PAGE_CACHE_SIZE / bh_size;
917 BUG_ON(max_bhs > MAX_BUF_PER_PAGE);
919 /* Were we called for sync purposes? */
920 sync = (wbc->sync_mode == WB_SYNC_ALL);
922 /* Make sure we have mapped buffers. */
923 bh = head = page_buffers(page);
926 rec_size_bits = ni->itype.index.block_size_bits;
927 BUG_ON(!(PAGE_CACHE_SIZE >> rec_size_bits));
928 bhs_per_rec = rec_size >> bh_size_bits;
929 BUG_ON(!bhs_per_rec);
931 /* The first block in the page. */
932 rec_block = block = (sector_t)page->index <<
933 (PAGE_CACHE_SHIFT - bh_size_bits);
935 /* The first out of bounds block for the data size. */
936 dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
939 err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
940 page_is_dirty = rec_is_dirty = FALSE;
943 BOOL is_retry = FALSE;
945 if (likely(block < rec_block)) {
946 if (unlikely(block >= dblock)) {
947 clear_buffer_dirty(bh);
948 set_buffer_uptodate(bh);
952 * This block is not the first one in the record. We
953 * ignore the buffer's dirty state because we could
954 * have raced with a parallel mark_ntfs_record_dirty().
958 if (unlikely(err2)) {
960 clear_buffer_dirty(bh);
963 } else /* if (block == rec_block) */ {
964 BUG_ON(block > rec_block);
965 /* This block is the first one in the record. */
966 rec_block += bhs_per_rec;
968 if (unlikely(block >= dblock)) {
969 clear_buffer_dirty(bh);
972 if (!buffer_dirty(bh)) {
973 /* Clean records are not written out. */
974 rec_is_dirty = FALSE;
980 /* Need to map the buffer if it is not mapped already. */
981 if (unlikely(!buffer_mapped(bh))) {
984 unsigned int vcn_ofs;
986 bh->b_bdev = vol->sb->s_bdev;
987 /* Obtain the vcn and offset of the current block. */
988 vcn = (VCN)block << bh_size_bits;
989 vcn_ofs = vcn & vol->cluster_size_mask;
990 vcn >>= vol->cluster_size_bits;
993 down_read(&ni->runlist.lock);
996 if (likely(rl != NULL)) {
997 /* Seek to element containing target vcn. */
998 while (rl->length && rl[1].vcn <= vcn)
1000 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
1002 lcn = LCN_RL_NOT_MAPPED;
1003 /* Successful remap. */
1004 if (likely(lcn >= 0)) {
1005 /* Setup buffer head to correct block. */
1006 bh->b_blocknr = ((lcn <<
1007 vol->cluster_size_bits) +
1008 vcn_ofs) >> bh_size_bits;
1009 set_buffer_mapped(bh);
1012 * Remap failed. Retry to map the runlist once
1013 * unless we are working on $MFT which always
1014 * has the whole of its runlist in memory.
1016 if (!is_mft && !is_retry &&
1017 lcn == LCN_RL_NOT_MAPPED) {
1020 * Attempt to map runlist, dropping
1021 * lock for the duration.
1023 up_read(&ni->runlist.lock);
1024 err2 = ntfs_map_runlist(ni, vcn);
1026 goto lock_retry_remap;
1027 if (err2 == -ENOMEM)
1028 page_is_dirty = TRUE;
1033 up_read(&ni->runlist.lock);
1035 /* Hard error. Abort writing this record. */
1036 if (!err || err == -ENOMEM)
1039 ntfs_error(vol->sb, "Cannot write ntfs record "
1040 "0x%llx (inode 0x%lx, "
1041 "attribute type 0x%x) because "
1042 "its location on disk could "
1043 "not be determined (error "
1047 vol->mft_record_size_bits,
1048 ni->mft_no, ni->type,
1051 * If this is not the first buffer, remove the
1052 * buffers in this record from the list of
1053 * buffers to write and clear their dirty bit
1054 * if not error -ENOMEM.
1056 if (rec_start_bh != bh) {
1057 while (bhs[--nr_bhs] != rec_start_bh)
1059 if (err2 != -ENOMEM) {
1063 } while ((rec_start_bh =
1072 BUG_ON(!buffer_uptodate(bh));
1073 BUG_ON(nr_bhs >= max_bhs);
1075 } while (block++, (bh = bh->b_this_page) != head);
1077 up_read(&ni->runlist.lock);
1078 /* If there were no dirty buffers, we are done. */
1081 /* Map the page so we can access its contents. */
1083 /* Clear the page uptodate flag whilst the mst fixups are applied. */
1084 BUG_ON(!PageUptodate(page));
1085 ClearPageUptodate(page);
1086 for (i = 0; i < nr_bhs; i++) {
1089 /* Skip buffers which are not at the beginning of records. */
1090 if (i % bhs_per_rec)
1093 ofs = bh_offset(tbh);
1096 unsigned long mft_no;
1098 /* Get the mft record number. */
1099 mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
1101 /* Check whether to write this mft record. */
1103 if (!ntfs_may_write_mft_record(vol, mft_no,
1104 (MFT_RECORD*)(kaddr + ofs), &tni)) {
1106 * The record should not be written. This
1107 * means we need to redirty the page before
1110 page_is_dirty = TRUE;
1112 * Remove the buffers in this mft record from
1113 * the list of buffers to write.
1117 } while (++i % bhs_per_rec);
1121 * The record should be written. If a locked ntfs
1122 * inode was returned, add it to the array of locked
1126 locked_nis[nr_locked_nis++] = tni;
1128 /* Apply the mst protection fixups. */
1129 err2 = pre_write_mst_fixup((NTFS_RECORD*)(kaddr + ofs),
1131 if (unlikely(err2)) {
1132 if (!err || err == -ENOMEM)
1134 ntfs_error(vol->sb, "Failed to apply mst fixups "
1135 "(inode 0x%lx, attribute type 0x%x, "
1136 "page index 0x%lx, page offset 0x%x)!"
1137 " Unmount and run chkdsk.", vi->i_ino,
1138 ni->type, page->index, ofs);
1140 * Mark all the buffers in this record clean as we do
1141 * not want to write corrupt data to disk.
1144 clear_buffer_dirty(bhs[i]);
1146 } while (++i % bhs_per_rec);
1151 /* If no records are to be written out, we are done. */
1154 flush_dcache_page(page);
1155 /* Lock buffers and start synchronous write i/o on them. */
1156 for (i = 0; i < nr_bhs; i++) {
1160 if (unlikely(test_set_buffer_locked(tbh)))
1162 /* The buffer dirty state is now irrelevant, just clean it. */
1163 clear_buffer_dirty(tbh);
1164 BUG_ON(!buffer_uptodate(tbh));
1165 BUG_ON(!buffer_mapped(tbh));
1167 tbh->b_end_io = end_buffer_write_sync;
1168 submit_bh(WRITE, tbh);
1170 /* Synchronize the mft mirror now if not @sync. */
1171 if (is_mft && !sync)
1174 /* Wait on i/o completion of buffers. */
1175 for (i = 0; i < nr_bhs; i++) {
1179 wait_on_buffer(tbh);
1180 if (unlikely(!buffer_uptodate(tbh))) {
1181 ntfs_error(vol->sb, "I/O error while writing ntfs "
1182 "record buffer (inode 0x%lx, "
1183 "attribute type 0x%x, page index "
1184 "0x%lx, page offset 0x%lx)! Unmount "
1185 "and run chkdsk.", vi->i_ino, ni->type,
1186 page->index, bh_offset(tbh));
1187 if (!err || err == -ENOMEM)
1190 * Set the buffer uptodate so the page and buffer
1191 * states do not become out of sync.
1193 set_buffer_uptodate(tbh);
1196 /* If @sync, now synchronize the mft mirror. */
1197 if (is_mft && sync) {
1199 for (i = 0; i < nr_bhs; i++) {
1200 unsigned long mft_no;
1204 * Skip buffers which are not at the beginning of
1207 if (i % bhs_per_rec)
1210 /* Skip removed buffers (and hence records). */
1213 ofs = bh_offset(tbh);
1214 /* Get the mft record number. */
1215 mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
1217 if (mft_no < vol->mftmirr_size)
1218 ntfs_sync_mft_mirror(vol, mft_no,
1219 (MFT_RECORD*)(kaddr + ofs),
1225 /* Remove the mst protection fixups again. */
1226 for (i = 0; i < nr_bhs; i++) {
1227 if (!(i % bhs_per_rec)) {
1231 post_write_mst_fixup((NTFS_RECORD*)(kaddr +
1235 flush_dcache_page(page);
1237 /* Unlock any locked inodes. */
1238 while (nr_locked_nis-- > 0) {
1239 ntfs_inode *tni, *base_tni;
1241 tni = locked_nis[nr_locked_nis];
1242 /* Get the base inode. */
1243 down(&tni->extent_lock);
1244 if (tni->nr_extents >= 0)
1247 base_tni = tni->ext.base_ntfs_ino;
1250 up(&tni->extent_lock);
1251 ntfs_debug("Unlocking %s inode 0x%lx.",
1252 tni == base_tni ? "base" : "extent",
1254 up(&tni->mrec_lock);
1255 atomic_dec(&tni->count);
1256 iput(VFS_I(base_tni));
1258 SetPageUptodate(page);
1261 if (unlikely(err && err != -ENOMEM)) {
1263 * Set page error if there is only one ntfs record in the page.
1264 * Otherwise we would loose per-record granularity.
1266 if (ni->itype.index.block_size == PAGE_CACHE_SIZE)
1270 if (page_is_dirty) {
1271 ntfs_debug("Page still contains one or more dirty ntfs "
1272 "records. Redirtying the page starting at "
1273 "record 0x%lx.", page->index <<
1274 (PAGE_CACHE_SHIFT - rec_size_bits));
1275 redirty_page_for_writepage(wbc, page);
1279 * Keep the VM happy. This must be done otherwise the
1280 * radix-tree tag PAGECACHE_TAG_DIRTY remains set even though
1281 * the page is clean.
1283 BUG_ON(PageWriteback(page));
1284 set_page_writeback(page);
1286 end_page_writeback(page);
1289 ntfs_debug("Done.");
1294 * ntfs_writepage - write a @page to the backing store
1295 * @page: page cache page to write out
1296 * @wbc: writeback control structure
1298 * This is called from the VM when it wants to have a dirty ntfs page cache
1299 * page cleaned. The VM has already locked the page and marked it clean.
1301 * For non-resident attributes, ntfs_writepage() writes the @page by calling
1302 * the ntfs version of the generic block_write_full_page() function,
1303 * ntfs_write_block(), which in turn if necessary creates and writes the
1304 * buffers associated with the page asynchronously.
1306 * For resident attributes, OTOH, ntfs_writepage() writes the @page by copying
1307 * the data to the mft record (which at this stage is most likely in memory).
1308 * The mft record is then marked dirty and written out asynchronously via the
1309 * vfs inode dirty code path for the inode the mft record belongs to or via the
1310 * vm page dirty code path for the page the mft record is in.
1312 * Based on ntfs_readpage() and fs/buffer.c::block_write_full_page().
1314 * Return 0 on success and -errno on error.
1316 static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1319 struct inode *vi = page->mapping->host;
1320 ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
1322 ntfs_attr_search_ctx *ctx = NULL;
1323 MFT_RECORD *m = NULL;
1328 BUG_ON(!PageLocked(page));
1329 i_size = i_size_read(vi);
1330 /* Is the page fully outside i_size? (truncate in progress) */
1331 if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >>
1332 PAGE_CACHE_SHIFT)) {
1334 * The page may have dirty, unmapped buffers. Make them
1335 * freeable here, so the page does not leak.
1337 block_invalidatepage(page, 0);
1339 ntfs_debug("Write outside i_size - truncated?");
1343 * Only $DATA attributes can be encrypted and only unnamed $DATA
1344 * attributes can be compressed. Index root can have the flags set but
1345 * this means to create compressed/encrypted files, not that the
1346 * attribute is compressed/encrypted. Note we need to check for
1347 * AT_INDEX_ALLOCATION since this is the type of both directory and
1350 if (ni->type != AT_INDEX_ALLOCATION) {
1351 /* If file is encrypted, deny access, just like NT4. */
1352 if (NInoEncrypted(ni)) {
1354 BUG_ON(ni->type != AT_DATA);
1355 ntfs_debug("Denying write access to encrypted "
1359 /* Compressed data streams are handled in compress.c. */
1360 if (NInoNonResident(ni) && NInoCompressed(ni)) {
1361 BUG_ON(ni->type != AT_DATA);
1362 BUG_ON(ni->name_len);
1363 // TODO: Implement and replace this with
1364 // return ntfs_write_compressed_block(page);
1366 ntfs_error(vi->i_sb, "Writing to compressed files is "
1367 "not supported yet. Sorry.");
1370 // TODO: Implement and remove this check.
1371 if (NInoNonResident(ni) && NInoSparse(ni)) {
1373 ntfs_error(vi->i_sb, "Writing to sparse files is not "
1374 "supported yet. Sorry.");
1378 /* NInoNonResident() == NInoIndexAllocPresent() */
1379 if (NInoNonResident(ni)) {
1380 /* We have to zero every time due to mmap-at-end-of-file. */
1381 if (page->index >= (i_size >> PAGE_CACHE_SHIFT)) {
1382 /* The page straddles i_size. */
1383 unsigned int ofs = i_size & ~PAGE_CACHE_MASK;
1384 kaddr = kmap_atomic(page, KM_USER0);
1385 memset(kaddr + ofs, 0, PAGE_CACHE_SIZE - ofs);
1386 flush_dcache_page(page);
1387 kunmap_atomic(kaddr, KM_USER0);
1389 /* Handle mst protected attributes. */
1390 if (NInoMstProtected(ni))
1391 return ntfs_write_mst_block(page, wbc);
1392 /* Normal, non-resident data stream. */
1393 return ntfs_write_block(page, wbc);
1396 * Attribute is resident, implying it is not compressed, encrypted, or
1397 * mst protected. This also means the attribute is smaller than an mft
1398 * record and hence smaller than a page, so can simply return error on
1399 * any pages with index above 0. Note the attribute can actually be
1400 * marked compressed but if it is resident the actual data is not
1401 * compressed so we are ok to ignore the compressed flag here.
1403 BUG_ON(page_has_buffers(page));
1404 BUG_ON(!PageUptodate(page));
1405 if (unlikely(page->index > 0)) {
1406 ntfs_error(vi->i_sb, "BUG()! page->index (0x%lx) > 0. "
1407 "Aborting write.", page->index);
1408 BUG_ON(PageWriteback(page));
1409 set_page_writeback(page);
1411 end_page_writeback(page);
1417 base_ni = ni->ext.base_ntfs_ino;
1418 /* Map, pin, and lock the mft record. */
1419 m = map_mft_record(base_ni);
1427 * If a parallel write made the attribute non-resident, drop the mft
1428 * record and retry the writepage.
1430 if (unlikely(NInoNonResident(ni))) {
1431 unmap_mft_record(base_ni);
1432 goto retry_writepage;
1434 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1435 if (unlikely(!ctx)) {
1439 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1440 CASE_SENSITIVE, 0, NULL, 0, ctx);
1444 * Keep the VM happy. This must be done otherwise the radix-tree tag
1445 * PAGECACHE_TAG_DIRTY remains set even though the page is clean.
1447 BUG_ON(PageWriteback(page));
1448 set_page_writeback(page);
1451 * Here, we do not need to zero the out of bounds area everytime
1452 * because the below memcpy() already takes care of the
1453 * mmap-at-end-of-file requirements. If the file is converted to a
1454 * non-resident one, then the code path use is switched to the
1455 * non-resident one where the zeroing happens on each ntfs_writepage()
1458 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
1459 i_size = i_size_read(vi);
1460 if (unlikely(attr_len > i_size)) {
1462 ctx->attr->data.resident.value_length = cpu_to_le32(attr_len);
1464 kaddr = kmap_atomic(page, KM_USER0);
1465 /* Copy the data from the page to the mft record. */
1466 memcpy((u8*)ctx->attr +
1467 le16_to_cpu(ctx->attr->data.resident.value_offset),
1469 flush_dcache_mft_record_page(ctx->ntfs_ino);
1470 /* Zero out of bounds area in the page cache page. */
1471 memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
1472 flush_dcache_page(page);
1473 kunmap_atomic(kaddr, KM_USER0);
1475 end_page_writeback(page);
1477 /* Mark the mft record dirty, so it gets written back. */
1478 mark_mft_record_dirty(ctx->ntfs_ino);
1479 ntfs_attr_put_search_ctx(ctx);
1480 unmap_mft_record(base_ni);
1483 if (err == -ENOMEM) {
1484 ntfs_warning(vi->i_sb, "Error allocating memory. Redirtying "
1485 "page so we try again later.");
1487 * Put the page back on mapping->dirty_pages, but leave its
1488 * buffers' dirty state as-is.
1490 redirty_page_for_writepage(wbc, page);
1493 ntfs_error(vi->i_sb, "Resident attribute write failed with "
1496 NVolSetErrors(ni->vol);
1501 ntfs_attr_put_search_ctx(ctx);
1503 unmap_mft_record(base_ni);
1508 * ntfs_prepare_nonresident_write -
1511 static int ntfs_prepare_nonresident_write(struct page *page,
1512 unsigned from, unsigned to)
1516 s64 initialized_size;
1518 sector_t block, ablock, iblock;
1522 runlist_element *rl;
1523 struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
1524 unsigned long flags;
1525 unsigned int vcn_ofs, block_start, block_end, blocksize;
1528 unsigned char blocksize_bits;
1530 vi = page->mapping->host;
1534 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
1535 "0x%lx, from = %u, to = %u.", ni->mft_no, ni->type,
1536 page->index, from, to);
1538 BUG_ON(!NInoNonResident(ni));
1540 blocksize_bits = vi->i_blkbits;
1541 blocksize = 1 << blocksize_bits;
1544 * create_empty_buffers() will create uptodate/dirty buffers if the
1545 * page is uptodate/dirty.
1547 if (!page_has_buffers(page))
1548 create_empty_buffers(page, blocksize, 0);
1549 bh = head = page_buffers(page);
1553 /* The first block in the page. */
1554 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
1556 read_lock_irqsave(&ni->size_lock, flags);
1558 * The first out of bounds block for the allocated size. No need to
1559 * round up as allocated_size is in multiples of cluster size and the
1560 * minimum cluster size is 512 bytes, which is equal to the smallest
1563 ablock = ni->allocated_size >> blocksize_bits;
1564 i_size = i_size_read(vi);
1565 initialized_size = ni->initialized_size;
1566 read_unlock_irqrestore(&ni->size_lock, flags);
1568 /* The last (fully or partially) initialized block. */
1569 iblock = initialized_size >> blocksize_bits;
1571 /* Loop through all the buffers in the page. */
1576 block_end = block_start + blocksize;
1578 * If buffer @bh is outside the write, just mark it uptodate
1579 * if the page is uptodate and continue with the next buffer.
1581 if (block_end <= from || block_start >= to) {
1582 if (PageUptodate(page)) {
1583 if (!buffer_uptodate(bh))
1584 set_buffer_uptodate(bh);
1589 * @bh is at least partially being written to.
1590 * Make sure it is not marked as new.
1592 //if (buffer_new(bh))
1593 // clear_buffer_new(bh);
1595 if (block >= ablock) {
1596 // TODO: block is above allocated_size, need to
1597 // allocate it. Best done in one go to accommodate not
1598 // only block but all above blocks up to and including:
1599 // ((page->index << PAGE_CACHE_SHIFT) + to + blocksize
1600 // - 1) >> blobksize_bits. Obviously will need to round
1601 // up to next cluster boundary, too. This should be
1602 // done with a helper function, so it can be reused.
1603 ntfs_error(vol->sb, "Writing beyond allocated size "
1604 "is not supported yet. Sorry.");
1607 // Need to update ablock.
1608 // Need to set_buffer_new() on all block bhs that are
1612 * Now we have enough allocated size to fulfill the whole
1613 * request, i.e. block < ablock is true.
1615 if (unlikely((block >= iblock) &&
1616 (initialized_size < i_size))) {
1618 * If this page is fully outside initialized size, zero
1619 * out all pages between the current initialized size
1620 * and the current page. Just use ntfs_readpage() to do
1621 * the zeroing transparently.
1623 if (block > iblock) {
1625 // For each page do:
1626 // - read_cache_page()
1627 // Again for each page do:
1628 // - wait_on_page_locked()
1629 // - Check (PageUptodate(page) &&
1630 // !PageError(page))
1631 // Update initialized size in the attribute and
1633 // Again, for each page do:
1634 // __set_page_dirty_buffers();
1635 // page_cache_release()
1636 // We don't need to wait on the writes.
1640 * The current page straddles initialized size. Zero
1641 * all non-uptodate buffers and set them uptodate (and
1642 * dirty?). Note, there aren't any non-uptodate buffers
1643 * if the page is uptodate.
1644 * FIXME: For an uptodate page, the buffers may need to
1645 * be written out because they were not initialized on
1648 if (!PageUptodate(page)) {
1650 // Zero any non-uptodate buffers up to i_size.
1651 // Set them uptodate and dirty.
1654 // Update initialized size in the attribute and in the
1655 // inode (up to i_size).
1657 // FIXME: This is inefficient. Try to batch the two
1658 // size changes to happen in one go.
1659 ntfs_error(vol->sb, "Writing beyond initialized size "
1660 "is not supported yet. Sorry.");
1663 // Do NOT set_buffer_new() BUT DO clear buffer range
1664 // outside write request range.
1665 // set_buffer_uptodate() on complete buffers as well as
1666 // set_buffer_dirty().
1669 /* Need to map unmapped buffers. */
1670 if (!buffer_mapped(bh)) {
1671 /* Unmapped buffer. Need to map it. */
1672 bh->b_bdev = vol->sb->s_bdev;
1674 /* Convert block into corresponding vcn and offset. */
1675 vcn = (VCN)block << blocksize_bits >>
1676 vol->cluster_size_bits;
1677 vcn_ofs = ((VCN)block << blocksize_bits) &
1678 vol->cluster_size_mask;
1683 down_read(&ni->runlist.lock);
1684 rl = ni->runlist.rl;
1686 if (likely(rl != NULL)) {
1687 /* Seek to element containing target vcn. */
1688 while (rl->length && rl[1].vcn <= vcn)
1690 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
1692 lcn = LCN_RL_NOT_MAPPED;
1693 if (unlikely(lcn < 0)) {
1695 * We extended the attribute allocation above.
1696 * If we hit an ENOENT here it means that the
1697 * allocation was insufficient which is a bug.
1699 BUG_ON(lcn == LCN_ENOENT);
1701 /* It is a hole, need to instantiate it. */
1702 if (lcn == LCN_HOLE) {
1703 // TODO: Instantiate the hole.
1704 // clear_buffer_new(bh);
1705 // unmap_underlying_metadata(bh->b_bdev,
1707 // For non-uptodate buffers, need to
1708 // zero out the region outside the
1709 // request in this bh or all bhs,
1710 // depending on what we implemented
1712 // Need to flush_dcache_page().
1713 // Or could use set_buffer_new()
1715 ntfs_error(vol->sb, "Writing into "
1716 "sparse regions is "
1717 "not supported yet. "
1721 up_read(&ni->runlist.lock);
1723 } else if (!is_retry &&
1724 lcn == LCN_RL_NOT_MAPPED) {
1727 * Attempt to map runlist, dropping
1728 * lock for the duration.
1730 up_read(&ni->runlist.lock);
1731 err = ntfs_map_runlist(ni, vcn);
1733 goto lock_retry_remap;
1736 up_read(&ni->runlist.lock);
1738 * Failed to map the buffer, even after
1744 ntfs_error(vol->sb, "Failed to write to inode "
1745 "0x%lx, attribute type 0x%x, "
1746 "vcn 0x%llx, offset 0x%x "
1747 "because its location on disk "
1748 "could not be determined%s "
1750 ni->mft_no, ni->type,
1751 (unsigned long long)vcn,
1752 vcn_ofs, is_retry ? " even "
1753 "after retrying" : "", err);
1756 /* We now have a successful remap, i.e. lcn >= 0. */
1758 /* Setup buffer head to correct block. */
1759 bh->b_blocknr = ((lcn << vol->cluster_size_bits)
1760 + vcn_ofs) >> blocksize_bits;
1761 set_buffer_mapped(bh);
1763 // FIXME: Something analogous to this is needed for
1764 // each newly allocated block, i.e. BH_New.
1765 // FIXME: Might need to take this out of the
1766 // if (!buffer_mapped(bh)) {}, depending on how we
1767 // implement things during the allocated_size and
1768 // initialized_size extension code above.
1769 if (buffer_new(bh)) {
1770 clear_buffer_new(bh);
1771 unmap_underlying_metadata(bh->b_bdev,
1773 if (PageUptodate(page)) {
1774 set_buffer_uptodate(bh);
1778 * Page is _not_ uptodate, zero surrounding
1779 * region. NOTE: This is how we decide if to
1782 if (block_end > to || block_start < from) {
1785 kaddr = kmap_atomic(page, KM_USER0);
1787 memset(kaddr + to, 0,
1789 if (block_start < from)
1790 memset(kaddr + block_start, 0,
1793 flush_dcache_page(page);
1794 kunmap_atomic(kaddr, KM_USER0);
1799 /* @bh is mapped, set it uptodate if the page is uptodate. */
1800 if (PageUptodate(page)) {
1801 if (!buffer_uptodate(bh))
1802 set_buffer_uptodate(bh);
1806 * The page is not uptodate. The buffer is mapped. If it is not
1807 * uptodate, and it is only partially being written to, we need
1808 * to read the buffer in before the write, i.e. right now.
1810 if (!buffer_uptodate(bh) &&
1811 (block_start < from || block_end > to)) {
1812 ll_rw_block(READ, 1, &bh);
1815 } while (block++, block_start = block_end,
1816 (bh = bh->b_this_page) != head);
1818 /* Release the lock if we took it. */
1820 up_read(&ni->runlist.lock);
1824 /* If we issued read requests, let them complete. */
1825 while (wait_bh > wait) {
1826 wait_on_buffer(*--wait_bh);
1827 if (!buffer_uptodate(*wait_bh))
1831 ntfs_debug("Done.");
1835 * Zero out any newly allocated blocks to avoid exposing stale data.
1836 * If BH_New is set, we know that the block was newly allocated in the
1838 * FIXME: What about initialized_size increments? Have we done all the
1839 * required zeroing above? If not this error handling is broken, and
1840 * in particular the if (block_end <= from) check is completely bogus.
1846 block_end = block_start + blocksize;
1847 if (block_end <= from)
1849 if (block_start >= to)
1851 if (buffer_new(bh)) {
1854 clear_buffer_new(bh);
1855 kaddr = kmap_atomic(page, KM_USER0);
1856 memset(kaddr + block_start, 0, bh->b_size);
1857 kunmap_atomic(kaddr, KM_USER0);
1858 set_buffer_uptodate(bh);
1859 mark_buffer_dirty(bh);
1862 } while (block_start = block_end, (bh = bh->b_this_page) != head);
1864 flush_dcache_page(page);
1866 up_read(&ni->runlist.lock);
1871 * ntfs_prepare_write - prepare a page for receiving data
1873 * This is called from generic_file_write() with i_sem held on the inode
1874 * (@page->mapping->host). The @page is locked but not kmap()ped. The source
1875 * data has not yet been copied into the @page.
1877 * Need to extend the attribute/fill in holes if necessary, create blocks and
1878 * make partially overwritten blocks uptodate,
1880 * i_size is not to be modified yet.
1882 * Return 0 on success or -errno on error.
1884 * Should be using block_prepare_write() [support for sparse files] or
1885 * cont_prepare_write() [no support for sparse files]. Cannot do that due to
1886 * ntfs specifics but can look at them for implementation guidance.
1888 * Note: In the range, @from is inclusive and @to is exclusive, i.e. @from is
1889 * the first byte in the page that will be written to and @to is the first byte
1890 * after the last byte that will be written to.
1892 static int ntfs_prepare_write(struct file *file, struct page *page,
1893 unsigned from, unsigned to)
1897 struct inode *vi = page->mapping->host;
1898 ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
1899 ntfs_volume *vol = ni->vol;
1900 ntfs_attr_search_ctx *ctx = NULL;
1901 MFT_RECORD *m = NULL;
1907 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
1908 "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
1909 page->index, from, to);
1910 BUG_ON(!PageLocked(page));
1911 BUG_ON(from > PAGE_CACHE_SIZE);
1912 BUG_ON(to > PAGE_CACHE_SIZE);
1914 BUG_ON(NInoMstProtected(ni));
1916 * If a previous ntfs_truncate() failed, repeat it and abort if it
1919 if (unlikely(NInoTruncateFailed(ni))) {
1920 down_write(&vi->i_alloc_sem);
1921 err = ntfs_truncate(vi);
1922 up_write(&vi->i_alloc_sem);
1923 if (err || NInoTruncateFailed(ni)) {
1929 /* If the attribute is not resident, deal with it elsewhere. */
1930 if (NInoNonResident(ni)) {
1932 * Only unnamed $DATA attributes can be compressed, encrypted,
1935 if (ni->type == AT_DATA && !ni->name_len) {
1936 /* If file is encrypted, deny access, just like NT4. */
1937 if (NInoEncrypted(ni)) {
1938 ntfs_debug("Denying write access to encrypted "
1942 /* Compressed data streams are handled in compress.c. */
1943 if (NInoCompressed(ni)) {
1944 // TODO: Implement and replace this check with
1945 // return ntfs_write_compressed_block(page);
1946 ntfs_error(vi->i_sb, "Writing to compressed "
1947 "files is not supported yet. "
1951 // TODO: Implement and remove this check.
1952 if (NInoSparse(ni)) {
1953 ntfs_error(vi->i_sb, "Writing to sparse files "
1954 "is not supported yet. Sorry.");
1958 /* Normal data stream. */
1959 return ntfs_prepare_nonresident_write(page, from, to);
1962 * Attribute is resident, implying it is not compressed, encrypted, or
1965 BUG_ON(page_has_buffers(page));
1966 new_size = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
1967 /* If we do not need to resize the attribute allocation we are done. */
1968 if (new_size <= i_size_read(vi))
1970 /* Map, pin, and lock the (base) mft record. */
1974 base_ni = ni->ext.base_ntfs_ino;
1975 m = map_mft_record(base_ni);
1982 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1983 if (unlikely(!ctx)) {
1987 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1988 CASE_SENSITIVE, 0, NULL, 0, ctx);
1989 if (unlikely(err)) {
1996 /* The total length of the attribute value. */
1997 attr_len = le32_to_cpu(a->data.resident.value_length);
1998 /* Fix an eventual previous failure of ntfs_commit_write(). */
1999 i_size = i_size_read(vi);
2000 if (unlikely(attr_len > i_size)) {
2002 a->data.resident.value_length = cpu_to_le32(attr_len);
2004 /* If we do not need to resize the attribute allocation we are done. */
2005 if (new_size <= attr_len)
2007 /* Check if new size is allowed in $AttrDef. */
2008 err = ntfs_attr_size_bounds_check(vol, ni->type, new_size);
2009 if (unlikely(err)) {
2010 if (err == -ERANGE) {
2011 ntfs_error(vol->sb, "Write would cause the inode "
2012 "0x%lx to exceed the maximum size for "
2013 "its attribute type (0x%x). Aborting "
2014 "write.", vi->i_ino,
2015 le32_to_cpu(ni->type));
2017 ntfs_error(vol->sb, "Inode 0x%lx has unknown "
2018 "attribute type 0x%x. Aborting "
2019 "write.", vi->i_ino,
2020 le32_to_cpu(ni->type));
2026 * Extend the attribute record to be able to store the new attribute
2029 if (new_size >= vol->mft_record_size || ntfs_attr_record_resize(m, a,
2030 le16_to_cpu(a->data.resident.value_offset) +
2032 /* Not enough space in the mft record. */
2033 ntfs_error(vol->sb, "Not enough space in the mft record for "
2034 "the resized attribute value. This is not "
2035 "supported yet. Aborting write.");
2040 * We have enough space in the mft record to fit the write. This
2041 * implies the attribute is smaller than the mft record and hence the
2042 * attribute must be in a single page and hence page->index must be 0.
2044 BUG_ON(page->index);
2046 * If the beginning of the write is past the old size, enlarge the
2047 * attribute value up to the beginning of the write and fill it with
2050 if (from > attr_len) {
2051 memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
2052 attr_len, 0, from - attr_len);
2053 a->data.resident.value_length = cpu_to_le32(from);
2054 /* Zero the corresponding area in the page as well. */
2055 if (PageUptodate(page)) {
2056 kaddr = kmap_atomic(page, KM_USER0);
2057 memset(kaddr + attr_len, 0, from - attr_len);
2058 kunmap_atomic(kaddr, KM_USER0);
2059 flush_dcache_page(page);
2062 flush_dcache_mft_record_page(ctx->ntfs_ino);
2063 mark_mft_record_dirty(ctx->ntfs_ino);
2065 ntfs_attr_put_search_ctx(ctx);
2066 unmap_mft_record(base_ni);
2068 * Because resident attributes are handled by memcpy() to/from the
2069 * corresponding MFT record, and because this form of i/o is byte
2070 * aligned rather than block aligned, there is no need to bring the
2071 * page uptodate here as in the non-resident case where we need to
2072 * bring the buffers straddled by the write uptodate before
2073 * generic_file_write() does the copying from userspace.
2075 * We thus defer the uptodate bringing of the page region outside the
2076 * region written to to ntfs_commit_write(), which makes the code
2077 * simpler and saves one atomic kmap which is good.
2080 ntfs_debug("Done.");
2084 ntfs_warning(vi->i_sb, "Error allocating memory required to "
2085 "prepare the write.");
2087 ntfs_error(vi->i_sb, "Resident attribute prepare write failed "
2088 "with error %i.", err);
2094 ntfs_attr_put_search_ctx(ctx);
2096 unmap_mft_record(base_ni);
2101 * ntfs_commit_nonresident_write -
2104 static int ntfs_commit_nonresident_write(struct page *page,
2105 unsigned from, unsigned to)
2107 s64 pos = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
2108 struct inode *vi = page->mapping->host;
2109 struct buffer_head *bh, *head;
2110 unsigned int block_start, block_end, blocksize;
2113 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
2114 "0x%lx, from = %u, to = %u.", vi->i_ino,
2115 NTFS_I(vi)->type, page->index, from, to);
2116 blocksize = 1 << vi->i_blkbits;
2118 // FIXME: We need a whole slew of special cases in here for compressed
2119 // files for example...
2120 // For now, we know ntfs_prepare_write() would have failed so we can't
2121 // get here in any of the cases which we have to special case, so we
2122 // are just a ripped off, unrolled generic_commit_write().
2124 bh = head = page_buffers(page);
2128 block_end = block_start + blocksize;
2129 if (block_end <= from || block_start >= to) {
2130 if (!buffer_uptodate(bh))
2133 set_buffer_uptodate(bh);
2134 mark_buffer_dirty(bh);
2136 } while (block_start = block_end, (bh = bh->b_this_page) != head);
2138 * If this is a partial write which happened to make all buffers
2139 * uptodate then we can optimize away a bogus ->readpage() for the next
2140 * read(). Here we 'discover' whether the page went uptodate as a
2141 * result of this (potentially partial) write.
2144 SetPageUptodate(page);
2146 * Not convinced about this at all. See disparity comment above. For
2147 * now we know ntfs_prepare_write() would have failed in the write
2148 * exceeds i_size case, so this will never trigger which is fine.
2150 if (pos > i_size_read(vi)) {
2151 ntfs_error(vi->i_sb, "Writing beyond the existing file size is "
2152 "not supported yet. Sorry.");
2154 // vi->i_size = pos;
2155 // mark_inode_dirty(vi);
2157 ntfs_debug("Done.");
2162 * ntfs_commit_write - commit the received data
2164 * This is called from generic_file_write() with i_sem held on the inode
2165 * (@page->mapping->host). The @page is locked but not kmap()ped. The source
2166 * data has already been copied into the @page. ntfs_prepare_write() has been
2167 * called before the data copied and it returned success so we can take the
2168 * results of various BUG checks and some error handling for granted.
2170 * Need to mark modified blocks dirty so they get written out later when
2171 * ntfs_writepage() is invoked by the VM.
2173 * Return 0 on success or -errno on error.
2175 * Should be using generic_commit_write(). This marks buffers uptodate and
2176 * dirty, sets the page uptodate if all buffers in the page are uptodate, and
2177 * updates i_size if the end of io is beyond i_size. In that case, it also
2178 * marks the inode dirty.
2180 * Cannot use generic_commit_write() due to ntfs specialities but can look at
2181 * it for implementation guidance.
2183 * If things have gone as outlined in ntfs_prepare_write(), then we do not
2184 * need to do any page content modifications here at all, except in the write
2185 * to resident attribute case, where we need to do the uptodate bringing here
2186 * which we combine with the copying into the mft record which means we save
2189 static int ntfs_commit_write(struct file *file, struct page *page,
2190 unsigned from, unsigned to)
2192 struct inode *vi = page->mapping->host;
2193 ntfs_inode *base_ni, *ni = NTFS_I(vi);
2194 char *kaddr, *kattr;
2195 ntfs_attr_search_ctx *ctx;
2201 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
2202 "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
2203 page->index, from, to);
2204 /* If the attribute is not resident, deal with it elsewhere. */
2205 if (NInoNonResident(ni)) {
2206 /* Only unnamed $DATA attributes can be compressed/encrypted. */
2207 if (ni->type == AT_DATA && !ni->name_len) {
2208 /* Encrypted files need separate handling. */
2209 if (NInoEncrypted(ni)) {
2210 // We never get here at present!
2213 /* Compressed data streams are handled in compress.c. */
2214 if (NInoCompressed(ni)) {
2215 // TODO: Implement this!
2216 // return ntfs_write_compressed_block(page);
2217 // We never get here at present!
2221 /* Normal data stream. */
2222 return ntfs_commit_nonresident_write(page, from, to);
2225 * Attribute is resident, implying it is not compressed, encrypted, or
2231 base_ni = ni->ext.base_ntfs_ino;
2232 /* Map, pin, and lock the mft record. */
2233 m = map_mft_record(base_ni);
2240 ctx = ntfs_attr_get_search_ctx(base_ni, m);
2241 if (unlikely(!ctx)) {
2245 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2246 CASE_SENSITIVE, 0, NULL, 0, ctx);
2247 if (unlikely(err)) {
2253 /* The total length of the attribute value. */
2254 attr_len = le32_to_cpu(a->data.resident.value_length);
2255 BUG_ON(from > attr_len);
2256 kattr = (u8*)a + le16_to_cpu(a->data.resident.value_offset);
2257 kaddr = kmap_atomic(page, KM_USER0);
2258 /* Copy the received data from the page to the mft record. */
2259 memcpy(kattr + from, kaddr + from, to - from);
2260 /* Update the attribute length if necessary. */
2261 if (to > attr_len) {
2263 a->data.resident.value_length = cpu_to_le32(attr_len);
2266 * If the page is not uptodate, bring the out of bounds area(s)
2267 * uptodate by copying data from the mft record to the page.
2269 if (!PageUptodate(page)) {
2271 memcpy(kaddr, kattr, from);
2273 memcpy(kaddr + to, kattr + to, attr_len - to);
2274 /* Zero the region outside the end of the attribute value. */
2275 if (attr_len < PAGE_CACHE_SIZE)
2276 memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
2278 * The probability of not having done any of the above is
2279 * extremely small, so we just flush unconditionally.
2281 flush_dcache_page(page);
2282 SetPageUptodate(page);
2284 kunmap_atomic(kaddr, KM_USER0);
2285 /* Update i_size if necessary. */
2286 if (i_size_read(vi) < attr_len) {
2287 unsigned long flags;
2289 write_lock_irqsave(&ni->size_lock, flags);
2290 ni->allocated_size = ni->initialized_size = attr_len;
2291 i_size_write(vi, attr_len);
2292 write_unlock_irqrestore(&ni->size_lock, flags);
2294 /* Mark the mft record dirty, so it gets written back. */
2295 flush_dcache_mft_record_page(ctx->ntfs_ino);
2296 mark_mft_record_dirty(ctx->ntfs_ino);
2297 ntfs_attr_put_search_ctx(ctx);
2298 unmap_mft_record(base_ni);
2299 ntfs_debug("Done.");
2302 if (err == -ENOMEM) {
2303 ntfs_warning(vi->i_sb, "Error allocating memory required to "
2304 "commit the write.");
2305 if (PageUptodate(page)) {
2306 ntfs_warning(vi->i_sb, "Page is uptodate, setting "
2307 "dirty so the write will be retried "
2308 "later on by the VM.");
2310 * Put the page on mapping->dirty_pages, but leave its
2311 * buffers' dirty state as-is.
2313 __set_page_dirty_nobuffers(page);
2316 ntfs_error(vi->i_sb, "Page is not uptodate. Written "
2317 "data has been lost.");
2319 ntfs_error(vi->i_sb, "Resident attribute commit write failed "
2320 "with error %i.", err);
2321 NVolSetErrors(ni->vol);
2325 ntfs_attr_put_search_ctx(ctx);
2327 unmap_mft_record(base_ni);
2331 #endif /* NTFS_RW */
2334 * ntfs_aops - general address space operations for inodes and attributes
2336 struct address_space_operations ntfs_aops = {
2337 .readpage = ntfs_readpage, /* Fill page with data. */
2338 .sync_page = block_sync_page, /* Currently, just unplugs the
2339 disk request queue. */
2341 .writepage = ntfs_writepage, /* Write dirty page to disk. */
2342 .prepare_write = ntfs_prepare_write, /* Prepare page and buffers
2343 ready to receive data. */
2344 .commit_write = ntfs_commit_write, /* Commit received data. */
2345 #endif /* NTFS_RW */
2349 * ntfs_mst_aops - general address space operations for mst protecteed inodes
2352 struct address_space_operations ntfs_mst_aops = {
2353 .readpage = ntfs_readpage, /* Fill page with data. */
2354 .sync_page = block_sync_page, /* Currently, just unplugs the
2355 disk request queue. */
2357 .writepage = ntfs_writepage, /* Write dirty page to disk. */
2358 .set_page_dirty = __set_page_dirty_nobuffers, /* Set the page dirty
2359 without touching the buffers
2360 belonging to the page. */
2361 #endif /* NTFS_RW */
2367 * mark_ntfs_record_dirty - mark an ntfs record dirty
2368 * @page: page containing the ntfs record to mark dirty
2369 * @ofs: byte offset within @page at which the ntfs record begins
2371 * Set the buffers and the page in which the ntfs record is located dirty.
2373 * The latter also marks the vfs inode the ntfs record belongs to dirty
2374 * (I_DIRTY_PAGES only).
2376 * If the page does not have buffers, we create them and set them uptodate.
2377 * The page may not be locked which is why we need to handle the buffers under
2378 * the mapping->private_lock. Once the buffers are marked dirty we no longer
2379 * need the lock since try_to_free_buffers() does not free dirty buffers.
2381 void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) {
2382 struct address_space *mapping = page->mapping;
2383 ntfs_inode *ni = NTFS_I(mapping->host);
2384 struct buffer_head *bh, *head, *buffers_to_free = NULL;
2385 unsigned int end, bh_size, bh_ofs;
2387 BUG_ON(!PageUptodate(page));
2388 end = ofs + ni->itype.index.block_size;
2389 bh_size = 1 << VFS_I(ni)->i_blkbits;
2390 spin_lock(&mapping->private_lock);
2391 if (unlikely(!page_has_buffers(page))) {
2392 spin_unlock(&mapping->private_lock);
2393 bh = head = alloc_page_buffers(page, bh_size, 1);
2394 spin_lock(&mapping->private_lock);
2395 if (likely(!page_has_buffers(page))) {
2396 struct buffer_head *tail;
2399 set_buffer_uptodate(bh);
2401 bh = bh->b_this_page;
2403 tail->b_this_page = head;
2404 attach_page_buffers(page, head);
2406 buffers_to_free = bh;
2408 bh = head = page_buffers(page);
2411 bh_ofs = bh_offset(bh);
2412 if (bh_ofs + bh_size <= ofs)
2414 if (unlikely(bh_ofs >= end))
2416 set_buffer_dirty(bh);
2417 } while ((bh = bh->b_this_page) != head);
2418 spin_unlock(&mapping->private_lock);
2419 __set_page_dirty_nobuffers(page);
2420 if (unlikely(buffers_to_free)) {
2422 bh = buffers_to_free->b_this_page;
2423 free_buffer_head(buffers_to_free);
2424 buffers_to_free = bh;
2425 } while (buffers_to_free);
2429 #endif /* NTFS_RW */