Darrick J. Wong | 673930c | 2016-08-03 11:33:43 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Red Hat, Inc. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | #include "xfs.h" |
| 19 | #include "xfs_fs.h" |
| 20 | #include "xfs_shared.h" |
| 21 | #include "xfs_format.h" |
| 22 | #include "xfs_log_format.h" |
| 23 | #include "xfs_trans_resv.h" |
| 24 | #include "xfs_bit.h" |
| 25 | #include "xfs_sb.h" |
| 26 | #include "xfs_mount.h" |
| 27 | #include "xfs_defer.h" |
| 28 | #include "xfs_da_format.h" |
| 29 | #include "xfs_da_btree.h" |
| 30 | #include "xfs_btree.h" |
| 31 | #include "xfs_trans.h" |
| 32 | #include "xfs_alloc.h" |
| 33 | #include "xfs_rmap.h" |
Darrick J. Wong | 0a1b0b3 | 2016-08-03 11:44:21 +1000 | [diff] [blame] | 34 | #include "xfs_rmap_btree.h" |
Darrick J. Wong | 673930c | 2016-08-03 11:33:43 +1000 | [diff] [blame] | 35 | #include "xfs_trans_space.h" |
| 36 | #include "xfs_trace.h" |
| 37 | #include "xfs_error.h" |
| 38 | #include "xfs_extent_busy.h" |
| 39 | |
Darrick J. Wong | 4b8ed67 | 2016-08-03 11:39:05 +1000 | [diff] [blame] | 40 | /* |
| 41 | * Lookup the first record less than or equal to [bno, len, owner, offset] |
| 42 | * in the btree given by cur. |
| 43 | */ |
| 44 | int |
| 45 | xfs_rmap_lookup_le( |
| 46 | struct xfs_btree_cur *cur, |
| 47 | xfs_agblock_t bno, |
| 48 | xfs_extlen_t len, |
| 49 | uint64_t owner, |
| 50 | uint64_t offset, |
| 51 | unsigned int flags, |
| 52 | int *stat) |
| 53 | { |
| 54 | cur->bc_rec.r.rm_startblock = bno; |
| 55 | cur->bc_rec.r.rm_blockcount = len; |
| 56 | cur->bc_rec.r.rm_owner = owner; |
| 57 | cur->bc_rec.r.rm_offset = offset; |
| 58 | cur->bc_rec.r.rm_flags = flags; |
| 59 | return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat); |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * Lookup the record exactly matching [bno, len, owner, offset] |
| 64 | * in the btree given by cur. |
| 65 | */ |
| 66 | int |
| 67 | xfs_rmap_lookup_eq( |
| 68 | struct xfs_btree_cur *cur, |
| 69 | xfs_agblock_t bno, |
| 70 | xfs_extlen_t len, |
| 71 | uint64_t owner, |
| 72 | uint64_t offset, |
| 73 | unsigned int flags, |
| 74 | int *stat) |
| 75 | { |
| 76 | cur->bc_rec.r.rm_startblock = bno; |
| 77 | cur->bc_rec.r.rm_blockcount = len; |
| 78 | cur->bc_rec.r.rm_owner = owner; |
| 79 | cur->bc_rec.r.rm_offset = offset; |
| 80 | cur->bc_rec.r.rm_flags = flags; |
| 81 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Update the record referred to by cur to the value given |
| 86 | * by [bno, len, owner, offset]. |
| 87 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 88 | */ |
| 89 | STATIC int |
| 90 | xfs_rmap_update( |
| 91 | struct xfs_btree_cur *cur, |
| 92 | struct xfs_rmap_irec *irec) |
| 93 | { |
| 94 | union xfs_btree_rec rec; |
| 95 | |
| 96 | rec.rmap.rm_startblock = cpu_to_be32(irec->rm_startblock); |
| 97 | rec.rmap.rm_blockcount = cpu_to_be32(irec->rm_blockcount); |
| 98 | rec.rmap.rm_owner = cpu_to_be64(irec->rm_owner); |
| 99 | rec.rmap.rm_offset = cpu_to_be64( |
| 100 | xfs_rmap_irec_offset_pack(irec)); |
| 101 | return xfs_btree_update(cur, &rec); |
| 102 | } |
| 103 | |
| 104 | static int |
| 105 | xfs_rmap_btrec_to_irec( |
| 106 | union xfs_btree_rec *rec, |
| 107 | struct xfs_rmap_irec *irec) |
| 108 | { |
| 109 | irec->rm_flags = 0; |
| 110 | irec->rm_startblock = be32_to_cpu(rec->rmap.rm_startblock); |
| 111 | irec->rm_blockcount = be32_to_cpu(rec->rmap.rm_blockcount); |
| 112 | irec->rm_owner = be64_to_cpu(rec->rmap.rm_owner); |
| 113 | return xfs_rmap_irec_offset_unpack(be64_to_cpu(rec->rmap.rm_offset), |
| 114 | irec); |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Get the data from the pointed-to record. |
| 119 | */ |
| 120 | int |
| 121 | xfs_rmap_get_rec( |
| 122 | struct xfs_btree_cur *cur, |
| 123 | struct xfs_rmap_irec *irec, |
| 124 | int *stat) |
| 125 | { |
| 126 | union xfs_btree_rec *rec; |
| 127 | int error; |
| 128 | |
| 129 | error = xfs_btree_get_rec(cur, &rec, stat); |
| 130 | if (error || !*stat) |
| 131 | return error; |
| 132 | |
| 133 | return xfs_rmap_btrec_to_irec(rec, irec); |
| 134 | } |
| 135 | |
Darrick J. Wong | f922cd9 | 2016-08-03 11:45:12 +1000 | [diff] [blame] | 136 | /* |
| 137 | * Find the extent in the rmap btree and remove it. |
| 138 | * |
| 139 | * The record we find should always be an exact match for the extent that we're |
| 140 | * looking for, since we insert them into the btree without modification. |
| 141 | * |
| 142 | * Special Case #1: when growing the filesystem, we "free" an extent when |
| 143 | * growing the last AG. This extent is new space and so it is not tracked as |
| 144 | * used space in the btree. The growfs code will pass in an owner of |
| 145 | * XFS_RMAP_OWN_NULL to indicate that it expected that there is no owner of this |
| 146 | * extent. We verify that - the extent lookup result in a record that does not |
| 147 | * overlap. |
| 148 | * |
| 149 | * Special Case #2: EFIs do not record the owner of the extent, so when |
| 150 | * recovering EFIs from the log we pass in XFS_RMAP_OWN_UNKNOWN to tell the rmap |
| 151 | * btree to ignore the owner (i.e. wildcard match) so we don't trigger |
| 152 | * corruption checks during log recovery. |
| 153 | */ |
| 154 | STATIC int |
| 155 | xfs_rmap_unmap( |
| 156 | struct xfs_btree_cur *cur, |
| 157 | xfs_agblock_t bno, |
| 158 | xfs_extlen_t len, |
| 159 | bool unwritten, |
| 160 | struct xfs_owner_info *oinfo) |
| 161 | { |
| 162 | struct xfs_mount *mp = cur->bc_mp; |
| 163 | struct xfs_rmap_irec ltrec; |
| 164 | uint64_t ltoff; |
| 165 | int error = 0; |
| 166 | int i; |
| 167 | uint64_t owner; |
| 168 | uint64_t offset; |
| 169 | unsigned int flags; |
| 170 | bool ignore_off; |
| 171 | |
| 172 | xfs_owner_info_unpack(oinfo, &owner, &offset, &flags); |
| 173 | ignore_off = XFS_RMAP_NON_INODE_OWNER(owner) || |
| 174 | (flags & XFS_RMAP_BMBT_BLOCK); |
| 175 | if (unwritten) |
| 176 | flags |= XFS_RMAP_UNWRITTEN; |
| 177 | trace_xfs_rmap_unmap(mp, cur->bc_private.a.agno, bno, len, |
| 178 | unwritten, oinfo); |
| 179 | |
| 180 | /* |
| 181 | * We should always have a left record because there's a static record |
| 182 | * for the AG headers at rm_startblock == 0 created by mkfs/growfs that |
| 183 | * will not ever be removed from the tree. |
| 184 | */ |
| 185 | error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, flags, &i); |
| 186 | if (error) |
| 187 | goto out_error; |
| 188 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error); |
| 189 | |
| 190 | error = xfs_rmap_get_rec(cur, <rec, &i); |
| 191 | if (error) |
| 192 | goto out_error; |
| 193 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error); |
| 194 | trace_xfs_rmap_lookup_le_range_result(cur->bc_mp, |
| 195 | cur->bc_private.a.agno, ltrec.rm_startblock, |
| 196 | ltrec.rm_blockcount, ltrec.rm_owner, |
| 197 | ltrec.rm_offset, ltrec.rm_flags); |
| 198 | ltoff = ltrec.rm_offset; |
| 199 | |
| 200 | /* |
| 201 | * For growfs, the incoming extent must be beyond the left record we |
| 202 | * just found as it is new space and won't be used by anyone. This is |
| 203 | * just a corruption check as we don't actually do anything with this |
| 204 | * extent. Note that we need to use >= instead of > because it might |
| 205 | * be the case that the "left" extent goes all the way to EOFS. |
| 206 | */ |
| 207 | if (owner == XFS_RMAP_OWN_NULL) { |
| 208 | XFS_WANT_CORRUPTED_GOTO(mp, bno >= ltrec.rm_startblock + |
| 209 | ltrec.rm_blockcount, out_error); |
| 210 | goto out_done; |
| 211 | } |
| 212 | |
| 213 | /* Make sure the unwritten flag matches. */ |
| 214 | XFS_WANT_CORRUPTED_GOTO(mp, (flags & XFS_RMAP_UNWRITTEN) == |
| 215 | (ltrec.rm_flags & XFS_RMAP_UNWRITTEN), out_error); |
| 216 | |
| 217 | /* Make sure the extent we found covers the entire freeing range. */ |
| 218 | XFS_WANT_CORRUPTED_GOTO(mp, ltrec.rm_startblock <= bno && |
| 219 | ltrec.rm_startblock + ltrec.rm_blockcount >= |
| 220 | bno + len, out_error); |
| 221 | |
| 222 | /* Make sure the owner matches what we expect to find in the tree. */ |
| 223 | XFS_WANT_CORRUPTED_GOTO(mp, owner == ltrec.rm_owner || |
| 224 | XFS_RMAP_NON_INODE_OWNER(owner), out_error); |
| 225 | |
| 226 | /* Check the offset, if necessary. */ |
| 227 | if (!XFS_RMAP_NON_INODE_OWNER(owner)) { |
| 228 | if (flags & XFS_RMAP_BMBT_BLOCK) { |
| 229 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 230 | ltrec.rm_flags & XFS_RMAP_BMBT_BLOCK, |
| 231 | out_error); |
| 232 | } else { |
| 233 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 234 | ltrec.rm_offset <= offset, out_error); |
| 235 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 236 | ltoff + ltrec.rm_blockcount >= offset + len, |
| 237 | out_error); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | if (ltrec.rm_startblock == bno && ltrec.rm_blockcount == len) { |
| 242 | /* exact match, simply remove the record from rmap tree */ |
| 243 | trace_xfs_rmap_delete(mp, cur->bc_private.a.agno, |
| 244 | ltrec.rm_startblock, ltrec.rm_blockcount, |
| 245 | ltrec.rm_owner, ltrec.rm_offset, |
| 246 | ltrec.rm_flags); |
| 247 | error = xfs_btree_delete(cur, &i); |
| 248 | if (error) |
| 249 | goto out_error; |
| 250 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error); |
| 251 | } else if (ltrec.rm_startblock == bno) { |
| 252 | /* |
| 253 | * overlap left hand side of extent: move the start, trim the |
| 254 | * length and update the current record. |
| 255 | * |
| 256 | * ltbno ltlen |
| 257 | * Orig: |oooooooooooooooooooo| |
| 258 | * Freeing: |fffffffff| |
| 259 | * Result: |rrrrrrrrrr| |
| 260 | * bno len |
| 261 | */ |
| 262 | ltrec.rm_startblock += len; |
| 263 | ltrec.rm_blockcount -= len; |
| 264 | if (!ignore_off) |
| 265 | ltrec.rm_offset += len; |
| 266 | error = xfs_rmap_update(cur, <rec); |
| 267 | if (error) |
| 268 | goto out_error; |
| 269 | } else if (ltrec.rm_startblock + ltrec.rm_blockcount == bno + len) { |
| 270 | /* |
| 271 | * overlap right hand side of extent: trim the length and update |
| 272 | * the current record. |
| 273 | * |
| 274 | * ltbno ltlen |
| 275 | * Orig: |oooooooooooooooooooo| |
| 276 | * Freeing: |fffffffff| |
| 277 | * Result: |rrrrrrrrrr| |
| 278 | * bno len |
| 279 | */ |
| 280 | ltrec.rm_blockcount -= len; |
| 281 | error = xfs_rmap_update(cur, <rec); |
| 282 | if (error) |
| 283 | goto out_error; |
| 284 | } else { |
| 285 | |
| 286 | /* |
| 287 | * overlap middle of extent: trim the length of the existing |
| 288 | * record to the length of the new left-extent size, increment |
| 289 | * the insertion position so we can insert a new record |
| 290 | * containing the remaining right-extent space. |
| 291 | * |
| 292 | * ltbno ltlen |
| 293 | * Orig: |oooooooooooooooooooo| |
| 294 | * Freeing: |fffffffff| |
| 295 | * Result: |rrrrr| |rrrr| |
| 296 | * bno len |
| 297 | */ |
| 298 | xfs_extlen_t orig_len = ltrec.rm_blockcount; |
| 299 | |
| 300 | ltrec.rm_blockcount = bno - ltrec.rm_startblock; |
| 301 | error = xfs_rmap_update(cur, <rec); |
| 302 | if (error) |
| 303 | goto out_error; |
| 304 | |
| 305 | error = xfs_btree_increment(cur, 0, &i); |
| 306 | if (error) |
| 307 | goto out_error; |
| 308 | |
| 309 | cur->bc_rec.r.rm_startblock = bno + len; |
| 310 | cur->bc_rec.r.rm_blockcount = orig_len - len - |
| 311 | ltrec.rm_blockcount; |
| 312 | cur->bc_rec.r.rm_owner = ltrec.rm_owner; |
| 313 | if (ignore_off) |
| 314 | cur->bc_rec.r.rm_offset = 0; |
| 315 | else |
| 316 | cur->bc_rec.r.rm_offset = offset + len; |
| 317 | cur->bc_rec.r.rm_flags = flags; |
| 318 | trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, |
| 319 | cur->bc_rec.r.rm_startblock, |
| 320 | cur->bc_rec.r.rm_blockcount, |
| 321 | cur->bc_rec.r.rm_owner, |
| 322 | cur->bc_rec.r.rm_offset, |
| 323 | cur->bc_rec.r.rm_flags); |
| 324 | error = xfs_btree_insert(cur, &i); |
| 325 | if (error) |
| 326 | goto out_error; |
| 327 | } |
| 328 | |
| 329 | out_done: |
| 330 | trace_xfs_rmap_unmap_done(mp, cur->bc_private.a.agno, bno, len, |
| 331 | unwritten, oinfo); |
| 332 | out_error: |
| 333 | if (error) |
| 334 | trace_xfs_rmap_unmap_error(mp, cur->bc_private.a.agno, |
| 335 | error, _RET_IP_); |
| 336 | return error; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Remove a reference to an extent in the rmap btree. |
| 341 | */ |
Darrick J. Wong | 673930c | 2016-08-03 11:33:43 +1000 | [diff] [blame] | 342 | int |
| 343 | xfs_rmap_free( |
| 344 | struct xfs_trans *tp, |
| 345 | struct xfs_buf *agbp, |
| 346 | xfs_agnumber_t agno, |
| 347 | xfs_agblock_t bno, |
| 348 | xfs_extlen_t len, |
| 349 | struct xfs_owner_info *oinfo) |
| 350 | { |
| 351 | struct xfs_mount *mp = tp->t_mountp; |
Darrick J. Wong | f922cd9 | 2016-08-03 11:45:12 +1000 | [diff] [blame] | 352 | struct xfs_btree_cur *cur; |
| 353 | int error; |
Darrick J. Wong | 673930c | 2016-08-03 11:33:43 +1000 | [diff] [blame] | 354 | |
| 355 | if (!xfs_sb_version_hasrmapbt(&mp->m_sb)) |
| 356 | return 0; |
| 357 | |
Darrick J. Wong | f922cd9 | 2016-08-03 11:45:12 +1000 | [diff] [blame] | 358 | cur = xfs_rmapbt_init_cursor(mp, tp, agbp, agno); |
| 359 | |
| 360 | error = xfs_rmap_unmap(cur, bno, len, false, oinfo); |
| 361 | if (error) |
Darrick J. Wong | 673930c | 2016-08-03 11:33:43 +1000 | [diff] [blame] | 362 | goto out_error; |
Darrick J. Wong | f922cd9 | 2016-08-03 11:45:12 +1000 | [diff] [blame] | 363 | |
| 364 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Darrick J. Wong | 673930c | 2016-08-03 11:33:43 +1000 | [diff] [blame] | 365 | return 0; |
| 366 | |
| 367 | out_error: |
Darrick J. Wong | f922cd9 | 2016-08-03 11:45:12 +1000 | [diff] [blame] | 368 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
Darrick J. Wong | 673930c | 2016-08-03 11:33:43 +1000 | [diff] [blame] | 369 | return error; |
| 370 | } |
| 371 | |
Darrick J. Wong | 0a1b0b3 | 2016-08-03 11:44:21 +1000 | [diff] [blame] | 372 | /* |
| 373 | * A mergeable rmap must have the same owner and the same values for |
| 374 | * the unwritten, attr_fork, and bmbt flags. The startblock and |
| 375 | * offset are checked separately. |
| 376 | */ |
| 377 | static bool |
| 378 | xfs_rmap_is_mergeable( |
| 379 | struct xfs_rmap_irec *irec, |
| 380 | uint64_t owner, |
| 381 | unsigned int flags) |
| 382 | { |
| 383 | if (irec->rm_owner == XFS_RMAP_OWN_NULL) |
| 384 | return false; |
| 385 | if (irec->rm_owner != owner) |
| 386 | return false; |
| 387 | if ((flags & XFS_RMAP_UNWRITTEN) ^ |
| 388 | (irec->rm_flags & XFS_RMAP_UNWRITTEN)) |
| 389 | return false; |
| 390 | if ((flags & XFS_RMAP_ATTR_FORK) ^ |
| 391 | (irec->rm_flags & XFS_RMAP_ATTR_FORK)) |
| 392 | return false; |
| 393 | if ((flags & XFS_RMAP_BMBT_BLOCK) ^ |
| 394 | (irec->rm_flags & XFS_RMAP_BMBT_BLOCK)) |
| 395 | return false; |
| 396 | return true; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * When we allocate a new block, the first thing we do is add a reference to |
| 401 | * the extent in the rmap btree. This takes the form of a [agbno, length, |
| 402 | * owner, offset] record. Flags are encoded in the high bits of the offset |
| 403 | * field. |
| 404 | */ |
| 405 | STATIC int |
| 406 | xfs_rmap_map( |
| 407 | struct xfs_btree_cur *cur, |
| 408 | xfs_agblock_t bno, |
| 409 | xfs_extlen_t len, |
| 410 | bool unwritten, |
| 411 | struct xfs_owner_info *oinfo) |
| 412 | { |
| 413 | struct xfs_mount *mp = cur->bc_mp; |
| 414 | struct xfs_rmap_irec ltrec; |
| 415 | struct xfs_rmap_irec gtrec; |
| 416 | int have_gt; |
| 417 | int have_lt; |
| 418 | int error = 0; |
| 419 | int i; |
| 420 | uint64_t owner; |
| 421 | uint64_t offset; |
| 422 | unsigned int flags = 0; |
| 423 | bool ignore_off; |
| 424 | |
| 425 | xfs_owner_info_unpack(oinfo, &owner, &offset, &flags); |
| 426 | ASSERT(owner != 0); |
| 427 | ignore_off = XFS_RMAP_NON_INODE_OWNER(owner) || |
| 428 | (flags & XFS_RMAP_BMBT_BLOCK); |
| 429 | if (unwritten) |
| 430 | flags |= XFS_RMAP_UNWRITTEN; |
| 431 | trace_xfs_rmap_map(mp, cur->bc_private.a.agno, bno, len, |
| 432 | unwritten, oinfo); |
| 433 | |
| 434 | /* |
| 435 | * For the initial lookup, look for an exact match or the left-adjacent |
| 436 | * record for our insertion point. This will also give us the record for |
| 437 | * start block contiguity tests. |
| 438 | */ |
| 439 | error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, flags, |
| 440 | &have_lt); |
| 441 | if (error) |
| 442 | goto out_error; |
| 443 | XFS_WANT_CORRUPTED_GOTO(mp, have_lt == 1, out_error); |
| 444 | |
| 445 | error = xfs_rmap_get_rec(cur, <rec, &have_lt); |
| 446 | if (error) |
| 447 | goto out_error; |
| 448 | XFS_WANT_CORRUPTED_GOTO(mp, have_lt == 1, out_error); |
| 449 | trace_xfs_rmap_lookup_le_range_result(cur->bc_mp, |
| 450 | cur->bc_private.a.agno, ltrec.rm_startblock, |
| 451 | ltrec.rm_blockcount, ltrec.rm_owner, |
| 452 | ltrec.rm_offset, ltrec.rm_flags); |
| 453 | |
| 454 | if (!xfs_rmap_is_mergeable(<rec, owner, flags)) |
| 455 | have_lt = 0; |
| 456 | |
| 457 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 458 | have_lt == 0 || |
| 459 | ltrec.rm_startblock + ltrec.rm_blockcount <= bno, out_error); |
| 460 | |
| 461 | /* |
| 462 | * Increment the cursor to see if we have a right-adjacent record to our |
| 463 | * insertion point. This will give us the record for end block |
| 464 | * contiguity tests. |
| 465 | */ |
| 466 | error = xfs_btree_increment(cur, 0, &have_gt); |
| 467 | if (error) |
| 468 | goto out_error; |
| 469 | if (have_gt) { |
| 470 | error = xfs_rmap_get_rec(cur, >rec, &have_gt); |
| 471 | if (error) |
| 472 | goto out_error; |
| 473 | XFS_WANT_CORRUPTED_GOTO(mp, have_gt == 1, out_error); |
| 474 | XFS_WANT_CORRUPTED_GOTO(mp, bno + len <= gtrec.rm_startblock, |
| 475 | out_error); |
| 476 | trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp, |
| 477 | cur->bc_private.a.agno, gtrec.rm_startblock, |
| 478 | gtrec.rm_blockcount, gtrec.rm_owner, |
| 479 | gtrec.rm_offset, gtrec.rm_flags); |
| 480 | if (!xfs_rmap_is_mergeable(>rec, owner, flags)) |
| 481 | have_gt = 0; |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * Note: cursor currently points one record to the right of ltrec, even |
| 486 | * if there is no record in the tree to the right. |
| 487 | */ |
| 488 | if (have_lt && |
| 489 | ltrec.rm_startblock + ltrec.rm_blockcount == bno && |
| 490 | (ignore_off || ltrec.rm_offset + ltrec.rm_blockcount == offset)) { |
| 491 | /* |
| 492 | * left edge contiguous, merge into left record. |
| 493 | * |
| 494 | * ltbno ltlen |
| 495 | * orig: |ooooooooo| |
| 496 | * adding: |aaaaaaaaa| |
| 497 | * result: |rrrrrrrrrrrrrrrrrrr| |
| 498 | * bno len |
| 499 | */ |
| 500 | ltrec.rm_blockcount += len; |
| 501 | if (have_gt && |
| 502 | bno + len == gtrec.rm_startblock && |
| 503 | (ignore_off || offset + len == gtrec.rm_offset) && |
| 504 | (unsigned long)ltrec.rm_blockcount + len + |
| 505 | gtrec.rm_blockcount <= XFS_RMAP_LEN_MAX) { |
| 506 | /* |
| 507 | * right edge also contiguous, delete right record |
| 508 | * and merge into left record. |
| 509 | * |
| 510 | * ltbno ltlen gtbno gtlen |
| 511 | * orig: |ooooooooo| |ooooooooo| |
| 512 | * adding: |aaaaaaaaa| |
| 513 | * result: |rrrrrrrrrrrrrrrrrrrrrrrrrrrrr| |
| 514 | */ |
| 515 | ltrec.rm_blockcount += gtrec.rm_blockcount; |
| 516 | trace_xfs_rmap_delete(mp, cur->bc_private.a.agno, |
| 517 | gtrec.rm_startblock, |
| 518 | gtrec.rm_blockcount, |
| 519 | gtrec.rm_owner, |
| 520 | gtrec.rm_offset, |
| 521 | gtrec.rm_flags); |
| 522 | error = xfs_btree_delete(cur, &i); |
| 523 | if (error) |
| 524 | goto out_error; |
| 525 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error); |
| 526 | } |
| 527 | |
| 528 | /* point the cursor back to the left record and update */ |
| 529 | error = xfs_btree_decrement(cur, 0, &have_gt); |
| 530 | if (error) |
| 531 | goto out_error; |
| 532 | error = xfs_rmap_update(cur, <rec); |
| 533 | if (error) |
| 534 | goto out_error; |
| 535 | } else if (have_gt && |
| 536 | bno + len == gtrec.rm_startblock && |
| 537 | (ignore_off || offset + len == gtrec.rm_offset)) { |
| 538 | /* |
| 539 | * right edge contiguous, merge into right record. |
| 540 | * |
| 541 | * gtbno gtlen |
| 542 | * Orig: |ooooooooo| |
| 543 | * adding: |aaaaaaaaa| |
| 544 | * Result: |rrrrrrrrrrrrrrrrrrr| |
| 545 | * bno len |
| 546 | */ |
| 547 | gtrec.rm_startblock = bno; |
| 548 | gtrec.rm_blockcount += len; |
| 549 | if (!ignore_off) |
| 550 | gtrec.rm_offset = offset; |
| 551 | error = xfs_rmap_update(cur, >rec); |
| 552 | if (error) |
| 553 | goto out_error; |
| 554 | } else { |
| 555 | /* |
| 556 | * no contiguous edge with identical owner, insert |
| 557 | * new record at current cursor position. |
| 558 | */ |
| 559 | cur->bc_rec.r.rm_startblock = bno; |
| 560 | cur->bc_rec.r.rm_blockcount = len; |
| 561 | cur->bc_rec.r.rm_owner = owner; |
| 562 | cur->bc_rec.r.rm_offset = offset; |
| 563 | cur->bc_rec.r.rm_flags = flags; |
| 564 | trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno, len, |
| 565 | owner, offset, flags); |
| 566 | error = xfs_btree_insert(cur, &i); |
| 567 | if (error) |
| 568 | goto out_error; |
| 569 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error); |
| 570 | } |
| 571 | |
| 572 | trace_xfs_rmap_map_done(mp, cur->bc_private.a.agno, bno, len, |
| 573 | unwritten, oinfo); |
| 574 | out_error: |
| 575 | if (error) |
| 576 | trace_xfs_rmap_map_error(mp, cur->bc_private.a.agno, |
| 577 | error, _RET_IP_); |
| 578 | return error; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * Add a reference to an extent in the rmap btree. |
| 583 | */ |
Darrick J. Wong | 673930c | 2016-08-03 11:33:43 +1000 | [diff] [blame] | 584 | int |
| 585 | xfs_rmap_alloc( |
| 586 | struct xfs_trans *tp, |
| 587 | struct xfs_buf *agbp, |
| 588 | xfs_agnumber_t agno, |
| 589 | xfs_agblock_t bno, |
| 590 | xfs_extlen_t len, |
| 591 | struct xfs_owner_info *oinfo) |
| 592 | { |
| 593 | struct xfs_mount *mp = tp->t_mountp; |
Darrick J. Wong | 0a1b0b3 | 2016-08-03 11:44:21 +1000 | [diff] [blame] | 594 | struct xfs_btree_cur *cur; |
| 595 | int error; |
Darrick J. Wong | 673930c | 2016-08-03 11:33:43 +1000 | [diff] [blame] | 596 | |
| 597 | if (!xfs_sb_version_hasrmapbt(&mp->m_sb)) |
| 598 | return 0; |
| 599 | |
Darrick J. Wong | 0a1b0b3 | 2016-08-03 11:44:21 +1000 | [diff] [blame] | 600 | cur = xfs_rmapbt_init_cursor(mp, tp, agbp, agno); |
| 601 | error = xfs_rmap_map(cur, bno, len, false, oinfo); |
| 602 | if (error) |
Darrick J. Wong | 673930c | 2016-08-03 11:33:43 +1000 | [diff] [blame] | 603 | goto out_error; |
Darrick J. Wong | 0a1b0b3 | 2016-08-03 11:44:21 +1000 | [diff] [blame] | 604 | |
| 605 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Darrick J. Wong | 673930c | 2016-08-03 11:33:43 +1000 | [diff] [blame] | 606 | return 0; |
| 607 | |
| 608 | out_error: |
Darrick J. Wong | 0a1b0b3 | 2016-08-03 11:44:21 +1000 | [diff] [blame] | 609 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
Darrick J. Wong | 673930c | 2016-08-03 11:33:43 +1000 | [diff] [blame] | 610 | return error; |
| 611 | } |
Darrick J. Wong | c543838 | 2016-08-03 11:42:39 +1000 | [diff] [blame] | 612 | |
Darrick J. Wong | fb7d926 | 2016-08-03 12:03:19 +1000 | [diff] [blame^] | 613 | #define RMAP_LEFT_CONTIG (1 << 0) |
| 614 | #define RMAP_RIGHT_CONTIG (1 << 1) |
| 615 | #define RMAP_LEFT_FILLING (1 << 2) |
| 616 | #define RMAP_RIGHT_FILLING (1 << 3) |
| 617 | #define RMAP_LEFT_VALID (1 << 6) |
| 618 | #define RMAP_RIGHT_VALID (1 << 7) |
| 619 | |
| 620 | #define LEFT r[0] |
| 621 | #define RIGHT r[1] |
| 622 | #define PREV r[2] |
| 623 | #define NEW r[3] |
| 624 | |
| 625 | /* |
| 626 | * Convert an unwritten extent to a real extent or vice versa. |
| 627 | * Does not handle overlapping extents. |
| 628 | */ |
| 629 | STATIC int |
| 630 | xfs_rmap_convert( |
| 631 | struct xfs_btree_cur *cur, |
| 632 | xfs_agblock_t bno, |
| 633 | xfs_extlen_t len, |
| 634 | bool unwritten, |
| 635 | struct xfs_owner_info *oinfo) |
| 636 | { |
| 637 | struct xfs_mount *mp = cur->bc_mp; |
| 638 | struct xfs_rmap_irec r[4]; /* neighbor extent entries */ |
| 639 | /* left is 0, right is 1, prev is 2 */ |
| 640 | /* new is 3 */ |
| 641 | uint64_t owner; |
| 642 | uint64_t offset; |
| 643 | uint64_t new_endoff; |
| 644 | unsigned int oldext; |
| 645 | unsigned int newext; |
| 646 | unsigned int flags = 0; |
| 647 | int i; |
| 648 | int state = 0; |
| 649 | int error; |
| 650 | |
| 651 | xfs_owner_info_unpack(oinfo, &owner, &offset, &flags); |
| 652 | ASSERT(!(XFS_RMAP_NON_INODE_OWNER(owner) || |
| 653 | (flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK)))); |
| 654 | oldext = unwritten ? XFS_RMAP_UNWRITTEN : 0; |
| 655 | new_endoff = offset + len; |
| 656 | trace_xfs_rmap_convert(mp, cur->bc_private.a.agno, bno, len, |
| 657 | unwritten, oinfo); |
| 658 | |
| 659 | /* |
| 660 | * For the initial lookup, look for an exact match or the left-adjacent |
| 661 | * record for our insertion point. This will also give us the record for |
| 662 | * start block contiguity tests. |
| 663 | */ |
| 664 | error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, oldext, &i); |
| 665 | if (error) |
| 666 | goto done; |
| 667 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 668 | |
| 669 | error = xfs_rmap_get_rec(cur, &PREV, &i); |
| 670 | if (error) |
| 671 | goto done; |
| 672 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 673 | trace_xfs_rmap_lookup_le_range_result(cur->bc_mp, |
| 674 | cur->bc_private.a.agno, PREV.rm_startblock, |
| 675 | PREV.rm_blockcount, PREV.rm_owner, |
| 676 | PREV.rm_offset, PREV.rm_flags); |
| 677 | |
| 678 | ASSERT(PREV.rm_offset <= offset); |
| 679 | ASSERT(PREV.rm_offset + PREV.rm_blockcount >= new_endoff); |
| 680 | ASSERT((PREV.rm_flags & XFS_RMAP_UNWRITTEN) == oldext); |
| 681 | newext = ~oldext & XFS_RMAP_UNWRITTEN; |
| 682 | |
| 683 | /* |
| 684 | * Set flags determining what part of the previous oldext allocation |
| 685 | * extent is being replaced by a newext allocation. |
| 686 | */ |
| 687 | if (PREV.rm_offset == offset) |
| 688 | state |= RMAP_LEFT_FILLING; |
| 689 | if (PREV.rm_offset + PREV.rm_blockcount == new_endoff) |
| 690 | state |= RMAP_RIGHT_FILLING; |
| 691 | |
| 692 | /* |
| 693 | * Decrement the cursor to see if we have a left-adjacent record to our |
| 694 | * insertion point. This will give us the record for end block |
| 695 | * contiguity tests. |
| 696 | */ |
| 697 | error = xfs_btree_decrement(cur, 0, &i); |
| 698 | if (error) |
| 699 | goto done; |
| 700 | if (i) { |
| 701 | state |= RMAP_LEFT_VALID; |
| 702 | error = xfs_rmap_get_rec(cur, &LEFT, &i); |
| 703 | if (error) |
| 704 | goto done; |
| 705 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 706 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 707 | LEFT.rm_startblock + LEFT.rm_blockcount <= bno, |
| 708 | done); |
| 709 | trace_xfs_rmap_find_left_neighbor_result(cur->bc_mp, |
| 710 | cur->bc_private.a.agno, LEFT.rm_startblock, |
| 711 | LEFT.rm_blockcount, LEFT.rm_owner, |
| 712 | LEFT.rm_offset, LEFT.rm_flags); |
| 713 | if (LEFT.rm_startblock + LEFT.rm_blockcount == bno && |
| 714 | LEFT.rm_offset + LEFT.rm_blockcount == offset && |
| 715 | xfs_rmap_is_mergeable(&LEFT, owner, newext)) |
| 716 | state |= RMAP_LEFT_CONTIG; |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * Increment the cursor to see if we have a right-adjacent record to our |
| 721 | * insertion point. This will give us the record for end block |
| 722 | * contiguity tests. |
| 723 | */ |
| 724 | error = xfs_btree_increment(cur, 0, &i); |
| 725 | if (error) |
| 726 | goto done; |
| 727 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 728 | error = xfs_btree_increment(cur, 0, &i); |
| 729 | if (error) |
| 730 | goto done; |
| 731 | if (i) { |
| 732 | state |= RMAP_RIGHT_VALID; |
| 733 | error = xfs_rmap_get_rec(cur, &RIGHT, &i); |
| 734 | if (error) |
| 735 | goto done; |
| 736 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 737 | XFS_WANT_CORRUPTED_GOTO(mp, bno + len <= RIGHT.rm_startblock, |
| 738 | done); |
| 739 | trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp, |
| 740 | cur->bc_private.a.agno, RIGHT.rm_startblock, |
| 741 | RIGHT.rm_blockcount, RIGHT.rm_owner, |
| 742 | RIGHT.rm_offset, RIGHT.rm_flags); |
| 743 | if (bno + len == RIGHT.rm_startblock && |
| 744 | offset + len == RIGHT.rm_offset && |
| 745 | xfs_rmap_is_mergeable(&RIGHT, owner, newext)) |
| 746 | state |= RMAP_RIGHT_CONTIG; |
| 747 | } |
| 748 | |
| 749 | /* check that left + prev + right is not too long */ |
| 750 | if ((state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG | |
| 751 | RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) == |
| 752 | (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG | |
| 753 | RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG) && |
| 754 | (unsigned long)LEFT.rm_blockcount + len + |
| 755 | RIGHT.rm_blockcount > XFS_RMAP_LEN_MAX) |
| 756 | state &= ~RMAP_RIGHT_CONTIG; |
| 757 | |
| 758 | trace_xfs_rmap_convert_state(mp, cur->bc_private.a.agno, state, |
| 759 | _RET_IP_); |
| 760 | |
| 761 | /* reset the cursor back to PREV */ |
| 762 | error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, oldext, &i); |
| 763 | if (error) |
| 764 | goto done; |
| 765 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 766 | |
| 767 | /* |
| 768 | * Switch out based on the FILLING and CONTIG state bits. |
| 769 | */ |
| 770 | switch (state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG | |
| 771 | RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) { |
| 772 | case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG | |
| 773 | RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG: |
| 774 | /* |
| 775 | * Setting all of a previous oldext extent to newext. |
| 776 | * The left and right neighbors are both contiguous with new. |
| 777 | */ |
| 778 | error = xfs_btree_increment(cur, 0, &i); |
| 779 | if (error) |
| 780 | goto done; |
| 781 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 782 | trace_xfs_rmap_delete(mp, cur->bc_private.a.agno, |
| 783 | RIGHT.rm_startblock, RIGHT.rm_blockcount, |
| 784 | RIGHT.rm_owner, RIGHT.rm_offset, |
| 785 | RIGHT.rm_flags); |
| 786 | error = xfs_btree_delete(cur, &i); |
| 787 | if (error) |
| 788 | goto done; |
| 789 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 790 | error = xfs_btree_decrement(cur, 0, &i); |
| 791 | if (error) |
| 792 | goto done; |
| 793 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 794 | trace_xfs_rmap_delete(mp, cur->bc_private.a.agno, |
| 795 | PREV.rm_startblock, PREV.rm_blockcount, |
| 796 | PREV.rm_owner, PREV.rm_offset, |
| 797 | PREV.rm_flags); |
| 798 | error = xfs_btree_delete(cur, &i); |
| 799 | if (error) |
| 800 | goto done; |
| 801 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 802 | error = xfs_btree_decrement(cur, 0, &i); |
| 803 | if (error) |
| 804 | goto done; |
| 805 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 806 | NEW = LEFT; |
| 807 | NEW.rm_blockcount += PREV.rm_blockcount + RIGHT.rm_blockcount; |
| 808 | error = xfs_rmap_update(cur, &NEW); |
| 809 | if (error) |
| 810 | goto done; |
| 811 | break; |
| 812 | |
| 813 | case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG: |
| 814 | /* |
| 815 | * Setting all of a previous oldext extent to newext. |
| 816 | * The left neighbor is contiguous, the right is not. |
| 817 | */ |
| 818 | trace_xfs_rmap_delete(mp, cur->bc_private.a.agno, |
| 819 | PREV.rm_startblock, PREV.rm_blockcount, |
| 820 | PREV.rm_owner, PREV.rm_offset, |
| 821 | PREV.rm_flags); |
| 822 | error = xfs_btree_delete(cur, &i); |
| 823 | if (error) |
| 824 | goto done; |
| 825 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 826 | error = xfs_btree_decrement(cur, 0, &i); |
| 827 | if (error) |
| 828 | goto done; |
| 829 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 830 | NEW = LEFT; |
| 831 | NEW.rm_blockcount += PREV.rm_blockcount; |
| 832 | error = xfs_rmap_update(cur, &NEW); |
| 833 | if (error) |
| 834 | goto done; |
| 835 | break; |
| 836 | |
| 837 | case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG: |
| 838 | /* |
| 839 | * Setting all of a previous oldext extent to newext. |
| 840 | * The right neighbor is contiguous, the left is not. |
| 841 | */ |
| 842 | error = xfs_btree_increment(cur, 0, &i); |
| 843 | if (error) |
| 844 | goto done; |
| 845 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 846 | trace_xfs_rmap_delete(mp, cur->bc_private.a.agno, |
| 847 | RIGHT.rm_startblock, RIGHT.rm_blockcount, |
| 848 | RIGHT.rm_owner, RIGHT.rm_offset, |
| 849 | RIGHT.rm_flags); |
| 850 | error = xfs_btree_delete(cur, &i); |
| 851 | if (error) |
| 852 | goto done; |
| 853 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 854 | error = xfs_btree_decrement(cur, 0, &i); |
| 855 | if (error) |
| 856 | goto done; |
| 857 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 858 | NEW = PREV; |
| 859 | NEW.rm_blockcount = len + RIGHT.rm_blockcount; |
| 860 | NEW.rm_flags = newext; |
| 861 | error = xfs_rmap_update(cur, &NEW); |
| 862 | if (error) |
| 863 | goto done; |
| 864 | break; |
| 865 | |
| 866 | case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING: |
| 867 | /* |
| 868 | * Setting all of a previous oldext extent to newext. |
| 869 | * Neither the left nor right neighbors are contiguous with |
| 870 | * the new one. |
| 871 | */ |
| 872 | NEW = PREV; |
| 873 | NEW.rm_flags = newext; |
| 874 | error = xfs_rmap_update(cur, &NEW); |
| 875 | if (error) |
| 876 | goto done; |
| 877 | break; |
| 878 | |
| 879 | case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG: |
| 880 | /* |
| 881 | * Setting the first part of a previous oldext extent to newext. |
| 882 | * The left neighbor is contiguous. |
| 883 | */ |
| 884 | NEW = PREV; |
| 885 | NEW.rm_offset += len; |
| 886 | NEW.rm_startblock += len; |
| 887 | NEW.rm_blockcount -= len; |
| 888 | error = xfs_rmap_update(cur, &NEW); |
| 889 | if (error) |
| 890 | goto done; |
| 891 | error = xfs_btree_decrement(cur, 0, &i); |
| 892 | if (error) |
| 893 | goto done; |
| 894 | NEW = LEFT; |
| 895 | NEW.rm_blockcount += len; |
| 896 | error = xfs_rmap_update(cur, &NEW); |
| 897 | if (error) |
| 898 | goto done; |
| 899 | break; |
| 900 | |
| 901 | case RMAP_LEFT_FILLING: |
| 902 | /* |
| 903 | * Setting the first part of a previous oldext extent to newext. |
| 904 | * The left neighbor is not contiguous. |
| 905 | */ |
| 906 | NEW = PREV; |
| 907 | NEW.rm_startblock += len; |
| 908 | NEW.rm_offset += len; |
| 909 | NEW.rm_blockcount -= len; |
| 910 | error = xfs_rmap_update(cur, &NEW); |
| 911 | if (error) |
| 912 | goto done; |
| 913 | NEW.rm_startblock = bno; |
| 914 | NEW.rm_owner = owner; |
| 915 | NEW.rm_offset = offset; |
| 916 | NEW.rm_blockcount = len; |
| 917 | NEW.rm_flags = newext; |
| 918 | cur->bc_rec.r = NEW; |
| 919 | trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno, |
| 920 | len, owner, offset, newext); |
| 921 | error = xfs_btree_insert(cur, &i); |
| 922 | if (error) |
| 923 | goto done; |
| 924 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 925 | break; |
| 926 | |
| 927 | case RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG: |
| 928 | /* |
| 929 | * Setting the last part of a previous oldext extent to newext. |
| 930 | * The right neighbor is contiguous with the new allocation. |
| 931 | */ |
| 932 | NEW = PREV; |
| 933 | NEW.rm_blockcount -= len; |
| 934 | error = xfs_rmap_update(cur, &NEW); |
| 935 | if (error) |
| 936 | goto done; |
| 937 | error = xfs_btree_increment(cur, 0, &i); |
| 938 | if (error) |
| 939 | goto done; |
| 940 | NEW = RIGHT; |
| 941 | NEW.rm_offset = offset; |
| 942 | NEW.rm_startblock = bno; |
| 943 | NEW.rm_blockcount += len; |
| 944 | error = xfs_rmap_update(cur, &NEW); |
| 945 | if (error) |
| 946 | goto done; |
| 947 | break; |
| 948 | |
| 949 | case RMAP_RIGHT_FILLING: |
| 950 | /* |
| 951 | * Setting the last part of a previous oldext extent to newext. |
| 952 | * The right neighbor is not contiguous. |
| 953 | */ |
| 954 | NEW = PREV; |
| 955 | NEW.rm_blockcount -= len; |
| 956 | error = xfs_rmap_update(cur, &NEW); |
| 957 | if (error) |
| 958 | goto done; |
| 959 | error = xfs_rmap_lookup_eq(cur, bno, len, owner, offset, |
| 960 | oldext, &i); |
| 961 | if (error) |
| 962 | goto done; |
| 963 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
| 964 | NEW.rm_startblock = bno; |
| 965 | NEW.rm_owner = owner; |
| 966 | NEW.rm_offset = offset; |
| 967 | NEW.rm_blockcount = len; |
| 968 | NEW.rm_flags = newext; |
| 969 | cur->bc_rec.r = NEW; |
| 970 | trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno, |
| 971 | len, owner, offset, newext); |
| 972 | error = xfs_btree_insert(cur, &i); |
| 973 | if (error) |
| 974 | goto done; |
| 975 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 976 | break; |
| 977 | |
| 978 | case 0: |
| 979 | /* |
| 980 | * Setting the middle part of a previous oldext extent to |
| 981 | * newext. Contiguity is impossible here. |
| 982 | * One extent becomes three extents. |
| 983 | */ |
| 984 | /* new right extent - oldext */ |
| 985 | NEW.rm_startblock = bno + len; |
| 986 | NEW.rm_owner = owner; |
| 987 | NEW.rm_offset = new_endoff; |
| 988 | NEW.rm_blockcount = PREV.rm_offset + PREV.rm_blockcount - |
| 989 | new_endoff; |
| 990 | NEW.rm_flags = PREV.rm_flags; |
| 991 | error = xfs_rmap_update(cur, &NEW); |
| 992 | if (error) |
| 993 | goto done; |
| 994 | /* new left extent - oldext */ |
| 995 | NEW = PREV; |
| 996 | NEW.rm_blockcount = offset - PREV.rm_offset; |
| 997 | cur->bc_rec.r = NEW; |
| 998 | trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, |
| 999 | NEW.rm_startblock, NEW.rm_blockcount, |
| 1000 | NEW.rm_owner, NEW.rm_offset, |
| 1001 | NEW.rm_flags); |
| 1002 | error = xfs_btree_insert(cur, &i); |
| 1003 | if (error) |
| 1004 | goto done; |
| 1005 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 1006 | /* |
| 1007 | * Reset the cursor to the position of the new extent |
| 1008 | * we are about to insert as we can't trust it after |
| 1009 | * the previous insert. |
| 1010 | */ |
| 1011 | error = xfs_rmap_lookup_eq(cur, bno, len, owner, offset, |
| 1012 | oldext, &i); |
| 1013 | if (error) |
| 1014 | goto done; |
| 1015 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
| 1016 | /* new middle extent - newext */ |
| 1017 | cur->bc_rec.r.rm_flags &= ~XFS_RMAP_UNWRITTEN; |
| 1018 | cur->bc_rec.r.rm_flags |= newext; |
| 1019 | trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno, len, |
| 1020 | owner, offset, newext); |
| 1021 | error = xfs_btree_insert(cur, &i); |
| 1022 | if (error) |
| 1023 | goto done; |
| 1024 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 1025 | break; |
| 1026 | |
| 1027 | case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG: |
| 1028 | case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG: |
| 1029 | case RMAP_LEFT_FILLING | RMAP_RIGHT_CONTIG: |
| 1030 | case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG: |
| 1031 | case RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG: |
| 1032 | case RMAP_LEFT_CONTIG: |
| 1033 | case RMAP_RIGHT_CONTIG: |
| 1034 | /* |
| 1035 | * These cases are all impossible. |
| 1036 | */ |
| 1037 | ASSERT(0); |
| 1038 | } |
| 1039 | |
| 1040 | trace_xfs_rmap_convert_done(mp, cur->bc_private.a.agno, bno, len, |
| 1041 | unwritten, oinfo); |
| 1042 | done: |
| 1043 | if (error) |
| 1044 | trace_xfs_rmap_convert_error(cur->bc_mp, |
| 1045 | cur->bc_private.a.agno, error, _RET_IP_); |
| 1046 | return error; |
| 1047 | } |
| 1048 | |
| 1049 | #undef NEW |
| 1050 | #undef LEFT |
| 1051 | #undef RIGHT |
| 1052 | #undef PREV |
| 1053 | |
Darrick J. Wong | c543838 | 2016-08-03 11:42:39 +1000 | [diff] [blame] | 1054 | struct xfs_rmap_query_range_info { |
| 1055 | xfs_rmap_query_range_fn fn; |
| 1056 | void *priv; |
| 1057 | }; |
| 1058 | |
| 1059 | /* Format btree record and pass to our callback. */ |
| 1060 | STATIC int |
| 1061 | xfs_rmap_query_range_helper( |
| 1062 | struct xfs_btree_cur *cur, |
| 1063 | union xfs_btree_rec *rec, |
| 1064 | void *priv) |
| 1065 | { |
| 1066 | struct xfs_rmap_query_range_info *query = priv; |
| 1067 | struct xfs_rmap_irec irec; |
| 1068 | int error; |
| 1069 | |
| 1070 | error = xfs_rmap_btrec_to_irec(rec, &irec); |
| 1071 | if (error) |
| 1072 | return error; |
| 1073 | return query->fn(cur, &irec, query->priv); |
| 1074 | } |
| 1075 | |
| 1076 | /* Find all rmaps between two keys. */ |
| 1077 | int |
| 1078 | xfs_rmap_query_range( |
| 1079 | struct xfs_btree_cur *cur, |
| 1080 | struct xfs_rmap_irec *low_rec, |
| 1081 | struct xfs_rmap_irec *high_rec, |
| 1082 | xfs_rmap_query_range_fn fn, |
| 1083 | void *priv) |
| 1084 | { |
| 1085 | union xfs_btree_irec low_brec; |
| 1086 | union xfs_btree_irec high_brec; |
| 1087 | struct xfs_rmap_query_range_info query; |
| 1088 | |
| 1089 | low_brec.r = *low_rec; |
| 1090 | high_brec.r = *high_rec; |
| 1091 | query.priv = priv; |
| 1092 | query.fn = fn; |
| 1093 | return xfs_btree_query_range(cur, &low_brec, &high_brec, |
| 1094 | xfs_rmap_query_range_helper, &query); |
| 1095 | } |