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" |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 29 | #include "xfs_inode_item.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 30 | #include "xfs_alloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_error.h" |
| 32 | #include "xfs_rw.h" |
| 33 | #include "xfs_iomap.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 34 | #include "xfs_vnodeops.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 35 | #include "xfs_trace.h" |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 36 | #include "xfs_bmap.h" |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/mpage.h> |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 39 | #include <linux/pagevec.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/writeback.h> |
| 41 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 42 | void |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 43 | xfs_count_page_state( |
| 44 | struct page *page, |
| 45 | int *delalloc, |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 46 | int *unwritten) |
| 47 | { |
| 48 | struct buffer_head *bh, *head; |
| 49 | |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 50 | *delalloc = *unwritten = 0; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 51 | |
| 52 | bh = head = page_buffers(page); |
| 53 | do { |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 54 | if (buffer_unwritten(bh)) |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 55 | (*unwritten) = 1; |
| 56 | else if (buffer_delay(bh)) |
| 57 | (*delalloc) = 1; |
| 58 | } while ((bh = bh->b_this_page) != head); |
| 59 | } |
| 60 | |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 61 | STATIC struct block_device * |
| 62 | xfs_find_bdev_for_inode( |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 63 | struct inode *inode) |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 64 | { |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 65 | struct xfs_inode *ip = XFS_I(inode); |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 66 | struct xfs_mount *mp = ip->i_mount; |
| 67 | |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 68 | if (XFS_IS_REALTIME_INODE(ip)) |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 69 | return mp->m_rtdev_targp->bt_bdev; |
| 70 | else |
| 71 | return mp->m_ddev_targp->bt_bdev; |
| 72 | } |
| 73 | |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 74 | /* |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 75 | * We're now finished for good with this ioend structure. |
| 76 | * Update the page state via the associated buffer_heads, |
| 77 | * release holds on the inode and bio, and finally free |
| 78 | * up memory. Do not use the ioend after this. |
| 79 | */ |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 80 | STATIC void |
| 81 | xfs_destroy_ioend( |
| 82 | xfs_ioend_t *ioend) |
| 83 | { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 84 | struct buffer_head *bh, *next; |
| 85 | |
| 86 | for (bh = ioend->io_buffer_head; bh; bh = next) { |
| 87 | next = bh->b_private; |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 88 | bh->b_end_io(bh, !ioend->io_error); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 89 | } |
Christoph Hellwig | 583fa58 | 2008-12-03 12:20:38 +0100 | [diff] [blame] | 90 | |
Christoph Hellwig | c859cdd | 2011-08-23 08:28:10 +0000 | [diff] [blame] | 91 | if (ioend->io_iocb) { |
Christoph Hellwig | 04f658e | 2011-08-24 05:59:25 +0000 | [diff] [blame] | 92 | if (ioend->io_isasync) { |
| 93 | aio_complete(ioend->io_iocb, ioend->io_error ? |
| 94 | ioend->io_error : ioend->io_result, 0); |
| 95 | } |
Christoph Hellwig | c859cdd | 2011-08-23 08:28:10 +0000 | [diff] [blame] | 96 | inode_dio_done(ioend->io_inode); |
| 97 | } |
Christoph Hellwig | 4a06fd2 | 2011-08-23 08:28:13 +0000 | [diff] [blame] | 98 | |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 99 | mempool_free(ioend, xfs_ioend_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* |
Christoph Hellwig | fc0063c | 2011-08-23 08:28:11 +0000 | [diff] [blame] | 103 | * Fast and loose check if this write could update the on-disk inode size. |
| 104 | */ |
| 105 | static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend) |
| 106 | { |
| 107 | return ioend->io_offset + ioend->io_size > |
| 108 | XFS_I(ioend->io_inode)->i_d.di_size; |
| 109 | } |
| 110 | |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 111 | STATIC int |
| 112 | xfs_setfilesize_trans_alloc( |
| 113 | struct xfs_ioend *ioend) |
| 114 | { |
| 115 | struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount; |
| 116 | struct xfs_trans *tp; |
| 117 | int error; |
| 118 | |
| 119 | tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS); |
| 120 | |
| 121 | error = xfs_trans_reserve(tp, 0, XFS_FSYNC_TS_LOG_RES(mp), 0, 0, 0); |
| 122 | if (error) { |
| 123 | xfs_trans_cancel(tp, 0); |
| 124 | return error; |
| 125 | } |
| 126 | |
| 127 | ioend->io_append_trans = tp; |
| 128 | |
| 129 | /* |
| 130 | * We hand off the transaction to the completion thread now, so |
| 131 | * clear the flag here. |
| 132 | */ |
| 133 | current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS); |
| 134 | return 0; |
| 135 | } |
| 136 | |
Christoph Hellwig | fc0063c | 2011-08-23 08:28:11 +0000 | [diff] [blame] | 137 | /* |
Christoph Hellwig | 2813d68 | 2011-12-18 20:00:12 +0000 | [diff] [blame] | 138 | * Update on-disk file size now that data has been written to disk. |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 139 | */ |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 140 | STATIC int |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 141 | xfs_setfilesize( |
Christoph Hellwig | aa6bf01 | 2012-02-29 09:53:48 +0000 | [diff] [blame] | 142 | struct xfs_ioend *ioend) |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 143 | { |
Christoph Hellwig | aa6bf01 | 2012-02-29 09:53:48 +0000 | [diff] [blame] | 144 | struct xfs_inode *ip = XFS_I(ioend->io_inode); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 145 | struct xfs_trans *tp = ioend->io_append_trans; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 146 | xfs_fsize_t isize; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 147 | |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 148 | /* |
| 149 | * The transaction was allocated in the I/O submission thread, |
| 150 | * thus we need to mark ourselves as beeing in a transaction |
| 151 | * manually. |
| 152 | */ |
| 153 | current_set_flags_nested(&tp->t_pflags, PF_FSTRANS); |
| 154 | |
Christoph Hellwig | aa6bf01 | 2012-02-29 09:53:48 +0000 | [diff] [blame] | 155 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 6923e68 | 2012-02-29 09:53:49 +0000 | [diff] [blame] | 156 | isize = xfs_new_eof(ip, ioend->io_offset + ioend->io_size); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 157 | if (!isize) { |
| 158 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 159 | xfs_trans_cancel(tp, 0); |
| 160 | return 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 161 | } |
| 162 | |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 163 | trace_xfs_setfilesize(ip, ioend->io_offset, ioend->io_size); |
| 164 | |
| 165 | ip->i_d.di_size = isize; |
| 166 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 167 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 168 | |
| 169 | return xfs_trans_commit(tp, 0); |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 173 | * Schedule IO completion handling on the final put of an ioend. |
Christoph Hellwig | fc0063c | 2011-08-23 08:28:11 +0000 | [diff] [blame] | 174 | * |
| 175 | * If there is no work to do we might as well call it a day and free the |
| 176 | * ioend right now. |
Dave Chinner | c626d17 | 2009-04-06 18:42:11 +0200 | [diff] [blame] | 177 | */ |
| 178 | STATIC void |
| 179 | xfs_finish_ioend( |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 180 | struct xfs_ioend *ioend) |
Dave Chinner | c626d17 | 2009-04-06 18:42:11 +0200 | [diff] [blame] | 181 | { |
| 182 | if (atomic_dec_and_test(&ioend->io_remaining)) { |
Christoph Hellwig | aa6bf01 | 2012-02-29 09:53:48 +0000 | [diff] [blame] | 183 | struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount; |
| 184 | |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 185 | if (ioend->io_type == IO_UNWRITTEN) |
Christoph Hellwig | aa6bf01 | 2012-02-29 09:53:48 +0000 | [diff] [blame] | 186 | queue_work(mp->m_unwritten_workqueue, &ioend->io_work); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 187 | else if (ioend->io_append_trans) |
Christoph Hellwig | aa6bf01 | 2012-02-29 09:53:48 +0000 | [diff] [blame] | 188 | queue_work(mp->m_data_workqueue, &ioend->io_work); |
Christoph Hellwig | fc0063c | 2011-08-23 08:28:11 +0000 | [diff] [blame] | 189 | else |
| 190 | xfs_destroy_ioend(ioend); |
Dave Chinner | c626d17 | 2009-04-06 18:42:11 +0200 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | /* |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 195 | * IO write completion. |
| 196 | */ |
| 197 | STATIC void |
| 198 | xfs_end_io( |
| 199 | struct work_struct *work) |
| 200 | { |
| 201 | xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work); |
| 202 | struct xfs_inode *ip = XFS_I(ioend->io_inode); |
Dave Chinner | 6941893 | 2010-03-04 00:57:09 +0000 | [diff] [blame] | 203 | int error = 0; |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 204 | |
Christoph Hellwig | 04f658e | 2011-08-24 05:59:25 +0000 | [diff] [blame] | 205 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
Christoph Hellwig | 810627d | 2011-11-08 08:56:15 +0000 | [diff] [blame] | 206 | ioend->io_error = -EIO; |
Christoph Hellwig | 04f658e | 2011-08-24 05:59:25 +0000 | [diff] [blame] | 207 | goto done; |
| 208 | } |
| 209 | if (ioend->io_error) |
| 210 | goto done; |
| 211 | |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 212 | /* |
| 213 | * For unwritten extents we need to issue transactions to convert a |
| 214 | * range to normal written extens after the data I/O has finished. |
| 215 | */ |
Christoph Hellwig | 04f658e | 2011-08-24 05:59:25 +0000 | [diff] [blame] | 216 | if (ioend->io_type == IO_UNWRITTEN) { |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 217 | /* |
| 218 | * For buffered I/O we never preallocate a transaction when |
| 219 | * doing the unwritten extent conversion, but for direct I/O |
| 220 | * we do not know if we are converting an unwritten extent |
| 221 | * or not at the point where we preallocate the transaction. |
| 222 | */ |
| 223 | if (ioend->io_append_trans) { |
| 224 | ASSERT(ioend->io_isdirect); |
| 225 | |
| 226 | current_set_flags_nested( |
| 227 | &ioend->io_append_trans->t_pflags, PF_FSTRANS); |
| 228 | xfs_trans_cancel(ioend->io_append_trans, 0); |
| 229 | } |
| 230 | |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 231 | error = xfs_iomap_write_unwritten(ip, ioend->io_offset, |
| 232 | ioend->io_size); |
Christoph Hellwig | 04f658e | 2011-08-24 05:59:25 +0000 | [diff] [blame] | 233 | if (error) { |
| 234 | ioend->io_error = -error; |
| 235 | goto done; |
| 236 | } |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 237 | } else if (ioend->io_append_trans) { |
| 238 | error = xfs_setfilesize(ioend); |
| 239 | if (error) |
| 240 | ioend->io_error = -error; |
Christoph Hellwig | 84803fb | 2012-02-29 09:53:50 +0000 | [diff] [blame] | 241 | } else { |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 242 | ASSERT(!xfs_ioend_is_append(ioend)); |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Christoph Hellwig | 04f658e | 2011-08-24 05:59:25 +0000 | [diff] [blame] | 245 | done: |
Christoph Hellwig | aa6bf01 | 2012-02-29 09:53:48 +0000 | [diff] [blame] | 246 | xfs_destroy_ioend(ioend); |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 250 | * Call IO completion handling in caller context on the final put of an ioend. |
| 251 | */ |
| 252 | STATIC void |
| 253 | xfs_finish_ioend_sync( |
| 254 | struct xfs_ioend *ioend) |
| 255 | { |
| 256 | if (atomic_dec_and_test(&ioend->io_remaining)) |
| 257 | xfs_end_io(&ioend->io_work); |
| 258 | } |
| 259 | |
| 260 | /* |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 261 | * Allocate and initialise an IO completion structure. |
| 262 | * We need to track unwritten extent write completion here initially. |
| 263 | * We'll need to extend this for updating the ondisk inode size later |
| 264 | * (vs. incore size). |
| 265 | */ |
| 266 | STATIC xfs_ioend_t * |
| 267 | xfs_alloc_ioend( |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 268 | struct inode *inode, |
| 269 | unsigned int type) |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 270 | { |
| 271 | xfs_ioend_t *ioend; |
| 272 | |
| 273 | ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS); |
| 274 | |
| 275 | /* |
| 276 | * Set the count to 1 initially, which will prevent an I/O |
| 277 | * completion callback from happening before we have started |
| 278 | * all the I/O from calling the completion routine too early. |
| 279 | */ |
| 280 | atomic_set(&ioend->io_remaining, 1); |
Christoph Hellwig | c859cdd | 2011-08-23 08:28:10 +0000 | [diff] [blame] | 281 | ioend->io_isasync = 0; |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 282 | ioend->io_isdirect = 0; |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 283 | ioend->io_error = 0; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 284 | ioend->io_list = NULL; |
| 285 | ioend->io_type = type; |
Christoph Hellwig | b677c21 | 2007-08-29 11:46:28 +1000 | [diff] [blame] | 286 | ioend->io_inode = inode; |
Christoph Hellwig | c1a073b | 2005-09-05 08:23:35 +1000 | [diff] [blame] | 287 | ioend->io_buffer_head = NULL; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 288 | ioend->io_buffer_tail = NULL; |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 289 | ioend->io_offset = 0; |
| 290 | ioend->io_size = 0; |
Christoph Hellwig | fb511f2 | 2010-07-18 21:17:10 +0000 | [diff] [blame] | 291 | ioend->io_iocb = NULL; |
| 292 | ioend->io_result = 0; |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 293 | ioend->io_append_trans = NULL; |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 294 | |
Christoph Hellwig | 5ec4fab | 2009-10-30 09:11:47 +0000 | [diff] [blame] | 295 | INIT_WORK(&ioend->io_work, xfs_end_io); |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 296 | return ioend; |
| 297 | } |
| 298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | STATIC int |
| 300 | xfs_map_blocks( |
| 301 | struct inode *inode, |
| 302 | loff_t offset, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 303 | struct xfs_bmbt_irec *imap, |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 304 | int type, |
| 305 | int nonblocking) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | { |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 307 | struct xfs_inode *ip = XFS_I(inode); |
| 308 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | ed1e7b7 | 2010-12-10 08:42:22 +0000 | [diff] [blame] | 309 | ssize_t count = 1 << inode->i_blkbits; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 310 | xfs_fileoff_t offset_fsb, end_fsb; |
| 311 | int error = 0; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 312 | int bmapi_flags = XFS_BMAPI_ENTIRE; |
| 313 | int nimaps = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 315 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 316 | return -XFS_ERROR(EIO); |
| 317 | |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 318 | if (type == IO_UNWRITTEN) |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 319 | bmapi_flags |= XFS_BMAPI_IGSTATE; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 320 | |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 321 | if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) { |
| 322 | if (nonblocking) |
| 323 | return -XFS_ERROR(EAGAIN); |
| 324 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 327 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
| 328 | (ip->i_df.if_flags & XFS_IFEXTENTS)); |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 329 | ASSERT(offset <= mp->m_maxioffset); |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 330 | |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 331 | if (offset + count > mp->m_maxioffset) |
| 332 | count = mp->m_maxioffset - offset; |
| 333 | end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count); |
| 334 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 335 | error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, |
| 336 | imap, &nimaps, bmapi_flags); |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 337 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 338 | |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 339 | if (error) |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 340 | return -XFS_ERROR(error); |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 341 | |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 342 | if (type == IO_DELALLOC && |
| 343 | (!nimaps || isnullstartblock(imap->br_startblock))) { |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 344 | error = xfs_iomap_write_allocate(ip, offset, count, imap); |
| 345 | if (!error) |
| 346 | trace_xfs_map_blocks_alloc(ip, offset, count, type, imap); |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 347 | return -XFS_ERROR(error); |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 350 | #ifdef DEBUG |
| 351 | if (type == IO_UNWRITTEN) { |
| 352 | ASSERT(nimaps); |
| 353 | ASSERT(imap->br_startblock != HOLESTARTBLOCK); |
| 354 | ASSERT(imap->br_startblock != DELAYSTARTBLOCK); |
| 355 | } |
| 356 | #endif |
| 357 | if (nimaps) |
| 358 | trace_xfs_map_blocks_found(ip, offset, count, type, imap); |
| 359 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Christoph Hellwig | b8f82a4 | 2009-11-14 16:17:22 +0000 | [diff] [blame] | 362 | STATIC int |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 363 | xfs_imap_valid( |
Christoph Hellwig | 8699bb0 | 2010-04-28 12:28:54 +0000 | [diff] [blame] | 364 | struct inode *inode, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 365 | struct xfs_bmbt_irec *imap, |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 366 | xfs_off_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 368 | offset >>= inode->i_blkbits; |
Christoph Hellwig | 8699bb0 | 2010-04-28 12:28:54 +0000 | [diff] [blame] | 369 | |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 370 | return offset >= imap->br_startoff && |
| 371 | offset < imap->br_startoff + imap->br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 374 | /* |
| 375 | * BIO completion handler for buffered IO. |
| 376 | */ |
Al Viro | 782e3b3 | 2007-10-12 07:17:47 +0100 | [diff] [blame] | 377 | STATIC void |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 378 | xfs_end_bio( |
| 379 | struct bio *bio, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 380 | int error) |
| 381 | { |
| 382 | xfs_ioend_t *ioend = bio->bi_private; |
| 383 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 384 | ASSERT(atomic_read(&bio->bi_cnt) >= 1); |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 385 | ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 386 | |
| 387 | /* Toss bio and pass work off to an xfsdatad thread */ |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 388 | bio->bi_private = NULL; |
| 389 | bio->bi_end_io = NULL; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 390 | bio_put(bio); |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 391 | |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 392 | xfs_finish_ioend(ioend); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | STATIC void |
| 396 | xfs_submit_ioend_bio( |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 397 | struct writeback_control *wbc, |
| 398 | xfs_ioend_t *ioend, |
| 399 | struct bio *bio) |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 400 | { |
| 401 | atomic_inc(&ioend->io_remaining); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 402 | bio->bi_private = ioend; |
| 403 | bio->bi_end_io = xfs_end_bio; |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 404 | submit_bio(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE, bio); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | STATIC struct bio * |
| 408 | xfs_alloc_ioend_bio( |
| 409 | struct buffer_head *bh) |
| 410 | { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 411 | int nvecs = bio_get_nr_vecs(bh->b_bdev); |
Christoph Hellwig | 221cb25 | 2010-12-10 08:42:17 +0000 | [diff] [blame] | 412 | struct bio *bio = bio_alloc(GFP_NOIO, nvecs); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 413 | |
| 414 | ASSERT(bio->bi_private == NULL); |
| 415 | bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9); |
| 416 | bio->bi_bdev = bh->b_bdev; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 417 | return bio; |
| 418 | } |
| 419 | |
| 420 | STATIC void |
| 421 | xfs_start_buffer_writeback( |
| 422 | struct buffer_head *bh) |
| 423 | { |
| 424 | ASSERT(buffer_mapped(bh)); |
| 425 | ASSERT(buffer_locked(bh)); |
| 426 | ASSERT(!buffer_delay(bh)); |
| 427 | ASSERT(!buffer_unwritten(bh)); |
| 428 | |
| 429 | mark_buffer_async_write(bh); |
| 430 | set_buffer_uptodate(bh); |
| 431 | clear_buffer_dirty(bh); |
| 432 | } |
| 433 | |
| 434 | STATIC void |
| 435 | xfs_start_page_writeback( |
| 436 | struct page *page, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 437 | int clear_dirty, |
| 438 | int buffers) |
| 439 | { |
| 440 | ASSERT(PageLocked(page)); |
| 441 | ASSERT(!PageWriteback(page)); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 442 | if (clear_dirty) |
David Chinner | 9213202 | 2006-12-21 10:24:01 +1100 | [diff] [blame] | 443 | clear_page_dirty_for_io(page); |
| 444 | set_page_writeback(page); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 445 | unlock_page(page); |
Fengguang Wu | 1f7decf | 2007-10-16 23:30:42 -0700 | [diff] [blame] | 446 | /* If no buffers on the page are to be written, finish it here */ |
| 447 | if (!buffers) |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 448 | end_page_writeback(page); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh) |
| 452 | { |
| 453 | return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh)); |
| 454 | } |
| 455 | |
| 456 | /* |
David Chinner | d88992f | 2006-01-18 13:38:12 +1100 | [diff] [blame] | 457 | * Submit all of the bios for all of the ioends we have saved up, covering the |
| 458 | * initial writepage page and also any probed pages. |
| 459 | * |
| 460 | * Because we may have multiple ioends spanning a page, we need to start |
| 461 | * writeback on all the buffers before we submit them for I/O. If we mark the |
| 462 | * buffers as we got, then we can end up with a page that only has buffers |
| 463 | * marked async write and I/O complete on can occur before we mark the other |
| 464 | * buffers async write. |
| 465 | * |
| 466 | * The end result of this is that we trip a bug in end_page_writeback() because |
| 467 | * we call it twice for the one page as the code in end_buffer_async_write() |
| 468 | * assumes that all buffers on the page are started at the same time. |
| 469 | * |
| 470 | * 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] | 471 | * 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] | 472 | */ |
| 473 | STATIC void |
| 474 | xfs_submit_ioend( |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 475 | struct writeback_control *wbc, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 476 | xfs_ioend_t *ioend) |
| 477 | { |
David Chinner | d88992f | 2006-01-18 13:38:12 +1100 | [diff] [blame] | 478 | xfs_ioend_t *head = ioend; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 479 | xfs_ioend_t *next; |
| 480 | struct buffer_head *bh; |
| 481 | struct bio *bio; |
| 482 | sector_t lastblock = 0; |
| 483 | |
David Chinner | d88992f | 2006-01-18 13:38:12 +1100 | [diff] [blame] | 484 | /* Pass 1 - start writeback */ |
| 485 | do { |
| 486 | next = ioend->io_list; |
Christoph Hellwig | 221cb25 | 2010-12-10 08:42:17 +0000 | [diff] [blame] | 487 | for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) |
David Chinner | d88992f | 2006-01-18 13:38:12 +1100 | [diff] [blame] | 488 | xfs_start_buffer_writeback(bh); |
David Chinner | d88992f | 2006-01-18 13:38:12 +1100 | [diff] [blame] | 489 | } while ((ioend = next) != NULL); |
| 490 | |
| 491 | /* Pass 2 - submit I/O */ |
| 492 | ioend = head; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 493 | do { |
| 494 | next = ioend->io_list; |
| 495 | bio = NULL; |
| 496 | |
| 497 | for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 498 | |
| 499 | if (!bio) { |
| 500 | retry: |
| 501 | bio = xfs_alloc_ioend_bio(bh); |
| 502 | } else if (bh->b_blocknr != lastblock + 1) { |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 503 | xfs_submit_ioend_bio(wbc, ioend, bio); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 504 | goto retry; |
| 505 | } |
| 506 | |
| 507 | if (bio_add_buffer(bio, bh) != bh->b_size) { |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 508 | xfs_submit_ioend_bio(wbc, ioend, bio); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 509 | goto retry; |
| 510 | } |
| 511 | |
| 512 | lastblock = bh->b_blocknr; |
| 513 | } |
| 514 | if (bio) |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 515 | xfs_submit_ioend_bio(wbc, ioend, bio); |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 516 | xfs_finish_ioend(ioend); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 517 | } while ((ioend = next) != NULL); |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | * Cancel submission of all buffer_heads so far in this endio. |
| 522 | * Toss the endio too. Only ever called for the initial page |
| 523 | * in a writepage request, so only ever one page. |
| 524 | */ |
| 525 | STATIC void |
| 526 | xfs_cancel_ioend( |
| 527 | xfs_ioend_t *ioend) |
| 528 | { |
| 529 | xfs_ioend_t *next; |
| 530 | struct buffer_head *bh, *next_bh; |
| 531 | |
| 532 | do { |
| 533 | next = ioend->io_list; |
| 534 | bh = ioend->io_buffer_head; |
| 535 | do { |
| 536 | next_bh = bh->b_private; |
| 537 | clear_buffer_async_write(bh); |
| 538 | unlock_buffer(bh); |
| 539 | } while ((bh = next_bh) != NULL); |
| 540 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 541 | mempool_free(ioend, xfs_ioend_pool); |
| 542 | } while ((ioend = next) != NULL); |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * Test to see if we've been building up a completion structure for |
| 547 | * earlier buffers -- if so, we try to append to this ioend if we |
| 548 | * can, otherwise we finish off any current ioend and start another. |
| 549 | * Return true if we've finished the given ioend. |
| 550 | */ |
| 551 | STATIC void |
| 552 | xfs_add_to_ioend( |
| 553 | struct inode *inode, |
| 554 | struct buffer_head *bh, |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 555 | xfs_off_t offset, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 556 | unsigned int type, |
| 557 | xfs_ioend_t **result, |
| 558 | int need_ioend) |
| 559 | { |
| 560 | xfs_ioend_t *ioend = *result; |
| 561 | |
| 562 | if (!ioend || need_ioend || type != ioend->io_type) { |
| 563 | xfs_ioend_t *previous = *result; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 564 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 565 | ioend = xfs_alloc_ioend(inode, type); |
| 566 | ioend->io_offset = offset; |
| 567 | ioend->io_buffer_head = bh; |
| 568 | ioend->io_buffer_tail = bh; |
| 569 | if (previous) |
| 570 | previous->io_list = ioend; |
| 571 | *result = ioend; |
| 572 | } else { |
| 573 | ioend->io_buffer_tail->b_private = bh; |
| 574 | ioend->io_buffer_tail = bh; |
| 575 | } |
| 576 | |
| 577 | bh->b_private = NULL; |
| 578 | ioend->io_size += bh->b_size; |
| 579 | } |
| 580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | STATIC void |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 582 | xfs_map_buffer( |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 583 | struct inode *inode, |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 584 | struct buffer_head *bh, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 585 | struct xfs_bmbt_irec *imap, |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 586 | xfs_off_t offset) |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 587 | { |
| 588 | sector_t bn; |
Christoph Hellwig | 8699bb0 | 2010-04-28 12:28:54 +0000 | [diff] [blame] | 589 | struct xfs_mount *m = XFS_I(inode)->i_mount; |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 590 | xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff); |
| 591 | 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] | 592 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 593 | ASSERT(imap->br_startblock != HOLESTARTBLOCK); |
| 594 | ASSERT(imap->br_startblock != DELAYSTARTBLOCK); |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 595 | |
Christoph Hellwig | e513182 | 2010-04-28 12:28:55 +0000 | [diff] [blame] | 596 | bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) + |
Christoph Hellwig | 8699bb0 | 2010-04-28 12:28:54 +0000 | [diff] [blame] | 597 | ((offset - iomap_offset) >> inode->i_blkbits); |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 598 | |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 599 | ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode))); |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 600 | |
| 601 | bh->b_blocknr = bn; |
| 602 | set_buffer_mapped(bh); |
| 603 | } |
| 604 | |
| 605 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | xfs_map_at_offset( |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 607 | struct inode *inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | struct buffer_head *bh, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 609 | struct xfs_bmbt_irec *imap, |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 610 | xfs_off_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | { |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 612 | ASSERT(imap->br_startblock != HOLESTARTBLOCK); |
| 613 | ASSERT(imap->br_startblock != DELAYSTARTBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 615 | xfs_map_buffer(inode, bh, imap, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | set_buffer_mapped(bh); |
| 617 | clear_buffer_delay(bh); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 618 | clear_buffer_unwritten(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /* |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 622 | * Test if a given page is suitable for writing as part of an unwritten |
| 623 | * or delayed allocate extent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | */ |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 625 | STATIC int |
| 626 | xfs_is_delayed_page( |
| 627 | struct page *page, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 628 | unsigned int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | if (PageWriteback(page)) |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 631 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
| 633 | if (page->mapping && page_has_buffers(page)) { |
| 634 | struct buffer_head *bh, *head; |
| 635 | int acceptable = 0; |
| 636 | |
| 637 | bh = head = page_buffers(page); |
| 638 | do { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 639 | if (buffer_unwritten(bh)) |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 640 | acceptable = (type == IO_UNWRITTEN); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 641 | else if (buffer_delay(bh)) |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 642 | acceptable = (type == IO_DELALLOC); |
David Chinner | 2ddee84 | 2006-03-22 12:47:40 +1100 | [diff] [blame] | 643 | else if (buffer_dirty(bh) && buffer_mapped(bh)) |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 644 | acceptable = (type == IO_OVERWRITE); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 645 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | } while ((bh = bh->b_this_page) != head); |
| 648 | |
| 649 | if (acceptable) |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 650 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 653 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | /* |
| 657 | * Allocate & map buffers for page given the extent map. Write it out. |
| 658 | * except for the original page of a writepage, this is called on |
| 659 | * delalloc/unwritten pages only, for the original page it is possible |
| 660 | * that the page has no mapping at all. |
| 661 | */ |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 662 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | xfs_convert_page( |
| 664 | struct inode *inode, |
| 665 | struct page *page, |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 666 | loff_t tindex, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 667 | struct xfs_bmbt_irec *imap, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 668 | xfs_ioend_t **ioendp, |
Christoph Hellwig | 2fa24f9 | 2010-12-10 08:42:23 +0000 | [diff] [blame] | 669 | struct writeback_control *wbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 671 | struct buffer_head *bh, *head; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 672 | xfs_off_t end_offset; |
| 673 | unsigned long p_offset; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 674 | unsigned int type; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 675 | int len, page_dirty; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 676 | int count = 0, done = 0, uptodate = 1; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 677 | xfs_off_t offset = page_offset(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 679 | if (page->index != tindex) |
| 680 | goto fail; |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 681 | if (!trylock_page(page)) |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 682 | goto fail; |
| 683 | if (PageWriteback(page)) |
| 684 | goto fail_unlock_page; |
| 685 | if (page->mapping != inode->i_mapping) |
| 686 | goto fail_unlock_page; |
| 687 | if (!xfs_is_delayed_page(page, (*ioendp)->io_type)) |
| 688 | goto fail_unlock_page; |
| 689 | |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 690 | /* |
| 691 | * page_dirty is initially a count of buffers on the page before |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 692 | * EOF and is decremented as we move each into a cleanable state. |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 693 | * |
| 694 | * Derivation: |
| 695 | * |
| 696 | * End offset is the highest offset that this page should represent. |
| 697 | * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1)) |
| 698 | * will evaluate non-zero and be less than PAGE_CACHE_SIZE and |
| 699 | * hence give us the correct page_dirty count. On any other page, |
| 700 | * it will be zero and in that case we need page_dirty to be the |
| 701 | * count of buffers on the page. |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 702 | */ |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 703 | end_offset = min_t(unsigned long long, |
| 704 | (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, |
| 705 | i_size_read(inode)); |
| 706 | |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 707 | len = 1 << inode->i_blkbits; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 708 | p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1), |
| 709 | PAGE_CACHE_SIZE); |
| 710 | p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE; |
| 711 | page_dirty = p_offset / len; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | bh = head = page_buffers(page); |
| 714 | do { |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 715 | if (offset >= end_offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | break; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 717 | if (!buffer_uptodate(bh)) |
| 718 | uptodate = 0; |
| 719 | if (!(PageUptodate(page) || buffer_uptodate(bh))) { |
| 720 | done = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | continue; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 722 | } |
| 723 | |
Christoph Hellwig | 2fa24f9 | 2010-12-10 08:42:23 +0000 | [diff] [blame] | 724 | if (buffer_unwritten(bh) || buffer_delay(bh) || |
| 725 | buffer_mapped(bh)) { |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 726 | if (buffer_unwritten(bh)) |
Christoph Hellwig | 34a52c6 | 2010-04-28 12:28:57 +0000 | [diff] [blame] | 727 | type = IO_UNWRITTEN; |
Christoph Hellwig | 2fa24f9 | 2010-12-10 08:42:23 +0000 | [diff] [blame] | 728 | else if (buffer_delay(bh)) |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 729 | type = IO_DELALLOC; |
Christoph Hellwig | 2fa24f9 | 2010-12-10 08:42:23 +0000 | [diff] [blame] | 730 | else |
| 731 | type = IO_OVERWRITE; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 732 | |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 733 | if (!xfs_imap_valid(inode, imap, offset)) { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 734 | done = 1; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 735 | continue; |
| 736 | } |
| 737 | |
Christoph Hellwig | ecff71e | 2010-12-10 08:42:25 +0000 | [diff] [blame] | 738 | lock_buffer(bh); |
| 739 | if (type != IO_OVERWRITE) |
Christoph Hellwig | 2fa24f9 | 2010-12-10 08:42:23 +0000 | [diff] [blame] | 740 | xfs_map_at_offset(inode, bh, imap, offset); |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 741 | xfs_add_to_ioend(inode, bh, offset, type, |
| 742 | ioendp, done); |
| 743 | |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 744 | page_dirty--; |
| 745 | count++; |
| 746 | } else { |
Christoph Hellwig | 2fa24f9 | 2010-12-10 08:42:23 +0000 | [diff] [blame] | 747 | done = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | } |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 749 | } while (offset += len, (bh = bh->b_this_page) != head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 751 | if (uptodate && bh == head) |
| 752 | SetPageUptodate(page); |
| 753 | |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 754 | if (count) { |
Dave Chinner | efceab1 | 2010-08-24 11:44:56 +1000 | [diff] [blame] | 755 | if (--wbc->nr_to_write <= 0 && |
| 756 | wbc->sync_mode == WB_SYNC_NONE) |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 757 | done = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | } |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 759 | xfs_start_page_writeback(page, !page_dirty, count); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 760 | |
| 761 | return done; |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 762 | fail_unlock_page: |
| 763 | unlock_page(page); |
| 764 | fail: |
| 765 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /* |
| 769 | * Convert & write out a cluster of pages in the same extent as defined |
| 770 | * by mp and following the start page. |
| 771 | */ |
| 772 | STATIC void |
| 773 | xfs_cluster_write( |
| 774 | struct inode *inode, |
| 775 | pgoff_t tindex, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 776 | struct xfs_bmbt_irec *imap, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 777 | xfs_ioend_t **ioendp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | struct writeback_control *wbc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | pgoff_t tlast) |
| 780 | { |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 781 | struct pagevec pvec; |
| 782 | int done = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 784 | pagevec_init(&pvec, 0); |
| 785 | while (!done && tindex <= tlast) { |
| 786 | unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1); |
| 787 | |
| 788 | if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | break; |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 790 | |
| 791 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 792 | done = xfs_convert_page(inode, pvec.pages[i], tindex++, |
Christoph Hellwig | 2fa24f9 | 2010-12-10 08:42:23 +0000 | [diff] [blame] | 793 | imap, ioendp, wbc); |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 794 | if (done) |
| 795 | break; |
| 796 | } |
| 797 | |
| 798 | pagevec_release(&pvec); |
| 799 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 803 | STATIC void |
| 804 | xfs_vm_invalidatepage( |
| 805 | struct page *page, |
| 806 | unsigned long offset) |
| 807 | { |
| 808 | trace_xfs_invalidatepage(page->mapping->host, page, offset); |
| 809 | block_invalidatepage(page, offset); |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | * If the page has delalloc buffers on it, we need to punch them out before we |
| 814 | * invalidate the page. If we don't, we leave a stale delalloc mapping on the |
| 815 | * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read |
| 816 | * is done on that same region - the delalloc extent is returned when none is |
| 817 | * supposed to be there. |
| 818 | * |
| 819 | * We prevent this by truncating away the delalloc regions on the page before |
| 820 | * invalidating it. Because they are delalloc, we can do this without needing a |
| 821 | * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this |
| 822 | * truncation without a transaction as there is no space left for block |
| 823 | * reservation (typically why we see a ENOSPC in writeback). |
| 824 | * |
| 825 | * This is not a performance critical path, so for now just do the punching a |
| 826 | * buffer head at a time. |
| 827 | */ |
| 828 | STATIC void |
| 829 | xfs_aops_discard_page( |
| 830 | struct page *page) |
| 831 | { |
| 832 | struct inode *inode = page->mapping->host; |
| 833 | struct xfs_inode *ip = XFS_I(inode); |
| 834 | struct buffer_head *bh, *head; |
| 835 | loff_t offset = page_offset(page); |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 836 | |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 837 | if (!xfs_is_delayed_page(page, IO_DELALLOC)) |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 838 | goto out_invalidate; |
| 839 | |
Dave Chinner | e8c3753 | 2010-03-15 02:36:35 +0000 | [diff] [blame] | 840 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 841 | goto out_invalidate; |
| 842 | |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 843 | xfs_alert(ip->i_mount, |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 844 | "page discard on page %p, inode 0x%llx, offset %llu.", |
| 845 | page, ip->i_ino, offset); |
| 846 | |
| 847 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 848 | bh = head = page_buffers(page); |
| 849 | do { |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 850 | int error; |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 851 | xfs_fileoff_t start_fsb; |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 852 | |
| 853 | if (!buffer_delay(bh)) |
| 854 | goto next_buffer; |
| 855 | |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 856 | start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset); |
| 857 | error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1); |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 858 | if (error) { |
| 859 | /* something screwed, just bail */ |
Dave Chinner | e8c3753 | 2010-03-15 02:36:35 +0000 | [diff] [blame] | 860 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 861 | xfs_alert(ip->i_mount, |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 862 | "page discard unable to remove delalloc mapping."); |
Dave Chinner | e8c3753 | 2010-03-15 02:36:35 +0000 | [diff] [blame] | 863 | } |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 864 | break; |
| 865 | } |
| 866 | next_buffer: |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 867 | offset += 1 << inode->i_blkbits; |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 868 | |
| 869 | } while ((bh = bh->b_this_page) != head); |
| 870 | |
| 871 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 872 | out_invalidate: |
| 873 | xfs_vm_invalidatepage(page, 0); |
| 874 | return; |
| 875 | } |
| 876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | /* |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 878 | * Write out a dirty page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | * |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 880 | * For delalloc space on the page we need to allocate space and flush it. |
| 881 | * For unwritten space on the page we need to start the conversion to |
| 882 | * regular allocated space. |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 883 | * For any other dirty buffer heads on the page we should flush them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | STATIC int |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 886 | xfs_vm_writepage( |
| 887 | struct page *page, |
| 888 | struct writeback_control *wbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | { |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 890 | struct inode *inode = page->mapping->host; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 891 | struct buffer_head *bh, *head; |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 892 | struct xfs_bmbt_irec imap; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 893 | xfs_ioend_t *ioend = NULL, *iohead = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | loff_t offset; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 895 | unsigned int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | __uint64_t end_offset; |
Christoph Hellwig | bd1556a | 2010-04-28 12:29:00 +0000 | [diff] [blame] | 897 | pgoff_t end_index, last_index; |
Christoph Hellwig | ed1e7b7 | 2010-12-10 08:42:22 +0000 | [diff] [blame] | 898 | ssize_t len; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 899 | int err, imap_valid = 0, uptodate = 1; |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 900 | int count = 0; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 901 | int nonblocking = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 903 | trace_xfs_writepage(inode, page, 0); |
| 904 | |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 905 | ASSERT(page_has_buffers(page)); |
| 906 | |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 907 | /* |
| 908 | * Refuse to write the page out if we are called from reclaim context. |
| 909 | * |
Christoph Hellwig | d4f7a5c | 2010-06-28 10:34:44 -0400 | [diff] [blame] | 910 | * This avoids stack overflows when called from deeply used stacks in |
| 911 | * random callers for direct reclaim or memcg reclaim. We explicitly |
| 912 | * allow reclaim from kswapd as the stack usage there is relatively low. |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 913 | * |
Mel Gorman | 94054fa | 2011-10-31 17:07:45 -0700 | [diff] [blame] | 914 | * This should never happen except in the case of a VM regression so |
| 915 | * warn about it. |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 916 | */ |
Mel Gorman | 94054fa | 2011-10-31 17:07:45 -0700 | [diff] [blame] | 917 | if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == |
| 918 | PF_MEMALLOC)) |
Christoph Hellwig | b5420f2 | 2010-08-24 11:47:51 +1000 | [diff] [blame] | 919 | goto redirty; |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 920 | |
| 921 | /* |
Christoph Hellwig | 680a647 | 2011-07-08 14:34:05 +0200 | [diff] [blame] | 922 | * Given that we do not allow direct reclaim to call us, we should |
| 923 | * never be called while in a filesystem transaction. |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 924 | */ |
Christoph Hellwig | 680a647 | 2011-07-08 14:34:05 +0200 | [diff] [blame] | 925 | if (WARN_ON(current->flags & PF_FSTRANS)) |
Christoph Hellwig | b5420f2 | 2010-08-24 11:47:51 +1000 | [diff] [blame] | 926 | goto redirty; |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 927 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | /* Is this page beyond the end of the file? */ |
| 929 | offset = i_size_read(inode); |
| 930 | end_index = offset >> PAGE_CACHE_SHIFT; |
| 931 | last_index = (offset - 1) >> PAGE_CACHE_SHIFT; |
| 932 | if (page->index >= end_index) { |
| 933 | if ((page->index >= end_index + 1) || |
| 934 | !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) { |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 935 | unlock_page(page); |
Nathan Scott | 19d5bcf | 2005-11-02 15:14:09 +1100 | [diff] [blame] | 936 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 940 | end_offset = min_t(unsigned long long, |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 941 | (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, |
| 942 | offset); |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 943 | len = 1 << inode->i_blkbits; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 944 | |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 945 | bh = head = page_buffers(page); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 946 | offset = page_offset(page); |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 947 | type = IO_OVERWRITE; |
| 948 | |
Christoph Hellwig | dbcdde3 | 2011-07-08 14:34:14 +0200 | [diff] [blame] | 949 | if (wbc->sync_mode == WB_SYNC_NONE) |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 950 | nonblocking = 1; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | do { |
Christoph Hellwig | 6ac7248 | 2010-12-10 08:42:18 +0000 | [diff] [blame] | 953 | int new_ioend = 0; |
| 954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | if (offset >= end_offset) |
| 956 | break; |
| 957 | if (!buffer_uptodate(bh)) |
| 958 | uptodate = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | |
Eric Sandeen | 3d9b02e | 2010-06-24 09:45:30 +1000 | [diff] [blame] | 960 | /* |
Christoph Hellwig | ece413f | 2010-11-10 21:39:11 +0000 | [diff] [blame] | 961 | * set_page_dirty dirties all buffers in a page, independent |
| 962 | * of their state. The dirty state however is entirely |
| 963 | * meaningless for holes (!mapped && uptodate), so skip |
| 964 | * buffers covering holes here. |
Eric Sandeen | 3d9b02e | 2010-06-24 09:45:30 +1000 | [diff] [blame] | 965 | */ |
| 966 | if (!buffer_mapped(bh) && buffer_uptodate(bh)) { |
Eric Sandeen | 3d9b02e | 2010-06-24 09:45:30 +1000 | [diff] [blame] | 967 | imap_valid = 0; |
| 968 | continue; |
| 969 | } |
| 970 | |
Christoph Hellwig | aeea1b1 | 2010-12-10 08:42:24 +0000 | [diff] [blame] | 971 | if (buffer_unwritten(bh)) { |
| 972 | if (type != IO_UNWRITTEN) { |
| 973 | type = IO_UNWRITTEN; |
| 974 | imap_valid = 0; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 975 | } |
Christoph Hellwig | aeea1b1 | 2010-12-10 08:42:24 +0000 | [diff] [blame] | 976 | } else if (buffer_delay(bh)) { |
| 977 | if (type != IO_DELALLOC) { |
| 978 | type = IO_DELALLOC; |
| 979 | imap_valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | } |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 981 | } else if (buffer_uptodate(bh)) { |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 982 | if (type != IO_OVERWRITE) { |
| 983 | type = IO_OVERWRITE; |
Christoph Hellwig | 85da94c | 2010-12-10 08:42:16 +0000 | [diff] [blame] | 984 | imap_valid = 0; |
| 985 | } |
Christoph Hellwig | aeea1b1 | 2010-12-10 08:42:24 +0000 | [diff] [blame] | 986 | } else { |
| 987 | if (PageUptodate(page)) { |
| 988 | ASSERT(buffer_mapped(bh)); |
| 989 | imap_valid = 0; |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 990 | } |
Christoph Hellwig | aeea1b1 | 2010-12-10 08:42:24 +0000 | [diff] [blame] | 991 | continue; |
| 992 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | |
Christoph Hellwig | aeea1b1 | 2010-12-10 08:42:24 +0000 | [diff] [blame] | 994 | if (imap_valid) |
| 995 | imap_valid = xfs_imap_valid(inode, &imap, offset); |
| 996 | if (!imap_valid) { |
| 997 | /* |
| 998 | * If we didn't have a valid mapping then we need to |
| 999 | * put the new mapping into a separate ioend structure. |
| 1000 | * This ensures non-contiguous extents always have |
| 1001 | * separate ioends, which is particularly important |
| 1002 | * for unwritten extent conversion at I/O completion |
| 1003 | * time. |
| 1004 | */ |
| 1005 | new_ioend = 1; |
| 1006 | err = xfs_map_blocks(inode, offset, &imap, type, |
| 1007 | nonblocking); |
| 1008 | if (err) |
| 1009 | goto error; |
| 1010 | imap_valid = xfs_imap_valid(inode, &imap, offset); |
| 1011 | } |
| 1012 | if (imap_valid) { |
Christoph Hellwig | ecff71e | 2010-12-10 08:42:25 +0000 | [diff] [blame] | 1013 | lock_buffer(bh); |
| 1014 | if (type != IO_OVERWRITE) |
Christoph Hellwig | aeea1b1 | 2010-12-10 08:42:24 +0000 | [diff] [blame] | 1015 | xfs_map_at_offset(inode, bh, &imap, offset); |
| 1016 | xfs_add_to_ioend(inode, bh, offset, type, &ioend, |
| 1017 | new_ioend); |
| 1018 | count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | } |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1020 | |
| 1021 | if (!iohead) |
| 1022 | iohead = ioend; |
| 1023 | |
| 1024 | } while (offset += len, ((bh = bh->b_this_page) != head)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | |
| 1026 | if (uptodate && bh == head) |
| 1027 | SetPageUptodate(page); |
| 1028 | |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1029 | xfs_start_page_writeback(page, 1, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 1031 | if (ioend && imap_valid) { |
Christoph Hellwig | bd1556a | 2010-04-28 12:29:00 +0000 | [diff] [blame] | 1032 | xfs_off_t end_index; |
Christoph Hellwig | 8699bb0 | 2010-04-28 12:28:54 +0000 | [diff] [blame] | 1033 | |
Christoph Hellwig | bd1556a | 2010-04-28 12:29:00 +0000 | [diff] [blame] | 1034 | end_index = imap.br_startoff + imap.br_blockcount; |
| 1035 | |
| 1036 | /* to bytes */ |
| 1037 | end_index <<= inode->i_blkbits; |
| 1038 | |
| 1039 | /* to pages */ |
| 1040 | end_index = (end_index - 1) >> PAGE_CACHE_SHIFT; |
| 1041 | |
| 1042 | /* check against file size */ |
| 1043 | if (end_index > last_index) |
| 1044 | end_index = last_index; |
| 1045 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1046 | xfs_cluster_write(inode, page->index + 1, &imap, &ioend, |
Christoph Hellwig | 2fa24f9 | 2010-12-10 08:42:23 +0000 | [diff] [blame] | 1047 | wbc, end_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 1050 | if (iohead) { |
| 1051 | /* |
| 1052 | * Reserve log space if we might write beyond the on-disk |
| 1053 | * inode size. |
| 1054 | */ |
| 1055 | if (ioend->io_type != IO_UNWRITTEN && |
| 1056 | xfs_ioend_is_append(ioend)) { |
| 1057 | err = xfs_setfilesize_trans_alloc(ioend); |
| 1058 | if (err) |
| 1059 | goto error; |
| 1060 | } |
| 1061 | |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 1062 | xfs_submit_ioend(wbc, iohead); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 1063 | } |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1064 | |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1065 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
| 1067 | error: |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1068 | if (iohead) |
| 1069 | xfs_cancel_ioend(iohead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
Christoph Hellwig | b5420f2 | 2010-08-24 11:47:51 +1000 | [diff] [blame] | 1071 | if (err == -EAGAIN) |
| 1072 | goto redirty; |
| 1073 | |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1074 | xfs_aops_discard_page(page); |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1075 | ClearPageUptodate(page); |
| 1076 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | return err; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1078 | |
Christoph Hellwig | b5420f2 | 2010-08-24 11:47:51 +1000 | [diff] [blame] | 1079 | redirty: |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1080 | redirty_page_for_writepage(wbc, page); |
| 1081 | unlock_page(page); |
| 1082 | return 0; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1083 | } |
| 1084 | |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 1085 | STATIC int |
| 1086 | xfs_vm_writepages( |
| 1087 | struct address_space *mapping, |
| 1088 | struct writeback_control *wbc) |
| 1089 | { |
Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 1090 | xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED); |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 1091 | return generic_writepages(mapping, wbc); |
| 1092 | } |
| 1093 | |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1094 | /* |
| 1095 | * Called to move a page into cleanable state - and from there |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1096 | * to be released. The page should already be clean. We always |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1097 | * have buffer heads in this call. |
| 1098 | * |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1099 | * Returns 1 if the page is ok to release, 0 otherwise. |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1100 | */ |
| 1101 | STATIC int |
Nathan Scott | 238f4c5 | 2006-03-17 17:26:25 +1100 | [diff] [blame] | 1102 | xfs_vm_releasepage( |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1103 | struct page *page, |
| 1104 | gfp_t gfp_mask) |
| 1105 | { |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1106 | int delalloc, unwritten; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1107 | |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1108 | trace_xfs_releasepage(page->mapping->host, page, 0); |
Nathan Scott | 238f4c5 | 2006-03-17 17:26:25 +1100 | [diff] [blame] | 1109 | |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1110 | xfs_count_page_state(page, &delalloc, &unwritten); |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1111 | |
Christoph Hellwig | 89f3b36 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1112 | if (WARN_ON(delalloc)) |
| 1113 | return 0; |
| 1114 | if (WARN_ON(unwritten)) |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1115 | return 0; |
| 1116 | |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1117 | return try_to_free_buffers(page); |
| 1118 | } |
| 1119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | STATIC int |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1121 | __xfs_get_blocks( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | struct inode *inode, |
| 1123 | sector_t iblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | struct buffer_head *bh_result, |
| 1125 | int create, |
Christoph Hellwig | f2bde9b | 2010-06-24 11:44:35 +1000 | [diff] [blame] | 1126 | int direct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | { |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1128 | struct xfs_inode *ip = XFS_I(inode); |
| 1129 | struct xfs_mount *mp = ip->i_mount; |
| 1130 | xfs_fileoff_t offset_fsb, end_fsb; |
| 1131 | int error = 0; |
| 1132 | int lockmode = 0; |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1133 | struct xfs_bmbt_irec imap; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1134 | int nimaps = 1; |
Nathan Scott | fdc7ed7 | 2005-11-02 15:13:13 +1100 | [diff] [blame] | 1135 | xfs_off_t offset; |
| 1136 | ssize_t size; |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1137 | int new = 0; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1138 | |
| 1139 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 1140 | return -XFS_ERROR(EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | |
Nathan Scott | fdc7ed7 | 2005-11-02 15:13:13 +1100 | [diff] [blame] | 1142 | offset = (xfs_off_t)iblock << inode->i_blkbits; |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1143 | ASSERT(bh_result->b_size >= (1 << inode->i_blkbits)); |
| 1144 | size = bh_result->b_size; |
Lachlan McIlroy | 364f358 | 2008-09-17 16:50:14 +1000 | [diff] [blame] | 1145 | |
| 1146 | if (!create && direct && offset >= i_size_read(inode)) |
| 1147 | return 0; |
| 1148 | |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1149 | if (create) { |
| 1150 | lockmode = XFS_ILOCK_EXCL; |
| 1151 | xfs_ilock(ip, lockmode); |
| 1152 | } else { |
| 1153 | lockmode = xfs_ilock_map_shared(ip); |
| 1154 | } |
Christoph Hellwig | f2bde9b | 2010-06-24 11:44:35 +1000 | [diff] [blame] | 1155 | |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1156 | ASSERT(offset <= mp->m_maxioffset); |
| 1157 | if (offset + size > mp->m_maxioffset) |
| 1158 | size = mp->m_maxioffset - offset; |
| 1159 | end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size); |
| 1160 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 1161 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 1162 | error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, |
| 1163 | &imap, &nimaps, XFS_BMAPI_ENTIRE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | if (error) |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1165 | goto out_unlock; |
| 1166 | |
| 1167 | if (create && |
| 1168 | (!nimaps || |
| 1169 | (imap.br_startblock == HOLESTARTBLOCK || |
| 1170 | imap.br_startblock == DELAYSTARTBLOCK))) { |
| 1171 | if (direct) { |
| 1172 | error = xfs_iomap_write_direct(ip, offset, size, |
| 1173 | &imap, nimaps); |
| 1174 | } else { |
| 1175 | error = xfs_iomap_write_delay(ip, offset, size, &imap); |
| 1176 | } |
| 1177 | if (error) |
| 1178 | goto out_unlock; |
| 1179 | |
| 1180 | trace_xfs_get_blocks_alloc(ip, offset, size, 0, &imap); |
| 1181 | } else if (nimaps) { |
| 1182 | trace_xfs_get_blocks_found(ip, offset, size, 0, &imap); |
| 1183 | } else { |
| 1184 | trace_xfs_get_blocks_notfound(ip, offset, size); |
| 1185 | goto out_unlock; |
| 1186 | } |
| 1187 | xfs_iunlock(ip, lockmode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1189 | if (imap.br_startblock != HOLESTARTBLOCK && |
| 1190 | imap.br_startblock != DELAYSTARTBLOCK) { |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 1191 | /* |
| 1192 | * For unwritten extents do not report a disk address on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | * the read case (treat as if we're reading into a hole). |
| 1194 | */ |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1195 | if (create || !ISUNWRITTEN(&imap)) |
| 1196 | xfs_map_buffer(inode, bh_result, &imap, offset); |
| 1197 | if (create && ISUNWRITTEN(&imap)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | if (direct) |
| 1199 | bh_result->b_private = inode; |
| 1200 | set_buffer_unwritten(bh_result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1204 | /* |
| 1205 | * If this is a realtime file, data may be on a different device. |
| 1206 | * to that pointed to from the buffer_head b_bdev currently. |
| 1207 | */ |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 1208 | bh_result->b_bdev = xfs_find_bdev_for_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1210 | /* |
David Chinner | 549054a | 2007-02-10 18:36:35 +1100 | [diff] [blame] | 1211 | * If we previously allocated a block out beyond eof and we are now |
| 1212 | * coming back to use it then we will need to flag it as new even if it |
| 1213 | * has a disk address. |
| 1214 | * |
| 1215 | * With sub-block writes into unwritten extents we also need to mark |
| 1216 | * the buffer as new so that the unwritten parts of the buffer gets |
| 1217 | * correctly zeroed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | */ |
| 1219 | if (create && |
| 1220 | ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) || |
David Chinner | 549054a | 2007-02-10 18:36:35 +1100 | [diff] [blame] | 1221 | (offset >= i_size_read(inode)) || |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1222 | (new || ISUNWRITTEN(&imap)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | set_buffer_new(bh_result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1225 | if (imap.br_startblock == DELAYSTARTBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | BUG_ON(direct); |
| 1227 | if (create) { |
| 1228 | set_buffer_uptodate(bh_result); |
| 1229 | set_buffer_mapped(bh_result); |
| 1230 | set_buffer_delay(bh_result); |
| 1231 | } |
| 1232 | } |
| 1233 | |
Christoph Hellwig | 2b8f12b | 2010-04-28 12:28:59 +0000 | [diff] [blame] | 1234 | /* |
| 1235 | * If this is O_DIRECT or the mpage code calling tell them how large |
| 1236 | * the mapping is, so that we can avoid repeated get_blocks calls. |
| 1237 | */ |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1238 | if (direct || size > (1 << inode->i_blkbits)) { |
Christoph Hellwig | 2b8f12b | 2010-04-28 12:28:59 +0000 | [diff] [blame] | 1239 | xfs_off_t mapping_size; |
Christoph Hellwig | 9563b3d | 2010-04-28 12:28:53 +0000 | [diff] [blame] | 1240 | |
Christoph Hellwig | 2b8f12b | 2010-04-28 12:28:59 +0000 | [diff] [blame] | 1241 | mapping_size = imap.br_startoff + imap.br_blockcount - iblock; |
| 1242 | mapping_size <<= inode->i_blkbits; |
| 1243 | |
| 1244 | ASSERT(mapping_size > 0); |
| 1245 | if (mapping_size > size) |
| 1246 | mapping_size = size; |
| 1247 | if (mapping_size > LONG_MAX) |
| 1248 | mapping_size = LONG_MAX; |
| 1249 | |
| 1250 | bh_result->b_size = mapping_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | return 0; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1254 | |
| 1255 | out_unlock: |
| 1256 | xfs_iunlock(ip, lockmode); |
| 1257 | return -error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | int |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1261 | xfs_get_blocks( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | struct inode *inode, |
| 1263 | sector_t iblock, |
| 1264 | struct buffer_head *bh_result, |
| 1265 | int create) |
| 1266 | { |
Christoph Hellwig | f2bde9b | 2010-06-24 11:44:35 +1000 | [diff] [blame] | 1267 | return __xfs_get_blocks(inode, iblock, bh_result, create, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | STATIC int |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1271 | xfs_get_blocks_direct( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | struct inode *inode, |
| 1273 | sector_t iblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | struct buffer_head *bh_result, |
| 1275 | int create) |
| 1276 | { |
Christoph Hellwig | f2bde9b | 2010-06-24 11:44:35 +1000 | [diff] [blame] | 1277 | return __xfs_get_blocks(inode, iblock, bh_result, create, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1280 | /* |
| 1281 | * Complete a direct I/O write request. |
| 1282 | * |
| 1283 | * If the private argument is non-NULL __xfs_get_blocks signals us that we |
| 1284 | * need to issue a transaction to convert the range from unwritten to written |
| 1285 | * extents. In case this is regular synchronous I/O we just call xfs_end_io |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1286 | * to do this and we are done. But in case this was a successful AIO |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1287 | * request this handler is called from interrupt context, from which we |
| 1288 | * can't start transactions. In that case offload the I/O completion to |
| 1289 | * the workqueues we also use for buffered I/O completion. |
| 1290 | */ |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1291 | STATIC void |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1292 | xfs_end_io_direct_write( |
| 1293 | struct kiocb *iocb, |
| 1294 | loff_t offset, |
| 1295 | ssize_t size, |
| 1296 | void *private, |
| 1297 | int ret, |
| 1298 | bool is_async) |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1299 | { |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1300 | struct xfs_ioend *ioend = iocb->private; |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1301 | |
| 1302 | /* |
Christoph Hellwig | 2813d68 | 2011-12-18 20:00:12 +0000 | [diff] [blame] | 1303 | * While the generic direct I/O code updates the inode size, it does |
| 1304 | * so only after the end_io handler is called, which means our |
| 1305 | * end_io handler thinks the on-disk size is outside the in-core |
| 1306 | * size. To prevent this just update it a little bit earlier here. |
| 1307 | */ |
| 1308 | if (offset + size > i_size_read(ioend->io_inode)) |
| 1309 | i_size_write(ioend->io_inode, offset + size); |
| 1310 | |
| 1311 | /* |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1312 | * blockdev_direct_IO can return an error even after the I/O |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1313 | * completion handler was called. Thus we need to protect |
| 1314 | * against double-freeing. |
| 1315 | */ |
| 1316 | iocb->private = NULL; |
Christoph Hellwig | 40e2e97 | 2010-07-18 21:17:09 +0000 | [diff] [blame] | 1317 | |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1318 | ioend->io_offset = offset; |
| 1319 | ioend->io_size = size; |
Christoph Hellwig | c859cdd | 2011-08-23 08:28:10 +0000 | [diff] [blame] | 1320 | ioend->io_iocb = iocb; |
| 1321 | ioend->io_result = ret; |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1322 | if (private && size > 0) |
| 1323 | ioend->io_type = IO_UNWRITTEN; |
| 1324 | |
| 1325 | if (is_async) { |
Christoph Hellwig | c859cdd | 2011-08-23 08:28:10 +0000 | [diff] [blame] | 1326 | ioend->io_isasync = 1; |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1327 | xfs_finish_ioend(ioend); |
| 1328 | } else { |
| 1329 | xfs_finish_ioend_sync(ioend); |
| 1330 | } |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1331 | } |
| 1332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | STATIC ssize_t |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1334 | xfs_vm_direct_IO( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | int rw, |
| 1336 | struct kiocb *iocb, |
| 1337 | const struct iovec *iov, |
| 1338 | loff_t offset, |
| 1339 | unsigned long nr_segs) |
| 1340 | { |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1341 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
| 1342 | struct block_device *bdev = xfs_find_bdev_for_inode(inode); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 1343 | struct xfs_ioend *ioend = NULL; |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1344 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1346 | if (rw & WRITE) { |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 1347 | size_t size = iov_length(iov, nr_segs); |
| 1348 | |
| 1349 | /* |
| 1350 | * We need to preallocate a transaction for a size update |
| 1351 | * here. In the case that this write both updates the size |
| 1352 | * and converts at least on unwritten extent we will cancel |
| 1353 | * the still clean transaction after the I/O has finished. |
| 1354 | */ |
| 1355 | iocb->private = ioend = xfs_alloc_ioend(inode, IO_DIRECT); |
| 1356 | if (offset + size > XFS_I(inode)->i_d.di_size) { |
| 1357 | ret = xfs_setfilesize_trans_alloc(ioend); |
| 1358 | if (ret) |
| 1359 | goto out_destroy_ioend; |
| 1360 | ioend->io_isdirect = 1; |
| 1361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
Christoph Hellwig | eafdc7d | 2010-06-04 11:29:53 +0200 | [diff] [blame] | 1363 | ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov, |
| 1364 | offset, nr_segs, |
| 1365 | xfs_get_blocks_direct, |
| 1366 | xfs_end_io_direct_write, NULL, 0); |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1367 | if (ret != -EIOCBQUEUED && iocb->private) |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 1368 | goto out_trans_cancel; |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1369 | } else { |
Christoph Hellwig | eafdc7d | 2010-06-04 11:29:53 +0200 | [diff] [blame] | 1370 | ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov, |
| 1371 | offset, nr_segs, |
| 1372 | xfs_get_blocks_direct, |
| 1373 | NULL, NULL, 0); |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1374 | } |
Christoph Hellwig | 5fe878a | 2009-12-15 16:47:50 -0800 | [diff] [blame] | 1375 | |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1376 | return ret; |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 1377 | |
| 1378 | out_trans_cancel: |
| 1379 | if (ioend->io_append_trans) { |
| 1380 | current_set_flags_nested(&ioend->io_append_trans->t_pflags, |
| 1381 | PF_FSTRANS); |
| 1382 | xfs_trans_cancel(ioend->io_append_trans, 0); |
| 1383 | } |
| 1384 | out_destroy_ioend: |
| 1385 | xfs_destroy_ioend(ioend); |
| 1386 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1389 | STATIC void |
| 1390 | xfs_vm_write_failed( |
| 1391 | struct address_space *mapping, |
| 1392 | loff_t to) |
| 1393 | { |
| 1394 | struct inode *inode = mapping->host; |
| 1395 | |
| 1396 | if (to > inode->i_size) { |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 1397 | /* |
Christoph Hellwig | 2813d68 | 2011-12-18 20:00:12 +0000 | [diff] [blame] | 1398 | * Punch out the delalloc blocks we have already allocated. |
| 1399 | * |
| 1400 | * Don't bother with xfs_setattr given that nothing can have |
| 1401 | * made it to disk yet as the page is still locked at this |
| 1402 | * point. |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 1403 | */ |
| 1404 | struct xfs_inode *ip = XFS_I(inode); |
| 1405 | xfs_fileoff_t start_fsb; |
| 1406 | xfs_fileoff_t end_fsb; |
| 1407 | int error; |
| 1408 | |
| 1409 | truncate_pagecache(inode, to, inode->i_size); |
| 1410 | |
| 1411 | /* |
| 1412 | * Check if there are any blocks that are outside of i_size |
| 1413 | * that need to be trimmed back. |
| 1414 | */ |
| 1415 | start_fsb = XFS_B_TO_FSB(ip->i_mount, inode->i_size) + 1; |
| 1416 | end_fsb = XFS_B_TO_FSB(ip->i_mount, to); |
| 1417 | if (end_fsb <= start_fsb) |
| 1418 | return; |
| 1419 | |
| 1420 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1421 | error = xfs_bmap_punch_delalloc_range(ip, start_fsb, |
| 1422 | end_fsb - start_fsb); |
| 1423 | if (error) { |
| 1424 | /* something screwed, just bail */ |
| 1425 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 1426 | xfs_alert(ip->i_mount, |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 1427 | "xfs_vm_write_failed: unable to clean up ino %lld", |
| 1428 | ip->i_ino); |
| 1429 | } |
| 1430 | } |
| 1431 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1432 | } |
| 1433 | } |
| 1434 | |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1435 | STATIC int |
Nick Piggin | d79689c | 2007-10-16 01:25:06 -0700 | [diff] [blame] | 1436 | xfs_vm_write_begin( |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1437 | struct file *file, |
Nick Piggin | d79689c | 2007-10-16 01:25:06 -0700 | [diff] [blame] | 1438 | struct address_space *mapping, |
| 1439 | loff_t pos, |
| 1440 | unsigned len, |
| 1441 | unsigned flags, |
| 1442 | struct page **pagep, |
| 1443 | void **fsdata) |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1444 | { |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 1445 | int ret; |
| 1446 | |
| 1447 | ret = block_write_begin(mapping, pos, len, flags | AOP_FLAG_NOFS, |
| 1448 | pagep, xfs_get_blocks); |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1449 | if (unlikely(ret)) |
| 1450 | xfs_vm_write_failed(mapping, pos + len); |
| 1451 | return ret; |
| 1452 | } |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 1453 | |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1454 | STATIC int |
| 1455 | xfs_vm_write_end( |
| 1456 | struct file *file, |
| 1457 | struct address_space *mapping, |
| 1458 | loff_t pos, |
| 1459 | unsigned len, |
| 1460 | unsigned copied, |
| 1461 | struct page *page, |
| 1462 | void *fsdata) |
| 1463 | { |
| 1464 | int ret; |
| 1465 | |
| 1466 | ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata); |
| 1467 | if (unlikely(ret < len)) |
| 1468 | xfs_vm_write_failed(mapping, pos + len); |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 1469 | return ret; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1470 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | |
| 1472 | STATIC sector_t |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1473 | xfs_vm_bmap( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | struct address_space *mapping, |
| 1475 | sector_t block) |
| 1476 | { |
| 1477 | struct inode *inode = (struct inode *)mapping->host; |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 1478 | struct xfs_inode *ip = XFS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | |
Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 1480 | trace_xfs_vm_bmap(XFS_I(inode)); |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 1481 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 1482 | xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF); |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 1483 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1484 | return generic_block_bmap(mapping, block, xfs_get_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | STATIC int |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1488 | xfs_vm_readpage( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | struct file *unused, |
| 1490 | struct page *page) |
| 1491 | { |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1492 | return mpage_readpage(page, xfs_get_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | STATIC int |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1496 | xfs_vm_readpages( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | struct file *unused, |
| 1498 | struct address_space *mapping, |
| 1499 | struct list_head *pages, |
| 1500 | unsigned nr_pages) |
| 1501 | { |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1502 | return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 1505 | const struct address_space_operations xfs_address_space_operations = { |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1506 | .readpage = xfs_vm_readpage, |
| 1507 | .readpages = xfs_vm_readpages, |
| 1508 | .writepage = xfs_vm_writepage, |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 1509 | .writepages = xfs_vm_writepages, |
Nathan Scott | 238f4c5 | 2006-03-17 17:26:25 +1100 | [diff] [blame] | 1510 | .releasepage = xfs_vm_releasepage, |
| 1511 | .invalidatepage = xfs_vm_invalidatepage, |
Nick Piggin | d79689c | 2007-10-16 01:25:06 -0700 | [diff] [blame] | 1512 | .write_begin = xfs_vm_write_begin, |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1513 | .write_end = xfs_vm_write_end, |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1514 | .bmap = xfs_vm_bmap, |
| 1515 | .direct_IO = xfs_vm_direct_IO, |
Christoph Lameter | e965f96 | 2006-02-01 03:05:41 -0800 | [diff] [blame] | 1516 | .migratepage = buffer_migrate_page, |
Hisashi Hifumi | bddaafa | 2009-03-29 09:53:38 +0200 | [diff] [blame] | 1517 | .is_partially_uptodate = block_is_partially_uptodate, |
Andi Kleen | aa261f5 | 2009-09-16 11:50:16 +0200 | [diff] [blame] | 1518 | .error_remove_page = generic_error_remove_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | }; |