2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "xfs_trans.h"
26 #include "xfs_dmapi.h"
27 #include "xfs_mount.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_alloc_btree.h"
30 #include "xfs_ialloc_btree.h"
31 #include "xfs_dir2_sf.h"
32 #include "xfs_attr_sf.h"
33 #include "xfs_dinode.h"
34 #include "xfs_inode.h"
35 #include "xfs_alloc.h"
36 #include "xfs_btree.h"
37 #include "xfs_error.h"
39 #include "xfs_iomap.h"
40 #include "xfs_vnodeops.h"
41 #include "xfs_trace.h"
43 #include <linux/gfp.h>
44 #include <linux/mpage.h>
45 #include <linux/pagevec.h>
46 #include <linux/writeback.h>
49 * Types of I/O for bmap clustering and I/O completion tracking.
52 IO_READ
, /* mapping for a read */
53 IO_DELAY
, /* mapping covers delalloc region */
54 IO_UNWRITTEN
, /* mapping covers allocated but uninitialized data */
55 IO_NEW
/* just allocated */
59 * Prime number of hash buckets since address is used as the key.
62 #define to_ioend_wq(v) (&xfs_ioend_wq[((unsigned long)v) % NVSYNC])
63 static wait_queue_head_t xfs_ioend_wq
[NVSYNC
];
70 for (i
= 0; i
< NVSYNC
; i
++)
71 init_waitqueue_head(&xfs_ioend_wq
[i
]);
78 wait_queue_head_t
*wq
= to_ioend_wq(ip
);
80 wait_event(*wq
, (atomic_read(&ip
->i_iocount
) == 0));
87 if (atomic_dec_and_test(&ip
->i_iocount
))
88 wake_up(to_ioend_wq(ip
));
98 struct buffer_head
*bh
, *head
;
100 *delalloc
= *unmapped
= *unwritten
= 0;
102 bh
= head
= page_buffers(page
);
104 if (buffer_uptodate(bh
) && !buffer_mapped(bh
))
106 else if (buffer_unwritten(bh
))
108 else if (buffer_delay(bh
))
110 } while ((bh
= bh
->b_this_page
) != head
);
113 STATIC
struct block_device
*
114 xfs_find_bdev_for_inode(
117 struct xfs_inode
*ip
= XFS_I(inode
);
118 struct xfs_mount
*mp
= ip
->i_mount
;
120 if (XFS_IS_REALTIME_INODE(ip
))
121 return mp
->m_rtdev_targp
->bt_bdev
;
123 return mp
->m_ddev_targp
->bt_bdev
;
127 * We're now finished for good with this ioend structure.
128 * Update the page state via the associated buffer_heads,
129 * release holds on the inode and bio, and finally free
130 * up memory. Do not use the ioend after this.
136 struct buffer_head
*bh
, *next
;
137 struct xfs_inode
*ip
= XFS_I(ioend
->io_inode
);
139 for (bh
= ioend
->io_buffer_head
; bh
; bh
= next
) {
140 next
= bh
->b_private
;
141 bh
->b_end_io(bh
, !ioend
->io_error
);
145 * Volume managers supporting multiple paths can send back ENODEV
146 * when the final path disappears. In this case continuing to fill
147 * the page cache with dirty data which cannot be written out is
148 * evil, so prevent that.
150 if (unlikely(ioend
->io_error
== -ENODEV
)) {
151 xfs_do_force_shutdown(ip
->i_mount
, SHUTDOWN_DEVICE_REQ
,
156 mempool_free(ioend
, xfs_ioend_pool
);
160 * If the end of the current ioend is beyond the current EOF,
161 * return the new EOF value, otherwise zero.
167 xfs_inode_t
*ip
= XFS_I(ioend
->io_inode
);
171 bsize
= ioend
->io_offset
+ ioend
->io_size
;
172 isize
= MAX(ip
->i_size
, ip
->i_new_size
);
173 isize
= MIN(isize
, bsize
);
174 return isize
> ip
->i_d
.di_size
? isize
: 0;
178 * Update on-disk file size now that data has been written to disk. The
179 * current in-memory file size is i_size. If a write is beyond eof i_new_size
180 * will be the intended file size until i_size is updated. If this write does
181 * not extend all the way to the valid file size then restrict this update to
182 * the end of the write.
184 * This function does not block as blocking on the inode lock in IO completion
185 * can lead to IO completion order dependency deadlocks.. If it can't get the
186 * inode ilock it will return EAGAIN. Callers must handle this.
192 xfs_inode_t
*ip
= XFS_I(ioend
->io_inode
);
195 ASSERT((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
);
196 ASSERT(ioend
->io_type
!= IO_READ
);
198 if (unlikely(ioend
->io_error
))
201 if (!xfs_ilock_nowait(ip
, XFS_ILOCK_EXCL
))
204 isize
= xfs_ioend_new_eof(ioend
);
206 ip
->i_d
.di_size
= isize
;
207 xfs_mark_inode_dirty(ip
);
210 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
215 * Schedule IO completion handling on a xfsdatad if this was
216 * the final hold on this ioend. If we are asked to wait,
217 * flush the workqueue.
224 if (atomic_dec_and_test(&ioend
->io_remaining
)) {
225 struct workqueue_struct
*wq
;
227 wq
= (ioend
->io_type
== IO_UNWRITTEN
) ?
228 xfsconvertd_workqueue
: xfsdatad_workqueue
;
229 queue_work(wq
, &ioend
->io_work
);
236 * IO write completion.
240 struct work_struct
*work
)
242 xfs_ioend_t
*ioend
= container_of(work
, xfs_ioend_t
, io_work
);
243 struct xfs_inode
*ip
= XFS_I(ioend
->io_inode
);
247 * For unwritten extents we need to issue transactions to convert a
248 * range to normal written extens after the data I/O has finished.
250 if (ioend
->io_type
== IO_UNWRITTEN
&&
251 likely(!ioend
->io_error
&& !XFS_FORCED_SHUTDOWN(ip
->i_mount
))) {
253 error
= xfs_iomap_write_unwritten(ip
, ioend
->io_offset
,
256 ioend
->io_error
= error
;
260 * We might have to update the on-disk file size after extending
263 if (ioend
->io_type
!= IO_READ
) {
264 error
= xfs_setfilesize(ioend
);
265 ASSERT(!error
|| error
== EAGAIN
);
269 * If we didn't complete processing of the ioend, requeue it to the
270 * tail of the workqueue for another attempt later. Otherwise destroy
273 if (error
== EAGAIN
) {
274 atomic_inc(&ioend
->io_remaining
);
275 xfs_finish_ioend(ioend
, 0);
276 /* ensure we don't spin on blocked ioends */
279 xfs_destroy_ioend(ioend
);
283 * Allocate and initialise an IO completion structure.
284 * We need to track unwritten extent write completion here initially.
285 * We'll need to extend this for updating the ondisk inode size later
295 ioend
= mempool_alloc(xfs_ioend_pool
, GFP_NOFS
);
298 * Set the count to 1 initially, which will prevent an I/O
299 * completion callback from happening before we have started
300 * all the I/O from calling the completion routine too early.
302 atomic_set(&ioend
->io_remaining
, 1);
304 ioend
->io_list
= NULL
;
305 ioend
->io_type
= type
;
306 ioend
->io_inode
= inode
;
307 ioend
->io_buffer_head
= NULL
;
308 ioend
->io_buffer_tail
= NULL
;
309 atomic_inc(&XFS_I(ioend
->io_inode
)->i_iocount
);
310 ioend
->io_offset
= 0;
313 INIT_WORK(&ioend
->io_work
, xfs_end_io
);
322 struct xfs_bmbt_irec
*imap
,
328 return -xfs_iomap(XFS_I(inode
), offset
, count
, flags
, imap
, &nmaps
, &new);
334 struct xfs_bmbt_irec
*imap
,
337 offset
>>= inode
->i_blkbits
;
339 return offset
>= imap
->br_startoff
&&
340 offset
< imap
->br_startoff
+ imap
->br_blockcount
;
344 * BIO completion handler for buffered IO.
351 xfs_ioend_t
*ioend
= bio
->bi_private
;
353 ASSERT(atomic_read(&bio
->bi_cnt
) >= 1);
354 ioend
->io_error
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
) ? 0 : error
;
356 /* Toss bio and pass work off to an xfsdatad thread */
357 bio
->bi_private
= NULL
;
358 bio
->bi_end_io
= NULL
;
361 xfs_finish_ioend(ioend
, 0);
365 xfs_submit_ioend_bio(
366 struct writeback_control
*wbc
,
370 atomic_inc(&ioend
->io_remaining
);
371 bio
->bi_private
= ioend
;
372 bio
->bi_end_io
= xfs_end_bio
;
375 * If the I/O is beyond EOF we mark the inode dirty immediately
376 * but don't update the inode size until I/O completion.
378 if (xfs_ioend_new_eof(ioend
))
379 xfs_mark_inode_dirty(XFS_I(ioend
->io_inode
));
381 submit_bio(wbc
->sync_mode
== WB_SYNC_ALL
?
382 WRITE_SYNC_PLUG
: WRITE
, bio
);
383 ASSERT(!bio_flagged(bio
, BIO_EOPNOTSUPP
));
389 struct buffer_head
*bh
)
392 int nvecs
= bio_get_nr_vecs(bh
->b_bdev
);
395 bio
= bio_alloc(GFP_NOIO
, nvecs
);
399 ASSERT(bio
->bi_private
== NULL
);
400 bio
->bi_sector
= bh
->b_blocknr
* (bh
->b_size
>> 9);
401 bio
->bi_bdev
= bh
->b_bdev
;
407 xfs_start_buffer_writeback(
408 struct buffer_head
*bh
)
410 ASSERT(buffer_mapped(bh
));
411 ASSERT(buffer_locked(bh
));
412 ASSERT(!buffer_delay(bh
));
413 ASSERT(!buffer_unwritten(bh
));
415 mark_buffer_async_write(bh
);
416 set_buffer_uptodate(bh
);
417 clear_buffer_dirty(bh
);
421 xfs_start_page_writeback(
426 ASSERT(PageLocked(page
));
427 ASSERT(!PageWriteback(page
));
429 clear_page_dirty_for_io(page
);
430 set_page_writeback(page
);
432 /* If no buffers on the page are to be written, finish it here */
434 end_page_writeback(page
);
437 static inline int bio_add_buffer(struct bio
*bio
, struct buffer_head
*bh
)
439 return bio_add_page(bio
, bh
->b_page
, bh
->b_size
, bh_offset(bh
));
443 * Submit all of the bios for all of the ioends we have saved up, covering the
444 * initial writepage page and also any probed pages.
446 * Because we may have multiple ioends spanning a page, we need to start
447 * writeback on all the buffers before we submit them for I/O. If we mark the
448 * buffers as we got, then we can end up with a page that only has buffers
449 * marked async write and I/O complete on can occur before we mark the other
450 * buffers async write.
452 * The end result of this is that we trip a bug in end_page_writeback() because
453 * we call it twice for the one page as the code in end_buffer_async_write()
454 * assumes that all buffers on the page are started at the same time.
456 * The fix is two passes across the ioend list - one to start writeback on the
457 * buffer_heads, and then submit them for I/O on the second pass.
461 struct writeback_control
*wbc
,
464 xfs_ioend_t
*head
= ioend
;
466 struct buffer_head
*bh
;
468 sector_t lastblock
= 0;
470 /* Pass 1 - start writeback */
472 next
= ioend
->io_list
;
473 for (bh
= ioend
->io_buffer_head
; bh
; bh
= bh
->b_private
) {
474 xfs_start_buffer_writeback(bh
);
476 } while ((ioend
= next
) != NULL
);
478 /* Pass 2 - submit I/O */
481 next
= ioend
->io_list
;
484 for (bh
= ioend
->io_buffer_head
; bh
; bh
= bh
->b_private
) {
488 bio
= xfs_alloc_ioend_bio(bh
);
489 } else if (bh
->b_blocknr
!= lastblock
+ 1) {
490 xfs_submit_ioend_bio(wbc
, ioend
, bio
);
494 if (bio_add_buffer(bio
, bh
) != bh
->b_size
) {
495 xfs_submit_ioend_bio(wbc
, ioend
, bio
);
499 lastblock
= bh
->b_blocknr
;
502 xfs_submit_ioend_bio(wbc
, ioend
, bio
);
503 xfs_finish_ioend(ioend
, 0);
504 } while ((ioend
= next
) != NULL
);
508 * Cancel submission of all buffer_heads so far in this endio.
509 * Toss the endio too. Only ever called for the initial page
510 * in a writepage request, so only ever one page.
517 struct buffer_head
*bh
, *next_bh
;
520 next
= ioend
->io_list
;
521 bh
= ioend
->io_buffer_head
;
523 next_bh
= bh
->b_private
;
524 clear_buffer_async_write(bh
);
526 } while ((bh
= next_bh
) != NULL
);
528 xfs_ioend_wake(XFS_I(ioend
->io_inode
));
529 mempool_free(ioend
, xfs_ioend_pool
);
530 } while ((ioend
= next
) != NULL
);
534 * Test to see if we've been building up a completion structure for
535 * earlier buffers -- if so, we try to append to this ioend if we
536 * can, otherwise we finish off any current ioend and start another.
537 * Return true if we've finished the given ioend.
542 struct buffer_head
*bh
,
545 xfs_ioend_t
**result
,
548 xfs_ioend_t
*ioend
= *result
;
550 if (!ioend
|| need_ioend
|| type
!= ioend
->io_type
) {
551 xfs_ioend_t
*previous
= *result
;
553 ioend
= xfs_alloc_ioend(inode
, type
);
554 ioend
->io_offset
= offset
;
555 ioend
->io_buffer_head
= bh
;
556 ioend
->io_buffer_tail
= bh
;
558 previous
->io_list
= ioend
;
561 ioend
->io_buffer_tail
->b_private
= bh
;
562 ioend
->io_buffer_tail
= bh
;
565 bh
->b_private
= NULL
;
566 ioend
->io_size
+= bh
->b_size
;
572 struct buffer_head
*bh
,
573 struct xfs_bmbt_irec
*imap
,
577 struct xfs_mount
*m
= XFS_I(inode
)->i_mount
;
578 xfs_off_t iomap_offset
= XFS_FSB_TO_B(m
, imap
->br_startoff
);
579 xfs_daddr_t iomap_bn
= xfs_fsb_to_db(XFS_I(inode
), imap
->br_startblock
);
581 ASSERT(imap
->br_startblock
!= HOLESTARTBLOCK
);
582 ASSERT(imap
->br_startblock
!= DELAYSTARTBLOCK
);
584 bn
= (iomap_bn
>> (inode
->i_blkbits
- BBSHIFT
)) +
585 ((offset
- iomap_offset
) >> inode
->i_blkbits
);
587 ASSERT(bn
|| XFS_IS_REALTIME_INODE(XFS_I(inode
)));
590 set_buffer_mapped(bh
);
596 struct buffer_head
*bh
,
597 struct xfs_bmbt_irec
*imap
,
600 ASSERT(imap
->br_startblock
!= HOLESTARTBLOCK
);
601 ASSERT(imap
->br_startblock
!= DELAYSTARTBLOCK
);
604 xfs_map_buffer(inode
, bh
, imap
, offset
);
605 bh
->b_bdev
= xfs_find_bdev_for_inode(inode
);
606 set_buffer_mapped(bh
);
607 clear_buffer_delay(bh
);
608 clear_buffer_unwritten(bh
);
612 * Look for a page at index that is suitable for clustering.
617 unsigned int pg_offset
,
622 if (PageWriteback(page
))
625 if (page
->mapping
&& PageDirty(page
)) {
626 if (page_has_buffers(page
)) {
627 struct buffer_head
*bh
, *head
;
629 bh
= head
= page_buffers(page
);
631 if (!buffer_uptodate(bh
))
633 if (mapped
!= buffer_mapped(bh
))
636 if (ret
>= pg_offset
)
638 } while ((bh
= bh
->b_this_page
) != head
);
640 ret
= mapped
? 0 : PAGE_CACHE_SIZE
;
649 struct page
*startpage
,
650 struct buffer_head
*bh
,
651 struct buffer_head
*head
,
655 pgoff_t tindex
, tlast
, tloff
;
659 /* First sum forwards in this page */
661 if (!buffer_uptodate(bh
) || (mapped
!= buffer_mapped(bh
)))
664 } while ((bh
= bh
->b_this_page
) != head
);
666 /* if we reached the end of the page, sum forwards in following pages */
667 tlast
= i_size_read(inode
) >> PAGE_CACHE_SHIFT
;
668 tindex
= startpage
->index
+ 1;
670 /* Prune this back to avoid pathological behavior */
671 tloff
= min(tlast
, startpage
->index
+ 64);
673 pagevec_init(&pvec
, 0);
674 while (!done
&& tindex
<= tloff
) {
675 unsigned len
= min_t(pgoff_t
, PAGEVEC_SIZE
, tlast
- tindex
+ 1);
677 if (!pagevec_lookup(&pvec
, inode
->i_mapping
, tindex
, len
))
680 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
681 struct page
*page
= pvec
.pages
[i
];
682 size_t pg_offset
, pg_len
= 0;
684 if (tindex
== tlast
) {
686 i_size_read(inode
) & (PAGE_CACHE_SIZE
- 1);
692 pg_offset
= PAGE_CACHE_SIZE
;
694 if (page
->index
== tindex
&& trylock_page(page
)) {
695 pg_len
= xfs_probe_page(page
, pg_offset
, mapped
);
708 pagevec_release(&pvec
);
716 * Test if a given page is suitable for writing as part of an unwritten
717 * or delayed allocate extent.
724 if (PageWriteback(page
))
727 if (page
->mapping
&& page_has_buffers(page
)) {
728 struct buffer_head
*bh
, *head
;
731 bh
= head
= page_buffers(page
);
733 if (buffer_unwritten(bh
))
734 acceptable
= (type
== IO_UNWRITTEN
);
735 else if (buffer_delay(bh
))
736 acceptable
= (type
== IO_DELAY
);
737 else if (buffer_dirty(bh
) && buffer_mapped(bh
))
738 acceptable
= (type
== IO_NEW
);
741 } while ((bh
= bh
->b_this_page
) != head
);
751 * Allocate & map buffers for page given the extent map. Write it out.
752 * except for the original page of a writepage, this is called on
753 * delalloc/unwritten pages only, for the original page it is possible
754 * that the page has no mapping at all.
761 struct xfs_bmbt_irec
*imap
,
762 xfs_ioend_t
**ioendp
,
763 struct writeback_control
*wbc
,
767 struct buffer_head
*bh
, *head
;
768 xfs_off_t end_offset
;
769 unsigned long p_offset
;
772 int count
= 0, done
= 0, uptodate
= 1;
773 xfs_off_t offset
= page_offset(page
);
775 if (page
->index
!= tindex
)
777 if (!trylock_page(page
))
779 if (PageWriteback(page
))
780 goto fail_unlock_page
;
781 if (page
->mapping
!= inode
->i_mapping
)
782 goto fail_unlock_page
;
783 if (!xfs_is_delayed_page(page
, (*ioendp
)->io_type
))
784 goto fail_unlock_page
;
787 * page_dirty is initially a count of buffers on the page before
788 * EOF and is decremented as we move each into a cleanable state.
792 * End offset is the highest offset that this page should represent.
793 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
794 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
795 * hence give us the correct page_dirty count. On any other page,
796 * it will be zero and in that case we need page_dirty to be the
797 * count of buffers on the page.
799 end_offset
= min_t(unsigned long long,
800 (xfs_off_t
)(page
->index
+ 1) << PAGE_CACHE_SHIFT
,
803 len
= 1 << inode
->i_blkbits
;
804 p_offset
= min_t(unsigned long, end_offset
& (PAGE_CACHE_SIZE
- 1),
806 p_offset
= p_offset
? roundup(p_offset
, len
) : PAGE_CACHE_SIZE
;
807 page_dirty
= p_offset
/ len
;
809 bh
= head
= page_buffers(page
);
811 if (offset
>= end_offset
)
813 if (!buffer_uptodate(bh
))
815 if (!(PageUptodate(page
) || buffer_uptodate(bh
))) {
820 if (buffer_unwritten(bh
) || buffer_delay(bh
)) {
821 if (buffer_unwritten(bh
))
826 if (!xfs_imap_valid(inode
, imap
, offset
)) {
831 ASSERT(imap
->br_startblock
!= HOLESTARTBLOCK
);
832 ASSERT(imap
->br_startblock
!= DELAYSTARTBLOCK
);
834 xfs_map_at_offset(inode
, bh
, imap
, offset
);
836 xfs_add_to_ioend(inode
, bh
, offset
,
839 set_buffer_dirty(bh
);
841 mark_buffer_dirty(bh
);
847 if (buffer_mapped(bh
) && all_bh
&& startio
) {
849 xfs_add_to_ioend(inode
, bh
, offset
,
857 } while (offset
+= len
, (bh
= bh
->b_this_page
) != head
);
859 if (uptodate
&& bh
== head
)
860 SetPageUptodate(page
);
865 if (wbc
->nr_to_write
<= 0)
868 xfs_start_page_writeback(page
, !page_dirty
, count
);
879 * Convert & write out a cluster of pages in the same extent as defined
880 * by mp and following the start page.
886 struct xfs_bmbt_irec
*imap
,
887 xfs_ioend_t
**ioendp
,
888 struct writeback_control
*wbc
,
896 pagevec_init(&pvec
, 0);
897 while (!done
&& tindex
<= tlast
) {
898 unsigned len
= min_t(pgoff_t
, PAGEVEC_SIZE
, tlast
- tindex
+ 1);
900 if (!pagevec_lookup(&pvec
, inode
->i_mapping
, tindex
, len
))
903 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
904 done
= xfs_convert_page(inode
, pvec
.pages
[i
], tindex
++,
905 imap
, ioendp
, wbc
, startio
, all_bh
);
910 pagevec_release(&pvec
);
916 xfs_vm_invalidatepage(
918 unsigned long offset
)
920 trace_xfs_invalidatepage(page
->mapping
->host
, page
, offset
);
921 block_invalidatepage(page
, offset
);
925 * If the page has delalloc buffers on it, we need to punch them out before we
926 * invalidate the page. If we don't, we leave a stale delalloc mapping on the
927 * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
928 * is done on that same region - the delalloc extent is returned when none is
929 * supposed to be there.
931 * We prevent this by truncating away the delalloc regions on the page before
932 * invalidating it. Because they are delalloc, we can do this without needing a
933 * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
934 * truncation without a transaction as there is no space left for block
935 * reservation (typically why we see a ENOSPC in writeback).
937 * This is not a performance critical path, so for now just do the punching a
938 * buffer head at a time.
941 xfs_aops_discard_page(
944 struct inode
*inode
= page
->mapping
->host
;
945 struct xfs_inode
*ip
= XFS_I(inode
);
946 struct buffer_head
*bh
, *head
;
947 loff_t offset
= page_offset(page
);
948 ssize_t len
= 1 << inode
->i_blkbits
;
950 if (!xfs_is_delayed_page(page
, IO_DELAY
))
953 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
956 xfs_fs_cmn_err(CE_ALERT
, ip
->i_mount
,
957 "page discard on page %p, inode 0x%llx, offset %llu.",
958 page
, ip
->i_ino
, offset
);
960 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
961 bh
= head
= page_buffers(page
);
964 xfs_fileoff_t offset_fsb
;
965 xfs_bmbt_irec_t imap
;
968 xfs_fsblock_t firstblock
;
969 xfs_bmap_free_t flist
;
971 if (!buffer_delay(bh
))
974 offset_fsb
= XFS_B_TO_FSBT(ip
->i_mount
, offset
);
977 * Map the range first and check that it is a delalloc extent
978 * before trying to unmap the range. Otherwise we will be
979 * trying to remove a real extent (which requires a
980 * transaction) or a hole, which is probably a bad idea...
982 error
= xfs_bmapi(NULL
, ip
, offset_fsb
, 1,
983 XFS_BMAPI_ENTIRE
, NULL
, 0, &imap
,
984 &nimaps
, NULL
, NULL
);
987 /* something screwed, just bail */
988 if (!XFS_FORCED_SHUTDOWN(ip
->i_mount
)) {
989 xfs_fs_cmn_err(CE_ALERT
, ip
->i_mount
,
990 "page discard failed delalloc mapping lookup.");
998 if (imap
.br_startblock
!= DELAYSTARTBLOCK
) {
999 /* been converted, ignore */
1002 WARN_ON(imap
.br_blockcount
== 0);
1005 * Note: while we initialise the firstblock/flist pair, they
1006 * should never be used because blocks should never be
1007 * allocated or freed for a delalloc extent and hence we need
1008 * don't cancel or finish them after the xfs_bunmapi() call.
1010 xfs_bmap_init(&flist
, &firstblock
);
1011 error
= xfs_bunmapi(NULL
, ip
, offset_fsb
, 1, 0, 1, &firstblock
,
1012 &flist
, NULL
, &done
);
1014 ASSERT(!flist
.xbf_count
&& !flist
.xbf_first
);
1016 /* something screwed, just bail */
1017 if (!XFS_FORCED_SHUTDOWN(ip
->i_mount
)) {
1018 xfs_fs_cmn_err(CE_ALERT
, ip
->i_mount
,
1019 "page discard unable to remove delalloc mapping.");
1026 } while ((bh
= bh
->b_this_page
) != head
);
1028 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1030 xfs_vm_invalidatepage(page
, 0);
1035 * Calling this without startio set means we are being asked to make a dirty
1036 * page ready for freeing it's buffers. When called with startio set then
1037 * we are coming from writepage.
1039 * When called with startio set it is important that we write the WHOLE
1041 * The bh->b_state's cannot know if any of the blocks or which block for
1042 * that matter are dirty due to mmap writes, and therefore bh uptodate is
1043 * only valid if the page itself isn't completely uptodate. Some layers
1044 * may clear the page dirty flag prior to calling write page, under the
1045 * assumption the entire page will be written out; by not writing out the
1046 * whole page the page can be reused before all valid dirty data is
1047 * written out. Note: in the case of a page that has been dirty'd by
1048 * mapwrite and but partially setup by block_prepare_write the
1049 * bh->b_states's will not agree and only ones setup by BPW/BCW will have
1050 * valid state, thus the whole page must be written out thing.
1054 xfs_page_state_convert(
1055 struct inode
*inode
,
1057 struct writeback_control
*wbc
,
1059 int unmapped
) /* also implies page uptodate */
1061 struct buffer_head
*bh
, *head
;
1062 struct xfs_bmbt_irec imap
;
1063 xfs_ioend_t
*ioend
= NULL
, *iohead
= NULL
;
1065 unsigned long p_offset
= 0;
1067 __uint64_t end_offset
;
1068 pgoff_t end_index
, last_index
;
1070 int flags
, err
, imap_valid
= 0, uptodate
= 1;
1071 int page_dirty
, count
= 0;
1073 int all_bh
= unmapped
;
1076 if (wbc
->sync_mode
== WB_SYNC_NONE
&& wbc
->nonblocking
)
1077 trylock
|= BMAPI_TRYLOCK
;
1080 /* Is this page beyond the end of the file? */
1081 offset
= i_size_read(inode
);
1082 end_index
= offset
>> PAGE_CACHE_SHIFT
;
1083 last_index
= (offset
- 1) >> PAGE_CACHE_SHIFT
;
1084 if (page
->index
>= end_index
) {
1085 if ((page
->index
>= end_index
+ 1) ||
1086 !(i_size_read(inode
) & (PAGE_CACHE_SIZE
- 1))) {
1094 * page_dirty is initially a count of buffers on the page before
1095 * EOF and is decremented as we move each into a cleanable state.
1099 * End offset is the highest offset that this page should represent.
1100 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
1101 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
1102 * hence give us the correct page_dirty count. On any other page,
1103 * it will be zero and in that case we need page_dirty to be the
1104 * count of buffers on the page.
1106 end_offset
= min_t(unsigned long long,
1107 (xfs_off_t
)(page
->index
+ 1) << PAGE_CACHE_SHIFT
, offset
);
1108 len
= 1 << inode
->i_blkbits
;
1109 p_offset
= min_t(unsigned long, end_offset
& (PAGE_CACHE_SIZE
- 1),
1111 p_offset
= p_offset
? roundup(p_offset
, len
) : PAGE_CACHE_SIZE
;
1112 page_dirty
= p_offset
/ len
;
1114 bh
= head
= page_buffers(page
);
1115 offset
= page_offset(page
);
1119 /* TODO: cleanup count and page_dirty */
1122 if (offset
>= end_offset
)
1124 if (!buffer_uptodate(bh
))
1126 if (!(PageUptodate(page
) || buffer_uptodate(bh
)) && !startio
) {
1128 * the iomap is actually still valid, but the ioend
1129 * isn't. shouldn't happen too often.
1136 imap_valid
= xfs_imap_valid(inode
, &imap
, offset
);
1139 * First case, map an unwritten extent and prepare for
1140 * extent state conversion transaction on completion.
1142 * Second case, allocate space for a delalloc buffer.
1143 * We can return EAGAIN here in the release page case.
1145 * Third case, an unmapped buffer was found, and we are
1146 * in a path where we need to write the whole page out.
1148 if (buffer_unwritten(bh
) || buffer_delay(bh
) ||
1149 ((buffer_uptodate(bh
) || PageUptodate(page
)) &&
1150 !buffer_mapped(bh
) && (unmapped
|| startio
))) {
1154 * Make sure we don't use a read-only iomap
1156 if (flags
== BMAPI_READ
)
1159 if (buffer_unwritten(bh
)) {
1160 type
= IO_UNWRITTEN
;
1161 flags
= BMAPI_WRITE
| BMAPI_IGNSTATE
;
1162 } else if (buffer_delay(bh
)) {
1164 flags
= BMAPI_ALLOCATE
| trylock
;
1167 flags
= BMAPI_WRITE
| BMAPI_MMAP
;
1172 * if we didn't have a valid mapping then we
1173 * need to ensure that we put the new mapping
1174 * in a new ioend structure. This needs to be
1175 * done to ensure that the ioends correctly
1176 * reflect the block mappings at io completion
1177 * for unwritten extent conversion.
1180 if (type
== IO_NEW
) {
1181 size
= xfs_probe_cluster(inode
,
1187 err
= xfs_map_blocks(inode
, offset
, size
,
1191 imap_valid
= xfs_imap_valid(inode
, &imap
,
1195 xfs_map_at_offset(inode
, bh
, &imap
, offset
);
1197 xfs_add_to_ioend(inode
, bh
, offset
,
1201 set_buffer_dirty(bh
);
1203 mark_buffer_dirty(bh
);
1208 } else if (buffer_uptodate(bh
) && startio
) {
1210 * we got here because the buffer is already mapped.
1211 * That means it must already have extents allocated
1212 * underneath it. Map the extent by reading it.
1214 if (!imap_valid
|| flags
!= BMAPI_READ
) {
1216 size
= xfs_probe_cluster(inode
, page
, bh
,
1218 err
= xfs_map_blocks(inode
, offset
, size
,
1222 imap_valid
= xfs_imap_valid(inode
, &imap
,
1227 * We set the type to IO_NEW in case we are doing a
1228 * small write at EOF that is extending the file but
1229 * without needing an allocation. We need to update the
1230 * file size on I/O completion in this case so it is
1231 * the same case as having just allocated a new extent
1232 * that we are writing into for the first time.
1235 if (trylock_buffer(bh
)) {
1236 ASSERT(buffer_mapped(bh
));
1239 xfs_add_to_ioend(inode
, bh
, offset
, type
,
1240 &ioend
, !imap_valid
);
1246 } else if ((buffer_uptodate(bh
) || PageUptodate(page
)) &&
1247 (unmapped
|| startio
)) {
1254 } while (offset
+= len
, ((bh
= bh
->b_this_page
) != head
));
1256 if (uptodate
&& bh
== head
)
1257 SetPageUptodate(page
);
1260 xfs_start_page_writeback(page
, 1, count
);
1262 if (ioend
&& imap_valid
) {
1263 xfs_off_t end_index
;
1265 end_index
= imap
.br_startoff
+ imap
.br_blockcount
;
1268 end_index
<<= inode
->i_blkbits
;
1271 end_index
= (end_index
- 1) >> PAGE_CACHE_SHIFT
;
1273 /* check against file size */
1274 if (end_index
> last_index
)
1275 end_index
= last_index
;
1277 xfs_cluster_write(inode
, page
->index
+ 1, &imap
, &ioend
,
1278 wbc
, startio
, all_bh
, end_index
);
1282 xfs_submit_ioend(wbc
, iohead
);
1288 xfs_cancel_ioend(iohead
);
1291 * If it's delalloc and we have nowhere to put it,
1292 * throw it away, unless the lower layers told
1295 if (err
!= -EAGAIN
) {
1297 xfs_aops_discard_page(page
);
1298 ClearPageUptodate(page
);
1304 * writepage: Called from one of two places:
1306 * 1. we are flushing a delalloc buffer head.
1308 * 2. we are writing out a dirty page. Typically the page dirty
1309 * state is cleared before we get here. In this case is it
1310 * conceivable we have no buffer heads.
1312 * For delalloc space on the page we need to allocate space and
1313 * flush it. For unmapped buffer heads on the page we should
1314 * allocate space if the page is uptodate. For any other dirty
1315 * buffer heads on the page we should flush them.
1317 * If we detect that a transaction would be required to flush
1318 * the page, we have to check the process flags first, if we
1319 * are already in a transaction or disk I/O during allocations
1320 * is off, we need to fail the writepage and redirty the page.
1326 struct writeback_control
*wbc
)
1330 int delalloc
, unmapped
, unwritten
;
1331 struct inode
*inode
= page
->mapping
->host
;
1333 trace_xfs_writepage(inode
, page
, 0);
1336 * Refuse to write the page out if we are called from reclaim context.
1338 * This is primarily to avoid stack overflows when called from deep
1339 * used stacks in random callers for direct reclaim, but disabling
1340 * reclaim for kswap is a nice side-effect as kswapd causes rather
1341 * suboptimal I/O patters, too.
1343 * This should really be done by the core VM, but until that happens
1344 * filesystems like XFS, btrfs and ext4 have to take care of this
1347 if (current
->flags
& PF_MEMALLOC
)
1351 * We need a transaction if:
1352 * 1. There are delalloc buffers on the page
1353 * 2. The page is uptodate and we have unmapped buffers
1354 * 3. The page is uptodate and we have no buffers
1355 * 4. There are unwritten buffers on the page
1358 if (!page_has_buffers(page
)) {
1362 xfs_count_page_state(page
, &delalloc
, &unmapped
, &unwritten
);
1363 if (!PageUptodate(page
))
1365 need_trans
= delalloc
+ unmapped
+ unwritten
;
1369 * If we need a transaction and the process flags say
1370 * we are already in a transaction, or no IO is allowed
1371 * then mark the page dirty again and leave the page
1374 if (current_test_flags(PF_FSTRANS
) && need_trans
)
1378 * Delay hooking up buffer heads until we have
1379 * made our go/no-go decision.
1381 if (!page_has_buffers(page
))
1382 create_empty_buffers(page
, 1 << inode
->i_blkbits
, 0);
1386 * VM calculation for nr_to_write seems off. Bump it way
1387 * up, this gets simple streaming writes zippy again.
1388 * To be reviewed again after Jens' writeback changes.
1390 wbc
->nr_to_write
*= 4;
1393 * Convert delayed allocate, unwritten or unmapped space
1394 * to real space and flush out to disk.
1396 error
= xfs_page_state_convert(inode
, page
, wbc
, 1, unmapped
);
1397 if (error
== -EAGAIN
)
1399 if (unlikely(error
< 0))
1405 redirty_page_for_writepage(wbc
, page
);
1415 struct address_space
*mapping
,
1416 struct writeback_control
*wbc
)
1418 xfs_iflags_clear(XFS_I(mapping
->host
), XFS_ITRUNCATED
);
1419 return generic_writepages(mapping
, wbc
);
1423 * Called to move a page into cleanable state - and from there
1424 * to be released. Possibly the page is already clean. We always
1425 * have buffer heads in this call.
1427 * Returns 0 if the page is ok to release, 1 otherwise.
1429 * Possible scenarios are:
1431 * 1. We are being called to release a page which has been written
1432 * to via regular I/O. buffer heads will be dirty and possibly
1433 * delalloc. If no delalloc buffer heads in this case then we
1434 * can just return zero.
1436 * 2. We are called to release a page which has been written via
1437 * mmap, all we need to do is ensure there is no delalloc
1438 * state in the buffer heads, if not we can let the caller
1439 * free them and we should come back later via writepage.
1446 struct inode
*inode
= page
->mapping
->host
;
1447 int dirty
, delalloc
, unmapped
, unwritten
;
1448 struct writeback_control wbc
= {
1449 .sync_mode
= WB_SYNC_ALL
,
1453 trace_xfs_releasepage(inode
, page
, 0);
1455 if (!page_has_buffers(page
))
1458 xfs_count_page_state(page
, &delalloc
, &unmapped
, &unwritten
);
1459 if (!delalloc
&& !unwritten
)
1462 if (!(gfp_mask
& __GFP_FS
))
1465 /* If we are already inside a transaction or the thread cannot
1466 * do I/O, we cannot release this page.
1468 if (current_test_flags(PF_FSTRANS
))
1472 * Convert delalloc space to real space, do not flush the
1473 * data out to disk, that will be done by the caller.
1474 * Never need to allocate space here - we will always
1475 * come back to writepage in that case.
1477 dirty
= xfs_page_state_convert(inode
, page
, &wbc
, 0, 0);
1478 if (dirty
== 0 && !unwritten
)
1483 return try_to_free_buffers(page
);
1488 struct inode
*inode
,
1490 struct buffer_head
*bh_result
,
1493 bmapi_flags_t flags
)
1495 struct xfs_bmbt_irec imap
;
1502 offset
= (xfs_off_t
)iblock
<< inode
->i_blkbits
;
1503 ASSERT(bh_result
->b_size
>= (1 << inode
->i_blkbits
));
1504 size
= bh_result
->b_size
;
1506 if (!create
&& direct
&& offset
>= i_size_read(inode
))
1509 error
= xfs_iomap(XFS_I(inode
), offset
, size
,
1510 create
? flags
: BMAPI_READ
, &imap
, &nimap
, &new);
1516 if (imap
.br_startblock
!= HOLESTARTBLOCK
&&
1517 imap
.br_startblock
!= DELAYSTARTBLOCK
) {
1519 * For unwritten extents do not report a disk address on
1520 * the read case (treat as if we're reading into a hole).
1522 if (create
|| !ISUNWRITTEN(&imap
))
1523 xfs_map_buffer(inode
, bh_result
, &imap
, offset
);
1524 if (create
&& ISUNWRITTEN(&imap
)) {
1526 bh_result
->b_private
= inode
;
1527 set_buffer_unwritten(bh_result
);
1532 * If this is a realtime file, data may be on a different device.
1533 * to that pointed to from the buffer_head b_bdev currently.
1535 bh_result
->b_bdev
= xfs_find_bdev_for_inode(inode
);
1538 * If we previously allocated a block out beyond eof and we are now
1539 * coming back to use it then we will need to flag it as new even if it
1540 * has a disk address.
1542 * With sub-block writes into unwritten extents we also need to mark
1543 * the buffer as new so that the unwritten parts of the buffer gets
1547 ((!buffer_mapped(bh_result
) && !buffer_uptodate(bh_result
)) ||
1548 (offset
>= i_size_read(inode
)) ||
1549 (new || ISUNWRITTEN(&imap
))))
1550 set_buffer_new(bh_result
);
1552 if (imap
.br_startblock
== DELAYSTARTBLOCK
) {
1555 set_buffer_uptodate(bh_result
);
1556 set_buffer_mapped(bh_result
);
1557 set_buffer_delay(bh_result
);
1562 * If this is O_DIRECT or the mpage code calling tell them how large
1563 * the mapping is, so that we can avoid repeated get_blocks calls.
1565 if (direct
|| size
> (1 << inode
->i_blkbits
)) {
1566 xfs_off_t mapping_size
;
1568 mapping_size
= imap
.br_startoff
+ imap
.br_blockcount
- iblock
;
1569 mapping_size
<<= inode
->i_blkbits
;
1571 ASSERT(mapping_size
> 0);
1572 if (mapping_size
> size
)
1573 mapping_size
= size
;
1574 if (mapping_size
> LONG_MAX
)
1575 mapping_size
= LONG_MAX
;
1577 bh_result
->b_size
= mapping_size
;
1585 struct inode
*inode
,
1587 struct buffer_head
*bh_result
,
1590 return __xfs_get_blocks(inode
, iblock
,
1591 bh_result
, create
, 0, BMAPI_WRITE
);
1595 xfs_get_blocks_direct(
1596 struct inode
*inode
,
1598 struct buffer_head
*bh_result
,
1601 return __xfs_get_blocks(inode
, iblock
,
1602 bh_result
, create
, 1, BMAPI_WRITE
|BMAPI_DIRECT
);
1612 xfs_ioend_t
*ioend
= iocb
->private;
1615 * Non-NULL private data means we need to issue a transaction to
1616 * convert a range from unwritten to written extents. This needs
1617 * to happen from process context but aio+dio I/O completion
1618 * happens from irq context so we need to defer it to a workqueue.
1619 * This is not necessary for synchronous direct I/O, but we do
1620 * it anyway to keep the code uniform and simpler.
1622 * Well, if only it were that simple. Because synchronous direct I/O
1623 * requires extent conversion to occur *before* we return to userspace,
1624 * we have to wait for extent conversion to complete. Look at the
1625 * iocb that has been passed to us to determine if this is AIO or
1626 * not. If it is synchronous, tell xfs_finish_ioend() to kick the
1627 * workqueue and wait for it to complete.
1629 * The core direct I/O code might be changed to always call the
1630 * completion handler in the future, in which case all this can
1633 ioend
->io_offset
= offset
;
1634 ioend
->io_size
= size
;
1635 if (ioend
->io_type
== IO_READ
) {
1636 xfs_finish_ioend(ioend
, 0);
1637 } else if (private && size
> 0) {
1638 xfs_finish_ioend(ioend
, is_sync_kiocb(iocb
));
1641 * A direct I/O write ioend starts it's life in unwritten
1642 * state in case they map an unwritten extent. This write
1643 * didn't map an unwritten extent so switch it's completion
1646 ioend
->io_type
= IO_NEW
;
1647 xfs_finish_ioend(ioend
, 0);
1651 * blockdev_direct_IO can return an error even after the I/O
1652 * completion handler was called. Thus we need to protect
1653 * against double-freeing.
1655 iocb
->private = NULL
;
1662 const struct iovec
*iov
,
1664 unsigned long nr_segs
)
1666 struct file
*file
= iocb
->ki_filp
;
1667 struct inode
*inode
= file
->f_mapping
->host
;
1668 struct block_device
*bdev
;
1671 bdev
= xfs_find_bdev_for_inode(inode
);
1673 iocb
->private = xfs_alloc_ioend(inode
, rw
== WRITE
?
1674 IO_UNWRITTEN
: IO_READ
);
1676 ret
= blockdev_direct_IO_no_locking(rw
, iocb
, inode
, bdev
, iov
,
1678 xfs_get_blocks_direct
,
1681 if (unlikely(ret
!= -EIOCBQUEUED
&& iocb
->private))
1682 xfs_destroy_ioend(iocb
->private);
1689 struct address_space
*mapping
,
1693 struct page
**pagep
,
1697 return block_write_begin(file
, mapping
, pos
, len
, flags
, pagep
, fsdata
,
1703 struct address_space
*mapping
,
1706 struct inode
*inode
= (struct inode
*)mapping
->host
;
1707 struct xfs_inode
*ip
= XFS_I(inode
);
1709 xfs_itrace_entry(XFS_I(inode
));
1710 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
1711 xfs_flush_pages(ip
, (xfs_off_t
)0, -1, 0, FI_REMAPF
);
1712 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
1713 return generic_block_bmap(mapping
, block
, xfs_get_blocks
);
1718 struct file
*unused
,
1721 return mpage_readpage(page
, xfs_get_blocks
);
1726 struct file
*unused
,
1727 struct address_space
*mapping
,
1728 struct list_head
*pages
,
1731 return mpage_readpages(mapping
, pages
, nr_pages
, xfs_get_blocks
);
1734 const struct address_space_operations xfs_address_space_operations
= {
1735 .readpage
= xfs_vm_readpage
,
1736 .readpages
= xfs_vm_readpages
,
1737 .writepage
= xfs_vm_writepage
,
1738 .writepages
= xfs_vm_writepages
,
1739 .sync_page
= block_sync_page
,
1740 .releasepage
= xfs_vm_releasepage
,
1741 .invalidatepage
= xfs_vm_invalidatepage
,
1742 .write_begin
= xfs_vm_write_begin
,
1743 .write_end
= generic_write_end
,
1744 .bmap
= xfs_vm_bmap
,
1745 .direct_IO
= xfs_vm_direct_IO
,
1746 .migratepage
= buffer_migrate_page
,
1747 .is_partially_uptodate
= block_is_partially_uptodate
,
1748 .error_remove_page
= generic_error_remove_page
,