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_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_mount.h" |
| 26 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_dinode.h" |
| 28 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 29 | #include "xfs_alloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_error.h" |
| 31 | #include "xfs_rw.h" |
| 32 | #include "xfs_iomap.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 33 | #include "xfs_vnodeops.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 34 | #include "xfs_trace.h" |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 35 | #include "xfs_bmap.h" |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/mpage.h> |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 38 | #include <linux/pagevec.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/writeback.h> |
| 40 | |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 41 | /* |
| 42 | * Types of I/O for bmap clustering and I/O completion tracking. |
| 43 | */ |
| 44 | enum { |
| 45 | IO_READ, /* mapping for a read */ |
| 46 | IO_DELAY, /* mapping covers delalloc region */ |
| 47 | IO_UNWRITTEN, /* mapping covers allocated but uninitialized data */ |
| 48 | IO_NEW /* just allocated */ |
| 49 | }; |
Christoph Hellwig | 25e41b3 | 2008-12-03 12:20:39 +0100 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * Prime number of hash buckets since address is used as the key. |
| 53 | */ |
| 54 | #define NVSYNC 37 |
| 55 | #define to_ioend_wq(v) (&xfs_ioend_wq[((unsigned long)v) % NVSYNC]) |
| 56 | static wait_queue_head_t xfs_ioend_wq[NVSYNC]; |
| 57 | |
| 58 | void __init |
| 59 | xfs_ioend_init(void) |
| 60 | { |
| 61 | int i; |
| 62 | |
| 63 | for (i = 0; i < NVSYNC; i++) |
| 64 | init_waitqueue_head(&xfs_ioend_wq[i]); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | xfs_ioend_wait( |
| 69 | xfs_inode_t *ip) |
| 70 | { |
| 71 | wait_queue_head_t *wq = to_ioend_wq(ip); |
| 72 | |
| 73 | wait_event(*wq, (atomic_read(&ip->i_iocount) == 0)); |
| 74 | } |
| 75 | |
| 76 | STATIC void |
| 77 | xfs_ioend_wake( |
| 78 | xfs_inode_t *ip) |
| 79 | { |
| 80 | if (atomic_dec_and_test(&ip->i_iocount)) |
| 81 | wake_up(to_ioend_wq(ip)); |
| 82 | } |
| 83 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 84 | void |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 85 | xfs_count_page_state( |
| 86 | struct page *page, |
| 87 | int *delalloc, |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 88 | int *unwritten) |
| 89 | { |
| 90 | struct buffer_head *bh, *head; |
| 91 | |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 92 | *delalloc = *unwritten = 0; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 93 | |
| 94 | bh = head = page_buffers(page); |
| 95 | do { |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 96 | if (buffer_unwritten(bh)) |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 97 | (*unwritten) = 1; |
| 98 | else if (buffer_delay(bh)) |
| 99 | (*delalloc) = 1; |
| 100 | } while ((bh = bh->b_this_page) != head); |
| 101 | } |
| 102 | |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 103 | STATIC struct block_device * |
| 104 | xfs_find_bdev_for_inode( |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 105 | struct inode *inode) |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 106 | { |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 107 | struct xfs_inode *ip = XFS_I(inode); |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 108 | struct xfs_mount *mp = ip->i_mount; |
| 109 | |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 110 | if (XFS_IS_REALTIME_INODE(ip)) |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 111 | return mp->m_rtdev_targp->bt_bdev; |
| 112 | else |
| 113 | return mp->m_ddev_targp->bt_bdev; |
| 114 | } |
| 115 | |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 116 | /* |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 117 | * We're now finished for good with this ioend structure. |
| 118 | * Update the page state via the associated buffer_heads, |
| 119 | * release holds on the inode and bio, and finally free |
| 120 | * up memory. Do not use the ioend after this. |
| 121 | */ |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 122 | STATIC void |
| 123 | xfs_destroy_ioend( |
| 124 | xfs_ioend_t *ioend) |
| 125 | { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 126 | struct buffer_head *bh, *next; |
Christoph Hellwig | 583fa58 | 2008-12-03 12:20:38 +0100 | [diff] [blame] | 127 | struct xfs_inode *ip = XFS_I(ioend->io_inode); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 128 | |
| 129 | for (bh = ioend->io_buffer_head; bh; bh = next) { |
| 130 | next = bh->b_private; |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 131 | bh->b_end_io(bh, !ioend->io_error); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 132 | } |
Christoph Hellwig | 583fa58 | 2008-12-03 12:20:38 +0100 | [diff] [blame] | 133 | |
| 134 | /* |
| 135 | * Volume managers supporting multiple paths can send back ENODEV |
| 136 | * when the final path disappears. In this case continuing to fill |
| 137 | * the page cache with dirty data which cannot be written out is |
| 138 | * evil, so prevent that. |
| 139 | */ |
| 140 | if (unlikely(ioend->io_error == -ENODEV)) { |
| 141 | xfs_do_force_shutdown(ip->i_mount, SHUTDOWN_DEVICE_REQ, |
| 142 | __FILE__, __LINE__); |
Christoph Hellwig | b677c21 | 2007-08-29 11:46:28 +1000 | [diff] [blame] | 143 | } |
Christoph Hellwig | 583fa58 | 2008-12-03 12:20:38 +0100 | [diff] [blame] | 144 | |
Christoph Hellwig | 25e41b3 | 2008-12-03 12:20:39 +0100 | [diff] [blame] | 145 | xfs_ioend_wake(ip); |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 146 | mempool_free(ioend, xfs_ioend_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* |
Dave Chinner | 932640e | 2009-10-06 20:29:29 +0000 | [diff] [blame] | 150 | * If the end of the current ioend is beyond the current EOF, |
| 151 | * return the new EOF value, otherwise zero. |
| 152 | */ |
| 153 | STATIC xfs_fsize_t |
| 154 | xfs_ioend_new_eof( |
| 155 | xfs_ioend_t *ioend) |
| 156 | { |
| 157 | xfs_inode_t *ip = XFS_I(ioend->io_inode); |
| 158 | xfs_fsize_t isize; |
| 159 | xfs_fsize_t bsize; |
| 160 | |
| 161 | bsize = ioend->io_offset + ioend->io_size; |
| 162 | isize = MAX(ip->i_size, ip->i_new_size); |
| 163 | isize = MIN(isize, bsize); |
| 164 | return isize > ip->i_d.di_size ? isize : 0; |
| 165 | } |
| 166 | |
| 167 | /* |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 168 | * Update on-disk file size now that data has been written to disk. The |
| 169 | * current in-memory file size is i_size. If a write is beyond eof i_new_size |
| 170 | * will be the intended file size until i_size is updated. If this write does |
| 171 | * not extend all the way to the valid file size then restrict this update to |
| 172 | * the end of the write. |
| 173 | * |
| 174 | * This function does not block as blocking on the inode lock in IO completion |
| 175 | * can lead to IO completion order dependency deadlocks.. If it can't get the |
| 176 | * inode ilock it will return EAGAIN. Callers must handle this. |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 177 | */ |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 178 | STATIC int |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 179 | xfs_setfilesize( |
| 180 | xfs_ioend_t *ioend) |
| 181 | { |
Christoph Hellwig | b677c21 | 2007-08-29 11:46:28 +1000 | [diff] [blame] | 182 | xfs_inode_t *ip = XFS_I(ioend->io_inode); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 183 | xfs_fsize_t isize; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 184 | |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 185 | ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG); |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 186 | ASSERT(ioend->io_type != IO_READ); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 187 | |
| 188 | if (unlikely(ioend->io_error)) |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 189 | return 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 190 | |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 191 | if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) |
| 192 | return EAGAIN; |
| 193 | |
Dave Chinner | 932640e | 2009-10-06 20:29:29 +0000 | [diff] [blame] | 194 | isize = xfs_ioend_new_eof(ioend); |
| 195 | if (isize) { |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 196 | ip->i_d.di_size = isize; |
Christoph Hellwig | 66d834e | 2010-02-15 09:44:49 +0000 | [diff] [blame] | 197 | xfs_mark_inode_dirty(ip); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 201 | return 0; |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 205 | * Schedule IO completion handling on the final put of an ioend. |
Dave Chinner | c626d17 | 2009-04-06 18:42:11 +0200 | [diff] [blame] | 206 | */ |
| 207 | STATIC void |
| 208 | xfs_finish_ioend( |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 209 | struct xfs_ioend *ioend) |
Dave Chinner | c626d17 | 2009-04-06 18:42:11 +0200 | [diff] [blame] | 210 | { |
| 211 | if (atomic_dec_and_test(&ioend->io_remaining)) { |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 212 | if (ioend->io_type == IO_UNWRITTEN) |
| 213 | queue_work(xfsconvertd_workqueue, &ioend->io_work); |
| 214 | else |
| 215 | queue_work(xfsdatad_workqueue, &ioend->io_work); |
Dave Chinner | c626d17 | 2009-04-06 18:42:11 +0200 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | /* |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 220 | * IO write completion. |
| 221 | */ |
| 222 | STATIC void |
| 223 | xfs_end_io( |
| 224 | struct work_struct *work) |
| 225 | { |
| 226 | xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work); |
| 227 | struct xfs_inode *ip = XFS_I(ioend->io_inode); |
Dave Chinner | 6941893 | 2010-03-04 00:57:09 +0000 | [diff] [blame] | 228 | int error = 0; |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 229 | |
| 230 | /* |
| 231 | * For unwritten extents we need to issue transactions to convert a |
| 232 | * range to normal written extens after the data I/O has finished. |
| 233 | */ |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 234 | if (ioend->io_type == IO_UNWRITTEN && |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 235 | likely(!ioend->io_error && !XFS_FORCED_SHUTDOWN(ip->i_mount))) { |
| 236 | |
| 237 | error = xfs_iomap_write_unwritten(ip, ioend->io_offset, |
| 238 | ioend->io_size); |
| 239 | if (error) |
| 240 | ioend->io_error = error; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * We might have to update the on-disk file size after extending |
| 245 | * writes. |
| 246 | */ |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 247 | if (ioend->io_type != IO_READ) { |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 248 | error = xfs_setfilesize(ioend); |
| 249 | ASSERT(!error || error == EAGAIN); |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * If we didn't complete processing of the ioend, requeue it to the |
| 254 | * tail of the workqueue for another attempt later. Otherwise destroy |
| 255 | * it. |
| 256 | */ |
| 257 | if (error == EAGAIN) { |
| 258 | atomic_inc(&ioend->io_remaining); |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 259 | xfs_finish_ioend(ioend); |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 260 | /* ensure we don't spin on blocked ioends */ |
| 261 | delay(1); |
Christoph Hellwig | fb511f2 | 2010-07-18 21:17:10 +0000 | [diff] [blame] | 262 | } else { |
| 263 | if (ioend->io_iocb) |
| 264 | aio_complete(ioend->io_iocb, ioend->io_result, 0); |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 265 | xfs_destroy_ioend(ioend); |
Christoph Hellwig | fb511f2 | 2010-07-18 21:17:10 +0000 | [diff] [blame] | 266 | } |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 270 | * Call IO completion handling in caller context on the final put of an ioend. |
| 271 | */ |
| 272 | STATIC void |
| 273 | xfs_finish_ioend_sync( |
| 274 | struct xfs_ioend *ioend) |
| 275 | { |
| 276 | if (atomic_dec_and_test(&ioend->io_remaining)) |
| 277 | xfs_end_io(&ioend->io_work); |
| 278 | } |
| 279 | |
| 280 | /* |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 281 | * Allocate and initialise an IO completion structure. |
| 282 | * We need to track unwritten extent write completion here initially. |
| 283 | * We'll need to extend this for updating the ondisk inode size later |
| 284 | * (vs. incore size). |
| 285 | */ |
| 286 | STATIC xfs_ioend_t * |
| 287 | xfs_alloc_ioend( |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 288 | struct inode *inode, |
| 289 | unsigned int type) |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 290 | { |
| 291 | xfs_ioend_t *ioend; |
| 292 | |
| 293 | ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS); |
| 294 | |
| 295 | /* |
| 296 | * Set the count to 1 initially, which will prevent an I/O |
| 297 | * completion callback from happening before we have started |
| 298 | * all the I/O from calling the completion routine too early. |
| 299 | */ |
| 300 | atomic_set(&ioend->io_remaining, 1); |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 301 | ioend->io_error = 0; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 302 | ioend->io_list = NULL; |
| 303 | ioend->io_type = type; |
Christoph Hellwig | b677c21 | 2007-08-29 11:46:28 +1000 | [diff] [blame] | 304 | ioend->io_inode = inode; |
Christoph Hellwig | c1a073b | 2005-09-05 08:23:35 +1000 | [diff] [blame] | 305 | ioend->io_buffer_head = NULL; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 306 | ioend->io_buffer_tail = NULL; |
Christoph Hellwig | b677c21 | 2007-08-29 11:46:28 +1000 | [diff] [blame] | 307 | atomic_inc(&XFS_I(ioend->io_inode)->i_iocount); |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 308 | ioend->io_offset = 0; |
| 309 | ioend->io_size = 0; |
Christoph Hellwig | fb511f2 | 2010-07-18 21:17:10 +0000 | [diff] [blame] | 310 | ioend->io_iocb = NULL; |
| 311 | ioend->io_result = 0; |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 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 | |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 361 | xfs_finish_ioend(ioend); |
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); |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 503 | xfs_finish_ioend(ioend); |
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 | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 617 | unsigned int pg_offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | { |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 619 | struct buffer_head *bh, *head; |
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; |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 624 | if (!PageDirty(page)) |
| 625 | return 0; |
| 626 | if (!page->mapping) |
| 627 | return 0; |
| 628 | if (!page_has_buffers(page)) |
| 629 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 631 | bh = head = page_buffers(page); |
| 632 | do { |
| 633 | if (!buffer_uptodate(bh)) |
| 634 | break; |
| 635 | if (!buffer_mapped(bh)) |
| 636 | break; |
| 637 | ret += bh->b_size; |
| 638 | if (ret >= pg_offset) |
| 639 | break; |
| 640 | } while ((bh = bh->b_this_page) != head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | return ret; |
| 643 | } |
| 644 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 645 | STATIC size_t |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 646 | xfs_probe_cluster( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | struct inode *inode, |
| 648 | struct page *startpage, |
| 649 | struct buffer_head *bh, |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 650 | struct buffer_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | { |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 652 | struct pagevec pvec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | pgoff_t tindex, tlast, tloff; |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 654 | size_t total = 0; |
| 655 | int done = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
| 657 | /* First sum forwards in this page */ |
| 658 | do { |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 659 | if (!buffer_uptodate(bh) || !buffer_mapped(bh)) |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 660 | return total; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | total += bh->b_size; |
| 662 | } while ((bh = bh->b_this_page) != head); |
| 663 | |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 664 | /* if we reached the end of the page, sum forwards in following pages */ |
| 665 | tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT; |
| 666 | tindex = startpage->index + 1; |
| 667 | |
| 668 | /* Prune this back to avoid pathological behavior */ |
| 669 | tloff = min(tlast, startpage->index + 64); |
| 670 | |
| 671 | pagevec_init(&pvec, 0); |
| 672 | while (!done && tindex <= tloff) { |
| 673 | unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1); |
| 674 | |
| 675 | if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len)) |
| 676 | break; |
| 677 | |
| 678 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 679 | struct page *page = pvec.pages[i]; |
Christoph Hellwig | 265c1fa | 2007-08-16 15:38:19 +1000 | [diff] [blame] | 680 | size_t pg_offset, pg_len = 0; |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 681 | |
| 682 | if (tindex == tlast) { |
| 683 | pg_offset = |
| 684 | i_size_read(inode) & (PAGE_CACHE_SIZE - 1); |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 685 | if (!pg_offset) { |
| 686 | done = 1; |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 687 | break; |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 688 | } |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 689 | } else |
| 690 | pg_offset = PAGE_CACHE_SIZE; |
| 691 | |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 692 | if (page->index == tindex && trylock_page(page)) { |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 693 | pg_len = xfs_probe_page(page, pg_offset); |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 694 | unlock_page(page); |
| 695 | } |
| 696 | |
Christoph Hellwig | 265c1fa | 2007-08-16 15:38:19 +1000 | [diff] [blame] | 697 | if (!pg_len) { |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 698 | done = 1; |
| 699 | break; |
| 700 | } |
| 701 | |
Christoph Hellwig | 265c1fa | 2007-08-16 15:38:19 +1000 | [diff] [blame] | 702 | total += pg_len; |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 703 | tindex++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | } |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 705 | |
| 706 | pagevec_release(&pvec); |
| 707 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | } |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | return total; |
| 711 | } |
| 712 | |
| 713 | /* |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 714 | * Test if a given page is suitable for writing as part of an unwritten |
| 715 | * or delayed allocate extent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | */ |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 717 | STATIC int |
| 718 | xfs_is_delayed_page( |
| 719 | struct page *page, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 720 | unsigned int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | if (PageWriteback(page)) |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 723 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
| 725 | if (page->mapping && page_has_buffers(page)) { |
| 726 | struct buffer_head *bh, *head; |
| 727 | int acceptable = 0; |
| 728 | |
| 729 | bh = head = page_buffers(page); |
| 730 | do { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 731 | if (buffer_unwritten(bh)) |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 732 | acceptable = (type == IO_UNWRITTEN); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 733 | else if (buffer_delay(bh)) |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 734 | acceptable = (type == IO_DELAY); |
David Chinner | 2ddee84 | 2006-03-22 12:47:40 +1100 | [diff] [blame] | 735 | else if (buffer_dirty(bh) && buffer_mapped(bh)) |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 736 | acceptable = (type == IO_NEW); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 737 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | } while ((bh = bh->b_this_page) != head); |
| 740 | |
| 741 | if (acceptable) |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 742 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 745 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | /* |
| 749 | * Allocate & map buffers for page given the extent map. Write it out. |
| 750 | * except for the original page of a writepage, this is called on |
| 751 | * delalloc/unwritten pages only, for the original page it is possible |
| 752 | * that the page has no mapping at all. |
| 753 | */ |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 754 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | xfs_convert_page( |
| 756 | struct inode *inode, |
| 757 | struct page *page, |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 758 | loff_t tindex, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 759 | struct xfs_bmbt_irec *imap, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 760 | xfs_ioend_t **ioendp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | struct writeback_control *wbc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | int all_bh) |
| 763 | { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 764 | struct buffer_head *bh, *head; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 765 | xfs_off_t end_offset; |
| 766 | unsigned long p_offset; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 767 | unsigned int type; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 768 | int len, page_dirty; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 769 | int count = 0, done = 0, uptodate = 1; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 770 | xfs_off_t offset = page_offset(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 772 | if (page->index != tindex) |
| 773 | goto fail; |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 774 | if (!trylock_page(page)) |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 775 | goto fail; |
| 776 | if (PageWriteback(page)) |
| 777 | goto fail_unlock_page; |
| 778 | if (page->mapping != inode->i_mapping) |
| 779 | goto fail_unlock_page; |
| 780 | if (!xfs_is_delayed_page(page, (*ioendp)->io_type)) |
| 781 | goto fail_unlock_page; |
| 782 | |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 783 | /* |
| 784 | * page_dirty is initially a count of buffers on the page before |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 785 | * EOF and is decremented as we move each into a cleanable state. |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 786 | * |
| 787 | * Derivation: |
| 788 | * |
| 789 | * End offset is the highest offset that this page should represent. |
| 790 | * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1)) |
| 791 | * will evaluate non-zero and be less than PAGE_CACHE_SIZE and |
| 792 | * hence give us the correct page_dirty count. On any other page, |
| 793 | * it will be zero and in that case we need page_dirty to be the |
| 794 | * count of buffers on the page. |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 795 | */ |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 796 | end_offset = min_t(unsigned long long, |
| 797 | (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, |
| 798 | i_size_read(inode)); |
| 799 | |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 800 | len = 1 << inode->i_blkbits; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 801 | p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1), |
| 802 | PAGE_CACHE_SIZE); |
| 803 | p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE; |
| 804 | page_dirty = p_offset / len; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | bh = head = page_buffers(page); |
| 807 | do { |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 808 | if (offset >= end_offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | break; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 810 | if (!buffer_uptodate(bh)) |
| 811 | uptodate = 0; |
| 812 | if (!(PageUptodate(page) || buffer_uptodate(bh))) { |
| 813 | done = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | continue; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 815 | } |
| 816 | |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 817 | if (buffer_unwritten(bh) || buffer_delay(bh)) { |
| 818 | if (buffer_unwritten(bh)) |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 819 | type = IO_UNWRITTEN; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 820 | else |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 821 | type = IO_DELAY; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 822 | |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 823 | if (!xfs_imap_valid(inode, imap, offset)) { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 824 | done = 1; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 825 | continue; |
| 826 | } |
| 827 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 828 | ASSERT(imap->br_startblock != HOLESTARTBLOCK); |
| 829 | ASSERT(imap->br_startblock != DELAYSTARTBLOCK); |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 830 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 831 | xfs_map_at_offset(inode, bh, imap, offset); |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 832 | xfs_add_to_ioend(inode, bh, offset, type, |
| 833 | ioendp, done); |
| 834 | |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 835 | page_dirty--; |
| 836 | count++; |
| 837 | } else { |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 838 | type = IO_NEW; |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 839 | if (buffer_mapped(bh) && all_bh) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | lock_buffer(bh); |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 841 | xfs_add_to_ioend(inode, bh, offset, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 842 | type, ioendp, done); |
| 843 | count++; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 844 | page_dirty--; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 845 | } else { |
| 846 | done = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | } |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 849 | } while (offset += len, (bh = bh->b_this_page) != head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 851 | if (uptodate && bh == head) |
| 852 | SetPageUptodate(page); |
| 853 | |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 854 | if (count) { |
Dave Chinner | efceab1 | 2010-08-24 11:44:56 +1000 | [diff] [blame] | 855 | if (--wbc->nr_to_write <= 0 && |
| 856 | wbc->sync_mode == WB_SYNC_NONE) |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 857 | done = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | } |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 859 | xfs_start_page_writeback(page, !page_dirty, count); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 860 | |
| 861 | return done; |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 862 | fail_unlock_page: |
| 863 | unlock_page(page); |
| 864 | fail: |
| 865 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | /* |
| 869 | * Convert & write out a cluster of pages in the same extent as defined |
| 870 | * by mp and following the start page. |
| 871 | */ |
| 872 | STATIC void |
| 873 | xfs_cluster_write( |
| 874 | struct inode *inode, |
| 875 | pgoff_t tindex, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 876 | struct xfs_bmbt_irec *imap, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 877 | xfs_ioend_t **ioendp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | struct writeback_control *wbc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | int all_bh, |
| 880 | pgoff_t tlast) |
| 881 | { |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 882 | struct pagevec pvec; |
| 883 | int done = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 885 | pagevec_init(&pvec, 0); |
| 886 | while (!done && tindex <= tlast) { |
| 887 | unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1); |
| 888 | |
| 889 | if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | break; |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 891 | |
| 892 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 893 | done = xfs_convert_page(inode, pvec.pages[i], tindex++, |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 894 | imap, ioendp, wbc, all_bh); |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 895 | if (done) |
| 896 | break; |
| 897 | } |
| 898 | |
| 899 | pagevec_release(&pvec); |
| 900 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 904 | STATIC void |
| 905 | xfs_vm_invalidatepage( |
| 906 | struct page *page, |
| 907 | unsigned long offset) |
| 908 | { |
| 909 | trace_xfs_invalidatepage(page->mapping->host, page, offset); |
| 910 | block_invalidatepage(page, offset); |
| 911 | } |
| 912 | |
| 913 | /* |
| 914 | * If the page has delalloc buffers on it, we need to punch them out before we |
| 915 | * invalidate the page. If we don't, we leave a stale delalloc mapping on the |
| 916 | * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read |
| 917 | * is done on that same region - the delalloc extent is returned when none is |
| 918 | * supposed to be there. |
| 919 | * |
| 920 | * We prevent this by truncating away the delalloc regions on the page before |
| 921 | * invalidating it. Because they are delalloc, we can do this without needing a |
| 922 | * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this |
| 923 | * truncation without a transaction as there is no space left for block |
| 924 | * reservation (typically why we see a ENOSPC in writeback). |
| 925 | * |
| 926 | * This is not a performance critical path, so for now just do the punching a |
| 927 | * buffer head at a time. |
| 928 | */ |
| 929 | STATIC void |
| 930 | xfs_aops_discard_page( |
| 931 | struct page *page) |
| 932 | { |
| 933 | struct inode *inode = page->mapping->host; |
| 934 | struct xfs_inode *ip = XFS_I(inode); |
| 935 | struct buffer_head *bh, *head; |
| 936 | loff_t offset = page_offset(page); |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 937 | |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 938 | if (!xfs_is_delayed_page(page, IO_DELAY)) |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 939 | goto out_invalidate; |
| 940 | |
Dave Chinner | e8c3753 | 2010-03-15 02:36:35 +0000 | [diff] [blame] | 941 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 942 | goto out_invalidate; |
| 943 | |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 944 | xfs_fs_cmn_err(CE_ALERT, ip->i_mount, |
| 945 | "page discard on page %p, inode 0x%llx, offset %llu.", |
| 946 | page, ip->i_ino, offset); |
| 947 | |
| 948 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 949 | bh = head = page_buffers(page); |
| 950 | do { |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 951 | int error; |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 952 | xfs_fileoff_t start_fsb; |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 953 | |
| 954 | if (!buffer_delay(bh)) |
| 955 | goto next_buffer; |
| 956 | |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 957 | start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset); |
| 958 | error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1); |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 959 | if (error) { |
| 960 | /* something screwed, just bail */ |
Dave Chinner | e8c3753 | 2010-03-15 02:36:35 +0000 | [diff] [blame] | 961 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
| 962 | xfs_fs_cmn_err(CE_ALERT, ip->i_mount, |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 963 | "page discard unable to remove delalloc mapping."); |
Dave Chinner | e8c3753 | 2010-03-15 02:36:35 +0000 | [diff] [blame] | 964 | } |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 965 | break; |
| 966 | } |
| 967 | next_buffer: |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 968 | offset += 1 << inode->i_blkbits; |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 969 | |
| 970 | } while ((bh = bh->b_this_page) != head); |
| 971 | |
| 972 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 973 | out_invalidate: |
| 974 | xfs_vm_invalidatepage(page, 0); |
| 975 | return; |
| 976 | } |
| 977 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | /* |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 979 | * Write out a dirty page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | * |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 981 | * For delalloc space on the page we need to allocate space and flush it. |
| 982 | * For unwritten space on the page we need to start the conversion to |
| 983 | * regular allocated space. |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 984 | * For any other dirty buffer heads on the page we should flush them. |
| 985 | * |
| 986 | * If we detect that a transaction would be required to flush the page, we |
| 987 | * have to check the process flags first, if we are already in a transaction |
| 988 | * or disk I/O during allocations is off, we need to fail the writepage and |
| 989 | * redirty the page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | STATIC int |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 992 | xfs_vm_writepage( |
| 993 | struct page *page, |
| 994 | struct writeback_control *wbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | { |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 996 | struct inode *inode = page->mapping->host; |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 997 | int delalloc, unwritten; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 998 | struct buffer_head *bh, *head; |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 999 | struct xfs_bmbt_irec imap; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1000 | xfs_ioend_t *ioend = NULL, *iohead = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | loff_t offset; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1002 | unsigned int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | __uint64_t end_offset; |
Christoph Hellwig | bd1556a | 2010-04-28 12:29:00 +0000 | [diff] [blame] | 1004 | pgoff_t end_index, last_index; |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 1005 | ssize_t size, len; |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1006 | int flags, err, imap_valid = 0, uptodate = 1; |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1007 | int count = 0; |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1008 | int all_bh = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1010 | trace_xfs_writepage(inode, page, 0); |
| 1011 | |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1012 | ASSERT(page_has_buffers(page)); |
| 1013 | |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1014 | /* |
| 1015 | * Refuse to write the page out if we are called from reclaim context. |
| 1016 | * |
Christoph Hellwig | d4f7a5c | 2010-06-28 10:34:44 -0400 | [diff] [blame] | 1017 | * This avoids stack overflows when called from deeply used stacks in |
| 1018 | * random callers for direct reclaim or memcg reclaim. We explicitly |
| 1019 | * allow reclaim from kswapd as the stack usage there is relatively low. |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1020 | * |
| 1021 | * This should really be done by the core VM, but until that happens |
| 1022 | * filesystems like XFS, btrfs and ext4 have to take care of this |
| 1023 | * by themselves. |
| 1024 | */ |
Christoph Hellwig | d4f7a5c | 2010-06-28 10:34:44 -0400 | [diff] [blame] | 1025 | if ((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == PF_MEMALLOC) |
Christoph Hellwig | b5420f2 | 2010-08-24 11:47:51 +1000 | [diff] [blame] | 1026 | goto redirty; |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1027 | |
| 1028 | /* |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1029 | * We need a transaction if there are delalloc or unwritten buffers |
| 1030 | * on the page. |
| 1031 | * |
| 1032 | * If we need a transaction and the process flags say we are already |
| 1033 | * in a transaction, or no IO is allowed then mark the page dirty |
| 1034 | * again and leave the page as is. |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1035 | */ |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1036 | xfs_count_page_state(page, &delalloc, &unwritten); |
| 1037 | if ((current->flags & PF_FSTRANS) && (delalloc || unwritten)) |
Christoph Hellwig | b5420f2 | 2010-08-24 11:47:51 +1000 | [diff] [blame] | 1038 | goto redirty; |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | /* Is this page beyond the end of the file? */ |
| 1041 | offset = i_size_read(inode); |
| 1042 | end_index = offset >> PAGE_CACHE_SHIFT; |
| 1043 | last_index = (offset - 1) >> PAGE_CACHE_SHIFT; |
| 1044 | if (page->index >= end_index) { |
| 1045 | if ((page->index >= end_index + 1) || |
| 1046 | !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) { |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1047 | unlock_page(page); |
Nathan Scott | 19d5bcf | 2005-11-02 15:14:09 +1100 | [diff] [blame] | 1048 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1052 | end_offset = min_t(unsigned long long, |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1053 | (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, |
| 1054 | offset); |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 1055 | len = 1 << inode->i_blkbits; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 1056 | |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 1057 | bh = head = page_buffers(page); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1058 | offset = page_offset(page); |
David Chinner | df3c724 | 2007-05-24 15:27:03 +1000 | [diff] [blame] | 1059 | flags = BMAPI_READ; |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 1060 | type = IO_NEW; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | do { |
| 1063 | if (offset >= end_offset) |
| 1064 | break; |
| 1065 | if (!buffer_uptodate(bh)) |
| 1066 | uptodate = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
Eric Sandeen | 3d9b02e | 2010-06-24 09:45:30 +1000 | [diff] [blame] | 1068 | /* |
Christoph Hellwig | ece413f | 2010-11-10 21:39:11 +0000 | [diff] [blame] | 1069 | * set_page_dirty dirties all buffers in a page, independent |
| 1070 | * of their state. The dirty state however is entirely |
| 1071 | * meaningless for holes (!mapped && uptodate), so skip |
| 1072 | * buffers covering holes here. |
Eric Sandeen | 3d9b02e | 2010-06-24 09:45:30 +1000 | [diff] [blame] | 1073 | */ |
| 1074 | if (!buffer_mapped(bh) && buffer_uptodate(bh)) { |
Eric Sandeen | 3d9b02e | 2010-06-24 09:45:30 +1000 | [diff] [blame] | 1075 | imap_valid = 0; |
| 1076 | continue; |
| 1077 | } |
| 1078 | |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1079 | if (imap_valid) |
| 1080 | imap_valid = xfs_imap_valid(inode, &imap, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1082 | if (buffer_unwritten(bh) || buffer_delay(bh)) { |
David Chinner | effd120 | 2007-06-18 16:49:58 +1000 | [diff] [blame] | 1083 | int new_ioend = 0; |
| 1084 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1085 | if (buffer_unwritten(bh)) { |
Christoph Hellwig | 85da94c | 2010-12-10 08:42:16 +0000 | [diff] [blame^] | 1086 | if (type != IO_UNWRITTEN) { |
| 1087 | type = IO_UNWRITTEN; |
| 1088 | imap_valid = 0; |
| 1089 | } |
Nathan Scott | 8272145 | 2006-04-11 15:10:55 +1000 | [diff] [blame] | 1090 | flags = BMAPI_WRITE | BMAPI_IGNSTATE; |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 1091 | } else if (buffer_delay(bh)) { |
Christoph Hellwig | 85da94c | 2010-12-10 08:42:16 +0000 | [diff] [blame^] | 1092 | if (type != IO_DELAY) { |
| 1093 | type = IO_DELAY; |
| 1094 | imap_valid = 0; |
| 1095 | } |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1096 | flags = BMAPI_ALLOCATE; |
| 1097 | |
Wu Fengguang | 1b430be | 2010-10-26 14:21:26 -0700 | [diff] [blame] | 1098 | if (wbc->sync_mode == WB_SYNC_NONE) |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1099 | flags |= BMAPI_TRYLOCK; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1100 | } |
| 1101 | |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1102 | if (!imap_valid) { |
David Chinner | effd120 | 2007-06-18 16:49:58 +1000 | [diff] [blame] | 1103 | /* |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1104 | * If we didn't have a valid mapping then we |
David Chinner | effd120 | 2007-06-18 16:49:58 +1000 | [diff] [blame] | 1105 | * need to ensure that we put the new mapping |
| 1106 | * in a new ioend structure. This needs to be |
| 1107 | * done to ensure that the ioends correctly |
| 1108 | * reflect the block mappings at io completion |
| 1109 | * for unwritten extent conversion. |
| 1110 | */ |
| 1111 | new_ioend = 1; |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1112 | err = xfs_map_blocks(inode, offset, len, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1113 | &imap, flags); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1114 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | goto error; |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1116 | imap_valid = xfs_imap_valid(inode, &imap, |
| 1117 | offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | } |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1119 | if (imap_valid) { |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1120 | xfs_map_at_offset(inode, bh, &imap, offset); |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1121 | xfs_add_to_ioend(inode, bh, offset, type, |
| 1122 | &ioend, new_ioend); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1123 | count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | } |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1125 | } else if (buffer_uptodate(bh)) { |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 1126 | /* |
| 1127 | * we got here because the buffer is already mapped. |
| 1128 | * That means it must already have extents allocated |
| 1129 | * underneath it. Map the extent by reading it. |
| 1130 | */ |
Christoph Hellwig | 85da94c | 2010-12-10 08:42:16 +0000 | [diff] [blame^] | 1131 | if (flags != BMAPI_READ) { |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 1132 | flags = BMAPI_READ; |
Christoph Hellwig | 85da94c | 2010-12-10 08:42:16 +0000 | [diff] [blame^] | 1133 | imap_valid = 0; |
| 1134 | } |
| 1135 | if (!imap_valid) { |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1136 | size = xfs_probe_cluster(inode, page, bh, head); |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 1137 | err = xfs_map_blocks(inode, offset, size, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1138 | &imap, flags); |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 1139 | if (err) |
| 1140 | goto error; |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1141 | imap_valid = xfs_imap_valid(inode, &imap, |
| 1142 | offset); |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 1143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | |
David Chinner | df3c724 | 2007-05-24 15:27:03 +1000 | [diff] [blame] | 1145 | /* |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 1146 | * 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] | 1147 | * small write at EOF that is extending the file but |
| 1148 | * without needing an allocation. We need to update the |
| 1149 | * file size on I/O completion in this case so it is |
| 1150 | * the same case as having just allocated a new extent |
| 1151 | * that we are writing into for the first time. |
| 1152 | */ |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 1153 | type = IO_NEW; |
Nick Piggin | ca5de40 | 2008-08-02 12:02:13 +0200 | [diff] [blame] | 1154 | if (trylock_buffer(bh)) { |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1155 | if (imap_valid) |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 1156 | all_bh = 1; |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 1157 | xfs_add_to_ioend(inode, bh, offset, type, |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1158 | &ioend, !imap_valid); |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 1159 | count++; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1160 | } else { |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1161 | imap_valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | } |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1163 | } else if (PageUptodate(page)) { |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1164 | ASSERT(buffer_mapped(bh)); |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1165 | imap_valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | } |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1167 | |
| 1168 | if (!iohead) |
| 1169 | iohead = ioend; |
| 1170 | |
| 1171 | } while (offset += len, ((bh = bh->b_this_page) != head)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | |
| 1173 | if (uptodate && bh == head) |
| 1174 | SetPageUptodate(page); |
| 1175 | |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1176 | xfs_start_page_writeback(page, 1, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1178 | if (ioend && imap_valid) { |
Christoph Hellwig | bd1556a | 2010-04-28 12:29:00 +0000 | [diff] [blame] | 1179 | xfs_off_t end_index; |
Christoph Hellwig | 8699bb0 | 2010-04-28 12:28:54 +0000 | [diff] [blame] | 1180 | |
Christoph Hellwig | bd1556a | 2010-04-28 12:29:00 +0000 | [diff] [blame] | 1181 | end_index = imap.br_startoff + imap.br_blockcount; |
| 1182 | |
| 1183 | /* to bytes */ |
| 1184 | end_index <<= inode->i_blkbits; |
| 1185 | |
| 1186 | /* to pages */ |
| 1187 | end_index = (end_index - 1) >> PAGE_CACHE_SHIFT; |
| 1188 | |
| 1189 | /* check against file size */ |
| 1190 | if (end_index > last_index) |
| 1191 | end_index = last_index; |
| 1192 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1193 | xfs_cluster_write(inode, page->index + 1, &imap, &ioend, |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1194 | wbc, all_bh, end_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1197 | if (iohead) |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 1198 | xfs_submit_ioend(wbc, iohead); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1199 | |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1200 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | |
| 1202 | error: |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1203 | if (iohead) |
| 1204 | xfs_cancel_ioend(iohead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | |
Christoph Hellwig | b5420f2 | 2010-08-24 11:47:51 +1000 | [diff] [blame] | 1206 | if (err == -EAGAIN) |
| 1207 | goto redirty; |
| 1208 | |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1209 | xfs_aops_discard_page(page); |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1210 | ClearPageUptodate(page); |
| 1211 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | return err; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1213 | |
Christoph Hellwig | b5420f2 | 2010-08-24 11:47:51 +1000 | [diff] [blame] | 1214 | redirty: |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1215 | redirty_page_for_writepage(wbc, page); |
| 1216 | unlock_page(page); |
| 1217 | return 0; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1218 | } |
| 1219 | |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 1220 | STATIC int |
| 1221 | xfs_vm_writepages( |
| 1222 | struct address_space *mapping, |
| 1223 | struct writeback_control *wbc) |
| 1224 | { |
Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 1225 | xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED); |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 1226 | return generic_writepages(mapping, wbc); |
| 1227 | } |
| 1228 | |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1229 | /* |
| 1230 | * Called to move a page into cleanable state - and from there |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1231 | * to be released. The page should already be clean. We always |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1232 | * have buffer heads in this call. |
| 1233 | * |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1234 | * Returns 1 if the page is ok to release, 0 otherwise. |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1235 | */ |
| 1236 | STATIC int |
Nathan Scott | 238f4c5 | 2006-03-17 17:26:25 +1100 | [diff] [blame] | 1237 | xfs_vm_releasepage( |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1238 | struct page *page, |
| 1239 | gfp_t gfp_mask) |
| 1240 | { |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1241 | int delalloc, unwritten; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1242 | |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1243 | trace_xfs_releasepage(page->mapping->host, page, 0); |
Nathan Scott | 238f4c5 | 2006-03-17 17:26:25 +1100 | [diff] [blame] | 1244 | |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1245 | xfs_count_page_state(page, &delalloc, &unwritten); |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1246 | |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1247 | if (WARN_ON(delalloc)) |
| 1248 | return 0; |
| 1249 | if (WARN_ON(unwritten)) |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1250 | return 0; |
| 1251 | |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1252 | return try_to_free_buffers(page); |
| 1253 | } |
| 1254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | STATIC int |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1256 | __xfs_get_blocks( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | struct inode *inode, |
| 1258 | sector_t iblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | struct buffer_head *bh_result, |
| 1260 | int create, |
Christoph Hellwig | f2bde9b | 2010-06-24 11:44:35 +1000 | [diff] [blame] | 1261 | int direct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | { |
Christoph Hellwig | f2bde9b | 2010-06-24 11:44:35 +1000 | [diff] [blame] | 1263 | int flags = create ? BMAPI_WRITE : BMAPI_READ; |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1264 | struct xfs_bmbt_irec imap; |
Nathan Scott | fdc7ed7 | 2005-11-02 15:13:13 +1100 | [diff] [blame] | 1265 | xfs_off_t offset; |
| 1266 | ssize_t size; |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1267 | int nimap = 1; |
| 1268 | int new = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | |
Nathan Scott | fdc7ed7 | 2005-11-02 15:13:13 +1100 | [diff] [blame] | 1271 | offset = (xfs_off_t)iblock << inode->i_blkbits; |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1272 | ASSERT(bh_result->b_size >= (1 << inode->i_blkbits)); |
| 1273 | size = bh_result->b_size; |
Lachlan McIlroy | 364f358 | 2008-09-17 16:50:14 +1000 | [diff] [blame] | 1274 | |
| 1275 | if (!create && direct && offset >= i_size_read(inode)) |
| 1276 | return 0; |
| 1277 | |
Christoph Hellwig | f2bde9b | 2010-06-24 11:44:35 +1000 | [diff] [blame] | 1278 | if (direct && create) |
| 1279 | flags |= BMAPI_DIRECT; |
| 1280 | |
| 1281 | error = xfs_iomap(XFS_I(inode), offset, size, flags, &imap, &nimap, |
| 1282 | &new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | if (error) |
| 1284 | return -error; |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1285 | if (nimap == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | return 0; |
| 1287 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1288 | if (imap.br_startblock != HOLESTARTBLOCK && |
| 1289 | imap.br_startblock != DELAYSTARTBLOCK) { |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 1290 | /* |
| 1291 | * For unwritten extents do not report a disk address on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | * the read case (treat as if we're reading into a hole). |
| 1293 | */ |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1294 | if (create || !ISUNWRITTEN(&imap)) |
| 1295 | xfs_map_buffer(inode, bh_result, &imap, offset); |
| 1296 | if (create && ISUNWRITTEN(&imap)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | if (direct) |
| 1298 | bh_result->b_private = inode; |
| 1299 | set_buffer_unwritten(bh_result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1303 | /* |
| 1304 | * If this is a realtime file, data may be on a different device. |
| 1305 | * to that pointed to from the buffer_head b_bdev currently. |
| 1306 | */ |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 1307 | bh_result->b_bdev = xfs_find_bdev_for_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1309 | /* |
David Chinner | 549054a | 2007-02-10 18:36:35 +1100 | [diff] [blame] | 1310 | * If we previously allocated a block out beyond eof and we are now |
| 1311 | * coming back to use it then we will need to flag it as new even if it |
| 1312 | * has a disk address. |
| 1313 | * |
| 1314 | * With sub-block writes into unwritten extents we also need to mark |
| 1315 | * the buffer as new so that the unwritten parts of the buffer gets |
| 1316 | * correctly zeroed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | */ |
| 1318 | if (create && |
| 1319 | ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) || |
David Chinner | 549054a | 2007-02-10 18:36:35 +1100 | [diff] [blame] | 1320 | (offset >= i_size_read(inode)) || |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1321 | (new || ISUNWRITTEN(&imap)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | set_buffer_new(bh_result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1324 | if (imap.br_startblock == DELAYSTARTBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | BUG_ON(direct); |
| 1326 | if (create) { |
| 1327 | set_buffer_uptodate(bh_result); |
| 1328 | set_buffer_mapped(bh_result); |
| 1329 | set_buffer_delay(bh_result); |
| 1330 | } |
| 1331 | } |
| 1332 | |
Christoph Hellwig | 2b8f12b | 2010-04-28 12:28:59 +0000 | [diff] [blame] | 1333 | /* |
| 1334 | * If this is O_DIRECT or the mpage code calling tell them how large |
| 1335 | * the mapping is, so that we can avoid repeated get_blocks calls. |
| 1336 | */ |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1337 | if (direct || size > (1 << inode->i_blkbits)) { |
Christoph Hellwig | 2b8f12b | 2010-04-28 12:28:59 +0000 | [diff] [blame] | 1338 | xfs_off_t mapping_size; |
Christoph Hellwig | 9563b3d | 2010-04-28 12:28:53 +0000 | [diff] [blame] | 1339 | |
Christoph Hellwig | 2b8f12b | 2010-04-28 12:28:59 +0000 | [diff] [blame] | 1340 | mapping_size = imap.br_startoff + imap.br_blockcount - iblock; |
| 1341 | mapping_size <<= inode->i_blkbits; |
| 1342 | |
| 1343 | ASSERT(mapping_size > 0); |
| 1344 | if (mapping_size > size) |
| 1345 | mapping_size = size; |
| 1346 | if (mapping_size > LONG_MAX) |
| 1347 | mapping_size = LONG_MAX; |
| 1348 | |
| 1349 | bh_result->b_size = mapping_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | return 0; |
| 1353 | } |
| 1354 | |
| 1355 | int |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1356 | xfs_get_blocks( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | struct inode *inode, |
| 1358 | sector_t iblock, |
| 1359 | struct buffer_head *bh_result, |
| 1360 | int create) |
| 1361 | { |
Christoph Hellwig | f2bde9b | 2010-06-24 11:44:35 +1000 | [diff] [blame] | 1362 | return __xfs_get_blocks(inode, iblock, bh_result, create, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | STATIC int |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1366 | xfs_get_blocks_direct( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | struct inode *inode, |
| 1368 | sector_t iblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | struct buffer_head *bh_result, |
| 1370 | int create) |
| 1371 | { |
Christoph Hellwig | f2bde9b | 2010-06-24 11:44:35 +1000 | [diff] [blame] | 1372 | return __xfs_get_blocks(inode, iblock, bh_result, create, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1375 | /* |
| 1376 | * Complete a direct I/O write request. |
| 1377 | * |
| 1378 | * If the private argument is non-NULL __xfs_get_blocks signals us that we |
| 1379 | * need to issue a transaction to convert the range from unwritten to written |
| 1380 | * extents. In case this is regular synchronous I/O we just call xfs_end_io |
| 1381 | * to do this and we are done. But in case this was a successfull AIO |
| 1382 | * request this handler is called from interrupt context, from which we |
| 1383 | * can't start transactions. In that case offload the I/O completion to |
| 1384 | * the workqueues we also use for buffered I/O completion. |
| 1385 | */ |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1386 | STATIC void |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1387 | xfs_end_io_direct_write( |
| 1388 | struct kiocb *iocb, |
| 1389 | loff_t offset, |
| 1390 | ssize_t size, |
| 1391 | void *private, |
| 1392 | int ret, |
| 1393 | bool is_async) |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1394 | { |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1395 | struct xfs_ioend *ioend = iocb->private; |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1396 | |
| 1397 | /* |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1398 | * blockdev_direct_IO can return an error even after the I/O |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1399 | * completion handler was called. Thus we need to protect |
| 1400 | * against double-freeing. |
| 1401 | */ |
| 1402 | iocb->private = NULL; |
Christoph Hellwig | 40e2e97 | 2010-07-18 21:17:09 +0000 | [diff] [blame] | 1403 | |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1404 | ioend->io_offset = offset; |
| 1405 | ioend->io_size = size; |
| 1406 | if (private && size > 0) |
| 1407 | ioend->io_type = IO_UNWRITTEN; |
| 1408 | |
| 1409 | if (is_async) { |
| 1410 | /* |
| 1411 | * If we are converting an unwritten extent we need to delay |
| 1412 | * the AIO completion until after the unwrittent extent |
| 1413 | * conversion has completed, otherwise do it ASAP. |
| 1414 | */ |
| 1415 | if (ioend->io_type == IO_UNWRITTEN) { |
| 1416 | ioend->io_iocb = iocb; |
| 1417 | ioend->io_result = ret; |
| 1418 | } else { |
| 1419 | aio_complete(iocb, ret, 0); |
| 1420 | } |
| 1421 | xfs_finish_ioend(ioend); |
| 1422 | } else { |
| 1423 | xfs_finish_ioend_sync(ioend); |
| 1424 | } |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1425 | } |
| 1426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | STATIC ssize_t |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1428 | xfs_vm_direct_IO( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | int rw, |
| 1430 | struct kiocb *iocb, |
| 1431 | const struct iovec *iov, |
| 1432 | loff_t offset, |
| 1433 | unsigned long nr_segs) |
| 1434 | { |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1435 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
| 1436 | struct block_device *bdev = xfs_find_bdev_for_inode(inode); |
| 1437 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1439 | if (rw & WRITE) { |
| 1440 | iocb->private = xfs_alloc_ioend(inode, IO_NEW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | |
Christoph Hellwig | eafdc7d | 2010-06-04 11:29:53 +0200 | [diff] [blame] | 1442 | ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov, |
| 1443 | offset, nr_segs, |
| 1444 | xfs_get_blocks_direct, |
| 1445 | xfs_end_io_direct_write, NULL, 0); |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1446 | if (ret != -EIOCBQUEUED && iocb->private) |
| 1447 | xfs_destroy_ioend(iocb->private); |
| 1448 | } else { |
Christoph Hellwig | eafdc7d | 2010-06-04 11:29:53 +0200 | [diff] [blame] | 1449 | ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov, |
| 1450 | offset, nr_segs, |
| 1451 | xfs_get_blocks_direct, |
| 1452 | NULL, NULL, 0); |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1453 | } |
Christoph Hellwig | 5fe878ae | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 1454 | |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1455 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1458 | STATIC void |
| 1459 | xfs_vm_write_failed( |
| 1460 | struct address_space *mapping, |
| 1461 | loff_t to) |
| 1462 | { |
| 1463 | struct inode *inode = mapping->host; |
| 1464 | |
| 1465 | if (to > inode->i_size) { |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 1466 | /* |
| 1467 | * punch out the delalloc blocks we have already allocated. We |
| 1468 | * don't call xfs_setattr() to do this as we may be in the |
| 1469 | * middle of a multi-iovec write and so the vfs inode->i_size |
| 1470 | * will not match the xfs ip->i_size and so it will zero too |
| 1471 | * much. Hence we jus truncate the page cache to zero what is |
| 1472 | * necessary and punch the delalloc blocks directly. |
| 1473 | */ |
| 1474 | struct xfs_inode *ip = XFS_I(inode); |
| 1475 | xfs_fileoff_t start_fsb; |
| 1476 | xfs_fileoff_t end_fsb; |
| 1477 | int error; |
| 1478 | |
| 1479 | truncate_pagecache(inode, to, inode->i_size); |
| 1480 | |
| 1481 | /* |
| 1482 | * Check if there are any blocks that are outside of i_size |
| 1483 | * that need to be trimmed back. |
| 1484 | */ |
| 1485 | start_fsb = XFS_B_TO_FSB(ip->i_mount, inode->i_size) + 1; |
| 1486 | end_fsb = XFS_B_TO_FSB(ip->i_mount, to); |
| 1487 | if (end_fsb <= start_fsb) |
| 1488 | return; |
| 1489 | |
| 1490 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1491 | error = xfs_bmap_punch_delalloc_range(ip, start_fsb, |
| 1492 | end_fsb - start_fsb); |
| 1493 | if (error) { |
| 1494 | /* something screwed, just bail */ |
| 1495 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
| 1496 | xfs_fs_cmn_err(CE_ALERT, ip->i_mount, |
| 1497 | "xfs_vm_write_failed: unable to clean up ino %lld", |
| 1498 | ip->i_ino); |
| 1499 | } |
| 1500 | } |
| 1501 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1505 | STATIC int |
Nick Piggin | d79689c | 2007-10-16 01:25:06 -0700 | [diff] [blame] | 1506 | xfs_vm_write_begin( |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1507 | struct file *file, |
Nick Piggin | d79689c | 2007-10-16 01:25:06 -0700 | [diff] [blame] | 1508 | struct address_space *mapping, |
| 1509 | loff_t pos, |
| 1510 | unsigned len, |
| 1511 | unsigned flags, |
| 1512 | struct page **pagep, |
| 1513 | void **fsdata) |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1514 | { |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 1515 | int ret; |
| 1516 | |
| 1517 | ret = block_write_begin(mapping, pos, len, flags | AOP_FLAG_NOFS, |
| 1518 | pagep, xfs_get_blocks); |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1519 | if (unlikely(ret)) |
| 1520 | xfs_vm_write_failed(mapping, pos + len); |
| 1521 | return ret; |
| 1522 | } |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 1523 | |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1524 | STATIC int |
| 1525 | xfs_vm_write_end( |
| 1526 | struct file *file, |
| 1527 | struct address_space *mapping, |
| 1528 | loff_t pos, |
| 1529 | unsigned len, |
| 1530 | unsigned copied, |
| 1531 | struct page *page, |
| 1532 | void *fsdata) |
| 1533 | { |
| 1534 | int ret; |
| 1535 | |
| 1536 | ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata); |
| 1537 | if (unlikely(ret < len)) |
| 1538 | xfs_vm_write_failed(mapping, pos + len); |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 1539 | return ret; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1540 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | |
| 1542 | STATIC sector_t |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1543 | xfs_vm_bmap( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | struct address_space *mapping, |
| 1545 | sector_t block) |
| 1546 | { |
| 1547 | struct inode *inode = (struct inode *)mapping->host; |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 1548 | struct xfs_inode *ip = XFS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | |
Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 1550 | trace_xfs_vm_bmap(XFS_I(inode)); |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 1551 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 1552 | xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF); |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 1553 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1554 | return generic_block_bmap(mapping, block, xfs_get_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | STATIC int |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1558 | xfs_vm_readpage( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | struct file *unused, |
| 1560 | struct page *page) |
| 1561 | { |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1562 | return mpage_readpage(page, xfs_get_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | STATIC int |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1566 | xfs_vm_readpages( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | struct file *unused, |
| 1568 | struct address_space *mapping, |
| 1569 | struct list_head *pages, |
| 1570 | unsigned nr_pages) |
| 1571 | { |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1572 | return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 1575 | const struct address_space_operations xfs_address_space_operations = { |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1576 | .readpage = xfs_vm_readpage, |
| 1577 | .readpages = xfs_vm_readpages, |
| 1578 | .writepage = xfs_vm_writepage, |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 1579 | .writepages = xfs_vm_writepages, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | .sync_page = block_sync_page, |
Nathan Scott | 238f4c5 | 2006-03-17 17:26:25 +1100 | [diff] [blame] | 1581 | .releasepage = xfs_vm_releasepage, |
| 1582 | .invalidatepage = xfs_vm_invalidatepage, |
Nick Piggin | d79689c | 2007-10-16 01:25:06 -0700 | [diff] [blame] | 1583 | .write_begin = xfs_vm_write_begin, |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1584 | .write_end = xfs_vm_write_end, |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1585 | .bmap = xfs_vm_bmap, |
| 1586 | .direct_IO = xfs_vm_direct_IO, |
Christoph Lameter | e965f96 | 2006-02-01 03:05:41 -0800 | [diff] [blame] | 1587 | .migratepage = buffer_migrate_page, |
Hisashi Hifumi | bddaafa | 2009-03-29 09:53:38 +0200 | [diff] [blame] | 1588 | .is_partially_uptodate = block_is_partially_uptodate, |
Andi Kleen | aa261f5 | 2009-09-16 11:50:16 +0200 | [diff] [blame] | 1589 | .error_remove_page = generic_error_remove_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | }; |