Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Oracle. All Rights Reserved. |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 4 | * Author: Darrick J. Wong <darrick.wong@oracle.com> |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 5 | */ |
| 6 | #include "xfs.h" |
| 7 | #include "xfs_fs.h" |
| 8 | #include "xfs_format.h" |
| 9 | #include "xfs_log_format.h" |
| 10 | #include "xfs_trans_resv.h" |
Darrick J. Wong | 9e88b5d | 2016-08-03 12:09:48 +1000 | [diff] [blame] | 11 | #include "xfs_bit.h" |
Darrick J. Wong | b31c2bd | 2018-02-22 14:41:25 -0800 | [diff] [blame] | 12 | #include "xfs_shared.h" |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 13 | #include "xfs_mount.h" |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 14 | #include "xfs_defer.h" |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 15 | #include "xfs_trans.h" |
| 16 | #include "xfs_trans_priv.h" |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 17 | #include "xfs_rmap_item.h" |
| 18 | #include "xfs_log.h" |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 19 | #include "xfs_rmap.h" |
Darrick J. Wong | a5155b8 | 2019-11-02 09:40:53 -0700 | [diff] [blame] | 20 | #include "xfs_error.h" |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 21 | |
| 22 | kmem_zone_t *xfs_rui_zone; |
| 23 | kmem_zone_t *xfs_rud_zone; |
| 24 | |
| 25 | static inline struct xfs_rui_log_item *RUI_ITEM(struct xfs_log_item *lip) |
| 26 | { |
| 27 | return container_of(lip, struct xfs_rui_log_item, rui_item); |
| 28 | } |
| 29 | |
| 30 | void |
| 31 | xfs_rui_item_free( |
| 32 | struct xfs_rui_log_item *ruip) |
| 33 | { |
| 34 | if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS) |
| 35 | kmem_free(ruip); |
| 36 | else |
Carlos Maiolino | 377bcd5 | 2019-11-14 12:43:04 -0800 | [diff] [blame] | 37 | kmem_cache_free(xfs_rui_zone, ruip); |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 38 | } |
| 39 | |
Dave Chinner | 0612d11 | 2018-04-02 20:08:27 -0700 | [diff] [blame] | 40 | /* |
| 41 | * Freeing the RUI requires that we remove it from the AIL if it has already |
| 42 | * been placed there. However, the RUI may not yet have been placed in the AIL |
| 43 | * when called by xfs_rui_release() from RUD processing due to the ordering of |
| 44 | * committed vs unpin operations in bulk insert operations. Hence the reference |
| 45 | * count to ensure only the last caller frees the RUI. |
| 46 | */ |
| 47 | void |
| 48 | xfs_rui_release( |
| 49 | struct xfs_rui_log_item *ruip) |
| 50 | { |
| 51 | ASSERT(atomic_read(&ruip->rui_refcount) > 0); |
| 52 | if (atomic_dec_and_test(&ruip->rui_refcount)) { |
| 53 | xfs_trans_ail_remove(&ruip->rui_item, SHUTDOWN_LOG_IO_ERROR); |
| 54 | xfs_rui_item_free(ruip); |
| 55 | } |
| 56 | } |
| 57 | |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 58 | STATIC void |
| 59 | xfs_rui_item_size( |
| 60 | struct xfs_log_item *lip, |
| 61 | int *nvecs, |
| 62 | int *nbytes) |
| 63 | { |
Darrick J. Wong | cd00158 | 2016-09-19 10:24:27 +1000 | [diff] [blame] | 64 | struct xfs_rui_log_item *ruip = RUI_ITEM(lip); |
| 65 | |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 66 | *nvecs += 1; |
Darrick J. Wong | cd00158 | 2016-09-19 10:24:27 +1000 | [diff] [blame] | 67 | *nbytes += xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents); |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* |
| 71 | * This is called to fill in the vector of log iovecs for the |
| 72 | * given rui log item. We use only 1 iovec, and we point that |
| 73 | * at the rui_log_format structure embedded in the rui item. |
| 74 | * It is at this point that we assert that all of the extent |
| 75 | * slots in the rui item have been filled. |
| 76 | */ |
| 77 | STATIC void |
| 78 | xfs_rui_item_format( |
| 79 | struct xfs_log_item *lip, |
| 80 | struct xfs_log_vec *lv) |
| 81 | { |
| 82 | struct xfs_rui_log_item *ruip = RUI_ITEM(lip); |
| 83 | struct xfs_log_iovec *vecp = NULL; |
| 84 | |
| 85 | ASSERT(atomic_read(&ruip->rui_next_extent) == |
| 86 | ruip->rui_format.rui_nextents); |
| 87 | |
| 88 | ruip->rui_format.rui_type = XFS_LI_RUI; |
| 89 | ruip->rui_format.rui_size = 1; |
| 90 | |
| 91 | xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format, |
Darrick J. Wong | cd00158 | 2016-09-19 10:24:27 +1000 | [diff] [blame] | 92 | xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents)); |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 96 | * The unpin operation is the last place an RUI is manipulated in the log. It is |
| 97 | * either inserted in the AIL or aborted in the event of a log I/O error. In |
| 98 | * either case, the RUI transaction has been successfully committed to make it |
| 99 | * this far. Therefore, we expect whoever committed the RUI to either construct |
| 100 | * and commit the RUD or drop the RUD's reference in the event of error. Simply |
| 101 | * drop the log's RUI reference now that the log is done with it. |
| 102 | */ |
| 103 | STATIC void |
| 104 | xfs_rui_item_unpin( |
| 105 | struct xfs_log_item *lip, |
| 106 | int remove) |
| 107 | { |
| 108 | struct xfs_rui_log_item *ruip = RUI_ITEM(lip); |
| 109 | |
| 110 | xfs_rui_release(ruip); |
| 111 | } |
| 112 | |
| 113 | /* |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 114 | * The RUI has been either committed or aborted if the transaction has been |
| 115 | * cancelled. If the transaction was cancelled, an RUD isn't going to be |
| 116 | * constructed and thus we free the RUI here directly. |
| 117 | */ |
| 118 | STATIC void |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 119 | xfs_rui_item_release( |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 120 | struct xfs_log_item *lip) |
| 121 | { |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 122 | xfs_rui_release(RUI_ITEM(lip)); |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 123 | } |
| 124 | |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 125 | static const struct xfs_item_ops xfs_rui_item_ops = { |
| 126 | .iop_size = xfs_rui_item_size, |
| 127 | .iop_format = xfs_rui_item_format, |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 128 | .iop_unpin = xfs_rui_item_unpin, |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 129 | .iop_release = xfs_rui_item_release, |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | /* |
| 133 | * Allocate and initialize an rui item with the given number of extents. |
| 134 | */ |
| 135 | struct xfs_rui_log_item * |
| 136 | xfs_rui_init( |
| 137 | struct xfs_mount *mp, |
| 138 | uint nextents) |
| 139 | |
| 140 | { |
| 141 | struct xfs_rui_log_item *ruip; |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 142 | |
| 143 | ASSERT(nextents > 0); |
Darrick J. Wong | cd00158 | 2016-09-19 10:24:27 +1000 | [diff] [blame] | 144 | if (nextents > XFS_RUI_MAX_FAST_EXTENTS) |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame] | 145 | ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), 0); |
Darrick J. Wong | cd00158 | 2016-09-19 10:24:27 +1000 | [diff] [blame] | 146 | else |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame] | 147 | ruip = kmem_zone_zalloc(xfs_rui_zone, 0); |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 148 | |
| 149 | xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops); |
| 150 | ruip->rui_format.rui_nextents = nextents; |
| 151 | ruip->rui_format.rui_id = (uintptr_t)(void *)ruip; |
| 152 | atomic_set(&ruip->rui_next_extent, 0); |
| 153 | atomic_set(&ruip->rui_refcount, 2); |
| 154 | |
| 155 | return ruip; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Copy an RUI format buffer from the given buf, and into the destination |
| 160 | * RUI format structure. The RUI/RUD items were designed not to need any |
| 161 | * special alignment handling. |
| 162 | */ |
| 163 | int |
| 164 | xfs_rui_copy_format( |
| 165 | struct xfs_log_iovec *buf, |
| 166 | struct xfs_rui_log_format *dst_rui_fmt) |
| 167 | { |
| 168 | struct xfs_rui_log_format *src_rui_fmt; |
| 169 | uint len; |
| 170 | |
| 171 | src_rui_fmt = buf->i_addr; |
Darrick J. Wong | cd00158 | 2016-09-19 10:24:27 +1000 | [diff] [blame] | 172 | len = xfs_rui_log_format_sizeof(src_rui_fmt->rui_nextents); |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 173 | |
Darrick J. Wong | a5155b8 | 2019-11-02 09:40:53 -0700 | [diff] [blame] | 174 | if (buf->i_len != len) { |
| 175 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL); |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 176 | return -EFSCORRUPTED; |
Darrick J. Wong | a5155b8 | 2019-11-02 09:40:53 -0700 | [diff] [blame] | 177 | } |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 178 | |
Darrick J. Wong | cd00158 | 2016-09-19 10:24:27 +1000 | [diff] [blame] | 179 | memcpy(dst_rui_fmt, src_rui_fmt, len); |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 183 | static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip) |
| 184 | { |
| 185 | return container_of(lip, struct xfs_rud_log_item, rud_item); |
| 186 | } |
| 187 | |
| 188 | STATIC void |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 189 | xfs_rud_item_size( |
| 190 | struct xfs_log_item *lip, |
| 191 | int *nvecs, |
| 192 | int *nbytes) |
| 193 | { |
| 194 | *nvecs += 1; |
Darrick J. Wong | 722e251 | 2016-08-03 12:28:43 +1000 | [diff] [blame] | 195 | *nbytes += sizeof(struct xfs_rud_log_format); |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* |
| 199 | * This is called to fill in the vector of log iovecs for the |
| 200 | * given rud log item. We use only 1 iovec, and we point that |
| 201 | * at the rud_log_format structure embedded in the rud item. |
| 202 | * It is at this point that we assert that all of the extent |
| 203 | * slots in the rud item have been filled. |
| 204 | */ |
| 205 | STATIC void |
| 206 | xfs_rud_item_format( |
| 207 | struct xfs_log_item *lip, |
| 208 | struct xfs_log_vec *lv) |
| 209 | { |
| 210 | struct xfs_rud_log_item *rudp = RUD_ITEM(lip); |
| 211 | struct xfs_log_iovec *vecp = NULL; |
| 212 | |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 213 | rudp->rud_format.rud_type = XFS_LI_RUD; |
| 214 | rudp->rud_format.rud_size = 1; |
| 215 | |
| 216 | xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format, |
Darrick J. Wong | 722e251 | 2016-08-03 12:28:43 +1000 | [diff] [blame] | 217 | sizeof(struct xfs_rud_log_format)); |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 221 | * The RUD is either committed or aborted if the transaction is cancelled. If |
| 222 | * the transaction is cancelled, drop our reference to the RUI and free the |
| 223 | * RUD. |
| 224 | */ |
| 225 | STATIC void |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 226 | xfs_rud_item_release( |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 227 | struct xfs_log_item *lip) |
| 228 | { |
| 229 | struct xfs_rud_log_item *rudp = RUD_ITEM(lip); |
| 230 | |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 231 | xfs_rui_release(rudp->rud_ruip); |
Carlos Maiolino | 377bcd5 | 2019-11-14 12:43:04 -0800 | [diff] [blame] | 232 | kmem_cache_free(xfs_rud_zone, rudp); |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 233 | } |
| 234 | |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 235 | static const struct xfs_item_ops xfs_rud_item_ops = { |
Christoph Hellwig | 9ce632a | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 236 | .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED, |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 237 | .iop_size = xfs_rud_item_size, |
| 238 | .iop_format = xfs_rud_item_format, |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 239 | .iop_release = xfs_rud_item_release, |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 240 | }; |
| 241 | |
Christoph Hellwig | 3cfce1e | 2019-06-28 19:29:41 -0700 | [diff] [blame] | 242 | static struct xfs_rud_log_item * |
Christoph Hellwig | 60883447 | 2019-06-28 19:27:36 -0700 | [diff] [blame] | 243 | xfs_trans_get_rud( |
| 244 | struct xfs_trans *tp, |
Darrick J. Wong | 722e251 | 2016-08-03 12:28:43 +1000 | [diff] [blame] | 245 | struct xfs_rui_log_item *ruip) |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 246 | { |
Christoph Hellwig | 60883447 | 2019-06-28 19:27:36 -0700 | [diff] [blame] | 247 | struct xfs_rud_log_item *rudp; |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 248 | |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame] | 249 | rudp = kmem_zone_zalloc(xfs_rud_zone, 0); |
Christoph Hellwig | 60883447 | 2019-06-28 19:27:36 -0700 | [diff] [blame] | 250 | xfs_log_item_init(tp->t_mountp, &rudp->rud_item, XFS_LI_RUD, |
| 251 | &xfs_rud_item_ops); |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 252 | rudp->rud_ruip = ruip; |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 253 | rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id; |
| 254 | |
Christoph Hellwig | 60883447 | 2019-06-28 19:27:36 -0700 | [diff] [blame] | 255 | xfs_trans_add_item(tp, &rudp->rud_item); |
Darrick J. Wong | 5880f2d7 | 2016-08-03 12:04:45 +1000 | [diff] [blame] | 256 | return rudp; |
| 257 | } |
Darrick J. Wong | 9e88b5d | 2016-08-03 12:09:48 +1000 | [diff] [blame] | 258 | |
Christoph Hellwig | 3cfce1e | 2019-06-28 19:29:41 -0700 | [diff] [blame] | 259 | /* Set the map extent flags for this reverse mapping. */ |
| 260 | static void |
| 261 | xfs_trans_set_rmap_flags( |
| 262 | struct xfs_map_extent *rmap, |
| 263 | enum xfs_rmap_intent_type type, |
| 264 | int whichfork, |
| 265 | xfs_exntst_t state) |
| 266 | { |
| 267 | rmap->me_flags = 0; |
| 268 | if (state == XFS_EXT_UNWRITTEN) |
| 269 | rmap->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN; |
| 270 | if (whichfork == XFS_ATTR_FORK) |
| 271 | rmap->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK; |
| 272 | switch (type) { |
| 273 | case XFS_RMAP_MAP: |
| 274 | rmap->me_flags |= XFS_RMAP_EXTENT_MAP; |
| 275 | break; |
| 276 | case XFS_RMAP_MAP_SHARED: |
| 277 | rmap->me_flags |= XFS_RMAP_EXTENT_MAP_SHARED; |
| 278 | break; |
| 279 | case XFS_RMAP_UNMAP: |
| 280 | rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP; |
| 281 | break; |
| 282 | case XFS_RMAP_UNMAP_SHARED: |
| 283 | rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP_SHARED; |
| 284 | break; |
| 285 | case XFS_RMAP_CONVERT: |
| 286 | rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT; |
| 287 | break; |
| 288 | case XFS_RMAP_CONVERT_SHARED: |
| 289 | rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT_SHARED; |
| 290 | break; |
| 291 | case XFS_RMAP_ALLOC: |
| 292 | rmap->me_flags |= XFS_RMAP_EXTENT_ALLOC; |
| 293 | break; |
| 294 | case XFS_RMAP_FREE: |
| 295 | rmap->me_flags |= XFS_RMAP_EXTENT_FREE; |
| 296 | break; |
| 297 | default: |
| 298 | ASSERT(0); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * Finish an rmap update and log it to the RUD. Note that the transaction is |
| 304 | * marked dirty regardless of whether the rmap update succeeds or fails to |
| 305 | * support the RUI/RUD lifecycle rules. |
| 306 | */ |
| 307 | static int |
| 308 | xfs_trans_log_finish_rmap_update( |
| 309 | struct xfs_trans *tp, |
| 310 | struct xfs_rud_log_item *rudp, |
| 311 | enum xfs_rmap_intent_type type, |
| 312 | uint64_t owner, |
| 313 | int whichfork, |
| 314 | xfs_fileoff_t startoff, |
| 315 | xfs_fsblock_t startblock, |
| 316 | xfs_filblks_t blockcount, |
| 317 | xfs_exntst_t state, |
| 318 | struct xfs_btree_cur **pcur) |
| 319 | { |
| 320 | int error; |
| 321 | |
| 322 | error = xfs_rmap_finish_one(tp, type, owner, whichfork, startoff, |
| 323 | startblock, blockcount, state, pcur); |
| 324 | |
| 325 | /* |
| 326 | * Mark the transaction dirty, even on error. This ensures the |
| 327 | * transaction is aborted, which: |
| 328 | * |
| 329 | * 1.) releases the RUI and frees the RUD |
| 330 | * 2.) shuts down the filesystem |
| 331 | */ |
| 332 | tp->t_flags |= XFS_TRANS_DIRTY; |
| 333 | set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags); |
| 334 | |
| 335 | return error; |
| 336 | } |
| 337 | |
| 338 | /* Sort rmap intents by AG. */ |
| 339 | static int |
| 340 | xfs_rmap_update_diff_items( |
| 341 | void *priv, |
| 342 | struct list_head *a, |
| 343 | struct list_head *b) |
| 344 | { |
| 345 | struct xfs_mount *mp = priv; |
| 346 | struct xfs_rmap_intent *ra; |
| 347 | struct xfs_rmap_intent *rb; |
| 348 | |
| 349 | ra = container_of(a, struct xfs_rmap_intent, ri_list); |
| 350 | rb = container_of(b, struct xfs_rmap_intent, ri_list); |
| 351 | return XFS_FSB_TO_AGNO(mp, ra->ri_bmap.br_startblock) - |
| 352 | XFS_FSB_TO_AGNO(mp, rb->ri_bmap.br_startblock); |
| 353 | } |
| 354 | |
Christoph Hellwig | 3cfce1e | 2019-06-28 19:29:41 -0700 | [diff] [blame] | 355 | /* Log rmap updates in the intent item. */ |
| 356 | STATIC void |
| 357 | xfs_rmap_update_log_item( |
| 358 | struct xfs_trans *tp, |
Christoph Hellwig | c1f0918 | 2020-04-30 12:52:20 -0700 | [diff] [blame] | 359 | struct xfs_rui_log_item *ruip, |
| 360 | struct xfs_rmap_intent *rmap) |
Christoph Hellwig | 3cfce1e | 2019-06-28 19:29:41 -0700 | [diff] [blame] | 361 | { |
Christoph Hellwig | 3cfce1e | 2019-06-28 19:29:41 -0700 | [diff] [blame] | 362 | uint next_extent; |
| 363 | struct xfs_map_extent *map; |
| 364 | |
Christoph Hellwig | 3cfce1e | 2019-06-28 19:29:41 -0700 | [diff] [blame] | 365 | tp->t_flags |= XFS_TRANS_DIRTY; |
| 366 | set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags); |
| 367 | |
| 368 | /* |
| 369 | * atomic_inc_return gives us the value after the increment; |
| 370 | * we want to use it as an array index so we need to subtract 1 from |
| 371 | * it. |
| 372 | */ |
| 373 | next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1; |
| 374 | ASSERT(next_extent < ruip->rui_format.rui_nextents); |
| 375 | map = &ruip->rui_format.rui_extents[next_extent]; |
| 376 | map->me_owner = rmap->ri_owner; |
| 377 | map->me_startblock = rmap->ri_bmap.br_startblock; |
| 378 | map->me_startoff = rmap->ri_bmap.br_startoff; |
| 379 | map->me_len = rmap->ri_bmap.br_blockcount; |
| 380 | xfs_trans_set_rmap_flags(map, rmap->ri_type, rmap->ri_whichfork, |
| 381 | rmap->ri_bmap.br_state); |
| 382 | } |
| 383 | |
Christoph Hellwig | c1f0918 | 2020-04-30 12:52:20 -0700 | [diff] [blame] | 384 | STATIC void * |
| 385 | xfs_rmap_update_create_intent( |
| 386 | struct xfs_trans *tp, |
| 387 | struct list_head *items, |
Christoph Hellwig | d367a86 | 2020-04-30 12:52:20 -0700 | [diff] [blame^] | 388 | unsigned int count, |
| 389 | bool sort) |
Christoph Hellwig | c1f0918 | 2020-04-30 12:52:20 -0700 | [diff] [blame] | 390 | { |
| 391 | struct xfs_mount *mp = tp->t_mountp; |
| 392 | struct xfs_rui_log_item *ruip = xfs_rui_init(mp, count); |
| 393 | struct xfs_rmap_intent *rmap; |
| 394 | |
| 395 | ASSERT(count > 0); |
| 396 | |
| 397 | xfs_trans_add_item(tp, &ruip->rui_item); |
Christoph Hellwig | d367a86 | 2020-04-30 12:52:20 -0700 | [diff] [blame^] | 398 | if (sort) |
| 399 | list_sort(mp, items, xfs_rmap_update_diff_items); |
Christoph Hellwig | c1f0918 | 2020-04-30 12:52:20 -0700 | [diff] [blame] | 400 | list_for_each_entry(rmap, items, ri_list) |
| 401 | xfs_rmap_update_log_item(tp, ruip, rmap); |
| 402 | return ruip; |
| 403 | } |
| 404 | |
Christoph Hellwig | 3cfce1e | 2019-06-28 19:29:41 -0700 | [diff] [blame] | 405 | /* Get an RUD so we can process all the deferred rmap updates. */ |
| 406 | STATIC void * |
| 407 | xfs_rmap_update_create_done( |
| 408 | struct xfs_trans *tp, |
| 409 | void *intent, |
| 410 | unsigned int count) |
| 411 | { |
| 412 | return xfs_trans_get_rud(tp, intent); |
| 413 | } |
| 414 | |
| 415 | /* Process a deferred rmap update. */ |
| 416 | STATIC int |
| 417 | xfs_rmap_update_finish_item( |
| 418 | struct xfs_trans *tp, |
| 419 | struct list_head *item, |
| 420 | void *done_item, |
| 421 | void **state) |
| 422 | { |
| 423 | struct xfs_rmap_intent *rmap; |
| 424 | int error; |
| 425 | |
| 426 | rmap = container_of(item, struct xfs_rmap_intent, ri_list); |
| 427 | error = xfs_trans_log_finish_rmap_update(tp, done_item, |
| 428 | rmap->ri_type, |
| 429 | rmap->ri_owner, rmap->ri_whichfork, |
| 430 | rmap->ri_bmap.br_startoff, |
| 431 | rmap->ri_bmap.br_startblock, |
| 432 | rmap->ri_bmap.br_blockcount, |
| 433 | rmap->ri_bmap.br_state, |
| 434 | (struct xfs_btree_cur **)state); |
| 435 | kmem_free(rmap); |
| 436 | return error; |
| 437 | } |
| 438 | |
| 439 | /* Clean up after processing deferred rmaps. */ |
| 440 | STATIC void |
| 441 | xfs_rmap_update_finish_cleanup( |
| 442 | struct xfs_trans *tp, |
| 443 | void *state, |
| 444 | int error) |
| 445 | { |
| 446 | struct xfs_btree_cur *rcur = state; |
| 447 | |
| 448 | xfs_rmap_finish_one_cleanup(tp, rcur, error); |
| 449 | } |
| 450 | |
| 451 | /* Abort all pending RUIs. */ |
| 452 | STATIC void |
| 453 | xfs_rmap_update_abort_intent( |
| 454 | void *intent) |
| 455 | { |
| 456 | xfs_rui_release(intent); |
| 457 | } |
| 458 | |
| 459 | /* Cancel a deferred rmap update. */ |
| 460 | STATIC void |
| 461 | xfs_rmap_update_cancel_item( |
| 462 | struct list_head *item) |
| 463 | { |
| 464 | struct xfs_rmap_intent *rmap; |
| 465 | |
| 466 | rmap = container_of(item, struct xfs_rmap_intent, ri_list); |
| 467 | kmem_free(rmap); |
| 468 | } |
| 469 | |
| 470 | const struct xfs_defer_op_type xfs_rmap_update_defer_type = { |
| 471 | .max_items = XFS_RUI_MAX_FAST_EXTENTS, |
Christoph Hellwig | 3cfce1e | 2019-06-28 19:29:41 -0700 | [diff] [blame] | 472 | .create_intent = xfs_rmap_update_create_intent, |
| 473 | .abort_intent = xfs_rmap_update_abort_intent, |
Christoph Hellwig | 3cfce1e | 2019-06-28 19:29:41 -0700 | [diff] [blame] | 474 | .create_done = xfs_rmap_update_create_done, |
| 475 | .finish_item = xfs_rmap_update_finish_item, |
| 476 | .finish_cleanup = xfs_rmap_update_finish_cleanup, |
| 477 | .cancel_item = xfs_rmap_update_cancel_item, |
| 478 | }; |
| 479 | |
Darrick J. Wong | 9e88b5d | 2016-08-03 12:09:48 +1000 | [diff] [blame] | 480 | /* |
| 481 | * Process an rmap update intent item that was recovered from the log. |
| 482 | * We need to update the rmapbt. |
| 483 | */ |
| 484 | int |
| 485 | xfs_rui_recover( |
| 486 | struct xfs_mount *mp, |
| 487 | struct xfs_rui_log_item *ruip) |
| 488 | { |
| 489 | int i; |
| 490 | int error = 0; |
| 491 | struct xfs_map_extent *rmap; |
| 492 | xfs_fsblock_t startblock_fsb; |
| 493 | bool op_ok; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 494 | struct xfs_rud_log_item *rudp; |
| 495 | enum xfs_rmap_intent_type type; |
| 496 | int whichfork; |
| 497 | xfs_exntst_t state; |
| 498 | struct xfs_trans *tp; |
| 499 | struct xfs_btree_cur *rcur = NULL; |
Darrick J. Wong | 9e88b5d | 2016-08-03 12:09:48 +1000 | [diff] [blame] | 500 | |
| 501 | ASSERT(!test_bit(XFS_RUI_RECOVERED, &ruip->rui_flags)); |
| 502 | |
| 503 | /* |
| 504 | * First check the validity of the extents described by the |
| 505 | * RUI. If any are bad, then assume that all are bad and |
| 506 | * just toss the RUI. |
| 507 | */ |
| 508 | for (i = 0; i < ruip->rui_format.rui_nextents; i++) { |
Darrick J. Wong | e127faf | 2016-08-03 12:29:32 +1000 | [diff] [blame] | 509 | rmap = &ruip->rui_format.rui_extents[i]; |
Darrick J. Wong | 9e88b5d | 2016-08-03 12:09:48 +1000 | [diff] [blame] | 510 | startblock_fsb = XFS_BB_TO_FSB(mp, |
| 511 | XFS_FSB_TO_DADDR(mp, rmap->me_startblock)); |
| 512 | switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) { |
| 513 | case XFS_RMAP_EXTENT_MAP: |
Darrick J. Wong | 0e07c03 | 2016-10-03 09:11:47 -0700 | [diff] [blame] | 514 | case XFS_RMAP_EXTENT_MAP_SHARED: |
Darrick J. Wong | 9e88b5d | 2016-08-03 12:09:48 +1000 | [diff] [blame] | 515 | case XFS_RMAP_EXTENT_UNMAP: |
Darrick J. Wong | 0e07c03 | 2016-10-03 09:11:47 -0700 | [diff] [blame] | 516 | case XFS_RMAP_EXTENT_UNMAP_SHARED: |
Darrick J. Wong | 9e88b5d | 2016-08-03 12:09:48 +1000 | [diff] [blame] | 517 | case XFS_RMAP_EXTENT_CONVERT: |
Darrick J. Wong | 0e07c03 | 2016-10-03 09:11:47 -0700 | [diff] [blame] | 518 | case XFS_RMAP_EXTENT_CONVERT_SHARED: |
Darrick J. Wong | 9e88b5d | 2016-08-03 12:09:48 +1000 | [diff] [blame] | 519 | case XFS_RMAP_EXTENT_ALLOC: |
| 520 | case XFS_RMAP_EXTENT_FREE: |
| 521 | op_ok = true; |
| 522 | break; |
| 523 | default: |
| 524 | op_ok = false; |
| 525 | break; |
| 526 | } |
Darrick J. Wong | e127faf | 2016-08-03 12:29:32 +1000 | [diff] [blame] | 527 | if (!op_ok || startblock_fsb == 0 || |
| 528 | rmap->me_len == 0 || |
| 529 | startblock_fsb >= mp->m_sb.sb_dblocks || |
| 530 | rmap->me_len >= mp->m_sb.sb_agblocks || |
Darrick J. Wong | 9e88b5d | 2016-08-03 12:09:48 +1000 | [diff] [blame] | 531 | (rmap->me_flags & ~XFS_RMAP_EXTENT_FLAGS)) { |
| 532 | /* |
| 533 | * This will pull the RUI from the AIL and |
| 534 | * free the memory associated with it. |
| 535 | */ |
| 536 | set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags); |
| 537 | xfs_rui_release(ruip); |
Darrick J. Wong | 895e196 | 2019-11-06 09:17:43 -0800 | [diff] [blame] | 538 | return -EFSCORRUPTED; |
Darrick J. Wong | 9e88b5d | 2016-08-03 12:09:48 +1000 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Darrick J. Wong | b31c2bd | 2018-02-22 14:41:25 -0800 | [diff] [blame] | 542 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, |
| 543 | mp->m_rmap_maxlevels, 0, XFS_TRANS_RESERVE, &tp); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 544 | if (error) |
| 545 | return error; |
Darrick J. Wong | 722e251 | 2016-08-03 12:28:43 +1000 | [diff] [blame] | 546 | rudp = xfs_trans_get_rud(tp, ruip); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 547 | |
| 548 | for (i = 0; i < ruip->rui_format.rui_nextents; i++) { |
Darrick J. Wong | e127faf | 2016-08-03 12:29:32 +1000 | [diff] [blame] | 549 | rmap = &ruip->rui_format.rui_extents[i]; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 550 | state = (rmap->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ? |
| 551 | XFS_EXT_UNWRITTEN : XFS_EXT_NORM; |
| 552 | whichfork = (rmap->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ? |
| 553 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 554 | switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) { |
| 555 | case XFS_RMAP_EXTENT_MAP: |
| 556 | type = XFS_RMAP_MAP; |
| 557 | break; |
Darrick J. Wong | ceeb9c8 | 2016-10-03 09:11:48 -0700 | [diff] [blame] | 558 | case XFS_RMAP_EXTENT_MAP_SHARED: |
| 559 | type = XFS_RMAP_MAP_SHARED; |
| 560 | break; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 561 | case XFS_RMAP_EXTENT_UNMAP: |
| 562 | type = XFS_RMAP_UNMAP; |
| 563 | break; |
Darrick J. Wong | ceeb9c8 | 2016-10-03 09:11:48 -0700 | [diff] [blame] | 564 | case XFS_RMAP_EXTENT_UNMAP_SHARED: |
| 565 | type = XFS_RMAP_UNMAP_SHARED; |
| 566 | break; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 567 | case XFS_RMAP_EXTENT_CONVERT: |
| 568 | type = XFS_RMAP_CONVERT; |
| 569 | break; |
Darrick J. Wong | 3f165b3 | 2016-10-03 09:11:48 -0700 | [diff] [blame] | 570 | case XFS_RMAP_EXTENT_CONVERT_SHARED: |
| 571 | type = XFS_RMAP_CONVERT_SHARED; |
| 572 | break; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 573 | case XFS_RMAP_EXTENT_ALLOC: |
| 574 | type = XFS_RMAP_ALLOC; |
| 575 | break; |
| 576 | case XFS_RMAP_EXTENT_FREE: |
| 577 | type = XFS_RMAP_FREE; |
| 578 | break; |
| 579 | default: |
Darrick J. Wong | a5155b8 | 2019-11-02 09:40:53 -0700 | [diff] [blame] | 580 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 581 | error = -EFSCORRUPTED; |
| 582 | goto abort_error; |
| 583 | } |
| 584 | error = xfs_trans_log_finish_rmap_update(tp, rudp, type, |
| 585 | rmap->me_owner, whichfork, |
| 586 | rmap->me_startoff, rmap->me_startblock, |
| 587 | rmap->me_len, state, &rcur); |
| 588 | if (error) |
| 589 | goto abort_error; |
| 590 | |
| 591 | } |
| 592 | |
| 593 | xfs_rmap_finish_one_cleanup(tp, rcur, error); |
Darrick J. Wong | 9e88b5d | 2016-08-03 12:09:48 +1000 | [diff] [blame] | 594 | set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 595 | error = xfs_trans_commit(tp); |
| 596 | return error; |
| 597 | |
| 598 | abort_error: |
| 599 | xfs_rmap_finish_one_cleanup(tp, rcur, error); |
| 600 | xfs_trans_cancel(tp); |
Darrick J. Wong | 9e88b5d | 2016-08-03 12:09:48 +1000 | [diff] [blame] | 601 | return error; |
| 602 | } |