Avoid beyond bounds copy while caching ACL
[zen-stable.git] / fs / xfs / xfs_aops.c
blob74b9baf36ac39038f827c8e262ea62aa33d81de8
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
18 #include "xfs.h"
19 #include "xfs_bit.h"
20 #include "xfs_log.h"
21 #include "xfs_inum.h"
22 #include "xfs_sb.h"
23 #include "xfs_ag.h"
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"
31 #include "xfs_rw.h"
32 #include "xfs_iomap.h"
33 #include "xfs_vnodeops.h"
34 #include "xfs_trace.h"
35 #include "xfs_bmap.h"
36 #include <linux/gfp.h>
37 #include <linux/mpage.h>
38 #include <linux/pagevec.h>
39 #include <linux/writeback.h>
41 void
42 xfs_count_page_state(
43 struct page *page,
44 int *delalloc,
45 int *unwritten)
47 struct buffer_head *bh, *head;
49 *delalloc = *unwritten = 0;
51 bh = head = page_buffers(page);
52 do {
53 if (buffer_unwritten(bh))
54 (*unwritten) = 1;
55 else if (buffer_delay(bh))
56 (*delalloc) = 1;
57 } while ((bh = bh->b_this_page) != head);
60 STATIC struct block_device *
61 xfs_find_bdev_for_inode(
62 struct inode *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;
69 else
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.
79 STATIC void
80 xfs_destroy_ioend(
81 xfs_ioend_t *ioend)
83 struct buffer_head *bh, *next;
85 for (bh = ioend->io_buffer_head; bh; bh = next) {
86 next = bh->b_private;
87 bh->b_end_io(bh, !ioend->io_error);
90 if (ioend->io_iocb) {
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.
105 STATIC xfs_fsize_t
106 xfs_ioend_new_eof(
107 xfs_ioend_t *ioend)
109 xfs_inode_t *ip = XFS_I(ioend->io_inode);
110 xfs_fsize_t isize;
111 xfs_fsize_t bsize;
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.
134 STATIC int
135 xfs_setfilesize(
136 xfs_ioend_t *ioend)
138 xfs_inode_t *ip = XFS_I(ioend->io_inode);
139 xfs_fsize_t isize;
141 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL))
142 return EAGAIN;
144 isize = xfs_ioend_new_eof(ioend);
145 if (isize) {
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);
152 return 0;
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
159 * ioend right now.
161 STATIC void
162 xfs_finish_ioend(
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);
170 else
171 xfs_destroy_ioend(ioend);
176 * IO write completion.
178 STATIC void
179 xfs_end_io(
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);
184 int error = 0;
186 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
187 ioend->io_error = -EIO;
188 goto done;
190 if (ioend->io_error)
191 goto done;
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,
199 ioend->io_size);
200 if (error) {
201 ioend->io_error = -error;
202 goto done;
207 * We might have to update the on-disk file size after extending
208 * writes.
210 error = xfs_setfilesize(ioend);
211 ASSERT(!error || error == EAGAIN);
213 done:
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
217 * it.
219 if (error == EAGAIN) {
220 atomic_inc(&ioend->io_remaining);
221 xfs_finish_ioend(ioend);
222 /* ensure we don't spin on blocked ioends */
223 delay(1);
224 } else {
225 xfs_destroy_ioend(ioend);
230 * Call IO completion handling in caller context on the final put of an ioend.
232 STATIC void
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
244 * (vs. incore size).
246 STATIC xfs_ioend_t *
247 xfs_alloc_ioend(
248 struct inode *inode,
249 unsigned int type)
251 xfs_ioend_t *ioend;
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;
262 ioend->io_error = 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;
269 ioend->io_size = 0;
270 ioend->io_iocb = NULL;
271 ioend->io_result = 0;
273 INIT_WORK(&ioend->io_work, xfs_end_io);
274 return ioend;
277 STATIC int
278 xfs_map_blocks(
279 struct inode *inode,
280 loff_t offset,
281 struct xfs_bmbt_irec *imap,
282 int type,
283 int nonblocking)
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;
289 int error = 0;
290 int bmapi_flags = XFS_BMAPI_ENTIRE;
291 int nimaps = 1;
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)) {
300 if (nonblocking)
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);
317 if (error)
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);
323 if (!error)
324 trace_xfs_map_blocks_alloc(ip, offset, count, type, imap);
325 return -XFS_ERROR(error);
328 #ifdef DEBUG
329 if (type == IO_UNWRITTEN) {
330 ASSERT(nimaps);
331 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
332 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
334 #endif
335 if (nimaps)
336 trace_xfs_map_blocks_found(ip, offset, count, type, imap);
337 return 0;
340 STATIC int
341 xfs_imap_valid(
342 struct inode *inode,
343 struct xfs_bmbt_irec *imap,
344 xfs_off_t offset)
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.
355 STATIC void
356 xfs_end_bio(
357 struct bio *bio,
358 int error)
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;
368 bio_put(bio);
370 xfs_finish_ioend(ioend);
373 STATIC void
374 xfs_submit_ioend_bio(
375 struct writeback_control *wbc,
376 xfs_ioend_t *ioend,
377 struct bio *bio)
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);
393 STATIC struct bio *
394 xfs_alloc_ioend_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;
403 return bio;
406 STATIC void
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);
420 STATIC void
421 xfs_start_page_writeback(
422 struct page *page,
423 int clear_dirty,
424 int buffers)
426 ASSERT(PageLocked(page));
427 ASSERT(!PageWriteback(page));
428 if (clear_dirty)
429 clear_page_dirty_for_io(page);
430 set_page_writeback(page);
431 unlock_page(page);
432 /* If no buffers on the page are to be written, finish it here */
433 if (!buffers)
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.
459 STATIC void
460 xfs_submit_ioend(
461 struct writeback_control *wbc,
462 xfs_ioend_t *ioend)
464 xfs_ioend_t *head = ioend;
465 xfs_ioend_t *next;
466 struct buffer_head *bh;
467 struct bio *bio;
468 sector_t lastblock = 0;
470 /* Pass 1 - start writeback */
471 do {
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 */
478 ioend = head;
479 do {
480 next = ioend->io_list;
481 bio = NULL;
483 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
485 if (!bio) {
486 retry:
487 bio = xfs_alloc_ioend_bio(bh);
488 } else if (bh->b_blocknr != lastblock + 1) {
489 xfs_submit_ioend_bio(wbc, ioend, bio);
490 goto retry;
493 if (bio_add_buffer(bio, bh) != bh->b_size) {
494 xfs_submit_ioend_bio(wbc, ioend, bio);
495 goto retry;
498 lastblock = bh->b_blocknr;
500 if (bio)
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.
511 STATIC void
512 xfs_cancel_ioend(
513 xfs_ioend_t *ioend)
515 xfs_ioend_t *next;
516 struct buffer_head *bh, *next_bh;
518 do {
519 next = ioend->io_list;
520 bh = ioend->io_buffer_head;
521 do {
522 next_bh = bh->b_private;
523 clear_buffer_async_write(bh);
524 unlock_buffer(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.
537 STATIC void
538 xfs_add_to_ioend(
539 struct inode *inode,
540 struct buffer_head *bh,
541 xfs_off_t offset,
542 unsigned int type,
543 xfs_ioend_t **result,
544 int need_ioend)
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;
555 if (previous)
556 previous->io_list = ioend;
557 *result = ioend;
558 } else {
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;
567 STATIC void
568 xfs_map_buffer(
569 struct inode *inode,
570 struct buffer_head *bh,
571 struct xfs_bmbt_irec *imap,
572 xfs_off_t offset)
574 sector_t bn;
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)));
587 bh->b_blocknr = bn;
588 set_buffer_mapped(bh);
591 STATIC void
592 xfs_map_at_offset(
593 struct inode *inode,
594 struct buffer_head *bh,
595 struct xfs_bmbt_irec *imap,
596 xfs_off_t offset)
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.
611 STATIC int
612 xfs_is_delayed_page(
613 struct page *page,
614 unsigned int type)
616 if (PageWriteback(page))
617 return 0;
619 if (page->mapping && page_has_buffers(page)) {
620 struct buffer_head *bh, *head;
621 int acceptable = 0;
623 bh = head = page_buffers(page);
624 do {
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);
631 else
632 break;
633 } while ((bh = bh->b_this_page) != head);
635 if (acceptable)
636 return 1;
639 return 0;
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.
648 STATIC int
649 xfs_convert_page(
650 struct inode *inode,
651 struct page *page,
652 loff_t tindex,
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;
660 unsigned int type;
661 int len, page_dirty;
662 int count = 0, done = 0, uptodate = 1;
663 xfs_off_t offset = page_offset(page);
665 if (page->index != tindex)
666 goto fail;
667 if (!trylock_page(page))
668 goto fail;
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.
680 * Derivation:
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,
691 i_size_read(inode));
693 len = 1 << inode->i_blkbits;
694 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
695 PAGE_CACHE_SIZE);
696 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
697 page_dirty = p_offset / len;
699 bh = head = page_buffers(page);
700 do {
701 if (offset >= end_offset)
702 break;
703 if (!buffer_uptodate(bh))
704 uptodate = 0;
705 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
706 done = 1;
707 continue;
710 if (buffer_unwritten(bh) || buffer_delay(bh) ||
711 buffer_mapped(bh)) {
712 if (buffer_unwritten(bh))
713 type = IO_UNWRITTEN;
714 else if (buffer_delay(bh))
715 type = IO_DELALLOC;
716 else
717 type = IO_OVERWRITE;
719 if (!xfs_imap_valid(inode, imap, offset)) {
720 done = 1;
721 continue;
724 lock_buffer(bh);
725 if (type != IO_OVERWRITE)
726 xfs_map_at_offset(inode, bh, imap, offset);
727 xfs_add_to_ioend(inode, bh, offset, type,
728 ioendp, done);
730 page_dirty--;
731 count++;
732 } else {
733 done = 1;
735 } while (offset += len, (bh = bh->b_this_page) != head);
737 if (uptodate && bh == head)
738 SetPageUptodate(page);
740 if (count) {
741 if (--wbc->nr_to_write <= 0 &&
742 wbc->sync_mode == WB_SYNC_NONE)
743 done = 1;
745 xfs_start_page_writeback(page, !page_dirty, count);
747 return done;
748 fail_unlock_page:
749 unlock_page(page);
750 fail:
751 return 1;
755 * Convert & write out a cluster of pages in the same extent as defined
756 * by mp and following the start page.
758 STATIC void
759 xfs_cluster_write(
760 struct inode *inode,
761 pgoff_t tindex,
762 struct xfs_bmbt_irec *imap,
763 xfs_ioend_t **ioendp,
764 struct writeback_control *wbc,
765 pgoff_t tlast)
767 struct pagevec pvec;
768 int done = 0, i;
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))
775 break;
777 for (i = 0; i < pagevec_count(&pvec); i++) {
778 done = xfs_convert_page(inode, pvec.pages[i], tindex++,
779 imap, ioendp, wbc);
780 if (done)
781 break;
784 pagevec_release(&pvec);
785 cond_resched();
789 STATIC void
790 xfs_vm_invalidatepage(
791 struct page *page,
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.
814 STATIC void
815 xfs_aops_discard_page(
816 struct page *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))
824 goto out_invalidate;
826 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
827 goto out_invalidate;
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);
835 do {
836 int error;
837 xfs_fileoff_t start_fsb;
839 if (!buffer_delay(bh))
840 goto next_buffer;
842 start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
843 error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1);
844 if (error) {
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.");
850 break;
852 next_buffer:
853 offset += 1 << inode->i_blkbits;
855 } while ((bh = bh->b_this_page) != head);
857 xfs_iunlock(ip, XFS_ILOCK_EXCL);
858 out_invalidate:
859 xfs_vm_invalidatepage(page, 0);
860 return;
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.
871 STATIC int
872 xfs_vm_writepage(
873 struct page *page,
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;
880 loff_t offset;
881 unsigned int type;
882 __uint64_t end_offset;
883 pgoff_t end_index, last_index;
884 ssize_t len;
885 int err, imap_valid = 0, uptodate = 1;
886 int count = 0;
887 int nonblocking = 0;
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
901 * warn about it.
903 if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
904 PF_MEMALLOC))
905 goto redirty;
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))
912 goto redirty;
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))) {
921 unlock_page(page);
922 return 0;
926 end_offset = min_t(unsigned long long,
927 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
928 offset);
929 len = 1 << inode->i_blkbits;
931 bh = head = page_buffers(page);
932 offset = page_offset(page);
933 type = IO_OVERWRITE;
935 if (wbc->sync_mode == WB_SYNC_NONE)
936 nonblocking = 1;
938 do {
939 int new_ioend = 0;
941 if (offset >= end_offset)
942 break;
943 if (!buffer_uptodate(bh))
944 uptodate = 0;
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)) {
953 imap_valid = 0;
954 continue;
957 if (buffer_unwritten(bh)) {
958 if (type != IO_UNWRITTEN) {
959 type = IO_UNWRITTEN;
960 imap_valid = 0;
962 } else if (buffer_delay(bh)) {
963 if (type != IO_DELALLOC) {
964 type = IO_DELALLOC;
965 imap_valid = 0;
967 } else if (buffer_uptodate(bh)) {
968 if (type != IO_OVERWRITE) {
969 type = IO_OVERWRITE;
970 imap_valid = 0;
972 } else {
973 if (PageUptodate(page)) {
974 ASSERT(buffer_mapped(bh));
975 imap_valid = 0;
977 continue;
980 if (imap_valid)
981 imap_valid = xfs_imap_valid(inode, &imap, offset);
982 if (!imap_valid) {
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
989 * time.
991 new_ioend = 1;
992 err = xfs_map_blocks(inode, offset, &imap, type,
993 nonblocking);
994 if (err)
995 goto error;
996 imap_valid = xfs_imap_valid(inode, &imap, offset);
998 if (imap_valid) {
999 lock_buffer(bh);
1000 if (type != IO_OVERWRITE)
1001 xfs_map_at_offset(inode, bh, &imap, offset);
1002 xfs_add_to_ioend(inode, bh, offset, type, &ioend,
1003 new_ioend);
1004 count++;
1007 if (!iohead)
1008 iohead = 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;
1022 /* to bytes */
1023 end_index <<= inode->i_blkbits;
1025 /* to pages */
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,
1033 wbc, end_index);
1036 if (iohead)
1037 xfs_submit_ioend(wbc, iohead);
1039 return 0;
1041 error:
1042 if (iohead)
1043 xfs_cancel_ioend(iohead);
1045 if (err == -EAGAIN)
1046 goto redirty;
1048 xfs_aops_discard_page(page);
1049 ClearPageUptodate(page);
1050 unlock_page(page);
1051 return err;
1053 redirty:
1054 redirty_page_for_writepage(wbc, page);
1055 unlock_page(page);
1056 return 0;
1059 STATIC int
1060 xfs_vm_writepages(
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.
1075 STATIC int
1076 xfs_vm_releasepage(
1077 struct page *page,
1078 gfp_t gfp_mask)
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))
1087 return 0;
1088 if (WARN_ON(unwritten))
1089 return 0;
1091 return try_to_free_buffers(page);
1094 STATIC int
1095 __xfs_get_blocks(
1096 struct inode *inode,
1097 sector_t iblock,
1098 struct buffer_head *bh_result,
1099 int create,
1100 int direct)
1102 struct xfs_inode *ip = XFS_I(inode);
1103 struct xfs_mount *mp = ip->i_mount;
1104 xfs_fileoff_t offset_fsb, end_fsb;
1105 int error = 0;
1106 int lockmode = 0;
1107 struct xfs_bmbt_irec imap;
1108 int nimaps = 1;
1109 xfs_off_t offset;
1110 ssize_t size;
1111 int new = 0;
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))
1121 return 0;
1123 if (create) {
1124 lockmode = XFS_ILOCK_EXCL;
1125 xfs_ilock(ip, lockmode);
1126 } else {
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);
1138 if (error)
1139 goto out_unlock;
1141 if (create &&
1142 (!nimaps ||
1143 (imap.br_startblock == HOLESTARTBLOCK ||
1144 imap.br_startblock == DELAYSTARTBLOCK))) {
1145 if (direct) {
1146 error = xfs_iomap_write_direct(ip, offset, size,
1147 &imap, nimaps);
1148 } else {
1149 error = xfs_iomap_write_delay(ip, offset, size, &imap);
1151 if (error)
1152 goto out_unlock;
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);
1157 } else {
1158 trace_xfs_get_blocks_notfound(ip, offset, size);
1159 goto out_unlock;
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)) {
1172 if (direct)
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
1191 * correctly zeroed.
1193 if (create &&
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) {
1200 BUG_ON(direct);
1201 if (create) {
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;
1227 return 0;
1229 out_unlock:
1230 xfs_iunlock(ip, lockmode);
1231 return -error;
1235 xfs_get_blocks(
1236 struct inode *inode,
1237 sector_t iblock,
1238 struct buffer_head *bh_result,
1239 int create)
1241 return __xfs_get_blocks(inode, iblock, bh_result, create, 0);
1244 STATIC int
1245 xfs_get_blocks_direct(
1246 struct inode *inode,
1247 sector_t iblock,
1248 struct buffer_head *bh_result,
1249 int create)
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.
1265 STATIC void
1266 xfs_end_io_direct_write(
1267 struct kiocb *iocb,
1268 loff_t offset,
1269 ssize_t size,
1270 void *private,
1271 int ret,
1272 bool is_async)
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;
1299 if (is_async) {
1300 ioend->io_isasync = 1;
1301 xfs_finish_ioend(ioend);
1302 } else {
1303 xfs_finish_ioend_sync(ioend);
1307 STATIC ssize_t
1308 xfs_vm_direct_IO(
1309 int rw,
1310 struct kiocb *iocb,
1311 const struct iovec *iov,
1312 loff_t offset,
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);
1317 ssize_t ret;
1319 if (rw & WRITE) {
1320 iocb->private = xfs_alloc_ioend(inode, IO_DIRECT);
1322 ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
1323 offset, nr_segs,
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);
1328 } else {
1329 ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
1330 offset, nr_segs,
1331 xfs_get_blocks_direct,
1332 NULL, NULL, 0);
1335 return ret;
1338 STATIC void
1339 xfs_vm_write_failed(
1340 struct address_space *mapping,
1341 loff_t to)
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
1351 * point.
1353 struct xfs_inode *ip = XFS_I(inode);
1354 xfs_fileoff_t start_fsb;
1355 xfs_fileoff_t end_fsb;
1356 int error;
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)
1367 return;
1369 xfs_ilock(ip, XFS_ILOCK_EXCL);
1370 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1371 end_fsb - start_fsb);
1372 if (error) {
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",
1377 ip->i_ino);
1380 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1384 STATIC int
1385 xfs_vm_write_begin(
1386 struct file *file,
1387 struct address_space *mapping,
1388 loff_t pos,
1389 unsigned len,
1390 unsigned flags,
1391 struct page **pagep,
1392 void **fsdata)
1394 int ret;
1396 ret = block_write_begin(mapping, pos, len, flags | AOP_FLAG_NOFS,
1397 pagep, xfs_get_blocks);
1398 if (unlikely(ret))
1399 xfs_vm_write_failed(mapping, pos + len);
1400 return ret;
1403 STATIC int
1404 xfs_vm_write_end(
1405 struct file *file,
1406 struct address_space *mapping,
1407 loff_t pos,
1408 unsigned len,
1409 unsigned copied,
1410 struct page *page,
1411 void *fsdata)
1413 int ret;
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);
1418 return ret;
1421 STATIC sector_t
1422 xfs_vm_bmap(
1423 struct address_space *mapping,
1424 sector_t block)
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);
1436 STATIC int
1437 xfs_vm_readpage(
1438 struct file *unused,
1439 struct page *page)
1441 return mpage_readpage(page, xfs_get_blocks);
1444 STATIC int
1445 xfs_vm_readpages(
1446 struct file *unused,
1447 struct address_space *mapping,
1448 struct list_head *pages,
1449 unsigned nr_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,