Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_trans.h" |
| 25 | #include "xfs_sb.h" |
| 26 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 29 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_dinode.h" |
| 32 | #include "xfs_inode.h" |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 33 | #include "xfs_inode_item.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 34 | #include "xfs_btree.h" |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 35 | #include "xfs_btree_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_error.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 37 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * Cursor allocation zone. |
| 41 | */ |
| 42 | kmem_zone_t *xfs_btree_cur_zone; |
| 43 | |
| 44 | /* |
| 45 | * Btree magic numbers. |
| 46 | */ |
Christoph Hellwig | cdcf4333 | 2008-08-13 16:23:50 +1000 | [diff] [blame] | 47 | const __uint32_t xfs_magics[XFS_BTNUM_MAX] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC |
| 49 | }; |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 52 | STATIC int /* error (0 or EFSCORRUPTED) */ |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 53 | xfs_btree_check_lblock( |
| 54 | struct xfs_btree_cur *cur, /* btree cursor */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 55 | struct xfs_btree_block *block, /* btree long form block pointer */ |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 56 | int level, /* level of the btree block */ |
| 57 | struct xfs_buf *bp) /* buffer for block, if any */ |
| 58 | { |
| 59 | int lblock_ok; /* block passes checks */ |
| 60 | struct xfs_mount *mp; /* file system mount point */ |
| 61 | |
| 62 | mp = cur->bc_mp; |
| 63 | lblock_ok = |
| 64 | be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] && |
| 65 | be16_to_cpu(block->bb_level) == level && |
| 66 | be16_to_cpu(block->bb_numrecs) <= |
Christoph Hellwig | ce5e42d | 2008-10-30 16:55:23 +1100 | [diff] [blame] | 67 | cur->bc_ops->get_maxrecs(cur, level) && |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 68 | block->bb_u.l.bb_leftsib && |
| 69 | (be64_to_cpu(block->bb_u.l.bb_leftsib) == NULLDFSBNO || |
| 70 | XFS_FSB_SANITY_CHECK(mp, |
| 71 | be64_to_cpu(block->bb_u.l.bb_leftsib))) && |
| 72 | block->bb_u.l.bb_rightsib && |
| 73 | (be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO || |
| 74 | XFS_FSB_SANITY_CHECK(mp, |
| 75 | be64_to_cpu(block->bb_u.l.bb_rightsib))); |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 76 | if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp, |
| 77 | XFS_ERRTAG_BTREE_CHECK_LBLOCK, |
| 78 | XFS_RANDOM_BTREE_CHECK_LBLOCK))) { |
| 79 | if (bp) |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 80 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 81 | XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW, |
| 82 | mp); |
| 83 | return XFS_ERROR(EFSCORRUPTED); |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 88 | STATIC int /* error (0 or EFSCORRUPTED) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | xfs_btree_check_sblock( |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 90 | struct xfs_btree_cur *cur, /* btree cursor */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 91 | struct xfs_btree_block *block, /* btree short form block pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | int level, /* level of the btree block */ |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 93 | struct xfs_buf *bp) /* buffer containing block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 95 | struct xfs_buf *agbp; /* buffer for ag. freespace struct */ |
| 96 | struct xfs_agf *agf; /* ag. freespace structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | xfs_agblock_t agflen; /* native ag. freespace length */ |
| 98 | int sblock_ok; /* block passes checks */ |
| 99 | |
| 100 | agbp = cur->bc_private.a.agbp; |
| 101 | agf = XFS_BUF_TO_AGF(agbp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 102 | agflen = be32_to_cpu(agf->agf_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | sblock_ok = |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 104 | be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] && |
| 105 | be16_to_cpu(block->bb_level) == level && |
| 106 | be16_to_cpu(block->bb_numrecs) <= |
Christoph Hellwig | ce5e42d | 2008-10-30 16:55:23 +1100 | [diff] [blame] | 107 | cur->bc_ops->get_maxrecs(cur, level) && |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 108 | (be32_to_cpu(block->bb_u.s.bb_leftsib) == NULLAGBLOCK || |
| 109 | be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) && |
| 110 | block->bb_u.s.bb_leftsib && |
| 111 | (be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK || |
| 112 | be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) && |
| 113 | block->bb_u.s.bb_rightsib; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp, |
| 115 | XFS_ERRTAG_BTREE_CHECK_SBLOCK, |
| 116 | XFS_RANDOM_BTREE_CHECK_SBLOCK))) { |
| 117 | if (bp) |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 118 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
Eric Sandeen | e0c222c | 2009-07-20 10:52:15 -0500 | [diff] [blame] | 119 | XFS_CORRUPTION_ERROR("xfs_btree_check_sblock", |
| 120 | XFS_ERRLEVEL_LOW, cur->bc_mp, block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | return XFS_ERROR(EFSCORRUPTED); |
| 122 | } |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | /* |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 127 | * Debug routine: check that block header is ok. |
| 128 | */ |
| 129 | int |
| 130 | xfs_btree_check_block( |
| 131 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 132 | struct xfs_btree_block *block, /* generic btree block pointer */ |
| 133 | int level, /* level of the btree block */ |
| 134 | struct xfs_buf *bp) /* buffer containing block, if any */ |
| 135 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 136 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
| 137 | return xfs_btree_check_lblock(cur, block, level, bp); |
| 138 | else |
| 139 | return xfs_btree_check_sblock(cur, block, level, bp); |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Check that (long) pointer is ok. |
| 144 | */ |
| 145 | int /* error (0 or EFSCORRUPTED) */ |
| 146 | xfs_btree_check_lptr( |
| 147 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 148 | xfs_dfsbno_t bno, /* btree block disk address */ |
| 149 | int level) /* btree block level */ |
| 150 | { |
| 151 | XFS_WANT_CORRUPTED_RETURN( |
| 152 | level > 0 && |
| 153 | bno != NULLDFSBNO && |
| 154 | XFS_FSB_SANITY_CHECK(cur->bc_mp, bno)); |
| 155 | return 0; |
| 156 | } |
| 157 | |
Lachlan McIlroy | 24ee0e4 | 2008-10-30 17:05:26 +1100 | [diff] [blame] | 158 | #ifdef DEBUG |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 159 | /* |
| 160 | * Check that (short) pointer is ok. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | */ |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 162 | STATIC int /* error (0 or EFSCORRUPTED) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | xfs_btree_check_sptr( |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 164 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 165 | xfs_agblock_t bno, /* btree block disk address */ |
| 166 | int level) /* btree block level */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 168 | xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | XFS_WANT_CORRUPTED_RETURN( |
| 171 | level > 0 && |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 172 | bno != NULLAGBLOCK && |
| 173 | bno != 0 && |
| 174 | bno < agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /* |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 179 | * Check that block ptr is ok. |
| 180 | */ |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 181 | STATIC int /* error (0 or EFSCORRUPTED) */ |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 182 | xfs_btree_check_ptr( |
| 183 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 184 | union xfs_btree_ptr *ptr, /* btree block disk address */ |
| 185 | int index, /* offset from ptr to check */ |
| 186 | int level) /* btree block level */ |
| 187 | { |
| 188 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { |
| 189 | return xfs_btree_check_lptr(cur, |
| 190 | be64_to_cpu((&ptr->l)[index]), level); |
| 191 | } else { |
| 192 | return xfs_btree_check_sptr(cur, |
| 193 | be32_to_cpu((&ptr->s)[index]), level); |
| 194 | } |
| 195 | } |
Lachlan McIlroy | 24ee0e4 | 2008-10-30 17:05:26 +1100 | [diff] [blame] | 196 | #endif |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 197 | |
| 198 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | * Delete the btree cursor. |
| 200 | */ |
| 201 | void |
| 202 | xfs_btree_del_cursor( |
| 203 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 204 | int error) /* del because of error */ |
| 205 | { |
| 206 | int i; /* btree level */ |
| 207 | |
| 208 | /* |
| 209 | * Clear the buffer pointers, and release the buffers. |
| 210 | * If we're doing this in the face of an error, we |
| 211 | * need to make sure to inspect all of the entries |
| 212 | * in the bc_bufs array for buffers to be unlocked. |
| 213 | * This is because some of the btree code works from |
| 214 | * level n down to 0, and if we get an error along |
| 215 | * the way we won't have initialized all the entries |
| 216 | * down to 0. |
| 217 | */ |
| 218 | for (i = 0; i < cur->bc_nlevels; i++) { |
| 219 | if (cur->bc_bufs[i]) |
| 220 | xfs_btree_setbuf(cur, i, NULL); |
| 221 | else if (!error) |
| 222 | break; |
| 223 | } |
| 224 | /* |
| 225 | * Can't free a bmap cursor without having dealt with the |
| 226 | * allocated indirect blocks' accounting. |
| 227 | */ |
| 228 | ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP || |
| 229 | cur->bc_private.b.allocated == 0); |
| 230 | /* |
| 231 | * Free the cursor. |
| 232 | */ |
| 233 | kmem_zone_free(xfs_btree_cur_zone, cur); |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Duplicate the btree cursor. |
| 238 | * Allocate a new one, copy the record, re-get the buffers. |
| 239 | */ |
| 240 | int /* error */ |
| 241 | xfs_btree_dup_cursor( |
| 242 | xfs_btree_cur_t *cur, /* input cursor */ |
| 243 | xfs_btree_cur_t **ncur) /* output cursor */ |
| 244 | { |
| 245 | xfs_buf_t *bp; /* btree block's buffer pointer */ |
| 246 | int error; /* error return value */ |
| 247 | int i; /* level number of btree block */ |
| 248 | xfs_mount_t *mp; /* mount structure for filesystem */ |
| 249 | xfs_btree_cur_t *new; /* new cursor value */ |
| 250 | xfs_trans_t *tp; /* transaction pointer, can be NULL */ |
| 251 | |
| 252 | tp = cur->bc_tp; |
| 253 | mp = cur->bc_mp; |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | /* |
| 256 | * Allocate a new cursor like the old one. |
| 257 | */ |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 258 | new = cur->bc_ops->dup_cursor(cur); |
| 259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | /* |
| 261 | * Copy the record currently in the cursor. |
| 262 | */ |
| 263 | new->bc_rec = cur->bc_rec; |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | /* |
| 266 | * For each level current, re-get the buffer and copy the ptr value. |
| 267 | */ |
| 268 | for (i = 0; i < new->bc_nlevels; i++) { |
| 269 | new->bc_ptrs[i] = cur->bc_ptrs[i]; |
| 270 | new->bc_ra[i] = cur->bc_ra[i]; |
| 271 | if ((bp = cur->bc_bufs[i])) { |
| 272 | if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, |
| 273 | XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) { |
| 274 | xfs_btree_del_cursor(new, error); |
| 275 | *ncur = NULL; |
| 276 | return error; |
| 277 | } |
| 278 | new->bc_bufs[i] = bp; |
| 279 | ASSERT(bp); |
| 280 | ASSERT(!XFS_BUF_GETERROR(bp)); |
| 281 | } else |
| 282 | new->bc_bufs[i] = NULL; |
| 283 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | *ncur = new; |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | /* |
Christoph Hellwig | 65f1eae | 2008-10-30 16:55:34 +1100 | [diff] [blame] | 289 | * XFS btree block layout and addressing: |
| 290 | * |
| 291 | * There are two types of blocks in the btree: leaf and non-leaf blocks. |
| 292 | * |
| 293 | * The leaf record start with a header then followed by records containing |
| 294 | * the values. A non-leaf block also starts with the same header, and |
| 295 | * then first contains lookup keys followed by an equal number of pointers |
| 296 | * to the btree blocks at the previous level. |
| 297 | * |
| 298 | * +--------+-------+-------+-------+-------+-------+-------+ |
| 299 | * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N | |
| 300 | * +--------+-------+-------+-------+-------+-------+-------+ |
| 301 | * |
| 302 | * +--------+-------+-------+-------+-------+-------+-------+ |
| 303 | * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N | |
| 304 | * +--------+-------+-------+-------+-------+-------+-------+ |
| 305 | * |
| 306 | * The header is called struct xfs_btree_block for reasons better left unknown |
| 307 | * and comes in different versions for short (32bit) and long (64bit) block |
| 308 | * pointers. The record and key structures are defined by the btree instances |
| 309 | * and opaque to the btree core. The block pointers are simple disk endian |
| 310 | * integers, available in a short (32bit) and long (64bit) variant. |
| 311 | * |
| 312 | * The helpers below calculate the offset of a given record, key or pointer |
| 313 | * into a btree block (xfs_btree_*_offset) or return a pointer to the given |
| 314 | * record, key or pointer (xfs_btree_*_addr). Note that all addressing |
| 315 | * inside the btree block is done using indices starting at one, not zero! |
| 316 | */ |
| 317 | |
| 318 | /* |
| 319 | * Return size of the btree block header for this btree instance. |
| 320 | */ |
| 321 | static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur) |
| 322 | { |
| 323 | return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ? |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 324 | XFS_BTREE_LBLOCK_LEN : |
| 325 | XFS_BTREE_SBLOCK_LEN; |
Christoph Hellwig | 65f1eae | 2008-10-30 16:55:34 +1100 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* |
| 329 | * Return size of btree block pointers for this btree instance. |
| 330 | */ |
| 331 | static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur) |
| 332 | { |
| 333 | return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ? |
| 334 | sizeof(__be64) : sizeof(__be32); |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Calculate offset of the n-th record in a btree block. |
| 339 | */ |
| 340 | STATIC size_t |
| 341 | xfs_btree_rec_offset( |
| 342 | struct xfs_btree_cur *cur, |
| 343 | int n) |
| 344 | { |
| 345 | return xfs_btree_block_len(cur) + |
| 346 | (n - 1) * cur->bc_ops->rec_len; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Calculate offset of the n-th key in a btree block. |
| 351 | */ |
| 352 | STATIC size_t |
| 353 | xfs_btree_key_offset( |
| 354 | struct xfs_btree_cur *cur, |
| 355 | int n) |
| 356 | { |
| 357 | return xfs_btree_block_len(cur) + |
| 358 | (n - 1) * cur->bc_ops->key_len; |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * Calculate offset of the n-th block pointer in a btree block. |
| 363 | */ |
| 364 | STATIC size_t |
| 365 | xfs_btree_ptr_offset( |
| 366 | struct xfs_btree_cur *cur, |
| 367 | int n, |
| 368 | int level) |
| 369 | { |
| 370 | return xfs_btree_block_len(cur) + |
| 371 | cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len + |
| 372 | (n - 1) * xfs_btree_ptr_len(cur); |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Return a pointer to the n-th record in the btree block. |
| 377 | */ |
| 378 | STATIC union xfs_btree_rec * |
| 379 | xfs_btree_rec_addr( |
| 380 | struct xfs_btree_cur *cur, |
| 381 | int n, |
| 382 | struct xfs_btree_block *block) |
| 383 | { |
| 384 | return (union xfs_btree_rec *) |
| 385 | ((char *)block + xfs_btree_rec_offset(cur, n)); |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * Return a pointer to the n-th key in the btree block. |
| 390 | */ |
| 391 | STATIC union xfs_btree_key * |
| 392 | xfs_btree_key_addr( |
| 393 | struct xfs_btree_cur *cur, |
| 394 | int n, |
| 395 | struct xfs_btree_block *block) |
| 396 | { |
| 397 | return (union xfs_btree_key *) |
| 398 | ((char *)block + xfs_btree_key_offset(cur, n)); |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Return a pointer to the n-th block pointer in the btree block. |
| 403 | */ |
| 404 | STATIC union xfs_btree_ptr * |
| 405 | xfs_btree_ptr_addr( |
| 406 | struct xfs_btree_cur *cur, |
| 407 | int n, |
| 408 | struct xfs_btree_block *block) |
| 409 | { |
| 410 | int level = xfs_btree_get_level(block); |
| 411 | |
| 412 | ASSERT(block->bb_level != 0); |
| 413 | |
| 414 | return (union xfs_btree_ptr *) |
| 415 | ((char *)block + xfs_btree_ptr_offset(cur, n, level)); |
| 416 | } |
| 417 | |
| 418 | /* |
Christoph Hellwig | 8186e51 | 2008-10-30 16:54:22 +1100 | [diff] [blame] | 419 | * Get a the root block which is stored in the inode. |
| 420 | * |
| 421 | * For now this btree implementation assumes the btree root is always |
| 422 | * stored in the if_broot field of an inode fork. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | */ |
Christoph Hellwig | 8186e51 | 2008-10-30 16:54:22 +1100 | [diff] [blame] | 424 | STATIC struct xfs_btree_block * |
| 425 | xfs_btree_get_iroot( |
| 426 | struct xfs_btree_cur *cur) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { |
Christoph Hellwig | 8186e51 | 2008-10-30 16:54:22 +1100 | [diff] [blame] | 428 | struct xfs_ifork *ifp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Christoph Hellwig | 8186e51 | 2008-10-30 16:54:22 +1100 | [diff] [blame] | 430 | ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork); |
| 431 | return (struct xfs_btree_block *)ifp->if_broot; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Retrieve the block pointer from the cursor at the given level. |
| 436 | * This may be an inode btree root or from a buffer. |
| 437 | */ |
| 438 | STATIC struct xfs_btree_block * /* generic btree block pointer */ |
| 439 | xfs_btree_get_block( |
| 440 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 441 | int level, /* level in btree */ |
| 442 | struct xfs_buf **bpp) /* buffer containing the block */ |
| 443 | { |
| 444 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && |
| 445 | (level == cur->bc_nlevels - 1)) { |
| 446 | *bpp = NULL; |
| 447 | return xfs_btree_get_iroot(cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | } |
Christoph Hellwig | 8186e51 | 2008-10-30 16:54:22 +1100 | [diff] [blame] | 449 | |
| 450 | *bpp = cur->bc_bufs[level]; |
| 451 | return XFS_BUF_TO_BLOCK(*bpp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* |
| 455 | * Get a buffer for the block, return it with no data read. |
| 456 | * Long-form addressing. |
| 457 | */ |
| 458 | xfs_buf_t * /* buffer for fsbno */ |
| 459 | xfs_btree_get_bufl( |
| 460 | xfs_mount_t *mp, /* file system mount point */ |
| 461 | xfs_trans_t *tp, /* transaction pointer */ |
| 462 | xfs_fsblock_t fsbno, /* file system block number */ |
| 463 | uint lock) /* lock flags for get_buf */ |
| 464 | { |
| 465 | xfs_buf_t *bp; /* buffer pointer (return value) */ |
| 466 | xfs_daddr_t d; /* real disk block address */ |
| 467 | |
| 468 | ASSERT(fsbno != NULLFSBLOCK); |
| 469 | d = XFS_FSB_TO_DADDR(mp, fsbno); |
| 470 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock); |
| 471 | ASSERT(bp); |
| 472 | ASSERT(!XFS_BUF_GETERROR(bp)); |
| 473 | return bp; |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * Get a buffer for the block, return it with no data read. |
| 478 | * Short-form addressing. |
| 479 | */ |
| 480 | xfs_buf_t * /* buffer for agno/agbno */ |
| 481 | xfs_btree_get_bufs( |
| 482 | xfs_mount_t *mp, /* file system mount point */ |
| 483 | xfs_trans_t *tp, /* transaction pointer */ |
| 484 | xfs_agnumber_t agno, /* allocation group number */ |
| 485 | xfs_agblock_t agbno, /* allocation group block number */ |
| 486 | uint lock) /* lock flags for get_buf */ |
| 487 | { |
| 488 | xfs_buf_t *bp; /* buffer pointer (return value) */ |
| 489 | xfs_daddr_t d; /* real disk block address */ |
| 490 | |
| 491 | ASSERT(agno != NULLAGNUMBER); |
| 492 | ASSERT(agbno != NULLAGBLOCK); |
| 493 | d = XFS_AGB_TO_DADDR(mp, agno, agbno); |
| 494 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock); |
| 495 | ASSERT(bp); |
| 496 | ASSERT(!XFS_BUF_GETERROR(bp)); |
| 497 | return bp; |
| 498 | } |
| 499 | |
| 500 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | * Check for the cursor referring to the last block at the given level. |
| 502 | */ |
| 503 | int /* 1=is last block, 0=not last block */ |
| 504 | xfs_btree_islastblock( |
| 505 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 506 | int level) /* level to check */ |
| 507 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 508 | struct xfs_btree_block *block; /* generic btree block pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | xfs_buf_t *bp; /* buffer containing block */ |
| 510 | |
| 511 | block = xfs_btree_get_block(cur, level, &bp); |
| 512 | xfs_btree_check_block(cur, block, level, bp); |
Christoph Hellwig | e99ab90 | 2008-10-30 16:54:33 +1100 | [diff] [blame] | 513 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 514 | return be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | else |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 516 | return be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /* |
Christoph Hellwig | cdcf4333 | 2008-08-13 16:23:50 +1000 | [diff] [blame] | 520 | * Change the cursor to point to the first record at the given level. |
| 521 | * Other levels are unaffected. |
| 522 | */ |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 523 | STATIC int /* success=1, failure=0 */ |
Christoph Hellwig | cdcf4333 | 2008-08-13 16:23:50 +1000 | [diff] [blame] | 524 | xfs_btree_firstrec( |
| 525 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 526 | int level) /* level to change */ |
| 527 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 528 | struct xfs_btree_block *block; /* generic btree block pointer */ |
Christoph Hellwig | cdcf4333 | 2008-08-13 16:23:50 +1000 | [diff] [blame] | 529 | xfs_buf_t *bp; /* buffer containing block */ |
| 530 | |
| 531 | /* |
| 532 | * Get the block pointer for this level. |
| 533 | */ |
| 534 | block = xfs_btree_get_block(cur, level, &bp); |
| 535 | xfs_btree_check_block(cur, block, level, bp); |
| 536 | /* |
| 537 | * It's empty, there is no such record. |
| 538 | */ |
Christoph Hellwig | f2277f0 | 2008-10-30 16:53:47 +1100 | [diff] [blame] | 539 | if (!block->bb_numrecs) |
Christoph Hellwig | cdcf4333 | 2008-08-13 16:23:50 +1000 | [diff] [blame] | 540 | return 0; |
| 541 | /* |
| 542 | * Set the ptr value to 1, that's the first record/key. |
| 543 | */ |
| 544 | cur->bc_ptrs[level] = 1; |
| 545 | return 1; |
| 546 | } |
| 547 | |
| 548 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | * Change the cursor to point to the last record in the current block |
| 550 | * at the given level. Other levels are unaffected. |
| 551 | */ |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 552 | STATIC int /* success=1, failure=0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | xfs_btree_lastrec( |
| 554 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 555 | int level) /* level to change */ |
| 556 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 557 | struct xfs_btree_block *block; /* generic btree block pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | xfs_buf_t *bp; /* buffer containing block */ |
| 559 | |
| 560 | /* |
| 561 | * Get the block pointer for this level. |
| 562 | */ |
| 563 | block = xfs_btree_get_block(cur, level, &bp); |
| 564 | xfs_btree_check_block(cur, block, level, bp); |
| 565 | /* |
| 566 | * It's empty, there is no such record. |
| 567 | */ |
Christoph Hellwig | f2277f0 | 2008-10-30 16:53:47 +1100 | [diff] [blame] | 568 | if (!block->bb_numrecs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | return 0; |
| 570 | /* |
| 571 | * Set the ptr value to numrecs, that's the last record/key. |
| 572 | */ |
Christoph Hellwig | f2277f0 | 2008-10-30 16:53:47 +1100 | [diff] [blame] | 573 | cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | return 1; |
| 575 | } |
| 576 | |
| 577 | /* |
| 578 | * Compute first and last byte offsets for the fields given. |
| 579 | * Interprets the offsets table, which contains struct field offsets. |
| 580 | */ |
| 581 | void |
| 582 | xfs_btree_offsets( |
| 583 | __int64_t fields, /* bitmask of fields */ |
| 584 | const short *offsets, /* table of field offsets */ |
| 585 | int nbits, /* number of bits to inspect */ |
| 586 | int *first, /* output: first byte offset */ |
| 587 | int *last) /* output: last byte offset */ |
| 588 | { |
| 589 | int i; /* current bit number */ |
| 590 | __int64_t imask; /* mask for current bit number */ |
| 591 | |
| 592 | ASSERT(fields != 0); |
| 593 | /* |
| 594 | * Find the lowest bit, so the first byte offset. |
| 595 | */ |
| 596 | for (i = 0, imask = 1LL; ; i++, imask <<= 1) { |
| 597 | if (imask & fields) { |
| 598 | *first = offsets[i]; |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | /* |
| 603 | * Find the highest bit, so the last byte offset. |
| 604 | */ |
| 605 | for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) { |
| 606 | if (imask & fields) { |
| 607 | *last = offsets[i + 1] - 1; |
| 608 | break; |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | /* |
| 614 | * Get a buffer for the block, return it read in. |
| 615 | * Long-form addressing. |
| 616 | */ |
| 617 | int /* error */ |
| 618 | xfs_btree_read_bufl( |
| 619 | xfs_mount_t *mp, /* file system mount point */ |
| 620 | xfs_trans_t *tp, /* transaction pointer */ |
| 621 | xfs_fsblock_t fsbno, /* file system block number */ |
| 622 | uint lock, /* lock flags for read_buf */ |
| 623 | xfs_buf_t **bpp, /* buffer for fsbno */ |
| 624 | int refval) /* ref count value for buffer */ |
| 625 | { |
| 626 | xfs_buf_t *bp; /* return value */ |
| 627 | xfs_daddr_t d; /* real disk block address */ |
| 628 | int error; |
| 629 | |
| 630 | ASSERT(fsbno != NULLFSBLOCK); |
| 631 | d = XFS_FSB_TO_DADDR(mp, fsbno); |
| 632 | if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d, |
| 633 | mp->m_bsize, lock, &bp))) { |
| 634 | return error; |
| 635 | } |
| 636 | ASSERT(!bp || !XFS_BUF_GETERROR(bp)); |
| 637 | if (bp != NULL) { |
| 638 | XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval); |
| 639 | } |
| 640 | *bpp = bp; |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | * Read-ahead the block, don't wait for it, don't return a buffer. |
| 646 | * Long-form addressing. |
| 647 | */ |
| 648 | /* ARGSUSED */ |
| 649 | void |
| 650 | xfs_btree_reada_bufl( |
| 651 | xfs_mount_t *mp, /* file system mount point */ |
| 652 | xfs_fsblock_t fsbno, /* file system block number */ |
| 653 | xfs_extlen_t count) /* count of filesystem blocks */ |
| 654 | { |
| 655 | xfs_daddr_t d; |
| 656 | |
| 657 | ASSERT(fsbno != NULLFSBLOCK); |
| 658 | d = XFS_FSB_TO_DADDR(mp, fsbno); |
| 659 | xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count); |
| 660 | } |
| 661 | |
| 662 | /* |
| 663 | * Read-ahead the block, don't wait for it, don't return a buffer. |
| 664 | * Short-form addressing. |
| 665 | */ |
| 666 | /* ARGSUSED */ |
| 667 | void |
| 668 | xfs_btree_reada_bufs( |
| 669 | xfs_mount_t *mp, /* file system mount point */ |
| 670 | xfs_agnumber_t agno, /* allocation group number */ |
| 671 | xfs_agblock_t agbno, /* allocation group block number */ |
| 672 | xfs_extlen_t count) /* count of filesystem blocks */ |
| 673 | { |
| 674 | xfs_daddr_t d; |
| 675 | |
| 676 | ASSERT(agno != NULLAGNUMBER); |
| 677 | ASSERT(agbno != NULLAGBLOCK); |
| 678 | d = XFS_AGB_TO_DADDR(mp, agno, agbno); |
| 679 | xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count); |
| 680 | } |
| 681 | |
Christoph Hellwig | b524bfe | 2008-10-30 16:54:43 +1100 | [diff] [blame] | 682 | STATIC int |
| 683 | xfs_btree_readahead_lblock( |
| 684 | struct xfs_btree_cur *cur, |
| 685 | int lr, |
| 686 | struct xfs_btree_block *block) |
| 687 | { |
| 688 | int rval = 0; |
Christoph Hellwig | e6edbd1 | 2009-01-08 13:42:23 -0500 | [diff] [blame] | 689 | xfs_dfsbno_t left = be64_to_cpu(block->bb_u.l.bb_leftsib); |
| 690 | xfs_dfsbno_t right = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Christoph Hellwig | b524bfe | 2008-10-30 16:54:43 +1100 | [diff] [blame] | 691 | |
| 692 | if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) { |
| 693 | xfs_btree_reada_bufl(cur->bc_mp, left, 1); |
| 694 | rval++; |
| 695 | } |
| 696 | |
| 697 | if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) { |
| 698 | xfs_btree_reada_bufl(cur->bc_mp, right, 1); |
| 699 | rval++; |
| 700 | } |
| 701 | |
| 702 | return rval; |
| 703 | } |
| 704 | |
| 705 | STATIC int |
| 706 | xfs_btree_readahead_sblock( |
| 707 | struct xfs_btree_cur *cur, |
| 708 | int lr, |
| 709 | struct xfs_btree_block *block) |
| 710 | { |
| 711 | int rval = 0; |
| 712 | xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib); |
| 713 | xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib); |
| 714 | |
| 715 | |
| 716 | if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) { |
| 717 | xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno, |
| 718 | left, 1); |
| 719 | rval++; |
| 720 | } |
| 721 | |
| 722 | if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) { |
| 723 | xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno, |
| 724 | right, 1); |
| 725 | rval++; |
| 726 | } |
| 727 | |
| 728 | return rval; |
| 729 | } |
| 730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | /* |
| 732 | * Read-ahead btree blocks, at the given level. |
| 733 | * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA. |
| 734 | */ |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 735 | STATIC int |
Christoph Hellwig | b524bfe | 2008-10-30 16:54:43 +1100 | [diff] [blame] | 736 | xfs_btree_readahead( |
| 737 | struct xfs_btree_cur *cur, /* btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | int lev, /* level in btree */ |
| 739 | int lr) /* left/right bits */ |
| 740 | { |
Christoph Hellwig | b524bfe | 2008-10-30 16:54:43 +1100 | [diff] [blame] | 741 | struct xfs_btree_block *block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | |
Christoph Hellwig | b524bfe | 2008-10-30 16:54:43 +1100 | [diff] [blame] | 743 | /* |
| 744 | * No readahead needed if we are at the root level and the |
| 745 | * btree root is stored in the inode. |
| 746 | */ |
| 747 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && |
| 748 | (lev == cur->bc_nlevels - 1)) |
| 749 | return 0; |
| 750 | |
| 751 | if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev]) |
| 752 | return 0; |
| 753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | cur->bc_ra[lev] |= lr; |
Christoph Hellwig | b524bfe | 2008-10-30 16:54:43 +1100 | [diff] [blame] | 755 | block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]); |
| 756 | |
| 757 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
| 758 | return xfs_btree_readahead_lblock(cur, lr, block); |
| 759 | return xfs_btree_readahead_sblock(cur, lr, block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | /* |
| 763 | * Set the buffer for level "lev" in the cursor to bp, releasing |
| 764 | * any previous buffer. |
| 765 | */ |
| 766 | void |
| 767 | xfs_btree_setbuf( |
| 768 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 769 | int lev, /* level in btree */ |
| 770 | xfs_buf_t *bp) /* new buffer to set */ |
| 771 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 772 | struct xfs_btree_block *b; /* btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | xfs_buf_t *obp; /* old buffer pointer */ |
| 774 | |
| 775 | obp = cur->bc_bufs[lev]; |
| 776 | if (obp) |
| 777 | xfs_trans_brelse(cur->bc_tp, obp); |
| 778 | cur->bc_bufs[lev] = bp; |
| 779 | cur->bc_ra[lev] = 0; |
| 780 | if (!bp) |
| 781 | return; |
| 782 | b = XFS_BUF_TO_BLOCK(bp); |
Christoph Hellwig | e99ab90 | 2008-10-30 16:54:33 +1100 | [diff] [blame] | 783 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 784 | if (be64_to_cpu(b->bb_u.l.bb_leftsib) == NULLDFSBNO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 786 | if (be64_to_cpu(b->bb_u.l.bb_rightsib) == NULLDFSBNO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA; |
| 788 | } else { |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 789 | if (be32_to_cpu(b->bb_u.s.bb_leftsib) == NULLAGBLOCK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 791 | if (be32_to_cpu(b->bb_u.s.bb_rightsib) == NULLAGBLOCK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA; |
| 793 | } |
| 794 | } |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 795 | |
| 796 | STATIC int |
| 797 | xfs_btree_ptr_is_null( |
| 798 | struct xfs_btree_cur *cur, |
| 799 | union xfs_btree_ptr *ptr) |
| 800 | { |
| 801 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
Dave Chinner | 33ad965 | 2009-01-21 15:22:17 +1100 | [diff] [blame] | 802 | return be64_to_cpu(ptr->l) == NULLDFSBNO; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 803 | else |
| 804 | return be32_to_cpu(ptr->s) == NULLAGBLOCK; |
| 805 | } |
| 806 | |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 807 | STATIC void |
| 808 | xfs_btree_set_ptr_null( |
| 809 | struct xfs_btree_cur *cur, |
| 810 | union xfs_btree_ptr *ptr) |
| 811 | { |
| 812 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
Dave Chinner | 33ad965 | 2009-01-21 15:22:17 +1100 | [diff] [blame] | 813 | ptr->l = cpu_to_be64(NULLDFSBNO); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 814 | else |
| 815 | ptr->s = cpu_to_be32(NULLAGBLOCK); |
| 816 | } |
| 817 | |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 818 | /* |
| 819 | * Get/set/init sibling pointers |
| 820 | */ |
| 821 | STATIC void |
| 822 | xfs_btree_get_sibling( |
| 823 | struct xfs_btree_cur *cur, |
| 824 | struct xfs_btree_block *block, |
| 825 | union xfs_btree_ptr *ptr, |
| 826 | int lr) |
| 827 | { |
| 828 | ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB); |
| 829 | |
| 830 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { |
| 831 | if (lr == XFS_BB_RIGHTSIB) |
| 832 | ptr->l = block->bb_u.l.bb_rightsib; |
| 833 | else |
| 834 | ptr->l = block->bb_u.l.bb_leftsib; |
| 835 | } else { |
| 836 | if (lr == XFS_BB_RIGHTSIB) |
| 837 | ptr->s = block->bb_u.s.bb_rightsib; |
| 838 | else |
| 839 | ptr->s = block->bb_u.s.bb_leftsib; |
| 840 | } |
| 841 | } |
| 842 | |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 843 | STATIC void |
| 844 | xfs_btree_set_sibling( |
| 845 | struct xfs_btree_cur *cur, |
| 846 | struct xfs_btree_block *block, |
| 847 | union xfs_btree_ptr *ptr, |
| 848 | int lr) |
| 849 | { |
| 850 | ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB); |
| 851 | |
| 852 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { |
| 853 | if (lr == XFS_BB_RIGHTSIB) |
| 854 | block->bb_u.l.bb_rightsib = ptr->l; |
| 855 | else |
| 856 | block->bb_u.l.bb_leftsib = ptr->l; |
| 857 | } else { |
| 858 | if (lr == XFS_BB_RIGHTSIB) |
| 859 | block->bb_u.s.bb_rightsib = ptr->s; |
| 860 | else |
| 861 | block->bb_u.s.bb_leftsib = ptr->s; |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | STATIC void |
| 866 | xfs_btree_init_block( |
| 867 | struct xfs_btree_cur *cur, |
| 868 | int level, |
| 869 | int numrecs, |
| 870 | struct xfs_btree_block *new) /* new block */ |
| 871 | { |
| 872 | new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]); |
| 873 | new->bb_level = cpu_to_be16(level); |
| 874 | new->bb_numrecs = cpu_to_be16(numrecs); |
| 875 | |
| 876 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { |
Dave Chinner | 33ad965 | 2009-01-21 15:22:17 +1100 | [diff] [blame] | 877 | new->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 878 | new->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 879 | } else { |
| 880 | new->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK); |
| 881 | new->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK); |
| 882 | } |
| 883 | } |
| 884 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 885 | /* |
| 886 | * Return true if ptr is the last record in the btree and |
| 887 | * we need to track updateѕ to this record. The decision |
| 888 | * will be further refined in the update_lastrec method. |
| 889 | */ |
| 890 | STATIC int |
| 891 | xfs_btree_is_lastrec( |
| 892 | struct xfs_btree_cur *cur, |
| 893 | struct xfs_btree_block *block, |
| 894 | int level) |
| 895 | { |
| 896 | union xfs_btree_ptr ptr; |
| 897 | |
| 898 | if (level > 0) |
| 899 | return 0; |
| 900 | if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE)) |
| 901 | return 0; |
| 902 | |
| 903 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB); |
| 904 | if (!xfs_btree_ptr_is_null(cur, &ptr)) |
| 905 | return 0; |
| 906 | return 1; |
| 907 | } |
| 908 | |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 909 | STATIC void |
| 910 | xfs_btree_buf_to_ptr( |
| 911 | struct xfs_btree_cur *cur, |
| 912 | struct xfs_buf *bp, |
| 913 | union xfs_btree_ptr *ptr) |
| 914 | { |
| 915 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
| 916 | ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp, |
| 917 | XFS_BUF_ADDR(bp))); |
| 918 | else { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 919 | ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp, |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 920 | XFS_BUF_ADDR(bp))); |
| 921 | } |
| 922 | } |
| 923 | |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 924 | STATIC xfs_daddr_t |
| 925 | xfs_btree_ptr_to_daddr( |
| 926 | struct xfs_btree_cur *cur, |
| 927 | union xfs_btree_ptr *ptr) |
| 928 | { |
| 929 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { |
Dave Chinner | 33ad965 | 2009-01-21 15:22:17 +1100 | [diff] [blame] | 930 | ASSERT(be64_to_cpu(ptr->l) != NULLDFSBNO); |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 931 | |
| 932 | return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l)); |
| 933 | } else { |
| 934 | ASSERT(cur->bc_private.a.agno != NULLAGNUMBER); |
| 935 | ASSERT(be32_to_cpu(ptr->s) != NULLAGBLOCK); |
| 936 | |
| 937 | return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno, |
| 938 | be32_to_cpu(ptr->s)); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | STATIC void |
| 943 | xfs_btree_set_refs( |
| 944 | struct xfs_btree_cur *cur, |
| 945 | struct xfs_buf *bp) |
| 946 | { |
| 947 | switch (cur->bc_btnum) { |
| 948 | case XFS_BTNUM_BNO: |
| 949 | case XFS_BTNUM_CNT: |
| 950 | XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_ALLOC_BTREE_REF); |
| 951 | break; |
| 952 | case XFS_BTNUM_INO: |
| 953 | XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_INOMAP, XFS_INO_BTREE_REF); |
| 954 | break; |
| 955 | case XFS_BTNUM_BMAP: |
| 956 | XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_BMAP_BTREE_REF); |
| 957 | break; |
| 958 | default: |
| 959 | ASSERT(0); |
| 960 | } |
| 961 | } |
| 962 | |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 963 | STATIC int |
| 964 | xfs_btree_get_buf_block( |
| 965 | struct xfs_btree_cur *cur, |
| 966 | union xfs_btree_ptr *ptr, |
| 967 | int flags, |
| 968 | struct xfs_btree_block **block, |
| 969 | struct xfs_buf **bpp) |
| 970 | { |
| 971 | struct xfs_mount *mp = cur->bc_mp; |
| 972 | xfs_daddr_t d; |
| 973 | |
| 974 | /* need to sort out how callers deal with failures first */ |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 975 | ASSERT(!(flags & XBF_TRYLOCK)); |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 976 | |
| 977 | d = xfs_btree_ptr_to_daddr(cur, ptr); |
| 978 | *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d, |
| 979 | mp->m_bsize, flags); |
| 980 | |
| 981 | ASSERT(*bpp); |
| 982 | ASSERT(!XFS_BUF_GETERROR(*bpp)); |
| 983 | |
| 984 | *block = XFS_BUF_TO_BLOCK(*bpp); |
| 985 | return 0; |
| 986 | } |
| 987 | |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 988 | /* |
| 989 | * Read in the buffer at the given ptr and return the buffer and |
| 990 | * the block pointer within the buffer. |
| 991 | */ |
| 992 | STATIC int |
| 993 | xfs_btree_read_buf_block( |
| 994 | struct xfs_btree_cur *cur, |
| 995 | union xfs_btree_ptr *ptr, |
| 996 | int level, |
| 997 | int flags, |
| 998 | struct xfs_btree_block **block, |
| 999 | struct xfs_buf **bpp) |
| 1000 | { |
| 1001 | struct xfs_mount *mp = cur->bc_mp; |
| 1002 | xfs_daddr_t d; |
| 1003 | int error; |
| 1004 | |
| 1005 | /* need to sort out how callers deal with failures first */ |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 1006 | ASSERT(!(flags & XBF_TRYLOCK)); |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1007 | |
| 1008 | d = xfs_btree_ptr_to_daddr(cur, ptr); |
| 1009 | error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d, |
| 1010 | mp->m_bsize, flags, bpp); |
| 1011 | if (error) |
| 1012 | return error; |
| 1013 | |
| 1014 | ASSERT(*bpp != NULL); |
| 1015 | ASSERT(!XFS_BUF_GETERROR(*bpp)); |
| 1016 | |
| 1017 | xfs_btree_set_refs(cur, *bpp); |
| 1018 | *block = XFS_BUF_TO_BLOCK(*bpp); |
| 1019 | |
| 1020 | error = xfs_btree_check_block(cur, *block, level, *bpp); |
| 1021 | if (error) |
| 1022 | xfs_trans_brelse(cur->bc_tp, *bpp); |
| 1023 | return error; |
| 1024 | } |
| 1025 | |
| 1026 | /* |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 1027 | * Copy keys from one btree block to another. |
| 1028 | */ |
| 1029 | STATIC void |
| 1030 | xfs_btree_copy_keys( |
| 1031 | struct xfs_btree_cur *cur, |
| 1032 | union xfs_btree_key *dst_key, |
| 1033 | union xfs_btree_key *src_key, |
| 1034 | int numkeys) |
| 1035 | { |
| 1036 | ASSERT(numkeys >= 0); |
| 1037 | memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len); |
| 1038 | } |
| 1039 | |
| 1040 | /* |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 1041 | * Copy records from one btree block to another. |
| 1042 | */ |
| 1043 | STATIC void |
| 1044 | xfs_btree_copy_recs( |
| 1045 | struct xfs_btree_cur *cur, |
| 1046 | union xfs_btree_rec *dst_rec, |
| 1047 | union xfs_btree_rec *src_rec, |
| 1048 | int numrecs) |
| 1049 | { |
| 1050 | ASSERT(numrecs >= 0); |
| 1051 | memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len); |
| 1052 | } |
| 1053 | |
| 1054 | /* |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 1055 | * Copy block pointers from one btree block to another. |
| 1056 | */ |
| 1057 | STATIC void |
| 1058 | xfs_btree_copy_ptrs( |
| 1059 | struct xfs_btree_cur *cur, |
| 1060 | union xfs_btree_ptr *dst_ptr, |
| 1061 | union xfs_btree_ptr *src_ptr, |
| 1062 | int numptrs) |
| 1063 | { |
| 1064 | ASSERT(numptrs >= 0); |
| 1065 | memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur)); |
| 1066 | } |
| 1067 | |
| 1068 | /* |
| 1069 | * Shift keys one index left/right inside a single btree block. |
| 1070 | */ |
| 1071 | STATIC void |
| 1072 | xfs_btree_shift_keys( |
| 1073 | struct xfs_btree_cur *cur, |
| 1074 | union xfs_btree_key *key, |
| 1075 | int dir, |
| 1076 | int numkeys) |
| 1077 | { |
| 1078 | char *dst_key; |
| 1079 | |
| 1080 | ASSERT(numkeys >= 0); |
| 1081 | ASSERT(dir == 1 || dir == -1); |
| 1082 | |
| 1083 | dst_key = (char *)key + (dir * cur->bc_ops->key_len); |
| 1084 | memmove(dst_key, key, numkeys * cur->bc_ops->key_len); |
| 1085 | } |
| 1086 | |
| 1087 | /* |
| 1088 | * Shift records one index left/right inside a single btree block. |
| 1089 | */ |
| 1090 | STATIC void |
| 1091 | xfs_btree_shift_recs( |
| 1092 | struct xfs_btree_cur *cur, |
| 1093 | union xfs_btree_rec *rec, |
| 1094 | int dir, |
| 1095 | int numrecs) |
| 1096 | { |
| 1097 | char *dst_rec; |
| 1098 | |
| 1099 | ASSERT(numrecs >= 0); |
| 1100 | ASSERT(dir == 1 || dir == -1); |
| 1101 | |
| 1102 | dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len); |
| 1103 | memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len); |
| 1104 | } |
| 1105 | |
| 1106 | /* |
| 1107 | * Shift block pointers one index left/right inside a single btree block. |
| 1108 | */ |
| 1109 | STATIC void |
| 1110 | xfs_btree_shift_ptrs( |
| 1111 | struct xfs_btree_cur *cur, |
| 1112 | union xfs_btree_ptr *ptr, |
| 1113 | int dir, |
| 1114 | int numptrs) |
| 1115 | { |
| 1116 | char *dst_ptr; |
| 1117 | |
| 1118 | ASSERT(numptrs >= 0); |
| 1119 | ASSERT(dir == 1 || dir == -1); |
| 1120 | |
| 1121 | dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur)); |
| 1122 | memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur)); |
| 1123 | } |
| 1124 | |
| 1125 | /* |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 1126 | * Log key values from the btree block. |
| 1127 | */ |
| 1128 | STATIC void |
| 1129 | xfs_btree_log_keys( |
| 1130 | struct xfs_btree_cur *cur, |
| 1131 | struct xfs_buf *bp, |
| 1132 | int first, |
| 1133 | int last) |
| 1134 | { |
| 1135 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1136 | XFS_BTREE_TRACE_ARGBII(cur, bp, first, last); |
| 1137 | |
| 1138 | if (bp) { |
| 1139 | xfs_trans_log_buf(cur->bc_tp, bp, |
| 1140 | xfs_btree_key_offset(cur, first), |
| 1141 | xfs_btree_key_offset(cur, last + 1) - 1); |
| 1142 | } else { |
| 1143 | xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip, |
| 1144 | xfs_ilog_fbroot(cur->bc_private.b.whichfork)); |
| 1145 | } |
| 1146 | |
| 1147 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1148 | } |
| 1149 | |
| 1150 | /* |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 1151 | * Log record values from the btree block. |
| 1152 | */ |
Christoph Hellwig | fd6bcc5b | 2008-10-30 16:58:21 +1100 | [diff] [blame] | 1153 | void |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 1154 | xfs_btree_log_recs( |
| 1155 | struct xfs_btree_cur *cur, |
| 1156 | struct xfs_buf *bp, |
| 1157 | int first, |
| 1158 | int last) |
| 1159 | { |
| 1160 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1161 | XFS_BTREE_TRACE_ARGBII(cur, bp, first, last); |
| 1162 | |
| 1163 | xfs_trans_log_buf(cur->bc_tp, bp, |
| 1164 | xfs_btree_rec_offset(cur, first), |
| 1165 | xfs_btree_rec_offset(cur, last + 1) - 1); |
| 1166 | |
| 1167 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1168 | } |
| 1169 | |
| 1170 | /* |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 1171 | * Log block pointer fields from a btree block (nonleaf). |
| 1172 | */ |
| 1173 | STATIC void |
| 1174 | xfs_btree_log_ptrs( |
| 1175 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 1176 | struct xfs_buf *bp, /* buffer containing btree block */ |
| 1177 | int first, /* index of first pointer to log */ |
| 1178 | int last) /* index of last pointer to log */ |
| 1179 | { |
| 1180 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1181 | XFS_BTREE_TRACE_ARGBII(cur, bp, first, last); |
| 1182 | |
| 1183 | if (bp) { |
| 1184 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 1185 | int level = xfs_btree_get_level(block); |
| 1186 | |
| 1187 | xfs_trans_log_buf(cur->bc_tp, bp, |
| 1188 | xfs_btree_ptr_offset(cur, first, level), |
| 1189 | xfs_btree_ptr_offset(cur, last + 1, level) - 1); |
| 1190 | } else { |
| 1191 | xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip, |
| 1192 | xfs_ilog_fbroot(cur->bc_private.b.whichfork)); |
| 1193 | } |
| 1194 | |
| 1195 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1196 | } |
| 1197 | |
| 1198 | /* |
| 1199 | * Log fields from a btree block header. |
| 1200 | */ |
Christoph Hellwig | fd6bcc5b | 2008-10-30 16:58:21 +1100 | [diff] [blame] | 1201 | void |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 1202 | xfs_btree_log_block( |
| 1203 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 1204 | struct xfs_buf *bp, /* buffer containing btree block */ |
| 1205 | int fields) /* mask of fields: XFS_BB_... */ |
| 1206 | { |
| 1207 | int first; /* first byte offset logged */ |
| 1208 | int last; /* last byte offset logged */ |
| 1209 | static const short soffsets[] = { /* table of offsets (short) */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 1210 | offsetof(struct xfs_btree_block, bb_magic), |
| 1211 | offsetof(struct xfs_btree_block, bb_level), |
| 1212 | offsetof(struct xfs_btree_block, bb_numrecs), |
| 1213 | offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib), |
| 1214 | offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib), |
| 1215 | XFS_BTREE_SBLOCK_LEN |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 1216 | }; |
| 1217 | static const short loffsets[] = { /* table of offsets (long) */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 1218 | offsetof(struct xfs_btree_block, bb_magic), |
| 1219 | offsetof(struct xfs_btree_block, bb_level), |
| 1220 | offsetof(struct xfs_btree_block, bb_numrecs), |
| 1221 | offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib), |
| 1222 | offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib), |
| 1223 | XFS_BTREE_LBLOCK_LEN |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 1224 | }; |
| 1225 | |
| 1226 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1227 | XFS_BTREE_TRACE_ARGBI(cur, bp, fields); |
| 1228 | |
| 1229 | if (bp) { |
| 1230 | xfs_btree_offsets(fields, |
| 1231 | (cur->bc_flags & XFS_BTREE_LONG_PTRS) ? |
| 1232 | loffsets : soffsets, |
| 1233 | XFS_BB_NUM_BITS, &first, &last); |
| 1234 | xfs_trans_log_buf(cur->bc_tp, bp, first, last); |
| 1235 | } else { |
| 1236 | xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip, |
| 1237 | xfs_ilog_fbroot(cur->bc_private.b.whichfork)); |
| 1238 | } |
| 1239 | |
| 1240 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1241 | } |
| 1242 | |
| 1243 | /* |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1244 | * Increment cursor by one record at the level. |
| 1245 | * For nonzero levels the leaf-ward information is untouched. |
| 1246 | */ |
| 1247 | int /* error */ |
| 1248 | xfs_btree_increment( |
| 1249 | struct xfs_btree_cur *cur, |
| 1250 | int level, |
| 1251 | int *stat) /* success/failure */ |
| 1252 | { |
| 1253 | struct xfs_btree_block *block; |
| 1254 | union xfs_btree_ptr ptr; |
| 1255 | struct xfs_buf *bp; |
| 1256 | int error; /* error return value */ |
| 1257 | int lev; |
| 1258 | |
| 1259 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1260 | XFS_BTREE_TRACE_ARGI(cur, level); |
| 1261 | |
| 1262 | ASSERT(level < cur->bc_nlevels); |
| 1263 | |
| 1264 | /* Read-ahead to the right at this level. */ |
| 1265 | xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA); |
| 1266 | |
| 1267 | /* Get a pointer to the btree block. */ |
| 1268 | block = xfs_btree_get_block(cur, level, &bp); |
| 1269 | |
| 1270 | #ifdef DEBUG |
| 1271 | error = xfs_btree_check_block(cur, block, level, bp); |
| 1272 | if (error) |
| 1273 | goto error0; |
| 1274 | #endif |
| 1275 | |
| 1276 | /* We're done if we remain in the block after the increment. */ |
| 1277 | if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block)) |
| 1278 | goto out1; |
| 1279 | |
| 1280 | /* Fail if we just went off the right edge of the tree. */ |
| 1281 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB); |
| 1282 | if (xfs_btree_ptr_is_null(cur, &ptr)) |
| 1283 | goto out0; |
| 1284 | |
| 1285 | XFS_BTREE_STATS_INC(cur, increment); |
| 1286 | |
| 1287 | /* |
| 1288 | * March up the tree incrementing pointers. |
| 1289 | * Stop when we don't go off the right edge of a block. |
| 1290 | */ |
| 1291 | for (lev = level + 1; lev < cur->bc_nlevels; lev++) { |
| 1292 | block = xfs_btree_get_block(cur, lev, &bp); |
| 1293 | |
| 1294 | #ifdef DEBUG |
| 1295 | error = xfs_btree_check_block(cur, block, lev, bp); |
| 1296 | if (error) |
| 1297 | goto error0; |
| 1298 | #endif |
| 1299 | |
| 1300 | if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block)) |
| 1301 | break; |
| 1302 | |
| 1303 | /* Read-ahead the right block for the next loop. */ |
| 1304 | xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA); |
| 1305 | } |
| 1306 | |
| 1307 | /* |
| 1308 | * If we went off the root then we are either seriously |
| 1309 | * confused or have the tree root in an inode. |
| 1310 | */ |
| 1311 | if (lev == cur->bc_nlevels) { |
| 1312 | if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) |
| 1313 | goto out0; |
| 1314 | ASSERT(0); |
| 1315 | error = EFSCORRUPTED; |
| 1316 | goto error0; |
| 1317 | } |
| 1318 | ASSERT(lev < cur->bc_nlevels); |
| 1319 | |
| 1320 | /* |
| 1321 | * Now walk back down the tree, fixing up the cursor's buffer |
| 1322 | * pointers and key numbers. |
| 1323 | */ |
| 1324 | for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) { |
| 1325 | union xfs_btree_ptr *ptrp; |
| 1326 | |
| 1327 | ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block); |
| 1328 | error = xfs_btree_read_buf_block(cur, ptrp, --lev, |
| 1329 | 0, &block, &bp); |
| 1330 | if (error) |
| 1331 | goto error0; |
| 1332 | |
| 1333 | xfs_btree_setbuf(cur, lev, bp); |
| 1334 | cur->bc_ptrs[lev] = 1; |
| 1335 | } |
| 1336 | out1: |
| 1337 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1338 | *stat = 1; |
| 1339 | return 0; |
| 1340 | |
| 1341 | out0: |
| 1342 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1343 | *stat = 0; |
| 1344 | return 0; |
| 1345 | |
| 1346 | error0: |
| 1347 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 1348 | return error; |
| 1349 | } |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1350 | |
| 1351 | /* |
| 1352 | * Decrement cursor by one record at the level. |
| 1353 | * For nonzero levels the leaf-ward information is untouched. |
| 1354 | */ |
| 1355 | int /* error */ |
| 1356 | xfs_btree_decrement( |
| 1357 | struct xfs_btree_cur *cur, |
| 1358 | int level, |
| 1359 | int *stat) /* success/failure */ |
| 1360 | { |
| 1361 | struct xfs_btree_block *block; |
| 1362 | xfs_buf_t *bp; |
| 1363 | int error; /* error return value */ |
| 1364 | int lev; |
| 1365 | union xfs_btree_ptr ptr; |
| 1366 | |
| 1367 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1368 | XFS_BTREE_TRACE_ARGI(cur, level); |
| 1369 | |
| 1370 | ASSERT(level < cur->bc_nlevels); |
| 1371 | |
| 1372 | /* Read-ahead to the left at this level. */ |
| 1373 | xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA); |
| 1374 | |
| 1375 | /* We're done if we remain in the block after the decrement. */ |
| 1376 | if (--cur->bc_ptrs[level] > 0) |
| 1377 | goto out1; |
| 1378 | |
| 1379 | /* Get a pointer to the btree block. */ |
| 1380 | block = xfs_btree_get_block(cur, level, &bp); |
| 1381 | |
| 1382 | #ifdef DEBUG |
| 1383 | error = xfs_btree_check_block(cur, block, level, bp); |
| 1384 | if (error) |
| 1385 | goto error0; |
| 1386 | #endif |
| 1387 | |
| 1388 | /* Fail if we just went off the left edge of the tree. */ |
| 1389 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB); |
| 1390 | if (xfs_btree_ptr_is_null(cur, &ptr)) |
| 1391 | goto out0; |
| 1392 | |
| 1393 | XFS_BTREE_STATS_INC(cur, decrement); |
| 1394 | |
| 1395 | /* |
| 1396 | * March up the tree decrementing pointers. |
| 1397 | * Stop when we don't go off the left edge of a block. |
| 1398 | */ |
| 1399 | for (lev = level + 1; lev < cur->bc_nlevels; lev++) { |
| 1400 | if (--cur->bc_ptrs[lev] > 0) |
| 1401 | break; |
| 1402 | /* Read-ahead the left block for the next loop. */ |
| 1403 | xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA); |
| 1404 | } |
| 1405 | |
| 1406 | /* |
| 1407 | * If we went off the root then we are seriously confused. |
| 1408 | * or the root of the tree is in an inode. |
| 1409 | */ |
| 1410 | if (lev == cur->bc_nlevels) { |
| 1411 | if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) |
| 1412 | goto out0; |
| 1413 | ASSERT(0); |
| 1414 | error = EFSCORRUPTED; |
| 1415 | goto error0; |
| 1416 | } |
| 1417 | ASSERT(lev < cur->bc_nlevels); |
| 1418 | |
| 1419 | /* |
| 1420 | * Now walk back down the tree, fixing up the cursor's buffer |
| 1421 | * pointers and key numbers. |
| 1422 | */ |
| 1423 | for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) { |
| 1424 | union xfs_btree_ptr *ptrp; |
| 1425 | |
| 1426 | ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block); |
| 1427 | error = xfs_btree_read_buf_block(cur, ptrp, --lev, |
| 1428 | 0, &block, &bp); |
| 1429 | if (error) |
| 1430 | goto error0; |
| 1431 | xfs_btree_setbuf(cur, lev, bp); |
| 1432 | cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block); |
| 1433 | } |
| 1434 | out1: |
| 1435 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1436 | *stat = 1; |
| 1437 | return 0; |
| 1438 | |
| 1439 | out0: |
| 1440 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1441 | *stat = 0; |
| 1442 | return 0; |
| 1443 | |
| 1444 | error0: |
| 1445 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 1446 | return error; |
| 1447 | } |
| 1448 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 1449 | STATIC int |
| 1450 | xfs_btree_lookup_get_block( |
| 1451 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 1452 | int level, /* level in the btree */ |
| 1453 | union xfs_btree_ptr *pp, /* ptr to btree block */ |
| 1454 | struct xfs_btree_block **blkp) /* return btree block */ |
| 1455 | { |
| 1456 | struct xfs_buf *bp; /* buffer pointer for btree block */ |
| 1457 | int error = 0; |
| 1458 | |
| 1459 | /* special case the root block if in an inode */ |
| 1460 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && |
| 1461 | (level == cur->bc_nlevels - 1)) { |
| 1462 | *blkp = xfs_btree_get_iroot(cur); |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
| 1466 | /* |
| 1467 | * If the old buffer at this level for the disk address we are |
| 1468 | * looking for re-use it. |
| 1469 | * |
| 1470 | * Otherwise throw it away and get a new one. |
| 1471 | */ |
| 1472 | bp = cur->bc_bufs[level]; |
| 1473 | if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) { |
| 1474 | *blkp = XFS_BUF_TO_BLOCK(bp); |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
| 1478 | error = xfs_btree_read_buf_block(cur, pp, level, 0, blkp, &bp); |
| 1479 | if (error) |
| 1480 | return error; |
| 1481 | |
| 1482 | xfs_btree_setbuf(cur, level, bp); |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | /* |
| 1487 | * Get current search key. For level 0 we don't actually have a key |
| 1488 | * structure so we make one up from the record. For all other levels |
| 1489 | * we just return the right key. |
| 1490 | */ |
| 1491 | STATIC union xfs_btree_key * |
| 1492 | xfs_lookup_get_search_key( |
| 1493 | struct xfs_btree_cur *cur, |
| 1494 | int level, |
| 1495 | int keyno, |
| 1496 | struct xfs_btree_block *block, |
| 1497 | union xfs_btree_key *kp) |
| 1498 | { |
| 1499 | if (level == 0) { |
| 1500 | cur->bc_ops->init_key_from_rec(kp, |
| 1501 | xfs_btree_rec_addr(cur, keyno, block)); |
| 1502 | return kp; |
| 1503 | } |
| 1504 | |
| 1505 | return xfs_btree_key_addr(cur, keyno, block); |
| 1506 | } |
| 1507 | |
| 1508 | /* |
| 1509 | * Lookup the record. The cursor is made to point to it, based on dir. |
| 1510 | * Return 0 if can't find any such record, 1 for success. |
| 1511 | */ |
| 1512 | int /* error */ |
| 1513 | xfs_btree_lookup( |
| 1514 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 1515 | xfs_lookup_t dir, /* <=, ==, or >= */ |
| 1516 | int *stat) /* success/failure */ |
| 1517 | { |
| 1518 | struct xfs_btree_block *block; /* current btree block */ |
| 1519 | __int64_t diff; /* difference for the current key */ |
| 1520 | int error; /* error return value */ |
| 1521 | int keyno; /* current key number */ |
| 1522 | int level; /* level in the btree */ |
| 1523 | union xfs_btree_ptr *pp; /* ptr to btree block */ |
| 1524 | union xfs_btree_ptr ptr; /* ptr to btree block */ |
| 1525 | |
| 1526 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1527 | XFS_BTREE_TRACE_ARGI(cur, dir); |
| 1528 | |
| 1529 | XFS_BTREE_STATS_INC(cur, lookup); |
| 1530 | |
| 1531 | block = NULL; |
| 1532 | keyno = 0; |
| 1533 | |
| 1534 | /* initialise start pointer from cursor */ |
| 1535 | cur->bc_ops->init_ptr_from_cur(cur, &ptr); |
| 1536 | pp = &ptr; |
| 1537 | |
| 1538 | /* |
| 1539 | * Iterate over each level in the btree, starting at the root. |
| 1540 | * For each level above the leaves, find the key we need, based |
| 1541 | * on the lookup record, then follow the corresponding block |
| 1542 | * pointer down to the next level. |
| 1543 | */ |
| 1544 | for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) { |
| 1545 | /* Get the block we need to do the lookup on. */ |
| 1546 | error = xfs_btree_lookup_get_block(cur, level, pp, &block); |
| 1547 | if (error) |
| 1548 | goto error0; |
| 1549 | |
| 1550 | if (diff == 0) { |
| 1551 | /* |
| 1552 | * If we already had a key match at a higher level, we |
| 1553 | * know we need to use the first entry in this block. |
| 1554 | */ |
| 1555 | keyno = 1; |
| 1556 | } else { |
| 1557 | /* Otherwise search this block. Do a binary search. */ |
| 1558 | |
| 1559 | int high; /* high entry number */ |
| 1560 | int low; /* low entry number */ |
| 1561 | |
| 1562 | /* Set low and high entry numbers, 1-based. */ |
| 1563 | low = 1; |
| 1564 | high = xfs_btree_get_numrecs(block); |
| 1565 | if (!high) { |
| 1566 | /* Block is empty, must be an empty leaf. */ |
| 1567 | ASSERT(level == 0 && cur->bc_nlevels == 1); |
| 1568 | |
| 1569 | cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE; |
| 1570 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1571 | *stat = 0; |
| 1572 | return 0; |
| 1573 | } |
| 1574 | |
| 1575 | /* Binary search the block. */ |
| 1576 | while (low <= high) { |
| 1577 | union xfs_btree_key key; |
| 1578 | union xfs_btree_key *kp; |
| 1579 | |
| 1580 | XFS_BTREE_STATS_INC(cur, compare); |
| 1581 | |
| 1582 | /* keyno is average of low and high. */ |
| 1583 | keyno = (low + high) >> 1; |
| 1584 | |
| 1585 | /* Get current search key */ |
| 1586 | kp = xfs_lookup_get_search_key(cur, level, |
| 1587 | keyno, block, &key); |
| 1588 | |
| 1589 | /* |
| 1590 | * Compute difference to get next direction: |
| 1591 | * - less than, move right |
| 1592 | * - greater than, move left |
| 1593 | * - equal, we're done |
| 1594 | */ |
| 1595 | diff = cur->bc_ops->key_diff(cur, kp); |
| 1596 | if (diff < 0) |
| 1597 | low = keyno + 1; |
| 1598 | else if (diff > 0) |
| 1599 | high = keyno - 1; |
| 1600 | else |
| 1601 | break; |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | /* |
| 1606 | * If there are more levels, set up for the next level |
| 1607 | * by getting the block number and filling in the cursor. |
| 1608 | */ |
| 1609 | if (level > 0) { |
| 1610 | /* |
| 1611 | * If we moved left, need the previous key number, |
| 1612 | * unless there isn't one. |
| 1613 | */ |
| 1614 | if (diff > 0 && --keyno < 1) |
| 1615 | keyno = 1; |
| 1616 | pp = xfs_btree_ptr_addr(cur, keyno, block); |
| 1617 | |
| 1618 | #ifdef DEBUG |
| 1619 | error = xfs_btree_check_ptr(cur, pp, 0, level); |
| 1620 | if (error) |
| 1621 | goto error0; |
| 1622 | #endif |
| 1623 | cur->bc_ptrs[level] = keyno; |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | /* Done with the search. See if we need to adjust the results. */ |
| 1628 | if (dir != XFS_LOOKUP_LE && diff < 0) { |
| 1629 | keyno++; |
| 1630 | /* |
| 1631 | * If ge search and we went off the end of the block, but it's |
| 1632 | * not the last block, we're in the wrong block. |
| 1633 | */ |
| 1634 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB); |
| 1635 | if (dir == XFS_LOOKUP_GE && |
| 1636 | keyno > xfs_btree_get_numrecs(block) && |
| 1637 | !xfs_btree_ptr_is_null(cur, &ptr)) { |
| 1638 | int i; |
| 1639 | |
| 1640 | cur->bc_ptrs[0] = keyno; |
| 1641 | error = xfs_btree_increment(cur, 0, &i); |
| 1642 | if (error) |
| 1643 | goto error0; |
| 1644 | XFS_WANT_CORRUPTED_RETURN(i == 1); |
| 1645 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1646 | *stat = 1; |
| 1647 | return 0; |
| 1648 | } |
| 1649 | } else if (dir == XFS_LOOKUP_LE && diff > 0) |
| 1650 | keyno--; |
| 1651 | cur->bc_ptrs[0] = keyno; |
| 1652 | |
| 1653 | /* Return if we succeeded or not. */ |
| 1654 | if (keyno == 0 || keyno > xfs_btree_get_numrecs(block)) |
| 1655 | *stat = 0; |
| 1656 | else if (dir != XFS_LOOKUP_EQ || diff == 0) |
| 1657 | *stat = 1; |
| 1658 | else |
| 1659 | *stat = 0; |
| 1660 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1661 | return 0; |
| 1662 | |
| 1663 | error0: |
| 1664 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 1665 | return error; |
| 1666 | } |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 1667 | |
| 1668 | /* |
| 1669 | * Update keys at all levels from here to the root along the cursor's path. |
| 1670 | */ |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 1671 | STATIC int |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 1672 | xfs_btree_updkey( |
| 1673 | struct xfs_btree_cur *cur, |
| 1674 | union xfs_btree_key *keyp, |
| 1675 | int level) |
| 1676 | { |
| 1677 | struct xfs_btree_block *block; |
| 1678 | struct xfs_buf *bp; |
| 1679 | union xfs_btree_key *kp; |
| 1680 | int ptr; |
| 1681 | |
| 1682 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1683 | XFS_BTREE_TRACE_ARGIK(cur, level, keyp); |
| 1684 | |
| 1685 | ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1); |
| 1686 | |
| 1687 | /* |
| 1688 | * Go up the tree from this level toward the root. |
| 1689 | * At each level, update the key value to the value input. |
| 1690 | * Stop when we reach a level where the cursor isn't pointing |
| 1691 | * at the first entry in the block. |
| 1692 | */ |
| 1693 | for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) { |
| 1694 | #ifdef DEBUG |
| 1695 | int error; |
| 1696 | #endif |
| 1697 | block = xfs_btree_get_block(cur, level, &bp); |
| 1698 | #ifdef DEBUG |
| 1699 | error = xfs_btree_check_block(cur, block, level, bp); |
| 1700 | if (error) { |
| 1701 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 1702 | return error; |
| 1703 | } |
| 1704 | #endif |
| 1705 | ptr = cur->bc_ptrs[level]; |
| 1706 | kp = xfs_btree_key_addr(cur, ptr, block); |
| 1707 | xfs_btree_copy_keys(cur, kp, keyp, 1); |
| 1708 | xfs_btree_log_keys(cur, bp, ptr, ptr); |
| 1709 | } |
| 1710 | |
| 1711 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1712 | return 0; |
| 1713 | } |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 1714 | |
| 1715 | /* |
| 1716 | * Update the record referred to by cur to the value in the |
| 1717 | * given record. This either works (return 0) or gets an |
| 1718 | * EFSCORRUPTED error. |
| 1719 | */ |
| 1720 | int |
| 1721 | xfs_btree_update( |
| 1722 | struct xfs_btree_cur *cur, |
| 1723 | union xfs_btree_rec *rec) |
| 1724 | { |
| 1725 | struct xfs_btree_block *block; |
| 1726 | struct xfs_buf *bp; |
| 1727 | int error; |
| 1728 | int ptr; |
| 1729 | union xfs_btree_rec *rp; |
| 1730 | |
| 1731 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1732 | XFS_BTREE_TRACE_ARGR(cur, rec); |
| 1733 | |
| 1734 | /* Pick up the current block. */ |
| 1735 | block = xfs_btree_get_block(cur, 0, &bp); |
| 1736 | |
| 1737 | #ifdef DEBUG |
| 1738 | error = xfs_btree_check_block(cur, block, 0, bp); |
| 1739 | if (error) |
| 1740 | goto error0; |
| 1741 | #endif |
| 1742 | /* Get the address of the rec to be updated. */ |
| 1743 | ptr = cur->bc_ptrs[0]; |
| 1744 | rp = xfs_btree_rec_addr(cur, ptr, block); |
| 1745 | |
| 1746 | /* Fill in the new contents and log them. */ |
| 1747 | xfs_btree_copy_recs(cur, rp, rec, 1); |
| 1748 | xfs_btree_log_recs(cur, bp, ptr, ptr); |
| 1749 | |
| 1750 | /* |
| 1751 | * If we are tracking the last record in the tree and |
| 1752 | * we are at the far right edge of the tree, update it. |
| 1753 | */ |
| 1754 | if (xfs_btree_is_lastrec(cur, block, 0)) { |
| 1755 | cur->bc_ops->update_lastrec(cur, block, rec, |
| 1756 | ptr, LASTREC_UPDATE); |
| 1757 | } |
| 1758 | |
| 1759 | /* Updating first rec in leaf. Pass new key value up to our parent. */ |
| 1760 | if (ptr == 1) { |
| 1761 | union xfs_btree_key key; |
| 1762 | |
| 1763 | cur->bc_ops->init_key_from_rec(&key, rec); |
| 1764 | error = xfs_btree_updkey(cur, &key, 1); |
| 1765 | if (error) |
| 1766 | goto error0; |
| 1767 | } |
| 1768 | |
| 1769 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1770 | return 0; |
| 1771 | |
| 1772 | error0: |
| 1773 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 1774 | return error; |
| 1775 | } |
| 1776 | |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 1777 | /* |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 1778 | * Move 1 record left from cur/level if possible. |
| 1779 | * Update cur to reflect the new path. |
| 1780 | */ |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 1781 | STATIC int /* error */ |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 1782 | xfs_btree_lshift( |
| 1783 | struct xfs_btree_cur *cur, |
| 1784 | int level, |
| 1785 | int *stat) /* success/failure */ |
| 1786 | { |
| 1787 | union xfs_btree_key key; /* btree key */ |
| 1788 | struct xfs_buf *lbp; /* left buffer pointer */ |
| 1789 | struct xfs_btree_block *left; /* left btree block */ |
| 1790 | int lrecs; /* left record count */ |
| 1791 | struct xfs_buf *rbp; /* right buffer pointer */ |
| 1792 | struct xfs_btree_block *right; /* right btree block */ |
| 1793 | int rrecs; /* right record count */ |
| 1794 | union xfs_btree_ptr lptr; /* left btree pointer */ |
| 1795 | union xfs_btree_key *rkp = NULL; /* right btree key */ |
| 1796 | union xfs_btree_ptr *rpp = NULL; /* right address pointer */ |
| 1797 | union xfs_btree_rec *rrp = NULL; /* right record pointer */ |
| 1798 | int error; /* error return value */ |
| 1799 | |
| 1800 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1801 | XFS_BTREE_TRACE_ARGI(cur, level); |
| 1802 | |
| 1803 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && |
| 1804 | level == cur->bc_nlevels - 1) |
| 1805 | goto out0; |
| 1806 | |
| 1807 | /* Set up variables for this block as "right". */ |
| 1808 | right = xfs_btree_get_block(cur, level, &rbp); |
| 1809 | |
| 1810 | #ifdef DEBUG |
| 1811 | error = xfs_btree_check_block(cur, right, level, rbp); |
| 1812 | if (error) |
| 1813 | goto error0; |
| 1814 | #endif |
| 1815 | |
| 1816 | /* If we've got no left sibling then we can't shift an entry left. */ |
| 1817 | xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB); |
| 1818 | if (xfs_btree_ptr_is_null(cur, &lptr)) |
| 1819 | goto out0; |
| 1820 | |
| 1821 | /* |
| 1822 | * If the cursor entry is the one that would be moved, don't |
| 1823 | * do it... it's too complicated. |
| 1824 | */ |
| 1825 | if (cur->bc_ptrs[level] <= 1) |
| 1826 | goto out0; |
| 1827 | |
| 1828 | /* Set up the left neighbor as "left". */ |
| 1829 | error = xfs_btree_read_buf_block(cur, &lptr, level, 0, &left, &lbp); |
| 1830 | if (error) |
| 1831 | goto error0; |
| 1832 | |
| 1833 | /* If it's full, it can't take another entry. */ |
| 1834 | lrecs = xfs_btree_get_numrecs(left); |
| 1835 | if (lrecs == cur->bc_ops->get_maxrecs(cur, level)) |
| 1836 | goto out0; |
| 1837 | |
| 1838 | rrecs = xfs_btree_get_numrecs(right); |
| 1839 | |
| 1840 | /* |
| 1841 | * We add one entry to the left side and remove one for the right side. |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 1842 | * Account for it here, the changes will be updated on disk and logged |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 1843 | * later. |
| 1844 | */ |
| 1845 | lrecs++; |
| 1846 | rrecs--; |
| 1847 | |
| 1848 | XFS_BTREE_STATS_INC(cur, lshift); |
| 1849 | XFS_BTREE_STATS_ADD(cur, moves, 1); |
| 1850 | |
| 1851 | /* |
| 1852 | * If non-leaf, copy a key and a ptr to the left block. |
| 1853 | * Log the changes to the left block. |
| 1854 | */ |
| 1855 | if (level > 0) { |
| 1856 | /* It's a non-leaf. Move keys and pointers. */ |
| 1857 | union xfs_btree_key *lkp; /* left btree key */ |
| 1858 | union xfs_btree_ptr *lpp; /* left address pointer */ |
| 1859 | |
| 1860 | lkp = xfs_btree_key_addr(cur, lrecs, left); |
| 1861 | rkp = xfs_btree_key_addr(cur, 1, right); |
| 1862 | |
| 1863 | lpp = xfs_btree_ptr_addr(cur, lrecs, left); |
| 1864 | rpp = xfs_btree_ptr_addr(cur, 1, right); |
| 1865 | #ifdef DEBUG |
| 1866 | error = xfs_btree_check_ptr(cur, rpp, 0, level); |
| 1867 | if (error) |
| 1868 | goto error0; |
| 1869 | #endif |
| 1870 | xfs_btree_copy_keys(cur, lkp, rkp, 1); |
| 1871 | xfs_btree_copy_ptrs(cur, lpp, rpp, 1); |
| 1872 | |
| 1873 | xfs_btree_log_keys(cur, lbp, lrecs, lrecs); |
| 1874 | xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs); |
| 1875 | |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 1876 | ASSERT(cur->bc_ops->keys_inorder(cur, |
| 1877 | xfs_btree_key_addr(cur, lrecs - 1, left), lkp)); |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 1878 | } else { |
| 1879 | /* It's a leaf. Move records. */ |
| 1880 | union xfs_btree_rec *lrp; /* left record pointer */ |
| 1881 | |
| 1882 | lrp = xfs_btree_rec_addr(cur, lrecs, left); |
| 1883 | rrp = xfs_btree_rec_addr(cur, 1, right); |
| 1884 | |
| 1885 | xfs_btree_copy_recs(cur, lrp, rrp, 1); |
| 1886 | xfs_btree_log_recs(cur, lbp, lrecs, lrecs); |
| 1887 | |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 1888 | ASSERT(cur->bc_ops->recs_inorder(cur, |
| 1889 | xfs_btree_rec_addr(cur, lrecs - 1, left), lrp)); |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | xfs_btree_set_numrecs(left, lrecs); |
| 1893 | xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS); |
| 1894 | |
| 1895 | xfs_btree_set_numrecs(right, rrecs); |
| 1896 | xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS); |
| 1897 | |
| 1898 | /* |
| 1899 | * Slide the contents of right down one entry. |
| 1900 | */ |
| 1901 | XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1); |
| 1902 | if (level > 0) { |
| 1903 | /* It's a nonleaf. operate on keys and ptrs */ |
| 1904 | #ifdef DEBUG |
| 1905 | int i; /* loop index */ |
| 1906 | |
| 1907 | for (i = 0; i < rrecs; i++) { |
| 1908 | error = xfs_btree_check_ptr(cur, rpp, i + 1, level); |
| 1909 | if (error) |
| 1910 | goto error0; |
| 1911 | } |
| 1912 | #endif |
| 1913 | xfs_btree_shift_keys(cur, |
| 1914 | xfs_btree_key_addr(cur, 2, right), |
| 1915 | -1, rrecs); |
| 1916 | xfs_btree_shift_ptrs(cur, |
| 1917 | xfs_btree_ptr_addr(cur, 2, right), |
| 1918 | -1, rrecs); |
| 1919 | |
| 1920 | xfs_btree_log_keys(cur, rbp, 1, rrecs); |
| 1921 | xfs_btree_log_ptrs(cur, rbp, 1, rrecs); |
| 1922 | } else { |
| 1923 | /* It's a leaf. operate on records */ |
| 1924 | xfs_btree_shift_recs(cur, |
| 1925 | xfs_btree_rec_addr(cur, 2, right), |
| 1926 | -1, rrecs); |
| 1927 | xfs_btree_log_recs(cur, rbp, 1, rrecs); |
| 1928 | |
| 1929 | /* |
| 1930 | * If it's the first record in the block, we'll need a key |
| 1931 | * structure to pass up to the next level (updkey). |
| 1932 | */ |
| 1933 | cur->bc_ops->init_key_from_rec(&key, |
| 1934 | xfs_btree_rec_addr(cur, 1, right)); |
| 1935 | rkp = &key; |
| 1936 | } |
| 1937 | |
| 1938 | /* Update the parent key values of right. */ |
| 1939 | error = xfs_btree_updkey(cur, rkp, level + 1); |
| 1940 | if (error) |
| 1941 | goto error0; |
| 1942 | |
| 1943 | /* Slide the cursor value left one. */ |
| 1944 | cur->bc_ptrs[level]--; |
| 1945 | |
| 1946 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1947 | *stat = 1; |
| 1948 | return 0; |
| 1949 | |
| 1950 | out0: |
| 1951 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1952 | *stat = 0; |
| 1953 | return 0; |
| 1954 | |
| 1955 | error0: |
| 1956 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 1957 | return error; |
| 1958 | } |
| 1959 | |
| 1960 | /* |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 1961 | * Move 1 record right from cur/level if possible. |
| 1962 | * Update cur to reflect the new path. |
| 1963 | */ |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 1964 | STATIC int /* error */ |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 1965 | xfs_btree_rshift( |
| 1966 | struct xfs_btree_cur *cur, |
| 1967 | int level, |
| 1968 | int *stat) /* success/failure */ |
| 1969 | { |
| 1970 | union xfs_btree_key key; /* btree key */ |
| 1971 | struct xfs_buf *lbp; /* left buffer pointer */ |
| 1972 | struct xfs_btree_block *left; /* left btree block */ |
| 1973 | struct xfs_buf *rbp; /* right buffer pointer */ |
| 1974 | struct xfs_btree_block *right; /* right btree block */ |
| 1975 | struct xfs_btree_cur *tcur; /* temporary btree cursor */ |
| 1976 | union xfs_btree_ptr rptr; /* right block pointer */ |
| 1977 | union xfs_btree_key *rkp; /* right btree key */ |
| 1978 | int rrecs; /* right record count */ |
| 1979 | int lrecs; /* left record count */ |
| 1980 | int error; /* error return value */ |
| 1981 | int i; /* loop counter */ |
| 1982 | |
| 1983 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1984 | XFS_BTREE_TRACE_ARGI(cur, level); |
| 1985 | |
| 1986 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && |
| 1987 | (level == cur->bc_nlevels - 1)) |
| 1988 | goto out0; |
| 1989 | |
| 1990 | /* Set up variables for this block as "left". */ |
| 1991 | left = xfs_btree_get_block(cur, level, &lbp); |
| 1992 | |
| 1993 | #ifdef DEBUG |
| 1994 | error = xfs_btree_check_block(cur, left, level, lbp); |
| 1995 | if (error) |
| 1996 | goto error0; |
| 1997 | #endif |
| 1998 | |
| 1999 | /* If we've got no right sibling then we can't shift an entry right. */ |
| 2000 | xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB); |
| 2001 | if (xfs_btree_ptr_is_null(cur, &rptr)) |
| 2002 | goto out0; |
| 2003 | |
| 2004 | /* |
| 2005 | * If the cursor entry is the one that would be moved, don't |
| 2006 | * do it... it's too complicated. |
| 2007 | */ |
| 2008 | lrecs = xfs_btree_get_numrecs(left); |
| 2009 | if (cur->bc_ptrs[level] >= lrecs) |
| 2010 | goto out0; |
| 2011 | |
| 2012 | /* Set up the right neighbor as "right". */ |
| 2013 | error = xfs_btree_read_buf_block(cur, &rptr, level, 0, &right, &rbp); |
| 2014 | if (error) |
| 2015 | goto error0; |
| 2016 | |
| 2017 | /* If it's full, it can't take another entry. */ |
| 2018 | rrecs = xfs_btree_get_numrecs(right); |
| 2019 | if (rrecs == cur->bc_ops->get_maxrecs(cur, level)) |
| 2020 | goto out0; |
| 2021 | |
| 2022 | XFS_BTREE_STATS_INC(cur, rshift); |
| 2023 | XFS_BTREE_STATS_ADD(cur, moves, rrecs); |
| 2024 | |
| 2025 | /* |
| 2026 | * Make a hole at the start of the right neighbor block, then |
| 2027 | * copy the last left block entry to the hole. |
| 2028 | */ |
| 2029 | if (level > 0) { |
| 2030 | /* It's a nonleaf. make a hole in the keys and ptrs */ |
| 2031 | union xfs_btree_key *lkp; |
| 2032 | union xfs_btree_ptr *lpp; |
| 2033 | union xfs_btree_ptr *rpp; |
| 2034 | |
| 2035 | lkp = xfs_btree_key_addr(cur, lrecs, left); |
| 2036 | lpp = xfs_btree_ptr_addr(cur, lrecs, left); |
| 2037 | rkp = xfs_btree_key_addr(cur, 1, right); |
| 2038 | rpp = xfs_btree_ptr_addr(cur, 1, right); |
| 2039 | |
| 2040 | #ifdef DEBUG |
| 2041 | for (i = rrecs - 1; i >= 0; i--) { |
| 2042 | error = xfs_btree_check_ptr(cur, rpp, i, level); |
| 2043 | if (error) |
| 2044 | goto error0; |
| 2045 | } |
| 2046 | #endif |
| 2047 | |
| 2048 | xfs_btree_shift_keys(cur, rkp, 1, rrecs); |
| 2049 | xfs_btree_shift_ptrs(cur, rpp, 1, rrecs); |
| 2050 | |
| 2051 | #ifdef DEBUG |
| 2052 | error = xfs_btree_check_ptr(cur, lpp, 0, level); |
| 2053 | if (error) |
| 2054 | goto error0; |
| 2055 | #endif |
| 2056 | |
| 2057 | /* Now put the new data in, and log it. */ |
| 2058 | xfs_btree_copy_keys(cur, rkp, lkp, 1); |
| 2059 | xfs_btree_copy_ptrs(cur, rpp, lpp, 1); |
| 2060 | |
| 2061 | xfs_btree_log_keys(cur, rbp, 1, rrecs + 1); |
| 2062 | xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1); |
| 2063 | |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 2064 | ASSERT(cur->bc_ops->keys_inorder(cur, rkp, |
| 2065 | xfs_btree_key_addr(cur, 2, right))); |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 2066 | } else { |
| 2067 | /* It's a leaf. make a hole in the records */ |
| 2068 | union xfs_btree_rec *lrp; |
| 2069 | union xfs_btree_rec *rrp; |
| 2070 | |
| 2071 | lrp = xfs_btree_rec_addr(cur, lrecs, left); |
| 2072 | rrp = xfs_btree_rec_addr(cur, 1, right); |
| 2073 | |
| 2074 | xfs_btree_shift_recs(cur, rrp, 1, rrecs); |
| 2075 | |
| 2076 | /* Now put the new data in, and log it. */ |
| 2077 | xfs_btree_copy_recs(cur, rrp, lrp, 1); |
| 2078 | xfs_btree_log_recs(cur, rbp, 1, rrecs + 1); |
| 2079 | |
| 2080 | cur->bc_ops->init_key_from_rec(&key, rrp); |
| 2081 | rkp = &key; |
| 2082 | |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 2083 | ASSERT(cur->bc_ops->recs_inorder(cur, rrp, |
| 2084 | xfs_btree_rec_addr(cur, 2, right))); |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | /* |
| 2088 | * Decrement and log left's numrecs, bump and log right's numrecs. |
| 2089 | */ |
| 2090 | xfs_btree_set_numrecs(left, --lrecs); |
| 2091 | xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS); |
| 2092 | |
| 2093 | xfs_btree_set_numrecs(right, ++rrecs); |
| 2094 | xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS); |
| 2095 | |
| 2096 | /* |
| 2097 | * Using a temporary cursor, update the parent key values of the |
| 2098 | * block on the right. |
| 2099 | */ |
| 2100 | error = xfs_btree_dup_cursor(cur, &tcur); |
| 2101 | if (error) |
| 2102 | goto error0; |
| 2103 | i = xfs_btree_lastrec(tcur, level); |
| 2104 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 2105 | |
| 2106 | error = xfs_btree_increment(tcur, level, &i); |
| 2107 | if (error) |
| 2108 | goto error1; |
| 2109 | |
| 2110 | error = xfs_btree_updkey(tcur, rkp, level + 1); |
| 2111 | if (error) |
| 2112 | goto error1; |
| 2113 | |
| 2114 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
| 2115 | |
| 2116 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2117 | *stat = 1; |
| 2118 | return 0; |
| 2119 | |
| 2120 | out0: |
| 2121 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2122 | *stat = 0; |
| 2123 | return 0; |
| 2124 | |
| 2125 | error0: |
| 2126 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 2127 | return error; |
| 2128 | |
| 2129 | error1: |
| 2130 | XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR); |
| 2131 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); |
| 2132 | return error; |
| 2133 | } |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 2134 | |
| 2135 | /* |
| 2136 | * Split cur/level block in half. |
| 2137 | * Return new block number and the key to its first |
| 2138 | * record (to be inserted into parent). |
| 2139 | */ |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 2140 | STATIC int /* error */ |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 2141 | xfs_btree_split( |
| 2142 | struct xfs_btree_cur *cur, |
| 2143 | int level, |
| 2144 | union xfs_btree_ptr *ptrp, |
| 2145 | union xfs_btree_key *key, |
| 2146 | struct xfs_btree_cur **curp, |
| 2147 | int *stat) /* success/failure */ |
| 2148 | { |
| 2149 | union xfs_btree_ptr lptr; /* left sibling block ptr */ |
| 2150 | struct xfs_buf *lbp; /* left buffer pointer */ |
| 2151 | struct xfs_btree_block *left; /* left btree block */ |
| 2152 | union xfs_btree_ptr rptr; /* right sibling block ptr */ |
| 2153 | struct xfs_buf *rbp; /* right buffer pointer */ |
| 2154 | struct xfs_btree_block *right; /* right btree block */ |
| 2155 | union xfs_btree_ptr rrptr; /* right-right sibling ptr */ |
| 2156 | struct xfs_buf *rrbp; /* right-right buffer pointer */ |
| 2157 | struct xfs_btree_block *rrblock; /* right-right btree block */ |
| 2158 | int lrecs; |
| 2159 | int rrecs; |
| 2160 | int src_index; |
| 2161 | int error; /* error return value */ |
| 2162 | #ifdef DEBUG |
| 2163 | int i; |
| 2164 | #endif |
| 2165 | |
| 2166 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 2167 | XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key); |
| 2168 | |
| 2169 | XFS_BTREE_STATS_INC(cur, split); |
| 2170 | |
| 2171 | /* Set up left block (current one). */ |
| 2172 | left = xfs_btree_get_block(cur, level, &lbp); |
| 2173 | |
| 2174 | #ifdef DEBUG |
| 2175 | error = xfs_btree_check_block(cur, left, level, lbp); |
| 2176 | if (error) |
| 2177 | goto error0; |
| 2178 | #endif |
| 2179 | |
| 2180 | xfs_btree_buf_to_ptr(cur, lbp, &lptr); |
| 2181 | |
| 2182 | /* Allocate the new block. If we can't do it, we're toast. Give up. */ |
| 2183 | error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, 1, stat); |
| 2184 | if (error) |
| 2185 | goto error0; |
| 2186 | if (*stat == 0) |
| 2187 | goto out0; |
| 2188 | XFS_BTREE_STATS_INC(cur, alloc); |
| 2189 | |
| 2190 | /* Set up the new block as "right". */ |
| 2191 | error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp); |
| 2192 | if (error) |
| 2193 | goto error0; |
| 2194 | |
| 2195 | /* Fill in the btree header for the new right block. */ |
| 2196 | xfs_btree_init_block(cur, xfs_btree_get_level(left), 0, right); |
| 2197 | |
| 2198 | /* |
| 2199 | * Split the entries between the old and the new block evenly. |
| 2200 | * Make sure that if there's an odd number of entries now, that |
| 2201 | * each new block will have the same number of entries. |
| 2202 | */ |
| 2203 | lrecs = xfs_btree_get_numrecs(left); |
| 2204 | rrecs = lrecs / 2; |
| 2205 | if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1) |
| 2206 | rrecs++; |
| 2207 | src_index = (lrecs - rrecs + 1); |
| 2208 | |
| 2209 | XFS_BTREE_STATS_ADD(cur, moves, rrecs); |
| 2210 | |
| 2211 | /* |
| 2212 | * Copy btree block entries from the left block over to the |
| 2213 | * new block, the right. Update the right block and log the |
| 2214 | * changes. |
| 2215 | */ |
| 2216 | if (level > 0) { |
| 2217 | /* It's a non-leaf. Move keys and pointers. */ |
| 2218 | union xfs_btree_key *lkp; /* left btree key */ |
| 2219 | union xfs_btree_ptr *lpp; /* left address pointer */ |
| 2220 | union xfs_btree_key *rkp; /* right btree key */ |
| 2221 | union xfs_btree_ptr *rpp; /* right address pointer */ |
| 2222 | |
| 2223 | lkp = xfs_btree_key_addr(cur, src_index, left); |
| 2224 | lpp = xfs_btree_ptr_addr(cur, src_index, left); |
| 2225 | rkp = xfs_btree_key_addr(cur, 1, right); |
| 2226 | rpp = xfs_btree_ptr_addr(cur, 1, right); |
| 2227 | |
| 2228 | #ifdef DEBUG |
| 2229 | for (i = src_index; i < rrecs; i++) { |
| 2230 | error = xfs_btree_check_ptr(cur, lpp, i, level); |
| 2231 | if (error) |
| 2232 | goto error0; |
| 2233 | } |
| 2234 | #endif |
| 2235 | |
| 2236 | xfs_btree_copy_keys(cur, rkp, lkp, rrecs); |
| 2237 | xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs); |
| 2238 | |
| 2239 | xfs_btree_log_keys(cur, rbp, 1, rrecs); |
| 2240 | xfs_btree_log_ptrs(cur, rbp, 1, rrecs); |
| 2241 | |
| 2242 | /* Grab the keys to the entries moved to the right block */ |
| 2243 | xfs_btree_copy_keys(cur, key, rkp, 1); |
| 2244 | } else { |
| 2245 | /* It's a leaf. Move records. */ |
| 2246 | union xfs_btree_rec *lrp; /* left record pointer */ |
| 2247 | union xfs_btree_rec *rrp; /* right record pointer */ |
| 2248 | |
| 2249 | lrp = xfs_btree_rec_addr(cur, src_index, left); |
| 2250 | rrp = xfs_btree_rec_addr(cur, 1, right); |
| 2251 | |
| 2252 | xfs_btree_copy_recs(cur, rrp, lrp, rrecs); |
| 2253 | xfs_btree_log_recs(cur, rbp, 1, rrecs); |
| 2254 | |
| 2255 | cur->bc_ops->init_key_from_rec(key, |
| 2256 | xfs_btree_rec_addr(cur, 1, right)); |
| 2257 | } |
| 2258 | |
| 2259 | |
| 2260 | /* |
| 2261 | * Find the left block number by looking in the buffer. |
| 2262 | * Adjust numrecs, sibling pointers. |
| 2263 | */ |
| 2264 | xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB); |
| 2265 | xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB); |
| 2266 | xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB); |
| 2267 | xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB); |
| 2268 | |
| 2269 | lrecs -= rrecs; |
| 2270 | xfs_btree_set_numrecs(left, lrecs); |
| 2271 | xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs); |
| 2272 | |
| 2273 | xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS); |
| 2274 | xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB); |
| 2275 | |
| 2276 | /* |
| 2277 | * If there's a block to the new block's right, make that block |
| 2278 | * point back to right instead of to left. |
| 2279 | */ |
| 2280 | if (!xfs_btree_ptr_is_null(cur, &rrptr)) { |
| 2281 | error = xfs_btree_read_buf_block(cur, &rrptr, level, |
| 2282 | 0, &rrblock, &rrbp); |
| 2283 | if (error) |
| 2284 | goto error0; |
| 2285 | xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB); |
| 2286 | xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB); |
| 2287 | } |
| 2288 | /* |
| 2289 | * If the cursor is really in the right block, move it there. |
| 2290 | * If it's just pointing past the last entry in left, then we'll |
| 2291 | * insert there, so don't change anything in that case. |
| 2292 | */ |
| 2293 | if (cur->bc_ptrs[level] > lrecs + 1) { |
| 2294 | xfs_btree_setbuf(cur, level, rbp); |
| 2295 | cur->bc_ptrs[level] -= lrecs; |
| 2296 | } |
| 2297 | /* |
| 2298 | * If there are more levels, we'll need another cursor which refers |
| 2299 | * the right block, no matter where this cursor was. |
| 2300 | */ |
| 2301 | if (level + 1 < cur->bc_nlevels) { |
| 2302 | error = xfs_btree_dup_cursor(cur, curp); |
| 2303 | if (error) |
| 2304 | goto error0; |
| 2305 | (*curp)->bc_ptrs[level + 1]++; |
| 2306 | } |
| 2307 | *ptrp = rptr; |
| 2308 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2309 | *stat = 1; |
| 2310 | return 0; |
| 2311 | out0: |
| 2312 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2313 | *stat = 0; |
| 2314 | return 0; |
| 2315 | |
| 2316 | error0: |
| 2317 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 2318 | return error; |
| 2319 | } |
Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 2320 | |
| 2321 | /* |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 2322 | * Copy the old inode root contents into a real block and make the |
| 2323 | * broot point to it. |
| 2324 | */ |
| 2325 | int /* error */ |
| 2326 | xfs_btree_new_iroot( |
| 2327 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 2328 | int *logflags, /* logging flags for inode */ |
| 2329 | int *stat) /* return status - 0 fail */ |
| 2330 | { |
| 2331 | struct xfs_buf *cbp; /* buffer for cblock */ |
| 2332 | struct xfs_btree_block *block; /* btree block */ |
| 2333 | struct xfs_btree_block *cblock; /* child btree block */ |
| 2334 | union xfs_btree_key *ckp; /* child key pointer */ |
| 2335 | union xfs_btree_ptr *cpp; /* child ptr pointer */ |
| 2336 | union xfs_btree_key *kp; /* pointer to btree key */ |
| 2337 | union xfs_btree_ptr *pp; /* pointer to block addr */ |
| 2338 | union xfs_btree_ptr nptr; /* new block addr */ |
| 2339 | int level; /* btree level */ |
| 2340 | int error; /* error return code */ |
| 2341 | #ifdef DEBUG |
| 2342 | int i; /* loop counter */ |
| 2343 | #endif |
| 2344 | |
| 2345 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 2346 | XFS_BTREE_STATS_INC(cur, newroot); |
| 2347 | |
| 2348 | ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE); |
| 2349 | |
| 2350 | level = cur->bc_nlevels - 1; |
| 2351 | |
| 2352 | block = xfs_btree_get_iroot(cur); |
| 2353 | pp = xfs_btree_ptr_addr(cur, 1, block); |
| 2354 | |
| 2355 | /* Allocate the new block. If we can't do it, we're toast. Give up. */ |
| 2356 | error = cur->bc_ops->alloc_block(cur, pp, &nptr, 1, stat); |
| 2357 | if (error) |
| 2358 | goto error0; |
| 2359 | if (*stat == 0) { |
| 2360 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2361 | return 0; |
| 2362 | } |
| 2363 | XFS_BTREE_STATS_INC(cur, alloc); |
| 2364 | |
| 2365 | /* Copy the root into a real block. */ |
| 2366 | error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp); |
| 2367 | if (error) |
| 2368 | goto error0; |
| 2369 | |
| 2370 | memcpy(cblock, block, xfs_btree_block_len(cur)); |
| 2371 | |
| 2372 | be16_add_cpu(&block->bb_level, 1); |
| 2373 | xfs_btree_set_numrecs(block, 1); |
| 2374 | cur->bc_nlevels++; |
| 2375 | cur->bc_ptrs[level + 1] = 1; |
| 2376 | |
| 2377 | kp = xfs_btree_key_addr(cur, 1, block); |
| 2378 | ckp = xfs_btree_key_addr(cur, 1, cblock); |
| 2379 | xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock)); |
| 2380 | |
| 2381 | cpp = xfs_btree_ptr_addr(cur, 1, cblock); |
| 2382 | #ifdef DEBUG |
| 2383 | for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) { |
| 2384 | error = xfs_btree_check_ptr(cur, pp, i, level); |
| 2385 | if (error) |
| 2386 | goto error0; |
| 2387 | } |
| 2388 | #endif |
| 2389 | xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock)); |
| 2390 | |
| 2391 | #ifdef DEBUG |
| 2392 | error = xfs_btree_check_ptr(cur, &nptr, 0, level); |
| 2393 | if (error) |
| 2394 | goto error0; |
| 2395 | #endif |
| 2396 | xfs_btree_copy_ptrs(cur, pp, &nptr, 1); |
| 2397 | |
| 2398 | xfs_iroot_realloc(cur->bc_private.b.ip, |
| 2399 | 1 - xfs_btree_get_numrecs(cblock), |
| 2400 | cur->bc_private.b.whichfork); |
| 2401 | |
| 2402 | xfs_btree_setbuf(cur, level, cbp); |
| 2403 | |
| 2404 | /* |
| 2405 | * Do all this logging at the end so that |
| 2406 | * the root is at the right level. |
| 2407 | */ |
| 2408 | xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS); |
| 2409 | xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs)); |
| 2410 | xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs)); |
| 2411 | |
| 2412 | *logflags |= |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2413 | XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 2414 | *stat = 1; |
| 2415 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2416 | return 0; |
| 2417 | error0: |
| 2418 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 2419 | return error; |
| 2420 | } |
| 2421 | |
| 2422 | /* |
Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 2423 | * Allocate a new root block, fill it in. |
| 2424 | */ |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 2425 | STATIC int /* error */ |
Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 2426 | xfs_btree_new_root( |
| 2427 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 2428 | int *stat) /* success/failure */ |
| 2429 | { |
| 2430 | struct xfs_btree_block *block; /* one half of the old root block */ |
| 2431 | struct xfs_buf *bp; /* buffer containing block */ |
| 2432 | int error; /* error return value */ |
| 2433 | struct xfs_buf *lbp; /* left buffer pointer */ |
| 2434 | struct xfs_btree_block *left; /* left btree block */ |
| 2435 | struct xfs_buf *nbp; /* new (root) buffer */ |
| 2436 | struct xfs_btree_block *new; /* new (root) btree block */ |
| 2437 | int nptr; /* new value for key index, 1 or 2 */ |
| 2438 | struct xfs_buf *rbp; /* right buffer pointer */ |
| 2439 | struct xfs_btree_block *right; /* right btree block */ |
| 2440 | union xfs_btree_ptr rptr; |
| 2441 | union xfs_btree_ptr lptr; |
| 2442 | |
| 2443 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 2444 | XFS_BTREE_STATS_INC(cur, newroot); |
| 2445 | |
| 2446 | /* initialise our start point from the cursor */ |
| 2447 | cur->bc_ops->init_ptr_from_cur(cur, &rptr); |
| 2448 | |
| 2449 | /* Allocate the new block. If we can't do it, we're toast. Give up. */ |
| 2450 | error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, 1, stat); |
| 2451 | if (error) |
| 2452 | goto error0; |
| 2453 | if (*stat == 0) |
| 2454 | goto out0; |
| 2455 | XFS_BTREE_STATS_INC(cur, alloc); |
| 2456 | |
| 2457 | /* Set up the new block. */ |
| 2458 | error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp); |
| 2459 | if (error) |
| 2460 | goto error0; |
| 2461 | |
| 2462 | /* Set the root in the holding structure increasing the level by 1. */ |
| 2463 | cur->bc_ops->set_root(cur, &lptr, 1); |
| 2464 | |
| 2465 | /* |
| 2466 | * At the previous root level there are now two blocks: the old root, |
| 2467 | * and the new block generated when it was split. We don't know which |
| 2468 | * one the cursor is pointing at, so we set up variables "left" and |
| 2469 | * "right" for each case. |
| 2470 | */ |
| 2471 | block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp); |
| 2472 | |
| 2473 | #ifdef DEBUG |
| 2474 | error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp); |
| 2475 | if (error) |
| 2476 | goto error0; |
| 2477 | #endif |
| 2478 | |
| 2479 | xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB); |
| 2480 | if (!xfs_btree_ptr_is_null(cur, &rptr)) { |
| 2481 | /* Our block is left, pick up the right block. */ |
| 2482 | lbp = bp; |
| 2483 | xfs_btree_buf_to_ptr(cur, lbp, &lptr); |
| 2484 | left = block; |
| 2485 | error = xfs_btree_read_buf_block(cur, &rptr, |
| 2486 | cur->bc_nlevels - 1, 0, &right, &rbp); |
| 2487 | if (error) |
| 2488 | goto error0; |
| 2489 | bp = rbp; |
| 2490 | nptr = 1; |
| 2491 | } else { |
| 2492 | /* Our block is right, pick up the left block. */ |
| 2493 | rbp = bp; |
| 2494 | xfs_btree_buf_to_ptr(cur, rbp, &rptr); |
| 2495 | right = block; |
| 2496 | xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB); |
| 2497 | error = xfs_btree_read_buf_block(cur, &lptr, |
| 2498 | cur->bc_nlevels - 1, 0, &left, &lbp); |
| 2499 | if (error) |
| 2500 | goto error0; |
| 2501 | bp = lbp; |
| 2502 | nptr = 2; |
| 2503 | } |
| 2504 | /* Fill in the new block's btree header and log it. */ |
| 2505 | xfs_btree_init_block(cur, cur->bc_nlevels, 2, new); |
| 2506 | xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS); |
| 2507 | ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) && |
| 2508 | !xfs_btree_ptr_is_null(cur, &rptr)); |
| 2509 | |
| 2510 | /* Fill in the key data in the new root. */ |
| 2511 | if (xfs_btree_get_level(left) > 0) { |
| 2512 | xfs_btree_copy_keys(cur, |
| 2513 | xfs_btree_key_addr(cur, 1, new), |
| 2514 | xfs_btree_key_addr(cur, 1, left), 1); |
| 2515 | xfs_btree_copy_keys(cur, |
| 2516 | xfs_btree_key_addr(cur, 2, new), |
| 2517 | xfs_btree_key_addr(cur, 1, right), 1); |
| 2518 | } else { |
| 2519 | cur->bc_ops->init_key_from_rec( |
| 2520 | xfs_btree_key_addr(cur, 1, new), |
| 2521 | xfs_btree_rec_addr(cur, 1, left)); |
| 2522 | cur->bc_ops->init_key_from_rec( |
| 2523 | xfs_btree_key_addr(cur, 2, new), |
| 2524 | xfs_btree_rec_addr(cur, 1, right)); |
| 2525 | } |
| 2526 | xfs_btree_log_keys(cur, nbp, 1, 2); |
| 2527 | |
| 2528 | /* Fill in the pointer data in the new root. */ |
| 2529 | xfs_btree_copy_ptrs(cur, |
| 2530 | xfs_btree_ptr_addr(cur, 1, new), &lptr, 1); |
| 2531 | xfs_btree_copy_ptrs(cur, |
| 2532 | xfs_btree_ptr_addr(cur, 2, new), &rptr, 1); |
| 2533 | xfs_btree_log_ptrs(cur, nbp, 1, 2); |
| 2534 | |
| 2535 | /* Fix up the cursor. */ |
| 2536 | xfs_btree_setbuf(cur, cur->bc_nlevels, nbp); |
| 2537 | cur->bc_ptrs[cur->bc_nlevels] = nptr; |
| 2538 | cur->bc_nlevels++; |
| 2539 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2540 | *stat = 1; |
| 2541 | return 0; |
| 2542 | error0: |
| 2543 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 2544 | return error; |
| 2545 | out0: |
| 2546 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2547 | *stat = 0; |
| 2548 | return 0; |
| 2549 | } |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2550 | |
| 2551 | STATIC int |
| 2552 | xfs_btree_make_block_unfull( |
| 2553 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 2554 | int level, /* btree level */ |
| 2555 | int numrecs,/* # of recs in block */ |
| 2556 | int *oindex,/* old tree index */ |
| 2557 | int *index, /* new tree index */ |
| 2558 | union xfs_btree_ptr *nptr, /* new btree ptr */ |
| 2559 | struct xfs_btree_cur **ncur, /* new btree cursor */ |
| 2560 | union xfs_btree_rec *nrec, /* new record */ |
| 2561 | int *stat) |
| 2562 | { |
| 2563 | union xfs_btree_key key; /* new btree key value */ |
| 2564 | int error = 0; |
| 2565 | |
| 2566 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && |
| 2567 | level == cur->bc_nlevels - 1) { |
| 2568 | struct xfs_inode *ip = cur->bc_private.b.ip; |
| 2569 | |
| 2570 | if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) { |
| 2571 | /* A root block that can be made bigger. */ |
| 2572 | |
| 2573 | xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork); |
| 2574 | } else { |
| 2575 | /* A root block that needs replacing */ |
| 2576 | int logflags = 0; |
| 2577 | |
| 2578 | error = xfs_btree_new_iroot(cur, &logflags, stat); |
| 2579 | if (error || *stat == 0) |
| 2580 | return error; |
| 2581 | |
| 2582 | xfs_trans_log_inode(cur->bc_tp, ip, logflags); |
| 2583 | } |
| 2584 | |
| 2585 | return 0; |
| 2586 | } |
| 2587 | |
| 2588 | /* First, try shifting an entry to the right neighbor. */ |
| 2589 | error = xfs_btree_rshift(cur, level, stat); |
| 2590 | if (error || *stat) |
| 2591 | return error; |
| 2592 | |
| 2593 | /* Next, try shifting an entry to the left neighbor. */ |
| 2594 | error = xfs_btree_lshift(cur, level, stat); |
| 2595 | if (error) |
| 2596 | return error; |
| 2597 | |
| 2598 | if (*stat) { |
| 2599 | *oindex = *index = cur->bc_ptrs[level]; |
| 2600 | return 0; |
| 2601 | } |
| 2602 | |
| 2603 | /* |
| 2604 | * Next, try splitting the current block in half. |
| 2605 | * |
| 2606 | * If this works we have to re-set our variables because we |
| 2607 | * could be in a different block now. |
| 2608 | */ |
| 2609 | error = xfs_btree_split(cur, level, nptr, &key, ncur, stat); |
| 2610 | if (error || *stat == 0) |
| 2611 | return error; |
| 2612 | |
| 2613 | |
| 2614 | *index = cur->bc_ptrs[level]; |
| 2615 | cur->bc_ops->init_rec_from_key(&key, nrec); |
| 2616 | return 0; |
| 2617 | } |
| 2618 | |
| 2619 | /* |
| 2620 | * Insert one record/level. Return information to the caller |
| 2621 | * allowing the next level up to proceed if necessary. |
| 2622 | */ |
| 2623 | STATIC int |
| 2624 | xfs_btree_insrec( |
| 2625 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 2626 | int level, /* level to insert record at */ |
| 2627 | union xfs_btree_ptr *ptrp, /* i/o: block number inserted */ |
| 2628 | union xfs_btree_rec *recp, /* i/o: record data inserted */ |
| 2629 | struct xfs_btree_cur **curp, /* output: new cursor replacing cur */ |
| 2630 | int *stat) /* success/failure */ |
| 2631 | { |
| 2632 | struct xfs_btree_block *block; /* btree block */ |
| 2633 | struct xfs_buf *bp; /* buffer for block */ |
| 2634 | union xfs_btree_key key; /* btree key */ |
| 2635 | union xfs_btree_ptr nptr; /* new block ptr */ |
| 2636 | struct xfs_btree_cur *ncur; /* new btree cursor */ |
| 2637 | union xfs_btree_rec nrec; /* new record count */ |
| 2638 | int optr; /* old key/record index */ |
| 2639 | int ptr; /* key/record index */ |
| 2640 | int numrecs;/* number of records */ |
| 2641 | int error; /* error return value */ |
| 2642 | #ifdef DEBUG |
| 2643 | int i; |
| 2644 | #endif |
| 2645 | |
| 2646 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 2647 | XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp); |
| 2648 | |
| 2649 | ncur = NULL; |
| 2650 | |
| 2651 | /* |
| 2652 | * If we have an external root pointer, and we've made it to the |
| 2653 | * root level, allocate a new root block and we're done. |
| 2654 | */ |
| 2655 | if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && |
| 2656 | (level >= cur->bc_nlevels)) { |
| 2657 | error = xfs_btree_new_root(cur, stat); |
| 2658 | xfs_btree_set_ptr_null(cur, ptrp); |
| 2659 | |
| 2660 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2661 | return error; |
| 2662 | } |
| 2663 | |
| 2664 | /* If we're off the left edge, return failure. */ |
| 2665 | ptr = cur->bc_ptrs[level]; |
| 2666 | if (ptr == 0) { |
| 2667 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2668 | *stat = 0; |
| 2669 | return 0; |
| 2670 | } |
| 2671 | |
| 2672 | /* Make a key out of the record data to be inserted, and save it. */ |
| 2673 | cur->bc_ops->init_key_from_rec(&key, recp); |
| 2674 | |
| 2675 | optr = ptr; |
| 2676 | |
| 2677 | XFS_BTREE_STATS_INC(cur, insrec); |
| 2678 | |
| 2679 | /* Get pointers to the btree buffer and block. */ |
| 2680 | block = xfs_btree_get_block(cur, level, &bp); |
| 2681 | numrecs = xfs_btree_get_numrecs(block); |
| 2682 | |
| 2683 | #ifdef DEBUG |
| 2684 | error = xfs_btree_check_block(cur, block, level, bp); |
| 2685 | if (error) |
| 2686 | goto error0; |
| 2687 | |
| 2688 | /* Check that the new entry is being inserted in the right place. */ |
| 2689 | if (ptr <= numrecs) { |
| 2690 | if (level == 0) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 2691 | ASSERT(cur->bc_ops->recs_inorder(cur, recp, |
| 2692 | xfs_btree_rec_addr(cur, ptr, block))); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2693 | } else { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 2694 | ASSERT(cur->bc_ops->keys_inorder(cur, &key, |
| 2695 | xfs_btree_key_addr(cur, ptr, block))); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2696 | } |
| 2697 | } |
| 2698 | #endif |
| 2699 | |
| 2700 | /* |
| 2701 | * If the block is full, we can't insert the new entry until we |
| 2702 | * make the block un-full. |
| 2703 | */ |
| 2704 | xfs_btree_set_ptr_null(cur, &nptr); |
| 2705 | if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) { |
| 2706 | error = xfs_btree_make_block_unfull(cur, level, numrecs, |
| 2707 | &optr, &ptr, &nptr, &ncur, &nrec, stat); |
| 2708 | if (error || *stat == 0) |
| 2709 | goto error0; |
| 2710 | } |
| 2711 | |
| 2712 | /* |
| 2713 | * The current block may have changed if the block was |
| 2714 | * previously full and we have just made space in it. |
| 2715 | */ |
| 2716 | block = xfs_btree_get_block(cur, level, &bp); |
| 2717 | numrecs = xfs_btree_get_numrecs(block); |
| 2718 | |
| 2719 | #ifdef DEBUG |
| 2720 | error = xfs_btree_check_block(cur, block, level, bp); |
| 2721 | if (error) |
| 2722 | return error; |
| 2723 | #endif |
| 2724 | |
| 2725 | /* |
| 2726 | * At this point we know there's room for our new entry in the block |
| 2727 | * we're pointing at. |
| 2728 | */ |
| 2729 | XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1); |
| 2730 | |
| 2731 | if (level > 0) { |
| 2732 | /* It's a nonleaf. make a hole in the keys and ptrs */ |
| 2733 | union xfs_btree_key *kp; |
| 2734 | union xfs_btree_ptr *pp; |
| 2735 | |
| 2736 | kp = xfs_btree_key_addr(cur, ptr, block); |
| 2737 | pp = xfs_btree_ptr_addr(cur, ptr, block); |
| 2738 | |
| 2739 | #ifdef DEBUG |
| 2740 | for (i = numrecs - ptr; i >= 0; i--) { |
| 2741 | error = xfs_btree_check_ptr(cur, pp, i, level); |
| 2742 | if (error) |
| 2743 | return error; |
| 2744 | } |
| 2745 | #endif |
| 2746 | |
| 2747 | xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1); |
| 2748 | xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1); |
| 2749 | |
| 2750 | #ifdef DEBUG |
| 2751 | error = xfs_btree_check_ptr(cur, ptrp, 0, level); |
| 2752 | if (error) |
| 2753 | goto error0; |
| 2754 | #endif |
| 2755 | |
| 2756 | /* Now put the new data in, bump numrecs and log it. */ |
| 2757 | xfs_btree_copy_keys(cur, kp, &key, 1); |
| 2758 | xfs_btree_copy_ptrs(cur, pp, ptrp, 1); |
| 2759 | numrecs++; |
| 2760 | xfs_btree_set_numrecs(block, numrecs); |
| 2761 | xfs_btree_log_ptrs(cur, bp, ptr, numrecs); |
| 2762 | xfs_btree_log_keys(cur, bp, ptr, numrecs); |
| 2763 | #ifdef DEBUG |
| 2764 | if (ptr < numrecs) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 2765 | ASSERT(cur->bc_ops->keys_inorder(cur, kp, |
| 2766 | xfs_btree_key_addr(cur, ptr + 1, block))); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2767 | } |
| 2768 | #endif |
| 2769 | } else { |
| 2770 | /* It's a leaf. make a hole in the records */ |
| 2771 | union xfs_btree_rec *rp; |
| 2772 | |
| 2773 | rp = xfs_btree_rec_addr(cur, ptr, block); |
| 2774 | |
| 2775 | xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1); |
| 2776 | |
| 2777 | /* Now put the new data in, bump numrecs and log it. */ |
| 2778 | xfs_btree_copy_recs(cur, rp, recp, 1); |
| 2779 | xfs_btree_set_numrecs(block, ++numrecs); |
| 2780 | xfs_btree_log_recs(cur, bp, ptr, numrecs); |
| 2781 | #ifdef DEBUG |
| 2782 | if (ptr < numrecs) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 2783 | ASSERT(cur->bc_ops->recs_inorder(cur, rp, |
| 2784 | xfs_btree_rec_addr(cur, ptr + 1, block))); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2785 | } |
| 2786 | #endif |
| 2787 | } |
| 2788 | |
| 2789 | /* Log the new number of records in the btree header. */ |
| 2790 | xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS); |
| 2791 | |
| 2792 | /* If we inserted at the start of a block, update the parents' keys. */ |
| 2793 | if (optr == 1) { |
| 2794 | error = xfs_btree_updkey(cur, &key, level + 1); |
| 2795 | if (error) |
| 2796 | goto error0; |
| 2797 | } |
| 2798 | |
| 2799 | /* |
| 2800 | * If we are tracking the last record in the tree and |
| 2801 | * we are at the far right edge of the tree, update it. |
| 2802 | */ |
| 2803 | if (xfs_btree_is_lastrec(cur, block, level)) { |
| 2804 | cur->bc_ops->update_lastrec(cur, block, recp, |
| 2805 | ptr, LASTREC_INSREC); |
| 2806 | } |
| 2807 | |
| 2808 | /* |
| 2809 | * Return the new block number, if any. |
| 2810 | * If there is one, give back a record value and a cursor too. |
| 2811 | */ |
| 2812 | *ptrp = nptr; |
| 2813 | if (!xfs_btree_ptr_is_null(cur, &nptr)) { |
| 2814 | *recp = nrec; |
| 2815 | *curp = ncur; |
| 2816 | } |
| 2817 | |
| 2818 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2819 | *stat = 1; |
| 2820 | return 0; |
| 2821 | |
| 2822 | error0: |
| 2823 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 2824 | return error; |
| 2825 | } |
| 2826 | |
| 2827 | /* |
| 2828 | * Insert the record at the point referenced by cur. |
| 2829 | * |
| 2830 | * A multi-level split of the tree on insert will invalidate the original |
| 2831 | * cursor. All callers of this function should assume that the cursor is |
| 2832 | * no longer valid and revalidate it. |
| 2833 | */ |
| 2834 | int |
| 2835 | xfs_btree_insert( |
| 2836 | struct xfs_btree_cur *cur, |
| 2837 | int *stat) |
| 2838 | { |
| 2839 | int error; /* error return value */ |
| 2840 | int i; /* result value, 0 for failure */ |
| 2841 | int level; /* current level number in btree */ |
| 2842 | union xfs_btree_ptr nptr; /* new block number (split result) */ |
| 2843 | struct xfs_btree_cur *ncur; /* new cursor (split result) */ |
| 2844 | struct xfs_btree_cur *pcur; /* previous level's cursor */ |
| 2845 | union xfs_btree_rec rec; /* record to insert */ |
| 2846 | |
| 2847 | level = 0; |
| 2848 | ncur = NULL; |
| 2849 | pcur = cur; |
| 2850 | |
| 2851 | xfs_btree_set_ptr_null(cur, &nptr); |
| 2852 | cur->bc_ops->init_rec_from_cur(cur, &rec); |
| 2853 | |
| 2854 | /* |
| 2855 | * Loop going up the tree, starting at the leaf level. |
| 2856 | * Stop when we don't get a split block, that must mean that |
| 2857 | * the insert is finished with this level. |
| 2858 | */ |
| 2859 | do { |
| 2860 | /* |
| 2861 | * Insert nrec/nptr into this level of the tree. |
| 2862 | * Note if we fail, nptr will be null. |
| 2863 | */ |
| 2864 | error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i); |
| 2865 | if (error) { |
| 2866 | if (pcur != cur) |
| 2867 | xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR); |
| 2868 | goto error0; |
| 2869 | } |
| 2870 | |
| 2871 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 2872 | level++; |
| 2873 | |
| 2874 | /* |
| 2875 | * See if the cursor we just used is trash. |
| 2876 | * Can't trash the caller's cursor, but otherwise we should |
| 2877 | * if ncur is a new cursor or we're about to be done. |
| 2878 | */ |
| 2879 | if (pcur != cur && |
| 2880 | (ncur || xfs_btree_ptr_is_null(cur, &nptr))) { |
| 2881 | /* Save the state from the cursor before we trash it */ |
| 2882 | if (cur->bc_ops->update_cursor) |
| 2883 | cur->bc_ops->update_cursor(pcur, cur); |
| 2884 | cur->bc_nlevels = pcur->bc_nlevels; |
| 2885 | xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR); |
| 2886 | } |
| 2887 | /* If we got a new cursor, switch to it. */ |
| 2888 | if (ncur) { |
| 2889 | pcur = ncur; |
| 2890 | ncur = NULL; |
| 2891 | } |
| 2892 | } while (!xfs_btree_ptr_is_null(cur, &nptr)); |
| 2893 | |
| 2894 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2895 | *stat = i; |
| 2896 | return 0; |
| 2897 | error0: |
| 2898 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 2899 | return error; |
| 2900 | } |
Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 2901 | |
| 2902 | /* |
| 2903 | * Try to merge a non-leaf block back into the inode root. |
| 2904 | * |
| 2905 | * Note: the killroot names comes from the fact that we're effectively |
| 2906 | * killing the old root block. But because we can't just delete the |
| 2907 | * inode we have to copy the single block it was pointing to into the |
| 2908 | * inode. |
| 2909 | */ |
Eric Sandeen | d96f8f8 | 2009-07-02 00:09:33 -0500 | [diff] [blame] | 2910 | STATIC int |
Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 2911 | xfs_btree_kill_iroot( |
| 2912 | struct xfs_btree_cur *cur) |
| 2913 | { |
| 2914 | int whichfork = cur->bc_private.b.whichfork; |
| 2915 | struct xfs_inode *ip = cur->bc_private.b.ip; |
| 2916 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2917 | struct xfs_btree_block *block; |
| 2918 | struct xfs_btree_block *cblock; |
| 2919 | union xfs_btree_key *kp; |
| 2920 | union xfs_btree_key *ckp; |
| 2921 | union xfs_btree_ptr *pp; |
| 2922 | union xfs_btree_ptr *cpp; |
| 2923 | struct xfs_buf *cbp; |
| 2924 | int level; |
| 2925 | int index; |
| 2926 | int numrecs; |
| 2927 | #ifdef DEBUG |
| 2928 | union xfs_btree_ptr ptr; |
| 2929 | int i; |
| 2930 | #endif |
| 2931 | |
| 2932 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 2933 | |
| 2934 | ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE); |
| 2935 | ASSERT(cur->bc_nlevels > 1); |
| 2936 | |
| 2937 | /* |
| 2938 | * Don't deal with the root block needs to be a leaf case. |
| 2939 | * We're just going to turn the thing back into extents anyway. |
| 2940 | */ |
| 2941 | level = cur->bc_nlevels - 1; |
| 2942 | if (level == 1) |
| 2943 | goto out0; |
| 2944 | |
| 2945 | /* |
| 2946 | * Give up if the root has multiple children. |
| 2947 | */ |
| 2948 | block = xfs_btree_get_iroot(cur); |
| 2949 | if (xfs_btree_get_numrecs(block) != 1) |
| 2950 | goto out0; |
| 2951 | |
| 2952 | cblock = xfs_btree_get_block(cur, level - 1, &cbp); |
| 2953 | numrecs = xfs_btree_get_numrecs(cblock); |
| 2954 | |
| 2955 | /* |
| 2956 | * Only do this if the next level will fit. |
| 2957 | * Then the data must be copied up to the inode, |
| 2958 | * instead of freeing the root you free the next level. |
| 2959 | */ |
| 2960 | if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level)) |
| 2961 | goto out0; |
| 2962 | |
| 2963 | XFS_BTREE_STATS_INC(cur, killroot); |
| 2964 | |
| 2965 | #ifdef DEBUG |
| 2966 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB); |
| 2967 | ASSERT(xfs_btree_ptr_is_null(cur, &ptr)); |
| 2968 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB); |
| 2969 | ASSERT(xfs_btree_ptr_is_null(cur, &ptr)); |
| 2970 | #endif |
| 2971 | |
| 2972 | index = numrecs - cur->bc_ops->get_maxrecs(cur, level); |
| 2973 | if (index) { |
| 2974 | xfs_iroot_realloc(cur->bc_private.b.ip, index, |
| 2975 | cur->bc_private.b.whichfork); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2976 | block = ifp->if_broot; |
Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | be16_add_cpu(&block->bb_numrecs, index); |
| 2980 | ASSERT(block->bb_numrecs == cblock->bb_numrecs); |
| 2981 | |
| 2982 | kp = xfs_btree_key_addr(cur, 1, block); |
| 2983 | ckp = xfs_btree_key_addr(cur, 1, cblock); |
| 2984 | xfs_btree_copy_keys(cur, kp, ckp, numrecs); |
| 2985 | |
| 2986 | pp = xfs_btree_ptr_addr(cur, 1, block); |
| 2987 | cpp = xfs_btree_ptr_addr(cur, 1, cblock); |
| 2988 | #ifdef DEBUG |
| 2989 | for (i = 0; i < numrecs; i++) { |
| 2990 | int error; |
| 2991 | |
| 2992 | error = xfs_btree_check_ptr(cur, cpp, i, level - 1); |
| 2993 | if (error) { |
| 2994 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 2995 | return error; |
| 2996 | } |
| 2997 | } |
| 2998 | #endif |
| 2999 | xfs_btree_copy_ptrs(cur, pp, cpp, numrecs); |
| 3000 | |
| 3001 | cur->bc_ops->free_block(cur, cbp); |
| 3002 | XFS_BTREE_STATS_INC(cur, free); |
| 3003 | |
| 3004 | cur->bc_bufs[level - 1] = NULL; |
| 3005 | be16_add_cpu(&block->bb_level, -1); |
| 3006 | xfs_trans_log_inode(cur->bc_tp, ip, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3007 | XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork)); |
Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 3008 | cur->bc_nlevels--; |
| 3009 | out0: |
| 3010 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3011 | return 0; |
| 3012 | } |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 3013 | |
| 3014 | STATIC int |
| 3015 | xfs_btree_dec_cursor( |
| 3016 | struct xfs_btree_cur *cur, |
| 3017 | int level, |
| 3018 | int *stat) |
| 3019 | { |
| 3020 | int error; |
| 3021 | int i; |
| 3022 | |
| 3023 | if (level > 0) { |
| 3024 | error = xfs_btree_decrement(cur, level, &i); |
| 3025 | if (error) |
| 3026 | return error; |
| 3027 | } |
| 3028 | |
| 3029 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3030 | *stat = 1; |
| 3031 | return 0; |
| 3032 | } |
| 3033 | |
| 3034 | /* |
| 3035 | * Single level of the btree record deletion routine. |
| 3036 | * Delete record pointed to by cur/level. |
| 3037 | * Remove the record from its block then rebalance the tree. |
| 3038 | * Return 0 for error, 1 for done, 2 to go on to the next level. |
| 3039 | */ |
| 3040 | STATIC int /* error */ |
| 3041 | xfs_btree_delrec( |
| 3042 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 3043 | int level, /* level removing record from */ |
| 3044 | int *stat) /* fail/done/go-on */ |
| 3045 | { |
| 3046 | struct xfs_btree_block *block; /* btree block */ |
| 3047 | union xfs_btree_ptr cptr; /* current block ptr */ |
| 3048 | struct xfs_buf *bp; /* buffer for block */ |
| 3049 | int error; /* error return value */ |
| 3050 | int i; /* loop counter */ |
| 3051 | union xfs_btree_key key; /* storage for keyp */ |
| 3052 | union xfs_btree_key *keyp = &key; /* passed to the next level */ |
| 3053 | union xfs_btree_ptr lptr; /* left sibling block ptr */ |
| 3054 | struct xfs_buf *lbp; /* left buffer pointer */ |
| 3055 | struct xfs_btree_block *left; /* left btree block */ |
| 3056 | int lrecs = 0; /* left record count */ |
| 3057 | int ptr; /* key/record index */ |
| 3058 | union xfs_btree_ptr rptr; /* right sibling block ptr */ |
| 3059 | struct xfs_buf *rbp; /* right buffer pointer */ |
| 3060 | struct xfs_btree_block *right; /* right btree block */ |
| 3061 | struct xfs_btree_block *rrblock; /* right-right btree block */ |
| 3062 | struct xfs_buf *rrbp; /* right-right buffer pointer */ |
| 3063 | int rrecs = 0; /* right record count */ |
| 3064 | struct xfs_btree_cur *tcur; /* temporary btree cursor */ |
| 3065 | int numrecs; /* temporary numrec count */ |
| 3066 | |
| 3067 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 3068 | XFS_BTREE_TRACE_ARGI(cur, level); |
| 3069 | |
| 3070 | tcur = NULL; |
| 3071 | |
| 3072 | /* Get the index of the entry being deleted, check for nothing there. */ |
| 3073 | ptr = cur->bc_ptrs[level]; |
| 3074 | if (ptr == 0) { |
| 3075 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3076 | *stat = 0; |
| 3077 | return 0; |
| 3078 | } |
| 3079 | |
| 3080 | /* Get the buffer & block containing the record or key/ptr. */ |
| 3081 | block = xfs_btree_get_block(cur, level, &bp); |
| 3082 | numrecs = xfs_btree_get_numrecs(block); |
| 3083 | |
| 3084 | #ifdef DEBUG |
| 3085 | error = xfs_btree_check_block(cur, block, level, bp); |
| 3086 | if (error) |
| 3087 | goto error0; |
| 3088 | #endif |
| 3089 | |
| 3090 | /* Fail if we're off the end of the block. */ |
| 3091 | if (ptr > numrecs) { |
| 3092 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3093 | *stat = 0; |
| 3094 | return 0; |
| 3095 | } |
| 3096 | |
| 3097 | XFS_BTREE_STATS_INC(cur, delrec); |
| 3098 | XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr); |
| 3099 | |
| 3100 | /* Excise the entries being deleted. */ |
| 3101 | if (level > 0) { |
| 3102 | /* It's a nonleaf. operate on keys and ptrs */ |
| 3103 | union xfs_btree_key *lkp; |
| 3104 | union xfs_btree_ptr *lpp; |
| 3105 | |
| 3106 | lkp = xfs_btree_key_addr(cur, ptr + 1, block); |
| 3107 | lpp = xfs_btree_ptr_addr(cur, ptr + 1, block); |
| 3108 | |
| 3109 | #ifdef DEBUG |
| 3110 | for (i = 0; i < numrecs - ptr; i++) { |
| 3111 | error = xfs_btree_check_ptr(cur, lpp, i, level); |
| 3112 | if (error) |
| 3113 | goto error0; |
| 3114 | } |
| 3115 | #endif |
| 3116 | |
| 3117 | if (ptr < numrecs) { |
| 3118 | xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr); |
| 3119 | xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr); |
| 3120 | xfs_btree_log_keys(cur, bp, ptr, numrecs - 1); |
| 3121 | xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1); |
| 3122 | } |
| 3123 | |
| 3124 | /* |
| 3125 | * If it's the first record in the block, we'll need to pass a |
| 3126 | * key up to the next level (updkey). |
| 3127 | */ |
| 3128 | if (ptr == 1) |
| 3129 | keyp = xfs_btree_key_addr(cur, 1, block); |
| 3130 | } else { |
| 3131 | /* It's a leaf. operate on records */ |
| 3132 | if (ptr < numrecs) { |
| 3133 | xfs_btree_shift_recs(cur, |
| 3134 | xfs_btree_rec_addr(cur, ptr + 1, block), |
| 3135 | -1, numrecs - ptr); |
| 3136 | xfs_btree_log_recs(cur, bp, ptr, numrecs - 1); |
| 3137 | } |
| 3138 | |
| 3139 | /* |
| 3140 | * If it's the first record in the block, we'll need a key |
| 3141 | * structure to pass up to the next level (updkey). |
| 3142 | */ |
| 3143 | if (ptr == 1) { |
| 3144 | cur->bc_ops->init_key_from_rec(&key, |
| 3145 | xfs_btree_rec_addr(cur, 1, block)); |
| 3146 | keyp = &key; |
| 3147 | } |
| 3148 | } |
| 3149 | |
| 3150 | /* |
| 3151 | * Decrement and log the number of entries in the block. |
| 3152 | */ |
| 3153 | xfs_btree_set_numrecs(block, --numrecs); |
| 3154 | xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS); |
| 3155 | |
| 3156 | /* |
| 3157 | * If we are tracking the last record in the tree and |
| 3158 | * we are at the far right edge of the tree, update it. |
| 3159 | */ |
| 3160 | if (xfs_btree_is_lastrec(cur, block, level)) { |
| 3161 | cur->bc_ops->update_lastrec(cur, block, NULL, |
| 3162 | ptr, LASTREC_DELREC); |
| 3163 | } |
| 3164 | |
| 3165 | /* |
| 3166 | * We're at the root level. First, shrink the root block in-memory. |
| 3167 | * Try to get rid of the next level down. If we can't then there's |
| 3168 | * nothing left to do. |
| 3169 | */ |
| 3170 | if (level == cur->bc_nlevels - 1) { |
| 3171 | if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) { |
| 3172 | xfs_iroot_realloc(cur->bc_private.b.ip, -1, |
| 3173 | cur->bc_private.b.whichfork); |
| 3174 | |
| 3175 | error = xfs_btree_kill_iroot(cur); |
| 3176 | if (error) |
| 3177 | goto error0; |
| 3178 | |
| 3179 | error = xfs_btree_dec_cursor(cur, level, stat); |
| 3180 | if (error) |
| 3181 | goto error0; |
| 3182 | *stat = 1; |
| 3183 | return 0; |
| 3184 | } |
| 3185 | |
| 3186 | /* |
| 3187 | * If this is the root level, and there's only one entry left, |
| 3188 | * and it's NOT the leaf level, then we can get rid of this |
| 3189 | * level. |
| 3190 | */ |
| 3191 | if (numrecs == 1 && level > 0) { |
| 3192 | union xfs_btree_ptr *pp; |
| 3193 | /* |
| 3194 | * pp is still set to the first pointer in the block. |
| 3195 | * Make it the new root of the btree. |
| 3196 | */ |
| 3197 | pp = xfs_btree_ptr_addr(cur, 1, block); |
| 3198 | error = cur->bc_ops->kill_root(cur, bp, level, pp); |
| 3199 | if (error) |
| 3200 | goto error0; |
| 3201 | } else if (level > 0) { |
| 3202 | error = xfs_btree_dec_cursor(cur, level, stat); |
| 3203 | if (error) |
| 3204 | goto error0; |
| 3205 | } |
| 3206 | *stat = 1; |
| 3207 | return 0; |
| 3208 | } |
| 3209 | |
| 3210 | /* |
| 3211 | * If we deleted the leftmost entry in the block, update the |
| 3212 | * key values above us in the tree. |
| 3213 | */ |
| 3214 | if (ptr == 1) { |
| 3215 | error = xfs_btree_updkey(cur, keyp, level + 1); |
| 3216 | if (error) |
| 3217 | goto error0; |
| 3218 | } |
| 3219 | |
| 3220 | /* |
| 3221 | * If the number of records remaining in the block is at least |
| 3222 | * the minimum, we're done. |
| 3223 | */ |
| 3224 | if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) { |
| 3225 | error = xfs_btree_dec_cursor(cur, level, stat); |
| 3226 | if (error) |
| 3227 | goto error0; |
| 3228 | return 0; |
| 3229 | } |
| 3230 | |
| 3231 | /* |
| 3232 | * Otherwise, we have to move some records around to keep the |
| 3233 | * tree balanced. Look at the left and right sibling blocks to |
| 3234 | * see if we can re-balance by moving only one record. |
| 3235 | */ |
| 3236 | xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB); |
| 3237 | xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB); |
| 3238 | |
| 3239 | if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) { |
| 3240 | /* |
| 3241 | * One child of root, need to get a chance to copy its contents |
| 3242 | * into the root and delete it. Can't go up to next level, |
| 3243 | * there's nothing to delete there. |
| 3244 | */ |
| 3245 | if (xfs_btree_ptr_is_null(cur, &rptr) && |
| 3246 | xfs_btree_ptr_is_null(cur, &lptr) && |
| 3247 | level == cur->bc_nlevels - 2) { |
| 3248 | error = xfs_btree_kill_iroot(cur); |
| 3249 | if (!error) |
| 3250 | error = xfs_btree_dec_cursor(cur, level, stat); |
| 3251 | if (error) |
| 3252 | goto error0; |
| 3253 | return 0; |
| 3254 | } |
| 3255 | } |
| 3256 | |
| 3257 | ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) || |
| 3258 | !xfs_btree_ptr_is_null(cur, &lptr)); |
| 3259 | |
| 3260 | /* |
| 3261 | * Duplicate the cursor so our btree manipulations here won't |
| 3262 | * disrupt the next level up. |
| 3263 | */ |
| 3264 | error = xfs_btree_dup_cursor(cur, &tcur); |
| 3265 | if (error) |
| 3266 | goto error0; |
| 3267 | |
| 3268 | /* |
| 3269 | * If there's a right sibling, see if it's ok to shift an entry |
| 3270 | * out of it. |
| 3271 | */ |
| 3272 | if (!xfs_btree_ptr_is_null(cur, &rptr)) { |
| 3273 | /* |
| 3274 | * Move the temp cursor to the last entry in the next block. |
| 3275 | * Actually any entry but the first would suffice. |
| 3276 | */ |
| 3277 | i = xfs_btree_lastrec(tcur, level); |
| 3278 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 3279 | |
| 3280 | error = xfs_btree_increment(tcur, level, &i); |
| 3281 | if (error) |
| 3282 | goto error0; |
| 3283 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 3284 | |
| 3285 | i = xfs_btree_lastrec(tcur, level); |
| 3286 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 3287 | |
| 3288 | /* Grab a pointer to the block. */ |
| 3289 | right = xfs_btree_get_block(tcur, level, &rbp); |
| 3290 | #ifdef DEBUG |
| 3291 | error = xfs_btree_check_block(tcur, right, level, rbp); |
| 3292 | if (error) |
| 3293 | goto error0; |
| 3294 | #endif |
| 3295 | /* Grab the current block number, for future use. */ |
| 3296 | xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB); |
| 3297 | |
| 3298 | /* |
| 3299 | * If right block is full enough so that removing one entry |
| 3300 | * won't make it too empty, and left-shifting an entry out |
| 3301 | * of right to us works, we're done. |
| 3302 | */ |
| 3303 | if (xfs_btree_get_numrecs(right) - 1 >= |
| 3304 | cur->bc_ops->get_minrecs(tcur, level)) { |
| 3305 | error = xfs_btree_lshift(tcur, level, &i); |
| 3306 | if (error) |
| 3307 | goto error0; |
| 3308 | if (i) { |
| 3309 | ASSERT(xfs_btree_get_numrecs(block) >= |
| 3310 | cur->bc_ops->get_minrecs(tcur, level)); |
| 3311 | |
| 3312 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
| 3313 | tcur = NULL; |
| 3314 | |
| 3315 | error = xfs_btree_dec_cursor(cur, level, stat); |
| 3316 | if (error) |
| 3317 | goto error0; |
| 3318 | return 0; |
| 3319 | } |
| 3320 | } |
| 3321 | |
| 3322 | /* |
| 3323 | * Otherwise, grab the number of records in right for |
| 3324 | * future reference, and fix up the temp cursor to point |
| 3325 | * to our block again (last record). |
| 3326 | */ |
| 3327 | rrecs = xfs_btree_get_numrecs(right); |
| 3328 | if (!xfs_btree_ptr_is_null(cur, &lptr)) { |
| 3329 | i = xfs_btree_firstrec(tcur, level); |
| 3330 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 3331 | |
| 3332 | error = xfs_btree_decrement(tcur, level, &i); |
| 3333 | if (error) |
| 3334 | goto error0; |
| 3335 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 3336 | } |
| 3337 | } |
| 3338 | |
| 3339 | /* |
| 3340 | * If there's a left sibling, see if it's ok to shift an entry |
| 3341 | * out of it. |
| 3342 | */ |
| 3343 | if (!xfs_btree_ptr_is_null(cur, &lptr)) { |
| 3344 | /* |
| 3345 | * Move the temp cursor to the first entry in the |
| 3346 | * previous block. |
| 3347 | */ |
| 3348 | i = xfs_btree_firstrec(tcur, level); |
| 3349 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 3350 | |
| 3351 | error = xfs_btree_decrement(tcur, level, &i); |
| 3352 | if (error) |
| 3353 | goto error0; |
| 3354 | i = xfs_btree_firstrec(tcur, level); |
| 3355 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 3356 | |
| 3357 | /* Grab a pointer to the block. */ |
| 3358 | left = xfs_btree_get_block(tcur, level, &lbp); |
| 3359 | #ifdef DEBUG |
| 3360 | error = xfs_btree_check_block(cur, left, level, lbp); |
| 3361 | if (error) |
| 3362 | goto error0; |
| 3363 | #endif |
| 3364 | /* Grab the current block number, for future use. */ |
| 3365 | xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB); |
| 3366 | |
| 3367 | /* |
| 3368 | * If left block is full enough so that removing one entry |
| 3369 | * won't make it too empty, and right-shifting an entry out |
| 3370 | * of left to us works, we're done. |
| 3371 | */ |
| 3372 | if (xfs_btree_get_numrecs(left) - 1 >= |
| 3373 | cur->bc_ops->get_minrecs(tcur, level)) { |
| 3374 | error = xfs_btree_rshift(tcur, level, &i); |
| 3375 | if (error) |
| 3376 | goto error0; |
| 3377 | if (i) { |
| 3378 | ASSERT(xfs_btree_get_numrecs(block) >= |
| 3379 | cur->bc_ops->get_minrecs(tcur, level)); |
| 3380 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
| 3381 | tcur = NULL; |
| 3382 | if (level == 0) |
| 3383 | cur->bc_ptrs[0]++; |
| 3384 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3385 | *stat = 1; |
| 3386 | return 0; |
| 3387 | } |
| 3388 | } |
| 3389 | |
| 3390 | /* |
| 3391 | * Otherwise, grab the number of records in right for |
| 3392 | * future reference. |
| 3393 | */ |
| 3394 | lrecs = xfs_btree_get_numrecs(left); |
| 3395 | } |
| 3396 | |
| 3397 | /* Delete the temp cursor, we're done with it. */ |
| 3398 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
| 3399 | tcur = NULL; |
| 3400 | |
| 3401 | /* If here, we need to do a join to keep the tree balanced. */ |
| 3402 | ASSERT(!xfs_btree_ptr_is_null(cur, &cptr)); |
| 3403 | |
| 3404 | if (!xfs_btree_ptr_is_null(cur, &lptr) && |
| 3405 | lrecs + xfs_btree_get_numrecs(block) <= |
| 3406 | cur->bc_ops->get_maxrecs(cur, level)) { |
| 3407 | /* |
| 3408 | * Set "right" to be the starting block, |
| 3409 | * "left" to be the left neighbor. |
| 3410 | */ |
| 3411 | rptr = cptr; |
| 3412 | right = block; |
| 3413 | rbp = bp; |
| 3414 | error = xfs_btree_read_buf_block(cur, &lptr, level, |
| 3415 | 0, &left, &lbp); |
| 3416 | if (error) |
| 3417 | goto error0; |
| 3418 | |
| 3419 | /* |
| 3420 | * If that won't work, see if we can join with the right neighbor block. |
| 3421 | */ |
| 3422 | } else if (!xfs_btree_ptr_is_null(cur, &rptr) && |
| 3423 | rrecs + xfs_btree_get_numrecs(block) <= |
| 3424 | cur->bc_ops->get_maxrecs(cur, level)) { |
| 3425 | /* |
| 3426 | * Set "left" to be the starting block, |
| 3427 | * "right" to be the right neighbor. |
| 3428 | */ |
| 3429 | lptr = cptr; |
| 3430 | left = block; |
| 3431 | lbp = bp; |
| 3432 | error = xfs_btree_read_buf_block(cur, &rptr, level, |
| 3433 | 0, &right, &rbp); |
| 3434 | if (error) |
| 3435 | goto error0; |
| 3436 | |
| 3437 | /* |
| 3438 | * Otherwise, we can't fix the imbalance. |
| 3439 | * Just return. This is probably a logic error, but it's not fatal. |
| 3440 | */ |
| 3441 | } else { |
| 3442 | error = xfs_btree_dec_cursor(cur, level, stat); |
| 3443 | if (error) |
| 3444 | goto error0; |
| 3445 | return 0; |
| 3446 | } |
| 3447 | |
| 3448 | rrecs = xfs_btree_get_numrecs(right); |
| 3449 | lrecs = xfs_btree_get_numrecs(left); |
| 3450 | |
| 3451 | /* |
| 3452 | * We're now going to join "left" and "right" by moving all the stuff |
| 3453 | * in "right" to "left" and deleting "right". |
| 3454 | */ |
| 3455 | XFS_BTREE_STATS_ADD(cur, moves, rrecs); |
| 3456 | if (level > 0) { |
| 3457 | /* It's a non-leaf. Move keys and pointers. */ |
| 3458 | union xfs_btree_key *lkp; /* left btree key */ |
| 3459 | union xfs_btree_ptr *lpp; /* left address pointer */ |
| 3460 | union xfs_btree_key *rkp; /* right btree key */ |
| 3461 | union xfs_btree_ptr *rpp; /* right address pointer */ |
| 3462 | |
| 3463 | lkp = xfs_btree_key_addr(cur, lrecs + 1, left); |
| 3464 | lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left); |
| 3465 | rkp = xfs_btree_key_addr(cur, 1, right); |
| 3466 | rpp = xfs_btree_ptr_addr(cur, 1, right); |
| 3467 | #ifdef DEBUG |
| 3468 | for (i = 1; i < rrecs; i++) { |
| 3469 | error = xfs_btree_check_ptr(cur, rpp, i, level); |
| 3470 | if (error) |
| 3471 | goto error0; |
| 3472 | } |
| 3473 | #endif |
| 3474 | xfs_btree_copy_keys(cur, lkp, rkp, rrecs); |
| 3475 | xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs); |
| 3476 | |
| 3477 | xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs); |
| 3478 | xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs); |
| 3479 | } else { |
| 3480 | /* It's a leaf. Move records. */ |
| 3481 | union xfs_btree_rec *lrp; /* left record pointer */ |
| 3482 | union xfs_btree_rec *rrp; /* right record pointer */ |
| 3483 | |
| 3484 | lrp = xfs_btree_rec_addr(cur, lrecs + 1, left); |
| 3485 | rrp = xfs_btree_rec_addr(cur, 1, right); |
| 3486 | |
| 3487 | xfs_btree_copy_recs(cur, lrp, rrp, rrecs); |
| 3488 | xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs); |
| 3489 | } |
| 3490 | |
| 3491 | XFS_BTREE_STATS_INC(cur, join); |
| 3492 | |
| 3493 | /* |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3494 | * Fix up the number of records and right block pointer in the |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 3495 | * surviving block, and log it. |
| 3496 | */ |
| 3497 | xfs_btree_set_numrecs(left, lrecs + rrecs); |
| 3498 | xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB), |
| 3499 | xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB); |
| 3500 | xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB); |
| 3501 | |
| 3502 | /* If there is a right sibling, point it to the remaining block. */ |
| 3503 | xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB); |
| 3504 | if (!xfs_btree_ptr_is_null(cur, &cptr)) { |
| 3505 | error = xfs_btree_read_buf_block(cur, &cptr, level, |
| 3506 | 0, &rrblock, &rrbp); |
| 3507 | if (error) |
| 3508 | goto error0; |
| 3509 | xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB); |
| 3510 | xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB); |
| 3511 | } |
| 3512 | |
| 3513 | /* Free the deleted block. */ |
| 3514 | error = cur->bc_ops->free_block(cur, rbp); |
| 3515 | if (error) |
| 3516 | goto error0; |
| 3517 | XFS_BTREE_STATS_INC(cur, free); |
| 3518 | |
| 3519 | /* |
| 3520 | * If we joined with the left neighbor, set the buffer in the |
| 3521 | * cursor to the left block, and fix up the index. |
| 3522 | */ |
| 3523 | if (bp != lbp) { |
| 3524 | cur->bc_bufs[level] = lbp; |
| 3525 | cur->bc_ptrs[level] += lrecs; |
| 3526 | cur->bc_ra[level] = 0; |
| 3527 | } |
| 3528 | /* |
| 3529 | * If we joined with the right neighbor and there's a level above |
| 3530 | * us, increment the cursor at that level. |
| 3531 | */ |
| 3532 | else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || |
| 3533 | (level + 1 < cur->bc_nlevels)) { |
| 3534 | error = xfs_btree_increment(cur, level + 1, &i); |
| 3535 | if (error) |
| 3536 | goto error0; |
| 3537 | } |
| 3538 | |
| 3539 | /* |
| 3540 | * Readjust the ptr at this level if it's not a leaf, since it's |
| 3541 | * still pointing at the deletion point, which makes the cursor |
| 3542 | * inconsistent. If this makes the ptr 0, the caller fixes it up. |
| 3543 | * We can't use decrement because it would change the next level up. |
| 3544 | */ |
| 3545 | if (level > 0) |
| 3546 | cur->bc_ptrs[level]--; |
| 3547 | |
| 3548 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3549 | /* Return value means the next level up has something to do. */ |
| 3550 | *stat = 2; |
| 3551 | return 0; |
| 3552 | |
| 3553 | error0: |
| 3554 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 3555 | if (tcur) |
| 3556 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); |
| 3557 | return error; |
| 3558 | } |
| 3559 | |
| 3560 | /* |
| 3561 | * Delete the record pointed to by cur. |
| 3562 | * The cursor refers to the place where the record was (could be inserted) |
| 3563 | * when the operation returns. |
| 3564 | */ |
| 3565 | int /* error */ |
| 3566 | xfs_btree_delete( |
| 3567 | struct xfs_btree_cur *cur, |
| 3568 | int *stat) /* success/failure */ |
| 3569 | { |
| 3570 | int error; /* error return value */ |
| 3571 | int level; |
| 3572 | int i; |
| 3573 | |
| 3574 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 3575 | |
| 3576 | /* |
| 3577 | * Go up the tree, starting at leaf level. |
| 3578 | * |
| 3579 | * If 2 is returned then a join was done; go to the next level. |
| 3580 | * Otherwise we are done. |
| 3581 | */ |
| 3582 | for (level = 0, i = 2; i == 2; level++) { |
| 3583 | error = xfs_btree_delrec(cur, level, &i); |
| 3584 | if (error) |
| 3585 | goto error0; |
| 3586 | } |
| 3587 | |
| 3588 | if (i == 0) { |
| 3589 | for (level = 1; level < cur->bc_nlevels; level++) { |
| 3590 | if (cur->bc_ptrs[level] == 0) { |
| 3591 | error = xfs_btree_decrement(cur, level, &i); |
| 3592 | if (error) |
| 3593 | goto error0; |
| 3594 | break; |
| 3595 | } |
| 3596 | } |
| 3597 | } |
| 3598 | |
| 3599 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3600 | *stat = i; |
| 3601 | return 0; |
| 3602 | error0: |
| 3603 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 3604 | return error; |
| 3605 | } |
Christoph Hellwig | 8cc938f | 2008-10-30 16:58:11 +1100 | [diff] [blame] | 3606 | |
| 3607 | /* |
| 3608 | * Get the data from the pointed-to record. |
| 3609 | */ |
| 3610 | int /* error */ |
| 3611 | xfs_btree_get_rec( |
| 3612 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 3613 | union xfs_btree_rec **recp, /* output: btree record */ |
| 3614 | int *stat) /* output: success/failure */ |
| 3615 | { |
| 3616 | struct xfs_btree_block *block; /* btree block */ |
| 3617 | struct xfs_buf *bp; /* buffer pointer */ |
| 3618 | int ptr; /* record number */ |
| 3619 | #ifdef DEBUG |
| 3620 | int error; /* error return value */ |
| 3621 | #endif |
| 3622 | |
| 3623 | ptr = cur->bc_ptrs[0]; |
| 3624 | block = xfs_btree_get_block(cur, 0, &bp); |
| 3625 | |
| 3626 | #ifdef DEBUG |
| 3627 | error = xfs_btree_check_block(cur, block, 0, bp); |
| 3628 | if (error) |
| 3629 | return error; |
| 3630 | #endif |
| 3631 | |
| 3632 | /* |
| 3633 | * Off the right end or left end, return failure. |
| 3634 | */ |
| 3635 | if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) { |
| 3636 | *stat = 0; |
| 3637 | return 0; |
| 3638 | } |
| 3639 | |
| 3640 | /* |
| 3641 | * Point to the record and extract its data. |
| 3642 | */ |
| 3643 | *recp = xfs_btree_rec_addr(cur, ptr, block); |
| 3644 | *stat = 1; |
| 3645 | return 0; |
| 3646 | } |