Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_sb.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_dir2.h" |
| 25 | #include "xfs_trans.h" |
| 26 | #include "xfs_dmapi.h" |
| 27 | #include "xfs_mount.h" |
| 28 | #include "xfs_bmap_btree.h" |
| 29 | #include "xfs_alloc_btree.h" |
| 30 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_dinode.h" |
| 34 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_alloc.h" |
| 36 | #include "xfs_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "xfs_error.h" |
| 38 | #include "xfs_rw.h" |
| 39 | #include "xfs_iomap.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 40 | #include "xfs_vnodeops.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 41 | #include "xfs_trace.h" |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 42 | #include "xfs_bmap.h" |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 43 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/mpage.h> |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 45 | #include <linux/pagevec.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/writeback.h> |
| 47 | |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 48 | /* |
| 49 | * Types of I/O for bmap clustering and I/O completion tracking. |
| 50 | */ |
| 51 | enum { |
| 52 | IO_READ, /* mapping for a read */ |
| 53 | IO_DELAY, /* mapping covers delalloc region */ |
| 54 | IO_UNWRITTEN, /* mapping covers allocated but uninitialized data */ |
| 55 | IO_NEW /* just allocated */ |
| 56 | }; |
Christoph Hellwig | 25e41b3 | 2008-12-03 12:20:39 +0100 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * Prime number of hash buckets since address is used as the key. |
| 60 | */ |
| 61 | #define NVSYNC 37 |
| 62 | #define to_ioend_wq(v) (&xfs_ioend_wq[((unsigned long)v) % NVSYNC]) |
| 63 | static wait_queue_head_t xfs_ioend_wq[NVSYNC]; |
| 64 | |
| 65 | void __init |
| 66 | xfs_ioend_init(void) |
| 67 | { |
| 68 | int i; |
| 69 | |
| 70 | for (i = 0; i < NVSYNC; i++) |
| 71 | init_waitqueue_head(&xfs_ioend_wq[i]); |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | xfs_ioend_wait( |
| 76 | xfs_inode_t *ip) |
| 77 | { |
| 78 | wait_queue_head_t *wq = to_ioend_wq(ip); |
| 79 | |
| 80 | wait_event(*wq, (atomic_read(&ip->i_iocount) == 0)); |
| 81 | } |
| 82 | |
| 83 | STATIC void |
| 84 | xfs_ioend_wake( |
| 85 | xfs_inode_t *ip) |
| 86 | { |
| 87 | if (atomic_dec_and_test(&ip->i_iocount)) |
| 88 | wake_up(to_ioend_wq(ip)); |
| 89 | } |
| 90 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 91 | void |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 92 | xfs_count_page_state( |
| 93 | struct page *page, |
| 94 | int *delalloc, |
| 95 | int *unmapped, |
| 96 | int *unwritten) |
| 97 | { |
| 98 | struct buffer_head *bh, *head; |
| 99 | |
| 100 | *delalloc = *unmapped = *unwritten = 0; |
| 101 | |
| 102 | bh = head = page_buffers(page); |
| 103 | do { |
| 104 | if (buffer_uptodate(bh) && !buffer_mapped(bh)) |
| 105 | (*unmapped) = 1; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 106 | else if (buffer_unwritten(bh)) |
| 107 | (*unwritten) = 1; |
| 108 | else if (buffer_delay(bh)) |
| 109 | (*delalloc) = 1; |
| 110 | } while ((bh = bh->b_this_page) != head); |
| 111 | } |
| 112 | |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 113 | STATIC struct block_device * |
| 114 | xfs_find_bdev_for_inode( |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 115 | struct inode *inode) |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 116 | { |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 117 | struct xfs_inode *ip = XFS_I(inode); |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 118 | struct xfs_mount *mp = ip->i_mount; |
| 119 | |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 120 | if (XFS_IS_REALTIME_INODE(ip)) |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 121 | return mp->m_rtdev_targp->bt_bdev; |
| 122 | else |
| 123 | return mp->m_ddev_targp->bt_bdev; |
| 124 | } |
| 125 | |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 126 | /* |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 127 | * We're now finished for good with this ioend structure. |
| 128 | * Update the page state via the associated buffer_heads, |
| 129 | * release holds on the inode and bio, and finally free |
| 130 | * up memory. Do not use the ioend after this. |
| 131 | */ |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 132 | STATIC void |
| 133 | xfs_destroy_ioend( |
| 134 | xfs_ioend_t *ioend) |
| 135 | { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 136 | struct buffer_head *bh, *next; |
Christoph Hellwig | 583fa58 | 2008-12-03 12:20:38 +0100 | [diff] [blame] | 137 | struct xfs_inode *ip = XFS_I(ioend->io_inode); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 138 | |
| 139 | for (bh = ioend->io_buffer_head; bh; bh = next) { |
| 140 | next = bh->b_private; |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 141 | bh->b_end_io(bh, !ioend->io_error); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 142 | } |
Christoph Hellwig | 583fa58 | 2008-12-03 12:20:38 +0100 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * Volume managers supporting multiple paths can send back ENODEV |
| 146 | * when the final path disappears. In this case continuing to fill |
| 147 | * the page cache with dirty data which cannot be written out is |
| 148 | * evil, so prevent that. |
| 149 | */ |
| 150 | if (unlikely(ioend->io_error == -ENODEV)) { |
| 151 | xfs_do_force_shutdown(ip->i_mount, SHUTDOWN_DEVICE_REQ, |
| 152 | __FILE__, __LINE__); |
Christoph Hellwig | b677c21 | 2007-08-29 11:46:28 +1000 | [diff] [blame] | 153 | } |
Christoph Hellwig | 583fa58 | 2008-12-03 12:20:38 +0100 | [diff] [blame] | 154 | |
Christoph Hellwig | 25e41b3 | 2008-12-03 12:20:39 +0100 | [diff] [blame] | 155 | xfs_ioend_wake(ip); |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 156 | mempool_free(ioend, xfs_ioend_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* |
Dave Chinner | 932640e | 2009-10-06 20:29:29 +0000 | [diff] [blame] | 160 | * If the end of the current ioend is beyond the current EOF, |
| 161 | * return the new EOF value, otherwise zero. |
| 162 | */ |
| 163 | STATIC xfs_fsize_t |
| 164 | xfs_ioend_new_eof( |
| 165 | xfs_ioend_t *ioend) |
| 166 | { |
| 167 | xfs_inode_t *ip = XFS_I(ioend->io_inode); |
| 168 | xfs_fsize_t isize; |
| 169 | xfs_fsize_t bsize; |
| 170 | |
| 171 | bsize = ioend->io_offset + ioend->io_size; |
| 172 | isize = MAX(ip->i_size, ip->i_new_size); |
| 173 | isize = MIN(isize, bsize); |
| 174 | return isize > ip->i_d.di_size ? isize : 0; |
| 175 | } |
| 176 | |
| 177 | /* |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 178 | * Update on-disk file size now that data has been written to disk. The |
| 179 | * current in-memory file size is i_size. If a write is beyond eof i_new_size |
| 180 | * will be the intended file size until i_size is updated. If this write does |
| 181 | * not extend all the way to the valid file size then restrict this update to |
| 182 | * the end of the write. |
| 183 | * |
| 184 | * This function does not block as blocking on the inode lock in IO completion |
| 185 | * can lead to IO completion order dependency deadlocks.. If it can't get the |
| 186 | * inode ilock it will return EAGAIN. Callers must handle this. |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 187 | */ |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 188 | STATIC int |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 189 | xfs_setfilesize( |
| 190 | xfs_ioend_t *ioend) |
| 191 | { |
Christoph Hellwig | b677c21 | 2007-08-29 11:46:28 +1000 | [diff] [blame] | 192 | xfs_inode_t *ip = XFS_I(ioend->io_inode); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 193 | xfs_fsize_t isize; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 194 | |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 195 | ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG); |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 196 | ASSERT(ioend->io_type != IO_READ); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 197 | |
| 198 | if (unlikely(ioend->io_error)) |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 199 | return 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 200 | |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 201 | if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) |
| 202 | return EAGAIN; |
| 203 | |
Dave Chinner | 932640e | 2009-10-06 20:29:29 +0000 | [diff] [blame] | 204 | isize = xfs_ioend_new_eof(ioend); |
| 205 | if (isize) { |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 206 | ip->i_d.di_size = isize; |
Christoph Hellwig | 66d834e | 2010-02-15 09:44:49 +0000 | [diff] [blame] | 207 | xfs_mark_inode_dirty(ip); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 211 | return 0; |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* |
Dave Chinner | c626d17 | 2009-04-06 18:42:11 +0200 | [diff] [blame] | 215 | * Schedule IO completion handling on a xfsdatad if this was |
| 216 | * the final hold on this ioend. If we are asked to wait, |
| 217 | * flush the workqueue. |
| 218 | */ |
| 219 | STATIC void |
| 220 | xfs_finish_ioend( |
| 221 | xfs_ioend_t *ioend, |
| 222 | int wait) |
| 223 | { |
| 224 | if (atomic_dec_and_test(&ioend->io_remaining)) { |
Christoph Hellwig | 5ec4fab | 2009-10-30 09:11:47 +0000 | [diff] [blame] | 225 | struct workqueue_struct *wq; |
Dave Chinner | c626d17 | 2009-04-06 18:42:11 +0200 | [diff] [blame] | 226 | |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 227 | wq = (ioend->io_type == IO_UNWRITTEN) ? |
Christoph Hellwig | 5ec4fab | 2009-10-30 09:11:47 +0000 | [diff] [blame] | 228 | xfsconvertd_workqueue : xfsdatad_workqueue; |
Dave Chinner | c626d17 | 2009-04-06 18:42:11 +0200 | [diff] [blame] | 229 | queue_work(wq, &ioend->io_work); |
| 230 | if (wait) |
| 231 | flush_workqueue(wq); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /* |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 236 | * IO write completion. |
| 237 | */ |
| 238 | STATIC void |
| 239 | xfs_end_io( |
| 240 | struct work_struct *work) |
| 241 | { |
| 242 | xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work); |
| 243 | struct xfs_inode *ip = XFS_I(ioend->io_inode); |
Dave Chinner | 6941893 | 2010-03-04 00:57:09 +0000 | [diff] [blame] | 244 | int error = 0; |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 245 | |
| 246 | /* |
| 247 | * For unwritten extents we need to issue transactions to convert a |
| 248 | * range to normal written extens after the data I/O has finished. |
| 249 | */ |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 250 | if (ioend->io_type == IO_UNWRITTEN && |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 251 | likely(!ioend->io_error && !XFS_FORCED_SHUTDOWN(ip->i_mount))) { |
| 252 | |
| 253 | error = xfs_iomap_write_unwritten(ip, ioend->io_offset, |
| 254 | ioend->io_size); |
| 255 | if (error) |
| 256 | ioend->io_error = error; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * We might have to update the on-disk file size after extending |
| 261 | * writes. |
| 262 | */ |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 263 | if (ioend->io_type != IO_READ) { |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 264 | error = xfs_setfilesize(ioend); |
| 265 | ASSERT(!error || error == EAGAIN); |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * If we didn't complete processing of the ioend, requeue it to the |
| 270 | * tail of the workqueue for another attempt later. Otherwise destroy |
| 271 | * it. |
| 272 | */ |
| 273 | if (error == EAGAIN) { |
| 274 | atomic_inc(&ioend->io_remaining); |
| 275 | xfs_finish_ioend(ioend, 0); |
| 276 | /* ensure we don't spin on blocked ioends */ |
| 277 | delay(1); |
| 278 | } else |
| 279 | xfs_destroy_ioend(ioend); |
| 280 | } |
| 281 | |
| 282 | /* |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 283 | * Allocate and initialise an IO completion structure. |
| 284 | * We need to track unwritten extent write completion here initially. |
| 285 | * We'll need to extend this for updating the ondisk inode size later |
| 286 | * (vs. incore size). |
| 287 | */ |
| 288 | STATIC xfs_ioend_t * |
| 289 | xfs_alloc_ioend( |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 290 | struct inode *inode, |
| 291 | unsigned int type) |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 292 | { |
| 293 | xfs_ioend_t *ioend; |
| 294 | |
| 295 | ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS); |
| 296 | |
| 297 | /* |
| 298 | * Set the count to 1 initially, which will prevent an I/O |
| 299 | * completion callback from happening before we have started |
| 300 | * all the I/O from calling the completion routine too early. |
| 301 | */ |
| 302 | atomic_set(&ioend->io_remaining, 1); |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 303 | ioend->io_error = 0; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 304 | ioend->io_list = NULL; |
| 305 | ioend->io_type = type; |
Christoph Hellwig | b677c21 | 2007-08-29 11:46:28 +1000 | [diff] [blame] | 306 | ioend->io_inode = inode; |
Christoph Hellwig | c1a073b | 2005-09-05 08:23:35 +1000 | [diff] [blame] | 307 | ioend->io_buffer_head = NULL; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 308 | ioend->io_buffer_tail = NULL; |
Christoph Hellwig | b677c21 | 2007-08-29 11:46:28 +1000 | [diff] [blame] | 309 | atomic_inc(&XFS_I(ioend->io_inode)->i_iocount); |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 310 | ioend->io_offset = 0; |
| 311 | ioend->io_size = 0; |
| 312 | |
Christoph Hellwig | 5ec4fab | 2009-10-30 09:11:47 +0000 | [diff] [blame] | 313 | INIT_WORK(&ioend->io_work, xfs_end_io); |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 314 | return ioend; |
| 315 | } |
| 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | STATIC int |
| 318 | xfs_map_blocks( |
| 319 | struct inode *inode, |
| 320 | loff_t offset, |
| 321 | ssize_t count, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 322 | struct xfs_bmbt_irec *imap, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | int flags) |
| 324 | { |
Christoph Hellwig | 6bd16ff | 2008-12-03 12:20:32 +0100 | [diff] [blame] | 325 | int nmaps = 1; |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 326 | int new = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 328 | return -xfs_iomap(XFS_I(inode), offset, count, flags, imap, &nmaps, &new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Christoph Hellwig | b8f82a4 | 2009-11-14 16:17:22 +0000 | [diff] [blame] | 331 | STATIC int |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 332 | xfs_imap_valid( |
Christoph Hellwig | 8699bb0 | 2010-04-28 12:28:54 +0000 | [diff] [blame] | 333 | struct inode *inode, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 334 | struct xfs_bmbt_irec *imap, |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 335 | xfs_off_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | { |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 337 | offset >>= inode->i_blkbits; |
Christoph Hellwig | 8699bb0 | 2010-04-28 12:28:54 +0000 | [diff] [blame] | 338 | |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 339 | return offset >= imap->br_startoff && |
| 340 | offset < imap->br_startoff + imap->br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 343 | /* |
| 344 | * BIO completion handler for buffered IO. |
| 345 | */ |
Al Viro | 782e3b3 | 2007-10-12 07:17:47 +0100 | [diff] [blame] | 346 | STATIC void |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 347 | xfs_end_bio( |
| 348 | struct bio *bio, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 349 | int error) |
| 350 | { |
| 351 | xfs_ioend_t *ioend = bio->bi_private; |
| 352 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 353 | ASSERT(atomic_read(&bio->bi_cnt) >= 1); |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 354 | ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 355 | |
| 356 | /* Toss bio and pass work off to an xfsdatad thread */ |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 357 | bio->bi_private = NULL; |
| 358 | bio->bi_end_io = NULL; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 359 | bio_put(bio); |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 360 | |
David Chinner | e927af9 | 2007-06-05 16:24:36 +1000 | [diff] [blame] | 361 | xfs_finish_ioend(ioend, 0); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | STATIC void |
| 365 | xfs_submit_ioend_bio( |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 366 | struct writeback_control *wbc, |
| 367 | xfs_ioend_t *ioend, |
| 368 | struct bio *bio) |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 369 | { |
| 370 | atomic_inc(&ioend->io_remaining); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 371 | bio->bi_private = ioend; |
| 372 | bio->bi_end_io = xfs_end_bio; |
| 373 | |
Dave Chinner | 932640e | 2009-10-06 20:29:29 +0000 | [diff] [blame] | 374 | /* |
| 375 | * If the I/O is beyond EOF we mark the inode dirty immediately |
| 376 | * but don't update the inode size until I/O completion. |
| 377 | */ |
| 378 | if (xfs_ioend_new_eof(ioend)) |
Christoph Hellwig | 66d834e | 2010-02-15 09:44:49 +0000 | [diff] [blame] | 379 | xfs_mark_inode_dirty(XFS_I(ioend->io_inode)); |
Dave Chinner | 932640e | 2009-10-06 20:29:29 +0000 | [diff] [blame] | 380 | |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 381 | submit_bio(wbc->sync_mode == WB_SYNC_ALL ? |
| 382 | WRITE_SYNC_PLUG : WRITE, bio); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 383 | ASSERT(!bio_flagged(bio, BIO_EOPNOTSUPP)); |
| 384 | bio_put(bio); |
| 385 | } |
| 386 | |
| 387 | STATIC struct bio * |
| 388 | xfs_alloc_ioend_bio( |
| 389 | struct buffer_head *bh) |
| 390 | { |
| 391 | struct bio *bio; |
| 392 | int nvecs = bio_get_nr_vecs(bh->b_bdev); |
| 393 | |
| 394 | do { |
| 395 | bio = bio_alloc(GFP_NOIO, nvecs); |
| 396 | nvecs >>= 1; |
| 397 | } while (!bio); |
| 398 | |
| 399 | ASSERT(bio->bi_private == NULL); |
| 400 | bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9); |
| 401 | bio->bi_bdev = bh->b_bdev; |
| 402 | bio_get(bio); |
| 403 | return bio; |
| 404 | } |
| 405 | |
| 406 | STATIC void |
| 407 | xfs_start_buffer_writeback( |
| 408 | struct buffer_head *bh) |
| 409 | { |
| 410 | ASSERT(buffer_mapped(bh)); |
| 411 | ASSERT(buffer_locked(bh)); |
| 412 | ASSERT(!buffer_delay(bh)); |
| 413 | ASSERT(!buffer_unwritten(bh)); |
| 414 | |
| 415 | mark_buffer_async_write(bh); |
| 416 | set_buffer_uptodate(bh); |
| 417 | clear_buffer_dirty(bh); |
| 418 | } |
| 419 | |
| 420 | STATIC void |
| 421 | xfs_start_page_writeback( |
| 422 | struct page *page, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 423 | int clear_dirty, |
| 424 | int buffers) |
| 425 | { |
| 426 | ASSERT(PageLocked(page)); |
| 427 | ASSERT(!PageWriteback(page)); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 428 | if (clear_dirty) |
David Chinner | 9213202 | 2006-12-21 10:24:01 +1100 | [diff] [blame] | 429 | clear_page_dirty_for_io(page); |
| 430 | set_page_writeback(page); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 431 | unlock_page(page); |
Fengguang Wu | 1f7decf | 2007-10-16 23:30:42 -0700 | [diff] [blame] | 432 | /* If no buffers on the page are to be written, finish it here */ |
| 433 | if (!buffers) |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 434 | end_page_writeback(page); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh) |
| 438 | { |
| 439 | return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh)); |
| 440 | } |
| 441 | |
| 442 | /* |
David Chinner | d88992f | 2006-01-18 13:38:12 +1100 | [diff] [blame] | 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. |
| 445 | * |
| 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. |
| 451 | * |
| 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. |
| 455 | * |
| 456 | * The fix is two passes across the ioend list - one to start writeback on the |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 457 | * buffer_heads, and then submit them for I/O on the second pass. |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 458 | */ |
| 459 | STATIC void |
| 460 | xfs_submit_ioend( |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 461 | struct writeback_control *wbc, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 462 | xfs_ioend_t *ioend) |
| 463 | { |
David Chinner | d88992f | 2006-01-18 13:38:12 +1100 | [diff] [blame] | 464 | xfs_ioend_t *head = ioend; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 465 | xfs_ioend_t *next; |
| 466 | struct buffer_head *bh; |
| 467 | struct bio *bio; |
| 468 | sector_t lastblock = 0; |
| 469 | |
David Chinner | d88992f | 2006-01-18 13:38:12 +1100 | [diff] [blame] | 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 | } |
| 476 | } while ((ioend = next) != NULL); |
| 477 | |
| 478 | /* Pass 2 - submit I/O */ |
| 479 | ioend = head; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 480 | do { |
| 481 | next = ioend->io_list; |
| 482 | bio = NULL; |
| 483 | |
| 484 | for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 485 | |
| 486 | if (!bio) { |
| 487 | retry: |
| 488 | bio = xfs_alloc_ioend_bio(bh); |
| 489 | } else if (bh->b_blocknr != lastblock + 1) { |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 490 | xfs_submit_ioend_bio(wbc, ioend, bio); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 491 | goto retry; |
| 492 | } |
| 493 | |
| 494 | if (bio_add_buffer(bio, bh) != bh->b_size) { |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 495 | xfs_submit_ioend_bio(wbc, ioend, bio); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 496 | goto retry; |
| 497 | } |
| 498 | |
| 499 | lastblock = bh->b_blocknr; |
| 500 | } |
| 501 | if (bio) |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 502 | xfs_submit_ioend_bio(wbc, ioend, bio); |
David Chinner | e927af9 | 2007-06-05 16:24:36 +1000 | [diff] [blame] | 503 | xfs_finish_ioend(ioend, 0); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 504 | } while ((ioend = next) != NULL); |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * Cancel submission of all buffer_heads so far in this endio. |
| 509 | * Toss the endio too. Only ever called for the initial page |
| 510 | * in a writepage request, so only ever one page. |
| 511 | */ |
| 512 | STATIC void |
| 513 | xfs_cancel_ioend( |
| 514 | xfs_ioend_t *ioend) |
| 515 | { |
| 516 | xfs_ioend_t *next; |
| 517 | struct buffer_head *bh, *next_bh; |
| 518 | |
| 519 | do { |
| 520 | next = ioend->io_list; |
| 521 | bh = ioend->io_buffer_head; |
| 522 | do { |
| 523 | next_bh = bh->b_private; |
| 524 | clear_buffer_async_write(bh); |
| 525 | unlock_buffer(bh); |
| 526 | } while ((bh = next_bh) != NULL); |
| 527 | |
Christoph Hellwig | 25e41b3 | 2008-12-03 12:20:39 +0100 | [diff] [blame] | 528 | xfs_ioend_wake(XFS_I(ioend->io_inode)); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 529 | mempool_free(ioend, xfs_ioend_pool); |
| 530 | } while ((ioend = next) != NULL); |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | * Test to see if we've been building up a completion structure for |
| 535 | * earlier buffers -- if so, we try to append to this ioend if we |
| 536 | * can, otherwise we finish off any current ioend and start another. |
| 537 | * Return true if we've finished the given ioend. |
| 538 | */ |
| 539 | STATIC void |
| 540 | xfs_add_to_ioend( |
| 541 | struct inode *inode, |
| 542 | struct buffer_head *bh, |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 543 | xfs_off_t offset, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 544 | unsigned int type, |
| 545 | xfs_ioend_t **result, |
| 546 | int need_ioend) |
| 547 | { |
| 548 | xfs_ioend_t *ioend = *result; |
| 549 | |
| 550 | if (!ioend || need_ioend || type != ioend->io_type) { |
| 551 | xfs_ioend_t *previous = *result; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 552 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 553 | ioend = xfs_alloc_ioend(inode, type); |
| 554 | ioend->io_offset = offset; |
| 555 | ioend->io_buffer_head = bh; |
| 556 | ioend->io_buffer_tail = bh; |
| 557 | if (previous) |
| 558 | previous->io_list = ioend; |
| 559 | *result = ioend; |
| 560 | } else { |
| 561 | ioend->io_buffer_tail->b_private = bh; |
| 562 | ioend->io_buffer_tail = bh; |
| 563 | } |
| 564 | |
| 565 | bh->b_private = NULL; |
| 566 | ioend->io_size += bh->b_size; |
| 567 | } |
| 568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | STATIC void |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 570 | xfs_map_buffer( |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 571 | struct inode *inode, |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 572 | struct buffer_head *bh, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 573 | struct xfs_bmbt_irec *imap, |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 574 | xfs_off_t offset) |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 575 | { |
| 576 | sector_t bn; |
Christoph Hellwig | 8699bb0 | 2010-04-28 12:28:54 +0000 | [diff] [blame] | 577 | struct xfs_mount *m = XFS_I(inode)->i_mount; |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 578 | xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff); |
| 579 | xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock); |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 580 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 581 | ASSERT(imap->br_startblock != HOLESTARTBLOCK); |
| 582 | ASSERT(imap->br_startblock != DELAYSTARTBLOCK); |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 583 | |
Christoph Hellwig | e513182 | 2010-04-28 12:28:55 +0000 | [diff] [blame] | 584 | bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) + |
Christoph Hellwig | 8699bb0 | 2010-04-28 12:28:54 +0000 | [diff] [blame] | 585 | ((offset - iomap_offset) >> inode->i_blkbits); |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 586 | |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 587 | ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode))); |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 588 | |
| 589 | bh->b_blocknr = bn; |
| 590 | set_buffer_mapped(bh); |
| 591 | } |
| 592 | |
| 593 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | xfs_map_at_offset( |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 595 | struct inode *inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | struct buffer_head *bh, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 597 | struct xfs_bmbt_irec *imap, |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 598 | xfs_off_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | { |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 600 | ASSERT(imap->br_startblock != HOLESTARTBLOCK); |
| 601 | ASSERT(imap->br_startblock != DELAYSTARTBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
| 603 | lock_buffer(bh); |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 604 | xfs_map_buffer(inode, bh, imap, offset); |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 605 | bh->b_bdev = xfs_find_bdev_for_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | set_buffer_mapped(bh); |
| 607 | clear_buffer_delay(bh); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 608 | clear_buffer_unwritten(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /* |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 612 | * Look for a page at index that is suitable for clustering. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | */ |
| 614 | STATIC unsigned int |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 615 | xfs_probe_page( |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 616 | struct page *page, |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 617 | unsigned int pg_offset, |
| 618 | int mapped) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | int ret = 0; |
| 621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | if (PageWriteback(page)) |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 623 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
| 625 | if (page->mapping && PageDirty(page)) { |
| 626 | if (page_has_buffers(page)) { |
| 627 | struct buffer_head *bh, *head; |
| 628 | |
| 629 | bh = head = page_buffers(page); |
| 630 | do { |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 631 | if (!buffer_uptodate(bh)) |
| 632 | break; |
| 633 | if (mapped != buffer_mapped(bh)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | break; |
| 635 | ret += bh->b_size; |
| 636 | if (ret >= pg_offset) |
| 637 | break; |
| 638 | } while ((bh = bh->b_this_page) != head); |
| 639 | } else |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 640 | ret = mapped ? 0 : PAGE_CACHE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | return ret; |
| 644 | } |
| 645 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 646 | STATIC size_t |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 647 | xfs_probe_cluster( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | struct inode *inode, |
| 649 | struct page *startpage, |
| 650 | struct buffer_head *bh, |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 651 | struct buffer_head *head, |
| 652 | int mapped) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | { |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 654 | struct pagevec pvec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | pgoff_t tindex, tlast, tloff; |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 656 | size_t total = 0; |
| 657 | int done = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
| 659 | /* First sum forwards in this page */ |
| 660 | do { |
Eric Sandeen | 2353e8e | 2006-02-28 12:30:30 +1100 | [diff] [blame] | 661 | if (!buffer_uptodate(bh) || (mapped != buffer_mapped(bh))) |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 662 | return total; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | total += bh->b_size; |
| 664 | } while ((bh = bh->b_this_page) != head); |
| 665 | |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 666 | /* if we reached the end of the page, sum forwards in following pages */ |
| 667 | tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT; |
| 668 | tindex = startpage->index + 1; |
| 669 | |
| 670 | /* Prune this back to avoid pathological behavior */ |
| 671 | tloff = min(tlast, startpage->index + 64); |
| 672 | |
| 673 | pagevec_init(&pvec, 0); |
| 674 | while (!done && tindex <= tloff) { |
| 675 | unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1); |
| 676 | |
| 677 | if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len)) |
| 678 | break; |
| 679 | |
| 680 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 681 | struct page *page = pvec.pages[i]; |
Christoph Hellwig | 265c1fa | 2007-08-16 15:38:19 +1000 | [diff] [blame] | 682 | size_t pg_offset, pg_len = 0; |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 683 | |
| 684 | if (tindex == tlast) { |
| 685 | pg_offset = |
| 686 | i_size_read(inode) & (PAGE_CACHE_SIZE - 1); |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 687 | if (!pg_offset) { |
| 688 | done = 1; |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 689 | break; |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 690 | } |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 691 | } else |
| 692 | pg_offset = PAGE_CACHE_SIZE; |
| 693 | |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 694 | if (page->index == tindex && trylock_page(page)) { |
Christoph Hellwig | 265c1fa | 2007-08-16 15:38:19 +1000 | [diff] [blame] | 695 | pg_len = xfs_probe_page(page, pg_offset, mapped); |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 696 | unlock_page(page); |
| 697 | } |
| 698 | |
Christoph Hellwig | 265c1fa | 2007-08-16 15:38:19 +1000 | [diff] [blame] | 699 | if (!pg_len) { |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 700 | done = 1; |
| 701 | break; |
| 702 | } |
| 703 | |
Christoph Hellwig | 265c1fa | 2007-08-16 15:38:19 +1000 | [diff] [blame] | 704 | total += pg_len; |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 705 | tindex++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | } |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 707 | |
| 708 | pagevec_release(&pvec); |
| 709 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | } |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | return total; |
| 713 | } |
| 714 | |
| 715 | /* |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 716 | * Test if a given page is suitable for writing as part of an unwritten |
| 717 | * or delayed allocate extent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | */ |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 719 | STATIC int |
| 720 | xfs_is_delayed_page( |
| 721 | struct page *page, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 722 | unsigned int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | if (PageWriteback(page)) |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 725 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
| 727 | if (page->mapping && page_has_buffers(page)) { |
| 728 | struct buffer_head *bh, *head; |
| 729 | int acceptable = 0; |
| 730 | |
| 731 | bh = head = page_buffers(page); |
| 732 | do { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 733 | if (buffer_unwritten(bh)) |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 734 | acceptable = (type == IO_UNWRITTEN); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 735 | else if (buffer_delay(bh)) |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 736 | acceptable = (type == IO_DELAY); |
David Chinner | 2ddee84 | 2006-03-22 12:47:40 +1100 | [diff] [blame] | 737 | else if (buffer_dirty(bh) && buffer_mapped(bh)) |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 738 | acceptable = (type == IO_NEW); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 739 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | } while ((bh = bh->b_this_page) != head); |
| 742 | |
| 743 | if (acceptable) |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 744 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 747 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | /* |
| 751 | * Allocate & map buffers for page given the extent map. Write it out. |
| 752 | * except for the original page of a writepage, this is called on |
| 753 | * delalloc/unwritten pages only, for the original page it is possible |
| 754 | * that the page has no mapping at all. |
| 755 | */ |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 756 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | xfs_convert_page( |
| 758 | struct inode *inode, |
| 759 | struct page *page, |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 760 | loff_t tindex, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 761 | struct xfs_bmbt_irec *imap, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 762 | xfs_ioend_t **ioendp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | struct writeback_control *wbc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | int startio, |
| 765 | int all_bh) |
| 766 | { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 767 | struct buffer_head *bh, *head; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 768 | xfs_off_t end_offset; |
| 769 | unsigned long p_offset; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 770 | unsigned int type; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 771 | int len, page_dirty; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 772 | int count = 0, done = 0, uptodate = 1; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 773 | xfs_off_t offset = page_offset(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 775 | if (page->index != tindex) |
| 776 | goto fail; |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 777 | if (!trylock_page(page)) |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 778 | goto fail; |
| 779 | if (PageWriteback(page)) |
| 780 | goto fail_unlock_page; |
| 781 | if (page->mapping != inode->i_mapping) |
| 782 | goto fail_unlock_page; |
| 783 | if (!xfs_is_delayed_page(page, (*ioendp)->io_type)) |
| 784 | goto fail_unlock_page; |
| 785 | |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 786 | /* |
| 787 | * page_dirty is initially a count of buffers on the page before |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 788 | * EOF and is decremented as we move each into a cleanable state. |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 789 | * |
| 790 | * Derivation: |
| 791 | * |
| 792 | * End offset is the highest offset that this page should represent. |
| 793 | * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1)) |
| 794 | * will evaluate non-zero and be less than PAGE_CACHE_SIZE and |
| 795 | * hence give us the correct page_dirty count. On any other page, |
| 796 | * it will be zero and in that case we need page_dirty to be the |
| 797 | * count of buffers on the page. |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 798 | */ |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 799 | end_offset = min_t(unsigned long long, |
| 800 | (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, |
| 801 | i_size_read(inode)); |
| 802 | |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 803 | len = 1 << inode->i_blkbits; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 804 | p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1), |
| 805 | PAGE_CACHE_SIZE); |
| 806 | p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE; |
| 807 | page_dirty = p_offset / len; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | bh = head = page_buffers(page); |
| 810 | do { |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 811 | if (offset >= end_offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | break; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 813 | if (!buffer_uptodate(bh)) |
| 814 | uptodate = 0; |
| 815 | if (!(PageUptodate(page) || buffer_uptodate(bh))) { |
| 816 | done = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | continue; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 818 | } |
| 819 | |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 820 | if (buffer_unwritten(bh) || buffer_delay(bh)) { |
| 821 | if (buffer_unwritten(bh)) |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 822 | type = IO_UNWRITTEN; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 823 | else |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 824 | type = IO_DELAY; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 825 | |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 826 | if (!xfs_imap_valid(inode, imap, offset)) { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 827 | done = 1; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 828 | continue; |
| 829 | } |
| 830 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 831 | ASSERT(imap->br_startblock != HOLESTARTBLOCK); |
| 832 | ASSERT(imap->br_startblock != DELAYSTARTBLOCK); |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 833 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 834 | xfs_map_at_offset(inode, bh, imap, offset); |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 835 | if (startio) { |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 836 | xfs_add_to_ioend(inode, bh, offset, |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 837 | type, ioendp, done); |
| 838 | } else { |
| 839 | set_buffer_dirty(bh); |
| 840 | unlock_buffer(bh); |
| 841 | mark_buffer_dirty(bh); |
| 842 | } |
| 843 | page_dirty--; |
| 844 | count++; |
| 845 | } else { |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 846 | type = IO_NEW; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 847 | if (buffer_mapped(bh) && all_bh && startio) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | lock_buffer(bh); |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 849 | xfs_add_to_ioend(inode, bh, offset, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 850 | type, ioendp, done); |
| 851 | count++; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 852 | page_dirty--; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 853 | } else { |
| 854 | done = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | } |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 857 | } while (offset += len, (bh = bh->b_this_page) != head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 859 | if (uptodate && bh == head) |
| 860 | SetPageUptodate(page); |
| 861 | |
| 862 | if (startio) { |
Christoph Hellwig | f5e596b | 2006-01-11 20:49:42 +1100 | [diff] [blame] | 863 | if (count) { |
David Chinner | 9fddaca | 2006-02-07 20:27:24 +1100 | [diff] [blame] | 864 | wbc->nr_to_write--; |
Wu Fengguang | 0d99519 | 2009-12-03 13:54:25 +0100 | [diff] [blame] | 865 | if (wbc->nr_to_write <= 0) |
Christoph Hellwig | f5e596b | 2006-01-11 20:49:42 +1100 | [diff] [blame] | 866 | done = 1; |
Christoph Hellwig | f5e596b | 2006-01-11 20:49:42 +1100 | [diff] [blame] | 867 | } |
Denys Vlasenko | b41759c | 2008-05-19 16:34:11 +1000 | [diff] [blame] | 868 | xfs_start_page_writeback(page, !page_dirty, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | } |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 870 | |
| 871 | return done; |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 872 | fail_unlock_page: |
| 873 | unlock_page(page); |
| 874 | fail: |
| 875 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | /* |
| 879 | * Convert & write out a cluster of pages in the same extent as defined |
| 880 | * by mp and following the start page. |
| 881 | */ |
| 882 | STATIC void |
| 883 | xfs_cluster_write( |
| 884 | struct inode *inode, |
| 885 | pgoff_t tindex, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 886 | struct xfs_bmbt_irec *imap, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 887 | xfs_ioend_t **ioendp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | struct writeback_control *wbc, |
| 889 | int startio, |
| 890 | int all_bh, |
| 891 | pgoff_t tlast) |
| 892 | { |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 893 | struct pagevec pvec; |
| 894 | int done = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 896 | pagevec_init(&pvec, 0); |
| 897 | while (!done && tindex <= tlast) { |
| 898 | unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1); |
| 899 | |
| 900 | if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | break; |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 902 | |
| 903 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 904 | done = xfs_convert_page(inode, pvec.pages[i], tindex++, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 905 | imap, ioendp, wbc, startio, all_bh); |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 906 | if (done) |
| 907 | break; |
| 908 | } |
| 909 | |
| 910 | pagevec_release(&pvec); |
| 911 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | } |
| 913 | } |
| 914 | |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 915 | STATIC void |
| 916 | xfs_vm_invalidatepage( |
| 917 | struct page *page, |
| 918 | unsigned long offset) |
| 919 | { |
| 920 | trace_xfs_invalidatepage(page->mapping->host, page, offset); |
| 921 | block_invalidatepage(page, offset); |
| 922 | } |
| 923 | |
| 924 | /* |
| 925 | * If the page has delalloc buffers on it, we need to punch them out before we |
| 926 | * invalidate the page. If we don't, we leave a stale delalloc mapping on the |
| 927 | * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read |
| 928 | * is done on that same region - the delalloc extent is returned when none is |
| 929 | * supposed to be there. |
| 930 | * |
| 931 | * We prevent this by truncating away the delalloc regions on the page before |
| 932 | * invalidating it. Because they are delalloc, we can do this without needing a |
| 933 | * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this |
| 934 | * truncation without a transaction as there is no space left for block |
| 935 | * reservation (typically why we see a ENOSPC in writeback). |
| 936 | * |
| 937 | * This is not a performance critical path, so for now just do the punching a |
| 938 | * buffer head at a time. |
| 939 | */ |
| 940 | STATIC void |
| 941 | xfs_aops_discard_page( |
| 942 | struct page *page) |
| 943 | { |
| 944 | struct inode *inode = page->mapping->host; |
| 945 | struct xfs_inode *ip = XFS_I(inode); |
| 946 | struct buffer_head *bh, *head; |
| 947 | loff_t offset = page_offset(page); |
| 948 | ssize_t len = 1 << inode->i_blkbits; |
| 949 | |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 950 | if (!xfs_is_delayed_page(page, IO_DELAY)) |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 951 | goto out_invalidate; |
| 952 | |
Dave Chinner | e8c3753 | 2010-03-15 02:36:35 +0000 | [diff] [blame] | 953 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 954 | goto out_invalidate; |
| 955 | |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 956 | xfs_fs_cmn_err(CE_ALERT, ip->i_mount, |
| 957 | "page discard on page %p, inode 0x%llx, offset %llu.", |
| 958 | page, ip->i_ino, offset); |
| 959 | |
| 960 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 961 | bh = head = page_buffers(page); |
| 962 | do { |
| 963 | int done; |
| 964 | xfs_fileoff_t offset_fsb; |
| 965 | xfs_bmbt_irec_t imap; |
| 966 | int nimaps = 1; |
| 967 | int error; |
| 968 | xfs_fsblock_t firstblock; |
| 969 | xfs_bmap_free_t flist; |
| 970 | |
| 971 | if (!buffer_delay(bh)) |
| 972 | goto next_buffer; |
| 973 | |
| 974 | offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset); |
| 975 | |
| 976 | /* |
| 977 | * Map the range first and check that it is a delalloc extent |
| 978 | * before trying to unmap the range. Otherwise we will be |
| 979 | * trying to remove a real extent (which requires a |
| 980 | * transaction) or a hole, which is probably a bad idea... |
| 981 | */ |
| 982 | error = xfs_bmapi(NULL, ip, offset_fsb, 1, |
| 983 | XFS_BMAPI_ENTIRE, NULL, 0, &imap, |
| 984 | &nimaps, NULL, NULL); |
| 985 | |
| 986 | if (error) { |
| 987 | /* something screwed, just bail */ |
Dave Chinner | e8c3753 | 2010-03-15 02:36:35 +0000 | [diff] [blame] | 988 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
| 989 | xfs_fs_cmn_err(CE_ALERT, ip->i_mount, |
| 990 | "page discard failed delalloc mapping lookup."); |
| 991 | } |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 992 | break; |
| 993 | } |
| 994 | if (!nimaps) { |
| 995 | /* nothing there */ |
| 996 | goto next_buffer; |
| 997 | } |
| 998 | if (imap.br_startblock != DELAYSTARTBLOCK) { |
| 999 | /* been converted, ignore */ |
| 1000 | goto next_buffer; |
| 1001 | } |
| 1002 | WARN_ON(imap.br_blockcount == 0); |
| 1003 | |
| 1004 | /* |
| 1005 | * Note: while we initialise the firstblock/flist pair, they |
| 1006 | * should never be used because blocks should never be |
| 1007 | * allocated or freed for a delalloc extent and hence we need |
| 1008 | * don't cancel or finish them after the xfs_bunmapi() call. |
| 1009 | */ |
| 1010 | xfs_bmap_init(&flist, &firstblock); |
| 1011 | error = xfs_bunmapi(NULL, ip, offset_fsb, 1, 0, 1, &firstblock, |
| 1012 | &flist, NULL, &done); |
| 1013 | |
| 1014 | ASSERT(!flist.xbf_count && !flist.xbf_first); |
| 1015 | if (error) { |
| 1016 | /* something screwed, just bail */ |
Dave Chinner | e8c3753 | 2010-03-15 02:36:35 +0000 | [diff] [blame] | 1017 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
| 1018 | xfs_fs_cmn_err(CE_ALERT, ip->i_mount, |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 1019 | "page discard unable to remove delalloc mapping."); |
Dave Chinner | e8c3753 | 2010-03-15 02:36:35 +0000 | [diff] [blame] | 1020 | } |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 1021 | break; |
| 1022 | } |
| 1023 | next_buffer: |
| 1024 | offset += len; |
| 1025 | |
| 1026 | } while ((bh = bh->b_this_page) != head); |
| 1027 | |
| 1028 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1029 | out_invalidate: |
| 1030 | xfs_vm_invalidatepage(page, 0); |
| 1031 | return; |
| 1032 | } |
| 1033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | /* |
| 1035 | * Calling this without startio set means we are being asked to make a dirty |
| 1036 | * page ready for freeing it's buffers. When called with startio set then |
| 1037 | * we are coming from writepage. |
| 1038 | * |
| 1039 | * When called with startio set it is important that we write the WHOLE |
| 1040 | * page if possible. |
| 1041 | * The bh->b_state's cannot know if any of the blocks or which block for |
| 1042 | * that matter are dirty due to mmap writes, and therefore bh uptodate is |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1043 | * only valid if the page itself isn't completely uptodate. Some layers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | * may clear the page dirty flag prior to calling write page, under the |
| 1045 | * assumption the entire page will be written out; by not writing out the |
| 1046 | * whole page the page can be reused before all valid dirty data is |
| 1047 | * written out. Note: in the case of a page that has been dirty'd by |
| 1048 | * mapwrite and but partially setup by block_prepare_write the |
| 1049 | * bh->b_states's will not agree and only ones setup by BPW/BCW will have |
| 1050 | * valid state, thus the whole page must be written out thing. |
| 1051 | */ |
| 1052 | |
| 1053 | STATIC int |
| 1054 | xfs_page_state_convert( |
| 1055 | struct inode *inode, |
| 1056 | struct page *page, |
| 1057 | struct writeback_control *wbc, |
| 1058 | int startio, |
| 1059 | int unmapped) /* also implies page uptodate */ |
| 1060 | { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1061 | struct buffer_head *bh, *head; |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1062 | struct xfs_bmbt_irec imap; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1063 | xfs_ioend_t *ioend = NULL, *iohead = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | loff_t offset; |
| 1065 | unsigned long p_offset = 0; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1066 | unsigned int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | __uint64_t end_offset; |
Christoph Hellwig | bd1556a | 2010-04-28 12:29:00 +0000 | [diff] [blame] | 1068 | pgoff_t end_index, last_index; |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 1069 | ssize_t size, len; |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1070 | int flags, err, imap_valid = 0, uptodate = 1; |
Nathan Scott | 8272145 | 2006-04-11 15:10:55 +1000 | [diff] [blame] | 1071 | int page_dirty, count = 0; |
| 1072 | int trylock = 0; |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 1073 | int all_bh = unmapped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | |
Nathan Scott | 8272145 | 2006-04-11 15:10:55 +1000 | [diff] [blame] | 1075 | if (startio) { |
| 1076 | if (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking) |
| 1077 | trylock |= BMAPI_TRYLOCK; |
| 1078 | } |
Daniel Moore | 3ba0815 | 2005-05-05 13:31:34 -0700 | [diff] [blame] | 1079 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | /* Is this page beyond the end of the file? */ |
| 1081 | offset = i_size_read(inode); |
| 1082 | end_index = offset >> PAGE_CACHE_SHIFT; |
| 1083 | last_index = (offset - 1) >> PAGE_CACHE_SHIFT; |
| 1084 | if (page->index >= end_index) { |
| 1085 | if ((page->index >= end_index + 1) || |
| 1086 | !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) { |
Nathan Scott | 19d5bcf | 2005-11-02 15:14:09 +1100 | [diff] [blame] | 1087 | if (startio) |
| 1088 | unlock_page(page); |
| 1089 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | } |
| 1091 | } |
| 1092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | /* |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 1094 | * page_dirty is initially a count of buffers on the page before |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1095 | * EOF and is decremented as we move each into a cleanable state. |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1096 | * |
| 1097 | * Derivation: |
| 1098 | * |
| 1099 | * End offset is the highest offset that this page should represent. |
| 1100 | * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1)) |
| 1101 | * will evaluate non-zero and be less than PAGE_CACHE_SIZE and |
| 1102 | * hence give us the correct page_dirty count. On any other page, |
| 1103 | * it will be zero and in that case we need page_dirty to be the |
| 1104 | * count of buffers on the page. |
| 1105 | */ |
| 1106 | end_offset = min_t(unsigned long long, |
| 1107 | (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset); |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 1108 | len = 1 << inode->i_blkbits; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1109 | p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1), |
| 1110 | PAGE_CACHE_SIZE); |
| 1111 | p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 1112 | page_dirty = p_offset / len; |
| 1113 | |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 1114 | bh = head = page_buffers(page); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1115 | offset = page_offset(page); |
David Chinner | df3c724 | 2007-05-24 15:27:03 +1000 | [diff] [blame] | 1116 | flags = BMAPI_READ; |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 1117 | type = IO_NEW; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1118 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1119 | /* TODO: cleanup count and page_dirty */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | |
| 1121 | do { |
| 1122 | if (offset >= end_offset) |
| 1123 | break; |
| 1124 | if (!buffer_uptodate(bh)) |
| 1125 | uptodate = 0; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1126 | if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) { |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 1127 | /* |
| 1128 | * the iomap is actually still valid, but the ioend |
| 1129 | * isn't. shouldn't happen too often. |
| 1130 | */ |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1131 | imap_valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | continue; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1133 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1135 | if (imap_valid) |
| 1136 | imap_valid = xfs_imap_valid(inode, &imap, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
| 1138 | /* |
| 1139 | * First case, map an unwritten extent and prepare for |
| 1140 | * extent state conversion transaction on completion. |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1141 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | * Second case, allocate space for a delalloc buffer. |
| 1143 | * We can return EAGAIN here in the release page case. |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 1144 | * |
| 1145 | * Third case, an unmapped buffer was found, and we are |
| 1146 | * in a path where we need to write the whole page out. |
David Chinner | df3c724 | 2007-05-24 15:27:03 +1000 | [diff] [blame] | 1147 | */ |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 1148 | if (buffer_unwritten(bh) || buffer_delay(bh) || |
| 1149 | ((buffer_uptodate(bh) || PageUptodate(page)) && |
| 1150 | !buffer_mapped(bh) && (unmapped || startio))) { |
David Chinner | effd120 | 2007-06-18 16:49:58 +1000 | [diff] [blame] | 1151 | int new_ioend = 0; |
| 1152 | |
David Chinner | df3c724 | 2007-05-24 15:27:03 +1000 | [diff] [blame] | 1153 | /* |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 1154 | * Make sure we don't use a read-only iomap |
| 1155 | */ |
David Chinner | df3c724 | 2007-05-24 15:27:03 +1000 | [diff] [blame] | 1156 | if (flags == BMAPI_READ) |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1157 | imap_valid = 0; |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 1158 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1159 | if (buffer_unwritten(bh)) { |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 1160 | type = IO_UNWRITTEN; |
Nathan Scott | 8272145 | 2006-04-11 15:10:55 +1000 | [diff] [blame] | 1161 | flags = BMAPI_WRITE | BMAPI_IGNSTATE; |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 1162 | } else if (buffer_delay(bh)) { |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 1163 | type = IO_DELAY; |
Nathan Scott | 8272145 | 2006-04-11 15:10:55 +1000 | [diff] [blame] | 1164 | flags = BMAPI_ALLOCATE | trylock; |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 1165 | } else { |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 1166 | type = IO_NEW; |
Nathan Scott | 8272145 | 2006-04-11 15:10:55 +1000 | [diff] [blame] | 1167 | flags = BMAPI_WRITE | BMAPI_MMAP; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1168 | } |
| 1169 | |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1170 | if (!imap_valid) { |
David Chinner | effd120 | 2007-06-18 16:49:58 +1000 | [diff] [blame] | 1171 | /* |
| 1172 | * if we didn't have a valid mapping then we |
| 1173 | * need to ensure that we put the new mapping |
| 1174 | * in a new ioend structure. This needs to be |
| 1175 | * done to ensure that the ioends correctly |
| 1176 | * reflect the block mappings at io completion |
| 1177 | * for unwritten extent conversion. |
| 1178 | */ |
| 1179 | new_ioend = 1; |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 1180 | if (type == IO_NEW) { |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 1181 | size = xfs_probe_cluster(inode, |
| 1182 | page, bh, head, 0); |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 1183 | } else { |
| 1184 | size = len; |
| 1185 | } |
| 1186 | |
| 1187 | err = xfs_map_blocks(inode, offset, size, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1188 | &imap, flags); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1189 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | goto error; |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1191 | imap_valid = xfs_imap_valid(inode, &imap, |
| 1192 | offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | } |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1194 | if (imap_valid) { |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1195 | xfs_map_at_offset(inode, bh, &imap, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | if (startio) { |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 1197 | xfs_add_to_ioend(inode, bh, offset, |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 1198 | type, &ioend, |
David Chinner | effd120 | 2007-06-18 16:49:58 +1000 | [diff] [blame] | 1199 | new_ioend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | } else { |
| 1201 | set_buffer_dirty(bh); |
| 1202 | unlock_buffer(bh); |
| 1203 | mark_buffer_dirty(bh); |
| 1204 | } |
| 1205 | page_dirty--; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1206 | count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | } |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 1208 | } else if (buffer_uptodate(bh) && startio) { |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 1209 | /* |
| 1210 | * we got here because the buffer is already mapped. |
| 1211 | * That means it must already have extents allocated |
| 1212 | * underneath it. Map the extent by reading it. |
| 1213 | */ |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1214 | if (!imap_valid || flags != BMAPI_READ) { |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 1215 | flags = BMAPI_READ; |
| 1216 | size = xfs_probe_cluster(inode, page, bh, |
| 1217 | head, 1); |
| 1218 | err = xfs_map_blocks(inode, offset, size, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1219 | &imap, flags); |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 1220 | if (err) |
| 1221 | goto error; |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1222 | imap_valid = xfs_imap_valid(inode, &imap, |
| 1223 | offset); |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 1224 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | |
David Chinner | df3c724 | 2007-05-24 15:27:03 +1000 | [diff] [blame] | 1226 | /* |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 1227 | * We set the type to IO_NEW in case we are doing a |
David Chinner | df3c724 | 2007-05-24 15:27:03 +1000 | [diff] [blame] | 1228 | * small write at EOF that is extending the file but |
| 1229 | * without needing an allocation. We need to update the |
| 1230 | * file size on I/O completion in this case so it is |
| 1231 | * the same case as having just allocated a new extent |
| 1232 | * that we are writing into for the first time. |
| 1233 | */ |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 1234 | type = IO_NEW; |
Nick Piggin | ca5de40 | 2008-08-02 12:02:13 +0200 | [diff] [blame] | 1235 | if (trylock_buffer(bh)) { |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 1236 | ASSERT(buffer_mapped(bh)); |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1237 | if (imap_valid) |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 1238 | all_bh = 1; |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 1239 | xfs_add_to_ioend(inode, bh, offset, type, |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1240 | &ioend, !imap_valid); |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 1241 | page_dirty--; |
| 1242 | count++; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1243 | } else { |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1244 | imap_valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | } |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 1246 | } else if ((buffer_uptodate(bh) || PageUptodate(page)) && |
| 1247 | (unmapped || startio)) { |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1248 | imap_valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | } |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1250 | |
| 1251 | if (!iohead) |
| 1252 | iohead = ioend; |
| 1253 | |
| 1254 | } while (offset += len, ((bh = bh->b_this_page) != head)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | |
| 1256 | if (uptodate && bh == head) |
| 1257 | SetPageUptodate(page); |
| 1258 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1259 | if (startio) |
Denys Vlasenko | b41759c | 2008-05-19 16:34:11 +1000 | [diff] [blame] | 1260 | xfs_start_page_writeback(page, 1, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1262 | if (ioend && imap_valid) { |
Christoph Hellwig | bd1556a | 2010-04-28 12:29:00 +0000 | [diff] [blame] | 1263 | xfs_off_t end_index; |
Christoph Hellwig | 8699bb0 | 2010-04-28 12:28:54 +0000 | [diff] [blame] | 1264 | |
Christoph Hellwig | bd1556a | 2010-04-28 12:29:00 +0000 | [diff] [blame] | 1265 | end_index = imap.br_startoff + imap.br_blockcount; |
| 1266 | |
| 1267 | /* to bytes */ |
| 1268 | end_index <<= inode->i_blkbits; |
| 1269 | |
| 1270 | /* to pages */ |
| 1271 | end_index = (end_index - 1) >> PAGE_CACHE_SHIFT; |
| 1272 | |
| 1273 | /* check against file size */ |
| 1274 | if (end_index > last_index) |
| 1275 | end_index = last_index; |
| 1276 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1277 | xfs_cluster_write(inode, page->index + 1, &imap, &ioend, |
Christoph Hellwig | bd1556a | 2010-04-28 12:29:00 +0000 | [diff] [blame] | 1278 | wbc, startio, all_bh, end_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1281 | if (iohead) |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 1282 | xfs_submit_ioend(wbc, iohead); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | return page_dirty; |
| 1285 | |
| 1286 | error: |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1287 | if (iohead) |
| 1288 | xfs_cancel_ioend(iohead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | |
| 1290 | /* |
| 1291 | * If it's delalloc and we have nowhere to put it, |
| 1292 | * throw it away, unless the lower layers told |
| 1293 | * us to try again. |
| 1294 | */ |
| 1295 | if (err != -EAGAIN) { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1296 | if (!unmapped) |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 1297 | xfs_aops_discard_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | ClearPageUptodate(page); |
| 1299 | } |
| 1300 | return err; |
| 1301 | } |
| 1302 | |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1303 | /* |
| 1304 | * writepage: Called from one of two places: |
| 1305 | * |
| 1306 | * 1. we are flushing a delalloc buffer head. |
| 1307 | * |
| 1308 | * 2. we are writing out a dirty page. Typically the page dirty |
| 1309 | * state is cleared before we get here. In this case is it |
| 1310 | * conceivable we have no buffer heads. |
| 1311 | * |
| 1312 | * For delalloc space on the page we need to allocate space and |
| 1313 | * flush it. For unmapped buffer heads on the page we should |
| 1314 | * allocate space if the page is uptodate. For any other dirty |
| 1315 | * buffer heads on the page we should flush them. |
| 1316 | * |
| 1317 | * If we detect that a transaction would be required to flush |
| 1318 | * the page, we have to check the process flags first, if we |
| 1319 | * are already in a transaction or disk I/O during allocations |
| 1320 | * is off, we need to fail the writepage and redirty the page. |
| 1321 | */ |
| 1322 | |
| 1323 | STATIC int |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1324 | xfs_vm_writepage( |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1325 | struct page *page, |
| 1326 | struct writeback_control *wbc) |
| 1327 | { |
| 1328 | int error; |
| 1329 | int need_trans; |
| 1330 | int delalloc, unmapped, unwritten; |
| 1331 | struct inode *inode = page->mapping->host; |
| 1332 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1333 | trace_xfs_writepage(inode, page, 0); |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1334 | |
| 1335 | /* |
Christoph Hellwig | 070ecdc | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1336 | * Refuse to write the page out if we are called from reclaim context. |
| 1337 | * |
| 1338 | * This is primarily to avoid stack overflows when called from deep |
| 1339 | * used stacks in random callers for direct reclaim, but disabling |
| 1340 | * reclaim for kswap is a nice side-effect as kswapd causes rather |
| 1341 | * suboptimal I/O patters, too. |
| 1342 | * |
| 1343 | * This should really be done by the core VM, but until that happens |
| 1344 | * filesystems like XFS, btrfs and ext4 have to take care of this |
| 1345 | * by themselves. |
| 1346 | */ |
| 1347 | if (current->flags & PF_MEMALLOC) |
| 1348 | goto out_fail; |
| 1349 | |
| 1350 | /* |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1351 | * We need a transaction if: |
| 1352 | * 1. There are delalloc buffers on the page |
| 1353 | * 2. The page is uptodate and we have unmapped buffers |
| 1354 | * 3. The page is uptodate and we have no buffers |
| 1355 | * 4. There are unwritten buffers on the page |
| 1356 | */ |
| 1357 | |
| 1358 | if (!page_has_buffers(page)) { |
| 1359 | unmapped = 1; |
| 1360 | need_trans = 1; |
| 1361 | } else { |
| 1362 | xfs_count_page_state(page, &delalloc, &unmapped, &unwritten); |
| 1363 | if (!PageUptodate(page)) |
| 1364 | unmapped = 0; |
| 1365 | need_trans = delalloc + unmapped + unwritten; |
| 1366 | } |
| 1367 | |
| 1368 | /* |
| 1369 | * If we need a transaction and the process flags say |
| 1370 | * we are already in a transaction, or no IO is allowed |
| 1371 | * then mark the page dirty again and leave the page |
| 1372 | * as is. |
| 1373 | */ |
Nathan Scott | 59c1b08 | 2006-06-09 14:59:13 +1000 | [diff] [blame] | 1374 | if (current_test_flags(PF_FSTRANS) && need_trans) |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1375 | goto out_fail; |
| 1376 | |
| 1377 | /* |
| 1378 | * Delay hooking up buffer heads until we have |
| 1379 | * made our go/no-go decision. |
| 1380 | */ |
| 1381 | if (!page_has_buffers(page)) |
| 1382 | create_empty_buffers(page, 1 << inode->i_blkbits, 0); |
| 1383 | |
| 1384 | /* |
| 1385 | * Convert delayed allocate, unwritten or unmapped space |
| 1386 | * to real space and flush out to disk. |
| 1387 | */ |
| 1388 | error = xfs_page_state_convert(inode, page, wbc, 1, unmapped); |
| 1389 | if (error == -EAGAIN) |
| 1390 | goto out_fail; |
| 1391 | if (unlikely(error < 0)) |
| 1392 | goto out_unlock; |
| 1393 | |
| 1394 | return 0; |
| 1395 | |
| 1396 | out_fail: |
| 1397 | redirty_page_for_writepage(wbc, page); |
| 1398 | unlock_page(page); |
| 1399 | return 0; |
| 1400 | out_unlock: |
| 1401 | unlock_page(page); |
| 1402 | return error; |
| 1403 | } |
| 1404 | |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 1405 | STATIC int |
| 1406 | xfs_vm_writepages( |
| 1407 | struct address_space *mapping, |
| 1408 | struct writeback_control *wbc) |
| 1409 | { |
Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 1410 | xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED); |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 1411 | return generic_writepages(mapping, wbc); |
| 1412 | } |
| 1413 | |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1414 | /* |
| 1415 | * Called to move a page into cleanable state - and from there |
| 1416 | * to be released. Possibly the page is already clean. We always |
| 1417 | * have buffer heads in this call. |
| 1418 | * |
| 1419 | * Returns 0 if the page is ok to release, 1 otherwise. |
| 1420 | * |
| 1421 | * Possible scenarios are: |
| 1422 | * |
| 1423 | * 1. We are being called to release a page which has been written |
| 1424 | * to via regular I/O. buffer heads will be dirty and possibly |
| 1425 | * delalloc. If no delalloc buffer heads in this case then we |
| 1426 | * can just return zero. |
| 1427 | * |
| 1428 | * 2. We are called to release a page which has been written via |
| 1429 | * mmap, all we need to do is ensure there is no delalloc |
| 1430 | * state in the buffer heads, if not we can let the caller |
| 1431 | * free them and we should come back later via writepage. |
| 1432 | */ |
| 1433 | STATIC int |
Nathan Scott | 238f4c5 | 2006-03-17 17:26:25 +1100 | [diff] [blame] | 1434 | xfs_vm_releasepage( |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1435 | struct page *page, |
| 1436 | gfp_t gfp_mask) |
| 1437 | { |
| 1438 | struct inode *inode = page->mapping->host; |
| 1439 | int dirty, delalloc, unmapped, unwritten; |
| 1440 | struct writeback_control wbc = { |
| 1441 | .sync_mode = WB_SYNC_ALL, |
| 1442 | .nr_to_write = 1, |
| 1443 | }; |
| 1444 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1445 | trace_xfs_releasepage(inode, page, 0); |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1446 | |
Nathan Scott | 238f4c5 | 2006-03-17 17:26:25 +1100 | [diff] [blame] | 1447 | if (!page_has_buffers(page)) |
| 1448 | return 0; |
| 1449 | |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1450 | xfs_count_page_state(page, &delalloc, &unmapped, &unwritten); |
| 1451 | if (!delalloc && !unwritten) |
| 1452 | goto free_buffers; |
| 1453 | |
| 1454 | if (!(gfp_mask & __GFP_FS)) |
| 1455 | return 0; |
| 1456 | |
| 1457 | /* If we are already inside a transaction or the thread cannot |
| 1458 | * do I/O, we cannot release this page. |
| 1459 | */ |
Nathan Scott | 59c1b08 | 2006-06-09 14:59:13 +1000 | [diff] [blame] | 1460 | if (current_test_flags(PF_FSTRANS)) |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1461 | return 0; |
| 1462 | |
| 1463 | /* |
| 1464 | * Convert delalloc space to real space, do not flush the |
| 1465 | * data out to disk, that will be done by the caller. |
| 1466 | * Never need to allocate space here - we will always |
| 1467 | * come back to writepage in that case. |
| 1468 | */ |
| 1469 | dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0); |
| 1470 | if (dirty == 0 && !unwritten) |
| 1471 | goto free_buffers; |
| 1472 | return 0; |
| 1473 | |
| 1474 | free_buffers: |
| 1475 | return try_to_free_buffers(page); |
| 1476 | } |
| 1477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | STATIC int |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1479 | __xfs_get_blocks( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | struct inode *inode, |
| 1481 | sector_t iblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | struct buffer_head *bh_result, |
| 1483 | int create, |
| 1484 | int direct, |
| 1485 | bmapi_flags_t flags) |
| 1486 | { |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1487 | struct xfs_bmbt_irec imap; |
Nathan Scott | fdc7ed7 | 2005-11-02 15:13:13 +1100 | [diff] [blame] | 1488 | xfs_off_t offset; |
| 1489 | ssize_t size; |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1490 | int nimap = 1; |
| 1491 | int new = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | |
Nathan Scott | fdc7ed7 | 2005-11-02 15:13:13 +1100 | [diff] [blame] | 1494 | offset = (xfs_off_t)iblock << inode->i_blkbits; |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1495 | ASSERT(bh_result->b_size >= (1 << inode->i_blkbits)); |
| 1496 | size = bh_result->b_size; |
Lachlan McIlroy | 364f358 | 2008-09-17 16:50:14 +1000 | [diff] [blame] | 1497 | |
| 1498 | if (!create && direct && offset >= i_size_read(inode)) |
| 1499 | return 0; |
| 1500 | |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 1501 | error = xfs_iomap(XFS_I(inode), offset, size, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1502 | create ? flags : BMAPI_READ, &imap, &nimap, &new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | if (error) |
| 1504 | return -error; |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1505 | if (nimap == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | return 0; |
| 1507 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1508 | if (imap.br_startblock != HOLESTARTBLOCK && |
| 1509 | imap.br_startblock != DELAYSTARTBLOCK) { |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 1510 | /* |
| 1511 | * For unwritten extents do not report a disk address on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | * the read case (treat as if we're reading into a hole). |
| 1513 | */ |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1514 | if (create || !ISUNWRITTEN(&imap)) |
| 1515 | xfs_map_buffer(inode, bh_result, &imap, offset); |
| 1516 | if (create && ISUNWRITTEN(&imap)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | if (direct) |
| 1518 | bh_result->b_private = inode; |
| 1519 | set_buffer_unwritten(bh_result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | } |
| 1521 | } |
| 1522 | |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1523 | /* |
| 1524 | * If this is a realtime file, data may be on a different device. |
| 1525 | * to that pointed to from the buffer_head b_bdev currently. |
| 1526 | */ |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 1527 | bh_result->b_bdev = xfs_find_bdev_for_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1529 | /* |
David Chinner | 549054a | 2007-02-10 18:36:35 +1100 | [diff] [blame] | 1530 | * If we previously allocated a block out beyond eof and we are now |
| 1531 | * coming back to use it then we will need to flag it as new even if it |
| 1532 | * has a disk address. |
| 1533 | * |
| 1534 | * With sub-block writes into unwritten extents we also need to mark |
| 1535 | * the buffer as new so that the unwritten parts of the buffer gets |
| 1536 | * correctly zeroed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | */ |
| 1538 | if (create && |
| 1539 | ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) || |
David Chinner | 549054a | 2007-02-10 18:36:35 +1100 | [diff] [blame] | 1540 | (offset >= i_size_read(inode)) || |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1541 | (new || ISUNWRITTEN(&imap)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | set_buffer_new(bh_result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1544 | if (imap.br_startblock == DELAYSTARTBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | BUG_ON(direct); |
| 1546 | if (create) { |
| 1547 | set_buffer_uptodate(bh_result); |
| 1548 | set_buffer_mapped(bh_result); |
| 1549 | set_buffer_delay(bh_result); |
| 1550 | } |
| 1551 | } |
| 1552 | |
Christoph Hellwig | 2b8f12b | 2010-04-28 12:28:59 +0000 | [diff] [blame] | 1553 | /* |
| 1554 | * If this is O_DIRECT or the mpage code calling tell them how large |
| 1555 | * the mapping is, so that we can avoid repeated get_blocks calls. |
| 1556 | */ |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1557 | if (direct || size > (1 << inode->i_blkbits)) { |
Christoph Hellwig | 2b8f12b | 2010-04-28 12:28:59 +0000 | [diff] [blame] | 1558 | xfs_off_t mapping_size; |
Christoph Hellwig | 9563b3d | 2010-04-28 12:28:53 +0000 | [diff] [blame] | 1559 | |
Christoph Hellwig | 2b8f12b | 2010-04-28 12:28:59 +0000 | [diff] [blame] | 1560 | mapping_size = imap.br_startoff + imap.br_blockcount - iblock; |
| 1561 | mapping_size <<= inode->i_blkbits; |
| 1562 | |
| 1563 | ASSERT(mapping_size > 0); |
| 1564 | if (mapping_size > size) |
| 1565 | mapping_size = size; |
| 1566 | if (mapping_size > LONG_MAX) |
| 1567 | mapping_size = LONG_MAX; |
| 1568 | |
| 1569 | bh_result->b_size = mapping_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | return 0; |
| 1573 | } |
| 1574 | |
| 1575 | int |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1576 | xfs_get_blocks( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | struct inode *inode, |
| 1578 | sector_t iblock, |
| 1579 | struct buffer_head *bh_result, |
| 1580 | int create) |
| 1581 | { |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1582 | return __xfs_get_blocks(inode, iblock, |
Badari Pulavarty | fa30bd0 | 2006-03-26 01:38:01 -0800 | [diff] [blame] | 1583 | bh_result, create, 0, BMAPI_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | STATIC int |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1587 | xfs_get_blocks_direct( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | struct inode *inode, |
| 1589 | sector_t iblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | struct buffer_head *bh_result, |
| 1591 | int create) |
| 1592 | { |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1593 | return __xfs_get_blocks(inode, iblock, |
Badari Pulavarty | 1d8fa7a | 2006-03-26 01:38:02 -0800 | [diff] [blame] | 1594 | bh_result, create, 1, BMAPI_WRITE|BMAPI_DIRECT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | } |
| 1596 | |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1597 | STATIC void |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1598 | xfs_end_io_direct( |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1599 | struct kiocb *iocb, |
| 1600 | loff_t offset, |
| 1601 | ssize_t size, |
| 1602 | void *private) |
| 1603 | { |
| 1604 | xfs_ioend_t *ioend = iocb->private; |
| 1605 | |
| 1606 | /* |
| 1607 | * Non-NULL private data means we need to issue a transaction to |
| 1608 | * convert a range from unwritten to written extents. This needs |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1609 | * to happen from process context but aio+dio I/O completion |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1610 | * happens from irq context so we need to defer it to a workqueue. |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1611 | * This is not necessary for synchronous direct I/O, but we do |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1612 | * it anyway to keep the code uniform and simpler. |
| 1613 | * |
David Chinner | e927af9 | 2007-06-05 16:24:36 +1000 | [diff] [blame] | 1614 | * Well, if only it were that simple. Because synchronous direct I/O |
| 1615 | * requires extent conversion to occur *before* we return to userspace, |
| 1616 | * we have to wait for extent conversion to complete. Look at the |
| 1617 | * iocb that has been passed to us to determine if this is AIO or |
| 1618 | * not. If it is synchronous, tell xfs_finish_ioend() to kick the |
| 1619 | * workqueue and wait for it to complete. |
| 1620 | * |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1621 | * The core direct I/O code might be changed to always call the |
| 1622 | * completion handler in the future, in which case all this can |
| 1623 | * go away. |
| 1624 | */ |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1625 | ioend->io_offset = offset; |
| 1626 | ioend->io_size = size; |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 1627 | if (ioend->io_type == IO_READ) { |
David Chinner | e927af9 | 2007-06-05 16:24:36 +1000 | [diff] [blame] | 1628 | xfs_finish_ioend(ioend, 0); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1629 | } else if (private && size > 0) { |
David Chinner | e927af9 | 2007-06-05 16:24:36 +1000 | [diff] [blame] | 1630 | xfs_finish_ioend(ioend, is_sync_kiocb(iocb)); |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1631 | } else { |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1632 | /* |
| 1633 | * A direct I/O write ioend starts it's life in unwritten |
| 1634 | * state in case they map an unwritten extent. This write |
| 1635 | * didn't map an unwritten extent so switch it's completion |
| 1636 | * handler. |
| 1637 | */ |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 1638 | ioend->io_type = IO_NEW; |
David Chinner | e927af9 | 2007-06-05 16:24:36 +1000 | [diff] [blame] | 1639 | xfs_finish_ioend(ioend, 0); |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | /* |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1643 | * blockdev_direct_IO can return an error even after the I/O |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1644 | * completion handler was called. Thus we need to protect |
| 1645 | * against double-freeing. |
| 1646 | */ |
| 1647 | iocb->private = NULL; |
| 1648 | } |
| 1649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | STATIC ssize_t |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1651 | xfs_vm_direct_IO( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | int rw, |
| 1653 | struct kiocb *iocb, |
| 1654 | const struct iovec *iov, |
| 1655 | loff_t offset, |
| 1656 | unsigned long nr_segs) |
| 1657 | { |
| 1658 | struct file *file = iocb->ki_filp; |
| 1659 | struct inode *inode = file->f_mapping->host; |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 1660 | struct block_device *bdev; |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1661 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 1663 | bdev = xfs_find_bdev_for_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | |
Christoph Hellwig | 5fe878ae | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 1665 | iocb->private = xfs_alloc_ioend(inode, rw == WRITE ? |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 1666 | IO_UNWRITTEN : IO_READ); |
Christoph Hellwig | 5fe878ae | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 1667 | |
| 1668 | ret = blockdev_direct_IO_no_locking(rw, iocb, inode, bdev, iov, |
| 1669 | offset, nr_segs, |
| 1670 | xfs_get_blocks_direct, |
| 1671 | xfs_end_io_direct); |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1672 | |
Zach Brown | 8459d86 | 2006-12-10 02:21:05 -0800 | [diff] [blame] | 1673 | if (unlikely(ret != -EIOCBQUEUED && iocb->private)) |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1674 | xfs_destroy_ioend(iocb->private); |
| 1675 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | } |
| 1677 | |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1678 | STATIC int |
Nick Piggin | d79689c | 2007-10-16 01:25:06 -0700 | [diff] [blame] | 1679 | xfs_vm_write_begin( |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1680 | struct file *file, |
Nick Piggin | d79689c | 2007-10-16 01:25:06 -0700 | [diff] [blame] | 1681 | struct address_space *mapping, |
| 1682 | loff_t pos, |
| 1683 | unsigned len, |
| 1684 | unsigned flags, |
| 1685 | struct page **pagep, |
| 1686 | void **fsdata) |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1687 | { |
Nick Piggin | d79689c | 2007-10-16 01:25:06 -0700 | [diff] [blame] | 1688 | *pagep = NULL; |
| 1689 | return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, |
| 1690 | xfs_get_blocks); |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1691 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | |
| 1693 | STATIC sector_t |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1694 | xfs_vm_bmap( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | struct address_space *mapping, |
| 1696 | sector_t block) |
| 1697 | { |
| 1698 | struct inode *inode = (struct inode *)mapping->host; |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 1699 | struct xfs_inode *ip = XFS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 1701 | xfs_itrace_entry(XFS_I(inode)); |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 1702 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 1703 | xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF); |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 1704 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1705 | return generic_block_bmap(mapping, block, xfs_get_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | STATIC int |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1709 | xfs_vm_readpage( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | struct file *unused, |
| 1711 | struct page *page) |
| 1712 | { |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1713 | return mpage_readpage(page, xfs_get_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | STATIC int |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1717 | xfs_vm_readpages( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | struct file *unused, |
| 1719 | struct address_space *mapping, |
| 1720 | struct list_head *pages, |
| 1721 | unsigned nr_pages) |
| 1722 | { |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1723 | return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | } |
| 1725 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 1726 | const struct address_space_operations xfs_address_space_operations = { |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1727 | .readpage = xfs_vm_readpage, |
| 1728 | .readpages = xfs_vm_readpages, |
| 1729 | .writepage = xfs_vm_writepage, |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 1730 | .writepages = xfs_vm_writepages, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | .sync_page = block_sync_page, |
Nathan Scott | 238f4c5 | 2006-03-17 17:26:25 +1100 | [diff] [blame] | 1732 | .releasepage = xfs_vm_releasepage, |
| 1733 | .invalidatepage = xfs_vm_invalidatepage, |
Nick Piggin | d79689c | 2007-10-16 01:25:06 -0700 | [diff] [blame] | 1734 | .write_begin = xfs_vm_write_begin, |
| 1735 | .write_end = generic_write_end, |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1736 | .bmap = xfs_vm_bmap, |
| 1737 | .direct_IO = xfs_vm_direct_IO, |
Christoph Lameter | e965f96 | 2006-02-01 03:05:41 -0800 | [diff] [blame] | 1738 | .migratepage = buffer_migrate_page, |
Hisashi Hifumi | bddaafa | 2009-03-29 09:53:38 +0200 | [diff] [blame] | 1739 | .is_partially_uptodate = block_is_partially_uptodate, |
Andi Kleen | aa261f5 | 2009-09-16 11:50:16 +0200 | [diff] [blame] | 1740 | .error_remove_page = generic_error_remove_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | }; |