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-2001,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" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 27 | #include "xfs_dir2.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_dmapi.h" |
| 29 | #include "xfs_mount.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 30 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_alloc_btree.h" |
| 32 | #include "xfs_ialloc_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #include "xfs_dir2_sf.h" |
| 34 | #include "xfs_attr_sf.h" |
| 35 | #include "xfs_dinode.h" |
| 36 | #include "xfs_inode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "xfs_btree.h" |
| 38 | #include "xfs_ialloc.h" |
| 39 | #include "xfs_alloc.h" |
| 40 | #include "xfs_error.h" |
| 41 | |
| 42 | /* |
| 43 | * Prototypes for internal functions. |
| 44 | */ |
| 45 | |
| 46 | STATIC void xfs_alloc_log_block(xfs_trans_t *, xfs_buf_t *, int); |
| 47 | STATIC void xfs_alloc_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int); |
| 48 | STATIC void xfs_alloc_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int); |
| 49 | STATIC void xfs_alloc_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int); |
| 50 | STATIC int xfs_alloc_lshift(xfs_btree_cur_t *, int, int *); |
| 51 | STATIC int xfs_alloc_newroot(xfs_btree_cur_t *, int *); |
| 52 | STATIC int xfs_alloc_rshift(xfs_btree_cur_t *, int, int *); |
| 53 | STATIC int xfs_alloc_split(xfs_btree_cur_t *, int, xfs_agblock_t *, |
| 54 | xfs_alloc_key_t *, xfs_btree_cur_t **, int *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * Internal functions. |
| 58 | */ |
| 59 | |
| 60 | /* |
| 61 | * Single level of the xfs_alloc_delete record deletion routine. |
| 62 | * Delete record pointed to by cur/level. |
| 63 | * Remove the record from its block then rebalance the tree. |
| 64 | * Return 0 for error, 1 for done, 2 to go on to the next level. |
| 65 | */ |
| 66 | STATIC int /* error */ |
| 67 | xfs_alloc_delrec( |
| 68 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 69 | int level, /* level removing record from */ |
| 70 | int *stat) /* fail/done/go-on */ |
| 71 | { |
| 72 | xfs_agf_t *agf; /* allocation group freelist header */ |
| 73 | xfs_alloc_block_t *block; /* btree block record/key lives in */ |
| 74 | xfs_agblock_t bno; /* btree block number */ |
| 75 | xfs_buf_t *bp; /* buffer for block */ |
| 76 | int error; /* error return value */ |
| 77 | int i; /* loop index */ |
| 78 | xfs_alloc_key_t key; /* kp points here if block is level 0 */ |
| 79 | xfs_agblock_t lbno; /* left block's block number */ |
| 80 | xfs_buf_t *lbp; /* left block's buffer pointer */ |
| 81 | xfs_alloc_block_t *left; /* left btree block */ |
| 82 | xfs_alloc_key_t *lkp=NULL; /* left block key pointer */ |
| 83 | xfs_alloc_ptr_t *lpp=NULL; /* left block address pointer */ |
| 84 | int lrecs=0; /* number of records in left block */ |
| 85 | xfs_alloc_rec_t *lrp; /* left block record pointer */ |
| 86 | xfs_mount_t *mp; /* mount structure */ |
| 87 | int ptr; /* index in btree block for this rec */ |
| 88 | xfs_agblock_t rbno; /* right block's block number */ |
| 89 | xfs_buf_t *rbp; /* right block's buffer pointer */ |
| 90 | xfs_alloc_block_t *right; /* right btree block */ |
| 91 | xfs_alloc_key_t *rkp; /* right block key pointer */ |
| 92 | xfs_alloc_ptr_t *rpp; /* right block address pointer */ |
| 93 | int rrecs=0; /* number of records in right block */ |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 94 | int numrecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | xfs_alloc_rec_t *rrp; /* right block record pointer */ |
| 96 | xfs_btree_cur_t *tcur; /* temporary btree cursor */ |
| 97 | |
| 98 | /* |
| 99 | * Get the index of the entry being deleted, check for nothing there. |
| 100 | */ |
| 101 | ptr = cur->bc_ptrs[level]; |
| 102 | if (ptr == 0) { |
| 103 | *stat = 0; |
| 104 | return 0; |
| 105 | } |
| 106 | /* |
| 107 | * Get the buffer & block containing the record or key/ptr. |
| 108 | */ |
| 109 | bp = cur->bc_bufs[level]; |
| 110 | block = XFS_BUF_TO_ALLOC_BLOCK(bp); |
| 111 | #ifdef DEBUG |
| 112 | if ((error = xfs_btree_check_sblock(cur, block, level, bp))) |
| 113 | return error; |
| 114 | #endif |
| 115 | /* |
| 116 | * Fail if we're off the end of the block. |
| 117 | */ |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 118 | numrecs = be16_to_cpu(block->bb_numrecs); |
| 119 | if (ptr > numrecs) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | *stat = 0; |
| 121 | return 0; |
| 122 | } |
| 123 | XFS_STATS_INC(xs_abt_delrec); |
| 124 | /* |
| 125 | * It's a nonleaf. Excise the key and ptr being deleted, by |
| 126 | * sliding the entries past them down one. |
| 127 | * Log the changed areas of the block. |
| 128 | */ |
| 129 | if (level > 0) { |
| 130 | lkp = XFS_ALLOC_KEY_ADDR(block, 1, cur); |
| 131 | lpp = XFS_ALLOC_PTR_ADDR(block, 1, cur); |
| 132 | #ifdef DEBUG |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 133 | for (i = ptr; i < numrecs; i++) { |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 134 | if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | return error; |
| 136 | } |
| 137 | #endif |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 138 | if (ptr < numrecs) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | memmove(&lkp[ptr - 1], &lkp[ptr], |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 140 | (numrecs - ptr) * sizeof(*lkp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | memmove(&lpp[ptr - 1], &lpp[ptr], |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 142 | (numrecs - ptr) * sizeof(*lpp)); |
| 143 | xfs_alloc_log_ptrs(cur, bp, ptr, numrecs - 1); |
| 144 | xfs_alloc_log_keys(cur, bp, ptr, numrecs - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | /* |
| 148 | * It's a leaf. Excise the record being deleted, by sliding the |
| 149 | * entries past it down one. Log the changed areas of the block. |
| 150 | */ |
| 151 | else { |
| 152 | lrp = XFS_ALLOC_REC_ADDR(block, 1, cur); |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 153 | if (ptr < numrecs) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | memmove(&lrp[ptr - 1], &lrp[ptr], |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 155 | (numrecs - ptr) * sizeof(*lrp)); |
| 156 | xfs_alloc_log_recs(cur, bp, ptr, numrecs - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | /* |
| 159 | * If it's the first record in the block, we'll need a key |
| 160 | * structure to pass up to the next level (updkey). |
| 161 | */ |
| 162 | if (ptr == 1) { |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 163 | key.ar_startblock = lrp->ar_startblock; |
| 164 | key.ar_blockcount = lrp->ar_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | lkp = &key; |
| 166 | } |
| 167 | } |
| 168 | /* |
| 169 | * Decrement and log the number of entries in the block. |
| 170 | */ |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 171 | numrecs--; |
| 172 | block->bb_numrecs = cpu_to_be16(numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS); |
| 174 | /* |
| 175 | * See if the longest free extent in the allocation group was |
| 176 | * changed by this operation. True if it's the by-size btree, and |
| 177 | * this is the leaf level, and there is no right sibling block, |
| 178 | * and this was the last record. |
| 179 | */ |
| 180 | agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp); |
| 181 | mp = cur->bc_mp; |
| 182 | |
| 183 | if (level == 0 && |
| 184 | cur->bc_btnum == XFS_BTNUM_CNT && |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 185 | be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK && |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 186 | ptr > numrecs) { |
| 187 | ASSERT(ptr == numrecs + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | /* |
| 189 | * There are still records in the block. Grab the size |
| 190 | * from the last one. |
| 191 | */ |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 192 | if (numrecs) { |
| 193 | rrp = XFS_ALLOC_REC_ADDR(block, numrecs, cur); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 194 | agf->agf_longest = rrp->ar_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | /* |
| 197 | * No free extents left. |
| 198 | */ |
| 199 | else |
| 200 | agf->agf_longest = 0; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 201 | mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest = |
| 202 | be32_to_cpu(agf->agf_longest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, |
| 204 | XFS_AGF_LONGEST); |
| 205 | } |
| 206 | /* |
| 207 | * Is this the root level? If so, we're almost done. |
| 208 | */ |
| 209 | if (level == cur->bc_nlevels - 1) { |
| 210 | /* |
| 211 | * If this is the root level, |
| 212 | * and there's only one entry left, |
| 213 | * and it's NOT the leaf level, |
| 214 | * then we can get rid of this level. |
| 215 | */ |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 216 | if (numrecs == 1 && level > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | /* |
| 218 | * lpp is still set to the first pointer in the block. |
| 219 | * Make it the new root of the btree. |
| 220 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 221 | bno = be32_to_cpu(agf->agf_roots[cur->bc_btnum]); |
| 222 | agf->agf_roots[cur->bc_btnum] = *lpp; |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 223 | be32_add_cpu(&agf->agf_levels[cur->bc_btnum], -1); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 224 | mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_levels[cur->bc_btnum]--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | /* |
| 226 | * Put this buffer/block on the ag's freelist. |
| 227 | */ |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 228 | error = xfs_alloc_put_freelist(cur->bc_tp, |
| 229 | cur->bc_private.a.agbp, NULL, bno, 1); |
| 230 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | return error; |
| 232 | /* |
| 233 | * Since blocks move to the free list without the |
| 234 | * coordination used in xfs_bmap_finish, we can't allow |
| 235 | * block to be available for reallocation and |
| 236 | * non-transaction writing (user data) until we know |
| 237 | * that the transaction that moved it to the free list |
| 238 | * is permanently on disk. We track the blocks by |
| 239 | * declaring these blocks as "busy"; the busy list is |
| 240 | * maintained on a per-ag basis and each transaction |
| 241 | * records which entries should be removed when the |
| 242 | * iclog commits to disk. If a busy block is |
| 243 | * allocated, the iclog is pushed up to the LSN |
| 244 | * that freed the block. |
| 245 | */ |
| 246 | xfs_alloc_mark_busy(cur->bc_tp, |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 247 | be32_to_cpu(agf->agf_seqno), bno, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | xfs_trans_agbtree_delta(cur->bc_tp, -1); |
| 250 | xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, |
| 251 | XFS_AGF_ROOTS | XFS_AGF_LEVELS); |
| 252 | /* |
| 253 | * Update the cursor so there's one fewer level. |
| 254 | */ |
| 255 | xfs_btree_setbuf(cur, level, NULL); |
| 256 | cur->bc_nlevels--; |
| 257 | } else if (level > 0 && |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 258 | (error = xfs_btree_decrement(cur, level, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | return error; |
| 260 | *stat = 1; |
| 261 | return 0; |
| 262 | } |
| 263 | /* |
| 264 | * If we deleted the leftmost entry in the block, update the |
| 265 | * key values above us in the tree. |
| 266 | */ |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 267 | if (ptr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)lkp, level + 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | return error; |
| 269 | /* |
| 270 | * If the number of records remaining in the block is at least |
| 271 | * the minimum, we're done. |
| 272 | */ |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 273 | if (numrecs >= XFS_ALLOC_BLOCK_MINRECS(level, cur)) { |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 274 | if (level > 0 && (error = xfs_btree_decrement(cur, level, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | return error; |
| 276 | *stat = 1; |
| 277 | return 0; |
| 278 | } |
| 279 | /* |
| 280 | * Otherwise, we have to move some records around to keep the |
| 281 | * tree balanced. Look at the left and right sibling blocks to |
| 282 | * see if we can re-balance by moving only one record. |
| 283 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 284 | rbno = be32_to_cpu(block->bb_rightsib); |
| 285 | lbno = be32_to_cpu(block->bb_leftsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | bno = NULLAGBLOCK; |
| 287 | ASSERT(rbno != NULLAGBLOCK || lbno != NULLAGBLOCK); |
| 288 | /* |
| 289 | * Duplicate the cursor so our btree manipulations here won't |
| 290 | * disrupt the next level up. |
| 291 | */ |
| 292 | if ((error = xfs_btree_dup_cursor(cur, &tcur))) |
| 293 | return error; |
| 294 | /* |
| 295 | * If there's a right sibling, see if it's ok to shift an entry |
| 296 | * out of it. |
| 297 | */ |
| 298 | if (rbno != NULLAGBLOCK) { |
| 299 | /* |
| 300 | * Move the temp cursor to the last entry in the next block. |
| 301 | * Actually any entry but the first would suffice. |
| 302 | */ |
| 303 | i = xfs_btree_lastrec(tcur, level); |
| 304 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 305 | if ((error = xfs_btree_increment(tcur, level, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | goto error0; |
| 307 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 308 | i = xfs_btree_lastrec(tcur, level); |
| 309 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 310 | /* |
| 311 | * Grab a pointer to the block. |
| 312 | */ |
| 313 | rbp = tcur->bc_bufs[level]; |
| 314 | right = XFS_BUF_TO_ALLOC_BLOCK(rbp); |
| 315 | #ifdef DEBUG |
| 316 | if ((error = xfs_btree_check_sblock(cur, right, level, rbp))) |
| 317 | goto error0; |
| 318 | #endif |
| 319 | /* |
| 320 | * Grab the current block number, for future use. |
| 321 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 322 | bno = be32_to_cpu(right->bb_leftsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | /* |
| 324 | * If right block is full enough so that removing one entry |
| 325 | * won't make it too empty, and left-shifting an entry out |
| 326 | * of right to us works, we're done. |
| 327 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 328 | if (be16_to_cpu(right->bb_numrecs) - 1 >= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | XFS_ALLOC_BLOCK_MINRECS(level, cur)) { |
| 330 | if ((error = xfs_alloc_lshift(tcur, level, &i))) |
| 331 | goto error0; |
| 332 | if (i) { |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 333 | ASSERT(be16_to_cpu(block->bb_numrecs) >= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | XFS_ALLOC_BLOCK_MINRECS(level, cur)); |
| 335 | xfs_btree_del_cursor(tcur, |
| 336 | XFS_BTREE_NOERROR); |
| 337 | if (level > 0 && |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 338 | (error = xfs_btree_decrement(cur, level, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | &i))) |
| 340 | return error; |
| 341 | *stat = 1; |
| 342 | return 0; |
| 343 | } |
| 344 | } |
| 345 | /* |
| 346 | * Otherwise, grab the number of records in right for |
| 347 | * future reference, and fix up the temp cursor to point |
| 348 | * to our block again (last record). |
| 349 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 350 | rrecs = be16_to_cpu(right->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | if (lbno != NULLAGBLOCK) { |
| 352 | i = xfs_btree_firstrec(tcur, level); |
| 353 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 354 | if ((error = xfs_btree_decrement(tcur, level, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | goto error0; |
| 356 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 357 | } |
| 358 | } |
| 359 | /* |
| 360 | * If there's a left sibling, see if it's ok to shift an entry |
| 361 | * out of it. |
| 362 | */ |
| 363 | if (lbno != NULLAGBLOCK) { |
| 364 | /* |
| 365 | * Move the temp cursor to the first entry in the |
| 366 | * previous block. |
| 367 | */ |
| 368 | i = xfs_btree_firstrec(tcur, level); |
| 369 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 370 | if ((error = xfs_btree_decrement(tcur, level, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | goto error0; |
| 372 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 373 | xfs_btree_firstrec(tcur, level); |
| 374 | /* |
| 375 | * Grab a pointer to the block. |
| 376 | */ |
| 377 | lbp = tcur->bc_bufs[level]; |
| 378 | left = XFS_BUF_TO_ALLOC_BLOCK(lbp); |
| 379 | #ifdef DEBUG |
| 380 | if ((error = xfs_btree_check_sblock(cur, left, level, lbp))) |
| 381 | goto error0; |
| 382 | #endif |
| 383 | /* |
| 384 | * Grab the current block number, for future use. |
| 385 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 386 | bno = be32_to_cpu(left->bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | /* |
| 388 | * If left block is full enough so that removing one entry |
| 389 | * won't make it too empty, and right-shifting an entry out |
| 390 | * of left to us works, we're done. |
| 391 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 392 | if (be16_to_cpu(left->bb_numrecs) - 1 >= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | XFS_ALLOC_BLOCK_MINRECS(level, cur)) { |
| 394 | if ((error = xfs_alloc_rshift(tcur, level, &i))) |
| 395 | goto error0; |
| 396 | if (i) { |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 397 | ASSERT(be16_to_cpu(block->bb_numrecs) >= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | XFS_ALLOC_BLOCK_MINRECS(level, cur)); |
| 399 | xfs_btree_del_cursor(tcur, |
| 400 | XFS_BTREE_NOERROR); |
| 401 | if (level == 0) |
| 402 | cur->bc_ptrs[0]++; |
| 403 | *stat = 1; |
| 404 | return 0; |
| 405 | } |
| 406 | } |
| 407 | /* |
| 408 | * Otherwise, grab the number of records in right for |
| 409 | * future reference. |
| 410 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 411 | lrecs = be16_to_cpu(left->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } |
| 413 | /* |
| 414 | * Delete the temp cursor, we're done with it. |
| 415 | */ |
| 416 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
| 417 | /* |
| 418 | * If here, we need to do a join to keep the tree balanced. |
| 419 | */ |
| 420 | ASSERT(bno != NULLAGBLOCK); |
| 421 | /* |
| 422 | * See if we can join with the left neighbor block. |
| 423 | */ |
| 424 | if (lbno != NULLAGBLOCK && |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 425 | lrecs + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | /* |
| 427 | * Set "right" to be the starting block, |
| 428 | * "left" to be the left neighbor. |
| 429 | */ |
| 430 | rbno = bno; |
| 431 | right = block; |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 432 | rrecs = be16_to_cpu(right->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | rbp = bp; |
| 434 | if ((error = xfs_btree_read_bufs(mp, cur->bc_tp, |
| 435 | cur->bc_private.a.agno, lbno, 0, &lbp, |
| 436 | XFS_ALLOC_BTREE_REF))) |
| 437 | return error; |
| 438 | left = XFS_BUF_TO_ALLOC_BLOCK(lbp); |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 439 | lrecs = be16_to_cpu(left->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | if ((error = xfs_btree_check_sblock(cur, left, level, lbp))) |
| 441 | return error; |
| 442 | } |
| 443 | /* |
| 444 | * If that won't work, see if we can join with the right neighbor block. |
| 445 | */ |
| 446 | else if (rbno != NULLAGBLOCK && |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 447 | rrecs + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | /* |
| 449 | * Set "left" to be the starting block, |
| 450 | * "right" to be the right neighbor. |
| 451 | */ |
| 452 | lbno = bno; |
| 453 | left = block; |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 454 | lrecs = be16_to_cpu(left->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | lbp = bp; |
| 456 | if ((error = xfs_btree_read_bufs(mp, cur->bc_tp, |
| 457 | cur->bc_private.a.agno, rbno, 0, &rbp, |
| 458 | XFS_ALLOC_BTREE_REF))) |
| 459 | return error; |
| 460 | right = XFS_BUF_TO_ALLOC_BLOCK(rbp); |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 461 | rrecs = be16_to_cpu(right->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | if ((error = xfs_btree_check_sblock(cur, right, level, rbp))) |
| 463 | return error; |
| 464 | } |
| 465 | /* |
| 466 | * Otherwise, we can't fix the imbalance. |
| 467 | * Just return. This is probably a logic error, but it's not fatal. |
| 468 | */ |
| 469 | else { |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 470 | if (level > 0 && (error = xfs_btree_decrement(cur, level, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | return error; |
| 472 | *stat = 1; |
| 473 | return 0; |
| 474 | } |
| 475 | /* |
| 476 | * We're now going to join "left" and "right" by moving all the stuff |
| 477 | * in "right" to "left" and deleting "right". |
| 478 | */ |
| 479 | if (level > 0) { |
| 480 | /* |
| 481 | * It's a non-leaf. Move keys and pointers. |
| 482 | */ |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 483 | lkp = XFS_ALLOC_KEY_ADDR(left, lrecs + 1, cur); |
| 484 | lpp = XFS_ALLOC_PTR_ADDR(left, lrecs + 1, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur); |
| 486 | rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur); |
| 487 | #ifdef DEBUG |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 488 | for (i = 0; i < rrecs; i++) { |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 489 | if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | return error; |
| 491 | } |
| 492 | #endif |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 493 | memcpy(lkp, rkp, rrecs * sizeof(*lkp)); |
| 494 | memcpy(lpp, rpp, rrecs * sizeof(*lpp)); |
| 495 | xfs_alloc_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs); |
| 496 | xfs_alloc_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } else { |
| 498 | /* |
| 499 | * It's a leaf. Move records. |
| 500 | */ |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 501 | lrp = XFS_ALLOC_REC_ADDR(left, lrecs + 1, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | rrp = XFS_ALLOC_REC_ADDR(right, 1, cur); |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 503 | memcpy(lrp, rrp, rrecs * sizeof(*lrp)); |
| 504 | xfs_alloc_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | /* |
| 507 | * If we joined with the left neighbor, set the buffer in the |
| 508 | * cursor to the left block, and fix up the index. |
| 509 | */ |
| 510 | if (bp != lbp) { |
| 511 | xfs_btree_setbuf(cur, level, lbp); |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 512 | cur->bc_ptrs[level] += lrecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | } |
| 514 | /* |
| 515 | * If we joined with the right neighbor and there's a level above |
| 516 | * us, increment the cursor at that level. |
| 517 | */ |
| 518 | else if (level + 1 < cur->bc_nlevels && |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 519 | (error = xfs_btree_increment(cur, level + 1, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | return error; |
| 521 | /* |
| 522 | * Fix up the number of records in the surviving block. |
| 523 | */ |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 524 | lrecs += rrecs; |
| 525 | left->bb_numrecs = cpu_to_be16(lrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | /* |
| 527 | * Fix up the right block pointer in the surviving block, and log it. |
| 528 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 529 | left->bb_rightsib = right->bb_rightsib; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB); |
| 531 | /* |
| 532 | * If there is a right sibling now, make it point to the |
| 533 | * remaining block. |
| 534 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 535 | if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | xfs_alloc_block_t *rrblock; |
| 537 | xfs_buf_t *rrbp; |
| 538 | |
| 539 | if ((error = xfs_btree_read_bufs(mp, cur->bc_tp, |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 540 | cur->bc_private.a.agno, be32_to_cpu(left->bb_rightsib), 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | &rrbp, XFS_ALLOC_BTREE_REF))) |
| 542 | return error; |
| 543 | rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp); |
| 544 | if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp))) |
| 545 | return error; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 546 | rrblock->bb_leftsib = cpu_to_be32(lbno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB); |
| 548 | } |
| 549 | /* |
| 550 | * Free the deleting block by putting it on the freelist. |
| 551 | */ |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 552 | error = xfs_alloc_put_freelist(cur->bc_tp, |
| 553 | cur->bc_private.a.agbp, NULL, rbno, 1); |
| 554 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | return error; |
| 556 | /* |
| 557 | * Since blocks move to the free list without the coordination |
| 558 | * used in xfs_bmap_finish, we can't allow block to be available |
| 559 | * for reallocation and non-transaction writing (user data) |
| 560 | * until we know that the transaction that moved it to the free |
| 561 | * list is permanently on disk. We track the blocks by declaring |
| 562 | * these blocks as "busy"; the busy list is maintained on a |
| 563 | * per-ag basis and each transaction records which entries |
| 564 | * should be removed when the iclog commits to disk. If a |
| 565 | * busy block is allocated, the iclog is pushed up to the |
| 566 | * LSN that freed the block. |
| 567 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 568 | xfs_alloc_mark_busy(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | xfs_trans_agbtree_delta(cur->bc_tp, -1); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | /* |
| 572 | * Adjust the current level's cursor so that we're left referring |
| 573 | * to the right node, after we're done. |
| 574 | * If this leaves the ptr value 0 our caller will fix it up. |
| 575 | */ |
| 576 | if (level > 0) |
| 577 | cur->bc_ptrs[level]--; |
| 578 | /* |
| 579 | * Return value means the next level up has something to do. |
| 580 | */ |
| 581 | *stat = 2; |
| 582 | return 0; |
| 583 | |
| 584 | error0: |
| 585 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); |
| 586 | return error; |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Insert one record/level. Return information to the caller |
| 591 | * allowing the next level up to proceed if necessary. |
| 592 | */ |
| 593 | STATIC int /* error */ |
| 594 | xfs_alloc_insrec( |
| 595 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 596 | int level, /* level to insert record at */ |
| 597 | xfs_agblock_t *bnop, /* i/o: block number inserted */ |
| 598 | xfs_alloc_rec_t *recp, /* i/o: record data inserted */ |
| 599 | xfs_btree_cur_t **curp, /* output: new cursor replacing cur */ |
| 600 | int *stat) /* output: success/failure */ |
| 601 | { |
| 602 | xfs_agf_t *agf; /* allocation group freelist header */ |
| 603 | xfs_alloc_block_t *block; /* btree block record/key lives in */ |
| 604 | xfs_buf_t *bp; /* buffer for block */ |
| 605 | int error; /* error return value */ |
| 606 | int i; /* loop index */ |
| 607 | xfs_alloc_key_t key; /* key value being inserted */ |
| 608 | xfs_alloc_key_t *kp; /* pointer to btree keys */ |
| 609 | xfs_agblock_t nbno; /* block number of allocated block */ |
| 610 | xfs_btree_cur_t *ncur; /* new cursor to be used at next lvl */ |
| 611 | xfs_alloc_key_t nkey; /* new key value, from split */ |
| 612 | xfs_alloc_rec_t nrec; /* new record value, for caller */ |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 613 | int numrecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | int optr; /* old ptr value */ |
| 615 | xfs_alloc_ptr_t *pp; /* pointer to btree addresses */ |
| 616 | int ptr; /* index in btree block for this rec */ |
| 617 | xfs_alloc_rec_t *rp; /* pointer to btree records */ |
| 618 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 619 | ASSERT(be32_to_cpu(recp->ar_blockcount) > 0); |
Christoph Hellwig | 5bde1ba9 | 2005-11-02 15:06:18 +1100 | [diff] [blame] | 620 | |
| 621 | /* |
| 622 | * GCC doesn't understand the (arguably complex) control flow in |
| 623 | * this function and complains about uninitialized structure fields |
| 624 | * without this. |
| 625 | */ |
| 626 | memset(&nrec, 0, sizeof(nrec)); |
| 627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | /* |
| 629 | * If we made it to the root level, allocate a new root block |
| 630 | * and we're done. |
| 631 | */ |
| 632 | if (level >= cur->bc_nlevels) { |
| 633 | XFS_STATS_INC(xs_abt_insrec); |
| 634 | if ((error = xfs_alloc_newroot(cur, &i))) |
| 635 | return error; |
| 636 | *bnop = NULLAGBLOCK; |
| 637 | *stat = i; |
| 638 | return 0; |
| 639 | } |
| 640 | /* |
| 641 | * Make a key out of the record data to be inserted, and save it. |
| 642 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 643 | key.ar_startblock = recp->ar_startblock; |
| 644 | key.ar_blockcount = recp->ar_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | optr = ptr = cur->bc_ptrs[level]; |
| 646 | /* |
| 647 | * If we're off the left edge, return failure. |
| 648 | */ |
| 649 | if (ptr == 0) { |
| 650 | *stat = 0; |
| 651 | return 0; |
| 652 | } |
| 653 | XFS_STATS_INC(xs_abt_insrec); |
| 654 | /* |
| 655 | * Get pointers to the btree buffer and block. |
| 656 | */ |
| 657 | bp = cur->bc_bufs[level]; |
| 658 | block = XFS_BUF_TO_ALLOC_BLOCK(bp); |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 659 | numrecs = be16_to_cpu(block->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | #ifdef DEBUG |
| 661 | if ((error = xfs_btree_check_sblock(cur, block, level, bp))) |
| 662 | return error; |
| 663 | /* |
| 664 | * Check that the new entry is being inserted in the right place. |
| 665 | */ |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 666 | if (ptr <= numrecs) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | if (level == 0) { |
| 668 | rp = XFS_ALLOC_REC_ADDR(block, ptr, cur); |
| 669 | xfs_btree_check_rec(cur->bc_btnum, recp, rp); |
| 670 | } else { |
| 671 | kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur); |
| 672 | xfs_btree_check_key(cur->bc_btnum, &key, kp); |
| 673 | } |
| 674 | } |
| 675 | #endif |
| 676 | nbno = NULLAGBLOCK; |
Nathan Scott | 1121b21 | 2006-09-28 10:58:40 +1000 | [diff] [blame] | 677 | ncur = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | /* |
| 679 | * If the block is full, we can't insert the new entry until we |
| 680 | * make the block un-full. |
| 681 | */ |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 682 | if (numrecs == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | /* |
| 684 | * First, try shifting an entry to the right neighbor. |
| 685 | */ |
| 686 | if ((error = xfs_alloc_rshift(cur, level, &i))) |
| 687 | return error; |
| 688 | if (i) { |
| 689 | /* nothing */ |
| 690 | } |
| 691 | /* |
| 692 | * Next, try shifting an entry to the left neighbor. |
| 693 | */ |
| 694 | else { |
| 695 | if ((error = xfs_alloc_lshift(cur, level, &i))) |
| 696 | return error; |
| 697 | if (i) |
| 698 | optr = ptr = cur->bc_ptrs[level]; |
| 699 | else { |
| 700 | /* |
| 701 | * Next, try splitting the current block in |
| 702 | * half. If this works we have to re-set our |
| 703 | * variables because we could be in a |
| 704 | * different block now. |
| 705 | */ |
| 706 | if ((error = xfs_alloc_split(cur, level, &nbno, |
| 707 | &nkey, &ncur, &i))) |
| 708 | return error; |
| 709 | if (i) { |
| 710 | bp = cur->bc_bufs[level]; |
| 711 | block = XFS_BUF_TO_ALLOC_BLOCK(bp); |
| 712 | #ifdef DEBUG |
| 713 | if ((error = |
| 714 | xfs_btree_check_sblock(cur, |
| 715 | block, level, bp))) |
| 716 | return error; |
| 717 | #endif |
| 718 | ptr = cur->bc_ptrs[level]; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 719 | nrec.ar_startblock = nkey.ar_startblock; |
| 720 | nrec.ar_blockcount = nkey.ar_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | } |
| 722 | /* |
| 723 | * Otherwise the insert fails. |
| 724 | */ |
| 725 | else { |
| 726 | *stat = 0; |
| 727 | return 0; |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | /* |
| 733 | * At this point we know there's room for our new entry in the block |
| 734 | * we're pointing at. |
| 735 | */ |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 736 | numrecs = be16_to_cpu(block->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | if (level > 0) { |
| 738 | /* |
| 739 | * It's a non-leaf entry. Make a hole for the new data |
| 740 | * in the key and ptr regions of the block. |
| 741 | */ |
| 742 | kp = XFS_ALLOC_KEY_ADDR(block, 1, cur); |
| 743 | pp = XFS_ALLOC_PTR_ADDR(block, 1, cur); |
| 744 | #ifdef DEBUG |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 745 | for (i = numrecs; i >= ptr; i--) { |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 746 | if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(pp[i - 1]), level))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | return error; |
| 748 | } |
| 749 | #endif |
| 750 | memmove(&kp[ptr], &kp[ptr - 1], |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 751 | (numrecs - ptr + 1) * sizeof(*kp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | memmove(&pp[ptr], &pp[ptr - 1], |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 753 | (numrecs - ptr + 1) * sizeof(*pp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | #ifdef DEBUG |
| 755 | if ((error = xfs_btree_check_sptr(cur, *bnop, level))) |
| 756 | return error; |
| 757 | #endif |
| 758 | /* |
| 759 | * Now stuff the new data in, bump numrecs and log the new data. |
| 760 | */ |
| 761 | kp[ptr - 1] = key; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 762 | pp[ptr - 1] = cpu_to_be32(*bnop); |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 763 | numrecs++; |
| 764 | block->bb_numrecs = cpu_to_be16(numrecs); |
| 765 | xfs_alloc_log_keys(cur, bp, ptr, numrecs); |
| 766 | xfs_alloc_log_ptrs(cur, bp, ptr, numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | #ifdef DEBUG |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 768 | if (ptr < numrecs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | xfs_btree_check_key(cur->bc_btnum, kp + ptr - 1, |
| 770 | kp + ptr); |
| 771 | #endif |
| 772 | } else { |
| 773 | /* |
| 774 | * It's a leaf entry. Make a hole for the new record. |
| 775 | */ |
| 776 | rp = XFS_ALLOC_REC_ADDR(block, 1, cur); |
| 777 | memmove(&rp[ptr], &rp[ptr - 1], |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 778 | (numrecs - ptr + 1) * sizeof(*rp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | /* |
| 780 | * Now stuff the new record in, bump numrecs |
| 781 | * and log the new data. |
| 782 | */ |
Christoph Hellwig | c38e5e8 | 2006-09-28 10:57:17 +1000 | [diff] [blame] | 783 | rp[ptr - 1] = *recp; |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 784 | numrecs++; |
| 785 | block->bb_numrecs = cpu_to_be16(numrecs); |
| 786 | xfs_alloc_log_recs(cur, bp, ptr, numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | #ifdef DEBUG |
Eric Sandeen | 91d8723 | 2006-09-28 11:05:40 +1000 | [diff] [blame] | 788 | if (ptr < numrecs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | xfs_btree_check_rec(cur->bc_btnum, rp + ptr - 1, |
| 790 | rp + ptr); |
| 791 | #endif |
| 792 | } |
| 793 | /* |
| 794 | * Log the new number of records in the btree header. |
| 795 | */ |
| 796 | xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS); |
| 797 | /* |
| 798 | * If we inserted at the start of a block, update the parents' keys. |
| 799 | */ |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 800 | if (optr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)&key, level + 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | return error; |
| 802 | /* |
| 803 | * Look to see if the longest extent in the allocation group |
| 804 | * needs to be updated. |
| 805 | */ |
| 806 | |
| 807 | agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp); |
| 808 | if (level == 0 && |
| 809 | cur->bc_btnum == XFS_BTNUM_CNT && |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 810 | be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK && |
| 811 | be32_to_cpu(recp->ar_blockcount) > be32_to_cpu(agf->agf_longest)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | /* |
| 813 | * If this is a leaf in the by-size btree and there |
| 814 | * is no right sibling block and this block is bigger |
| 815 | * than the previous longest block, update it. |
| 816 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 817 | agf->agf_longest = recp->ar_blockcount; |
| 818 | cur->bc_mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest |
| 819 | = be32_to_cpu(recp->ar_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, |
| 821 | XFS_AGF_LONGEST); |
| 822 | } |
| 823 | /* |
| 824 | * Return the new block number, if any. |
| 825 | * If there is one, give back a record value and a cursor too. |
| 826 | */ |
| 827 | *bnop = nbno; |
| 828 | if (nbno != NULLAGBLOCK) { |
Christoph Hellwig | c38e5e8 | 2006-09-28 10:57:17 +1000 | [diff] [blame] | 829 | *recp = nrec; |
| 830 | *curp = ncur; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | } |
| 832 | *stat = 1; |
| 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | /* |
| 837 | * Log header fields from a btree block. |
| 838 | */ |
| 839 | STATIC void |
| 840 | xfs_alloc_log_block( |
| 841 | xfs_trans_t *tp, /* transaction pointer */ |
| 842 | xfs_buf_t *bp, /* buffer containing btree block */ |
| 843 | int fields) /* mask of fields: XFS_BB_... */ |
| 844 | { |
| 845 | int first; /* first byte offset logged */ |
| 846 | int last; /* last byte offset logged */ |
| 847 | static const short offsets[] = { /* table of offsets */ |
| 848 | offsetof(xfs_alloc_block_t, bb_magic), |
| 849 | offsetof(xfs_alloc_block_t, bb_level), |
| 850 | offsetof(xfs_alloc_block_t, bb_numrecs), |
| 851 | offsetof(xfs_alloc_block_t, bb_leftsib), |
| 852 | offsetof(xfs_alloc_block_t, bb_rightsib), |
| 853 | sizeof(xfs_alloc_block_t) |
| 854 | }; |
| 855 | |
| 856 | xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last); |
| 857 | xfs_trans_log_buf(tp, bp, first, last); |
| 858 | } |
| 859 | |
| 860 | /* |
| 861 | * Log keys from a btree block (nonleaf). |
| 862 | */ |
| 863 | STATIC void |
| 864 | xfs_alloc_log_keys( |
| 865 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 866 | xfs_buf_t *bp, /* buffer containing btree block */ |
| 867 | int kfirst, /* index of first key to log */ |
| 868 | int klast) /* index of last key to log */ |
| 869 | { |
| 870 | xfs_alloc_block_t *block; /* btree block to log from */ |
| 871 | int first; /* first byte offset logged */ |
| 872 | xfs_alloc_key_t *kp; /* key pointer in btree block */ |
| 873 | int last; /* last byte offset logged */ |
| 874 | |
| 875 | block = XFS_BUF_TO_ALLOC_BLOCK(bp); |
| 876 | kp = XFS_ALLOC_KEY_ADDR(block, 1, cur); |
| 877 | first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block); |
| 878 | last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block); |
| 879 | xfs_trans_log_buf(cur->bc_tp, bp, first, last); |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | * Log block pointer fields from a btree block (nonleaf). |
| 884 | */ |
| 885 | STATIC void |
| 886 | xfs_alloc_log_ptrs( |
| 887 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 888 | xfs_buf_t *bp, /* buffer containing btree block */ |
| 889 | int pfirst, /* index of first pointer to log */ |
| 890 | int plast) /* index of last pointer to log */ |
| 891 | { |
| 892 | xfs_alloc_block_t *block; /* btree block to log from */ |
| 893 | int first; /* first byte offset logged */ |
| 894 | int last; /* last byte offset logged */ |
| 895 | xfs_alloc_ptr_t *pp; /* block-pointer pointer in btree blk */ |
| 896 | |
| 897 | block = XFS_BUF_TO_ALLOC_BLOCK(bp); |
| 898 | pp = XFS_ALLOC_PTR_ADDR(block, 1, cur); |
| 899 | first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block); |
| 900 | last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block); |
| 901 | xfs_trans_log_buf(cur->bc_tp, bp, first, last); |
| 902 | } |
| 903 | |
| 904 | /* |
| 905 | * Log records from a btree block (leaf). |
| 906 | */ |
| 907 | STATIC void |
| 908 | xfs_alloc_log_recs( |
| 909 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 910 | xfs_buf_t *bp, /* buffer containing btree block */ |
| 911 | int rfirst, /* index of first record to log */ |
| 912 | int rlast) /* index of last record to log */ |
| 913 | { |
| 914 | xfs_alloc_block_t *block; /* btree block to log from */ |
| 915 | int first; /* first byte offset logged */ |
| 916 | int last; /* last byte offset logged */ |
| 917 | xfs_alloc_rec_t *rp; /* record pointer for btree block */ |
| 918 | |
| 919 | |
| 920 | block = XFS_BUF_TO_ALLOC_BLOCK(bp); |
| 921 | rp = XFS_ALLOC_REC_ADDR(block, 1, cur); |
| 922 | #ifdef DEBUG |
| 923 | { |
| 924 | xfs_agf_t *agf; |
| 925 | xfs_alloc_rec_t *p; |
| 926 | |
| 927 | agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp); |
| 928 | for (p = &rp[rfirst - 1]; p <= &rp[rlast - 1]; p++) |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 929 | ASSERT(be32_to_cpu(p->ar_startblock) + |
| 930 | be32_to_cpu(p->ar_blockcount) <= |
| 931 | be32_to_cpu(agf->agf_length)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | } |
| 933 | #endif |
| 934 | first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block); |
| 935 | last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block); |
| 936 | xfs_trans_log_buf(cur->bc_tp, bp, first, last); |
| 937 | } |
| 938 | |
| 939 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | * Move 1 record left from cur/level if possible. |
| 941 | * Update cur to reflect the new path. |
| 942 | */ |
| 943 | STATIC int /* error */ |
| 944 | xfs_alloc_lshift( |
| 945 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 946 | int level, /* level to shift record on */ |
| 947 | int *stat) /* success/failure */ |
| 948 | { |
| 949 | int error; /* error return value */ |
| 950 | #ifdef DEBUG |
| 951 | int i; /* loop index */ |
| 952 | #endif |
| 953 | xfs_alloc_key_t key; /* key value for leaf level upward */ |
| 954 | xfs_buf_t *lbp; /* buffer for left neighbor block */ |
| 955 | xfs_alloc_block_t *left; /* left neighbor btree block */ |
| 956 | int nrec; /* new number of left block entries */ |
| 957 | xfs_buf_t *rbp; /* buffer for right (current) block */ |
| 958 | xfs_alloc_block_t *right; /* right (current) btree block */ |
| 959 | xfs_alloc_key_t *rkp=NULL; /* key pointer for right block */ |
| 960 | xfs_alloc_ptr_t *rpp=NULL; /* address pointer for right block */ |
| 961 | xfs_alloc_rec_t *rrp=NULL; /* record pointer for right block */ |
| 962 | |
| 963 | /* |
| 964 | * Set up variables for this block as "right". |
| 965 | */ |
| 966 | rbp = cur->bc_bufs[level]; |
| 967 | right = XFS_BUF_TO_ALLOC_BLOCK(rbp); |
| 968 | #ifdef DEBUG |
| 969 | if ((error = xfs_btree_check_sblock(cur, right, level, rbp))) |
| 970 | return error; |
| 971 | #endif |
| 972 | /* |
| 973 | * If we've got no left sibling then we can't shift an entry left. |
| 974 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 975 | if (be32_to_cpu(right->bb_leftsib) == NULLAGBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | *stat = 0; |
| 977 | return 0; |
| 978 | } |
| 979 | /* |
| 980 | * If the cursor entry is the one that would be moved, don't |
| 981 | * do it... it's too complicated. |
| 982 | */ |
| 983 | if (cur->bc_ptrs[level] <= 1) { |
| 984 | *stat = 0; |
| 985 | return 0; |
| 986 | } |
| 987 | /* |
| 988 | * Set up the left neighbor as "left". |
| 989 | */ |
| 990 | if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp, |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 991 | cur->bc_private.a.agno, be32_to_cpu(right->bb_leftsib), |
| 992 | 0, &lbp, XFS_ALLOC_BTREE_REF))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | return error; |
| 994 | left = XFS_BUF_TO_ALLOC_BLOCK(lbp); |
| 995 | if ((error = xfs_btree_check_sblock(cur, left, level, lbp))) |
| 996 | return error; |
| 997 | /* |
| 998 | * If it's full, it can't take another entry. |
| 999 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1000 | if (be16_to_cpu(left->bb_numrecs) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | *stat = 0; |
| 1002 | return 0; |
| 1003 | } |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1004 | nrec = be16_to_cpu(left->bb_numrecs) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | /* |
| 1006 | * If non-leaf, copy a key and a ptr to the left block. |
| 1007 | */ |
| 1008 | if (level > 0) { |
| 1009 | xfs_alloc_key_t *lkp; /* key pointer for left block */ |
| 1010 | xfs_alloc_ptr_t *lpp; /* address pointer for left block */ |
| 1011 | |
| 1012 | lkp = XFS_ALLOC_KEY_ADDR(left, nrec, cur); |
| 1013 | rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur); |
| 1014 | *lkp = *rkp; |
| 1015 | xfs_alloc_log_keys(cur, lbp, nrec, nrec); |
| 1016 | lpp = XFS_ALLOC_PTR_ADDR(left, nrec, cur); |
| 1017 | rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur); |
| 1018 | #ifdef DEBUG |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1019 | if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(*rpp), level))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | return error; |
| 1021 | #endif |
Christoph Hellwig | c38e5e8 | 2006-09-28 10:57:17 +1000 | [diff] [blame] | 1022 | *lpp = *rpp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | xfs_alloc_log_ptrs(cur, lbp, nrec, nrec); |
| 1024 | xfs_btree_check_key(cur->bc_btnum, lkp - 1, lkp); |
| 1025 | } |
| 1026 | /* |
| 1027 | * If leaf, copy a record to the left block. |
| 1028 | */ |
| 1029 | else { |
| 1030 | xfs_alloc_rec_t *lrp; /* record pointer for left block */ |
| 1031 | |
| 1032 | lrp = XFS_ALLOC_REC_ADDR(left, nrec, cur); |
| 1033 | rrp = XFS_ALLOC_REC_ADDR(right, 1, cur); |
| 1034 | *lrp = *rrp; |
| 1035 | xfs_alloc_log_recs(cur, lbp, nrec, nrec); |
| 1036 | xfs_btree_check_rec(cur->bc_btnum, lrp - 1, lrp); |
| 1037 | } |
| 1038 | /* |
| 1039 | * Bump and log left's numrecs, decrement and log right's numrecs. |
| 1040 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1041 | be16_add_cpu(&left->bb_numrecs, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS); |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1043 | be16_add_cpu(&right->bb_numrecs, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS); |
| 1045 | /* |
| 1046 | * Slide the contents of right down one entry. |
| 1047 | */ |
| 1048 | if (level > 0) { |
| 1049 | #ifdef DEBUG |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1050 | for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) { |
| 1051 | if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i + 1]), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | level))) |
| 1053 | return error; |
| 1054 | } |
| 1055 | #endif |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1056 | memmove(rkp, rkp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp)); |
| 1057 | memmove(rpp, rpp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp)); |
| 1058 | xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs)); |
| 1059 | xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | } else { |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1061 | memmove(rrp, rrp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp)); |
| 1062 | xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs)); |
| 1063 | key.ar_startblock = rrp->ar_startblock; |
| 1064 | key.ar_blockcount = rrp->ar_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | rkp = &key; |
| 1066 | } |
| 1067 | /* |
| 1068 | * Update the parent key values of right. |
| 1069 | */ |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 1070 | if ((error = xfs_btree_updkey(cur, (union xfs_btree_key *)rkp, level + 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | return error; |
| 1072 | /* |
| 1073 | * Slide the cursor value left one. |
| 1074 | */ |
| 1075 | cur->bc_ptrs[level]--; |
| 1076 | *stat = 1; |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
| 1080 | /* |
| 1081 | * Allocate a new root block, fill it in. |
| 1082 | */ |
| 1083 | STATIC int /* error */ |
| 1084 | xfs_alloc_newroot( |
| 1085 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 1086 | int *stat) /* success/failure */ |
| 1087 | { |
| 1088 | int error; /* error return value */ |
| 1089 | xfs_agblock_t lbno; /* left block number */ |
| 1090 | xfs_buf_t *lbp; /* left btree buffer */ |
| 1091 | xfs_alloc_block_t *left; /* left btree block */ |
| 1092 | xfs_mount_t *mp; /* mount structure */ |
| 1093 | xfs_agblock_t nbno; /* new block number */ |
| 1094 | xfs_buf_t *nbp; /* new (root) buffer */ |
| 1095 | xfs_alloc_block_t *new; /* new (root) btree block */ |
| 1096 | int nptr; /* new value for key index, 1 or 2 */ |
| 1097 | xfs_agblock_t rbno; /* right block number */ |
| 1098 | xfs_buf_t *rbp; /* right btree buffer */ |
| 1099 | xfs_alloc_block_t *right; /* right btree block */ |
| 1100 | |
| 1101 | mp = cur->bc_mp; |
| 1102 | |
| 1103 | ASSERT(cur->bc_nlevels < XFS_AG_MAXLEVELS(mp)); |
| 1104 | /* |
| 1105 | * Get a buffer from the freelist blocks, for the new root. |
| 1106 | */ |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 1107 | error = xfs_alloc_get_freelist(cur->bc_tp, |
| 1108 | cur->bc_private.a.agbp, &nbno, 1); |
| 1109 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | return error; |
| 1111 | /* |
| 1112 | * None available, we fail. |
| 1113 | */ |
| 1114 | if (nbno == NULLAGBLOCK) { |
| 1115 | *stat = 0; |
| 1116 | return 0; |
| 1117 | } |
| 1118 | xfs_trans_agbtree_delta(cur->bc_tp, 1); |
| 1119 | nbp = xfs_btree_get_bufs(mp, cur->bc_tp, cur->bc_private.a.agno, nbno, |
| 1120 | 0); |
| 1121 | new = XFS_BUF_TO_ALLOC_BLOCK(nbp); |
| 1122 | /* |
| 1123 | * Set the root data in the a.g. freespace structure. |
| 1124 | */ |
| 1125 | { |
| 1126 | xfs_agf_t *agf; /* a.g. freespace header */ |
| 1127 | xfs_agnumber_t seqno; |
| 1128 | |
| 1129 | agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1130 | agf->agf_roots[cur->bc_btnum] = cpu_to_be32(nbno); |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1131 | be32_add_cpu(&agf->agf_levels[cur->bc_btnum], 1); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1132 | seqno = be32_to_cpu(agf->agf_seqno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | mp->m_perag[seqno].pagf_levels[cur->bc_btnum]++; |
| 1134 | xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, |
| 1135 | XFS_AGF_ROOTS | XFS_AGF_LEVELS); |
| 1136 | } |
| 1137 | /* |
| 1138 | * At the previous root level there are now two blocks: the old |
| 1139 | * root, and the new block generated when it was split. |
| 1140 | * We don't know which one the cursor is pointing at, so we |
| 1141 | * set up variables "left" and "right" for each case. |
| 1142 | */ |
| 1143 | lbp = cur->bc_bufs[cur->bc_nlevels - 1]; |
| 1144 | left = XFS_BUF_TO_ALLOC_BLOCK(lbp); |
| 1145 | #ifdef DEBUG |
| 1146 | if ((error = xfs_btree_check_sblock(cur, left, cur->bc_nlevels - 1, lbp))) |
| 1147 | return error; |
| 1148 | #endif |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1149 | if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | /* |
| 1151 | * Our block is left, pick up the right block. |
| 1152 | */ |
| 1153 | lbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(lbp)); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1154 | rbno = be32_to_cpu(left->bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | if ((error = xfs_btree_read_bufs(mp, cur->bc_tp, |
| 1156 | cur->bc_private.a.agno, rbno, 0, &rbp, |
| 1157 | XFS_ALLOC_BTREE_REF))) |
| 1158 | return error; |
| 1159 | right = XFS_BUF_TO_ALLOC_BLOCK(rbp); |
| 1160 | if ((error = xfs_btree_check_sblock(cur, right, |
| 1161 | cur->bc_nlevels - 1, rbp))) |
| 1162 | return error; |
| 1163 | nptr = 1; |
| 1164 | } else { |
| 1165 | /* |
| 1166 | * Our block is right, pick up the left block. |
| 1167 | */ |
| 1168 | rbp = lbp; |
| 1169 | right = left; |
| 1170 | rbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(rbp)); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1171 | lbno = be32_to_cpu(right->bb_leftsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | if ((error = xfs_btree_read_bufs(mp, cur->bc_tp, |
| 1173 | cur->bc_private.a.agno, lbno, 0, &lbp, |
| 1174 | XFS_ALLOC_BTREE_REF))) |
| 1175 | return error; |
| 1176 | left = XFS_BUF_TO_ALLOC_BLOCK(lbp); |
| 1177 | if ((error = xfs_btree_check_sblock(cur, left, |
| 1178 | cur->bc_nlevels - 1, lbp))) |
| 1179 | return error; |
| 1180 | nptr = 2; |
| 1181 | } |
| 1182 | /* |
| 1183 | * Fill in the new block's btree header and log it. |
| 1184 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1185 | new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]); |
| 1186 | new->bb_level = cpu_to_be16(cur->bc_nlevels); |
| 1187 | new->bb_numrecs = cpu_to_be16(2); |
| 1188 | new->bb_leftsib = cpu_to_be32(NULLAGBLOCK); |
| 1189 | new->bb_rightsib = cpu_to_be32(NULLAGBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | xfs_alloc_log_block(cur->bc_tp, nbp, XFS_BB_ALL_BITS); |
| 1191 | ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK); |
| 1192 | /* |
| 1193 | * Fill in the key data in the new root. |
| 1194 | */ |
| 1195 | { |
| 1196 | xfs_alloc_key_t *kp; /* btree key pointer */ |
| 1197 | |
| 1198 | kp = XFS_ALLOC_KEY_ADDR(new, 1, cur); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1199 | if (be16_to_cpu(left->bb_level) > 0) { |
Christoph Hellwig | c38e5e8 | 2006-09-28 10:57:17 +1000 | [diff] [blame] | 1200 | kp[0] = *XFS_ALLOC_KEY_ADDR(left, 1, cur); |
| 1201 | kp[1] = *XFS_ALLOC_KEY_ADDR(right, 1, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | } else { |
| 1203 | xfs_alloc_rec_t *rp; /* btree record pointer */ |
| 1204 | |
| 1205 | rp = XFS_ALLOC_REC_ADDR(left, 1, cur); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1206 | kp[0].ar_startblock = rp->ar_startblock; |
| 1207 | kp[0].ar_blockcount = rp->ar_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | rp = XFS_ALLOC_REC_ADDR(right, 1, cur); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1209 | kp[1].ar_startblock = rp->ar_startblock; |
| 1210 | kp[1].ar_blockcount = rp->ar_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | xfs_alloc_log_keys(cur, nbp, 1, 2); |
| 1214 | /* |
| 1215 | * Fill in the pointer data in the new root. |
| 1216 | */ |
| 1217 | { |
| 1218 | xfs_alloc_ptr_t *pp; /* btree address pointer */ |
| 1219 | |
| 1220 | pp = XFS_ALLOC_PTR_ADDR(new, 1, cur); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1221 | pp[0] = cpu_to_be32(lbno); |
| 1222 | pp[1] = cpu_to_be32(rbno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | } |
| 1224 | xfs_alloc_log_ptrs(cur, nbp, 1, 2); |
| 1225 | /* |
| 1226 | * Fix up the cursor. |
| 1227 | */ |
| 1228 | xfs_btree_setbuf(cur, cur->bc_nlevels, nbp); |
| 1229 | cur->bc_ptrs[cur->bc_nlevels] = nptr; |
| 1230 | cur->bc_nlevels++; |
| 1231 | *stat = 1; |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
| 1235 | /* |
| 1236 | * Move 1 record right from cur/level if possible. |
| 1237 | * Update cur to reflect the new path. |
| 1238 | */ |
| 1239 | STATIC int /* error */ |
| 1240 | xfs_alloc_rshift( |
| 1241 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 1242 | int level, /* level to shift record on */ |
| 1243 | int *stat) /* success/failure */ |
| 1244 | { |
| 1245 | int error; /* error return value */ |
| 1246 | int i; /* loop index */ |
| 1247 | xfs_alloc_key_t key; /* key value for leaf level upward */ |
| 1248 | xfs_buf_t *lbp; /* buffer for left (current) block */ |
| 1249 | xfs_alloc_block_t *left; /* left (current) btree block */ |
| 1250 | xfs_buf_t *rbp; /* buffer for right neighbor block */ |
| 1251 | xfs_alloc_block_t *right; /* right neighbor btree block */ |
| 1252 | xfs_alloc_key_t *rkp; /* key pointer for right block */ |
| 1253 | xfs_btree_cur_t *tcur; /* temporary cursor */ |
| 1254 | |
| 1255 | /* |
| 1256 | * Set up variables for this block as "left". |
| 1257 | */ |
| 1258 | lbp = cur->bc_bufs[level]; |
| 1259 | left = XFS_BUF_TO_ALLOC_BLOCK(lbp); |
| 1260 | #ifdef DEBUG |
| 1261 | if ((error = xfs_btree_check_sblock(cur, left, level, lbp))) |
| 1262 | return error; |
| 1263 | #endif |
| 1264 | /* |
| 1265 | * If we've got no right sibling then we can't shift an entry right. |
| 1266 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1267 | if (be32_to_cpu(left->bb_rightsib) == NULLAGBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | *stat = 0; |
| 1269 | return 0; |
| 1270 | } |
| 1271 | /* |
| 1272 | * If the cursor entry is the one that would be moved, don't |
| 1273 | * do it... it's too complicated. |
| 1274 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1275 | if (cur->bc_ptrs[level] >= be16_to_cpu(left->bb_numrecs)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | *stat = 0; |
| 1277 | return 0; |
| 1278 | } |
| 1279 | /* |
| 1280 | * Set up the right neighbor as "right". |
| 1281 | */ |
| 1282 | if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp, |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1283 | cur->bc_private.a.agno, be32_to_cpu(left->bb_rightsib), |
| 1284 | 0, &rbp, XFS_ALLOC_BTREE_REF))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | return error; |
| 1286 | right = XFS_BUF_TO_ALLOC_BLOCK(rbp); |
| 1287 | if ((error = xfs_btree_check_sblock(cur, right, level, rbp))) |
| 1288 | return error; |
| 1289 | /* |
| 1290 | * If it's full, it can't take another entry. |
| 1291 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1292 | if (be16_to_cpu(right->bb_numrecs) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | *stat = 0; |
| 1294 | return 0; |
| 1295 | } |
| 1296 | /* |
| 1297 | * Make a hole at the start of the right neighbor block, then |
| 1298 | * copy the last left block entry to the hole. |
| 1299 | */ |
| 1300 | if (level > 0) { |
| 1301 | xfs_alloc_key_t *lkp; /* key pointer for left block */ |
| 1302 | xfs_alloc_ptr_t *lpp; /* address pointer for left block */ |
| 1303 | xfs_alloc_ptr_t *rpp; /* address pointer for right block */ |
| 1304 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1305 | lkp = XFS_ALLOC_KEY_ADDR(left, be16_to_cpu(left->bb_numrecs), cur); |
| 1306 | lpp = XFS_ALLOC_PTR_ADDR(left, be16_to_cpu(left->bb_numrecs), cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur); |
| 1308 | rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur); |
| 1309 | #ifdef DEBUG |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1310 | for (i = be16_to_cpu(right->bb_numrecs) - 1; i >= 0; i--) { |
| 1311 | if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | return error; |
| 1313 | } |
| 1314 | #endif |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1315 | memmove(rkp + 1, rkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp)); |
| 1316 | memmove(rpp + 1, rpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | #ifdef DEBUG |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1318 | if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(*lpp), level))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | return error; |
| 1320 | #endif |
Christoph Hellwig | c38e5e8 | 2006-09-28 10:57:17 +1000 | [diff] [blame] | 1321 | *rkp = *lkp; |
| 1322 | *rpp = *lpp; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1323 | xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1); |
| 1324 | xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | xfs_btree_check_key(cur->bc_btnum, rkp, rkp + 1); |
| 1326 | } else { |
| 1327 | xfs_alloc_rec_t *lrp; /* record pointer for left block */ |
| 1328 | xfs_alloc_rec_t *rrp; /* record pointer for right block */ |
| 1329 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1330 | lrp = XFS_ALLOC_REC_ADDR(left, be16_to_cpu(left->bb_numrecs), cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | rrp = XFS_ALLOC_REC_ADDR(right, 1, cur); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1332 | memmove(rrp + 1, rrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | *rrp = *lrp; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1334 | xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1); |
| 1335 | key.ar_startblock = rrp->ar_startblock; |
| 1336 | key.ar_blockcount = rrp->ar_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | rkp = &key; |
| 1338 | xfs_btree_check_rec(cur->bc_btnum, rrp, rrp + 1); |
| 1339 | } |
| 1340 | /* |
| 1341 | * Decrement and log left's numrecs, bump and log right's numrecs. |
| 1342 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1343 | be16_add_cpu(&left->bb_numrecs, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS); |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1345 | be16_add_cpu(&right->bb_numrecs, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS); |
| 1347 | /* |
| 1348 | * Using a temporary cursor, update the parent key values of the |
| 1349 | * block on the right. |
| 1350 | */ |
| 1351 | if ((error = xfs_btree_dup_cursor(cur, &tcur))) |
| 1352 | return error; |
| 1353 | i = xfs_btree_lastrec(tcur, level); |
| 1354 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1355 | if ((error = xfs_btree_increment(tcur, level, &i)) || |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 1356 | (error = xfs_btree_updkey(tcur, (union xfs_btree_key *)rkp, level + 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | goto error0; |
| 1358 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
| 1359 | *stat = 1; |
| 1360 | return 0; |
| 1361 | error0: |
| 1362 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); |
| 1363 | return error; |
| 1364 | } |
| 1365 | |
| 1366 | /* |
| 1367 | * Split cur/level block in half. |
| 1368 | * Return new block number and its first record (to be inserted into parent). |
| 1369 | */ |
| 1370 | STATIC int /* error */ |
| 1371 | xfs_alloc_split( |
| 1372 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 1373 | int level, /* level to split */ |
| 1374 | xfs_agblock_t *bnop, /* output: block number allocated */ |
| 1375 | xfs_alloc_key_t *keyp, /* output: first key of new block */ |
| 1376 | xfs_btree_cur_t **curp, /* output: new cursor */ |
| 1377 | int *stat) /* success/failure */ |
| 1378 | { |
| 1379 | int error; /* error return value */ |
| 1380 | int i; /* loop index/record number */ |
| 1381 | xfs_agblock_t lbno; /* left (current) block number */ |
| 1382 | xfs_buf_t *lbp; /* buffer for left block */ |
| 1383 | xfs_alloc_block_t *left; /* left (current) btree block */ |
| 1384 | xfs_agblock_t rbno; /* right (new) block number */ |
| 1385 | xfs_buf_t *rbp; /* buffer for right block */ |
| 1386 | xfs_alloc_block_t *right; /* right (new) btree block */ |
| 1387 | |
| 1388 | /* |
| 1389 | * Allocate the new block from the freelist. |
| 1390 | * If we can't do it, we're toast. Give up. |
| 1391 | */ |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 1392 | error = xfs_alloc_get_freelist(cur->bc_tp, |
| 1393 | cur->bc_private.a.agbp, &rbno, 1); |
| 1394 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | return error; |
| 1396 | if (rbno == NULLAGBLOCK) { |
| 1397 | *stat = 0; |
| 1398 | return 0; |
| 1399 | } |
| 1400 | xfs_trans_agbtree_delta(cur->bc_tp, 1); |
| 1401 | rbp = xfs_btree_get_bufs(cur->bc_mp, cur->bc_tp, cur->bc_private.a.agno, |
| 1402 | rbno, 0); |
| 1403 | /* |
| 1404 | * Set up the new block as "right". |
| 1405 | */ |
| 1406 | right = XFS_BUF_TO_ALLOC_BLOCK(rbp); |
| 1407 | /* |
| 1408 | * "Left" is the current (according to the cursor) block. |
| 1409 | */ |
| 1410 | lbp = cur->bc_bufs[level]; |
| 1411 | left = XFS_BUF_TO_ALLOC_BLOCK(lbp); |
| 1412 | #ifdef DEBUG |
| 1413 | if ((error = xfs_btree_check_sblock(cur, left, level, lbp))) |
| 1414 | return error; |
| 1415 | #endif |
| 1416 | /* |
| 1417 | * Fill in the btree header for the new block. |
| 1418 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1419 | right->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]); |
| 1420 | right->bb_level = left->bb_level; |
| 1421 | right->bb_numrecs = cpu_to_be16(be16_to_cpu(left->bb_numrecs) / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | /* |
| 1423 | * Make sure that if there's an odd number of entries now, that |
| 1424 | * each new block will have the same number of entries. |
| 1425 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1426 | if ((be16_to_cpu(left->bb_numrecs) & 1) && |
| 1427 | cur->bc_ptrs[level] <= be16_to_cpu(right->bb_numrecs) + 1) |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1428 | be16_add_cpu(&right->bb_numrecs, 1); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1429 | i = be16_to_cpu(left->bb_numrecs) - be16_to_cpu(right->bb_numrecs) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | /* |
| 1431 | * For non-leaf blocks, copy keys and addresses over to the new block. |
| 1432 | */ |
| 1433 | if (level > 0) { |
| 1434 | xfs_alloc_key_t *lkp; /* left btree key pointer */ |
| 1435 | xfs_alloc_ptr_t *lpp; /* left btree address pointer */ |
| 1436 | xfs_alloc_key_t *rkp; /* right btree key pointer */ |
| 1437 | xfs_alloc_ptr_t *rpp; /* right btree address pointer */ |
| 1438 | |
| 1439 | lkp = XFS_ALLOC_KEY_ADDR(left, i, cur); |
| 1440 | lpp = XFS_ALLOC_PTR_ADDR(left, i, cur); |
| 1441 | rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur); |
| 1442 | rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur); |
| 1443 | #ifdef DEBUG |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1444 | for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) { |
| 1445 | if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | return error; |
| 1447 | } |
| 1448 | #endif |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1449 | memcpy(rkp, lkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp)); |
| 1450 | memcpy(rpp, lpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp)); |
| 1451 | xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs)); |
| 1452 | xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | *keyp = *rkp; |
| 1454 | } |
| 1455 | /* |
| 1456 | * For leaf blocks, copy records over to the new block. |
| 1457 | */ |
| 1458 | else { |
| 1459 | xfs_alloc_rec_t *lrp; /* left btree record pointer */ |
| 1460 | xfs_alloc_rec_t *rrp; /* right btree record pointer */ |
| 1461 | |
| 1462 | lrp = XFS_ALLOC_REC_ADDR(left, i, cur); |
| 1463 | rrp = XFS_ALLOC_REC_ADDR(right, 1, cur); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1464 | memcpy(rrp, lrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp)); |
| 1465 | xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs)); |
| 1466 | keyp->ar_startblock = rrp->ar_startblock; |
| 1467 | keyp->ar_blockcount = rrp->ar_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | } |
| 1469 | /* |
| 1470 | * Find the left block number by looking in the buffer. |
| 1471 | * Adjust numrecs, sibling pointers. |
| 1472 | */ |
| 1473 | lbno = XFS_DADDR_TO_AGBNO(cur->bc_mp, XFS_BUF_ADDR(lbp)); |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1474 | be16_add_cpu(&left->bb_numrecs, -(be16_to_cpu(right->bb_numrecs))); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1475 | right->bb_rightsib = left->bb_rightsib; |
| 1476 | left->bb_rightsib = cpu_to_be32(rbno); |
| 1477 | right->bb_leftsib = cpu_to_be32(lbno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_ALL_BITS); |
| 1479 | xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB); |
| 1480 | /* |
| 1481 | * If there's a block to the new block's right, make that block |
| 1482 | * point back to right instead of to left. |
| 1483 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1484 | if (be32_to_cpu(right->bb_rightsib) != NULLAGBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | xfs_alloc_block_t *rrblock; /* rr btree block */ |
| 1486 | xfs_buf_t *rrbp; /* buffer for rrblock */ |
| 1487 | |
| 1488 | if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp, |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1489 | cur->bc_private.a.agno, be32_to_cpu(right->bb_rightsib), 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | &rrbp, XFS_ALLOC_BTREE_REF))) |
| 1491 | return error; |
| 1492 | rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp); |
| 1493 | if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp))) |
| 1494 | return error; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1495 | rrblock->bb_leftsib = cpu_to_be32(rbno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB); |
| 1497 | } |
| 1498 | /* |
| 1499 | * If the cursor is really in the right block, move it there. |
| 1500 | * If it's just pointing past the last entry in left, then we'll |
| 1501 | * insert there, so don't change anything in that case. |
| 1502 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1503 | if (cur->bc_ptrs[level] > be16_to_cpu(left->bb_numrecs) + 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | xfs_btree_setbuf(cur, level, rbp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1505 | cur->bc_ptrs[level] -= be16_to_cpu(left->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | } |
| 1507 | /* |
| 1508 | * If there are more levels, we'll need another cursor which refers to |
| 1509 | * the right block, no matter where this cursor was. |
| 1510 | */ |
| 1511 | if (level + 1 < cur->bc_nlevels) { |
| 1512 | if ((error = xfs_btree_dup_cursor(cur, curp))) |
| 1513 | return error; |
| 1514 | (*curp)->bc_ptrs[level + 1]++; |
| 1515 | } |
| 1516 | *bnop = rbno; |
| 1517 | *stat = 1; |
| 1518 | return 0; |
| 1519 | } |
| 1520 | |
| 1521 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | * Externally visible routines. |
| 1523 | */ |
| 1524 | |
| 1525 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | * Delete the record pointed to by cur. |
| 1527 | * The cursor refers to the place where the record was (could be inserted) |
| 1528 | * when the operation returns. |
| 1529 | */ |
| 1530 | int /* error */ |
| 1531 | xfs_alloc_delete( |
| 1532 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 1533 | int *stat) /* success/failure */ |
| 1534 | { |
| 1535 | int error; /* error return value */ |
| 1536 | int i; /* result code */ |
| 1537 | int level; /* btree level */ |
| 1538 | |
| 1539 | /* |
| 1540 | * Go up the tree, starting at leaf level. |
| 1541 | * If 2 is returned then a join was done; go to the next level. |
| 1542 | * Otherwise we are done. |
| 1543 | */ |
| 1544 | for (level = 0, i = 2; i == 2; level++) { |
| 1545 | if ((error = xfs_alloc_delrec(cur, level, &i))) |
| 1546 | return error; |
| 1547 | } |
| 1548 | if (i == 0) { |
| 1549 | for (level = 1; level < cur->bc_nlevels; level++) { |
| 1550 | if (cur->bc_ptrs[level] == 0) { |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1551 | if ((error = xfs_btree_decrement(cur, level, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | return error; |
| 1553 | break; |
| 1554 | } |
| 1555 | } |
| 1556 | } |
| 1557 | *stat = i; |
| 1558 | return 0; |
| 1559 | } |
| 1560 | |
| 1561 | /* |
| 1562 | * Get the data from the pointed-to record. |
| 1563 | */ |
| 1564 | int /* error */ |
| 1565 | xfs_alloc_get_rec( |
| 1566 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 1567 | xfs_agblock_t *bno, /* output: starting block of extent */ |
| 1568 | xfs_extlen_t *len, /* output: length of extent */ |
| 1569 | int *stat) /* output: success/failure */ |
| 1570 | { |
| 1571 | xfs_alloc_block_t *block; /* btree block */ |
| 1572 | #ifdef DEBUG |
| 1573 | int error; /* error return value */ |
| 1574 | #endif |
| 1575 | int ptr; /* record number */ |
| 1576 | |
| 1577 | ptr = cur->bc_ptrs[0]; |
| 1578 | block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]); |
| 1579 | #ifdef DEBUG |
| 1580 | if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0]))) |
| 1581 | return error; |
| 1582 | #endif |
| 1583 | /* |
| 1584 | * Off the right end or left end, return failure. |
| 1585 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1586 | if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | *stat = 0; |
| 1588 | return 0; |
| 1589 | } |
| 1590 | /* |
| 1591 | * Point to the record and extract its data. |
| 1592 | */ |
| 1593 | { |
| 1594 | xfs_alloc_rec_t *rec; /* record data */ |
| 1595 | |
| 1596 | rec = XFS_ALLOC_REC_ADDR(block, ptr, cur); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1597 | *bno = be32_to_cpu(rec->ar_startblock); |
| 1598 | *len = be32_to_cpu(rec->ar_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | } |
| 1600 | *stat = 1; |
| 1601 | return 0; |
| 1602 | } |
| 1603 | |
| 1604 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | * Insert the current record at the point referenced by cur. |
| 1606 | * The cursor may be inconsistent on return if splits have been done. |
| 1607 | */ |
| 1608 | int /* error */ |
| 1609 | xfs_alloc_insert( |
| 1610 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 1611 | int *stat) /* success/failure */ |
| 1612 | { |
| 1613 | int error; /* error return value */ |
| 1614 | int i; /* result value, 0 for failure */ |
| 1615 | int level; /* current level number in btree */ |
| 1616 | xfs_agblock_t nbno; /* new block number (split result) */ |
| 1617 | xfs_btree_cur_t *ncur; /* new cursor (split result) */ |
| 1618 | xfs_alloc_rec_t nrec; /* record being inserted this level */ |
| 1619 | xfs_btree_cur_t *pcur; /* previous level's cursor */ |
| 1620 | |
| 1621 | level = 0; |
| 1622 | nbno = NULLAGBLOCK; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1623 | nrec.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock); |
| 1624 | nrec.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount); |
Nathan Scott | 1121b21 | 2006-09-28 10:58:40 +1000 | [diff] [blame] | 1625 | ncur = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | pcur = cur; |
| 1627 | /* |
| 1628 | * Loop going up the tree, starting at the leaf level. |
| 1629 | * Stop when we don't get a split block, that must mean that |
| 1630 | * the insert is finished with this level. |
| 1631 | */ |
| 1632 | do { |
| 1633 | /* |
| 1634 | * Insert nrec/nbno into this level of the tree. |
| 1635 | * Note if we fail, nbno will be null. |
| 1636 | */ |
| 1637 | if ((error = xfs_alloc_insrec(pcur, level++, &nbno, &nrec, &ncur, |
| 1638 | &i))) { |
| 1639 | if (pcur != cur) |
| 1640 | xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR); |
| 1641 | return error; |
| 1642 | } |
| 1643 | /* |
| 1644 | * See if the cursor we just used is trash. |
| 1645 | * Can't trash the caller's cursor, but otherwise we should |
| 1646 | * if ncur is a new cursor or we're about to be done. |
| 1647 | */ |
| 1648 | if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) { |
| 1649 | cur->bc_nlevels = pcur->bc_nlevels; |
| 1650 | xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR); |
| 1651 | } |
| 1652 | /* |
| 1653 | * If we got a new cursor, switch to it. |
| 1654 | */ |
| 1655 | if (ncur) { |
| 1656 | pcur = ncur; |
Nathan Scott | 1121b21 | 2006-09-28 10:58:40 +1000 | [diff] [blame] | 1657 | ncur = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | } |
| 1659 | } while (nbno != NULLAGBLOCK); |
| 1660 | *stat = i; |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 1664 | STATIC struct xfs_btree_cur * |
| 1665 | xfs_allocbt_dup_cursor( |
| 1666 | struct xfs_btree_cur *cur) |
| 1667 | { |
| 1668 | return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp, |
| 1669 | cur->bc_private.a.agbp, cur->bc_private.a.agno, |
| 1670 | cur->bc_btnum); |
| 1671 | } |
| 1672 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame^] | 1673 | /* |
| 1674 | * Update the longest extent in the AGF |
| 1675 | */ |
| 1676 | STATIC void |
| 1677 | xfs_allocbt_update_lastrec( |
| 1678 | struct xfs_btree_cur *cur, |
| 1679 | struct xfs_btree_block *block, |
| 1680 | union xfs_btree_rec *rec, |
| 1681 | int ptr, |
| 1682 | int reason) |
| 1683 | { |
| 1684 | struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp); |
| 1685 | xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno); |
| 1686 | __be32 len; |
| 1687 | |
| 1688 | ASSERT(cur->bc_btnum == XFS_BTNUM_CNT); |
| 1689 | |
| 1690 | switch (reason) { |
| 1691 | case LASTREC_UPDATE: |
| 1692 | /* |
| 1693 | * If this is the last leaf block and it's the last record, |
| 1694 | * then update the size of the longest extent in the AG. |
| 1695 | */ |
| 1696 | if (ptr != xfs_btree_get_numrecs(block)) |
| 1697 | return; |
| 1698 | len = rec->alloc.ar_blockcount; |
| 1699 | break; |
| 1700 | default: |
| 1701 | ASSERT(0); |
| 1702 | return; |
| 1703 | } |
| 1704 | |
| 1705 | agf->agf_longest = len; |
| 1706 | cur->bc_mp->m_perag[seqno].pagf_longest = be32_to_cpu(len); |
| 1707 | xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST); |
| 1708 | } |
| 1709 | |
Christoph Hellwig | ce5e42d | 2008-10-30 16:55:23 +1100 | [diff] [blame] | 1710 | STATIC int |
| 1711 | xfs_allocbt_get_maxrecs( |
| 1712 | struct xfs_btree_cur *cur, |
| 1713 | int level) |
| 1714 | { |
| 1715 | return cur->bc_mp->m_alloc_mxr[level != 0]; |
| 1716 | } |
| 1717 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 1718 | STATIC void |
| 1719 | xfs_allocbt_init_key_from_rec( |
| 1720 | union xfs_btree_key *key, |
| 1721 | union xfs_btree_rec *rec) |
| 1722 | { |
| 1723 | ASSERT(rec->alloc.ar_startblock != 0); |
| 1724 | |
| 1725 | key->alloc.ar_startblock = rec->alloc.ar_startblock; |
| 1726 | key->alloc.ar_blockcount = rec->alloc.ar_blockcount; |
| 1727 | } |
| 1728 | |
| 1729 | STATIC void |
| 1730 | xfs_allocbt_init_ptr_from_cur( |
| 1731 | struct xfs_btree_cur *cur, |
| 1732 | union xfs_btree_ptr *ptr) |
| 1733 | { |
| 1734 | struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp); |
| 1735 | |
| 1736 | ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno)); |
| 1737 | ASSERT(agf->agf_roots[cur->bc_btnum] != 0); |
| 1738 | |
| 1739 | ptr->s = agf->agf_roots[cur->bc_btnum]; |
| 1740 | } |
| 1741 | |
| 1742 | STATIC __int64_t |
| 1743 | xfs_allocbt_key_diff( |
| 1744 | struct xfs_btree_cur *cur, |
| 1745 | union xfs_btree_key *key) |
| 1746 | { |
| 1747 | xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a; |
| 1748 | xfs_alloc_key_t *kp = &key->alloc; |
| 1749 | __int64_t diff; |
| 1750 | |
| 1751 | if (cur->bc_btnum == XFS_BTNUM_BNO) { |
| 1752 | return (__int64_t)be32_to_cpu(kp->ar_startblock) - |
| 1753 | rec->ar_startblock; |
| 1754 | } |
| 1755 | |
| 1756 | diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount; |
| 1757 | if (diff) |
| 1758 | return diff; |
| 1759 | |
| 1760 | return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock; |
| 1761 | } |
| 1762 | |
Christoph Hellwig | 8c4ed63 | 2008-10-30 16:55:13 +1100 | [diff] [blame] | 1763 | #ifdef XFS_BTREE_TRACE |
| 1764 | ktrace_t *xfs_allocbt_trace_buf; |
| 1765 | |
| 1766 | STATIC void |
| 1767 | xfs_allocbt_trace_enter( |
| 1768 | struct xfs_btree_cur *cur, |
| 1769 | const char *func, |
| 1770 | char *s, |
| 1771 | int type, |
| 1772 | int line, |
| 1773 | __psunsigned_t a0, |
| 1774 | __psunsigned_t a1, |
| 1775 | __psunsigned_t a2, |
| 1776 | __psunsigned_t a3, |
| 1777 | __psunsigned_t a4, |
| 1778 | __psunsigned_t a5, |
| 1779 | __psunsigned_t a6, |
| 1780 | __psunsigned_t a7, |
| 1781 | __psunsigned_t a8, |
| 1782 | __psunsigned_t a9, |
| 1783 | __psunsigned_t a10) |
| 1784 | { |
| 1785 | ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type, |
| 1786 | (void *)func, (void *)s, NULL, (void *)cur, |
| 1787 | (void *)a0, (void *)a1, (void *)a2, (void *)a3, |
| 1788 | (void *)a4, (void *)a5, (void *)a6, (void *)a7, |
| 1789 | (void *)a8, (void *)a9, (void *)a10); |
| 1790 | } |
| 1791 | |
| 1792 | STATIC void |
| 1793 | xfs_allocbt_trace_cursor( |
| 1794 | struct xfs_btree_cur *cur, |
| 1795 | __uint32_t *s0, |
| 1796 | __uint64_t *l0, |
| 1797 | __uint64_t *l1) |
| 1798 | { |
| 1799 | *s0 = cur->bc_private.a.agno; |
| 1800 | *l0 = cur->bc_rec.a.ar_startblock; |
| 1801 | *l1 = cur->bc_rec.a.ar_blockcount; |
| 1802 | } |
| 1803 | |
| 1804 | STATIC void |
| 1805 | xfs_allocbt_trace_key( |
| 1806 | struct xfs_btree_cur *cur, |
| 1807 | union xfs_btree_key *key, |
| 1808 | __uint64_t *l0, |
| 1809 | __uint64_t *l1) |
| 1810 | { |
| 1811 | *l0 = be32_to_cpu(key->alloc.ar_startblock); |
| 1812 | *l1 = be32_to_cpu(key->alloc.ar_blockcount); |
| 1813 | } |
| 1814 | |
| 1815 | STATIC void |
| 1816 | xfs_allocbt_trace_record( |
| 1817 | struct xfs_btree_cur *cur, |
| 1818 | union xfs_btree_rec *rec, |
| 1819 | __uint64_t *l0, |
| 1820 | __uint64_t *l1, |
| 1821 | __uint64_t *l2) |
| 1822 | { |
| 1823 | *l0 = be32_to_cpu(rec->alloc.ar_startblock); |
| 1824 | *l1 = be32_to_cpu(rec->alloc.ar_blockcount); |
| 1825 | *l2 = 0; |
| 1826 | } |
| 1827 | #endif /* XFS_BTREE_TRACE */ |
| 1828 | |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 1829 | static const struct xfs_btree_ops xfs_allocbt_ops = { |
Christoph Hellwig | 65f1eae | 2008-10-30 16:55:34 +1100 | [diff] [blame] | 1830 | .rec_len = sizeof(xfs_alloc_rec_t), |
| 1831 | .key_len = sizeof(xfs_alloc_key_t), |
| 1832 | |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 1833 | .dup_cursor = xfs_allocbt_dup_cursor, |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame^] | 1834 | .update_lastrec = xfs_allocbt_update_lastrec, |
Christoph Hellwig | ce5e42d | 2008-10-30 16:55:23 +1100 | [diff] [blame] | 1835 | .get_maxrecs = xfs_allocbt_get_maxrecs, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 1836 | .init_key_from_rec = xfs_allocbt_init_key_from_rec, |
| 1837 | .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur, |
| 1838 | .key_diff = xfs_allocbt_key_diff, |
Christoph Hellwig | 8c4ed63 | 2008-10-30 16:55:13 +1100 | [diff] [blame] | 1839 | |
| 1840 | #ifdef XFS_BTREE_TRACE |
| 1841 | .trace_enter = xfs_allocbt_trace_enter, |
| 1842 | .trace_cursor = xfs_allocbt_trace_cursor, |
| 1843 | .trace_key = xfs_allocbt_trace_key, |
| 1844 | .trace_record = xfs_allocbt_trace_record, |
| 1845 | #endif |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 1846 | }; |
| 1847 | |
| 1848 | /* |
| 1849 | * Allocate a new allocation btree cursor. |
| 1850 | */ |
| 1851 | struct xfs_btree_cur * /* new alloc btree cursor */ |
| 1852 | xfs_allocbt_init_cursor( |
| 1853 | struct xfs_mount *mp, /* file system mount point */ |
| 1854 | struct xfs_trans *tp, /* transaction pointer */ |
| 1855 | struct xfs_buf *agbp, /* buffer for agf structure */ |
| 1856 | xfs_agnumber_t agno, /* allocation group number */ |
| 1857 | xfs_btnum_t btnum) /* btree identifier */ |
| 1858 | { |
| 1859 | struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp); |
| 1860 | struct xfs_btree_cur *cur; |
| 1861 | |
| 1862 | ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT); |
| 1863 | |
| 1864 | cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP); |
| 1865 | |
| 1866 | cur->bc_tp = tp; |
| 1867 | cur->bc_mp = mp; |
| 1868 | cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]); |
| 1869 | cur->bc_btnum = btnum; |
| 1870 | cur->bc_blocklog = mp->m_sb.sb_blocklog; |
| 1871 | |
| 1872 | cur->bc_ops = &xfs_allocbt_ops; |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame^] | 1873 | if (btnum == XFS_BTNUM_CNT) |
| 1874 | cur->bc_flags = XFS_BTREE_LASTREC_UPDATE; |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 1875 | |
| 1876 | cur->bc_private.a.agbp = agbp; |
| 1877 | cur->bc_private.a.agno = agno; |
| 1878 | |
| 1879 | return cur; |
| 1880 | } |