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
24 #include "xfs_trans.h"
25 #include "xfs_mount.h"
26 #include "xfs_bmap_btree.h"
27 #include "xfs_dinode.h"
28 #include "xfs_inode.h"
29 #include "xfs_alloc.h"
30 #include "xfs_error.h"
32 #include "xfs_iomap.h"
33 #include "xfs_vnodeops.h"
34 #include "xfs_trace.h"
36 #include <linux/gfp.h>
37 #include <linux/mpage.h>
38 #include <linux/pagevec.h>
39 #include <linux/writeback.h>
47 struct buffer_head
*bh
, *head
;
49 *delalloc
= *unwritten
= 0;
51 bh
= head
= page_buffers(page
);
53 if (buffer_unwritten(bh
))
55 else if (buffer_delay(bh
))
57 } while ((bh
= bh
->b_this_page
) != head
);
60 STATIC
struct block_device
*
61 xfs_find_bdev_for_inode(
64 struct xfs_inode
*ip
= XFS_I(inode
);
65 struct xfs_mount
*mp
= ip
->i_mount
;
67 if (XFS_IS_REALTIME_INODE(ip
))
68 return mp
->m_rtdev_targp
->bt_bdev
;
70 return mp
->m_ddev_targp
->bt_bdev
;
74 * We're now finished for good with this ioend structure.
75 * Update the page state via the associated buffer_heads,
76 * release holds on the inode and bio, and finally free
77 * up memory. Do not use the ioend after this.
83 struct buffer_head
*bh
, *next
;
85 for (bh
= ioend
->io_buffer_head
; bh
; bh
= next
) {
87 bh
->b_end_io(bh
, !ioend
->io_error
);
91 if (ioend
->io_isasync
) {
92 aio_complete(ioend
->io_iocb
, ioend
->io_error
?
93 ioend
->io_error
: ioend
->io_result
, 0);
95 inode_dio_done(ioend
->io_inode
);
98 mempool_free(ioend
, xfs_ioend_pool
);
102 * If the end of the current ioend is beyond the current EOF,
103 * return the new EOF value, otherwise zero.
109 xfs_inode_t
*ip
= XFS_I(ioend
->io_inode
);
113 bsize
= ioend
->io_offset
+ ioend
->io_size
;
114 isize
= MIN(i_size_read(VFS_I(ip
)), bsize
);
115 return isize
> ip
->i_d
.di_size
? isize
: 0;
119 * Fast and loose check if this write could update the on-disk inode size.
121 static inline bool xfs_ioend_is_append(struct xfs_ioend
*ioend
)
123 return ioend
->io_offset
+ ioend
->io_size
>
124 XFS_I(ioend
->io_inode
)->i_d
.di_size
;
128 * Update on-disk file size now that data has been written to disk.
130 * This function does not block as blocking on the inode lock in IO completion
131 * can lead to IO completion order dependency deadlocks.. If it can't get the
132 * inode ilock it will return EAGAIN. Callers must handle this.
138 xfs_inode_t
*ip
= XFS_I(ioend
->io_inode
);
141 if (!xfs_ilock_nowait(ip
, XFS_ILOCK_EXCL
))
144 isize
= xfs_ioend_new_eof(ioend
);
146 trace_xfs_setfilesize(ip
, ioend
->io_offset
, ioend
->io_size
);
147 ip
->i_d
.di_size
= isize
;
148 xfs_mark_inode_dirty(ip
);
151 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
156 * Schedule IO completion handling on the final put of an ioend.
158 * If there is no work to do we might as well call it a day and free the
163 struct xfs_ioend
*ioend
)
165 if (atomic_dec_and_test(&ioend
->io_remaining
)) {
166 if (ioend
->io_type
== IO_UNWRITTEN
)
167 queue_work(xfsconvertd_workqueue
, &ioend
->io_work
);
168 else if (xfs_ioend_is_append(ioend
))
169 queue_work(xfsdatad_workqueue
, &ioend
->io_work
);
171 xfs_destroy_ioend(ioend
);
176 * IO write completion.
180 struct work_struct
*work
)
182 xfs_ioend_t
*ioend
= container_of(work
, xfs_ioend_t
, io_work
);
183 struct xfs_inode
*ip
= XFS_I(ioend
->io_inode
);
186 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
)) {
187 ioend
->io_error
= -EIO
;
194 * For unwritten extents we need to issue transactions to convert a
195 * range to normal written extens after the data I/O has finished.
197 if (ioend
->io_type
== IO_UNWRITTEN
) {
198 error
= xfs_iomap_write_unwritten(ip
, ioend
->io_offset
,
201 ioend
->io_error
= -error
;
207 * We might have to update the on-disk file size after extending
210 error
= xfs_setfilesize(ioend
);
211 ASSERT(!error
|| error
== EAGAIN
);
215 * If we didn't complete processing of the ioend, requeue it to the
216 * tail of the workqueue for another attempt later. Otherwise destroy
219 if (error
== EAGAIN
) {
220 atomic_inc(&ioend
->io_remaining
);
221 xfs_finish_ioend(ioend
);
222 /* ensure we don't spin on blocked ioends */
225 xfs_destroy_ioend(ioend
);
230 * Call IO completion handling in caller context on the final put of an ioend.
233 xfs_finish_ioend_sync(
234 struct xfs_ioend
*ioend
)
236 if (atomic_dec_and_test(&ioend
->io_remaining
))
237 xfs_end_io(&ioend
->io_work
);
241 * Allocate and initialise an IO completion structure.
242 * We need to track unwritten extent write completion here initially.
243 * We'll need to extend this for updating the ondisk inode size later
253 ioend
= mempool_alloc(xfs_ioend_pool
, GFP_NOFS
);
256 * Set the count to 1 initially, which will prevent an I/O
257 * completion callback from happening before we have started
258 * all the I/O from calling the completion routine too early.
260 atomic_set(&ioend
->io_remaining
, 1);
261 ioend
->io_isasync
= 0;
263 ioend
->io_list
= NULL
;
264 ioend
->io_type
= type
;
265 ioend
->io_inode
= inode
;
266 ioend
->io_buffer_head
= NULL
;
267 ioend
->io_buffer_tail
= NULL
;
268 ioend
->io_offset
= 0;
270 ioend
->io_iocb
= NULL
;
271 ioend
->io_result
= 0;
273 INIT_WORK(&ioend
->io_work
, xfs_end_io
);
281 struct xfs_bmbt_irec
*imap
,
285 struct xfs_inode
*ip
= XFS_I(inode
);
286 struct xfs_mount
*mp
= ip
->i_mount
;
287 ssize_t count
= 1 << inode
->i_blkbits
;
288 xfs_fileoff_t offset_fsb
, end_fsb
;
290 int bmapi_flags
= XFS_BMAPI_ENTIRE
;
293 if (XFS_FORCED_SHUTDOWN(mp
))
294 return -XFS_ERROR(EIO
);
296 if (type
== IO_UNWRITTEN
)
297 bmapi_flags
|= XFS_BMAPI_IGSTATE
;
299 if (!xfs_ilock_nowait(ip
, XFS_ILOCK_SHARED
)) {
301 return -XFS_ERROR(EAGAIN
);
302 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
305 ASSERT(ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
||
306 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
));
307 ASSERT(offset
<= mp
->m_maxioffset
);
309 if (offset
+ count
> mp
->m_maxioffset
)
310 count
= mp
->m_maxioffset
- offset
;
311 end_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)offset
+ count
);
312 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
313 error
= xfs_bmapi_read(ip
, offset_fsb
, end_fsb
- offset_fsb
,
314 imap
, &nimaps
, bmapi_flags
);
315 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
318 return -XFS_ERROR(error
);
320 if (type
== IO_DELALLOC
&&
321 (!nimaps
|| isnullstartblock(imap
->br_startblock
))) {
322 error
= xfs_iomap_write_allocate(ip
, offset
, count
, imap
);
324 trace_xfs_map_blocks_alloc(ip
, offset
, count
, type
, imap
);
325 return -XFS_ERROR(error
);
329 if (type
== IO_UNWRITTEN
) {
331 ASSERT(imap
->br_startblock
!= HOLESTARTBLOCK
);
332 ASSERT(imap
->br_startblock
!= DELAYSTARTBLOCK
);
336 trace_xfs_map_blocks_found(ip
, offset
, count
, type
, imap
);
343 struct xfs_bmbt_irec
*imap
,
346 offset
>>= inode
->i_blkbits
;
348 return offset
>= imap
->br_startoff
&&
349 offset
< imap
->br_startoff
+ imap
->br_blockcount
;
353 * BIO completion handler for buffered IO.
360 xfs_ioend_t
*ioend
= bio
->bi_private
;
362 ASSERT(atomic_read(&bio
->bi_cnt
) >= 1);
363 ioend
->io_error
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
) ? 0 : error
;
365 /* Toss bio and pass work off to an xfsdatad thread */
366 bio
->bi_private
= NULL
;
367 bio
->bi_end_io
= NULL
;
370 xfs_finish_ioend(ioend
);
374 xfs_submit_ioend_bio(
375 struct writeback_control
*wbc
,
379 atomic_inc(&ioend
->io_remaining
);
380 bio
->bi_private
= ioend
;
381 bio
->bi_end_io
= xfs_end_bio
;
384 * If the I/O is beyond EOF we mark the inode dirty immediately
385 * but don't update the inode size until I/O completion.
387 if (xfs_ioend_new_eof(ioend
))
388 xfs_mark_inode_dirty(XFS_I(ioend
->io_inode
));
390 submit_bio(wbc
->sync_mode
== WB_SYNC_ALL
? WRITE_SYNC
: WRITE
, bio
);
395 struct buffer_head
*bh
)
397 int nvecs
= bio_get_nr_vecs(bh
->b_bdev
);
398 struct bio
*bio
= bio_alloc(GFP_NOIO
, nvecs
);
400 ASSERT(bio
->bi_private
== NULL
);
401 bio
->bi_sector
= bh
->b_blocknr
* (bh
->b_size
>> 9);
402 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
);
475 } while ((ioend
= next
) != NULL
);
477 /* Pass 2 - submit I/O */
480 next
= ioend
->io_list
;
483 for (bh
= ioend
->io_buffer_head
; bh
; bh
= bh
->b_private
) {
487 bio
= xfs_alloc_ioend_bio(bh
);
488 } else if (bh
->b_blocknr
!= lastblock
+ 1) {
489 xfs_submit_ioend_bio(wbc
, ioend
, bio
);
493 if (bio_add_buffer(bio
, bh
) != bh
->b_size
) {
494 xfs_submit_ioend_bio(wbc
, ioend
, bio
);
498 lastblock
= bh
->b_blocknr
;
501 xfs_submit_ioend_bio(wbc
, ioend
, bio
);
502 xfs_finish_ioend(ioend
);
503 } while ((ioend
= next
) != NULL
);
507 * Cancel submission of all buffer_heads so far in this endio.
508 * Toss the endio too. Only ever called for the initial page
509 * in a writepage request, so only ever one page.
516 struct buffer_head
*bh
, *next_bh
;
519 next
= ioend
->io_list
;
520 bh
= ioend
->io_buffer_head
;
522 next_bh
= bh
->b_private
;
523 clear_buffer_async_write(bh
);
525 } while ((bh
= next_bh
) != NULL
);
527 mempool_free(ioend
, xfs_ioend_pool
);
528 } while ((ioend
= next
) != NULL
);
532 * Test to see if we've been building up a completion structure for
533 * earlier buffers -- if so, we try to append to this ioend if we
534 * can, otherwise we finish off any current ioend and start another.
535 * Return true if we've finished the given ioend.
540 struct buffer_head
*bh
,
543 xfs_ioend_t
**result
,
546 xfs_ioend_t
*ioend
= *result
;
548 if (!ioend
|| need_ioend
|| type
!= ioend
->io_type
) {
549 xfs_ioend_t
*previous
= *result
;
551 ioend
= xfs_alloc_ioend(inode
, type
);
552 ioend
->io_offset
= offset
;
553 ioend
->io_buffer_head
= bh
;
554 ioend
->io_buffer_tail
= bh
;
556 previous
->io_list
= ioend
;
559 ioend
->io_buffer_tail
->b_private
= bh
;
560 ioend
->io_buffer_tail
= bh
;
563 bh
->b_private
= NULL
;
564 ioend
->io_size
+= bh
->b_size
;
570 struct buffer_head
*bh
,
571 struct xfs_bmbt_irec
*imap
,
575 struct xfs_mount
*m
= XFS_I(inode
)->i_mount
;
576 xfs_off_t iomap_offset
= XFS_FSB_TO_B(m
, imap
->br_startoff
);
577 xfs_daddr_t iomap_bn
= xfs_fsb_to_db(XFS_I(inode
), imap
->br_startblock
);
579 ASSERT(imap
->br_startblock
!= HOLESTARTBLOCK
);
580 ASSERT(imap
->br_startblock
!= DELAYSTARTBLOCK
);
582 bn
= (iomap_bn
>> (inode
->i_blkbits
- BBSHIFT
)) +
583 ((offset
- iomap_offset
) >> inode
->i_blkbits
);
585 ASSERT(bn
|| XFS_IS_REALTIME_INODE(XFS_I(inode
)));
588 set_buffer_mapped(bh
);
594 struct buffer_head
*bh
,
595 struct xfs_bmbt_irec
*imap
,
598 ASSERT(imap
->br_startblock
!= HOLESTARTBLOCK
);
599 ASSERT(imap
->br_startblock
!= DELAYSTARTBLOCK
);
601 xfs_map_buffer(inode
, bh
, imap
, offset
);
602 set_buffer_mapped(bh
);
603 clear_buffer_delay(bh
);
604 clear_buffer_unwritten(bh
);
608 * Test if a given page is suitable for writing as part of an unwritten
609 * or delayed allocate extent.
616 if (PageWriteback(page
))
619 if (page
->mapping
&& page_has_buffers(page
)) {
620 struct buffer_head
*bh
, *head
;
623 bh
= head
= page_buffers(page
);
625 if (buffer_unwritten(bh
))
626 acceptable
= (type
== IO_UNWRITTEN
);
627 else if (buffer_delay(bh
))
628 acceptable
= (type
== IO_DELALLOC
);
629 else if (buffer_dirty(bh
) && buffer_mapped(bh
))
630 acceptable
= (type
== IO_OVERWRITE
);
633 } while ((bh
= bh
->b_this_page
) != head
);
643 * Allocate & map buffers for page given the extent map. Write it out.
644 * except for the original page of a writepage, this is called on
645 * delalloc/unwritten pages only, for the original page it is possible
646 * that the page has no mapping at all.
653 struct xfs_bmbt_irec
*imap
,
654 xfs_ioend_t
**ioendp
,
655 struct writeback_control
*wbc
)
657 struct buffer_head
*bh
, *head
;
658 xfs_off_t end_offset
;
659 unsigned long p_offset
;
662 int count
= 0, done
= 0, uptodate
= 1;
663 xfs_off_t offset
= page_offset(page
);
665 if (page
->index
!= tindex
)
667 if (!trylock_page(page
))
669 if (PageWriteback(page
))
670 goto fail_unlock_page
;
671 if (page
->mapping
!= inode
->i_mapping
)
672 goto fail_unlock_page
;
673 if (!xfs_is_delayed_page(page
, (*ioendp
)->io_type
))
674 goto fail_unlock_page
;
677 * page_dirty is initially a count of buffers on the page before
678 * EOF and is decremented as we move each into a cleanable state.
682 * End offset is the highest offset that this page should represent.
683 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
684 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
685 * hence give us the correct page_dirty count. On any other page,
686 * it will be zero and in that case we need page_dirty to be the
687 * count of buffers on the page.
689 end_offset
= min_t(unsigned long long,
690 (xfs_off_t
)(page
->index
+ 1) << PAGE_CACHE_SHIFT
,
693 len
= 1 << inode
->i_blkbits
;
694 p_offset
= min_t(unsigned long, end_offset
& (PAGE_CACHE_SIZE
- 1),
696 p_offset
= p_offset
? roundup(p_offset
, len
) : PAGE_CACHE_SIZE
;
697 page_dirty
= p_offset
/ len
;
699 bh
= head
= page_buffers(page
);
701 if (offset
>= end_offset
)
703 if (!buffer_uptodate(bh
))
705 if (!(PageUptodate(page
) || buffer_uptodate(bh
))) {
710 if (buffer_unwritten(bh
) || buffer_delay(bh
) ||
712 if (buffer_unwritten(bh
))
714 else if (buffer_delay(bh
))
719 if (!xfs_imap_valid(inode
, imap
, offset
)) {
725 if (type
!= IO_OVERWRITE
)
726 xfs_map_at_offset(inode
, bh
, imap
, offset
);
727 xfs_add_to_ioend(inode
, bh
, offset
, type
,
735 } while (offset
+= len
, (bh
= bh
->b_this_page
) != head
);
737 if (uptodate
&& bh
== head
)
738 SetPageUptodate(page
);
741 if (--wbc
->nr_to_write
<= 0 &&
742 wbc
->sync_mode
== WB_SYNC_NONE
)
745 xfs_start_page_writeback(page
, !page_dirty
, count
);
755 * Convert & write out a cluster of pages in the same extent as defined
756 * by mp and following the start page.
762 struct xfs_bmbt_irec
*imap
,
763 xfs_ioend_t
**ioendp
,
764 struct writeback_control
*wbc
,
770 pagevec_init(&pvec
, 0);
771 while (!done
&& tindex
<= tlast
) {
772 unsigned len
= min_t(pgoff_t
, PAGEVEC_SIZE
, tlast
- tindex
+ 1);
774 if (!pagevec_lookup(&pvec
, inode
->i_mapping
, tindex
, len
))
777 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
778 done
= xfs_convert_page(inode
, pvec
.pages
[i
], tindex
++,
784 pagevec_release(&pvec
);
790 xfs_vm_invalidatepage(
792 unsigned long offset
)
794 trace_xfs_invalidatepage(page
->mapping
->host
, page
, offset
);
795 block_invalidatepage(page
, offset
);
799 * If the page has delalloc buffers on it, we need to punch them out before we
800 * invalidate the page. If we don't, we leave a stale delalloc mapping on the
801 * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
802 * is done on that same region - the delalloc extent is returned when none is
803 * supposed to be there.
805 * We prevent this by truncating away the delalloc regions on the page before
806 * invalidating it. Because they are delalloc, we can do this without needing a
807 * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
808 * truncation without a transaction as there is no space left for block
809 * reservation (typically why we see a ENOSPC in writeback).
811 * This is not a performance critical path, so for now just do the punching a
812 * buffer head at a time.
815 xfs_aops_discard_page(
818 struct inode
*inode
= page
->mapping
->host
;
819 struct xfs_inode
*ip
= XFS_I(inode
);
820 struct buffer_head
*bh
, *head
;
821 loff_t offset
= page_offset(page
);
823 if (!xfs_is_delayed_page(page
, IO_DELALLOC
))
826 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
829 xfs_alert(ip
->i_mount
,
830 "page discard on page %p, inode 0x%llx, offset %llu.",
831 page
, ip
->i_ino
, offset
);
833 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
834 bh
= head
= page_buffers(page
);
837 xfs_fileoff_t start_fsb
;
839 if (!buffer_delay(bh
))
842 start_fsb
= XFS_B_TO_FSBT(ip
->i_mount
, offset
);
843 error
= xfs_bmap_punch_delalloc_range(ip
, start_fsb
, 1);
845 /* something screwed, just bail */
846 if (!XFS_FORCED_SHUTDOWN(ip
->i_mount
)) {
847 xfs_alert(ip
->i_mount
,
848 "page discard unable to remove delalloc mapping.");
853 offset
+= 1 << inode
->i_blkbits
;
855 } while ((bh
= bh
->b_this_page
) != head
);
857 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
859 xfs_vm_invalidatepage(page
, 0);
864 * Write out a dirty page.
866 * For delalloc space on the page we need to allocate space and flush it.
867 * For unwritten space on the page we need to start the conversion to
868 * regular allocated space.
869 * For any other dirty buffer heads on the page we should flush them.
874 struct writeback_control
*wbc
)
876 struct inode
*inode
= page
->mapping
->host
;
877 struct buffer_head
*bh
, *head
;
878 struct xfs_bmbt_irec imap
;
879 xfs_ioend_t
*ioend
= NULL
, *iohead
= NULL
;
882 __uint64_t end_offset
;
883 pgoff_t end_index
, last_index
;
885 int err
, imap_valid
= 0, uptodate
= 1;
889 trace_xfs_writepage(inode
, page
, 0);
891 ASSERT(page_has_buffers(page
));
894 * Refuse to write the page out if we are called from reclaim context.
896 * This avoids stack overflows when called from deeply used stacks in
897 * random callers for direct reclaim or memcg reclaim. We explicitly
898 * allow reclaim from kswapd as the stack usage there is relatively low.
900 * This should never happen except in the case of a VM regression so
903 if (WARN_ON_ONCE((current
->flags
& (PF_MEMALLOC
|PF_KSWAPD
)) ==
908 * Given that we do not allow direct reclaim to call us, we should
909 * never be called while in a filesystem transaction.
911 if (WARN_ON(current
->flags
& PF_FSTRANS
))
914 /* Is this page beyond the end of the file? */
915 offset
= i_size_read(inode
);
916 end_index
= offset
>> PAGE_CACHE_SHIFT
;
917 last_index
= (offset
- 1) >> PAGE_CACHE_SHIFT
;
918 if (page
->index
>= end_index
) {
919 if ((page
->index
>= end_index
+ 1) ||
920 !(i_size_read(inode
) & (PAGE_CACHE_SIZE
- 1))) {
926 end_offset
= min_t(unsigned long long,
927 (xfs_off_t
)(page
->index
+ 1) << PAGE_CACHE_SHIFT
,
929 len
= 1 << inode
->i_blkbits
;
931 bh
= head
= page_buffers(page
);
932 offset
= page_offset(page
);
935 if (wbc
->sync_mode
== WB_SYNC_NONE
)
941 if (offset
>= end_offset
)
943 if (!buffer_uptodate(bh
))
947 * set_page_dirty dirties all buffers in a page, independent
948 * of their state. The dirty state however is entirely
949 * meaningless for holes (!mapped && uptodate), so skip
950 * buffers covering holes here.
952 if (!buffer_mapped(bh
) && buffer_uptodate(bh
)) {
957 if (buffer_unwritten(bh
)) {
958 if (type
!= IO_UNWRITTEN
) {
962 } else if (buffer_delay(bh
)) {
963 if (type
!= IO_DELALLOC
) {
967 } else if (buffer_uptodate(bh
)) {
968 if (type
!= IO_OVERWRITE
) {
973 if (PageUptodate(page
)) {
974 ASSERT(buffer_mapped(bh
));
981 imap_valid
= xfs_imap_valid(inode
, &imap
, offset
);
984 * If we didn't have a valid mapping then we need to
985 * put the new mapping into a separate ioend structure.
986 * This ensures non-contiguous extents always have
987 * separate ioends, which is particularly important
988 * for unwritten extent conversion at I/O completion
992 err
= xfs_map_blocks(inode
, offset
, &imap
, type
,
996 imap_valid
= xfs_imap_valid(inode
, &imap
, offset
);
1000 if (type
!= IO_OVERWRITE
)
1001 xfs_map_at_offset(inode
, bh
, &imap
, offset
);
1002 xfs_add_to_ioend(inode
, bh
, offset
, type
, &ioend
,
1010 } while (offset
+= len
, ((bh
= bh
->b_this_page
) != head
));
1012 if (uptodate
&& bh
== head
)
1013 SetPageUptodate(page
);
1015 xfs_start_page_writeback(page
, 1, count
);
1017 if (ioend
&& imap_valid
) {
1018 xfs_off_t end_index
;
1020 end_index
= imap
.br_startoff
+ imap
.br_blockcount
;
1023 end_index
<<= inode
->i_blkbits
;
1026 end_index
= (end_index
- 1) >> PAGE_CACHE_SHIFT
;
1028 /* check against file size */
1029 if (end_index
> last_index
)
1030 end_index
= last_index
;
1032 xfs_cluster_write(inode
, page
->index
+ 1, &imap
, &ioend
,
1037 xfs_submit_ioend(wbc
, iohead
);
1043 xfs_cancel_ioend(iohead
);
1048 xfs_aops_discard_page(page
);
1049 ClearPageUptodate(page
);
1054 redirty_page_for_writepage(wbc
, page
);
1061 struct address_space
*mapping
,
1062 struct writeback_control
*wbc
)
1064 xfs_iflags_clear(XFS_I(mapping
->host
), XFS_ITRUNCATED
);
1065 return generic_writepages(mapping
, wbc
);
1069 * Called to move a page into cleanable state - and from there
1070 * to be released. The page should already be clean. We always
1071 * have buffer heads in this call.
1073 * Returns 1 if the page is ok to release, 0 otherwise.
1080 int delalloc
, unwritten
;
1082 trace_xfs_releasepage(page
->mapping
->host
, page
, 0);
1084 xfs_count_page_state(page
, &delalloc
, &unwritten
);
1086 if (WARN_ON(delalloc
))
1088 if (WARN_ON(unwritten
))
1091 return try_to_free_buffers(page
);
1096 struct inode
*inode
,
1098 struct buffer_head
*bh_result
,
1102 struct xfs_inode
*ip
= XFS_I(inode
);
1103 struct xfs_mount
*mp
= ip
->i_mount
;
1104 xfs_fileoff_t offset_fsb
, end_fsb
;
1107 struct xfs_bmbt_irec imap
;
1113 if (XFS_FORCED_SHUTDOWN(mp
))
1114 return -XFS_ERROR(EIO
);
1116 offset
= (xfs_off_t
)iblock
<< inode
->i_blkbits
;
1117 ASSERT(bh_result
->b_size
>= (1 << inode
->i_blkbits
));
1118 size
= bh_result
->b_size
;
1120 if (!create
&& direct
&& offset
>= i_size_read(inode
))
1124 lockmode
= XFS_ILOCK_EXCL
;
1125 xfs_ilock(ip
, lockmode
);
1127 lockmode
= xfs_ilock_map_shared(ip
);
1130 ASSERT(offset
<= mp
->m_maxioffset
);
1131 if (offset
+ size
> mp
->m_maxioffset
)
1132 size
= mp
->m_maxioffset
- offset
;
1133 end_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)offset
+ size
);
1134 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
1136 error
= xfs_bmapi_read(ip
, offset_fsb
, end_fsb
- offset_fsb
,
1137 &imap
, &nimaps
, XFS_BMAPI_ENTIRE
);
1143 (imap
.br_startblock
== HOLESTARTBLOCK
||
1144 imap
.br_startblock
== DELAYSTARTBLOCK
))) {
1146 error
= xfs_iomap_write_direct(ip
, offset
, size
,
1149 error
= xfs_iomap_write_delay(ip
, offset
, size
, &imap
);
1154 trace_xfs_get_blocks_alloc(ip
, offset
, size
, 0, &imap
);
1155 } else if (nimaps
) {
1156 trace_xfs_get_blocks_found(ip
, offset
, size
, 0, &imap
);
1158 trace_xfs_get_blocks_notfound(ip
, offset
, size
);
1161 xfs_iunlock(ip
, lockmode
);
1163 if (imap
.br_startblock
!= HOLESTARTBLOCK
&&
1164 imap
.br_startblock
!= DELAYSTARTBLOCK
) {
1166 * For unwritten extents do not report a disk address on
1167 * the read case (treat as if we're reading into a hole).
1169 if (create
|| !ISUNWRITTEN(&imap
))
1170 xfs_map_buffer(inode
, bh_result
, &imap
, offset
);
1171 if (create
&& ISUNWRITTEN(&imap
)) {
1173 bh_result
->b_private
= inode
;
1174 set_buffer_unwritten(bh_result
);
1179 * If this is a realtime file, data may be on a different device.
1180 * to that pointed to from the buffer_head b_bdev currently.
1182 bh_result
->b_bdev
= xfs_find_bdev_for_inode(inode
);
1185 * If we previously allocated a block out beyond eof and we are now
1186 * coming back to use it then we will need to flag it as new even if it
1187 * has a disk address.
1189 * With sub-block writes into unwritten extents we also need to mark
1190 * the buffer as new so that the unwritten parts of the buffer gets
1194 ((!buffer_mapped(bh_result
) && !buffer_uptodate(bh_result
)) ||
1195 (offset
>= i_size_read(inode
)) ||
1196 (new || ISUNWRITTEN(&imap
))))
1197 set_buffer_new(bh_result
);
1199 if (imap
.br_startblock
== DELAYSTARTBLOCK
) {
1202 set_buffer_uptodate(bh_result
);
1203 set_buffer_mapped(bh_result
);
1204 set_buffer_delay(bh_result
);
1209 * If this is O_DIRECT or the mpage code calling tell them how large
1210 * the mapping is, so that we can avoid repeated get_blocks calls.
1212 if (direct
|| size
> (1 << inode
->i_blkbits
)) {
1213 xfs_off_t mapping_size
;
1215 mapping_size
= imap
.br_startoff
+ imap
.br_blockcount
- iblock
;
1216 mapping_size
<<= inode
->i_blkbits
;
1218 ASSERT(mapping_size
> 0);
1219 if (mapping_size
> size
)
1220 mapping_size
= size
;
1221 if (mapping_size
> LONG_MAX
)
1222 mapping_size
= LONG_MAX
;
1224 bh_result
->b_size
= mapping_size
;
1230 xfs_iunlock(ip
, lockmode
);
1236 struct inode
*inode
,
1238 struct buffer_head
*bh_result
,
1241 return __xfs_get_blocks(inode
, iblock
, bh_result
, create
, 0);
1245 xfs_get_blocks_direct(
1246 struct inode
*inode
,
1248 struct buffer_head
*bh_result
,
1251 return __xfs_get_blocks(inode
, iblock
, bh_result
, create
, 1);
1255 * Complete a direct I/O write request.
1257 * If the private argument is non-NULL __xfs_get_blocks signals us that we
1258 * need to issue a transaction to convert the range from unwritten to written
1259 * extents. In case this is regular synchronous I/O we just call xfs_end_io
1260 * to do this and we are done. But in case this was a successful AIO
1261 * request this handler is called from interrupt context, from which we
1262 * can't start transactions. In that case offload the I/O completion to
1263 * the workqueues we also use for buffered I/O completion.
1266 xfs_end_io_direct_write(
1274 struct xfs_ioend
*ioend
= iocb
->private;
1277 * While the generic direct I/O code updates the inode size, it does
1278 * so only after the end_io handler is called, which means our
1279 * end_io handler thinks the on-disk size is outside the in-core
1280 * size. To prevent this just update it a little bit earlier here.
1282 if (offset
+ size
> i_size_read(ioend
->io_inode
))
1283 i_size_write(ioend
->io_inode
, offset
+ size
);
1286 * blockdev_direct_IO can return an error even after the I/O
1287 * completion handler was called. Thus we need to protect
1288 * against double-freeing.
1290 iocb
->private = NULL
;
1292 ioend
->io_offset
= offset
;
1293 ioend
->io_size
= size
;
1294 ioend
->io_iocb
= iocb
;
1295 ioend
->io_result
= ret
;
1296 if (private && size
> 0)
1297 ioend
->io_type
= IO_UNWRITTEN
;
1300 ioend
->io_isasync
= 1;
1301 xfs_finish_ioend(ioend
);
1303 xfs_finish_ioend_sync(ioend
);
1311 const struct iovec
*iov
,
1313 unsigned long nr_segs
)
1315 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
1316 struct block_device
*bdev
= xfs_find_bdev_for_inode(inode
);
1320 iocb
->private = xfs_alloc_ioend(inode
, IO_DIRECT
);
1322 ret
= __blockdev_direct_IO(rw
, iocb
, inode
, bdev
, iov
,
1324 xfs_get_blocks_direct
,
1325 xfs_end_io_direct_write
, NULL
, 0);
1326 if (ret
!= -EIOCBQUEUED
&& iocb
->private)
1327 xfs_destroy_ioend(iocb
->private);
1329 ret
= __blockdev_direct_IO(rw
, iocb
, inode
, bdev
, iov
,
1331 xfs_get_blocks_direct
,
1339 xfs_vm_write_failed(
1340 struct address_space
*mapping
,
1343 struct inode
*inode
= mapping
->host
;
1345 if (to
> inode
->i_size
) {
1347 * Punch out the delalloc blocks we have already allocated.
1349 * Don't bother with xfs_setattr given that nothing can have
1350 * made it to disk yet as the page is still locked at this
1353 struct xfs_inode
*ip
= XFS_I(inode
);
1354 xfs_fileoff_t start_fsb
;
1355 xfs_fileoff_t end_fsb
;
1358 truncate_pagecache(inode
, to
, inode
->i_size
);
1361 * Check if there are any blocks that are outside of i_size
1362 * that need to be trimmed back.
1364 start_fsb
= XFS_B_TO_FSB(ip
->i_mount
, inode
->i_size
) + 1;
1365 end_fsb
= XFS_B_TO_FSB(ip
->i_mount
, to
);
1366 if (end_fsb
<= start_fsb
)
1369 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1370 error
= xfs_bmap_punch_delalloc_range(ip
, start_fsb
,
1371 end_fsb
- start_fsb
);
1373 /* something screwed, just bail */
1374 if (!XFS_FORCED_SHUTDOWN(ip
->i_mount
)) {
1375 xfs_alert(ip
->i_mount
,
1376 "xfs_vm_write_failed: unable to clean up ino %lld",
1380 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1387 struct address_space
*mapping
,
1391 struct page
**pagep
,
1396 ret
= block_write_begin(mapping
, pos
, len
, flags
| AOP_FLAG_NOFS
,
1397 pagep
, xfs_get_blocks
);
1399 xfs_vm_write_failed(mapping
, pos
+ len
);
1406 struct address_space
*mapping
,
1415 ret
= generic_write_end(file
, mapping
, pos
, len
, copied
, page
, fsdata
);
1416 if (unlikely(ret
< len
))
1417 xfs_vm_write_failed(mapping
, pos
+ len
);
1423 struct address_space
*mapping
,
1426 struct inode
*inode
= (struct inode
*)mapping
->host
;
1427 struct xfs_inode
*ip
= XFS_I(inode
);
1429 trace_xfs_vm_bmap(XFS_I(inode
));
1430 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
1431 xfs_flush_pages(ip
, (xfs_off_t
)0, -1, 0, FI_REMAPF
);
1432 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
1433 return generic_block_bmap(mapping
, block
, xfs_get_blocks
);
1438 struct file
*unused
,
1441 return mpage_readpage(page
, xfs_get_blocks
);
1446 struct file
*unused
,
1447 struct address_space
*mapping
,
1448 struct list_head
*pages
,
1451 return mpage_readpages(mapping
, pages
, nr_pages
, xfs_get_blocks
);
1454 const struct address_space_operations xfs_address_space_operations
= {
1455 .readpage
= xfs_vm_readpage
,
1456 .readpages
= xfs_vm_readpages
,
1457 .writepage
= xfs_vm_writepage
,
1458 .writepages
= xfs_vm_writepages
,
1459 .releasepage
= xfs_vm_releasepage
,
1460 .invalidatepage
= xfs_vm_invalidatepage
,
1461 .write_begin
= xfs_vm_write_begin
,
1462 .write_end
= xfs_vm_write_end
,
1463 .bmap
= xfs_vm_bmap
,
1464 .direct_IO
= xfs_vm_direct_IO
,
1465 .migratepage
= buffer_migrate_page
,
1466 .is_partially_uptodate
= block_is_partially_uptodate
,
1467 .error_remove_page
= generic_error_remove_page
,