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 xfs_bmbt_key_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,
202 o = INT_GET(k->br_startoff, ARCH_CONVERT);
203 xfs_bmbt_trace_enter(func, cur, ARGS, XFS_BMBT_KTRACE_ARGIFK, line,
204 i, d >> 32, (int)d, o >> 32,
210 * Add a trace buffer entry for arguments, for int, fsblock, rec.
213 xfs_bmbt_trace_argifr(
215 xfs_btree_cur_t *cur,
228 xfs_bmbt_disk_get_all(r, &s);
229 o = (xfs_dfiloff_t)s.br_startoff;
230 b = (xfs_dfsbno_t)s.br_startblock;
232 xfs_bmbt_trace_enter(func, cur, ARGS, XFS_BMBT_KTRACE_ARGIFR, line,
233 i, d >> 32, (int)d, o >> 32,
234 (int)o, b >> 32, (int)b, c >> 32,
239 * Add a trace buffer entry for arguments, for int, key.
242 xfs_bmbt_trace_argik(
244 xfs_btree_cur_t *cur,
251 o = INT_GET(k->br_startoff, ARCH_CONVERT);
252 xfs_bmbt_trace_enter(func, cur, ARGS, XFS_BMBT_KTRACE_ARGIFK, line,
253 i, o >> 32, (int)o, 0,
259 * Add a trace buffer entry for the cursor/operation.
262 xfs_bmbt_trace_cursor(
264 xfs_btree_cur_t *cur,
270 xfs_bmbt_set_all(&r, &cur->bc_rec.b);
271 xfs_bmbt_trace_enter(func, cur, s, XFS_BMBT_KTRACE_CUR, line,
272 (cur->bc_nlevels << 24) | (cur->bc_private.b.flags << 16) |
273 cur->bc_private.b.allocated,
274 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),
275 (unsigned long)cur->bc_bufs[0], (unsigned long)cur->bc_bufs[1],
276 (unsigned long)cur->bc_bufs[2], (unsigned long)cur->bc_bufs[3],
277 (cur->bc_ptrs[0] << 16) | cur->bc_ptrs[1],
278 (cur->bc_ptrs[2] << 16) | cur->bc_ptrs[3]);
281 #define XFS_BMBT_TRACE_ARGBI(c,b,i) \
282 xfs_bmbt_trace_argbi(fname, c, b, i, __LINE__)
283 #define XFS_BMBT_TRACE_ARGBII(c,b,i,j) \
284 xfs_bmbt_trace_argbii(fname, c, b, i, j, __LINE__)
285 #define XFS_BMBT_TRACE_ARGFFFI(c,o,b,i,j) \
286 xfs_bmbt_trace_argfffi(fname, c, o, b, i, j, __LINE__)
287 #define XFS_BMBT_TRACE_ARGI(c,i) \
288 xfs_bmbt_trace_argi(fname, c, i, __LINE__)
289 #define XFS_BMBT_TRACE_ARGIFK(c,i,f,k) \
290 xfs_bmbt_trace_argifk(fname, c, i, f, k, __LINE__)
291 #define XFS_BMBT_TRACE_ARGIFR(c,i,f,r) \
292 xfs_bmbt_trace_argifr(fname, c, i, f, r, __LINE__)
293 #define XFS_BMBT_TRACE_ARGIK(c,i,k) \
294 xfs_bmbt_trace_argik(fname, c, i, k, __LINE__)
295 #define XFS_BMBT_TRACE_CURSOR(c,s) \
296 xfs_bmbt_trace_cursor(fname, c, s, __LINE__)
298 #define XFS_BMBT_TRACE_ARGBI(c,b,i)
299 #define XFS_BMBT_TRACE_ARGBII(c,b,i,j)
300 #define XFS_BMBT_TRACE_ARGFFFI(c,o,b,i,j)
301 #define XFS_BMBT_TRACE_ARGI(c,i)
302 #define XFS_BMBT_TRACE_ARGIFK(c,i,f,k)
303 #define XFS_BMBT_TRACE_ARGIFR(c,i,f,r)
304 #define XFS_BMBT_TRACE_ARGIK(c,i,k)
305 #define XFS_BMBT_TRACE_CURSOR(c,s)
306 #endif /* XFS_BMBT_TRACE */
310 * Internal functions.
314 * Delete record pointed to by cur/level.
316 STATIC int /* error */
318 xfs_btree_cur_t *cur,
320 int *stat) /* success/failure */
322 xfs_bmbt_block_t *block; /* bmap btree block */
323 xfs_fsblock_t bno; /* fs-relative block number */
324 xfs_buf_t *bp; /* buffer for block */
325 int error; /* error return value */
326 #ifdef XFS_BMBT_TRACE
327 static char fname[] = "xfs_bmbt_delrec";
329 int i; /* loop counter */
330 int j; /* temp state */
331 xfs_bmbt_key_t key; /* bmap btree key */
332 xfs_bmbt_key_t *kp=NULL; /* pointer to bmap btree key */
333 xfs_fsblock_t lbno; /* left sibling block number */
334 xfs_buf_t *lbp; /* left buffer pointer */
335 xfs_bmbt_block_t *left; /* left btree block */
336 xfs_bmbt_key_t *lkp; /* left btree key */
337 xfs_bmbt_ptr_t *lpp; /* left address pointer */
338 int lrecs=0; /* left record count */
339 xfs_bmbt_rec_t *lrp; /* left record pointer */
340 xfs_mount_t *mp; /* file system mount point */
341 xfs_bmbt_ptr_t *pp; /* pointer to bmap block addr */
342 int ptr; /* key/record index */
343 xfs_fsblock_t rbno; /* right sibling block number */
344 xfs_buf_t *rbp; /* right buffer pointer */
345 xfs_bmbt_block_t *right; /* right btree block */
346 xfs_bmbt_key_t *rkp; /* right btree key */
347 xfs_bmbt_rec_t *rp; /* pointer to bmap btree rec */
348 xfs_bmbt_ptr_t *rpp; /* right address pointer */
349 xfs_bmbt_block_t *rrblock; /* right-right btree block */
350 xfs_buf_t *rrbp; /* right-right buffer pointer */
351 int rrecs=0; /* right record count */
352 xfs_bmbt_rec_t *rrp; /* right record pointer */
353 xfs_btree_cur_t *tcur; /* temporary btree cursor */
354 int numrecs; /* temporary numrec count */
355 int numlrecs, numrrecs;
357 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
358 XFS_BMBT_TRACE_ARGI(cur, level);
359 ptr = cur->bc_ptrs[level];
360 tcur = (xfs_btree_cur_t *)0;
362 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
366 block = xfs_bmbt_get_block(cur, level, &bp);
367 numrecs = be16_to_cpu(block->bb_numrecs);
369 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
370 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
375 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
379 XFS_STATS_INC(xs_bmbt_delrec);
381 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
382 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
384 for (i = ptr; i < numrecs; i++) {
385 if ((error = xfs_btree_check_lptr_disk(cur, pp[i], level))) {
386 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
392 memmove(&kp[ptr - 1], &kp[ptr],
393 (numrecs - ptr) * sizeof(*kp));
394 memmove(&pp[ptr - 1], &pp[ptr], /* INT_: direct copy */
395 (numrecs - ptr) * sizeof(*pp));
396 xfs_bmbt_log_ptrs(cur, bp, ptr, numrecs - 1);
397 xfs_bmbt_log_keys(cur, bp, ptr, numrecs - 1);
400 rp = XFS_BMAP_REC_IADDR(block, 1, cur);
402 memmove(&rp[ptr - 1], &rp[ptr],
403 (numrecs - ptr) * sizeof(*rp));
404 xfs_bmbt_log_recs(cur, bp, ptr, numrecs - 1);
407 INT_SET(key.br_startoff, ARCH_CONVERT, xfs_bmbt_disk_get_startoff(rp));
412 block->bb_numrecs = cpu_to_be16(numrecs);
413 xfs_bmbt_log_block(cur, bp, XFS_BB_NUMRECS);
415 * We're at the root level.
416 * First, shrink the root block in-memory.
417 * Try to get rid of the next level down.
418 * If we can't then there's nothing left to do.
420 if (level == cur->bc_nlevels - 1) {
421 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
422 cur->bc_private.b.whichfork);
423 if ((error = xfs_bmbt_killroot(cur))) {
424 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
427 if (level > 0 && (error = xfs_bmbt_decrement(cur, level, &j))) {
428 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
431 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
435 if (ptr == 1 && (error = xfs_bmbt_updkey(cur, kp, level + 1))) {
436 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
439 if (numrecs >= XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
440 if (level > 0 && (error = xfs_bmbt_decrement(cur, level, &j))) {
441 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
444 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
448 rbno = be64_to_cpu(block->bb_rightsib);
449 lbno = be64_to_cpu(block->bb_leftsib);
451 * One child of root, need to get a chance to copy its contents
452 * into the root and delete it. Can't go up to next level,
453 * there's nothing to delete there.
455 if (lbno == NULLFSBLOCK && rbno == NULLFSBLOCK &&
456 level == cur->bc_nlevels - 2) {
457 if ((error = xfs_bmbt_killroot(cur))) {
458 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
461 if (level > 0 && (error = xfs_bmbt_decrement(cur, level, &i))) {
462 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
465 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
469 ASSERT(rbno != NULLFSBLOCK || lbno != NULLFSBLOCK);
470 if ((error = xfs_btree_dup_cursor(cur, &tcur))) {
471 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
475 if (rbno != NULLFSBLOCK) {
476 i = xfs_btree_lastrec(tcur, level);
477 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
478 if ((error = xfs_bmbt_increment(tcur, level, &i))) {
479 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
482 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
483 i = xfs_btree_lastrec(tcur, level);
484 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
485 rbp = tcur->bc_bufs[level];
486 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
488 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
489 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
493 bno = be64_to_cpu(right->bb_leftsib);
494 if (be16_to_cpu(right->bb_numrecs) - 1 >=
495 XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
496 if ((error = xfs_bmbt_lshift(tcur, level, &i))) {
497 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
501 ASSERT(be16_to_cpu(block->bb_numrecs) >=
502 XFS_BMAP_BLOCK_IMINRECS(level, tcur));
503 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
506 if ((error = xfs_bmbt_decrement(cur,
508 XFS_BMBT_TRACE_CURSOR(cur,
513 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
518 rrecs = be16_to_cpu(right->bb_numrecs);
519 if (lbno != NULLFSBLOCK) {
520 i = xfs_btree_firstrec(tcur, level);
521 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
522 if ((error = xfs_bmbt_decrement(tcur, level, &i))) {
523 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
526 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
529 if (lbno != NULLFSBLOCK) {
530 i = xfs_btree_firstrec(tcur, level);
531 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
533 * decrement to last in block
535 if ((error = xfs_bmbt_decrement(tcur, level, &i))) {
536 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
539 i = xfs_btree_firstrec(tcur, level);
540 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
541 lbp = tcur->bc_bufs[level];
542 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
544 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
545 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
549 bno = be64_to_cpu(left->bb_rightsib);
550 if (be16_to_cpu(left->bb_numrecs) - 1 >=
551 XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
552 if ((error = xfs_bmbt_rshift(tcur, level, &i))) {
553 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
557 ASSERT(be16_to_cpu(block->bb_numrecs) >=
558 XFS_BMAP_BLOCK_IMINRECS(level, tcur));
559 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
563 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
568 lrecs = be16_to_cpu(left->bb_numrecs);
570 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
573 ASSERT(bno != NULLFSBLOCK);
574 if (lbno != NULLFSBLOCK &&
575 lrecs + be16_to_cpu(block->bb_numrecs) <= XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
579 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, lbno, 0, &lbp,
580 XFS_BMAP_BTREE_REF))) {
581 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
584 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
585 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
586 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
589 } else if (rbno != NULLFSBLOCK &&
590 rrecs + be16_to_cpu(block->bb_numrecs) <=
591 XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
595 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, rbno, 0, &rbp,
596 XFS_BMAP_BTREE_REF))) {
597 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
600 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
601 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
602 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
605 lrecs = be16_to_cpu(left->bb_numrecs);
607 if (level > 0 && (error = xfs_bmbt_decrement(cur, level, &i))) {
608 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
611 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
615 numlrecs = be16_to_cpu(left->bb_numrecs);
616 numrrecs = be16_to_cpu(right->bb_numrecs);
618 lkp = XFS_BMAP_KEY_IADDR(left, numlrecs + 1, cur);
619 lpp = XFS_BMAP_PTR_IADDR(left, numlrecs + 1, cur);
620 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
621 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
623 for (i = 0; i < numrrecs; i++) {
624 if ((error = xfs_btree_check_lptr_disk(cur, rpp[i], level))) {
625 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
630 memcpy(lkp, rkp, numrrecs * sizeof(*lkp));
631 memcpy(lpp, rpp, numrrecs * sizeof(*lpp));
632 xfs_bmbt_log_keys(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
633 xfs_bmbt_log_ptrs(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
635 lrp = XFS_BMAP_REC_IADDR(left, numlrecs + 1, cur);
636 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
637 memcpy(lrp, rrp, numrrecs * sizeof(*lrp));
638 xfs_bmbt_log_recs(cur, lbp, numlrecs + 1, numlrecs + numrrecs);
640 be16_add(&left->bb_numrecs, numrrecs);
641 left->bb_rightsib = right->bb_rightsib;
642 xfs_bmbt_log_block(cur, lbp, XFS_BB_RIGHTSIB | XFS_BB_NUMRECS);
643 if (be64_to_cpu(left->bb_rightsib) != NULLDFSBNO) {
644 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp,
645 be64_to_cpu(left->bb_rightsib),
646 0, &rrbp, XFS_BMAP_BTREE_REF))) {
647 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
650 rrblock = XFS_BUF_TO_BMBT_BLOCK(rrbp);
651 if ((error = xfs_btree_check_lblock(cur, rrblock, level, rrbp))) {
652 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
655 rrblock->bb_leftsib = cpu_to_be64(lbno);
656 xfs_bmbt_log_block(cur, rrbp, XFS_BB_LEFTSIB);
658 xfs_bmap_add_free(XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(rbp)), 1,
659 cur->bc_private.b.flist, mp);
660 cur->bc_private.b.ip->i_d.di_nblocks--;
661 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
662 XFS_TRANS_MOD_DQUOT_BYINO(mp, cur->bc_tp, cur->bc_private.b.ip,
663 XFS_TRANS_DQ_BCOUNT, -1L);
664 xfs_trans_binval(cur->bc_tp, rbp);
666 cur->bc_bufs[level] = lbp;
667 cur->bc_ptrs[level] += lrecs;
668 cur->bc_ra[level] = 0;
669 } else if ((error = xfs_bmbt_increment(cur, level + 1, &i))) {
670 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
674 cur->bc_ptrs[level]--;
675 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
681 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
687 * Get the data from the pointed-to record.
691 xfs_btree_cur_t *cur,
698 xfs_bmbt_block_t *block;
706 block = xfs_bmbt_get_block(cur, 0, &bp);
707 ptr = cur->bc_ptrs[0];
709 if ((error = xfs_btree_check_lblock(cur, block, 0, bp)))
712 if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) {
716 rp = XFS_BMAP_REC_IADDR(block, ptr, cur);
717 *off = xfs_bmbt_disk_get_startoff(rp);
718 *bno = xfs_bmbt_disk_get_startblock(rp);
719 *len = xfs_bmbt_disk_get_blockcount(rp);
720 *state = xfs_bmbt_disk_get_state(rp);
727 * Insert one record/level. Return information to the caller
728 * allowing the next level up to proceed if necessary.
730 STATIC int /* error */
732 xfs_btree_cur_t *cur,
735 xfs_bmbt_rec_t *recp,
736 xfs_btree_cur_t **curp,
737 int *stat) /* no-go/done/continue */
739 xfs_bmbt_block_t *block; /* bmap btree block */
740 xfs_buf_t *bp; /* buffer for block */
741 int error; /* error return value */
742 #ifdef XFS_BMBT_TRACE
743 static char fname[] = "xfs_bmbt_insrec";
745 int i; /* loop index */
746 xfs_bmbt_key_t key; /* bmap btree key */
747 xfs_bmbt_key_t *kp=NULL; /* pointer to bmap btree key */
748 int logflags; /* inode logging flags */
749 xfs_fsblock_t nbno; /* new block number */
750 struct xfs_btree_cur *ncur; /* new btree cursor */
751 xfs_bmbt_key_t nkey; /* new btree key value */
752 xfs_bmbt_rec_t nrec; /* new record count */
753 int optr; /* old key/record index */
754 xfs_bmbt_ptr_t *pp; /* pointer to bmap block addr */
755 int ptr; /* key/record index */
756 xfs_bmbt_rec_t *rp=NULL; /* pointer to bmap btree rec */
759 ASSERT(level < cur->bc_nlevels);
760 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
761 XFS_BMBT_TRACE_ARGIFR(cur, level, *bnop, recp);
762 ncur = (xfs_btree_cur_t *)0;
763 INT_SET(key.br_startoff, ARCH_CONVERT,
764 xfs_bmbt_disk_get_startoff(recp));
765 optr = ptr = cur->bc_ptrs[level];
767 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
771 XFS_STATS_INC(xs_bmbt_insrec);
772 block = xfs_bmbt_get_block(cur, level, &bp);
773 numrecs = be16_to_cpu(block->bb_numrecs);
775 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
776 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
779 if (ptr <= numrecs) {
781 rp = XFS_BMAP_REC_IADDR(block, ptr, cur);
782 xfs_btree_check_rec(XFS_BTNUM_BMAP, recp, rp);
784 kp = XFS_BMAP_KEY_IADDR(block, ptr, cur);
785 xfs_btree_check_key(XFS_BTNUM_BMAP, &key, kp);
790 if (numrecs == XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
791 if (numrecs < XFS_BMAP_BLOCK_DMAXRECS(level, cur)) {
793 * A root block, that can be made bigger.
795 xfs_iroot_realloc(cur->bc_private.b.ip, 1,
796 cur->bc_private.b.whichfork);
797 block = xfs_bmbt_get_block(cur, level, &bp);
798 } else if (level == cur->bc_nlevels - 1) {
799 if ((error = xfs_bmbt_newroot(cur, &logflags, stat)) ||
801 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
804 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
806 block = xfs_bmbt_get_block(cur, level, &bp);
808 if ((error = xfs_bmbt_rshift(cur, level, &i))) {
809 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
815 if ((error = xfs_bmbt_lshift(cur, level, &i))) {
816 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
820 optr = ptr = cur->bc_ptrs[level];
822 if ((error = xfs_bmbt_split(cur, level,
825 XFS_BMBT_TRACE_CURSOR(cur,
830 block = xfs_bmbt_get_block(
834 xfs_btree_check_lblock(cur,
835 block, level, bp))) {
836 XFS_BMBT_TRACE_CURSOR(
841 ptr = cur->bc_ptrs[level];
842 xfs_bmbt_disk_set_allf(&nrec,
843 nkey.br_startoff, 0, 0,
846 XFS_BMBT_TRACE_CURSOR(cur,
855 numrecs = be16_to_cpu(block->bb_numrecs);
857 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
858 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
860 for (i = numrecs; i >= ptr; i--) {
861 if ((error = xfs_btree_check_lptr_disk(cur, pp[i - 1],
863 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
868 memmove(&kp[ptr], &kp[ptr - 1],
869 (numrecs - ptr + 1) * sizeof(*kp));
870 memmove(&pp[ptr], &pp[ptr - 1], /* INT_: direct copy */
871 (numrecs - ptr + 1) * sizeof(*pp));
873 if ((error = xfs_btree_check_lptr(cur, *bnop, level))) {
874 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
879 pp[ptr - 1] = cpu_to_be64(*bnop);
881 block->bb_numrecs = cpu_to_be16(numrecs);
882 xfs_bmbt_log_keys(cur, bp, ptr, numrecs);
883 xfs_bmbt_log_ptrs(cur, bp, ptr, numrecs);
885 rp = XFS_BMAP_REC_IADDR(block, 1, cur);
886 memmove(&rp[ptr], &rp[ptr - 1],
887 (numrecs - ptr + 1) * sizeof(*rp));
890 block->bb_numrecs = cpu_to_be16(numrecs);
891 xfs_bmbt_log_recs(cur, bp, ptr, numrecs);
893 xfs_bmbt_log_block(cur, bp, XFS_BB_NUMRECS);
897 xfs_btree_check_rec(XFS_BTNUM_BMAP, rp + ptr - 1,
900 xfs_btree_check_key(XFS_BTNUM_BMAP, kp + ptr - 1,
904 if (optr == 1 && (error = xfs_bmbt_updkey(cur, &key, level + 1))) {
905 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
909 if (nbno != NULLFSBLOCK) {
913 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
920 xfs_btree_cur_t *cur)
922 xfs_bmbt_block_t *block;
923 xfs_bmbt_block_t *cblock;
930 #ifdef XFS_BMBT_TRACE
931 static char fname[] = "xfs_bmbt_killroot";
940 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
941 level = cur->bc_nlevels - 1;
944 * Don't deal with the root block needs to be a leaf case.
945 * We're just going to turn the thing back into extents anyway.
948 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
951 block = xfs_bmbt_get_block(cur, level, &cbp);
953 * Give up if the root has multiple children.
955 if (be16_to_cpu(block->bb_numrecs) != 1) {
956 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
960 * Only do this if the next level will fit.
961 * Then the data must be copied up to the inode,
962 * instead of freeing the root you free the next level.
964 cbp = cur->bc_bufs[level - 1];
965 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
966 if (be16_to_cpu(cblock->bb_numrecs) > XFS_BMAP_BLOCK_DMAXRECS(level, cur)) {
967 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
970 ASSERT(be64_to_cpu(cblock->bb_leftsib) == NULLDFSBNO);
971 ASSERT(be64_to_cpu(cblock->bb_rightsib) == NULLDFSBNO);
972 ip = cur->bc_private.b.ip;
973 ifp = XFS_IFORK_PTR(ip, cur->bc_private.b.whichfork);
974 ASSERT(XFS_BMAP_BLOCK_IMAXRECS(level, cur) ==
975 XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes));
976 i = (int)(be16_to_cpu(cblock->bb_numrecs) - XFS_BMAP_BLOCK_IMAXRECS(level, cur));
978 xfs_iroot_realloc(ip, i, cur->bc_private.b.whichfork);
979 block = ifp->if_broot;
981 be16_add(&block->bb_numrecs, i);
982 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
983 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
984 ckp = XFS_BMAP_KEY_IADDR(cblock, 1, cur);
985 memcpy(kp, ckp, be16_to_cpu(block->bb_numrecs) * sizeof(*kp));
986 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
987 cpp = XFS_BMAP_PTR_IADDR(cblock, 1, cur);
989 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
990 if ((error = xfs_btree_check_lptr_disk(cur, cpp[i], level - 1))) {
991 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
996 memcpy(pp, cpp, be16_to_cpu(block->bb_numrecs) * sizeof(*pp));
997 xfs_bmap_add_free(XFS_DADDR_TO_FSB(cur->bc_mp, XFS_BUF_ADDR(cbp)), 1,
998 cur->bc_private.b.flist, cur->bc_mp);
999 ip->i_d.di_nblocks--;
1000 XFS_TRANS_MOD_DQUOT_BYINO(cur->bc_mp, cur->bc_tp, ip,
1001 XFS_TRANS_DQ_BCOUNT, -1L);
1002 xfs_trans_binval(cur->bc_tp, cbp);
1003 cur->bc_bufs[level - 1] = NULL;
1004 be16_add(&block->bb_level, -1);
1005 xfs_trans_log_inode(cur->bc_tp, ip,
1006 XFS_ILOG_CORE | XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
1008 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1013 * Log key values from the btree block.
1017 xfs_btree_cur_t *cur,
1022 #ifdef XFS_BMBT_TRACE
1023 static char fname[] = "xfs_bmbt_log_keys";
1027 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1028 XFS_BMBT_TRACE_ARGBII(cur, bp, kfirst, klast);
1031 xfs_bmbt_block_t *block;
1036 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1037 kp = XFS_BMAP_KEY_DADDR(block, 1, cur);
1038 first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
1039 last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
1040 xfs_trans_log_buf(tp, bp, first, last);
1044 ip = cur->bc_private.b.ip;
1045 xfs_trans_log_inode(tp, ip,
1046 XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
1048 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1052 * Log pointer values from the btree block.
1056 xfs_btree_cur_t *cur,
1061 #ifdef XFS_BMBT_TRACE
1062 static char fname[] = "xfs_bmbt_log_ptrs";
1066 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1067 XFS_BMBT_TRACE_ARGBII(cur, bp, pfirst, plast);
1070 xfs_bmbt_block_t *block;
1075 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1076 pp = XFS_BMAP_PTR_DADDR(block, 1, cur);
1077 first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
1078 last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
1079 xfs_trans_log_buf(tp, bp, first, last);
1083 ip = cur->bc_private.b.ip;
1084 xfs_trans_log_inode(tp, ip,
1085 XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
1087 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1091 * Lookup the record. The cursor is made to point to it, based on dir.
1093 STATIC int /* error */
1095 xfs_btree_cur_t *cur,
1097 int *stat) /* success/failure */
1099 xfs_bmbt_block_t *block=NULL;
1103 int error; /* error return value */
1104 #ifdef XFS_BMBT_TRACE
1105 static char fname[] = "xfs_bmbt_lookup";
1107 xfs_fsblock_t fsbno=0;
1111 xfs_bmbt_key_t *kkbase=NULL;
1112 xfs_bmbt_key_t *kkp;
1113 xfs_bmbt_rec_t *krbase=NULL;
1114 xfs_bmbt_rec_t *krp;
1119 xfs_bmbt_irec_t *rp;
1120 xfs_fileoff_t startoff;
1123 XFS_STATS_INC(xs_bmbt_lookup);
1124 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1125 XFS_BMBT_TRACE_ARGI(cur, (int)dir);
1128 rp = &cur->bc_rec.b;
1129 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1130 if (level < cur->bc_nlevels - 1) {
1131 d = XFS_FSB_TO_DADDR(mp, fsbno);
1132 bp = cur->bc_bufs[level];
1133 if (bp && XFS_BUF_ADDR(bp) != d)
1134 bp = (xfs_buf_t *)0;
1136 if ((error = xfs_btree_read_bufl(mp, tp, fsbno,
1137 0, &bp, XFS_BMAP_BTREE_REF))) {
1138 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1141 xfs_btree_setbuf(cur, level, bp);
1142 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1143 if ((error = xfs_btree_check_lblock(cur, block,
1145 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1149 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1151 block = xfs_bmbt_get_block(cur, level, &bp);
1156 kkbase = XFS_BMAP_KEY_IADDR(block, 1, cur);
1158 krbase = XFS_BMAP_REC_IADDR(block, 1, cur);
1160 if (!(high = be16_to_cpu(block->bb_numrecs))) {
1162 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1163 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1167 while (low <= high) {
1168 XFS_STATS_INC(xs_bmbt_compare);
1169 keyno = (low + high) >> 1;
1171 kkp = kkbase + keyno - 1;
1172 startoff = INT_GET(kkp->br_startoff, ARCH_CONVERT);
1174 krp = krbase + keyno - 1;
1175 startoff = xfs_bmbt_disk_get_startoff(krp);
1177 diff = (xfs_sfiloff_t)
1178 (startoff - rp->br_startoff);
1188 if (diff > 0 && --keyno < 1)
1190 pp = XFS_BMAP_PTR_IADDR(block, keyno, cur);
1191 fsbno = be64_to_cpu(*pp);
1193 if ((error = xfs_btree_check_lptr(cur, fsbno, level))) {
1194 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1198 cur->bc_ptrs[level] = keyno;
1201 if (dir != XFS_LOOKUP_LE && diff < 0) {
1204 * If ge search and we went off the end of the block, but it's
1205 * not the last block, we're in the wrong block.
1207 if (dir == XFS_LOOKUP_GE && keyno > be16_to_cpu(block->bb_numrecs) &&
1208 be64_to_cpu(block->bb_rightsib) != NULLDFSBNO) {
1209 cur->bc_ptrs[0] = keyno;
1210 if ((error = xfs_bmbt_increment(cur, 0, &i))) {
1211 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1214 XFS_WANT_CORRUPTED_RETURN(i == 1);
1215 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1220 else if (dir == XFS_LOOKUP_LE && diff > 0)
1222 cur->bc_ptrs[0] = keyno;
1223 if (keyno == 0 || keyno > be16_to_cpu(block->bb_numrecs)) {
1224 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1227 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1228 *stat = ((dir != XFS_LOOKUP_EQ) || (diff == 0));
1234 * Move 1 record left from cur/level if possible.
1235 * Update cur to reflect the new path.
1237 STATIC int /* error */
1239 xfs_btree_cur_t *cur,
1241 int *stat) /* success/failure */
1243 int error; /* error return value */
1244 #ifdef XFS_BMBT_TRACE
1245 static char fname[] = "xfs_bmbt_lshift";
1248 int i; /* loop counter */
1250 xfs_bmbt_key_t key; /* bmap btree key */
1251 xfs_buf_t *lbp; /* left buffer pointer */
1252 xfs_bmbt_block_t *left; /* left btree block */
1253 xfs_bmbt_key_t *lkp=NULL; /* left btree key */
1254 xfs_bmbt_ptr_t *lpp; /* left address pointer */
1255 int lrecs; /* left record count */
1256 xfs_bmbt_rec_t *lrp=NULL; /* left record pointer */
1257 xfs_mount_t *mp; /* file system mount point */
1258 xfs_buf_t *rbp; /* right buffer pointer */
1259 xfs_bmbt_block_t *right; /* right btree block */
1260 xfs_bmbt_key_t *rkp=NULL; /* right btree key */
1261 xfs_bmbt_ptr_t *rpp=NULL; /* right address pointer */
1262 xfs_bmbt_rec_t *rrp=NULL; /* right record pointer */
1263 int rrecs; /* right record count */
1265 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1266 XFS_BMBT_TRACE_ARGI(cur, level);
1267 if (level == cur->bc_nlevels - 1) {
1268 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1272 rbp = cur->bc_bufs[level];
1273 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
1275 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
1276 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1280 if (be64_to_cpu(right->bb_leftsib) == NULLDFSBNO) {
1281 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1285 if (cur->bc_ptrs[level] <= 1) {
1286 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1291 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, be64_to_cpu(right->bb_leftsib), 0,
1292 &lbp, XFS_BMAP_BTREE_REF))) {
1293 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1296 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
1297 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
1298 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1301 if (be16_to_cpu(left->bb_numrecs) == XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
1302 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1306 lrecs = be16_to_cpu(left->bb_numrecs) + 1;
1308 lkp = XFS_BMAP_KEY_IADDR(left, lrecs, cur);
1309 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
1311 xfs_bmbt_log_keys(cur, lbp, lrecs, lrecs);
1312 lpp = XFS_BMAP_PTR_IADDR(left, lrecs, cur);
1313 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
1315 if ((error = xfs_btree_check_lptr_disk(cur, *rpp, level))) {
1316 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1320 *lpp = *rpp; /* INT_: direct copy */
1321 xfs_bmbt_log_ptrs(cur, lbp, lrecs, lrecs);
1323 lrp = XFS_BMAP_REC_IADDR(left, lrecs, cur);
1324 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
1326 xfs_bmbt_log_recs(cur, lbp, lrecs, lrecs);
1328 left->bb_numrecs = cpu_to_be16(lrecs);
1329 xfs_bmbt_log_block(cur, lbp, XFS_BB_NUMRECS);
1332 xfs_btree_check_key(XFS_BTNUM_BMAP, lkp - 1, lkp);
1334 xfs_btree_check_rec(XFS_BTNUM_BMAP, lrp - 1, lrp);
1336 rrecs = be16_to_cpu(right->bb_numrecs) - 1;
1337 right->bb_numrecs = cpu_to_be16(rrecs);
1338 xfs_bmbt_log_block(cur, rbp, XFS_BB_NUMRECS);
1341 for (i = 0; i < rrecs; i++) {
1342 if ((error = xfs_btree_check_lptr_disk(cur, rpp[i + 1],
1344 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1349 memmove(rkp, rkp + 1, rrecs * sizeof(*rkp));
1350 memmove(rpp, rpp + 1, rrecs * sizeof(*rpp));
1351 xfs_bmbt_log_keys(cur, rbp, 1, rrecs);
1352 xfs_bmbt_log_ptrs(cur, rbp, 1, rrecs);
1354 memmove(rrp, rrp + 1, rrecs * sizeof(*rrp));
1355 xfs_bmbt_log_recs(cur, rbp, 1, rrecs);
1356 INT_SET(key.br_startoff, ARCH_CONVERT,
1357 xfs_bmbt_disk_get_startoff(rrp));
1360 if ((error = xfs_bmbt_updkey(cur, rkp, level + 1))) {
1361 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1364 cur->bc_ptrs[level]--;
1365 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1371 * Move 1 record right from cur/level if possible.
1372 * Update cur to reflect the new path.
1374 STATIC int /* error */
1376 xfs_btree_cur_t *cur,
1378 int *stat) /* success/failure */
1380 int error; /* error return value */
1381 #ifdef XFS_BMBT_TRACE
1382 static char fname[] = "xfs_bmbt_rshift";
1384 int i; /* loop counter */
1385 xfs_bmbt_key_t key; /* bmap btree key */
1386 xfs_buf_t *lbp; /* left buffer pointer */
1387 xfs_bmbt_block_t *left; /* left btree block */
1388 xfs_bmbt_key_t *lkp; /* left btree key */
1389 xfs_bmbt_ptr_t *lpp; /* left address pointer */
1390 xfs_bmbt_rec_t *lrp; /* left record pointer */
1391 xfs_mount_t *mp; /* file system mount point */
1392 xfs_buf_t *rbp; /* right buffer pointer */
1393 xfs_bmbt_block_t *right; /* right btree block */
1394 xfs_bmbt_key_t *rkp; /* right btree key */
1395 xfs_bmbt_ptr_t *rpp; /* right address pointer */
1396 xfs_bmbt_rec_t *rrp=NULL; /* right record pointer */
1397 struct xfs_btree_cur *tcur; /* temporary btree cursor */
1399 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1400 XFS_BMBT_TRACE_ARGI(cur, level);
1401 if (level == cur->bc_nlevels - 1) {
1402 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1406 lbp = cur->bc_bufs[level];
1407 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
1409 if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
1410 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1414 if (be64_to_cpu(left->bb_rightsib) == NULLDFSBNO) {
1415 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1419 if (cur->bc_ptrs[level] >= be16_to_cpu(left->bb_numrecs)) {
1420 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1425 if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, be64_to_cpu(left->bb_rightsib), 0,
1426 &rbp, XFS_BMAP_BTREE_REF))) {
1427 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1430 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
1431 if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
1432 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1435 if (be16_to_cpu(right->bb_numrecs) == XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
1436 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1441 lkp = XFS_BMAP_KEY_IADDR(left, be16_to_cpu(left->bb_numrecs), cur);
1442 lpp = XFS_BMAP_PTR_IADDR(left, be16_to_cpu(left->bb_numrecs), cur);
1443 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
1444 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
1446 for (i = be16_to_cpu(right->bb_numrecs) - 1; i >= 0; i--) {
1447 if ((error = xfs_btree_check_lptr_disk(cur, rpp[i], level))) {
1448 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1453 memmove(rkp + 1, rkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1454 memmove(rpp + 1, rpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1456 if ((error = xfs_btree_check_lptr_disk(cur, *lpp, level))) {
1457 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1462 *rpp = *lpp; /* INT_: direct copy */
1463 xfs_bmbt_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
1464 xfs_bmbt_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
1466 lrp = XFS_BMAP_REC_IADDR(left, be16_to_cpu(left->bb_numrecs), cur);
1467 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
1468 memmove(rrp + 1, rrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1470 xfs_bmbt_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
1471 INT_SET(key.br_startoff, ARCH_CONVERT,
1472 xfs_bmbt_disk_get_startoff(rrp));
1475 be16_add(&left->bb_numrecs, -1);
1476 xfs_bmbt_log_block(cur, lbp, XFS_BB_NUMRECS);
1477 be16_add(&right->bb_numrecs, 1);
1480 xfs_btree_check_key(XFS_BTNUM_BMAP, rkp, rkp + 1);
1482 xfs_btree_check_rec(XFS_BTNUM_BMAP, rrp, rrp + 1);
1484 xfs_bmbt_log_block(cur, rbp, XFS_BB_NUMRECS);
1485 if ((error = xfs_btree_dup_cursor(cur, &tcur))) {
1486 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1489 i = xfs_btree_lastrec(tcur, level);
1490 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1491 if ((error = xfs_bmbt_increment(tcur, level, &i))) {
1492 XFS_BMBT_TRACE_CURSOR(tcur, ERROR);
1495 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1496 if ((error = xfs_bmbt_updkey(tcur, rkp, level + 1))) {
1497 XFS_BMBT_TRACE_CURSOR(tcur, ERROR);
1500 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1501 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1505 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1507 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1512 * Determine the extent state.
1521 ASSERT(blks != 0); /* saved for DMIG */
1522 return XFS_EXT_UNWRITTEN;
1524 return XFS_EXT_NORM;
1529 * Split cur/level block in half.
1530 * Return new block number and its first record (to be inserted into parent).
1532 STATIC int /* error */
1534 xfs_btree_cur_t *cur,
1536 xfs_fsblock_t *bnop,
1537 xfs_bmbt_key_t *keyp,
1538 xfs_btree_cur_t **curp,
1539 int *stat) /* success/failure */
1541 xfs_alloc_arg_t args; /* block allocation args */
1542 int error; /* error return value */
1543 #ifdef XFS_BMBT_TRACE
1544 static char fname[] = "xfs_bmbt_split";
1546 int i; /* loop counter */
1547 xfs_fsblock_t lbno; /* left sibling block number */
1548 xfs_buf_t *lbp; /* left buffer pointer */
1549 xfs_bmbt_block_t *left; /* left btree block */
1550 xfs_bmbt_key_t *lkp; /* left btree key */
1551 xfs_bmbt_ptr_t *lpp; /* left address pointer */
1552 xfs_bmbt_rec_t *lrp; /* left record pointer */
1553 xfs_buf_t *rbp; /* right buffer pointer */
1554 xfs_bmbt_block_t *right; /* right btree block */
1555 xfs_bmbt_key_t *rkp; /* right btree key */
1556 xfs_bmbt_ptr_t *rpp; /* right address pointer */
1557 xfs_bmbt_block_t *rrblock; /* right-right btree block */
1558 xfs_buf_t *rrbp; /* right-right buffer pointer */
1559 xfs_bmbt_rec_t *rrp; /* right record pointer */
1561 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1562 XFS_BMBT_TRACE_ARGIFK(cur, level, *bnop, keyp);
1563 args.tp = cur->bc_tp;
1564 args.mp = cur->bc_mp;
1565 lbp = cur->bc_bufs[level];
1566 lbno = XFS_DADDR_TO_FSB(args.mp, XFS_BUF_ADDR(lbp));
1567 left = XFS_BUF_TO_BMBT_BLOCK(lbp);
1568 args.fsbno = cur->bc_private.b.firstblock;
1569 args.firstblock = args.fsbno;
1570 if (args.fsbno == NULLFSBLOCK) {
1572 args.type = XFS_ALLOCTYPE_START_BNO;
1574 args.type = XFS_ALLOCTYPE_NEAR_BNO;
1575 args.mod = args.minleft = args.alignment = args.total = args.isfl =
1576 args.userdata = args.minalignslop = 0;
1577 args.minlen = args.maxlen = args.prod = 1;
1578 args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL;
1579 if (!args.wasdel && xfs_trans_get_block_res(args.tp) == 0) {
1580 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1581 return XFS_ERROR(ENOSPC);
1583 if ((error = xfs_alloc_vextent(&args))) {
1584 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1587 if (args.fsbno == NULLFSBLOCK) {
1588 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1592 ASSERT(args.len == 1);
1593 cur->bc_private.b.firstblock = args.fsbno;
1594 cur->bc_private.b.allocated++;
1595 cur->bc_private.b.ip->i_d.di_nblocks++;
1596 xfs_trans_log_inode(args.tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
1597 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, args.tp, cur->bc_private.b.ip,
1598 XFS_TRANS_DQ_BCOUNT, 1L);
1599 rbp = xfs_btree_get_bufl(args.mp, args.tp, args.fsbno, 0);
1600 right = XFS_BUF_TO_BMBT_BLOCK(rbp);
1602 if ((error = xfs_btree_check_lblock(cur, left, level, rbp))) {
1603 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1607 right->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
1608 right->bb_level = left->bb_level;
1609 right->bb_numrecs = cpu_to_be16(be16_to_cpu(left->bb_numrecs) / 2);
1610 if ((be16_to_cpu(left->bb_numrecs) & 1) &&
1611 cur->bc_ptrs[level] <= be16_to_cpu(right->bb_numrecs) + 1)
1612 be16_add(&right->bb_numrecs, 1);
1613 i = be16_to_cpu(left->bb_numrecs) - be16_to_cpu(right->bb_numrecs) + 1;
1615 lkp = XFS_BMAP_KEY_IADDR(left, i, cur);
1616 lpp = XFS_BMAP_PTR_IADDR(left, i, cur);
1617 rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
1618 rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
1620 for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
1621 if ((error = xfs_btree_check_lptr_disk(cur, lpp[i], level))) {
1622 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1627 memcpy(rkp, lkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1628 memcpy(rpp, lpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1629 xfs_bmbt_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1630 xfs_bmbt_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1631 keyp->br_startoff = INT_GET(rkp->br_startoff, ARCH_CONVERT);
1633 lrp = XFS_BMAP_REC_IADDR(left, i, cur);
1634 rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
1635 memcpy(rrp, lrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1636 xfs_bmbt_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1637 keyp->br_startoff = xfs_bmbt_disk_get_startoff(rrp);
1639 be16_add(&left->bb_numrecs, -(be16_to_cpu(right->bb_numrecs)));
1640 right->bb_rightsib = left->bb_rightsib;
1641 left->bb_rightsib = cpu_to_be64(args.fsbno);
1642 right->bb_leftsib = cpu_to_be64(lbno);
1643 xfs_bmbt_log_block(cur, rbp, XFS_BB_ALL_BITS);
1644 xfs_bmbt_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
1645 if (be64_to_cpu(right->bb_rightsib) != NULLDFSBNO) {
1646 if ((error = xfs_btree_read_bufl(args.mp, args.tp,
1647 be64_to_cpu(right->bb_rightsib), 0, &rrbp,
1648 XFS_BMAP_BTREE_REF))) {
1649 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1652 rrblock = XFS_BUF_TO_BMBT_BLOCK(rrbp);
1653 if ((error = xfs_btree_check_lblock(cur, rrblock, level, rrbp))) {
1654 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1657 rrblock->bb_leftsib = cpu_to_be64(args.fsbno);
1658 xfs_bmbt_log_block(cur, rrbp, XFS_BB_LEFTSIB);
1660 if (cur->bc_ptrs[level] > be16_to_cpu(left->bb_numrecs) + 1) {
1661 xfs_btree_setbuf(cur, level, rbp);
1662 cur->bc_ptrs[level] -= be16_to_cpu(left->bb_numrecs);
1664 if (level + 1 < cur->bc_nlevels) {
1665 if ((error = xfs_btree_dup_cursor(cur, curp))) {
1666 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1669 (*curp)->bc_ptrs[level + 1]++;
1672 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1679 * Update keys for the record.
1683 xfs_btree_cur_t *cur,
1684 xfs_bmbt_key_t *keyp, /* on-disk format */
1687 xfs_bmbt_block_t *block;
1692 #ifdef XFS_BMBT_TRACE
1693 static char fname[] = "xfs_bmbt_updkey";
1699 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1700 XFS_BMBT_TRACE_ARGIK(cur, level, keyp);
1701 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1702 block = xfs_bmbt_get_block(cur, level, &bp);
1704 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
1705 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1709 ptr = cur->bc_ptrs[level];
1710 kp = XFS_BMAP_KEY_IADDR(block, ptr, cur);
1712 xfs_bmbt_log_keys(cur, bp, ptr, ptr);
1714 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1719 * Convert on-disk form of btree root to in-memory form.
1723 xfs_bmdr_block_t *dblock,
1725 xfs_bmbt_block_t *rblock,
1729 xfs_bmbt_key_t *fkp;
1731 xfs_bmbt_key_t *tkp;
1734 rblock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
1735 rblock->bb_level = dblock->bb_level;
1736 ASSERT(be16_to_cpu(rblock->bb_level) > 0);
1737 rblock->bb_numrecs = dblock->bb_numrecs;
1738 rblock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
1739 rblock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
1740 dmxr = (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen, xfs_bmdr, 0);
1741 fkp = XFS_BTREE_KEY_ADDR(dblocklen, xfs_bmdr, dblock, 1, dmxr);
1742 tkp = XFS_BMAP_BROOT_KEY_ADDR(rblock, 1, rblocklen);
1743 fpp = XFS_BTREE_PTR_ADDR(dblocklen, xfs_bmdr, dblock, 1, dmxr);
1744 tpp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, rblocklen);
1745 dmxr = be16_to_cpu(dblock->bb_numrecs);
1746 memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
1747 memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
1751 * Decrement cursor by one record at the level.
1752 * For nonzero levels the leaf-ward information is untouched.
1756 xfs_btree_cur_t *cur,
1758 int *stat) /* success/failure */
1760 xfs_bmbt_block_t *block;
1762 int error; /* error return value */
1763 #ifdef XFS_BMBT_TRACE
1764 static char fname[] = "xfs_bmbt_decrement";
1766 xfs_fsblock_t fsbno;
1771 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1772 XFS_BMBT_TRACE_ARGI(cur, level);
1773 ASSERT(level < cur->bc_nlevels);
1774 if (level < cur->bc_nlevels - 1)
1775 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1776 if (--cur->bc_ptrs[level] > 0) {
1777 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1781 block = xfs_bmbt_get_block(cur, level, &bp);
1783 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
1784 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1788 if (be64_to_cpu(block->bb_leftsib) == NULLDFSBNO) {
1789 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1793 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1794 if (--cur->bc_ptrs[lev] > 0)
1796 if (lev < cur->bc_nlevels - 1)
1797 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1799 if (lev == cur->bc_nlevels) {
1800 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1806 for (block = xfs_bmbt_get_block(cur, lev, &bp); lev > level; ) {
1807 fsbno = be64_to_cpu(*XFS_BMAP_PTR_IADDR(block, cur->bc_ptrs[lev], cur));
1808 if ((error = xfs_btree_read_bufl(mp, tp, fsbno, 0, &bp,
1809 XFS_BMAP_BTREE_REF))) {
1810 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1814 xfs_btree_setbuf(cur, lev, bp);
1815 block = XFS_BUF_TO_BMBT_BLOCK(bp);
1816 if ((error = xfs_btree_check_lblock(cur, block, lev, bp))) {
1817 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1820 cur->bc_ptrs[lev] = be16_to_cpu(block->bb_numrecs);
1822 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1828 * Delete the record pointed to by cur.
1832 xfs_btree_cur_t *cur,
1833 int *stat) /* success/failure */
1835 int error; /* error return value */
1836 #ifdef XFS_BMBT_TRACE
1837 static char fname[] = "xfs_bmbt_delete";
1842 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
1843 for (level = 0, i = 2; i == 2; level++) {
1844 if ((error = xfs_bmbt_delrec(cur, level, &i))) {
1845 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1850 for (level = 1; level < cur->bc_nlevels; level++) {
1851 if (cur->bc_ptrs[level] == 0) {
1852 if ((error = xfs_bmbt_decrement(cur, level,
1854 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
1861 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
1867 * Convert a compressed bmap extent record to an uncompressed form.
1868 * This code must be in sync with the routines xfs_bmbt_get_startoff,
1869 * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state.
1872 STATIC __inline__ void
1881 ext_flag = (int)(l0 >> (64 - BMBT_EXNTFLAG_BITLEN));
1882 s->br_startoff = ((xfs_fileoff_t)l0 &
1883 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
1885 s->br_startblock = (((xfs_fsblock_t)l0 & XFS_MASK64LO(9)) << 43) |
1886 (((xfs_fsblock_t)l1) >> 21);
1892 b = (((xfs_dfsbno_t)l0 & XFS_MASK64LO(9)) << 43) |
1893 (((xfs_dfsbno_t)l1) >> 21);
1894 ASSERT((b >> 32) == 0 || ISNULLDSTARTBLOCK(b));
1895 s->br_startblock = (xfs_fsblock_t)b;
1898 s->br_startblock = (xfs_fsblock_t)(((xfs_dfsbno_t)l1) >> 21);
1900 #endif /* XFS_BIG_BLKNOS */
1901 s->br_blockcount = (xfs_filblks_t)(l1 & XFS_MASK64LO(21));
1902 /* This is xfs_extent_state() in-line */
1904 ASSERT(s->br_blockcount != 0); /* saved for DMIG */
1905 st = XFS_EXT_UNWRITTEN;
1916 __xfs_bmbt_get_all(r->l0, r->l1, s);
1920 * Get the block pointer for the given level of the cursor.
1921 * Fill in the buffer pointer, if applicable.
1925 xfs_btree_cur_t *cur,
1930 xfs_bmbt_block_t *rval;
1932 if (level < cur->bc_nlevels - 1) {
1933 *bpp = cur->bc_bufs[level];
1934 rval = XFS_BUF_TO_BMBT_BLOCK(*bpp);
1937 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
1938 cur->bc_private.b.whichfork);
1939 rval = ifp->if_broot;
1945 * Extract the blockcount field from an in memory bmap extent record.
1948 xfs_bmbt_get_blockcount(
1951 return (xfs_filblks_t)(r->l1 & XFS_MASK64LO(21));
1955 * Extract the startblock field from an in memory bmap extent record.
1958 xfs_bmbt_get_startblock(
1962 return (((xfs_fsblock_t)r->l0 & XFS_MASK64LO(9)) << 43) |
1963 (((xfs_fsblock_t)r->l1) >> 21);
1968 b = (((xfs_dfsbno_t)r->l0 & XFS_MASK64LO(9)) << 43) |
1969 (((xfs_dfsbno_t)r->l1) >> 21);
1970 ASSERT((b >> 32) == 0 || ISNULLDSTARTBLOCK(b));
1971 return (xfs_fsblock_t)b;
1973 return (xfs_fsblock_t)(((xfs_dfsbno_t)r->l1) >> 21);
1975 #endif /* XFS_BIG_BLKNOS */
1979 * Extract the startoff field from an in memory bmap extent record.
1982 xfs_bmbt_get_startoff(
1985 return ((xfs_fileoff_t)r->l0 &
1986 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
1995 ext_flag = (int)((r->l0) >> (64 - BMBT_EXNTFLAG_BITLEN));
1996 return xfs_extent_state(xfs_bmbt_get_blockcount(r),
2000 #ifndef XFS_NATIVE_HOST
2001 /* Endian flipping versions of the bmbt extraction functions */
2003 xfs_bmbt_disk_get_all(
2009 l0 = INT_GET(r->l0, ARCH_CONVERT);
2010 l1 = INT_GET(r->l1, ARCH_CONVERT);
2012 __xfs_bmbt_get_all(l0, l1, s);
2016 * Extract the blockcount field from an on disk bmap extent record.
2019 xfs_bmbt_disk_get_blockcount(
2022 return (xfs_filblks_t)(INT_GET(r->l1, ARCH_CONVERT) & XFS_MASK64LO(21));
2026 * Extract the startblock field from an on disk bmap extent record.
2029 xfs_bmbt_disk_get_startblock(
2033 return (((xfs_fsblock_t)INT_GET(r->l0, ARCH_CONVERT) & XFS_MASK64LO(9)) << 43) |
2034 (((xfs_fsblock_t)INT_GET(r->l1, ARCH_CONVERT)) >> 21);
2039 b = (((xfs_dfsbno_t)INT_GET(r->l0, ARCH_CONVERT) & XFS_MASK64LO(9)) << 43) |
2040 (((xfs_dfsbno_t)INT_GET(r->l1, ARCH_CONVERT)) >> 21);
2041 ASSERT((b >> 32) == 0 || ISNULLDSTARTBLOCK(b));
2042 return (xfs_fsblock_t)b;
2044 return (xfs_fsblock_t)(((xfs_dfsbno_t)INT_GET(r->l1, ARCH_CONVERT)) >> 21);
2046 #endif /* XFS_BIG_BLKNOS */
2050 * Extract the startoff field from a disk format bmap extent record.
2053 xfs_bmbt_disk_get_startoff(
2056 return ((xfs_fileoff_t)INT_GET(r->l0, ARCH_CONVERT) &
2057 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
2061 xfs_bmbt_disk_get_state(
2066 ext_flag = (int)((INT_GET(r->l0, ARCH_CONVERT)) >> (64 - BMBT_EXNTFLAG_BITLEN));
2067 return xfs_extent_state(xfs_bmbt_disk_get_blockcount(r),
2070 #endif /* XFS_NATIVE_HOST */
2074 * Increment cursor by one record at the level.
2075 * For nonzero levels the leaf-ward information is untouched.
2079 xfs_btree_cur_t *cur,
2081 int *stat) /* success/failure */
2083 xfs_bmbt_block_t *block;
2085 int error; /* error return value */
2086 #ifdef XFS_BMBT_TRACE
2087 static char fname[] = "xfs_bmbt_increment";
2089 xfs_fsblock_t fsbno;
2094 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2095 XFS_BMBT_TRACE_ARGI(cur, level);
2096 ASSERT(level < cur->bc_nlevels);
2097 if (level < cur->bc_nlevels - 1)
2098 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
2099 block = xfs_bmbt_get_block(cur, level, &bp);
2101 if ((error = xfs_btree_check_lblock(cur, block, level, bp))) {
2102 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2106 if (++cur->bc_ptrs[level] <= be16_to_cpu(block->bb_numrecs)) {
2107 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2111 if (be64_to_cpu(block->bb_rightsib) == NULLDFSBNO) {
2112 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2116 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
2117 block = xfs_bmbt_get_block(cur, lev, &bp);
2119 if ((error = xfs_btree_check_lblock(cur, block, lev, bp))) {
2120 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2124 if (++cur->bc_ptrs[lev] <= be16_to_cpu(block->bb_numrecs))
2126 if (lev < cur->bc_nlevels - 1)
2127 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
2129 if (lev == cur->bc_nlevels) {
2130 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2136 for (block = xfs_bmbt_get_block(cur, lev, &bp); lev > level; ) {
2137 fsbno = be64_to_cpu(*XFS_BMAP_PTR_IADDR(block, cur->bc_ptrs[lev], cur));
2138 if ((error = xfs_btree_read_bufl(mp, tp, fsbno, 0, &bp,
2139 XFS_BMAP_BTREE_REF))) {
2140 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2144 xfs_btree_setbuf(cur, lev, bp);
2145 block = XFS_BUF_TO_BMBT_BLOCK(bp);
2146 if ((error = xfs_btree_check_lblock(cur, block, lev, bp))) {
2147 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2150 cur->bc_ptrs[lev] = 1;
2152 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2158 * Insert the current record at the point referenced by cur.
2162 xfs_btree_cur_t *cur,
2163 int *stat) /* success/failure */
2165 int error; /* error return value */
2166 #ifdef XFS_BMBT_TRACE
2167 static char fname[] = "xfs_bmbt_insert";
2172 xfs_btree_cur_t *ncur;
2173 xfs_bmbt_rec_t nrec;
2174 xfs_btree_cur_t *pcur;
2176 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2179 xfs_bmbt_disk_set_all(&nrec, &cur->bc_rec.b);
2180 ncur = (xfs_btree_cur_t *)0;
2183 if ((error = xfs_bmbt_insrec(pcur, level++, &nbno, &nrec, &ncur,
2186 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
2187 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2190 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2191 if (pcur != cur && (ncur || nbno == NULLFSBLOCK)) {
2192 cur->bc_nlevels = pcur->bc_nlevels;
2193 cur->bc_private.b.allocated +=
2194 pcur->bc_private.b.allocated;
2195 pcur->bc_private.b.allocated = 0;
2196 ASSERT((cur->bc_private.b.firstblock != NULLFSBLOCK) ||
2197 (cur->bc_private.b.ip->i_d.di_flags &
2198 XFS_DIFLAG_REALTIME));
2199 cur->bc_private.b.firstblock =
2200 pcur->bc_private.b.firstblock;
2201 ASSERT(cur->bc_private.b.flist ==
2202 pcur->bc_private.b.flist);
2203 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
2207 ncur = (xfs_btree_cur_t *)0;
2209 } while (nbno != NULLFSBLOCK);
2210 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2214 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2219 * Log fields from the btree block header.
2223 xfs_btree_cur_t *cur,
2228 #ifdef XFS_BMBT_TRACE
2229 static char fname[] = "xfs_bmbt_log_block";
2233 static const short offsets[] = {
2234 offsetof(xfs_bmbt_block_t, bb_magic),
2235 offsetof(xfs_bmbt_block_t, bb_level),
2236 offsetof(xfs_bmbt_block_t, bb_numrecs),
2237 offsetof(xfs_bmbt_block_t, bb_leftsib),
2238 offsetof(xfs_bmbt_block_t, bb_rightsib),
2239 sizeof(xfs_bmbt_block_t)
2242 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2243 XFS_BMBT_TRACE_ARGBI(cur, bp, fields);
2246 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first,
2248 xfs_trans_log_buf(tp, bp, first, last);
2250 xfs_trans_log_inode(tp, cur->bc_private.b.ip,
2251 XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
2252 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2256 * Log record values from the btree block.
2260 xfs_btree_cur_t *cur,
2265 xfs_bmbt_block_t *block;
2267 #ifdef XFS_BMBT_TRACE
2268 static char fname[] = "xfs_bmbt_log_recs";
2274 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2275 XFS_BMBT_TRACE_ARGBII(cur, bp, rfirst, rlast);
2278 block = XFS_BUF_TO_BMBT_BLOCK(bp);
2279 rp = XFS_BMAP_REC_DADDR(block, 1, cur);
2280 first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
2281 last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
2282 xfs_trans_log_buf(tp, bp, first, last);
2283 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2288 xfs_btree_cur_t *cur,
2292 int *stat) /* success/failure */
2294 cur->bc_rec.b.br_startoff = off;
2295 cur->bc_rec.b.br_startblock = bno;
2296 cur->bc_rec.b.br_blockcount = len;
2297 return xfs_bmbt_lookup(cur, XFS_LOOKUP_EQ, stat);
2302 xfs_btree_cur_t *cur,
2306 int *stat) /* success/failure */
2308 cur->bc_rec.b.br_startoff = off;
2309 cur->bc_rec.b.br_startblock = bno;
2310 cur->bc_rec.b.br_blockcount = len;
2311 return xfs_bmbt_lookup(cur, XFS_LOOKUP_GE, stat);
2315 * Give the bmap btree a new root block. Copy the old broot contents
2316 * down into a real block and make the broot point to it.
2320 xfs_btree_cur_t *cur, /* btree cursor */
2321 int *logflags, /* logging flags for inode */
2322 int *stat) /* return status - 0 fail */
2324 xfs_alloc_arg_t args; /* allocation arguments */
2325 xfs_bmbt_block_t *block; /* bmap btree block */
2326 xfs_buf_t *bp; /* buffer for block */
2327 xfs_bmbt_block_t *cblock; /* child btree block */
2328 xfs_bmbt_key_t *ckp; /* child key pointer */
2329 xfs_bmbt_ptr_t *cpp; /* child ptr pointer */
2330 int error; /* error return code */
2331 #ifdef XFS_BMBT_TRACE
2332 static char fname[] = "xfs_bmbt_newroot";
2335 int i; /* loop counter */
2337 xfs_bmbt_key_t *kp; /* pointer to bmap btree key */
2338 int level; /* btree level */
2339 xfs_bmbt_ptr_t *pp; /* pointer to bmap block addr */
2341 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2342 level = cur->bc_nlevels - 1;
2343 block = xfs_bmbt_get_block(cur, level, &bp);
2345 * Copy the root into a real block.
2347 args.mp = cur->bc_mp;
2348 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
2349 args.tp = cur->bc_tp;
2350 args.fsbno = cur->bc_private.b.firstblock;
2351 args.mod = args.minleft = args.alignment = args.total = args.isfl =
2352 args.userdata = args.minalignslop = 0;
2353 args.minlen = args.maxlen = args.prod = 1;
2354 args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL;
2355 args.firstblock = args.fsbno;
2356 if (args.fsbno == NULLFSBLOCK) {
2358 if ((error = xfs_btree_check_lptr_disk(cur, *pp, level))) {
2359 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2363 args.fsbno = be64_to_cpu(*pp);
2364 args.type = XFS_ALLOCTYPE_START_BNO;
2366 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2367 if ((error = xfs_alloc_vextent(&args))) {
2368 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2371 if (args.fsbno == NULLFSBLOCK) {
2372 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2376 ASSERT(args.len == 1);
2377 cur->bc_private.b.firstblock = args.fsbno;
2378 cur->bc_private.b.allocated++;
2379 cur->bc_private.b.ip->i_d.di_nblocks++;
2380 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, args.tp, cur->bc_private.b.ip,
2381 XFS_TRANS_DQ_BCOUNT, 1L);
2382 bp = xfs_btree_get_bufl(args.mp, cur->bc_tp, args.fsbno, 0);
2383 cblock = XFS_BUF_TO_BMBT_BLOCK(bp);
2385 be16_add(&block->bb_level, 1);
2386 block->bb_numrecs = cpu_to_be16(1);
2388 cur->bc_ptrs[level + 1] = 1;
2389 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
2390 ckp = XFS_BMAP_KEY_IADDR(cblock, 1, cur);
2391 memcpy(ckp, kp, be16_to_cpu(cblock->bb_numrecs) * sizeof(*kp));
2392 cpp = XFS_BMAP_PTR_IADDR(cblock, 1, cur);
2394 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2395 if ((error = xfs_btree_check_lptr_disk(cur, pp[i], level))) {
2396 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2401 memcpy(cpp, pp, be16_to_cpu(cblock->bb_numrecs) * sizeof(*pp));
2403 if ((error = xfs_btree_check_lptr(cur, args.fsbno, level))) {
2404 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2408 *pp = cpu_to_be64(args.fsbno);
2409 xfs_iroot_realloc(cur->bc_private.b.ip, 1 - be16_to_cpu(cblock->bb_numrecs),
2410 cur->bc_private.b.whichfork);
2411 xfs_btree_setbuf(cur, level, bp);
2413 * Do all this logging at the end so that
2414 * the root is at the right level.
2416 xfs_bmbt_log_block(cur, bp, XFS_BB_ALL_BITS);
2417 xfs_bmbt_log_keys(cur, bp, 1, be16_to_cpu(cblock->bb_numrecs));
2418 xfs_bmbt_log_ptrs(cur, bp, 1, be16_to_cpu(cblock->bb_numrecs));
2419 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2421 XFS_ILOG_CORE | XFS_ILOG_FBROOT(cur->bc_private.b.whichfork);
2427 * Set all the fields in a bmap extent record from the uncompressed form.
2436 ASSERT((s->br_state == XFS_EXT_NORM) ||
2437 (s->br_state == XFS_EXT_UNWRITTEN));
2438 extent_flag = (s->br_state == XFS_EXT_NORM) ? 0 : 1;
2439 ASSERT((s->br_startoff & XFS_MASK64HI(9)) == 0);
2440 ASSERT((s->br_blockcount & XFS_MASK64HI(43)) == 0);
2442 ASSERT((s->br_startblock & XFS_MASK64HI(12)) == 0);
2443 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2444 ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
2445 ((xfs_bmbt_rec_base_t)s->br_startblock >> 43);
2446 r->l1 = ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2447 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2448 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2449 #else /* !XFS_BIG_BLKNOS */
2450 if (ISNULLSTARTBLOCK(s->br_startblock)) {
2451 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2452 ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
2453 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
2454 r->l1 = XFS_MASK64HI(11) |
2455 ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2456 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2457 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2459 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2460 ((xfs_bmbt_rec_base_t)s->br_startoff << 9);
2461 r->l1 = ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2462 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2463 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2465 #endif /* XFS_BIG_BLKNOS */
2469 * Set all the fields in a bmap extent record from the arguments.
2481 ASSERT((v == XFS_EXT_NORM) || (v == XFS_EXT_UNWRITTEN));
2482 extent_flag = (v == XFS_EXT_NORM) ? 0 : 1;
2483 ASSERT((o & XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN)) == 0);
2484 ASSERT((c & XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
2486 ASSERT((b & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
2487 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2488 ((xfs_bmbt_rec_base_t)o << 9) |
2489 ((xfs_bmbt_rec_base_t)b >> 43);
2490 r->l1 = ((xfs_bmbt_rec_base_t)b << 21) |
2491 ((xfs_bmbt_rec_base_t)c &
2492 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2493 #else /* !XFS_BIG_BLKNOS */
2494 if (ISNULLSTARTBLOCK(b)) {
2495 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2496 ((xfs_bmbt_rec_base_t)o << 9) |
2497 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
2498 r->l1 = XFS_MASK64HI(11) |
2499 ((xfs_bmbt_rec_base_t)b << 21) |
2500 ((xfs_bmbt_rec_base_t)c &
2501 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2503 r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2504 ((xfs_bmbt_rec_base_t)o << 9);
2505 r->l1 = ((xfs_bmbt_rec_base_t)b << 21) |
2506 ((xfs_bmbt_rec_base_t)c &
2507 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2509 #endif /* XFS_BIG_BLKNOS */
2512 #ifndef XFS_NATIVE_HOST
2514 * Set all the fields in a bmap extent record from the uncompressed form.
2517 xfs_bmbt_disk_set_all(
2523 ASSERT((s->br_state == XFS_EXT_NORM) ||
2524 (s->br_state == XFS_EXT_UNWRITTEN));
2525 extent_flag = (s->br_state == XFS_EXT_NORM) ? 0 : 1;
2526 ASSERT((s->br_startoff & XFS_MASK64HI(9)) == 0);
2527 ASSERT((s->br_blockcount & XFS_MASK64HI(43)) == 0);
2529 ASSERT((s->br_startblock & XFS_MASK64HI(12)) == 0);
2530 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2531 ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
2532 ((xfs_bmbt_rec_base_t)s->br_startblock >> 43));
2533 INT_SET(r->l1, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2534 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2535 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2536 #else /* !XFS_BIG_BLKNOS */
2537 if (ISNULLSTARTBLOCK(s->br_startblock)) {
2538 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2539 ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
2540 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
2541 INT_SET(r->l1, ARCH_CONVERT, XFS_MASK64HI(11) |
2542 ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2543 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2544 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2546 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2547 ((xfs_bmbt_rec_base_t)s->br_startoff << 9));
2548 INT_SET(r->l1, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
2549 ((xfs_bmbt_rec_base_t)s->br_blockcount &
2550 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2552 #endif /* XFS_BIG_BLKNOS */
2556 * Set all the fields in a disk format bmap extent record from the arguments.
2559 xfs_bmbt_disk_set_allf(
2568 ASSERT((v == XFS_EXT_NORM) || (v == XFS_EXT_UNWRITTEN));
2569 extent_flag = (v == XFS_EXT_NORM) ? 0 : 1;
2570 ASSERT((o & XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN)) == 0);
2571 ASSERT((c & XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
2573 ASSERT((b & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
2574 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2575 ((xfs_bmbt_rec_base_t)o << 9) |
2576 ((xfs_bmbt_rec_base_t)b >> 43));
2577 INT_SET(r->l1, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)b << 21) |
2578 ((xfs_bmbt_rec_base_t)c &
2579 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2580 #else /* !XFS_BIG_BLKNOS */
2581 if (ISNULLSTARTBLOCK(b)) {
2582 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2583 ((xfs_bmbt_rec_base_t)o << 9) |
2584 (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
2585 INT_SET(r->l1, ARCH_CONVERT, XFS_MASK64HI(11) |
2586 ((xfs_bmbt_rec_base_t)b << 21) |
2587 ((xfs_bmbt_rec_base_t)c &
2588 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2590 INT_SET(r->l0, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)extent_flag << 63) |
2591 ((xfs_bmbt_rec_base_t)o << 9));
2592 INT_SET(r->l1, ARCH_CONVERT, ((xfs_bmbt_rec_base_t)b << 21) |
2593 ((xfs_bmbt_rec_base_t)c &
2594 (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
2596 #endif /* XFS_BIG_BLKNOS */
2598 #endif /* XFS_NATIVE_HOST */
2601 * Set the blockcount field in a bmap extent record.
2604 xfs_bmbt_set_blockcount(
2608 ASSERT((v & XFS_MASK64HI(43)) == 0);
2609 r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64HI(43)) |
2610 (xfs_bmbt_rec_base_t)(v & XFS_MASK64LO(21));
2614 * Set the startblock field in a bmap extent record.
2617 xfs_bmbt_set_startblock(
2622 ASSERT((v & XFS_MASK64HI(12)) == 0);
2623 r->l0 = (r->l0 & (xfs_bmbt_rec_base_t)XFS_MASK64HI(55)) |
2624 (xfs_bmbt_rec_base_t)(v >> 43);
2625 r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)) |
2626 (xfs_bmbt_rec_base_t)(v << 21);
2627 #else /* !XFS_BIG_BLKNOS */
2628 if (ISNULLSTARTBLOCK(v)) {
2629 r->l0 |= (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
2630 r->l1 = (xfs_bmbt_rec_base_t)XFS_MASK64HI(11) |
2631 ((xfs_bmbt_rec_base_t)v << 21) |
2632 (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2634 r->l0 &= ~(xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
2635 r->l1 = ((xfs_bmbt_rec_base_t)v << 21) |
2636 (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
2638 #endif /* XFS_BIG_BLKNOS */
2642 * Set the startoff field in a bmap extent record.
2645 xfs_bmbt_set_startoff(
2649 ASSERT((v & XFS_MASK64HI(9)) == 0);
2650 r->l0 = (r->l0 & (xfs_bmbt_rec_base_t) XFS_MASK64HI(1)) |
2651 ((xfs_bmbt_rec_base_t)v << 9) |
2652 (r->l0 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
2656 * Set the extent state field in a bmap extent record.
2663 ASSERT(v == XFS_EXT_NORM || v == XFS_EXT_UNWRITTEN);
2664 if (v == XFS_EXT_NORM)
2665 r->l0 &= XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN);
2667 r->l0 |= XFS_MASK64HI(BMBT_EXNTFLAG_BITLEN);
2671 * Convert in-memory form of btree root to on-disk form.
2675 xfs_bmbt_block_t *rblock,
2677 xfs_bmdr_block_t *dblock,
2681 xfs_bmbt_key_t *fkp;
2683 xfs_bmbt_key_t *tkp;
2686 ASSERT(be32_to_cpu(rblock->bb_magic) == XFS_BMAP_MAGIC);
2687 ASSERT(be64_to_cpu(rblock->bb_leftsib) == NULLDFSBNO);
2688 ASSERT(be64_to_cpu(rblock->bb_rightsib) == NULLDFSBNO);
2689 ASSERT(be16_to_cpu(rblock->bb_level) > 0);
2690 dblock->bb_level = rblock->bb_level;
2691 dblock->bb_numrecs = rblock->bb_numrecs;
2692 dmxr = (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen, xfs_bmdr, 0);
2693 fkp = XFS_BMAP_BROOT_KEY_ADDR(rblock, 1, rblocklen);
2694 tkp = XFS_BTREE_KEY_ADDR(dblocklen, xfs_bmdr, dblock, 1, dmxr);
2695 fpp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, rblocklen);
2696 tpp = XFS_BTREE_PTR_ADDR(dblocklen, xfs_bmdr, dblock, 1, dmxr);
2697 dmxr = be16_to_cpu(dblock->bb_numrecs);
2698 memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
2699 memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
2703 * Update the record to the passed values.
2707 xfs_btree_cur_t *cur,
2713 xfs_bmbt_block_t *block;
2716 #ifdef XFS_BMBT_TRACE
2717 static char fname[] = "xfs_bmbt_update";
2723 XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
2724 XFS_BMBT_TRACE_ARGFFFI(cur, (xfs_dfiloff_t)off, (xfs_dfsbno_t)bno,
2725 (xfs_dfilblks_t)len, (int)state);
2726 block = xfs_bmbt_get_block(cur, 0, &bp);
2728 if ((error = xfs_btree_check_lblock(cur, block, 0, bp))) {
2729 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2733 ptr = cur->bc_ptrs[0];
2734 rp = XFS_BMAP_REC_IADDR(block, ptr, cur);
2735 xfs_bmbt_disk_set_allf(rp, off, bno, len, state);
2736 xfs_bmbt_log_recs(cur, bp, ptr, ptr);
2738 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2741 INT_SET(key.br_startoff, ARCH_CONVERT, off);
2742 if ((error = xfs_bmbt_updkey(cur, &key, 1))) {
2743 XFS_BMBT_TRACE_CURSOR(cur, ERROR);
2746 XFS_BMBT_TRACE_CURSOR(cur, EXIT);
2751 * Check extent records, which have just been read, for
2752 * any bit in the extent flag field. ASSERT on debug
2753 * kernels, as this condition should not occur.
2754 * Return an error condition (1) if any flags found,
2755 * otherwise return 0.
2759 xfs_check_nostate_extents(
2766 for (; num > 0; num--, idx++) {
2767 ep = xfs_iext_get_ext(ifp, idx);
2769 (64 - BMBT_EXNTFLAG_BITLEN)) != 0) {