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