Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
Christoph Hellwig | 98c1a7c0 | 2018-07-11 22:26:06 -0700 | [diff] [blame] | 4 | * Copyright (c) 2016-2018 Christoph Hellwig. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include "xfs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 8 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 9 | #include "xfs_format.h" |
| 10 | #include "xfs_log_format.h" |
| 11 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "xfs_inode.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 14 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include "xfs_iomap.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 16 | #include "xfs_trace.h" |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 17 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 18 | #include "xfs_bmap_util.h" |
Darrick J. Wong | ef47366 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 19 | #include "xfs_reflink.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 21 | /* |
| 22 | * structure owned by writepages passed to individual writepage calls |
| 23 | */ |
| 24 | struct xfs_writepage_ctx { |
| 25 | struct xfs_bmbt_irec imap; |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 26 | int fork; |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 27 | unsigned int data_seq; |
Christoph Hellwig | e666aa3 | 2018-07-17 16:51:52 -0700 | [diff] [blame] | 28 | unsigned int cow_seq; |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 29 | struct xfs_ioend *ioend; |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 30 | }; |
| 31 | |
Ross Zwisler | 20a90f5 | 2016-02-26 15:19:52 -0800 | [diff] [blame] | 32 | struct block_device * |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 33 | xfs_find_bdev_for_inode( |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 34 | struct inode *inode) |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 35 | { |
Christoph Hellwig | 046f168 | 2010-04-28 12:28:52 +0000 | [diff] [blame] | 36 | struct xfs_inode *ip = XFS_I(inode); |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 37 | struct xfs_mount *mp = ip->i_mount; |
| 38 | |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 39 | if (XFS_IS_REALTIME_INODE(ip)) |
Christoph Hellwig | 6214ed4 | 2007-09-14 15:23:17 +1000 | [diff] [blame] | 40 | return mp->m_rtdev_targp->bt_bdev; |
| 41 | else |
| 42 | return mp->m_ddev_targp->bt_bdev; |
| 43 | } |
| 44 | |
Dan Williams | 486aff5 | 2017-08-24 15:12:50 -0700 | [diff] [blame] | 45 | struct dax_device * |
| 46 | xfs_find_daxdev_for_inode( |
| 47 | struct inode *inode) |
| 48 | { |
| 49 | struct xfs_inode *ip = XFS_I(inode); |
| 50 | struct xfs_mount *mp = ip->i_mount; |
| 51 | |
| 52 | if (XFS_IS_REALTIME_INODE(ip)) |
| 53 | return mp->m_rtdev_targp->bt_daxdev; |
| 54 | else |
| 55 | return mp->m_ddev_targp->bt_daxdev; |
| 56 | } |
| 57 | |
Christoph Hellwig | ac8ee54 | 2018-07-11 22:26:04 -0700 | [diff] [blame] | 58 | static void |
| 59 | xfs_finish_page_writeback( |
| 60 | struct inode *inode, |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 61 | struct bio_vec *bvec, |
Christoph Hellwig | ac8ee54 | 2018-07-11 22:26:04 -0700 | [diff] [blame] | 62 | int error) |
| 63 | { |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 64 | struct iomap_page *iop = to_iomap_page(bvec->bv_page); |
| 65 | |
Christoph Hellwig | ac8ee54 | 2018-07-11 22:26:04 -0700 | [diff] [blame] | 66 | if (error) { |
| 67 | SetPageError(bvec->bv_page); |
| 68 | mapping_set_error(inode->i_mapping, -EIO); |
| 69 | } |
Christoph Hellwig | ac8ee54 | 2018-07-11 22:26:04 -0700 | [diff] [blame] | 70 | |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 71 | ASSERT(iop || i_blocksize(inode) == PAGE_SIZE); |
| 72 | ASSERT(!iop || atomic_read(&iop->write_count) > 0); |
Dave Chinner | 37992c1 | 2016-04-06 08:12:28 +1000 | [diff] [blame] | 73 | |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 74 | if (!iop || atomic_dec_and_test(&iop->write_count)) |
Christoph Hellwig | 8353a81 | 2017-09-02 09:53:41 -0700 | [diff] [blame] | 75 | end_page_writeback(bvec->bv_page); |
Dave Chinner | 37992c1 | 2016-04-06 08:12:28 +1000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /* |
| 79 | * We're now finished for good with this ioend structure. Update the page |
| 80 | * state, release holds on bios, and finally free up memory. Do not use the |
| 81 | * ioend after this. |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 82 | */ |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 83 | STATIC void |
| 84 | xfs_destroy_ioend( |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 85 | struct xfs_ioend *ioend, |
| 86 | int error) |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 87 | { |
Dave Chinner | 37992c1 | 2016-04-06 08:12:28 +1000 | [diff] [blame] | 88 | struct inode *inode = ioend->io_inode; |
Christoph Hellwig | 8353a81 | 2017-09-02 09:53:41 -0700 | [diff] [blame] | 89 | struct bio *bio = &ioend->io_inline_bio; |
| 90 | struct bio *last = ioend->io_bio, *next; |
| 91 | u64 start = bio->bi_iter.bi_sector; |
| 92 | bool quiet = bio_flagged(bio, BIO_QUIET); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 93 | |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 94 | for (bio = &ioend->io_inline_bio; bio; bio = next) { |
Dave Chinner | 37992c1 | 2016-04-06 08:12:28 +1000 | [diff] [blame] | 95 | struct bio_vec *bvec; |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 96 | struct bvec_iter_all iter_all; |
Dave Chinner | 37992c1 | 2016-04-06 08:12:28 +1000 | [diff] [blame] | 97 | |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 98 | /* |
| 99 | * For the last bio, bi_private points to the ioend, so we |
| 100 | * need to explicitly end the iteration here. |
| 101 | */ |
| 102 | if (bio == last) |
| 103 | next = NULL; |
| 104 | else |
| 105 | next = bio->bi_private; |
Dave Chinner | 37992c1 | 2016-04-06 08:12:28 +1000 | [diff] [blame] | 106 | |
| 107 | /* walk each page on bio, ending page IO on them */ |
Christoph Hellwig | 2b070cf | 2019-04-25 09:03:00 +0200 | [diff] [blame] | 108 | bio_for_each_segment_all(bvec, bio, iter_all) |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 109 | xfs_finish_page_writeback(inode, bvec, error); |
Dave Chinner | 37992c1 | 2016-04-06 08:12:28 +1000 | [diff] [blame] | 110 | bio_put(bio); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 111 | } |
Christoph Hellwig | 8353a81 | 2017-09-02 09:53:41 -0700 | [diff] [blame] | 112 | |
| 113 | if (unlikely(error && !quiet)) { |
| 114 | xfs_err_ratelimited(XFS_I(inode)->i_mount, |
| 115 | "writeback error on sector %llu", start); |
| 116 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* |
Christoph Hellwig | fc0063c | 2011-08-23 08:28:11 +0000 | [diff] [blame] | 120 | * Fast and loose check if this write could update the on-disk inode size. |
| 121 | */ |
| 122 | static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend) |
| 123 | { |
| 124 | return ioend->io_offset + ioend->io_size > |
| 125 | XFS_I(ioend->io_inode)->i_d.di_size; |
| 126 | } |
| 127 | |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 128 | STATIC int |
| 129 | xfs_setfilesize_trans_alloc( |
| 130 | struct xfs_ioend *ioend) |
| 131 | { |
| 132 | struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount; |
| 133 | struct xfs_trans *tp; |
| 134 | int error; |
| 135 | |
Christoph Hellwig | 73d30d4 | 2019-06-28 19:31:38 -0700 | [diff] [blame] | 136 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 137 | if (error) |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 138 | return error; |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 139 | |
| 140 | ioend->io_append_trans = tp; |
| 141 | |
| 142 | /* |
Dave Chinner | 437a255 | 2012-11-28 13:01:00 +1100 | [diff] [blame] | 143 | * We may pass freeze protection with a transaction. So tell lockdep |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 144 | * we released it. |
| 145 | */ |
Oleg Nesterov | bee9182 | 2015-07-19 23:48:20 +0200 | [diff] [blame] | 146 | __sb_writers_release(ioend->io_inode->i_sb, SB_FREEZE_FS); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 147 | /* |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 148 | * We hand off the transaction to the completion thread now, so |
| 149 | * clear the flag here. |
| 150 | */ |
Michal Hocko | 9070733 | 2017-05-03 14:53:12 -0700 | [diff] [blame] | 151 | current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
Christoph Hellwig | fc0063c | 2011-08-23 08:28:11 +0000 | [diff] [blame] | 155 | /* |
Christoph Hellwig | 2813d68 | 2011-12-18 20:00:12 +0000 | [diff] [blame] | 156 | * 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] | 157 | */ |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 158 | STATIC int |
Christoph Hellwig | e372843 | 2016-09-19 11:26:41 +1000 | [diff] [blame] | 159 | __xfs_setfilesize( |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 160 | struct xfs_inode *ip, |
| 161 | struct xfs_trans *tp, |
| 162 | xfs_off_t offset, |
| 163 | size_t size) |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 164 | { |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 165 | xfs_fsize_t isize; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 166 | |
Christoph Hellwig | aa6bf01 | 2012-02-29 09:53:48 +0000 | [diff] [blame] | 167 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 168 | isize = xfs_new_eof(ip, offset + size); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 169 | if (!isize) { |
| 170 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 171 | xfs_trans_cancel(tp); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 172 | return 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 173 | } |
| 174 | |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 175 | trace_xfs_setfilesize(ip, offset, size); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 176 | |
| 177 | ip->i_d.di_size = isize; |
| 178 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 179 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 180 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 181 | return xfs_trans_commit(tp); |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 182 | } |
| 183 | |
Christoph Hellwig | e372843 | 2016-09-19 11:26:41 +1000 | [diff] [blame] | 184 | int |
| 185 | xfs_setfilesize( |
| 186 | struct xfs_inode *ip, |
| 187 | xfs_off_t offset, |
| 188 | size_t size) |
| 189 | { |
| 190 | struct xfs_mount *mp = ip->i_mount; |
| 191 | struct xfs_trans *tp; |
| 192 | int error; |
| 193 | |
| 194 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp); |
| 195 | if (error) |
| 196 | return error; |
| 197 | |
| 198 | return __xfs_setfilesize(ip, tp, offset, size); |
| 199 | } |
| 200 | |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 201 | STATIC int |
| 202 | xfs_setfilesize_ioend( |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 203 | struct xfs_ioend *ioend, |
| 204 | int error) |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 205 | { |
| 206 | struct xfs_inode *ip = XFS_I(ioend->io_inode); |
| 207 | struct xfs_trans *tp = ioend->io_append_trans; |
| 208 | |
| 209 | /* |
| 210 | * The transaction may have been allocated in the I/O submission thread, |
| 211 | * thus we need to mark ourselves as being in a transaction manually. |
| 212 | * Similarly for freeze protection. |
| 213 | */ |
Michal Hocko | 9070733 | 2017-05-03 14:53:12 -0700 | [diff] [blame] | 214 | current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); |
Oleg Nesterov | bee9182 | 2015-07-19 23:48:20 +0200 | [diff] [blame] | 215 | __sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS); |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 216 | |
Zhaohongjiang | 5cb13dc | 2015-10-12 15:28:39 +1100 | [diff] [blame] | 217 | /* we abort the update if there was an IO error */ |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 218 | if (error) { |
Zhaohongjiang | 5cb13dc | 2015-10-12 15:28:39 +1100 | [diff] [blame] | 219 | xfs_trans_cancel(tp); |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 220 | return error; |
Zhaohongjiang | 5cb13dc | 2015-10-12 15:28:39 +1100 | [diff] [blame] | 221 | } |
| 222 | |
Christoph Hellwig | e372843 | 2016-09-19 11:26:41 +1000 | [diff] [blame] | 223 | return __xfs_setfilesize(ip, tp, ioend->io_offset, ioend->io_size); |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 224 | } |
| 225 | |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 226 | /* |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 227 | * IO write completion. |
| 228 | */ |
| 229 | STATIC void |
Darrick J. Wong | cb357bf | 2019-04-15 13:13:20 -0700 | [diff] [blame] | 230 | xfs_end_ioend( |
| 231 | struct xfs_ioend *ioend) |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 232 | { |
Darrick J. Wong | 3994fc4 | 2019-04-15 13:13:21 -0700 | [diff] [blame] | 233 | struct list_head ioend_list; |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 234 | struct xfs_inode *ip = XFS_I(ioend->io_inode); |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 235 | xfs_off_t offset = ioend->io_offset; |
| 236 | size_t size = ioend->io_size; |
Christoph Hellwig | 73d30d4 | 2019-06-28 19:31:38 -0700 | [diff] [blame] | 237 | unsigned int nofs_flag; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 238 | int error; |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 239 | |
Brian Foster | af055e3 | 2016-02-08 15:00:02 +1100 | [diff] [blame] | 240 | /* |
Christoph Hellwig | 73d30d4 | 2019-06-28 19:31:38 -0700 | [diff] [blame] | 241 | * We can allocate memory here while doing writeback on behalf of |
| 242 | * memory reclaim. To avoid memory allocation deadlocks set the |
| 243 | * task-wide nofs context for the following operations. |
| 244 | */ |
| 245 | nofs_flag = memalloc_nofs_save(); |
| 246 | |
| 247 | /* |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 248 | * Just clean up the in-memory strutures if the fs has been shut down. |
Brian Foster | af055e3 | 2016-02-08 15:00:02 +1100 | [diff] [blame] | 249 | */ |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 250 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 251 | error = -EIO; |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 252 | goto done; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 256 | * Clean up any COW blocks on an I/O error. |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 257 | */ |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 258 | error = blk_status_to_errno(ioend->io_bio->bi_status); |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 259 | if (unlikely(error)) { |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 260 | if (ioend->io_fork == XFS_COW_FORK) |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 261 | xfs_reflink_cancel_cow_range(ip, offset, size, true); |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 262 | goto done; |
| 263 | } |
| 264 | |
| 265 | /* |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 266 | * Success: commit the COW or unwritten blocks if needed. |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 267 | */ |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 268 | if (ioend->io_fork == XFS_COW_FORK) |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 269 | error = xfs_reflink_end_cow(ip, offset, size); |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 270 | else if (ioend->io_state == XFS_EXT_UNWRITTEN) |
Eryu Guan | ee70daa | 2017-09-21 11:26:18 -0700 | [diff] [blame] | 271 | error = xfs_iomap_write_unwritten(ip, offset, size, false); |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 272 | else |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 273 | ASSERT(!xfs_ioend_is_append(ioend) || ioend->io_append_trans); |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 274 | |
Christoph Hellwig | 04f658e | 2011-08-24 05:59:25 +0000 | [diff] [blame] | 275 | done: |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 276 | if (ioend->io_append_trans) |
| 277 | error = xfs_setfilesize_ioend(ioend, error); |
Darrick J. Wong | 3994fc4 | 2019-04-15 13:13:21 -0700 | [diff] [blame] | 278 | list_replace_init(&ioend->io_list, &ioend_list); |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 279 | xfs_destroy_ioend(ioend, error); |
Darrick J. Wong | 3994fc4 | 2019-04-15 13:13:21 -0700 | [diff] [blame] | 280 | |
| 281 | while (!list_empty(&ioend_list)) { |
| 282 | ioend = list_first_entry(&ioend_list, struct xfs_ioend, |
| 283 | io_list); |
| 284 | list_del_init(&ioend->io_list); |
| 285 | xfs_destroy_ioend(ioend, error); |
| 286 | } |
Christoph Hellwig | 73d30d4 | 2019-06-28 19:31:38 -0700 | [diff] [blame] | 287 | |
| 288 | memalloc_nofs_restore(nofs_flag); |
Darrick J. Wong | 3994fc4 | 2019-04-15 13:13:21 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* |
| 292 | * We can merge two adjacent ioends if they have the same set of work to do. |
| 293 | */ |
| 294 | static bool |
| 295 | xfs_ioend_can_merge( |
| 296 | struct xfs_ioend *ioend, |
Darrick J. Wong | 3994fc4 | 2019-04-15 13:13:21 -0700 | [diff] [blame] | 297 | struct xfs_ioend *next) |
| 298 | { |
Christoph Hellwig | fe64e0d | 2019-06-28 19:31:37 -0700 | [diff] [blame] | 299 | if (ioend->io_bio->bi_status != next->io_bio->bi_status) |
Darrick J. Wong | 3994fc4 | 2019-04-15 13:13:21 -0700 | [diff] [blame] | 300 | return false; |
| 301 | if ((ioend->io_fork == XFS_COW_FORK) ^ (next->io_fork == XFS_COW_FORK)) |
| 302 | return false; |
| 303 | if ((ioend->io_state == XFS_EXT_UNWRITTEN) ^ |
| 304 | (next->io_state == XFS_EXT_UNWRITTEN)) |
| 305 | return false; |
| 306 | if (ioend->io_offset + ioend->io_size != next->io_offset) |
| 307 | return false; |
Darrick J. Wong | 3994fc4 | 2019-04-15 13:13:21 -0700 | [diff] [blame] | 308 | return true; |
| 309 | } |
| 310 | |
Christoph Hellwig | 7dbae9f | 2019-06-28 19:31:37 -0700 | [diff] [blame] | 311 | /* |
| 312 | * If the to be merged ioend has a preallocated transaction for file |
| 313 | * size updates we need to ensure the ioend it is merged into also |
| 314 | * has one. If it already has one we can simply cancel the transaction |
| 315 | * as it is guaranteed to be clean. |
| 316 | */ |
| 317 | static void |
| 318 | xfs_ioend_merge_append_transactions( |
| 319 | struct xfs_ioend *ioend, |
| 320 | struct xfs_ioend *next) |
| 321 | { |
| 322 | if (!ioend->io_append_trans) { |
| 323 | ioend->io_append_trans = next->io_append_trans; |
| 324 | next->io_append_trans = NULL; |
| 325 | } else { |
| 326 | xfs_setfilesize_ioend(next, -ECANCELED); |
| 327 | } |
| 328 | } |
| 329 | |
Darrick J. Wong | 3994fc4 | 2019-04-15 13:13:21 -0700 | [diff] [blame] | 330 | /* Try to merge adjacent completions. */ |
| 331 | STATIC void |
| 332 | xfs_ioend_try_merge( |
| 333 | struct xfs_ioend *ioend, |
| 334 | struct list_head *more_ioends) |
| 335 | { |
| 336 | struct xfs_ioend *next_ioend; |
Darrick J. Wong | 3994fc4 | 2019-04-15 13:13:21 -0700 | [diff] [blame] | 337 | |
| 338 | while (!list_empty(more_ioends)) { |
| 339 | next_ioend = list_first_entry(more_ioends, struct xfs_ioend, |
| 340 | io_list); |
Christoph Hellwig | fe64e0d | 2019-06-28 19:31:37 -0700 | [diff] [blame] | 341 | if (!xfs_ioend_can_merge(ioend, next_ioend)) |
Darrick J. Wong | 3994fc4 | 2019-04-15 13:13:21 -0700 | [diff] [blame] | 342 | break; |
| 343 | list_move_tail(&next_ioend->io_list, &ioend->io_list); |
| 344 | ioend->io_size += next_ioend->io_size; |
Christoph Hellwig | 7dbae9f | 2019-06-28 19:31:37 -0700 | [diff] [blame] | 345 | if (next_ioend->io_append_trans) |
| 346 | xfs_ioend_merge_append_transactions(ioend, next_ioend); |
Darrick J. Wong | 3994fc4 | 2019-04-15 13:13:21 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
| 350 | /* list_sort compare function for ioends */ |
| 351 | static int |
| 352 | xfs_ioend_compare( |
| 353 | void *priv, |
| 354 | struct list_head *a, |
| 355 | struct list_head *b) |
| 356 | { |
| 357 | struct xfs_ioend *ia; |
| 358 | struct xfs_ioend *ib; |
| 359 | |
| 360 | ia = container_of(a, struct xfs_ioend, io_list); |
| 361 | ib = container_of(b, struct xfs_ioend, io_list); |
| 362 | if (ia->io_offset < ib->io_offset) |
| 363 | return -1; |
| 364 | else if (ia->io_offset > ib->io_offset) |
| 365 | return 1; |
| 366 | return 0; |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Darrick J. Wong | cb357bf | 2019-04-15 13:13:20 -0700 | [diff] [blame] | 369 | /* Finish all pending io completions. */ |
| 370 | void |
| 371 | xfs_end_io( |
| 372 | struct work_struct *work) |
| 373 | { |
| 374 | struct xfs_inode *ip; |
| 375 | struct xfs_ioend *ioend; |
| 376 | struct list_head completion_list; |
| 377 | unsigned long flags; |
| 378 | |
| 379 | ip = container_of(work, struct xfs_inode, i_ioend_work); |
| 380 | |
| 381 | spin_lock_irqsave(&ip->i_ioend_lock, flags); |
| 382 | list_replace_init(&ip->i_ioend_list, &completion_list); |
| 383 | spin_unlock_irqrestore(&ip->i_ioend_lock, flags); |
| 384 | |
Darrick J. Wong | 3994fc4 | 2019-04-15 13:13:21 -0700 | [diff] [blame] | 385 | list_sort(NULL, &completion_list, xfs_ioend_compare); |
| 386 | |
Darrick J. Wong | cb357bf | 2019-04-15 13:13:20 -0700 | [diff] [blame] | 387 | while (!list_empty(&completion_list)) { |
| 388 | ioend = list_first_entry(&completion_list, struct xfs_ioend, |
| 389 | io_list); |
| 390 | list_del_init(&ioend->io_list); |
Darrick J. Wong | 3994fc4 | 2019-04-15 13:13:21 -0700 | [diff] [blame] | 391 | xfs_ioend_try_merge(ioend, &completion_list); |
Darrick J. Wong | cb357bf | 2019-04-15 13:13:20 -0700 | [diff] [blame] | 392 | xfs_end_ioend(ioend); |
| 393 | } |
| 394 | } |
| 395 | |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 396 | STATIC void |
| 397 | xfs_end_bio( |
| 398 | struct bio *bio) |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 399 | { |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 400 | struct xfs_ioend *ioend = bio->bi_private; |
Darrick J. Wong | cb357bf | 2019-04-15 13:13:20 -0700 | [diff] [blame] | 401 | struct xfs_inode *ip = XFS_I(ioend->io_inode); |
| 402 | struct xfs_mount *mp = ip->i_mount; |
| 403 | unsigned long flags; |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 404 | |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 405 | if (ioend->io_fork == XFS_COW_FORK || |
Darrick J. Wong | cb357bf | 2019-04-15 13:13:20 -0700 | [diff] [blame] | 406 | ioend->io_state == XFS_EXT_UNWRITTEN || |
| 407 | ioend->io_append_trans != NULL) { |
| 408 | spin_lock_irqsave(&ip->i_ioend_lock, flags); |
| 409 | if (list_empty(&ip->i_ioend_list)) |
| 410 | WARN_ON_ONCE(!queue_work(mp->m_unwritten_workqueue, |
| 411 | &ip->i_ioend_work)); |
| 412 | list_add_tail(&ioend->io_list, &ip->i_ioend_list); |
| 413 | spin_unlock_irqrestore(&ip->i_ioend_lock, flags); |
| 414 | } else |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 415 | xfs_destroy_ioend(ioend, blk_status_to_errno(bio->bi_status)); |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 416 | } |
| 417 | |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 418 | /* |
| 419 | * Fast revalidation of the cached writeback mapping. Return true if the current |
| 420 | * mapping is valid, false otherwise. |
| 421 | */ |
| 422 | static bool |
| 423 | xfs_imap_valid( |
| 424 | struct xfs_writepage_ctx *wpc, |
| 425 | struct xfs_inode *ip, |
| 426 | xfs_fileoff_t offset_fsb) |
| 427 | { |
| 428 | if (offset_fsb < wpc->imap.br_startoff || |
| 429 | offset_fsb >= wpc->imap.br_startoff + wpc->imap.br_blockcount) |
| 430 | return false; |
| 431 | /* |
| 432 | * If this is a COW mapping, it is sufficient to check that the mapping |
| 433 | * covers the offset. Be careful to check this first because the caller |
| 434 | * can revalidate a COW mapping without updating the data seqno. |
| 435 | */ |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 436 | if (wpc->fork == XFS_COW_FORK) |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 437 | return true; |
| 438 | |
| 439 | /* |
| 440 | * This is not a COW mapping. Check the sequence number of the data fork |
| 441 | * because concurrent changes could have invalidated the extent. Check |
| 442 | * the COW fork because concurrent changes since the last time we |
| 443 | * checked (and found nothing at this offset) could have added |
| 444 | * overlapping blocks. |
| 445 | */ |
| 446 | if (wpc->data_seq != READ_ONCE(ip->i_df.if_seq)) |
| 447 | return false; |
| 448 | if (xfs_inode_has_cow_data(ip) && |
| 449 | wpc->cow_seq != READ_ONCE(ip->i_cowfp->if_seq)) |
| 450 | return false; |
| 451 | return true; |
| 452 | } |
| 453 | |
Christoph Hellwig | 4ad765e | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 454 | /* |
| 455 | * Pass in a dellalloc extent and convert it to real extents, return the real |
| 456 | * extent that maps offset_fsb in wpc->imap. |
| 457 | * |
| 458 | * The current page is held locked so nothing could have removed the block |
Christoph Hellwig | 7588cbe | 2019-02-15 08:02:50 -0800 | [diff] [blame] | 459 | * backing offset_fsb, although it could have moved from the COW to the data |
| 460 | * fork by another thread. |
Christoph Hellwig | 4ad765e | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 461 | */ |
| 462 | static int |
| 463 | xfs_convert_blocks( |
| 464 | struct xfs_writepage_ctx *wpc, |
| 465 | struct xfs_inode *ip, |
| 466 | xfs_fileoff_t offset_fsb) |
| 467 | { |
| 468 | int error; |
| 469 | |
| 470 | /* |
| 471 | * Attempt to allocate whatever delalloc extent currently backs |
| 472 | * offset_fsb and put the result into wpc->imap. Allocate in a loop |
| 473 | * because it may take several attempts to allocate real blocks for a |
| 474 | * contiguous delalloc extent if free space is sufficiently fragmented. |
| 475 | */ |
| 476 | do { |
| 477 | error = xfs_bmapi_convert_delalloc(ip, wpc->fork, offset_fsb, |
| 478 | &wpc->imap, wpc->fork == XFS_COW_FORK ? |
| 479 | &wpc->cow_seq : &wpc->data_seq); |
| 480 | if (error) |
| 481 | return error; |
| 482 | } while (wpc->imap.br_startoff + wpc->imap.br_blockcount <= offset_fsb); |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | STATIC int |
| 488 | xfs_map_blocks( |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 489 | struct xfs_writepage_ctx *wpc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | struct inode *inode, |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 491 | loff_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | { |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 493 | struct xfs_inode *ip = XFS_I(inode); |
| 494 | struct xfs_mount *mp = ip->i_mount; |
Fabian Frederick | 9340747 | 2017-02-27 14:28:32 -0800 | [diff] [blame] | 495 | ssize_t count = i_blocksize(inode); |
Christoph Hellwig | b4e2903 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 496 | xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 497 | xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count); |
Christoph Hellwig | e666aa3 | 2018-07-17 16:51:52 -0700 | [diff] [blame] | 498 | xfs_fileoff_t cow_fsb = NULLFILEOFF; |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 499 | struct xfs_bmbt_irec imap; |
Christoph Hellwig | 060d4ea | 2018-07-11 22:26:01 -0700 | [diff] [blame] | 500 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 7588cbe | 2019-02-15 08:02:50 -0800 | [diff] [blame] | 501 | int retries = 0; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 502 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 504 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 505 | return -EIO; |
| 506 | |
Christoph Hellwig | 889c65b | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 507 | /* |
Christoph Hellwig | 889c65b | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 508 | * COW fork blocks can overlap data fork blocks even if the blocks |
| 509 | * aren't shared. COW I/O always takes precedent, so we must always |
| 510 | * check for overlap on reflink inodes unless the mapping is already a |
Christoph Hellwig | e666aa3 | 2018-07-17 16:51:52 -0700 | [diff] [blame] | 511 | * COW one, or the COW fork hasn't changed from the last time we looked |
| 512 | * at it. |
| 513 | * |
| 514 | * It's safe to check the COW fork if_seq here without the ILOCK because |
| 515 | * we've indirectly protected against concurrent updates: writeback has |
| 516 | * the page locked, which prevents concurrent invalidations by reflink |
| 517 | * and directio and prevents concurrent buffered writes to the same |
| 518 | * page. Changes to if_seq always happen under i_lock, which protects |
| 519 | * against concurrent updates and provides a memory barrier on the way |
| 520 | * out that ensures that we always see the current value. |
Christoph Hellwig | 889c65b | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 521 | */ |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 522 | if (xfs_imap_valid(wpc, ip, offset_fsb)) |
Christoph Hellwig | 889c65b | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 523 | return 0; |
| 524 | |
Christoph Hellwig | 889c65b | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 525 | /* |
| 526 | * If we don't have a valid map, now it's time to get a new one for this |
| 527 | * offset. This will convert delayed allocations (including COW ones) |
| 528 | * into real extents. If we return without a valid map, it means we |
| 529 | * landed in a hole and we skip the block. |
| 530 | */ |
Christoph Hellwig | 7588cbe | 2019-02-15 08:02:50 -0800 | [diff] [blame] | 531 | retry: |
Dave Chinner | 988ef92 | 2016-02-15 17:20:50 +1100 | [diff] [blame] | 532 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 533 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
| 534 | (ip->i_df.if_flags & XFS_IFEXTENTS)); |
Christoph Hellwig | 060d4ea | 2018-07-11 22:26:01 -0700 | [diff] [blame] | 535 | |
| 536 | /* |
| 537 | * Check if this is offset is covered by a COW extents, and if yes use |
| 538 | * it directly instead of looking up anything in the data fork. |
| 539 | */ |
Christoph Hellwig | 51d6269 | 2018-07-17 16:51:51 -0700 | [diff] [blame] | 540 | if (xfs_inode_has_cow_data(ip) && |
Christoph Hellwig | e666aa3 | 2018-07-17 16:51:52 -0700 | [diff] [blame] | 541 | xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &imap)) |
| 542 | cow_fsb = imap.br_startoff; |
| 543 | if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) { |
Christoph Hellwig | 2ba090d | 2018-08-07 10:57:12 -0700 | [diff] [blame] | 544 | wpc->cow_seq = READ_ONCE(ip->i_cowfp->if_seq); |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 545 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 546 | |
| 547 | wpc->fork = XFS_COW_FORK; |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 548 | goto allocate_blocks; |
| 549 | } |
| 550 | |
| 551 | /* |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 552 | * No COW extent overlap. Revalidate now that we may have updated |
| 553 | * ->cow_seq. If the data mapping is still valid, we're done. |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 554 | */ |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 555 | if (xfs_imap_valid(wpc, ip, offset_fsb)) { |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 556 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * If we don't have a valid map, now it's time to get a new one for this |
| 562 | * offset. This will convert delayed allocations (including COW ones) |
| 563 | * into real extents. |
| 564 | */ |
Christoph Hellwig | 3345746 | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 565 | if (!xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap)) |
| 566 | imap.br_startoff = end_fsb; /* fake a hole past EOF */ |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 567 | wpc->data_seq = READ_ONCE(ip->i_df.if_seq); |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 568 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 569 | |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 570 | wpc->fork = XFS_DATA_FORK; |
| 571 | |
Christoph Hellwig | 12df89f | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 572 | /* landed in a hole or beyond EOF? */ |
Christoph Hellwig | 3345746 | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 573 | if (imap.br_startoff > offset_fsb) { |
Christoph Hellwig | 3345746 | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 574 | imap.br_blockcount = imap.br_startoff - offset_fsb; |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 575 | imap.br_startoff = offset_fsb; |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 576 | imap.br_startblock = HOLESTARTBLOCK; |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 577 | imap.br_state = XFS_EXT_NORM; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Christoph Hellwig | 12df89f | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 580 | /* |
| 581 | * Truncate to the next COW extent if there is one. This is the only |
| 582 | * opportunity to do this because we can skip COW fork lookups for the |
| 583 | * subsequent blocks in the mapping; however, the requirement to treat |
| 584 | * the COW range separately remains. |
| 585 | */ |
| 586 | if (cow_fsb != NULLFILEOFF && |
| 587 | cow_fsb < imap.br_startoff + imap.br_blockcount) |
| 588 | imap.br_blockcount = cow_fsb - imap.br_startoff; |
| 589 | |
| 590 | /* got a delalloc extent? */ |
| 591 | if (imap.br_startblock != HOLESTARTBLOCK && |
| 592 | isnullstartblock(imap.br_startblock)) |
| 593 | goto allocate_blocks; |
| 594 | |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 595 | wpc->imap = imap; |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 596 | trace_xfs_map_blocks_found(ip, offset, count, wpc->fork, &imap); |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 597 | return 0; |
| 598 | allocate_blocks: |
Christoph Hellwig | 4ad765e | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 599 | error = xfs_convert_blocks(wpc, ip, offset_fsb); |
Christoph Hellwig | 7588cbe | 2019-02-15 08:02:50 -0800 | [diff] [blame] | 600 | if (error) { |
| 601 | /* |
| 602 | * If we failed to find the extent in the COW fork we might have |
| 603 | * raced with a COW to data fork conversion or truncate. |
| 604 | * Restart the lookup to catch the extent in the data fork for |
| 605 | * the former case, but prevent additional retries to avoid |
| 606 | * looping forever for the latter case. |
| 607 | */ |
| 608 | if (error == -EAGAIN && wpc->fork == XFS_COW_FORK && !retries++) |
| 609 | goto retry; |
| 610 | ASSERT(error != -EAGAIN); |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 611 | return error; |
Christoph Hellwig | 7588cbe | 2019-02-15 08:02:50 -0800 | [diff] [blame] | 612 | } |
Christoph Hellwig | 4ad765e | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 613 | |
| 614 | /* |
| 615 | * Due to merging the return real extent might be larger than the |
| 616 | * original delalloc one. Trim the return extent to the next COW |
| 617 | * boundary again to force a re-lookup. |
| 618 | */ |
| 619 | if (wpc->fork != XFS_COW_FORK && cow_fsb != NULLFILEOFF && |
| 620 | cow_fsb < wpc->imap.br_startoff + wpc->imap.br_blockcount) |
| 621 | wpc->imap.br_blockcount = cow_fsb - wpc->imap.br_startoff; |
| 622 | |
| 623 | ASSERT(wpc->imap.br_startoff <= offset_fsb); |
| 624 | ASSERT(wpc->imap.br_startoff + wpc->imap.br_blockcount > offset_fsb); |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 625 | trace_xfs_map_blocks_alloc(ip, offset, count, wpc->fork, &imap); |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 626 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 629 | /* |
Dave Chinner | bb18782 | 2016-04-06 08:11:25 +1000 | [diff] [blame] | 630 | * Submit the bio for an ioend. We are passed an ioend with a bio attached to |
| 631 | * it, and we submit that bio. The ioend may be used for multiple bio |
| 632 | * submissions, so we only want to allocate an append transaction for the ioend |
| 633 | * once. In the case of multiple bio submission, each bio will take an IO |
| 634 | * reference to the ioend to ensure that the ioend completion is only done once |
| 635 | * all bios have been submitted and the ioend is really done. |
Dave Chinner | 7bf7f35 | 2012-11-12 22:09:45 +1100 | [diff] [blame] | 636 | * |
Christoph Hellwig | 0290d9c | 2019-06-28 19:31:36 -0700 | [diff] [blame] | 637 | * If @status is non-zero, it means that we have a situation where some part of |
Dave Chinner | 7bf7f35 | 2012-11-12 22:09:45 +1100 | [diff] [blame] | 638 | * the submission process has failed after we have marked paged for writeback |
Dave Chinner | bb18782 | 2016-04-06 08:11:25 +1000 | [diff] [blame] | 639 | * and unlocked them. In this situation, we need to fail the bio and ioend |
| 640 | * rather than submit it to IO. This typically only happens on a filesystem |
| 641 | * shutdown. |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 642 | */ |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 643 | STATIC int |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 644 | xfs_submit_ioend( |
Christoph Hellwig | 06342cf | 2009-10-30 09:09:15 +0000 | [diff] [blame] | 645 | struct writeback_control *wbc, |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 646 | struct xfs_ioend *ioend, |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 647 | int status) |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 648 | { |
Christoph Hellwig | 73d30d4 | 2019-06-28 19:31:38 -0700 | [diff] [blame] | 649 | unsigned int nofs_flag; |
| 650 | |
| 651 | /* |
| 652 | * We can allocate memory here while doing writeback on behalf of |
| 653 | * memory reclaim. To avoid memory allocation deadlocks set the |
| 654 | * task-wide nofs context for the following operations. |
| 655 | */ |
| 656 | nofs_flag = memalloc_nofs_save(); |
| 657 | |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 658 | /* Convert CoW extents to regular */ |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 659 | if (!status && ioend->io_fork == XFS_COW_FORK) { |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 660 | status = xfs_reflink_convert_cow(XFS_I(ioend->io_inode), |
| 661 | ioend->io_offset, ioend->io_size); |
| 662 | } |
| 663 | |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 664 | /* Reserve log space if we might write beyond the on-disk inode size. */ |
| 665 | if (!status && |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 666 | (ioend->io_fork == XFS_COW_FORK || |
| 667 | ioend->io_state != XFS_EXT_UNWRITTEN) && |
Dave Chinner | bb18782 | 2016-04-06 08:11:25 +1000 | [diff] [blame] | 668 | xfs_ioend_is_append(ioend) && |
| 669 | !ioend->io_append_trans) |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 670 | status = xfs_setfilesize_trans_alloc(ioend); |
Dave Chinner | bb18782 | 2016-04-06 08:11:25 +1000 | [diff] [blame] | 671 | |
Christoph Hellwig | 73d30d4 | 2019-06-28 19:31:38 -0700 | [diff] [blame] | 672 | memalloc_nofs_restore(nofs_flag); |
| 673 | |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 674 | ioend->io_bio->bi_private = ioend; |
| 675 | ioend->io_bio->bi_end_io = xfs_end_bio; |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 676 | |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 677 | /* |
| 678 | * If we are failing the IO now, just mark the ioend with an |
| 679 | * error and finish it. This will run IO completion immediately |
| 680 | * as there is only one reference to the ioend at this point in |
| 681 | * time. |
| 682 | */ |
| 683 | if (status) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 684 | ioend->io_bio->bi_status = errno_to_blk_status(status); |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 685 | bio_endio(ioend->io_bio); |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 686 | return status; |
| 687 | } |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 688 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 689 | submit_bio(ioend->io_bio); |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 690 | return 0; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 691 | } |
| 692 | |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 693 | static struct xfs_ioend * |
| 694 | xfs_alloc_ioend( |
| 695 | struct inode *inode, |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 696 | int fork, |
| 697 | xfs_exntst_t state, |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 698 | xfs_off_t offset, |
Christoph Hellwig | 3faed66 | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 699 | struct block_device *bdev, |
Christoph Hellwig | a247373 | 2019-06-28 19:30:22 -0700 | [diff] [blame] | 700 | sector_t sector, |
| 701 | struct writeback_control *wbc) |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 702 | { |
| 703 | struct xfs_ioend *ioend; |
| 704 | struct bio *bio; |
| 705 | |
Kent Overstreet | e292d7b | 2018-05-20 18:25:57 -0400 | [diff] [blame] | 706 | bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &xfs_ioend_bioset); |
Christoph Hellwig | 3faed66 | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 707 | bio_set_dev(bio, bdev); |
| 708 | bio->bi_iter.bi_sector = sector; |
Christoph Hellwig | a247373 | 2019-06-28 19:30:22 -0700 | [diff] [blame] | 709 | bio->bi_opf = REQ_OP_WRITE | wbc_to_write_flags(wbc); |
| 710 | bio->bi_write_hint = inode->i_write_hint; |
Christoph Hellwig | adfb5fb | 2019-06-28 19:30:22 -0700 | [diff] [blame] | 711 | wbc_init_bio(wbc, bio); |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 712 | |
| 713 | ioend = container_of(bio, struct xfs_ioend, io_inline_bio); |
| 714 | INIT_LIST_HEAD(&ioend->io_list); |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 715 | ioend->io_fork = fork; |
| 716 | ioend->io_state = state; |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 717 | ioend->io_inode = inode; |
| 718 | ioend->io_size = 0; |
| 719 | ioend->io_offset = offset; |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 720 | ioend->io_append_trans = NULL; |
| 721 | ioend->io_bio = bio; |
| 722 | return ioend; |
| 723 | } |
| 724 | |
| 725 | /* |
| 726 | * Allocate a new bio, and chain the old bio to the new one. |
| 727 | * |
| 728 | * Note that we have to do perform the chaining in this unintuitive order |
| 729 | * so that the bi_private linkage is set up in the right direction for the |
| 730 | * traversal in xfs_destroy_ioend(). |
| 731 | */ |
Christoph Hellwig | a247373 | 2019-06-28 19:30:22 -0700 | [diff] [blame] | 732 | static struct bio * |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 733 | xfs_chain_bio( |
Christoph Hellwig | a247373 | 2019-06-28 19:30:22 -0700 | [diff] [blame] | 734 | struct bio *prev) |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 735 | { |
| 736 | struct bio *new; |
| 737 | |
| 738 | new = bio_alloc(GFP_NOFS, BIO_MAX_PAGES); |
Christoph Hellwig | adfb5fb | 2019-06-28 19:30:22 -0700 | [diff] [blame] | 739 | bio_copy_dev(new, prev);/* also copies over blkcg information */ |
Christoph Hellwig | a247373 | 2019-06-28 19:30:22 -0700 | [diff] [blame] | 740 | new->bi_iter.bi_sector = bio_end_sector(prev); |
| 741 | new->bi_opf = prev->bi_opf; |
| 742 | new->bi_write_hint = prev->bi_write_hint; |
| 743 | |
| 744 | bio_chain(prev, new); |
| 745 | bio_get(prev); /* for xfs_destroy_ioend */ |
| 746 | submit_bio(prev); |
| 747 | return new; |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 748 | } |
| 749 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 750 | /* |
Christoph Hellwig | 3faed66 | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 751 | * Test to see if we have an existing ioend structure that we could append to |
| 752 | * first, otherwise finish off the current ioend and start another. |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 753 | */ |
| 754 | STATIC void |
| 755 | xfs_add_to_ioend( |
| 756 | struct inode *inode, |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 757 | xfs_off_t offset, |
Christoph Hellwig | 3faed66 | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 758 | struct page *page, |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 759 | struct iomap_page *iop, |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 760 | struct xfs_writepage_ctx *wpc, |
Dave Chinner | bb18782 | 2016-04-06 08:11:25 +1000 | [diff] [blame] | 761 | struct writeback_control *wbc, |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 762 | struct list_head *iolist) |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 763 | { |
Christoph Hellwig | 3faed66 | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 764 | struct xfs_inode *ip = XFS_I(inode); |
| 765 | struct xfs_mount *mp = ip->i_mount; |
| 766 | struct block_device *bdev = xfs_find_bdev_for_inode(inode); |
| 767 | unsigned len = i_blocksize(inode); |
| 768 | unsigned poff = offset & (PAGE_SIZE - 1); |
Christoph Hellwig | ff89673 | 2019-06-17 11:14:11 +0200 | [diff] [blame] | 769 | bool merged, same_page = false; |
Christoph Hellwig | 3faed66 | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 770 | sector_t sector; |
| 771 | |
| 772 | sector = xfs_fsb_to_db(ip, wpc->imap.br_startblock) + |
| 773 | ((offset - XFS_FSB_TO_B(mp, wpc->imap.br_startoff)) >> 9); |
| 774 | |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 775 | if (!wpc->ioend || |
| 776 | wpc->fork != wpc->ioend->io_fork || |
| 777 | wpc->imap.br_state != wpc->ioend->io_state || |
Christoph Hellwig | 3faed66 | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 778 | sector != bio_end_sector(wpc->ioend->io_bio) || |
Darrick J. Wong | 0df61da | 2016-03-07 09:32:14 +1100 | [diff] [blame] | 779 | offset != wpc->ioend->io_offset + wpc->ioend->io_size) { |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 780 | if (wpc->ioend) |
| 781 | list_add(&wpc->ioend->io_list, iolist); |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 782 | wpc->ioend = xfs_alloc_ioend(inode, wpc->fork, |
Christoph Hellwig | a247373 | 2019-06-28 19:30:22 -0700 | [diff] [blame] | 783 | wpc->imap.br_state, offset, bdev, sector, wbc); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 784 | } |
| 785 | |
Christoph Hellwig | ff89673 | 2019-06-17 11:14:11 +0200 | [diff] [blame] | 786 | merged = __bio_try_merge_page(wpc->ioend->io_bio, page, len, poff, |
| 787 | &same_page); |
| 788 | |
| 789 | if (iop && !same_page) |
| 790 | atomic_inc(&iop->write_count); |
| 791 | |
| 792 | if (!merged) { |
Ming Lei | 79d08f8 | 2019-07-01 15:14:46 +0800 | [diff] [blame] | 793 | if (bio_full(wpc->ioend->io_bio, len)) |
Christoph Hellwig | a247373 | 2019-06-28 19:30:22 -0700 | [diff] [blame] | 794 | wpc->ioend->io_bio = xfs_chain_bio(wpc->ioend->io_bio); |
Ming Lei | 07173c3 | 2019-02-15 19:13:20 +0800 | [diff] [blame] | 795 | bio_add_page(wpc->ioend->io_bio, page, len, poff); |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 796 | } |
Dave Chinner | bb18782 | 2016-04-06 08:11:25 +1000 | [diff] [blame] | 797 | |
Christoph Hellwig | 3faed66 | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 798 | wpc->ioend->io_size += len; |
Linus Torvalds | 9637d51 | 2019-07-15 21:20:52 -0700 | [diff] [blame] | 799 | wbc_account_cgroup_owner(wbc, page, len); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 800 | } |
| 801 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | STATIC void |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 803 | xfs_vm_invalidatepage( |
| 804 | struct page *page, |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 805 | unsigned int offset, |
| 806 | unsigned int length) |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 807 | { |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 808 | trace_xfs_invalidatepage(page->mapping->host, page, offset, length); |
| 809 | iomap_invalidatepage(page, offset, length); |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | /* |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 813 | * If the page has delalloc blocks on it, we need to punch them out before we |
| 814 | * invalidate the page. If we don't, we leave a stale delalloc mapping on the |
| 815 | * inode that can trip up a later direct I/O read operation on the same region. |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 816 | * |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 817 | * We prevent this by truncating away the delalloc regions on the page. Because |
| 818 | * they are delalloc, we can do this without needing a transaction. Indeed - if |
| 819 | * we get ENOSPC errors, we have to be able to do this truncation without a |
| 820 | * transaction as there is no space left for block reservation (typically why we |
| 821 | * see a ENOSPC in writeback). |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 822 | */ |
| 823 | STATIC void |
| 824 | xfs_aops_discard_page( |
| 825 | struct page *page) |
| 826 | { |
| 827 | struct inode *inode = page->mapping->host; |
| 828 | struct xfs_inode *ip = XFS_I(inode); |
Christoph Hellwig | 0362572 | 2018-07-11 22:25:57 -0700 | [diff] [blame] | 829 | struct xfs_mount *mp = ip->i_mount; |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 830 | loff_t offset = page_offset(page); |
Christoph Hellwig | 0362572 | 2018-07-11 22:25:57 -0700 | [diff] [blame] | 831 | xfs_fileoff_t start_fsb = XFS_B_TO_FSBT(mp, offset); |
| 832 | int error; |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 833 | |
Christoph Hellwig | 0362572 | 2018-07-11 22:25:57 -0700 | [diff] [blame] | 834 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 835 | goto out_invalidate; |
| 836 | |
Christoph Hellwig | 0362572 | 2018-07-11 22:25:57 -0700 | [diff] [blame] | 837 | xfs_alert(mp, |
Darrick J. Wong | c969004 | 2018-01-09 12:02:55 -0800 | [diff] [blame] | 838 | "page discard on page "PTR_FMT", inode 0x%llx, offset %llu.", |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 839 | page, ip->i_ino, offset); |
| 840 | |
Christoph Hellwig | 0362572 | 2018-07-11 22:25:57 -0700 | [diff] [blame] | 841 | error = xfs_bmap_punch_delalloc_range(ip, start_fsb, |
| 842 | PAGE_SIZE / i_blocksize(inode)); |
Christoph Hellwig | 0362572 | 2018-07-11 22:25:57 -0700 | [diff] [blame] | 843 | if (error && !XFS_FORCED_SHUTDOWN(mp)) |
| 844 | xfs_alert(mp, "page discard unable to remove delalloc mapping."); |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 845 | out_invalidate: |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 846 | xfs_vm_invalidatepage(page, 0, PAGE_SIZE); |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | /* |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 850 | * We implement an immediate ioend submission policy here to avoid needing to |
| 851 | * chain multiple ioends and hence nest mempool allocations which can violate |
| 852 | * forward progress guarantees we need to provide. The current ioend we are |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 853 | * adding blocks to is cached on the writepage context, and if the new block |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 854 | * does not append to the cached ioend it will create a new ioend and cache that |
| 855 | * instead. |
| 856 | * |
| 857 | * If a new ioend is created and cached, the old ioend is returned and queued |
| 858 | * locally for submission once the entire page is processed or an error has been |
| 859 | * detected. While ioends are submitted immediately after they are completed, |
| 860 | * batching optimisations are provided by higher level block plugging. |
| 861 | * |
| 862 | * At the end of a writeback pass, there will be a cached ioend remaining on the |
| 863 | * writepage context that the caller will need to submit. |
| 864 | */ |
Dave Chinner | bfce7d2 | 2016-02-15 17:21:37 +1100 | [diff] [blame] | 865 | static int |
| 866 | xfs_writepage_map( |
| 867 | struct xfs_writepage_ctx *wpc, |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 868 | struct writeback_control *wbc, |
Dave Chinner | bfce7d2 | 2016-02-15 17:21:37 +1100 | [diff] [blame] | 869 | struct inode *inode, |
| 870 | struct page *page, |
Darrick J. Wong | 2d5f4b5 | 2017-11-27 09:50:22 -0800 | [diff] [blame] | 871 | uint64_t end_offset) |
Dave Chinner | bfce7d2 | 2016-02-15 17:21:37 +1100 | [diff] [blame] | 872 | { |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 873 | LIST_HEAD(submit_list); |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 874 | struct iomap_page *iop = to_iomap_page(page); |
| 875 | unsigned len = i_blocksize(inode); |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 876 | struct xfs_ioend *ioend, *next; |
Christoph Hellwig | 6a4c950 | 2018-07-11 22:26:00 -0700 | [diff] [blame] | 877 | uint64_t file_offset; /* file offset of page */ |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 878 | int error = 0, count = 0, i; |
Dave Chinner | bfce7d2 | 2016-02-15 17:21:37 +1100 | [diff] [blame] | 879 | |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 880 | ASSERT(iop || i_blocksize(inode) == PAGE_SIZE); |
| 881 | ASSERT(!iop || atomic_read(&iop->write_count) == 0); |
Christoph Hellwig | ac8ee54 | 2018-07-11 22:26:04 -0700 | [diff] [blame] | 882 | |
Dave Chinner | e2f6ad4 | 2018-07-11 22:26:00 -0700 | [diff] [blame] | 883 | /* |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 884 | * Walk through the page to find areas to write back. If we run off the |
| 885 | * end of the current map or find the current map invalid, grab a new |
| 886 | * one. |
Dave Chinner | e2f6ad4 | 2018-07-11 22:26:00 -0700 | [diff] [blame] | 887 | */ |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 888 | for (i = 0, file_offset = page_offset(page); |
| 889 | i < (PAGE_SIZE >> inode->i_blkbits) && file_offset < end_offset; |
| 890 | i++, file_offset += len) { |
| 891 | if (iop && !test_bit(i, iop->uptodate)) |
Dave Chinner | bfce7d2 | 2016-02-15 17:21:37 +1100 | [diff] [blame] | 892 | continue; |
Dave Chinner | bfce7d2 | 2016-02-15 17:21:37 +1100 | [diff] [blame] | 893 | |
Christoph Hellwig | 889c65b | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 894 | error = xfs_map_blocks(wpc, inode, file_offset); |
| 895 | if (error) |
| 896 | break; |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 897 | if (wpc->imap.br_startblock == HOLESTARTBLOCK) |
Christoph Hellwig | ac8ee54 | 2018-07-11 22:26:04 -0700 | [diff] [blame] | 898 | continue; |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 899 | xfs_add_to_ioend(inode, file_offset, page, iop, wpc, wbc, |
| 900 | &submit_list); |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 901 | count++; |
Dave Chinner | e2f6ad4 | 2018-07-11 22:26:00 -0700 | [diff] [blame] | 902 | } |
Dave Chinner | bfce7d2 | 2016-02-15 17:21:37 +1100 | [diff] [blame] | 903 | |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 904 | ASSERT(wpc->ioend || list_empty(&submit_list)); |
Christoph Hellwig | 1b65d3dd | 2018-07-11 22:26:03 -0700 | [diff] [blame] | 905 | ASSERT(PageLocked(page)); |
| 906 | ASSERT(!PageWriteback(page)); |
Dave Chinner | bfce7d2 | 2016-02-15 17:21:37 +1100 | [diff] [blame] | 907 | |
Dave Chinner | bfce7d2 | 2016-02-15 17:21:37 +1100 | [diff] [blame] | 908 | /* |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 909 | * On error, we have to fail the ioend here because we may have set |
| 910 | * pages under writeback, we have to make sure we run IO completion to |
| 911 | * mark the error state of the IO appropriately, so we can't cancel the |
| 912 | * ioend directly here. That means we have to mark this page as under |
| 913 | * writeback if we included any blocks from it in the ioend chain so |
| 914 | * that completion treats it correctly. |
Dave Chinner | bfce7d2 | 2016-02-15 17:21:37 +1100 | [diff] [blame] | 915 | * |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 916 | * If we didn't include the page in the ioend, the on error we can |
| 917 | * simply discard and unlock it as there are no other users of the page |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 918 | * now. The caller will still need to trigger submission of outstanding |
| 919 | * ioends on the writepage context so they are treated correctly on |
| 920 | * error. |
Dave Chinner | bfce7d2 | 2016-02-15 17:21:37 +1100 | [diff] [blame] | 921 | */ |
Christoph Hellwig | 8e1f065 | 2018-07-11 22:26:04 -0700 | [diff] [blame] | 922 | if (unlikely(error)) { |
| 923 | if (!count) { |
| 924 | xfs_aops_discard_page(page); |
| 925 | ClearPageUptodate(page); |
| 926 | unlock_page(page); |
| 927 | goto done; |
| 928 | } |
| 929 | |
Christoph Hellwig | 1b65d3dd | 2018-07-11 22:26:03 -0700 | [diff] [blame] | 930 | /* |
| 931 | * If the page was not fully cleaned, we need to ensure that the |
| 932 | * higher layers come back to it correctly. That means we need |
| 933 | * to keep the page dirty, and for WB_SYNC_ALL writeback we need |
| 934 | * to ensure the PAGECACHE_TAG_TOWRITE index mark is not removed |
| 935 | * so another attempt to write this page in this writeback sweep |
| 936 | * will be made. |
| 937 | */ |
Christoph Hellwig | 8e1f065 | 2018-07-11 22:26:04 -0700 | [diff] [blame] | 938 | set_page_writeback_keepwrite(page); |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 939 | } else { |
Christoph Hellwig | 1b65d3dd | 2018-07-11 22:26:03 -0700 | [diff] [blame] | 940 | clear_page_dirty_for_io(page); |
| 941 | set_page_writeback(page); |
Dave Chinner | bfce7d2 | 2016-02-15 17:21:37 +1100 | [diff] [blame] | 942 | } |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 943 | |
Christoph Hellwig | 8e1f065 | 2018-07-11 22:26:04 -0700 | [diff] [blame] | 944 | unlock_page(page); |
| 945 | |
| 946 | /* |
| 947 | * Preserve the original error if there was one, otherwise catch |
| 948 | * submission errors here and propagate into subsequent ioend |
| 949 | * submissions. |
| 950 | */ |
| 951 | list_for_each_entry_safe(ioend, next, &submit_list, io_list) { |
| 952 | int error2; |
| 953 | |
| 954 | list_del_init(&ioend->io_list); |
| 955 | error2 = xfs_submit_ioend(wbc, ioend, error); |
| 956 | if (error2 && !error) |
| 957 | error = error2; |
| 958 | } |
| 959 | |
| 960 | /* |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 961 | * We can end up here with no error and nothing to write only if we race |
| 962 | * with a partial page truncate on a sub-page block sized filesystem. |
Christoph Hellwig | 8e1f065 | 2018-07-11 22:26:04 -0700 | [diff] [blame] | 963 | */ |
| 964 | if (!count) |
| 965 | end_page_writeback(page); |
| 966 | done: |
Dave Chinner | bfce7d2 | 2016-02-15 17:21:37 +1100 | [diff] [blame] | 967 | mapping_set_error(page->mapping, error); |
| 968 | return error; |
| 969 | } |
| 970 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | /* |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 972 | * Write out a dirty page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | * |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 974 | * For delalloc space on the page we need to allocate space and flush it. |
| 975 | * For unwritten space on the page we need to start the conversion to |
| 976 | * regular allocated space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | STATIC int |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 979 | xfs_do_writepage( |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 980 | struct page *page, |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 981 | struct writeback_control *wbc, |
| 982 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | { |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 984 | struct xfs_writepage_ctx *wpc = data; |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 985 | struct inode *inode = page->mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | loff_t offset; |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 987 | uint64_t end_offset; |
Dave Chinner | ad68972 | 2016-02-15 17:21:31 +1100 | [diff] [blame] | 988 | pgoff_t end_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | |
Lukas Czerner | 34097df | 2013-05-21 23:58:01 -0400 | [diff] [blame] | 990 | trace_xfs_writepage(inode, page, 0, 0); |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 991 | |
| 992 | /* |
| 993 | * Refuse to write the page out if we are called from reclaim context. |
| 994 | * |
Christoph Hellwig | d4f7a5c | 2010-06-28 10:34:44 -0400 | [diff] [blame] | 995 | * This avoids stack overflows when called from deeply used stacks in |
| 996 | * random callers for direct reclaim or memcg reclaim. We explicitly |
| 997 | * allow reclaim from kswapd as the stack usage there is relatively low. |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 998 | * |
Mel Gorman | 94054fa | 2011-10-31 17:07:45 -0700 | [diff] [blame] | 999 | * This should never happen except in the case of a VM regression so |
| 1000 | * warn about it. |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1001 | */ |
Mel Gorman | 94054fa | 2011-10-31 17:07:45 -0700 | [diff] [blame] | 1002 | if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == |
| 1003 | PF_MEMALLOC)) |
Christoph Hellwig | b5420f2 | 2010-08-24 11:47:51 +1000 | [diff] [blame] | 1004 | goto redirty; |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1005 | |
| 1006 | /* |
Christoph Hellwig | 680a647 | 2011-07-08 14:34:05 +0200 | [diff] [blame] | 1007 | * Given that we do not allow direct reclaim to call us, we should |
| 1008 | * never be called while in a filesystem transaction. |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1009 | */ |
Michal Hocko | 9070733 | 2017-05-03 14:53:12 -0700 | [diff] [blame] | 1010 | if (WARN_ON_ONCE(current->flags & PF_MEMALLOC_NOFS)) |
Christoph Hellwig | b5420f2 | 2010-08-24 11:47:51 +1000 | [diff] [blame] | 1011 | goto redirty; |
Christoph Hellwig | 89f3b363 | 2010-06-24 09:45:48 +1000 | [diff] [blame] | 1012 | |
Jie Liu | 8695d27 | 2014-05-20 08:24:26 +1000 | [diff] [blame] | 1013 | /* |
Dave Chinner | ad68972 | 2016-02-15 17:21:31 +1100 | [diff] [blame] | 1014 | * Is this page beyond the end of the file? |
| 1015 | * |
Jie Liu | 8695d27 | 2014-05-20 08:24:26 +1000 | [diff] [blame] | 1016 | * The page index is less than the end_index, adjust the end_offset |
| 1017 | * to the highest offset that this page should represent. |
| 1018 | * ----------------------------------------------------- |
| 1019 | * | file mapping | <EOF> | |
| 1020 | * ----------------------------------------------------- |
| 1021 | * | Page ... | Page N-2 | Page N-1 | Page N | | |
| 1022 | * ^--------------------------------^----------|-------- |
| 1023 | * | desired writeback range | see else | |
| 1024 | * ---------------------------------^------------------| |
| 1025 | */ |
Dave Chinner | ad68972 | 2016-02-15 17:21:31 +1100 | [diff] [blame] | 1026 | offset = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1027 | end_index = offset >> PAGE_SHIFT; |
Jie Liu | 8695d27 | 2014-05-20 08:24:26 +1000 | [diff] [blame] | 1028 | if (page->index < end_index) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1029 | end_offset = (xfs_off_t)(page->index + 1) << PAGE_SHIFT; |
Jie Liu | 8695d27 | 2014-05-20 08:24:26 +1000 | [diff] [blame] | 1030 | else { |
| 1031 | /* |
| 1032 | * Check whether the page to write out is beyond or straddles |
| 1033 | * i_size or not. |
| 1034 | * ------------------------------------------------------- |
| 1035 | * | file mapping | <EOF> | |
| 1036 | * ------------------------------------------------------- |
| 1037 | * | Page ... | Page N-2 | Page N-1 | Page N | Beyond | |
| 1038 | * ^--------------------------------^-----------|--------- |
| 1039 | * | | Straddles | |
| 1040 | * ---------------------------------^-----------|--------| |
| 1041 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1042 | unsigned offset_into_page = offset & (PAGE_SIZE - 1); |
Christoph Hellwig | 6b7a03f | 2012-07-03 12:20:00 -0400 | [diff] [blame] | 1043 | |
| 1044 | /* |
Jan Kara | ff9a28f | 2013-03-14 14:30:54 +0100 | [diff] [blame] | 1045 | * Skip the page if it is fully outside i_size, e.g. due to a |
| 1046 | * truncate operation that is in progress. We must redirty the |
| 1047 | * page so that reclaim stops reclaiming it. Otherwise |
| 1048 | * xfs_vm_releasepage() is called on it and gets confused. |
Jie Liu | 8695d27 | 2014-05-20 08:24:26 +1000 | [diff] [blame] | 1049 | * |
| 1050 | * Note that the end_index is unsigned long, it would overflow |
| 1051 | * if the given offset is greater than 16TB on 32-bit system |
| 1052 | * and if we do check the page is fully outside i_size or not |
| 1053 | * via "if (page->index >= end_index + 1)" as "end_index + 1" |
| 1054 | * will be evaluated to 0. Hence this page will be redirtied |
| 1055 | * and be written out repeatedly which would result in an |
| 1056 | * infinite loop, the user program that perform this operation |
| 1057 | * will hang. Instead, we can verify this situation by checking |
| 1058 | * if the page to write is totally beyond the i_size or if it's |
| 1059 | * offset is just equal to the EOF. |
Christoph Hellwig | 6b7a03f | 2012-07-03 12:20:00 -0400 | [diff] [blame] | 1060 | */ |
Jie Liu | 8695d27 | 2014-05-20 08:24:26 +1000 | [diff] [blame] | 1061 | if (page->index > end_index || |
| 1062 | (page->index == end_index && offset_into_page == 0)) |
Jan Kara | ff9a28f | 2013-03-14 14:30:54 +0100 | [diff] [blame] | 1063 | goto redirty; |
Christoph Hellwig | 6b7a03f | 2012-07-03 12:20:00 -0400 | [diff] [blame] | 1064 | |
| 1065 | /* |
| 1066 | * The page straddles i_size. It must be zeroed out on each |
| 1067 | * and every writepage invocation because it may be mmapped. |
| 1068 | * "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] | 1069 | * that is not a multiple of the page size, the remaining |
Christoph Hellwig | 6b7a03f | 2012-07-03 12:20:00 -0400 | [diff] [blame] | 1070 | * memory is zeroed when mapped, and writes to that region are |
| 1071 | * not written out to the file." |
| 1072 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1073 | zero_user_segment(page, offset_into_page, PAGE_SIZE); |
Jie Liu | 8695d27 | 2014-05-20 08:24:26 +1000 | [diff] [blame] | 1074 | |
| 1075 | /* Adjust the end_offset to the end of file */ |
| 1076 | end_offset = offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Darrick J. Wong | 2d5f4b5 | 2017-11-27 09:50:22 -0800 | [diff] [blame] | 1079 | return xfs_writepage_map(wpc, wbc, inode, page, end_offset); |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1080 | |
Christoph Hellwig | b5420f2 | 2010-08-24 11:47:51 +1000 | [diff] [blame] | 1081 | redirty: |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1082 | redirty_page_for_writepage(wbc, page); |
| 1083 | unlock_page(page); |
| 1084 | return 0; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1085 | } |
| 1086 | |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 1087 | STATIC int |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 1088 | xfs_vm_writepage( |
| 1089 | struct page *page, |
| 1090 | struct writeback_control *wbc) |
| 1091 | { |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 1092 | struct xfs_writepage_ctx wpc = { }; |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 1093 | int ret; |
| 1094 | |
| 1095 | ret = xfs_do_writepage(page, wbc, &wpc); |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 1096 | if (wpc.ioend) |
| 1097 | ret = xfs_submit_ioend(wbc, wpc.ioend, ret); |
| 1098 | return ret; |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | STATIC int |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 1102 | xfs_vm_writepages( |
| 1103 | struct address_space *mapping, |
| 1104 | struct writeback_control *wbc) |
| 1105 | { |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 1106 | struct xfs_writepage_ctx wpc = { }; |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 1107 | int ret; |
| 1108 | |
Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 1109 | xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED); |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 1110 | ret = write_cache_pages(mapping, wbc, xfs_do_writepage, &wpc); |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 1111 | if (wpc.ioend) |
| 1112 | ret = xfs_submit_ioend(wbc, wpc.ioend, ret); |
| 1113 | return ret; |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 1114 | } |
| 1115 | |
Dan Williams | 6e2608d | 2018-03-07 15:26:44 -0800 | [diff] [blame] | 1116 | STATIC int |
| 1117 | xfs_dax_writepages( |
| 1118 | struct address_space *mapping, |
| 1119 | struct writeback_control *wbc) |
| 1120 | { |
| 1121 | xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED); |
| 1122 | return dax_writeback_mapping_range(mapping, |
| 1123 | xfs_find_bdev_for_inode(mapping->host), wbc); |
| 1124 | } |
| 1125 | |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1126 | STATIC int |
Nathan Scott | 238f4c5 | 2006-03-17 17:26:25 +1100 | [diff] [blame] | 1127 | xfs_vm_releasepage( |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 1128 | struct page *page, |
| 1129 | gfp_t gfp_mask) |
| 1130 | { |
Lukas Czerner | 34097df | 2013-05-21 23:58:01 -0400 | [diff] [blame] | 1131 | trace_xfs_releasepage(page->mapping->host, page, 0, 0); |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 1132 | return iomap_releasepage(page, gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | STATIC sector_t |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1136 | xfs_vm_bmap( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | struct address_space *mapping, |
| 1138 | sector_t block) |
| 1139 | { |
Christoph Hellwig | b84e772 | 2018-06-01 09:03:09 -0700 | [diff] [blame] | 1140 | struct xfs_inode *ip = XFS_I(mapping->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | |
Christoph Hellwig | b84e772 | 2018-06-01 09:03:09 -0700 | [diff] [blame] | 1142 | trace_xfs_vm_bmap(ip); |
Darrick J. Wong | db1327b | 2016-10-03 09:11:36 -0700 | [diff] [blame] | 1143 | |
| 1144 | /* |
| 1145 | * The swap code (ab-)uses ->bmap to get a block mapping and then |
Ingo Molnar | 793057e | 2018-02-28 09:39:48 +0100 | [diff] [blame] | 1146 | * bypasses the file system for actual I/O. We really can't allow |
Darrick J. Wong | db1327b | 2016-10-03 09:11:36 -0700 | [diff] [blame] | 1147 | * that on reflinks inodes, so we have to skip out here. And yes, |
Darrick J. Wong | eb5e248 | 2017-06-21 20:27:35 -0700 | [diff] [blame] | 1148 | * 0 is the magic code for a bmap error. |
| 1149 | * |
| 1150 | * Since we don't pass back blockdev info, we can't return bmap |
| 1151 | * information for rt files either. |
Darrick J. Wong | db1327b | 2016-10-03 09:11:36 -0700 | [diff] [blame] | 1152 | */ |
Christoph Hellwig | 66ae56a | 2019-02-18 09:38:49 -0800 | [diff] [blame] | 1153 | if (xfs_is_cow_inode(ip) || XFS_IS_REALTIME_INODE(ip)) |
Darrick J. Wong | db1327b | 2016-10-03 09:11:36 -0700 | [diff] [blame] | 1154 | return 0; |
Christoph Hellwig | b84e772 | 2018-06-01 09:03:09 -0700 | [diff] [blame] | 1155 | return iomap_bmap(mapping, block, &xfs_iomap_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | STATIC int |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1159 | xfs_vm_readpage( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | struct file *unused, |
| 1161 | struct page *page) |
| 1162 | { |
Dave Chinner | 121e213 | 2016-01-08 11:28:35 +1100 | [diff] [blame] | 1163 | trace_xfs_vm_readpage(page->mapping->host, 1); |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 1164 | return iomap_readpage(page, &xfs_iomap_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | STATIC int |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1168 | xfs_vm_readpages( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | struct file *unused, |
| 1170 | struct address_space *mapping, |
| 1171 | struct list_head *pages, |
| 1172 | unsigned nr_pages) |
| 1173 | { |
Dave Chinner | 121e213 | 2016-01-08 11:28:35 +1100 | [diff] [blame] | 1174 | trace_xfs_vm_readpages(mapping->host, nr_pages); |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 1175 | return iomap_readpages(mapping, pages, nr_pages, &xfs_iomap_ops); |
Dave Chinner | 22e757a | 2014-09-02 12:12:51 +1000 | [diff] [blame] | 1176 | } |
| 1177 | |
Darrick J. Wong | 6748212 | 2018-05-10 08:38:15 -0700 | [diff] [blame] | 1178 | static int |
| 1179 | xfs_iomap_swapfile_activate( |
| 1180 | struct swap_info_struct *sis, |
| 1181 | struct file *swap_file, |
| 1182 | sector_t *span) |
| 1183 | { |
| 1184 | sis->bdev = xfs_find_bdev_for_inode(file_inode(swap_file)); |
| 1185 | return iomap_swapfile_activate(sis, swap_file, span, &xfs_iomap_ops); |
| 1186 | } |
| 1187 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 1188 | const struct address_space_operations xfs_address_space_operations = { |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1189 | .readpage = xfs_vm_readpage, |
| 1190 | .readpages = xfs_vm_readpages, |
| 1191 | .writepage = xfs_vm_writepage, |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 1192 | .writepages = xfs_vm_writepages, |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 1193 | .set_page_dirty = iomap_set_page_dirty, |
Nathan Scott | 238f4c5 | 2006-03-17 17:26:25 +1100 | [diff] [blame] | 1194 | .releasepage = xfs_vm_releasepage, |
| 1195 | .invalidatepage = xfs_vm_invalidatepage, |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 1196 | .bmap = xfs_vm_bmap, |
Dan Williams | 6e2608d | 2018-03-07 15:26:44 -0800 | [diff] [blame] | 1197 | .direct_IO = noop_direct_IO, |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 1198 | .migratepage = iomap_migrate_page, |
| 1199 | .is_partially_uptodate = iomap_is_partially_uptodate, |
Andi Kleen | aa261f5 | 2009-09-16 11:50:16 +0200 | [diff] [blame] | 1200 | .error_remove_page = generic_error_remove_page, |
Darrick J. Wong | 6748212 | 2018-05-10 08:38:15 -0700 | [diff] [blame] | 1201 | .swap_activate = xfs_iomap_swapfile_activate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | }; |
Dan Williams | 6e2608d | 2018-03-07 15:26:44 -0800 | [diff] [blame] | 1203 | |
| 1204 | const struct address_space_operations xfs_dax_aops = { |
| 1205 | .writepages = xfs_dax_writepages, |
| 1206 | .direct_IO = noop_direct_IO, |
| 1207 | .set_page_dirty = noop_set_page_dirty, |
| 1208 | .invalidatepage = noop_invalidatepage, |
Darrick J. Wong | 6748212 | 2018-05-10 08:38:15 -0700 | [diff] [blame] | 1209 | .swap_activate = xfs_iomap_swapfile_activate, |
Dan Williams | 6e2608d | 2018-03-07 15:26:44 -0800 | [diff] [blame] | 1210 | }; |