blob: c76190a83e4e1bd854a14055682671bb26db8ac5 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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"
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +110038#include "xfs_btree_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_ialloc.h"
40#include "xfs_alloc.h"
41#include "xfs_error.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043STATIC void xfs_inobt_log_block(xfs_trans_t *, xfs_buf_t *, int);
44STATIC void xfs_inobt_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
45STATIC void xfs_inobt_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
46STATIC void xfs_inobt_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047STATIC int xfs_inobt_newroot(xfs_btree_cur_t *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * Single level of the xfs_inobt_delete record deletion routine.
51 * Delete record pointed to by cur/level.
52 * Remove the record from its block then rebalance the tree.
53 * Return 0 for error, 1 for done, 2 to go on to the next level.
54 */
55STATIC int /* error */
56xfs_inobt_delrec(
57 xfs_btree_cur_t *cur, /* btree cursor */
58 int level, /* level removing record from */
59 int *stat) /* fail/done/go-on */
60{
61 xfs_buf_t *agbp; /* buffer for a.g. inode header */
62 xfs_mount_t *mp; /* mount structure */
63 xfs_agi_t *agi; /* allocation group inode header */
64 xfs_inobt_block_t *block; /* btree block record/key lives in */
65 xfs_agblock_t bno; /* btree block number */
66 xfs_buf_t *bp; /* buffer for block */
67 int error; /* error return value */
68 int i; /* loop index */
69 xfs_inobt_key_t key; /* kp points here if block is level 0 */
70 xfs_inobt_key_t *kp = NULL; /* pointer to btree keys */
71 xfs_agblock_t lbno; /* left block's block number */
72 xfs_buf_t *lbp; /* left block's buffer pointer */
73 xfs_inobt_block_t *left; /* left btree block */
74 xfs_inobt_key_t *lkp; /* left block key pointer */
75 xfs_inobt_ptr_t *lpp; /* left block address pointer */
76 int lrecs = 0; /* number of records in left block */
77 xfs_inobt_rec_t *lrp; /* left block record pointer */
78 xfs_inobt_ptr_t *pp = NULL; /* pointer to btree addresses */
79 int ptr; /* index in btree block for this rec */
80 xfs_agblock_t rbno; /* right block's block number */
81 xfs_buf_t *rbp; /* right block's buffer pointer */
82 xfs_inobt_block_t *right; /* right btree block */
83 xfs_inobt_key_t *rkp; /* right block key pointer */
84 xfs_inobt_rec_t *rp; /* pointer to btree records */
85 xfs_inobt_ptr_t *rpp; /* right block address pointer */
86 int rrecs = 0; /* number of records in right block */
87 int numrecs;
88 xfs_inobt_rec_t *rrp; /* right block record pointer */
89 xfs_btree_cur_t *tcur; /* temporary btree cursor */
90
91 mp = cur->bc_mp;
92
93 /*
94 * Get the index of the entry being deleted, check for nothing there.
95 */
96 ptr = cur->bc_ptrs[level];
97 if (ptr == 0) {
98 *stat = 0;
99 return 0;
100 }
101
102 /*
103 * Get the buffer & block containing the record or key/ptr.
104 */
105 bp = cur->bc_bufs[level];
106 block = XFS_BUF_TO_INOBT_BLOCK(bp);
107#ifdef DEBUG
108 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
109 return error;
110#endif
111 /*
112 * Fail if we're off the end of the block.
113 */
114
Christoph Hellwig16259e72005-11-02 15:11:25 +1100115 numrecs = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (ptr > numrecs) {
117 *stat = 0;
118 return 0;
119 }
120 /*
121 * It's a nonleaf. Excise the key and ptr being deleted, by
122 * sliding the entries past them down one.
123 * Log the changed areas of the block.
124 */
125 if (level > 0) {
126 kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
127 pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
128#ifdef DEBUG
129 for (i = ptr; i < numrecs; i++) {
Christoph Hellwig16259e72005-11-02 15:11:25 +1100130 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(pp[i]), level)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return error;
132 }
133#endif
134 if (ptr < numrecs) {
135 memmove(&kp[ptr - 1], &kp[ptr],
136 (numrecs - ptr) * sizeof(*kp));
137 memmove(&pp[ptr - 1], &pp[ptr],
138 (numrecs - ptr) * sizeof(*kp));
139 xfs_inobt_log_keys(cur, bp, ptr, numrecs - 1);
140 xfs_inobt_log_ptrs(cur, bp, ptr, numrecs - 1);
141 }
142 }
143 /*
144 * It's a leaf. Excise the record being deleted, by sliding the
145 * entries past it down one. Log the changed areas of the block.
146 */
147 else {
148 rp = XFS_INOBT_REC_ADDR(block, 1, cur);
149 if (ptr < numrecs) {
150 memmove(&rp[ptr - 1], &rp[ptr],
151 (numrecs - ptr) * sizeof(*rp));
152 xfs_inobt_log_recs(cur, bp, ptr, numrecs - 1);
153 }
154 /*
155 * If it's the first record in the block, we'll need a key
156 * structure to pass up to the next level (updkey).
157 */
158 if (ptr == 1) {
159 key.ir_startino = rp->ir_startino;
160 kp = &key;
161 }
162 }
163 /*
164 * Decrement and log the number of entries in the block.
165 */
166 numrecs--;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100167 block->bb_numrecs = cpu_to_be16(numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 xfs_inobt_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
169 /*
170 * Is this the root level? If so, we're almost done.
171 */
172 if (level == cur->bc_nlevels - 1) {
173 /*
174 * If this is the root level,
175 * and there's only one entry left,
176 * and it's NOT the leaf level,
177 * then we can get rid of this level.
178 */
179 if (numrecs == 1 && level > 0) {
Christoph Hellwig169d6222008-08-13 16:25:27 +1000180 agbp = cur->bc_private.a.agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 agi = XFS_BUF_TO_AGI(agbp);
182 /*
183 * pp is still set to the first pointer in the block.
184 * Make it the new root of the btree.
185 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100186 bno = be32_to_cpu(agi->agi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 agi->agi_root = *pp;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800188 be32_add_cpu(&agi->agi_level, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /*
190 * Free the block.
191 */
192 if ((error = xfs_free_extent(cur->bc_tp,
Christoph Hellwig169d6222008-08-13 16:25:27 +1000193 XFS_AGB_TO_FSB(mp, cur->bc_private.a.agno, bno), 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return error;
195 xfs_trans_binval(cur->bc_tp, bp);
196 xfs_ialloc_log_agi(cur->bc_tp, agbp,
197 XFS_AGI_ROOT | XFS_AGI_LEVEL);
198 /*
199 * Update the cursor so there's one fewer level.
200 */
201 cur->bc_bufs[level] = NULL;
202 cur->bc_nlevels--;
203 } else if (level > 0 &&
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100204 (error = xfs_btree_decrement(cur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return error;
206 *stat = 1;
207 return 0;
208 }
209 /*
210 * If we deleted the leftmost entry in the block, update the
211 * key values above us in the tree.
212 */
Christoph Hellwig38bb7422008-10-30 16:56:22 +1100213 if (ptr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)kp, level + 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return error;
215 /*
216 * If the number of records remaining in the block is at least
217 * the minimum, we're done.
218 */
219 if (numrecs >= XFS_INOBT_BLOCK_MINRECS(level, cur)) {
220 if (level > 0 &&
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100221 (error = xfs_btree_decrement(cur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return error;
223 *stat = 1;
224 return 0;
225 }
226 /*
227 * Otherwise, we have to move some records around to keep the
228 * tree balanced. Look at the left and right sibling blocks to
229 * see if we can re-balance by moving only one record.
230 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100231 rbno = be32_to_cpu(block->bb_rightsib);
232 lbno = be32_to_cpu(block->bb_leftsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 bno = NULLAGBLOCK;
234 ASSERT(rbno != NULLAGBLOCK || lbno != NULLAGBLOCK);
235 /*
236 * Duplicate the cursor so our btree manipulations here won't
237 * disrupt the next level up.
238 */
239 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
240 return error;
241 /*
242 * If there's a right sibling, see if it's ok to shift an entry
243 * out of it.
244 */
245 if (rbno != NULLAGBLOCK) {
246 /*
247 * Move the temp cursor to the last entry in the next block.
248 * Actually any entry but the first would suffice.
249 */
250 i = xfs_btree_lastrec(tcur, level);
251 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig637aa502008-10-30 16:55:45 +1100252 if ((error = xfs_btree_increment(tcur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 goto error0;
254 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
255 i = xfs_btree_lastrec(tcur, level);
256 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
257 /*
258 * Grab a pointer to the block.
259 */
260 rbp = tcur->bc_bufs[level];
261 right = XFS_BUF_TO_INOBT_BLOCK(rbp);
262#ifdef DEBUG
263 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
264 goto error0;
265#endif
266 /*
267 * Grab the current block number, for future use.
268 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100269 bno = be32_to_cpu(right->bb_leftsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /*
271 * If right block is full enough so that removing one entry
272 * won't make it too empty, and left-shifting an entry out
273 * of right to us works, we're done.
274 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100275 if (be16_to_cpu(right->bb_numrecs) - 1 >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 XFS_INOBT_BLOCK_MINRECS(level, cur)) {
Christoph Hellwig687b8902008-10-30 16:56:53 +1100277 if ((error = xfs_btree_lshift(tcur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 goto error0;
279 if (i) {
Christoph Hellwig16259e72005-11-02 15:11:25 +1100280 ASSERT(be16_to_cpu(block->bb_numrecs) >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 XFS_INOBT_BLOCK_MINRECS(level, cur));
282 xfs_btree_del_cursor(tcur,
283 XFS_BTREE_NOERROR);
284 if (level > 0 &&
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100285 (error = xfs_btree_decrement(cur, level,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 &i)))
287 return error;
288 *stat = 1;
289 return 0;
290 }
291 }
292 /*
293 * Otherwise, grab the number of records in right for
294 * future reference, and fix up the temp cursor to point
295 * to our block again (last record).
296 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100297 rrecs = be16_to_cpu(right->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (lbno != NULLAGBLOCK) {
299 xfs_btree_firstrec(tcur, level);
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100300 if ((error = xfs_btree_decrement(tcur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 goto error0;
302 }
303 }
304 /*
305 * If there's a left sibling, see if it's ok to shift an entry
306 * out of it.
307 */
308 if (lbno != NULLAGBLOCK) {
309 /*
310 * Move the temp cursor to the first entry in the
311 * previous block.
312 */
313 xfs_btree_firstrec(tcur, level);
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100314 if ((error = xfs_btree_decrement(tcur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 goto error0;
316 xfs_btree_firstrec(tcur, level);
317 /*
318 * Grab a pointer to the block.
319 */
320 lbp = tcur->bc_bufs[level];
321 left = XFS_BUF_TO_INOBT_BLOCK(lbp);
322#ifdef DEBUG
323 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
324 goto error0;
325#endif
326 /*
327 * Grab the current block number, for future use.
328 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100329 bno = be32_to_cpu(left->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /*
331 * If left block is full enough so that removing one entry
332 * won't make it too empty, and right-shifting an entry out
333 * of left to us works, we're done.
334 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100335 if (be16_to_cpu(left->bb_numrecs) - 1 >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 XFS_INOBT_BLOCK_MINRECS(level, cur)) {
Christoph Hellwig9eaead52008-10-30 16:56:43 +1100337 if ((error = xfs_btree_rshift(tcur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 goto error0;
339 if (i) {
Christoph Hellwig16259e72005-11-02 15:11:25 +1100340 ASSERT(be16_to_cpu(block->bb_numrecs) >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 XFS_INOBT_BLOCK_MINRECS(level, cur));
342 xfs_btree_del_cursor(tcur,
343 XFS_BTREE_NOERROR);
344 if (level == 0)
345 cur->bc_ptrs[0]++;
346 *stat = 1;
347 return 0;
348 }
349 }
350 /*
351 * Otherwise, grab the number of records in right for
352 * future reference.
353 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100354 lrecs = be16_to_cpu(left->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356 /*
357 * Delete the temp cursor, we're done with it.
358 */
359 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
360 /*
361 * If here, we need to do a join to keep the tree balanced.
362 */
363 ASSERT(bno != NULLAGBLOCK);
364 /*
365 * See if we can join with the left neighbor block.
366 */
367 if (lbno != NULLAGBLOCK &&
368 lrecs + numrecs <= XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
369 /*
370 * Set "right" to be the starting block,
371 * "left" to be the left neighbor.
372 */
373 rbno = bno;
374 right = block;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100375 rrecs = be16_to_cpu(right->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 rbp = bp;
377 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
Christoph Hellwig169d6222008-08-13 16:25:27 +1000378 cur->bc_private.a.agno, lbno, 0, &lbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 XFS_INO_BTREE_REF)))
380 return error;
381 left = XFS_BUF_TO_INOBT_BLOCK(lbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100382 lrecs = be16_to_cpu(left->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
384 return error;
385 }
386 /*
387 * If that won't work, see if we can join with the right neighbor block.
388 */
389 else if (rbno != NULLAGBLOCK &&
390 rrecs + numrecs <= XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
391 /*
392 * Set "left" to be the starting block,
393 * "right" to be the right neighbor.
394 */
395 lbno = bno;
396 left = block;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100397 lrecs = be16_to_cpu(left->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 lbp = bp;
399 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
Christoph Hellwig169d6222008-08-13 16:25:27 +1000400 cur->bc_private.a.agno, rbno, 0, &rbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 XFS_INO_BTREE_REF)))
402 return error;
403 right = XFS_BUF_TO_INOBT_BLOCK(rbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100404 rrecs = be16_to_cpu(right->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
406 return error;
407 }
408 /*
409 * Otherwise, we can't fix the imbalance.
410 * Just return. This is probably a logic error, but it's not fatal.
411 */
412 else {
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100413 if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return error;
415 *stat = 1;
416 return 0;
417 }
418 /*
419 * We're now going to join "left" and "right" by moving all the stuff
420 * in "right" to "left" and deleting "right".
421 */
422 if (level > 0) {
423 /*
424 * It's a non-leaf. Move keys and pointers.
425 */
426 lkp = XFS_INOBT_KEY_ADDR(left, lrecs + 1, cur);
427 lpp = XFS_INOBT_PTR_ADDR(left, lrecs + 1, cur);
428 rkp = XFS_INOBT_KEY_ADDR(right, 1, cur);
429 rpp = XFS_INOBT_PTR_ADDR(right, 1, cur);
430#ifdef DEBUG
431 for (i = 0; i < rrecs; i++) {
Christoph Hellwig16259e72005-11-02 15:11:25 +1100432 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return error;
434 }
435#endif
436 memcpy(lkp, rkp, rrecs * sizeof(*lkp));
437 memcpy(lpp, rpp, rrecs * sizeof(*lpp));
438 xfs_inobt_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
439 xfs_inobt_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
440 } else {
441 /*
442 * It's a leaf. Move records.
443 */
444 lrp = XFS_INOBT_REC_ADDR(left, lrecs + 1, cur);
445 rrp = XFS_INOBT_REC_ADDR(right, 1, cur);
446 memcpy(lrp, rrp, rrecs * sizeof(*lrp));
447 xfs_inobt_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
448 }
449 /*
450 * If we joined with the left neighbor, set the buffer in the
451 * cursor to the left block, and fix up the index.
452 */
453 if (bp != lbp) {
454 xfs_btree_setbuf(cur, level, lbp);
455 cur->bc_ptrs[level] += lrecs;
456 }
457 /*
458 * If we joined with the right neighbor and there's a level above
459 * us, increment the cursor at that level.
460 */
461 else if (level + 1 < cur->bc_nlevels &&
Christoph Hellwig637aa502008-10-30 16:55:45 +1100462 (error = xfs_btree_increment(cur, level + 1, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return error;
464 /*
465 * Fix up the number of records in the surviving block.
466 */
467 lrecs += rrecs;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100468 left->bb_numrecs = cpu_to_be16(lrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /*
470 * Fix up the right block pointer in the surviving block, and log it.
471 */
472 left->bb_rightsib = right->bb_rightsib;
473 xfs_inobt_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
474 /*
475 * If there is a right sibling now, make it point to the
476 * remaining block.
477 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100478 if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 xfs_inobt_block_t *rrblock;
480 xfs_buf_t *rrbp;
481
482 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
Christoph Hellwig169d6222008-08-13 16:25:27 +1000483 cur->bc_private.a.agno, be32_to_cpu(left->bb_rightsib), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 &rrbp, XFS_INO_BTREE_REF)))
485 return error;
486 rrblock = XFS_BUF_TO_INOBT_BLOCK(rrbp);
487 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
488 return error;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100489 rrblock->bb_leftsib = cpu_to_be32(lbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 xfs_inobt_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
491 }
492 /*
493 * Free the deleting block.
494 */
495 if ((error = xfs_free_extent(cur->bc_tp, XFS_AGB_TO_FSB(mp,
Christoph Hellwig169d6222008-08-13 16:25:27 +1000496 cur->bc_private.a.agno, rbno), 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return error;
498 xfs_trans_binval(cur->bc_tp, rbp);
499 /*
500 * Readjust the ptr at this level if it's not a leaf, since it's
501 * still pointing at the deletion point, which makes the cursor
502 * inconsistent. If this makes the ptr 0, the caller fixes it up.
503 * We can't use decrement because it would change the next level up.
504 */
505 if (level > 0)
506 cur->bc_ptrs[level]--;
507 /*
508 * Return value means the next level up has something to do.
509 */
510 *stat = 2;
511 return 0;
512
513error0:
514 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
515 return error;
516}
517
518/*
519 * Insert one record/level. Return information to the caller
520 * allowing the next level up to proceed if necessary.
521 */
522STATIC int /* error */
523xfs_inobt_insrec(
524 xfs_btree_cur_t *cur, /* btree cursor */
525 int level, /* level to insert record at */
526 xfs_agblock_t *bnop, /* i/o: block number inserted */
527 xfs_inobt_rec_t *recp, /* i/o: record data inserted */
528 xfs_btree_cur_t **curp, /* output: new cursor replacing cur */
529 int *stat) /* success/failure */
530{
531 xfs_inobt_block_t *block; /* btree block record/key lives in */
532 xfs_buf_t *bp; /* buffer for block */
533 int error; /* error return value */
534 int i; /* loop index */
535 xfs_inobt_key_t key; /* key value being inserted */
536 xfs_inobt_key_t *kp=NULL; /* pointer to btree keys */
537 xfs_agblock_t nbno; /* block number of allocated block */
538 xfs_btree_cur_t *ncur; /* new cursor to be used at next lvl */
539 xfs_inobt_key_t nkey; /* new key value, from split */
540 xfs_inobt_rec_t nrec; /* new record value, for caller */
541 int numrecs;
542 int optr; /* old ptr value */
543 xfs_inobt_ptr_t *pp; /* pointer to btree addresses */
544 int ptr; /* index in btree block for this rec */
545 xfs_inobt_rec_t *rp=NULL; /* pointer to btree records */
546
547 /*
Christoph Hellwig5bde1ba2005-11-02 15:06:18 +1100548 * GCC doesn't understand the (arguably complex) control flow in
549 * this function and complains about uninitialized structure fields
550 * without this.
551 */
552 memset(&nrec, 0, sizeof(nrec));
553
554 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 * If we made it to the root level, allocate a new root block
556 * and we're done.
557 */
558 if (level >= cur->bc_nlevels) {
559 error = xfs_inobt_newroot(cur, &i);
560 *bnop = NULLAGBLOCK;
561 *stat = i;
562 return error;
563 }
564 /*
565 * Make a key out of the record data to be inserted, and save it.
566 */
Christoph Hellwig61a25842006-09-28 10:57:04 +1000567 key.ir_startino = recp->ir_startino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 optr = ptr = cur->bc_ptrs[level];
569 /*
570 * If we're off the left edge, return failure.
571 */
572 if (ptr == 0) {
573 *stat = 0;
574 return 0;
575 }
576 /*
577 * Get pointers to the btree buffer and block.
578 */
579 bp = cur->bc_bufs[level];
580 block = XFS_BUF_TO_INOBT_BLOCK(bp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100581 numrecs = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582#ifdef DEBUG
583 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
584 return error;
585 /*
586 * Check that the new entry is being inserted in the right place.
587 */
588 if (ptr <= numrecs) {
589 if (level == 0) {
590 rp = XFS_INOBT_REC_ADDR(block, ptr, cur);
591 xfs_btree_check_rec(cur->bc_btnum, recp, rp);
592 } else {
593 kp = XFS_INOBT_KEY_ADDR(block, ptr, cur);
594 xfs_btree_check_key(cur->bc_btnum, &key, kp);
595 }
596 }
597#endif
598 nbno = NULLAGBLOCK;
Nathan Scott1121b212006-09-28 10:58:40 +1000599 ncur = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /*
601 * If the block is full, we can't insert the new entry until we
602 * make the block un-full.
603 */
604 if (numrecs == XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
605 /*
606 * First, try shifting an entry to the right neighbor.
607 */
Christoph Hellwig9eaead52008-10-30 16:56:43 +1100608 if ((error = xfs_btree_rshift(cur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return error;
610 if (i) {
611 /* nothing */
612 }
613 /*
614 * Next, try shifting an entry to the left neighbor.
615 */
616 else {
Christoph Hellwig687b8902008-10-30 16:56:53 +1100617 if ((error = xfs_btree_lshift(cur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return error;
619 if (i) {
620 optr = ptr = cur->bc_ptrs[level];
621 } else {
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +1100622 union xfs_btree_ptr bno = { .s = cpu_to_be32(nbno) };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /*
624 * Next, try splitting the current block
625 * in half. If this works we have to
626 * re-set our variables because
627 * we could be in a different block now.
628 */
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +1100629 if ((error = xfs_btree_split(cur, level, &bno,
630 (union xfs_btree_key *)&nkey,
631 &ncur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return error;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +1100633 nbno = be32_to_cpu(bno.s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (i) {
635 bp = cur->bc_bufs[level];
636 block = XFS_BUF_TO_INOBT_BLOCK(bp);
637#ifdef DEBUG
638 if ((error = xfs_btree_check_sblock(cur,
639 block, level, bp)))
640 return error;
641#endif
642 ptr = cur->bc_ptrs[level];
Christoph Hellwig61a25842006-09-28 10:57:04 +1000643 nrec.ir_startino = nkey.ir_startino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 } else {
645 /*
646 * Otherwise the insert fails.
647 */
648 *stat = 0;
649 return 0;
650 }
651 }
652 }
653 }
654 /*
655 * At this point we know there's room for our new entry in the block
656 * we're pointing at.
657 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100658 numrecs = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (level > 0) {
660 /*
661 * It's a non-leaf entry. Make a hole for the new data
662 * in the key and ptr regions of the block.
663 */
664 kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
665 pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
666#ifdef DEBUG
667 for (i = numrecs; i >= ptr; i--) {
Christoph Hellwig16259e72005-11-02 15:11:25 +1100668 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(pp[i - 1]), level)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return error;
670 }
671#endif
672 memmove(&kp[ptr], &kp[ptr - 1],
673 (numrecs - ptr + 1) * sizeof(*kp));
674 memmove(&pp[ptr], &pp[ptr - 1],
675 (numrecs - ptr + 1) * sizeof(*pp));
676 /*
677 * Now stuff the new data in, bump numrecs and log the new data.
678 */
679#ifdef DEBUG
680 if ((error = xfs_btree_check_sptr(cur, *bnop, level)))
681 return error;
682#endif
Christoph Hellwigc38e5e82006-09-28 10:57:17 +1000683 kp[ptr - 1] = key;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100684 pp[ptr - 1] = cpu_to_be32(*bnop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 numrecs++;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100686 block->bb_numrecs = cpu_to_be16(numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 xfs_inobt_log_keys(cur, bp, ptr, numrecs);
688 xfs_inobt_log_ptrs(cur, bp, ptr, numrecs);
689 } else {
690 /*
691 * It's a leaf entry. Make a hole for the new record.
692 */
693 rp = XFS_INOBT_REC_ADDR(block, 1, cur);
694 memmove(&rp[ptr], &rp[ptr - 1],
695 (numrecs - ptr + 1) * sizeof(*rp));
696 /*
697 * Now stuff the new record in, bump numrecs
698 * and log the new data.
699 */
Christoph Hellwigc38e5e82006-09-28 10:57:17 +1000700 rp[ptr - 1] = *recp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 numrecs++;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100702 block->bb_numrecs = cpu_to_be16(numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 xfs_inobt_log_recs(cur, bp, ptr, numrecs);
704 }
705 /*
706 * Log the new number of records in the btree header.
707 */
708 xfs_inobt_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
709#ifdef DEBUG
710 /*
711 * Check that the key/record is in the right place, now.
712 */
713 if (ptr < numrecs) {
714 if (level == 0)
715 xfs_btree_check_rec(cur->bc_btnum, rp + ptr - 1,
716 rp + ptr);
717 else
718 xfs_btree_check_key(cur->bc_btnum, kp + ptr - 1,
719 kp + ptr);
720 }
721#endif
722 /*
723 * If we inserted at the start of a block, update the parents' keys.
724 */
Christoph Hellwig38bb7422008-10-30 16:56:22 +1100725 if (optr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)&key, level + 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return error;
727 /*
728 * Return the new block number, if any.
729 * If there is one, give back a record value and a cursor too.
730 */
731 *bnop = nbno;
732 if (nbno != NULLAGBLOCK) {
Christoph Hellwigc38e5e82006-09-28 10:57:17 +1000733 *recp = nrec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 *curp = ncur;
735 }
736 *stat = 1;
737 return 0;
738}
739
740/*
741 * Log header fields from a btree block.
742 */
743STATIC void
744xfs_inobt_log_block(
745 xfs_trans_t *tp, /* transaction pointer */
746 xfs_buf_t *bp, /* buffer containing btree block */
747 int fields) /* mask of fields: XFS_BB_... */
748{
749 int first; /* first byte offset logged */
750 int last; /* last byte offset logged */
751 static const short offsets[] = { /* table of offsets */
752 offsetof(xfs_inobt_block_t, bb_magic),
753 offsetof(xfs_inobt_block_t, bb_level),
754 offsetof(xfs_inobt_block_t, bb_numrecs),
755 offsetof(xfs_inobt_block_t, bb_leftsib),
756 offsetof(xfs_inobt_block_t, bb_rightsib),
757 sizeof(xfs_inobt_block_t)
758 };
759
760 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
761 xfs_trans_log_buf(tp, bp, first, last);
762}
763
764/*
765 * Log keys from a btree block (nonleaf).
766 */
767STATIC void
768xfs_inobt_log_keys(
769 xfs_btree_cur_t *cur, /* btree cursor */
770 xfs_buf_t *bp, /* buffer containing btree block */
771 int kfirst, /* index of first key to log */
772 int klast) /* index of last key to log */
773{
774 xfs_inobt_block_t *block; /* btree block to log from */
775 int first; /* first byte offset logged */
776 xfs_inobt_key_t *kp; /* key pointer in btree block */
777 int last; /* last byte offset logged */
778
779 block = XFS_BUF_TO_INOBT_BLOCK(bp);
780 kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
781 first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
782 last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
783 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
784}
785
786/*
787 * Log block pointer fields from a btree block (nonleaf).
788 */
789STATIC void
790xfs_inobt_log_ptrs(
791 xfs_btree_cur_t *cur, /* btree cursor */
792 xfs_buf_t *bp, /* buffer containing btree block */
793 int pfirst, /* index of first pointer to log */
794 int plast) /* index of last pointer to log */
795{
796 xfs_inobt_block_t *block; /* btree block to log from */
797 int first; /* first byte offset logged */
798 int last; /* last byte offset logged */
799 xfs_inobt_ptr_t *pp; /* block-pointer pointer in btree blk */
800
801 block = XFS_BUF_TO_INOBT_BLOCK(bp);
802 pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
803 first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
804 last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
805 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
806}
807
808/*
809 * Log records from a btree block (leaf).
810 */
811STATIC void
812xfs_inobt_log_recs(
813 xfs_btree_cur_t *cur, /* btree cursor */
814 xfs_buf_t *bp, /* buffer containing btree block */
815 int rfirst, /* index of first record to log */
816 int rlast) /* index of last record to log */
817{
818 xfs_inobt_block_t *block; /* btree block to log from */
819 int first; /* first byte offset logged */
820 int last; /* last byte offset logged */
821 xfs_inobt_rec_t *rp; /* record pointer for btree block */
822
823 block = XFS_BUF_TO_INOBT_BLOCK(bp);
824 rp = XFS_INOBT_REC_ADDR(block, 1, cur);
825 first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
826 last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
827 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
828}
829
830/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 * Allocate a new root block, fill it in.
832 */
833STATIC int /* error */
834xfs_inobt_newroot(
835 xfs_btree_cur_t *cur, /* btree cursor */
836 int *stat) /* success/failure */
837{
838 xfs_agi_t *agi; /* a.g. inode header */
839 xfs_alloc_arg_t args; /* allocation argument structure */
840 xfs_inobt_block_t *block; /* one half of the old root block */
841 xfs_buf_t *bp; /* buffer containing block */
842 int error; /* error return value */
843 xfs_inobt_key_t *kp; /* btree key pointer */
844 xfs_agblock_t lbno; /* left block number */
845 xfs_buf_t *lbp; /* left buffer pointer */
846 xfs_inobt_block_t *left; /* left btree block */
847 xfs_buf_t *nbp; /* new (root) buffer */
848 xfs_inobt_block_t *new; /* new (root) btree block */
849 int nptr; /* new value for key index, 1 or 2 */
850 xfs_inobt_ptr_t *pp; /* btree address pointer */
851 xfs_agblock_t rbno; /* right block number */
852 xfs_buf_t *rbp; /* right buffer pointer */
853 xfs_inobt_block_t *right; /* right btree block */
854 xfs_inobt_rec_t *rp; /* btree record pointer */
855
856 ASSERT(cur->bc_nlevels < XFS_IN_MAXLEVELS(cur->bc_mp));
857
858 /*
859 * Get a block & a buffer.
860 */
Christoph Hellwig169d6222008-08-13 16:25:27 +1000861 agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 args.tp = cur->bc_tp;
863 args.mp = cur->bc_mp;
Christoph Hellwig169d6222008-08-13 16:25:27 +1000864 args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.a.agno,
Christoph Hellwig16259e72005-11-02 15:11:25 +1100865 be32_to_cpu(agi->agi_root));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 args.mod = args.minleft = args.alignment = args.total = args.wasdel =
867 args.isfl = args.userdata = args.minalignslop = 0;
868 args.minlen = args.maxlen = args.prod = 1;
869 args.type = XFS_ALLOCTYPE_NEAR_BNO;
870 if ((error = xfs_alloc_vextent(&args)))
871 return error;
872 /*
873 * None available, we fail.
874 */
875 if (args.fsbno == NULLFSBLOCK) {
876 *stat = 0;
877 return 0;
878 }
879 ASSERT(args.len == 1);
880 nbp = xfs_btree_get_bufs(args.mp, args.tp, args.agno, args.agbno, 0);
881 new = XFS_BUF_TO_INOBT_BLOCK(nbp);
882 /*
883 * Set the root data in the a.g. inode structure.
884 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100885 agi->agi_root = cpu_to_be32(args.agbno);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800886 be32_add_cpu(&agi->agi_level, 1);
Christoph Hellwig169d6222008-08-13 16:25:27 +1000887 xfs_ialloc_log_agi(args.tp, cur->bc_private.a.agbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 XFS_AGI_ROOT | XFS_AGI_LEVEL);
889 /*
890 * At the previous root level there are now two blocks: the old
891 * root, and the new block generated when it was split.
892 * We don't know which one the cursor is pointing at, so we
893 * set up variables "left" and "right" for each case.
894 */
895 bp = cur->bc_bufs[cur->bc_nlevels - 1];
896 block = XFS_BUF_TO_INOBT_BLOCK(bp);
897#ifdef DEBUG
898 if ((error = xfs_btree_check_sblock(cur, block, cur->bc_nlevels - 1, bp)))
899 return error;
900#endif
Christoph Hellwig16259e72005-11-02 15:11:25 +1100901 if (be32_to_cpu(block->bb_rightsib) != NULLAGBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /*
903 * Our block is left, pick up the right block.
904 */
905 lbp = bp;
906 lbno = XFS_DADDR_TO_AGBNO(args.mp, XFS_BUF_ADDR(lbp));
907 left = block;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100908 rbno = be32_to_cpu(left->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if ((error = xfs_btree_read_bufs(args.mp, args.tp, args.agno,
910 rbno, 0, &rbp, XFS_INO_BTREE_REF)))
911 return error;
912 bp = rbp;
913 right = XFS_BUF_TO_INOBT_BLOCK(rbp);
914 if ((error = xfs_btree_check_sblock(cur, right,
915 cur->bc_nlevels - 1, rbp)))
916 return error;
917 nptr = 1;
918 } else {
919 /*
920 * Our block is right, pick up the left block.
921 */
922 rbp = bp;
923 rbno = XFS_DADDR_TO_AGBNO(args.mp, XFS_BUF_ADDR(rbp));
924 right = block;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100925 lbno = be32_to_cpu(right->bb_leftsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if ((error = xfs_btree_read_bufs(args.mp, args.tp, args.agno,
927 lbno, 0, &lbp, XFS_INO_BTREE_REF)))
928 return error;
929 bp = lbp;
930 left = XFS_BUF_TO_INOBT_BLOCK(lbp);
931 if ((error = xfs_btree_check_sblock(cur, left,
932 cur->bc_nlevels - 1, lbp)))
933 return error;
934 nptr = 2;
935 }
936 /*
937 * Fill in the new block's btree header and log it.
938 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100939 new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
940 new->bb_level = cpu_to_be16(cur->bc_nlevels);
941 new->bb_numrecs = cpu_to_be16(2);
942 new->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
943 new->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 xfs_inobt_log_block(args.tp, nbp, XFS_BB_ALL_BITS);
945 ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK);
946 /*
947 * Fill in the key data in the new root.
948 */
949 kp = XFS_INOBT_KEY_ADDR(new, 1, cur);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100950 if (be16_to_cpu(left->bb_level) > 0) {
Christoph Hellwigc38e5e82006-09-28 10:57:17 +1000951 kp[0] = *XFS_INOBT_KEY_ADDR(left, 1, cur);
952 kp[1] = *XFS_INOBT_KEY_ADDR(right, 1, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 } else {
954 rp = XFS_INOBT_REC_ADDR(left, 1, cur);
Christoph Hellwig61a25842006-09-28 10:57:04 +1000955 kp[0].ir_startino = rp->ir_startino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 rp = XFS_INOBT_REC_ADDR(right, 1, cur);
Christoph Hellwig61a25842006-09-28 10:57:04 +1000957 kp[1].ir_startino = rp->ir_startino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959 xfs_inobt_log_keys(cur, nbp, 1, 2);
960 /*
961 * Fill in the pointer data in the new root.
962 */
963 pp = XFS_INOBT_PTR_ADDR(new, 1, cur);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100964 pp[0] = cpu_to_be32(lbno);
965 pp[1] = cpu_to_be32(rbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 xfs_inobt_log_ptrs(cur, nbp, 1, 2);
967 /*
968 * Fix up the cursor.
969 */
970 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
971 cur->bc_ptrs[cur->bc_nlevels] = nptr;
972 cur->bc_nlevels++;
973 *stat = 1;
974 return 0;
975}
976
977/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 * Externally visible routines.
979 */
980
981/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 * Delete the record pointed to by cur.
983 * The cursor refers to the place where the record was (could be inserted)
984 * when the operation returns.
985 */
986int /* error */
987xfs_inobt_delete(
988 xfs_btree_cur_t *cur, /* btree cursor */
989 int *stat) /* success/failure */
990{
991 int error;
992 int i; /* result code */
993 int level; /* btree level */
994
995 /*
996 * Go up the tree, starting at leaf level.
997 * If 2 is returned then a join was done; go to the next level.
998 * Otherwise we are done.
999 */
1000 for (level = 0, i = 2; i == 2; level++) {
1001 if ((error = xfs_inobt_delrec(cur, level, &i)))
1002 return error;
1003 }
1004 if (i == 0) {
1005 for (level = 1; level < cur->bc_nlevels; level++) {
1006 if (cur->bc_ptrs[level] == 0) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001007 if ((error = xfs_btree_decrement(cur, level, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return error;
1009 break;
1010 }
1011 }
1012 }
1013 *stat = i;
1014 return 0;
1015}
1016
1017
1018/*
1019 * Get the data from the pointed-to record.
1020 */
1021int /* error */
1022xfs_inobt_get_rec(
1023 xfs_btree_cur_t *cur, /* btree cursor */
1024 xfs_agino_t *ino, /* output: starting inode of chunk */
1025 __int32_t *fcnt, /* output: number of free inodes */
1026 xfs_inofree_t *free, /* output: free inode mask */
1027 int *stat) /* output: success/failure */
1028{
1029 xfs_inobt_block_t *block; /* btree block */
1030 xfs_buf_t *bp; /* buffer containing btree block */
1031#ifdef DEBUG
1032 int error; /* error return value */
1033#endif
1034 int ptr; /* record number */
1035 xfs_inobt_rec_t *rec; /* record data */
1036
1037 bp = cur->bc_bufs[0];
1038 ptr = cur->bc_ptrs[0];
1039 block = XFS_BUF_TO_INOBT_BLOCK(bp);
1040#ifdef DEBUG
1041 if ((error = xfs_btree_check_sblock(cur, block, 0, bp)))
1042 return error;
1043#endif
1044 /*
1045 * Off the right end or left end, return failure.
1046 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001047 if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 *stat = 0;
1049 return 0;
1050 }
1051 /*
1052 * Point to the record and extract its data.
1053 */
1054 rec = XFS_INOBT_REC_ADDR(block, ptr, cur);
Christoph Hellwig61a25842006-09-28 10:57:04 +10001055 *ino = be32_to_cpu(rec->ir_startino);
1056 *fcnt = be32_to_cpu(rec->ir_freecount);
1057 *free = be64_to_cpu(rec->ir_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 *stat = 1;
1059 return 0;
1060}
1061
1062/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 * Insert the current record at the point referenced by cur.
1064 * The cursor may be inconsistent on return if splits have been done.
1065 */
1066int /* error */
1067xfs_inobt_insert(
1068 xfs_btree_cur_t *cur, /* btree cursor */
1069 int *stat) /* success/failure */
1070{
1071 int error; /* error return value */
1072 int i; /* result value, 0 for failure */
1073 int level; /* current level number in btree */
1074 xfs_agblock_t nbno; /* new block number (split result) */
1075 xfs_btree_cur_t *ncur; /* new cursor (split result) */
1076 xfs_inobt_rec_t nrec; /* record being inserted this level */
1077 xfs_btree_cur_t *pcur; /* previous level's cursor */
1078
1079 level = 0;
1080 nbno = NULLAGBLOCK;
Christoph Hellwig61a25842006-09-28 10:57:04 +10001081 nrec.ir_startino = cpu_to_be32(cur->bc_rec.i.ir_startino);
1082 nrec.ir_freecount = cpu_to_be32(cur->bc_rec.i.ir_freecount);
1083 nrec.ir_free = cpu_to_be64(cur->bc_rec.i.ir_free);
Nathan Scott1121b212006-09-28 10:58:40 +10001084 ncur = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 pcur = cur;
1086 /*
1087 * Loop going up the tree, starting at the leaf level.
1088 * Stop when we don't get a split block, that must mean that
1089 * the insert is finished with this level.
1090 */
1091 do {
1092 /*
1093 * Insert nrec/nbno into this level of the tree.
1094 * Note if we fail, nbno will be null.
1095 */
1096 if ((error = xfs_inobt_insrec(pcur, level++, &nbno, &nrec, &ncur,
1097 &i))) {
1098 if (pcur != cur)
1099 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
1100 return error;
1101 }
1102 /*
1103 * See if the cursor we just used is trash.
1104 * Can't trash the caller's cursor, but otherwise we should
1105 * if ncur is a new cursor or we're about to be done.
1106 */
1107 if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) {
1108 cur->bc_nlevels = pcur->bc_nlevels;
1109 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
1110 }
1111 /*
1112 * If we got a new cursor, switch to it.
1113 */
1114 if (ncur) {
1115 pcur = ncur;
Nathan Scott1121b212006-09-28 10:58:40 +10001116 ncur = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118 } while (nbno != NULLAGBLOCK);
1119 *stat = i;
1120 return 0;
1121}
1122
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001123STATIC struct xfs_btree_cur *
1124xfs_inobt_dup_cursor(
1125 struct xfs_btree_cur *cur)
1126{
1127 return xfs_inobt_init_cursor(cur->bc_mp, cur->bc_tp,
1128 cur->bc_private.a.agbp, cur->bc_private.a.agno);
1129}
1130
Christoph Hellwigce5e42d2008-10-30 16:55:23 +11001131STATIC int
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001132xfs_inobt_alloc_block(
1133 struct xfs_btree_cur *cur,
1134 union xfs_btree_ptr *start,
1135 union xfs_btree_ptr *new,
1136 int length,
1137 int *stat)
1138{
1139 xfs_alloc_arg_t args; /* block allocation args */
1140 int error; /* error return value */
1141 xfs_agblock_t sbno = be32_to_cpu(start->s);
1142
1143 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1144
1145 memset(&args, 0, sizeof(args));
1146 args.tp = cur->bc_tp;
1147 args.mp = cur->bc_mp;
1148 args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.a.agno, sbno);
1149 args.minlen = 1;
1150 args.maxlen = 1;
1151 args.prod = 1;
1152 args.type = XFS_ALLOCTYPE_NEAR_BNO;
1153
1154 error = xfs_alloc_vextent(&args);
1155 if (error) {
1156 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1157 return error;
1158 }
1159 if (args.fsbno == NULLFSBLOCK) {
1160 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1161 *stat = 0;
1162 return 0;
1163 }
1164 ASSERT(args.len == 1);
1165 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1166
1167 new->s = cpu_to_be32(XFS_FSB_TO_AGBNO(args.mp, args.fsbno));
1168 *stat = 1;
1169 return 0;
1170}
1171
1172
1173STATIC int
Christoph Hellwigce5e42d2008-10-30 16:55:23 +11001174xfs_inobt_get_maxrecs(
1175 struct xfs_btree_cur *cur,
1176 int level)
1177{
1178 return cur->bc_mp->m_inobt_mxr[level != 0];
1179}
1180
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001181STATIC void
1182xfs_inobt_init_key_from_rec(
1183 union xfs_btree_key *key,
1184 union xfs_btree_rec *rec)
1185{
1186 key->inobt.ir_startino = rec->inobt.ir_startino;
1187}
1188
1189/*
1190 * intial value of ptr for lookup
1191 */
1192STATIC void
1193xfs_inobt_init_ptr_from_cur(
1194 struct xfs_btree_cur *cur,
1195 union xfs_btree_ptr *ptr)
1196{
1197 struct xfs_agi *agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp);
1198
1199 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agi->agi_seqno));
1200
1201 ptr->s = agi->agi_root;
1202}
1203
1204STATIC __int64_t
1205xfs_inobt_key_diff(
1206 struct xfs_btree_cur *cur,
1207 union xfs_btree_key *key)
1208{
1209 return (__int64_t)be32_to_cpu(key->inobt.ir_startino) -
1210 cur->bc_rec.i.ir_startino;
1211}
1212
Christoph Hellwig8c4ed632008-10-30 16:55:13 +11001213#ifdef XFS_BTREE_TRACE
1214ktrace_t *xfs_inobt_trace_buf;
1215
1216STATIC void
1217xfs_inobt_trace_enter(
1218 struct xfs_btree_cur *cur,
1219 const char *func,
1220 char *s,
1221 int type,
1222 int line,
1223 __psunsigned_t a0,
1224 __psunsigned_t a1,
1225 __psunsigned_t a2,
1226 __psunsigned_t a3,
1227 __psunsigned_t a4,
1228 __psunsigned_t a5,
1229 __psunsigned_t a6,
1230 __psunsigned_t a7,
1231 __psunsigned_t a8,
1232 __psunsigned_t a9,
1233 __psunsigned_t a10)
1234{
1235 ktrace_enter(xfs_inobt_trace_buf, (void *)(__psint_t)type,
1236 (void *)func, (void *)s, NULL, (void *)cur,
1237 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
1238 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
1239 (void *)a8, (void *)a9, (void *)a10);
1240}
1241
1242STATIC void
1243xfs_inobt_trace_cursor(
1244 struct xfs_btree_cur *cur,
1245 __uint32_t *s0,
1246 __uint64_t *l0,
1247 __uint64_t *l1)
1248{
1249 *s0 = cur->bc_private.a.agno;
1250 *l0 = cur->bc_rec.i.ir_startino;
1251 *l1 = cur->bc_rec.i.ir_free;
1252}
1253
1254STATIC void
1255xfs_inobt_trace_key(
1256 struct xfs_btree_cur *cur,
1257 union xfs_btree_key *key,
1258 __uint64_t *l0,
1259 __uint64_t *l1)
1260{
1261 *l0 = be32_to_cpu(key->inobt.ir_startino);
1262 *l1 = 0;
1263}
1264
1265STATIC void
1266xfs_inobt_trace_record(
1267 struct xfs_btree_cur *cur,
1268 union xfs_btree_rec *rec,
1269 __uint64_t *l0,
1270 __uint64_t *l1,
1271 __uint64_t *l2)
1272{
1273 *l0 = be32_to_cpu(rec->inobt.ir_startino);
1274 *l1 = be32_to_cpu(rec->inobt.ir_freecount);
1275 *l2 = be64_to_cpu(rec->inobt.ir_free);
1276}
1277#endif /* XFS_BTREE_TRACE */
1278
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001279static const struct xfs_btree_ops xfs_inobt_ops = {
Christoph Hellwig65f1eae2008-10-30 16:55:34 +11001280 .rec_len = sizeof(xfs_inobt_rec_t),
1281 .key_len = sizeof(xfs_inobt_key_t),
1282
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001283 .dup_cursor = xfs_inobt_dup_cursor,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001284 .alloc_block = xfs_inobt_alloc_block,
Christoph Hellwigce5e42d2008-10-30 16:55:23 +11001285 .get_maxrecs = xfs_inobt_get_maxrecs,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001286 .init_key_from_rec = xfs_inobt_init_key_from_rec,
1287 .init_ptr_from_cur = xfs_inobt_init_ptr_from_cur,
1288 .key_diff = xfs_inobt_key_diff,
Christoph Hellwig8c4ed632008-10-30 16:55:13 +11001289
1290#ifdef XFS_BTREE_TRACE
1291 .trace_enter = xfs_inobt_trace_enter,
1292 .trace_cursor = xfs_inobt_trace_cursor,
1293 .trace_key = xfs_inobt_trace_key,
1294 .trace_record = xfs_inobt_trace_record,
1295#endif
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001296};
1297
1298/*
1299 * Allocate a new inode btree cursor.
1300 */
1301struct xfs_btree_cur * /* new inode btree cursor */
1302xfs_inobt_init_cursor(
1303 struct xfs_mount *mp, /* file system mount point */
1304 struct xfs_trans *tp, /* transaction pointer */
1305 struct xfs_buf *agbp, /* buffer for agi structure */
1306 xfs_agnumber_t agno) /* allocation group number */
1307{
1308 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1309 struct xfs_btree_cur *cur;
1310
1311 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
1312
1313 cur->bc_tp = tp;
1314 cur->bc_mp = mp;
1315 cur->bc_nlevels = be32_to_cpu(agi->agi_level);
1316 cur->bc_btnum = XFS_BTNUM_INO;
1317 cur->bc_blocklog = mp->m_sb.sb_blocklog;
1318
1319 cur->bc_ops = &xfs_inobt_ops;
1320
1321 cur->bc_private.a.agbp = agbp;
1322 cur->bc_private.a.agno = agno;
1323
1324 return cur;
1325}