2 * aops.c - NTFS kernel address space operations and page cache handling.
3 * Part of the Linux-NTFS project.
5 * Copyright (c) 2001-2006 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>
27 #include <linux/pagemap.h>
28 #include <linux/swap.h>
29 #include <linux/buffer_head.h>
30 #include <linux/writeback.h>
31 #include <linux/bit_spinlock.h>
43 * ntfs_end_buffer_async_read - async io completion for reading attributes
44 * @bh: buffer head on which io is completed
45 * @uptodate: whether @bh is now uptodate or not
47 * Asynchronous I/O completion handler for reading pages belonging to the
48 * attribute address space of an inode. The inodes can either be files or
49 * directories or they can be fake inodes describing some attribute.
51 * If NInoMstProtected(), perform the post read mst fixups when all IO on the
52 * page has been completed and mark the page uptodate or set the error bit on
53 * the page. To determine the size of the records that need fixing up, we
54 * cheat a little bit by setting the index_block_size in ntfs_inode to the ntfs
55 * record size, and index_block_size_bits, to the log(base 2) of the ntfs
58 static void ntfs_end_buffer_async_read(struct buffer_head
*bh
, int uptodate
)
61 struct buffer_head
*first
, *tmp
;
65 int page_uptodate
= 1;
68 vi
= page
->mapping
->host
;
71 if (likely(uptodate
)) {
73 s64 file_ofs
, init_size
;
75 set_buffer_uptodate(bh
);
77 file_ofs
= ((s64
)page
->index
<< PAGE_CACHE_SHIFT
) +
79 read_lock_irqsave(&ni
->size_lock
, flags
);
80 init_size
= ni
->initialized_size
;
81 i_size
= i_size_read(vi
);
82 read_unlock_irqrestore(&ni
->size_lock
, flags
);
83 if (unlikely(init_size
> i_size
)) {
84 /* Race with shrinking truncate. */
87 /* Check for the current buffer head overflowing. */
88 if (unlikely(file_ofs
+ bh
->b_size
> init_size
)) {
93 if (file_ofs
< init_size
)
94 ofs
= init_size
- file_ofs
;
95 kaddr
= kmap_atomic(page
, KM_BIO_SRC_IRQ
);
96 memset(kaddr
+ bh_offset(bh
) + ofs
, 0,
98 kunmap_atomic(kaddr
, KM_BIO_SRC_IRQ
);
99 flush_dcache_page(page
);
102 clear_buffer_uptodate(bh
);
104 ntfs_error(ni
->vol
->sb
, "Buffer I/O error, logical block "
105 "0x%llx.", (unsigned long long)bh
->b_blocknr
);
107 first
= page_buffers(page
);
108 local_irq_save(flags
);
109 bit_spin_lock(BH_Uptodate_Lock
, &first
->b_state
);
110 clear_buffer_async_read(bh
);
114 if (!buffer_uptodate(tmp
))
116 if (buffer_async_read(tmp
)) {
117 if (likely(buffer_locked(tmp
)))
119 /* Async buffers must be locked. */
122 tmp
= tmp
->b_this_page
;
124 bit_spin_unlock(BH_Uptodate_Lock
, &first
->b_state
);
125 local_irq_restore(flags
);
127 * If none of the buffers had errors then we can set the page uptodate,
128 * but we first have to perform the post read mst fixups, if the
129 * attribute is mst protected, i.e. if NInoMstProteced(ni) is true.
130 * Note we ignore fixup errors as those are detected when
131 * map_mft_record() is called which gives us per record granularity
132 * rather than per page granularity.
134 if (!NInoMstProtected(ni
)) {
135 if (likely(page_uptodate
&& !PageError(page
)))
136 SetPageUptodate(page
);
139 unsigned int i
, recs
;
142 rec_size
= ni
->itype
.index
.block_size
;
143 recs
= PAGE_CACHE_SIZE
/ rec_size
;
144 /* Should have been verified before we got here... */
146 kaddr
= kmap_atomic(page
, KM_BIO_SRC_IRQ
);
147 for (i
= 0; i
< recs
; i
++)
148 post_read_mst_fixup((NTFS_RECORD
*)(kaddr
+
149 i
* rec_size
), rec_size
);
150 kunmap_atomic(kaddr
, KM_BIO_SRC_IRQ
);
151 flush_dcache_page(page
);
152 if (likely(page_uptodate
&& !PageError(page
)))
153 SetPageUptodate(page
);
158 bit_spin_unlock(BH_Uptodate_Lock
, &first
->b_state
);
159 local_irq_restore(flags
);
164 * ntfs_read_block - fill a @page of an address space with data
165 * @page: page cache page to fill with data
167 * Fill the page @page of the address space belonging to the @page->host inode.
168 * We read each buffer asynchronously and when all buffers are read in, our io
169 * completion handler ntfs_end_buffer_read_async(), if required, automatically
170 * applies the mst fixups to the page before finally marking it uptodate and
173 * We only enforce allocated_size limit because i_size is checked for in
174 * generic_file_read().
176 * Return 0 on success and -errno on error.
178 * Contains an adapted version of fs/buffer.c::block_read_full_page().
180 static int ntfs_read_block(struct page
*page
)
190 struct buffer_head
*bh
, *head
, *arr
[MAX_BUF_PER_PAGE
];
191 sector_t iblock
, lblock
, zblock
;
193 unsigned int blocksize
, vcn_ofs
;
195 unsigned char blocksize_bits
;
197 vi
= page
->mapping
->host
;
201 /* $MFT/$DATA must have its complete runlist in memory at all times. */
202 BUG_ON(!ni
->runlist
.rl
&& !ni
->mft_no
&& !NInoAttr(ni
));
204 blocksize
= vol
->sb
->s_blocksize
;
205 blocksize_bits
= vol
->sb
->s_blocksize_bits
;
207 if (!page_has_buffers(page
)) {
208 create_empty_buffers(page
, blocksize
, 0);
209 if (unlikely(!page_has_buffers(page
))) {
214 bh
= head
= page_buffers(page
);
218 * We may be racing with truncate. To avoid some of the problems we
219 * now take a snapshot of the various sizes and use those for the whole
220 * of the function. In case of an extending truncate it just means we
221 * may leave some buffers unmapped which are now allocated. This is
222 * not a problem since these buffers will just get mapped when a write
223 * occurs. In case of a shrinking truncate, we will detect this later
224 * on due to the runlist being incomplete and if the page is being
225 * fully truncated, truncate will throw it away as soon as we unlock
226 * it so no need to worry what we do with it.
228 iblock
= (s64
)page
->index
<< (PAGE_CACHE_SHIFT
- blocksize_bits
);
229 read_lock_irqsave(&ni
->size_lock
, flags
);
230 lblock
= (ni
->allocated_size
+ blocksize
- 1) >> blocksize_bits
;
231 init_size
= ni
->initialized_size
;
232 i_size
= i_size_read(vi
);
233 read_unlock_irqrestore(&ni
->size_lock
, flags
);
234 if (unlikely(init_size
> i_size
)) {
235 /* Race with shrinking truncate. */
238 zblock
= (init_size
+ blocksize
- 1) >> blocksize_bits
;
240 /* Loop through all the buffers in the page. */
247 if (unlikely(buffer_uptodate(bh
)))
249 if (unlikely(buffer_mapped(bh
))) {
254 bh
->b_bdev
= vol
->sb
->s_bdev
;
255 /* Is the block within the allowed limits? */
256 if (iblock
< lblock
) {
257 BOOL is_retry
= FALSE
;
259 /* Convert iblock into corresponding vcn and offset. */
260 vcn
= (VCN
)iblock
<< blocksize_bits
>>
261 vol
->cluster_size_bits
;
262 vcn_ofs
= ((VCN
)iblock
<< blocksize_bits
) &
263 vol
->cluster_size_mask
;
266 down_read(&ni
->runlist
.lock
);
269 if (likely(rl
!= NULL
)) {
270 /* Seek to element containing target vcn. */
271 while (rl
->length
&& rl
[1].vcn
<= vcn
)
273 lcn
= ntfs_rl_vcn_to_lcn(rl
, vcn
);
275 lcn
= LCN_RL_NOT_MAPPED
;
276 /* Successful remap. */
278 /* Setup buffer head to correct block. */
279 bh
->b_blocknr
= ((lcn
<< vol
->cluster_size_bits
)
280 + vcn_ofs
) >> blocksize_bits
;
281 set_buffer_mapped(bh
);
282 /* Only read initialized data blocks. */
283 if (iblock
< zblock
) {
287 /* Fully non-initialized data block, zero it. */
290 /* It is a hole, need to zero it. */
293 /* If first try and runlist unmapped, map and retry. */
294 if (!is_retry
&& lcn
== LCN_RL_NOT_MAPPED
) {
297 * Attempt to map runlist, dropping lock for
300 up_read(&ni
->runlist
.lock
);
301 err
= ntfs_map_runlist(ni
, vcn
);
303 goto lock_retry_remap
;
306 up_read(&ni
->runlist
.lock
);
308 * If buffer is outside the runlist, treat it as a
309 * hole. This can happen due to concurrent truncate
312 if (err
== -ENOENT
|| lcn
== LCN_ENOENT
) {
316 /* Hard error, zero out region. */
321 ntfs_error(vol
->sb
, "Failed to read from inode 0x%lx, "
322 "attribute type 0x%x, vcn 0x%llx, "
323 "offset 0x%x because its location on "
324 "disk could not be determined%s "
325 "(error code %i).", ni
->mft_no
,
326 ni
->type
, (unsigned long long)vcn
,
327 vcn_ofs
, is_retry
? " even after "
328 "retrying" : "", err
);
331 * Either iblock was outside lblock limits or
332 * ntfs_rl_vcn_to_lcn() returned error. Just zero that portion
333 * of the page and set the buffer uptodate.
336 bh
->b_blocknr
= -1UL;
337 clear_buffer_mapped(bh
);
339 kaddr
= kmap_atomic(page
, KM_USER0
);
340 memset(kaddr
+ i
* blocksize
, 0, blocksize
);
341 kunmap_atomic(kaddr
, KM_USER0
);
342 flush_dcache_page(page
);
344 set_buffer_uptodate(bh
);
345 } while (i
++, iblock
++, (bh
= bh
->b_this_page
) != head
);
347 /* Release the lock if we took it. */
349 up_read(&ni
->runlist
.lock
);
351 /* Check we have at least one buffer ready for i/o. */
353 struct buffer_head
*tbh
;
355 /* Lock the buffers. */
356 for (i
= 0; i
< nr
; i
++) {
359 tbh
->b_end_io
= ntfs_end_buffer_async_read
;
360 set_buffer_async_read(tbh
);
362 /* Finally, start i/o on the buffers. */
363 for (i
= 0; i
< nr
; i
++) {
365 if (likely(!buffer_uptodate(tbh
)))
366 submit_bh(READ
, tbh
);
368 ntfs_end_buffer_async_read(tbh
, 1);
372 /* No i/o was scheduled on any of the buffers. */
373 if (likely(!PageError(page
)))
374 SetPageUptodate(page
);
375 else /* Signal synchronous i/o error. */
382 * ntfs_readpage - fill a @page of a @file with data from the device
383 * @file: open file to which the page @page belongs or NULL
384 * @page: page cache page to fill with data
386 * For non-resident attributes, ntfs_readpage() fills the @page of the open
387 * file @file by calling the ntfs version of the generic block_read_full_page()
388 * function, ntfs_read_block(), which in turn creates and reads in the buffers
389 * associated with the page asynchronously.
391 * For resident attributes, OTOH, ntfs_readpage() fills @page by copying the
392 * data from the mft record (which at this stage is most likely in memory) and
393 * fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as
394 * even if the mft record is not cached at this point in time, we need to wait
395 * for it to be read in before we can do the copy.
397 * Return 0 on success and -errno on error.
399 static int ntfs_readpage(struct file
*file
, struct page
*page
)
403 ntfs_inode
*ni
, *base_ni
;
405 ntfs_attr_search_ctx
*ctx
;
412 BUG_ON(!PageLocked(page
));
414 * This can potentially happen because we clear PageUptodate() during
415 * ntfs_writepage() of MstProtected() attributes.
417 if (PageUptodate(page
)) {
421 vi
= page
->mapping
->host
;
424 * Only $DATA attributes can be encrypted and only unnamed $DATA
425 * attributes can be compressed. Index root can have the flags set but
426 * this means to create compressed/encrypted files, not that the
427 * attribute is compressed/encrypted. Note we need to check for
428 * AT_INDEX_ALLOCATION since this is the type of both directory and
431 if (ni
->type
!= AT_INDEX_ALLOCATION
) {
432 /* If attribute is encrypted, deny access, just like NT4. */
433 if (NInoEncrypted(ni
)) {
434 BUG_ON(ni
->type
!= AT_DATA
);
438 /* Compressed data streams are handled in compress.c. */
439 if (NInoNonResident(ni
) && NInoCompressed(ni
)) {
440 BUG_ON(ni
->type
!= AT_DATA
);
441 BUG_ON(ni
->name_len
);
442 return ntfs_read_compressed_block(page
);
445 /* NInoNonResident() == NInoIndexAllocPresent() */
446 if (NInoNonResident(ni
)) {
447 /* Normal, non-resident data stream. */
448 return ntfs_read_block(page
);
451 * Attribute is resident, implying it is not compressed or encrypted.
452 * This also means the attribute is smaller than an mft record and
453 * hence smaller than a page, so can simply zero out any pages with
454 * index above 0. Note the attribute can actually be marked compressed
455 * but if it is resident the actual data is not compressed so we are
456 * ok to ignore the compressed flag here.
458 if (unlikely(page
->index
> 0)) {
459 kaddr
= kmap_atomic(page
, KM_USER0
);
460 memset(kaddr
, 0, PAGE_CACHE_SIZE
);
461 flush_dcache_page(page
);
462 kunmap_atomic(kaddr
, KM_USER0
);
468 base_ni
= ni
->ext
.base_ntfs_ino
;
469 /* Map, pin, and lock the mft record. */
470 mrec
= map_mft_record(base_ni
);
476 * If a parallel write made the attribute non-resident, drop the mft
477 * record and retry the readpage.
479 if (unlikely(NInoNonResident(ni
))) {
480 unmap_mft_record(base_ni
);
483 ctx
= ntfs_attr_get_search_ctx(base_ni
, mrec
);
484 if (unlikely(!ctx
)) {
488 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
489 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
491 goto put_unm_err_out
;
492 attr_len
= le32_to_cpu(ctx
->attr
->data
.resident
.value_length
);
493 read_lock_irqsave(&ni
->size_lock
, flags
);
494 if (unlikely(attr_len
> ni
->initialized_size
))
495 attr_len
= ni
->initialized_size
;
496 i_size
= i_size_read(vi
);
497 read_unlock_irqrestore(&ni
->size_lock
, flags
);
498 if (unlikely(attr_len
> i_size
)) {
499 /* Race with shrinking truncate. */
502 kaddr
= kmap_atomic(page
, KM_USER0
);
503 /* Copy the data to the page. */
504 memcpy(kaddr
, (u8
*)ctx
->attr
+
505 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
),
507 /* Zero the remainder of the page. */
508 memset(kaddr
+ attr_len
, 0, PAGE_CACHE_SIZE
- attr_len
);
509 flush_dcache_page(page
);
510 kunmap_atomic(kaddr
, KM_USER0
);
512 ntfs_attr_put_search_ctx(ctx
);
514 unmap_mft_record(base_ni
);
516 SetPageUptodate(page
);
525 * ntfs_write_block - write a @page to the backing store
526 * @page: page cache page to write out
527 * @wbc: writeback control structure
529 * This function is for writing pages belonging to non-resident, non-mst
530 * protected attributes to their backing store.
532 * For a page with buffers, map and write the dirty buffers asynchronously
533 * under page writeback. For a page without buffers, create buffers for the
534 * page, then proceed as above.
536 * If a page doesn't have buffers the page dirty state is definitive. If a page
537 * does have buffers, the page dirty state is just a hint, and the buffer dirty
538 * state is definitive. (A hint which has rules: dirty buffers against a clean
539 * page is illegal. Other combinations are legal and need to be handled. In
540 * particular a dirty page containing clean buffers for example.)
542 * Return 0 on success and -errno on error.
544 * Based on ntfs_read_block() and __block_write_full_page().
546 static int ntfs_write_block(struct page
*page
, struct writeback_control
*wbc
)
550 s64 initialized_size
;
552 sector_t block
, dblock
, iblock
;
557 struct buffer_head
*bh
, *head
;
559 unsigned int blocksize
, vcn_ofs
;
561 BOOL need_end_writeback
;
562 unsigned char blocksize_bits
;
564 vi
= page
->mapping
->host
;
568 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
569 "0x%lx.", ni
->mft_no
, ni
->type
, page
->index
);
571 BUG_ON(!NInoNonResident(ni
));
572 BUG_ON(NInoMstProtected(ni
));
573 blocksize
= vol
->sb
->s_blocksize
;
574 blocksize_bits
= vol
->sb
->s_blocksize_bits
;
575 if (!page_has_buffers(page
)) {
576 BUG_ON(!PageUptodate(page
));
577 create_empty_buffers(page
, blocksize
,
578 (1 << BH_Uptodate
) | (1 << BH_Dirty
));
579 if (unlikely(!page_has_buffers(page
))) {
580 ntfs_warning(vol
->sb
, "Error allocating page "
581 "buffers. Redirtying page so we try "
584 * Put the page back on mapping->dirty_pages, but leave
585 * its buffers' dirty state as-is.
587 redirty_page_for_writepage(wbc
, page
);
592 bh
= head
= page_buffers(page
);
595 /* NOTE: Different naming scheme to ntfs_read_block()! */
597 /* The first block in the page. */
598 block
= (s64
)page
->index
<< (PAGE_CACHE_SHIFT
- blocksize_bits
);
600 read_lock_irqsave(&ni
->size_lock
, flags
);
601 i_size
= i_size_read(vi
);
602 initialized_size
= ni
->initialized_size
;
603 read_unlock_irqrestore(&ni
->size_lock
, flags
);
605 /* The first out of bounds block for the data size. */
606 dblock
= (i_size
+ blocksize
- 1) >> blocksize_bits
;
608 /* The last (fully or partially) initialized block. */
609 iblock
= initialized_size
>> blocksize_bits
;
612 * Be very careful. We have no exclusion from __set_page_dirty_buffers
613 * here, and the (potentially unmapped) buffers may become dirty at
614 * any time. If a buffer becomes dirty here after we've inspected it
615 * then we just miss that fact, and the page stays dirty.
617 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
618 * handle that here by just cleaning them.
622 * Loop through all the buffers in the page, mapping all the dirty
623 * buffers to disk addresses and handling any aliases from the
624 * underlying block device's mapping.
629 BOOL is_retry
= FALSE
;
631 if (unlikely(block
>= dblock
)) {
633 * Mapped buffers outside i_size will occur, because
634 * this page can be outside i_size when there is a
635 * truncate in progress. The contents of such buffers
636 * were zeroed by ntfs_writepage().
638 * FIXME: What about the small race window where
639 * ntfs_writepage() has not done any clearing because
640 * the page was within i_size but before we get here,
641 * vmtruncate() modifies i_size?
643 clear_buffer_dirty(bh
);
644 set_buffer_uptodate(bh
);
648 /* Clean buffers are not written out, so no need to map them. */
649 if (!buffer_dirty(bh
))
652 /* Make sure we have enough initialized size. */
653 if (unlikely((block
>= iblock
) &&
654 (initialized_size
< i_size
))) {
656 * If this page is fully outside initialized size, zero
657 * out all pages between the current initialized size
658 * and the current page. Just use ntfs_readpage() to do
659 * the zeroing transparently.
661 if (block
> iblock
) {
664 // - read_cache_page()
665 // Again for each page do:
666 // - wait_on_page_locked()
667 // - Check (PageUptodate(page) &&
669 // Update initialized size in the attribute and
671 // Again, for each page do:
672 // __set_page_dirty_buffers();
673 // page_cache_release()
674 // We don't need to wait on the writes.
678 * The current page straddles initialized size. Zero
679 * all non-uptodate buffers and set them uptodate (and
680 * dirty?). Note, there aren't any non-uptodate buffers
681 * if the page is uptodate.
682 * FIXME: For an uptodate page, the buffers may need to
683 * be written out because they were not initialized on
686 if (!PageUptodate(page
)) {
688 // Zero any non-uptodate buffers up to i_size.
689 // Set them uptodate and dirty.
692 // Update initialized size in the attribute and in the
693 // inode (up to i_size).
695 // FIXME: This is inefficient. Try to batch the two
696 // size changes to happen in one go.
697 ntfs_error(vol
->sb
, "Writing beyond initialized size "
698 "is not supported yet. Sorry.");
701 // Do NOT set_buffer_new() BUT DO clear buffer range
702 // outside write request range.
703 // set_buffer_uptodate() on complete buffers as well as
704 // set_buffer_dirty().
707 /* No need to map buffers that are already mapped. */
708 if (buffer_mapped(bh
))
711 /* Unmapped, dirty buffer. Need to map it. */
712 bh
->b_bdev
= vol
->sb
->s_bdev
;
714 /* Convert block into corresponding vcn and offset. */
715 vcn
= (VCN
)block
<< blocksize_bits
;
716 vcn_ofs
= vcn
& vol
->cluster_size_mask
;
717 vcn
>>= vol
->cluster_size_bits
;
720 down_read(&ni
->runlist
.lock
);
723 if (likely(rl
!= NULL
)) {
724 /* Seek to element containing target vcn. */
725 while (rl
->length
&& rl
[1].vcn
<= vcn
)
727 lcn
= ntfs_rl_vcn_to_lcn(rl
, vcn
);
729 lcn
= LCN_RL_NOT_MAPPED
;
730 /* Successful remap. */
732 /* Setup buffer head to point to correct block. */
733 bh
->b_blocknr
= ((lcn
<< vol
->cluster_size_bits
) +
734 vcn_ofs
) >> blocksize_bits
;
735 set_buffer_mapped(bh
);
738 /* It is a hole, need to instantiate it. */
739 if (lcn
== LCN_HOLE
) {
741 unsigned long *bpos
, *bend
;
743 /* Check if the buffer is zero. */
744 kaddr
= kmap_atomic(page
, KM_USER0
);
745 bpos
= (unsigned long *)(kaddr
+ bh_offset(bh
));
746 bend
= (unsigned long *)((u8
*)bpos
+ blocksize
);
750 } while (likely(++bpos
< bend
));
751 kunmap_atomic(kaddr
, KM_USER0
);
754 * Buffer is zero and sparse, no need to write
758 clear_buffer_dirty(bh
);
761 // TODO: Instantiate the hole.
762 // clear_buffer_new(bh);
763 // unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
764 ntfs_error(vol
->sb
, "Writing into sparse regions is "
765 "not supported yet. Sorry.");
769 /* If first try and runlist unmapped, map and retry. */
770 if (!is_retry
&& lcn
== LCN_RL_NOT_MAPPED
) {
773 * Attempt to map runlist, dropping lock for
776 up_read(&ni
->runlist
.lock
);
777 err
= ntfs_map_runlist(ni
, vcn
);
779 goto lock_retry_remap
;
782 up_read(&ni
->runlist
.lock
);
784 * If buffer is outside the runlist, truncate has cut it out
785 * of the runlist. Just clean and clear the buffer and set it
786 * uptodate so it can get discarded by the VM.
788 if (err
== -ENOENT
|| lcn
== LCN_ENOENT
) {
792 clear_buffer_dirty(bh
);
793 kaddr
= kmap_atomic(page
, KM_USER0
);
794 memset(kaddr
+ bh_offset(bh
), 0, blocksize
);
795 kunmap_atomic(kaddr
, KM_USER0
);
796 flush_dcache_page(page
);
797 set_buffer_uptodate(bh
);
801 /* Failed to map the buffer, even after retrying. */
805 ntfs_error(vol
->sb
, "Failed to write to inode 0x%lx, "
806 "attribute type 0x%x, vcn 0x%llx, offset 0x%x "
807 "because its location on disk could not be "
808 "determined%s (error code %i).", ni
->mft_no
,
809 ni
->type
, (unsigned long long)vcn
,
810 vcn_ofs
, is_retry
? " even after "
811 "retrying" : "", err
);
813 } while (block
++, (bh
= bh
->b_this_page
) != head
);
815 /* Release the lock if we took it. */
817 up_read(&ni
->runlist
.lock
);
819 /* For the error case, need to reset bh to the beginning. */
822 /* Just an optimization, so ->readpage() is not called later. */
823 if (unlikely(!PageUptodate(page
))) {
826 if (!buffer_uptodate(bh
)) {
831 } while ((bh
= bh
->b_this_page
) != head
);
833 SetPageUptodate(page
);
836 /* Setup all mapped, dirty buffers for async write i/o. */
838 if (buffer_mapped(bh
) && buffer_dirty(bh
)) {
840 if (test_clear_buffer_dirty(bh
)) {
841 BUG_ON(!buffer_uptodate(bh
));
842 mark_buffer_async_write(bh
);
845 } else if (unlikely(err
)) {
847 * For the error case. The buffer may have been set
848 * dirty during attachment to a dirty page.
851 clear_buffer_dirty(bh
);
853 } while ((bh
= bh
->b_this_page
) != head
);
856 // TODO: Remove the -EOPNOTSUPP check later on...
857 if (unlikely(err
== -EOPNOTSUPP
))
859 else if (err
== -ENOMEM
) {
860 ntfs_warning(vol
->sb
, "Error allocating memory. "
861 "Redirtying page so we try again "
864 * Put the page back on mapping->dirty_pages, but
865 * leave its buffer's dirty state as-is.
867 redirty_page_for_writepage(wbc
, page
);
873 BUG_ON(PageWriteback(page
));
874 set_page_writeback(page
); /* Keeps try_to_free_buffers() away. */
876 /* Submit the prepared buffers for i/o. */
877 need_end_writeback
= TRUE
;
879 struct buffer_head
*next
= bh
->b_this_page
;
880 if (buffer_async_write(bh
)) {
881 submit_bh(WRITE
, bh
);
882 need_end_writeback
= FALSE
;
885 } while (bh
!= head
);
888 /* If no i/o was started, need to end_page_writeback(). */
889 if (unlikely(need_end_writeback
))
890 end_page_writeback(page
);
897 * ntfs_write_mst_block - write a @page to the backing store
898 * @page: page cache page to write out
899 * @wbc: writeback control structure
901 * This function is for writing pages belonging to non-resident, mst protected
902 * attributes to their backing store. The only supported attributes are index
903 * allocation and $MFT/$DATA. Both directory inodes and index inodes are
904 * supported for the index allocation case.
906 * The page must remain locked for the duration of the write because we apply
907 * the mst fixups, write, and then undo the fixups, so if we were to unlock the
908 * page before undoing the fixups, any other user of the page will see the
909 * page contents as corrupt.
911 * We clear the page uptodate flag for the duration of the function to ensure
912 * exclusion for the $MFT/$DATA case against someone mapping an mft record we
913 * are about to apply the mst fixups to.
915 * Return 0 on success and -errno on error.
917 * Based on ntfs_write_block(), ntfs_mft_writepage(), and
918 * write_mft_record_nolock().
920 static int ntfs_write_mst_block(struct page
*page
,
921 struct writeback_control
*wbc
)
923 sector_t block
, dblock
, rec_block
;
924 struct inode
*vi
= page
->mapping
->host
;
925 ntfs_inode
*ni
= NTFS_I(vi
);
926 ntfs_volume
*vol
= ni
->vol
;
928 unsigned int rec_size
= ni
->itype
.index
.block_size
;
929 ntfs_inode
*locked_nis
[PAGE_CACHE_SIZE
/ rec_size
];
930 struct buffer_head
*bh
, *head
, *tbh
, *rec_start_bh
;
931 struct buffer_head
*bhs
[MAX_BUF_PER_PAGE
];
933 int i
, nr_locked_nis
, nr_recs
, nr_bhs
, max_bhs
, bhs_per_rec
, err
, err2
;
934 unsigned bh_size
, rec_size_bits
;
935 BOOL sync
, is_mft
, page_is_dirty
, rec_is_dirty
;
936 unsigned char bh_size_bits
;
938 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
939 "0x%lx.", vi
->i_ino
, ni
->type
, page
->index
);
940 BUG_ON(!NInoNonResident(ni
));
941 BUG_ON(!NInoMstProtected(ni
));
942 is_mft
= (S_ISREG(vi
->i_mode
) && !vi
->i_ino
);
944 * NOTE: ntfs_write_mst_block() would be called for $MFTMirr if a page
945 * in its page cache were to be marked dirty. However this should
946 * never happen with the current driver and considering we do not
947 * handle this case here we do want to BUG(), at least for now.
949 BUG_ON(!(is_mft
|| S_ISDIR(vi
->i_mode
) ||
950 (NInoAttr(ni
) && ni
->type
== AT_INDEX_ALLOCATION
)));
951 bh_size
= vol
->sb
->s_blocksize
;
952 bh_size_bits
= vol
->sb
->s_blocksize_bits
;
953 max_bhs
= PAGE_CACHE_SIZE
/ bh_size
;
955 BUG_ON(max_bhs
> MAX_BUF_PER_PAGE
);
957 /* Were we called for sync purposes? */
958 sync
= (wbc
->sync_mode
== WB_SYNC_ALL
);
960 /* Make sure we have mapped buffers. */
961 bh
= head
= page_buffers(page
);
964 rec_size_bits
= ni
->itype
.index
.block_size_bits
;
965 BUG_ON(!(PAGE_CACHE_SIZE
>> rec_size_bits
));
966 bhs_per_rec
= rec_size
>> bh_size_bits
;
967 BUG_ON(!bhs_per_rec
);
969 /* The first block in the page. */
970 rec_block
= block
= (sector_t
)page
->index
<<
971 (PAGE_CACHE_SHIFT
- bh_size_bits
);
973 /* The first out of bounds block for the data size. */
974 dblock
= (i_size_read(vi
) + bh_size
- 1) >> bh_size_bits
;
977 err
= err2
= nr_bhs
= nr_recs
= nr_locked_nis
= 0;
978 page_is_dirty
= rec_is_dirty
= FALSE
;
981 BOOL is_retry
= FALSE
;
983 if (likely(block
< rec_block
)) {
984 if (unlikely(block
>= dblock
)) {
985 clear_buffer_dirty(bh
);
986 set_buffer_uptodate(bh
);
990 * This block is not the first one in the record. We
991 * ignore the buffer's dirty state because we could
992 * have raced with a parallel mark_ntfs_record_dirty().
996 if (unlikely(err2
)) {
998 clear_buffer_dirty(bh
);
1001 } else /* if (block == rec_block) */ {
1002 BUG_ON(block
> rec_block
);
1003 /* This block is the first one in the record. */
1004 rec_block
+= bhs_per_rec
;
1006 if (unlikely(block
>= dblock
)) {
1007 clear_buffer_dirty(bh
);
1010 if (!buffer_dirty(bh
)) {
1011 /* Clean records are not written out. */
1012 rec_is_dirty
= FALSE
;
1015 rec_is_dirty
= TRUE
;
1018 /* Need to map the buffer if it is not mapped already. */
1019 if (unlikely(!buffer_mapped(bh
))) {
1022 unsigned int vcn_ofs
;
1024 bh
->b_bdev
= vol
->sb
->s_bdev
;
1025 /* Obtain the vcn and offset of the current block. */
1026 vcn
= (VCN
)block
<< bh_size_bits
;
1027 vcn_ofs
= vcn
& vol
->cluster_size_mask
;
1028 vcn
>>= vol
->cluster_size_bits
;
1031 down_read(&ni
->runlist
.lock
);
1032 rl
= ni
->runlist
.rl
;
1034 if (likely(rl
!= NULL
)) {
1035 /* Seek to element containing target vcn. */
1036 while (rl
->length
&& rl
[1].vcn
<= vcn
)
1038 lcn
= ntfs_rl_vcn_to_lcn(rl
, vcn
);
1040 lcn
= LCN_RL_NOT_MAPPED
;
1041 /* Successful remap. */
1042 if (likely(lcn
>= 0)) {
1043 /* Setup buffer head to correct block. */
1044 bh
->b_blocknr
= ((lcn
<<
1045 vol
->cluster_size_bits
) +
1046 vcn_ofs
) >> bh_size_bits
;
1047 set_buffer_mapped(bh
);
1050 * Remap failed. Retry to map the runlist once
1051 * unless we are working on $MFT which always
1052 * has the whole of its runlist in memory.
1054 if (!is_mft
&& !is_retry
&&
1055 lcn
== LCN_RL_NOT_MAPPED
) {
1058 * Attempt to map runlist, dropping
1059 * lock for the duration.
1061 up_read(&ni
->runlist
.lock
);
1062 err2
= ntfs_map_runlist(ni
, vcn
);
1064 goto lock_retry_remap
;
1065 if (err2
== -ENOMEM
)
1066 page_is_dirty
= TRUE
;
1071 up_read(&ni
->runlist
.lock
);
1073 /* Hard error. Abort writing this record. */
1074 if (!err
|| err
== -ENOMEM
)
1077 ntfs_error(vol
->sb
, "Cannot write ntfs record "
1078 "0x%llx (inode 0x%lx, "
1079 "attribute type 0x%x) because "
1080 "its location on disk could "
1081 "not be determined (error "
1085 vol
->mft_record_size_bits
,
1086 ni
->mft_no
, ni
->type
,
1089 * If this is not the first buffer, remove the
1090 * buffers in this record from the list of
1091 * buffers to write and clear their dirty bit
1092 * if not error -ENOMEM.
1094 if (rec_start_bh
!= bh
) {
1095 while (bhs
[--nr_bhs
] != rec_start_bh
)
1097 if (err2
!= -ENOMEM
) {
1101 } while ((rec_start_bh
=
1110 BUG_ON(!buffer_uptodate(bh
));
1111 BUG_ON(nr_bhs
>= max_bhs
);
1113 } while (block
++, (bh
= bh
->b_this_page
) != head
);
1115 up_read(&ni
->runlist
.lock
);
1116 /* If there were no dirty buffers, we are done. */
1119 /* Map the page so we can access its contents. */
1121 /* Clear the page uptodate flag whilst the mst fixups are applied. */
1122 BUG_ON(!PageUptodate(page
));
1123 ClearPageUptodate(page
);
1124 for (i
= 0; i
< nr_bhs
; i
++) {
1127 /* Skip buffers which are not at the beginning of records. */
1128 if (i
% bhs_per_rec
)
1131 ofs
= bh_offset(tbh
);
1134 unsigned long mft_no
;
1136 /* Get the mft record number. */
1137 mft_no
= (((s64
)page
->index
<< PAGE_CACHE_SHIFT
) + ofs
)
1139 /* Check whether to write this mft record. */
1141 if (!ntfs_may_write_mft_record(vol
, mft_no
,
1142 (MFT_RECORD
*)(kaddr
+ ofs
), &tni
)) {
1144 * The record should not be written. This
1145 * means we need to redirty the page before
1148 page_is_dirty
= TRUE
;
1150 * Remove the buffers in this mft record from
1151 * the list of buffers to write.
1155 } while (++i
% bhs_per_rec
);
1159 * The record should be written. If a locked ntfs
1160 * inode was returned, add it to the array of locked
1164 locked_nis
[nr_locked_nis
++] = tni
;
1166 /* Apply the mst protection fixups. */
1167 err2
= pre_write_mst_fixup((NTFS_RECORD
*)(kaddr
+ ofs
),
1169 if (unlikely(err2
)) {
1170 if (!err
|| err
== -ENOMEM
)
1172 ntfs_error(vol
->sb
, "Failed to apply mst fixups "
1173 "(inode 0x%lx, attribute type 0x%x, "
1174 "page index 0x%lx, page offset 0x%x)!"
1175 " Unmount and run chkdsk.", vi
->i_ino
,
1176 ni
->type
, page
->index
, ofs
);
1178 * Mark all the buffers in this record clean as we do
1179 * not want to write corrupt data to disk.
1182 clear_buffer_dirty(bhs
[i
]);
1184 } while (++i
% bhs_per_rec
);
1189 /* If no records are to be written out, we are done. */
1192 flush_dcache_page(page
);
1193 /* Lock buffers and start synchronous write i/o on them. */
1194 for (i
= 0; i
< nr_bhs
; i
++) {
1198 if (unlikely(test_set_buffer_locked(tbh
)))
1200 /* The buffer dirty state is now irrelevant, just clean it. */
1201 clear_buffer_dirty(tbh
);
1202 BUG_ON(!buffer_uptodate(tbh
));
1203 BUG_ON(!buffer_mapped(tbh
));
1205 tbh
->b_end_io
= end_buffer_write_sync
;
1206 submit_bh(WRITE
, tbh
);
1208 /* Synchronize the mft mirror now if not @sync. */
1209 if (is_mft
&& !sync
)
1212 /* Wait on i/o completion of buffers. */
1213 for (i
= 0; i
< nr_bhs
; i
++) {
1217 wait_on_buffer(tbh
);
1218 if (unlikely(!buffer_uptodate(tbh
))) {
1219 ntfs_error(vol
->sb
, "I/O error while writing ntfs "
1220 "record buffer (inode 0x%lx, "
1221 "attribute type 0x%x, page index "
1222 "0x%lx, page offset 0x%lx)! Unmount "
1223 "and run chkdsk.", vi
->i_ino
, ni
->type
,
1224 page
->index
, bh_offset(tbh
));
1225 if (!err
|| err
== -ENOMEM
)
1228 * Set the buffer uptodate so the page and buffer
1229 * states do not become out of sync.
1231 set_buffer_uptodate(tbh
);
1234 /* If @sync, now synchronize the mft mirror. */
1235 if (is_mft
&& sync
) {
1237 for (i
= 0; i
< nr_bhs
; i
++) {
1238 unsigned long mft_no
;
1242 * Skip buffers which are not at the beginning of
1245 if (i
% bhs_per_rec
)
1248 /* Skip removed buffers (and hence records). */
1251 ofs
= bh_offset(tbh
);
1252 /* Get the mft record number. */
1253 mft_no
= (((s64
)page
->index
<< PAGE_CACHE_SHIFT
) + ofs
)
1255 if (mft_no
< vol
->mftmirr_size
)
1256 ntfs_sync_mft_mirror(vol
, mft_no
,
1257 (MFT_RECORD
*)(kaddr
+ ofs
),
1263 /* Remove the mst protection fixups again. */
1264 for (i
= 0; i
< nr_bhs
; i
++) {
1265 if (!(i
% bhs_per_rec
)) {
1269 post_write_mst_fixup((NTFS_RECORD
*)(kaddr
+
1273 flush_dcache_page(page
);
1275 /* Unlock any locked inodes. */
1276 while (nr_locked_nis
-- > 0) {
1277 ntfs_inode
*tni
, *base_tni
;
1279 tni
= locked_nis
[nr_locked_nis
];
1280 /* Get the base inode. */
1281 mutex_lock(&tni
->extent_lock
);
1282 if (tni
->nr_extents
>= 0)
1285 base_tni
= tni
->ext
.base_ntfs_ino
;
1288 mutex_unlock(&tni
->extent_lock
);
1289 ntfs_debug("Unlocking %s inode 0x%lx.",
1290 tni
== base_tni
? "base" : "extent",
1292 mutex_unlock(&tni
->mrec_lock
);
1293 atomic_dec(&tni
->count
);
1294 iput(VFS_I(base_tni
));
1296 SetPageUptodate(page
);
1299 if (unlikely(err
&& err
!= -ENOMEM
)) {
1301 * Set page error if there is only one ntfs record in the page.
1302 * Otherwise we would loose per-record granularity.
1304 if (ni
->itype
.index
.block_size
== PAGE_CACHE_SIZE
)
1308 if (page_is_dirty
) {
1309 ntfs_debug("Page still contains one or more dirty ntfs "
1310 "records. Redirtying the page starting at "
1311 "record 0x%lx.", page
->index
<<
1312 (PAGE_CACHE_SHIFT
- rec_size_bits
));
1313 redirty_page_for_writepage(wbc
, page
);
1317 * Keep the VM happy. This must be done otherwise the
1318 * radix-tree tag PAGECACHE_TAG_DIRTY remains set even though
1319 * the page is clean.
1321 BUG_ON(PageWriteback(page
));
1322 set_page_writeback(page
);
1324 end_page_writeback(page
);
1327 ntfs_debug("Done.");
1332 * ntfs_writepage - write a @page to the backing store
1333 * @page: page cache page to write out
1334 * @wbc: writeback control structure
1336 * This is called from the VM when it wants to have a dirty ntfs page cache
1337 * page cleaned. The VM has already locked the page and marked it clean.
1339 * For non-resident attributes, ntfs_writepage() writes the @page by calling
1340 * the ntfs version of the generic block_write_full_page() function,
1341 * ntfs_write_block(), which in turn if necessary creates and writes the
1342 * buffers associated with the page asynchronously.
1344 * For resident attributes, OTOH, ntfs_writepage() writes the @page by copying
1345 * the data to the mft record (which at this stage is most likely in memory).
1346 * The mft record is then marked dirty and written out asynchronously via the
1347 * vfs inode dirty code path for the inode the mft record belongs to or via the
1348 * vm page dirty code path for the page the mft record is in.
1350 * Based on ntfs_readpage() and fs/buffer.c::block_write_full_page().
1352 * Return 0 on success and -errno on error.
1354 static int ntfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1357 struct inode
*vi
= page
->mapping
->host
;
1358 ntfs_inode
*base_ni
= NULL
, *ni
= NTFS_I(vi
);
1360 ntfs_attr_search_ctx
*ctx
= NULL
;
1361 MFT_RECORD
*m
= NULL
;
1366 BUG_ON(!PageLocked(page
));
1367 i_size
= i_size_read(vi
);
1368 /* Is the page fully outside i_size? (truncate in progress) */
1369 if (unlikely(page
->index
>= (i_size
+ PAGE_CACHE_SIZE
- 1) >>
1370 PAGE_CACHE_SHIFT
)) {
1372 * The page may have dirty, unmapped buffers. Make them
1373 * freeable here, so the page does not leak.
1375 block_invalidatepage(page
, 0);
1377 ntfs_debug("Write outside i_size - truncated?");
1381 * Only $DATA attributes can be encrypted and only unnamed $DATA
1382 * attributes can be compressed. Index root can have the flags set but
1383 * this means to create compressed/encrypted files, not that the
1384 * attribute is compressed/encrypted. Note we need to check for
1385 * AT_INDEX_ALLOCATION since this is the type of both directory and
1388 if (ni
->type
!= AT_INDEX_ALLOCATION
) {
1389 /* If file is encrypted, deny access, just like NT4. */
1390 if (NInoEncrypted(ni
)) {
1392 BUG_ON(ni
->type
!= AT_DATA
);
1393 ntfs_debug("Denying write access to encrypted file.");
1396 /* Compressed data streams are handled in compress.c. */
1397 if (NInoNonResident(ni
) && NInoCompressed(ni
)) {
1398 BUG_ON(ni
->type
!= AT_DATA
);
1399 BUG_ON(ni
->name_len
);
1400 // TODO: Implement and replace this with
1401 // return ntfs_write_compressed_block(page);
1403 ntfs_error(vi
->i_sb
, "Writing to compressed files is "
1404 "not supported yet. Sorry.");
1407 // TODO: Implement and remove this check.
1408 if (NInoNonResident(ni
) && NInoSparse(ni
)) {
1410 ntfs_error(vi
->i_sb
, "Writing to sparse files is not "
1411 "supported yet. Sorry.");
1415 /* NInoNonResident() == NInoIndexAllocPresent() */
1416 if (NInoNonResident(ni
)) {
1417 /* We have to zero every time due to mmap-at-end-of-file. */
1418 if (page
->index
>= (i_size
>> PAGE_CACHE_SHIFT
)) {
1419 /* The page straddles i_size. */
1420 unsigned int ofs
= i_size
& ~PAGE_CACHE_MASK
;
1421 kaddr
= kmap_atomic(page
, KM_USER0
);
1422 memset(kaddr
+ ofs
, 0, PAGE_CACHE_SIZE
- ofs
);
1423 kunmap_atomic(kaddr
, KM_USER0
);
1424 flush_dcache_page(page
);
1426 /* Handle mst protected attributes. */
1427 if (NInoMstProtected(ni
))
1428 return ntfs_write_mst_block(page
, wbc
);
1429 /* Normal, non-resident data stream. */
1430 return ntfs_write_block(page
, wbc
);
1433 * Attribute is resident, implying it is not compressed, encrypted, or
1434 * mst protected. This also means the attribute is smaller than an mft
1435 * record and hence smaller than a page, so can simply return error on
1436 * any pages with index above 0. Note the attribute can actually be
1437 * marked compressed but if it is resident the actual data is not
1438 * compressed so we are ok to ignore the compressed flag here.
1440 BUG_ON(page_has_buffers(page
));
1441 BUG_ON(!PageUptodate(page
));
1442 if (unlikely(page
->index
> 0)) {
1443 ntfs_error(vi
->i_sb
, "BUG()! page->index (0x%lx) > 0. "
1444 "Aborting write.", page
->index
);
1445 BUG_ON(PageWriteback(page
));
1446 set_page_writeback(page
);
1448 end_page_writeback(page
);
1454 base_ni
= ni
->ext
.base_ntfs_ino
;
1455 /* Map, pin, and lock the mft record. */
1456 m
= map_mft_record(base_ni
);
1464 * If a parallel write made the attribute non-resident, drop the mft
1465 * record and retry the writepage.
1467 if (unlikely(NInoNonResident(ni
))) {
1468 unmap_mft_record(base_ni
);
1469 goto retry_writepage
;
1471 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
1472 if (unlikely(!ctx
)) {
1476 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
1477 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
1481 * Keep the VM happy. This must be done otherwise the radix-tree tag
1482 * PAGECACHE_TAG_DIRTY remains set even though the page is clean.
1484 BUG_ON(PageWriteback(page
));
1485 set_page_writeback(page
);
1487 attr_len
= le32_to_cpu(ctx
->attr
->data
.resident
.value_length
);
1488 i_size
= i_size_read(vi
);
1489 if (unlikely(attr_len
> i_size
)) {
1490 /* Race with shrinking truncate or a failed truncate. */
1493 * If the truncate failed, fix it up now. If a concurrent
1494 * truncate, we do its job, so it does not have to do anything.
1496 err
= ntfs_resident_attr_value_resize(ctx
->mrec
, ctx
->attr
,
1498 /* Shrinking cannot fail. */
1501 kaddr
= kmap_atomic(page
, KM_USER0
);
1502 /* Copy the data from the page to the mft record. */
1503 memcpy((u8
*)ctx
->attr
+
1504 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
),
1506 /* Zero out of bounds area in the page cache page. */
1507 memset(kaddr
+ attr_len
, 0, PAGE_CACHE_SIZE
- attr_len
);
1508 kunmap_atomic(kaddr
, KM_USER0
);
1509 flush_dcache_page(page
);
1510 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1511 /* We are done with the page. */
1512 end_page_writeback(page
);
1513 /* Finally, mark the mft record dirty, so it gets written back. */
1514 mark_mft_record_dirty(ctx
->ntfs_ino
);
1515 ntfs_attr_put_search_ctx(ctx
);
1516 unmap_mft_record(base_ni
);
1519 if (err
== -ENOMEM
) {
1520 ntfs_warning(vi
->i_sb
, "Error allocating memory. Redirtying "
1521 "page so we try again later.");
1523 * Put the page back on mapping->dirty_pages, but leave its
1524 * buffers' dirty state as-is.
1526 redirty_page_for_writepage(wbc
, page
);
1529 ntfs_error(vi
->i_sb
, "Resident attribute write failed with "
1532 NVolSetErrors(ni
->vol
);
1536 ntfs_attr_put_search_ctx(ctx
);
1538 unmap_mft_record(base_ni
);
1542 #endif /* NTFS_RW */
1545 * ntfs_aops - general address space operations for inodes and attributes
1547 const struct address_space_operations ntfs_aops
= {
1548 .readpage
= ntfs_readpage
, /* Fill page with data. */
1549 .sync_page
= block_sync_page
, /* Currently, just unplugs the
1550 disk request queue. */
1552 .writepage
= ntfs_writepage
, /* Write dirty page to disk. */
1553 #endif /* NTFS_RW */
1554 .migratepage
= buffer_migrate_page
, /* Move a page cache page from
1555 one physical page to an
1560 * ntfs_mst_aops - general address space operations for mst protecteed inodes
1563 const struct address_space_operations ntfs_mst_aops
= {
1564 .readpage
= ntfs_readpage
, /* Fill page with data. */
1565 .sync_page
= block_sync_page
, /* Currently, just unplugs the
1566 disk request queue. */
1568 .writepage
= ntfs_writepage
, /* Write dirty page to disk. */
1569 .set_page_dirty
= __set_page_dirty_nobuffers
, /* Set the page dirty
1570 without touching the buffers
1571 belonging to the page. */
1572 #endif /* NTFS_RW */
1573 .migratepage
= buffer_migrate_page
, /* Move a page cache page from
1574 one physical page to an
1581 * mark_ntfs_record_dirty - mark an ntfs record dirty
1582 * @page: page containing the ntfs record to mark dirty
1583 * @ofs: byte offset within @page at which the ntfs record begins
1585 * Set the buffers and the page in which the ntfs record is located dirty.
1587 * The latter also marks the vfs inode the ntfs record belongs to dirty
1588 * (I_DIRTY_PAGES only).
1590 * If the page does not have buffers, we create them and set them uptodate.
1591 * The page may not be locked which is why we need to handle the buffers under
1592 * the mapping->private_lock. Once the buffers are marked dirty we no longer
1593 * need the lock since try_to_free_buffers() does not free dirty buffers.
1595 void mark_ntfs_record_dirty(struct page
*page
, const unsigned int ofs
) {
1596 struct address_space
*mapping
= page
->mapping
;
1597 ntfs_inode
*ni
= NTFS_I(mapping
->host
);
1598 struct buffer_head
*bh
, *head
, *buffers_to_free
= NULL
;
1599 unsigned int end
, bh_size
, bh_ofs
;
1601 BUG_ON(!PageUptodate(page
));
1602 end
= ofs
+ ni
->itype
.index
.block_size
;
1603 bh_size
= VFS_I(ni
)->i_sb
->s_blocksize
;
1604 spin_lock(&mapping
->private_lock
);
1605 if (unlikely(!page_has_buffers(page
))) {
1606 spin_unlock(&mapping
->private_lock
);
1607 bh
= head
= alloc_page_buffers(page
, bh_size
, 1);
1608 spin_lock(&mapping
->private_lock
);
1609 if (likely(!page_has_buffers(page
))) {
1610 struct buffer_head
*tail
;
1613 set_buffer_uptodate(bh
);
1615 bh
= bh
->b_this_page
;
1617 tail
->b_this_page
= head
;
1618 attach_page_buffers(page
, head
);
1620 buffers_to_free
= bh
;
1622 bh
= head
= page_buffers(page
);
1625 bh_ofs
= bh_offset(bh
);
1626 if (bh_ofs
+ bh_size
<= ofs
)
1628 if (unlikely(bh_ofs
>= end
))
1630 set_buffer_dirty(bh
);
1631 } while ((bh
= bh
->b_this_page
) != head
);
1632 spin_unlock(&mapping
->private_lock
);
1633 __set_page_dirty_nobuffers(page
);
1634 if (unlikely(buffers_to_free
)) {
1636 bh
= buffers_to_free
->b_this_page
;
1637 free_buffer_head(buffers_to_free
);
1638 buffers_to_free
= bh
;
1639 } while (buffers_to_free
);
1643 #endif /* NTFS_RW */