2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_inode_item.h"
38 #include "xfs_alloc.h"
39 #include "xfs_btree.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_itable.h"
43 #include "xfs_error.h"
44 #include "xfs_quota.h"
46 #if defined(XFS_BMBT_TRACE)
47 ktrace_t *xfs_bmbt_trace_buf;
51 * Prototypes for internal btree functions.
55 STATIC int xfs_bmbt_killroot(xfs_btree_cur_t *);
56 STATIC void xfs_bmbt_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
57 STATIC void xfs_bmbt_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
58 STATIC int xfs_bmbt_lshift(xfs_btree_cur_t *, int, int *);
59 STATIC int xfs_bmbt_rshift(xfs_btree_cur_t *, int, int *);
60 STATIC int xfs_bmbt_split(xfs_btree_cur_t *, int, xfs_fsblock_t *,
61 __uint64_t *, xfs_btree_cur_t **, int *);
62 STATIC int xfs_bmbt_updkey(xfs_btree_cur_t *, xfs_bmbt_key_t *, int);
65 #if defined(XFS_BMBT_TRACE)
67 static char ARGS[] = "args";
68 static char ENTRY[] = "entry";
69 static char ERROR[] = "error";
71 static char EXIT[] = "exit";
74 * Add a trace buffer entry for the arguments given to the routine,
99 ip = cur->bc_private.b.ip;
100 whichfork = cur->bc_private.b.whichfork;
101 ktrace_enter(xfs_bmbt_trace_buf,
102 (void *)((__psint_t)type | (whichfork << 8) | (line << 16)),
103 (void *)func, (void *)s, (void *)ip, (void *)cur,
104 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
105 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
106 (void *)a8, (void *)a9, (void *)a10);
107 ASSERT(ip->i_btrace);
108 ktrace_enter(ip->i_btrace,
109 (void *)((__psint_t)type | (whichfork << 8) | (line << 16)),
110 (void *)func, (void *)s, (void *)ip, (void *)cur,
111 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
112 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
113 (void *)a8, (void *)a9, (void *)a10);
116 * Add a trace buffer entry for arguments, for a buffer & 1 integer arg.
119 xfs_bmbt_trace_argbi(
121 xfs_btree_cur_t *cur,
126 xfs_bmbt_trace_enter(func, cur, ARGS, XFS_BMBT_KTRACE_ARGBI, line,
127 (__psunsigned_t)b, i, 0, 0,
133 * Add a trace buffer entry for arguments, for a buffer & 2 integer args.
136 xfs_bmbt_trace_argbii(
138 xfs_btree_cur_t *cur,
144 xfs_bmbt_trace_enter(func, cur, ARGS, XFS_BMBT_KTRACE_ARGBII, line,
145 (__psunsigned_t)b, i0, i1, 0,
151 * Add a trace buffer entry for arguments, for 3 block-length args
152 * and an integer arg.
155 xfs_bmbt_trace_argfffi(
157 xfs_btree_cur_t *cur,
164 xfs_bmbt_trace_enter(func, cur, ARGS, XFS_BMBT_KTRACE_ARGFFFI, line,
165 o >> 32, (int)o, b >> 32, (int)b,
166 i >> 32, (int)i, (int)j, 0,
171 * Add a trace buffer entry for arguments, for one integer arg.
176 xfs_btree_cur_t *cur,
180 xfs_bmbt_trace_enter(func, cur, ARGS, XFS_BMBT_KTRACE_ARGI, line,
187 * Add a trace buffer entry for arguments, for int, fsblock, key.
190 xfs_bmbt_trace_argifk(
192 xfs_btree_cur_t *cur,
198 xfs_bmbt_trace_enter(func, cur, ARGS, XFS_BMBT_KTRACE_ARGIFK, line,
199 i, (xfs_dfsbno_t)f >> 32, (int)f, o >> 32,
205 * Add a trace buffer entry for arguments, for int, fsblock, rec.
208 xfs_bmbt_trace_argifr(
210 xfs_btree_cur_t *cur,
223 xfs_bmbt_disk_get_all(r, &s);
224 o = (xfs_dfiloff_t)s.br_startoff;
225 b = (xfs_dfsbno_t)s.br_startblock;
227 xfs_bmbt_trace_enter(func, cur, ARGS, XFS_BMBT_KTRACE_ARGIFR, line,
228 i, d >> 32, (int)d, o >> 32,
229 (int)o, b >> 32, (int)b, c >> 32,
234 * Add a trace buffer entry for arguments, for int, key.
237 xfs_bmbt_trace_argik(
239 xfs_btree_cur_t *cur,
246 o = be64_to_cpu(k->br_startoff);
247 xfs_bmbt_trace_enter(func, cur, ARGS, XFS_BMBT_KTRACE_ARGIFK, line,
248 i, o >> 32, (int)o, 0,
254 * Add a trace buffer entry for the cursor/operation.
257 xfs_bmbt_trace_cursor(
259 xfs_btree_cur_t *cur,
265 xfs_bmbt_set_all(&r, &cur->bc_rec.b);
266 xfs_bmbt_trace_enter(func, cur, s, XFS_BMBT_KTRACE_CUR, line,
267 (cur->bc_nlevels << 24) | (cur->bc_private.b.flags << 16) |
268 cur->bc_private.b.allocated,
269 INT_GET(r.l0, ARCH_CONVERT) >> 32, (int)INT_GET(r.l0, ARCH_CONVERT), INT_GET(r.l1, ARCH_CONVERT) >> 32, (int)INT_GET(r.l1, ARCH_CONVERT),
270 (unsigned long)cur->bc_bufs[0], (unsigned long)cur->bc_bufs[1],
271 (unsigned long)cur->bc_bufs[2], (unsigned long)cur->bc_bufs[3],
272 (cur->bc_ptrs[0] << 16) | cur->bc_ptrs[1],
273 (cur->bc_ptrs[2] << 16) | cur->bc_ptrs[3]);
276 #define XFS_BMBT_TRACE_ARGBI(c,b,i) \
277 xfs_bmbt_trace_argbi(__FUNCTION__, c, b, i, __LINE__)
278 #define XFS_BMBT_TRACE_ARGBII(c,b,i,j) \
279 xfs_bmbt_trace_argbii(__FUNCTION__, c, b, i, j, __LINE__)
280 #define XFS_BMBT_TRACE_ARGFFFI(c,o,b,i,j) \
281 xfs_bmbt_trace_argfffi(__FUNCTION__, c, o, b, i, j, __LINE__)
282 #define XFS_BMBT_TRACE_ARGI(c,i) \
283 xfs_bmbt_trace_argi(__FUNCTION__, c, i, __LINE__)
284 #define XFS_BMBT_TRACE_ARGIFK(c,i,f,s) \
285 xfs_bmbt_trace_argifk(__FUNCTION__, c, i, f, s, __LINE__)
286 #define XFS_BMBT_TRACE_ARGIFR(c,i,f,r) \
287 xfs_bmbt_trace_argifr(__FUNCTION__, c, i, f, r, __LINE__)
288 #define XFS_BMBT_TRACE_ARGIK(c,i,k) \
289 xfs_bmbt_trace_argik(__FUNCTION__, c, i, k, __LINE__)
290 #define XFS_BMBT_TRACE_CURSOR(c,s) \
291 xfs_bmbt_trace_cursor(__FUNCTION__, c, s, __LINE__)
293 #define XFS_BMBT_TRACE_ARGBI(c,b,i)
294 #define XFS_BMBT_TRACE_ARGBII(c,b,i,j)
295 #define XFS_BMBT_TRACE_ARGFFFI(c,o,b,i,j)
296 #define XFS_BMBT_TRACE_ARGI(c,i)
297 #define XFS_BMBT_TRACE_ARGIFK(c,i,f,s)
298 #define XFS_BMBT_TRACE_ARGIFR(c,i,f,r)
299 #define XFS_BMBT_TRACE_ARGIK(c,i,k)
300 #define XFS_BMBT_TRACE_CURSOR(c,s)
301 #endif /* XFS_BMBT_TRACE */
305 * Internal functions.
309 * Delete record pointed to by cur/level.
311 STATIC int /* error */
313 xfs_btree_cur_t *cur,
315 int *stat) /* success/failure */
317 xfs_bmbt_block_t *block; /* bmap btree block */
318 xfs_fsblock_t bno; /* fs-relative block number */
319 xfs_buf_t *bp; /* buffer for block */
320 int error; /* error return value */
321 int i; /* loop counter */
322 int j; /* temp state */
323 xfs_bmbt_key_t key; /* bmap btree key */
324 xfs_bmbt_key_t *kp=NULL; /* pointer to bmap btree key */
325 xfs_fsblock_t lbno; /* left sibling block number */
326 xfs_buf_t *lbp; /* left buffer pointer */
327 xfs_bmbt_block_t *left; /* left btree block */
328 xfs_bmbt_key_t *lkp; /* left btree key */
329 xfs_bmbt_ptr_t *lpp; /* left address pointer */
330 int lrecs=0; /* left record count */
331 xfs_bmbt_rec_t *lrp; /* left record pointer */
332 xfs_mount_t *mp; /* file system mount point */
333 xfs_bmbt_ptr_t *pp; /* pointer to bmap block addr */
334 int ptr; /* key/record index */
335 xfs_fsblock_t rbno; /* right sibling block number */
336 xfs_buf_t *rbp; /* right buffer pointer */
337 xfs_bmbt_block_t *right; /* right btree block */
338 xfs_bmbt_key_t *rkp; /* right btree key */
339 xfs_bmbt_rec_t *rp; /* pointer to bmap btree rec */
340 xfs_bmbt_ptr_t *rpp; /* right address pointer */
341 xfs_bmbt_block_t *rrblock; /* right-right btree block */
342 xfs_buf_t *rrbp; /* right-right buffer pointer */
343 int rrecs=0; /* right record count */
344 xfs_bmbt_rec_t *rrp; /* right record pointer */
345 xfs_btree_cur_t *tcur; /* temporary btree cursor */
346 int numrecs; /* temporary numrec count */
347 int numlrecs, numrrecs;
349 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
350 XFS_BMBT_TRACE_ARGI(cur, level);
351 ptr = cur->bc_ptrs[level];
354 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
358 block = xfs_bmbt_get_block(cur, level, &bp);
359 numrecs = be16_to_cpu(block->bb_numrecs);
361 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
362 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
367 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
371 XFS_STATS_INC(xs_bmbt_delrec);
373 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
374 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
376 for (i = ptr; i < numrecs; i++) {
377 if ((error = xfs_btree_check_lptr_disk(cur, pp[i], level))) {
378 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
384 memmove(&kp[ptr - 1], &kp[ptr],
385 (numrecs - ptr) * sizeof(*kp));
386 memmove(&pp[ptr - 1], &pp[ptr], /* INT_: direct copy */
387 (numrecs - ptr) * sizeof(*pp));
388 xfs_bmbt_log_ptrs(cur, bp, ptr, numrecs - 1);
389 xfs_bmbt_log_keys(cur, bp, ptr, numrecs - 1);
392 rp = XFS_BMAP_REC_IADDR(block, 1, cur);
394 memmove(&rp[ptr - 1], &rp[ptr],
395 (numrecs - ptr) * sizeof(*rp));
396 xfs_bmbt_log_recs(cur, bp, ptr, numrecs - 1);
400 cpu_to_be64(xfs_bmbt_disk_get_startoff(rp));
405 block->bb_numrecs = cpu_to_be16(numrecs);
406 xfs_bmbt_log_block(cur, bp, XFS_BB_NUMRECS);
408 * We're at the root level.
409 * First, shrink the root block in-memory.
410 * Try to get rid of the next level down.
411 * If we can't then there's nothing left to do.
413 if (level == cur->bc_nlevels - 1) {
414 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
415 cur->bc_private.b.whichfork);
416 if ((error = xfs_bmbt_killroot(cur))) {
417 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
420 if (level > 0 && (error = xfs_bmbt_decrement(cur, level, &j))) {
421 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
424 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
428 if (ptr == 1 && (error = xfs_bmbt_updkey(cur, kp, level + 1))) {
429 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
432 if (numrecs >= XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
433 if (level > 0 && (error = xfs_bmbt_decrement(cur, level, &j))) {
434 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
437 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
441 rbno = be64_to_cpu(block->bb_rightsib);
442 lbno = be64_to_cpu(block->bb_leftsib);
444 * One child of root, need to get a chance to copy its contents
445 * into the root and delete it. Can't go up to next level,
446 * there's nothing to delete there.
448 if (lbno == NULLFSBLOCK && rbno == NULLFSBLOCK &&
449 level == cur->bc_nlevels - 2) {
450 if ((error = xfs_bmbt_killroot(cur))) {
451 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
454 if (level > 0 && (error = xfs_bmbt_decrement(cur, level, &i))) {
455 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
458 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
462 ASSERT(rbno != NULLFSBLOCK || lbno != NULLFSBLOCK);
463 if ((error = xfs_btree_dup_cursor(cur, &tcur))) {
464 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
468 if (rbno != NULLFSBLOCK) {
469 i = xfs_btree_lastrec(tcur, level);
470 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
471 if ((error = xfs_bmbt_increment(tcur, level, &i))) {
472 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
475 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
476 i = xfs_btree_lastrec(tcur, level);
477 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
478 rbp = tcur->bc_bufs[level];
479 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
481 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
482 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
486 bno = be64_to_cpu(right->bb_leftsib);
487 if (be16_to_cpu(right->bb_numrecs) - 1 >=
488 XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
489 if ((error = xfs_bmbt_lshift(tcur, level, &i))) {
490 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
494 ASSERT(be16_to_cpu(block->bb_numrecs) >=
495 XFS_BMAP_BLOCK_IMINRECS(level, tcur));
496 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
499 if ((error = xfs_bmbt_decrement(cur,
501 XFS_BMBT_TRACE_CURSOR(cur,
506 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
511 rrecs = be16_to_cpu(right->bb_numrecs);
512 if (lbno != NULLFSBLOCK) {
513 i = xfs_btree_firstrec(tcur, level);
514 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
515 if ((error = xfs_bmbt_decrement(tcur, level, &i))) {
516 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
519 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
522 if (lbno != NULLFSBLOCK) {
523 i = xfs_btree_firstrec(tcur, level);
524 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
526 * decrement to last in block
528 if ((error = xfs_bmbt_decrement(tcur, level, &i))) {
529 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
532 i = xfs_btree_firstrec(tcur, level);
533 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
534 lbp = tcur->bc_bufs[level];
535 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
537 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
538 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
542 bno = be64_to_cpu(left->bb_rightsib);
543 if (be16_to_cpu(left->bb_numrecs) - 1 >=
544 XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
545 if ((error = xfs_bmbt_rshift(tcur, level, &i))) {
546 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
550 ASSERT(be16_to_cpu(block->bb_numrecs) >=
551 XFS_BMAP_BLOCK_IMINRECS(level, tcur));
552 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
556 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
561 lrecs = be16_to_cpu(left->bb_numrecs);
563 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
566 ASSERT(bno != NULLFSBLOCK);
567 if (lbno != NULLFSBLOCK &&
568 lrecs + be16_to_cpu(block->bb_numrecs) <= XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
572 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, lbno, 0, &lbp,
573 XFS_BMAP_BTREE_REF))) {
574 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
577 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
578 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
579 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
582 } else if (rbno != NULLFSBLOCK &&
583 rrecs + be16_to_cpu(block->bb_numrecs) <=
584 XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
588 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, rbno, 0, &rbp,
589 XFS_BMAP_BTREE_REF))) {
590 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
593 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
594 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
595 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
598 lrecs = be16_to_cpu(left->bb_numrecs);
600 if (level > 0 && (error = xfs_bmbt_decrement(cur, level, &i))) {
601 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
604 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
608 numlrecs = be16_to_cpu(left->bb_numrecs);
609 numrrecs = be16_to_cpu(right->bb_numrecs);
611 lkp = XFS_BMAP_KEY_IADDR(left, numlrecs + 1, cur);
612 lpp = XFS_BMAP_PTR_IADDR(left, numlrecs + 1, cur);
613 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
614 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
616 for (i = 0; i < numrrecs; i++) {
617 if ((error = xfs_btree_check_lptr_disk(cur, rpp[i], level))) {
618 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
623 memcpy(lkp, rkp, numrrecs * sizeof(*lkp));
624 memcpy(lpp, rpp, numrrecs * sizeof(*lpp));
625 xfs_bmbt_log_keys(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
626 xfs_bmbt_log_ptrs(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
628 lrp = XFS_BMAP_REC_IADDR(left, numlrecs + 1, cur);
629 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
630 memcpy(lrp, rrp, numrrecs * sizeof(*lrp));
631 xfs_bmbt_log_recs(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
633 be16_add(&left->bb_numrecs, numrrecs);
634 left->bb_rightsib = right->bb_rightsib;
635 xfs_bmbt_log_block(cur, lbp, XFS_BB_RIGHTSIB | XFS_BB_NUMRECS);
636 if (be64_to_cpu(left->bb_rightsib) != NULLDFSBNO) {
637 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp,
638 be64_to_cpu(left->bb_rightsib),
639 0, &rrbp, XFS_BMAP_BTREE_REF))) {
640 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
643 rrblock = XFS_BUF_TO_BMBT_BLOCK(rrbp);
644 if ((error = xfs_btree_check_lblock(cur, rrblock, level, rrbp))) {
645 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
648 rrblock->bb_leftsib = cpu_to_be64(lbno);
649 xfs_bmbt_log_block(cur, rrbp, XFS_BB_LEFTSIB);
651 xfs_bmap_add_free(XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(rbp)), 1,
652 cur->bc_private.b.flist, mp);
653 cur->bc_private.b.ip->i_d.di_nblocks--;
654 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
655 XFS_TRANS_MOD_DQUOT_BYINO(mp, cur->bc_tp, cur->bc_private.b.ip,
656 XFS_TRANS_DQ_BCOUNT, -1L);
657 xfs_trans_binval(cur->bc_tp, rbp);
659 cur->bc_bufs[level] = lbp;
660 cur->bc_ptrs[level] += lrecs;
661 cur->bc_ra[level] = 0;
662 } else if ((error = xfs_bmbt_increment(cur, level + 1, &i))) {
663 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
667 cur->bc_ptrs[level]--;
668 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
674 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
679 * Insert one record/level. Return information to the caller
680 * allowing the next level up to proceed if necessary.
682 STATIC int /* error */
684 xfs_btree_cur_t *cur,
687 xfs_bmbt_rec_t *recp,
688 xfs_btree_cur_t **curp,
689 int *stat) /* no-go/done/continue */
691 xfs_bmbt_block_t *block; /* bmap btree block */
692 xfs_buf_t *bp; /* buffer for block */
693 int error; /* error return value */
694 int i; /* loop index */
695 xfs_bmbt_key_t key; /* bmap btree key */
696 xfs_bmbt_key_t *kp=NULL; /* pointer to bmap btree key */
697 int logflags; /* inode logging flags */
698 xfs_fsblock_t nbno; /* new block number */
699 struct xfs_btree_cur *ncur; /* new btree cursor */
700 __uint64_t startoff; /* new btree key value */
701 xfs_bmbt_rec_t nrec; /* new record count */
702 int optr; /* old key/record index */
703 xfs_bmbt_ptr_t *pp; /* pointer to bmap block addr */
704 int ptr; /* key/record index */
705 xfs_bmbt_rec_t *rp=NULL; /* pointer to bmap btree rec */
708 ASSERT(level < cur->bc_nlevels);
709 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
710 XFS_BMBT_TRACE_ARGIFR(cur, level, *bnop, recp);
712 key.br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(recp));
713 optr = ptr = cur->bc_ptrs[level];
715 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
719 XFS_STATS_INC(xs_bmbt_insrec);
720 block = xfs_bmbt_get_block(cur, level, &bp);
721 numrecs = be16_to_cpu(block->bb_numrecs);
723 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
724 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
727 if (ptr <= numrecs) {
729 rp = XFS_BMAP_REC_IADDR(block, ptr, cur);
730 xfs_btree_check_rec(XFS_BTNUM_BMAP, recp, rp);
732 kp = XFS_BMAP_KEY_IADDR(block, ptr, cur);
733 xfs_btree_check_key(XFS_BTNUM_BMAP, &key, kp);
738 if (numrecs == XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
739 if (numrecs < XFS_BMAP_BLOCK_DMAXRECS(level, cur)) {
741 * A root block, that can be made bigger.
743 xfs_iroot_realloc(cur->bc_private.b.ip, 1,
744 cur->bc_private.b.whichfork);
745 block = xfs_bmbt_get_block(cur, level, &bp);
746 } else if (level == cur->bc_nlevels - 1) {
747 if ((error = xfs_bmbt_newroot(cur, &logflags, stat)) ||
749 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
752 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
754 block = xfs_bmbt_get_block(cur, level, &bp);
756 if ((error = xfs_bmbt_rshift(cur, level, &i))) {
757 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
763 if ((error = xfs_bmbt_lshift(cur, level, &i))) {
764 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
768 optr = ptr = cur->bc_ptrs[level];
770 if ((error = xfs_bmbt_split(cur, level,
771 &nbno, &startoff, &ncur,
773 XFS_BMBT_TRACE_CURSOR(cur,
778 block = xfs_bmbt_get_block(
782 xfs_btree_check_lblock(cur,
783 block, level, bp))) {
784 XFS_BMBT_TRACE_CURSOR(
789 ptr = cur->bc_ptrs[level];
790 xfs_bmbt_disk_set_allf(&nrec,
794 XFS_BMBT_TRACE_CURSOR(cur,
803 numrecs = be16_to_cpu(block->bb_numrecs);
805 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
806 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
808 for (i = numrecs; i >= ptr; i--) {
809 if ((error = xfs_btree_check_lptr_disk(cur, pp[i - 1],
811 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
816 memmove(&kp[ptr], &kp[ptr - 1],
817 (numrecs - ptr + 1) * sizeof(*kp));
818 memmove(&pp[ptr], &pp[ptr - 1], /* INT_: direct copy */
819 (numrecs - ptr + 1) * sizeof(*pp));
821 if ((error = xfs_btree_check_lptr(cur, *bnop, level))) {
822 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
827 pp[ptr - 1] = cpu_to_be64(*bnop);
829 block->bb_numrecs = cpu_to_be16(numrecs);
830 xfs_bmbt_log_keys(cur, bp, ptr, numrecs);
831 xfs_bmbt_log_ptrs(cur, bp, ptr, numrecs);
833 rp = XFS_BMAP_REC_IADDR(block, 1, cur);
834 memmove(&rp[ptr], &rp[ptr - 1],
835 (numrecs - ptr + 1) * sizeof(*rp));
838 block->bb_numrecs = cpu_to_be16(numrecs);
839 xfs_bmbt_log_recs(cur, bp, ptr, numrecs);
841 xfs_bmbt_log_block(cur, bp, XFS_BB_NUMRECS);
845 xfs_btree_check_rec(XFS_BTNUM_BMAP, rp + ptr - 1,
848 xfs_btree_check_key(XFS_BTNUM_BMAP, kp + ptr - 1,
852 if (optr == 1 && (error = xfs_bmbt_updkey(cur, &key, level + 1))) {
853 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
857 if (nbno != NULLFSBLOCK) {
861 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
868 xfs_btree_cur_t *cur)
870 xfs_bmbt_block_t *block;
871 xfs_bmbt_block_t *cblock;
885 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
886 level = cur->bc_nlevels - 1;
889 * Don't deal with the root block needs to be a leaf case.
890 * We're just going to turn the thing back into extents anyway.
893 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
896 block = xfs_bmbt_get_block(cur, level, &cbp);
898 * Give up if the root has multiple children.
900 if (be16_to_cpu(block->bb_numrecs) != 1) {
901 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
905 * Only do this if the next level will fit.
906 * Then the data must be copied up to the inode,
907 * instead of freeing the root you free the next level.
909 cbp = cur->bc_bufs[level - 1];
910 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
911 if (be16_to_cpu(cblock->bb_numrecs) > XFS_BMAP_BLOCK_DMAXRECS(level, cur)) {
912 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
915 ASSERT(be64_to_cpu(cblock->bb_leftsib) == NULLDFSBNO);
916 ASSERT(be64_to_cpu(cblock->bb_rightsib) == NULLDFSBNO);
917 ip = cur->bc_private.b.ip;
918 ifp = XFS_IFORK_PTR(ip, cur->bc_private.b.whichfork);
919 ASSERT(XFS_BMAP_BLOCK_IMAXRECS(level, cur) ==
920 XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes));
921 i = (int)(be16_to_cpu(cblock->bb_numrecs) - XFS_BMAP_BLOCK_IMAXRECS(level, cur));
923 xfs_iroot_realloc(ip, i, cur->bc_private.b.whichfork);
924 block = ifp->if_broot;
926 be16_add(&block->bb_numrecs, i);
927 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
928 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
929 ckp = XFS_BMAP_KEY_IADDR(cblock, 1, cur);
930 memcpy(kp, ckp, be16_to_cpu(block->bb_numrecs) * sizeof(*kp));
931 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
932 cpp = XFS_BMAP_PTR_IADDR(cblock, 1, cur);
934 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
935 if ((error = xfs_btree_check_lptr_disk(cur, cpp[i], level - 1))) {
936 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
941 memcpy(pp, cpp, be16_to_cpu(block->bb_numrecs) * sizeof(*pp));
942 xfs_bmap_add_free(XFS_DADDR_TO_FSB(cur->bc_mp, XFS_BUF_ADDR(cbp)), 1,
943 cur->bc_private.b.flist, cur->bc_mp);
944 ip->i_d.di_nblocks--;
945 XFS_TRANS_MOD_DQUOT_BYINO(cur->bc_mp, cur->bc_tp, ip,
946 XFS_TRANS_DQ_BCOUNT, -1L);
947 xfs_trans_binval(cur->bc_tp, cbp);
948 cur->bc_bufs[level - 1] = NULL;
949 be16_add(&block->bb_level, -1);
950 xfs_trans_log_inode(cur->bc_tp, ip,
951 XFS_ILOG_CORE | XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
953 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
958 * Log key values from the btree block.
962 xfs_btree_cur_t *cur,
969 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
970 XFS_BMBT_TRACE_ARGBII(cur, bp, kfirst, klast);
973 xfs_bmbt_block_t *block;
978 block = XFS_BUF_TO_BMBT_BLOCK(bp);
979 kp = XFS_BMAP_KEY_DADDR(block, 1, cur);
980 first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
981 last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
982 xfs_trans_log_buf(tp, bp, first, last);
986 ip = cur->bc_private.b.ip;
987 xfs_trans_log_inode(tp, ip,
988 XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
990 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
994 * Log pointer values from the btree block.
998 xfs_btree_cur_t *cur,
1005 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1006 XFS_BMBT_TRACE_ARGBII(cur, bp, pfirst, plast);
1009 xfs_bmbt_block_t *block;
1014 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1015 pp = XFS_BMAP_PTR_DADDR(block, 1, cur);
1016 first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
1017 last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
1018 xfs_trans_log_buf(tp, bp, first, last);
1022 ip = cur->bc_private.b.ip;
1023 xfs_trans_log_inode(tp, ip,
1024 XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
1026 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1030 * Lookup the record. The cursor is made to point to it, based on dir.
1032 STATIC int /* error */
1034 xfs_btree_cur_t *cur,
1036 int *stat) /* success/failure */
1038 xfs_bmbt_block_t *block=NULL;
1042 int error; /* error return value */
1043 xfs_fsblock_t fsbno=0;
1047 xfs_bmbt_key_t *kkbase=NULL;
1048 xfs_bmbt_key_t *kkp;
1049 xfs_bmbt_rec_t *krbase=NULL;
1050 xfs_bmbt_rec_t *krp;
1055 xfs_bmbt_irec_t *rp;
1056 xfs_fileoff_t startoff;
1059 XFS_STATS_INC(xs_bmbt_lookup);
1060 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1061 XFS_BMBT_TRACE_ARGI(cur, (int)dir);
1064 rp = &cur->bc_rec.b;
1065 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1066 if (level < cur->bc_nlevels - 1) {
1067 d = XFS_FSB_TO_DADDR(mp, fsbno);
1068 bp = cur->bc_bufs[level];
1069 if (bp && XFS_BUF_ADDR(bp) != d)
1072 if ((error = xfs_btree_read_bufl(mp, tp, fsbno,
1073 0, &bp, XFS_BMAP_BTREE_REF))) {
1074 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1077 xfs_btree_setbuf(cur, level, bp);
1078 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1079 if ((error = xfs_btree_check_lblock(cur, block,
1081 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1085 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1087 block = xfs_bmbt_get_block(cur, level, &bp);
1092 kkbase = XFS_BMAP_KEY_IADDR(block, 1, cur);
1094 krbase = XFS_BMAP_REC_IADDR(block, 1, cur);
1096 if (!(high = be16_to_cpu(block->bb_numrecs))) {
1098 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1099 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1103 while (low <= high) {
1104 XFS_STATS_INC(xs_bmbt_compare);
1105 keyno = (low + high) >> 1;
1107 kkp = kkbase + keyno - 1;
1108 startoff = be64_to_cpu(kkp->br_startoff);
1110 krp = krbase + keyno - 1;
1111 startoff = xfs_bmbt_disk_get_startoff(krp);
1113 diff = (xfs_sfiloff_t)
1114 (startoff - rp->br_startoff);
1124 if (diff > 0 && --keyno < 1)
1126 pp = XFS_BMAP_PTR_IADDR(block, keyno, cur);
1127 fsbno = be64_to_cpu(*pp);
1129 if ((error = xfs_btree_check_lptr(cur, fsbno, level))) {
1130 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1134 cur->bc_ptrs[level] = keyno;
1137 if (dir != XFS_LOOKUP_LE && diff < 0) {
1140 * If ge search and we went off the end of the block, but it's
1141 * not the last block, we're in the wrong block.
1143 if (dir == XFS_LOOKUP_GE && keyno > be16_to_cpu(block->bb_numrecs) &&
1144 be64_to_cpu(block->bb_rightsib) != NULLDFSBNO) {
1145 cur->bc_ptrs[0] = keyno;
1146 if ((error = xfs_bmbt_increment(cur, 0, &i))) {
1147 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1150 XFS_WANT_CORRUPTED_RETURN(i == 1);
1151 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1156 else if (dir == XFS_LOOKUP_LE && diff > 0)
1158 cur->bc_ptrs[0] = keyno;
1159 if (keyno == 0 || keyno > be16_to_cpu(block->bb_numrecs)) {
1160 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1163 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1164 *stat = ((dir != XFS_LOOKUP_EQ) || (diff == 0));
1170 * Move 1 record left from cur/level if possible.
1171 * Update cur to reflect the new path.
1173 STATIC int /* error */
1175 xfs_btree_cur_t *cur,
1177 int *stat) /* success/failure */
1179 int error; /* error return value */
1181 int i; /* loop counter */
1183 xfs_bmbt_key_t key; /* bmap btree key */
1184 xfs_buf_t *lbp; /* left buffer pointer */
1185 xfs_bmbt_block_t *left; /* left btree block */
1186 xfs_bmbt_key_t *lkp=NULL; /* left btree key */
1187 xfs_bmbt_ptr_t *lpp; /* left address pointer */
1188 int lrecs; /* left record count */
1189 xfs_bmbt_rec_t *lrp=NULL; /* left record pointer */
1190 xfs_mount_t *mp; /* file system mount point */
1191 xfs_buf_t *rbp; /* right buffer pointer */
1192 xfs_bmbt_block_t *right; /* right btree block */
1193 xfs_bmbt_key_t *rkp=NULL; /* right btree key */
1194 xfs_bmbt_ptr_t *rpp=NULL; /* right address pointer */
1195 xfs_bmbt_rec_t *rrp=NULL; /* right record pointer */
1196 int rrecs; /* right record count */
1198 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1199 XFS_BMBT_TRACE_ARGI(cur, level);
1200 if (level == cur->bc_nlevels - 1) {
1201 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1205 rbp = cur->bc_bufs[level];
1206 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
1208 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
1209 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1213 if (be64_to_cpu(right->bb_leftsib) == NULLDFSBNO) {
1214 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1218 if (cur->bc_ptrs[level] <= 1) {
1219 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1224 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, be64_to_cpu(right->bb_leftsib), 0,
1225 &lbp, XFS_BMAP_BTREE_REF))) {
1226 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1229 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
1230 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
1231 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1234 if (be16_to_cpu(left->bb_numrecs) == XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
1235 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1239 lrecs = be16_to_cpu(left->bb_numrecs) + 1;
1241 lkp = XFS_BMAP_KEY_IADDR(left, lrecs, cur);
1242 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
1244 xfs_bmbt_log_keys(cur, lbp, lrecs, lrecs);
1245 lpp = XFS_BMAP_PTR_IADDR(left, lrecs, cur);
1246 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
1248 if ((error = xfs_btree_check_lptr_disk(cur, *rpp, level))) {
1249 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1253 *lpp = *rpp; /* INT_: direct copy */
1254 xfs_bmbt_log_ptrs(cur, lbp, lrecs, lrecs);
1256 lrp = XFS_BMAP_REC_IADDR(left, lrecs, cur);
1257 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
1259 xfs_bmbt_log_recs(cur, lbp, lrecs, lrecs);
1261 left->bb_numrecs = cpu_to_be16(lrecs);
1262 xfs_bmbt_log_block(cur, lbp, XFS_BB_NUMRECS);
1265 xfs_btree_check_key(XFS_BTNUM_BMAP, lkp - 1, lkp);
1267 xfs_btree_check_rec(XFS_BTNUM_BMAP, lrp - 1, lrp);
1269 rrecs = be16_to_cpu(right->bb_numrecs) - 1;
1270 right->bb_numrecs = cpu_to_be16(rrecs);
1271 xfs_bmbt_log_block(cur, rbp, XFS_BB_NUMRECS);
1274 for (i = 0; i < rrecs; i++) {
1275 if ((error = xfs_btree_check_lptr_disk(cur, rpp[i + 1],
1277 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1282 memmove(rkp, rkp + 1, rrecs * sizeof(*rkp));
1283 memmove(rpp, rpp + 1, rrecs * sizeof(*rpp));
1284 xfs_bmbt_log_keys(cur, rbp, 1, rrecs);
1285 xfs_bmbt_log_ptrs(cur, rbp, 1, rrecs);
1287 memmove(rrp, rrp + 1, rrecs * sizeof(*rrp));
1288 xfs_bmbt_log_recs(cur, rbp, 1, rrecs);
1289 key.br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(rrp));
1292 if ((error = xfs_bmbt_updkey(cur, rkp, level + 1))) {
1293 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1296 cur->bc_ptrs[level]--;
1297 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1303 * Move 1 record right from cur/level if possible.
1304 * Update cur to reflect the new path.
1306 STATIC int /* error */
1308 xfs_btree_cur_t *cur,
1310 int *stat) /* success/failure */
1312 int error; /* error return value */
1313 int i; /* loop counter */
1314 xfs_bmbt_key_t key; /* bmap btree key */
1315 xfs_buf_t *lbp; /* left buffer pointer */
1316 xfs_bmbt_block_t *left; /* left btree block */
1317 xfs_bmbt_key_t *lkp; /* left btree key */
1318 xfs_bmbt_ptr_t *lpp; /* left address pointer */
1319 xfs_bmbt_rec_t *lrp; /* left record pointer */
1320 xfs_mount_t *mp; /* file system mount point */
1321 xfs_buf_t *rbp; /* right buffer pointer */
1322 xfs_bmbt_block_t *right; /* right btree block */
1323 xfs_bmbt_key_t *rkp; /* right btree key */
1324 xfs_bmbt_ptr_t *rpp; /* right address pointer */
1325 xfs_bmbt_rec_t *rrp=NULL; /* right record pointer */
1326 struct xfs_btree_cur *tcur; /* temporary btree cursor */
1328 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1329 XFS_BMBT_TRACE_ARGI(cur, level);
1330 if (level == cur->bc_nlevels - 1) {
1331 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1335 lbp = cur->bc_bufs[level];
1336 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
1338 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
1339 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1343 if (be64_to_cpu(left->bb_rightsib) == NULLDFSBNO) {
1344 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1348 if (cur->bc_ptrs[level] >= be16_to_cpu(left->bb_numrecs)) {
1349 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1354 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, be64_to_cpu(left->bb_rightsib), 0,
1355 &rbp, XFS_BMAP_BTREE_REF))) {
1356 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1359 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
1360 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
1361 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1364 if (be16_to_cpu(right->bb_numrecs) == XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
1365 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1370 lkp = XFS_BMAP_KEY_IADDR(left, be16_to_cpu(left->bb_numrecs), cur);
1371 lpp = XFS_BMAP_PTR_IADDR(left, be16_to_cpu(left->bb_numrecs), cur);
1372 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
1373 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
1375 for (i = be16_to_cpu(right->bb_numrecs) - 1; i >= 0; i--) {
1376 if ((error = xfs_btree_check_lptr_disk(cur, rpp[i], level))) {
1377 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1382 memmove(rkp + 1, rkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1383 memmove(rpp + 1, rpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1385 if ((error = xfs_btree_check_lptr_disk(cur, *lpp, level))) {
1386 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1391 *rpp = *lpp; /* INT_: direct copy */
1392 xfs_bmbt_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
1393 xfs_bmbt_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
1395 lrp = XFS_BMAP_REC_IADDR(left, be16_to_cpu(left->bb_numrecs), cur);
1396 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
1397 memmove(rrp + 1, rrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1399 xfs_bmbt_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
1400 key.br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(rrp));
1403 be16_add(&left->bb_numrecs, -1);
1404 xfs_bmbt_log_block(cur, lbp, XFS_BB_NUMRECS);
1405 be16_add(&right->bb_numrecs, 1);
1408 xfs_btree_check_key(XFS_BTNUM_BMAP, rkp, rkp + 1);
1410 xfs_btree_check_rec(XFS_BTNUM_BMAP, rrp, rrp + 1);
1412 xfs_bmbt_log_block(cur, rbp, XFS_BB_NUMRECS);
1413 if ((error = xfs_btree_dup_cursor(cur, &tcur))) {
1414 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1417 i = xfs_btree_lastrec(tcur, level);
1418 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1419 if ((error = xfs_bmbt_increment(tcur, level, &i))) {
1420 XFS_BMBT_TRACE_CURSOR(tcur, ERROR);
1423 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1424 if ((error = xfs_bmbt_updkey(tcur, rkp, level + 1))) {
1425 XFS_BMBT_TRACE_CURSOR(tcur, ERROR);
1428 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1429 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1433 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1435 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1440 * Determine the extent state.
1449 ASSERT(blks != 0); /* saved for DMIG */
1450 return XFS_EXT_UNWRITTEN;
1452 return XFS_EXT_NORM;
1457 * Split cur/level block in half.
1458 * Return new block number and its first record (to be inserted into parent).
1460 STATIC int /* error */
1462 xfs_btree_cur_t *cur,
1464 xfs_fsblock_t *bnop,
1465 __uint64_t *startoff,
1466 xfs_btree_cur_t **curp,
1467 int *stat) /* success/failure */
1469 xfs_alloc_arg_t args; /* block allocation args */
1470 int error; /* error return value */
1471 int i; /* loop counter */
1472 xfs_fsblock_t lbno; /* left sibling block number */
1473 xfs_buf_t *lbp; /* left buffer pointer */
1474 xfs_bmbt_block_t *left; /* left btree block */
1475 xfs_bmbt_key_t *lkp; /* left btree key */
1476 xfs_bmbt_ptr_t *lpp; /* left address pointer */
1477 xfs_bmbt_rec_t *lrp; /* left record pointer */
1478 xfs_buf_t *rbp; /* right buffer pointer */
1479 xfs_bmbt_block_t *right; /* right btree block */
1480 xfs_bmbt_key_t *rkp; /* right btree key */
1481 xfs_bmbt_ptr_t *rpp; /* right address pointer */
1482 xfs_bmbt_block_t *rrblock; /* right-right btree block */
1483 xfs_buf_t *rrbp; /* right-right buffer pointer */
1484 xfs_bmbt_rec_t *rrp; /* right record pointer */
1486 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1487 XFS_BMBT_TRACE_ARGIFK(cur, level, *bnop, *startoff);
1488 args.tp = cur->bc_tp;
1489 args.mp = cur->bc_mp;
1490 lbp = cur->bc_bufs[level];
1491 lbno = XFS_DADDR_TO_FSB(args.mp, XFS_BUF_ADDR(lbp));
1492 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
1493 args.fsbno = cur->bc_private.b.firstblock;
1494 args.firstblock = args.fsbno;
1495 if (args.fsbno == NULLFSBLOCK) {
1497 args.type = XFS_ALLOCTYPE_START_BNO;
1499 args.type = XFS_ALLOCTYPE_NEAR_BNO;
1500 args.mod = args.minleft = args.alignment = args.total = args.isfl =
1501 args.userdata = args.minalignslop = 0;
1502 args.minlen = args.maxlen = args.prod = 1;
1503 args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL;
1504 if (!args.wasdel && xfs_trans_get_block_res(args.tp) == 0) {
1505 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1506 return XFS_ERROR(ENOSPC);
1508 if ((error = xfs_alloc_vextent(&args))) {
1509 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1512 if (args.fsbno == NULLFSBLOCK) {
1513 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1517 ASSERT(args.len == 1);
1518 cur->bc_private.b.firstblock = args.fsbno;
1519 cur->bc_private.b.allocated++;
1520 cur->bc_private.b.ip->i_d.di_nblocks++;
1521 xfs_trans_log_inode(args.tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
1522 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, args.tp, cur->bc_private.b.ip,
1523 XFS_TRANS_DQ_BCOUNT, 1L);
1524 rbp = xfs_btree_get_bufl(args.mp, args.tp, args.fsbno, 0);
1525 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
1527 if ((error = xfs_btree_check_lblock(cur, left, level, rbp))) {
1528 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1532 right->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
1533 right->bb_level = left->bb_level;
1534 right->bb_numrecs = cpu_to_be16(be16_to_cpu(left->bb_numrecs) / 2);
1535 if ((be16_to_cpu(left->bb_numrecs) & 1) &&
1536 cur->bc_ptrs[level] <= be16_to_cpu(right->bb_numrecs) + 1)
1537 be16_add(&right->bb_numrecs, 1);
1538 i = be16_to_cpu(left->bb_numrecs) - be16_to_cpu(right->bb_numrecs) + 1;
1540 lkp = XFS_BMAP_KEY_IADDR(left, i, cur);
1541 lpp = XFS_BMAP_PTR_IADDR(left, i, cur);
1542 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
1543 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
1545 for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
1546 if ((error = xfs_btree_check_lptr_disk(cur, lpp[i], level))) {
1547 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1552 memcpy(rkp, lkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1553 memcpy(rpp, lpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1554 xfs_bmbt_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1555 xfs_bmbt_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1556 *startoff = be64_to_cpu(rkp->br_startoff);
1558 lrp = XFS_BMAP_REC_IADDR(left, i, cur);
1559 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
1560 memcpy(rrp, lrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1561 xfs_bmbt_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1562 *startoff = xfs_bmbt_disk_get_startoff(rrp);
1564 be16_add(&left->bb_numrecs, -(be16_to_cpu(right->bb_numrecs)));
1565 right->bb_rightsib = left->bb_rightsib;
1566 left->bb_rightsib = cpu_to_be64(args.fsbno);
1567 right->bb_leftsib = cpu_to_be64(lbno);
1568 xfs_bmbt_log_block(cur, rbp, XFS_BB_ALL_BITS);
1569 xfs_bmbt_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
1570 if (be64_to_cpu(right->bb_rightsib) != NULLDFSBNO) {
1571 if ((error = xfs_btree_read_bufl(args.mp, args.tp,
1572 be64_to_cpu(right->bb_rightsib), 0, &rrbp,
1573 XFS_BMAP_BTREE_REF))) {
1574 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1577 rrblock = XFS_BUF_TO_BMBT_BLOCK(rrbp);
1578 if ((error = xfs_btree_check_lblock(cur, rrblock, level, rrbp))) {
1579 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1582 rrblock->bb_leftsib = cpu_to_be64(args.fsbno);
1583 xfs_bmbt_log_block(cur, rrbp, XFS_BB_LEFTSIB);
1585 if (cur->bc_ptrs[level] > be16_to_cpu(left->bb_numrecs) + 1) {
1586 xfs_btree_setbuf(cur, level, rbp);
1587 cur->bc_ptrs[level] -= be16_to_cpu(left->bb_numrecs);
1589 if (level + 1 < cur->bc_nlevels) {
1590 if ((error = xfs_btree_dup_cursor(cur, curp))) {
1591 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1594 (*curp)->bc_ptrs[level + 1]++;
1597 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1604 * Update keys for the record.
1608 xfs_btree_cur_t *cur,
1609 xfs_bmbt_key_t *keyp, /* on-disk format */
1612 xfs_bmbt_block_t *block;
1621 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1622 XFS_BMBT_TRACE_ARGIK(cur, level, keyp);
1623 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1624 block = xfs_bmbt_get_block(cur, level, &bp);
1626 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
1627 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1631 ptr = cur->bc_ptrs[level];
1632 kp = XFS_BMAP_KEY_IADDR(block, ptr, cur);
1634 xfs_bmbt_log_keys(cur, bp, ptr, ptr);
1636 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1641 * Convert on-disk form of btree root to in-memory form.
1645 xfs_bmdr_block_t *dblock,
1647 xfs_bmbt_block_t *rblock,
1651 xfs_bmbt_key_t *fkp;
1653 xfs_bmbt_key_t *tkp;
1656 rblock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
1657 rblock->bb_level = dblock->bb_level;
1658 ASSERT(be16_to_cpu(rblock->bb_level) > 0);
1659 rblock->bb_numrecs = dblock->bb_numrecs;
1660 rblock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
1661 rblock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
1662 dmxr = (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen, xfs_bmdr, 0);
1663 fkp = XFS_BTREE_KEY_ADDR(xfs_bmdr, dblock, 1);
1664 tkp = XFS_BMAP_BROOT_KEY_ADDR(rblock, 1, rblocklen);
1665 fpp = XFS_BTREE_PTR_ADDR(xfs_bmdr, dblock, 1, dmxr);
1666 tpp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, rblocklen);
1667 dmxr = be16_to_cpu(dblock->bb_numrecs);
1668 memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
1669 memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
1673 * Decrement cursor by one record at the level.
1674 * For nonzero levels the leaf-ward information is untouched.
1678 xfs_btree_cur_t *cur,
1680 int *stat) /* success/failure */
1682 xfs_bmbt_block_t *block;
1684 int error; /* error return value */
1685 xfs_fsblock_t fsbno;
1690 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1691 XFS_BMBT_TRACE_ARGI(cur, level);
1692 ASSERT(level < cur->bc_nlevels);
1693 if (level < cur->bc_nlevels - 1)
1694 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1695 if (--cur->bc_ptrs[level] > 0) {
1696 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1700 block = xfs_bmbt_get_block(cur, level, &bp);
1702 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
1703 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1707 if (be64_to_cpu(block->bb_leftsib) == NULLDFSBNO) {
1708 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1712 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1713 if (--cur->bc_ptrs[lev] > 0)
1715 if (lev < cur->bc_nlevels - 1)
1716 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1718 if (lev == cur->bc_nlevels) {
1719 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1725 for (block = xfs_bmbt_get_block(cur, lev, &bp); lev > level; ) {
1726 fsbno = be64_to_cpu(*XFS_BMAP_PTR_IADDR(block, cur->bc_ptrs[lev], cur));
1727 if ((error = xfs_btree_read_bufl(mp, tp, fsbno, 0, &bp,
1728 XFS_BMAP_BTREE_REF))) {
1729 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1733 xfs_btree_setbuf(cur, lev, bp);
1734 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1735 if ((error = xfs_btree_check_lblock(cur, block, lev, bp))) {
1736 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1739 cur->bc_ptrs[lev] = be16_to_cpu(block->bb_numrecs);
1741 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1747 * Delete the record pointed to by cur.
1751 xfs_btree_cur_t *cur,
1752 int *stat) /* success/failure */
1754 int error; /* error return value */
1758 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1759 for (level = 0, i = 2; i == 2; level++) {
1760 if ((error = xfs_bmbt_delrec(cur, level, &i))) {
1761 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1766 for (level = 1; level < cur->bc_nlevels; level++) {
1767 if (cur->bc_ptrs[level] == 0) {
1768 if ((error = xfs_bmbt_decrement(cur, level,
1770 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1777 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1783 * Convert a compressed bmap extent record to an uncompressed form.
1784 * This code must be in sync with the routines xfs_bmbt_get_startoff,
1785 * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state.
1797 ext_flag = (int)(l0 >> (64 - BMBT_EXNTFLAG_BITLEN));
1798 s->br_startoff = ((xfs_fileoff_t)l0 &
1799 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
1801 s->br_startblock = (((xfs_fsblock_t)l0 & XFS_MASK64LO(9)) << 43) |
1802 (((xfs_fsblock_t)l1) >> 21);
1808 b = (((xfs_dfsbno_t)l0 & XFS_MASK64LO(9)) << 43) |
1809 (((xfs_dfsbno_t)l1) >> 21);
1810 ASSERT((b >> 32) == 0 || ISNULLDSTARTBLOCK(b));
1811 s->br_startblock = (xfs_fsblock_t)b;
1814 s->br_startblock = (xfs_fsblock_t)(((xfs_dfsbno_t)l1) >> 21);
1816 #endif /* XFS_BIG_BLKNOS */
1817 s->br_blockcount = (xfs_filblks_t)(l1 & XFS_MASK64LO(21));
1818 /* This is xfs_extent_state() in-line */
1820 ASSERT(s->br_blockcount != 0); /* saved for DMIG */
1821 st = XFS_EXT_UNWRITTEN;
1832 __xfs_bmbt_get_all(r->l0, r->l1, s);
1836 * Get the block pointer for the given level of the cursor.
1837 * Fill in the buffer pointer, if applicable.
1841 xfs_btree_cur_t *cur,
1846 xfs_bmbt_block_t *rval;
1848 if (level < cur->bc_nlevels - 1) {
1849 *bpp = cur->bc_bufs[level];
1850 rval = XFS_BUF_TO_BMBT_BLOCK(*bpp);
1853 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
1854 cur->bc_private.b.whichfork);
1855 rval = ifp->if_broot;
1861 * Extract the blockcount field from an in memory bmap extent record.
1864 xfs_bmbt_get_blockcount(
1867 return (xfs_filblks_t)(r->l1 & XFS_MASK64LO(21));
1871 * Extract the startblock field from an in memory bmap extent record.
1874 xfs_bmbt_get_startblock(
1878 return (((xfs_fsblock_t)r->l0 & XFS_MASK64LO(9)) << 43) |
1879 (((xfs_fsblock_t)r->l1) >> 21);
1884 b = (((xfs_dfsbno_t)r->l0 & XFS_MASK64LO(9)) << 43) |
1885 (((xfs_dfsbno_t)r->l1) >> 21);
1886 ASSERT((b >> 32) == 0 || ISNULLDSTARTBLOCK(b));
1887 return (xfs_fsblock_t)b;
1889 return (xfs_fsblock_t)(((xfs_dfsbno_t)r->l1) >> 21);
1891 #endif /* XFS_BIG_BLKNOS */
1895 * Extract the startoff field from an in memory bmap extent record.
1898 xfs_bmbt_get_startoff(
1901 return ((xfs_fileoff_t)r->l0 &
1902 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
1911 ext_flag = (int)((r->l0) >> (64 - BMBT_EXNTFLAG_BITLEN));
1912 return xfs_extent_state(xfs_bmbt_get_blockcount(r),
1916 #ifndef XFS_NATIVE_HOST
1917 /* Endian flipping versions of the bmbt extraction functions */
1919 xfs_bmbt_disk_get_all(
1925 l0 = INT_GET(r->l0, ARCH_CONVERT);
1926 l1 = INT_GET(r->l1, ARCH_CONVERT);
1928 __xfs_bmbt_get_all(l0, l1, s);
1932 * Extract the blockcount field from an on disk bmap extent record.
1935 xfs_bmbt_disk_get_blockcount(
1938 return (xfs_filblks_t)(INT_GET(r->l1, ARCH_CONVERT) & XFS_MASK64LO(21));
1942 * Extract the startoff field from a disk format bmap extent record.
1945 xfs_bmbt_disk_get_startoff(
1948 return ((xfs_fileoff_t)INT_GET(r->l0, ARCH_CONVERT) &
1949 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
1951 #endif /* XFS_NATIVE_HOST */
1955 * Increment cursor by one record at the level.
1956 * For nonzero levels the leaf-ward information is untouched.
1960 xfs_btree_cur_t *cur,
1962 int *stat) /* success/failure */
1964 xfs_bmbt_block_t *block;
1966 int error; /* error return value */
1967 xfs_fsblock_t fsbno;
1972 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1973 XFS_BMBT_TRACE_ARGI(cur, level);
1974 ASSERT(level < cur->bc_nlevels);
1975 if (level < cur->bc_nlevels - 1)
1976 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1977 block = xfs_bmbt_get_block(cur, level, &bp);
1979 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
1980 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1984 if (++cur->bc_ptrs[level] <= be16_to_cpu(block->bb_numrecs)) {
1985 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1989 if (be64_to_cpu(block->bb_rightsib) == NULLDFSBNO) {
1990 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1994 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1995 block = xfs_bmbt_get_block(cur, lev, &bp);
1997 if ((error = xfs_btree_check_lblock(cur, block, lev, bp))) {
1998 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2002 if (++cur->bc_ptrs[lev] <= be16_to_cpu(block->bb_numrecs))
2004 if (lev < cur->bc_nlevels - 1)
2005 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
2007 if (lev == cur->bc_nlevels) {
2008 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2014 for (block = xfs_bmbt_get_block(cur, lev, &bp); lev > level; ) {
2015 fsbno = be64_to_cpu(*XFS_BMAP_PTR_IADDR(block, cur->bc_ptrs[lev], cur));
2016 if ((error = xfs_btree_read_bufl(mp, tp, fsbno, 0, &bp,
2017 XFS_BMAP_BTREE_REF))) {
2018 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2022 xfs_btree_setbuf(cur, lev, bp);
2023 block = XFS_BUF_TO_BMBT_BLOCK(bp);
2024 if ((error = xfs_btree_check_lblock(cur, block, lev, bp))) {
2025 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2028 cur->bc_ptrs[lev] = 1;
2030 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2036 * Insert the current record at the point referenced by cur.
2040 xfs_btree_cur_t *cur,
2041 int *stat) /* success/failure */
2043 int error; /* error return value */
2047 xfs_btree_cur_t *ncur;
2048 xfs_bmbt_rec_t nrec;
2049 xfs_btree_cur_t *pcur;
2051 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2054 xfs_bmbt_disk_set_all(&nrec, &cur->bc_rec.b);
2058 if ((error = xfs_bmbt_insrec(pcur, level++, &nbno, &nrec, &ncur,
2061 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
2062 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2065 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2066 if (pcur != cur && (ncur || nbno == NULLFSBLOCK)) {
2067 cur->bc_nlevels = pcur->bc_nlevels;
2068 cur->bc_private.b.allocated +=
2069 pcur->bc_private.b.allocated;
2070 pcur->bc_private.b.allocated = 0;
2071 ASSERT((cur->bc_private.b.firstblock != NULLFSBLOCK) ||
2072 (cur->bc_private.b.ip->i_d.di_flags &
2073 XFS_DIFLAG_REALTIME));
2074 cur->bc_private.b.firstblock =
2075 pcur->bc_private.b.firstblock;
2076 ASSERT(cur->bc_private.b.flist ==
2077 pcur->bc_private.b.flist);
2078 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
2084 } while (nbno != NULLFSBLOCK);
2085 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2089 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2094 * Log fields from the btree block header.
2098 xfs_btree_cur_t *cur,
2105 static const short offsets[] = {
2106 offsetof(xfs_bmbt_block_t, bb_magic),
2107 offsetof(xfs_bmbt_block_t, bb_level),
2108 offsetof(xfs_bmbt_block_t, bb_numrecs),
2109 offsetof(xfs_bmbt_block_t, bb_leftsib),
2110 offsetof(xfs_bmbt_block_t, bb_rightsib),
2111 sizeof(xfs_bmbt_block_t)
2114 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2115 XFS_BMBT_TRACE_ARGBI(cur, bp, fields);
2118 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first,
2120 xfs_trans_log_buf(tp, bp, first, last);
2122 xfs_trans_log_inode(tp, cur->bc_private.b.ip,
2123 XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
2124 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2128 * Log record values from the btree block.
2132 xfs_btree_cur_t *cur,
2137 xfs_bmbt_block_t *block;
2143 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2144 XFS_BMBT_TRACE_ARGBII(cur, bp, rfirst, rlast);
2147 block = XFS_BUF_TO_BMBT_BLOCK(bp);
2148 rp = XFS_BMAP_REC_DADDR(block, 1, cur);
2149 first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
2150 last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
2151 xfs_trans_log_buf(tp, bp, first, last);
2152 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2157 xfs_btree_cur_t *cur,
2161 int *stat) /* success/failure */
2163 cur->bc_rec.b.br_startoff = off;
2164 cur->bc_rec.b.br_startblock = bno;
2165 cur->bc_rec.b.br_blockcount = len;
2166 return xfs_bmbt_lookup(cur, XFS_LOOKUP_EQ, stat);
2171 xfs_btree_cur_t *cur,
2175 int *stat) /* success/failure */
2177 cur->bc_rec.b.br_startoff = off;
2178 cur->bc_rec.b.br_startblock = bno;
2179 cur->bc_rec.b.br_blockcount = len;
2180 return xfs_bmbt_lookup(cur, XFS_LOOKUP_GE, stat);
2184 * Give the bmap btree a new root block. Copy the old broot contents
2185 * down into a real block and make the broot point to it.
2189 xfs_btree_cur_t *cur, /* btree cursor */
2190 int *logflags, /* logging flags for inode */
2191 int *stat) /* return status - 0 fail */
2193 xfs_alloc_arg_t args; /* allocation arguments */
2194 xfs_bmbt_block_t *block; /* bmap btree block */
2195 xfs_buf_t *bp; /* buffer for block */
2196 xfs_bmbt_block_t *cblock; /* child btree block */
2197 xfs_bmbt_key_t *ckp; /* child key pointer */
2198 xfs_bmbt_ptr_t *cpp; /* child ptr pointer */
2199 int error; /* error return code */
2201 int i; /* loop counter */
2203 xfs_bmbt_key_t *kp; /* pointer to bmap btree key */
2204 int level; /* btree level */
2205 xfs_bmbt_ptr_t *pp; /* pointer to bmap block addr */
2207 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2208 level = cur->bc_nlevels - 1;
2209 block = xfs_bmbt_get_block(cur, level, &bp);
2211 * Copy the root into a real block.
2213 args.mp = cur->bc_mp;
2214 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
2215 args.tp = cur->bc_tp;
2216 args.fsbno = cur->bc_private.b.firstblock;
2217 args.mod = args.minleft = args.alignment = args.total = args.isfl =
2218 args.userdata = args.minalignslop = 0;
2219 args.minlen = args.maxlen = args.prod = 1;
2220 args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL;
2221 args.firstblock = args.fsbno;
2222 if (args.fsbno == NULLFSBLOCK) {
2224 if ((error = xfs_btree_check_lptr_disk(cur, *pp, level))) {
2225 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2229 args.fsbno = be64_to_cpu(*pp);
2230 args.type = XFS_ALLOCTYPE_START_BNO;
2232 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2233 if ((error = xfs_alloc_vextent(&args))) {
2234 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2237 if (args.fsbno == NULLFSBLOCK) {
2238 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2242 ASSERT(args.len == 1);
2243 cur->bc_private.b.firstblock = args.fsbno;
2244 cur->bc_private.b.allocated++;
2245 cur->bc_private.b.ip->i_d.di_nblocks++;
2246 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, args.tp, cur->bc_private.b.ip,
2247 XFS_TRANS_DQ_BCOUNT, 1L);
2248 bp = xfs_btree_get_bufl(args.mp, cur->bc_tp, args.fsbno, 0);
2249 cblock = XFS_BUF_TO_BMBT_BLOCK(bp);
2251 be16_add(&block->bb_level, 1);
2252 block->bb_numrecs = cpu_to_be16(1);
2254 cur->bc_ptrs[level + 1] = 1;
2255 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
2256 ckp = XFS_BMAP_KEY_IADDR(cblock, 1, cur);
2257 memcpy(ckp, kp, be16_to_cpu(cblock->bb_numrecs) * sizeof(*kp));
2258 cpp = XFS_BMAP_PTR_IADDR(cblock, 1, cur);
2260 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2261 if ((error = xfs_btree_check_lptr_disk(cur, pp[i], level))) {
2262 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2267 memcpy(cpp, pp, be16_to_cpu(cblock->bb_numrecs) * sizeof(*pp));
2269 if ((error = xfs_btree_check_lptr(cur, args.fsbno, level))) {
2270 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2274 *pp = cpu_to_be64(args.fsbno);
2275 xfs_iroot_realloc(cur->bc_private.b.ip, 1 - be16_to_cpu(cblock->bb_numrecs),
2276 cur->bc_private.b.whichfork);
2277 xfs_btree_setbuf(cur, level, bp);
2279 * Do all this logging at the end so that
2280 * the root is at the right level.
2282 xfs_bmbt_log_block(cur, bp, XFS_BB_ALL_BITS);
2283 xfs_bmbt_log_keys(cur, bp, 1, be16_to_cpu(cblock->bb_numrecs));
2284 xfs_bmbt_log_ptrs(cur, bp, 1, be16_to_cpu(cblock->bb_numrecs));
2285 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2287 XFS_ILOG_CORE | XFS_ILOG_FBROOT(cur->bc_private.b.whichfork);
2293 * Set all the fields in a bmap extent record from the uncompressed form.
2302 ASSERT((s->br_state == XFS_EXT_NORM) ||
2303 (s->br_state == XFS_EXT_UNWRITTEN));
2304 extent_flag = (s->br_state == XFS_EXT_NORM) ? 0 : 1;
2305 ASSERT((s->br_startoff & XFS_MASK64HI(9)) == 0);
2306 ASSERT((s->br_blockcount & XFS_MASK64HI(43)) == 0);
2308 ASSERT((s->br_startblock & XFS_MASK64HI(12)) == 0);
2309 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2310 ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
2311 ((xfs_bmbt_rec_base_t)s->br_startblock >> 43);
2312 r->l1 = ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2313 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2314 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2315 #else /* !XFS_BIG_BLKNOS */
2316 if (ISNULLSTARTBLOCK(s->br_startblock)) {
2317 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2318 ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
2319 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
2320 r->l1 = XFS_MASK64HI(11) |
2321 ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2322 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2323 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2325 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2326 ((xfs_bmbt_rec_base_t)s->br_startoff << 9);
2327 r->l1 = ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2328 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2329 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2331 #endif /* XFS_BIG_BLKNOS */
2335 * Set all the fields in a bmap extent record from the arguments.
2347 ASSERT((v == XFS_EXT_NORM) || (v == XFS_EXT_UNWRITTEN));
2348 extent_flag = (v == XFS_EXT_NORM) ? 0 : 1;
2349 ASSERT((o & XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN)) == 0);
2350 ASSERT((c & XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
2352 ASSERT((b & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
2353 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2354 ((xfs_bmbt_rec_base_t)o << 9) |
2355 ((xfs_bmbt_rec_base_t)b >> 43);
2356 r->l1 = ((xfs_bmbt_rec_base_t)b << 21) |
2357 ((xfs_bmbt_rec_base_t)c &
2358 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2359 #else /* !XFS_BIG_BLKNOS */
2360 if (ISNULLSTARTBLOCK(b)) {
2361 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2362 ((xfs_bmbt_rec_base_t)o << 9) |
2363 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
2364 r->l1 = XFS_MASK64HI(11) |
2365 ((xfs_bmbt_rec_base_t)b << 21) |
2366 ((xfs_bmbt_rec_base_t)c &
2367 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2369 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2370 ((xfs_bmbt_rec_base_t)o << 9);
2371 r->l1 = ((xfs_bmbt_rec_base_t)b << 21) |
2372 ((xfs_bmbt_rec_base_t)c &
2373 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2375 #endif /* XFS_BIG_BLKNOS */
2378 #ifndef XFS_NATIVE_HOST
2380 * Set all the fields in a bmap extent record from the uncompressed form.
2383 xfs_bmbt_disk_set_all(
2389 ASSERT((s->br_state == XFS_EXT_NORM) ||
2390 (s->br_state == XFS_EXT_UNWRITTEN));
2391 extent_flag = (s->br_state == XFS_EXT_NORM) ? 0 : 1;
2392 ASSERT((s->br_startoff & XFS_MASK64HI(9)) == 0);
2393 ASSERT((s->br_blockcount & XFS_MASK64HI(43)) == 0);
2395 ASSERT((s->br_startblock & XFS_MASK64HI(12)) == 0);
2396 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2397 ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
2398 ((xfs_bmbt_rec_base_t)s->br_startblock >> 43));
2399 INT_SET(r->l1, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2400 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2401 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2402 #else /* !XFS_BIG_BLKNOS */
2403 if (ISNULLSTARTBLOCK(s->br_startblock)) {
2404 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2405 ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
2406 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
2407 INT_SET(r->l1, ARCH_CONVERT, XFS_MASK64HI(11) |
2408 ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2409 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2410 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2412 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2413 ((xfs_bmbt_rec_base_t)s->br_startoff << 9));
2414 INT_SET(r->l1, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2415 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2416 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2418 #endif /* XFS_BIG_BLKNOS */
2422 * Set all the fields in a disk format bmap extent record from the arguments.
2425 xfs_bmbt_disk_set_allf(
2434 ASSERT((v == XFS_EXT_NORM) || (v == XFS_EXT_UNWRITTEN));
2435 extent_flag = (v == XFS_EXT_NORM) ? 0 : 1;
2436 ASSERT((o & XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN)) == 0);
2437 ASSERT((c & XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
2439 ASSERT((b & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
2440 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2441 ((xfs_bmbt_rec_base_t)o << 9) |
2442 ((xfs_bmbt_rec_base_t)b >> 43));
2443 INT_SET(r->l1, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)b << 21) |
2444 ((xfs_bmbt_rec_base_t)c &
2445 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2446 #else /* !XFS_BIG_BLKNOS */
2447 if (ISNULLSTARTBLOCK(b)) {
2448 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2449 ((xfs_bmbt_rec_base_t)o << 9) |
2450 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
2451 INT_SET(r->l1, ARCH_CONVERT, XFS_MASK64HI(11) |
2452 ((xfs_bmbt_rec_base_t)b << 21) |
2453 ((xfs_bmbt_rec_base_t)c &
2454 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2456 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2457 ((xfs_bmbt_rec_base_t)o << 9));
2458 INT_SET(r->l1, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)b << 21) |
2459 ((xfs_bmbt_rec_base_t)c &
2460 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2462 #endif /* XFS_BIG_BLKNOS */
2464 #endif /* XFS_NATIVE_HOST */
2467 * Set the blockcount field in a bmap extent record.
2470 xfs_bmbt_set_blockcount(
2474 ASSERT((v & XFS_MASK64HI(43)) == 0);
2475 r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64HI(43)) |
2476 (xfs_bmbt_rec_base_t)(v & XFS_MASK64LO(21));
2480 * Set the startblock field in a bmap extent record.
2483 xfs_bmbt_set_startblock(
2488 ASSERT((v & XFS_MASK64HI(12)) == 0);
2489 r->l0 = (r->l0 & (xfs_bmbt_rec_base_t)XFS_MASK64HI(55)) |
2490 (xfs_bmbt_rec_base_t)(v >> 43);
2491 r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)) |
2492 (xfs_bmbt_rec_base_t)(v << 21);
2493 #else /* !XFS_BIG_BLKNOS */
2494 if (ISNULLSTARTBLOCK(v)) {
2495 r->l0 |= (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
2496 r->l1 = (xfs_bmbt_rec_base_t)XFS_MASK64HI(11) |
2497 ((xfs_bmbt_rec_base_t)v << 21) |
2498 (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2500 r->l0 &= ~(xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
2501 r->l1 = ((xfs_bmbt_rec_base_t)v << 21) |
2502 (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2504 #endif /* XFS_BIG_BLKNOS */
2508 * Set the startoff field in a bmap extent record.
2511 xfs_bmbt_set_startoff(
2515 ASSERT((v & XFS_MASK64HI(9)) == 0);
2516 r->l0 = (r->l0 & (xfs_bmbt_rec_base_t) XFS_MASK64HI(1)) |
2517 ((xfs_bmbt_rec_base_t)v << 9) |
2518 (r->l0 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
2522 * Set the extent state field in a bmap extent record.
2529 ASSERT(v == XFS_EXT_NORM || v == XFS_EXT_UNWRITTEN);
2530 if (v == XFS_EXT_NORM)
2531 r->l0 &= XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN);
2533 r->l0 |= XFS_MASK64HI(BMBT_EXNTFLAG_BITLEN);
2537 * Convert in-memory form of btree root to on-disk form.
2541 xfs_bmbt_block_t *rblock,
2543 xfs_bmdr_block_t *dblock,
2547 xfs_bmbt_key_t *fkp;
2549 xfs_bmbt_key_t *tkp;
2552 ASSERT(be32_to_cpu(rblock->bb_magic) == XFS_BMAP_MAGIC);
2553 ASSERT(be64_to_cpu(rblock->bb_leftsib) == NULLDFSBNO);
2554 ASSERT(be64_to_cpu(rblock->bb_rightsib) == NULLDFSBNO);
2555 ASSERT(be16_to_cpu(rblock->bb_level) > 0);
2556 dblock->bb_level = rblock->bb_level;
2557 dblock->bb_numrecs = rblock->bb_numrecs;
2558 dmxr = (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen, xfs_bmdr, 0);
2559 fkp = XFS_BMAP_BROOT_KEY_ADDR(rblock, 1, rblocklen);
2560 tkp = XFS_BTREE_KEY_ADDR(xfs_bmdr, dblock, 1);
2561 fpp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, rblocklen);
2562 tpp = XFS_BTREE_PTR_ADDR(xfs_bmdr, dblock, 1, dmxr);
2563 dmxr = be16_to_cpu(dblock->bb_numrecs);
2564 memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
2565 memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
2569 * Update the record to the passed values.
2573 xfs_btree_cur_t *cur,
2579 xfs_bmbt_block_t *block;
2586 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2587 XFS_BMBT_TRACE_ARGFFFI(cur, (xfs_dfiloff_t)off, (xfs_dfsbno_t)bno,
2588 (xfs_dfilblks_t)len, (int)state);
2589 block = xfs_bmbt_get_block(cur, 0, &bp);
2591 if ((error = xfs_btree_check_lblock(cur, block, 0, bp))) {
2592 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2596 ptr = cur->bc_ptrs[0];
2597 rp = XFS_BMAP_REC_IADDR(block, ptr, cur);
2598 xfs_bmbt_disk_set_allf(rp, off, bno, len, state);
2599 xfs_bmbt_log_recs(cur, bp, ptr, ptr);
2601 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2604 key.br_startoff = cpu_to_be64(off);
2605 if ((error = xfs_bmbt_updkey(cur, &key, 1))) {
2606 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2609 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2614 * Check extent records, which have just been read, for
2615 * any bit in the extent flag field. ASSERT on debug
2616 * kernels, as this condition should not occur.
2617 * Return an error condition (1) if any flags found,
2618 * otherwise return 0.
2622 xfs_check_nostate_extents(
2629 for (; num > 0; num--, idx++) {
2630 ep = xfs_iext_get_ext(ifp, idx);
2632 (64 - BMBT_EXNTFLAG_BITLEN)) != 0) {