2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
39 #include "xfs_trans.h"
40 #include "xfs_dmapi.h"
41 #include "xfs_mount.h"
42 #include "xfs_bmap_btree.h"
43 #include "xfs_alloc_btree.h"
44 #include "xfs_ialloc_btree.h"
45 #include "xfs_alloc.h"
46 #include "xfs_btree.h"
47 #include "xfs_attr_sf.h"
48 #include "xfs_dir_sf.h"
49 #include "xfs_dir2_sf.h"
50 #include "xfs_dinode.h"
51 #include "xfs_inode.h"
52 #include "xfs_error.h"
54 #include "xfs_iomap.h"
55 #include <linux/mpage.h>
56 #include <linux/writeback.h>
58 STATIC void xfs_count_page_state(struct page *, int *, int *, int *);
59 STATIC void xfs_convert_page(struct inode *, struct page *, xfs_iomap_t *,
60 struct writeback_control *wbc, void *, int, int);
62 #if defined(XFS_RW_TRACE)
72 vnode_t *vp = LINVFS_GET_VP(inode);
73 loff_t isize = i_size_read(inode);
74 loff_t offset = page->index << PAGE_CACHE_SHIFT;
75 int delalloc = -1, unmapped = -1, unwritten = -1;
77 if (page_has_buffers(page))
78 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
80 bdp = vn_bhv_lookup(VN_BHV_HEAD(vp), &xfs_vnodeops);
85 ktrace_enter(ip->i_rwtrace,
86 (void *)((unsigned long)tag),
90 (void *)((unsigned long)mask),
91 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
92 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
93 (void *)((unsigned long)((isize >> 32) & 0xffffffff)),
94 (void *)((unsigned long)(isize & 0xffffffff)),
95 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
96 (void *)((unsigned long)(offset & 0xffffffff)),
97 (void *)((unsigned long)delalloc),
98 (void *)((unsigned long)unmapped),
99 (void *)((unsigned long)unwritten),
104 #define xfs_page_trace(tag, inode, page, mask)
108 linvfs_unwritten_done(
109 struct buffer_head *bh,
112 xfs_buf_t *pb = (xfs_buf_t *)bh->b_private;
114 ASSERT(buffer_unwritten(bh));
116 clear_buffer_unwritten(bh);
118 pagebuf_ioerror(pb, EIO);
119 if (atomic_dec_and_test(&pb->pb_io_remaining) == 1) {
120 pagebuf_iodone(pb, 1, 1);
122 end_buffer_async_write(bh, uptodate);
126 * Issue transactions to convert a buffer range from unwritten
127 * to written extents (buffered IO).
130 linvfs_unwritten_convert(
133 vnode_t *vp = XFS_BUF_FSPRIVATE(bp, vnode_t *);
136 BUG_ON(atomic_read(&bp->pb_hold) < 1);
137 VOP_BMAP(vp, XFS_BUF_OFFSET(bp), XFS_BUF_SIZE(bp),
138 BMAPI_UNWRITTEN, NULL, NULL, error);
139 XFS_BUF_SET_FSPRIVATE(bp, NULL);
140 XFS_BUF_CLR_IODONE_FUNC(bp);
141 XFS_BUF_UNDATAIO(bp);
142 iput(LINVFS_GET_IP(vp));
143 pagebuf_iodone(bp, 0, 0);
147 * Issue transactions to convert a buffer range from unwritten
148 * to written extents (direct IO).
151 linvfs_unwritten_convert_direct(
157 ASSERT(!private || inode == (struct inode *)private);
159 /* private indicates an unwritten extent lay beneath this IO,
160 * see linvfs_get_block_core.
162 if (private && size > 0) {
163 vnode_t *vp = LINVFS_GET_VP(inode);
166 VOP_BMAP(vp, offset, size, BMAPI_UNWRITTEN, NULL, NULL, error);
178 vnode_t *vp = LINVFS_GET_VP(inode);
179 int error, nmaps = 1;
181 VOP_BMAP(vp, offset, count, flags, mapp, &nmaps, error);
182 if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)))
188 * Finds the corresponding mapping in block @map array of the
189 * given @offset within a @page.
195 unsigned long offset)
197 loff_t full_offset; /* offset from start of file */
199 ASSERT(offset < PAGE_CACHE_SIZE);
201 full_offset = page->index; /* NB: using 64bit number */
202 full_offset <<= PAGE_CACHE_SHIFT; /* offset from file start */
203 full_offset += offset; /* offset from page start */
205 if (full_offset < iomapp->iomap_offset)
207 if (iomapp->iomap_offset + (iomapp->iomap_bsize -1) >= full_offset)
215 struct buffer_head *bh,
216 unsigned long offset,
224 ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
225 ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
226 ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL);
229 delta <<= PAGE_CACHE_SHIFT;
231 delta -= iomapp->iomap_offset;
232 delta >>= block_bits;
234 sector_shift = block_bits - BBSHIFT;
235 bn = iomapp->iomap_bn >> sector_shift;
237 ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
241 bh->b_bdev = iomapp->iomap_target->pbr_bdev;
242 set_buffer_mapped(bh);
243 clear_buffer_delay(bh);
247 * Look for a page at index which is unlocked and contains our
248 * unwritten extent flagged buffers at its head. Returns page
249 * locked and with an extra reference count, and length of the
250 * unwritten extent component on this page that we can write,
251 * in units of filesystem blocks.
254 xfs_probe_unwritten_page(
255 struct address_space *mapping,
259 unsigned long max_offset,
265 page = find_trylock_page(mapping, index);
268 if (PageWriteback(page))
271 if (page->mapping && page_has_buffers(page)) {
272 struct buffer_head *bh, *head;
273 unsigned long p_offset = 0;
276 bh = head = page_buffers(page);
278 if (!buffer_unwritten(bh) || !buffer_uptodate(bh))
280 if (!xfs_offset_to_map(page, iomapp, p_offset))
282 if (p_offset >= max_offset)
284 xfs_map_at_offset(page, bh, p_offset, bbits, iomapp);
285 set_buffer_unwritten_io(bh);
287 p_offset += bh->b_size;
289 } while ((bh = bh->b_this_page) != head);
301 * Look for a page at index which is unlocked and not mapped
302 * yet - clustering for mmap write case.
305 xfs_probe_unmapped_page(
306 struct address_space *mapping,
308 unsigned int pg_offset)
313 page = find_trylock_page(mapping, index);
316 if (PageWriteback(page))
319 if (page->mapping && PageDirty(page)) {
320 if (page_has_buffers(page)) {
321 struct buffer_head *bh, *head;
323 bh = head = page_buffers(page);
325 if (buffer_mapped(bh) || !buffer_uptodate(bh))
328 if (ret >= pg_offset)
330 } while ((bh = bh->b_this_page) != head);
332 ret = PAGE_CACHE_SIZE;
341 xfs_probe_unmapped_cluster(
343 struct page *startpage,
344 struct buffer_head *bh,
345 struct buffer_head *head)
347 pgoff_t tindex, tlast, tloff;
348 unsigned int pg_offset, len, total = 0;
349 struct address_space *mapping = inode->i_mapping;
351 /* First sum forwards in this page */
353 if (buffer_mapped(bh))
356 } while ((bh = bh->b_this_page) != head);
358 /* If we reached the end of the page, sum forwards in
362 tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
363 /* Prune this back to avoid pathological behavior */
364 tloff = min(tlast, startpage->index + 64);
365 for (tindex = startpage->index + 1; tindex < tloff; tindex++) {
366 len = xfs_probe_unmapped_page(mapping, tindex,
372 if (tindex == tlast &&
373 (pg_offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
374 total += xfs_probe_unmapped_page(mapping,
382 * Probe for a given page (index) in the inode and test if it is delayed
383 * and without unwritten buffers. Returns page locked and with an extra
387 xfs_probe_delalloc_page(
393 page = find_trylock_page(inode->i_mapping, index);
396 if (PageWriteback(page))
399 if (page->mapping && page_has_buffers(page)) {
400 struct buffer_head *bh, *head;
403 bh = head = page_buffers(page);
405 if (buffer_unwritten(bh)) {
408 } else if (buffer_delay(bh)) {
411 } while ((bh = bh->b_this_page) != head);
425 struct page *start_page,
426 struct buffer_head *head,
427 struct buffer_head *curr,
428 unsigned long p_offset,
431 struct writeback_control *wbc,
435 struct buffer_head *bh = curr;
439 unsigned long nblocks = 0;
441 offset = start_page->index;
442 offset <<= PAGE_CACHE_SHIFT;
445 /* get an "empty" pagebuf to manage IO completion
446 * Proper values will be set before returning */
447 pb = pagebuf_lookup(iomapp->iomap_target, 0, 0, 0);
451 /* Take a reference to the inode to prevent it from
452 * being reclaimed while we have outstanding unwritten
455 if ((igrab(inode)) != inode) {
460 /* Set the count to 1 initially, this will stop an I/O
461 * completion callout which happens before we have started
462 * all the I/O from calling pagebuf_iodone too early.
464 atomic_set(&pb->pb_io_remaining, 1);
466 /* First map forwards in the page consecutive buffers
467 * covering this unwritten extent
470 if (!buffer_unwritten(bh))
472 tmp = xfs_offset_to_map(start_page, iomapp, p_offset);
475 xfs_map_at_offset(start_page, bh, p_offset, block_bits, iomapp);
476 set_buffer_unwritten_io(bh);
478 p_offset += bh->b_size;
480 } while ((bh = bh->b_this_page) != head);
482 atomic_add(nblocks, &pb->pb_io_remaining);
484 /* If we reached the end of the page, map forwards in any
485 * following pages which are also covered by this extent.
488 struct address_space *mapping = inode->i_mapping;
489 pgoff_t tindex, tloff, tlast;
491 unsigned int pg_offset, bbits = inode->i_blkbits;
494 tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
495 tloff = (iomapp->iomap_offset + iomapp->iomap_bsize) >> PAGE_CACHE_SHIFT;
496 tloff = min(tlast, tloff);
497 for (tindex = start_page->index + 1; tindex < tloff; tindex++) {
498 page = xfs_probe_unwritten_page(mapping,
500 PAGE_CACHE_SIZE, &bs, bbits);
504 atomic_add(bs, &pb->pb_io_remaining);
505 xfs_convert_page(inode, page, iomapp, wbc, pb,
507 /* stop if converting the next page might add
508 * enough blocks that the corresponding byte
509 * count won't fit in our ulong page buf length */
510 if (nblocks >= ((ULONG_MAX - PAGE_SIZE) >> block_bits))
514 if (tindex == tlast &&
515 (pg_offset = (i_size_read(inode) & (PAGE_CACHE_SIZE - 1)))) {
516 page = xfs_probe_unwritten_page(mapping,
518 pg_offset, &bs, bbits);
521 atomic_add(bs, &pb->pb_io_remaining);
522 xfs_convert_page(inode, page, iomapp, wbc, pb,
524 if (nblocks >= ((ULONG_MAX - PAGE_SIZE) >> block_bits))
531 size = nblocks; /* NB: using 64bit number here */
532 size <<= block_bits; /* convert fsb's to byte range */
536 XFS_BUF_SET_SIZE(pb, size);
537 XFS_BUF_SET_COUNT(pb, size);
538 XFS_BUF_SET_OFFSET(pb, offset);
539 XFS_BUF_SET_FSPRIVATE(pb, LINVFS_GET_VP(inode));
540 XFS_BUF_SET_IODONE_FUNC(pb, linvfs_unwritten_convert);
542 if (atomic_dec_and_test(&pb->pb_io_remaining) == 1) {
543 pagebuf_iodone(pb, 1, 1);
552 struct buffer_head *bh_arr[],
555 struct buffer_head *bh;
558 BUG_ON(PageWriteback(page));
559 set_page_writeback(page);
560 clear_page_dirty(page);
564 for (i = 0; i < cnt; i++) {
566 mark_buffer_async_write(bh);
567 if (buffer_unwritten(bh))
568 set_buffer_unwritten_io(bh);
569 set_buffer_uptodate(bh);
570 clear_buffer_dirty(bh);
573 for (i = 0; i < cnt; i++)
574 submit_bh(WRITE, bh_arr[i]);
576 end_page_writeback(page);
580 * Allocate & map buffers for page given the extent map. Write it out.
581 * except for the original page of a writepage, this is called on
582 * delalloc/unwritten pages only, for the original page it is possible
583 * that the page has no mapping at all.
590 struct writeback_control *wbc,
595 struct buffer_head *bh_arr[MAX_BUF_PER_PAGE], *bh, *head;
596 xfs_iomap_t *mp = iomapp, *tmp;
597 unsigned long end, offset;
599 int i = 0, index = 0;
600 int bbits = inode->i_blkbits;
602 end_index = i_size_read(inode) >> PAGE_CACHE_SHIFT;
603 if (page->index < end_index) {
604 end = PAGE_CACHE_SIZE;
606 end = i_size_read(inode) & (PAGE_CACHE_SIZE-1);
608 bh = head = page_buffers(page);
611 if (!(PageUptodate(page) || buffer_uptodate(bh)))
613 if (buffer_mapped(bh) && all_bh &&
614 !buffer_unwritten(bh) && !buffer_delay(bh)) {
615 if (startio && (offset < end)) {
617 bh_arr[index++] = bh;
621 tmp = xfs_offset_to_map(page, mp, offset);
624 ASSERT(!(tmp->iomap_flags & IOMAP_HOLE));
625 ASSERT(!(tmp->iomap_flags & IOMAP_DELAY));
627 /* If this is a new unwritten extent buffer (i.e. one
628 * that we haven't passed in private data for, we must
629 * now map this buffer too.
631 if (buffer_unwritten(bh) && !bh->b_end_io) {
632 ASSERT(tmp->iomap_flags & IOMAP_UNWRITTEN);
633 xfs_map_unwritten(inode, page, head, bh, offset,
634 bbits, tmp, wbc, startio, all_bh);
635 } else if (! (buffer_unwritten(bh) && buffer_locked(bh))) {
636 xfs_map_at_offset(page, bh, offset, bbits, tmp);
637 if (buffer_unwritten(bh)) {
638 set_buffer_unwritten_io(bh);
639 bh->b_private = private;
643 if (startio && (offset < end)) {
644 bh_arr[index++] = bh;
646 set_buffer_dirty(bh);
648 mark_buffer_dirty(bh);
650 } while (i++, (bh = bh->b_this_page) != head);
654 xfs_submit_page(page, bh_arr, index);
661 * Convert & write out a cluster of pages in the same extent as defined
662 * by mp and following the start page.
669 struct writeback_control *wbc,
676 for (; tindex <= tlast; tindex++) {
677 page = xfs_probe_delalloc_page(inode, tindex);
680 xfs_convert_page(inode, page, iomapp, wbc, NULL,
686 * Calling this without startio set means we are being asked to make a dirty
687 * page ready for freeing it's buffers. When called with startio set then
688 * we are coming from writepage.
690 * When called with startio set it is important that we write the WHOLE
692 * The bh->b_state's cannot know if any of the blocks or which block for
693 * that matter are dirty due to mmap writes, and therefore bh uptodate is
694 * only vaild if the page itself isn't completely uptodate. Some layers
695 * may clear the page dirty flag prior to calling write page, under the
696 * assumption the entire page will be written out; by not writing out the
697 * whole page the page can be reused before all valid dirty data is
698 * written out. Note: in the case of a page that has been dirty'd by
699 * mapwrite and but partially setup by block_prepare_write the
700 * bh->b_states's will not agree and only ones setup by BPW/BCW will have
701 * valid state, thus the whole page must be written out thing.
705 xfs_page_state_convert(
708 struct writeback_control *wbc,
710 int unmapped) /* also implies page uptodate */
712 struct buffer_head *bh_arr[MAX_BUF_PER_PAGE], *bh, *head;
713 xfs_iomap_t *iomp, iomap;
715 unsigned long p_offset = 0;
716 __uint64_t end_offset;
717 pgoff_t end_index, last_index, tlast;
718 int len, err, i, cnt = 0, uptodate = 1;
719 int flags = startio ? 0 : BMAPI_TRYLOCK;
724 /* Are we off the end of the file ? */
725 offset = i_size_read(inode);
726 end_index = offset >> PAGE_CACHE_SHIFT;
727 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
728 if (page->index >= end_index) {
729 if ((page->index >= end_index + 1) ||
730 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
736 offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
737 end_offset = min_t(unsigned long long,
738 offset + PAGE_CACHE_SIZE, i_size_read(inode));
740 bh = head = page_buffers(page);
745 if (offset >= end_offset)
747 if (!buffer_uptodate(bh))
749 if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio)
753 iomp = xfs_offset_to_map(page, &iomap, p_offset);
757 * First case, map an unwritten extent and prepare for
758 * extent state conversion transaction on completion.
760 if (buffer_unwritten(bh)) {
764 err = xfs_map_blocks(inode, offset, len, &iomap,
765 BMAPI_READ|BMAPI_IGNSTATE);
769 iomp = xfs_offset_to_map(page, &iomap,
774 err = xfs_map_unwritten(inode, page,
776 inode->i_blkbits, iomp,
777 wbc, startio, unmapped);
782 set_bit(BH_Lock, &bh->b_state);
784 BUG_ON(!buffer_locked(bh));
789 * Second case, allocate space for a delalloc buffer.
790 * We can return EAGAIN here in the release page case.
792 } else if (buffer_delay(bh)) {
795 err = xfs_map_blocks(inode, offset, len, &iomap,
796 BMAPI_ALLOCATE | flags);
800 iomp = xfs_offset_to_map(page, &iomap,
804 xfs_map_at_offset(page, bh, p_offset,
805 inode->i_blkbits, iomp);
809 set_buffer_dirty(bh);
811 mark_buffer_dirty(bh);
815 } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
816 (unmapped || startio)) {
818 if (!buffer_mapped(bh)) {
822 * Getting here implies an unmapped buffer
823 * was found, and we are in a path where we
824 * need to write the whole page out.
827 size = xfs_probe_unmapped_cluster(
828 inode, page, bh, head);
829 err = xfs_map_blocks(inode, offset,
831 BMAPI_WRITE|BMAPI_MMAP);
835 iomp = xfs_offset_to_map(page, &iomap,
839 xfs_map_at_offset(page,
841 inode->i_blkbits, iomp);
845 set_buffer_dirty(bh);
847 mark_buffer_dirty(bh);
851 } else if (startio) {
852 if (buffer_uptodate(bh) &&
853 !test_and_set_bit(BH_Lock, &bh->b_state)) {
859 } while (offset += len, p_offset += len,
860 ((bh = bh->b_this_page) != head));
862 if (uptodate && bh == head)
863 SetPageUptodate(page);
866 xfs_submit_page(page, bh_arr, cnt);
869 tlast = (iomp->iomap_offset + iomp->iomap_bsize - 1) >>
871 if (delalloc && (tlast > last_index))
873 xfs_cluster_write(inode, page->index + 1, iomp, wbc,
874 startio, unmapped, tlast);
880 for (i = 0; i < cnt; i++) {
881 unlock_buffer(bh_arr[i]);
885 * If it's delalloc and we have nowhere to put it,
886 * throw it away, unless the lower layers told
889 if (err != -EAGAIN) {
891 block_invalidatepage(page, 0);
893 ClearPageUptodate(page);
899 linvfs_get_block_core(
902 unsigned long blocks,
903 struct buffer_head *bh_result,
908 vnode_t *vp = LINVFS_GET_VP(inode);
913 loff_t offset = (loff_t)iblock << inode->i_blkbits;
916 size = blocks << inode->i_blkbits;
918 size = 1 << inode->i_blkbits;
920 VOP_BMAP(vp, offset, size,
921 create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
928 if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
932 /* For unwritten extents do not report a disk address on
933 * the read case (treat as if we're reading into a hole).
935 if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
936 delta = offset - iomap.iomap_offset;
937 delta >>= inode->i_blkbits;
939 bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
942 bh_result->b_blocknr = bn;
943 bh_result->b_bdev = iomap.iomap_target->pbr_bdev;
944 set_buffer_mapped(bh_result);
946 if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
948 bh_result->b_private = inode;
949 set_buffer_unwritten(bh_result);
950 set_buffer_delay(bh_result);
954 /* If this is a realtime file, data might be on a new device */
955 bh_result->b_bdev = iomap.iomap_target->pbr_bdev;
957 /* If we previously allocated a block out beyond eof and
958 * we are now coming back to use it then we will need to
959 * flag it as new even if it has a disk address.
962 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
963 (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW))) {
964 set_buffer_new(bh_result);
967 if (iomap.iomap_flags & IOMAP_DELAY) {
968 if (unlikely(direct))
971 set_buffer_mapped(bh_result);
972 set_buffer_uptodate(bh_result);
974 bh_result->b_bdev = iomap.iomap_target->pbr_bdev;
975 set_buffer_delay(bh_result);
980 iosize = (iomap.iomap_bsize - iomap.iomap_delta);
982 (ssize_t)min(iosize, (loff_t)(blocks << inode->i_blkbits));
992 struct buffer_head *bh_result,
995 return linvfs_get_block_core(inode, iblock, 0, bh_result,
996 create, 0, BMAPI_WRITE);
1000 linvfs_get_block_sync(
1001 struct inode *inode,
1003 struct buffer_head *bh_result,
1006 return linvfs_get_block_core(inode, iblock, 0, bh_result,
1007 create, 0, BMAPI_SYNC|BMAPI_WRITE);
1011 linvfs_get_blocks_direct(
1012 struct inode *inode,
1014 unsigned long max_blocks,
1015 struct buffer_head *bh_result,
1018 return linvfs_get_block_core(inode, iblock, max_blocks, bh_result,
1019 create, 1, BMAPI_WRITE|BMAPI_DIRECT);
1026 const struct iovec *iov,
1028 unsigned long nr_segs)
1030 struct file *file = iocb->ki_filp;
1031 struct inode *inode = file->f_mapping->host;
1032 vnode_t *vp = LINVFS_GET_VP(inode);
1037 VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
1041 return blockdev_direct_IO_no_locking(rw, iocb, inode,
1042 iomap.iomap_target->pbr_bdev,
1043 iov, offset, nr_segs,
1044 linvfs_get_blocks_direct,
1045 linvfs_unwritten_convert_direct);
1051 struct address_space *mapping,
1054 struct inode *inode = (struct inode *)mapping->host;
1055 vnode_t *vp = LINVFS_GET_VP(inode);
1058 vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
1060 VOP_RWLOCK(vp, VRWLOCK_READ);
1061 VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
1062 VOP_RWUNLOCK(vp, VRWLOCK_READ);
1063 return generic_block_bmap(mapping, block, linvfs_get_block);
1068 struct file *unused,
1071 return mpage_readpage(page, linvfs_get_block);
1076 struct file *unused,
1077 struct address_space *mapping,
1078 struct list_head *pages,
1081 return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
1085 xfs_count_page_state(
1091 struct buffer_head *bh, *head;
1093 *delalloc = *unmapped = *unwritten = 0;
1095 bh = head = page_buffers(page);
1097 if (buffer_uptodate(bh) && !buffer_mapped(bh))
1099 else if (buffer_unwritten(bh) && !buffer_delay(bh))
1100 clear_buffer_unwritten(bh);
1101 else if (buffer_unwritten(bh))
1103 else if (buffer_delay(bh))
1105 } while ((bh = bh->b_this_page) != head);
1110 * writepage: Called from one of two places:
1112 * 1. we are flushing a delalloc buffer head.
1114 * 2. we are writing out a dirty page. Typically the page dirty
1115 * state is cleared before we get here. In this case is it
1116 * conceivable we have no buffer heads.
1118 * For delalloc space on the page we need to allocate space and
1119 * flush it. For unmapped buffer heads on the page we should
1120 * allocate space if the page is uptodate. For any other dirty
1121 * buffer heads on the page we should flush them.
1123 * If we detect that a transaction would be required to flush
1124 * the page, we have to check the process flags first, if we
1125 * are already in a transaction or disk I/O during allocations
1126 * is off, we need to fail the writepage and redirty the page.
1132 struct writeback_control *wbc)
1136 int delalloc, unmapped, unwritten;
1137 struct inode *inode = page->mapping->host;
1139 xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
1142 * We need a transaction if:
1143 * 1. There are delalloc buffers on the page
1144 * 2. The page is uptodate and we have unmapped buffers
1145 * 3. The page is uptodate and we have no buffers
1146 * 4. There are unwritten buffers on the page
1149 if (!page_has_buffers(page)) {
1153 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1154 if (!PageUptodate(page))
1156 need_trans = delalloc + unmapped + unwritten;
1160 * If we need a transaction and the process flags say
1161 * we are already in a transaction, or no IO is allowed
1162 * then mark the page dirty again and leave the page
1165 if (PFLAGS_TEST_FSTRANS() && need_trans)
1169 * Delay hooking up buffer heads until we have
1170 * made our go/no-go decision.
1172 if (!page_has_buffers(page))
1173 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
1176 * Convert delayed allocate, unwritten or unmapped space
1177 * to real space and flush out to disk.
1179 error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
1180 if (error == -EAGAIN)
1182 if (unlikely(error < 0))
1188 set_page_dirty(page);
1197 * Called to move a page into cleanable state - and from there
1198 * to be released. Possibly the page is already clean. We always
1199 * have buffer heads in this call.
1201 * Returns 0 if the page is ok to release, 1 otherwise.
1203 * Possible scenarios are:
1205 * 1. We are being called to release a page which has been written
1206 * to via regular I/O. buffer heads will be dirty and possibly
1207 * delalloc. If no delalloc buffer heads in this case then we
1208 * can just return zero.
1210 * 2. We are called to release a page which has been written via
1211 * mmap, all we need to do is ensure there is no delalloc
1212 * state in the buffer heads, if not we can let the caller
1213 * free them and we should come back later via writepage.
1216 linvfs_release_page(
1220 struct inode *inode = page->mapping->host;
1221 int dirty, delalloc, unmapped, unwritten;
1222 struct writeback_control wbc = {
1223 .sync_mode = WB_SYNC_ALL,
1227 xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
1229 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1230 if (!delalloc && !unwritten)
1233 if (!(gfp_mask & __GFP_FS))
1236 /* If we are already inside a transaction or the thread cannot
1237 * do I/O, we cannot release this page.
1239 if (PFLAGS_TEST_FSTRANS())
1243 * Convert delalloc space to real space, do not flush the
1244 * data out to disk, that will be done by the caller.
1245 * Never need to allocate space here - we will always
1246 * come back to writepage in that case.
1248 dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
1249 if (dirty == 0 && !unwritten)
1254 return try_to_free_buffers(page);
1258 linvfs_prepare_write(
1264 if (file && (file->f_flags & O_SYNC)) {
1265 return block_prepare_write(page, from, to,
1266 linvfs_get_block_sync);
1268 return block_prepare_write(page, from, to,
1273 struct address_space_operations linvfs_aops = {
1274 .readpage = linvfs_readpage,
1275 .readpages = linvfs_readpages,
1276 .writepage = linvfs_writepage,
1277 .sync_page = block_sync_page,
1278 .releasepage = linvfs_release_page,
1279 .prepare_write = linvfs_prepare_write,
1280 .commit_write = generic_commit_write,
1281 .bmap = linvfs_bmap,
1282 .direct_IO = linvfs_direct_IO,