2 * aops.c - NTFS kernel address space operations and page cache handling.
3 * Part of the Linux-NTFS project.
5 * Copyright (c) 2001-2005 Anton Altaparmakov
6 * Copyright (c) 2002 Richard Russon
8 * This program/include file is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program/include file is distributed in the hope that it will be
14 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program (in the main directory of the Linux-NTFS
20 * distribution in the file COPYING); if not, write to the Free Software
21 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/errno.h>
26 #include <linux/pagemap.h>
27 #include <linux/swap.h>
28 #include <linux/buffer_head.h>
29 #include <linux/writeback.h>
30 #include <linux/bit_spinlock.h>
42 * ntfs_end_buffer_async_read - async io completion for reading attributes
43 * @bh: buffer head on which io is completed
44 * @uptodate: whether @bh is now uptodate or not
46 * Asynchronous I/O completion handler for reading pages belonging to the
47 * attribute address space of an inode. The inodes can either be files or
48 * directories or they can be fake inodes describing some attribute.
50 * If NInoMstProtected(), perform the post read mst fixups when all IO on the
51 * page has been completed and mark the page uptodate or set the error bit on
52 * the page. To determine the size of the records that need fixing up, we
53 * cheat a little bit by setting the index_block_size in ntfs_inode to the ntfs
54 * record size, and index_block_size_bits, to the log(base 2) of the ntfs
57 static void ntfs_end_buffer_async_read(struct buffer_head
*bh
, int uptodate
)
60 struct buffer_head
*first
, *tmp
;
64 int page_uptodate
= 1;
67 vi
= page
->mapping
->host
;
70 if (likely(uptodate
)) {
72 s64 file_ofs
, init_size
;
74 set_buffer_uptodate(bh
);
76 file_ofs
= ((s64
)page
->index
<< PAGE_CACHE_SHIFT
) +
78 read_lock_irqsave(&ni
->size_lock
, flags
);
79 init_size
= ni
->initialized_size
;
80 i_size
= i_size_read(vi
);
81 read_unlock_irqrestore(&ni
->size_lock
, flags
);
82 if (unlikely(init_size
> i_size
)) {
83 /* Race with shrinking truncate. */
86 /* Check for the current buffer head overflowing. */
87 if (unlikely(file_ofs
+ bh
->b_size
> init_size
)) {
92 if (file_ofs
< init_size
)
93 ofs
= init_size
- file_ofs
;
94 kaddr
= kmap_atomic(page
, KM_BIO_SRC_IRQ
);
95 memset(kaddr
+ bh_offset(bh
) + ofs
, 0,
97 kunmap_atomic(kaddr
, KM_BIO_SRC_IRQ
);
98 flush_dcache_page(page
);
101 clear_buffer_uptodate(bh
);
103 ntfs_error(ni
->vol
->sb
, "Buffer I/O error, logical block "
104 "0x%llx.", (unsigned long long)bh
->b_blocknr
);
106 first
= page_buffers(page
);
107 local_irq_save(flags
);
108 bit_spin_lock(BH_Uptodate_Lock
, &first
->b_state
);
109 clear_buffer_async_read(bh
);
113 if (!buffer_uptodate(tmp
))
115 if (buffer_async_read(tmp
)) {
116 if (likely(buffer_locked(tmp
)))
118 /* Async buffers must be locked. */
121 tmp
= tmp
->b_this_page
;
123 bit_spin_unlock(BH_Uptodate_Lock
, &first
->b_state
);
124 local_irq_restore(flags
);
126 * If none of the buffers had errors then we can set the page uptodate,
127 * but we first have to perform the post read mst fixups, if the
128 * attribute is mst protected, i.e. if NInoMstProteced(ni) is true.
129 * Note we ignore fixup errors as those are detected when
130 * map_mft_record() is called which gives us per record granularity
131 * rather than per page granularity.
133 if (!NInoMstProtected(ni
)) {
134 if (likely(page_uptodate
&& !PageError(page
)))
135 SetPageUptodate(page
);
138 unsigned int i
, recs
;
141 rec_size
= ni
->itype
.index
.block_size
;
142 recs
= PAGE_CACHE_SIZE
/ rec_size
;
143 /* Should have been verified before we got here... */
145 kaddr
= kmap_atomic(page
, KM_BIO_SRC_IRQ
);
146 for (i
= 0; i
< recs
; i
++)
147 post_read_mst_fixup((NTFS_RECORD
*)(kaddr
+
148 i
* rec_size
), rec_size
);
149 kunmap_atomic(kaddr
, KM_BIO_SRC_IRQ
);
150 flush_dcache_page(page
);
151 if (likely(page_uptodate
&& !PageError(page
)))
152 SetPageUptodate(page
);
157 bit_spin_unlock(BH_Uptodate_Lock
, &first
->b_state
);
158 local_irq_restore(flags
);
163 * ntfs_read_block - fill a @page of an address space with data
164 * @page: page cache page to fill with data
166 * Fill the page @page of the address space belonging to the @page->host inode.
167 * We read each buffer asynchronously and when all buffers are read in, our io
168 * completion handler ntfs_end_buffer_read_async(), if required, automatically
169 * applies the mst fixups to the page before finally marking it uptodate and
172 * We only enforce allocated_size limit because i_size is checked for in
173 * generic_file_read().
175 * Return 0 on success and -errno on error.
177 * Contains an adapted version of fs/buffer.c::block_read_full_page().
179 static int ntfs_read_block(struct page
*page
)
189 struct buffer_head
*bh
, *head
, *arr
[MAX_BUF_PER_PAGE
];
190 sector_t iblock
, lblock
, zblock
;
192 unsigned int blocksize
, vcn_ofs
;
194 unsigned char blocksize_bits
;
196 vi
= page
->mapping
->host
;
200 /* $MFT/$DATA must have its complete runlist in memory at all times. */
201 BUG_ON(!ni
->runlist
.rl
&& !ni
->mft_no
&& !NInoAttr(ni
));
203 blocksize_bits
= VFS_I(ni
)->i_blkbits
;
204 blocksize
= 1 << blocksize_bits
;
206 if (!page_has_buffers(page
)) {
207 create_empty_buffers(page
, blocksize
, 0);
208 if (unlikely(!page_has_buffers(page
))) {
213 bh
= head
= page_buffers(page
);
217 * We may be racing with truncate. To avoid some of the problems we
218 * now take a snapshot of the various sizes and use those for the whole
219 * of the function. In case of an extending truncate it just means we
220 * may leave some buffers unmapped which are now allocated. This is
221 * not a problem since these buffers will just get mapped when a write
222 * occurs. In case of a shrinking truncate, we will detect this later
223 * on due to the runlist being incomplete and if the page is being
224 * fully truncated, truncate will throw it away as soon as we unlock
225 * it so no need to worry what we do with it.
227 iblock
= (s64
)page
->index
<< (PAGE_CACHE_SHIFT
- blocksize_bits
);
228 read_lock_irqsave(&ni
->size_lock
, flags
);
229 lblock
= (ni
->allocated_size
+ blocksize
- 1) >> blocksize_bits
;
230 init_size
= ni
->initialized_size
;
231 i_size
= i_size_read(vi
);
232 read_unlock_irqrestore(&ni
->size_lock
, flags
);
233 if (unlikely(init_size
> i_size
)) {
234 /* Race with shrinking truncate. */
237 zblock
= (init_size
+ blocksize
- 1) >> blocksize_bits
;
239 /* Loop through all the buffers in the page. */
246 if (unlikely(buffer_uptodate(bh
)))
248 if (unlikely(buffer_mapped(bh
))) {
253 bh
->b_bdev
= vol
->sb
->s_bdev
;
254 /* Is the block within the allowed limits? */
255 if (iblock
< lblock
) {
256 BOOL is_retry
= FALSE
;
258 /* Convert iblock into corresponding vcn and offset. */
259 vcn
= (VCN
)iblock
<< blocksize_bits
>>
260 vol
->cluster_size_bits
;
261 vcn_ofs
= ((VCN
)iblock
<< blocksize_bits
) &
262 vol
->cluster_size_mask
;
265 down_read(&ni
->runlist
.lock
);
268 if (likely(rl
!= NULL
)) {
269 /* Seek to element containing target vcn. */
270 while (rl
->length
&& rl
[1].vcn
<= vcn
)
272 lcn
= ntfs_rl_vcn_to_lcn(rl
, vcn
);
274 lcn
= LCN_RL_NOT_MAPPED
;
275 /* Successful remap. */
277 /* Setup buffer head to correct block. */
278 bh
->b_blocknr
= ((lcn
<< vol
->cluster_size_bits
)
279 + vcn_ofs
) >> blocksize_bits
;
280 set_buffer_mapped(bh
);
281 /* Only read initialized data blocks. */
282 if (iblock
< zblock
) {
286 /* Fully non-initialized data block, zero it. */
289 /* It is a hole, need to zero it. */
292 /* If first try and runlist unmapped, map and retry. */
293 if (!is_retry
&& lcn
== LCN_RL_NOT_MAPPED
) {
296 * Attempt to map runlist, dropping lock for
299 up_read(&ni
->runlist
.lock
);
300 err
= ntfs_map_runlist(ni
, vcn
);
302 goto lock_retry_remap
;
305 up_read(&ni
->runlist
.lock
);
307 * If buffer is outside the runlist, treat it as a
308 * hole. This can happen due to concurrent truncate
311 if (err
== -ENOENT
|| lcn
== LCN_ENOENT
) {
315 /* Hard error, zero out region. */
320 ntfs_error(vol
->sb
, "Failed to read from inode 0x%lx, "
321 "attribute type 0x%x, vcn 0x%llx, "
322 "offset 0x%x because its location on "
323 "disk could not be determined%s "
324 "(error code %i).", ni
->mft_no
,
325 ni
->type
, (unsigned long long)vcn
,
326 vcn_ofs
, is_retry
? " even after "
327 "retrying" : "", err
);
330 * Either iblock was outside lblock limits or
331 * ntfs_rl_vcn_to_lcn() returned error. Just zero that portion
332 * of the page and set the buffer uptodate.
335 bh
->b_blocknr
= -1UL;
336 clear_buffer_mapped(bh
);
338 kaddr
= kmap_atomic(page
, KM_USER0
);
339 memset(kaddr
+ i
* blocksize
, 0, blocksize
);
340 kunmap_atomic(kaddr
, KM_USER0
);
341 flush_dcache_page(page
);
343 set_buffer_uptodate(bh
);
344 } while (i
++, iblock
++, (bh
= bh
->b_this_page
) != head
);
346 /* Release the lock if we took it. */
348 up_read(&ni
->runlist
.lock
);
350 /* Check we have at least one buffer ready for i/o. */
352 struct buffer_head
*tbh
;
354 /* Lock the buffers. */
355 for (i
= 0; i
< nr
; i
++) {
358 tbh
->b_end_io
= ntfs_end_buffer_async_read
;
359 set_buffer_async_read(tbh
);
361 /* Finally, start i/o on the buffers. */
362 for (i
= 0; i
< nr
; i
++) {
364 if (likely(!buffer_uptodate(tbh
)))
365 submit_bh(READ
, tbh
);
367 ntfs_end_buffer_async_read(tbh
, 1);
371 /* No i/o was scheduled on any of the buffers. */
372 if (likely(!PageError(page
)))
373 SetPageUptodate(page
);
374 else /* Signal synchronous i/o error. */
381 * ntfs_readpage - fill a @page of a @file with data from the device
382 * @file: open file to which the page @page belongs or NULL
383 * @page: page cache page to fill with data
385 * For non-resident attributes, ntfs_readpage() fills the @page of the open
386 * file @file by calling the ntfs version of the generic block_read_full_page()
387 * function, ntfs_read_block(), which in turn creates and reads in the buffers
388 * associated with the page asynchronously.
390 * For resident attributes, OTOH, ntfs_readpage() fills @page by copying the
391 * data from the mft record (which at this stage is most likely in memory) and
392 * fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as
393 * even if the mft record is not cached at this point in time, we need to wait
394 * for it to be read in before we can do the copy.
396 * Return 0 on success and -errno on error.
398 static int ntfs_readpage(struct file
*file
, struct page
*page
)
402 ntfs_inode
*ni
, *base_ni
;
404 ntfs_attr_search_ctx
*ctx
;
411 BUG_ON(!PageLocked(page
));
413 * This can potentially happen because we clear PageUptodate() during
414 * ntfs_writepage() of MstProtected() attributes.
416 if (PageUptodate(page
)) {
420 vi
= page
->mapping
->host
;
423 * Only $DATA attributes can be encrypted and only unnamed $DATA
424 * attributes can be compressed. Index root can have the flags set but
425 * this means to create compressed/encrypted files, not that the
426 * attribute is compressed/encrypted. Note we need to check for
427 * AT_INDEX_ALLOCATION since this is the type of both directory and
430 if (ni
->type
!= AT_INDEX_ALLOCATION
) {
431 /* If attribute is encrypted, deny access, just like NT4. */
432 if (NInoEncrypted(ni
)) {
433 BUG_ON(ni
->type
!= AT_DATA
);
437 /* Compressed data streams are handled in compress.c. */
438 if (NInoNonResident(ni
) && NInoCompressed(ni
)) {
439 BUG_ON(ni
->type
!= AT_DATA
);
440 BUG_ON(ni
->name_len
);
441 return ntfs_read_compressed_block(page
);
444 /* NInoNonResident() == NInoIndexAllocPresent() */
445 if (NInoNonResident(ni
)) {
446 /* Normal, non-resident data stream. */
447 return ntfs_read_block(page
);
450 * Attribute is resident, implying it is not compressed or encrypted.
451 * This also means the attribute is smaller than an mft record and
452 * hence smaller than a page, so can simply zero out any pages with
453 * index above 0. Note the attribute can actually be marked compressed
454 * but if it is resident the actual data is not compressed so we are
455 * ok to ignore the compressed flag here.
457 if (unlikely(page
->index
> 0)) {
458 kaddr
= kmap_atomic(page
, KM_USER0
);
459 memset(kaddr
, 0, PAGE_CACHE_SIZE
);
460 flush_dcache_page(page
);
461 kunmap_atomic(kaddr
, KM_USER0
);
467 base_ni
= ni
->ext
.base_ntfs_ino
;
468 /* Map, pin, and lock the mft record. */
469 mrec
= map_mft_record(base_ni
);
475 * If a parallel write made the attribute non-resident, drop the mft
476 * record and retry the readpage.
478 if (unlikely(NInoNonResident(ni
))) {
479 unmap_mft_record(base_ni
);
482 ctx
= ntfs_attr_get_search_ctx(base_ni
, mrec
);
483 if (unlikely(!ctx
)) {
487 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
488 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
490 goto put_unm_err_out
;
491 attr_len
= le32_to_cpu(ctx
->attr
->data
.resident
.value_length
);
492 read_lock_irqsave(&ni
->size_lock
, flags
);
493 if (unlikely(attr_len
> ni
->initialized_size
))
494 attr_len
= ni
->initialized_size
;
495 i_size
= i_size_read(vi
);
496 read_unlock_irqrestore(&ni
->size_lock
, flags
);
497 if (unlikely(attr_len
> i_size
)) {
498 /* Race with shrinking truncate. */
501 kaddr
= kmap_atomic(page
, KM_USER0
);
502 /* Copy the data to the page. */
503 memcpy(kaddr
, (u8
*)ctx
->attr
+
504 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
),
506 /* Zero the remainder of the page. */
507 memset(kaddr
+ attr_len
, 0, PAGE_CACHE_SIZE
- attr_len
);
508 flush_dcache_page(page
);
509 kunmap_atomic(kaddr
, KM_USER0
);
511 ntfs_attr_put_search_ctx(ctx
);
513 unmap_mft_record(base_ni
);
515 SetPageUptodate(page
);
524 * ntfs_write_block - write a @page to the backing store
525 * @page: page cache page to write out
526 * @wbc: writeback control structure
528 * This function is for writing pages belonging to non-resident, non-mst
529 * protected attributes to their backing store.
531 * For a page with buffers, map and write the dirty buffers asynchronously
532 * under page writeback. For a page without buffers, create buffers for the
533 * page, then proceed as above.
535 * If a page doesn't have buffers the page dirty state is definitive. If a page
536 * does have buffers, the page dirty state is just a hint, and the buffer dirty
537 * state is definitive. (A hint which has rules: dirty buffers against a clean
538 * page is illegal. Other combinations are legal and need to be handled. In
539 * particular a dirty page containing clean buffers for example.)
541 * Return 0 on success and -errno on error.
543 * Based on ntfs_read_block() and __block_write_full_page().
545 static int ntfs_write_block(struct page
*page
, struct writeback_control
*wbc
)
549 s64 initialized_size
;
551 sector_t block
, dblock
, iblock
;
556 struct buffer_head
*bh
, *head
;
558 unsigned int blocksize
, vcn_ofs
;
560 BOOL need_end_writeback
;
561 unsigned char blocksize_bits
;
563 vi
= page
->mapping
->host
;
567 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
568 "0x%lx.", ni
->mft_no
, ni
->type
, page
->index
);
570 BUG_ON(!NInoNonResident(ni
));
571 BUG_ON(NInoMstProtected(ni
));
573 blocksize_bits
= vi
->i_blkbits
;
574 blocksize
= 1 << blocksize_bits
;
576 if (!page_has_buffers(page
)) {
577 BUG_ON(!PageUptodate(page
));
578 create_empty_buffers(page
, blocksize
,
579 (1 << BH_Uptodate
) | (1 << BH_Dirty
));
580 if (unlikely(!page_has_buffers(page
))) {
581 ntfs_warning(vol
->sb
, "Error allocating page "
582 "buffers. Redirtying page so we try "
585 * Put the page back on mapping->dirty_pages, but leave
586 * its buffers' dirty state as-is.
588 redirty_page_for_writepage(wbc
, page
);
593 bh
= head
= page_buffers(page
);
596 /* NOTE: Different naming scheme to ntfs_read_block()! */
598 /* The first block in the page. */
599 block
= (s64
)page
->index
<< (PAGE_CACHE_SHIFT
- blocksize_bits
);
601 read_lock_irqsave(&ni
->size_lock
, flags
);
602 i_size
= i_size_read(vi
);
603 initialized_size
= ni
->initialized_size
;
604 read_unlock_irqrestore(&ni
->size_lock
, flags
);
606 /* The first out of bounds block for the data size. */
607 dblock
= (i_size
+ blocksize
- 1) >> blocksize_bits
;
609 /* The last (fully or partially) initialized block. */
610 iblock
= initialized_size
>> blocksize_bits
;
613 * Be very careful. We have no exclusion from __set_page_dirty_buffers
614 * here, and the (potentially unmapped) buffers may become dirty at
615 * any time. If a buffer becomes dirty here after we've inspected it
616 * then we just miss that fact, and the page stays dirty.
618 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
619 * handle that here by just cleaning them.
623 * Loop through all the buffers in the page, mapping all the dirty
624 * buffers to disk addresses and handling any aliases from the
625 * underlying block device's mapping.
630 BOOL is_retry
= FALSE
;
632 if (unlikely(block
>= dblock
)) {
634 * Mapped buffers outside i_size will occur, because
635 * this page can be outside i_size when there is a
636 * truncate in progress. The contents of such buffers
637 * were zeroed by ntfs_writepage().
639 * FIXME: What about the small race window where
640 * ntfs_writepage() has not done any clearing because
641 * the page was within i_size but before we get here,
642 * vmtruncate() modifies i_size?
644 clear_buffer_dirty(bh
);
645 set_buffer_uptodate(bh
);
649 /* Clean buffers are not written out, so no need to map them. */
650 if (!buffer_dirty(bh
))
653 /* Make sure we have enough initialized size. */
654 if (unlikely((block
>= iblock
) &&
655 (initialized_size
< i_size
))) {
657 * If this page is fully outside initialized size, zero
658 * out all pages between the current initialized size
659 * and the current page. Just use ntfs_readpage() to do
660 * the zeroing transparently.
662 if (block
> iblock
) {
665 // - read_cache_page()
666 // Again for each page do:
667 // - wait_on_page_locked()
668 // - Check (PageUptodate(page) &&
670 // Update initialized size in the attribute and
672 // Again, for each page do:
673 // __set_page_dirty_buffers();
674 // page_cache_release()
675 // We don't need to wait on the writes.
679 * The current page straddles initialized size. Zero
680 * all non-uptodate buffers and set them uptodate (and
681 * dirty?). Note, there aren't any non-uptodate buffers
682 * if the page is uptodate.
683 * FIXME: For an uptodate page, the buffers may need to
684 * be written out because they were not initialized on
687 if (!PageUptodate(page
)) {
689 // Zero any non-uptodate buffers up to i_size.
690 // Set them uptodate and dirty.
693 // Update initialized size in the attribute and in the
694 // inode (up to i_size).
696 // FIXME: This is inefficient. Try to batch the two
697 // size changes to happen in one go.
698 ntfs_error(vol
->sb
, "Writing beyond initialized size "
699 "is not supported yet. Sorry.");
702 // Do NOT set_buffer_new() BUT DO clear buffer range
703 // outside write request range.
704 // set_buffer_uptodate() on complete buffers as well as
705 // set_buffer_dirty().
708 /* No need to map buffers that are already mapped. */
709 if (buffer_mapped(bh
))
712 /* Unmapped, dirty buffer. Need to map it. */
713 bh
->b_bdev
= vol
->sb
->s_bdev
;
715 /* Convert block into corresponding vcn and offset. */
716 vcn
= (VCN
)block
<< blocksize_bits
;
717 vcn_ofs
= vcn
& vol
->cluster_size_mask
;
718 vcn
>>= vol
->cluster_size_bits
;
721 down_read(&ni
->runlist
.lock
);
724 if (likely(rl
!= NULL
)) {
725 /* Seek to element containing target vcn. */
726 while (rl
->length
&& rl
[1].vcn
<= vcn
)
728 lcn
= ntfs_rl_vcn_to_lcn(rl
, vcn
);
730 lcn
= LCN_RL_NOT_MAPPED
;
731 /* Successful remap. */
733 /* Setup buffer head to point to correct block. */
734 bh
->b_blocknr
= ((lcn
<< vol
->cluster_size_bits
) +
735 vcn_ofs
) >> blocksize_bits
;
736 set_buffer_mapped(bh
);
739 /* It is a hole, need to instantiate it. */
740 if (lcn
== LCN_HOLE
) {
742 unsigned long *bpos
, *bend
;
744 /* Check if the buffer is zero. */
745 kaddr
= kmap_atomic(page
, KM_USER0
);
746 bpos
= (unsigned long *)(kaddr
+ bh_offset(bh
));
747 bend
= (unsigned long *)((u8
*)bpos
+ blocksize
);
751 } while (likely(++bpos
< bend
));
752 kunmap_atomic(kaddr
, KM_USER0
);
755 * Buffer is zero and sparse, no need to write
759 clear_buffer_dirty(bh
);
762 // TODO: Instantiate the hole.
763 // clear_buffer_new(bh);
764 // unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
765 ntfs_error(vol
->sb
, "Writing into sparse regions is "
766 "not supported yet. Sorry.");
770 /* If first try and runlist unmapped, map and retry. */
771 if (!is_retry
&& lcn
== LCN_RL_NOT_MAPPED
) {
774 * Attempt to map runlist, dropping lock for
777 up_read(&ni
->runlist
.lock
);
778 err
= ntfs_map_runlist(ni
, vcn
);
780 goto lock_retry_remap
;
783 up_read(&ni
->runlist
.lock
);
785 * If buffer is outside the runlist, truncate has cut it out
786 * of the runlist. Just clean and clear the buffer and set it
787 * uptodate so it can get discarded by the VM.
789 if (err
== -ENOENT
|| lcn
== LCN_ENOENT
) {
793 clear_buffer_dirty(bh
);
794 kaddr
= kmap_atomic(page
, KM_USER0
);
795 memset(kaddr
+ bh_offset(bh
), 0, blocksize
);
796 kunmap_atomic(kaddr
, KM_USER0
);
797 flush_dcache_page(page
);
798 set_buffer_uptodate(bh
);
802 /* Failed to map the buffer, even after retrying. */
806 ntfs_error(vol
->sb
, "Failed to write to inode 0x%lx, "
807 "attribute type 0x%x, vcn 0x%llx, offset 0x%x "
808 "because its location on disk could not be "
809 "determined%s (error code %i).", ni
->mft_no
,
810 ni
->type
, (unsigned long long)vcn
,
811 vcn_ofs
, is_retry
? " even after "
812 "retrying" : "", err
);
814 } while (block
++, (bh
= bh
->b_this_page
) != head
);
816 /* Release the lock if we took it. */
818 up_read(&ni
->runlist
.lock
);
820 /* For the error case, need to reset bh to the beginning. */
823 /* Just an optimization, so ->readpage() is not called later. */
824 if (unlikely(!PageUptodate(page
))) {
827 if (!buffer_uptodate(bh
)) {
832 } while ((bh
= bh
->b_this_page
) != head
);
834 SetPageUptodate(page
);
837 /* Setup all mapped, dirty buffers for async write i/o. */
839 if (buffer_mapped(bh
) && buffer_dirty(bh
)) {
841 if (test_clear_buffer_dirty(bh
)) {
842 BUG_ON(!buffer_uptodate(bh
));
843 mark_buffer_async_write(bh
);
846 } else if (unlikely(err
)) {
848 * For the error case. The buffer may have been set
849 * dirty during attachment to a dirty page.
852 clear_buffer_dirty(bh
);
854 } while ((bh
= bh
->b_this_page
) != head
);
857 // TODO: Remove the -EOPNOTSUPP check later on...
858 if (unlikely(err
== -EOPNOTSUPP
))
860 else if (err
== -ENOMEM
) {
861 ntfs_warning(vol
->sb
, "Error allocating memory. "
862 "Redirtying page so we try again "
865 * Put the page back on mapping->dirty_pages, but
866 * leave its buffer's dirty state as-is.
868 redirty_page_for_writepage(wbc
, page
);
874 BUG_ON(PageWriteback(page
));
875 set_page_writeback(page
); /* Keeps try_to_free_buffers() away. */
877 /* Submit the prepared buffers for i/o. */
878 need_end_writeback
= TRUE
;
880 struct buffer_head
*next
= bh
->b_this_page
;
881 if (buffer_async_write(bh
)) {
882 submit_bh(WRITE
, bh
);
883 need_end_writeback
= FALSE
;
886 } while (bh
!= head
);
889 /* If no i/o was started, need to end_page_writeback(). */
890 if (unlikely(need_end_writeback
))
891 end_page_writeback(page
);
898 * ntfs_write_mst_block - write a @page to the backing store
899 * @page: page cache page to write out
900 * @wbc: writeback control structure
902 * This function is for writing pages belonging to non-resident, mst protected
903 * attributes to their backing store. The only supported attributes are index
904 * allocation and $MFT/$DATA. Both directory inodes and index inodes are
905 * supported for the index allocation case.
907 * The page must remain locked for the duration of the write because we apply
908 * the mst fixups, write, and then undo the fixups, so if we were to unlock the
909 * page before undoing the fixups, any other user of the page will see the
910 * page contents as corrupt.
912 * We clear the page uptodate flag for the duration of the function to ensure
913 * exclusion for the $MFT/$DATA case against someone mapping an mft record we
914 * are about to apply the mst fixups to.
916 * Return 0 on success and -errno on error.
918 * Based on ntfs_write_block(), ntfs_mft_writepage(), and
919 * write_mft_record_nolock().
921 static int ntfs_write_mst_block(struct page
*page
,
922 struct writeback_control
*wbc
)
924 sector_t block
, dblock
, rec_block
;
925 struct inode
*vi
= page
->mapping
->host
;
926 ntfs_inode
*ni
= NTFS_I(vi
);
927 ntfs_volume
*vol
= ni
->vol
;
929 unsigned int rec_size
= ni
->itype
.index
.block_size
;
930 ntfs_inode
*locked_nis
[PAGE_CACHE_SIZE
/ rec_size
];
931 struct buffer_head
*bh
, *head
, *tbh
, *rec_start_bh
;
932 struct buffer_head
*bhs
[MAX_BUF_PER_PAGE
];
934 int i
, nr_locked_nis
, nr_recs
, nr_bhs
, max_bhs
, bhs_per_rec
, err
, err2
;
935 unsigned bh_size
, rec_size_bits
;
936 BOOL sync
, is_mft
, page_is_dirty
, rec_is_dirty
;
937 unsigned char bh_size_bits
;
939 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
940 "0x%lx.", vi
->i_ino
, ni
->type
, page
->index
);
941 BUG_ON(!NInoNonResident(ni
));
942 BUG_ON(!NInoMstProtected(ni
));
943 is_mft
= (S_ISREG(vi
->i_mode
) && !vi
->i_ino
);
945 * NOTE: ntfs_write_mst_block() would be called for $MFTMirr if a page
946 * in its page cache were to be marked dirty. However this should
947 * never happen with the current driver and considering we do not
948 * handle this case here we do want to BUG(), at least for now.
950 BUG_ON(!(is_mft
|| S_ISDIR(vi
->i_mode
) ||
951 (NInoAttr(ni
) && ni
->type
== AT_INDEX_ALLOCATION
)));
952 bh_size_bits
= vi
->i_blkbits
;
953 bh_size
= 1 << bh_size_bits
;
954 max_bhs
= PAGE_CACHE_SIZE
/ bh_size
;
956 BUG_ON(max_bhs
> MAX_BUF_PER_PAGE
);
958 /* Were we called for sync purposes? */
959 sync
= (wbc
->sync_mode
== WB_SYNC_ALL
);
961 /* Make sure we have mapped buffers. */
962 bh
= head
= page_buffers(page
);
965 rec_size_bits
= ni
->itype
.index
.block_size_bits
;
966 BUG_ON(!(PAGE_CACHE_SIZE
>> rec_size_bits
));
967 bhs_per_rec
= rec_size
>> bh_size_bits
;
968 BUG_ON(!bhs_per_rec
);
970 /* The first block in the page. */
971 rec_block
= block
= (sector_t
)page
->index
<<
972 (PAGE_CACHE_SHIFT
- bh_size_bits
);
974 /* The first out of bounds block for the data size. */
975 dblock
= (i_size_read(vi
) + bh_size
- 1) >> bh_size_bits
;
978 err
= err2
= nr_bhs
= nr_recs
= nr_locked_nis
= 0;
979 page_is_dirty
= rec_is_dirty
= FALSE
;
982 BOOL is_retry
= FALSE
;
984 if (likely(block
< rec_block
)) {
985 if (unlikely(block
>= dblock
)) {
986 clear_buffer_dirty(bh
);
987 set_buffer_uptodate(bh
);
991 * This block is not the first one in the record. We
992 * ignore the buffer's dirty state because we could
993 * have raced with a parallel mark_ntfs_record_dirty().
997 if (unlikely(err2
)) {
999 clear_buffer_dirty(bh
);
1002 } else /* if (block == rec_block) */ {
1003 BUG_ON(block
> rec_block
);
1004 /* This block is the first one in the record. */
1005 rec_block
+= bhs_per_rec
;
1007 if (unlikely(block
>= dblock
)) {
1008 clear_buffer_dirty(bh
);
1011 if (!buffer_dirty(bh
)) {
1012 /* Clean records are not written out. */
1013 rec_is_dirty
= FALSE
;
1016 rec_is_dirty
= TRUE
;
1019 /* Need to map the buffer if it is not mapped already. */
1020 if (unlikely(!buffer_mapped(bh
))) {
1023 unsigned int vcn_ofs
;
1025 bh
->b_bdev
= vol
->sb
->s_bdev
;
1026 /* Obtain the vcn and offset of the current block. */
1027 vcn
= (VCN
)block
<< bh_size_bits
;
1028 vcn_ofs
= vcn
& vol
->cluster_size_mask
;
1029 vcn
>>= vol
->cluster_size_bits
;
1032 down_read(&ni
->runlist
.lock
);
1033 rl
= ni
->runlist
.rl
;
1035 if (likely(rl
!= NULL
)) {
1036 /* Seek to element containing target vcn. */
1037 while (rl
->length
&& rl
[1].vcn
<= vcn
)
1039 lcn
= ntfs_rl_vcn_to_lcn(rl
, vcn
);
1041 lcn
= LCN_RL_NOT_MAPPED
;
1042 /* Successful remap. */
1043 if (likely(lcn
>= 0)) {
1044 /* Setup buffer head to correct block. */
1045 bh
->b_blocknr
= ((lcn
<<
1046 vol
->cluster_size_bits
) +
1047 vcn_ofs
) >> bh_size_bits
;
1048 set_buffer_mapped(bh
);
1051 * Remap failed. Retry to map the runlist once
1052 * unless we are working on $MFT which always
1053 * has the whole of its runlist in memory.
1055 if (!is_mft
&& !is_retry
&&
1056 lcn
== LCN_RL_NOT_MAPPED
) {
1059 * Attempt to map runlist, dropping
1060 * lock for the duration.
1062 up_read(&ni
->runlist
.lock
);
1063 err2
= ntfs_map_runlist(ni
, vcn
);
1065 goto lock_retry_remap
;
1066 if (err2
== -ENOMEM
)
1067 page_is_dirty
= TRUE
;
1072 up_read(&ni
->runlist
.lock
);
1074 /* Hard error. Abort writing this record. */
1075 if (!err
|| err
== -ENOMEM
)
1078 ntfs_error(vol
->sb
, "Cannot write ntfs record "
1079 "0x%llx (inode 0x%lx, "
1080 "attribute type 0x%x) because "
1081 "its location on disk could "
1082 "not be determined (error "
1086 vol
->mft_record_size_bits
,
1087 ni
->mft_no
, ni
->type
,
1090 * If this is not the first buffer, remove the
1091 * buffers in this record from the list of
1092 * buffers to write and clear their dirty bit
1093 * if not error -ENOMEM.
1095 if (rec_start_bh
!= bh
) {
1096 while (bhs
[--nr_bhs
] != rec_start_bh
)
1098 if (err2
!= -ENOMEM
) {
1102 } while ((rec_start_bh
=
1111 BUG_ON(!buffer_uptodate(bh
));
1112 BUG_ON(nr_bhs
>= max_bhs
);
1114 } while (block
++, (bh
= bh
->b_this_page
) != head
);
1116 up_read(&ni
->runlist
.lock
);
1117 /* If there were no dirty buffers, we are done. */
1120 /* Map the page so we can access its contents. */
1122 /* Clear the page uptodate flag whilst the mst fixups are applied. */
1123 BUG_ON(!PageUptodate(page
));
1124 ClearPageUptodate(page
);
1125 for (i
= 0; i
< nr_bhs
; i
++) {
1128 /* Skip buffers which are not at the beginning of records. */
1129 if (i
% bhs_per_rec
)
1132 ofs
= bh_offset(tbh
);
1135 unsigned long mft_no
;
1137 /* Get the mft record number. */
1138 mft_no
= (((s64
)page
->index
<< PAGE_CACHE_SHIFT
) + ofs
)
1140 /* Check whether to write this mft record. */
1142 if (!ntfs_may_write_mft_record(vol
, mft_no
,
1143 (MFT_RECORD
*)(kaddr
+ ofs
), &tni
)) {
1145 * The record should not be written. This
1146 * means we need to redirty the page before
1149 page_is_dirty
= TRUE
;
1151 * Remove the buffers in this mft record from
1152 * the list of buffers to write.
1156 } while (++i
% bhs_per_rec
);
1160 * The record should be written. If a locked ntfs
1161 * inode was returned, add it to the array of locked
1165 locked_nis
[nr_locked_nis
++] = tni
;
1167 /* Apply the mst protection fixups. */
1168 err2
= pre_write_mst_fixup((NTFS_RECORD
*)(kaddr
+ ofs
),
1170 if (unlikely(err2
)) {
1171 if (!err
|| err
== -ENOMEM
)
1173 ntfs_error(vol
->sb
, "Failed to apply mst fixups "
1174 "(inode 0x%lx, attribute type 0x%x, "
1175 "page index 0x%lx, page offset 0x%x)!"
1176 " Unmount and run chkdsk.", vi
->i_ino
,
1177 ni
->type
, page
->index
, ofs
);
1179 * Mark all the buffers in this record clean as we do
1180 * not want to write corrupt data to disk.
1183 clear_buffer_dirty(bhs
[i
]);
1185 } while (++i
% bhs_per_rec
);
1190 /* If no records are to be written out, we are done. */
1193 flush_dcache_page(page
);
1194 /* Lock buffers and start synchronous write i/o on them. */
1195 for (i
= 0; i
< nr_bhs
; i
++) {
1199 if (unlikely(test_set_buffer_locked(tbh
)))
1201 /* The buffer dirty state is now irrelevant, just clean it. */
1202 clear_buffer_dirty(tbh
);
1203 BUG_ON(!buffer_uptodate(tbh
));
1204 BUG_ON(!buffer_mapped(tbh
));
1206 tbh
->b_end_io
= end_buffer_write_sync
;
1207 submit_bh(WRITE
, tbh
);
1209 /* Synchronize the mft mirror now if not @sync. */
1210 if (is_mft
&& !sync
)
1213 /* Wait on i/o completion of buffers. */
1214 for (i
= 0; i
< nr_bhs
; i
++) {
1218 wait_on_buffer(tbh
);
1219 if (unlikely(!buffer_uptodate(tbh
))) {
1220 ntfs_error(vol
->sb
, "I/O error while writing ntfs "
1221 "record buffer (inode 0x%lx, "
1222 "attribute type 0x%x, page index "
1223 "0x%lx, page offset 0x%lx)! Unmount "
1224 "and run chkdsk.", vi
->i_ino
, ni
->type
,
1225 page
->index
, bh_offset(tbh
));
1226 if (!err
|| err
== -ENOMEM
)
1229 * Set the buffer uptodate so the page and buffer
1230 * states do not become out of sync.
1232 set_buffer_uptodate(tbh
);
1235 /* If @sync, now synchronize the mft mirror. */
1236 if (is_mft
&& sync
) {
1238 for (i
= 0; i
< nr_bhs
; i
++) {
1239 unsigned long mft_no
;
1243 * Skip buffers which are not at the beginning of
1246 if (i
% bhs_per_rec
)
1249 /* Skip removed buffers (and hence records). */
1252 ofs
= bh_offset(tbh
);
1253 /* Get the mft record number. */
1254 mft_no
= (((s64
)page
->index
<< PAGE_CACHE_SHIFT
) + ofs
)
1256 if (mft_no
< vol
->mftmirr_size
)
1257 ntfs_sync_mft_mirror(vol
, mft_no
,
1258 (MFT_RECORD
*)(kaddr
+ ofs
),
1264 /* Remove the mst protection fixups again. */
1265 for (i
= 0; i
< nr_bhs
; i
++) {
1266 if (!(i
% bhs_per_rec
)) {
1270 post_write_mst_fixup((NTFS_RECORD
*)(kaddr
+
1274 flush_dcache_page(page
);
1276 /* Unlock any locked inodes. */
1277 while (nr_locked_nis
-- > 0) {
1278 ntfs_inode
*tni
, *base_tni
;
1280 tni
= locked_nis
[nr_locked_nis
];
1281 /* Get the base inode. */
1282 down(&tni
->extent_lock
);
1283 if (tni
->nr_extents
>= 0)
1286 base_tni
= tni
->ext
.base_ntfs_ino
;
1289 up(&tni
->extent_lock
);
1290 ntfs_debug("Unlocking %s inode 0x%lx.",
1291 tni
== base_tni
? "base" : "extent",
1293 up(&tni
->mrec_lock
);
1294 atomic_dec(&tni
->count
);
1295 iput(VFS_I(base_tni
));
1297 SetPageUptodate(page
);
1300 if (unlikely(err
&& err
!= -ENOMEM
)) {
1302 * Set page error if there is only one ntfs record in the page.
1303 * Otherwise we would loose per-record granularity.
1305 if (ni
->itype
.index
.block_size
== PAGE_CACHE_SIZE
)
1309 if (page_is_dirty
) {
1310 ntfs_debug("Page still contains one or more dirty ntfs "
1311 "records. Redirtying the page starting at "
1312 "record 0x%lx.", page
->index
<<
1313 (PAGE_CACHE_SHIFT
- rec_size_bits
));
1314 redirty_page_for_writepage(wbc
, page
);
1318 * Keep the VM happy. This must be done otherwise the
1319 * radix-tree tag PAGECACHE_TAG_DIRTY remains set even though
1320 * the page is clean.
1322 BUG_ON(PageWriteback(page
));
1323 set_page_writeback(page
);
1325 end_page_writeback(page
);
1328 ntfs_debug("Done.");
1333 * ntfs_writepage - write a @page to the backing store
1334 * @page: page cache page to write out
1335 * @wbc: writeback control structure
1337 * This is called from the VM when it wants to have a dirty ntfs page cache
1338 * page cleaned. The VM has already locked the page and marked it clean.
1340 * For non-resident attributes, ntfs_writepage() writes the @page by calling
1341 * the ntfs version of the generic block_write_full_page() function,
1342 * ntfs_write_block(), which in turn if necessary creates and writes the
1343 * buffers associated with the page asynchronously.
1345 * For resident attributes, OTOH, ntfs_writepage() writes the @page by copying
1346 * the data to the mft record (which at this stage is most likely in memory).
1347 * The mft record is then marked dirty and written out asynchronously via the
1348 * vfs inode dirty code path for the inode the mft record belongs to or via the
1349 * vm page dirty code path for the page the mft record is in.
1351 * Based on ntfs_readpage() and fs/buffer.c::block_write_full_page().
1353 * Return 0 on success and -errno on error.
1355 static int ntfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1358 struct inode
*vi
= page
->mapping
->host
;
1359 ntfs_inode
*base_ni
= NULL
, *ni
= NTFS_I(vi
);
1361 ntfs_attr_search_ctx
*ctx
= NULL
;
1362 MFT_RECORD
*m
= NULL
;
1367 BUG_ON(!PageLocked(page
));
1368 i_size
= i_size_read(vi
);
1369 /* Is the page fully outside i_size? (truncate in progress) */
1370 if (unlikely(page
->index
>= (i_size
+ PAGE_CACHE_SIZE
- 1) >>
1371 PAGE_CACHE_SHIFT
)) {
1373 * The page may have dirty, unmapped buffers. Make them
1374 * freeable here, so the page does not leak.
1376 block_invalidatepage(page
, 0);
1378 ntfs_debug("Write outside i_size - truncated?");
1382 * Only $DATA attributes can be encrypted and only unnamed $DATA
1383 * attributes can be compressed. Index root can have the flags set but
1384 * this means to create compressed/encrypted files, not that the
1385 * attribute is compressed/encrypted. Note we need to check for
1386 * AT_INDEX_ALLOCATION since this is the type of both directory and
1389 if (ni
->type
!= AT_INDEX_ALLOCATION
) {
1390 /* If file is encrypted, deny access, just like NT4. */
1391 if (NInoEncrypted(ni
)) {
1393 BUG_ON(ni
->type
!= AT_DATA
);
1394 ntfs_debug("Denying write access to encrypted file.");
1397 /* Compressed data streams are handled in compress.c. */
1398 if (NInoNonResident(ni
) && NInoCompressed(ni
)) {
1399 BUG_ON(ni
->type
!= AT_DATA
);
1400 BUG_ON(ni
->name_len
);
1401 // TODO: Implement and replace this with
1402 // return ntfs_write_compressed_block(page);
1404 ntfs_error(vi
->i_sb
, "Writing to compressed files is "
1405 "not supported yet. Sorry.");
1408 // TODO: Implement and remove this check.
1409 if (NInoNonResident(ni
) && NInoSparse(ni
)) {
1411 ntfs_error(vi
->i_sb
, "Writing to sparse files is not "
1412 "supported yet. Sorry.");
1416 /* NInoNonResident() == NInoIndexAllocPresent() */
1417 if (NInoNonResident(ni
)) {
1418 /* We have to zero every time due to mmap-at-end-of-file. */
1419 if (page
->index
>= (i_size
>> PAGE_CACHE_SHIFT
)) {
1420 /* The page straddles i_size. */
1421 unsigned int ofs
= i_size
& ~PAGE_CACHE_MASK
;
1422 kaddr
= kmap_atomic(page
, KM_USER0
);
1423 memset(kaddr
+ ofs
, 0, PAGE_CACHE_SIZE
- ofs
);
1424 kunmap_atomic(kaddr
, KM_USER0
);
1425 flush_dcache_page(page
);
1427 /* Handle mst protected attributes. */
1428 if (NInoMstProtected(ni
))
1429 return ntfs_write_mst_block(page
, wbc
);
1430 /* Normal, non-resident data stream. */
1431 return ntfs_write_block(page
, wbc
);
1434 * Attribute is resident, implying it is not compressed, encrypted, or
1435 * mst protected. This also means the attribute is smaller than an mft
1436 * record and hence smaller than a page, so can simply return error on
1437 * any pages with index above 0. Note the attribute can actually be
1438 * marked compressed but if it is resident the actual data is not
1439 * compressed so we are ok to ignore the compressed flag here.
1441 BUG_ON(page_has_buffers(page
));
1442 BUG_ON(!PageUptodate(page
));
1443 if (unlikely(page
->index
> 0)) {
1444 ntfs_error(vi
->i_sb
, "BUG()! page->index (0x%lx) > 0. "
1445 "Aborting write.", page
->index
);
1446 BUG_ON(PageWriteback(page
));
1447 set_page_writeback(page
);
1449 end_page_writeback(page
);
1455 base_ni
= ni
->ext
.base_ntfs_ino
;
1456 /* Map, pin, and lock the mft record. */
1457 m
= map_mft_record(base_ni
);
1465 * If a parallel write made the attribute non-resident, drop the mft
1466 * record and retry the writepage.
1468 if (unlikely(NInoNonResident(ni
))) {
1469 unmap_mft_record(base_ni
);
1470 goto retry_writepage
;
1472 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
1473 if (unlikely(!ctx
)) {
1477 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
1478 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
1482 * Keep the VM happy. This must be done otherwise the radix-tree tag
1483 * PAGECACHE_TAG_DIRTY remains set even though the page is clean.
1485 BUG_ON(PageWriteback(page
));
1486 set_page_writeback(page
);
1488 attr_len
= le32_to_cpu(ctx
->attr
->data
.resident
.value_length
);
1489 i_size
= i_size_read(vi
);
1490 if (unlikely(attr_len
> i_size
)) {
1491 /* Race with shrinking truncate or a failed truncate. */
1494 * If the truncate failed, fix it up now. If a concurrent
1495 * truncate, we do its job, so it does not have to do anything.
1497 err
= ntfs_resident_attr_value_resize(ctx
->mrec
, ctx
->attr
,
1499 /* Shrinking cannot fail. */
1502 kaddr
= kmap_atomic(page
, KM_USER0
);
1503 /* Copy the data from the page to the mft record. */
1504 memcpy((u8
*)ctx
->attr
+
1505 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
),
1507 /* Zero out of bounds area in the page cache page. */
1508 memset(kaddr
+ attr_len
, 0, PAGE_CACHE_SIZE
- attr_len
);
1509 kunmap_atomic(kaddr
, KM_USER0
);
1510 flush_dcache_page(page
);
1511 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1512 /* We are done with the page. */
1513 end_page_writeback(page
);
1514 /* Finally, mark the mft record dirty, so it gets written back. */
1515 mark_mft_record_dirty(ctx
->ntfs_ino
);
1516 ntfs_attr_put_search_ctx(ctx
);
1517 unmap_mft_record(base_ni
);
1520 if (err
== -ENOMEM
) {
1521 ntfs_warning(vi
->i_sb
, "Error allocating memory. Redirtying "
1522 "page so we try again later.");
1524 * Put the page back on mapping->dirty_pages, but leave its
1525 * buffers' dirty state as-is.
1527 redirty_page_for_writepage(wbc
, page
);
1530 ntfs_error(vi
->i_sb
, "Resident attribute write failed with "
1533 NVolSetErrors(ni
->vol
);
1538 ntfs_attr_put_search_ctx(ctx
);
1540 unmap_mft_record(base_ni
);
1544 #endif /* NTFS_RW */
1547 * ntfs_aops - general address space operations for inodes and attributes
1549 struct address_space_operations ntfs_aops
= {
1550 .readpage
= ntfs_readpage
, /* Fill page with data. */
1551 .sync_page
= block_sync_page
, /* Currently, just unplugs the
1552 disk request queue. */
1554 .writepage
= ntfs_writepage
, /* Write dirty page to disk. */
1555 #endif /* NTFS_RW */
1559 * ntfs_mst_aops - general address space operations for mst protecteed inodes
1562 struct address_space_operations ntfs_mst_aops
= {
1563 .readpage
= ntfs_readpage
, /* Fill page with data. */
1564 .sync_page
= block_sync_page
, /* Currently, just unplugs the
1565 disk request queue. */
1567 .writepage
= ntfs_writepage
, /* Write dirty page to disk. */
1568 .set_page_dirty
= __set_page_dirty_nobuffers
, /* Set the page dirty
1569 without touching the buffers
1570 belonging to the page. */
1571 #endif /* NTFS_RW */
1577 * mark_ntfs_record_dirty - mark an ntfs record dirty
1578 * @page: page containing the ntfs record to mark dirty
1579 * @ofs: byte offset within @page at which the ntfs record begins
1581 * Set the buffers and the page in which the ntfs record is located dirty.
1583 * The latter also marks the vfs inode the ntfs record belongs to dirty
1584 * (I_DIRTY_PAGES only).
1586 * If the page does not have buffers, we create them and set them uptodate.
1587 * The page may not be locked which is why we need to handle the buffers under
1588 * the mapping->private_lock. Once the buffers are marked dirty we no longer
1589 * need the lock since try_to_free_buffers() does not free dirty buffers.
1591 void mark_ntfs_record_dirty(struct page
*page
, const unsigned int ofs
) {
1592 struct address_space
*mapping
= page
->mapping
;
1593 ntfs_inode
*ni
= NTFS_I(mapping
->host
);
1594 struct buffer_head
*bh
, *head
, *buffers_to_free
= NULL
;
1595 unsigned int end
, bh_size
, bh_ofs
;
1597 BUG_ON(!PageUptodate(page
));
1598 end
= ofs
+ ni
->itype
.index
.block_size
;
1599 bh_size
= 1 << VFS_I(ni
)->i_blkbits
;
1600 spin_lock(&mapping
->private_lock
);
1601 if (unlikely(!page_has_buffers(page
))) {
1602 spin_unlock(&mapping
->private_lock
);
1603 bh
= head
= alloc_page_buffers(page
, bh_size
, 1);
1604 spin_lock(&mapping
->private_lock
);
1605 if (likely(!page_has_buffers(page
))) {
1606 struct buffer_head
*tail
;
1609 set_buffer_uptodate(bh
);
1611 bh
= bh
->b_this_page
;
1613 tail
->b_this_page
= head
;
1614 attach_page_buffers(page
, head
);
1616 buffers_to_free
= bh
;
1618 bh
= head
= page_buffers(page
);
1621 bh_ofs
= bh_offset(bh
);
1622 if (bh_ofs
+ bh_size
<= ofs
)
1624 if (unlikely(bh_ofs
>= end
))
1626 set_buffer_dirty(bh
);
1627 } while ((bh
= bh
->b_this_page
) != head
);
1628 spin_unlock(&mapping
->private_lock
);
1629 __set_page_dirty_nobuffers(page
);
1630 if (unlikely(buffers_to_free
)) {
1632 bh
= buffers_to_free
->b_this_page
;
1633 free_buffer_head(buffers_to_free
);
1634 buffers_to_free
= bh
;
1635 } while (buffers_to_free
);
1639 #endif /* NTFS_RW */