blob: 31e42891fc9a8ac7dff411fb0b0a4bd6ac8dcd93 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * 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 Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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 Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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 Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_alloc_btree.h"
32#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_dir2_sf.h"
34#include "xfs_attr_sf.h"
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#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
46STATIC void xfs_alloc_log_block(xfs_trans_t *, xfs_buf_t *, int);
47STATIC void xfs_alloc_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
48STATIC void xfs_alloc_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
49STATIC void xfs_alloc_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
50STATIC int xfs_alloc_lshift(xfs_btree_cur_t *, int, int *);
51STATIC int xfs_alloc_newroot(xfs_btree_cur_t *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052STATIC int xfs_alloc_split(xfs_btree_cur_t *, int, xfs_agblock_t *,
53 xfs_alloc_key_t *, xfs_btree_cur_t **, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
56 * Internal functions.
57 */
58
59/*
60 * Single level of the xfs_alloc_delete record deletion routine.
61 * Delete record pointed to by cur/level.
62 * Remove the record from its block then rebalance the tree.
63 * Return 0 for error, 1 for done, 2 to go on to the next level.
64 */
65STATIC int /* error */
66xfs_alloc_delrec(
67 xfs_btree_cur_t *cur, /* btree cursor */
68 int level, /* level removing record from */
69 int *stat) /* fail/done/go-on */
70{
71 xfs_agf_t *agf; /* allocation group freelist header */
72 xfs_alloc_block_t *block; /* btree block record/key lives in */
73 xfs_agblock_t bno; /* btree block number */
74 xfs_buf_t *bp; /* buffer for block */
75 int error; /* error return value */
76 int i; /* loop index */
77 xfs_alloc_key_t key; /* kp points here if block is level 0 */
78 xfs_agblock_t lbno; /* left block's block number */
79 xfs_buf_t *lbp; /* left block's buffer pointer */
80 xfs_alloc_block_t *left; /* left btree block */
81 xfs_alloc_key_t *lkp=NULL; /* left block key pointer */
82 xfs_alloc_ptr_t *lpp=NULL; /* left block address pointer */
83 int lrecs=0; /* number of records in left block */
84 xfs_alloc_rec_t *lrp; /* left block record pointer */
85 xfs_mount_t *mp; /* mount structure */
86 int ptr; /* index in btree block for this rec */
87 xfs_agblock_t rbno; /* right block's block number */
88 xfs_buf_t *rbp; /* right block's buffer pointer */
89 xfs_alloc_block_t *right; /* right btree block */
90 xfs_alloc_key_t *rkp; /* right block key pointer */
91 xfs_alloc_ptr_t *rpp; /* right block address pointer */
92 int rrecs=0; /* number of records in right block */
Eric Sandeen91d87232006-09-28 11:05:40 +100093 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 xfs_alloc_rec_t *rrp; /* right block record pointer */
95 xfs_btree_cur_t *tcur; /* temporary btree cursor */
96
97 /*
98 * Get the index of the entry being deleted, check for nothing there.
99 */
100 ptr = cur->bc_ptrs[level];
101 if (ptr == 0) {
102 *stat = 0;
103 return 0;
104 }
105 /*
106 * Get the buffer & block containing the record or key/ptr.
107 */
108 bp = cur->bc_bufs[level];
109 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
110#ifdef DEBUG
111 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
112 return error;
113#endif
114 /*
115 * Fail if we're off the end of the block.
116 */
Eric Sandeen91d87232006-09-28 11:05:40 +1000117 numrecs = be16_to_cpu(block->bb_numrecs);
118 if (ptr > numrecs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 *stat = 0;
120 return 0;
121 }
122 XFS_STATS_INC(xs_abt_delrec);
123 /*
124 * It's a nonleaf. Excise the key and ptr being deleted, by
125 * sliding the entries past them down one.
126 * Log the changed areas of the block.
127 */
128 if (level > 0) {
129 lkp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
130 lpp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
131#ifdef DEBUG
Eric Sandeen91d87232006-09-28 11:05:40 +1000132 for (i = ptr; i < numrecs; i++) {
Christoph Hellwig16259e72005-11-02 15:11:25 +1100133 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return error;
135 }
136#endif
Eric Sandeen91d87232006-09-28 11:05:40 +1000137 if (ptr < numrecs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 memmove(&lkp[ptr - 1], &lkp[ptr],
Eric Sandeen91d87232006-09-28 11:05:40 +1000139 (numrecs - ptr) * sizeof(*lkp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 memmove(&lpp[ptr - 1], &lpp[ptr],
Eric Sandeen91d87232006-09-28 11:05:40 +1000141 (numrecs - ptr) * sizeof(*lpp));
142 xfs_alloc_log_ptrs(cur, bp, ptr, numrecs - 1);
143 xfs_alloc_log_keys(cur, bp, ptr, numrecs - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145 }
146 /*
147 * It's a leaf. Excise the record being deleted, by sliding the
148 * entries past it down one. Log the changed areas of the block.
149 */
150 else {
151 lrp = XFS_ALLOC_REC_ADDR(block, 1, cur);
Eric Sandeen91d87232006-09-28 11:05:40 +1000152 if (ptr < numrecs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 memmove(&lrp[ptr - 1], &lrp[ptr],
Eric Sandeen91d87232006-09-28 11:05:40 +1000154 (numrecs - ptr) * sizeof(*lrp));
155 xfs_alloc_log_recs(cur, bp, ptr, numrecs - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157 /*
158 * If it's the first record in the block, we'll need a key
159 * structure to pass up to the next level (updkey).
160 */
161 if (ptr == 1) {
Christoph Hellwig16259e72005-11-02 15:11:25 +1100162 key.ar_startblock = lrp->ar_startblock;
163 key.ar_blockcount = lrp->ar_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 lkp = &key;
165 }
166 }
167 /*
168 * Decrement and log the number of entries in the block.
169 */
Eric Sandeen91d87232006-09-28 11:05:40 +1000170 numrecs--;
171 block->bb_numrecs = cpu_to_be16(numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
173 /*
174 * See if the longest free extent in the allocation group was
175 * changed by this operation. True if it's the by-size btree, and
176 * this is the leaf level, and there is no right sibling block,
177 * and this was the last record.
178 */
179 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
180 mp = cur->bc_mp;
181
182 if (level == 0 &&
183 cur->bc_btnum == XFS_BTNUM_CNT &&
Christoph Hellwig16259e72005-11-02 15:11:25 +1100184 be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
Eric Sandeen91d87232006-09-28 11:05:40 +1000185 ptr > numrecs) {
186 ASSERT(ptr == numrecs + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /*
188 * There are still records in the block. Grab the size
189 * from the last one.
190 */
Eric Sandeen91d87232006-09-28 11:05:40 +1000191 if (numrecs) {
192 rrp = XFS_ALLOC_REC_ADDR(block, numrecs, cur);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100193 agf->agf_longest = rrp->ar_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195 /*
196 * No free extents left.
197 */
198 else
199 agf->agf_longest = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100200 mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest =
201 be32_to_cpu(agf->agf_longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
203 XFS_AGF_LONGEST);
204 }
205 /*
206 * Is this the root level? If so, we're almost done.
207 */
208 if (level == cur->bc_nlevels - 1) {
209 /*
210 * If this is the root level,
211 * and there's only one entry left,
212 * and it's NOT the leaf level,
213 * then we can get rid of this level.
214 */
Eric Sandeen91d87232006-09-28 11:05:40 +1000215 if (numrecs == 1 && level > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /*
217 * lpp is still set to the first pointer in the block.
218 * Make it the new root of the btree.
219 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100220 bno = be32_to_cpu(agf->agf_roots[cur->bc_btnum]);
221 agf->agf_roots[cur->bc_btnum] = *lpp;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800222 be32_add_cpu(&agf->agf_levels[cur->bc_btnum], -1);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100223 mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_levels[cur->bc_btnum]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /*
225 * Put this buffer/block on the ag's freelist.
226 */
David Chinner92821e22007-05-24 15:26:31 +1000227 error = xfs_alloc_put_freelist(cur->bc_tp,
228 cur->bc_private.a.agbp, NULL, bno, 1);
229 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return error;
231 /*
232 * Since blocks move to the free list without the
233 * coordination used in xfs_bmap_finish, we can't allow
234 * block to be available for reallocation and
235 * non-transaction writing (user data) until we know
236 * that the transaction that moved it to the free list
237 * is permanently on disk. We track the blocks by
238 * declaring these blocks as "busy"; the busy list is
239 * maintained on a per-ag basis and each transaction
240 * records which entries should be removed when the
241 * iclog commits to disk. If a busy block is
242 * allocated, the iclog is pushed up to the LSN
243 * that freed the block.
244 */
245 xfs_alloc_mark_busy(cur->bc_tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +1100246 be32_to_cpu(agf->agf_seqno), bno, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 xfs_trans_agbtree_delta(cur->bc_tp, -1);
249 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
250 XFS_AGF_ROOTS | XFS_AGF_LEVELS);
251 /*
252 * Update the cursor so there's one fewer level.
253 */
254 xfs_btree_setbuf(cur, level, NULL);
255 cur->bc_nlevels--;
256 } else if (level > 0 &&
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100257 (error = xfs_btree_decrement(cur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return error;
259 *stat = 1;
260 return 0;
261 }
262 /*
263 * If we deleted the leftmost entry in the block, update the
264 * key values above us in the tree.
265 */
Christoph Hellwig38bb7422008-10-30 16:56:22 +1100266 if (ptr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)lkp, level + 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return error;
268 /*
269 * If the number of records remaining in the block is at least
270 * the minimum, we're done.
271 */
Eric Sandeen91d87232006-09-28 11:05:40 +1000272 if (numrecs >= XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100273 if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return error;
275 *stat = 1;
276 return 0;
277 }
278 /*
279 * Otherwise, we have to move some records around to keep the
280 * tree balanced. Look at the left and right sibling blocks to
281 * see if we can re-balance by moving only one record.
282 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100283 rbno = be32_to_cpu(block->bb_rightsib);
284 lbno = be32_to_cpu(block->bb_leftsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 bno = NULLAGBLOCK;
286 ASSERT(rbno != NULLAGBLOCK || lbno != NULLAGBLOCK);
287 /*
288 * Duplicate the cursor so our btree manipulations here won't
289 * disrupt the next level up.
290 */
291 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
292 return error;
293 /*
294 * If there's a right sibling, see if it's ok to shift an entry
295 * out of it.
296 */
297 if (rbno != NULLAGBLOCK) {
298 /*
299 * Move the temp cursor to the last entry in the next block.
300 * Actually any entry but the first would suffice.
301 */
302 i = xfs_btree_lastrec(tcur, level);
303 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig637aa502008-10-30 16:55:45 +1100304 if ((error = xfs_btree_increment(tcur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 goto error0;
306 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
307 i = xfs_btree_lastrec(tcur, level);
308 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
309 /*
310 * Grab a pointer to the block.
311 */
312 rbp = tcur->bc_bufs[level];
313 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
314#ifdef DEBUG
315 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
316 goto error0;
317#endif
318 /*
319 * Grab the current block number, for future use.
320 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100321 bno = be32_to_cpu(right->bb_leftsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /*
323 * If right block is full enough so that removing one entry
324 * won't make it too empty, and left-shifting an entry out
325 * of right to us works, we're done.
326 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100327 if (be16_to_cpu(right->bb_numrecs) - 1 >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
329 if ((error = xfs_alloc_lshift(tcur, level, &i)))
330 goto error0;
331 if (i) {
Christoph Hellwig16259e72005-11-02 15:11:25 +1100332 ASSERT(be16_to_cpu(block->bb_numrecs) >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 XFS_ALLOC_BLOCK_MINRECS(level, cur));
334 xfs_btree_del_cursor(tcur,
335 XFS_BTREE_NOERROR);
336 if (level > 0 &&
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100337 (error = xfs_btree_decrement(cur, level,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 &i)))
339 return error;
340 *stat = 1;
341 return 0;
342 }
343 }
344 /*
345 * Otherwise, grab the number of records in right for
346 * future reference, and fix up the temp cursor to point
347 * to our block again (last record).
348 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100349 rrecs = be16_to_cpu(right->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (lbno != NULLAGBLOCK) {
351 i = xfs_btree_firstrec(tcur, level);
352 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100353 if ((error = xfs_btree_decrement(tcur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 goto error0;
355 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
356 }
357 }
358 /*
359 * If there's a left sibling, see if it's ok to shift an entry
360 * out of it.
361 */
362 if (lbno != NULLAGBLOCK) {
363 /*
364 * Move the temp cursor to the first entry in the
365 * previous block.
366 */
367 i = xfs_btree_firstrec(tcur, level);
368 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100369 if ((error = xfs_btree_decrement(tcur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 goto error0;
371 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
372 xfs_btree_firstrec(tcur, level);
373 /*
374 * Grab a pointer to the block.
375 */
376 lbp = tcur->bc_bufs[level];
377 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
378#ifdef DEBUG
379 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
380 goto error0;
381#endif
382 /*
383 * Grab the current block number, for future use.
384 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100385 bno = be32_to_cpu(left->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /*
387 * If left block is full enough so that removing one entry
388 * won't make it too empty, and right-shifting an entry out
389 * of left to us works, we're done.
390 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100391 if (be16_to_cpu(left->bb_numrecs) - 1 >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
Christoph Hellwig9eaead52008-10-30 16:56:43 +1100393 if ((error = xfs_btree_rshift(tcur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 goto error0;
395 if (i) {
Christoph Hellwig16259e72005-11-02 15:11:25 +1100396 ASSERT(be16_to_cpu(block->bb_numrecs) >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 XFS_ALLOC_BLOCK_MINRECS(level, cur));
398 xfs_btree_del_cursor(tcur,
399 XFS_BTREE_NOERROR);
400 if (level == 0)
401 cur->bc_ptrs[0]++;
402 *stat = 1;
403 return 0;
404 }
405 }
406 /*
407 * Otherwise, grab the number of records in right for
408 * future reference.
409 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100410 lrecs = be16_to_cpu(left->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 /*
413 * Delete the temp cursor, we're done with it.
414 */
415 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
416 /*
417 * If here, we need to do a join to keep the tree balanced.
418 */
419 ASSERT(bno != NULLAGBLOCK);
420 /*
421 * See if we can join with the left neighbor block.
422 */
423 if (lbno != NULLAGBLOCK &&
Eric Sandeen91d87232006-09-28 11:05:40 +1000424 lrecs + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /*
426 * Set "right" to be the starting block,
427 * "left" to be the left neighbor.
428 */
429 rbno = bno;
430 right = block;
Eric Sandeen91d87232006-09-28 11:05:40 +1000431 rrecs = be16_to_cpu(right->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 rbp = bp;
433 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
434 cur->bc_private.a.agno, lbno, 0, &lbp,
435 XFS_ALLOC_BTREE_REF)))
436 return error;
437 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
Eric Sandeen91d87232006-09-28 11:05:40 +1000438 lrecs = be16_to_cpu(left->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
440 return error;
441 }
442 /*
443 * If that won't work, see if we can join with the right neighbor block.
444 */
445 else if (rbno != NULLAGBLOCK &&
Eric Sandeen91d87232006-09-28 11:05:40 +1000446 rrecs + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /*
448 * Set "left" to be the starting block,
449 * "right" to be the right neighbor.
450 */
451 lbno = bno;
452 left = block;
Eric Sandeen91d87232006-09-28 11:05:40 +1000453 lrecs = be16_to_cpu(left->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 lbp = bp;
455 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
456 cur->bc_private.a.agno, rbno, 0, &rbp,
457 XFS_ALLOC_BTREE_REF)))
458 return error;
459 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
Eric Sandeen91d87232006-09-28 11:05:40 +1000460 rrecs = be16_to_cpu(right->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
462 return error;
463 }
464 /*
465 * Otherwise, we can't fix the imbalance.
466 * Just return. This is probably a logic error, but it's not fatal.
467 */
468 else {
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100469 if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return error;
471 *stat = 1;
472 return 0;
473 }
474 /*
475 * We're now going to join "left" and "right" by moving all the stuff
476 * in "right" to "left" and deleting "right".
477 */
478 if (level > 0) {
479 /*
480 * It's a non-leaf. Move keys and pointers.
481 */
Eric Sandeen91d87232006-09-28 11:05:40 +1000482 lkp = XFS_ALLOC_KEY_ADDR(left, lrecs + 1, cur);
483 lpp = XFS_ALLOC_PTR_ADDR(left, lrecs + 1, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
485 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
486#ifdef DEBUG
Eric Sandeen91d87232006-09-28 11:05:40 +1000487 for (i = 0; i < rrecs; i++) {
Christoph Hellwig16259e72005-11-02 15:11:25 +1100488 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return error;
490 }
491#endif
Eric Sandeen91d87232006-09-28 11:05:40 +1000492 memcpy(lkp, rkp, rrecs * sizeof(*lkp));
493 memcpy(lpp, rpp, rrecs * sizeof(*lpp));
494 xfs_alloc_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
495 xfs_alloc_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 } else {
497 /*
498 * It's a leaf. Move records.
499 */
Eric Sandeen91d87232006-09-28 11:05:40 +1000500 lrp = XFS_ALLOC_REC_ADDR(left, lrecs + 1, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
Eric Sandeen91d87232006-09-28 11:05:40 +1000502 memcpy(lrp, rrp, rrecs * sizeof(*lrp));
503 xfs_alloc_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505 /*
506 * If we joined with the left neighbor, set the buffer in the
507 * cursor to the left block, and fix up the index.
508 */
509 if (bp != lbp) {
510 xfs_btree_setbuf(cur, level, lbp);
Eric Sandeen91d87232006-09-28 11:05:40 +1000511 cur->bc_ptrs[level] += lrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 /*
514 * If we joined with the right neighbor and there's a level above
515 * us, increment the cursor at that level.
516 */
517 else if (level + 1 < cur->bc_nlevels &&
Christoph Hellwig637aa502008-10-30 16:55:45 +1100518 (error = xfs_btree_increment(cur, level + 1, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return error;
520 /*
521 * Fix up the number of records in the surviving block.
522 */
Eric Sandeen91d87232006-09-28 11:05:40 +1000523 lrecs += rrecs;
524 left->bb_numrecs = cpu_to_be16(lrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /*
526 * Fix up the right block pointer in the surviving block, and log it.
527 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100528 left->bb_rightsib = right->bb_rightsib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
530 /*
531 * If there is a right sibling now, make it point to the
532 * remaining block.
533 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100534 if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 xfs_alloc_block_t *rrblock;
536 xfs_buf_t *rrbp;
537
538 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +1100539 cur->bc_private.a.agno, be32_to_cpu(left->bb_rightsib), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 &rrbp, XFS_ALLOC_BTREE_REF)))
541 return error;
542 rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
543 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
544 return error;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100545 rrblock->bb_leftsib = cpu_to_be32(lbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
547 }
548 /*
549 * Free the deleting block by putting it on the freelist.
550 */
David Chinner92821e22007-05-24 15:26:31 +1000551 error = xfs_alloc_put_freelist(cur->bc_tp,
552 cur->bc_private.a.agbp, NULL, rbno, 1);
553 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return error;
555 /*
556 * Since blocks move to the free list without the coordination
557 * used in xfs_bmap_finish, we can't allow block to be available
558 * for reallocation and non-transaction writing (user data)
559 * until we know that the transaction that moved it to the free
560 * list is permanently on disk. We track the blocks by declaring
561 * these blocks as "busy"; the busy list is maintained on a
562 * per-ag basis and each transaction records which entries
563 * should be removed when the iclog commits to disk. If a
564 * busy block is allocated, the iclog is pushed up to the
565 * LSN that freed the block.
566 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100567 xfs_alloc_mark_busy(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 xfs_trans_agbtree_delta(cur->bc_tp, -1);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /*
571 * Adjust the current level's cursor so that we're left referring
572 * to the right node, after we're done.
573 * If this leaves the ptr value 0 our caller will fix it up.
574 */
575 if (level > 0)
576 cur->bc_ptrs[level]--;
577 /*
578 * Return value means the next level up has something to do.
579 */
580 *stat = 2;
581 return 0;
582
583error0:
584 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
585 return error;
586}
587
588/*
589 * Insert one record/level. Return information to the caller
590 * allowing the next level up to proceed if necessary.
591 */
592STATIC int /* error */
593xfs_alloc_insrec(
594 xfs_btree_cur_t *cur, /* btree cursor */
595 int level, /* level to insert record at */
596 xfs_agblock_t *bnop, /* i/o: block number inserted */
597 xfs_alloc_rec_t *recp, /* i/o: record data inserted */
598 xfs_btree_cur_t **curp, /* output: new cursor replacing cur */
599 int *stat) /* output: success/failure */
600{
601 xfs_agf_t *agf; /* allocation group freelist header */
602 xfs_alloc_block_t *block; /* btree block record/key lives in */
603 xfs_buf_t *bp; /* buffer for block */
604 int error; /* error return value */
605 int i; /* loop index */
606 xfs_alloc_key_t key; /* key value being inserted */
607 xfs_alloc_key_t *kp; /* pointer to btree keys */
608 xfs_agblock_t nbno; /* block number of allocated block */
609 xfs_btree_cur_t *ncur; /* new cursor to be used at next lvl */
610 xfs_alloc_key_t nkey; /* new key value, from split */
611 xfs_alloc_rec_t nrec; /* new record value, for caller */
Eric Sandeen91d87232006-09-28 11:05:40 +1000612 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int optr; /* old ptr value */
614 xfs_alloc_ptr_t *pp; /* pointer to btree addresses */
615 int ptr; /* index in btree block for this rec */
616 xfs_alloc_rec_t *rp; /* pointer to btree records */
617
Christoph Hellwig16259e72005-11-02 15:11:25 +1100618 ASSERT(be32_to_cpu(recp->ar_blockcount) > 0);
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100619
620 /*
621 * GCC doesn't understand the (arguably complex) control flow in
622 * this function and complains about uninitialized structure fields
623 * without this.
624 */
625 memset(&nrec, 0, sizeof(nrec));
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /*
628 * If we made it to the root level, allocate a new root block
629 * and we're done.
630 */
631 if (level >= cur->bc_nlevels) {
632 XFS_STATS_INC(xs_abt_insrec);
633 if ((error = xfs_alloc_newroot(cur, &i)))
634 return error;
635 *bnop = NULLAGBLOCK;
636 *stat = i;
637 return 0;
638 }
639 /*
640 * Make a key out of the record data to be inserted, and save it.
641 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100642 key.ar_startblock = recp->ar_startblock;
643 key.ar_blockcount = recp->ar_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 optr = ptr = cur->bc_ptrs[level];
645 /*
646 * If we're off the left edge, return failure.
647 */
648 if (ptr == 0) {
649 *stat = 0;
650 return 0;
651 }
652 XFS_STATS_INC(xs_abt_insrec);
653 /*
654 * Get pointers to the btree buffer and block.
655 */
656 bp = cur->bc_bufs[level];
657 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
Eric Sandeen91d87232006-09-28 11:05:40 +1000658 numrecs = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659#ifdef DEBUG
660 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
661 return error;
662 /*
663 * Check that the new entry is being inserted in the right place.
664 */
Eric Sandeen91d87232006-09-28 11:05:40 +1000665 if (ptr <= numrecs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (level == 0) {
667 rp = XFS_ALLOC_REC_ADDR(block, ptr, cur);
668 xfs_btree_check_rec(cur->bc_btnum, recp, rp);
669 } else {
670 kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur);
671 xfs_btree_check_key(cur->bc_btnum, &key, kp);
672 }
673 }
674#endif
675 nbno = NULLAGBLOCK;
Nathan Scott1121b212006-09-28 10:58:40 +1000676 ncur = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /*
678 * If the block is full, we can't insert the new entry until we
679 * make the block un-full.
680 */
Eric Sandeen91d87232006-09-28 11:05:40 +1000681 if (numrecs == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /*
683 * First, try shifting an entry to the right neighbor.
684 */
Christoph Hellwig9eaead52008-10-30 16:56:43 +1100685 if ((error = xfs_btree_rshift(cur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return error;
687 if (i) {
688 /* nothing */
689 }
690 /*
691 * Next, try shifting an entry to the left neighbor.
692 */
693 else {
694 if ((error = xfs_alloc_lshift(cur, level, &i)))
695 return error;
696 if (i)
697 optr = ptr = cur->bc_ptrs[level];
698 else {
699 /*
700 * Next, try splitting the current block in
701 * half. If this works we have to re-set our
702 * variables because we could be in a
703 * different block now.
704 */
705 if ((error = xfs_alloc_split(cur, level, &nbno,
706 &nkey, &ncur, &i)))
707 return error;
708 if (i) {
709 bp = cur->bc_bufs[level];
710 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
711#ifdef DEBUG
712 if ((error =
713 xfs_btree_check_sblock(cur,
714 block, level, bp)))
715 return error;
716#endif
717 ptr = cur->bc_ptrs[level];
Christoph Hellwig16259e72005-11-02 15:11:25 +1100718 nrec.ar_startblock = nkey.ar_startblock;
719 nrec.ar_blockcount = nkey.ar_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721 /*
722 * Otherwise the insert fails.
723 */
724 else {
725 *stat = 0;
726 return 0;
727 }
728 }
729 }
730 }
731 /*
732 * At this point we know there's room for our new entry in the block
733 * we're pointing at.
734 */
Eric Sandeen91d87232006-09-28 11:05:40 +1000735 numrecs = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (level > 0) {
737 /*
738 * It's a non-leaf entry. Make a hole for the new data
739 * in the key and ptr regions of the block.
740 */
741 kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
742 pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
743#ifdef DEBUG
Eric Sandeen91d87232006-09-28 11:05:40 +1000744 for (i = numrecs; i >= ptr; i--) {
Christoph Hellwig16259e72005-11-02 15:11:25 +1100745 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(pp[i - 1]), level)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return error;
747 }
748#endif
749 memmove(&kp[ptr], &kp[ptr - 1],
Eric Sandeen91d87232006-09-28 11:05:40 +1000750 (numrecs - ptr + 1) * sizeof(*kp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 memmove(&pp[ptr], &pp[ptr - 1],
Eric Sandeen91d87232006-09-28 11:05:40 +1000752 (numrecs - ptr + 1) * sizeof(*pp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753#ifdef DEBUG
754 if ((error = xfs_btree_check_sptr(cur, *bnop, level)))
755 return error;
756#endif
757 /*
758 * Now stuff the new data in, bump numrecs and log the new data.
759 */
760 kp[ptr - 1] = key;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100761 pp[ptr - 1] = cpu_to_be32(*bnop);
Eric Sandeen91d87232006-09-28 11:05:40 +1000762 numrecs++;
763 block->bb_numrecs = cpu_to_be16(numrecs);
764 xfs_alloc_log_keys(cur, bp, ptr, numrecs);
765 xfs_alloc_log_ptrs(cur, bp, ptr, numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766#ifdef DEBUG
Eric Sandeen91d87232006-09-28 11:05:40 +1000767 if (ptr < numrecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 xfs_btree_check_key(cur->bc_btnum, kp + ptr - 1,
769 kp + ptr);
770#endif
771 } else {
772 /*
773 * It's a leaf entry. Make a hole for the new record.
774 */
775 rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
776 memmove(&rp[ptr], &rp[ptr - 1],
Eric Sandeen91d87232006-09-28 11:05:40 +1000777 (numrecs - ptr + 1) * sizeof(*rp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /*
779 * Now stuff the new record in, bump numrecs
780 * and log the new data.
781 */
Christoph Hellwigc38e5e82006-09-28 10:57:17 +1000782 rp[ptr - 1] = *recp;
Eric Sandeen91d87232006-09-28 11:05:40 +1000783 numrecs++;
784 block->bb_numrecs = cpu_to_be16(numrecs);
785 xfs_alloc_log_recs(cur, bp, ptr, numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786#ifdef DEBUG
Eric Sandeen91d87232006-09-28 11:05:40 +1000787 if (ptr < numrecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 xfs_btree_check_rec(cur->bc_btnum, rp + ptr - 1,
789 rp + ptr);
790#endif
791 }
792 /*
793 * Log the new number of records in the btree header.
794 */
795 xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
796 /*
797 * If we inserted at the start of a block, update the parents' keys.
798 */
Christoph Hellwig38bb7422008-10-30 16:56:22 +1100799 if (optr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)&key, level + 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return error;
801 /*
802 * Look to see if the longest extent in the allocation group
803 * needs to be updated.
804 */
805
806 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
807 if (level == 0 &&
808 cur->bc_btnum == XFS_BTNUM_CNT &&
Christoph Hellwig16259e72005-11-02 15:11:25 +1100809 be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
810 be32_to_cpu(recp->ar_blockcount) > be32_to_cpu(agf->agf_longest)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /*
812 * If this is a leaf in the by-size btree and there
813 * is no right sibling block and this block is bigger
814 * than the previous longest block, update it.
815 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100816 agf->agf_longest = recp->ar_blockcount;
817 cur->bc_mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest
818 = be32_to_cpu(recp->ar_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
820 XFS_AGF_LONGEST);
821 }
822 /*
823 * Return the new block number, if any.
824 * If there is one, give back a record value and a cursor too.
825 */
826 *bnop = nbno;
827 if (nbno != NULLAGBLOCK) {
Christoph Hellwigc38e5e82006-09-28 10:57:17 +1000828 *recp = nrec;
829 *curp = ncur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831 *stat = 1;
832 return 0;
833}
834
835/*
836 * Log header fields from a btree block.
837 */
838STATIC void
839xfs_alloc_log_block(
840 xfs_trans_t *tp, /* transaction pointer */
841 xfs_buf_t *bp, /* buffer containing btree block */
842 int fields) /* mask of fields: XFS_BB_... */
843{
844 int first; /* first byte offset logged */
845 int last; /* last byte offset logged */
846 static const short offsets[] = { /* table of offsets */
847 offsetof(xfs_alloc_block_t, bb_magic),
848 offsetof(xfs_alloc_block_t, bb_level),
849 offsetof(xfs_alloc_block_t, bb_numrecs),
850 offsetof(xfs_alloc_block_t, bb_leftsib),
851 offsetof(xfs_alloc_block_t, bb_rightsib),
852 sizeof(xfs_alloc_block_t)
853 };
854
855 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
856 xfs_trans_log_buf(tp, bp, first, last);
857}
858
859/*
860 * Log keys from a btree block (nonleaf).
861 */
862STATIC void
863xfs_alloc_log_keys(
864 xfs_btree_cur_t *cur, /* btree cursor */
865 xfs_buf_t *bp, /* buffer containing btree block */
866 int kfirst, /* index of first key to log */
867 int klast) /* index of last key to log */
868{
869 xfs_alloc_block_t *block; /* btree block to log from */
870 int first; /* first byte offset logged */
871 xfs_alloc_key_t *kp; /* key pointer in btree block */
872 int last; /* last byte offset logged */
873
874 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
875 kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
876 first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
877 last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
878 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
879}
880
881/*
882 * Log block pointer fields from a btree block (nonleaf).
883 */
884STATIC void
885xfs_alloc_log_ptrs(
886 xfs_btree_cur_t *cur, /* btree cursor */
887 xfs_buf_t *bp, /* buffer containing btree block */
888 int pfirst, /* index of first pointer to log */
889 int plast) /* index of last pointer to log */
890{
891 xfs_alloc_block_t *block; /* btree block to log from */
892 int first; /* first byte offset logged */
893 int last; /* last byte offset logged */
894 xfs_alloc_ptr_t *pp; /* block-pointer pointer in btree blk */
895
896 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
897 pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
898 first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
899 last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
900 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
901}
902
903/*
904 * Log records from a btree block (leaf).
905 */
906STATIC void
907xfs_alloc_log_recs(
908 xfs_btree_cur_t *cur, /* btree cursor */
909 xfs_buf_t *bp, /* buffer containing btree block */
910 int rfirst, /* index of first record to log */
911 int rlast) /* index of last record to log */
912{
913 xfs_alloc_block_t *block; /* btree block to log from */
914 int first; /* first byte offset logged */
915 int last; /* last byte offset logged */
916 xfs_alloc_rec_t *rp; /* record pointer for btree block */
917
918
919 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
920 rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
921#ifdef DEBUG
922 {
923 xfs_agf_t *agf;
924 xfs_alloc_rec_t *p;
925
926 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
927 for (p = &rp[rfirst - 1]; p <= &rp[rlast - 1]; p++)
Christoph Hellwig16259e72005-11-02 15:11:25 +1100928 ASSERT(be32_to_cpu(p->ar_startblock) +
929 be32_to_cpu(p->ar_blockcount) <=
930 be32_to_cpu(agf->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932#endif
933 first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
934 last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
935 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
936}
937
938/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 * Move 1 record left from cur/level if possible.
940 * Update cur to reflect the new path.
941 */
942STATIC int /* error */
943xfs_alloc_lshift(
944 xfs_btree_cur_t *cur, /* btree cursor */
945 int level, /* level to shift record on */
946 int *stat) /* success/failure */
947{
948 int error; /* error return value */
949#ifdef DEBUG
950 int i; /* loop index */
951#endif
952 xfs_alloc_key_t key; /* key value for leaf level upward */
953 xfs_buf_t *lbp; /* buffer for left neighbor block */
954 xfs_alloc_block_t *left; /* left neighbor btree block */
955 int nrec; /* new number of left block entries */
956 xfs_buf_t *rbp; /* buffer for right (current) block */
957 xfs_alloc_block_t *right; /* right (current) btree block */
958 xfs_alloc_key_t *rkp=NULL; /* key pointer for right block */
959 xfs_alloc_ptr_t *rpp=NULL; /* address pointer for right block */
960 xfs_alloc_rec_t *rrp=NULL; /* record pointer for right block */
961
962 /*
963 * Set up variables for this block as "right".
964 */
965 rbp = cur->bc_bufs[level];
966 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
967#ifdef DEBUG
968 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
969 return error;
970#endif
971 /*
972 * If we've got no left sibling then we can't shift an entry left.
973 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100974 if (be32_to_cpu(right->bb_leftsib) == NULLAGBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 *stat = 0;
976 return 0;
977 }
978 /*
979 * If the cursor entry is the one that would be moved, don't
980 * do it... it's too complicated.
981 */
982 if (cur->bc_ptrs[level] <= 1) {
983 *stat = 0;
984 return 0;
985 }
986 /*
987 * Set up the left neighbor as "left".
988 */
989 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +1100990 cur->bc_private.a.agno, be32_to_cpu(right->bb_leftsib),
991 0, &lbp, XFS_ALLOC_BTREE_REF)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return error;
993 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
994 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
995 return error;
996 /*
997 * If it's full, it can't take another entry.
998 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100999 if (be16_to_cpu(left->bb_numrecs) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 *stat = 0;
1001 return 0;
1002 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11001003 nrec = be16_to_cpu(left->bb_numrecs) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /*
1005 * If non-leaf, copy a key and a ptr to the left block.
1006 */
1007 if (level > 0) {
1008 xfs_alloc_key_t *lkp; /* key pointer for left block */
1009 xfs_alloc_ptr_t *lpp; /* address pointer for left block */
1010
1011 lkp = XFS_ALLOC_KEY_ADDR(left, nrec, cur);
1012 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1013 *lkp = *rkp;
1014 xfs_alloc_log_keys(cur, lbp, nrec, nrec);
1015 lpp = XFS_ALLOC_PTR_ADDR(left, nrec, cur);
1016 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1017#ifdef DEBUG
Christoph Hellwig16259e72005-11-02 15:11:25 +11001018 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(*rpp), level)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return error;
1020#endif
Christoph Hellwigc38e5e82006-09-28 10:57:17 +10001021 *lpp = *rpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 xfs_alloc_log_ptrs(cur, lbp, nrec, nrec);
1023 xfs_btree_check_key(cur->bc_btnum, lkp - 1, lkp);
1024 }
1025 /*
1026 * If leaf, copy a record to the left block.
1027 */
1028 else {
1029 xfs_alloc_rec_t *lrp; /* record pointer for left block */
1030
1031 lrp = XFS_ALLOC_REC_ADDR(left, nrec, cur);
1032 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1033 *lrp = *rrp;
1034 xfs_alloc_log_recs(cur, lbp, nrec, nrec);
1035 xfs_btree_check_rec(cur->bc_btnum, lrp - 1, lrp);
1036 }
1037 /*
1038 * Bump and log left's numrecs, decrement and log right's numrecs.
1039 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001040 be16_add_cpu(&left->bb_numrecs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001042 be16_add_cpu(&right->bb_numrecs, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
1044 /*
1045 * Slide the contents of right down one entry.
1046 */
1047 if (level > 0) {
1048#ifdef DEBUG
Christoph Hellwig16259e72005-11-02 15:11:25 +11001049 for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
1050 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i + 1]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 level)))
1052 return error;
1053 }
1054#endif
Christoph Hellwig16259e72005-11-02 15:11:25 +11001055 memmove(rkp, rkp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1056 memmove(rpp, rpp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1057 xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1058 xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 } else {
Christoph Hellwig16259e72005-11-02 15:11:25 +11001060 memmove(rrp, rrp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1061 xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1062 key.ar_startblock = rrp->ar_startblock;
1063 key.ar_blockcount = rrp->ar_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 rkp = &key;
1065 }
1066 /*
1067 * Update the parent key values of right.
1068 */
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001069 if ((error = xfs_btree_updkey(cur, (union xfs_btree_key *)rkp, level + 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return error;
1071 /*
1072 * Slide the cursor value left one.
1073 */
1074 cur->bc_ptrs[level]--;
1075 *stat = 1;
1076 return 0;
1077}
1078
1079/*
1080 * Allocate a new root block, fill it in.
1081 */
1082STATIC int /* error */
1083xfs_alloc_newroot(
1084 xfs_btree_cur_t *cur, /* btree cursor */
1085 int *stat) /* success/failure */
1086{
1087 int error; /* error return value */
1088 xfs_agblock_t lbno; /* left block number */
1089 xfs_buf_t *lbp; /* left btree buffer */
1090 xfs_alloc_block_t *left; /* left btree block */
1091 xfs_mount_t *mp; /* mount structure */
1092 xfs_agblock_t nbno; /* new block number */
1093 xfs_buf_t *nbp; /* new (root) buffer */
1094 xfs_alloc_block_t *new; /* new (root) btree block */
1095 int nptr; /* new value for key index, 1 or 2 */
1096 xfs_agblock_t rbno; /* right block number */
1097 xfs_buf_t *rbp; /* right btree buffer */
1098 xfs_alloc_block_t *right; /* right btree block */
1099
1100 mp = cur->bc_mp;
1101
1102 ASSERT(cur->bc_nlevels < XFS_AG_MAXLEVELS(mp));
1103 /*
1104 * Get a buffer from the freelist blocks, for the new root.
1105 */
David Chinner92821e22007-05-24 15:26:31 +10001106 error = xfs_alloc_get_freelist(cur->bc_tp,
1107 cur->bc_private.a.agbp, &nbno, 1);
1108 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return error;
1110 /*
1111 * None available, we fail.
1112 */
1113 if (nbno == NULLAGBLOCK) {
1114 *stat = 0;
1115 return 0;
1116 }
1117 xfs_trans_agbtree_delta(cur->bc_tp, 1);
1118 nbp = xfs_btree_get_bufs(mp, cur->bc_tp, cur->bc_private.a.agno, nbno,
1119 0);
1120 new = XFS_BUF_TO_ALLOC_BLOCK(nbp);
1121 /*
1122 * Set the root data in the a.g. freespace structure.
1123 */
1124 {
1125 xfs_agf_t *agf; /* a.g. freespace header */
1126 xfs_agnumber_t seqno;
1127
1128 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001129 agf->agf_roots[cur->bc_btnum] = cpu_to_be32(nbno);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001130 be32_add_cpu(&agf->agf_levels[cur->bc_btnum], 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001131 seqno = be32_to_cpu(agf->agf_seqno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 mp->m_perag[seqno].pagf_levels[cur->bc_btnum]++;
1133 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
1134 XFS_AGF_ROOTS | XFS_AGF_LEVELS);
1135 }
1136 /*
1137 * At the previous root level there are now two blocks: the old
1138 * root, and the new block generated when it was split.
1139 * We don't know which one the cursor is pointing at, so we
1140 * set up variables "left" and "right" for each case.
1141 */
1142 lbp = cur->bc_bufs[cur->bc_nlevels - 1];
1143 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1144#ifdef DEBUG
1145 if ((error = xfs_btree_check_sblock(cur, left, cur->bc_nlevels - 1, lbp)))
1146 return error;
1147#endif
Christoph Hellwig16259e72005-11-02 15:11:25 +11001148 if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 /*
1150 * Our block is left, pick up the right block.
1151 */
1152 lbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(lbp));
Christoph Hellwig16259e72005-11-02 15:11:25 +11001153 rbno = be32_to_cpu(left->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1155 cur->bc_private.a.agno, rbno, 0, &rbp,
1156 XFS_ALLOC_BTREE_REF)))
1157 return error;
1158 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1159 if ((error = xfs_btree_check_sblock(cur, right,
1160 cur->bc_nlevels - 1, rbp)))
1161 return error;
1162 nptr = 1;
1163 } else {
1164 /*
1165 * Our block is right, pick up the left block.
1166 */
1167 rbp = lbp;
1168 right = left;
1169 rbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(rbp));
Christoph Hellwig16259e72005-11-02 15:11:25 +11001170 lbno = be32_to_cpu(right->bb_leftsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1172 cur->bc_private.a.agno, lbno, 0, &lbp,
1173 XFS_ALLOC_BTREE_REF)))
1174 return error;
1175 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1176 if ((error = xfs_btree_check_sblock(cur, left,
1177 cur->bc_nlevels - 1, lbp)))
1178 return error;
1179 nptr = 2;
1180 }
1181 /*
1182 * Fill in the new block's btree header and log it.
1183 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001184 new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
1185 new->bb_level = cpu_to_be16(cur->bc_nlevels);
1186 new->bb_numrecs = cpu_to_be16(2);
1187 new->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1188 new->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 xfs_alloc_log_block(cur->bc_tp, nbp, XFS_BB_ALL_BITS);
1190 ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK);
1191 /*
1192 * Fill in the key data in the new root.
1193 */
1194 {
1195 xfs_alloc_key_t *kp; /* btree key pointer */
1196
1197 kp = XFS_ALLOC_KEY_ADDR(new, 1, cur);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001198 if (be16_to_cpu(left->bb_level) > 0) {
Christoph Hellwigc38e5e82006-09-28 10:57:17 +10001199 kp[0] = *XFS_ALLOC_KEY_ADDR(left, 1, cur);
1200 kp[1] = *XFS_ALLOC_KEY_ADDR(right, 1, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 } else {
1202 xfs_alloc_rec_t *rp; /* btree record pointer */
1203
1204 rp = XFS_ALLOC_REC_ADDR(left, 1, cur);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001205 kp[0].ar_startblock = rp->ar_startblock;
1206 kp[0].ar_blockcount = rp->ar_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 rp = XFS_ALLOC_REC_ADDR(right, 1, cur);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001208 kp[1].ar_startblock = rp->ar_startblock;
1209 kp[1].ar_blockcount = rp->ar_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211 }
1212 xfs_alloc_log_keys(cur, nbp, 1, 2);
1213 /*
1214 * Fill in the pointer data in the new root.
1215 */
1216 {
1217 xfs_alloc_ptr_t *pp; /* btree address pointer */
1218
1219 pp = XFS_ALLOC_PTR_ADDR(new, 1, cur);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001220 pp[0] = cpu_to_be32(lbno);
1221 pp[1] = cpu_to_be32(rbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223 xfs_alloc_log_ptrs(cur, nbp, 1, 2);
1224 /*
1225 * Fix up the cursor.
1226 */
1227 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
1228 cur->bc_ptrs[cur->bc_nlevels] = nptr;
1229 cur->bc_nlevels++;
1230 *stat = 1;
1231 return 0;
1232}
1233
1234/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 * Split cur/level block in half.
1236 * Return new block number and its first record (to be inserted into parent).
1237 */
1238STATIC int /* error */
1239xfs_alloc_split(
1240 xfs_btree_cur_t *cur, /* btree cursor */
1241 int level, /* level to split */
1242 xfs_agblock_t *bnop, /* output: block number allocated */
1243 xfs_alloc_key_t *keyp, /* output: first key of new block */
1244 xfs_btree_cur_t **curp, /* output: new cursor */
1245 int *stat) /* success/failure */
1246{
1247 int error; /* error return value */
1248 int i; /* loop index/record number */
1249 xfs_agblock_t lbno; /* left (current) block number */
1250 xfs_buf_t *lbp; /* buffer for left block */
1251 xfs_alloc_block_t *left; /* left (current) btree block */
1252 xfs_agblock_t rbno; /* right (new) block number */
1253 xfs_buf_t *rbp; /* buffer for right block */
1254 xfs_alloc_block_t *right; /* right (new) btree block */
1255
1256 /*
1257 * Allocate the new block from the freelist.
1258 * If we can't do it, we're toast. Give up.
1259 */
David Chinner92821e22007-05-24 15:26:31 +10001260 error = xfs_alloc_get_freelist(cur->bc_tp,
1261 cur->bc_private.a.agbp, &rbno, 1);
1262 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return error;
1264 if (rbno == NULLAGBLOCK) {
1265 *stat = 0;
1266 return 0;
1267 }
1268 xfs_trans_agbtree_delta(cur->bc_tp, 1);
1269 rbp = xfs_btree_get_bufs(cur->bc_mp, cur->bc_tp, cur->bc_private.a.agno,
1270 rbno, 0);
1271 /*
1272 * Set up the new block as "right".
1273 */
1274 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1275 /*
1276 * "Left" is the current (according to the cursor) block.
1277 */
1278 lbp = cur->bc_bufs[level];
1279 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1280#ifdef DEBUG
1281 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1282 return error;
1283#endif
1284 /*
1285 * Fill in the btree header for the new block.
1286 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001287 right->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
1288 right->bb_level = left->bb_level;
1289 right->bb_numrecs = cpu_to_be16(be16_to_cpu(left->bb_numrecs) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 /*
1291 * Make sure that if there's an odd number of entries now, that
1292 * each new block will have the same number of entries.
1293 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001294 if ((be16_to_cpu(left->bb_numrecs) & 1) &&
1295 cur->bc_ptrs[level] <= be16_to_cpu(right->bb_numrecs) + 1)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001296 be16_add_cpu(&right->bb_numrecs, 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001297 i = be16_to_cpu(left->bb_numrecs) - be16_to_cpu(right->bb_numrecs) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 /*
1299 * For non-leaf blocks, copy keys and addresses over to the new block.
1300 */
1301 if (level > 0) {
1302 xfs_alloc_key_t *lkp; /* left btree key pointer */
1303 xfs_alloc_ptr_t *lpp; /* left btree address pointer */
1304 xfs_alloc_key_t *rkp; /* right btree key pointer */
1305 xfs_alloc_ptr_t *rpp; /* right btree address pointer */
1306
1307 lkp = XFS_ALLOC_KEY_ADDR(left, i, cur);
1308 lpp = XFS_ALLOC_PTR_ADDR(left, i, cur);
1309 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1310 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1311#ifdef DEBUG
Christoph Hellwig16259e72005-11-02 15:11:25 +11001312 for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
1313 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return error;
1315 }
1316#endif
Christoph Hellwig16259e72005-11-02 15:11:25 +11001317 memcpy(rkp, lkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1318 memcpy(rpp, lpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1319 xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1320 xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 *keyp = *rkp;
1322 }
1323 /*
1324 * For leaf blocks, copy records over to the new block.
1325 */
1326 else {
1327 xfs_alloc_rec_t *lrp; /* left btree record pointer */
1328 xfs_alloc_rec_t *rrp; /* right btree record pointer */
1329
1330 lrp = XFS_ALLOC_REC_ADDR(left, i, cur);
1331 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001332 memcpy(rrp, lrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1333 xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1334 keyp->ar_startblock = rrp->ar_startblock;
1335 keyp->ar_blockcount = rrp->ar_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337 /*
1338 * Find the left block number by looking in the buffer.
1339 * Adjust numrecs, sibling pointers.
1340 */
1341 lbno = XFS_DADDR_TO_AGBNO(cur->bc_mp, XFS_BUF_ADDR(lbp));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001342 be16_add_cpu(&left->bb_numrecs, -(be16_to_cpu(right->bb_numrecs)));
Christoph Hellwig16259e72005-11-02 15:11:25 +11001343 right->bb_rightsib = left->bb_rightsib;
1344 left->bb_rightsib = cpu_to_be32(rbno);
1345 right->bb_leftsib = cpu_to_be32(lbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_ALL_BITS);
1347 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
1348 /*
1349 * If there's a block to the new block's right, make that block
1350 * point back to right instead of to left.
1351 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001352 if (be32_to_cpu(right->bb_rightsib) != NULLAGBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 xfs_alloc_block_t *rrblock; /* rr btree block */
1354 xfs_buf_t *rrbp; /* buffer for rrblock */
1355
1356 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11001357 cur->bc_private.a.agno, be32_to_cpu(right->bb_rightsib), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 &rrbp, XFS_ALLOC_BTREE_REF)))
1359 return error;
1360 rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
1361 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
1362 return error;
Christoph Hellwig16259e72005-11-02 15:11:25 +11001363 rrblock->bb_leftsib = cpu_to_be32(rbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
1365 }
1366 /*
1367 * If the cursor is really in the right block, move it there.
1368 * If it's just pointing past the last entry in left, then we'll
1369 * insert there, so don't change anything in that case.
1370 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001371 if (cur->bc_ptrs[level] > be16_to_cpu(left->bb_numrecs) + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 xfs_btree_setbuf(cur, level, rbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001373 cur->bc_ptrs[level] -= be16_to_cpu(left->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375 /*
1376 * If there are more levels, we'll need another cursor which refers to
1377 * the right block, no matter where this cursor was.
1378 */
1379 if (level + 1 < cur->bc_nlevels) {
1380 if ((error = xfs_btree_dup_cursor(cur, curp)))
1381 return error;
1382 (*curp)->bc_ptrs[level + 1]++;
1383 }
1384 *bnop = rbno;
1385 *stat = 1;
1386 return 0;
1387}
1388
1389/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 * Externally visible routines.
1391 */
1392
1393/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 * Delete the record pointed to by cur.
1395 * The cursor refers to the place where the record was (could be inserted)
1396 * when the operation returns.
1397 */
1398int /* error */
1399xfs_alloc_delete(
1400 xfs_btree_cur_t *cur, /* btree cursor */
1401 int *stat) /* success/failure */
1402{
1403 int error; /* error return value */
1404 int i; /* result code */
1405 int level; /* btree level */
1406
1407 /*
1408 * Go up the tree, starting at leaf level.
1409 * If 2 is returned then a join was done; go to the next level.
1410 * Otherwise we are done.
1411 */
1412 for (level = 0, i = 2; i == 2; level++) {
1413 if ((error = xfs_alloc_delrec(cur, level, &i)))
1414 return error;
1415 }
1416 if (i == 0) {
1417 for (level = 1; level < cur->bc_nlevels; level++) {
1418 if (cur->bc_ptrs[level] == 0) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001419 if ((error = xfs_btree_decrement(cur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return error;
1421 break;
1422 }
1423 }
1424 }
1425 *stat = i;
1426 return 0;
1427}
1428
1429/*
1430 * Get the data from the pointed-to record.
1431 */
1432int /* error */
1433xfs_alloc_get_rec(
1434 xfs_btree_cur_t *cur, /* btree cursor */
1435 xfs_agblock_t *bno, /* output: starting block of extent */
1436 xfs_extlen_t *len, /* output: length of extent */
1437 int *stat) /* output: success/failure */
1438{
1439 xfs_alloc_block_t *block; /* btree block */
1440#ifdef DEBUG
1441 int error; /* error return value */
1442#endif
1443 int ptr; /* record number */
1444
1445 ptr = cur->bc_ptrs[0];
1446 block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
1447#ifdef DEBUG
1448 if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
1449 return error;
1450#endif
1451 /*
1452 * Off the right end or left end, return failure.
1453 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001454 if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 *stat = 0;
1456 return 0;
1457 }
1458 /*
1459 * Point to the record and extract its data.
1460 */
1461 {
1462 xfs_alloc_rec_t *rec; /* record data */
1463
1464 rec = XFS_ALLOC_REC_ADDR(block, ptr, cur);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001465 *bno = be32_to_cpu(rec->ar_startblock);
1466 *len = be32_to_cpu(rec->ar_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
1468 *stat = 1;
1469 return 0;
1470}
1471
1472/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 * Insert the current record at the point referenced by cur.
1474 * The cursor may be inconsistent on return if splits have been done.
1475 */
1476int /* error */
1477xfs_alloc_insert(
1478 xfs_btree_cur_t *cur, /* btree cursor */
1479 int *stat) /* success/failure */
1480{
1481 int error; /* error return value */
1482 int i; /* result value, 0 for failure */
1483 int level; /* current level number in btree */
1484 xfs_agblock_t nbno; /* new block number (split result) */
1485 xfs_btree_cur_t *ncur; /* new cursor (split result) */
1486 xfs_alloc_rec_t nrec; /* record being inserted this level */
1487 xfs_btree_cur_t *pcur; /* previous level's cursor */
1488
1489 level = 0;
1490 nbno = NULLAGBLOCK;
Christoph Hellwig16259e72005-11-02 15:11:25 +11001491 nrec.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
1492 nrec.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
Nathan Scott1121b212006-09-28 10:58:40 +10001493 ncur = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 pcur = cur;
1495 /*
1496 * Loop going up the tree, starting at the leaf level.
1497 * Stop when we don't get a split block, that must mean that
1498 * the insert is finished with this level.
1499 */
1500 do {
1501 /*
1502 * Insert nrec/nbno into this level of the tree.
1503 * Note if we fail, nbno will be null.
1504 */
1505 if ((error = xfs_alloc_insrec(pcur, level++, &nbno, &nrec, &ncur,
1506 &i))) {
1507 if (pcur != cur)
1508 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
1509 return error;
1510 }
1511 /*
1512 * See if the cursor we just used is trash.
1513 * Can't trash the caller's cursor, but otherwise we should
1514 * if ncur is a new cursor or we're about to be done.
1515 */
1516 if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) {
1517 cur->bc_nlevels = pcur->bc_nlevels;
1518 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
1519 }
1520 /*
1521 * If we got a new cursor, switch to it.
1522 */
1523 if (ncur) {
1524 pcur = ncur;
Nathan Scott1121b212006-09-28 10:58:40 +10001525 ncur = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527 } while (nbno != NULLAGBLOCK);
1528 *stat = i;
1529 return 0;
1530}
1531
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001532STATIC struct xfs_btree_cur *
1533xfs_allocbt_dup_cursor(
1534 struct xfs_btree_cur *cur)
1535{
1536 return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
1537 cur->bc_private.a.agbp, cur->bc_private.a.agno,
1538 cur->bc_btnum);
1539}
1540
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001541/*
1542 * Update the longest extent in the AGF
1543 */
1544STATIC void
1545xfs_allocbt_update_lastrec(
1546 struct xfs_btree_cur *cur,
1547 struct xfs_btree_block *block,
1548 union xfs_btree_rec *rec,
1549 int ptr,
1550 int reason)
1551{
1552 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
1553 xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
1554 __be32 len;
1555
1556 ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
1557
1558 switch (reason) {
1559 case LASTREC_UPDATE:
1560 /*
1561 * If this is the last leaf block and it's the last record,
1562 * then update the size of the longest extent in the AG.
1563 */
1564 if (ptr != xfs_btree_get_numrecs(block))
1565 return;
1566 len = rec->alloc.ar_blockcount;
1567 break;
1568 default:
1569 ASSERT(0);
1570 return;
1571 }
1572
1573 agf->agf_longest = len;
1574 cur->bc_mp->m_perag[seqno].pagf_longest = be32_to_cpu(len);
1575 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
1576}
1577
Christoph Hellwigce5e42d2008-10-30 16:55:23 +11001578STATIC int
1579xfs_allocbt_get_maxrecs(
1580 struct xfs_btree_cur *cur,
1581 int level)
1582{
1583 return cur->bc_mp->m_alloc_mxr[level != 0];
1584}
1585
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001586STATIC void
1587xfs_allocbt_init_key_from_rec(
1588 union xfs_btree_key *key,
1589 union xfs_btree_rec *rec)
1590{
1591 ASSERT(rec->alloc.ar_startblock != 0);
1592
1593 key->alloc.ar_startblock = rec->alloc.ar_startblock;
1594 key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
1595}
1596
1597STATIC void
1598xfs_allocbt_init_ptr_from_cur(
1599 struct xfs_btree_cur *cur,
1600 union xfs_btree_ptr *ptr)
1601{
1602 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
1603
1604 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
1605 ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
1606
1607 ptr->s = agf->agf_roots[cur->bc_btnum];
1608}
1609
1610STATIC __int64_t
1611xfs_allocbt_key_diff(
1612 struct xfs_btree_cur *cur,
1613 union xfs_btree_key *key)
1614{
1615 xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
1616 xfs_alloc_key_t *kp = &key->alloc;
1617 __int64_t diff;
1618
1619 if (cur->bc_btnum == XFS_BTNUM_BNO) {
1620 return (__int64_t)be32_to_cpu(kp->ar_startblock) -
1621 rec->ar_startblock;
1622 }
1623
1624 diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
1625 if (diff)
1626 return diff;
1627
1628 return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
1629}
1630
Christoph Hellwig8c4ed632008-10-30 16:55:13 +11001631#ifdef XFS_BTREE_TRACE
1632ktrace_t *xfs_allocbt_trace_buf;
1633
1634STATIC void
1635xfs_allocbt_trace_enter(
1636 struct xfs_btree_cur *cur,
1637 const char *func,
1638 char *s,
1639 int type,
1640 int line,
1641 __psunsigned_t a0,
1642 __psunsigned_t a1,
1643 __psunsigned_t a2,
1644 __psunsigned_t a3,
1645 __psunsigned_t a4,
1646 __psunsigned_t a5,
1647 __psunsigned_t a6,
1648 __psunsigned_t a7,
1649 __psunsigned_t a8,
1650 __psunsigned_t a9,
1651 __psunsigned_t a10)
1652{
1653 ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
1654 (void *)func, (void *)s, NULL, (void *)cur,
1655 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
1656 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
1657 (void *)a8, (void *)a9, (void *)a10);
1658}
1659
1660STATIC void
1661xfs_allocbt_trace_cursor(
1662 struct xfs_btree_cur *cur,
1663 __uint32_t *s0,
1664 __uint64_t *l0,
1665 __uint64_t *l1)
1666{
1667 *s0 = cur->bc_private.a.agno;
1668 *l0 = cur->bc_rec.a.ar_startblock;
1669 *l1 = cur->bc_rec.a.ar_blockcount;
1670}
1671
1672STATIC void
1673xfs_allocbt_trace_key(
1674 struct xfs_btree_cur *cur,
1675 union xfs_btree_key *key,
1676 __uint64_t *l0,
1677 __uint64_t *l1)
1678{
1679 *l0 = be32_to_cpu(key->alloc.ar_startblock);
1680 *l1 = be32_to_cpu(key->alloc.ar_blockcount);
1681}
1682
1683STATIC void
1684xfs_allocbt_trace_record(
1685 struct xfs_btree_cur *cur,
1686 union xfs_btree_rec *rec,
1687 __uint64_t *l0,
1688 __uint64_t *l1,
1689 __uint64_t *l2)
1690{
1691 *l0 = be32_to_cpu(rec->alloc.ar_startblock);
1692 *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
1693 *l2 = 0;
1694}
1695#endif /* XFS_BTREE_TRACE */
1696
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001697static const struct xfs_btree_ops xfs_allocbt_ops = {
Christoph Hellwig65f1eae2008-10-30 16:55:34 +11001698 .rec_len = sizeof(xfs_alloc_rec_t),
1699 .key_len = sizeof(xfs_alloc_key_t),
1700
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001701 .dup_cursor = xfs_allocbt_dup_cursor,
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001702 .update_lastrec = xfs_allocbt_update_lastrec,
Christoph Hellwigce5e42d2008-10-30 16:55:23 +11001703 .get_maxrecs = xfs_allocbt_get_maxrecs,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001704 .init_key_from_rec = xfs_allocbt_init_key_from_rec,
1705 .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
1706 .key_diff = xfs_allocbt_key_diff,
Christoph Hellwig8c4ed632008-10-30 16:55:13 +11001707
1708#ifdef XFS_BTREE_TRACE
1709 .trace_enter = xfs_allocbt_trace_enter,
1710 .trace_cursor = xfs_allocbt_trace_cursor,
1711 .trace_key = xfs_allocbt_trace_key,
1712 .trace_record = xfs_allocbt_trace_record,
1713#endif
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001714};
1715
1716/*
1717 * Allocate a new allocation btree cursor.
1718 */
1719struct xfs_btree_cur * /* new alloc btree cursor */
1720xfs_allocbt_init_cursor(
1721 struct xfs_mount *mp, /* file system mount point */
1722 struct xfs_trans *tp, /* transaction pointer */
1723 struct xfs_buf *agbp, /* buffer for agf structure */
1724 xfs_agnumber_t agno, /* allocation group number */
1725 xfs_btnum_t btnum) /* btree identifier */
1726{
1727 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
1728 struct xfs_btree_cur *cur;
1729
1730 ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
1731
1732 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
1733
1734 cur->bc_tp = tp;
1735 cur->bc_mp = mp;
1736 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
1737 cur->bc_btnum = btnum;
1738 cur->bc_blocklog = mp->m_sb.sb_blocklog;
1739
1740 cur->bc_ops = &xfs_allocbt_ops;
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001741 if (btnum == XFS_BTNUM_CNT)
1742 cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001743
1744 cur->bc_private.a.agbp = agbp;
1745 cur->bc_private.a.agno = agno;
1746
1747 return cur;
1748}