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 | struct xfs_writepage_ctx { |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 22 | struct iomap_writepage_ctx ctx; |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 23 | unsigned int data_seq; |
Christoph Hellwig | e666aa3 | 2018-07-17 16:51:52 -0700 | [diff] [blame] | 24 | unsigned int cow_seq; |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 25 | }; |
| 26 | |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 27 | static inline struct xfs_writepage_ctx * |
| 28 | XFS_WPC(struct iomap_writepage_ctx *ctx) |
| 29 | { |
| 30 | return container_of(ctx, struct xfs_writepage_ctx, ctx); |
| 31 | } |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | /* |
Christoph Hellwig | fc0063c | 2011-08-23 08:28:11 +0000 | [diff] [blame] | 34 | * Fast and loose check if this write could update the on-disk inode size. |
| 35 | */ |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 36 | static inline bool xfs_ioend_is_append(struct iomap_ioend *ioend) |
Christoph Hellwig | fc0063c | 2011-08-23 08:28:11 +0000 | [diff] [blame] | 37 | { |
| 38 | return ioend->io_offset + ioend->io_size > |
| 39 | XFS_I(ioend->io_inode)->i_d.di_size; |
| 40 | } |
| 41 | |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 42 | STATIC int |
| 43 | xfs_setfilesize_trans_alloc( |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 44 | struct iomap_ioend *ioend) |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 45 | { |
| 46 | struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount; |
| 47 | struct xfs_trans *tp; |
| 48 | int error; |
| 49 | |
Christoph Hellwig | 73d30d4 | 2019-06-28 19:31:38 -0700 | [diff] [blame] | 50 | 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] | 51 | if (error) |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 52 | return error; |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 53 | |
Christoph Hellwig | 5653017 | 2019-10-17 13:12:09 -0700 | [diff] [blame] | 54 | ioend->io_private = tp; |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 55 | |
| 56 | /* |
Dave Chinner | 437a255 | 2012-11-28 13:01:00 +1100 | [diff] [blame] | 57 | * We may pass freeze protection with a transaction. So tell lockdep |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 58 | * we released it. |
| 59 | */ |
Oleg Nesterov | bee9182 | 2015-07-19 23:48:20 +0200 | [diff] [blame] | 60 | __sb_writers_release(ioend->io_inode->i_sb, SB_FREEZE_FS); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 61 | /* |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 62 | * We hand off the transaction to the completion thread now, so |
| 63 | * clear the flag here. |
| 64 | */ |
Michal Hocko | 9070733 | 2017-05-03 14:53:12 -0700 | [diff] [blame] | 65 | current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
Christoph Hellwig | fc0063c | 2011-08-23 08:28:11 +0000 | [diff] [blame] | 69 | /* |
Christoph Hellwig | 2813d68 | 2011-12-18 20:00:12 +0000 | [diff] [blame] | 70 | * 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] | 71 | */ |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 72 | STATIC int |
Christoph Hellwig | e372843 | 2016-09-19 11:26:41 +1000 | [diff] [blame] | 73 | __xfs_setfilesize( |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 74 | struct xfs_inode *ip, |
| 75 | struct xfs_trans *tp, |
| 76 | xfs_off_t offset, |
| 77 | size_t size) |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 78 | { |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 79 | xfs_fsize_t isize; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 80 | |
Christoph Hellwig | aa6bf01 | 2012-02-29 09:53:48 +0000 | [diff] [blame] | 81 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 82 | isize = xfs_new_eof(ip, offset + size); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 83 | if (!isize) { |
| 84 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 85 | xfs_trans_cancel(tp); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 86 | return 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 87 | } |
| 88 | |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 89 | trace_xfs_setfilesize(ip, offset, size); |
Christoph Hellwig | 281627d | 2012-03-13 08:41:05 +0000 | [diff] [blame] | 90 | |
| 91 | ip->i_d.di_size = isize; |
| 92 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 93 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 94 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 95 | return xfs_trans_commit(tp); |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 96 | } |
| 97 | |
Christoph Hellwig | e372843 | 2016-09-19 11:26:41 +1000 | [diff] [blame] | 98 | int |
| 99 | xfs_setfilesize( |
| 100 | struct xfs_inode *ip, |
| 101 | xfs_off_t offset, |
| 102 | size_t size) |
| 103 | { |
| 104 | struct xfs_mount *mp = ip->i_mount; |
| 105 | struct xfs_trans *tp; |
| 106 | int error; |
| 107 | |
| 108 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp); |
| 109 | if (error) |
| 110 | return error; |
| 111 | |
| 112 | return __xfs_setfilesize(ip, tp, offset, size); |
| 113 | } |
| 114 | |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 115 | STATIC int |
| 116 | xfs_setfilesize_ioend( |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 117 | struct iomap_ioend *ioend, |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 118 | int error) |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 119 | { |
| 120 | struct xfs_inode *ip = XFS_I(ioend->io_inode); |
Christoph Hellwig | 5653017 | 2019-10-17 13:12:09 -0700 | [diff] [blame] | 121 | struct xfs_trans *tp = ioend->io_private; |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * The transaction may have been allocated in the I/O submission thread, |
| 125 | * thus we need to mark ourselves as being in a transaction manually. |
| 126 | * Similarly for freeze protection. |
| 127 | */ |
Michal Hocko | 9070733 | 2017-05-03 14:53:12 -0700 | [diff] [blame] | 128 | current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); |
Oleg Nesterov | bee9182 | 2015-07-19 23:48:20 +0200 | [diff] [blame] | 129 | __sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS); |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 130 | |
Zhaohongjiang | 5cb13dc | 2015-10-12 15:28:39 +1100 | [diff] [blame] | 131 | /* we abort the update if there was an IO error */ |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 132 | if (error) { |
Zhaohongjiang | 5cb13dc | 2015-10-12 15:28:39 +1100 | [diff] [blame] | 133 | xfs_trans_cancel(tp); |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 134 | return error; |
Zhaohongjiang | 5cb13dc | 2015-10-12 15:28:39 +1100 | [diff] [blame] | 135 | } |
| 136 | |
Christoph Hellwig | e372843 | 2016-09-19 11:26:41 +1000 | [diff] [blame] | 137 | return __xfs_setfilesize(ip, tp, ioend->io_offset, ioend->io_size); |
Christoph Hellwig | 2ba6623 | 2015-02-02 10:02:09 +1100 | [diff] [blame] | 138 | } |
| 139 | |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 140 | /* |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 141 | * IO write completion. |
| 142 | */ |
| 143 | STATIC void |
Darrick J. Wong | cb357bf | 2019-04-15 13:13:20 -0700 | [diff] [blame] | 144 | xfs_end_ioend( |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 145 | struct iomap_ioend *ioend) |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 146 | { |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 147 | struct xfs_inode *ip = XFS_I(ioend->io_inode); |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 148 | xfs_off_t offset = ioend->io_offset; |
| 149 | size_t size = ioend->io_size; |
Christoph Hellwig | 73d30d4 | 2019-06-28 19:31:38 -0700 | [diff] [blame] | 150 | unsigned int nofs_flag; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 151 | int error; |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 152 | |
Brian Foster | af055e3 | 2016-02-08 15:00:02 +1100 | [diff] [blame] | 153 | /* |
Christoph Hellwig | 73d30d4 | 2019-06-28 19:31:38 -0700 | [diff] [blame] | 154 | * We can allocate memory here while doing writeback on behalf of |
| 155 | * memory reclaim. To avoid memory allocation deadlocks set the |
| 156 | * task-wide nofs context for the following operations. |
| 157 | */ |
| 158 | nofs_flag = memalloc_nofs_save(); |
| 159 | |
| 160 | /* |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 161 | * 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] | 162 | */ |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 163 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 164 | error = -EIO; |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 165 | goto done; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 169 | * Clean up any COW blocks on an I/O error. |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 170 | */ |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 171 | error = blk_status_to_errno(ioend->io_bio->bi_status); |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 172 | if (unlikely(error)) { |
Christoph Hellwig | 760fea8 | 2019-10-17 13:12:10 -0700 | [diff] [blame] | 173 | if (ioend->io_flags & IOMAP_F_SHARED) |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 174 | xfs_reflink_cancel_cow_range(ip, offset, size, true); |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 175 | goto done; |
| 176 | } |
| 177 | |
| 178 | /* |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 179 | * Success: commit the COW or unwritten blocks if needed. |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 180 | */ |
Christoph Hellwig | 760fea8 | 2019-10-17 13:12:10 -0700 | [diff] [blame] | 181 | if (ioend->io_flags & IOMAP_F_SHARED) |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 182 | error = xfs_reflink_end_cow(ip, offset, size); |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 183 | else if (ioend->io_type == IOMAP_UNWRITTEN) |
Eryu Guan | ee70daa | 2017-09-21 11:26:18 -0700 | [diff] [blame] | 184 | error = xfs_iomap_write_unwritten(ip, offset, size, false); |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 185 | else |
Christoph Hellwig | 5653017 | 2019-10-17 13:12:09 -0700 | [diff] [blame] | 186 | ASSERT(!xfs_ioend_is_append(ioend) || ioend->io_private); |
Dave Chinner | 77d7a0c | 2010-02-17 05:36:29 +0000 | [diff] [blame] | 187 | |
Christoph Hellwig | 04f658e | 2011-08-24 05:59:25 +0000 | [diff] [blame] | 188 | done: |
Christoph Hellwig | 5653017 | 2019-10-17 13:12:09 -0700 | [diff] [blame] | 189 | if (ioend->io_private) |
Christoph Hellwig | 787eb48 | 2017-03-02 15:02:51 -0800 | [diff] [blame] | 190 | error = xfs_setfilesize_ioend(ioend, error); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 191 | iomap_finish_ioends(ioend, error); |
Christoph Hellwig | 73d30d4 | 2019-06-28 19:31:38 -0700 | [diff] [blame] | 192 | memalloc_nofs_restore(nofs_flag); |
Darrick J. Wong | 3994fc4 | 2019-04-15 13:13:21 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /* |
Christoph Hellwig | 7dbae9f | 2019-06-28 19:31:37 -0700 | [diff] [blame] | 196 | * If the to be merged ioend has a preallocated transaction for file |
| 197 | * size updates we need to ensure the ioend it is merged into also |
| 198 | * has one. If it already has one we can simply cancel the transaction |
| 199 | * as it is guaranteed to be clean. |
| 200 | */ |
| 201 | static void |
Christoph Hellwig | 5653017 | 2019-10-17 13:12:09 -0700 | [diff] [blame] | 202 | xfs_ioend_merge_private( |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 203 | struct iomap_ioend *ioend, |
| 204 | struct iomap_ioend *next) |
Christoph Hellwig | 7dbae9f | 2019-06-28 19:31:37 -0700 | [diff] [blame] | 205 | { |
Christoph Hellwig | 5653017 | 2019-10-17 13:12:09 -0700 | [diff] [blame] | 206 | if (!ioend->io_private) { |
| 207 | ioend->io_private = next->io_private; |
| 208 | next->io_private = NULL; |
Christoph Hellwig | 7dbae9f | 2019-06-28 19:31:37 -0700 | [diff] [blame] | 209 | } else { |
| 210 | xfs_setfilesize_ioend(next, -ECANCELED); |
| 211 | } |
| 212 | } |
| 213 | |
Darrick J. Wong | cb357bf | 2019-04-15 13:13:20 -0700 | [diff] [blame] | 214 | /* Finish all pending io completions. */ |
| 215 | void |
| 216 | xfs_end_io( |
| 217 | struct work_struct *work) |
| 218 | { |
Christoph Hellwig | 433dad9 | 2019-10-17 13:12:07 -0700 | [diff] [blame] | 219 | struct xfs_inode *ip = |
| 220 | container_of(work, struct xfs_inode, i_ioend_work); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 221 | struct iomap_ioend *ioend; |
Christoph Hellwig | 433dad9 | 2019-10-17 13:12:07 -0700 | [diff] [blame] | 222 | struct list_head tmp; |
Darrick J. Wong | cb357bf | 2019-04-15 13:13:20 -0700 | [diff] [blame] | 223 | unsigned long flags; |
| 224 | |
Darrick J. Wong | cb357bf | 2019-04-15 13:13:20 -0700 | [diff] [blame] | 225 | spin_lock_irqsave(&ip->i_ioend_lock, flags); |
Christoph Hellwig | 433dad9 | 2019-10-17 13:12:07 -0700 | [diff] [blame] | 226 | list_replace_init(&ip->i_ioend_list, &tmp); |
Darrick J. Wong | cb357bf | 2019-04-15 13:13:20 -0700 | [diff] [blame] | 227 | spin_unlock_irqrestore(&ip->i_ioend_lock, flags); |
| 228 | |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 229 | iomap_sort_ioends(&tmp); |
| 230 | while ((ioend = list_first_entry_or_null(&tmp, struct iomap_ioend, |
Christoph Hellwig | 433dad9 | 2019-10-17 13:12:07 -0700 | [diff] [blame] | 231 | io_list))) { |
Darrick J. Wong | cb357bf | 2019-04-15 13:13:20 -0700 | [diff] [blame] | 232 | list_del_init(&ioend->io_list); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 233 | iomap_ioend_try_merge(ioend, &tmp, xfs_ioend_merge_private); |
Darrick J. Wong | cb357bf | 2019-04-15 13:13:20 -0700 | [diff] [blame] | 234 | xfs_end_ioend(ioend); |
| 235 | } |
| 236 | } |
| 237 | |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 238 | static inline bool xfs_ioend_needs_workqueue(struct iomap_ioend *ioend) |
Christoph Hellwig | 760fea8 | 2019-10-17 13:12:10 -0700 | [diff] [blame] | 239 | { |
| 240 | return ioend->io_private || |
| 241 | ioend->io_type == IOMAP_UNWRITTEN || |
| 242 | (ioend->io_flags & IOMAP_F_SHARED); |
| 243 | } |
| 244 | |
Christoph Hellwig | 0e51a8e | 2016-04-06 08:34:30 +1000 | [diff] [blame] | 245 | STATIC void |
| 246 | xfs_end_bio( |
| 247 | struct bio *bio) |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 248 | { |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 249 | struct iomap_ioend *ioend = bio->bi_private; |
Darrick J. Wong | cb357bf | 2019-04-15 13:13:20 -0700 | [diff] [blame] | 250 | struct xfs_inode *ip = XFS_I(ioend->io_inode); |
Darrick J. Wong | cb357bf | 2019-04-15 13:13:20 -0700 | [diff] [blame] | 251 | unsigned long flags; |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 252 | |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 253 | ASSERT(xfs_ioend_needs_workqueue(ioend)); |
| 254 | |
| 255 | spin_lock_irqsave(&ip->i_ioend_lock, flags); |
| 256 | if (list_empty(&ip->i_ioend_list)) |
| 257 | WARN_ON_ONCE(!queue_work(ip->i_mount->m_unwritten_workqueue, |
| 258 | &ip->i_ioend_work)); |
| 259 | list_add_tail(&ioend->io_list, &ip->i_ioend_list); |
| 260 | spin_unlock_irqrestore(&ip->i_ioend_lock, flags); |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 261 | } |
| 262 | |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 263 | /* |
| 264 | * Fast revalidation of the cached writeback mapping. Return true if the current |
| 265 | * mapping is valid, false otherwise. |
| 266 | */ |
| 267 | static bool |
| 268 | xfs_imap_valid( |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 269 | struct iomap_writepage_ctx *wpc, |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 270 | struct xfs_inode *ip, |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 271 | loff_t offset) |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 272 | { |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 273 | if (offset < wpc->iomap.offset || |
| 274 | offset >= wpc->iomap.offset + wpc->iomap.length) |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 275 | return false; |
| 276 | /* |
| 277 | * If this is a COW mapping, it is sufficient to check that the mapping |
| 278 | * covers the offset. Be careful to check this first because the caller |
| 279 | * can revalidate a COW mapping without updating the data seqno. |
| 280 | */ |
Christoph Hellwig | 760fea8 | 2019-10-17 13:12:10 -0700 | [diff] [blame] | 281 | if (wpc->iomap.flags & IOMAP_F_SHARED) |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 282 | return true; |
| 283 | |
| 284 | /* |
| 285 | * This is not a COW mapping. Check the sequence number of the data fork |
| 286 | * because concurrent changes could have invalidated the extent. Check |
| 287 | * the COW fork because concurrent changes since the last time we |
| 288 | * checked (and found nothing at this offset) could have added |
| 289 | * overlapping blocks. |
| 290 | */ |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 291 | if (XFS_WPC(wpc)->data_seq != READ_ONCE(ip->i_df.if_seq)) |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 292 | return false; |
| 293 | if (xfs_inode_has_cow_data(ip) && |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 294 | XFS_WPC(wpc)->cow_seq != READ_ONCE(ip->i_cowfp->if_seq)) |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 295 | return false; |
| 296 | return true; |
| 297 | } |
| 298 | |
Christoph Hellwig | 4ad765e | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 299 | /* |
| 300 | * Pass in a dellalloc extent and convert it to real extents, return the real |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 301 | * extent that maps offset_fsb in wpc->iomap. |
Christoph Hellwig | 4ad765e | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 302 | * |
| 303 | * 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] | 304 | * backing offset_fsb, although it could have moved from the COW to the data |
| 305 | * fork by another thread. |
Christoph Hellwig | 4ad765e | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 306 | */ |
| 307 | static int |
| 308 | xfs_convert_blocks( |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 309 | struct iomap_writepage_ctx *wpc, |
Christoph Hellwig | 4ad765e | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 310 | struct xfs_inode *ip, |
Christoph Hellwig | 760fea8 | 2019-10-17 13:12:10 -0700 | [diff] [blame] | 311 | int whichfork, |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 312 | loff_t offset) |
Christoph Hellwig | 4ad765e | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 313 | { |
| 314 | int error; |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 315 | unsigned *seq; |
| 316 | |
| 317 | if (whichfork == XFS_COW_FORK) |
| 318 | seq = &XFS_WPC(wpc)->cow_seq; |
| 319 | else |
| 320 | seq = &XFS_WPC(wpc)->data_seq; |
Christoph Hellwig | 4ad765e | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 321 | |
| 322 | /* |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 323 | * Attempt to allocate whatever delalloc extent currently backs offset |
| 324 | * and put the result into wpc->iomap. Allocate in a loop because it |
| 325 | * may take several attempts to allocate real blocks for a contiguous |
| 326 | * delalloc extent if free space is sufficiently fragmented. |
Christoph Hellwig | 4ad765e | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 327 | */ |
| 328 | do { |
Christoph Hellwig | 760fea8 | 2019-10-17 13:12:10 -0700 | [diff] [blame] | 329 | error = xfs_bmapi_convert_delalloc(ip, whichfork, offset, |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 330 | &wpc->iomap, seq); |
Christoph Hellwig | 4ad765e | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 331 | if (error) |
| 332 | return error; |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 333 | } while (wpc->iomap.offset + wpc->iomap.length <= offset); |
Christoph Hellwig | 4ad765e | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 338 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | xfs_map_blocks( |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 340 | struct iomap_writepage_ctx *wpc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | struct inode *inode, |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 342 | loff_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | { |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 344 | struct xfs_inode *ip = XFS_I(inode); |
| 345 | struct xfs_mount *mp = ip->i_mount; |
Fabian Frederick | 9340747 | 2017-02-27 14:28:32 -0800 | [diff] [blame] | 346 | ssize_t count = i_blocksize(inode); |
Christoph Hellwig | b4e2903 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 347 | xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 348 | xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count); |
Christoph Hellwig | e666aa3 | 2018-07-17 16:51:52 -0700 | [diff] [blame] | 349 | xfs_fileoff_t cow_fsb = NULLFILEOFF; |
Christoph Hellwig | 760fea8 | 2019-10-17 13:12:10 -0700 | [diff] [blame] | 350 | int whichfork = XFS_DATA_FORK; |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 351 | struct xfs_bmbt_irec imap; |
Christoph Hellwig | 060d4ea | 2018-07-11 22:26:01 -0700 | [diff] [blame] | 352 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 7588cbe | 2019-02-15 08:02:50 -0800 | [diff] [blame] | 353 | int retries = 0; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 354 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 356 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 357 | return -EIO; |
| 358 | |
Christoph Hellwig | 889c65b | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 359 | /* |
Christoph Hellwig | 889c65b | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 360 | * COW fork blocks can overlap data fork blocks even if the blocks |
| 361 | * aren't shared. COW I/O always takes precedent, so we must always |
| 362 | * check for overlap on reflink inodes unless the mapping is already a |
Christoph Hellwig | e666aa3 | 2018-07-17 16:51:52 -0700 | [diff] [blame] | 363 | * COW one, or the COW fork hasn't changed from the last time we looked |
| 364 | * at it. |
| 365 | * |
| 366 | * It's safe to check the COW fork if_seq here without the ILOCK because |
| 367 | * we've indirectly protected against concurrent updates: writeback has |
| 368 | * the page locked, which prevents concurrent invalidations by reflink |
| 369 | * and directio and prevents concurrent buffered writes to the same |
| 370 | * page. Changes to if_seq always happen under i_lock, which protects |
| 371 | * against concurrent updates and provides a memory barrier on the way |
| 372 | * out that ensures that we always see the current value. |
Christoph Hellwig | 889c65b | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 373 | */ |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 374 | if (xfs_imap_valid(wpc, ip, offset)) |
Christoph Hellwig | 889c65b | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 375 | return 0; |
| 376 | |
Christoph Hellwig | 889c65b | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 377 | /* |
| 378 | * If we don't have a valid map, now it's time to get a new one for this |
| 379 | * offset. This will convert delayed allocations (including COW ones) |
| 380 | * into real extents. If we return without a valid map, it means we |
| 381 | * landed in a hole and we skip the block. |
| 382 | */ |
Christoph Hellwig | 7588cbe | 2019-02-15 08:02:50 -0800 | [diff] [blame] | 383 | retry: |
Dave Chinner | 988ef92 | 2016-02-15 17:20:50 +1100 | [diff] [blame] | 384 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 385 | ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 386 | (ip->i_df.if_flags & XFS_IFEXTENTS)); |
Christoph Hellwig | 060d4ea | 2018-07-11 22:26:01 -0700 | [diff] [blame] | 387 | |
| 388 | /* |
| 389 | * Check if this is offset is covered by a COW extents, and if yes use |
| 390 | * it directly instead of looking up anything in the data fork. |
| 391 | */ |
Christoph Hellwig | 51d6269 | 2018-07-17 16:51:51 -0700 | [diff] [blame] | 392 | if (xfs_inode_has_cow_data(ip) && |
Christoph Hellwig | e666aa3 | 2018-07-17 16:51:52 -0700 | [diff] [blame] | 393 | xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &imap)) |
| 394 | cow_fsb = imap.br_startoff; |
| 395 | if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) { |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 396 | XFS_WPC(wpc)->cow_seq = READ_ONCE(ip->i_cowfp->if_seq); |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 397 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 398 | |
Christoph Hellwig | 760fea8 | 2019-10-17 13:12:10 -0700 | [diff] [blame] | 399 | whichfork = XFS_COW_FORK; |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 400 | goto allocate_blocks; |
| 401 | } |
| 402 | |
| 403 | /* |
Brian Foster | d9252d5 | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 404 | * No COW extent overlap. Revalidate now that we may have updated |
| 405 | * ->cow_seq. If the data mapping is still valid, we're done. |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 406 | */ |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 407 | if (xfs_imap_valid(wpc, ip, offset)) { |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 408 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * If we don't have a valid map, now it's time to get a new one for this |
| 414 | * offset. This will convert delayed allocations (including COW ones) |
| 415 | * into real extents. |
| 416 | */ |
Christoph Hellwig | 3345746 | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 417 | if (!xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap)) |
| 418 | imap.br_startoff = end_fsb; /* fake a hole past EOF */ |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 419 | XFS_WPC(wpc)->data_seq = READ_ONCE(ip->i_df.if_seq); |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 420 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 421 | |
Christoph Hellwig | 12df89f | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 422 | /* landed in a hole or beyond EOF? */ |
Christoph Hellwig | 3345746 | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 423 | if (imap.br_startoff > offset_fsb) { |
Christoph Hellwig | 3345746 | 2018-07-11 22:26:02 -0700 | [diff] [blame] | 424 | imap.br_blockcount = imap.br_startoff - offset_fsb; |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 425 | imap.br_startoff = offset_fsb; |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 426 | imap.br_startblock = HOLESTARTBLOCK; |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 427 | imap.br_state = XFS_EXT_NORM; |
Christoph Hellwig | a206c81 | 2010-12-10 08:42:20 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Christoph Hellwig | 12df89f | 2019-02-18 09:38:47 -0800 | [diff] [blame] | 430 | /* |
| 431 | * Truncate to the next COW extent if there is one. This is the only |
| 432 | * opportunity to do this because we can skip COW fork lookups for the |
| 433 | * subsequent blocks in the mapping; however, the requirement to treat |
| 434 | * the COW range separately remains. |
| 435 | */ |
| 436 | if (cow_fsb != NULLFILEOFF && |
| 437 | cow_fsb < imap.br_startoff + imap.br_blockcount) |
| 438 | imap.br_blockcount = cow_fsb - imap.br_startoff; |
| 439 | |
| 440 | /* got a delalloc extent? */ |
| 441 | if (imap.br_startblock != HOLESTARTBLOCK && |
| 442 | isnullstartblock(imap.br_startblock)) |
| 443 | goto allocate_blocks; |
| 444 | |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 445 | xfs_bmbt_to_iomap(ip, &wpc->iomap, &imap, 0); |
Christoph Hellwig | 760fea8 | 2019-10-17 13:12:10 -0700 | [diff] [blame] | 446 | trace_xfs_map_blocks_found(ip, offset, count, whichfork, &imap); |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 447 | return 0; |
| 448 | allocate_blocks: |
Christoph Hellwig | 760fea8 | 2019-10-17 13:12:10 -0700 | [diff] [blame] | 449 | error = xfs_convert_blocks(wpc, ip, whichfork, offset); |
Christoph Hellwig | 7588cbe | 2019-02-15 08:02:50 -0800 | [diff] [blame] | 450 | if (error) { |
| 451 | /* |
| 452 | * If we failed to find the extent in the COW fork we might have |
| 453 | * raced with a COW to data fork conversion or truncate. |
| 454 | * Restart the lookup to catch the extent in the data fork for |
| 455 | * the former case, but prevent additional retries to avoid |
| 456 | * looping forever for the latter case. |
| 457 | */ |
Christoph Hellwig | 760fea8 | 2019-10-17 13:12:10 -0700 | [diff] [blame] | 458 | if (error == -EAGAIN && whichfork == XFS_COW_FORK && !retries++) |
Christoph Hellwig | 7588cbe | 2019-02-15 08:02:50 -0800 | [diff] [blame] | 459 | goto retry; |
| 460 | ASSERT(error != -EAGAIN); |
Christoph Hellwig | 5c665e5 | 2018-07-11 22:25:59 -0700 | [diff] [blame] | 461 | return error; |
Christoph Hellwig | 7588cbe | 2019-02-15 08:02:50 -0800 | [diff] [blame] | 462 | } |
Christoph Hellwig | 4ad765e | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 463 | |
| 464 | /* |
| 465 | * Due to merging the return real extent might be larger than the |
| 466 | * original delalloc one. Trim the return extent to the next COW |
| 467 | * boundary again to force a re-lookup. |
| 468 | */ |
Christoph Hellwig | 760fea8 | 2019-10-17 13:12:10 -0700 | [diff] [blame] | 469 | if (whichfork != XFS_COW_FORK && cow_fsb != NULLFILEOFF) { |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 470 | loff_t cow_offset = XFS_FSB_TO_B(mp, cow_fsb); |
Christoph Hellwig | 4ad765e | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 471 | |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 472 | if (cow_offset < wpc->iomap.offset + wpc->iomap.length) |
| 473 | wpc->iomap.length = cow_offset - wpc->iomap.offset; |
| 474 | } |
| 475 | |
| 476 | ASSERT(wpc->iomap.offset <= offset); |
| 477 | ASSERT(wpc->iomap.offset + wpc->iomap.length > offset); |
Christoph Hellwig | 760fea8 | 2019-10-17 13:12:10 -0700 | [diff] [blame] | 478 | trace_xfs_map_blocks_alloc(ip, offset, count, whichfork, &imap); |
Christoph Hellwig | 8ff2957 | 2010-12-10 08:42:21 +0000 | [diff] [blame] | 479 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 482 | static int |
| 483 | xfs_prepare_ioend( |
| 484 | struct iomap_ioend *ioend, |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 485 | int status) |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 486 | { |
Christoph Hellwig | 73d30d4 | 2019-06-28 19:31:38 -0700 | [diff] [blame] | 487 | unsigned int nofs_flag; |
| 488 | |
| 489 | /* |
| 490 | * We can allocate memory here while doing writeback on behalf of |
| 491 | * memory reclaim. To avoid memory allocation deadlocks set the |
| 492 | * task-wide nofs context for the following operations. |
| 493 | */ |
| 494 | nofs_flag = memalloc_nofs_save(); |
| 495 | |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 496 | /* Convert CoW extents to regular */ |
Christoph Hellwig | 760fea8 | 2019-10-17 13:12:10 -0700 | [diff] [blame] | 497 | if (!status && (ioend->io_flags & IOMAP_F_SHARED)) { |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 498 | status = xfs_reflink_convert_cow(XFS_I(ioend->io_inode), |
| 499 | ioend->io_offset, ioend->io_size); |
| 500 | } |
| 501 | |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 502 | /* Reserve log space if we might write beyond the on-disk inode size. */ |
| 503 | if (!status && |
Christoph Hellwig | 760fea8 | 2019-10-17 13:12:10 -0700 | [diff] [blame] | 504 | ((ioend->io_flags & IOMAP_F_SHARED) || |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 505 | ioend->io_type != IOMAP_UNWRITTEN) && |
Dave Chinner | bb18782 | 2016-04-06 08:11:25 +1000 | [diff] [blame] | 506 | xfs_ioend_is_append(ioend) && |
Christoph Hellwig | 5653017 | 2019-10-17 13:12:09 -0700 | [diff] [blame] | 507 | !ioend->io_private) |
Dave Chinner | e10de37 | 2016-02-15 17:23:12 +1100 | [diff] [blame] | 508 | status = xfs_setfilesize_trans_alloc(ioend); |
Dave Chinner | bb18782 | 2016-04-06 08:11:25 +1000 | [diff] [blame] | 509 | |
Christoph Hellwig | 73d30d4 | 2019-06-28 19:31:38 -0700 | [diff] [blame] | 510 | memalloc_nofs_restore(nofs_flag); |
| 511 | |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 512 | if (xfs_ioend_needs_workqueue(ioend)) |
| 513 | ioend->io_bio->bi_end_io = xfs_end_bio; |
| 514 | return status; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 515 | } |
| 516 | |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 517 | /* |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 518 | * If the page has delalloc blocks on it, we need to punch them out before we |
| 519 | * invalidate the page. If we don't, we leave a stale delalloc mapping on the |
| 520 | * 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] | 521 | * |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 522 | * We prevent this by truncating away the delalloc regions on the page. Because |
| 523 | * they are delalloc, we can do this without needing a transaction. Indeed - if |
| 524 | * we get ENOSPC errors, we have to be able to do this truncation without a |
| 525 | * transaction as there is no space left for block reservation (typically why we |
| 526 | * see a ENOSPC in writeback). |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 527 | */ |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 528 | static void |
| 529 | xfs_discard_page( |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 530 | struct page *page) |
| 531 | { |
| 532 | struct inode *inode = page->mapping->host; |
| 533 | struct xfs_inode *ip = XFS_I(inode); |
Christoph Hellwig | 0362572 | 2018-07-11 22:25:57 -0700 | [diff] [blame] | 534 | struct xfs_mount *mp = ip->i_mount; |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 535 | loff_t offset = page_offset(page); |
Christoph Hellwig | 0362572 | 2018-07-11 22:25:57 -0700 | [diff] [blame] | 536 | xfs_fileoff_t start_fsb = XFS_B_TO_FSBT(mp, offset); |
| 537 | int error; |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 538 | |
Christoph Hellwig | 0362572 | 2018-07-11 22:25:57 -0700 | [diff] [blame] | 539 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 540 | goto out_invalidate; |
| 541 | |
Christoph Hellwig | 4ab45e2 | 2020-02-21 07:34:48 -0800 | [diff] [blame] | 542 | xfs_alert_ratelimited(mp, |
Darrick J. Wong | c969004 | 2018-01-09 12:02:55 -0800 | [diff] [blame] | 543 | "page discard on page "PTR_FMT", inode 0x%llx, offset %llu.", |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 544 | page, ip->i_ino, offset); |
| 545 | |
Christoph Hellwig | 0362572 | 2018-07-11 22:25:57 -0700 | [diff] [blame] | 546 | error = xfs_bmap_punch_delalloc_range(ip, start_fsb, |
| 547 | PAGE_SIZE / i_blocksize(inode)); |
Christoph Hellwig | 0362572 | 2018-07-11 22:25:57 -0700 | [diff] [blame] | 548 | if (error && !XFS_FORCED_SHUTDOWN(mp)) |
| 549 | xfs_alert(mp, "page discard unable to remove delalloc mapping."); |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 550 | out_invalidate: |
Christoph Hellwig | 9e91c57 | 2019-10-17 13:12:13 -0700 | [diff] [blame] | 551 | iomap_invalidatepage(page, 0, PAGE_SIZE); |
Dave Chinner | 3ed3a43 | 2010-03-05 02:00:42 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 554 | static const struct iomap_writeback_ops xfs_writeback_ops = { |
| 555 | .map_blocks = xfs_map_blocks, |
| 556 | .prepare_ioend = xfs_prepare_ioend, |
| 557 | .discard_page = xfs_discard_page, |
| 558 | }; |
Nathan Scott | f51623b | 2006-03-14 13:26:27 +1100 | [diff] [blame] | 559 | |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 560 | STATIC int |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 561 | xfs_vm_writepage( |
| 562 | struct page *page, |
| 563 | struct writeback_control *wbc) |
| 564 | { |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 565 | struct xfs_writepage_ctx wpc = { }; |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 566 | |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 567 | return iomap_writepage(page, wbc, &wpc.ctx, &xfs_writeback_ops); |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | STATIC int |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 571 | xfs_vm_writepages( |
| 572 | struct address_space *mapping, |
| 573 | struct writeback_control *wbc) |
| 574 | { |
Christoph Hellwig | be225fe | 2019-02-15 08:02:46 -0800 | [diff] [blame] | 575 | struct xfs_writepage_ctx wpc = { }; |
Dave Chinner | fbcc025 | 2016-02-15 17:21:19 +1100 | [diff] [blame] | 576 | |
Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 577 | xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED); |
Christoph Hellwig | 598ecfb | 2019-10-17 13:12:15 -0700 | [diff] [blame] | 578 | return iomap_writepages(mapping, wbc, &wpc.ctx, &xfs_writeback_ops); |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 579 | } |
| 580 | |
Dan Williams | 6e2608d | 2018-03-07 15:26:44 -0800 | [diff] [blame] | 581 | STATIC int |
| 582 | xfs_dax_writepages( |
| 583 | struct address_space *mapping, |
| 584 | struct writeback_control *wbc) |
| 585 | { |
Christoph Hellwig | 30fa529 | 2019-10-24 22:25:38 -0700 | [diff] [blame] | 586 | struct xfs_inode *ip = XFS_I(mapping->host); |
| 587 | |
| 588 | xfs_iflags_clear(ip, XFS_ITRUNCATED); |
Dan Williams | 6e2608d | 2018-03-07 15:26:44 -0800 | [diff] [blame] | 589 | return dax_writeback_mapping_range(mapping, |
Vivek Goyal | 3f666c5 | 2020-01-03 13:33:07 -0500 | [diff] [blame] | 590 | xfs_inode_buftarg(ip)->bt_daxdev, wbc); |
Dan Williams | 6e2608d | 2018-03-07 15:26:44 -0800 | [diff] [blame] | 591 | } |
| 592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | STATIC sector_t |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 594 | xfs_vm_bmap( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | struct address_space *mapping, |
| 596 | sector_t block) |
| 597 | { |
Christoph Hellwig | b84e772 | 2018-06-01 09:03:09 -0700 | [diff] [blame] | 598 | struct xfs_inode *ip = XFS_I(mapping->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
Christoph Hellwig | b84e772 | 2018-06-01 09:03:09 -0700 | [diff] [blame] | 600 | trace_xfs_vm_bmap(ip); |
Darrick J. Wong | db1327b | 2016-10-03 09:11:36 -0700 | [diff] [blame] | 601 | |
| 602 | /* |
| 603 | * 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] | 604 | * 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] | 605 | * 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] | 606 | * 0 is the magic code for a bmap error. |
| 607 | * |
| 608 | * Since we don't pass back blockdev info, we can't return bmap |
| 609 | * information for rt files either. |
Darrick J. Wong | db1327b | 2016-10-03 09:11:36 -0700 | [diff] [blame] | 610 | */ |
Christoph Hellwig | 66ae56a | 2019-02-18 09:38:49 -0800 | [diff] [blame] | 611 | if (xfs_is_cow_inode(ip) || XFS_IS_REALTIME_INODE(ip)) |
Darrick J. Wong | db1327b | 2016-10-03 09:11:36 -0700 | [diff] [blame] | 612 | return 0; |
Christoph Hellwig | 690c2a3 | 2019-10-19 09:09:45 -0700 | [diff] [blame] | 613 | return iomap_bmap(mapping, block, &xfs_read_iomap_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | STATIC int |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 617 | xfs_vm_readpage( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | struct file *unused, |
| 619 | struct page *page) |
| 620 | { |
Christoph Hellwig | 690c2a3 | 2019-10-19 09:09:45 -0700 | [diff] [blame] | 621 | return iomap_readpage(page, &xfs_read_iomap_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Matthew Wilcox (Oracle) | 9d24a13 | 2020-06-01 21:47:34 -0700 | [diff] [blame] | 624 | STATIC void |
| 625 | xfs_vm_readahead( |
| 626 | struct readahead_control *rac) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | { |
Matthew Wilcox (Oracle) | 9d24a13 | 2020-06-01 21:47:34 -0700 | [diff] [blame] | 628 | iomap_readahead(rac, &xfs_read_iomap_ops); |
Dave Chinner | 22e757a | 2014-09-02 12:12:51 +1000 | [diff] [blame] | 629 | } |
| 630 | |
Darrick J. Wong | 6748212 | 2018-05-10 08:38:15 -0700 | [diff] [blame] | 631 | static int |
| 632 | xfs_iomap_swapfile_activate( |
| 633 | struct swap_info_struct *sis, |
| 634 | struct file *swap_file, |
| 635 | sector_t *span) |
| 636 | { |
Christoph Hellwig | 30fa529 | 2019-10-24 22:25:38 -0700 | [diff] [blame] | 637 | sis->bdev = xfs_inode_buftarg(XFS_I(file_inode(swap_file)))->bt_bdev; |
Christoph Hellwig | 690c2a3 | 2019-10-19 09:09:45 -0700 | [diff] [blame] | 638 | return iomap_swapfile_activate(sis, swap_file, span, |
| 639 | &xfs_read_iomap_ops); |
Darrick J. Wong | 6748212 | 2018-05-10 08:38:15 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 642 | const struct address_space_operations xfs_address_space_operations = { |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 643 | .readpage = xfs_vm_readpage, |
Matthew Wilcox (Oracle) | 9d24a13 | 2020-06-01 21:47:34 -0700 | [diff] [blame] | 644 | .readahead = xfs_vm_readahead, |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 645 | .writepage = xfs_vm_writepage, |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 646 | .writepages = xfs_vm_writepages, |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 647 | .set_page_dirty = iomap_set_page_dirty, |
Christoph Hellwig | 9e91c57 | 2019-10-17 13:12:13 -0700 | [diff] [blame] | 648 | .releasepage = iomap_releasepage, |
| 649 | .invalidatepage = iomap_invalidatepage, |
Nathan Scott | e4c573b | 2006-03-14 13:54:26 +1100 | [diff] [blame] | 650 | .bmap = xfs_vm_bmap, |
Dan Williams | 6e2608d | 2018-03-07 15:26:44 -0800 | [diff] [blame] | 651 | .direct_IO = noop_direct_IO, |
Christoph Hellwig | 82cb141 | 2018-07-11 22:26:05 -0700 | [diff] [blame] | 652 | .migratepage = iomap_migrate_page, |
| 653 | .is_partially_uptodate = iomap_is_partially_uptodate, |
Andi Kleen | aa261f5 | 2009-09-16 11:50:16 +0200 | [diff] [blame] | 654 | .error_remove_page = generic_error_remove_page, |
Darrick J. Wong | 6748212 | 2018-05-10 08:38:15 -0700 | [diff] [blame] | 655 | .swap_activate = xfs_iomap_swapfile_activate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | }; |
Dan Williams | 6e2608d | 2018-03-07 15:26:44 -0800 | [diff] [blame] | 657 | |
| 658 | const struct address_space_operations xfs_dax_aops = { |
| 659 | .writepages = xfs_dax_writepages, |
| 660 | .direct_IO = noop_direct_IO, |
| 661 | .set_page_dirty = noop_set_page_dirty, |
| 662 | .invalidatepage = noop_invalidatepage, |
Darrick J. Wong | 6748212 | 2018-05-10 08:38:15 -0700 | [diff] [blame] | 663 | .swap_activate = xfs_iomap_swapfile_activate, |
Dan Williams | 6e2608d | 2018-03-07 15:26:44 -0800 | [diff] [blame] | 664 | }; |