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_result, 0);
93 inode_dio_done(ioend->io_inode);
96 mempool_free(ioend, xfs_ioend_pool);
100 * If the end of the current ioend is beyond the current EOF,
101 * return the new EOF value, otherwise zero.
107 xfs_inode_t *ip = XFS_I(ioend->io_inode);
111 bsize = ioend->io_offset + ioend->io_size;
112 isize = MAX(ip->i_size, ip->i_new_size);
113 isize = MIN(isize, bsize);
114 return isize > ip->i_d.di_size ? isize : 0;
118 * Fast and loose check if this write could update the on-disk inode size.
120 static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend)
122 return ioend->io_offset + ioend->io_size >
123 XFS_I(ioend->io_inode)->i_d.di_size;
127 * Update on-disk file size now that data has been written to disk. The
128 * current in-memory file size is i_size. If a write is beyond eof i_new_size
129 * will be the intended file size until i_size is updated. If this write does
130 * not extend all the way to the valid file size then restrict this update to
131 * the end of the write.
133 * This function does not block as blocking on the inode lock in IO completion
134 * can lead to IO completion order dependency deadlocks.. If it can't get the
135 * inode ilock it will return EAGAIN. Callers must handle this.
141 xfs_inode_t *ip = XFS_I(ioend->io_inode);
144 if (unlikely(ioend->io_error))
147 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL))
150 isize = xfs_ioend_new_eof(ioend);
152 trace_xfs_setfilesize(ip, ioend->io_offset, ioend->io_size);
153 ip->i_d.di_size = isize;
154 xfs_mark_inode_dirty(ip);
157 xfs_iunlock(ip, XFS_ILOCK_EXCL);
162 * Schedule IO completion handling on the final put of an ioend.
164 * If there is no work to do we might as well call it a day and free the
169 struct xfs_ioend *ioend)
171 if (atomic_dec_and_test(&ioend->io_remaining)) {
172 if (ioend->io_type == IO_UNWRITTEN)
173 queue_work(xfsconvertd_workqueue, &ioend->io_work);
174 else if (xfs_ioend_is_append(ioend))
175 queue_work(xfsdatad_workqueue, &ioend->io_work);
177 xfs_destroy_ioend(ioend);
182 * IO write completion.
186 struct work_struct *work)
188 xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work);
189 struct xfs_inode *ip = XFS_I(ioend->io_inode);
193 * For unwritten extents we need to issue transactions to convert a
194 * range to normal written extens after the data I/O has finished.
196 if (ioend->io_type == IO_UNWRITTEN &&
197 likely(!ioend->io_error && !XFS_FORCED_SHUTDOWN(ip->i_mount))) {
199 error = xfs_iomap_write_unwritten(ip, ioend->io_offset,
202 ioend->io_error = error;
206 * We might have to update the on-disk file size after extending
209 error = xfs_setfilesize(ioend);
210 ASSERT(!error || error == EAGAIN);
213 * If we didn't complete processing of the ioend, requeue it to the
214 * tail of the workqueue for another attempt later. Otherwise destroy
217 if (error == EAGAIN) {
218 atomic_inc(&ioend->io_remaining);
219 xfs_finish_ioend(ioend);
220 /* ensure we don't spin on blocked ioends */
223 xfs_destroy_ioend(ioend);
228 * Call IO completion handling in caller context on the final put of an ioend.
231 xfs_finish_ioend_sync(
232 struct xfs_ioend *ioend)
234 if (atomic_dec_and_test(&ioend->io_remaining))
235 xfs_end_io(&ioend->io_work);
239 * Allocate and initialise an IO completion structure.
240 * We need to track unwritten extent write completion here initially.
241 * We'll need to extend this for updating the ondisk inode size later
251 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
254 * Set the count to 1 initially, which will prevent an I/O
255 * completion callback from happening before we have started
256 * all the I/O from calling the completion routine too early.
258 atomic_set(&ioend->io_remaining, 1);
259 ioend->io_isasync = 0;
261 ioend->io_list = NULL;
262 ioend->io_type = type;
263 ioend->io_inode = inode;
264 ioend->io_buffer_head = NULL;
265 ioend->io_buffer_tail = NULL;
266 ioend->io_offset = 0;
268 ioend->io_iocb = NULL;
269 ioend->io_result = 0;
271 INIT_WORK(&ioend->io_work, xfs_end_io);
279 struct xfs_bmbt_irec *imap,
283 struct xfs_inode *ip = XFS_I(inode);
284 struct xfs_mount *mp = ip->i_mount;
285 ssize_t count = 1 << inode->i_blkbits;
286 xfs_fileoff_t offset_fsb, end_fsb;
288 int bmapi_flags = XFS_BMAPI_ENTIRE;
291 if (XFS_FORCED_SHUTDOWN(mp))
292 return -XFS_ERROR(EIO);
294 if (type == IO_UNWRITTEN)
295 bmapi_flags |= XFS_BMAPI_IGSTATE;
297 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
299 return -XFS_ERROR(EAGAIN);
300 xfs_ilock(ip, XFS_ILOCK_SHARED);
303 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
304 (ip->i_df.if_flags & XFS_IFEXTENTS));
305 ASSERT(offset <= mp->m_maxioffset);
307 if (offset + count > mp->m_maxioffset)
308 count = mp->m_maxioffset - offset;
309 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
310 offset_fsb = XFS_B_TO_FSBT(mp, offset);
311 error = xfs_bmapi(NULL, ip, offset_fsb, end_fsb - offset_fsb,
312 bmapi_flags, NULL, 0, imap, &nimaps, NULL);
313 xfs_iunlock(ip, XFS_ILOCK_SHARED);
316 return -XFS_ERROR(error);
318 if (type == IO_DELALLOC &&
319 (!nimaps || isnullstartblock(imap->br_startblock))) {
320 error = xfs_iomap_write_allocate(ip, offset, count, imap);
322 trace_xfs_map_blocks_alloc(ip, offset, count, type, imap);
323 return -XFS_ERROR(error);
327 if (type == IO_UNWRITTEN) {
329 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
330 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
334 trace_xfs_map_blocks_found(ip, offset, count, type, imap);
341 struct xfs_bmbt_irec *imap,
344 offset >>= inode->i_blkbits;
346 return offset >= imap->br_startoff &&
347 offset < imap->br_startoff + imap->br_blockcount;
351 * BIO completion handler for buffered IO.
358 xfs_ioend_t *ioend = bio->bi_private;
360 ASSERT(atomic_read(&bio->bi_cnt) >= 1);
361 ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error;
363 /* Toss bio and pass work off to an xfsdatad thread */
364 bio->bi_private = NULL;
365 bio->bi_end_io = NULL;
368 xfs_finish_ioend(ioend);
372 xfs_submit_ioend_bio(
373 struct writeback_control *wbc,
377 atomic_inc(&ioend->io_remaining);
378 bio->bi_private = ioend;
379 bio->bi_end_io = xfs_end_bio;
382 * If the I/O is beyond EOF we mark the inode dirty immediately
383 * but don't update the inode size until I/O completion.
385 if (xfs_ioend_new_eof(ioend))
386 xfs_mark_inode_dirty(XFS_I(ioend->io_inode));
388 submit_bio(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE, bio);
393 struct buffer_head *bh)
395 int nvecs = bio_get_nr_vecs(bh->b_bdev);
396 struct bio *bio = bio_alloc(GFP_NOIO, nvecs);
398 ASSERT(bio->bi_private == NULL);
399 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
400 bio->bi_bdev = bh->b_bdev;
405 xfs_start_buffer_writeback(
406 struct buffer_head *bh)
408 ASSERT(buffer_mapped(bh));
409 ASSERT(buffer_locked(bh));
410 ASSERT(!buffer_delay(bh));
411 ASSERT(!buffer_unwritten(bh));
413 mark_buffer_async_write(bh);
414 set_buffer_uptodate(bh);
415 clear_buffer_dirty(bh);
419 xfs_start_page_writeback(
424 ASSERT(PageLocked(page));
425 ASSERT(!PageWriteback(page));
427 clear_page_dirty_for_io(page);
428 set_page_writeback(page);
430 /* If no buffers on the page are to be written, finish it here */
432 end_page_writeback(page);
435 static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
437 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
441 * Submit all of the bios for all of the ioends we have saved up, covering the
442 * initial writepage page and also any probed pages.
444 * Because we may have multiple ioends spanning a page, we need to start
445 * writeback on all the buffers before we submit them for I/O. If we mark the
446 * buffers as we got, then we can end up with a page that only has buffers
447 * marked async write and I/O complete on can occur before we mark the other
448 * buffers async write.
450 * The end result of this is that we trip a bug in end_page_writeback() because
451 * we call it twice for the one page as the code in end_buffer_async_write()
452 * assumes that all buffers on the page are started at the same time.
454 * The fix is two passes across the ioend list - one to start writeback on the
455 * buffer_heads, and then submit them for I/O on the second pass.
459 struct writeback_control *wbc,
462 xfs_ioend_t *head = ioend;
464 struct buffer_head *bh;
466 sector_t lastblock = 0;
468 /* Pass 1 - start writeback */
470 next = ioend->io_list;
471 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private)
472 xfs_start_buffer_writeback(bh);
473 } while ((ioend = next) != NULL);
475 /* Pass 2 - submit I/O */
478 next = ioend->io_list;
481 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
485 bio = xfs_alloc_ioend_bio(bh);
486 } else if (bh->b_blocknr != lastblock + 1) {
487 xfs_submit_ioend_bio(wbc, ioend, bio);
491 if (bio_add_buffer(bio, bh) != bh->b_size) {
492 xfs_submit_ioend_bio(wbc, ioend, bio);
496 lastblock = bh->b_blocknr;
499 xfs_submit_ioend_bio(wbc, ioend, bio);
500 xfs_finish_ioend(ioend);
501 } while ((ioend = next) != NULL);
505 * Cancel submission of all buffer_heads so far in this endio.
506 * Toss the endio too. Only ever called for the initial page
507 * in a writepage request, so only ever one page.
514 struct buffer_head *bh, *next_bh;
517 next = ioend->io_list;
518 bh = ioend->io_buffer_head;
520 next_bh = bh->b_private;
521 clear_buffer_async_write(bh);
523 } while ((bh = next_bh) != NULL);
525 mempool_free(ioend, xfs_ioend_pool);
526 } while ((ioend = next) != NULL);
530 * Test to see if we've been building up a completion structure for
531 * earlier buffers -- if so, we try to append to this ioend if we
532 * can, otherwise we finish off any current ioend and start another.
533 * Return true if we've finished the given ioend.
538 struct buffer_head *bh,
541 xfs_ioend_t **result,
544 xfs_ioend_t *ioend = *result;
546 if (!ioend || need_ioend || type != ioend->io_type) {
547 xfs_ioend_t *previous = *result;
549 ioend = xfs_alloc_ioend(inode, type);
550 ioend->io_offset = offset;
551 ioend->io_buffer_head = bh;
552 ioend->io_buffer_tail = bh;
554 previous->io_list = ioend;
557 ioend->io_buffer_tail->b_private = bh;
558 ioend->io_buffer_tail = bh;
561 bh->b_private = NULL;
562 ioend->io_size += bh->b_size;
568 struct buffer_head *bh,
569 struct xfs_bmbt_irec *imap,
573 struct xfs_mount *m = XFS_I(inode)->i_mount;
574 xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff);
575 xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock);
577 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
578 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
580 bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
581 ((offset - iomap_offset) >> inode->i_blkbits);
583 ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
586 set_buffer_mapped(bh);
592 struct buffer_head *bh,
593 struct xfs_bmbt_irec *imap,
596 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
597 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
599 xfs_map_buffer(inode, bh, imap, offset);
600 set_buffer_mapped(bh);
601 clear_buffer_delay(bh);
602 clear_buffer_unwritten(bh);
606 * Test if a given page is suitable for writing as part of an unwritten
607 * or delayed allocate extent.
614 if (PageWriteback(page))
617 if (page->mapping && page_has_buffers(page)) {
618 struct buffer_head *bh, *head;
621 bh = head = page_buffers(page);
623 if (buffer_unwritten(bh))
624 acceptable = (type == IO_UNWRITTEN);
625 else if (buffer_delay(bh))
626 acceptable = (type == IO_DELALLOC);
627 else if (buffer_dirty(bh) && buffer_mapped(bh))
628 acceptable = (type == IO_OVERWRITE);
631 } while ((bh = bh->b_this_page) != head);
641 * Allocate & map buffers for page given the extent map. Write it out.
642 * except for the original page of a writepage, this is called on
643 * delalloc/unwritten pages only, for the original page it is possible
644 * that the page has no mapping at all.
651 struct xfs_bmbt_irec *imap,
652 xfs_ioend_t **ioendp,
653 struct writeback_control *wbc)
655 struct buffer_head *bh, *head;
656 xfs_off_t end_offset;
657 unsigned long p_offset;
660 int count = 0, done = 0, uptodate = 1;
661 xfs_off_t offset = page_offset(page);
663 if (page->index != tindex)
665 if (!trylock_page(page))
667 if (PageWriteback(page))
668 goto fail_unlock_page;
669 if (page->mapping != inode->i_mapping)
670 goto fail_unlock_page;
671 if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
672 goto fail_unlock_page;
675 * page_dirty is initially a count of buffers on the page before
676 * EOF and is decremented as we move each into a cleanable state.
680 * End offset is the highest offset that this page should represent.
681 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
682 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
683 * hence give us the correct page_dirty count. On any other page,
684 * it will be zero and in that case we need page_dirty to be the
685 * count of buffers on the page.
687 end_offset = min_t(unsigned long long,
688 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
691 len = 1 << inode->i_blkbits;
692 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
694 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
695 page_dirty = p_offset / len;
697 bh = head = page_buffers(page);
699 if (offset >= end_offset)
701 if (!buffer_uptodate(bh))
703 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
708 if (buffer_unwritten(bh) || buffer_delay(bh) ||
710 if (buffer_unwritten(bh))
712 else if (buffer_delay(bh))
717 if (!xfs_imap_valid(inode, imap, offset)) {
723 if (type != IO_OVERWRITE)
724 xfs_map_at_offset(inode, bh, imap, offset);
725 xfs_add_to_ioend(inode, bh, offset, type,
733 } while (offset += len, (bh = bh->b_this_page) != head);
735 if (uptodate && bh == head)
736 SetPageUptodate(page);
739 if (--wbc->nr_to_write <= 0 &&
740 wbc->sync_mode == WB_SYNC_NONE)
743 xfs_start_page_writeback(page, !page_dirty, count);
753 * Convert & write out a cluster of pages in the same extent as defined
754 * by mp and following the start page.
760 struct xfs_bmbt_irec *imap,
761 xfs_ioend_t **ioendp,
762 struct writeback_control *wbc,
768 pagevec_init(&pvec, 0);
769 while (!done && tindex <= tlast) {
770 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
772 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
775 for (i = 0; i < pagevec_count(&pvec); i++) {
776 done = xfs_convert_page(inode, pvec.pages[i], tindex++,
782 pagevec_release(&pvec);
788 xfs_vm_invalidatepage(
790 unsigned long offset)
792 trace_xfs_invalidatepage(page->mapping->host, page, offset);
793 block_invalidatepage(page, offset);
797 * If the page has delalloc buffers on it, we need to punch them out before we
798 * invalidate the page. If we don't, we leave a stale delalloc mapping on the
799 * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
800 * is done on that same region - the delalloc extent is returned when none is
801 * supposed to be there.
803 * We prevent this by truncating away the delalloc regions on the page before
804 * invalidating it. Because they are delalloc, we can do this without needing a
805 * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
806 * truncation without a transaction as there is no space left for block
807 * reservation (typically why we see a ENOSPC in writeback).
809 * This is not a performance critical path, so for now just do the punching a
810 * buffer head at a time.
813 xfs_aops_discard_page(
816 struct inode *inode = page->mapping->host;
817 struct xfs_inode *ip = XFS_I(inode);
818 struct buffer_head *bh, *head;
819 loff_t offset = page_offset(page);
821 if (!xfs_is_delayed_page(page, IO_DELALLOC))
824 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
827 xfs_alert(ip->i_mount,
828 "page discard on page %p, inode 0x%llx, offset %llu.",
829 page, ip->i_ino, offset);
831 xfs_ilock(ip, XFS_ILOCK_EXCL);
832 bh = head = page_buffers(page);
835 xfs_fileoff_t start_fsb;
837 if (!buffer_delay(bh))
840 start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
841 error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1);
843 /* something screwed, just bail */
844 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
845 xfs_alert(ip->i_mount,
846 "page discard unable to remove delalloc mapping.");
851 offset += 1 << inode->i_blkbits;
853 } while ((bh = bh->b_this_page) != head);
855 xfs_iunlock(ip, XFS_ILOCK_EXCL);
857 xfs_vm_invalidatepage(page, 0);
862 * Write out a dirty page.
864 * For delalloc space on the page we need to allocate space and flush it.
865 * For unwritten space on the page we need to start the conversion to
866 * regular allocated space.
867 * For any other dirty buffer heads on the page we should flush them.
872 struct writeback_control *wbc)
874 struct inode *inode = page->mapping->host;
875 struct buffer_head *bh, *head;
876 struct xfs_bmbt_irec imap;
877 xfs_ioend_t *ioend = NULL, *iohead = NULL;
880 __uint64_t end_offset;
881 pgoff_t end_index, last_index;
883 int err, imap_valid = 0, uptodate = 1;
887 trace_xfs_writepage(inode, page, 0);
889 ASSERT(page_has_buffers(page));
892 * Refuse to write the page out if we are called from reclaim context.
894 * This avoids stack overflows when called from deeply used stacks in
895 * random callers for direct reclaim or memcg reclaim. We explicitly
896 * allow reclaim from kswapd as the stack usage there is relatively low.
898 * This should really be done by the core VM, but until that happens
899 * filesystems like XFS, btrfs and ext4 have to take care of this
902 if ((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == PF_MEMALLOC)
906 * Given that we do not allow direct reclaim to call us, we should
907 * never be called while in a filesystem transaction.
909 if (WARN_ON(current->flags & PF_FSTRANS))
912 /* Is this page beyond the end of the file? */
913 offset = i_size_read(inode);
914 end_index = offset >> PAGE_CACHE_SHIFT;
915 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
916 if (page->index >= end_index) {
917 if ((page->index >= end_index + 1) ||
918 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
924 end_offset = min_t(unsigned long long,
925 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
927 len = 1 << inode->i_blkbits;
929 bh = head = page_buffers(page);
930 offset = page_offset(page);
933 if (wbc->sync_mode == WB_SYNC_NONE)
939 if (offset >= end_offset)
941 if (!buffer_uptodate(bh))
945 * set_page_dirty dirties all buffers in a page, independent
946 * of their state. The dirty state however is entirely
947 * meaningless for holes (!mapped && uptodate), so skip
948 * buffers covering holes here.
950 if (!buffer_mapped(bh) && buffer_uptodate(bh)) {
955 if (buffer_unwritten(bh)) {
956 if (type != IO_UNWRITTEN) {
960 } else if (buffer_delay(bh)) {
961 if (type != IO_DELALLOC) {
965 } else if (buffer_uptodate(bh)) {
966 if (type != IO_OVERWRITE) {
971 if (PageUptodate(page)) {
972 ASSERT(buffer_mapped(bh));
979 imap_valid = xfs_imap_valid(inode, &imap, offset);
982 * If we didn't have a valid mapping then we need to
983 * put the new mapping into a separate ioend structure.
984 * This ensures non-contiguous extents always have
985 * separate ioends, which is particularly important
986 * for unwritten extent conversion at I/O completion
990 err = xfs_map_blocks(inode, offset, &imap, type,
994 imap_valid = xfs_imap_valid(inode, &imap, offset);
998 if (type != IO_OVERWRITE)
999 xfs_map_at_offset(inode, bh, &imap, offset);
1000 xfs_add_to_ioend(inode, bh, offset, type, &ioend,
1008 } while (offset += len, ((bh = bh->b_this_page) != head));
1010 if (uptodate && bh == head)
1011 SetPageUptodate(page);
1013 xfs_start_page_writeback(page, 1, count);
1015 if (ioend && imap_valid) {
1016 xfs_off_t end_index;
1018 end_index = imap.br_startoff + imap.br_blockcount;
1021 end_index <<= inode->i_blkbits;
1024 end_index = (end_index - 1) >> PAGE_CACHE_SHIFT;
1026 /* check against file size */
1027 if (end_index > last_index)
1028 end_index = last_index;
1030 xfs_cluster_write(inode, page->index + 1, &imap, &ioend,
1035 xfs_submit_ioend(wbc, iohead);
1041 xfs_cancel_ioend(iohead);
1046 xfs_aops_discard_page(page);
1047 ClearPageUptodate(page);
1052 redirty_page_for_writepage(wbc, page);
1059 struct address_space *mapping,
1060 struct writeback_control *wbc)
1062 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
1063 return generic_writepages(mapping, wbc);
1067 * Called to move a page into cleanable state - and from there
1068 * to be released. The page should already be clean. We always
1069 * have buffer heads in this call.
1071 * Returns 1 if the page is ok to release, 0 otherwise.
1078 int delalloc, unwritten;
1080 trace_xfs_releasepage(page->mapping->host, page, 0);
1082 xfs_count_page_state(page, &delalloc, &unwritten);
1084 if (WARN_ON(delalloc))
1086 if (WARN_ON(unwritten))
1089 return try_to_free_buffers(page);
1094 struct inode *inode,
1096 struct buffer_head *bh_result,
1100 struct xfs_inode *ip = XFS_I(inode);
1101 struct xfs_mount *mp = ip->i_mount;
1102 xfs_fileoff_t offset_fsb, end_fsb;
1105 struct xfs_bmbt_irec imap;
1111 if (XFS_FORCED_SHUTDOWN(mp))
1112 return -XFS_ERROR(EIO);
1114 offset = (xfs_off_t)iblock << inode->i_blkbits;
1115 ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
1116 size = bh_result->b_size;
1118 if (!create && direct && offset >= i_size_read(inode))
1122 lockmode = XFS_ILOCK_EXCL;
1123 xfs_ilock(ip, lockmode);
1125 lockmode = xfs_ilock_map_shared(ip);
1128 ASSERT(offset <= mp->m_maxioffset);
1129 if (offset + size > mp->m_maxioffset)
1130 size = mp->m_maxioffset - offset;
1131 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
1132 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1134 error = xfs_bmapi(NULL, ip, offset_fsb, end_fsb - offset_fsb,
1135 XFS_BMAPI_ENTIRE, NULL, 0, &imap, &nimaps, NULL);
1141 (imap.br_startblock == HOLESTARTBLOCK ||
1142 imap.br_startblock == DELAYSTARTBLOCK))) {
1144 error = xfs_iomap_write_direct(ip, offset, size,
1147 error = xfs_iomap_write_delay(ip, offset, size, &imap);
1152 trace_xfs_get_blocks_alloc(ip, offset, size, 0, &imap);
1153 } else if (nimaps) {
1154 trace_xfs_get_blocks_found(ip, offset, size, 0, &imap);
1156 trace_xfs_get_blocks_notfound(ip, offset, size);
1159 xfs_iunlock(ip, lockmode);
1161 if (imap.br_startblock != HOLESTARTBLOCK &&
1162 imap.br_startblock != DELAYSTARTBLOCK) {
1164 * For unwritten extents do not report a disk address on
1165 * the read case (treat as if we're reading into a hole).
1167 if (create || !ISUNWRITTEN(&imap))
1168 xfs_map_buffer(inode, bh_result, &imap, offset);
1169 if (create && ISUNWRITTEN(&imap)) {
1171 bh_result->b_private = inode;
1172 set_buffer_unwritten(bh_result);
1177 * If this is a realtime file, data may be on a different device.
1178 * to that pointed to from the buffer_head b_bdev currently.
1180 bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
1183 * If we previously allocated a block out beyond eof and we are now
1184 * coming back to use it then we will need to flag it as new even if it
1185 * has a disk address.
1187 * With sub-block writes into unwritten extents we also need to mark
1188 * the buffer as new so that the unwritten parts of the buffer gets
1192 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
1193 (offset >= i_size_read(inode)) ||
1194 (new || ISUNWRITTEN(&imap))))
1195 set_buffer_new(bh_result);
1197 if (imap.br_startblock == DELAYSTARTBLOCK) {
1200 set_buffer_uptodate(bh_result);
1201 set_buffer_mapped(bh_result);
1202 set_buffer_delay(bh_result);
1207 * If this is O_DIRECT or the mpage code calling tell them how large
1208 * the mapping is, so that we can avoid repeated get_blocks calls.
1210 if (direct || size > (1 << inode->i_blkbits)) {
1211 xfs_off_t mapping_size;
1213 mapping_size = imap.br_startoff + imap.br_blockcount - iblock;
1214 mapping_size <<= inode->i_blkbits;
1216 ASSERT(mapping_size > 0);
1217 if (mapping_size > size)
1218 mapping_size = size;
1219 if (mapping_size > LONG_MAX)
1220 mapping_size = LONG_MAX;
1222 bh_result->b_size = mapping_size;
1228 xfs_iunlock(ip, lockmode);
1234 struct inode *inode,
1236 struct buffer_head *bh_result,
1239 return __xfs_get_blocks(inode, iblock, bh_result, create, 0);
1243 xfs_get_blocks_direct(
1244 struct inode *inode,
1246 struct buffer_head *bh_result,
1249 return __xfs_get_blocks(inode, iblock, bh_result, create, 1);
1253 * Complete a direct I/O write request.
1255 * If the private argument is non-NULL __xfs_get_blocks signals us that we
1256 * need to issue a transaction to convert the range from unwritten to written
1257 * extents. In case this is regular synchronous I/O we just call xfs_end_io
1258 * to do this and we are done. But in case this was a successful AIO
1259 * request this handler is called from interrupt context, from which we
1260 * can't start transactions. In that case offload the I/O completion to
1261 * the workqueues we also use for buffered I/O completion.
1264 xfs_end_io_direct_write(
1272 struct xfs_ioend *ioend = iocb->private;
1275 * blockdev_direct_IO can return an error even after the I/O
1276 * completion handler was called. Thus we need to protect
1277 * against double-freeing.
1279 iocb->private = NULL;
1281 ioend->io_offset = offset;
1282 ioend->io_size = size;
1283 ioend->io_iocb = iocb;
1284 ioend->io_result = ret;
1285 if (private && size > 0)
1286 ioend->io_type = IO_UNWRITTEN;
1289 ioend->io_isasync = 1;
1290 xfs_finish_ioend(ioend);
1292 xfs_finish_ioend_sync(ioend);
1300 const struct iovec *iov,
1302 unsigned long nr_segs)
1304 struct inode *inode = iocb->ki_filp->f_mapping->host;
1305 struct block_device *bdev = xfs_find_bdev_for_inode(inode);
1309 iocb->private = xfs_alloc_ioend(inode, IO_DIRECT);
1311 ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
1313 xfs_get_blocks_direct,
1314 xfs_end_io_direct_write, NULL, 0);
1315 if (ret != -EIOCBQUEUED && iocb->private)
1316 xfs_destroy_ioend(iocb->private);
1318 ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
1320 xfs_get_blocks_direct,
1328 xfs_vm_write_failed(
1329 struct address_space *mapping,
1332 struct inode *inode = mapping->host;
1334 if (to > inode->i_size) {
1336 * punch out the delalloc blocks we have already allocated. We
1337 * don't call xfs_setattr() to do this as we may be in the
1338 * middle of a multi-iovec write and so the vfs inode->i_size
1339 * will not match the xfs ip->i_size and so it will zero too
1340 * much. Hence we jus truncate the page cache to zero what is
1341 * necessary and punch the delalloc blocks directly.
1343 struct xfs_inode *ip = XFS_I(inode);
1344 xfs_fileoff_t start_fsb;
1345 xfs_fileoff_t end_fsb;
1348 truncate_pagecache(inode, to, inode->i_size);
1351 * Check if there are any blocks that are outside of i_size
1352 * that need to be trimmed back.
1354 start_fsb = XFS_B_TO_FSB(ip->i_mount, inode->i_size) + 1;
1355 end_fsb = XFS_B_TO_FSB(ip->i_mount, to);
1356 if (end_fsb <= start_fsb)
1359 xfs_ilock(ip, XFS_ILOCK_EXCL);
1360 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1361 end_fsb - start_fsb);
1363 /* something screwed, just bail */
1364 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1365 xfs_alert(ip->i_mount,
1366 "xfs_vm_write_failed: unable to clean up ino %lld",
1370 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1377 struct address_space *mapping,
1381 struct page **pagep,
1386 ret = block_write_begin(mapping, pos, len, flags | AOP_FLAG_NOFS,
1387 pagep, xfs_get_blocks);
1389 xfs_vm_write_failed(mapping, pos + len);
1396 struct address_space *mapping,
1405 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
1406 if (unlikely(ret < len))
1407 xfs_vm_write_failed(mapping, pos + len);
1413 struct address_space *mapping,
1416 struct inode *inode = (struct inode *)mapping->host;
1417 struct xfs_inode *ip = XFS_I(inode);
1419 trace_xfs_vm_bmap(XFS_I(inode));
1420 xfs_ilock(ip, XFS_IOLOCK_SHARED);
1421 xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF);
1422 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
1423 return generic_block_bmap(mapping, block, xfs_get_blocks);
1428 struct file *unused,
1431 return mpage_readpage(page, xfs_get_blocks);
1436 struct file *unused,
1437 struct address_space *mapping,
1438 struct list_head *pages,
1441 return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
1444 const struct address_space_operations xfs_address_space_operations = {
1445 .readpage = xfs_vm_readpage,
1446 .readpages = xfs_vm_readpages,
1447 .writepage = xfs_vm_writepage,
1448 .writepages = xfs_vm_writepages,
1449 .releasepage = xfs_vm_releasepage,
1450 .invalidatepage = xfs_vm_invalidatepage,
1451 .write_begin = xfs_vm_write_begin,
1452 .write_end = xfs_vm_write_end,
1453 .bmap = xfs_vm_bmap,
1454 .direct_IO = xfs_vm_direct_IO,
1455 .migratepage = buffer_migrate_page,
1456 .is_partially_uptodate = block_is_partially_uptodate,
1457 .error_remove_page = generic_error_remove_page,