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 <linux/mpage.h>
41 #include <linux/pagevec.h>
42 #include <linux/writeback.h>
51 struct buffer_head
*bh
, *head
;
53 *delalloc
= *unmapped
= *unwritten
= 0;
55 bh
= head
= page_buffers(page
);
57 if (buffer_uptodate(bh
) && !buffer_mapped(bh
))
59 else if (buffer_unwritten(bh
))
61 else if (buffer_delay(bh
))
63 } while ((bh
= bh
->b_this_page
) != head
);
66 #if defined(XFS_RW_TRACE)
75 bhv_vnode_t
*vp
= vn_from_inode(inode
);
76 loff_t isize
= i_size_read(inode
);
77 loff_t offset
= page_offset(page
);
78 int delalloc
= -1, unmapped
= -1, unwritten
= -1;
80 if (page_has_buffers(page
))
81 xfs_count_page_state(page
, &delalloc
, &unmapped
, &unwritten
);
87 ktrace_enter(ip
->i_rwtrace
,
88 (void *)((unsigned long)tag
),
93 (void *)((unsigned long)((ip
->i_d
.di_size
>> 32) & 0xffffffff)),
94 (void *)((unsigned long)(ip
->i_d
.di_size
& 0xffffffff)),
95 (void *)((unsigned long)((isize
>> 32) & 0xffffffff)),
96 (void *)((unsigned long)(isize
& 0xffffffff)),
97 (void *)((unsigned long)((offset
>> 32) & 0xffffffff)),
98 (void *)((unsigned long)(offset
& 0xffffffff)),
99 (void *)((unsigned long)delalloc
),
100 (void *)((unsigned long)unmapped
),
101 (void *)((unsigned long)unwritten
),
102 (void *)((unsigned long)current_pid()),
106 #define xfs_page_trace(tag, inode, page, pgoff)
110 * Schedule IO completion handling on a xfsdatad if this was
111 * the final hold on this ioend. If we are asked to wait,
112 * flush the workqueue.
119 if (atomic_dec_and_test(&ioend
->io_remaining
)) {
120 queue_work(xfsdatad_workqueue
, &ioend
->io_work
);
122 flush_workqueue(xfsdatad_workqueue
);
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
;
138 for (bh
= ioend
->io_buffer_head
; bh
; bh
= next
) {
139 next
= bh
->b_private
;
140 bh
->b_end_io(bh
, !ioend
->io_error
);
142 if (unlikely(ioend
->io_error
))
143 vn_ioerror(ioend
->io_vnode
, ioend
->io_error
, __FILE__
,__LINE__
);
144 vn_iowake(ioend
->io_vnode
);
145 mempool_free(ioend
, xfs_ioend_pool
);
149 * Update on-disk file size now that data has been written to disk.
150 * The current in-memory file size is i_size. If a write is beyond
151 * eof io_new_size will be the intended file size until i_size is
152 * updated. If this write does not extend all the way to the valid
153 * file size then restrict this update to the end of the write.
163 ip
= xfs_vtoi(ioend
->io_vnode
);
167 ASSERT((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
);
168 ASSERT(ioend
->io_type
!= IOMAP_READ
);
170 if (unlikely(ioend
->io_error
))
173 bsize
= ioend
->io_offset
+ ioend
->io_size
;
175 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
177 isize
= MAX(ip
->i_size
, ip
->i_iocore
.io_new_size
);
178 isize
= MIN(isize
, bsize
);
180 if (ip
->i_d
.di_size
< isize
) {
181 ip
->i_d
.di_size
= isize
;
182 ip
->i_update_core
= 1;
183 ip
->i_update_size
= 1;
184 mark_inode_dirty_sync(vn_to_inode(ioend
->io_vnode
));
187 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
191 * Buffered IO write completion for delayed allocate extents.
194 xfs_end_bio_delalloc(
195 struct work_struct
*work
)
198 container_of(work
, xfs_ioend_t
, io_work
);
200 xfs_setfilesize(ioend
);
201 xfs_destroy_ioend(ioend
);
205 * Buffered IO write completion for regular, written extents.
209 struct work_struct
*work
)
212 container_of(work
, xfs_ioend_t
, io_work
);
214 xfs_setfilesize(ioend
);
215 xfs_destroy_ioend(ioend
);
219 * IO write completion for unwritten extents.
221 * Issue transactions to convert a buffer range from unwritten
222 * to written extents.
225 xfs_end_bio_unwritten(
226 struct work_struct
*work
)
229 container_of(work
, xfs_ioend_t
, io_work
);
230 bhv_vnode_t
*vp
= ioend
->io_vnode
;
231 xfs_off_t offset
= ioend
->io_offset
;
232 size_t size
= ioend
->io_size
;
234 if (likely(!ioend
->io_error
)) {
235 bhv_vop_bmap(vp
, offset
, size
, BMAPI_UNWRITTEN
, NULL
, NULL
);
236 xfs_setfilesize(ioend
);
238 xfs_destroy_ioend(ioend
);
242 * IO read completion for regular, written extents.
246 struct work_struct
*work
)
249 container_of(work
, xfs_ioend_t
, io_work
);
251 xfs_destroy_ioend(ioend
);
255 * Allocate and initialise an IO completion structure.
256 * We need to track unwritten extent write completion here initially.
257 * We'll need to extend this for updating the ondisk inode size later
267 ioend
= mempool_alloc(xfs_ioend_pool
, GFP_NOFS
);
270 * Set the count to 1 initially, which will prevent an I/O
271 * completion callback from happening before we have started
272 * all the I/O from calling the completion routine too early.
274 atomic_set(&ioend
->io_remaining
, 1);
276 ioend
->io_list
= NULL
;
277 ioend
->io_type
= type
;
278 ioend
->io_vnode
= vn_from_inode(inode
);
279 ioend
->io_buffer_head
= NULL
;
280 ioend
->io_buffer_tail
= NULL
;
281 atomic_inc(&ioend
->io_vnode
->v_iocount
);
282 ioend
->io_offset
= 0;
285 if (type
== IOMAP_UNWRITTEN
)
286 INIT_WORK(&ioend
->io_work
, xfs_end_bio_unwritten
);
287 else if (type
== IOMAP_DELAY
)
288 INIT_WORK(&ioend
->io_work
, xfs_end_bio_delalloc
);
289 else if (type
== IOMAP_READ
)
290 INIT_WORK(&ioend
->io_work
, xfs_end_bio_read
);
292 INIT_WORK(&ioend
->io_work
, xfs_end_bio_written
);
305 bhv_vnode_t
*vp
= vn_from_inode(inode
);
306 int error
, nmaps
= 1;
308 error
= bhv_vop_bmap(vp
, offset
, count
, flags
, mapp
, &nmaps
);
309 if (!error
&& (flags
& (BMAPI_WRITE
|BMAPI_ALLOCATE
)))
319 return offset
>= iomapp
->iomap_offset
&&
320 offset
< iomapp
->iomap_offset
+ iomapp
->iomap_bsize
;
324 * BIO completion handler for buffered IO.
331 xfs_ioend_t
*ioend
= bio
->bi_private
;
333 ASSERT(atomic_read(&bio
->bi_cnt
) >= 1);
334 ioend
->io_error
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
) ? 0 : error
;
336 /* Toss bio and pass work off to an xfsdatad thread */
337 bio
->bi_private
= NULL
;
338 bio
->bi_end_io
= NULL
;
341 xfs_finish_ioend(ioend
, 0);
345 xfs_submit_ioend_bio(
349 atomic_inc(&ioend
->io_remaining
);
351 bio
->bi_private
= ioend
;
352 bio
->bi_end_io
= xfs_end_bio
;
354 submit_bio(WRITE
, bio
);
355 ASSERT(!bio_flagged(bio
, BIO_EOPNOTSUPP
));
361 struct buffer_head
*bh
)
364 int nvecs
= bio_get_nr_vecs(bh
->b_bdev
);
367 bio
= bio_alloc(GFP_NOIO
, nvecs
);
371 ASSERT(bio
->bi_private
== NULL
);
372 bio
->bi_sector
= bh
->b_blocknr
* (bh
->b_size
>> 9);
373 bio
->bi_bdev
= bh
->b_bdev
;
379 xfs_start_buffer_writeback(
380 struct buffer_head
*bh
)
382 ASSERT(buffer_mapped(bh
));
383 ASSERT(buffer_locked(bh
));
384 ASSERT(!buffer_delay(bh
));
385 ASSERT(!buffer_unwritten(bh
));
387 mark_buffer_async_write(bh
);
388 set_buffer_uptodate(bh
);
389 clear_buffer_dirty(bh
);
393 xfs_start_page_writeback(
395 struct writeback_control
*wbc
,
399 ASSERT(PageLocked(page
));
400 ASSERT(!PageWriteback(page
));
402 clear_page_dirty_for_io(page
);
403 set_page_writeback(page
);
406 end_page_writeback(page
);
407 wbc
->pages_skipped
++; /* We didn't write this page */
411 static inline int bio_add_buffer(struct bio
*bio
, struct buffer_head
*bh
)
413 return bio_add_page(bio
, bh
->b_page
, bh
->b_size
, bh_offset(bh
));
417 * Submit all of the bios for all of the ioends we have saved up, covering the
418 * initial writepage page and also any probed pages.
420 * Because we may have multiple ioends spanning a page, we need to start
421 * writeback on all the buffers before we submit them for I/O. If we mark the
422 * buffers as we got, then we can end up with a page that only has buffers
423 * marked async write and I/O complete on can occur before we mark the other
424 * buffers async write.
426 * The end result of this is that we trip a bug in end_page_writeback() because
427 * we call it twice for the one page as the code in end_buffer_async_write()
428 * assumes that all buffers on the page are started at the same time.
430 * The fix is two passes across the ioend list - one to start writeback on the
431 * buffer_heads, and then submit them for I/O on the second pass.
437 xfs_ioend_t
*head
= ioend
;
439 struct buffer_head
*bh
;
441 sector_t lastblock
= 0;
443 /* Pass 1 - start writeback */
445 next
= ioend
->io_list
;
446 for (bh
= ioend
->io_buffer_head
; bh
; bh
= bh
->b_private
) {
447 xfs_start_buffer_writeback(bh
);
449 } while ((ioend
= next
) != NULL
);
451 /* Pass 2 - submit I/O */
454 next
= ioend
->io_list
;
457 for (bh
= ioend
->io_buffer_head
; bh
; bh
= bh
->b_private
) {
461 bio
= xfs_alloc_ioend_bio(bh
);
462 } else if (bh
->b_blocknr
!= lastblock
+ 1) {
463 xfs_submit_ioend_bio(ioend
, bio
);
467 if (bio_add_buffer(bio
, bh
) != bh
->b_size
) {
468 xfs_submit_ioend_bio(ioend
, bio
);
472 lastblock
= bh
->b_blocknr
;
475 xfs_submit_ioend_bio(ioend
, bio
);
476 xfs_finish_ioend(ioend
, 0);
477 } while ((ioend
= next
) != NULL
);
481 * Cancel submission of all buffer_heads so far in this endio.
482 * Toss the endio too. Only ever called for the initial page
483 * in a writepage request, so only ever one page.
490 struct buffer_head
*bh
, *next_bh
;
493 next
= ioend
->io_list
;
494 bh
= ioend
->io_buffer_head
;
496 next_bh
= bh
->b_private
;
497 clear_buffer_async_write(bh
);
499 } while ((bh
= next_bh
) != NULL
);
501 vn_iowake(ioend
->io_vnode
);
502 mempool_free(ioend
, xfs_ioend_pool
);
503 } while ((ioend
= next
) != NULL
);
507 * Test to see if we've been building up a completion structure for
508 * earlier buffers -- if so, we try to append to this ioend if we
509 * can, otherwise we finish off any current ioend and start another.
510 * Return true if we've finished the given ioend.
515 struct buffer_head
*bh
,
518 xfs_ioend_t
**result
,
521 xfs_ioend_t
*ioend
= *result
;
523 if (!ioend
|| need_ioend
|| type
!= ioend
->io_type
) {
524 xfs_ioend_t
*previous
= *result
;
526 ioend
= xfs_alloc_ioend(inode
, type
);
527 ioend
->io_offset
= offset
;
528 ioend
->io_buffer_head
= bh
;
529 ioend
->io_buffer_tail
= bh
;
531 previous
->io_list
= ioend
;
534 ioend
->io_buffer_tail
->b_private
= bh
;
535 ioend
->io_buffer_tail
= bh
;
538 bh
->b_private
= NULL
;
539 ioend
->io_size
+= bh
->b_size
;
544 struct buffer_head
*bh
,
551 ASSERT(mp
->iomap_bn
!= IOMAP_DADDR_NULL
);
553 bn
= (mp
->iomap_bn
>> (block_bits
- BBSHIFT
)) +
554 ((offset
- mp
->iomap_offset
) >> block_bits
);
556 ASSERT(bn
|| (mp
->iomap_flags
& IOMAP_REALTIME
));
559 set_buffer_mapped(bh
);
564 struct buffer_head
*bh
,
569 ASSERT(!(iomapp
->iomap_flags
& IOMAP_HOLE
));
570 ASSERT(!(iomapp
->iomap_flags
& IOMAP_DELAY
));
573 xfs_map_buffer(bh
, iomapp
, offset
, block_bits
);
574 bh
->b_bdev
= iomapp
->iomap_target
->bt_bdev
;
575 set_buffer_mapped(bh
);
576 clear_buffer_delay(bh
);
577 clear_buffer_unwritten(bh
);
581 * Look for a page at index that is suitable for clustering.
586 unsigned int pg_offset
,
591 if (PageWriteback(page
))
594 if (page
->mapping
&& PageDirty(page
)) {
595 if (page_has_buffers(page
)) {
596 struct buffer_head
*bh
, *head
;
598 bh
= head
= page_buffers(page
);
600 if (!buffer_uptodate(bh
))
602 if (mapped
!= buffer_mapped(bh
))
605 if (ret
>= pg_offset
)
607 } while ((bh
= bh
->b_this_page
) != head
);
609 ret
= mapped
? 0 : PAGE_CACHE_SIZE
;
618 struct page
*startpage
,
619 struct buffer_head
*bh
,
620 struct buffer_head
*head
,
624 pgoff_t tindex
, tlast
, tloff
;
628 /* First sum forwards in this page */
630 if (!buffer_uptodate(bh
) || (mapped
!= buffer_mapped(bh
)))
633 } while ((bh
= bh
->b_this_page
) != head
);
635 /* if we reached the end of the page, sum forwards in following pages */
636 tlast
= i_size_read(inode
) >> PAGE_CACHE_SHIFT
;
637 tindex
= startpage
->index
+ 1;
639 /* Prune this back to avoid pathological behavior */
640 tloff
= min(tlast
, startpage
->index
+ 64);
642 pagevec_init(&pvec
, 0);
643 while (!done
&& tindex
<= tloff
) {
644 unsigned len
= min_t(pgoff_t
, PAGEVEC_SIZE
, tlast
- tindex
+ 1);
646 if (!pagevec_lookup(&pvec
, inode
->i_mapping
, tindex
, len
))
649 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
650 struct page
*page
= pvec
.pages
[i
];
651 size_t pg_offset
, pg_len
= 0;
653 if (tindex
== tlast
) {
655 i_size_read(inode
) & (PAGE_CACHE_SIZE
- 1);
661 pg_offset
= PAGE_CACHE_SIZE
;
663 if (page
->index
== tindex
&& !TestSetPageLocked(page
)) {
664 pg_len
= xfs_probe_page(page
, pg_offset
, mapped
);
677 pagevec_release(&pvec
);
685 * Test if a given page is suitable for writing as part of an unwritten
686 * or delayed allocate extent.
693 if (PageWriteback(page
))
696 if (page
->mapping
&& page_has_buffers(page
)) {
697 struct buffer_head
*bh
, *head
;
700 bh
= head
= page_buffers(page
);
702 if (buffer_unwritten(bh
))
703 acceptable
= (type
== IOMAP_UNWRITTEN
);
704 else if (buffer_delay(bh
))
705 acceptable
= (type
== IOMAP_DELAY
);
706 else if (buffer_dirty(bh
) && buffer_mapped(bh
))
707 acceptable
= (type
== IOMAP_NEW
);
710 } while ((bh
= bh
->b_this_page
) != head
);
720 * Allocate & map buffers for page given the extent map. Write it out.
721 * except for the original page of a writepage, this is called on
722 * delalloc/unwritten pages only, for the original page it is possible
723 * that the page has no mapping at all.
731 xfs_ioend_t
**ioendp
,
732 struct writeback_control
*wbc
,
736 struct buffer_head
*bh
, *head
;
737 xfs_off_t end_offset
;
738 unsigned long p_offset
;
740 int bbits
= inode
->i_blkbits
;
742 int count
= 0, done
= 0, uptodate
= 1;
743 xfs_off_t offset
= page_offset(page
);
745 if (page
->index
!= tindex
)
747 if (TestSetPageLocked(page
))
749 if (PageWriteback(page
))
750 goto fail_unlock_page
;
751 if (page
->mapping
!= inode
->i_mapping
)
752 goto fail_unlock_page
;
753 if (!xfs_is_delayed_page(page
, (*ioendp
)->io_type
))
754 goto fail_unlock_page
;
757 * page_dirty is initially a count of buffers on the page before
758 * EOF and is decremented as we move each into a cleanable state.
762 * End offset is the highest offset that this page should represent.
763 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
764 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
765 * hence give us the correct page_dirty count. On any other page,
766 * it will be zero and in that case we need page_dirty to be the
767 * count of buffers on the page.
769 end_offset
= min_t(unsigned long long,
770 (xfs_off_t
)(page
->index
+ 1) << PAGE_CACHE_SHIFT
,
773 len
= 1 << inode
->i_blkbits
;
774 p_offset
= min_t(unsigned long, end_offset
& (PAGE_CACHE_SIZE
- 1),
776 p_offset
= p_offset
? roundup(p_offset
, len
) : PAGE_CACHE_SIZE
;
777 page_dirty
= p_offset
/ len
;
779 bh
= head
= page_buffers(page
);
781 if (offset
>= end_offset
)
783 if (!buffer_uptodate(bh
))
785 if (!(PageUptodate(page
) || buffer_uptodate(bh
))) {
790 if (buffer_unwritten(bh
) || buffer_delay(bh
)) {
791 if (buffer_unwritten(bh
))
792 type
= IOMAP_UNWRITTEN
;
796 if (!xfs_iomap_valid(mp
, offset
)) {
801 ASSERT(!(mp
->iomap_flags
& IOMAP_HOLE
));
802 ASSERT(!(mp
->iomap_flags
& IOMAP_DELAY
));
804 xfs_map_at_offset(bh
, offset
, bbits
, mp
);
806 xfs_add_to_ioend(inode
, bh
, offset
,
809 set_buffer_dirty(bh
);
811 mark_buffer_dirty(bh
);
817 if (buffer_mapped(bh
) && all_bh
&& startio
) {
819 xfs_add_to_ioend(inode
, bh
, offset
,
827 } while (offset
+= len
, (bh
= bh
->b_this_page
) != head
);
829 if (uptodate
&& bh
== head
)
830 SetPageUptodate(page
);
834 struct backing_dev_info
*bdi
;
836 bdi
= inode
->i_mapping
->backing_dev_info
;
838 if (bdi_write_congested(bdi
)) {
839 wbc
->encountered_congestion
= 1;
841 } else if (wbc
->nr_to_write
<= 0) {
845 xfs_start_page_writeback(page
, wbc
, !page_dirty
, count
);
856 * Convert & write out a cluster of pages in the same extent as defined
857 * by mp and following the start page.
864 xfs_ioend_t
**ioendp
,
865 struct writeback_control
*wbc
,
873 pagevec_init(&pvec
, 0);
874 while (!done
&& tindex
<= tlast
) {
875 unsigned len
= min_t(pgoff_t
, PAGEVEC_SIZE
, tlast
- tindex
+ 1);
877 if (!pagevec_lookup(&pvec
, inode
->i_mapping
, tindex
, len
))
880 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
881 done
= xfs_convert_page(inode
, pvec
.pages
[i
], tindex
++,
882 iomapp
, ioendp
, wbc
, startio
, all_bh
);
887 pagevec_release(&pvec
);
893 * Calling this without startio set means we are being asked to make a dirty
894 * page ready for freeing it's buffers. When called with startio set then
895 * we are coming from writepage.
897 * When called with startio set it is important that we write the WHOLE
899 * The bh->b_state's cannot know if any of the blocks or which block for
900 * that matter are dirty due to mmap writes, and therefore bh uptodate is
901 * only valid if the page itself isn't completely uptodate. Some layers
902 * may clear the page dirty flag prior to calling write page, under the
903 * assumption the entire page will be written out; by not writing out the
904 * whole page the page can be reused before all valid dirty data is
905 * written out. Note: in the case of a page that has been dirty'd by
906 * mapwrite and but partially setup by block_prepare_write the
907 * bh->b_states's will not agree and only ones setup by BPW/BCW will have
908 * valid state, thus the whole page must be written out thing.
912 xfs_page_state_convert(
915 struct writeback_control
*wbc
,
917 int unmapped
) /* also implies page uptodate */
919 struct buffer_head
*bh
, *head
;
921 xfs_ioend_t
*ioend
= NULL
, *iohead
= NULL
;
923 unsigned long p_offset
= 0;
925 __uint64_t end_offset
;
926 pgoff_t end_index
, last_index
, tlast
;
928 int flags
, err
, iomap_valid
= 0, uptodate
= 1;
929 int page_dirty
, count
= 0;
931 int all_bh
= unmapped
;
934 if (wbc
->sync_mode
== WB_SYNC_NONE
&& wbc
->nonblocking
)
935 trylock
|= BMAPI_TRYLOCK
;
938 /* Is this page beyond the end of the file? */
939 offset
= i_size_read(inode
);
940 end_index
= offset
>> PAGE_CACHE_SHIFT
;
941 last_index
= (offset
- 1) >> PAGE_CACHE_SHIFT
;
942 if (page
->index
>= end_index
) {
943 if ((page
->index
>= end_index
+ 1) ||
944 !(i_size_read(inode
) & (PAGE_CACHE_SIZE
- 1))) {
952 * page_dirty is initially a count of buffers on the page before
953 * EOF and is decremented as we move each into a cleanable state.
957 * End offset is the highest offset that this page should represent.
958 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
959 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
960 * hence give us the correct page_dirty count. On any other page,
961 * it will be zero and in that case we need page_dirty to be the
962 * count of buffers on the page.
964 end_offset
= min_t(unsigned long long,
965 (xfs_off_t
)(page
->index
+ 1) << PAGE_CACHE_SHIFT
, offset
);
966 len
= 1 << inode
->i_blkbits
;
967 p_offset
= min_t(unsigned long, end_offset
& (PAGE_CACHE_SIZE
- 1),
969 p_offset
= p_offset
? roundup(p_offset
, len
) : PAGE_CACHE_SIZE
;
970 page_dirty
= p_offset
/ len
;
972 bh
= head
= page_buffers(page
);
973 offset
= page_offset(page
);
977 /* TODO: cleanup count and page_dirty */
980 if (offset
>= end_offset
)
982 if (!buffer_uptodate(bh
))
984 if (!(PageUptodate(page
) || buffer_uptodate(bh
)) && !startio
) {
986 * the iomap is actually still valid, but the ioend
987 * isn't. shouldn't happen too often.
994 iomap_valid
= xfs_iomap_valid(&iomap
, offset
);
997 * First case, map an unwritten extent and prepare for
998 * extent state conversion transaction on completion.
1000 * Second case, allocate space for a delalloc buffer.
1001 * We can return EAGAIN here in the release page case.
1003 * Third case, an unmapped buffer was found, and we are
1004 * in a path where we need to write the whole page out.
1006 if (buffer_unwritten(bh
) || buffer_delay(bh
) ||
1007 ((buffer_uptodate(bh
) || PageUptodate(page
)) &&
1008 !buffer_mapped(bh
) && (unmapped
|| startio
))) {
1012 * Make sure we don't use a read-only iomap
1014 if (flags
== BMAPI_READ
)
1017 if (buffer_unwritten(bh
)) {
1018 type
= IOMAP_UNWRITTEN
;
1019 flags
= BMAPI_WRITE
| BMAPI_IGNSTATE
;
1020 } else if (buffer_delay(bh
)) {
1022 flags
= BMAPI_ALLOCATE
| trylock
;
1025 flags
= BMAPI_WRITE
| BMAPI_MMAP
;
1030 * if we didn't have a valid mapping then we
1031 * need to ensure that we put the new mapping
1032 * in a new ioend structure. This needs to be
1033 * done to ensure that the ioends correctly
1034 * reflect the block mappings at io completion
1035 * for unwritten extent conversion.
1038 if (type
== IOMAP_NEW
) {
1039 size
= xfs_probe_cluster(inode
,
1045 err
= xfs_map_blocks(inode
, offset
, size
,
1049 iomap_valid
= xfs_iomap_valid(&iomap
, offset
);
1052 xfs_map_at_offset(bh
, offset
,
1053 inode
->i_blkbits
, &iomap
);
1055 xfs_add_to_ioend(inode
, bh
, offset
,
1059 set_buffer_dirty(bh
);
1061 mark_buffer_dirty(bh
);
1066 } else if (buffer_uptodate(bh
) && startio
) {
1068 * we got here because the buffer is already mapped.
1069 * That means it must already have extents allocated
1070 * underneath it. Map the extent by reading it.
1072 if (!iomap_valid
|| flags
!= BMAPI_READ
) {
1074 size
= xfs_probe_cluster(inode
, page
, bh
,
1076 err
= xfs_map_blocks(inode
, offset
, size
,
1080 iomap_valid
= xfs_iomap_valid(&iomap
, offset
);
1084 * We set the type to IOMAP_NEW in case we are doing a
1085 * small write at EOF that is extending the file but
1086 * without needing an allocation. We need to update the
1087 * file size on I/O completion in this case so it is
1088 * the same case as having just allocated a new extent
1089 * that we are writing into for the first time.
1092 if (!test_and_set_bit(BH_Lock
, &bh
->b_state
)) {
1093 ASSERT(buffer_mapped(bh
));
1096 xfs_add_to_ioend(inode
, bh
, offset
, type
,
1097 &ioend
, !iomap_valid
);
1103 } else if ((buffer_uptodate(bh
) || PageUptodate(page
)) &&
1104 (unmapped
|| startio
)) {
1111 } while (offset
+= len
, ((bh
= bh
->b_this_page
) != head
));
1113 if (uptodate
&& bh
== head
)
1114 SetPageUptodate(page
);
1117 xfs_start_page_writeback(page
, wbc
, 1, count
);
1119 if (ioend
&& iomap_valid
) {
1120 offset
= (iomap
.iomap_offset
+ iomap
.iomap_bsize
- 1) >>
1122 tlast
= min_t(pgoff_t
, offset
, last_index
);
1123 xfs_cluster_write(inode
, page
->index
+ 1, &iomap
, &ioend
,
1124 wbc
, startio
, all_bh
, tlast
);
1128 xfs_submit_ioend(iohead
);
1134 xfs_cancel_ioend(iohead
);
1137 * If it's delalloc and we have nowhere to put it,
1138 * throw it away, unless the lower layers told
1141 if (err
!= -EAGAIN
) {
1143 block_invalidatepage(page
, 0);
1144 ClearPageUptodate(page
);
1150 * writepage: Called from one of two places:
1152 * 1. we are flushing a delalloc buffer head.
1154 * 2. we are writing out a dirty page. Typically the page dirty
1155 * state is cleared before we get here. In this case is it
1156 * conceivable we have no buffer heads.
1158 * For delalloc space on the page we need to allocate space and
1159 * flush it. For unmapped buffer heads on the page we should
1160 * allocate space if the page is uptodate. For any other dirty
1161 * buffer heads on the page we should flush them.
1163 * If we detect that a transaction would be required to flush
1164 * the page, we have to check the process flags first, if we
1165 * are already in a transaction or disk I/O during allocations
1166 * is off, we need to fail the writepage and redirty the page.
1172 struct writeback_control
*wbc
)
1176 int delalloc
, unmapped
, unwritten
;
1177 struct inode
*inode
= page
->mapping
->host
;
1179 xfs_page_trace(XFS_WRITEPAGE_ENTER
, inode
, page
, 0);
1182 * We need a transaction if:
1183 * 1. There are delalloc buffers on the page
1184 * 2. The page is uptodate and we have unmapped buffers
1185 * 3. The page is uptodate and we have no buffers
1186 * 4. There are unwritten buffers on the page
1189 if (!page_has_buffers(page
)) {
1193 xfs_count_page_state(page
, &delalloc
, &unmapped
, &unwritten
);
1194 if (!PageUptodate(page
))
1196 need_trans
= delalloc
+ unmapped
+ unwritten
;
1200 * If we need a transaction and the process flags say
1201 * we are already in a transaction, or no IO is allowed
1202 * then mark the page dirty again and leave the page
1205 if (current_test_flags(PF_FSTRANS
) && need_trans
)
1209 * Delay hooking up buffer heads until we have
1210 * made our go/no-go decision.
1212 if (!page_has_buffers(page
))
1213 create_empty_buffers(page
, 1 << inode
->i_blkbits
, 0);
1216 * Convert delayed allocate, unwritten or unmapped space
1217 * to real space and flush out to disk.
1219 error
= xfs_page_state_convert(inode
, page
, wbc
, 1, unmapped
);
1220 if (error
== -EAGAIN
)
1222 if (unlikely(error
< 0))
1228 redirty_page_for_writepage(wbc
, page
);
1238 struct address_space
*mapping
,
1239 struct writeback_control
*wbc
)
1241 struct bhv_vnode
*vp
= vn_from_inode(mapping
->host
);
1245 return generic_writepages(mapping
, wbc
);
1249 * Called to move a page into cleanable state - and from there
1250 * to be released. Possibly the page is already clean. We always
1251 * have buffer heads in this call.
1253 * Returns 0 if the page is ok to release, 1 otherwise.
1255 * Possible scenarios are:
1257 * 1. We are being called to release a page which has been written
1258 * to via regular I/O. buffer heads will be dirty and possibly
1259 * delalloc. If no delalloc buffer heads in this case then we
1260 * can just return zero.
1262 * 2. We are called to release a page which has been written via
1263 * mmap, all we need to do is ensure there is no delalloc
1264 * state in the buffer heads, if not we can let the caller
1265 * free them and we should come back later via writepage.
1272 struct inode
*inode
= page
->mapping
->host
;
1273 int dirty
, delalloc
, unmapped
, unwritten
;
1274 struct writeback_control wbc
= {
1275 .sync_mode
= WB_SYNC_ALL
,
1279 xfs_page_trace(XFS_RELEASEPAGE_ENTER
, inode
, page
, 0);
1281 if (!page_has_buffers(page
))
1284 xfs_count_page_state(page
, &delalloc
, &unmapped
, &unwritten
);
1285 if (!delalloc
&& !unwritten
)
1288 if (!(gfp_mask
& __GFP_FS
))
1291 /* If we are already inside a transaction or the thread cannot
1292 * do I/O, we cannot release this page.
1294 if (current_test_flags(PF_FSTRANS
))
1298 * Convert delalloc space to real space, do not flush the
1299 * data out to disk, that will be done by the caller.
1300 * Never need to allocate space here - we will always
1301 * come back to writepage in that case.
1303 dirty
= xfs_page_state_convert(inode
, page
, &wbc
, 0, 0);
1304 if (dirty
== 0 && !unwritten
)
1309 return try_to_free_buffers(page
);
1314 struct inode
*inode
,
1316 struct buffer_head
*bh_result
,
1319 bmapi_flags_t flags
)
1321 bhv_vnode_t
*vp
= vn_from_inode(inode
);
1328 offset
= (xfs_off_t
)iblock
<< inode
->i_blkbits
;
1329 ASSERT(bh_result
->b_size
>= (1 << inode
->i_blkbits
));
1330 size
= bh_result
->b_size
;
1331 error
= bhv_vop_bmap(vp
, offset
, size
,
1332 create
? flags
: BMAPI_READ
, &iomap
, &niomap
);
1338 if (iomap
.iomap_bn
!= IOMAP_DADDR_NULL
) {
1340 * For unwritten extents do not report a disk address on
1341 * the read case (treat as if we're reading into a hole).
1343 if (create
|| !(iomap
.iomap_flags
& IOMAP_UNWRITTEN
)) {
1344 xfs_map_buffer(bh_result
, &iomap
, offset
,
1347 if (create
&& (iomap
.iomap_flags
& IOMAP_UNWRITTEN
)) {
1349 bh_result
->b_private
= inode
;
1350 set_buffer_unwritten(bh_result
);
1355 * If this is a realtime file, data may be on a different device.
1356 * to that pointed to from the buffer_head b_bdev currently.
1358 bh_result
->b_bdev
= iomap
.iomap_target
->bt_bdev
;
1361 * If we previously allocated a block out beyond eof and we are now
1362 * coming back to use it then we will need to flag it as new even if it
1363 * has a disk address.
1365 * With sub-block writes into unwritten extents we also need to mark
1366 * the buffer as new so that the unwritten parts of the buffer gets
1370 ((!buffer_mapped(bh_result
) && !buffer_uptodate(bh_result
)) ||
1371 (offset
>= i_size_read(inode
)) ||
1372 (iomap
.iomap_flags
& (IOMAP_NEW
|IOMAP_UNWRITTEN
))))
1373 set_buffer_new(bh_result
);
1375 if (iomap
.iomap_flags
& IOMAP_DELAY
) {
1378 set_buffer_uptodate(bh_result
);
1379 set_buffer_mapped(bh_result
);
1380 set_buffer_delay(bh_result
);
1384 if (direct
|| size
> (1 << inode
->i_blkbits
)) {
1385 ASSERT(iomap
.iomap_bsize
- iomap
.iomap_delta
> 0);
1386 offset
= min_t(xfs_off_t
,
1387 iomap
.iomap_bsize
- iomap
.iomap_delta
, size
);
1388 bh_result
->b_size
= (ssize_t
)min_t(xfs_off_t
, LONG_MAX
, offset
);
1396 struct inode
*inode
,
1398 struct buffer_head
*bh_result
,
1401 return __xfs_get_blocks(inode
, iblock
,
1402 bh_result
, create
, 0, BMAPI_WRITE
);
1406 xfs_get_blocks_direct(
1407 struct inode
*inode
,
1409 struct buffer_head
*bh_result
,
1412 return __xfs_get_blocks(inode
, iblock
,
1413 bh_result
, create
, 1, BMAPI_WRITE
|BMAPI_DIRECT
);
1423 xfs_ioend_t
*ioend
= iocb
->private;
1426 * Non-NULL private data means we need to issue a transaction to
1427 * convert a range from unwritten to written extents. This needs
1428 * to happen from process context but aio+dio I/O completion
1429 * happens from irq context so we need to defer it to a workqueue.
1430 * This is not necessary for synchronous direct I/O, but we do
1431 * it anyway to keep the code uniform and simpler.
1433 * Well, if only it were that simple. Because synchronous direct I/O
1434 * requires extent conversion to occur *before* we return to userspace,
1435 * we have to wait for extent conversion to complete. Look at the
1436 * iocb that has been passed to us to determine if this is AIO or
1437 * not. If it is synchronous, tell xfs_finish_ioend() to kick the
1438 * workqueue and wait for it to complete.
1440 * The core direct I/O code might be changed to always call the
1441 * completion handler in the future, in which case all this can
1444 ioend
->io_offset
= offset
;
1445 ioend
->io_size
= size
;
1446 if (ioend
->io_type
== IOMAP_READ
) {
1447 xfs_finish_ioend(ioend
, 0);
1448 } else if (private && size
> 0) {
1449 xfs_finish_ioend(ioend
, is_sync_kiocb(iocb
));
1452 * A direct I/O write ioend starts it's life in unwritten
1453 * state in case they map an unwritten extent. This write
1454 * didn't map an unwritten extent so switch it's completion
1457 INIT_WORK(&ioend
->io_work
, xfs_end_bio_written
);
1458 xfs_finish_ioend(ioend
, 0);
1462 * blockdev_direct_IO can return an error even after the I/O
1463 * completion handler was called. Thus we need to protect
1464 * against double-freeing.
1466 iocb
->private = NULL
;
1473 const struct iovec
*iov
,
1475 unsigned long nr_segs
)
1477 struct file
*file
= iocb
->ki_filp
;
1478 struct inode
*inode
= file
->f_mapping
->host
;
1479 bhv_vnode_t
*vp
= vn_from_inode(inode
);
1485 error
= bhv_vop_bmap(vp
, offset
, 0, BMAPI_DEVICE
, &iomap
, &maps
);
1490 iocb
->private = xfs_alloc_ioend(inode
, IOMAP_UNWRITTEN
);
1491 ret
= blockdev_direct_IO_own_locking(rw
, iocb
, inode
,
1492 iomap
.iomap_target
->bt_bdev
,
1493 iov
, offset
, nr_segs
,
1494 xfs_get_blocks_direct
,
1497 iocb
->private = xfs_alloc_ioend(inode
, IOMAP_READ
);
1498 ret
= blockdev_direct_IO_no_locking(rw
, iocb
, inode
,
1499 iomap
.iomap_target
->bt_bdev
,
1500 iov
, offset
, nr_segs
,
1501 xfs_get_blocks_direct
,
1505 if (unlikely(ret
!= -EIOCBQUEUED
&& iocb
->private))
1506 xfs_destroy_ioend(iocb
->private);
1513 struct address_space
*mapping
,
1517 struct page
**pagep
,
1521 return block_write_begin(file
, mapping
, pos
, len
, flags
, pagep
, fsdata
,
1527 struct address_space
*mapping
,
1530 struct inode
*inode
= (struct inode
*)mapping
->host
;
1531 bhv_vnode_t
*vp
= vn_from_inode(inode
);
1533 vn_trace_entry(vp
, __FUNCTION__
, (inst_t
*)__return_address
);
1534 bhv_vop_rwlock(vp
, VRWLOCK_READ
);
1535 bhv_vop_flush_pages(vp
, (xfs_off_t
)0, -1, 0, FI_REMAPF
);
1536 bhv_vop_rwunlock(vp
, VRWLOCK_READ
);
1537 return generic_block_bmap(mapping
, block
, xfs_get_blocks
);
1542 struct file
*unused
,
1545 return mpage_readpage(page
, xfs_get_blocks
);
1550 struct file
*unused
,
1551 struct address_space
*mapping
,
1552 struct list_head
*pages
,
1555 return mpage_readpages(mapping
, pages
, nr_pages
, xfs_get_blocks
);
1559 xfs_vm_invalidatepage(
1561 unsigned long offset
)
1563 xfs_page_trace(XFS_INVALIDPAGE_ENTER
,
1564 page
->mapping
->host
, page
, offset
);
1565 block_invalidatepage(page
, offset
);
1568 const struct address_space_operations xfs_address_space_operations
= {
1569 .readpage
= xfs_vm_readpage
,
1570 .readpages
= xfs_vm_readpages
,
1571 .writepage
= xfs_vm_writepage
,
1572 .writepages
= xfs_vm_writepages
,
1573 .sync_page
= block_sync_page
,
1574 .releasepage
= xfs_vm_releasepage
,
1575 .invalidatepage
= xfs_vm_invalidatepage
,
1576 .write_begin
= xfs_vm_write_begin
,
1577 .write_end
= generic_write_end
,
1578 .bmap
= xfs_vm_bmap
,
1579 .direct_IO
= xfs_vm_direct_IO
,
1580 .migratepage
= buffer_migrate_page
,