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>
41 * ntfs_end_buffer_async_read - async io completion for reading attributes
42 * @bh: buffer head on which io is completed
43 * @uptodate: whether @bh is now uptodate or not
45 * Asynchronous I/O completion handler for reading pages belonging to the
46 * attribute address space of an inode. The inodes can either be files or
47 * directories or they can be fake inodes describing some attribute.
49 * If NInoMstProtected(), perform the post read mst fixups when all IO on the
50 * page has been completed and mark the page uptodate or set the error bit on
51 * the page. To determine the size of the records that need fixing up, we
52 * cheat a little bit by setting the index_block_size in ntfs_inode to the ntfs
53 * record size, and index_block_size_bits, to the log(base 2) of the ntfs
56 static void ntfs_end_buffer_async_read(struct buffer_head
*bh
, int uptodate
)
59 struct buffer_head
*first
, *tmp
;
62 int page_uptodate
= 1;
65 ni
= NTFS_I(page
->mapping
->host
);
67 if (likely(uptodate
)) {
68 s64 file_ofs
, initialized_size
;
70 set_buffer_uptodate(bh
);
72 file_ofs
= ((s64
)page
->index
<< PAGE_CACHE_SHIFT
) +
74 read_lock_irqsave(&ni
->size_lock
, flags
);
75 initialized_size
= ni
->initialized_size
;
76 read_unlock_irqrestore(&ni
->size_lock
, flags
);
77 /* Check for the current buffer head overflowing. */
78 if (file_ofs
+ bh
->b_size
> initialized_size
) {
82 if (file_ofs
< initialized_size
)
83 ofs
= initialized_size
- file_ofs
;
84 addr
= kmap_atomic(page
, KM_BIO_SRC_IRQ
);
85 memset(addr
+ bh_offset(bh
) + ofs
, 0, bh
->b_size
- ofs
);
86 flush_dcache_page(page
);
87 kunmap_atomic(addr
, KM_BIO_SRC_IRQ
);
90 clear_buffer_uptodate(bh
);
92 ntfs_error(ni
->vol
->sb
, "Buffer I/O error, logical block %llu.",
93 (unsigned long long)bh
->b_blocknr
);
95 first
= page_buffers(page
);
96 local_irq_save(flags
);
97 bit_spin_lock(BH_Uptodate_Lock
, &first
->b_state
);
98 clear_buffer_async_read(bh
);
102 if (!buffer_uptodate(tmp
))
104 if (buffer_async_read(tmp
)) {
105 if (likely(buffer_locked(tmp
)))
107 /* Async buffers must be locked. */
110 tmp
= tmp
->b_this_page
;
112 bit_spin_unlock(BH_Uptodate_Lock
, &first
->b_state
);
113 local_irq_restore(flags
);
115 * If none of the buffers had errors then we can set the page uptodate,
116 * but we first have to perform the post read mst fixups, if the
117 * attribute is mst protected, i.e. if NInoMstProteced(ni) is true.
118 * Note we ignore fixup errors as those are detected when
119 * map_mft_record() is called which gives us per record granularity
120 * rather than per page granularity.
122 if (!NInoMstProtected(ni
)) {
123 if (likely(page_uptodate
&& !PageError(page
)))
124 SetPageUptodate(page
);
127 unsigned int i
, recs
;
130 rec_size
= ni
->itype
.index
.block_size
;
131 recs
= PAGE_CACHE_SIZE
/ rec_size
;
132 /* Should have been verified before we got here... */
134 addr
= kmap_atomic(page
, KM_BIO_SRC_IRQ
);
135 for (i
= 0; i
< recs
; i
++)
136 post_read_mst_fixup((NTFS_RECORD
*)(addr
+
137 i
* rec_size
), rec_size
);
138 flush_dcache_page(page
);
139 kunmap_atomic(addr
, KM_BIO_SRC_IRQ
);
140 if (likely(page_uptodate
&& !PageError(page
)))
141 SetPageUptodate(page
);
146 bit_spin_unlock(BH_Uptodate_Lock
, &first
->b_state
);
147 local_irq_restore(flags
);
152 * ntfs_read_block - fill a @page of an address space with data
153 * @page: page cache page to fill with data
155 * Fill the page @page of the address space belonging to the @page->host inode.
156 * We read each buffer asynchronously and when all buffers are read in, our io
157 * completion handler ntfs_end_buffer_read_async(), if required, automatically
158 * applies the mst fixups to the page before finally marking it uptodate and
161 * We only enforce allocated_size limit because i_size is checked for in
162 * generic_file_read().
164 * Return 0 on success and -errno on error.
166 * Contains an adapted version of fs/buffer.c::block_read_full_page().
168 static int ntfs_read_block(struct page
*page
)
175 struct buffer_head
*bh
, *head
, *arr
[MAX_BUF_PER_PAGE
];
176 sector_t iblock
, lblock
, zblock
;
178 unsigned int blocksize
, vcn_ofs
;
180 unsigned char blocksize_bits
;
182 ni
= NTFS_I(page
->mapping
->host
);
185 /* $MFT/$DATA must have its complete runlist in memory at all times. */
186 BUG_ON(!ni
->runlist
.rl
&& !ni
->mft_no
&& !NInoAttr(ni
));
188 blocksize_bits
= VFS_I(ni
)->i_blkbits
;
189 blocksize
= 1 << blocksize_bits
;
191 if (!page_has_buffers(page
)) {
192 create_empty_buffers(page
, blocksize
, 0);
193 if (unlikely(!page_has_buffers(page
))) {
198 bh
= head
= page_buffers(page
);
201 iblock
= (s64
)page
->index
<< (PAGE_CACHE_SHIFT
- blocksize_bits
);
202 read_lock_irqsave(&ni
->size_lock
, flags
);
203 lblock
= (ni
->allocated_size
+ blocksize
- 1) >> blocksize_bits
;
204 zblock
= (ni
->initialized_size
+ blocksize
- 1) >> blocksize_bits
;
205 read_unlock_irqrestore(&ni
->size_lock
, flags
);
207 /* Loop through all the buffers in the page. */
214 if (unlikely(buffer_uptodate(bh
)))
216 if (unlikely(buffer_mapped(bh
))) {
221 bh
->b_bdev
= vol
->sb
->s_bdev
;
222 /* Is the block within the allowed limits? */
223 if (iblock
< lblock
) {
224 BOOL is_retry
= FALSE
;
226 /* Convert iblock into corresponding vcn and offset. */
227 vcn
= (VCN
)iblock
<< blocksize_bits
>>
228 vol
->cluster_size_bits
;
229 vcn_ofs
= ((VCN
)iblock
<< blocksize_bits
) &
230 vol
->cluster_size_mask
;
233 down_read(&ni
->runlist
.lock
);
236 if (likely(rl
!= NULL
)) {
237 /* Seek to element containing target vcn. */
238 while (rl
->length
&& rl
[1].vcn
<= vcn
)
240 lcn
= ntfs_rl_vcn_to_lcn(rl
, vcn
);
242 lcn
= LCN_RL_NOT_MAPPED
;
243 /* Successful remap. */
245 /* Setup buffer head to correct block. */
246 bh
->b_blocknr
= ((lcn
<< vol
->cluster_size_bits
)
247 + vcn_ofs
) >> blocksize_bits
;
248 set_buffer_mapped(bh
);
249 /* Only read initialized data blocks. */
250 if (iblock
< zblock
) {
254 /* Fully non-initialized data block, zero it. */
257 /* It is a hole, need to zero it. */
260 /* If first try and runlist unmapped, map and retry. */
261 if (!is_retry
&& lcn
== LCN_RL_NOT_MAPPED
) {
264 * Attempt to map runlist, dropping lock for
267 up_read(&ni
->runlist
.lock
);
268 err
= ntfs_map_runlist(ni
, vcn
);
270 goto lock_retry_remap
;
273 up_read(&ni
->runlist
.lock
);
275 * If buffer is outside the runlist, treat it as a
276 * hole. This can happen due to concurrent truncate
279 if (err
== -ENOENT
|| lcn
== LCN_ENOENT
) {
283 /* Hard error, zero out region. */
288 ntfs_error(vol
->sb
, "Failed to read from inode 0x%lx, "
289 "attribute type 0x%x, vcn 0x%llx, "
290 "offset 0x%x because its location on "
291 "disk could not be determined%s "
292 "(error code %i).", ni
->mft_no
,
293 ni
->type
, (unsigned long long)vcn
,
294 vcn_ofs
, is_retry
? " even after "
295 "retrying" : "", err
);
298 * Either iblock was outside lblock limits or
299 * ntfs_rl_vcn_to_lcn() returned error. Just zero that portion
300 * of the page and set the buffer uptodate.
303 bh
->b_blocknr
= -1UL;
304 clear_buffer_mapped(bh
);
306 kaddr
= kmap_atomic(page
, KM_USER0
);
307 memset(kaddr
+ i
* blocksize
, 0, blocksize
);
308 kunmap_atomic(kaddr
, KM_USER0
);
309 flush_dcache_page(page
);
311 set_buffer_uptodate(bh
);
312 } while (i
++, iblock
++, (bh
= bh
->b_this_page
) != head
);
314 /* Release the lock if we took it. */
316 up_read(&ni
->runlist
.lock
);
318 /* Check we have at least one buffer ready for i/o. */
320 struct buffer_head
*tbh
;
322 /* Lock the buffers. */
323 for (i
= 0; i
< nr
; i
++) {
326 tbh
->b_end_io
= ntfs_end_buffer_async_read
;
327 set_buffer_async_read(tbh
);
329 /* Finally, start i/o on the buffers. */
330 for (i
= 0; i
< nr
; i
++) {
332 if (likely(!buffer_uptodate(tbh
)))
333 submit_bh(READ
, tbh
);
335 ntfs_end_buffer_async_read(tbh
, 1);
339 /* No i/o was scheduled on any of the buffers. */
340 if (likely(!PageError(page
)))
341 SetPageUptodate(page
);
342 else /* Signal synchronous i/o error. */
349 * ntfs_readpage - fill a @page of a @file with data from the device
350 * @file: open file to which the page @page belongs or NULL
351 * @page: page cache page to fill with data
353 * For non-resident attributes, ntfs_readpage() fills the @page of the open
354 * file @file by calling the ntfs version of the generic block_read_full_page()
355 * function, ntfs_read_block(), which in turn creates and reads in the buffers
356 * associated with the page asynchronously.
358 * For resident attributes, OTOH, ntfs_readpage() fills @page by copying the
359 * data from the mft record (which at this stage is most likely in memory) and
360 * fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as
361 * even if the mft record is not cached at this point in time, we need to wait
362 * for it to be read in before we can do the copy.
364 * Return 0 on success and -errno on error.
366 static int ntfs_readpage(struct file
*file
, struct page
*page
)
368 ntfs_inode
*ni
, *base_ni
;
370 ntfs_attr_search_ctx
*ctx
;
377 BUG_ON(!PageLocked(page
));
379 * This can potentially happen because we clear PageUptodate() during
380 * ntfs_writepage() of MstProtected() attributes.
382 if (PageUptodate(page
)) {
386 ni
= NTFS_I(page
->mapping
->host
);
388 * Only $DATA attributes can be encrypted and only unnamed $DATA
389 * attributes can be compressed. Index root can have the flags set but
390 * this means to create compressed/encrypted files, not that the
391 * attribute is compressed/encrypted.
393 if (ni
->type
!= AT_INDEX_ROOT
) {
394 /* If attribute is encrypted, deny access, just like NT4. */
395 if (NInoEncrypted(ni
)) {
396 BUG_ON(ni
->type
!= AT_DATA
);
400 /* Compressed data streams are handled in compress.c. */
401 if (NInoNonResident(ni
) && NInoCompressed(ni
)) {
402 BUG_ON(ni
->type
!= AT_DATA
);
403 BUG_ON(ni
->name_len
);
404 return ntfs_read_compressed_block(page
);
407 /* NInoNonResident() == NInoIndexAllocPresent() */
408 if (NInoNonResident(ni
)) {
409 /* Normal, non-resident data stream. */
410 return ntfs_read_block(page
);
413 * Attribute is resident, implying it is not compressed or encrypted.
414 * This also means the attribute is smaller than an mft record and
415 * hence smaller than a page, so can simply zero out any pages with
416 * index above 0. Note the attribute can actually be marked compressed
417 * but if it is resident the actual data is not compressed so we are
418 * ok to ignore the compressed flag here.
420 if (unlikely(page
->index
> 0)) {
421 kaddr
= kmap_atomic(page
, KM_USER0
);
422 memset(kaddr
, 0, PAGE_CACHE_SIZE
);
423 flush_dcache_page(page
);
424 kunmap_atomic(kaddr
, KM_USER0
);
430 base_ni
= ni
->ext
.base_ntfs_ino
;
431 /* Map, pin, and lock the mft record. */
432 mrec
= map_mft_record(base_ni
);
438 * If a parallel write made the attribute non-resident, drop the mft
439 * record and retry the readpage.
441 if (unlikely(NInoNonResident(ni
))) {
442 unmap_mft_record(base_ni
);
445 ctx
= ntfs_attr_get_search_ctx(base_ni
, mrec
);
446 if (unlikely(!ctx
)) {
450 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
451 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
453 goto put_unm_err_out
;
454 attr_len
= le32_to_cpu(ctx
->attr
->data
.resident
.value_length
);
455 read_lock_irqsave(&ni
->size_lock
, flags
);
456 if (unlikely(attr_len
> ni
->initialized_size
))
457 attr_len
= ni
->initialized_size
;
458 read_unlock_irqrestore(&ni
->size_lock
, flags
);
459 kaddr
= kmap_atomic(page
, KM_USER0
);
460 /* Copy the data to the page. */
461 memcpy(kaddr
, (u8
*)ctx
->attr
+
462 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
),
464 /* Zero the remainder of the page. */
465 memset(kaddr
+ attr_len
, 0, PAGE_CACHE_SIZE
- attr_len
);
466 flush_dcache_page(page
);
467 kunmap_atomic(kaddr
, KM_USER0
);
469 ntfs_attr_put_search_ctx(ctx
);
471 unmap_mft_record(base_ni
);
473 SetPageUptodate(page
);
482 * ntfs_write_block - write a @page to the backing store
483 * @page: page cache page to write out
484 * @wbc: writeback control structure
486 * This function is for writing pages belonging to non-resident, non-mst
487 * protected attributes to their backing store.
489 * For a page with buffers, map and write the dirty buffers asynchronously
490 * under page writeback. For a page without buffers, create buffers for the
491 * page, then proceed as above.
493 * If a page doesn't have buffers the page dirty state is definitive. If a page
494 * does have buffers, the page dirty state is just a hint, and the buffer dirty
495 * state is definitive. (A hint which has rules: dirty buffers against a clean
496 * page is illegal. Other combinations are legal and need to be handled. In
497 * particular a dirty page containing clean buffers for example.)
499 * Return 0 on success and -errno on error.
501 * Based on ntfs_read_block() and __block_write_full_page().
503 static int ntfs_write_block(struct page
*page
, struct writeback_control
*wbc
)
507 s64 initialized_size
;
509 sector_t block
, dblock
, iblock
;
514 struct buffer_head
*bh
, *head
;
516 unsigned int blocksize
, vcn_ofs
;
518 BOOL need_end_writeback
;
519 unsigned char blocksize_bits
;
521 vi
= page
->mapping
->host
;
525 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
526 "0x%lx.", ni
->mft_no
, ni
->type
, page
->index
);
528 BUG_ON(!NInoNonResident(ni
));
529 BUG_ON(NInoMstProtected(ni
));
531 blocksize_bits
= vi
->i_blkbits
;
532 blocksize
= 1 << blocksize_bits
;
534 if (!page_has_buffers(page
)) {
535 BUG_ON(!PageUptodate(page
));
536 create_empty_buffers(page
, blocksize
,
537 (1 << BH_Uptodate
) | (1 << BH_Dirty
));
538 if (unlikely(!page_has_buffers(page
))) {
539 ntfs_warning(vol
->sb
, "Error allocating page "
540 "buffers. Redirtying page so we try "
543 * Put the page back on mapping->dirty_pages, but leave
544 * its buffers' dirty state as-is.
546 redirty_page_for_writepage(wbc
, page
);
551 bh
= head
= page_buffers(page
);
554 /* NOTE: Different naming scheme to ntfs_read_block()! */
556 /* The first block in the page. */
557 block
= (s64
)page
->index
<< (PAGE_CACHE_SHIFT
- blocksize_bits
);
559 read_lock_irqsave(&ni
->size_lock
, flags
);
560 i_size
= i_size_read(vi
);
561 initialized_size
= ni
->initialized_size
;
562 read_unlock_irqrestore(&ni
->size_lock
, flags
);
564 /* The first out of bounds block for the data size. */
565 dblock
= (i_size
+ blocksize
- 1) >> blocksize_bits
;
567 /* The last (fully or partially) initialized block. */
568 iblock
= initialized_size
>> blocksize_bits
;
571 * Be very careful. We have no exclusion from __set_page_dirty_buffers
572 * here, and the (potentially unmapped) buffers may become dirty at
573 * any time. If a buffer becomes dirty here after we've inspected it
574 * then we just miss that fact, and the page stays dirty.
576 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
577 * handle that here by just cleaning them.
581 * Loop through all the buffers in the page, mapping all the dirty
582 * buffers to disk addresses and handling any aliases from the
583 * underlying block device's mapping.
588 BOOL is_retry
= FALSE
;
590 if (unlikely(block
>= dblock
)) {
592 * Mapped buffers outside i_size will occur, because
593 * this page can be outside i_size when there is a
594 * truncate in progress. The contents of such buffers
595 * were zeroed by ntfs_writepage().
597 * FIXME: What about the small race window where
598 * ntfs_writepage() has not done any clearing because
599 * the page was within i_size but before we get here,
600 * vmtruncate() modifies i_size?
602 clear_buffer_dirty(bh
);
603 set_buffer_uptodate(bh
);
607 /* Clean buffers are not written out, so no need to map them. */
608 if (!buffer_dirty(bh
))
611 /* Make sure we have enough initialized size. */
612 if (unlikely((block
>= iblock
) &&
613 (initialized_size
< i_size
))) {
615 * If this page is fully outside initialized size, zero
616 * out all pages between the current initialized size
617 * and the current page. Just use ntfs_readpage() to do
618 * the zeroing transparently.
620 if (block
> iblock
) {
623 // - read_cache_page()
624 // Again for each page do:
625 // - wait_on_page_locked()
626 // - Check (PageUptodate(page) &&
628 // Update initialized size in the attribute and
630 // Again, for each page do:
631 // __set_page_dirty_buffers();
632 // page_cache_release()
633 // We don't need to wait on the writes.
637 * The current page straddles initialized size. Zero
638 * all non-uptodate buffers and set them uptodate (and
639 * dirty?). Note, there aren't any non-uptodate buffers
640 * if the page is uptodate.
641 * FIXME: For an uptodate page, the buffers may need to
642 * be written out because they were not initialized on
645 if (!PageUptodate(page
)) {
647 // Zero any non-uptodate buffers up to i_size.
648 // Set them uptodate and dirty.
651 // Update initialized size in the attribute and in the
652 // inode (up to i_size).
654 // FIXME: This is inefficient. Try to batch the two
655 // size changes to happen in one go.
656 ntfs_error(vol
->sb
, "Writing beyond initialized size "
657 "is not supported yet. Sorry.");
660 // Do NOT set_buffer_new() BUT DO clear buffer range
661 // outside write request range.
662 // set_buffer_uptodate() on complete buffers as well as
663 // set_buffer_dirty().
666 /* No need to map buffers that are already mapped. */
667 if (buffer_mapped(bh
))
670 /* Unmapped, dirty buffer. Need to map it. */
671 bh
->b_bdev
= vol
->sb
->s_bdev
;
673 /* Convert block into corresponding vcn and offset. */
674 vcn
= (VCN
)block
<< blocksize_bits
;
675 vcn_ofs
= vcn
& vol
->cluster_size_mask
;
676 vcn
>>= vol
->cluster_size_bits
;
679 down_read(&ni
->runlist
.lock
);
682 if (likely(rl
!= NULL
)) {
683 /* Seek to element containing target vcn. */
684 while (rl
->length
&& rl
[1].vcn
<= vcn
)
686 lcn
= ntfs_rl_vcn_to_lcn(rl
, vcn
);
688 lcn
= LCN_RL_NOT_MAPPED
;
689 /* Successful remap. */
691 /* Setup buffer head to point to correct block. */
692 bh
->b_blocknr
= ((lcn
<< vol
->cluster_size_bits
) +
693 vcn_ofs
) >> blocksize_bits
;
694 set_buffer_mapped(bh
);
697 /* It is a hole, need to instantiate it. */
698 if (lcn
== LCN_HOLE
) {
700 unsigned long *bpos
, *bend
;
702 /* Check if the buffer is zero. */
703 kaddr
= kmap_atomic(page
, KM_USER0
);
704 bpos
= (unsigned long *)(kaddr
+ bh_offset(bh
));
705 bend
= (unsigned long *)((u8
*)bpos
+ blocksize
);
709 } while (likely(++bpos
< bend
));
710 kunmap_atomic(kaddr
, KM_USER0
);
713 * Buffer is zero and sparse, no need to write
717 clear_buffer_dirty(bh
);
720 // TODO: Instantiate the hole.
721 // clear_buffer_new(bh);
722 // unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
723 ntfs_error(vol
->sb
, "Writing into sparse regions is "
724 "not supported yet. Sorry.");
728 /* If first try and runlist unmapped, map and retry. */
729 if (!is_retry
&& lcn
== LCN_RL_NOT_MAPPED
) {
732 * Attempt to map runlist, dropping lock for
735 up_read(&ni
->runlist
.lock
);
736 err
= ntfs_map_runlist(ni
, vcn
);
738 goto lock_retry_remap
;
741 up_read(&ni
->runlist
.lock
);
743 * If buffer is outside the runlist, truncate has cut it out
744 * of the runlist. Just clean and clear the buffer and set it
745 * uptodate so it can get discarded by the VM.
747 if (err
== -ENOENT
|| lcn
== LCN_ENOENT
) {
751 clear_buffer_dirty(bh
);
752 kaddr
= kmap_atomic(page
, KM_USER0
);
753 memset(kaddr
+ bh_offset(bh
), 0, blocksize
);
754 kunmap_atomic(kaddr
, KM_USER0
);
755 flush_dcache_page(page
);
756 set_buffer_uptodate(bh
);
760 /* Failed to map the buffer, even after retrying. */
764 ntfs_error(vol
->sb
, "Failed to write to inode 0x%lx, "
765 "attribute type 0x%x, vcn 0x%llx, offset 0x%x "
766 "because its location on disk could not be "
767 "determined%s (error code %i).", ni
->mft_no
,
768 ni
->type
, (unsigned long long)vcn
,
769 vcn_ofs
, is_retry
? " even after "
770 "retrying" : "", err
);
772 } while (block
++, (bh
= bh
->b_this_page
) != head
);
774 /* Release the lock if we took it. */
776 up_read(&ni
->runlist
.lock
);
778 /* For the error case, need to reset bh to the beginning. */
781 /* Just an optimization, so ->readpage() is not called later. */
782 if (unlikely(!PageUptodate(page
))) {
785 if (!buffer_uptodate(bh
)) {
790 } while ((bh
= bh
->b_this_page
) != head
);
792 SetPageUptodate(page
);
795 /* Setup all mapped, dirty buffers for async write i/o. */
797 if (buffer_mapped(bh
) && buffer_dirty(bh
)) {
799 if (test_clear_buffer_dirty(bh
)) {
800 BUG_ON(!buffer_uptodate(bh
));
801 mark_buffer_async_write(bh
);
804 } else if (unlikely(err
)) {
806 * For the error case. The buffer may have been set
807 * dirty during attachment to a dirty page.
810 clear_buffer_dirty(bh
);
812 } while ((bh
= bh
->b_this_page
) != head
);
815 // TODO: Remove the -EOPNOTSUPP check later on...
816 if (unlikely(err
== -EOPNOTSUPP
))
818 else if (err
== -ENOMEM
) {
819 ntfs_warning(vol
->sb
, "Error allocating memory. "
820 "Redirtying page so we try again "
823 * Put the page back on mapping->dirty_pages, but
824 * leave its buffer's dirty state as-is.
826 redirty_page_for_writepage(wbc
, page
);
832 BUG_ON(PageWriteback(page
));
833 set_page_writeback(page
); /* Keeps try_to_free_buffers() away. */
835 /* Submit the prepared buffers for i/o. */
836 need_end_writeback
= TRUE
;
838 struct buffer_head
*next
= bh
->b_this_page
;
839 if (buffer_async_write(bh
)) {
840 submit_bh(WRITE
, bh
);
841 need_end_writeback
= FALSE
;
844 } while (bh
!= head
);
847 /* If no i/o was started, need to end_page_writeback(). */
848 if (unlikely(need_end_writeback
))
849 end_page_writeback(page
);
856 * ntfs_write_mst_block - write a @page to the backing store
857 * @page: page cache page to write out
858 * @wbc: writeback control structure
860 * This function is for writing pages belonging to non-resident, mst protected
861 * attributes to their backing store. The only supported attributes are index
862 * allocation and $MFT/$DATA. Both directory inodes and index inodes are
863 * supported for the index allocation case.
865 * The page must remain locked for the duration of the write because we apply
866 * the mst fixups, write, and then undo the fixups, so if we were to unlock the
867 * page before undoing the fixups, any other user of the page will see the
868 * page contents as corrupt.
870 * We clear the page uptodate flag for the duration of the function to ensure
871 * exclusion for the $MFT/$DATA case against someone mapping an mft record we
872 * are about to apply the mst fixups to.
874 * Return 0 on success and -errno on error.
876 * Based on ntfs_write_block(), ntfs_mft_writepage(), and
877 * write_mft_record_nolock().
879 static int ntfs_write_mst_block(struct page
*page
,
880 struct writeback_control
*wbc
)
882 sector_t block
, dblock
, rec_block
;
883 struct inode
*vi
= page
->mapping
->host
;
884 ntfs_inode
*ni
= NTFS_I(vi
);
885 ntfs_volume
*vol
= ni
->vol
;
887 unsigned int rec_size
= ni
->itype
.index
.block_size
;
888 ntfs_inode
*locked_nis
[PAGE_CACHE_SIZE
/ rec_size
];
889 struct buffer_head
*bh
, *head
, *tbh
, *rec_start_bh
;
890 struct buffer_head
*bhs
[MAX_BUF_PER_PAGE
];
892 int i
, nr_locked_nis
, nr_recs
, nr_bhs
, max_bhs
, bhs_per_rec
, err
, err2
;
893 unsigned bh_size
, rec_size_bits
;
894 BOOL sync
, is_mft
, page_is_dirty
, rec_is_dirty
;
895 unsigned char bh_size_bits
;
897 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
898 "0x%lx.", vi
->i_ino
, ni
->type
, page
->index
);
899 BUG_ON(!NInoNonResident(ni
));
900 BUG_ON(!NInoMstProtected(ni
));
901 is_mft
= (S_ISREG(vi
->i_mode
) && !vi
->i_ino
);
903 * NOTE: ntfs_write_mst_block() would be called for $MFTMirr if a page
904 * in its page cache were to be marked dirty. However this should
905 * never happen with the current driver and considering we do not
906 * handle this case here we do want to BUG(), at least for now.
908 BUG_ON(!(is_mft
|| S_ISDIR(vi
->i_mode
) ||
909 (NInoAttr(ni
) && ni
->type
== AT_INDEX_ALLOCATION
)));
910 bh_size_bits
= vi
->i_blkbits
;
911 bh_size
= 1 << bh_size_bits
;
912 max_bhs
= PAGE_CACHE_SIZE
/ bh_size
;
914 BUG_ON(max_bhs
> MAX_BUF_PER_PAGE
);
916 /* Were we called for sync purposes? */
917 sync
= (wbc
->sync_mode
== WB_SYNC_ALL
);
919 /* Make sure we have mapped buffers. */
920 bh
= head
= page_buffers(page
);
923 rec_size_bits
= ni
->itype
.index
.block_size_bits
;
924 BUG_ON(!(PAGE_CACHE_SIZE
>> rec_size_bits
));
925 bhs_per_rec
= rec_size
>> bh_size_bits
;
926 BUG_ON(!bhs_per_rec
);
928 /* The first block in the page. */
929 rec_block
= block
= (sector_t
)page
->index
<<
930 (PAGE_CACHE_SHIFT
- bh_size_bits
);
932 /* The first out of bounds block for the data size. */
933 dblock
= (i_size_read(vi
) + bh_size
- 1) >> bh_size_bits
;
936 err
= err2
= nr_bhs
= nr_recs
= nr_locked_nis
= 0;
937 page_is_dirty
= rec_is_dirty
= FALSE
;
940 BOOL is_retry
= FALSE
;
942 if (likely(block
< rec_block
)) {
943 if (unlikely(block
>= dblock
)) {
944 clear_buffer_dirty(bh
);
945 set_buffer_uptodate(bh
);
949 * This block is not the first one in the record. We
950 * ignore the buffer's dirty state because we could
951 * have raced with a parallel mark_ntfs_record_dirty().
955 if (unlikely(err2
)) {
957 clear_buffer_dirty(bh
);
960 } else /* if (block == rec_block) */ {
961 BUG_ON(block
> rec_block
);
962 /* This block is the first one in the record. */
963 rec_block
+= bhs_per_rec
;
965 if (unlikely(block
>= dblock
)) {
966 clear_buffer_dirty(bh
);
969 if (!buffer_dirty(bh
)) {
970 /* Clean records are not written out. */
971 rec_is_dirty
= FALSE
;
977 /* Need to map the buffer if it is not mapped already. */
978 if (unlikely(!buffer_mapped(bh
))) {
981 unsigned int vcn_ofs
;
983 bh
->b_bdev
= vol
->sb
->s_bdev
;
984 /* Obtain the vcn and offset of the current block. */
985 vcn
= (VCN
)block
<< bh_size_bits
;
986 vcn_ofs
= vcn
& vol
->cluster_size_mask
;
987 vcn
>>= vol
->cluster_size_bits
;
990 down_read(&ni
->runlist
.lock
);
993 if (likely(rl
!= NULL
)) {
994 /* Seek to element containing target vcn. */
995 while (rl
->length
&& rl
[1].vcn
<= vcn
)
997 lcn
= ntfs_rl_vcn_to_lcn(rl
, vcn
);
999 lcn
= LCN_RL_NOT_MAPPED
;
1000 /* Successful remap. */
1001 if (likely(lcn
>= 0)) {
1002 /* Setup buffer head to correct block. */
1003 bh
->b_blocknr
= ((lcn
<<
1004 vol
->cluster_size_bits
) +
1005 vcn_ofs
) >> bh_size_bits
;
1006 set_buffer_mapped(bh
);
1009 * Remap failed. Retry to map the runlist once
1010 * unless we are working on $MFT which always
1011 * has the whole of its runlist in memory.
1013 if (!is_mft
&& !is_retry
&&
1014 lcn
== LCN_RL_NOT_MAPPED
) {
1017 * Attempt to map runlist, dropping
1018 * lock for the duration.
1020 up_read(&ni
->runlist
.lock
);
1021 err2
= ntfs_map_runlist(ni
, vcn
);
1023 goto lock_retry_remap
;
1024 if (err2
== -ENOMEM
)
1025 page_is_dirty
= TRUE
;
1030 up_read(&ni
->runlist
.lock
);
1032 /* Hard error. Abort writing this record. */
1033 if (!err
|| err
== -ENOMEM
)
1036 ntfs_error(vol
->sb
, "Cannot write ntfs record "
1037 "0x%llx (inode 0x%lx, "
1038 "attribute type 0x%x) because "
1039 "its location on disk could "
1040 "not be determined (error "
1044 vol
->mft_record_size_bits
,
1045 ni
->mft_no
, ni
->type
,
1048 * If this is not the first buffer, remove the
1049 * buffers in this record from the list of
1050 * buffers to write and clear their dirty bit
1051 * if not error -ENOMEM.
1053 if (rec_start_bh
!= bh
) {
1054 while (bhs
[--nr_bhs
] != rec_start_bh
)
1056 if (err2
!= -ENOMEM
) {
1060 } while ((rec_start_bh
=
1069 BUG_ON(!buffer_uptodate(bh
));
1070 BUG_ON(nr_bhs
>= max_bhs
);
1072 } while (block
++, (bh
= bh
->b_this_page
) != head
);
1074 up_read(&ni
->runlist
.lock
);
1075 /* If there were no dirty buffers, we are done. */
1078 /* Map the page so we can access its contents. */
1080 /* Clear the page uptodate flag whilst the mst fixups are applied. */
1081 BUG_ON(!PageUptodate(page
));
1082 ClearPageUptodate(page
);
1083 for (i
= 0; i
< nr_bhs
; i
++) {
1086 /* Skip buffers which are not at the beginning of records. */
1087 if (i
% bhs_per_rec
)
1090 ofs
= bh_offset(tbh
);
1093 unsigned long mft_no
;
1095 /* Get the mft record number. */
1096 mft_no
= (((s64
)page
->index
<< PAGE_CACHE_SHIFT
) + ofs
)
1098 /* Check whether to write this mft record. */
1100 if (!ntfs_may_write_mft_record(vol
, mft_no
,
1101 (MFT_RECORD
*)(kaddr
+ ofs
), &tni
)) {
1103 * The record should not be written. This
1104 * means we need to redirty the page before
1107 page_is_dirty
= TRUE
;
1109 * Remove the buffers in this mft record from
1110 * the list of buffers to write.
1114 } while (++i
% bhs_per_rec
);
1118 * The record should be written. If a locked ntfs
1119 * inode was returned, add it to the array of locked
1123 locked_nis
[nr_locked_nis
++] = tni
;
1125 /* Apply the mst protection fixups. */
1126 err2
= pre_write_mst_fixup((NTFS_RECORD
*)(kaddr
+ ofs
),
1128 if (unlikely(err2
)) {
1129 if (!err
|| err
== -ENOMEM
)
1131 ntfs_error(vol
->sb
, "Failed to apply mst fixups "
1132 "(inode 0x%lx, attribute type 0x%x, "
1133 "page index 0x%lx, page offset 0x%x)!"
1134 " Unmount and run chkdsk.", vi
->i_ino
,
1135 ni
->type
, page
->index
, ofs
);
1137 * Mark all the buffers in this record clean as we do
1138 * not want to write corrupt data to disk.
1141 clear_buffer_dirty(bhs
[i
]);
1143 } while (++i
% bhs_per_rec
);
1148 /* If no records are to be written out, we are done. */
1151 flush_dcache_page(page
);
1152 /* Lock buffers and start synchronous write i/o on them. */
1153 for (i
= 0; i
< nr_bhs
; i
++) {
1157 if (unlikely(test_set_buffer_locked(tbh
)))
1159 /* The buffer dirty state is now irrelevant, just clean it. */
1160 clear_buffer_dirty(tbh
);
1161 BUG_ON(!buffer_uptodate(tbh
));
1162 BUG_ON(!buffer_mapped(tbh
));
1164 tbh
->b_end_io
= end_buffer_write_sync
;
1165 submit_bh(WRITE
, tbh
);
1167 /* Synchronize the mft mirror now if not @sync. */
1168 if (is_mft
&& !sync
)
1171 /* Wait on i/o completion of buffers. */
1172 for (i
= 0; i
< nr_bhs
; i
++) {
1176 wait_on_buffer(tbh
);
1177 if (unlikely(!buffer_uptodate(tbh
))) {
1178 ntfs_error(vol
->sb
, "I/O error while writing ntfs "
1179 "record buffer (inode 0x%lx, "
1180 "attribute type 0x%x, page index "
1181 "0x%lx, page offset 0x%lx)! Unmount "
1182 "and run chkdsk.", vi
->i_ino
, ni
->type
,
1183 page
->index
, bh_offset(tbh
));
1184 if (!err
|| err
== -ENOMEM
)
1187 * Set the buffer uptodate so the page and buffer
1188 * states do not become out of sync.
1190 set_buffer_uptodate(tbh
);
1193 /* If @sync, now synchronize the mft mirror. */
1194 if (is_mft
&& sync
) {
1196 for (i
= 0; i
< nr_bhs
; i
++) {
1197 unsigned long mft_no
;
1201 * Skip buffers which are not at the beginning of
1204 if (i
% bhs_per_rec
)
1207 /* Skip removed buffers (and hence records). */
1210 ofs
= bh_offset(tbh
);
1211 /* Get the mft record number. */
1212 mft_no
= (((s64
)page
->index
<< PAGE_CACHE_SHIFT
) + ofs
)
1214 if (mft_no
< vol
->mftmirr_size
)
1215 ntfs_sync_mft_mirror(vol
, mft_no
,
1216 (MFT_RECORD
*)(kaddr
+ ofs
),
1222 /* Remove the mst protection fixups again. */
1223 for (i
= 0; i
< nr_bhs
; i
++) {
1224 if (!(i
% bhs_per_rec
)) {
1228 post_write_mst_fixup((NTFS_RECORD
*)(kaddr
+
1232 flush_dcache_page(page
);
1234 /* Unlock any locked inodes. */
1235 while (nr_locked_nis
-- > 0) {
1236 ntfs_inode
*tni
, *base_tni
;
1238 tni
= locked_nis
[nr_locked_nis
];
1239 /* Get the base inode. */
1240 down(&tni
->extent_lock
);
1241 if (tni
->nr_extents
>= 0)
1244 base_tni
= tni
->ext
.base_ntfs_ino
;
1247 up(&tni
->extent_lock
);
1248 ntfs_debug("Unlocking %s inode 0x%lx.",
1249 tni
== base_tni
? "base" : "extent",
1251 up(&tni
->mrec_lock
);
1252 atomic_dec(&tni
->count
);
1253 iput(VFS_I(base_tni
));
1255 SetPageUptodate(page
);
1258 if (unlikely(err
&& err
!= -ENOMEM
)) {
1260 * Set page error if there is only one ntfs record in the page.
1261 * Otherwise we would loose per-record granularity.
1263 if (ni
->itype
.index
.block_size
== PAGE_CACHE_SIZE
)
1267 if (page_is_dirty
) {
1268 ntfs_debug("Page still contains one or more dirty ntfs "
1269 "records. Redirtying the page starting at "
1270 "record 0x%lx.", page
->index
<<
1271 (PAGE_CACHE_SHIFT
- rec_size_bits
));
1272 redirty_page_for_writepage(wbc
, page
);
1276 * Keep the VM happy. This must be done otherwise the
1277 * radix-tree tag PAGECACHE_TAG_DIRTY remains set even though
1278 * the page is clean.
1280 BUG_ON(PageWriteback(page
));
1281 set_page_writeback(page
);
1283 end_page_writeback(page
);
1286 ntfs_debug("Done.");
1291 * ntfs_writepage - write a @page to the backing store
1292 * @page: page cache page to write out
1293 * @wbc: writeback control structure
1295 * This is called from the VM when it wants to have a dirty ntfs page cache
1296 * page cleaned. The VM has already locked the page and marked it clean.
1298 * For non-resident attributes, ntfs_writepage() writes the @page by calling
1299 * the ntfs version of the generic block_write_full_page() function,
1300 * ntfs_write_block(), which in turn if necessary creates and writes the
1301 * buffers associated with the page asynchronously.
1303 * For resident attributes, OTOH, ntfs_writepage() writes the @page by copying
1304 * the data to the mft record (which at this stage is most likely in memory).
1305 * The mft record is then marked dirty and written out asynchronously via the
1306 * vfs inode dirty code path for the inode the mft record belongs to or via the
1307 * vm page dirty code path for the page the mft record is in.
1309 * Based on ntfs_readpage() and fs/buffer.c::block_write_full_page().
1311 * Return 0 on success and -errno on error.
1313 static int ntfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1316 struct inode
*vi
= page
->mapping
->host
;
1317 ntfs_inode
*base_ni
= NULL
, *ni
= NTFS_I(vi
);
1319 ntfs_attr_search_ctx
*ctx
= NULL
;
1320 MFT_RECORD
*m
= NULL
;
1325 BUG_ON(!PageLocked(page
));
1326 i_size
= i_size_read(vi
);
1327 /* Is the page fully outside i_size? (truncate in progress) */
1328 if (unlikely(page
->index
>= (i_size
+ PAGE_CACHE_SIZE
- 1) >>
1329 PAGE_CACHE_SHIFT
)) {
1331 * The page may have dirty, unmapped buffers. Make them
1332 * freeable here, so the page does not leak.
1334 block_invalidatepage(page
, 0);
1336 ntfs_debug("Write outside i_size - truncated?");
1340 * Only $DATA attributes can be encrypted and only unnamed $DATA
1341 * attributes can be compressed. Index root can have the flags set but
1342 * this means to create compressed/encrypted files, not that the
1343 * attribute is compressed/encrypted.
1345 if (ni
->type
!= AT_INDEX_ROOT
) {
1346 /* If file is encrypted, deny access, just like NT4. */
1347 if (NInoEncrypted(ni
)) {
1349 BUG_ON(ni
->type
!= AT_DATA
);
1350 ntfs_debug("Denying write access to encrypted "
1354 /* Compressed data streams are handled in compress.c. */
1355 if (NInoNonResident(ni
) && NInoCompressed(ni
)) {
1356 BUG_ON(ni
->type
!= AT_DATA
);
1357 BUG_ON(ni
->name_len
);
1358 // TODO: Implement and replace this with
1359 // return ntfs_write_compressed_block(page);
1361 ntfs_error(vi
->i_sb
, "Writing to compressed files is "
1362 "not supported yet. Sorry.");
1365 // TODO: Implement and remove this check.
1366 if (NInoNonResident(ni
) && NInoSparse(ni
)) {
1368 ntfs_error(vi
->i_sb
, "Writing to sparse files is not "
1369 "supported yet. Sorry.");
1373 /* NInoNonResident() == NInoIndexAllocPresent() */
1374 if (NInoNonResident(ni
)) {
1375 /* We have to zero every time due to mmap-at-end-of-file. */
1376 if (page
->index
>= (i_size
>> PAGE_CACHE_SHIFT
)) {
1377 /* The page straddles i_size. */
1378 unsigned int ofs
= i_size
& ~PAGE_CACHE_MASK
;
1379 kaddr
= kmap_atomic(page
, KM_USER0
);
1380 memset(kaddr
+ ofs
, 0, PAGE_CACHE_SIZE
- ofs
);
1381 flush_dcache_page(page
);
1382 kunmap_atomic(kaddr
, KM_USER0
);
1384 /* Handle mst protected attributes. */
1385 if (NInoMstProtected(ni
))
1386 return ntfs_write_mst_block(page
, wbc
);
1387 /* Normal, non-resident data stream. */
1388 return ntfs_write_block(page
, wbc
);
1391 * Attribute is resident, implying it is not compressed, encrypted, or
1392 * mst protected. This also means the attribute is smaller than an mft
1393 * record and hence smaller than a page, so can simply return error on
1394 * any pages with index above 0. Note the attribute can actually be
1395 * marked compressed but if it is resident the actual data is not
1396 * compressed so we are ok to ignore the compressed flag here.
1398 BUG_ON(page_has_buffers(page
));
1399 BUG_ON(!PageUptodate(page
));
1400 if (unlikely(page
->index
> 0)) {
1401 ntfs_error(vi
->i_sb
, "BUG()! page->index (0x%lx) > 0. "
1402 "Aborting write.", page
->index
);
1403 BUG_ON(PageWriteback(page
));
1404 set_page_writeback(page
);
1406 end_page_writeback(page
);
1412 base_ni
= ni
->ext
.base_ntfs_ino
;
1413 /* Map, pin, and lock the mft record. */
1414 m
= map_mft_record(base_ni
);
1422 * If a parallel write made the attribute non-resident, drop the mft
1423 * record and retry the writepage.
1425 if (unlikely(NInoNonResident(ni
))) {
1426 unmap_mft_record(base_ni
);
1427 goto retry_writepage
;
1429 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
1430 if (unlikely(!ctx
)) {
1434 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
1435 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
1439 * Keep the VM happy. This must be done otherwise the radix-tree tag
1440 * PAGECACHE_TAG_DIRTY remains set even though the page is clean.
1442 BUG_ON(PageWriteback(page
));
1443 set_page_writeback(page
);
1446 * Here, we do not need to zero the out of bounds area everytime
1447 * because the below memcpy() already takes care of the
1448 * mmap-at-end-of-file requirements. If the file is converted to a
1449 * non-resident one, then the code path use is switched to the
1450 * non-resident one where the zeroing happens on each ntfs_writepage()
1453 attr_len
= le32_to_cpu(ctx
->attr
->data
.resident
.value_length
);
1454 i_size
= i_size_read(vi
);
1455 if (unlikely(attr_len
> i_size
)) {
1457 ctx
->attr
->data
.resident
.value_length
= cpu_to_le32(attr_len
);
1459 kaddr
= kmap_atomic(page
, KM_USER0
);
1460 /* Copy the data from the page to the mft record. */
1461 memcpy((u8
*)ctx
->attr
+
1462 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
),
1464 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1465 /* Zero out of bounds area in the page cache page. */
1466 memset(kaddr
+ attr_len
, 0, PAGE_CACHE_SIZE
- attr_len
);
1467 flush_dcache_page(page
);
1468 kunmap_atomic(kaddr
, KM_USER0
);
1470 end_page_writeback(page
);
1472 /* Mark the mft record dirty, so it gets written back. */
1473 mark_mft_record_dirty(ctx
->ntfs_ino
);
1474 ntfs_attr_put_search_ctx(ctx
);
1475 unmap_mft_record(base_ni
);
1478 if (err
== -ENOMEM
) {
1479 ntfs_warning(vi
->i_sb
, "Error allocating memory. Redirtying "
1480 "page so we try again later.");
1482 * Put the page back on mapping->dirty_pages, but leave its
1483 * buffers' dirty state as-is.
1485 redirty_page_for_writepage(wbc
, page
);
1488 ntfs_error(vi
->i_sb
, "Resident attribute write failed with "
1491 NVolSetErrors(ni
->vol
);
1496 ntfs_attr_put_search_ctx(ctx
);
1498 unmap_mft_record(base_ni
);
1503 * ntfs_prepare_nonresident_write -
1506 static int ntfs_prepare_nonresident_write(struct page
*page
,
1507 unsigned from
, unsigned to
)
1511 s64 initialized_size
;
1513 sector_t block
, ablock
, iblock
;
1517 runlist_element
*rl
;
1518 struct buffer_head
*bh
, *head
, *wait
[2], **wait_bh
= wait
;
1519 unsigned long flags
;
1520 unsigned int vcn_ofs
, block_start
, block_end
, blocksize
;
1523 unsigned char blocksize_bits
;
1525 vi
= page
->mapping
->host
;
1529 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
1530 "0x%lx, from = %u, to = %u.", ni
->mft_no
, ni
->type
,
1531 page
->index
, from
, to
);
1533 BUG_ON(!NInoNonResident(ni
));
1535 blocksize_bits
= vi
->i_blkbits
;
1536 blocksize
= 1 << blocksize_bits
;
1539 * create_empty_buffers() will create uptodate/dirty buffers if the
1540 * page is uptodate/dirty.
1542 if (!page_has_buffers(page
))
1543 create_empty_buffers(page
, blocksize
, 0);
1544 bh
= head
= page_buffers(page
);
1548 /* The first block in the page. */
1549 block
= (s64
)page
->index
<< (PAGE_CACHE_SHIFT
- blocksize_bits
);
1551 read_lock_irqsave(&ni
->size_lock
, flags
);
1553 * The first out of bounds block for the allocated size. No need to
1554 * round up as allocated_size is in multiples of cluster size and the
1555 * minimum cluster size is 512 bytes, which is equal to the smallest
1558 ablock
= ni
->allocated_size
>> blocksize_bits
;
1559 i_size
= i_size_read(vi
);
1560 initialized_size
= ni
->initialized_size
;
1561 read_unlock_irqrestore(&ni
->size_lock
, flags
);
1563 /* The last (fully or partially) initialized block. */
1564 iblock
= initialized_size
>> blocksize_bits
;
1566 /* Loop through all the buffers in the page. */
1571 block_end
= block_start
+ blocksize
;
1573 * If buffer @bh is outside the write, just mark it uptodate
1574 * if the page is uptodate and continue with the next buffer.
1576 if (block_end
<= from
|| block_start
>= to
) {
1577 if (PageUptodate(page
)) {
1578 if (!buffer_uptodate(bh
))
1579 set_buffer_uptodate(bh
);
1584 * @bh is at least partially being written to.
1585 * Make sure it is not marked as new.
1587 //if (buffer_new(bh))
1588 // clear_buffer_new(bh);
1590 if (block
>= ablock
) {
1591 // TODO: block is above allocated_size, need to
1592 // allocate it. Best done in one go to accommodate not
1593 // only block but all above blocks up to and including:
1594 // ((page->index << PAGE_CACHE_SHIFT) + to + blocksize
1595 // - 1) >> blobksize_bits. Obviously will need to round
1596 // up to next cluster boundary, too. This should be
1597 // done with a helper function, so it can be reused.
1598 ntfs_error(vol
->sb
, "Writing beyond allocated size "
1599 "is not supported yet. Sorry.");
1602 // Need to update ablock.
1603 // Need to set_buffer_new() on all block bhs that are
1607 * Now we have enough allocated size to fulfill the whole
1608 * request, i.e. block < ablock is true.
1610 if (unlikely((block
>= iblock
) &&
1611 (initialized_size
< i_size
))) {
1613 * If this page is fully outside initialized size, zero
1614 * out all pages between the current initialized size
1615 * and the current page. Just use ntfs_readpage() to do
1616 * the zeroing transparently.
1618 if (block
> iblock
) {
1620 // For each page do:
1621 // - read_cache_page()
1622 // Again for each page do:
1623 // - wait_on_page_locked()
1624 // - Check (PageUptodate(page) &&
1625 // !PageError(page))
1626 // Update initialized size in the attribute and
1628 // Again, for each page do:
1629 // __set_page_dirty_buffers();
1630 // page_cache_release()
1631 // We don't need to wait on the writes.
1635 * The current page straddles initialized size. Zero
1636 * all non-uptodate buffers and set them uptodate (and
1637 * dirty?). Note, there aren't any non-uptodate buffers
1638 * if the page is uptodate.
1639 * FIXME: For an uptodate page, the buffers may need to
1640 * be written out because they were not initialized on
1643 if (!PageUptodate(page
)) {
1645 // Zero any non-uptodate buffers up to i_size.
1646 // Set them uptodate and dirty.
1649 // Update initialized size in the attribute and in the
1650 // inode (up to i_size).
1652 // FIXME: This is inefficient. Try to batch the two
1653 // size changes to happen in one go.
1654 ntfs_error(vol
->sb
, "Writing beyond initialized size "
1655 "is not supported yet. Sorry.");
1658 // Do NOT set_buffer_new() BUT DO clear buffer range
1659 // outside write request range.
1660 // set_buffer_uptodate() on complete buffers as well as
1661 // set_buffer_dirty().
1664 /* Need to map unmapped buffers. */
1665 if (!buffer_mapped(bh
)) {
1666 /* Unmapped buffer. Need to map it. */
1667 bh
->b_bdev
= vol
->sb
->s_bdev
;
1669 /* Convert block into corresponding vcn and offset. */
1670 vcn
= (VCN
)block
<< blocksize_bits
>>
1671 vol
->cluster_size_bits
;
1672 vcn_ofs
= ((VCN
)block
<< blocksize_bits
) &
1673 vol
->cluster_size_mask
;
1678 down_read(&ni
->runlist
.lock
);
1679 rl
= ni
->runlist
.rl
;
1681 if (likely(rl
!= NULL
)) {
1682 /* Seek to element containing target vcn. */
1683 while (rl
->length
&& rl
[1].vcn
<= vcn
)
1685 lcn
= ntfs_rl_vcn_to_lcn(rl
, vcn
);
1687 lcn
= LCN_RL_NOT_MAPPED
;
1688 if (unlikely(lcn
< 0)) {
1690 * We extended the attribute allocation above.
1691 * If we hit an ENOENT here it means that the
1692 * allocation was insufficient which is a bug.
1694 BUG_ON(lcn
== LCN_ENOENT
);
1696 /* It is a hole, need to instantiate it. */
1697 if (lcn
== LCN_HOLE
) {
1698 // TODO: Instantiate the hole.
1699 // clear_buffer_new(bh);
1700 // unmap_underlying_metadata(bh->b_bdev,
1702 // For non-uptodate buffers, need to
1703 // zero out the region outside the
1704 // request in this bh or all bhs,
1705 // depending on what we implemented
1707 // Need to flush_dcache_page().
1708 // Or could use set_buffer_new()
1710 ntfs_error(vol
->sb
, "Writing into "
1711 "sparse regions is "
1712 "not supported yet. "
1716 up_read(&ni
->runlist
.lock
);
1718 } else if (!is_retry
&&
1719 lcn
== LCN_RL_NOT_MAPPED
) {
1722 * Attempt to map runlist, dropping
1723 * lock for the duration.
1725 up_read(&ni
->runlist
.lock
);
1726 err
= ntfs_map_runlist(ni
, vcn
);
1728 goto lock_retry_remap
;
1731 up_read(&ni
->runlist
.lock
);
1733 * Failed to map the buffer, even after
1739 ntfs_error(vol
->sb
, "Failed to write to inode "
1740 "0x%lx, attribute type 0x%x, "
1741 "vcn 0x%llx, offset 0x%x "
1742 "because its location on disk "
1743 "could not be determined%s "
1745 ni
->mft_no
, ni
->type
,
1746 (unsigned long long)vcn
,
1747 vcn_ofs
, is_retry
? " even "
1748 "after retrying" : "", err
);
1751 /* We now have a successful remap, i.e. lcn >= 0. */
1753 /* Setup buffer head to correct block. */
1754 bh
->b_blocknr
= ((lcn
<< vol
->cluster_size_bits
)
1755 + vcn_ofs
) >> blocksize_bits
;
1756 set_buffer_mapped(bh
);
1758 // FIXME: Something analogous to this is needed for
1759 // each newly allocated block, i.e. BH_New.
1760 // FIXME: Might need to take this out of the
1761 // if (!buffer_mapped(bh)) {}, depending on how we
1762 // implement things during the allocated_size and
1763 // initialized_size extension code above.
1764 if (buffer_new(bh
)) {
1765 clear_buffer_new(bh
);
1766 unmap_underlying_metadata(bh
->b_bdev
,
1768 if (PageUptodate(page
)) {
1769 set_buffer_uptodate(bh
);
1773 * Page is _not_ uptodate, zero surrounding
1774 * region. NOTE: This is how we decide if to
1777 if (block_end
> to
|| block_start
< from
) {
1780 kaddr
= kmap_atomic(page
, KM_USER0
);
1782 memset(kaddr
+ to
, 0,
1784 if (block_start
< from
)
1785 memset(kaddr
+ block_start
, 0,
1788 flush_dcache_page(page
);
1789 kunmap_atomic(kaddr
, KM_USER0
);
1794 /* @bh is mapped, set it uptodate if the page is uptodate. */
1795 if (PageUptodate(page
)) {
1796 if (!buffer_uptodate(bh
))
1797 set_buffer_uptodate(bh
);
1801 * The page is not uptodate. The buffer is mapped. If it is not
1802 * uptodate, and it is only partially being written to, we need
1803 * to read the buffer in before the write, i.e. right now.
1805 if (!buffer_uptodate(bh
) &&
1806 (block_start
< from
|| block_end
> to
)) {
1807 ll_rw_block(READ
, 1, &bh
);
1810 } while (block
++, block_start
= block_end
,
1811 (bh
= bh
->b_this_page
) != head
);
1813 /* Release the lock if we took it. */
1815 up_read(&ni
->runlist
.lock
);
1819 /* If we issued read requests, let them complete. */
1820 while (wait_bh
> wait
) {
1821 wait_on_buffer(*--wait_bh
);
1822 if (!buffer_uptodate(*wait_bh
))
1826 ntfs_debug("Done.");
1830 * Zero out any newly allocated blocks to avoid exposing stale data.
1831 * If BH_New is set, we know that the block was newly allocated in the
1833 * FIXME: What about initialized_size increments? Have we done all the
1834 * required zeroing above? If not this error handling is broken, and
1835 * in particular the if (block_end <= from) check is completely bogus.
1841 block_end
= block_start
+ blocksize
;
1842 if (block_end
<= from
)
1844 if (block_start
>= to
)
1846 if (buffer_new(bh
)) {
1849 clear_buffer_new(bh
);
1850 kaddr
= kmap_atomic(page
, KM_USER0
);
1851 memset(kaddr
+ block_start
, 0, bh
->b_size
);
1852 kunmap_atomic(kaddr
, KM_USER0
);
1853 set_buffer_uptodate(bh
);
1854 mark_buffer_dirty(bh
);
1857 } while (block_start
= block_end
, (bh
= bh
->b_this_page
) != head
);
1859 flush_dcache_page(page
);
1861 up_read(&ni
->runlist
.lock
);
1866 * ntfs_prepare_write - prepare a page for receiving data
1868 * This is called from generic_file_write() with i_sem held on the inode
1869 * (@page->mapping->host). The @page is locked but not kmap()ped. The source
1870 * data has not yet been copied into the @page.
1872 * Need to extend the attribute/fill in holes if necessary, create blocks and
1873 * make partially overwritten blocks uptodate,
1875 * i_size is not to be modified yet.
1877 * Return 0 on success or -errno on error.
1879 * Should be using block_prepare_write() [support for sparse files] or
1880 * cont_prepare_write() [no support for sparse files]. Cannot do that due to
1881 * ntfs specifics but can look at them for implementation guidance.
1883 * Note: In the range, @from is inclusive and @to is exclusive, i.e. @from is
1884 * the first byte in the page that will be written to and @to is the first byte
1885 * after the last byte that will be written to.
1887 static int ntfs_prepare_write(struct file
*file
, struct page
*page
,
1888 unsigned from
, unsigned to
)
1892 struct inode
*vi
= page
->mapping
->host
;
1893 ntfs_inode
*base_ni
= NULL
, *ni
= NTFS_I(vi
);
1894 ntfs_volume
*vol
= ni
->vol
;
1895 ntfs_attr_search_ctx
*ctx
= NULL
;
1896 MFT_RECORD
*m
= NULL
;
1902 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
1903 "0x%lx, from = %u, to = %u.", vi
->i_ino
, ni
->type
,
1904 page
->index
, from
, to
);
1905 BUG_ON(!PageLocked(page
));
1906 BUG_ON(from
> PAGE_CACHE_SIZE
);
1907 BUG_ON(to
> PAGE_CACHE_SIZE
);
1909 BUG_ON(NInoMstProtected(ni
));
1911 * If a previous ntfs_truncate() failed, repeat it and abort if it
1914 if (unlikely(NInoTruncateFailed(ni
))) {
1915 down_write(&vi
->i_alloc_sem
);
1916 err
= ntfs_truncate(vi
);
1917 up_write(&vi
->i_alloc_sem
);
1918 if (err
|| NInoTruncateFailed(ni
)) {
1924 /* If the attribute is not resident, deal with it elsewhere. */
1925 if (NInoNonResident(ni
)) {
1927 * Only unnamed $DATA attributes can be compressed, encrypted,
1930 if (ni
->type
== AT_DATA
&& !ni
->name_len
) {
1931 /* If file is encrypted, deny access, just like NT4. */
1932 if (NInoEncrypted(ni
)) {
1933 ntfs_debug("Denying write access to encrypted "
1937 /* Compressed data streams are handled in compress.c. */
1938 if (NInoCompressed(ni
)) {
1939 // TODO: Implement and replace this check with
1940 // return ntfs_write_compressed_block(page);
1941 ntfs_error(vi
->i_sb
, "Writing to compressed "
1942 "files is not supported yet. "
1946 // TODO: Implement and remove this check.
1947 if (NInoSparse(ni
)) {
1948 ntfs_error(vi
->i_sb
, "Writing to sparse files "
1949 "is not supported yet. Sorry.");
1953 /* Normal data stream. */
1954 return ntfs_prepare_nonresident_write(page
, from
, to
);
1957 * Attribute is resident, implying it is not compressed, encrypted, or
1960 BUG_ON(page_has_buffers(page
));
1961 new_size
= ((s64
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
1962 /* If we do not need to resize the attribute allocation we are done. */
1963 if (new_size
<= i_size_read(vi
))
1965 /* Map, pin, and lock the (base) mft record. */
1969 base_ni
= ni
->ext
.base_ntfs_ino
;
1970 m
= map_mft_record(base_ni
);
1977 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
1978 if (unlikely(!ctx
)) {
1982 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
1983 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
1984 if (unlikely(err
)) {
1991 /* The total length of the attribute value. */
1992 attr_len
= le32_to_cpu(a
->data
.resident
.value_length
);
1993 /* Fix an eventual previous failure of ntfs_commit_write(). */
1994 i_size
= i_size_read(vi
);
1995 if (unlikely(attr_len
> i_size
)) {
1997 a
->data
.resident
.value_length
= cpu_to_le32(attr_len
);
1999 /* If we do not need to resize the attribute allocation we are done. */
2000 if (new_size
<= attr_len
)
2002 /* Check if new size is allowed in $AttrDef. */
2003 err
= ntfs_attr_size_bounds_check(vol
, ni
->type
, new_size
);
2004 if (unlikely(err
)) {
2005 if (err
== -ERANGE
) {
2006 ntfs_error(vol
->sb
, "Write would cause the inode "
2007 "0x%lx to exceed the maximum size for "
2008 "its attribute type (0x%x). Aborting "
2009 "write.", vi
->i_ino
,
2010 le32_to_cpu(ni
->type
));
2012 ntfs_error(vol
->sb
, "Inode 0x%lx has unknown "
2013 "attribute type 0x%x. Aborting "
2014 "write.", vi
->i_ino
,
2015 le32_to_cpu(ni
->type
));
2021 * Extend the attribute record to be able to store the new attribute
2024 if (new_size
>= vol
->mft_record_size
|| ntfs_attr_record_resize(m
, a
,
2025 le16_to_cpu(a
->data
.resident
.value_offset
) +
2027 /* Not enough space in the mft record. */
2028 ntfs_error(vol
->sb
, "Not enough space in the mft record for "
2029 "the resized attribute value. This is not "
2030 "supported yet. Aborting write.");
2035 * We have enough space in the mft record to fit the write. This
2036 * implies the attribute is smaller than the mft record and hence the
2037 * attribute must be in a single page and hence page->index must be 0.
2039 BUG_ON(page
->index
);
2041 * If the beginning of the write is past the old size, enlarge the
2042 * attribute value up to the beginning of the write and fill it with
2045 if (from
> attr_len
) {
2046 memset((u8
*)a
+ le16_to_cpu(a
->data
.resident
.value_offset
) +
2047 attr_len
, 0, from
- attr_len
);
2048 a
->data
.resident
.value_length
= cpu_to_le32(from
);
2049 /* Zero the corresponding area in the page as well. */
2050 if (PageUptodate(page
)) {
2051 kaddr
= kmap_atomic(page
, KM_USER0
);
2052 memset(kaddr
+ attr_len
, 0, from
- attr_len
);
2053 kunmap_atomic(kaddr
, KM_USER0
);
2054 flush_dcache_page(page
);
2057 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
2058 mark_mft_record_dirty(ctx
->ntfs_ino
);
2060 ntfs_attr_put_search_ctx(ctx
);
2061 unmap_mft_record(base_ni
);
2063 * Because resident attributes are handled by memcpy() to/from the
2064 * corresponding MFT record, and because this form of i/o is byte
2065 * aligned rather than block aligned, there is no need to bring the
2066 * page uptodate here as in the non-resident case where we need to
2067 * bring the buffers straddled by the write uptodate before
2068 * generic_file_write() does the copying from userspace.
2070 * We thus defer the uptodate bringing of the page region outside the
2071 * region written to to ntfs_commit_write(), which makes the code
2072 * simpler and saves one atomic kmap which is good.
2075 ntfs_debug("Done.");
2079 ntfs_warning(vi
->i_sb
, "Error allocating memory required to "
2080 "prepare the write.");
2082 ntfs_error(vi
->i_sb
, "Resident attribute prepare write failed "
2083 "with error %i.", err
);
2089 ntfs_attr_put_search_ctx(ctx
);
2091 unmap_mft_record(base_ni
);
2096 * ntfs_commit_nonresident_write -
2099 static int ntfs_commit_nonresident_write(struct page
*page
,
2100 unsigned from
, unsigned to
)
2102 s64 pos
= ((s64
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
2103 struct inode
*vi
= page
->mapping
->host
;
2104 struct buffer_head
*bh
, *head
;
2105 unsigned int block_start
, block_end
, blocksize
;
2108 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
2109 "0x%lx, from = %u, to = %u.", vi
->i_ino
,
2110 NTFS_I(vi
)->type
, page
->index
, from
, to
);
2111 blocksize
= 1 << vi
->i_blkbits
;
2113 // FIXME: We need a whole slew of special cases in here for compressed
2114 // files for example...
2115 // For now, we know ntfs_prepare_write() would have failed so we can't
2116 // get here in any of the cases which we have to special case, so we
2117 // are just a ripped off, unrolled generic_commit_write().
2119 bh
= head
= page_buffers(page
);
2123 block_end
= block_start
+ blocksize
;
2124 if (block_end
<= from
|| block_start
>= to
) {
2125 if (!buffer_uptodate(bh
))
2128 set_buffer_uptodate(bh
);
2129 mark_buffer_dirty(bh
);
2131 } while (block_start
= block_end
, (bh
= bh
->b_this_page
) != head
);
2133 * If this is a partial write which happened to make all buffers
2134 * uptodate then we can optimize away a bogus ->readpage() for the next
2135 * read(). Here we 'discover' whether the page went uptodate as a
2136 * result of this (potentially partial) write.
2139 SetPageUptodate(page
);
2141 * Not convinced about this at all. See disparity comment above. For
2142 * now we know ntfs_prepare_write() would have failed in the write
2143 * exceeds i_size case, so this will never trigger which is fine.
2145 if (pos
> i_size_read(vi
)) {
2146 ntfs_error(vi
->i_sb
, "Writing beyond the existing file size is "
2147 "not supported yet. Sorry.");
2149 // vi->i_size = pos;
2150 // mark_inode_dirty(vi);
2152 ntfs_debug("Done.");
2157 * ntfs_commit_write - commit the received data
2159 * This is called from generic_file_write() with i_sem held on the inode
2160 * (@page->mapping->host). The @page is locked but not kmap()ped. The source
2161 * data has already been copied into the @page. ntfs_prepare_write() has been
2162 * called before the data copied and it returned success so we can take the
2163 * results of various BUG checks and some error handling for granted.
2165 * Need to mark modified blocks dirty so they get written out later when
2166 * ntfs_writepage() is invoked by the VM.
2168 * Return 0 on success or -errno on error.
2170 * Should be using generic_commit_write(). This marks buffers uptodate and
2171 * dirty, sets the page uptodate if all buffers in the page are uptodate, and
2172 * updates i_size if the end of io is beyond i_size. In that case, it also
2173 * marks the inode dirty.
2175 * Cannot use generic_commit_write() due to ntfs specialities but can look at
2176 * it for implementation guidance.
2178 * If things have gone as outlined in ntfs_prepare_write(), then we do not
2179 * need to do any page content modifications here at all, except in the write
2180 * to resident attribute case, where we need to do the uptodate bringing here
2181 * which we combine with the copying into the mft record which means we save
2184 static int ntfs_commit_write(struct file
*file
, struct page
*page
,
2185 unsigned from
, unsigned to
)
2187 struct inode
*vi
= page
->mapping
->host
;
2188 ntfs_inode
*base_ni
, *ni
= NTFS_I(vi
);
2189 char *kaddr
, *kattr
;
2190 ntfs_attr_search_ctx
*ctx
;
2196 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
2197 "0x%lx, from = %u, to = %u.", vi
->i_ino
, ni
->type
,
2198 page
->index
, from
, to
);
2199 /* If the attribute is not resident, deal with it elsewhere. */
2200 if (NInoNonResident(ni
)) {
2201 /* Only unnamed $DATA attributes can be compressed/encrypted. */
2202 if (ni
->type
== AT_DATA
&& !ni
->name_len
) {
2203 /* Encrypted files need separate handling. */
2204 if (NInoEncrypted(ni
)) {
2205 // We never get here at present!
2208 /* Compressed data streams are handled in compress.c. */
2209 if (NInoCompressed(ni
)) {
2210 // TODO: Implement this!
2211 // return ntfs_write_compressed_block(page);
2212 // We never get here at present!
2216 /* Normal data stream. */
2217 return ntfs_commit_nonresident_write(page
, from
, to
);
2220 * Attribute is resident, implying it is not compressed, encrypted, or
2226 base_ni
= ni
->ext
.base_ntfs_ino
;
2227 /* Map, pin, and lock the mft record. */
2228 m
= map_mft_record(base_ni
);
2235 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
2236 if (unlikely(!ctx
)) {
2240 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
2241 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
2242 if (unlikely(err
)) {
2248 /* The total length of the attribute value. */
2249 attr_len
= le32_to_cpu(a
->data
.resident
.value_length
);
2250 BUG_ON(from
> attr_len
);
2251 kattr
= (u8
*)a
+ le16_to_cpu(a
->data
.resident
.value_offset
);
2252 kaddr
= kmap_atomic(page
, KM_USER0
);
2253 /* Copy the received data from the page to the mft record. */
2254 memcpy(kattr
+ from
, kaddr
+ from
, to
- from
);
2255 /* Update the attribute length if necessary. */
2256 if (to
> attr_len
) {
2258 a
->data
.resident
.value_length
= cpu_to_le32(attr_len
);
2261 * If the page is not uptodate, bring the out of bounds area(s)
2262 * uptodate by copying data from the mft record to the page.
2264 if (!PageUptodate(page
)) {
2266 memcpy(kaddr
, kattr
, from
);
2268 memcpy(kaddr
+ to
, kattr
+ to
, attr_len
- to
);
2269 /* Zero the region outside the end of the attribute value. */
2270 if (attr_len
< PAGE_CACHE_SIZE
)
2271 memset(kaddr
+ attr_len
, 0, PAGE_CACHE_SIZE
- attr_len
);
2273 * The probability of not having done any of the above is
2274 * extremely small, so we just flush unconditionally.
2276 flush_dcache_page(page
);
2277 SetPageUptodate(page
);
2279 kunmap_atomic(kaddr
, KM_USER0
);
2280 /* Update i_size if necessary. */
2281 if (i_size_read(vi
) < attr_len
) {
2282 unsigned long flags
;
2284 write_lock_irqsave(&ni
->size_lock
, flags
);
2285 ni
->allocated_size
= ni
->initialized_size
= attr_len
;
2286 i_size_write(vi
, attr_len
);
2287 write_unlock_irqrestore(&ni
->size_lock
, flags
);
2289 /* Mark the mft record dirty, so it gets written back. */
2290 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
2291 mark_mft_record_dirty(ctx
->ntfs_ino
);
2292 ntfs_attr_put_search_ctx(ctx
);
2293 unmap_mft_record(base_ni
);
2294 ntfs_debug("Done.");
2297 if (err
== -ENOMEM
) {
2298 ntfs_warning(vi
->i_sb
, "Error allocating memory required to "
2299 "commit the write.");
2300 if (PageUptodate(page
)) {
2301 ntfs_warning(vi
->i_sb
, "Page is uptodate, setting "
2302 "dirty so the write will be retried "
2303 "later on by the VM.");
2305 * Put the page on mapping->dirty_pages, but leave its
2306 * buffers' dirty state as-is.
2308 __set_page_dirty_nobuffers(page
);
2311 ntfs_error(vi
->i_sb
, "Page is not uptodate. Written "
2312 "data has been lost.");
2314 ntfs_error(vi
->i_sb
, "Resident attribute commit write failed "
2315 "with error %i.", err
);
2316 NVolSetErrors(ni
->vol
);
2320 ntfs_attr_put_search_ctx(ctx
);
2322 unmap_mft_record(base_ni
);
2326 #endif /* NTFS_RW */
2329 * ntfs_aops - general address space operations for inodes and attributes
2331 struct address_space_operations ntfs_aops
= {
2332 .readpage
= ntfs_readpage
, /* Fill page with data. */
2333 .sync_page
= block_sync_page
, /* Currently, just unplugs the
2334 disk request queue. */
2336 .writepage
= ntfs_writepage
, /* Write dirty page to disk. */
2337 .prepare_write
= ntfs_prepare_write
, /* Prepare page and buffers
2338 ready to receive data. */
2339 .commit_write
= ntfs_commit_write
, /* Commit received data. */
2340 #endif /* NTFS_RW */
2344 * ntfs_mst_aops - general address space operations for mst protecteed inodes
2347 struct address_space_operations ntfs_mst_aops
= {
2348 .readpage
= ntfs_readpage
, /* Fill page with data. */
2349 .sync_page
= block_sync_page
, /* Currently, just unplugs the
2350 disk request queue. */
2352 .writepage
= ntfs_writepage
, /* Write dirty page to disk. */
2353 .set_page_dirty
= __set_page_dirty_nobuffers
, /* Set the page dirty
2354 without touching the buffers
2355 belonging to the page. */
2356 #endif /* NTFS_RW */
2362 * mark_ntfs_record_dirty - mark an ntfs record dirty
2363 * @page: page containing the ntfs record to mark dirty
2364 * @ofs: byte offset within @page at which the ntfs record begins
2366 * Set the buffers and the page in which the ntfs record is located dirty.
2368 * The latter also marks the vfs inode the ntfs record belongs to dirty
2369 * (I_DIRTY_PAGES only).
2371 * If the page does not have buffers, we create them and set them uptodate.
2372 * The page may not be locked which is why we need to handle the buffers under
2373 * the mapping->private_lock. Once the buffers are marked dirty we no longer
2374 * need the lock since try_to_free_buffers() does not free dirty buffers.
2376 void mark_ntfs_record_dirty(struct page
*page
, const unsigned int ofs
) {
2377 struct address_space
*mapping
= page
->mapping
;
2378 ntfs_inode
*ni
= NTFS_I(mapping
->host
);
2379 struct buffer_head
*bh
, *head
, *buffers_to_free
= NULL
;
2380 unsigned int end
, bh_size
, bh_ofs
;
2382 BUG_ON(!PageUptodate(page
));
2383 end
= ofs
+ ni
->itype
.index
.block_size
;
2384 bh_size
= 1 << VFS_I(ni
)->i_blkbits
;
2385 spin_lock(&mapping
->private_lock
);
2386 if (unlikely(!page_has_buffers(page
))) {
2387 spin_unlock(&mapping
->private_lock
);
2388 bh
= head
= alloc_page_buffers(page
, bh_size
, 1);
2389 spin_lock(&mapping
->private_lock
);
2390 if (likely(!page_has_buffers(page
))) {
2391 struct buffer_head
*tail
;
2394 set_buffer_uptodate(bh
);
2396 bh
= bh
->b_this_page
;
2398 tail
->b_this_page
= head
;
2399 attach_page_buffers(page
, head
);
2401 buffers_to_free
= bh
;
2403 bh
= head
= page_buffers(page
);
2406 bh_ofs
= bh_offset(bh
);
2407 if (bh_ofs
+ bh_size
<= ofs
)
2409 if (unlikely(bh_ofs
>= end
))
2411 set_buffer_dirty(bh
);
2412 } while ((bh
= bh
->b_this_page
) != head
);
2413 spin_unlock(&mapping
->private_lock
);
2414 __set_page_dirty_nobuffers(page
);
2415 if (unlikely(buffers_to_free
)) {
2417 bh
= buffers_to_free
->b_this_page
;
2418 free_buffer_head(buffers_to_free
);
2419 buffers_to_free
= bh
;
2420 } while (buffers_to_free
);
2424 #endif /* NTFS_RW */