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" |
Dave Chinner | 70a9883c | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 19 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 20 | #include "xfs_format.h" |
| 21 | #include "xfs_log_format.h" |
| 22 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_inode.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 25 | #include "xfs_trans.h" |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 26 | #include "xfs_inode_item.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 27 | #include "xfs_alloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_iomap.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 30 | #include "xfs_trace.h" |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 31 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 32 | #include "xfs_bmap_util.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 33 | #include "xfs_bmap_btree.h" |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/mpage.h> |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 36 | #include <linux/pagevec.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/writeback.h> |
| 38 | |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 39 | /* |
| 40 | * structure owned by writepages passed to individual writepage calls |
| 41 | */ |
| 42 | struct xfs_writepage_ctx { |
| 43 | struct xfs_bmbt_irec imap; |
| 44 | bool imap_valid; |
| 45 | unsigned int io_type; |
| 46 | struct xfs_ioend *iohead; |
| 47 | struct xfs_ioend *ioend; |
| 48 | sector_t last_block; |
| 49 | }; |
| 50 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 51 | void |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 52 | xfs_count_page_state( |
| 53 | struct page *page, |
| 54 | int *delalloc, |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 55 | int *unwritten) |
| 56 | { |
| 57 | struct buffer_head *bh, *head; |
| 58 | |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 59 | *delalloc = *unwritten = 0; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 60 | |
| 61 | bh = head = page_buffers(page); |
| 62 | do { |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 63 | if (buffer_unwritten(bh)) |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 64 | (*unwritten) = 1; |
| 65 | else if (buffer_delay(bh)) |
| 66 | (*delalloc) = 1; |
| 67 | } while ((bh = bh->b_this_page) != head); |
| 68 | } |
| 69 | |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 70 | STATIC struct block_device * |
| 71 | xfs_find_bdev_for_inode( |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 72 | struct inode *inode) |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 73 | { |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 74 | struct xfs_inode *ip = XFS_I(inode); |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 75 | struct xfs_mount *mp = ip->i_mount; |
| 76 | |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 77 | if (XFS_IS_REALTIME_INODE(ip)) |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 78 | return mp->m_rtdev_targp->bt_bdev; |
| 79 | else |
| 80 | return mp->m_ddev_targp->bt_bdev; |
| 81 | } |
| 82 | |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 83 | /* |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 84 | * We're now finished for good with this ioend structure. |
| 85 | * Update the page state via the associated buffer_heads, |
| 86 | * release holds on the inode and bio, and finally free |
| 87 | * up memory. Do not use the ioend after this. |
| 88 | */ |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 89 | STATIC void |
| 90 | xfs_destroy_ioend( |
| 91 | xfs_ioend_t *ioend) |
| 92 | { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 93 | struct buffer_head *bh, *next; |
| 94 | |
| 95 | for (bh = ioend->io_buffer_head; bh; bh = next) { |
| 96 | next = bh->b_private; |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 97 | bh->b_end_io(bh, !ioend->io_error); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 98 | } |
Christoph Hellwig | 583fa58 | 2008-12-03 12:20:38 +0100 | [diff] [blame] | 99 | |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 100 | mempool_free(ioend, xfs_ioend_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
Christoph Hellwig | fc0063c | 2011-08-23 08:28:11 +0000 | [diff] [blame] | 104 | * Fast and loose check if this write could update the on-disk inode size. |
| 105 | */ |
| 106 | static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend) |
| 107 | { |
| 108 | return ioend->io_offset + ioend->io_size > |
| 109 | XFS_I(ioend->io_inode)->i_d.di_size; |
| 110 | } |
| 111 | |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 112 | STATIC int |
| 113 | xfs_setfilesize_trans_alloc( |
| 114 | struct xfs_ioend *ioend) |
| 115 | { |
| 116 | struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount; |
| 117 | struct xfs_trans *tp; |
| 118 | int error; |
| 119 | |
| 120 | tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS); |
| 121 | |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 122 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_fsyncts, 0, 0); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 123 | if (error) { |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 124 | xfs_trans_cancel(tp); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 125 | return error; |
| 126 | } |
| 127 | |
| 128 | ioend->io_append_trans = tp; |
| 129 | |
| 130 | /* |
Dave Chinner | 437a255 | 2012-11-28 13:01:00 +1100 | [diff] [blame] | 131 | * We may pass freeze protection with a transaction. So tell lockdep |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 132 | * we released it. |
| 133 | */ |
Oleg Nesterov | bee9182 | 2015-07-19 23:48:20 +0200 | [diff] [blame] | 134 | __sb_writers_release(ioend->io_inode->i_sb, SB_FREEZE_FS); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 135 | /* |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 136 | * We hand off the transaction to the completion thread now, so |
| 137 | * clear the flag here. |
| 138 | */ |
| 139 | current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS); |
| 140 | return 0; |
| 141 | } |
| 142 | |
Christoph Hellwig | fc0063c | 2011-08-23 08:28:11 +0000 | [diff] [blame] | 143 | /* |
Christoph Hellwig | 2813d68 | 2011-12-18 20:00:12 +0000 | [diff] [blame] | 144 | * 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] | 145 | */ |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 146 | STATIC int |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 147 | xfs_setfilesize( |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 148 | struct xfs_inode *ip, |
| 149 | struct xfs_trans *tp, |
| 150 | xfs_off_t offset, |
| 151 | size_t size) |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 152 | { |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 153 | xfs_fsize_t isize; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 154 | |
Christoph Hellwig | aa6bf01 | 2012-02-29 09:53:48 +0000 | [diff] [blame] | 155 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 156 | isize = xfs_new_eof(ip, offset + size); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 157 | if (!isize) { |
| 158 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 159 | xfs_trans_cancel(tp); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 160 | return 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 161 | } |
| 162 | |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 163 | trace_xfs_setfilesize(ip, offset, size); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 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 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 169 | return xfs_trans_commit(tp); |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 170 | } |
| 171 | |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 172 | STATIC int |
| 173 | xfs_setfilesize_ioend( |
| 174 | struct xfs_ioend *ioend) |
| 175 | { |
| 176 | struct xfs_inode *ip = XFS_I(ioend->io_inode); |
| 177 | struct xfs_trans *tp = ioend->io_append_trans; |
| 178 | |
| 179 | /* |
| 180 | * The transaction may have been allocated in the I/O submission thread, |
| 181 | * thus we need to mark ourselves as being in a transaction manually. |
| 182 | * Similarly for freeze protection. |
| 183 | */ |
| 184 | current_set_flags_nested(&tp->t_pflags, PF_FSTRANS); |
Oleg Nesterov | bee9182 | 2015-07-19 23:48:20 +0200 | [diff] [blame] | 185 | __sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS); |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 186 | |
Zhaohongjiang | 5cb13dc | 2015-10-12 15:28:39 +1100 | [diff] [blame] | 187 | /* we abort the update if there was an IO error */ |
| 188 | if (ioend->io_error) { |
| 189 | xfs_trans_cancel(tp); |
| 190 | return ioend->io_error; |
| 191 | } |
| 192 | |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 193 | return xfs_setfilesize(ip, tp, ioend->io_offset, ioend->io_size); |
| 194 | } |
| 195 | |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 196 | /* |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 197 | * Schedule IO completion handling on the final put of an ioend. |
Christoph Hellwig | fc0063c | 2011-08-23 08:28:11 +0000 | [diff] [blame] | 198 | * |
| 199 | * If there is no work to do we might as well call it a day and free the |
| 200 | * ioend right now. |
Dave Chinner | c626d17 | 2009-04-06 18:42:11 +0200 | [diff] [blame] | 201 | */ |
| 202 | STATIC void |
| 203 | xfs_finish_ioend( |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 204 | struct xfs_ioend *ioend) |
Dave Chinner | c626d17 | 2009-04-06 18:42:11 +0200 | [diff] [blame] | 205 | { |
| 206 | if (atomic_dec_and_test(&ioend->io_remaining)) { |
Christoph Hellwig | aa6bf01 | 2012-02-29 09:53:48 +0000 | [diff] [blame] | 207 | struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount; |
| 208 | |
Alain Renaud | 0d882a3 | 2012-05-22 15:56:21 -0500 | [diff] [blame] | 209 | if (ioend->io_type == XFS_IO_UNWRITTEN) |
Christoph Hellwig | aa6bf01 | 2012-02-29 09:53:48 +0000 | [diff] [blame] | 210 | queue_work(mp->m_unwritten_workqueue, &ioend->io_work); |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 211 | else if (ioend->io_append_trans) |
Christoph Hellwig | aa6bf01 | 2012-02-29 09:53:48 +0000 | [diff] [blame] | 212 | queue_work(mp->m_data_workqueue, &ioend->io_work); |
Christoph Hellwig | fc0063c | 2011-08-23 08:28:11 +0000 | [diff] [blame] | 213 | else |
| 214 | xfs_destroy_ioend(ioend); |
Dave Chinner | c626d17 | 2009-04-06 18:42:11 +0200 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | /* |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 219 | * IO write completion. |
| 220 | */ |
| 221 | STATIC void |
| 222 | xfs_end_io( |
| 223 | struct work_struct *work) |
| 224 | { |
| 225 | xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work); |
| 226 | struct xfs_inode *ip = XFS_I(ioend->io_inode); |
Dave Chinner | 6941893 | 2010-03-04 00:57:09 +0000 | [diff] [blame] | 227 | int error = 0; |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 228 | |
Christoph Hellwig | 04f658e | 2011-08-24 05:59:25 +0000 | [diff] [blame] | 229 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
Christoph Hellwig | 810627d | 2011-11-08 08:56:15 +0000 | [diff] [blame] | 230 | ioend->io_error = -EIO; |
Christoph Hellwig | 04f658e | 2011-08-24 05:59:25 +0000 | [diff] [blame] | 231 | goto done; |
| 232 | } |
Christoph Hellwig | 04f658e | 2011-08-24 05:59:25 +0000 | [diff] [blame] | 233 | |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 234 | /* |
| 235 | * For unwritten extents we need to issue transactions to convert a |
| 236 | * range to normal written extens after the data I/O has finished. |
Zhaohongjiang | 5cb13dc | 2015-10-12 15:28:39 +1100 | [diff] [blame] | 237 | * Detecting and handling completion IO errors is done individually |
| 238 | * for each case as different cleanup operations need to be performed |
| 239 | * on error. |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 240 | */ |
Alain Renaud | 0d882a3 | 2012-05-22 15:56:21 -0500 | [diff] [blame] | 241 | if (ioend->io_type == XFS_IO_UNWRITTEN) { |
Zhaohongjiang | 5cb13dc | 2015-10-12 15:28:39 +1100 | [diff] [blame] | 242 | if (ioend->io_error) |
| 243 | goto done; |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 244 | error = xfs_iomap_write_unwritten(ip, ioend->io_offset, |
Dave Chinner | 437a255 | 2012-11-28 13:01:00 +1100 | [diff] [blame] | 245 | ioend->io_size); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 246 | } else if (ioend->io_append_trans) { |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 247 | error = xfs_setfilesize_ioend(ioend); |
Christoph Hellwig | 84803fb | 2012-02-29 09:53:50 +0000 | [diff] [blame] | 248 | } else { |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 249 | ASSERT(!xfs_ioend_is_append(ioend)); |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Christoph Hellwig | 04f658e | 2011-08-24 05:59:25 +0000 | [diff] [blame] | 252 | done: |
Dave Chinner | 437a255 | 2012-11-28 13:01:00 +1100 | [diff] [blame] | 253 | if (error) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 254 | ioend->io_error = error; |
Christoph Hellwig | aa6bf01 | 2012-02-29 09:53:48 +0000 | [diff] [blame] | 255 | xfs_destroy_ioend(ioend); |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 259 | * Allocate and initialise an IO completion structure. |
| 260 | * We need to track unwritten extent write completion here initially. |
| 261 | * We'll need to extend this for updating the ondisk inode size later |
| 262 | * (vs. incore size). |
| 263 | */ |
| 264 | STATIC xfs_ioend_t * |
| 265 | xfs_alloc_ioend( |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 266 | struct inode *inode, |
| 267 | unsigned int type) |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 268 | { |
| 269 | xfs_ioend_t *ioend; |
| 270 | |
| 271 | ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS); |
| 272 | |
| 273 | /* |
| 274 | * Set the count to 1 initially, which will prevent an I/O |
| 275 | * completion callback from happening before we have started |
| 276 | * all the I/O from calling the completion routine too early. |
| 277 | */ |
| 278 | atomic_set(&ioend->io_remaining, 1); |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 279 | ioend->io_error = 0; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 280 | ioend->io_list = NULL; |
| 281 | ioend->io_type = type; |
Christoph Hellwig | b677c21 | 2007-08-29 11:46:28 +1000 | [diff] [blame] | 282 | ioend->io_inode = inode; |
Christoph Hellwig | c1a073b | 2005-09-05 08:23:35 +1000 | [diff] [blame] | 283 | ioend->io_buffer_head = NULL; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 284 | ioend->io_buffer_tail = NULL; |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 285 | ioend->io_offset = 0; |
| 286 | ioend->io_size = 0; |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 287 | ioend->io_append_trans = NULL; |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 288 | |
Christoph Hellwig | 5ec4fab | 2009-10-30 09:11:47 +0000 | [diff] [blame] | 289 | INIT_WORK(&ioend->io_work, xfs_end_io); |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 290 | return ioend; |
| 291 | } |
| 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | STATIC int |
| 294 | xfs_map_blocks( |
| 295 | struct inode *inode, |
| 296 | loff_t offset, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 297 | struct xfs_bmbt_irec *imap, |
Dave Chinner | 988ef92 | 2016-02-15 17:20:50 +1100 | [diff] [blame] | 298 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 300 | struct xfs_inode *ip = XFS_I(inode); |
| 301 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | ed1e7b7 | 2010-12-10 08:42:22 +0000 | [diff] [blame] | 302 | ssize_t count = 1 << inode->i_blkbits; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 303 | xfs_fileoff_t offset_fsb, end_fsb; |
| 304 | int error = 0; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 305 | int bmapi_flags = XFS_BMAPI_ENTIRE; |
| 306 | int nimaps = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 308 | if (XFS_FORCED_SHUTDOWN(mp)) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 309 | return -EIO; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 310 | |
Alain Renaud | 0d882a3 | 2012-05-22 15:56:21 -0500 | [diff] [blame] | 311 | if (type == XFS_IO_UNWRITTEN) |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 312 | bmapi_flags |= XFS_BMAPI_IGSTATE; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 313 | |
Dave Chinner | 988ef92 | 2016-02-15 17:20:50 +1100 | [diff] [blame] | 314 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 315 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
| 316 | (ip->i_df.if_flags & XFS_IFEXTENTS)); |
Dave Chinner | d2c2819 | 2012-06-08 15:44:53 +1000 | [diff] [blame] | 317 | ASSERT(offset <= mp->m_super->s_maxbytes); |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 318 | |
Dave Chinner | d2c2819 | 2012-06-08 15:44:53 +1000 | [diff] [blame] | 319 | if (offset + count > mp->m_super->s_maxbytes) |
| 320 | count = mp->m_super->s_maxbytes - offset; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 321 | end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count); |
| 322 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 323 | error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, |
| 324 | imap, &nimaps, bmapi_flags); |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 325 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 326 | |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 327 | if (error) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 328 | return error; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 329 | |
Alain Renaud | 0d882a3 | 2012-05-22 15:56:21 -0500 | [diff] [blame] | 330 | if (type == XFS_IO_DELALLOC && |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 331 | (!nimaps || isnullstartblock(imap->br_startblock))) { |
Jie Liu | 0799a3e | 2013-09-29 18:56:04 +0800 | [diff] [blame] | 332 | error = xfs_iomap_write_allocate(ip, offset, imap); |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 333 | if (!error) |
| 334 | trace_xfs_map_blocks_alloc(ip, offset, count, type, imap); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 335 | return error; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 338 | #ifdef DEBUG |
Alain Renaud | 0d882a3 | 2012-05-22 15:56:21 -0500 | [diff] [blame] | 339 | if (type == XFS_IO_UNWRITTEN) { |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 340 | ASSERT(nimaps); |
| 341 | ASSERT(imap->br_startblock != HOLESTARTBLOCK); |
| 342 | ASSERT(imap->br_startblock != DELAYSTARTBLOCK); |
| 343 | } |
| 344 | #endif |
| 345 | if (nimaps) |
| 346 | trace_xfs_map_blocks_found(ip, offset, count, type, imap); |
| 347 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 350 | STATIC bool |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 351 | xfs_imap_valid( |
Christoph Hellwig | 8699bb0 | 2010-04-28 12:28:54 +0000 | [diff] [blame] | 352 | struct inode *inode, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 353 | struct xfs_bmbt_irec *imap, |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 354 | xfs_off_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | { |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 356 | offset >>= inode->i_blkbits; |
Christoph Hellwig | 8699bb0 | 2010-04-28 12:28:54 +0000 | [diff] [blame] | 357 | |
Christoph Hellwig | 558e689 | 2010-04-28 12:28:58 +0000 | [diff] [blame] | 358 | return offset >= imap->br_startoff && |
| 359 | offset < imap->br_startoff + imap->br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 362 | /* |
| 363 | * BIO completion handler for buffered IO. |
| 364 | */ |
Al Viro | 782e3b3 | 2007-10-12 07:17:47 +0100 | [diff] [blame] | 365 | STATIC void |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 366 | xfs_end_bio( |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 367 | struct bio *bio) |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 368 | { |
| 369 | xfs_ioend_t *ioend = bio->bi_private; |
| 370 | |
Linus Torvalds | 77a7880 | 2015-09-07 13:28:32 -0700 | [diff] [blame] | 371 | if (!ioend->io_error) |
| 372 | ioend->io_error = bio->bi_error; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 373 | |
| 374 | /* Toss bio and pass work off to an xfsdatad thread */ |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 375 | bio->bi_private = NULL; |
| 376 | bio->bi_end_io = NULL; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 377 | bio_put(bio); |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 378 | |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 379 | xfs_finish_ioend(ioend); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | STATIC void |
| 383 | xfs_submit_ioend_bio( |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 384 | struct writeback_control *wbc, |
| 385 | xfs_ioend_t *ioend, |
| 386 | struct bio *bio) |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 387 | { |
| 388 | atomic_inc(&ioend->io_remaining); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 389 | bio->bi_private = ioend; |
| 390 | bio->bi_end_io = xfs_end_bio; |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 391 | submit_bio(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE, bio); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | STATIC struct bio * |
| 395 | xfs_alloc_ioend_bio( |
| 396 | struct buffer_head *bh) |
| 397 | { |
Kent Overstreet | b54ffb7 | 2015-05-19 14:31:01 +0200 | [diff] [blame] | 398 | struct bio *bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 399 | |
| 400 | ASSERT(bio->bi_private == NULL); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 401 | bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 402 | bio->bi_bdev = bh->b_bdev; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 403 | return bio; |
| 404 | } |
| 405 | |
| 406 | STATIC void |
| 407 | xfs_start_buffer_writeback( |
| 408 | struct buffer_head *bh) |
| 409 | { |
| 410 | ASSERT(buffer_mapped(bh)); |
| 411 | ASSERT(buffer_locked(bh)); |
| 412 | ASSERT(!buffer_delay(bh)); |
| 413 | ASSERT(!buffer_unwritten(bh)); |
| 414 | |
| 415 | mark_buffer_async_write(bh); |
| 416 | set_buffer_uptodate(bh); |
| 417 | clear_buffer_dirty(bh); |
| 418 | } |
| 419 | |
| 420 | STATIC void |
| 421 | xfs_start_page_writeback( |
| 422 | struct page *page, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 423 | int clear_dirty, |
| 424 | int buffers) |
| 425 | { |
| 426 | ASSERT(PageLocked(page)); |
| 427 | ASSERT(!PageWriteback(page)); |
Dave Chinner | 0d085a5 | 2014-09-23 15:36:27 +1000 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * if the page was not fully cleaned, we need to ensure that the higher |
| 431 | * layers come back to it correctly. That means we need to keep the page |
| 432 | * dirty, and for WB_SYNC_ALL writeback we need to ensure the |
| 433 | * PAGECACHE_TAG_TOWRITE index mark is not removed so another attempt to |
| 434 | * write this page in this writeback sweep will be made. |
| 435 | */ |
| 436 | if (clear_dirty) { |
David Chinner | 9213202 | 2006-12-21 10:24:01 +1100 | [diff] [blame] | 437 | clear_page_dirty_for_io(page); |
Dave Chinner | 0d085a5 | 2014-09-23 15:36:27 +1000 | [diff] [blame] | 438 | set_page_writeback(page); |
| 439 | } else |
| 440 | set_page_writeback_keepwrite(page); |
| 441 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 442 | unlock_page(page); |
Dave Chinner | 0d085a5 | 2014-09-23 15:36:27 +1000 | [diff] [blame] | 443 | |
Fengguang Wu | 1f7decf | 2007-10-16 23:30:42 -0700 | [diff] [blame] | 444 | /* If no buffers on the page are to be written, finish it here */ |
| 445 | if (!buffers) |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 446 | end_page_writeback(page); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 447 | } |
| 448 | |
Zhi Yong Wu | c7c1a7d | 2013-08-07 10:11:09 +0000 | [diff] [blame] | 449 | static inline int xfs_bio_add_buffer(struct bio *bio, struct buffer_head *bh) |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 450 | { |
| 451 | return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh)); |
| 452 | } |
| 453 | |
| 454 | /* |
David Chinner | d88992f | 2006-01-18 13:38:12 +1100 | [diff] [blame] | 455 | * Submit all of the bios for all of the ioends we have saved up, covering the |
| 456 | * initial writepage page and also any probed pages. |
| 457 | * |
| 458 | * Because we may have multiple ioends spanning a page, we need to start |
| 459 | * writeback on all the buffers before we submit them for I/O. If we mark the |
| 460 | * buffers as we got, then we can end up with a page that only has buffers |
| 461 | * marked async write and I/O complete on can occur before we mark the other |
| 462 | * buffers async write. |
| 463 | * |
| 464 | * The end result of this is that we trip a bug in end_page_writeback() because |
| 465 | * we call it twice for the one page as the code in end_buffer_async_write() |
| 466 | * assumes that all buffers on the page are started at the same time. |
| 467 | * |
| 468 | * 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] | 469 | * buffer_heads, and then submit them for I/O on the second pass. |
Dave Chinner | 7bf7f35 | 2012-11-12 22:09:45 +1100 | [diff] [blame] | 470 | * |
| 471 | * If @fail is non-zero, it means that we have a situation where some part of |
| 472 | * the submission process has failed after we have marked paged for writeback |
| 473 | * and unlocked them. In this situation, we need to fail the ioend chain rather |
| 474 | * than submit it to IO. This typically only happens on a filesystem shutdown. |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 475 | */ |
| 476 | STATIC void |
| 477 | xfs_submit_ioend( |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 478 | struct writeback_control *wbc, |
Dave Chinner | 7bf7f35 | 2012-11-12 22:09:45 +1100 | [diff] [blame] | 479 | xfs_ioend_t *ioend, |
| 480 | int fail) |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 481 | { |
David Chinner | d88992f | 2006-01-18 13:38:12 +1100 | [diff] [blame] | 482 | xfs_ioend_t *head = ioend; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 483 | xfs_ioend_t *next; |
| 484 | struct buffer_head *bh; |
| 485 | struct bio *bio; |
| 486 | sector_t lastblock = 0; |
| 487 | |
David Chinner | d88992f | 2006-01-18 13:38:12 +1100 | [diff] [blame] | 488 | /* Pass 1 - start writeback */ |
| 489 | do { |
| 490 | next = ioend->io_list; |
Christoph Hellwig | 221cb25 | 2010-12-10 08:42:17 +0000 | [diff] [blame] | 491 | for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) |
David Chinner | d88992f | 2006-01-18 13:38:12 +1100 | [diff] [blame] | 492 | xfs_start_buffer_writeback(bh); |
David Chinner | d88992f | 2006-01-18 13:38:12 +1100 | [diff] [blame] | 493 | } while ((ioend = next) != NULL); |
| 494 | |
| 495 | /* Pass 2 - submit I/O */ |
| 496 | ioend = head; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 497 | do { |
| 498 | next = ioend->io_list; |
| 499 | bio = NULL; |
| 500 | |
Dave Chinner | 7bf7f35 | 2012-11-12 22:09:45 +1100 | [diff] [blame] | 501 | /* |
| 502 | * If we are failing the IO now, just mark the ioend with an |
| 503 | * error and finish it. This will run IO completion immediately |
| 504 | * as there is only one reference to the ioend at this point in |
| 505 | * time. |
| 506 | */ |
| 507 | if (fail) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 508 | ioend->io_error = fail; |
Dave Chinner | 7bf7f35 | 2012-11-12 22:09:45 +1100 | [diff] [blame] | 509 | xfs_finish_ioend(ioend); |
| 510 | continue; |
| 511 | } |
| 512 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 513 | for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 514 | |
| 515 | if (!bio) { |
| 516 | retry: |
| 517 | bio = xfs_alloc_ioend_bio(bh); |
| 518 | } else if (bh->b_blocknr != lastblock + 1) { |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 519 | xfs_submit_ioend_bio(wbc, ioend, bio); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 520 | goto retry; |
| 521 | } |
| 522 | |
Zhi Yong Wu | c7c1a7d | 2013-08-07 10:11:09 +0000 | [diff] [blame] | 523 | if (xfs_bio_add_buffer(bio, bh) != bh->b_size) { |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 524 | xfs_submit_ioend_bio(wbc, ioend, bio); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 525 | goto retry; |
| 526 | } |
| 527 | |
| 528 | lastblock = bh->b_blocknr; |
| 529 | } |
| 530 | if (bio) |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 531 | xfs_submit_ioend_bio(wbc, ioend, bio); |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 532 | xfs_finish_ioend(ioend); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 533 | } while ((ioend = next) != NULL); |
| 534 | } |
| 535 | |
| 536 | /* |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 537 | * Test to see if we've been building up a completion structure for |
| 538 | * earlier buffers -- if so, we try to append to this ioend if we |
| 539 | * can, otherwise we finish off any current ioend and start another. |
| 540 | * Return true if we've finished the given ioend. |
| 541 | */ |
| 542 | STATIC void |
| 543 | xfs_add_to_ioend( |
| 544 | struct inode *inode, |
| 545 | struct buffer_head *bh, |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 546 | xfs_off_t offset, |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 547 | struct xfs_writepage_ctx *wpc) |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 548 | { |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 549 | if (!wpc->ioend || wpc->io_type != wpc->ioend->io_type || |
| 550 | bh->b_blocknr != wpc->last_block + 1) { |
| 551 | struct xfs_ioend *new; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 552 | |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 553 | new = xfs_alloc_ioend(inode, wpc->io_type); |
| 554 | new->io_offset = offset; |
| 555 | new->io_buffer_head = bh; |
| 556 | new->io_buffer_tail = bh; |
| 557 | if (wpc->ioend) |
| 558 | wpc->ioend->io_list = new; |
| 559 | wpc->ioend = new; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 560 | } else { |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 561 | wpc->ioend->io_buffer_tail->b_private = bh; |
| 562 | wpc->ioend->io_buffer_tail = bh; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | bh->b_private = NULL; |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 566 | wpc->ioend->io_size += bh->b_size; |
| 567 | wpc->last_block = bh->b_blocknr; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 568 | } |
| 569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | STATIC void |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 571 | xfs_map_buffer( |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 572 | struct inode *inode, |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 573 | struct buffer_head *bh, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 574 | struct xfs_bmbt_irec *imap, |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 575 | xfs_off_t offset) |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 576 | { |
| 577 | sector_t bn; |
Christoph Hellwig | 8699bb0 | 2010-04-28 12:28:54 +0000 | [diff] [blame] | 578 | struct xfs_mount *m = XFS_I(inode)->i_mount; |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 579 | xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff); |
| 580 | 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] | 581 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 582 | ASSERT(imap->br_startblock != HOLESTARTBLOCK); |
| 583 | ASSERT(imap->br_startblock != DELAYSTARTBLOCK); |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 584 | |
Christoph Hellwig | e513182 | 2010-04-28 12:28:55 +0000 | [diff] [blame] | 585 | bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) + |
Christoph Hellwig | 8699bb0 | 2010-04-28 12:28:54 +0000 | [diff] [blame] | 586 | ((offset - iomap_offset) >> inode->i_blkbits); |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 587 | |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 588 | ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode))); |
Nathan Scott | 87cbc49 | 2006-03-14 13:26:43 +1100 | [diff] [blame] | 589 | |
| 590 | bh->b_blocknr = bn; |
| 591 | set_buffer_mapped(bh); |
| 592 | } |
| 593 | |
| 594 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | xfs_map_at_offset( |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 596 | struct inode *inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | struct buffer_head *bh, |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 598 | struct xfs_bmbt_irec *imap, |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 599 | xfs_off_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | { |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 601 | ASSERT(imap->br_startblock != HOLESTARTBLOCK); |
| 602 | ASSERT(imap->br_startblock != DELAYSTARTBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 604 | xfs_map_buffer(inode, bh, imap, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | set_buffer_mapped(bh); |
| 606 | clear_buffer_delay(bh); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 607 | clear_buffer_unwritten(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /* |
Dave Chinner | a49935f | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 611 | * Test if a given page contains at least one buffer of a given @type. |
| 612 | * If @check_all_buffers is true, then we walk all the buffers in the page to |
| 613 | * try to find one of the type passed in. If it is not set, then the caller only |
| 614 | * needs to check the first buffer on the page for a match. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | */ |
Dave Chinner | a49935f | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 616 | STATIC bool |
Dave Chinner | 6ffc4db | 2012-04-23 15:58:43 +1000 | [diff] [blame] | 617 | xfs_check_page_type( |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 618 | struct page *page, |
Dave Chinner | a49935f | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 619 | unsigned int type, |
| 620 | bool check_all_buffers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | { |
Dave Chinner | a49935f | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 622 | struct buffer_head *bh; |
| 623 | struct buffer_head *head; |
| 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | if (PageWriteback(page)) |
Dave Chinner | a49935f | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 626 | return false; |
| 627 | if (!page->mapping) |
| 628 | return false; |
| 629 | if (!page_has_buffers(page)) |
| 630 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
Dave Chinner | a49935f | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 632 | bh = head = page_buffers(page); |
| 633 | do { |
| 634 | if (buffer_unwritten(bh)) { |
| 635 | if (type == XFS_IO_UNWRITTEN) |
| 636 | return true; |
| 637 | } else if (buffer_delay(bh)) { |
Dan Carpenter | 805eeb8 | 2014-04-04 06:56:30 +1100 | [diff] [blame] | 638 | if (type == XFS_IO_DELALLOC) |
Dave Chinner | a49935f | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 639 | return true; |
| 640 | } else if (buffer_dirty(bh) && buffer_mapped(bh)) { |
Dan Carpenter | 805eeb8 | 2014-04-04 06:56:30 +1100 | [diff] [blame] | 641 | if (type == XFS_IO_OVERWRITE) |
Dave Chinner | a49935f | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 642 | return true; |
| 643 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
Dave Chinner | a49935f | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 645 | /* If we are only checking the first buffer, we are done now. */ |
| 646 | if (!check_all_buffers) |
| 647 | break; |
| 648 | } while ((bh = bh->b_this_page) != head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
Dave Chinner | a49935f | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 650 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 653 | STATIC void |
| 654 | xfs_vm_invalidatepage( |
| 655 | struct page *page, |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 656 | unsigned int offset, |
| 657 | unsigned int length) |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 658 | { |
Lukas Czerner | 34097df | 2013-05-21 23:58:01 -0400 | [diff] [blame] | 659 | trace_xfs_invalidatepage(page->mapping->host, page, offset, |
| 660 | length); |
| 661 | block_invalidatepage(page, offset, length); |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* |
| 665 | * If the page has delalloc buffers on it, we need to punch them out before we |
| 666 | * invalidate the page. If we don't, we leave a stale delalloc mapping on the |
| 667 | * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read |
| 668 | * is done on that same region - the delalloc extent is returned when none is |
| 669 | * supposed to be there. |
| 670 | * |
| 671 | * We prevent this by truncating away the delalloc regions on the page before |
| 672 | * invalidating it. Because they are delalloc, we can do this without needing a |
| 673 | * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this |
| 674 | * truncation without a transaction as there is no space left for block |
| 675 | * reservation (typically why we see a ENOSPC in writeback). |
| 676 | * |
| 677 | * This is not a performance critical path, so for now just do the punching a |
| 678 | * buffer head at a time. |
| 679 | */ |
| 680 | STATIC void |
| 681 | xfs_aops_discard_page( |
| 682 | struct page *page) |
| 683 | { |
| 684 | struct inode *inode = page->mapping->host; |
| 685 | struct xfs_inode *ip = XFS_I(inode); |
| 686 | struct buffer_head *bh, *head; |
| 687 | loff_t offset = page_offset(page); |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 688 | |
Dave Chinner | a49935f | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 689 | if (!xfs_check_page_type(page, XFS_IO_DELALLOC, true)) |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 690 | goto out_invalidate; |
| 691 | |
Dave Chinner | e8c3753 | 2010-03-15 02:36:35 +0000 | [diff] [blame] | 692 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 693 | goto out_invalidate; |
| 694 | |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 695 | xfs_alert(ip->i_mount, |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 696 | "page discard on page %p, inode 0x%llx, offset %llu.", |
| 697 | page, ip->i_ino, offset); |
| 698 | |
| 699 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 700 | bh = head = page_buffers(page); |
| 701 | do { |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 702 | int error; |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 703 | xfs_fileoff_t start_fsb; |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 704 | |
| 705 | if (!buffer_delay(bh)) |
| 706 | goto next_buffer; |
| 707 | |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 708 | start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset); |
| 709 | error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1); |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 710 | if (error) { |
| 711 | /* something screwed, just bail */ |
Dave Chinner | e8c3753 | 2010-03-15 02:36:35 +0000 | [diff] [blame] | 712 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 713 | xfs_alert(ip->i_mount, |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 714 | "page discard unable to remove delalloc mapping."); |
Dave Chinner | e8c3753 | 2010-03-15 02:36:35 +0000 | [diff] [blame] | 715 | } |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 716 | break; |
| 717 | } |
| 718 | next_buffer: |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 719 | offset += 1 << inode->i_blkbits; |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 720 | |
| 721 | } while ((bh = bh->b_this_page) != head); |
| 722 | |
| 723 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 724 | out_invalidate: |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 725 | xfs_vm_invalidatepage(page, 0, PAGE_CACHE_SIZE); |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 726 | return; |
| 727 | } |
| 728 | |
Dave Chinner | 150d5be | 2016-02-15 17:21:12 +1100 | [diff] [blame] | 729 | static int |
| 730 | xfs_writepage_submit( |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 731 | struct xfs_writepage_ctx *wpc, |
Dave Chinner | 150d5be | 2016-02-15 17:21:12 +1100 | [diff] [blame] | 732 | struct writeback_control *wbc, |
| 733 | int status) |
| 734 | { |
| 735 | struct blk_plug plug; |
| 736 | |
| 737 | /* Reserve log space if we might write beyond the on-disk inode size. */ |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 738 | if (!status && wpc->ioend && wpc->ioend->io_type != XFS_IO_UNWRITTEN && |
| 739 | xfs_ioend_is_append(wpc->ioend)) |
| 740 | status = xfs_setfilesize_trans_alloc(wpc->ioend); |
Dave Chinner | 150d5be | 2016-02-15 17:21:12 +1100 | [diff] [blame] | 741 | |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 742 | if (wpc->iohead) { |
Dave Chinner | 150d5be | 2016-02-15 17:21:12 +1100 | [diff] [blame] | 743 | blk_start_plug(&plug); |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 744 | xfs_submit_ioend(wbc, wpc->iohead, status); |
Dave Chinner | 150d5be | 2016-02-15 17:21:12 +1100 | [diff] [blame] | 745 | blk_finish_plug(&plug); |
| 746 | } |
| 747 | return status; |
| 748 | } |
| 749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | /* |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 751 | * Write out a dirty page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | * |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 753 | * For delalloc space on the page we need to allocate space and flush it. |
| 754 | * For unwritten space on the page we need to start the conversion to |
| 755 | * regular allocated space. |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 756 | * 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] | 757 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | STATIC int |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 759 | xfs_do_writepage( |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 760 | struct page *page, |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 761 | struct writeback_control *wbc, |
| 762 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | { |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 764 | struct xfs_writepage_ctx *wpc = data; |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 765 | struct inode *inode = page->mapping->host; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 766 | struct buffer_head *bh, *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | loff_t offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | __uint64_t end_offset; |
Dave Chinner | ad68972 | 2016-02-15 17:21:31 +1100 | [diff] [blame^] | 769 | pgoff_t end_index; |
Christoph Hellwig | ed1e7b7 | 2010-12-10 08:42:22 +0000 | [diff] [blame] | 770 | ssize_t len; |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 771 | int err, uptodate = 1; |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 772 | int count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
Lukas Czerner | 34097df | 2013-05-21 23:58:01 -0400 | [diff] [blame] | 774 | trace_xfs_writepage(inode, page, 0, 0); |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 775 | |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 776 | ASSERT(page_has_buffers(page)); |
| 777 | |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 778 | /* |
| 779 | * Refuse to write the page out if we are called from reclaim context. |
| 780 | * |
Christoph Hellwig | d4f7a5c | 2010-06-28 10:34:44 -0400 | [diff] [blame] | 781 | * This avoids stack overflows when called from deeply used stacks in |
| 782 | * random callers for direct reclaim or memcg reclaim. We explicitly |
| 783 | * allow reclaim from kswapd as the stack usage there is relatively low. |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 784 | * |
Mel Gorman | 94054fa | 2011-10-31 17:07:45 -0700 | [diff] [blame] | 785 | * This should never happen except in the case of a VM regression so |
| 786 | * warn about it. |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 787 | */ |
Mel Gorman | 94054fa | 2011-10-31 17:07:45 -0700 | [diff] [blame] | 788 | if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == |
| 789 | PF_MEMALLOC)) |
Christoph Hellwig | b5420f2 | 2010-08-24 11:47:51 +1000 | [diff] [blame] | 790 | goto redirty; |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 791 | |
| 792 | /* |
Christoph Hellwig | 680a647 | 2011-07-08 14:34:05 +0200 | [diff] [blame] | 793 | * Given that we do not allow direct reclaim to call us, we should |
| 794 | * never be called while in a filesystem transaction. |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 795 | */ |
Christoph Hellwig | 448011e | 2014-06-06 16:05:15 +1000 | [diff] [blame] | 796 | if (WARN_ON_ONCE(current->flags & PF_FSTRANS)) |
Christoph Hellwig | b5420f2 | 2010-08-24 11:47:51 +1000 | [diff] [blame] | 797 | goto redirty; |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 798 | |
Jie Liu | 8695d27 | 2014-05-20 08:24:26 +1000 | [diff] [blame] | 799 | /* |
Dave Chinner | ad68972 | 2016-02-15 17:21:31 +1100 | [diff] [blame^] | 800 | * Is this page beyond the end of the file? |
| 801 | * |
Jie Liu | 8695d27 | 2014-05-20 08:24:26 +1000 | [diff] [blame] | 802 | * The page index is less than the end_index, adjust the end_offset |
| 803 | * to the highest offset that this page should represent. |
| 804 | * ----------------------------------------------------- |
| 805 | * | file mapping | <EOF> | |
| 806 | * ----------------------------------------------------- |
| 807 | * | Page ... | Page N-2 | Page N-1 | Page N | | |
| 808 | * ^--------------------------------^----------|-------- |
| 809 | * | desired writeback range | see else | |
| 810 | * ---------------------------------^------------------| |
| 811 | */ |
Dave Chinner | ad68972 | 2016-02-15 17:21:31 +1100 | [diff] [blame^] | 812 | offset = i_size_read(inode); |
| 813 | end_index = offset >> PAGE_CACHE_SHIFT; |
Jie Liu | 8695d27 | 2014-05-20 08:24:26 +1000 | [diff] [blame] | 814 | if (page->index < end_index) |
| 815 | end_offset = (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT; |
| 816 | else { |
| 817 | /* |
| 818 | * Check whether the page to write out is beyond or straddles |
| 819 | * i_size or not. |
| 820 | * ------------------------------------------------------- |
| 821 | * | file mapping | <EOF> | |
| 822 | * ------------------------------------------------------- |
| 823 | * | Page ... | Page N-2 | Page N-1 | Page N | Beyond | |
| 824 | * ^--------------------------------^-----------|--------- |
| 825 | * | | Straddles | |
| 826 | * ---------------------------------^-----------|--------| |
| 827 | */ |
Christoph Hellwig | 6b7a03f | 2012-07-03 12:20:00 -0400 | [diff] [blame] | 828 | unsigned offset_into_page = offset & (PAGE_CACHE_SIZE - 1); |
| 829 | |
| 830 | /* |
Jan Kara | ff9a28f | 2013-03-14 14:30:54 +0100 | [diff] [blame] | 831 | * Skip the page if it is fully outside i_size, e.g. due to a |
| 832 | * truncate operation that is in progress. We must redirty the |
| 833 | * page so that reclaim stops reclaiming it. Otherwise |
| 834 | * xfs_vm_releasepage() is called on it and gets confused. |
Jie Liu | 8695d27 | 2014-05-20 08:24:26 +1000 | [diff] [blame] | 835 | * |
| 836 | * Note that the end_index is unsigned long, it would overflow |
| 837 | * if the given offset is greater than 16TB on 32-bit system |
| 838 | * and if we do check the page is fully outside i_size or not |
| 839 | * via "if (page->index >= end_index + 1)" as "end_index + 1" |
| 840 | * will be evaluated to 0. Hence this page will be redirtied |
| 841 | * and be written out repeatedly which would result in an |
| 842 | * infinite loop, the user program that perform this operation |
| 843 | * will hang. Instead, we can verify this situation by checking |
| 844 | * if the page to write is totally beyond the i_size or if it's |
| 845 | * offset is just equal to the EOF. |
Christoph Hellwig | 6b7a03f | 2012-07-03 12:20:00 -0400 | [diff] [blame] | 846 | */ |
Jie Liu | 8695d27 | 2014-05-20 08:24:26 +1000 | [diff] [blame] | 847 | if (page->index > end_index || |
| 848 | (page->index == end_index && offset_into_page == 0)) |
Jan Kara | ff9a28f | 2013-03-14 14:30:54 +0100 | [diff] [blame] | 849 | goto redirty; |
Christoph Hellwig | 6b7a03f | 2012-07-03 12:20:00 -0400 | [diff] [blame] | 850 | |
| 851 | /* |
| 852 | * The page straddles i_size. It must be zeroed out on each |
| 853 | * and every writepage invocation because it may be mmapped. |
| 854 | * "A file is mapped in multiples of the page size. For a file |
Jie Liu | 8695d27 | 2014-05-20 08:24:26 +1000 | [diff] [blame] | 855 | * that is not a multiple of the page size, the remaining |
Christoph Hellwig | 6b7a03f | 2012-07-03 12:20:00 -0400 | [diff] [blame] | 856 | * memory is zeroed when mapped, and writes to that region are |
| 857 | * not written out to the file." |
| 858 | */ |
| 859 | zero_user_segment(page, offset_into_page, PAGE_CACHE_SIZE); |
Jie Liu | 8695d27 | 2014-05-20 08:24:26 +1000 | [diff] [blame] | 860 | |
| 861 | /* Adjust the end_offset to the end of file */ |
| 862 | end_offset = offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 865 | len = 1 << inode->i_blkbits; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 866 | |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 867 | bh = head = page_buffers(page); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 868 | offset = page_offset(page); |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | do { |
| 871 | if (offset >= end_offset) |
| 872 | break; |
| 873 | if (!buffer_uptodate(bh)) |
| 874 | uptodate = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
Eric Sandeen | 3d9b02e | 2010-06-24 09:45:30 +1000 | [diff] [blame] | 876 | /* |
Christoph Hellwig | ece413f | 2010-11-10 21:39:11 +0000 | [diff] [blame] | 877 | * set_page_dirty dirties all buffers in a page, independent |
| 878 | * of their state. The dirty state however is entirely |
| 879 | * meaningless for holes (!mapped && uptodate), so skip |
| 880 | * buffers covering holes here. |
Eric Sandeen | 3d9b02e | 2010-06-24 09:45:30 +1000 | [diff] [blame] | 881 | */ |
| 882 | if (!buffer_mapped(bh) && buffer_uptodate(bh)) { |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 883 | wpc->imap_valid = false; |
Eric Sandeen | 3d9b02e | 2010-06-24 09:45:30 +1000 | [diff] [blame] | 884 | continue; |
| 885 | } |
| 886 | |
Christoph Hellwig | aeea1b1 | 2010-12-10 08:42:24 +0000 | [diff] [blame] | 887 | if (buffer_unwritten(bh)) { |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 888 | if (wpc->io_type != XFS_IO_UNWRITTEN) { |
| 889 | wpc->io_type = XFS_IO_UNWRITTEN; |
| 890 | wpc->imap_valid = false; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 891 | } |
Christoph Hellwig | aeea1b1 | 2010-12-10 08:42:24 +0000 | [diff] [blame] | 892 | } else if (buffer_delay(bh)) { |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 893 | if (wpc->io_type != XFS_IO_DELALLOC) { |
| 894 | wpc->io_type = XFS_IO_DELALLOC; |
| 895 | wpc->imap_valid = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | } |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 897 | } else if (buffer_uptodate(bh)) { |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 898 | if (wpc->io_type != XFS_IO_OVERWRITE) { |
| 899 | wpc->io_type = XFS_IO_OVERWRITE; |
| 900 | wpc->imap_valid = false; |
Christoph Hellwig | 85da94c | 2010-12-10 08:42:16 +0000 | [diff] [blame] | 901 | } |
Christoph Hellwig | aeea1b1 | 2010-12-10 08:42:24 +0000 | [diff] [blame] | 902 | } else { |
Alain Renaud | 7d0fa3e | 2012-06-08 15:34:46 -0400 | [diff] [blame] | 903 | if (PageUptodate(page)) |
Christoph Hellwig | aeea1b1 | 2010-12-10 08:42:24 +0000 | [diff] [blame] | 904 | ASSERT(buffer_mapped(bh)); |
Alain Renaud | 7d0fa3e | 2012-06-08 15:34:46 -0400 | [diff] [blame] | 905 | /* |
| 906 | * This buffer is not uptodate and will not be |
| 907 | * written to disk. Ensure that we will put any |
| 908 | * subsequent writeable buffers into a new |
| 909 | * ioend. |
| 910 | */ |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 911 | wpc->imap_valid = 0; |
Christoph Hellwig | aeea1b1 | 2010-12-10 08:42:24 +0000 | [diff] [blame] | 912 | continue; |
| 913 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 915 | if (wpc->imap_valid) |
| 916 | wpc->imap_valid = xfs_imap_valid(inode, &wpc->imap, offset); |
| 917 | if (!wpc->imap_valid) { |
| 918 | err = xfs_map_blocks(inode, offset, &wpc->imap, |
| 919 | wpc->io_type); |
Christoph Hellwig | aeea1b1 | 2010-12-10 08:42:24 +0000 | [diff] [blame] | 920 | if (err) |
| 921 | goto error; |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 922 | wpc->imap_valid = xfs_imap_valid(inode, &wpc->imap, offset); |
Christoph Hellwig | aeea1b1 | 2010-12-10 08:42:24 +0000 | [diff] [blame] | 923 | } |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 924 | if (wpc->imap_valid) { |
Christoph Hellwig | ecff71e | 2010-12-10 08:42:25 +0000 | [diff] [blame] | 925 | lock_buffer(bh); |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 926 | if (wpc->io_type != XFS_IO_OVERWRITE) |
| 927 | xfs_map_at_offset(inode, bh, &wpc->imap, offset); |
| 928 | xfs_add_to_ioend(inode, bh, offset, wpc); |
Christoph Hellwig | aeea1b1 | 2010-12-10 08:42:24 +0000 | [diff] [blame] | 929 | count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | } |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 931 | |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 932 | if (!wpc->iohead) |
| 933 | wpc->iohead = wpc->ioend; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 934 | |
| 935 | } while (offset += len, ((bh = bh->b_this_page) != head)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | |
| 937 | if (uptodate && bh == head) |
| 938 | SetPageUptodate(page); |
| 939 | |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 940 | xfs_start_page_writeback(page, 1, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | |
Dave Chinner | ad68972 | 2016-02-15 17:21:31 +1100 | [diff] [blame^] | 942 | ASSERT(wpc->iohead || !count); |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 943 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | |
| 945 | error: |
Dave Chinner | 150d5be | 2016-02-15 17:21:12 +1100 | [diff] [blame] | 946 | /* |
| 947 | * On error, we have to fail the iohead here because we buffers locked |
| 948 | * in the ioend chain. If we don't do this, we'll deadlock invalidating |
| 949 | * the page as that tries to lock the buffers on the page. Also, because |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 950 | * we may have set pages under writeback, we have to make sure we run |
| 951 | * IO completion to mark the error state of the IO appropriately, so we |
| 952 | * can't cancel the ioend directly here. That means we have to mark this |
| 953 | * page as under writeback if we included any buffers from it in the |
| 954 | * ioend chain so that completion treats it correctly. |
| 955 | * |
| 956 | * If we didn't include the page in the ioend, then we can simply |
| 957 | * discard and unlock it as there are no other users of the page or it's |
| 958 | * buffers right now. The caller will still need to trigger submission |
| 959 | * of outstanding ioends on the writepage context so they are treated |
| 960 | * correctly on error. |
Dave Chinner | 150d5be | 2016-02-15 17:21:12 +1100 | [diff] [blame] | 961 | */ |
| 962 | if (count) |
| 963 | xfs_start_page_writeback(page, 0, count); |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 964 | else { |
Dave Chinner | 150d5be | 2016-02-15 17:21:12 +1100 | [diff] [blame] | 965 | xfs_aops_discard_page(page); |
| 966 | ClearPageUptodate(page); |
| 967 | unlock_page(page); |
| 968 | } |
| 969 | mapping_set_error(page->mapping, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | return err; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 971 | |
Christoph Hellwig | b5420f2 | 2010-08-24 11:47:51 +1000 | [diff] [blame] | 972 | redirty: |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 973 | redirty_page_for_writepage(wbc, page); |
| 974 | unlock_page(page); |
| 975 | return 0; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 976 | } |
| 977 | |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 978 | STATIC int |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 979 | xfs_vm_writepage( |
| 980 | struct page *page, |
| 981 | struct writeback_control *wbc) |
| 982 | { |
| 983 | struct xfs_writepage_ctx wpc = { |
| 984 | .io_type = XFS_IO_INVALID, |
| 985 | }; |
| 986 | int ret; |
| 987 | |
| 988 | ret = xfs_do_writepage(page, wbc, &wpc); |
| 989 | return xfs_writepage_submit(&wpc, wbc, ret); |
| 990 | } |
| 991 | |
| 992 | STATIC int |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 993 | xfs_vm_writepages( |
| 994 | struct address_space *mapping, |
| 995 | struct writeback_control *wbc) |
| 996 | { |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 997 | struct xfs_writepage_ctx wpc = { |
| 998 | .io_type = XFS_IO_INVALID, |
| 999 | }; |
| 1000 | int ret; |
| 1001 | |
Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 1002 | xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED); |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 1003 | ret = write_cache_pages(mapping, wbc, xfs_do_writepage, &wpc); |
| 1004 | return xfs_writepage_submit(&wpc, wbc, ret); |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 1005 | } |
| 1006 | |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1007 | /* |
| 1008 | * Called to move a page into cleanable state - and from there |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1009 | * to be released. The page should already be clean. We always |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1010 | * have buffer heads in this call. |
| 1011 | * |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1012 | * Returns 1 if the page is ok to release, 0 otherwise. |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1013 | */ |
| 1014 | STATIC int |
Nathan Scott | 238f4c5 | 2006-03-17 17:26:25 +1100 | [diff] [blame] | 1015 | xfs_vm_releasepage( |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1016 | struct page *page, |
| 1017 | gfp_t gfp_mask) |
| 1018 | { |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1019 | int delalloc, unwritten; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1020 | |
Lukas Czerner | 34097df | 2013-05-21 23:58:01 -0400 | [diff] [blame] | 1021 | trace_xfs_releasepage(page->mapping->host, page, 0, 0); |
Nathan Scott | 238f4c5 | 2006-03-17 17:26:25 +1100 | [diff] [blame] | 1022 | |
Christoph Hellwig | 20cb52e | 2010-06-24 09:46:01 +1000 | [diff] [blame] | 1023 | xfs_count_page_state(page, &delalloc, &unwritten); |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1024 | |
Christoph Hellwig | 448011e | 2014-06-06 16:05:15 +1000 | [diff] [blame] | 1025 | if (WARN_ON_ONCE(delalloc)) |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1026 | return 0; |
Christoph Hellwig | 448011e | 2014-06-06 16:05:15 +1000 | [diff] [blame] | 1027 | if (WARN_ON_ONCE(unwritten)) |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1028 | return 0; |
| 1029 | |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1030 | return try_to_free_buffers(page); |
| 1031 | } |
| 1032 | |
Dave Chinner | a719370 | 2015-04-16 21:57:48 +1000 | [diff] [blame] | 1033 | /* |
Dave Chinner | a06c277 | 2015-04-16 22:00:00 +1000 | [diff] [blame] | 1034 | * When we map a DIO buffer, we may need to attach an ioend that describes the |
| 1035 | * type of write IO we are doing. This passes to the completion function the |
| 1036 | * operations it needs to perform. If the mapping is for an overwrite wholly |
| 1037 | * within the EOF then we don't need an ioend and so we don't allocate one. |
| 1038 | * This avoids the unnecessary overhead of allocating and freeing ioends for |
| 1039 | * workloads that don't require transactions on IO completion. |
Dave Chinner | d5cc2e3 | 2015-04-16 21:59:07 +1000 | [diff] [blame] | 1040 | * |
| 1041 | * If we get multiple mappings in a single IO, we might be mapping different |
| 1042 | * types. But because the direct IO can only have a single private pointer, we |
| 1043 | * need to ensure that: |
| 1044 | * |
Dave Chinner | a06c277 | 2015-04-16 22:00:00 +1000 | [diff] [blame] | 1045 | * a) i) the ioend spans the entire region of unwritten mappings; or |
| 1046 | * ii) the ioend spans all the mappings that cross or are beyond EOF; and |
Dave Chinner | d5cc2e3 | 2015-04-16 21:59:07 +1000 | [diff] [blame] | 1047 | * b) if it contains unwritten extents, it is *permanently* marked as such |
| 1048 | * |
| 1049 | * We could do this by chaining ioends like buffered IO does, but we only |
| 1050 | * actually get one IO completion callback from the direct IO, and that spans |
| 1051 | * the entire IO regardless of how many mappings and IOs are needed to complete |
| 1052 | * the DIO. There is only going to be one reference to the ioend and its life |
| 1053 | * cycle is constrained by the DIO completion code. hence we don't need |
| 1054 | * reference counting here. |
Dave Chinner | 3e12dbb | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 1055 | * |
| 1056 | * Note that for DIO, an IO to the highest supported file block offset (i.e. |
| 1057 | * 2^63 - 1FSB bytes) will result in the offset + count overflowing a signed 64 |
| 1058 | * bit variable. Hence if we see this overflow, we have to assume that the IO is |
| 1059 | * extending the file size. We won't know for sure until IO completion is run |
| 1060 | * and the actual max write offset is communicated to the IO completion |
| 1061 | * routine. |
| 1062 | * |
| 1063 | * For DAX page faults, we are preparing to never see unwritten extents here, |
| 1064 | * nor should we ever extend the inode size. Hence we will soon have nothing to |
| 1065 | * do here for this case, ensuring we don't have to provide an IO completion |
| 1066 | * callback to free an ioend that we don't actually need for a fault into the |
| 1067 | * page at offset (2^63 - 1FSB) bytes. |
Dave Chinner | a719370 | 2015-04-16 21:57:48 +1000 | [diff] [blame] | 1068 | */ |
Dave Chinner | 3e12dbb | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 1069 | |
Dave Chinner | a719370 | 2015-04-16 21:57:48 +1000 | [diff] [blame] | 1070 | static void |
| 1071 | xfs_map_direct( |
| 1072 | struct inode *inode, |
| 1073 | struct buffer_head *bh_result, |
| 1074 | struct xfs_bmbt_irec *imap, |
Dave Chinner | 3e12dbb | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 1075 | xfs_off_t offset, |
| 1076 | bool dax_fault) |
Dave Chinner | a719370 | 2015-04-16 21:57:48 +1000 | [diff] [blame] | 1077 | { |
Dave Chinner | d5cc2e3 | 2015-04-16 21:59:07 +1000 | [diff] [blame] | 1078 | struct xfs_ioend *ioend; |
| 1079 | xfs_off_t size = bh_result->b_size; |
| 1080 | int type; |
| 1081 | |
| 1082 | if (ISUNWRITTEN(imap)) |
| 1083 | type = XFS_IO_UNWRITTEN; |
| 1084 | else |
| 1085 | type = XFS_IO_OVERWRITE; |
| 1086 | |
| 1087 | trace_xfs_gbmap_direct(XFS_I(inode), offset, size, type, imap); |
| 1088 | |
Dave Chinner | 3e12dbb | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 1089 | if (dax_fault) { |
| 1090 | ASSERT(type == XFS_IO_OVERWRITE); |
| 1091 | trace_xfs_gbmap_direct_none(XFS_I(inode), offset, size, type, |
| 1092 | imap); |
| 1093 | return; |
| 1094 | } |
Dave Chinner | 3e12dbb | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 1095 | |
Dave Chinner | d5cc2e3 | 2015-04-16 21:59:07 +1000 | [diff] [blame] | 1096 | if (bh_result->b_private) { |
| 1097 | ioend = bh_result->b_private; |
| 1098 | ASSERT(ioend->io_size > 0); |
| 1099 | ASSERT(offset >= ioend->io_offset); |
| 1100 | if (offset + size > ioend->io_offset + ioend->io_size) |
| 1101 | ioend->io_size = offset - ioend->io_offset + size; |
| 1102 | |
| 1103 | if (type == XFS_IO_UNWRITTEN && type != ioend->io_type) |
| 1104 | ioend->io_type = XFS_IO_UNWRITTEN; |
| 1105 | |
| 1106 | trace_xfs_gbmap_direct_update(XFS_I(inode), ioend->io_offset, |
| 1107 | ioend->io_size, ioend->io_type, |
| 1108 | imap); |
Dave Chinner | a06c277 | 2015-04-16 22:00:00 +1000 | [diff] [blame] | 1109 | } else if (type == XFS_IO_UNWRITTEN || |
Dave Chinner | 3e12dbb | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 1110 | offset + size > i_size_read(inode) || |
| 1111 | offset + size < 0) { |
Dave Chinner | d5cc2e3 | 2015-04-16 21:59:07 +1000 | [diff] [blame] | 1112 | ioend = xfs_alloc_ioend(inode, type); |
| 1113 | ioend->io_offset = offset; |
| 1114 | ioend->io_size = size; |
Dave Chinner | a06c277 | 2015-04-16 22:00:00 +1000 | [diff] [blame] | 1115 | |
Dave Chinner | d5cc2e3 | 2015-04-16 21:59:07 +1000 | [diff] [blame] | 1116 | bh_result->b_private = ioend; |
Dave Chinner | a06c277 | 2015-04-16 22:00:00 +1000 | [diff] [blame] | 1117 | set_buffer_defer_completion(bh_result); |
Dave Chinner | d5cc2e3 | 2015-04-16 21:59:07 +1000 | [diff] [blame] | 1118 | |
| 1119 | trace_xfs_gbmap_direct_new(XFS_I(inode), offset, size, type, |
| 1120 | imap); |
Dave Chinner | a06c277 | 2015-04-16 22:00:00 +1000 | [diff] [blame] | 1121 | } else { |
| 1122 | trace_xfs_gbmap_direct_none(XFS_I(inode), offset, size, type, |
| 1123 | imap); |
Dave Chinner | a719370 | 2015-04-16 21:57:48 +1000 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | |
Dave Chinner | 1fdca9c | 2015-04-16 21:58:21 +1000 | [diff] [blame] | 1127 | /* |
| 1128 | * If this is O_DIRECT or the mpage code calling tell them how large the mapping |
| 1129 | * is, so that we can avoid repeated get_blocks calls. |
| 1130 | * |
| 1131 | * If the mapping spans EOF, then we have to break the mapping up as the mapping |
| 1132 | * for blocks beyond EOF must be marked new so that sub block regions can be |
| 1133 | * correctly zeroed. We can't do this for mappings within EOF unless the mapping |
| 1134 | * was just allocated or is unwritten, otherwise the callers would overwrite |
| 1135 | * existing data with zeros. Hence we have to split the mapping into a range up |
| 1136 | * to and including EOF, and a second mapping for beyond EOF. |
| 1137 | */ |
| 1138 | static void |
| 1139 | xfs_map_trim_size( |
| 1140 | struct inode *inode, |
| 1141 | sector_t iblock, |
| 1142 | struct buffer_head *bh_result, |
| 1143 | struct xfs_bmbt_irec *imap, |
| 1144 | xfs_off_t offset, |
| 1145 | ssize_t size) |
| 1146 | { |
| 1147 | xfs_off_t mapping_size; |
| 1148 | |
| 1149 | mapping_size = imap->br_startoff + imap->br_blockcount - iblock; |
| 1150 | mapping_size <<= inode->i_blkbits; |
| 1151 | |
| 1152 | ASSERT(mapping_size > 0); |
| 1153 | if (mapping_size > size) |
| 1154 | mapping_size = size; |
| 1155 | if (offset < i_size_read(inode) && |
| 1156 | offset + mapping_size >= i_size_read(inode)) { |
| 1157 | /* limit mapping to block that spans EOF */ |
| 1158 | mapping_size = roundup_64(i_size_read(inode) - offset, |
| 1159 | 1 << inode->i_blkbits); |
| 1160 | } |
| 1161 | if (mapping_size > LONG_MAX) |
| 1162 | mapping_size = LONG_MAX; |
| 1163 | |
| 1164 | bh_result->b_size = mapping_size; |
| 1165 | } |
| 1166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | STATIC int |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1168 | __xfs_get_blocks( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | struct inode *inode, |
| 1170 | sector_t iblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | struct buffer_head *bh_result, |
| 1172 | int create, |
Dave Chinner | 3e12dbb | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 1173 | bool direct, |
| 1174 | bool dax_fault) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | { |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1176 | struct xfs_inode *ip = XFS_I(inode); |
| 1177 | struct xfs_mount *mp = ip->i_mount; |
| 1178 | xfs_fileoff_t offset_fsb, end_fsb; |
| 1179 | int error = 0; |
| 1180 | int lockmode = 0; |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1181 | struct xfs_bmbt_irec imap; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1182 | int nimaps = 1; |
Nathan Scott | fdc7ed7 | 2005-11-02 15:13:13 +1100 | [diff] [blame] | 1183 | xfs_off_t offset; |
| 1184 | ssize_t size; |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1185 | int new = 0; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1186 | |
| 1187 | if (XFS_FORCED_SHUTDOWN(mp)) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 1188 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | |
Nathan Scott | fdc7ed7 | 2005-11-02 15:13:13 +1100 | [diff] [blame] | 1190 | offset = (xfs_off_t)iblock << inode->i_blkbits; |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1191 | ASSERT(bh_result->b_size >= (1 << inode->i_blkbits)); |
| 1192 | size = bh_result->b_size; |
Lachlan McIlroy | 364f358 | 2008-09-17 16:50:14 +1000 | [diff] [blame] | 1193 | |
| 1194 | if (!create && direct && offset >= i_size_read(inode)) |
| 1195 | return 0; |
| 1196 | |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 1197 | /* |
| 1198 | * Direct I/O is usually done on preallocated files, so try getting |
| 1199 | * a block mapping without an exclusive lock first. For buffered |
| 1200 | * writes we already have the exclusive iolock anyway, so avoiding |
| 1201 | * a lock roundtrip here by taking the ilock exclusive from the |
| 1202 | * beginning is a useful micro optimization. |
| 1203 | */ |
| 1204 | if (create && !direct) { |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1205 | lockmode = XFS_ILOCK_EXCL; |
| 1206 | xfs_ilock(ip, lockmode); |
| 1207 | } else { |
Christoph Hellwig | 309ecac | 2013-12-06 12:30:09 -0800 | [diff] [blame] | 1208 | lockmode = xfs_ilock_data_map_shared(ip); |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1209 | } |
Christoph Hellwig | f2bde9b | 2010-06-24 11:44:35 +1000 | [diff] [blame] | 1210 | |
Dave Chinner | d2c2819 | 2012-06-08 15:44:53 +1000 | [diff] [blame] | 1211 | ASSERT(offset <= mp->m_super->s_maxbytes); |
| 1212 | if (offset + size > mp->m_super->s_maxbytes) |
| 1213 | size = mp->m_super->s_maxbytes - offset; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1214 | end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size); |
| 1215 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 1216 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 1217 | error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, |
| 1218 | &imap, &nimaps, XFS_BMAPI_ENTIRE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | if (error) |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1220 | goto out_unlock; |
| 1221 | |
Dave Chinner | 1ca1915 | 2015-11-03 12:37:00 +1100 | [diff] [blame] | 1222 | /* for DAX, we convert unwritten extents directly */ |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1223 | if (create && |
| 1224 | (!nimaps || |
| 1225 | (imap.br_startblock == HOLESTARTBLOCK || |
Dave Chinner | 1ca1915 | 2015-11-03 12:37:00 +1100 | [diff] [blame] | 1226 | imap.br_startblock == DELAYSTARTBLOCK) || |
| 1227 | (IS_DAX(inode) && ISUNWRITTEN(&imap)))) { |
Dave Chinner | aff3a9e | 2012-04-23 15:58:44 +1000 | [diff] [blame] | 1228 | if (direct || xfs_get_extsz_hint(ip)) { |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 1229 | /* |
Brian Foster | 009c6e8 | 2015-10-12 15:34:20 +1100 | [diff] [blame] | 1230 | * xfs_iomap_write_direct() expects the shared lock. It |
| 1231 | * is unlocked on return. |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 1232 | */ |
Brian Foster | 009c6e8 | 2015-10-12 15:34:20 +1100 | [diff] [blame] | 1233 | if (lockmode == XFS_ILOCK_EXCL) |
| 1234 | xfs_ilock_demote(ip, lockmode); |
| 1235 | |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1236 | error = xfs_iomap_write_direct(ip, offset, size, |
| 1237 | &imap, nimaps); |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 1238 | if (error) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1239 | return error; |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1240 | new = 1; |
Dave Chinner | 6b698ed | 2015-06-04 09:18:53 +1000 | [diff] [blame] | 1241 | |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1242 | } else { |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 1243 | /* |
| 1244 | * Delalloc reservations do not require a transaction, |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1245 | * we can go on without dropping the lock here. If we |
| 1246 | * are allocating a new delalloc block, make sure that |
| 1247 | * we set the new flag so that we mark the buffer new so |
| 1248 | * that we know that it is newly allocated if the write |
| 1249 | * fails. |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 1250 | */ |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1251 | if (nimaps && imap.br_startblock == HOLESTARTBLOCK) |
| 1252 | new = 1; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1253 | error = xfs_iomap_write_delay(ip, offset, size, &imap); |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 1254 | if (error) |
| 1255 | goto out_unlock; |
| 1256 | |
| 1257 | xfs_iunlock(ip, lockmode); |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1258 | } |
Dave Chinner | d5cc2e3 | 2015-04-16 21:59:07 +1000 | [diff] [blame] | 1259 | trace_xfs_get_blocks_alloc(ip, offset, size, |
| 1260 | ISUNWRITTEN(&imap) ? XFS_IO_UNWRITTEN |
| 1261 | : XFS_IO_DELALLOC, &imap); |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1262 | } else if (nimaps) { |
Dave Chinner | d5cc2e3 | 2015-04-16 21:59:07 +1000 | [diff] [blame] | 1263 | trace_xfs_get_blocks_found(ip, offset, size, |
| 1264 | ISUNWRITTEN(&imap) ? XFS_IO_UNWRITTEN |
| 1265 | : XFS_IO_OVERWRITE, &imap); |
Dave Chinner | 507630b | 2012-03-27 10:34:50 -0400 | [diff] [blame] | 1266 | xfs_iunlock(ip, lockmode); |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1267 | } else { |
| 1268 | trace_xfs_get_blocks_notfound(ip, offset, size); |
| 1269 | goto out_unlock; |
| 1270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | |
Dave Chinner | 1ca1915 | 2015-11-03 12:37:00 +1100 | [diff] [blame] | 1272 | if (IS_DAX(inode) && create) { |
| 1273 | ASSERT(!ISUNWRITTEN(&imap)); |
| 1274 | /* zeroing is not needed at a higher layer */ |
| 1275 | new = 0; |
| 1276 | } |
| 1277 | |
Dave Chinner | 1fdca9c | 2015-04-16 21:58:21 +1000 | [diff] [blame] | 1278 | /* trim mapping down to size requested */ |
| 1279 | if (direct || size > (1 << inode->i_blkbits)) |
| 1280 | xfs_map_trim_size(inode, iblock, bh_result, |
| 1281 | &imap, offset, size); |
| 1282 | |
Dave Chinner | a719370 | 2015-04-16 21:57:48 +1000 | [diff] [blame] | 1283 | /* |
| 1284 | * For unwritten extents do not report a disk address in the buffered |
| 1285 | * read case (treat as if we're reading into a hole). |
| 1286 | */ |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1287 | if (imap.br_startblock != HOLESTARTBLOCK && |
Dave Chinner | a719370 | 2015-04-16 21:57:48 +1000 | [diff] [blame] | 1288 | imap.br_startblock != DELAYSTARTBLOCK && |
| 1289 | (create || !ISUNWRITTEN(&imap))) { |
| 1290 | xfs_map_buffer(inode, bh_result, &imap, offset); |
| 1291 | if (ISUNWRITTEN(&imap)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | set_buffer_unwritten(bh_result); |
Dave Chinner | a719370 | 2015-04-16 21:57:48 +1000 | [diff] [blame] | 1293 | /* direct IO needs special help */ |
| 1294 | if (create && direct) |
Dave Chinner | 3e12dbb | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 1295 | xfs_map_direct(inode, bh_result, &imap, offset, |
| 1296 | dax_fault); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1299 | /* |
| 1300 | * If this is a realtime file, data may be on a different device. |
| 1301 | * to that pointed to from the buffer_head b_bdev currently. |
| 1302 | */ |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 1303 | bh_result->b_bdev = xfs_find_bdev_for_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1305 | /* |
David Chinner | 549054a | 2007-02-10 18:36:35 +1100 | [diff] [blame] | 1306 | * If we previously allocated a block out beyond eof and we are now |
| 1307 | * coming back to use it then we will need to flag it as new even if it |
| 1308 | * has a disk address. |
| 1309 | * |
| 1310 | * With sub-block writes into unwritten extents we also need to mark |
| 1311 | * the buffer as new so that the unwritten parts of the buffer gets |
| 1312 | * correctly zeroed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | */ |
| 1314 | if (create && |
| 1315 | ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) || |
David Chinner | 549054a | 2007-02-10 18:36:35 +1100 | [diff] [blame] | 1316 | (offset >= i_size_read(inode)) || |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1317 | (new || ISUNWRITTEN(&imap)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | set_buffer_new(bh_result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | |
Christoph Hellwig | 207d041 | 2010-04-28 12:28:56 +0000 | [diff] [blame] | 1320 | if (imap.br_startblock == DELAYSTARTBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | BUG_ON(direct); |
| 1322 | if (create) { |
| 1323 | set_buffer_uptodate(bh_result); |
| 1324 | set_buffer_mapped(bh_result); |
| 1325 | set_buffer_delay(bh_result); |
| 1326 | } |
| 1327 | } |
| 1328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | return 0; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 1330 | |
| 1331 | out_unlock: |
| 1332 | xfs_iunlock(ip, lockmode); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1333 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | int |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1337 | xfs_get_blocks( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | struct inode *inode, |
| 1339 | sector_t iblock, |
| 1340 | struct buffer_head *bh_result, |
| 1341 | int create) |
| 1342 | { |
Dave Chinner | 3e12dbb | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 1343 | return __xfs_get_blocks(inode, iblock, bh_result, create, false, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
Dave Chinner | 6b698ed | 2015-06-04 09:18:53 +1000 | [diff] [blame] | 1346 | int |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1347 | xfs_get_blocks_direct( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | struct inode *inode, |
| 1349 | sector_t iblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | struct buffer_head *bh_result, |
| 1351 | int create) |
| 1352 | { |
Dave Chinner | 3e12dbb | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 1353 | return __xfs_get_blocks(inode, iblock, bh_result, create, true, false); |
| 1354 | } |
| 1355 | |
| 1356 | int |
| 1357 | xfs_get_blocks_dax_fault( |
| 1358 | struct inode *inode, |
| 1359 | sector_t iblock, |
| 1360 | struct buffer_head *bh_result, |
| 1361 | int create) |
| 1362 | { |
| 1363 | return __xfs_get_blocks(inode, iblock, bh_result, create, true, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
Dave Chinner | 6b698ed | 2015-06-04 09:18:53 +1000 | [diff] [blame] | 1366 | static void |
| 1367 | __xfs_end_io_direct_write( |
| 1368 | struct inode *inode, |
| 1369 | struct xfs_ioend *ioend, |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1370 | loff_t offset, |
Dave Chinner | 6b698ed | 2015-06-04 09:18:53 +1000 | [diff] [blame] | 1371 | ssize_t size) |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1372 | { |
Dave Chinner | 6b698ed | 2015-06-04 09:18:53 +1000 | [diff] [blame] | 1373 | struct xfs_mount *mp = XFS_I(inode)->i_mount; |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 1374 | |
Dave Chinner | 6b698ed | 2015-06-04 09:18:53 +1000 | [diff] [blame] | 1375 | if (XFS_FORCED_SHUTDOWN(mp) || ioend->io_error) |
Dave Chinner | 6dfa1b6 | 2015-04-16 21:59:34 +1000 | [diff] [blame] | 1376 | goto out_end_io; |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1377 | |
| 1378 | /* |
Dave Chinner | d5cc2e3 | 2015-04-16 21:59:07 +1000 | [diff] [blame] | 1379 | * dio completion end_io functions are only called on writes if more |
| 1380 | * than 0 bytes was written. |
Christoph Hellwig | 2813d68 | 2011-12-18 20:00:12 +0000 | [diff] [blame] | 1381 | */ |
Dave Chinner | d5cc2e3 | 2015-04-16 21:59:07 +1000 | [diff] [blame] | 1382 | ASSERT(size > 0); |
| 1383 | |
| 1384 | /* |
| 1385 | * The ioend only maps whole blocks, while the IO may be sector aligned. |
Dave Chinner | a06c277 | 2015-04-16 22:00:00 +1000 | [diff] [blame] | 1386 | * Hence the ioend offset/size may not match the IO offset/size exactly. |
| 1387 | * Because we don't map overwrites within EOF into the ioend, the offset |
| 1388 | * may not match, but only if the endio spans EOF. Either way, write |
| 1389 | * the IO sizes into the ioend so that completion processing does the |
| 1390 | * right thing. |
Dave Chinner | d5cc2e3 | 2015-04-16 21:59:07 +1000 | [diff] [blame] | 1391 | */ |
Dave Chinner | d5cc2e3 | 2015-04-16 21:59:07 +1000 | [diff] [blame] | 1392 | ASSERT(offset + size <= ioend->io_offset + ioend->io_size); |
| 1393 | ioend->io_size = size; |
| 1394 | ioend->io_offset = offset; |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1395 | |
| 1396 | /* |
Dave Chinner | 6dfa1b6 | 2015-04-16 21:59:34 +1000 | [diff] [blame] | 1397 | * The ioend tells us whether we are doing unwritten extent conversion |
| 1398 | * or an append transaction that updates the on-disk file size. These |
| 1399 | * cases are the only cases where we should *potentially* be needing |
Dave Chinner | a06c277 | 2015-04-16 22:00:00 +1000 | [diff] [blame] | 1400 | * to update the VFS inode size. |
Dave Chinner | 6dfa1b6 | 2015-04-16 21:59:34 +1000 | [diff] [blame] | 1401 | * |
| 1402 | * We need to update the in-core inode size here so that we don't end up |
Dave Chinner | a06c277 | 2015-04-16 22:00:00 +1000 | [diff] [blame] | 1403 | * with the on-disk inode size being outside the in-core inode size. We |
| 1404 | * have no other method of updating EOF for AIO, so always do it here |
| 1405 | * if necessary. |
Dave Chinner | b9d5984 | 2015-04-16 22:03:07 +1000 | [diff] [blame] | 1406 | * |
| 1407 | * We need to lock the test/set EOF update as we can be racing with |
| 1408 | * other IO completions here to update the EOF. Failing to serialise |
| 1409 | * here can result in EOF moving backwards and Bad Things Happen when |
| 1410 | * that occurs. |
Christoph Hellwig | 2813d68 | 2011-12-18 20:00:12 +0000 | [diff] [blame] | 1411 | */ |
Dave Chinner | 6b698ed | 2015-06-04 09:18:53 +1000 | [diff] [blame] | 1412 | spin_lock(&XFS_I(inode)->i_flags_lock); |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 1413 | if (offset + size > i_size_read(inode)) |
| 1414 | i_size_write(inode, offset + size); |
Dave Chinner | 6b698ed | 2015-06-04 09:18:53 +1000 | [diff] [blame] | 1415 | spin_unlock(&XFS_I(inode)->i_flags_lock); |
Christoph Hellwig | 2813d68 | 2011-12-18 20:00:12 +0000 | [diff] [blame] | 1416 | |
| 1417 | /* |
Dave Chinner | 6dfa1b6 | 2015-04-16 21:59:34 +1000 | [diff] [blame] | 1418 | * If we are doing an append IO that needs to update the EOF on disk, |
| 1419 | * do the transaction reserve now so we can use common end io |
| 1420 | * processing. Stashing the error (if there is one) in the ioend will |
| 1421 | * result in the ioend processing passing on the error if it is |
| 1422 | * possible as we can't return it from here. |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1423 | */ |
Dave Chinner | a06c277 | 2015-04-16 22:00:00 +1000 | [diff] [blame] | 1424 | if (ioend->io_type == XFS_IO_OVERWRITE) |
Dave Chinner | 6dfa1b6 | 2015-04-16 21:59:34 +1000 | [diff] [blame] | 1425 | ioend->io_error = xfs_setfilesize_trans_alloc(ioend); |
Christoph Hellwig | 40e2e97 | 2010-07-18 21:17:09 +0000 | [diff] [blame] | 1426 | |
Dave Chinner | 6dfa1b6 | 2015-04-16 21:59:34 +1000 | [diff] [blame] | 1427 | out_end_io: |
| 1428 | xfs_end_io(&ioend->io_work); |
| 1429 | return; |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1430 | } |
| 1431 | |
Dave Chinner | 6b698ed | 2015-06-04 09:18:53 +1000 | [diff] [blame] | 1432 | /* |
| 1433 | * Complete a direct I/O write request. |
| 1434 | * |
| 1435 | * The ioend structure is passed from __xfs_get_blocks() to tell us what to do. |
| 1436 | * If no ioend exists (i.e. @private == NULL) then the write IO is an overwrite |
| 1437 | * wholly within the EOF and so there is nothing for us to do. Note that in this |
| 1438 | * case the completion can be called in interrupt context, whereas if we have an |
| 1439 | * ioend we will always be called in task context (i.e. from a workqueue). |
| 1440 | */ |
| 1441 | STATIC void |
| 1442 | xfs_end_io_direct_write( |
| 1443 | struct kiocb *iocb, |
| 1444 | loff_t offset, |
| 1445 | ssize_t size, |
| 1446 | void *private) |
| 1447 | { |
| 1448 | struct inode *inode = file_inode(iocb->ki_filp); |
| 1449 | struct xfs_ioend *ioend = private; |
| 1450 | |
| 1451 | trace_xfs_gbmap_direct_endio(XFS_I(inode), offset, size, |
| 1452 | ioend ? ioend->io_type : 0, NULL); |
| 1453 | |
| 1454 | if (!ioend) { |
| 1455 | ASSERT(offset + size <= i_size_read(inode)); |
| 1456 | return; |
| 1457 | } |
| 1458 | |
| 1459 | __xfs_end_io_direct_write(inode, ioend, offset, size); |
| 1460 | } |
| 1461 | |
Dave Chinner | 6e1ba0b | 2015-06-04 09:19:15 +1000 | [diff] [blame] | 1462 | static inline ssize_t |
| 1463 | xfs_vm_do_dio( |
| 1464 | struct inode *inode, |
| 1465 | struct kiocb *iocb, |
| 1466 | struct iov_iter *iter, |
| 1467 | loff_t offset, |
| 1468 | void (*endio)(struct kiocb *iocb, |
| 1469 | loff_t offset, |
| 1470 | ssize_t size, |
| 1471 | void *private), |
| 1472 | int flags) |
| 1473 | { |
| 1474 | struct block_device *bdev; |
| 1475 | |
| 1476 | if (IS_DAX(inode)) |
| 1477 | return dax_do_io(iocb, inode, iter, offset, |
| 1478 | xfs_get_blocks_direct, endio, 0); |
| 1479 | |
| 1480 | bdev = xfs_find_bdev_for_inode(inode); |
| 1481 | return __blockdev_direct_IO(iocb, inode, bdev, iter, offset, |
| 1482 | xfs_get_blocks_direct, endio, NULL, flags); |
| 1483 | } |
| 1484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | STATIC ssize_t |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1486 | xfs_vm_direct_IO( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | struct kiocb *iocb, |
Al Viro | d8d3d94 | 2014-03-04 21:27:34 -0500 | [diff] [blame] | 1488 | struct iov_iter *iter, |
| 1489 | loff_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | { |
Christoph Hellwig | 209fb87 | 2010-07-18 21:17:11 +0000 | [diff] [blame] | 1491 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | |
Dave Chinner | 6e1ba0b | 2015-06-04 09:19:15 +1000 | [diff] [blame] | 1493 | if (iov_iter_rw(iter) == WRITE) |
| 1494 | return xfs_vm_do_dio(inode, iocb, iter, offset, |
| 1495 | xfs_end_io_direct_write, DIO_ASYNC_EXTEND); |
| 1496 | return xfs_vm_do_dio(inode, iocb, iter, offset, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | } |
| 1498 | |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1499 | /* |
| 1500 | * Punch out the delalloc blocks we have already allocated. |
| 1501 | * |
| 1502 | * Don't bother with xfs_setattr given that nothing can have made it to disk yet |
| 1503 | * as the page is still locked at this point. |
| 1504 | */ |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1505 | STATIC void |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1506 | xfs_vm_kill_delalloc_range( |
| 1507 | struct inode *inode, |
| 1508 | loff_t start, |
| 1509 | loff_t end) |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1510 | { |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1511 | struct xfs_inode *ip = XFS_I(inode); |
| 1512 | xfs_fileoff_t start_fsb; |
| 1513 | xfs_fileoff_t end_fsb; |
| 1514 | int error; |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1515 | |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1516 | start_fsb = XFS_B_TO_FSB(ip->i_mount, start); |
| 1517 | end_fsb = XFS_B_TO_FSB(ip->i_mount, end); |
| 1518 | if (end_fsb <= start_fsb) |
| 1519 | return; |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 1520 | |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1521 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1522 | error = xfs_bmap_punch_delalloc_range(ip, start_fsb, |
| 1523 | end_fsb - start_fsb); |
| 1524 | if (error) { |
| 1525 | /* something screwed, just bail */ |
| 1526 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
| 1527 | xfs_alert(ip->i_mount, |
| 1528 | "xfs_vm_write_failed: unable to clean up ino %lld", |
| 1529 | ip->i_ino); |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 1530 | } |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1531 | } |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1532 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1533 | } |
| 1534 | |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1535 | STATIC void |
| 1536 | xfs_vm_write_failed( |
| 1537 | struct inode *inode, |
| 1538 | struct page *page, |
| 1539 | loff_t pos, |
| 1540 | unsigned len) |
| 1541 | { |
Jie Liu | 58e5985 | 2013-07-16 13:11:16 +0800 | [diff] [blame] | 1542 | loff_t block_offset; |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1543 | loff_t block_start; |
| 1544 | loff_t block_end; |
| 1545 | loff_t from = pos & (PAGE_CACHE_SIZE - 1); |
| 1546 | loff_t to = from + len; |
| 1547 | struct buffer_head *bh, *head; |
| 1548 | |
Jie Liu | 58e5985 | 2013-07-16 13:11:16 +0800 | [diff] [blame] | 1549 | /* |
| 1550 | * The request pos offset might be 32 or 64 bit, this is all fine |
| 1551 | * on 64-bit platform. However, for 64-bit pos request on 32-bit |
| 1552 | * platform, the high 32-bit will be masked off if we evaluate the |
| 1553 | * block_offset via (pos & PAGE_MASK) because the PAGE_MASK is |
| 1554 | * 0xfffff000 as an unsigned long, hence the result is incorrect |
| 1555 | * which could cause the following ASSERT failed in most cases. |
| 1556 | * In order to avoid this, we can evaluate the block_offset of the |
| 1557 | * start of the page by using shifts rather than masks the mismatch |
| 1558 | * problem. |
| 1559 | */ |
| 1560 | block_offset = (pos >> PAGE_CACHE_SHIFT) << PAGE_CACHE_SHIFT; |
| 1561 | |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1562 | ASSERT(block_offset + from == pos); |
| 1563 | |
| 1564 | head = page_buffers(page); |
| 1565 | block_start = 0; |
| 1566 | for (bh = head; bh != head || !block_start; |
| 1567 | bh = bh->b_this_page, block_start = block_end, |
| 1568 | block_offset += bh->b_size) { |
| 1569 | block_end = block_start + bh->b_size; |
| 1570 | |
| 1571 | /* skip buffers before the write */ |
| 1572 | if (block_end <= from) |
| 1573 | continue; |
| 1574 | |
| 1575 | /* if the buffer is after the write, we're done */ |
| 1576 | if (block_start >= to) |
| 1577 | break; |
| 1578 | |
| 1579 | if (!buffer_delay(bh)) |
| 1580 | continue; |
| 1581 | |
| 1582 | if (!buffer_new(bh) && block_offset < i_size_read(inode)) |
| 1583 | continue; |
| 1584 | |
| 1585 | xfs_vm_kill_delalloc_range(inode, block_offset, |
| 1586 | block_offset + bh->b_size); |
Dave Chinner | 4ab9ed5 | 2014-04-14 18:11:58 +1000 | [diff] [blame] | 1587 | |
| 1588 | /* |
| 1589 | * This buffer does not contain data anymore. make sure anyone |
| 1590 | * who finds it knows that for certain. |
| 1591 | */ |
| 1592 | clear_buffer_delay(bh); |
| 1593 | clear_buffer_uptodate(bh); |
| 1594 | clear_buffer_mapped(bh); |
| 1595 | clear_buffer_new(bh); |
| 1596 | clear_buffer_dirty(bh); |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | } |
| 1600 | |
| 1601 | /* |
| 1602 | * This used to call block_write_begin(), but it unlocks and releases the page |
| 1603 | * on error, and we need that page to be able to punch stale delalloc blocks out |
| 1604 | * on failure. hence we copy-n-waste it here and call xfs_vm_write_failed() at |
| 1605 | * the appropriate point. |
| 1606 | */ |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1607 | STATIC int |
Nick Piggin | d79689c | 2007-10-16 01:25:06 -0700 | [diff] [blame] | 1608 | xfs_vm_write_begin( |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1609 | struct file *file, |
Nick Piggin | d79689c | 2007-10-16 01:25:06 -0700 | [diff] [blame] | 1610 | struct address_space *mapping, |
| 1611 | loff_t pos, |
| 1612 | unsigned len, |
| 1613 | unsigned flags, |
| 1614 | struct page **pagep, |
| 1615 | void **fsdata) |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1616 | { |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1617 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; |
| 1618 | struct page *page; |
| 1619 | int status; |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 1620 | |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1621 | ASSERT(len <= PAGE_CACHE_SIZE); |
| 1622 | |
Dave Chinner | ad22c7a | 2013-10-29 22:11:57 +1100 | [diff] [blame] | 1623 | page = grab_cache_page_write_begin(mapping, index, flags); |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1624 | if (!page) |
| 1625 | return -ENOMEM; |
| 1626 | |
| 1627 | status = __block_write_begin(page, pos, len, xfs_get_blocks); |
| 1628 | if (unlikely(status)) { |
| 1629 | struct inode *inode = mapping->host; |
Dave Chinner | 72ab70a | 2014-04-14 18:13:29 +1000 | [diff] [blame] | 1630 | size_t isize = i_size_read(inode); |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1631 | |
| 1632 | xfs_vm_write_failed(inode, page, pos, len); |
| 1633 | unlock_page(page); |
| 1634 | |
Dave Chinner | 72ab70a | 2014-04-14 18:13:29 +1000 | [diff] [blame] | 1635 | /* |
| 1636 | * If the write is beyond EOF, we only want to kill blocks |
| 1637 | * allocated in this write, not blocks that were previously |
| 1638 | * written successfully. |
| 1639 | */ |
| 1640 | if (pos + len > isize) { |
| 1641 | ssize_t start = max_t(ssize_t, pos, isize); |
| 1642 | |
| 1643 | truncate_pagecache_range(inode, start, pos + len); |
| 1644 | } |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1645 | |
| 1646 | page_cache_release(page); |
| 1647 | page = NULL; |
| 1648 | } |
| 1649 | |
| 1650 | *pagep = page; |
| 1651 | return status; |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1652 | } |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 1653 | |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1654 | /* |
Dave Chinner | aad3f37 | 2014-04-14 18:14:11 +1000 | [diff] [blame] | 1655 | * On failure, we only need to kill delalloc blocks beyond EOF in the range of |
| 1656 | * this specific write because they will never be written. Previous writes |
| 1657 | * beyond EOF where block allocation succeeded do not need to be trashed, so |
| 1658 | * only new blocks from this write should be trashed. For blocks within |
| 1659 | * EOF, generic_write_end() zeros them so they are safe to leave alone and be |
| 1660 | * written with all the other valid data. |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1661 | */ |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1662 | STATIC int |
| 1663 | xfs_vm_write_end( |
| 1664 | struct file *file, |
| 1665 | struct address_space *mapping, |
| 1666 | loff_t pos, |
| 1667 | unsigned len, |
| 1668 | unsigned copied, |
| 1669 | struct page *page, |
| 1670 | void *fsdata) |
| 1671 | { |
| 1672 | int ret; |
| 1673 | |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1674 | ASSERT(len <= PAGE_CACHE_SIZE); |
| 1675 | |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1676 | ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata); |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1677 | if (unlikely(ret < len)) { |
| 1678 | struct inode *inode = mapping->host; |
| 1679 | size_t isize = i_size_read(inode); |
| 1680 | loff_t to = pos + len; |
| 1681 | |
| 1682 | if (to > isize) { |
Dave Chinner | aad3f37 | 2014-04-14 18:14:11 +1000 | [diff] [blame] | 1683 | /* only kill blocks in this write beyond EOF */ |
| 1684 | if (pos > isize) |
| 1685 | isize = pos; |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1686 | xfs_vm_kill_delalloc_range(inode, isize, to); |
Dave Chinner | aad3f37 | 2014-04-14 18:14:11 +1000 | [diff] [blame] | 1687 | truncate_pagecache_range(inode, isize, to); |
Dave Chinner | d3bc815 | 2012-04-27 19:45:21 +1000 | [diff] [blame] | 1688 | } |
| 1689 | } |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 1690 | return ret; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1691 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | |
| 1693 | STATIC sector_t |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1694 | xfs_vm_bmap( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | struct address_space *mapping, |
| 1696 | sector_t block) |
| 1697 | { |
| 1698 | struct inode *inode = (struct inode *)mapping->host; |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 1699 | struct xfs_inode *ip = XFS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | |
Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 1701 | trace_xfs_vm_bmap(XFS_I(inode)); |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 1702 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
Dave Chinner | 4bc1ea6 | 2012-11-12 22:53:56 +1100 | [diff] [blame] | 1703 | filemap_write_and_wait(mapping); |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 1704 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1705 | return generic_block_bmap(mapping, block, xfs_get_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | STATIC int |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1709 | xfs_vm_readpage( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | struct file *unused, |
| 1711 | struct page *page) |
| 1712 | { |
Dave Chinner | 121e213 | 2016-01-08 11:28:35 +1100 | [diff] [blame] | 1713 | trace_xfs_vm_readpage(page->mapping->host, 1); |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1714 | return mpage_readpage(page, xfs_get_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | STATIC int |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1718 | xfs_vm_readpages( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | struct file *unused, |
| 1720 | struct address_space *mapping, |
| 1721 | struct list_head *pages, |
| 1722 | unsigned nr_pages) |
| 1723 | { |
Dave Chinner | 121e213 | 2016-01-08 11:28:35 +1100 | [diff] [blame] | 1724 | trace_xfs_vm_readpages(mapping->host, nr_pages); |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 1725 | return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | } |
| 1727 | |
Dave Chinner | 22e757a | 2014-09-02 12:12:51 +1000 | [diff] [blame] | 1728 | /* |
| 1729 | * This is basically a copy of __set_page_dirty_buffers() with one |
| 1730 | * small tweak: buffers beyond EOF do not get marked dirty. If we mark them |
| 1731 | * dirty, we'll never be able to clean them because we don't write buffers |
| 1732 | * beyond EOF, and that means we can't invalidate pages that span EOF |
| 1733 | * that have been marked dirty. Further, the dirty state can leak into |
| 1734 | * the file interior if the file is extended, resulting in all sorts of |
| 1735 | * bad things happening as the state does not match the underlying data. |
| 1736 | * |
| 1737 | * XXX: this really indicates that bufferheads in XFS need to die. Warts like |
| 1738 | * this only exist because of bufferheads and how the generic code manages them. |
| 1739 | */ |
| 1740 | STATIC int |
| 1741 | xfs_vm_set_page_dirty( |
| 1742 | struct page *page) |
| 1743 | { |
| 1744 | struct address_space *mapping = page->mapping; |
| 1745 | struct inode *inode = mapping->host; |
| 1746 | loff_t end_offset; |
| 1747 | loff_t offset; |
| 1748 | int newly_dirty; |
Greg Thelen | c4843a7 | 2015-05-22 17:13:16 -0400 | [diff] [blame] | 1749 | struct mem_cgroup *memcg; |
Dave Chinner | 22e757a | 2014-09-02 12:12:51 +1000 | [diff] [blame] | 1750 | |
| 1751 | if (unlikely(!mapping)) |
| 1752 | return !TestSetPageDirty(page); |
| 1753 | |
| 1754 | end_offset = i_size_read(inode); |
| 1755 | offset = page_offset(page); |
| 1756 | |
| 1757 | spin_lock(&mapping->private_lock); |
| 1758 | if (page_has_buffers(page)) { |
| 1759 | struct buffer_head *head = page_buffers(page); |
| 1760 | struct buffer_head *bh = head; |
| 1761 | |
| 1762 | do { |
| 1763 | if (offset < end_offset) |
| 1764 | set_buffer_dirty(bh); |
| 1765 | bh = bh->b_this_page; |
| 1766 | offset += 1 << inode->i_blkbits; |
| 1767 | } while (bh != head); |
| 1768 | } |
Greg Thelen | c4843a7 | 2015-05-22 17:13:16 -0400 | [diff] [blame] | 1769 | /* |
| 1770 | * Use mem_group_begin_page_stat() to keep PageDirty synchronized with |
| 1771 | * per-memcg dirty page counters. |
| 1772 | */ |
| 1773 | memcg = mem_cgroup_begin_page_stat(page); |
Dave Chinner | 22e757a | 2014-09-02 12:12:51 +1000 | [diff] [blame] | 1774 | newly_dirty = !TestSetPageDirty(page); |
| 1775 | spin_unlock(&mapping->private_lock); |
| 1776 | |
| 1777 | if (newly_dirty) { |
| 1778 | /* sigh - __set_page_dirty() is static, so copy it here, too */ |
| 1779 | unsigned long flags; |
| 1780 | |
| 1781 | spin_lock_irqsave(&mapping->tree_lock, flags); |
| 1782 | if (page->mapping) { /* Race with truncate? */ |
| 1783 | WARN_ON_ONCE(!PageUptodate(page)); |
Greg Thelen | c4843a7 | 2015-05-22 17:13:16 -0400 | [diff] [blame] | 1784 | account_page_dirtied(page, mapping, memcg); |
Dave Chinner | 22e757a | 2014-09-02 12:12:51 +1000 | [diff] [blame] | 1785 | radix_tree_tag_set(&mapping->page_tree, |
| 1786 | page_index(page), PAGECACHE_TAG_DIRTY); |
| 1787 | } |
| 1788 | spin_unlock_irqrestore(&mapping->tree_lock, flags); |
Dave Chinner | 22e757a | 2014-09-02 12:12:51 +1000 | [diff] [blame] | 1789 | } |
Greg Thelen | c4843a7 | 2015-05-22 17:13:16 -0400 | [diff] [blame] | 1790 | mem_cgroup_end_page_stat(memcg); |
| 1791 | if (newly_dirty) |
| 1792 | __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); |
Dave Chinner | 22e757a | 2014-09-02 12:12:51 +1000 | [diff] [blame] | 1793 | return newly_dirty; |
| 1794 | } |
| 1795 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 1796 | const struct address_space_operations xfs_address_space_operations = { |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1797 | .readpage = xfs_vm_readpage, |
| 1798 | .readpages = xfs_vm_readpages, |
| 1799 | .writepage = xfs_vm_writepage, |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 1800 | .writepages = xfs_vm_writepages, |
Dave Chinner | 22e757a | 2014-09-02 12:12:51 +1000 | [diff] [blame] | 1801 | .set_page_dirty = xfs_vm_set_page_dirty, |
Nathan Scott | 238f4c5 | 2006-03-17 17:26:25 +1100 | [diff] [blame] | 1802 | .releasepage = xfs_vm_releasepage, |
| 1803 | .invalidatepage = xfs_vm_invalidatepage, |
Nick Piggin | d79689c | 2007-10-16 01:25:06 -0700 | [diff] [blame] | 1804 | .write_begin = xfs_vm_write_begin, |
Christoph Hellwig | fa9b227 | 2010-06-14 05:17:31 -0400 | [diff] [blame] | 1805 | .write_end = xfs_vm_write_end, |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1806 | .bmap = xfs_vm_bmap, |
| 1807 | .direct_IO = xfs_vm_direct_IO, |
Christoph Lameter | e965f96 | 2006-02-01 03:05:41 -0800 | [diff] [blame] | 1808 | .migratepage = buffer_migrate_page, |
Hisashi Hifumi | bddaafa | 2009-03-29 09:53:38 +0200 | [diff] [blame] | 1809 | .is_partially_uptodate = block_is_partially_uptodate, |
Andi Kleen | aa261f5 | 2009-09-16 11:50:16 +0200 | [diff] [blame] | 1810 | .error_remove_page = generic_error_remove_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | }; |