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(fname, c, b, i, __LINE__)
278 #define XFS_BMBT_TRACE_ARGBII(c,b,i,j) \
279 xfs_bmbt_trace_argbii(fname, c, b, i, j, __LINE__)
280 #define XFS_BMBT_TRACE_ARGFFFI(c,o,b,i,j) \
281 xfs_bmbt_trace_argfffi(fname, c, o, b, i, j, __LINE__)
282 #define XFS_BMBT_TRACE_ARGI(c,i) \
283 xfs_bmbt_trace_argi(fname, c, i, __LINE__)
284 #define XFS_BMBT_TRACE_ARGIFK(c,i,f,s) \
285 xfs_bmbt_trace_argifk(fname, c, i, f, s, __LINE__)
286 #define XFS_BMBT_TRACE_ARGIFR(c,i,f,r) \
287 xfs_bmbt_trace_argifr(fname, c, i, f, r, __LINE__)
288 #define XFS_BMBT_TRACE_ARGIK(c,i,k) \
289 xfs_bmbt_trace_argik(fname, c, i, k, __LINE__)
290 #define XFS_BMBT_TRACE_CURSOR(c,s) \
291 xfs_bmbt_trace_cursor(fname, 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 #ifdef XFS_BMBT_TRACE
322 static char fname[] = "xfs_bmbt_delrec";
324 int i; /* loop counter */
325 int j; /* temp state */
326 xfs_bmbt_key_t key; /* bmap btree key */
327 xfs_bmbt_key_t *kp=NULL; /* pointer to bmap btree key */
328 xfs_fsblock_t lbno; /* left sibling block number */
329 xfs_buf_t *lbp; /* left buffer pointer */
330 xfs_bmbt_block_t *left; /* left btree block */
331 xfs_bmbt_key_t *lkp; /* left btree key */
332 xfs_bmbt_ptr_t *lpp; /* left address pointer */
333 int lrecs=0; /* left record count */
334 xfs_bmbt_rec_t *lrp; /* left record pointer */
335 xfs_mount_t *mp; /* file system mount point */
336 xfs_bmbt_ptr_t *pp; /* pointer to bmap block addr */
337 int ptr; /* key/record index */
338 xfs_fsblock_t rbno; /* right sibling block number */
339 xfs_buf_t *rbp; /* right buffer pointer */
340 xfs_bmbt_block_t *right; /* right btree block */
341 xfs_bmbt_key_t *rkp; /* right btree key */
342 xfs_bmbt_rec_t *rp; /* pointer to bmap btree rec */
343 xfs_bmbt_ptr_t *rpp; /* right address pointer */
344 xfs_bmbt_block_t *rrblock; /* right-right btree block */
345 xfs_buf_t *rrbp; /* right-right buffer pointer */
346 int rrecs=0; /* right record count */
347 xfs_bmbt_rec_t *rrp; /* right record pointer */
348 xfs_btree_cur_t *tcur; /* temporary btree cursor */
349 int numrecs; /* temporary numrec count */
350 int numlrecs, numrrecs;
352 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
353 XFS_BMBT_TRACE_ARGI(cur, level);
354 ptr = cur->bc_ptrs[level];
357 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
361 block = xfs_bmbt_get_block(cur, level, &bp);
362 numrecs = be16_to_cpu(block->bb_numrecs);
364 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
365 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
370 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
374 XFS_STATS_INC(xs_bmbt_delrec);
376 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
377 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
379 for (i = ptr; i < numrecs; i++) {
380 if ((error = xfs_btree_check_lptr_disk(cur, pp[i], level))) {
381 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
387 memmove(&kp[ptr - 1], &kp[ptr],
388 (numrecs - ptr) * sizeof(*kp));
389 memmove(&pp[ptr - 1], &pp[ptr], /* INT_: direct copy */
390 (numrecs - ptr) * sizeof(*pp));
391 xfs_bmbt_log_ptrs(cur, bp, ptr, numrecs - 1);
392 xfs_bmbt_log_keys(cur, bp, ptr, numrecs - 1);
395 rp = XFS_BMAP_REC_IADDR(block, 1, cur);
397 memmove(&rp[ptr - 1], &rp[ptr],
398 (numrecs - ptr) * sizeof(*rp));
399 xfs_bmbt_log_recs(cur, bp, ptr, numrecs - 1);
403 cpu_to_be64(xfs_bmbt_disk_get_startoff(rp));
408 block->bb_numrecs = cpu_to_be16(numrecs);
409 xfs_bmbt_log_block(cur, bp, XFS_BB_NUMRECS);
411 * We're at the root level.
412 * First, shrink the root block in-memory.
413 * Try to get rid of the next level down.
414 * If we can't then there's nothing left to do.
416 if (level == cur->bc_nlevels - 1) {
417 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
418 cur->bc_private.b.whichfork);
419 if ((error = xfs_bmbt_killroot(cur))) {
420 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
423 if (level > 0 && (error = xfs_bmbt_decrement(cur, level, &j))) {
424 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
427 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
431 if (ptr == 1 && (error = xfs_bmbt_updkey(cur, kp, level + 1))) {
432 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
435 if (numrecs >= XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
436 if (level > 0 && (error = xfs_bmbt_decrement(cur, level, &j))) {
437 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
440 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
444 rbno = be64_to_cpu(block->bb_rightsib);
445 lbno = be64_to_cpu(block->bb_leftsib);
447 * One child of root, need to get a chance to copy its contents
448 * into the root and delete it. Can't go up to next level,
449 * there's nothing to delete there.
451 if (lbno == NULLFSBLOCK && rbno == NULLFSBLOCK &&
452 level == cur->bc_nlevels - 2) {
453 if ((error = xfs_bmbt_killroot(cur))) {
454 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
457 if (level > 0 && (error = xfs_bmbt_decrement(cur, level, &i))) {
458 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
461 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
465 ASSERT(rbno != NULLFSBLOCK || lbno != NULLFSBLOCK);
466 if ((error = xfs_btree_dup_cursor(cur, &tcur))) {
467 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
471 if (rbno != NULLFSBLOCK) {
472 i = xfs_btree_lastrec(tcur, level);
473 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
474 if ((error = xfs_bmbt_increment(tcur, level, &i))) {
475 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
478 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
479 i = xfs_btree_lastrec(tcur, level);
480 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
481 rbp = tcur->bc_bufs[level];
482 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
484 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
485 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
489 bno = be64_to_cpu(right->bb_leftsib);
490 if (be16_to_cpu(right->bb_numrecs) - 1 >=
491 XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
492 if ((error = xfs_bmbt_lshift(tcur, level, &i))) {
493 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
497 ASSERT(be16_to_cpu(block->bb_numrecs) >=
498 XFS_BMAP_BLOCK_IMINRECS(level, tcur));
499 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
502 if ((error = xfs_bmbt_decrement(cur,
504 XFS_BMBT_TRACE_CURSOR(cur,
509 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
514 rrecs = be16_to_cpu(right->bb_numrecs);
515 if (lbno != NULLFSBLOCK) {
516 i = xfs_btree_firstrec(tcur, level);
517 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
518 if ((error = xfs_bmbt_decrement(tcur, level, &i))) {
519 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
522 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
525 if (lbno != NULLFSBLOCK) {
526 i = xfs_btree_firstrec(tcur, level);
527 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
529 * decrement to last in block
531 if ((error = xfs_bmbt_decrement(tcur, level, &i))) {
532 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
535 i = xfs_btree_firstrec(tcur, level);
536 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
537 lbp = tcur->bc_bufs[level];
538 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
540 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
541 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
545 bno = be64_to_cpu(left->bb_rightsib);
546 if (be16_to_cpu(left->bb_numrecs) - 1 >=
547 XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
548 if ((error = xfs_bmbt_rshift(tcur, level, &i))) {
549 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
553 ASSERT(be16_to_cpu(block->bb_numrecs) >=
554 XFS_BMAP_BLOCK_IMINRECS(level, tcur));
555 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
559 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
564 lrecs = be16_to_cpu(left->bb_numrecs);
566 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
569 ASSERT(bno != NULLFSBLOCK);
570 if (lbno != NULLFSBLOCK &&
571 lrecs + be16_to_cpu(block->bb_numrecs) <= XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
575 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, lbno, 0, &lbp,
576 XFS_BMAP_BTREE_REF))) {
577 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
580 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
581 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
582 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
585 } else if (rbno != NULLFSBLOCK &&
586 rrecs + be16_to_cpu(block->bb_numrecs) <=
587 XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
591 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, rbno, 0, &rbp,
592 XFS_BMAP_BTREE_REF))) {
593 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
596 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
597 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
598 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
601 lrecs = be16_to_cpu(left->bb_numrecs);
603 if (level > 0 && (error = xfs_bmbt_decrement(cur, level, &i))) {
604 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
607 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
611 numlrecs = be16_to_cpu(left->bb_numrecs);
612 numrrecs = be16_to_cpu(right->bb_numrecs);
614 lkp = XFS_BMAP_KEY_IADDR(left, numlrecs + 1, cur);
615 lpp = XFS_BMAP_PTR_IADDR(left, numlrecs + 1, cur);
616 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
617 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
619 for (i = 0; i < numrrecs; i++) {
620 if ((error = xfs_btree_check_lptr_disk(cur, rpp[i], level))) {
621 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
626 memcpy(lkp, rkp, numrrecs * sizeof(*lkp));
627 memcpy(lpp, rpp, numrrecs * sizeof(*lpp));
628 xfs_bmbt_log_keys(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
629 xfs_bmbt_log_ptrs(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
631 lrp = XFS_BMAP_REC_IADDR(left, numlrecs + 1, cur);
632 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
633 memcpy(lrp, rrp, numrrecs * sizeof(*lrp));
634 xfs_bmbt_log_recs(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
636 be16_add(&left->bb_numrecs, numrrecs);
637 left->bb_rightsib = right->bb_rightsib;
638 xfs_bmbt_log_block(cur, lbp, XFS_BB_RIGHTSIB | XFS_BB_NUMRECS);
639 if (be64_to_cpu(left->bb_rightsib) != NULLDFSBNO) {
640 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp,
641 be64_to_cpu(left->bb_rightsib),
642 0, &rrbp, XFS_BMAP_BTREE_REF))) {
643 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
646 rrblock = XFS_BUF_TO_BMBT_BLOCK(rrbp);
647 if ((error = xfs_btree_check_lblock(cur, rrblock, level, rrbp))) {
648 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
651 rrblock->bb_leftsib = cpu_to_be64(lbno);
652 xfs_bmbt_log_block(cur, rrbp, XFS_BB_LEFTSIB);
654 xfs_bmap_add_free(XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(rbp)), 1,
655 cur->bc_private.b.flist, mp);
656 cur->bc_private.b.ip->i_d.di_nblocks--;
657 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
658 XFS_TRANS_MOD_DQUOT_BYINO(mp, cur->bc_tp, cur->bc_private.b.ip,
659 XFS_TRANS_DQ_BCOUNT, -1L);
660 xfs_trans_binval(cur->bc_tp, rbp);
662 cur->bc_bufs[level] = lbp;
663 cur->bc_ptrs[level] += lrecs;
664 cur->bc_ra[level] = 0;
665 } else if ((error = xfs_bmbt_increment(cur, level + 1, &i))) {
666 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
670 cur->bc_ptrs[level]--;
671 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
677 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
683 * Get the data from the pointed-to record.
687 xfs_btree_cur_t *cur,
694 xfs_bmbt_block_t *block;
702 block = xfs_bmbt_get_block(cur, 0, &bp);
703 ptr = cur->bc_ptrs[0];
705 if ((error = xfs_btree_check_lblock(cur, block, 0, bp)))
708 if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) {
712 rp = XFS_BMAP_REC_IADDR(block, ptr, cur);
713 *off = xfs_bmbt_disk_get_startoff(rp);
714 *bno = xfs_bmbt_disk_get_startblock(rp);
715 *len = xfs_bmbt_disk_get_blockcount(rp);
716 *state = xfs_bmbt_disk_get_state(rp);
723 * Insert one record/level. Return information to the caller
724 * allowing the next level up to proceed if necessary.
726 STATIC int /* error */
728 xfs_btree_cur_t *cur,
731 xfs_bmbt_rec_t *recp,
732 xfs_btree_cur_t **curp,
733 int *stat) /* no-go/done/continue */
735 xfs_bmbt_block_t *block; /* bmap btree block */
736 xfs_buf_t *bp; /* buffer for block */
737 int error; /* error return value */
738 #ifdef XFS_BMBT_TRACE
739 static char fname[] = "xfs_bmbt_insrec";
741 int i; /* loop index */
742 xfs_bmbt_key_t key; /* bmap btree key */
743 xfs_bmbt_key_t *kp=NULL; /* pointer to bmap btree key */
744 int logflags; /* inode logging flags */
745 xfs_fsblock_t nbno; /* new block number */
746 struct xfs_btree_cur *ncur; /* new btree cursor */
747 __uint64_t startoff; /* new btree key value */
748 xfs_bmbt_rec_t nrec; /* new record count */
749 int optr; /* old key/record index */
750 xfs_bmbt_ptr_t *pp; /* pointer to bmap block addr */
751 int ptr; /* key/record index */
752 xfs_bmbt_rec_t *rp=NULL; /* pointer to bmap btree rec */
755 ASSERT(level < cur->bc_nlevels);
756 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
757 XFS_BMBT_TRACE_ARGIFR(cur, level, *bnop, recp);
759 key.br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(recp));
760 optr = ptr = cur->bc_ptrs[level];
762 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
766 XFS_STATS_INC(xs_bmbt_insrec);
767 block = xfs_bmbt_get_block(cur, level, &bp);
768 numrecs = be16_to_cpu(block->bb_numrecs);
770 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
771 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
774 if (ptr <= numrecs) {
776 rp = XFS_BMAP_REC_IADDR(block, ptr, cur);
777 xfs_btree_check_rec(XFS_BTNUM_BMAP, recp, rp);
779 kp = XFS_BMAP_KEY_IADDR(block, ptr, cur);
780 xfs_btree_check_key(XFS_BTNUM_BMAP, &key, kp);
785 if (numrecs == XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
786 if (numrecs < XFS_BMAP_BLOCK_DMAXRECS(level, cur)) {
788 * A root block, that can be made bigger.
790 xfs_iroot_realloc(cur->bc_private.b.ip, 1,
791 cur->bc_private.b.whichfork);
792 block = xfs_bmbt_get_block(cur, level, &bp);
793 } else if (level == cur->bc_nlevels - 1) {
794 if ((error = xfs_bmbt_newroot(cur, &logflags, stat)) ||
796 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
799 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
801 block = xfs_bmbt_get_block(cur, level, &bp);
803 if ((error = xfs_bmbt_rshift(cur, level, &i))) {
804 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
810 if ((error = xfs_bmbt_lshift(cur, level, &i))) {
811 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
815 optr = ptr = cur->bc_ptrs[level];
817 if ((error = xfs_bmbt_split(cur, level,
818 &nbno, &startoff, &ncur,
820 XFS_BMBT_TRACE_CURSOR(cur,
825 block = xfs_bmbt_get_block(
829 xfs_btree_check_lblock(cur,
830 block, level, bp))) {
831 XFS_BMBT_TRACE_CURSOR(
836 ptr = cur->bc_ptrs[level];
837 xfs_bmbt_disk_set_allf(&nrec,
841 XFS_BMBT_TRACE_CURSOR(cur,
850 numrecs = be16_to_cpu(block->bb_numrecs);
852 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
853 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
855 for (i = numrecs; i >= ptr; i--) {
856 if ((error = xfs_btree_check_lptr_disk(cur, pp[i - 1],
858 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
863 memmove(&kp[ptr], &kp[ptr - 1],
864 (numrecs - ptr + 1) * sizeof(*kp));
865 memmove(&pp[ptr], &pp[ptr - 1], /* INT_: direct copy */
866 (numrecs - ptr + 1) * sizeof(*pp));
868 if ((error = xfs_btree_check_lptr(cur, *bnop, level))) {
869 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
874 pp[ptr - 1] = cpu_to_be64(*bnop);
876 block->bb_numrecs = cpu_to_be16(numrecs);
877 xfs_bmbt_log_keys(cur, bp, ptr, numrecs);
878 xfs_bmbt_log_ptrs(cur, bp, ptr, numrecs);
880 rp = XFS_BMAP_REC_IADDR(block, 1, cur);
881 memmove(&rp[ptr], &rp[ptr - 1],
882 (numrecs - ptr + 1) * sizeof(*rp));
885 block->bb_numrecs = cpu_to_be16(numrecs);
886 xfs_bmbt_log_recs(cur, bp, ptr, numrecs);
888 xfs_bmbt_log_block(cur, bp, XFS_BB_NUMRECS);
892 xfs_btree_check_rec(XFS_BTNUM_BMAP, rp + ptr - 1,
895 xfs_btree_check_key(XFS_BTNUM_BMAP, kp + ptr - 1,
899 if (optr == 1 && (error = xfs_bmbt_updkey(cur, &key, level + 1))) {
900 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
904 if (nbno != NULLFSBLOCK) {
908 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
915 xfs_btree_cur_t *cur)
917 xfs_bmbt_block_t *block;
918 xfs_bmbt_block_t *cblock;
925 #ifdef XFS_BMBT_TRACE
926 static char fname[] = "xfs_bmbt_killroot";
935 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
936 level = cur->bc_nlevels - 1;
939 * Don't deal with the root block needs to be a leaf case.
940 * We're just going to turn the thing back into extents anyway.
943 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
946 block = xfs_bmbt_get_block(cur, level, &cbp);
948 * Give up if the root has multiple children.
950 if (be16_to_cpu(block->bb_numrecs) != 1) {
951 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
955 * Only do this if the next level will fit.
956 * Then the data must be copied up to the inode,
957 * instead of freeing the root you free the next level.
959 cbp = cur->bc_bufs[level - 1];
960 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
961 if (be16_to_cpu(cblock->bb_numrecs) > XFS_BMAP_BLOCK_DMAXRECS(level, cur)) {
962 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
965 ASSERT(be64_to_cpu(cblock->bb_leftsib) == NULLDFSBNO);
966 ASSERT(be64_to_cpu(cblock->bb_rightsib) == NULLDFSBNO);
967 ip = cur->bc_private.b.ip;
968 ifp = XFS_IFORK_PTR(ip, cur->bc_private.b.whichfork);
969 ASSERT(XFS_BMAP_BLOCK_IMAXRECS(level, cur) ==
970 XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes));
971 i = (int)(be16_to_cpu(cblock->bb_numrecs) - XFS_BMAP_BLOCK_IMAXRECS(level, cur));
973 xfs_iroot_realloc(ip, i, cur->bc_private.b.whichfork);
974 block = ifp->if_broot;
976 be16_add(&block->bb_numrecs, i);
977 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
978 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
979 ckp = XFS_BMAP_KEY_IADDR(cblock, 1, cur);
980 memcpy(kp, ckp, be16_to_cpu(block->bb_numrecs) * sizeof(*kp));
981 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
982 cpp = XFS_BMAP_PTR_IADDR(cblock, 1, cur);
984 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
985 if ((error = xfs_btree_check_lptr_disk(cur, cpp[i], level - 1))) {
986 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
991 memcpy(pp, cpp, be16_to_cpu(block->bb_numrecs) * sizeof(*pp));
992 xfs_bmap_add_free(XFS_DADDR_TO_FSB(cur->bc_mp, XFS_BUF_ADDR(cbp)), 1,
993 cur->bc_private.b.flist, cur->bc_mp);
994 ip->i_d.di_nblocks--;
995 XFS_TRANS_MOD_DQUOT_BYINO(cur->bc_mp, cur->bc_tp, ip,
996 XFS_TRANS_DQ_BCOUNT, -1L);
997 xfs_trans_binval(cur->bc_tp, cbp);
998 cur->bc_bufs[level - 1] = NULL;
999 be16_add(&block->bb_level, -1);
1000 xfs_trans_log_inode(cur->bc_tp, ip,
1001 XFS_ILOG_CORE | XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
1003 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1008 * Log key values from the btree block.
1012 xfs_btree_cur_t *cur,
1017 #ifdef XFS_BMBT_TRACE
1018 static char fname[] = "xfs_bmbt_log_keys";
1022 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1023 XFS_BMBT_TRACE_ARGBII(cur, bp, kfirst, klast);
1026 xfs_bmbt_block_t *block;
1031 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1032 kp = XFS_BMAP_KEY_DADDR(block, 1, cur);
1033 first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
1034 last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
1035 xfs_trans_log_buf(tp, bp, first, last);
1039 ip = cur->bc_private.b.ip;
1040 xfs_trans_log_inode(tp, ip,
1041 XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
1043 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1047 * Log pointer values from the btree block.
1051 xfs_btree_cur_t *cur,
1056 #ifdef XFS_BMBT_TRACE
1057 static char fname[] = "xfs_bmbt_log_ptrs";
1061 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1062 XFS_BMBT_TRACE_ARGBII(cur, bp, pfirst, plast);
1065 xfs_bmbt_block_t *block;
1070 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1071 pp = XFS_BMAP_PTR_DADDR(block, 1, cur);
1072 first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
1073 last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
1074 xfs_trans_log_buf(tp, bp, first, last);
1078 ip = cur->bc_private.b.ip;
1079 xfs_trans_log_inode(tp, ip,
1080 XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
1082 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1086 * Lookup the record. The cursor is made to point to it, based on dir.
1088 STATIC int /* error */
1090 xfs_btree_cur_t *cur,
1092 int *stat) /* success/failure */
1094 xfs_bmbt_block_t *block=NULL;
1098 int error; /* error return value */
1099 #ifdef XFS_BMBT_TRACE
1100 static char fname[] = "xfs_bmbt_lookup";
1102 xfs_fsblock_t fsbno=0;
1106 xfs_bmbt_key_t *kkbase=NULL;
1107 xfs_bmbt_key_t *kkp;
1108 xfs_bmbt_rec_t *krbase=NULL;
1109 xfs_bmbt_rec_t *krp;
1114 xfs_bmbt_irec_t *rp;
1115 xfs_fileoff_t startoff;
1118 XFS_STATS_INC(xs_bmbt_lookup);
1119 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1120 XFS_BMBT_TRACE_ARGI(cur, (int)dir);
1123 rp = &cur->bc_rec.b;
1124 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1125 if (level < cur->bc_nlevels - 1) {
1126 d = XFS_FSB_TO_DADDR(mp, fsbno);
1127 bp = cur->bc_bufs[level];
1128 if (bp && XFS_BUF_ADDR(bp) != d)
1131 if ((error = xfs_btree_read_bufl(mp, tp, fsbno,
1132 0, &bp, XFS_BMAP_BTREE_REF))) {
1133 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1136 xfs_btree_setbuf(cur, level, bp);
1137 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1138 if ((error = xfs_btree_check_lblock(cur, block,
1140 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1144 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1146 block = xfs_bmbt_get_block(cur, level, &bp);
1151 kkbase = XFS_BMAP_KEY_IADDR(block, 1, cur);
1153 krbase = XFS_BMAP_REC_IADDR(block, 1, cur);
1155 if (!(high = be16_to_cpu(block->bb_numrecs))) {
1157 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1158 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1162 while (low <= high) {
1163 XFS_STATS_INC(xs_bmbt_compare);
1164 keyno = (low + high) >> 1;
1166 kkp = kkbase + keyno - 1;
1167 startoff = be64_to_cpu(kkp->br_startoff);
1169 krp = krbase + keyno - 1;
1170 startoff = xfs_bmbt_disk_get_startoff(krp);
1172 diff = (xfs_sfiloff_t)
1173 (startoff - rp->br_startoff);
1183 if (diff > 0 && --keyno < 1)
1185 pp = XFS_BMAP_PTR_IADDR(block, keyno, cur);
1186 fsbno = be64_to_cpu(*pp);
1188 if ((error = xfs_btree_check_lptr(cur, fsbno, level))) {
1189 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1193 cur->bc_ptrs[level] = keyno;
1196 if (dir != XFS_LOOKUP_LE && diff < 0) {
1199 * If ge search and we went off the end of the block, but it's
1200 * not the last block, we're in the wrong block.
1202 if (dir == XFS_LOOKUP_GE && keyno > be16_to_cpu(block->bb_numrecs) &&
1203 be64_to_cpu(block->bb_rightsib) != NULLDFSBNO) {
1204 cur->bc_ptrs[0] = keyno;
1205 if ((error = xfs_bmbt_increment(cur, 0, &i))) {
1206 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1209 XFS_WANT_CORRUPTED_RETURN(i == 1);
1210 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1215 else if (dir == XFS_LOOKUP_LE && diff > 0)
1217 cur->bc_ptrs[0] = keyno;
1218 if (keyno == 0 || keyno > be16_to_cpu(block->bb_numrecs)) {
1219 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1222 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1223 *stat = ((dir != XFS_LOOKUP_EQ) || (diff == 0));
1229 * Move 1 record left from cur/level if possible.
1230 * Update cur to reflect the new path.
1232 STATIC int /* error */
1234 xfs_btree_cur_t *cur,
1236 int *stat) /* success/failure */
1238 int error; /* error return value */
1239 #ifdef XFS_BMBT_TRACE
1240 static char fname[] = "xfs_bmbt_lshift";
1243 int i; /* loop counter */
1245 xfs_bmbt_key_t key; /* bmap btree key */
1246 xfs_buf_t *lbp; /* left buffer pointer */
1247 xfs_bmbt_block_t *left; /* left btree block */
1248 xfs_bmbt_key_t *lkp=NULL; /* left btree key */
1249 xfs_bmbt_ptr_t *lpp; /* left address pointer */
1250 int lrecs; /* left record count */
1251 xfs_bmbt_rec_t *lrp=NULL; /* left record pointer */
1252 xfs_mount_t *mp; /* file system mount point */
1253 xfs_buf_t *rbp; /* right buffer pointer */
1254 xfs_bmbt_block_t *right; /* right btree block */
1255 xfs_bmbt_key_t *rkp=NULL; /* right btree key */
1256 xfs_bmbt_ptr_t *rpp=NULL; /* right address pointer */
1257 xfs_bmbt_rec_t *rrp=NULL; /* right record pointer */
1258 int rrecs; /* right record count */
1260 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1261 XFS_BMBT_TRACE_ARGI(cur, level);
1262 if (level == cur->bc_nlevels - 1) {
1263 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1267 rbp = cur->bc_bufs[level];
1268 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
1270 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
1271 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1275 if (be64_to_cpu(right->bb_leftsib) == NULLDFSBNO) {
1276 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1280 if (cur->bc_ptrs[level] <= 1) {
1281 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1286 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, be64_to_cpu(right->bb_leftsib), 0,
1287 &lbp, XFS_BMAP_BTREE_REF))) {
1288 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1291 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
1292 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
1293 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1296 if (be16_to_cpu(left->bb_numrecs) == XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
1297 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1301 lrecs = be16_to_cpu(left->bb_numrecs) + 1;
1303 lkp = XFS_BMAP_KEY_IADDR(left, lrecs, cur);
1304 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
1306 xfs_bmbt_log_keys(cur, lbp, lrecs, lrecs);
1307 lpp = XFS_BMAP_PTR_IADDR(left, lrecs, cur);
1308 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
1310 if ((error = xfs_btree_check_lptr_disk(cur, *rpp, level))) {
1311 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1315 *lpp = *rpp; /* INT_: direct copy */
1316 xfs_bmbt_log_ptrs(cur, lbp, lrecs, lrecs);
1318 lrp = XFS_BMAP_REC_IADDR(left, lrecs, cur);
1319 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
1321 xfs_bmbt_log_recs(cur, lbp, lrecs, lrecs);
1323 left->bb_numrecs = cpu_to_be16(lrecs);
1324 xfs_bmbt_log_block(cur, lbp, XFS_BB_NUMRECS);
1327 xfs_btree_check_key(XFS_BTNUM_BMAP, lkp - 1, lkp);
1329 xfs_btree_check_rec(XFS_BTNUM_BMAP, lrp - 1, lrp);
1331 rrecs = be16_to_cpu(right->bb_numrecs) - 1;
1332 right->bb_numrecs = cpu_to_be16(rrecs);
1333 xfs_bmbt_log_block(cur, rbp, XFS_BB_NUMRECS);
1336 for (i = 0; i < rrecs; i++) {
1337 if ((error = xfs_btree_check_lptr_disk(cur, rpp[i + 1],
1339 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1344 memmove(rkp, rkp + 1, rrecs * sizeof(*rkp));
1345 memmove(rpp, rpp + 1, rrecs * sizeof(*rpp));
1346 xfs_bmbt_log_keys(cur, rbp, 1, rrecs);
1347 xfs_bmbt_log_ptrs(cur, rbp, 1, rrecs);
1349 memmove(rrp, rrp + 1, rrecs * sizeof(*rrp));
1350 xfs_bmbt_log_recs(cur, rbp, 1, rrecs);
1351 key.br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(rrp));
1354 if ((error = xfs_bmbt_updkey(cur, rkp, level + 1))) {
1355 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1358 cur->bc_ptrs[level]--;
1359 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1365 * Move 1 record right from cur/level if possible.
1366 * Update cur to reflect the new path.
1368 STATIC int /* error */
1370 xfs_btree_cur_t *cur,
1372 int *stat) /* success/failure */
1374 int error; /* error return value */
1375 #ifdef XFS_BMBT_TRACE
1376 static char fname[] = "xfs_bmbt_rshift";
1378 int i; /* loop counter */
1379 xfs_bmbt_key_t key; /* bmap btree key */
1380 xfs_buf_t *lbp; /* left buffer pointer */
1381 xfs_bmbt_block_t *left; /* left btree block */
1382 xfs_bmbt_key_t *lkp; /* left btree key */
1383 xfs_bmbt_ptr_t *lpp; /* left address pointer */
1384 xfs_bmbt_rec_t *lrp; /* left record pointer */
1385 xfs_mount_t *mp; /* file system mount point */
1386 xfs_buf_t *rbp; /* right buffer pointer */
1387 xfs_bmbt_block_t *right; /* right btree block */
1388 xfs_bmbt_key_t *rkp; /* right btree key */
1389 xfs_bmbt_ptr_t *rpp; /* right address pointer */
1390 xfs_bmbt_rec_t *rrp=NULL; /* right record pointer */
1391 struct xfs_btree_cur *tcur; /* temporary btree cursor */
1393 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1394 XFS_BMBT_TRACE_ARGI(cur, level);
1395 if (level == cur->bc_nlevels - 1) {
1396 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1400 lbp = cur->bc_bufs[level];
1401 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
1403 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
1404 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1408 if (be64_to_cpu(left->bb_rightsib) == NULLDFSBNO) {
1409 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1413 if (cur->bc_ptrs[level] >= be16_to_cpu(left->bb_numrecs)) {
1414 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1419 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, be64_to_cpu(left->bb_rightsib), 0,
1420 &rbp, XFS_BMAP_BTREE_REF))) {
1421 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1424 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
1425 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
1426 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1429 if (be16_to_cpu(right->bb_numrecs) == XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
1430 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1435 lkp = XFS_BMAP_KEY_IADDR(left, be16_to_cpu(left->bb_numrecs), cur);
1436 lpp = XFS_BMAP_PTR_IADDR(left, be16_to_cpu(left->bb_numrecs), cur);
1437 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
1438 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
1440 for (i = be16_to_cpu(right->bb_numrecs) - 1; i >= 0; i--) {
1441 if ((error = xfs_btree_check_lptr_disk(cur, rpp[i], level))) {
1442 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1447 memmove(rkp + 1, rkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1448 memmove(rpp + 1, rpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1450 if ((error = xfs_btree_check_lptr_disk(cur, *lpp, level))) {
1451 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1456 *rpp = *lpp; /* INT_: direct copy */
1457 xfs_bmbt_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
1458 xfs_bmbt_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
1460 lrp = XFS_BMAP_REC_IADDR(left, be16_to_cpu(left->bb_numrecs), cur);
1461 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
1462 memmove(rrp + 1, rrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1464 xfs_bmbt_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
1465 key.br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(rrp));
1468 be16_add(&left->bb_numrecs, -1);
1469 xfs_bmbt_log_block(cur, lbp, XFS_BB_NUMRECS);
1470 be16_add(&right->bb_numrecs, 1);
1473 xfs_btree_check_key(XFS_BTNUM_BMAP, rkp, rkp + 1);
1475 xfs_btree_check_rec(XFS_BTNUM_BMAP, rrp, rrp + 1);
1477 xfs_bmbt_log_block(cur, rbp, XFS_BB_NUMRECS);
1478 if ((error = xfs_btree_dup_cursor(cur, &tcur))) {
1479 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1482 i = xfs_btree_lastrec(tcur, level);
1483 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1484 if ((error = xfs_bmbt_increment(tcur, level, &i))) {
1485 XFS_BMBT_TRACE_CURSOR(tcur, ERROR);
1488 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1489 if ((error = xfs_bmbt_updkey(tcur, rkp, level + 1))) {
1490 XFS_BMBT_TRACE_CURSOR(tcur, ERROR);
1493 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1494 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1498 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1500 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1505 * Determine the extent state.
1514 ASSERT(blks != 0); /* saved for DMIG */
1515 return XFS_EXT_UNWRITTEN;
1517 return XFS_EXT_NORM;
1522 * Split cur/level block in half.
1523 * Return new block number and its first record (to be inserted into parent).
1525 STATIC int /* error */
1527 xfs_btree_cur_t *cur,
1529 xfs_fsblock_t *bnop,
1530 __uint64_t *startoff,
1531 xfs_btree_cur_t **curp,
1532 int *stat) /* success/failure */
1534 xfs_alloc_arg_t args; /* block allocation args */
1535 int error; /* error return value */
1536 #ifdef XFS_BMBT_TRACE
1537 static char fname[] = "xfs_bmbt_split";
1539 int i; /* loop counter */
1540 xfs_fsblock_t lbno; /* left sibling block number */
1541 xfs_buf_t *lbp; /* left buffer pointer */
1542 xfs_bmbt_block_t *left; /* left btree block */
1543 xfs_bmbt_key_t *lkp; /* left btree key */
1544 xfs_bmbt_ptr_t *lpp; /* left address pointer */
1545 xfs_bmbt_rec_t *lrp; /* left record pointer */
1546 xfs_buf_t *rbp; /* right buffer pointer */
1547 xfs_bmbt_block_t *right; /* right btree block */
1548 xfs_bmbt_key_t *rkp; /* right btree key */
1549 xfs_bmbt_ptr_t *rpp; /* right address pointer */
1550 xfs_bmbt_block_t *rrblock; /* right-right btree block */
1551 xfs_buf_t *rrbp; /* right-right buffer pointer */
1552 xfs_bmbt_rec_t *rrp; /* right record pointer */
1554 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1555 XFS_BMBT_TRACE_ARGIFK(cur, level, *bnop, *startoff);
1556 args.tp = cur->bc_tp;
1557 args.mp = cur->bc_mp;
1558 lbp = cur->bc_bufs[level];
1559 lbno = XFS_DADDR_TO_FSB(args.mp, XFS_BUF_ADDR(lbp));
1560 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
1561 args.fsbno = cur->bc_private.b.firstblock;
1562 args.firstblock = args.fsbno;
1563 if (args.fsbno == NULLFSBLOCK) {
1565 args.type = XFS_ALLOCTYPE_START_BNO;
1567 args.type = XFS_ALLOCTYPE_NEAR_BNO;
1568 args.mod = args.minleft = args.alignment = args.total = args.isfl =
1569 args.userdata = args.minalignslop = 0;
1570 args.minlen = args.maxlen = args.prod = 1;
1571 args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL;
1572 if (!args.wasdel && xfs_trans_get_block_res(args.tp) == 0) {
1573 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1574 return XFS_ERROR(ENOSPC);
1576 if ((error = xfs_alloc_vextent(&args))) {
1577 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1580 if (args.fsbno == NULLFSBLOCK) {
1581 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1585 ASSERT(args.len == 1);
1586 cur->bc_private.b.firstblock = args.fsbno;
1587 cur->bc_private.b.allocated++;
1588 cur->bc_private.b.ip->i_d.di_nblocks++;
1589 xfs_trans_log_inode(args.tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
1590 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, args.tp, cur->bc_private.b.ip,
1591 XFS_TRANS_DQ_BCOUNT, 1L);
1592 rbp = xfs_btree_get_bufl(args.mp, args.tp, args.fsbno, 0);
1593 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
1595 if ((error = xfs_btree_check_lblock(cur, left, level, rbp))) {
1596 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1600 right->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
1601 right->bb_level = left->bb_level;
1602 right->bb_numrecs = cpu_to_be16(be16_to_cpu(left->bb_numrecs) / 2);
1603 if ((be16_to_cpu(left->bb_numrecs) & 1) &&
1604 cur->bc_ptrs[level] <= be16_to_cpu(right->bb_numrecs) + 1)
1605 be16_add(&right->bb_numrecs, 1);
1606 i = be16_to_cpu(left->bb_numrecs) - be16_to_cpu(right->bb_numrecs) + 1;
1608 lkp = XFS_BMAP_KEY_IADDR(left, i, cur);
1609 lpp = XFS_BMAP_PTR_IADDR(left, i, cur);
1610 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
1611 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
1613 for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
1614 if ((error = xfs_btree_check_lptr_disk(cur, lpp[i], level))) {
1615 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1620 memcpy(rkp, lkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1621 memcpy(rpp, lpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1622 xfs_bmbt_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1623 xfs_bmbt_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1624 *startoff = be64_to_cpu(rkp->br_startoff);
1626 lrp = XFS_BMAP_REC_IADDR(left, i, cur);
1627 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
1628 memcpy(rrp, lrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1629 xfs_bmbt_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1630 *startoff = xfs_bmbt_disk_get_startoff(rrp);
1632 be16_add(&left->bb_numrecs, -(be16_to_cpu(right->bb_numrecs)));
1633 right->bb_rightsib = left->bb_rightsib;
1634 left->bb_rightsib = cpu_to_be64(args.fsbno);
1635 right->bb_leftsib = cpu_to_be64(lbno);
1636 xfs_bmbt_log_block(cur, rbp, XFS_BB_ALL_BITS);
1637 xfs_bmbt_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
1638 if (be64_to_cpu(right->bb_rightsib) != NULLDFSBNO) {
1639 if ((error = xfs_btree_read_bufl(args.mp, args.tp,
1640 be64_to_cpu(right->bb_rightsib), 0, &rrbp,
1641 XFS_BMAP_BTREE_REF))) {
1642 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1645 rrblock = XFS_BUF_TO_BMBT_BLOCK(rrbp);
1646 if ((error = xfs_btree_check_lblock(cur, rrblock, level, rrbp))) {
1647 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1650 rrblock->bb_leftsib = cpu_to_be64(args.fsbno);
1651 xfs_bmbt_log_block(cur, rrbp, XFS_BB_LEFTSIB);
1653 if (cur->bc_ptrs[level] > be16_to_cpu(left->bb_numrecs) + 1) {
1654 xfs_btree_setbuf(cur, level, rbp);
1655 cur->bc_ptrs[level] -= be16_to_cpu(left->bb_numrecs);
1657 if (level + 1 < cur->bc_nlevels) {
1658 if ((error = xfs_btree_dup_cursor(cur, curp))) {
1659 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1662 (*curp)->bc_ptrs[level + 1]++;
1665 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1672 * Update keys for the record.
1676 xfs_btree_cur_t *cur,
1677 xfs_bmbt_key_t *keyp, /* on-disk format */
1680 xfs_bmbt_block_t *block;
1685 #ifdef XFS_BMBT_TRACE
1686 static char fname[] = "xfs_bmbt_updkey";
1692 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1693 XFS_BMBT_TRACE_ARGIK(cur, level, keyp);
1694 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1695 block = xfs_bmbt_get_block(cur, level, &bp);
1697 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
1698 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1702 ptr = cur->bc_ptrs[level];
1703 kp = XFS_BMAP_KEY_IADDR(block, ptr, cur);
1705 xfs_bmbt_log_keys(cur, bp, ptr, ptr);
1707 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1712 * Convert on-disk form of btree root to in-memory form.
1716 xfs_bmdr_block_t *dblock,
1718 xfs_bmbt_block_t *rblock,
1722 xfs_bmbt_key_t *fkp;
1724 xfs_bmbt_key_t *tkp;
1727 rblock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
1728 rblock->bb_level = dblock->bb_level;
1729 ASSERT(be16_to_cpu(rblock->bb_level) > 0);
1730 rblock->bb_numrecs = dblock->bb_numrecs;
1731 rblock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
1732 rblock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
1733 dmxr = (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen, xfs_bmdr, 0);
1734 fkp = XFS_BTREE_KEY_ADDR(dblocklen, xfs_bmdr, dblock, 1, dmxr);
1735 tkp = XFS_BMAP_BROOT_KEY_ADDR(rblock, 1, rblocklen);
1736 fpp = XFS_BTREE_PTR_ADDR(dblocklen, xfs_bmdr, dblock, 1, dmxr);
1737 tpp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, rblocklen);
1738 dmxr = be16_to_cpu(dblock->bb_numrecs);
1739 memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
1740 memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
1744 * Decrement cursor by one record at the level.
1745 * For nonzero levels the leaf-ward information is untouched.
1749 xfs_btree_cur_t *cur,
1751 int *stat) /* success/failure */
1753 xfs_bmbt_block_t *block;
1755 int error; /* error return value */
1756 #ifdef XFS_BMBT_TRACE
1757 static char fname[] = "xfs_bmbt_decrement";
1759 xfs_fsblock_t fsbno;
1764 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1765 XFS_BMBT_TRACE_ARGI(cur, level);
1766 ASSERT(level < cur->bc_nlevels);
1767 if (level < cur->bc_nlevels - 1)
1768 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1769 if (--cur->bc_ptrs[level] > 0) {
1770 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1774 block = xfs_bmbt_get_block(cur, level, &bp);
1776 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
1777 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1781 if (be64_to_cpu(block->bb_leftsib) == NULLDFSBNO) {
1782 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1786 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1787 if (--cur->bc_ptrs[lev] > 0)
1789 if (lev < cur->bc_nlevels - 1)
1790 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1792 if (lev == cur->bc_nlevels) {
1793 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1799 for (block = xfs_bmbt_get_block(cur, lev, &bp); lev > level; ) {
1800 fsbno = be64_to_cpu(*XFS_BMAP_PTR_IADDR(block, cur->bc_ptrs[lev], cur));
1801 if ((error = xfs_btree_read_bufl(mp, tp, fsbno, 0, &bp,
1802 XFS_BMAP_BTREE_REF))) {
1803 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1807 xfs_btree_setbuf(cur, lev, bp);
1808 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1809 if ((error = xfs_btree_check_lblock(cur, block, lev, bp))) {
1810 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1813 cur->bc_ptrs[lev] = be16_to_cpu(block->bb_numrecs);
1815 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1821 * Delete the record pointed to by cur.
1825 xfs_btree_cur_t *cur,
1826 int *stat) /* success/failure */
1828 int error; /* error return value */
1829 #ifdef XFS_BMBT_TRACE
1830 static char fname[] = "xfs_bmbt_delete";
1835 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1836 for (level = 0, i = 2; i == 2; level++) {
1837 if ((error = xfs_bmbt_delrec(cur, level, &i))) {
1838 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1843 for (level = 1; level < cur->bc_nlevels; level++) {
1844 if (cur->bc_ptrs[level] == 0) {
1845 if ((error = xfs_bmbt_decrement(cur, level,
1847 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1854 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1860 * Convert a compressed bmap extent record to an uncompressed form.
1861 * This code must be in sync with the routines xfs_bmbt_get_startoff,
1862 * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state.
1865 STATIC __inline__ void
1874 ext_flag = (int)(l0 >> (64 - BMBT_EXNTFLAG_BITLEN));
1875 s->br_startoff = ((xfs_fileoff_t)l0 &
1876 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
1878 s->br_startblock = (((xfs_fsblock_t)l0 & XFS_MASK64LO(9)) << 43) |
1879 (((xfs_fsblock_t)l1) >> 21);
1885 b = (((xfs_dfsbno_t)l0 & XFS_MASK64LO(9)) << 43) |
1886 (((xfs_dfsbno_t)l1) >> 21);
1887 ASSERT((b >> 32) == 0 || ISNULLDSTARTBLOCK(b));
1888 s->br_startblock = (xfs_fsblock_t)b;
1891 s->br_startblock = (xfs_fsblock_t)(((xfs_dfsbno_t)l1) >> 21);
1893 #endif /* XFS_BIG_BLKNOS */
1894 s->br_blockcount = (xfs_filblks_t)(l1 & XFS_MASK64LO(21));
1895 /* This is xfs_extent_state() in-line */
1897 ASSERT(s->br_blockcount != 0); /* saved for DMIG */
1898 st = XFS_EXT_UNWRITTEN;
1909 __xfs_bmbt_get_all(r->l0, r->l1, s);
1913 * Get the block pointer for the given level of the cursor.
1914 * Fill in the buffer pointer, if applicable.
1918 xfs_btree_cur_t *cur,
1923 xfs_bmbt_block_t *rval;
1925 if (level < cur->bc_nlevels - 1) {
1926 *bpp = cur->bc_bufs[level];
1927 rval = XFS_BUF_TO_BMBT_BLOCK(*bpp);
1930 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
1931 cur->bc_private.b.whichfork);
1932 rval = ifp->if_broot;
1938 * Extract the blockcount field from an in memory bmap extent record.
1941 xfs_bmbt_get_blockcount(
1944 return (xfs_filblks_t)(r->l1 & XFS_MASK64LO(21));
1948 * Extract the startblock field from an in memory bmap extent record.
1951 xfs_bmbt_get_startblock(
1955 return (((xfs_fsblock_t)r->l0 & XFS_MASK64LO(9)) << 43) |
1956 (((xfs_fsblock_t)r->l1) >> 21);
1961 b = (((xfs_dfsbno_t)r->l0 & XFS_MASK64LO(9)) << 43) |
1962 (((xfs_dfsbno_t)r->l1) >> 21);
1963 ASSERT((b >> 32) == 0 || ISNULLDSTARTBLOCK(b));
1964 return (xfs_fsblock_t)b;
1966 return (xfs_fsblock_t)(((xfs_dfsbno_t)r->l1) >> 21);
1968 #endif /* XFS_BIG_BLKNOS */
1972 * Extract the startoff field from an in memory bmap extent record.
1975 xfs_bmbt_get_startoff(
1978 return ((xfs_fileoff_t)r->l0 &
1979 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
1988 ext_flag = (int)((r->l0) >> (64 - BMBT_EXNTFLAG_BITLEN));
1989 return xfs_extent_state(xfs_bmbt_get_blockcount(r),
1993 #ifndef XFS_NATIVE_HOST
1994 /* Endian flipping versions of the bmbt extraction functions */
1996 xfs_bmbt_disk_get_all(
2002 l0 = INT_GET(r->l0, ARCH_CONVERT);
2003 l1 = INT_GET(r->l1, ARCH_CONVERT);
2005 __xfs_bmbt_get_all(l0, l1, s);
2009 * Extract the blockcount field from an on disk bmap extent record.
2012 xfs_bmbt_disk_get_blockcount(
2015 return (xfs_filblks_t)(INT_GET(r->l1, ARCH_CONVERT) & XFS_MASK64LO(21));
2019 * Extract the startblock field from an on disk bmap extent record.
2022 xfs_bmbt_disk_get_startblock(
2026 return (((xfs_fsblock_t)INT_GET(r->l0, ARCH_CONVERT) & XFS_MASK64LO(9)) << 43) |
2027 (((xfs_fsblock_t)INT_GET(r->l1, ARCH_CONVERT)) >> 21);
2032 b = (((xfs_dfsbno_t)INT_GET(r->l0, ARCH_CONVERT) & XFS_MASK64LO(9)) << 43) |
2033 (((xfs_dfsbno_t)INT_GET(r->l1, ARCH_CONVERT)) >> 21);
2034 ASSERT((b >> 32) == 0 || ISNULLDSTARTBLOCK(b));
2035 return (xfs_fsblock_t)b;
2037 return (xfs_fsblock_t)(((xfs_dfsbno_t)INT_GET(r->l1, ARCH_CONVERT)) >> 21);
2039 #endif /* XFS_BIG_BLKNOS */
2043 * Extract the startoff field from a disk format bmap extent record.
2046 xfs_bmbt_disk_get_startoff(
2049 return ((xfs_fileoff_t)INT_GET(r->l0, ARCH_CONVERT) &
2050 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
2054 xfs_bmbt_disk_get_state(
2059 ext_flag = (int)((INT_GET(r->l0, ARCH_CONVERT)) >> (64 - BMBT_EXNTFLAG_BITLEN));
2060 return xfs_extent_state(xfs_bmbt_disk_get_blockcount(r),
2063 #endif /* XFS_NATIVE_HOST */
2067 * Increment cursor by one record at the level.
2068 * For nonzero levels the leaf-ward information is untouched.
2072 xfs_btree_cur_t *cur,
2074 int *stat) /* success/failure */
2076 xfs_bmbt_block_t *block;
2078 int error; /* error return value */
2079 #ifdef XFS_BMBT_TRACE
2080 static char fname[] = "xfs_bmbt_increment";
2082 xfs_fsblock_t fsbno;
2087 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2088 XFS_BMBT_TRACE_ARGI(cur, level);
2089 ASSERT(level < cur->bc_nlevels);
2090 if (level < cur->bc_nlevels - 1)
2091 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
2092 block = xfs_bmbt_get_block(cur, level, &bp);
2094 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
2095 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2099 if (++cur->bc_ptrs[level] <= be16_to_cpu(block->bb_numrecs)) {
2100 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2104 if (be64_to_cpu(block->bb_rightsib) == NULLDFSBNO) {
2105 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2109 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
2110 block = xfs_bmbt_get_block(cur, lev, &bp);
2112 if ((error = xfs_btree_check_lblock(cur, block, lev, bp))) {
2113 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2117 if (++cur->bc_ptrs[lev] <= be16_to_cpu(block->bb_numrecs))
2119 if (lev < cur->bc_nlevels - 1)
2120 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
2122 if (lev == cur->bc_nlevels) {
2123 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2129 for (block = xfs_bmbt_get_block(cur, lev, &bp); lev > level; ) {
2130 fsbno = be64_to_cpu(*XFS_BMAP_PTR_IADDR(block, cur->bc_ptrs[lev], cur));
2131 if ((error = xfs_btree_read_bufl(mp, tp, fsbno, 0, &bp,
2132 XFS_BMAP_BTREE_REF))) {
2133 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2137 xfs_btree_setbuf(cur, lev, bp);
2138 block = XFS_BUF_TO_BMBT_BLOCK(bp);
2139 if ((error = xfs_btree_check_lblock(cur, block, lev, bp))) {
2140 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2143 cur->bc_ptrs[lev] = 1;
2145 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2151 * Insert the current record at the point referenced by cur.
2155 xfs_btree_cur_t *cur,
2156 int *stat) /* success/failure */
2158 int error; /* error return value */
2159 #ifdef XFS_BMBT_TRACE
2160 static char fname[] = "xfs_bmbt_insert";
2165 xfs_btree_cur_t *ncur;
2166 xfs_bmbt_rec_t nrec;
2167 xfs_btree_cur_t *pcur;
2169 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2172 xfs_bmbt_disk_set_all(&nrec, &cur->bc_rec.b);
2176 if ((error = xfs_bmbt_insrec(pcur, level++, &nbno, &nrec, &ncur,
2179 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
2180 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2183 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2184 if (pcur != cur && (ncur || nbno == NULLFSBLOCK)) {
2185 cur->bc_nlevels = pcur->bc_nlevels;
2186 cur->bc_private.b.allocated +=
2187 pcur->bc_private.b.allocated;
2188 pcur->bc_private.b.allocated = 0;
2189 ASSERT((cur->bc_private.b.firstblock != NULLFSBLOCK) ||
2190 (cur->bc_private.b.ip->i_d.di_flags &
2191 XFS_DIFLAG_REALTIME));
2192 cur->bc_private.b.firstblock =
2193 pcur->bc_private.b.firstblock;
2194 ASSERT(cur->bc_private.b.flist ==
2195 pcur->bc_private.b.flist);
2196 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
2202 } while (nbno != NULLFSBLOCK);
2203 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2207 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2212 * Log fields from the btree block header.
2216 xfs_btree_cur_t *cur,
2221 #ifdef XFS_BMBT_TRACE
2222 static char fname[] = "xfs_bmbt_log_block";
2226 static const short offsets[] = {
2227 offsetof(xfs_bmbt_block_t, bb_magic),
2228 offsetof(xfs_bmbt_block_t, bb_level),
2229 offsetof(xfs_bmbt_block_t, bb_numrecs),
2230 offsetof(xfs_bmbt_block_t, bb_leftsib),
2231 offsetof(xfs_bmbt_block_t, bb_rightsib),
2232 sizeof(xfs_bmbt_block_t)
2235 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2236 XFS_BMBT_TRACE_ARGBI(cur, bp, fields);
2239 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first,
2241 xfs_trans_log_buf(tp, bp, first, last);
2243 xfs_trans_log_inode(tp, cur->bc_private.b.ip,
2244 XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
2245 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2249 * Log record values from the btree block.
2253 xfs_btree_cur_t *cur,
2258 xfs_bmbt_block_t *block;
2260 #ifdef XFS_BMBT_TRACE
2261 static char fname[] = "xfs_bmbt_log_recs";
2267 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2268 XFS_BMBT_TRACE_ARGBII(cur, bp, rfirst, rlast);
2271 block = XFS_BUF_TO_BMBT_BLOCK(bp);
2272 rp = XFS_BMAP_REC_DADDR(block, 1, cur);
2273 first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
2274 last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
2275 xfs_trans_log_buf(tp, bp, first, last);
2276 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2281 xfs_btree_cur_t *cur,
2285 int *stat) /* success/failure */
2287 cur->bc_rec.b.br_startoff = off;
2288 cur->bc_rec.b.br_startblock = bno;
2289 cur->bc_rec.b.br_blockcount = len;
2290 return xfs_bmbt_lookup(cur, XFS_LOOKUP_EQ, stat);
2295 xfs_btree_cur_t *cur,
2299 int *stat) /* success/failure */
2301 cur->bc_rec.b.br_startoff = off;
2302 cur->bc_rec.b.br_startblock = bno;
2303 cur->bc_rec.b.br_blockcount = len;
2304 return xfs_bmbt_lookup(cur, XFS_LOOKUP_GE, stat);
2308 * Give the bmap btree a new root block. Copy the old broot contents
2309 * down into a real block and make the broot point to it.
2313 xfs_btree_cur_t *cur, /* btree cursor */
2314 int *logflags, /* logging flags for inode */
2315 int *stat) /* return status - 0 fail */
2317 xfs_alloc_arg_t args; /* allocation arguments */
2318 xfs_bmbt_block_t *block; /* bmap btree block */
2319 xfs_buf_t *bp; /* buffer for block */
2320 xfs_bmbt_block_t *cblock; /* child btree block */
2321 xfs_bmbt_key_t *ckp; /* child key pointer */
2322 xfs_bmbt_ptr_t *cpp; /* child ptr pointer */
2323 int error; /* error return code */
2324 #ifdef XFS_BMBT_TRACE
2325 static char fname[] = "xfs_bmbt_newroot";
2328 int i; /* loop counter */
2330 xfs_bmbt_key_t *kp; /* pointer to bmap btree key */
2331 int level; /* btree level */
2332 xfs_bmbt_ptr_t *pp; /* pointer to bmap block addr */
2334 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2335 level = cur->bc_nlevels - 1;
2336 block = xfs_bmbt_get_block(cur, level, &bp);
2338 * Copy the root into a real block.
2340 args.mp = cur->bc_mp;
2341 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
2342 args.tp = cur->bc_tp;
2343 args.fsbno = cur->bc_private.b.firstblock;
2344 args.mod = args.minleft = args.alignment = args.total = args.isfl =
2345 args.userdata = args.minalignslop = 0;
2346 args.minlen = args.maxlen = args.prod = 1;
2347 args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL;
2348 args.firstblock = args.fsbno;
2349 if (args.fsbno == NULLFSBLOCK) {
2351 if ((error = xfs_btree_check_lptr_disk(cur, *pp, level))) {
2352 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2356 args.fsbno = be64_to_cpu(*pp);
2357 args.type = XFS_ALLOCTYPE_START_BNO;
2359 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2360 if ((error = xfs_alloc_vextent(&args))) {
2361 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2364 if (args.fsbno == NULLFSBLOCK) {
2365 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2369 ASSERT(args.len == 1);
2370 cur->bc_private.b.firstblock = args.fsbno;
2371 cur->bc_private.b.allocated++;
2372 cur->bc_private.b.ip->i_d.di_nblocks++;
2373 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, args.tp, cur->bc_private.b.ip,
2374 XFS_TRANS_DQ_BCOUNT, 1L);
2375 bp = xfs_btree_get_bufl(args.mp, cur->bc_tp, args.fsbno, 0);
2376 cblock = XFS_BUF_TO_BMBT_BLOCK(bp);
2378 be16_add(&block->bb_level, 1);
2379 block->bb_numrecs = cpu_to_be16(1);
2381 cur->bc_ptrs[level + 1] = 1;
2382 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
2383 ckp = XFS_BMAP_KEY_IADDR(cblock, 1, cur);
2384 memcpy(ckp, kp, be16_to_cpu(cblock->bb_numrecs) * sizeof(*kp));
2385 cpp = XFS_BMAP_PTR_IADDR(cblock, 1, cur);
2387 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2388 if ((error = xfs_btree_check_lptr_disk(cur, pp[i], level))) {
2389 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2394 memcpy(cpp, pp, be16_to_cpu(cblock->bb_numrecs) * sizeof(*pp));
2396 if ((error = xfs_btree_check_lptr(cur, args.fsbno, level))) {
2397 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2401 *pp = cpu_to_be64(args.fsbno);
2402 xfs_iroot_realloc(cur->bc_private.b.ip, 1 - be16_to_cpu(cblock->bb_numrecs),
2403 cur->bc_private.b.whichfork);
2404 xfs_btree_setbuf(cur, level, bp);
2406 * Do all this logging at the end so that
2407 * the root is at the right level.
2409 xfs_bmbt_log_block(cur, bp, XFS_BB_ALL_BITS);
2410 xfs_bmbt_log_keys(cur, bp, 1, be16_to_cpu(cblock->bb_numrecs));
2411 xfs_bmbt_log_ptrs(cur, bp, 1, be16_to_cpu(cblock->bb_numrecs));
2412 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2414 XFS_ILOG_CORE | XFS_ILOG_FBROOT(cur->bc_private.b.whichfork);
2420 * Set all the fields in a bmap extent record from the uncompressed form.
2429 ASSERT((s->br_state == XFS_EXT_NORM) ||
2430 (s->br_state == XFS_EXT_UNWRITTEN));
2431 extent_flag = (s->br_state == XFS_EXT_NORM) ? 0 : 1;
2432 ASSERT((s->br_startoff & XFS_MASK64HI(9)) == 0);
2433 ASSERT((s->br_blockcount & XFS_MASK64HI(43)) == 0);
2435 ASSERT((s->br_startblock & XFS_MASK64HI(12)) == 0);
2436 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2437 ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
2438 ((xfs_bmbt_rec_base_t)s->br_startblock >> 43);
2439 r->l1 = ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2440 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2441 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2442 #else /* !XFS_BIG_BLKNOS */
2443 if (ISNULLSTARTBLOCK(s->br_startblock)) {
2444 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2445 ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
2446 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
2447 r->l1 = XFS_MASK64HI(11) |
2448 ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2449 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2450 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2452 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2453 ((xfs_bmbt_rec_base_t)s->br_startoff << 9);
2454 r->l1 = ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2455 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2456 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2458 #endif /* XFS_BIG_BLKNOS */
2462 * Set all the fields in a bmap extent record from the arguments.
2474 ASSERT((v == XFS_EXT_NORM) || (v == XFS_EXT_UNWRITTEN));
2475 extent_flag = (v == XFS_EXT_NORM) ? 0 : 1;
2476 ASSERT((o & XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN)) == 0);
2477 ASSERT((c & XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
2479 ASSERT((b & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
2480 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2481 ((xfs_bmbt_rec_base_t)o << 9) |
2482 ((xfs_bmbt_rec_base_t)b >> 43);
2483 r->l1 = ((xfs_bmbt_rec_base_t)b << 21) |
2484 ((xfs_bmbt_rec_base_t)c &
2485 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2486 #else /* !XFS_BIG_BLKNOS */
2487 if (ISNULLSTARTBLOCK(b)) {
2488 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2489 ((xfs_bmbt_rec_base_t)o << 9) |
2490 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
2491 r->l1 = XFS_MASK64HI(11) |
2492 ((xfs_bmbt_rec_base_t)b << 21) |
2493 ((xfs_bmbt_rec_base_t)c &
2494 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2496 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2497 ((xfs_bmbt_rec_base_t)o << 9);
2498 r->l1 = ((xfs_bmbt_rec_base_t)b << 21) |
2499 ((xfs_bmbt_rec_base_t)c &
2500 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2502 #endif /* XFS_BIG_BLKNOS */
2505 #ifndef XFS_NATIVE_HOST
2507 * Set all the fields in a bmap extent record from the uncompressed form.
2510 xfs_bmbt_disk_set_all(
2516 ASSERT((s->br_state == XFS_EXT_NORM) ||
2517 (s->br_state == XFS_EXT_UNWRITTEN));
2518 extent_flag = (s->br_state == XFS_EXT_NORM) ? 0 : 1;
2519 ASSERT((s->br_startoff & XFS_MASK64HI(9)) == 0);
2520 ASSERT((s->br_blockcount & XFS_MASK64HI(43)) == 0);
2522 ASSERT((s->br_startblock & XFS_MASK64HI(12)) == 0);
2523 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2524 ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
2525 ((xfs_bmbt_rec_base_t)s->br_startblock >> 43));
2526 INT_SET(r->l1, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2527 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2528 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2529 #else /* !XFS_BIG_BLKNOS */
2530 if (ISNULLSTARTBLOCK(s->br_startblock)) {
2531 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2532 ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
2533 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
2534 INT_SET(r->l1, ARCH_CONVERT, XFS_MASK64HI(11) |
2535 ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2536 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2537 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2539 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2540 ((xfs_bmbt_rec_base_t)s->br_startoff << 9));
2541 INT_SET(r->l1, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2542 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2543 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2545 #endif /* XFS_BIG_BLKNOS */
2549 * Set all the fields in a disk format bmap extent record from the arguments.
2552 xfs_bmbt_disk_set_allf(
2561 ASSERT((v == XFS_EXT_NORM) || (v == XFS_EXT_UNWRITTEN));
2562 extent_flag = (v == XFS_EXT_NORM) ? 0 : 1;
2563 ASSERT((o & XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN)) == 0);
2564 ASSERT((c & XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
2566 ASSERT((b & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
2567 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2568 ((xfs_bmbt_rec_base_t)o << 9) |
2569 ((xfs_bmbt_rec_base_t)b >> 43));
2570 INT_SET(r->l1, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)b << 21) |
2571 ((xfs_bmbt_rec_base_t)c &
2572 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2573 #else /* !XFS_BIG_BLKNOS */
2574 if (ISNULLSTARTBLOCK(b)) {
2575 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2576 ((xfs_bmbt_rec_base_t)o << 9) |
2577 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
2578 INT_SET(r->l1, ARCH_CONVERT, XFS_MASK64HI(11) |
2579 ((xfs_bmbt_rec_base_t)b << 21) |
2580 ((xfs_bmbt_rec_base_t)c &
2581 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2583 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2584 ((xfs_bmbt_rec_base_t)o << 9));
2585 INT_SET(r->l1, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)b << 21) |
2586 ((xfs_bmbt_rec_base_t)c &
2587 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2589 #endif /* XFS_BIG_BLKNOS */
2591 #endif /* XFS_NATIVE_HOST */
2594 * Set the blockcount field in a bmap extent record.
2597 xfs_bmbt_set_blockcount(
2601 ASSERT((v & XFS_MASK64HI(43)) == 0);
2602 r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64HI(43)) |
2603 (xfs_bmbt_rec_base_t)(v & XFS_MASK64LO(21));
2607 * Set the startblock field in a bmap extent record.
2610 xfs_bmbt_set_startblock(
2615 ASSERT((v & XFS_MASK64HI(12)) == 0);
2616 r->l0 = (r->l0 & (xfs_bmbt_rec_base_t)XFS_MASK64HI(55)) |
2617 (xfs_bmbt_rec_base_t)(v >> 43);
2618 r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)) |
2619 (xfs_bmbt_rec_base_t)(v << 21);
2620 #else /* !XFS_BIG_BLKNOS */
2621 if (ISNULLSTARTBLOCK(v)) {
2622 r->l0 |= (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
2623 r->l1 = (xfs_bmbt_rec_base_t)XFS_MASK64HI(11) |
2624 ((xfs_bmbt_rec_base_t)v << 21) |
2625 (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2627 r->l0 &= ~(xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
2628 r->l1 = ((xfs_bmbt_rec_base_t)v << 21) |
2629 (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2631 #endif /* XFS_BIG_BLKNOS */
2635 * Set the startoff field in a bmap extent record.
2638 xfs_bmbt_set_startoff(
2642 ASSERT((v & XFS_MASK64HI(9)) == 0);
2643 r->l0 = (r->l0 & (xfs_bmbt_rec_base_t) XFS_MASK64HI(1)) |
2644 ((xfs_bmbt_rec_base_t)v << 9) |
2645 (r->l0 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
2649 * Set the extent state field in a bmap extent record.
2656 ASSERT(v == XFS_EXT_NORM || v == XFS_EXT_UNWRITTEN);
2657 if (v == XFS_EXT_NORM)
2658 r->l0 &= XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN);
2660 r->l0 |= XFS_MASK64HI(BMBT_EXNTFLAG_BITLEN);
2664 * Convert in-memory form of btree root to on-disk form.
2668 xfs_bmbt_block_t *rblock,
2670 xfs_bmdr_block_t *dblock,
2674 xfs_bmbt_key_t *fkp;
2676 xfs_bmbt_key_t *tkp;
2679 ASSERT(be32_to_cpu(rblock->bb_magic) == XFS_BMAP_MAGIC);
2680 ASSERT(be64_to_cpu(rblock->bb_leftsib) == NULLDFSBNO);
2681 ASSERT(be64_to_cpu(rblock->bb_rightsib) == NULLDFSBNO);
2682 ASSERT(be16_to_cpu(rblock->bb_level) > 0);
2683 dblock->bb_level = rblock->bb_level;
2684 dblock->bb_numrecs = rblock->bb_numrecs;
2685 dmxr = (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen, xfs_bmdr, 0);
2686 fkp = XFS_BMAP_BROOT_KEY_ADDR(rblock, 1, rblocklen);
2687 tkp = XFS_BTREE_KEY_ADDR(dblocklen, xfs_bmdr, dblock, 1, dmxr);
2688 fpp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, rblocklen);
2689 tpp = XFS_BTREE_PTR_ADDR(dblocklen, xfs_bmdr, dblock, 1, dmxr);
2690 dmxr = be16_to_cpu(dblock->bb_numrecs);
2691 memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
2692 memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
2696 * Update the record to the passed values.
2700 xfs_btree_cur_t *cur,
2706 xfs_bmbt_block_t *block;
2709 #ifdef XFS_BMBT_TRACE
2710 static char fname[] = "xfs_bmbt_update";
2716 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2717 XFS_BMBT_TRACE_ARGFFFI(cur, (xfs_dfiloff_t)off, (xfs_dfsbno_t)bno,
2718 (xfs_dfilblks_t)len, (int)state);
2719 block = xfs_bmbt_get_block(cur, 0, &bp);
2721 if ((error = xfs_btree_check_lblock(cur, block, 0, bp))) {
2722 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2726 ptr = cur->bc_ptrs[0];
2727 rp = XFS_BMAP_REC_IADDR(block, ptr, cur);
2728 xfs_bmbt_disk_set_allf(rp, off, bno, len, state);
2729 xfs_bmbt_log_recs(cur, bp, ptr, ptr);
2731 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2734 key.br_startoff = cpu_to_be64(off);
2735 if ((error = xfs_bmbt_updkey(cur, &key, 1))) {
2736 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2739 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2744 * Check extent records, which have just been read, for
2745 * any bit in the extent flag field. ASSERT on debug
2746 * kernels, as this condition should not occur.
2747 * Return an error condition (1) if any flags found,
2748 * otherwise return 0.
2752 xfs_check_nostate_extents(
2759 for (; num > 0; num--, idx++) {
2760 ep = xfs_iext_get_ext(ifp, idx);
2762 (64 - BMBT_EXNTFLAG_BITLEN)) != 0) {