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
26 #include "xfs_trans.h"
27 #include "xfs_dmapi.h"
28 #include "xfs_mount.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_dir_sf.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_alloc.h"
38 #include "xfs_btree.h"
39 #include "xfs_error.h"
41 #include "xfs_iomap.h"
42 #include <linux/mpage.h>
43 #include <linux/writeback.h>
45 STATIC void xfs_count_page_state(struct page *, int *, int *, int *);
47 #if defined(XFS_RW_TRACE)
57 vnode_t *vp = LINVFS_GET_VP(inode);
58 loff_t isize = i_size_read(inode);
59 loff_t offset = page_offset(page);
60 int delalloc = -1, unmapped = -1, unwritten = -1;
62 if (page_has_buffers(page))
63 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
65 bdp = vn_bhv_lookup(VN_BHV_HEAD(vp), &xfs_vnodeops);
70 ktrace_enter(ip->i_rwtrace,
71 (void *)((unsigned long)tag),
75 (void *)((unsigned long)mask),
76 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
77 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
78 (void *)((unsigned long)((isize >> 32) & 0xffffffff)),
79 (void *)((unsigned long)(isize & 0xffffffff)),
80 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
81 (void *)((unsigned long)(offset & 0xffffffff)),
82 (void *)((unsigned long)delalloc),
83 (void *)((unsigned long)unmapped),
84 (void *)((unsigned long)unwritten),
89 #define xfs_page_trace(tag, inode, page, mask)
93 * Schedule IO completion handling on a xfsdatad if this was
94 * the final hold on this ioend.
100 if (atomic_dec_and_test(&ioend->io_remaining))
101 queue_work(xfsdatad_workqueue, &ioend->io_work);
105 * We're now finished for good with this ioend structure.
106 * Update the page state via the associated buffer_heads,
107 * release holds on the inode and bio, and finally free
108 * up memory. Do not use the ioend after this.
114 struct buffer_head *bh, *next;
116 for (bh = ioend->io_buffer_head; bh; bh = next) {
117 next = bh->b_private;
118 bh->b_end_io(bh, ioend->io_uptodate);
121 vn_iowake(ioend->io_vnode);
122 mempool_free(ioend, xfs_ioend_pool);
126 * Buffered IO write completion for delayed allocate extents.
127 * TODO: Update ondisk isize now that we know the file data
128 * has been flushed (i.e. the notorious "NULL file" problem).
131 xfs_end_bio_delalloc(
134 xfs_ioend_t *ioend = data;
136 xfs_destroy_ioend(ioend);
140 * Buffered IO write completion for regular, written extents.
146 xfs_ioend_t *ioend = data;
148 xfs_destroy_ioend(ioend);
152 * IO write completion for unwritten extents.
154 * Issue transactions to convert a buffer range from unwritten
155 * to written extents.
158 xfs_end_bio_unwritten(
161 xfs_ioend_t *ioend = data;
162 vnode_t *vp = ioend->io_vnode;
163 xfs_off_t offset = ioend->io_offset;
164 size_t size = ioend->io_size;
167 if (ioend->io_uptodate)
168 VOP_BMAP(vp, offset, size, BMAPI_UNWRITTEN, NULL, NULL, error);
169 xfs_destroy_ioend(ioend);
173 * Allocate and initialise an IO completion structure.
174 * We need to track unwritten extent write completion here initially.
175 * We'll need to extend this for updating the ondisk inode size later
185 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
188 * Set the count to 1 initially, which will prevent an I/O
189 * completion callback from happening before we have started
190 * all the I/O from calling the completion routine too early.
192 atomic_set(&ioend->io_remaining, 1);
193 ioend->io_uptodate = 1; /* cleared if any I/O fails */
194 ioend->io_list = NULL;
195 ioend->io_type = type;
196 ioend->io_vnode = LINVFS_GET_VP(inode);
197 ioend->io_buffer_head = NULL;
198 ioend->io_buffer_tail = NULL;
199 atomic_inc(&ioend->io_vnode->v_iocount);
200 ioend->io_offset = 0;
203 if (type == IOMAP_UNWRITTEN)
204 INIT_WORK(&ioend->io_work, xfs_end_bio_unwritten, ioend);
205 else if (type == IOMAP_DELAY)
206 INIT_WORK(&ioend->io_work, xfs_end_bio_delalloc, ioend);
208 INIT_WORK(&ioend->io_work, xfs_end_bio_written, ioend);
221 vnode_t *vp = LINVFS_GET_VP(inode);
222 int error, nmaps = 1;
224 VOP_BMAP(vp, offset, count, flags, mapp, &nmaps, error);
225 if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)))
231 * Finds the corresponding mapping in block @map array of the
232 * given @offset within a @page.
238 unsigned long offset)
240 xfs_off_t full_offset; /* offset from start of file */
242 ASSERT(offset < PAGE_CACHE_SIZE);
244 full_offset = page->index; /* NB: using 64bit number */
245 full_offset <<= PAGE_CACHE_SHIFT; /* offset from file start */
246 full_offset += offset; /* offset from page start */
248 if (full_offset < iomapp->iomap_offset)
250 if (iomapp->iomap_offset + (iomapp->iomap_bsize -1) >= full_offset)
256 * BIO completion handler for buffered IO.
261 unsigned int bytes_done,
264 xfs_ioend_t *ioend = bio->bi_private;
270 ASSERT(atomic_read(&bio->bi_cnt) >= 1);
272 /* Toss bio and pass work off to an xfsdatad thread */
273 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
274 ioend->io_uptodate = 0;
275 bio->bi_private = NULL;
276 bio->bi_end_io = NULL;
279 xfs_finish_ioend(ioend);
284 xfs_submit_ioend_bio(
288 atomic_inc(&ioend->io_remaining);
290 bio->bi_private = ioend;
291 bio->bi_end_io = xfs_end_bio;
293 submit_bio(WRITE, bio);
294 ASSERT(!bio_flagged(bio, BIO_EOPNOTSUPP));
300 struct buffer_head *bh)
303 int nvecs = bio_get_nr_vecs(bh->b_bdev);
306 bio = bio_alloc(GFP_NOIO, nvecs);
310 ASSERT(bio->bi_private == NULL);
311 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
312 bio->bi_bdev = bh->b_bdev;
318 xfs_start_buffer_writeback(
319 struct buffer_head *bh)
321 ASSERT(buffer_mapped(bh));
322 ASSERT(buffer_locked(bh));
323 ASSERT(!buffer_delay(bh));
324 ASSERT(!buffer_unwritten(bh));
326 mark_buffer_async_write(bh);
327 set_buffer_uptodate(bh);
328 clear_buffer_dirty(bh);
332 xfs_start_page_writeback(
334 struct writeback_control *wbc,
338 ASSERT(PageLocked(page));
339 ASSERT(!PageWriteback(page));
340 set_page_writeback(page);
342 clear_page_dirty(page);
345 end_page_writeback(page);
346 wbc->pages_skipped++; /* We didn't write this page */
350 static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
352 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
356 * Submit all of the bios for all of the ioends we have saved up,
357 * covering the initial writepage page and also any probed pages.
364 struct buffer_head *bh;
366 sector_t lastblock = 0;
369 next = ioend->io_list;
372 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
373 xfs_start_buffer_writeback(bh);
377 bio = xfs_alloc_ioend_bio(bh);
378 } else if (bh->b_blocknr != lastblock + 1) {
379 xfs_submit_ioend_bio(ioend, bio);
383 if (bio_add_buffer(bio, bh) != bh->b_size) {
384 xfs_submit_ioend_bio(ioend, bio);
388 lastblock = bh->b_blocknr;
391 xfs_submit_ioend_bio(ioend, bio);
392 xfs_finish_ioend(ioend);
393 } while ((ioend = next) != NULL);
397 * Cancel submission of all buffer_heads so far in this endio.
398 * Toss the endio too. Only ever called for the initial page
399 * in a writepage request, so only ever one page.
406 struct buffer_head *bh, *next_bh;
409 next = ioend->io_list;
410 bh = ioend->io_buffer_head;
412 next_bh = bh->b_private;
413 clear_buffer_async_write(bh);
415 } while ((bh = next_bh) != NULL);
417 vn_iowake(ioend->io_vnode);
418 mempool_free(ioend, xfs_ioend_pool);
419 } while ((ioend = next) != NULL);
423 * Test to see if we've been building up a completion structure for
424 * earlier buffers -- if so, we try to append to this ioend if we
425 * can, otherwise we finish off any current ioend and start another.
426 * Return true if we've finished the given ioend.
431 struct buffer_head *bh,
432 unsigned int p_offset,
434 xfs_ioend_t **result,
437 xfs_ioend_t *ioend = *result;
439 if (!ioend || need_ioend || type != ioend->io_type) {
440 xfs_ioend_t *previous = *result;
443 offset = (xfs_off_t)bh->b_page->index << PAGE_CACHE_SHIFT;
445 ioend = xfs_alloc_ioend(inode, type);
446 ioend->io_offset = offset;
447 ioend->io_buffer_head = bh;
448 ioend->io_buffer_tail = bh;
450 previous->io_list = ioend;
453 ioend->io_buffer_tail->b_private = bh;
454 ioend->io_buffer_tail = bh;
457 bh->b_private = NULL;
458 ioend->io_size += bh->b_size;
464 struct buffer_head *bh,
465 unsigned long offset,
474 ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
475 ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
476 ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL);
479 delta <<= PAGE_CACHE_SHIFT;
481 delta -= iomapp->iomap_offset;
482 delta >>= block_bits;
484 sector_shift = block_bits - BBSHIFT;
485 bn = iomapp->iomap_bn >> sector_shift;
487 BUG_ON(!bn && !(iomapp->iomap_flags & IOMAP_REALTIME));
488 ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
492 bh->b_bdev = iomapp->iomap_target->bt_bdev;
493 set_buffer_mapped(bh);
494 clear_buffer_delay(bh);
495 clear_buffer_unwritten(bh);
499 * Look for a page at index which is unlocked and not mapped
500 * yet - clustering for mmap write case.
503 xfs_probe_unmapped_page(
504 struct address_space *mapping,
506 unsigned int pg_offset)
511 page = find_trylock_page(mapping, index);
514 if (PageWriteback(page))
517 if (page->mapping && PageDirty(page)) {
518 if (page_has_buffers(page)) {
519 struct buffer_head *bh, *head;
521 bh = head = page_buffers(page);
523 if (buffer_mapped(bh) || !buffer_uptodate(bh))
526 if (ret >= pg_offset)
528 } while ((bh = bh->b_this_page) != head);
530 ret = PAGE_CACHE_SIZE;
539 xfs_probe_unmapped_cluster(
541 struct page *startpage,
542 struct buffer_head *bh,
543 struct buffer_head *head)
545 size_t len, total = 0;
546 pgoff_t tindex, tlast, tloff;
547 unsigned int pg_offset;
548 struct address_space *mapping = inode->i_mapping;
550 /* First sum forwards in this page */
552 if (buffer_mapped(bh))
555 } while ((bh = bh->b_this_page) != head);
557 /* If we reached the end of the page, sum forwards in
561 tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
562 /* Prune this back to avoid pathological behavior */
563 tloff = min(tlast, startpage->index + 64);
564 for (tindex = startpage->index + 1; tindex < tloff; tindex++) {
565 len = xfs_probe_unmapped_page(mapping, tindex,
571 if (tindex == tlast &&
572 (pg_offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
573 total += xfs_probe_unmapped_page(mapping,
581 * Probe for a given page (index) in the inode and test if it is suitable
582 * for writing as part of an unwritten or delayed allocate extent.
583 * Returns page locked and with an extra reference count if so, else NULL.
586 xfs_probe_delayed_page(
593 page = find_trylock_page(inode->i_mapping, index);
596 if (PageWriteback(page))
599 if (page->mapping && page_has_buffers(page)) {
600 struct buffer_head *bh, *head;
603 bh = head = page_buffers(page);
605 if (buffer_unwritten(bh))
606 acceptable = (type == IOMAP_UNWRITTEN);
607 else if (buffer_delay(bh))
608 acceptable = (type == IOMAP_DELAY);
611 } while ((bh = bh->b_this_page) != head);
623 * Allocate & map buffers for page given the extent map. Write it out.
624 * except for the original page of a writepage, this is called on
625 * delalloc/unwritten pages only, for the original page it is possible
626 * that the page has no mapping at all.
633 xfs_ioend_t **ioendp,
634 struct writeback_control *wbc,
639 struct buffer_head *bh, *head;
640 xfs_iomap_t *mp = iomapp, *tmp;
641 unsigned long p_offset, end_offset;
643 int bbits = inode->i_blkbits;
645 int count = 0, done = 0, uptodate = 1;
647 end_offset = (i_size_read(inode) & (PAGE_CACHE_SIZE - 1));
650 * page_dirty is initially a count of buffers on the page before
651 * EOF and is decrememted as we move each into a cleanable state.
653 len = 1 << inode->i_blkbits;
654 end_offset = max(end_offset, PAGE_CACHE_SIZE);
655 end_offset = roundup(end_offset, len);
656 page_dirty = end_offset / len;
659 bh = head = page_buffers(page);
661 if (p_offset >= end_offset)
663 if (!buffer_uptodate(bh))
665 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
670 if (buffer_unwritten(bh))
671 type = IOMAP_UNWRITTEN;
672 else if (buffer_delay(bh))
676 if (!(buffer_mapped(bh) && all_bh && startio)) {
678 } else if (startio) {
680 xfs_add_to_ioend(inode, bh, p_offset,
687 tmp = xfs_offset_to_map(page, mp, p_offset);
692 ASSERT(!(tmp->iomap_flags & IOMAP_HOLE));
693 ASSERT(!(tmp->iomap_flags & IOMAP_DELAY));
695 xfs_map_at_offset(page, bh, p_offset, bbits, tmp, *ioendp);
697 xfs_add_to_ioend(inode, bh, p_offset,
701 set_buffer_dirty(bh);
703 mark_buffer_dirty(bh);
706 } while (p_offset += len, (bh = bh->b_this_page) != head);
708 if (uptodate && bh == head)
709 SetPageUptodate(page);
714 xfs_start_page_writeback(page, wbc, !page_dirty, count);
721 * Convert & write out a cluster of pages in the same extent as defined
722 * by mp and following the start page.
729 xfs_ioend_t **ioendp,
730 struct writeback_control *wbc,
736 unsigned int type = (*ioendp)->io_type;
739 for (done = 0; tindex <= tlast && !done; tindex++) {
740 page = xfs_probe_delayed_page(inode, tindex, type);
743 done = xfs_convert_page(inode, page, iomapp, ioendp,
744 wbc, NULL, startio, all_bh);
749 * Calling this without startio set means we are being asked to make a dirty
750 * page ready for freeing it's buffers. When called with startio set then
751 * we are coming from writepage.
753 * When called with startio set it is important that we write the WHOLE
755 * The bh->b_state's cannot know if any of the blocks or which block for
756 * that matter are dirty due to mmap writes, and therefore bh uptodate is
757 * only vaild if the page itself isn't completely uptodate. Some layers
758 * may clear the page dirty flag prior to calling write page, under the
759 * assumption the entire page will be written out; by not writing out the
760 * whole page the page can be reused before all valid dirty data is
761 * written out. Note: in the case of a page that has been dirty'd by
762 * mapwrite and but partially setup by block_prepare_write the
763 * bh->b_states's will not agree and only ones setup by BPW/BCW will have
764 * valid state, thus the whole page must be written out thing.
768 xfs_page_state_convert(
771 struct writeback_control *wbc,
773 int unmapped) /* also implies page uptodate */
775 struct buffer_head *bh, *head;
776 xfs_iomap_t *iomp, iomap;
777 xfs_ioend_t *ioend = NULL, *iohead = NULL;
779 unsigned long p_offset = 0;
781 __uint64_t end_offset;
782 pgoff_t end_index, last_index, tlast;
783 int flags, len, err, done = 1;
785 int page_dirty, count = 0, trylock_flag = 0;
787 /* wait for other IO threads? */
788 if (startio && wbc->sync_mode != WB_SYNC_NONE)
789 trylock_flag |= BMAPI_TRYLOCK;
791 /* Is this page beyond the end of the file? */
792 offset = i_size_read(inode);
793 end_index = offset >> PAGE_CACHE_SHIFT;
794 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
795 if (page->index >= end_index) {
796 if ((page->index >= end_index + 1) ||
797 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
805 * page_dirty is initially a count of buffers on the page before
806 * EOF and is decrememted as we move each into a cleanable state.
810 * End offset is the highest offset that this page should represent.
811 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
812 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
813 * hence give us the correct page_dirty count. On any other page,
814 * it will be zero and in that case we need page_dirty to be the
815 * count of buffers on the page.
817 end_offset = min_t(unsigned long long,
818 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
819 len = 1 << inode->i_blkbits;
820 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
822 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
823 page_dirty = p_offset / len;
826 bh = head = page_buffers(page);
827 offset = page_offset(page);
829 /* TODO: fix up "done" variable and iomap pointer (boolean) */
830 /* TODO: cleanup count and page_dirty */
833 if (offset >= end_offset)
835 if (!buffer_uptodate(bh))
837 if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
843 iomp = xfs_offset_to_map(page, &iomap, p_offset);
844 done = (iomp == NULL);
848 * First case, map an unwritten extent and prepare for
849 * extent state conversion transaction on completion.
851 * Second case, allocate space for a delalloc buffer.
852 * We can return EAGAIN here in the release page case.
854 if (buffer_unwritten(bh) || buffer_delay(bh)) {
855 if (buffer_unwritten(bh)) {
856 type = IOMAP_UNWRITTEN;
857 flags = BMAPI_WRITE|BMAPI_IGNSTATE;
860 flags = BMAPI_ALLOCATE;
862 flags |= trylock_flag;
867 err = xfs_map_blocks(inode, offset, len, &iomap,
871 iomp = xfs_offset_to_map(page, &iomap,
873 done = (iomp == NULL);
876 xfs_map_at_offset(page, bh, p_offset,
877 inode->i_blkbits, iomp, ioend);
879 xfs_add_to_ioend(inode, bh, p_offset,
882 set_buffer_dirty(bh);
884 mark_buffer_dirty(bh);
891 } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
892 (unmapped || startio)) {
895 if (!buffer_mapped(bh)) {
898 * Getting here implies an unmapped buffer
899 * was found, and we are in a path where we
900 * need to write the whole page out.
905 size = xfs_probe_unmapped_cluster(
906 inode, page, bh, head);
907 err = xfs_map_blocks(inode, offset,
909 BMAPI_WRITE|BMAPI_MMAP);
913 iomp = xfs_offset_to_map(page, &iomap,
915 done = (iomp == NULL);
918 xfs_map_at_offset(page, bh, p_offset,
919 inode->i_blkbits, iomp,
922 xfs_add_to_ioend(inode,
926 set_buffer_dirty(bh);
928 mark_buffer_dirty(bh);
935 } else if (startio) {
936 if (buffer_uptodate(bh) &&
937 !test_and_set_bit(BH_Lock, &bh->b_state)) {
938 ASSERT(buffer_mapped(bh));
939 xfs_add_to_ioend(inode,
955 } while (offset += len, ((bh = bh->b_this_page) != head));
957 if (uptodate && bh == head)
958 SetPageUptodate(page);
961 xfs_start_page_writeback(page, wbc, 1, count);
963 if (ioend && iomp && !done) {
964 offset = (iomp->iomap_offset + iomp->iomap_bsize - 1) >>
966 tlast = min_t(pgoff_t, offset, last_index);
967 xfs_cluster_write(inode, page->index + 1, iomp, &ioend,
968 wbc, startio, unmapped, tlast);
972 xfs_submit_ioend(iohead);
978 xfs_cancel_ioend(iohead);
981 * If it's delalloc and we have nowhere to put it,
982 * throw it away, unless the lower layers told
985 if (err != -EAGAIN) {
987 block_invalidatepage(page, 0);
988 ClearPageUptodate(page);
997 unsigned long blocks,
998 struct buffer_head *bh_result,
1001 bmapi_flags_t flags)
1003 vnode_t *vp = LINVFS_GET_VP(inode);
1010 offset = (xfs_off_t)iblock << inode->i_blkbits;
1012 size = (ssize_t) min_t(xfs_off_t, LONG_MAX,
1013 (xfs_off_t)blocks << inode->i_blkbits);
1015 size = 1 << inode->i_blkbits;
1017 VOP_BMAP(vp, offset, size,
1018 create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
1025 if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
1029 /* For unwritten extents do not report a disk address on
1030 * the read case (treat as if we're reading into a hole).
1032 if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1033 delta = offset - iomap.iomap_offset;
1034 delta >>= inode->i_blkbits;
1036 bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
1038 BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME));
1039 bh_result->b_blocknr = bn;
1040 set_buffer_mapped(bh_result);
1042 if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1044 bh_result->b_private = inode;
1045 set_buffer_unwritten(bh_result);
1046 set_buffer_delay(bh_result);
1050 /* If this is a realtime file, data might be on a new device */
1051 bh_result->b_bdev = iomap.iomap_target->bt_bdev;
1053 /* If we previously allocated a block out beyond eof and
1054 * we are now coming back to use it then we will need to
1055 * flag it as new even if it has a disk address.
1058 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
1059 (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW)))
1060 set_buffer_new(bh_result);
1062 if (iomap.iomap_flags & IOMAP_DELAY) {
1065 set_buffer_uptodate(bh_result);
1066 set_buffer_mapped(bh_result);
1067 set_buffer_delay(bh_result);
1072 ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
1073 offset = min_t(xfs_off_t,
1074 iomap.iomap_bsize - iomap.iomap_delta,
1075 (xfs_off_t)blocks << inode->i_blkbits);
1076 bh_result->b_size = (u32) min_t(xfs_off_t, UINT_MAX, offset);
1084 struct inode *inode,
1086 struct buffer_head *bh_result,
1089 return __linvfs_get_block(inode, iblock, 0, bh_result,
1090 create, 0, BMAPI_WRITE);
1094 linvfs_get_blocks_direct(
1095 struct inode *inode,
1097 unsigned long max_blocks,
1098 struct buffer_head *bh_result,
1101 return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
1102 create, 1, BMAPI_WRITE|BMAPI_DIRECT);
1106 linvfs_end_io_direct(
1112 xfs_ioend_t *ioend = iocb->private;
1115 * Non-NULL private data means we need to issue a transaction to
1116 * convert a range from unwritten to written extents. This needs
1117 * to happen from process contect but aio+dio I/O completion
1118 * happens from irq context so we need to defer it to a workqueue.
1119 * This is not nessecary for synchronous direct I/O, but we do
1120 * it anyway to keep the code uniform and simpler.
1122 * The core direct I/O code might be changed to always call the
1123 * completion handler in the future, in which case all this can
1126 if (private && size > 0) {
1127 ioend->io_offset = offset;
1128 ioend->io_size = size;
1129 xfs_finish_ioend(ioend);
1132 xfs_destroy_ioend(ioend);
1136 * blockdev_direct_IO can return an error even afer the I/O
1137 * completion handler was called. Thus we need to protect
1138 * against double-freeing.
1140 iocb->private = NULL;
1147 const struct iovec *iov,
1149 unsigned long nr_segs)
1151 struct file *file = iocb->ki_filp;
1152 struct inode *inode = file->f_mapping->host;
1153 vnode_t *vp = LINVFS_GET_VP(inode);
1159 VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
1163 iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
1165 ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
1166 iomap.iomap_target->bt_bdev,
1167 iov, offset, nr_segs,
1168 linvfs_get_blocks_direct,
1169 linvfs_end_io_direct);
1171 if (unlikely(ret <= 0 && iocb->private))
1172 xfs_destroy_ioend(iocb->private);
1179 struct address_space *mapping,
1182 struct inode *inode = (struct inode *)mapping->host;
1183 vnode_t *vp = LINVFS_GET_VP(inode);
1186 vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
1188 VOP_RWLOCK(vp, VRWLOCK_READ);
1189 VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
1190 VOP_RWUNLOCK(vp, VRWLOCK_READ);
1191 return generic_block_bmap(mapping, block, linvfs_get_block);
1196 struct file *unused,
1199 return mpage_readpage(page, linvfs_get_block);
1204 struct file *unused,
1205 struct address_space *mapping,
1206 struct list_head *pages,
1209 return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
1213 xfs_count_page_state(
1219 struct buffer_head *bh, *head;
1221 *delalloc = *unmapped = *unwritten = 0;
1223 bh = head = page_buffers(page);
1225 if (buffer_uptodate(bh) && !buffer_mapped(bh))
1227 else if (buffer_unwritten(bh) && !buffer_delay(bh))
1228 clear_buffer_unwritten(bh);
1229 else if (buffer_unwritten(bh))
1231 else if (buffer_delay(bh))
1233 } while ((bh = bh->b_this_page) != head);
1238 * writepage: Called from one of two places:
1240 * 1. we are flushing a delalloc buffer head.
1242 * 2. we are writing out a dirty page. Typically the page dirty
1243 * state is cleared before we get here. In this case is it
1244 * conceivable we have no buffer heads.
1246 * For delalloc space on the page we need to allocate space and
1247 * flush it. For unmapped buffer heads on the page we should
1248 * allocate space if the page is uptodate. For any other dirty
1249 * buffer heads on the page we should flush them.
1251 * If we detect that a transaction would be required to flush
1252 * the page, we have to check the process flags first, if we
1253 * are already in a transaction or disk I/O during allocations
1254 * is off, we need to fail the writepage and redirty the page.
1260 struct writeback_control *wbc)
1264 int delalloc, unmapped, unwritten;
1265 struct inode *inode = page->mapping->host;
1267 xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
1270 * We need a transaction if:
1271 * 1. There are delalloc buffers on the page
1272 * 2. The page is uptodate and we have unmapped buffers
1273 * 3. The page is uptodate and we have no buffers
1274 * 4. There are unwritten buffers on the page
1277 if (!page_has_buffers(page)) {
1281 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1282 if (!PageUptodate(page))
1284 need_trans = delalloc + unmapped + unwritten;
1288 * If we need a transaction and the process flags say
1289 * we are already in a transaction, or no IO is allowed
1290 * then mark the page dirty again and leave the page
1293 if (PFLAGS_TEST_FSTRANS() && need_trans)
1297 * Delay hooking up buffer heads until we have
1298 * made our go/no-go decision.
1300 if (!page_has_buffers(page))
1301 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
1304 * Convert delayed allocate, unwritten or unmapped space
1305 * to real space and flush out to disk.
1307 error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
1308 if (error == -EAGAIN)
1310 if (unlikely(error < 0))
1316 redirty_page_for_writepage(wbc, page);
1325 linvfs_invalidate_page(
1327 unsigned long offset)
1329 xfs_page_trace(XFS_INVALIDPAGE_ENTER,
1330 page->mapping->host, page, offset);
1331 return block_invalidatepage(page, offset);
1335 * Called to move a page into cleanable state - and from there
1336 * to be released. Possibly the page is already clean. We always
1337 * have buffer heads in this call.
1339 * Returns 0 if the page is ok to release, 1 otherwise.
1341 * Possible scenarios are:
1343 * 1. We are being called to release a page which has been written
1344 * to via regular I/O. buffer heads will be dirty and possibly
1345 * delalloc. If no delalloc buffer heads in this case then we
1346 * can just return zero.
1348 * 2. We are called to release a page which has been written via
1349 * mmap, all we need to do is ensure there is no delalloc
1350 * state in the buffer heads, if not we can let the caller
1351 * free them and we should come back later via writepage.
1354 linvfs_release_page(
1358 struct inode *inode = page->mapping->host;
1359 int dirty, delalloc, unmapped, unwritten;
1360 struct writeback_control wbc = {
1361 .sync_mode = WB_SYNC_ALL,
1365 xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
1367 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1368 if (!delalloc && !unwritten)
1371 if (!(gfp_mask & __GFP_FS))
1374 /* If we are already inside a transaction or the thread cannot
1375 * do I/O, we cannot release this page.
1377 if (PFLAGS_TEST_FSTRANS())
1381 * Convert delalloc space to real space, do not flush the
1382 * data out to disk, that will be done by the caller.
1383 * Never need to allocate space here - we will always
1384 * come back to writepage in that case.
1386 dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
1387 if (dirty == 0 && !unwritten)
1392 return try_to_free_buffers(page);
1396 linvfs_prepare_write(
1402 return block_prepare_write(page, from, to, linvfs_get_block);
1405 struct address_space_operations linvfs_aops = {
1406 .readpage = linvfs_readpage,
1407 .readpages = linvfs_readpages,
1408 .writepage = linvfs_writepage,
1409 .sync_page = block_sync_page,
1410 .releasepage = linvfs_release_page,
1411 .invalidatepage = linvfs_invalidate_page,
1412 .prepare_write = linvfs_prepare_write,
1413 .commit_write = generic_commit_write,
1414 .bmap = linvfs_bmap,
1415 .direct_IO = linvfs_direct_IO,