2 * aops.c - NTFS kernel address space operations and page cache handling.
4 * Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc.
5 * Copyright (c) 2002 Richard Russon
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/errno.h>
25 #include <linux/gfp.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>
32 #include <linux/bio.h>
44 * ntfs_end_buffer_async_read - async io completion for reading attributes
45 * @bh: buffer head on which io is completed
46 * @uptodate: whether @bh is now uptodate or not
48 * Asynchronous I/O completion handler for reading pages belonging to the
49 * attribute address space of an inode. The inodes can either be files or
50 * directories or they can be fake inodes describing some attribute.
52 * If NInoMstProtected(), perform the post read mst fixups when all IO on the
53 * page has been completed and mark the page uptodate or set the error bit on
54 * the page. To determine the size of the records that need fixing up, we
55 * cheat a little bit by setting the index_block_size in ntfs_inode to the ntfs
56 * record size, and index_block_size_bits, to the log(base 2) of the ntfs
59 static void ntfs_end_buffer_async_read(struct buffer_head
*bh
, int uptodate
)
62 struct buffer_head
*first
, *tmp
;
66 int page_uptodate
= 1;
69 vi
= page
->mapping
->host
;
72 if (likely(uptodate
)) {
74 s64 file_ofs
, init_size
;
76 set_buffer_uptodate(bh
);
78 file_ofs
= ((s64
)page
->index
<< PAGE_SHIFT
) +
80 read_lock_irqsave(&ni
->size_lock
, flags
);
81 init_size
= ni
->initialized_size
;
82 i_size
= i_size_read(vi
);
83 read_unlock_irqrestore(&ni
->size_lock
, flags
);
84 if (unlikely(init_size
> i_size
)) {
85 /* Race with shrinking truncate. */
88 /* Check for the current buffer head overflowing. */
89 if (unlikely(file_ofs
+ bh
->b_size
> init_size
)) {
94 if (file_ofs
< init_size
)
95 ofs
= init_size
- file_ofs
;
96 kaddr
= kmap_atomic(page
);
97 memset(kaddr
+ bh_offset(bh
) + ofs
, 0,
99 flush_dcache_page(page
);
100 kunmap_atomic(kaddr
);
103 clear_buffer_uptodate(bh
);
105 ntfs_error(ni
->vol
->sb
, "Buffer I/O error, logical block "
106 "0x%llx.", (unsigned long long)bh
->b_blocknr
);
108 first
= page_buffers(page
);
109 local_irq_save(flags
);
110 bit_spin_lock(BH_Uptodate_Lock
, &first
->b_state
);
111 clear_buffer_async_read(bh
);
115 if (!buffer_uptodate(tmp
))
117 if (buffer_async_read(tmp
)) {
118 if (likely(buffer_locked(tmp
)))
120 /* Async buffers must be locked. */
123 tmp
= tmp
->b_this_page
;
125 bit_spin_unlock(BH_Uptodate_Lock
, &first
->b_state
);
126 local_irq_restore(flags
);
128 * If none of the buffers had errors then we can set the page uptodate,
129 * but we first have to perform the post read mst fixups, if the
130 * attribute is mst protected, i.e. if NInoMstProteced(ni) is true.
131 * Note we ignore fixup errors as those are detected when
132 * map_mft_record() is called which gives us per record granularity
133 * rather than per page granularity.
135 if (!NInoMstProtected(ni
)) {
136 if (likely(page_uptodate
&& !PageError(page
)))
137 SetPageUptodate(page
);
140 unsigned int i
, recs
;
143 rec_size
= ni
->itype
.index
.block_size
;
144 recs
= PAGE_SIZE
/ rec_size
;
145 /* Should have been verified before we got here... */
147 kaddr
= kmap_atomic(page
);
148 for (i
= 0; i
< recs
; i
++)
149 post_read_mst_fixup((NTFS_RECORD
*)(kaddr
+
150 i
* rec_size
), rec_size
);
151 kunmap_atomic(kaddr
);
152 flush_dcache_page(page
);
153 if (likely(page_uptodate
&& !PageError(page
)))
154 SetPageUptodate(page
);
159 bit_spin_unlock(BH_Uptodate_Lock
, &first
->b_state
);
160 local_irq_restore(flags
);
165 * ntfs_read_block - fill a @page of an address space with data
166 * @page: page cache page to fill with data
168 * Fill the page @page of the address space belonging to the @page->host inode.
169 * We read each buffer asynchronously and when all buffers are read in, our io
170 * completion handler ntfs_end_buffer_read_async(), if required, automatically
171 * applies the mst fixups to the page before finally marking it uptodate and
174 * We only enforce allocated_size limit because i_size is checked for in
175 * generic_file_read().
177 * Return 0 on success and -errno on error.
179 * Contains an adapted version of fs/buffer.c::block_read_full_page().
181 static int ntfs_read_block(struct page
*page
)
191 struct buffer_head
*bh
, *head
, *arr
[MAX_BUF_PER_PAGE
];
192 sector_t iblock
, lblock
, zblock
;
194 unsigned int blocksize
, vcn_ofs
;
196 unsigned char blocksize_bits
;
198 vi
= page
->mapping
->host
;
202 /* $MFT/$DATA must have its complete runlist in memory at all times. */
203 BUG_ON(!ni
->runlist
.rl
&& !ni
->mft_no
&& !NInoAttr(ni
));
205 blocksize
= vol
->sb
->s_blocksize
;
206 blocksize_bits
= vol
->sb
->s_blocksize_bits
;
208 if (!page_has_buffers(page
)) {
209 create_empty_buffers(page
, blocksize
, 0);
210 if (unlikely(!page_has_buffers(page
))) {
215 bh
= head
= page_buffers(page
);
219 * We may be racing with truncate. To avoid some of the problems we
220 * now take a snapshot of the various sizes and use those for the whole
221 * of the function. In case of an extending truncate it just means we
222 * may leave some buffers unmapped which are now allocated. This is
223 * not a problem since these buffers will just get mapped when a write
224 * occurs. In case of a shrinking truncate, we will detect this later
225 * on due to the runlist being incomplete and if the page is being
226 * fully truncated, truncate will throw it away as soon as we unlock
227 * it so no need to worry what we do with it.
229 iblock
= (s64
)page
->index
<< (PAGE_SHIFT
- blocksize_bits
);
230 read_lock_irqsave(&ni
->size_lock
, flags
);
231 lblock
= (ni
->allocated_size
+ blocksize
- 1) >> blocksize_bits
;
232 init_size
= ni
->initialized_size
;
233 i_size
= i_size_read(vi
);
234 read_unlock_irqrestore(&ni
->size_lock
, flags
);
235 if (unlikely(init_size
> i_size
)) {
236 /* Race with shrinking truncate. */
239 zblock
= (init_size
+ blocksize
- 1) >> blocksize_bits
;
241 /* Loop through all the buffers in the page. */
247 if (unlikely(buffer_uptodate(bh
)))
249 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 zero_user(page
, i
* blocksize
, blocksize
);
340 set_buffer_uptodate(bh
);
341 } while (i
++, iblock
++, (bh
= bh
->b_this_page
) != head
);
343 /* Release the lock if we took it. */
345 up_read(&ni
->runlist
.lock
);
347 /* Check we have at least one buffer ready for i/o. */
349 struct buffer_head
*tbh
;
351 /* Lock the buffers. */
352 for (i
= 0; i
< nr
; i
++) {
355 tbh
->b_end_io
= ntfs_end_buffer_async_read
;
356 set_buffer_async_read(tbh
);
358 /* Finally, start i/o on the buffers. */
359 for (i
= 0; i
< nr
; i
++) {
361 if (likely(!buffer_uptodate(tbh
)))
362 submit_bh(REQ_OP_READ
, 0, tbh
);
364 ntfs_end_buffer_async_read(tbh
, 1);
368 /* No i/o was scheduled on any of the buffers. */
369 if (likely(!PageError(page
)))
370 SetPageUptodate(page
);
371 else /* Signal synchronous i/o error. */
378 * ntfs_readpage - fill a @page of a @file with data from the device
379 * @file: open file to which the page @page belongs or NULL
380 * @page: page cache page to fill with data
382 * For non-resident attributes, ntfs_readpage() fills the @page of the open
383 * file @file by calling the ntfs version of the generic block_read_full_page()
384 * function, ntfs_read_block(), which in turn creates and reads in the buffers
385 * associated with the page asynchronously.
387 * For resident attributes, OTOH, ntfs_readpage() fills @page by copying the
388 * data from the mft record (which at this stage is most likely in memory) and
389 * fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as
390 * even if the mft record is not cached at this point in time, we need to wait
391 * for it to be read in before we can do the copy.
393 * Return 0 on success and -errno on error.
395 static int ntfs_readpage(struct file
*file
, struct page
*page
)
399 ntfs_inode
*ni
, *base_ni
;
401 ntfs_attr_search_ctx
*ctx
;
408 BUG_ON(!PageLocked(page
));
409 vi
= page
->mapping
->host
;
410 i_size
= i_size_read(vi
);
411 /* Is the page fully outside i_size? (truncate in progress) */
412 if (unlikely(page
->index
>= (i_size
+ PAGE_SIZE
- 1) >>
414 zero_user(page
, 0, PAGE_SIZE
);
415 ntfs_debug("Read outside i_size - truncated?");
419 * This can potentially happen because we clear PageUptodate() during
420 * ntfs_writepage() of MstProtected() attributes.
422 if (PageUptodate(page
)) {
428 * Only $DATA attributes can be encrypted and only unnamed $DATA
429 * attributes can be compressed. Index root can have the flags set but
430 * this means to create compressed/encrypted files, not that the
431 * attribute is compressed/encrypted. Note we need to check for
432 * AT_INDEX_ALLOCATION since this is the type of both directory and
435 if (ni
->type
!= AT_INDEX_ALLOCATION
) {
436 /* If attribute is encrypted, deny access, just like NT4. */
437 if (NInoEncrypted(ni
)) {
438 BUG_ON(ni
->type
!= AT_DATA
);
442 /* Compressed data streams are handled in compress.c. */
443 if (NInoNonResident(ni
) && NInoCompressed(ni
)) {
444 BUG_ON(ni
->type
!= AT_DATA
);
445 BUG_ON(ni
->name_len
);
446 return ntfs_read_compressed_block(page
);
449 /* NInoNonResident() == NInoIndexAllocPresent() */
450 if (NInoNonResident(ni
)) {
451 /* Normal, non-resident data stream. */
452 return ntfs_read_block(page
);
455 * Attribute is resident, implying it is not compressed or encrypted.
456 * This also means the attribute is smaller than an mft record and
457 * hence smaller than a page, so can simply zero out any pages with
458 * index above 0. Note the attribute can actually be marked compressed
459 * but if it is resident the actual data is not compressed so we are
460 * ok to ignore the compressed flag here.
462 if (unlikely(page
->index
> 0)) {
463 zero_user(page
, 0, PAGE_SIZE
);
469 base_ni
= ni
->ext
.base_ntfs_ino
;
470 /* Map, pin, and lock the mft record. */
471 mrec
= map_mft_record(base_ni
);
477 * If a parallel write made the attribute non-resident, drop the mft
478 * record and retry the readpage.
480 if (unlikely(NInoNonResident(ni
))) {
481 unmap_mft_record(base_ni
);
484 ctx
= ntfs_attr_get_search_ctx(base_ni
, mrec
);
485 if (unlikely(!ctx
)) {
489 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
490 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
492 goto put_unm_err_out
;
493 attr_len
= le32_to_cpu(ctx
->attr
->data
.resident
.value_length
);
494 read_lock_irqsave(&ni
->size_lock
, flags
);
495 if (unlikely(attr_len
> ni
->initialized_size
))
496 attr_len
= ni
->initialized_size
;
497 i_size
= i_size_read(vi
);
498 read_unlock_irqrestore(&ni
->size_lock
, flags
);
499 if (unlikely(attr_len
> i_size
)) {
500 /* Race with shrinking truncate. */
503 addr
= kmap_atomic(page
);
504 /* Copy the data to the page. */
505 memcpy(addr
, (u8
*)ctx
->attr
+
506 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
),
508 /* Zero the remainder of the page. */
509 memset(addr
+ attr_len
, 0, PAGE_SIZE
- attr_len
);
510 flush_dcache_page(page
);
513 ntfs_attr_put_search_ctx(ctx
);
515 unmap_mft_record(base_ni
);
517 SetPageUptodate(page
);
526 * ntfs_write_block - write a @page to the backing store
527 * @page: page cache page to write out
528 * @wbc: writeback control structure
530 * This function is for writing pages belonging to non-resident, non-mst
531 * protected attributes to their backing store.
533 * For a page with buffers, map and write the dirty buffers asynchronously
534 * under page writeback. For a page without buffers, create buffers for the
535 * page, then proceed as above.
537 * If a page doesn't have buffers the page dirty state is definitive. If a page
538 * does have buffers, the page dirty state is just a hint, and the buffer dirty
539 * state is definitive. (A hint which has rules: dirty buffers against a clean
540 * page is illegal. Other combinations are legal and need to be handled. In
541 * particular a dirty page containing clean buffers for example.)
543 * Return 0 on success and -errno on error.
545 * Based on ntfs_read_block() and __block_write_full_page().
547 static int ntfs_write_block(struct page
*page
, struct writeback_control
*wbc
)
551 s64 initialized_size
;
553 sector_t block
, dblock
, iblock
;
558 struct buffer_head
*bh
, *head
;
560 unsigned int blocksize
, vcn_ofs
;
562 bool need_end_writeback
;
563 unsigned char blocksize_bits
;
565 vi
= page
->mapping
->host
;
569 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
570 "0x%lx.", ni
->mft_no
, ni
->type
, page
->index
);
572 BUG_ON(!NInoNonResident(ni
));
573 BUG_ON(NInoMstProtected(ni
));
574 blocksize
= vol
->sb
->s_blocksize
;
575 blocksize_bits
= vol
->sb
->s_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_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();
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
);
746 bpos
= (unsigned long *)(kaddr
+ bh_offset(bh
));
747 bend
= (unsigned long *)((u8
*)bpos
+ blocksize
);
751 } while (likely(++bpos
< bend
));
752 kunmap_atomic(kaddr
);
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 // clean_bdev_bh_alias(bh);
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
) {
791 clear_buffer_dirty(bh
);
792 zero_user(page
, bh_offset(bh
), blocksize
);
793 set_buffer_uptodate(bh
);
797 /* Failed to map the buffer, even after retrying. */
801 ntfs_error(vol
->sb
, "Failed to write to inode 0x%lx, "
802 "attribute type 0x%x, vcn 0x%llx, offset 0x%x "
803 "because its location on disk could not be "
804 "determined%s (error code %i).", ni
->mft_no
,
805 ni
->type
, (unsigned long long)vcn
,
806 vcn_ofs
, is_retry
? " even after "
807 "retrying" : "", err
);
809 } while (block
++, (bh
= bh
->b_this_page
) != head
);
811 /* Release the lock if we took it. */
813 up_read(&ni
->runlist
.lock
);
815 /* For the error case, need to reset bh to the beginning. */
818 /* Just an optimization, so ->readpage() is not called later. */
819 if (unlikely(!PageUptodate(page
))) {
822 if (!buffer_uptodate(bh
)) {
827 } while ((bh
= bh
->b_this_page
) != head
);
829 SetPageUptodate(page
);
832 /* Setup all mapped, dirty buffers for async write i/o. */
834 if (buffer_mapped(bh
) && buffer_dirty(bh
)) {
836 if (test_clear_buffer_dirty(bh
)) {
837 BUG_ON(!buffer_uptodate(bh
));
838 mark_buffer_async_write(bh
);
841 } else if (unlikely(err
)) {
843 * For the error case. The buffer may have been set
844 * dirty during attachment to a dirty page.
847 clear_buffer_dirty(bh
);
849 } while ((bh
= bh
->b_this_page
) != head
);
852 // TODO: Remove the -EOPNOTSUPP check later on...
853 if (unlikely(err
== -EOPNOTSUPP
))
855 else if (err
== -ENOMEM
) {
856 ntfs_warning(vol
->sb
, "Error allocating memory. "
857 "Redirtying page so we try again "
860 * Put the page back on mapping->dirty_pages, but
861 * leave its buffer's dirty state as-is.
863 redirty_page_for_writepage(wbc
, page
);
869 BUG_ON(PageWriteback(page
));
870 set_page_writeback(page
); /* Keeps try_to_free_buffers() away. */
872 /* Submit the prepared buffers for i/o. */
873 need_end_writeback
= true;
875 struct buffer_head
*next
= bh
->b_this_page
;
876 if (buffer_async_write(bh
)) {
877 submit_bh(REQ_OP_WRITE
, 0, bh
);
878 need_end_writeback
= false;
881 } while (bh
!= head
);
884 /* If no i/o was started, need to end_page_writeback(). */
885 if (unlikely(need_end_writeback
))
886 end_page_writeback(page
);
893 * ntfs_write_mst_block - write a @page to the backing store
894 * @page: page cache page to write out
895 * @wbc: writeback control structure
897 * This function is for writing pages belonging to non-resident, mst protected
898 * attributes to their backing store. The only supported attributes are index
899 * allocation and $MFT/$DATA. Both directory inodes and index inodes are
900 * supported for the index allocation case.
902 * The page must remain locked for the duration of the write because we apply
903 * the mst fixups, write, and then undo the fixups, so if we were to unlock the
904 * page before undoing the fixups, any other user of the page will see the
905 * page contents as corrupt.
907 * We clear the page uptodate flag for the duration of the function to ensure
908 * exclusion for the $MFT/$DATA case against someone mapping an mft record we
909 * are about to apply the mst fixups to.
911 * Return 0 on success and -errno on error.
913 * Based on ntfs_write_block(), ntfs_mft_writepage(), and
914 * write_mft_record_nolock().
916 static int ntfs_write_mst_block(struct page
*page
,
917 struct writeback_control
*wbc
)
919 sector_t block
, dblock
, rec_block
;
920 struct inode
*vi
= page
->mapping
->host
;
921 ntfs_inode
*ni
= NTFS_I(vi
);
922 ntfs_volume
*vol
= ni
->vol
;
924 unsigned int rec_size
= ni
->itype
.index
.block_size
;
925 ntfs_inode
*locked_nis
[PAGE_SIZE
/ NTFS_BLOCK_SIZE
];
926 struct buffer_head
*bh
, *head
, *tbh
, *rec_start_bh
;
927 struct buffer_head
*bhs
[MAX_BUF_PER_PAGE
];
929 int i
, nr_locked_nis
, nr_recs
, nr_bhs
, max_bhs
, bhs_per_rec
, err
, err2
;
930 unsigned bh_size
, rec_size_bits
;
931 bool sync
, is_mft
, page_is_dirty
, rec_is_dirty
;
932 unsigned char bh_size_bits
;
934 if (WARN_ON(rec_size
< NTFS_BLOCK_SIZE
))
937 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
938 "0x%lx.", vi
->i_ino
, ni
->type
, page
->index
);
939 BUG_ON(!NInoNonResident(ni
));
940 BUG_ON(!NInoMstProtected(ni
));
941 is_mft
= (S_ISREG(vi
->i_mode
) && !vi
->i_ino
);
943 * NOTE: ntfs_write_mst_block() would be called for $MFTMirr if a page
944 * in its page cache were to be marked dirty. However this should
945 * never happen with the current driver and considering we do not
946 * handle this case here we do want to BUG(), at least for now.
948 BUG_ON(!(is_mft
|| S_ISDIR(vi
->i_mode
) ||
949 (NInoAttr(ni
) && ni
->type
== AT_INDEX_ALLOCATION
)));
950 bh_size
= vol
->sb
->s_blocksize
;
951 bh_size_bits
= vol
->sb
->s_blocksize_bits
;
952 max_bhs
= PAGE_SIZE
/ bh_size
;
954 BUG_ON(max_bhs
> MAX_BUF_PER_PAGE
);
956 /* Were we called for sync purposes? */
957 sync
= (wbc
->sync_mode
== WB_SYNC_ALL
);
959 /* Make sure we have mapped buffers. */
960 bh
= head
= page_buffers(page
);
963 rec_size_bits
= ni
->itype
.index
.block_size_bits
;
964 BUG_ON(!(PAGE_SIZE
>> rec_size_bits
));
965 bhs_per_rec
= rec_size
>> bh_size_bits
;
966 BUG_ON(!bhs_per_rec
);
968 /* The first block in the page. */
969 rec_block
= block
= (sector_t
)page
->index
<<
970 (PAGE_SHIFT
- bh_size_bits
);
972 /* The first out of bounds block for the data size. */
973 dblock
= (i_size_read(vi
) + bh_size
- 1) >> bh_size_bits
;
976 err
= err2
= nr_bhs
= nr_recs
= nr_locked_nis
= 0;
977 page_is_dirty
= rec_is_dirty
= false;
980 bool is_retry
= false;
982 if (likely(block
< rec_block
)) {
983 if (unlikely(block
>= dblock
)) {
984 clear_buffer_dirty(bh
);
985 set_buffer_uptodate(bh
);
989 * This block is not the first one in the record. We
990 * ignore the buffer's dirty state because we could
991 * have raced with a parallel mark_ntfs_record_dirty().
995 if (unlikely(err2
)) {
997 clear_buffer_dirty(bh
);
1000 } else /* if (block == rec_block) */ {
1001 BUG_ON(block
> rec_block
);
1002 /* This block is the first one in the record. */
1003 rec_block
+= bhs_per_rec
;
1005 if (unlikely(block
>= dblock
)) {
1006 clear_buffer_dirty(bh
);
1009 if (!buffer_dirty(bh
)) {
1010 /* Clean records are not written out. */
1011 rec_is_dirty
= false;
1014 rec_is_dirty
= true;
1017 /* Need to map the buffer if it is not mapped already. */
1018 if (unlikely(!buffer_mapped(bh
))) {
1021 unsigned int vcn_ofs
;
1023 bh
->b_bdev
= vol
->sb
->s_bdev
;
1024 /* Obtain the vcn and offset of the current block. */
1025 vcn
= (VCN
)block
<< bh_size_bits
;
1026 vcn_ofs
= vcn
& vol
->cluster_size_mask
;
1027 vcn
>>= vol
->cluster_size_bits
;
1030 down_read(&ni
->runlist
.lock
);
1031 rl
= ni
->runlist
.rl
;
1033 if (likely(rl
!= NULL
)) {
1034 /* Seek to element containing target vcn. */
1035 while (rl
->length
&& rl
[1].vcn
<= vcn
)
1037 lcn
= ntfs_rl_vcn_to_lcn(rl
, vcn
);
1039 lcn
= LCN_RL_NOT_MAPPED
;
1040 /* Successful remap. */
1041 if (likely(lcn
>= 0)) {
1042 /* Setup buffer head to correct block. */
1043 bh
->b_blocknr
= ((lcn
<<
1044 vol
->cluster_size_bits
) +
1045 vcn_ofs
) >> bh_size_bits
;
1046 set_buffer_mapped(bh
);
1049 * Remap failed. Retry to map the runlist once
1050 * unless we are working on $MFT which always
1051 * has the whole of its runlist in memory.
1053 if (!is_mft
&& !is_retry
&&
1054 lcn
== LCN_RL_NOT_MAPPED
) {
1057 * Attempt to map runlist, dropping
1058 * lock for the duration.
1060 up_read(&ni
->runlist
.lock
);
1061 err2
= ntfs_map_runlist(ni
, vcn
);
1063 goto lock_retry_remap
;
1064 if (err2
== -ENOMEM
)
1065 page_is_dirty
= true;
1070 up_read(&ni
->runlist
.lock
);
1072 /* Hard error. Abort writing this record. */
1073 if (!err
|| err
== -ENOMEM
)
1076 ntfs_error(vol
->sb
, "Cannot write ntfs record "
1077 "0x%llx (inode 0x%lx, "
1078 "attribute type 0x%x) because "
1079 "its location on disk could "
1080 "not be determined (error "
1084 vol
->mft_record_size_bits
,
1085 ni
->mft_no
, ni
->type
,
1088 * If this is not the first buffer, remove the
1089 * buffers in this record from the list of
1090 * buffers to write and clear their dirty bit
1091 * if not error -ENOMEM.
1093 if (rec_start_bh
!= bh
) {
1094 while (bhs
[--nr_bhs
] != rec_start_bh
)
1096 if (err2
!= -ENOMEM
) {
1100 } while ((rec_start_bh
=
1109 BUG_ON(!buffer_uptodate(bh
));
1110 BUG_ON(nr_bhs
>= max_bhs
);
1112 } while (block
++, (bh
= bh
->b_this_page
) != head
);
1114 up_read(&ni
->runlist
.lock
);
1115 /* If there were no dirty buffers, we are done. */
1118 /* Map the page so we can access its contents. */
1120 /* Clear the page uptodate flag whilst the mst fixups are applied. */
1121 BUG_ON(!PageUptodate(page
));
1122 ClearPageUptodate(page
);
1123 for (i
= 0; i
< nr_bhs
; i
++) {
1126 /* Skip buffers which are not at the beginning of records. */
1127 if (i
% bhs_per_rec
)
1130 ofs
= bh_offset(tbh
);
1133 unsigned long mft_no
;
1135 /* Get the mft record number. */
1136 mft_no
= (((s64
)page
->index
<< PAGE_SHIFT
) + ofs
)
1138 /* Check whether to write this mft record. */
1140 if (!ntfs_may_write_mft_record(vol
, mft_no
,
1141 (MFT_RECORD
*)(kaddr
+ ofs
), &tni
)) {
1143 * The record should not be written. This
1144 * means we need to redirty the page before
1147 page_is_dirty
= true;
1149 * Remove the buffers in this mft record from
1150 * the list of buffers to write.
1154 } while (++i
% bhs_per_rec
);
1158 * The record should be written. If a locked ntfs
1159 * inode was returned, add it to the array of locked
1163 locked_nis
[nr_locked_nis
++] = tni
;
1165 /* Apply the mst protection fixups. */
1166 err2
= pre_write_mst_fixup((NTFS_RECORD
*)(kaddr
+ ofs
),
1168 if (unlikely(err2
)) {
1169 if (!err
|| err
== -ENOMEM
)
1171 ntfs_error(vol
->sb
, "Failed to apply mst fixups "
1172 "(inode 0x%lx, attribute type 0x%x, "
1173 "page index 0x%lx, page offset 0x%x)!"
1174 " Unmount and run chkdsk.", vi
->i_ino
,
1175 ni
->type
, page
->index
, ofs
);
1177 * Mark all the buffers in this record clean as we do
1178 * not want to write corrupt data to disk.
1181 clear_buffer_dirty(bhs
[i
]);
1183 } while (++i
% bhs_per_rec
);
1188 /* If no records are to be written out, we are done. */
1191 flush_dcache_page(page
);
1192 /* Lock buffers and start synchronous write i/o on them. */
1193 for (i
= 0; i
< nr_bhs
; i
++) {
1197 if (!trylock_buffer(tbh
))
1199 /* The buffer dirty state is now irrelevant, just clean it. */
1200 clear_buffer_dirty(tbh
);
1201 BUG_ON(!buffer_uptodate(tbh
));
1202 BUG_ON(!buffer_mapped(tbh
));
1204 tbh
->b_end_io
= end_buffer_write_sync
;
1205 submit_bh(REQ_OP_WRITE
, 0, tbh
);
1207 /* Synchronize the mft mirror now if not @sync. */
1208 if (is_mft
&& !sync
)
1211 /* Wait on i/o completion of buffers. */
1212 for (i
= 0; i
< nr_bhs
; i
++) {
1216 wait_on_buffer(tbh
);
1217 if (unlikely(!buffer_uptodate(tbh
))) {
1218 ntfs_error(vol
->sb
, "I/O error while writing ntfs "
1219 "record buffer (inode 0x%lx, "
1220 "attribute type 0x%x, page index "
1221 "0x%lx, page offset 0x%lx)! Unmount "
1222 "and run chkdsk.", vi
->i_ino
, ni
->type
,
1223 page
->index
, bh_offset(tbh
));
1224 if (!err
|| err
== -ENOMEM
)
1227 * Set the buffer uptodate so the page and buffer
1228 * states do not become out of sync.
1230 set_buffer_uptodate(tbh
);
1233 /* If @sync, now synchronize the mft mirror. */
1234 if (is_mft
&& sync
) {
1236 for (i
= 0; i
< nr_bhs
; i
++) {
1237 unsigned long mft_no
;
1241 * Skip buffers which are not at the beginning of
1244 if (i
% bhs_per_rec
)
1247 /* Skip removed buffers (and hence records). */
1250 ofs
= bh_offset(tbh
);
1251 /* Get the mft record number. */
1252 mft_no
= (((s64
)page
->index
<< PAGE_SHIFT
) + ofs
)
1254 if (mft_no
< vol
->mftmirr_size
)
1255 ntfs_sync_mft_mirror(vol
, mft_no
,
1256 (MFT_RECORD
*)(kaddr
+ ofs
),
1262 /* Remove the mst protection fixups again. */
1263 for (i
= 0; i
< nr_bhs
; i
++) {
1264 if (!(i
% bhs_per_rec
)) {
1268 post_write_mst_fixup((NTFS_RECORD
*)(kaddr
+
1272 flush_dcache_page(page
);
1274 /* Unlock any locked inodes. */
1275 while (nr_locked_nis
-- > 0) {
1276 ntfs_inode
*tni
, *base_tni
;
1278 tni
= locked_nis
[nr_locked_nis
];
1279 /* Get the base inode. */
1280 mutex_lock(&tni
->extent_lock
);
1281 if (tni
->nr_extents
>= 0)
1284 base_tni
= tni
->ext
.base_ntfs_ino
;
1287 mutex_unlock(&tni
->extent_lock
);
1288 ntfs_debug("Unlocking %s inode 0x%lx.",
1289 tni
== base_tni
? "base" : "extent",
1291 mutex_unlock(&tni
->mrec_lock
);
1292 atomic_dec(&tni
->count
);
1293 iput(VFS_I(base_tni
));
1295 SetPageUptodate(page
);
1298 if (unlikely(err
&& err
!= -ENOMEM
)) {
1300 * Set page error if there is only one ntfs record in the page.
1301 * Otherwise we would loose per-record granularity.
1303 if (ni
->itype
.index
.block_size
== PAGE_SIZE
)
1307 if (page_is_dirty
) {
1308 ntfs_debug("Page still contains one or more dirty ntfs "
1309 "records. Redirtying the page starting at "
1310 "record 0x%lx.", page
->index
<<
1311 (PAGE_SHIFT
- rec_size_bits
));
1312 redirty_page_for_writepage(wbc
, page
);
1316 * Keep the VM happy. This must be done otherwise the
1317 * radix-tree tag PAGECACHE_TAG_DIRTY remains set even though
1318 * the page is clean.
1320 BUG_ON(PageWriteback(page
));
1321 set_page_writeback(page
);
1323 end_page_writeback(page
);
1326 ntfs_debug("Done.");
1331 * ntfs_writepage - write a @page to the backing store
1332 * @page: page cache page to write out
1333 * @wbc: writeback control structure
1335 * This is called from the VM when it wants to have a dirty ntfs page cache
1336 * page cleaned. The VM has already locked the page and marked it clean.
1338 * For non-resident attributes, ntfs_writepage() writes the @page by calling
1339 * the ntfs version of the generic block_write_full_page() function,
1340 * ntfs_write_block(), which in turn if necessary creates and writes the
1341 * buffers associated with the page asynchronously.
1343 * For resident attributes, OTOH, ntfs_writepage() writes the @page by copying
1344 * the data to the mft record (which at this stage is most likely in memory).
1345 * The mft record is then marked dirty and written out asynchronously via the
1346 * vfs inode dirty code path for the inode the mft record belongs to or via the
1347 * vm page dirty code path for the page the mft record is in.
1349 * Based on ntfs_readpage() and fs/buffer.c::block_write_full_page().
1351 * Return 0 on success and -errno on error.
1353 static int ntfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1356 struct inode
*vi
= page
->mapping
->host
;
1357 ntfs_inode
*base_ni
= NULL
, *ni
= NTFS_I(vi
);
1359 ntfs_attr_search_ctx
*ctx
= NULL
;
1360 MFT_RECORD
*m
= NULL
;
1365 BUG_ON(!PageLocked(page
));
1366 i_size
= i_size_read(vi
);
1367 /* Is the page fully outside i_size? (truncate in progress) */
1368 if (unlikely(page
->index
>= (i_size
+ PAGE_SIZE
- 1) >>
1371 * The page may have dirty, unmapped buffers. Make them
1372 * freeable here, so the page does not leak.
1374 block_invalidatepage(page
, 0, PAGE_SIZE
);
1376 ntfs_debug("Write outside i_size - truncated?");
1380 * Only $DATA attributes can be encrypted and only unnamed $DATA
1381 * attributes can be compressed. Index root can have the flags set but
1382 * this means to create compressed/encrypted files, not that the
1383 * attribute is compressed/encrypted. Note we need to check for
1384 * AT_INDEX_ALLOCATION since this is the type of both directory and
1387 if (ni
->type
!= AT_INDEX_ALLOCATION
) {
1388 /* If file is encrypted, deny access, just like NT4. */
1389 if (NInoEncrypted(ni
)) {
1391 BUG_ON(ni
->type
!= AT_DATA
);
1392 ntfs_debug("Denying write access to encrypted file.");
1395 /* Compressed data streams are handled in compress.c. */
1396 if (NInoNonResident(ni
) && NInoCompressed(ni
)) {
1397 BUG_ON(ni
->type
!= AT_DATA
);
1398 BUG_ON(ni
->name_len
);
1399 // TODO: Implement and replace this with
1400 // return ntfs_write_compressed_block(page);
1402 ntfs_error(vi
->i_sb
, "Writing to compressed files is "
1403 "not supported yet. Sorry.");
1406 // TODO: Implement and remove this check.
1407 if (NInoNonResident(ni
) && NInoSparse(ni
)) {
1409 ntfs_error(vi
->i_sb
, "Writing to sparse files is not "
1410 "supported yet. Sorry.");
1414 /* NInoNonResident() == NInoIndexAllocPresent() */
1415 if (NInoNonResident(ni
)) {
1416 /* We have to zero every time due to mmap-at-end-of-file. */
1417 if (page
->index
>= (i_size
>> PAGE_SHIFT
)) {
1418 /* The page straddles i_size. */
1419 unsigned int ofs
= i_size
& ~PAGE_MASK
;
1420 zero_user_segment(page
, ofs
, PAGE_SIZE
);
1422 /* Handle mst protected attributes. */
1423 if (NInoMstProtected(ni
))
1424 return ntfs_write_mst_block(page
, wbc
);
1425 /* Normal, non-resident data stream. */
1426 return ntfs_write_block(page
, wbc
);
1429 * Attribute is resident, implying it is not compressed, encrypted, or
1430 * mst protected. This also means the attribute is smaller than an mft
1431 * record and hence smaller than a page, so can simply return error on
1432 * any pages with index above 0. Note the attribute can actually be
1433 * marked compressed but if it is resident the actual data is not
1434 * compressed so we are ok to ignore the compressed flag here.
1436 BUG_ON(page_has_buffers(page
));
1437 BUG_ON(!PageUptodate(page
));
1438 if (unlikely(page
->index
> 0)) {
1439 ntfs_error(vi
->i_sb
, "BUG()! page->index (0x%lx) > 0. "
1440 "Aborting write.", page
->index
);
1441 BUG_ON(PageWriteback(page
));
1442 set_page_writeback(page
);
1444 end_page_writeback(page
);
1450 base_ni
= ni
->ext
.base_ntfs_ino
;
1451 /* Map, pin, and lock the mft record. */
1452 m
= map_mft_record(base_ni
);
1460 * If a parallel write made the attribute non-resident, drop the mft
1461 * record and retry the writepage.
1463 if (unlikely(NInoNonResident(ni
))) {
1464 unmap_mft_record(base_ni
);
1465 goto retry_writepage
;
1467 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
1468 if (unlikely(!ctx
)) {
1472 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
1473 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
1477 * Keep the VM happy. This must be done otherwise the radix-tree tag
1478 * PAGECACHE_TAG_DIRTY remains set even though the page is clean.
1480 BUG_ON(PageWriteback(page
));
1481 set_page_writeback(page
);
1483 attr_len
= le32_to_cpu(ctx
->attr
->data
.resident
.value_length
);
1484 i_size
= i_size_read(vi
);
1485 if (unlikely(attr_len
> i_size
)) {
1486 /* Race with shrinking truncate or a failed truncate. */
1489 * If the truncate failed, fix it up now. If a concurrent
1490 * truncate, we do its job, so it does not have to do anything.
1492 err
= ntfs_resident_attr_value_resize(ctx
->mrec
, ctx
->attr
,
1494 /* Shrinking cannot fail. */
1497 addr
= kmap_atomic(page
);
1498 /* Copy the data from the page to the mft record. */
1499 memcpy((u8
*)ctx
->attr
+
1500 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
),
1502 /* Zero out of bounds area in the page cache page. */
1503 memset(addr
+ attr_len
, 0, PAGE_SIZE
- attr_len
);
1504 kunmap_atomic(addr
);
1505 flush_dcache_page(page
);
1506 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1507 /* We are done with the page. */
1508 end_page_writeback(page
);
1509 /* Finally, mark the mft record dirty, so it gets written back. */
1510 mark_mft_record_dirty(ctx
->ntfs_ino
);
1511 ntfs_attr_put_search_ctx(ctx
);
1512 unmap_mft_record(base_ni
);
1515 if (err
== -ENOMEM
) {
1516 ntfs_warning(vi
->i_sb
, "Error allocating memory. Redirtying "
1517 "page so we try again later.");
1519 * Put the page back on mapping->dirty_pages, but leave its
1520 * buffers' dirty state as-is.
1522 redirty_page_for_writepage(wbc
, page
);
1525 ntfs_error(vi
->i_sb
, "Resident attribute write failed with "
1528 NVolSetErrors(ni
->vol
);
1532 ntfs_attr_put_search_ctx(ctx
);
1534 unmap_mft_record(base_ni
);
1538 #endif /* NTFS_RW */
1541 * ntfs_bmap - map logical file block to physical device block
1542 * @mapping: address space mapping to which the block to be mapped belongs
1543 * @block: logical block to map to its physical device block
1545 * For regular, non-resident files (i.e. not compressed and not encrypted), map
1546 * the logical @block belonging to the file described by the address space
1547 * mapping @mapping to its physical device block.
1549 * The size of the block is equal to the @s_blocksize field of the super block
1550 * of the mounted file system which is guaranteed to be smaller than or equal
1551 * to the cluster size thus the block is guaranteed to fit entirely inside the
1552 * cluster which means we do not need to care how many contiguous bytes are
1553 * available after the beginning of the block.
1555 * Return the physical device block if the mapping succeeded or 0 if the block
1556 * is sparse or there was an error.
1558 * Note: This is a problem if someone tries to run bmap() on $Boot system file
1559 * as that really is in block zero but there is nothing we can do. bmap() is
1560 * just broken in that respect (just like it cannot distinguish sparse from
1561 * not available or error).
1563 static sector_t
ntfs_bmap(struct address_space
*mapping
, sector_t block
)
1568 unsigned long blocksize
, flags
;
1569 ntfs_inode
*ni
= NTFS_I(mapping
->host
);
1570 ntfs_volume
*vol
= ni
->vol
;
1572 unsigned char blocksize_bits
, cluster_size_shift
;
1574 ntfs_debug("Entering for mft_no 0x%lx, logical block 0x%llx.",
1575 ni
->mft_no
, (unsigned long long)block
);
1576 if (ni
->type
!= AT_DATA
|| !NInoNonResident(ni
) || NInoEncrypted(ni
)) {
1577 ntfs_error(vol
->sb
, "BMAP does not make sense for %s "
1578 "attributes, returning 0.",
1579 (ni
->type
!= AT_DATA
) ? "non-data" :
1580 (!NInoNonResident(ni
) ? "resident" :
1584 /* None of these can happen. */
1585 BUG_ON(NInoCompressed(ni
));
1586 BUG_ON(NInoMstProtected(ni
));
1587 blocksize
= vol
->sb
->s_blocksize
;
1588 blocksize_bits
= vol
->sb
->s_blocksize_bits
;
1589 ofs
= (s64
)block
<< blocksize_bits
;
1590 read_lock_irqsave(&ni
->size_lock
, flags
);
1591 size
= ni
->initialized_size
;
1592 i_size
= i_size_read(VFS_I(ni
));
1593 read_unlock_irqrestore(&ni
->size_lock
, flags
);
1595 * If the offset is outside the initialized size or the block straddles
1596 * the initialized size then pretend it is a hole unless the
1597 * initialized size equals the file size.
1599 if (unlikely(ofs
>= size
|| (ofs
+ blocksize
> size
&& size
< i_size
)))
1601 cluster_size_shift
= vol
->cluster_size_bits
;
1602 down_read(&ni
->runlist
.lock
);
1603 lcn
= ntfs_attr_vcn_to_lcn_nolock(ni
, ofs
>> cluster_size_shift
, false);
1604 up_read(&ni
->runlist
.lock
);
1605 if (unlikely(lcn
< LCN_HOLE
)) {
1607 * Step down to an integer to avoid gcc doing a long long
1608 * comparision in the switch when we know @lcn is between
1609 * LCN_HOLE and LCN_EIO (i.e. -1 to -5).
1611 * Otherwise older gcc (at least on some architectures) will
1612 * try to use __cmpdi2() which is of course not available in
1618 * If the offset is out of bounds then pretend it is a
1623 ntfs_error(vol
->sb
, "Not enough memory to complete "
1624 "mapping for inode 0x%lx. "
1625 "Returning 0.", ni
->mft_no
);
1628 ntfs_error(vol
->sb
, "Failed to complete mapping for "
1629 "inode 0x%lx. Run chkdsk. "
1630 "Returning 0.", ni
->mft_no
);
1638 ntfs_debug("Done (returning hole).");
1642 * The block is really allocated and fullfils all our criteria.
1643 * Convert the cluster to units of block size and return the result.
1645 delta
= ofs
& vol
->cluster_size_mask
;
1646 if (unlikely(sizeof(block
) < sizeof(lcn
))) {
1647 block
= lcn
= ((lcn
<< cluster_size_shift
) + delta
) >>
1649 /* If the block number was truncated return 0. */
1650 if (unlikely(block
!= lcn
)) {
1651 ntfs_error(vol
->sb
, "Physical block 0x%llx is too "
1652 "large to be returned, returning 0.",
1657 block
= ((lcn
<< cluster_size_shift
) + delta
) >>
1659 ntfs_debug("Done (returning block 0x%llx).", (unsigned long long)lcn
);
1664 * ntfs_normal_aops - address space operations for normal inodes and attributes
1666 * Note these are not used for compressed or mst protected inodes and
1669 const struct address_space_operations ntfs_normal_aops
= {
1670 .readpage
= ntfs_readpage
,
1672 .writepage
= ntfs_writepage
,
1673 .set_page_dirty
= __set_page_dirty_buffers
,
1674 #endif /* NTFS_RW */
1676 .migratepage
= buffer_migrate_page
,
1677 .is_partially_uptodate
= block_is_partially_uptodate
,
1678 .error_remove_page
= generic_error_remove_page
,
1682 * ntfs_compressed_aops - address space operations for compressed inodes
1684 const struct address_space_operations ntfs_compressed_aops
= {
1685 .readpage
= ntfs_readpage
,
1687 .writepage
= ntfs_writepage
,
1688 .set_page_dirty
= __set_page_dirty_buffers
,
1689 #endif /* NTFS_RW */
1690 .migratepage
= buffer_migrate_page
,
1691 .is_partially_uptodate
= block_is_partially_uptodate
,
1692 .error_remove_page
= generic_error_remove_page
,
1696 * ntfs_mst_aops - general address space operations for mst protecteed inodes
1699 const struct address_space_operations ntfs_mst_aops
= {
1700 .readpage
= ntfs_readpage
, /* Fill page with data. */
1702 .writepage
= ntfs_writepage
, /* Write dirty page to disk. */
1703 .set_page_dirty
= __set_page_dirty_nobuffers
, /* Set the page dirty
1704 without touching the buffers
1705 belonging to the page. */
1706 #endif /* NTFS_RW */
1707 .migratepage
= buffer_migrate_page
,
1708 .is_partially_uptodate
= block_is_partially_uptodate
,
1709 .error_remove_page
= generic_error_remove_page
,
1715 * mark_ntfs_record_dirty - mark an ntfs record dirty
1716 * @page: page containing the ntfs record to mark dirty
1717 * @ofs: byte offset within @page at which the ntfs record begins
1719 * Set the buffers and the page in which the ntfs record is located dirty.
1721 * The latter also marks the vfs inode the ntfs record belongs to dirty
1722 * (I_DIRTY_PAGES only).
1724 * If the page does not have buffers, we create them and set them uptodate.
1725 * The page may not be locked which is why we need to handle the buffers under
1726 * the mapping->private_lock. Once the buffers are marked dirty we no longer
1727 * need the lock since try_to_free_buffers() does not free dirty buffers.
1729 void mark_ntfs_record_dirty(struct page
*page
, const unsigned int ofs
) {
1730 struct address_space
*mapping
= page
->mapping
;
1731 ntfs_inode
*ni
= NTFS_I(mapping
->host
);
1732 struct buffer_head
*bh
, *head
, *buffers_to_free
= NULL
;
1733 unsigned int end
, bh_size
, bh_ofs
;
1735 BUG_ON(!PageUptodate(page
));
1736 end
= ofs
+ ni
->itype
.index
.block_size
;
1737 bh_size
= VFS_I(ni
)->i_sb
->s_blocksize
;
1738 spin_lock(&mapping
->private_lock
);
1739 if (unlikely(!page_has_buffers(page
))) {
1740 spin_unlock(&mapping
->private_lock
);
1741 bh
= head
= alloc_page_buffers(page
, bh_size
, true);
1742 spin_lock(&mapping
->private_lock
);
1743 if (likely(!page_has_buffers(page
))) {
1744 struct buffer_head
*tail
;
1747 set_buffer_uptodate(bh
);
1749 bh
= bh
->b_this_page
;
1751 tail
->b_this_page
= head
;
1752 attach_page_buffers(page
, head
);
1754 buffers_to_free
= bh
;
1756 bh
= head
= page_buffers(page
);
1759 bh_ofs
= bh_offset(bh
);
1760 if (bh_ofs
+ bh_size
<= ofs
)
1762 if (unlikely(bh_ofs
>= end
))
1764 set_buffer_dirty(bh
);
1765 } while ((bh
= bh
->b_this_page
) != head
);
1766 spin_unlock(&mapping
->private_lock
);
1767 __set_page_dirty_nobuffers(page
);
1768 if (unlikely(buffers_to_free
)) {
1770 bh
= buffers_to_free
->b_this_page
;
1771 free_buffer_head(buffers_to_free
);
1772 buffers_to_free
= bh
;
1773 } while (buffers_to_free
);
1777 #endif /* NTFS_RW */