2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
35 #include "xfs_macros.h"
36 #include "xfs_types.h"
39 #include "xfs_trans.h"
44 #include "xfs_dmapi.h"
45 #include "xfs_mount.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_bmap_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_btree.h"
50 #include "xfs_ialloc.h"
51 #include "xfs_itable.h"
52 #include "xfs_attr_sf.h"
53 #include "xfs_dir_sf.h"
54 #include "xfs_dir2_sf.h"
55 #include "xfs_dinode.h"
56 #include "xfs_inode_item.h"
57 #include "xfs_inode.h"
58 #include "xfs_alloc.h"
61 #include "xfs_error.h"
62 #include "xfs_quota.h"
64 #if defined(XFS_BMBT_TRACE)
65 ktrace_t
*xfs_bmbt_trace_buf
;
69 * Prototypes for internal btree functions.
73 STATIC
int xfs_bmbt_killroot(xfs_btree_cur_t
*);
74 STATIC
void xfs_bmbt_log_keys(xfs_btree_cur_t
*, xfs_buf_t
*, int, int);
75 STATIC
void xfs_bmbt_log_ptrs(xfs_btree_cur_t
*, xfs_buf_t
*, int, int);
76 STATIC
int xfs_bmbt_lshift(xfs_btree_cur_t
*, int, int *);
77 STATIC
int xfs_bmbt_rshift(xfs_btree_cur_t
*, int, int *);
78 STATIC
int xfs_bmbt_split(xfs_btree_cur_t
*, int, xfs_fsblock_t
*,
79 xfs_bmbt_key_t
*, xfs_btree_cur_t
**, int *);
80 STATIC
int xfs_bmbt_updkey(xfs_btree_cur_t
*, xfs_bmbt_key_t
*, int);
83 #if defined(XFS_BMBT_TRACE)
85 static char ARGS
[] = "args";
86 static char ENTRY
[] = "entry";
87 static char ERROR
[] = "error";
89 static char EXIT
[] = "exit";
92 * Add a trace buffer entry for the arguments given to the routine,
117 ip
= cur
->bc_private
.b
.ip
;
118 whichfork
= cur
->bc_private
.b
.whichfork
;
119 ktrace_enter(xfs_bmbt_trace_buf
,
120 (void *)((__psint_t
)type
| (whichfork
<< 8) | (line
<< 16)),
121 (void *)func
, (void *)s
, (void *)ip
, (void *)cur
,
122 (void *)a0
, (void *)a1
, (void *)a2
, (void *)a3
,
123 (void *)a4
, (void *)a5
, (void *)a6
, (void *)a7
,
124 (void *)a8
, (void *)a9
, (void *)a10
);
125 ASSERT(ip
->i_btrace
);
126 ktrace_enter(ip
->i_btrace
,
127 (void *)((__psint_t
)type
| (whichfork
<< 8) | (line
<< 16)),
128 (void *)func
, (void *)s
, (void *)ip
, (void *)cur
,
129 (void *)a0
, (void *)a1
, (void *)a2
, (void *)a3
,
130 (void *)a4
, (void *)a5
, (void *)a6
, (void *)a7
,
131 (void *)a8
, (void *)a9
, (void *)a10
);
134 * Add a trace buffer entry for arguments, for a buffer & 1 integer arg.
137 xfs_bmbt_trace_argbi(
139 xfs_btree_cur_t
*cur
,
144 xfs_bmbt_trace_enter(func
, cur
, ARGS
, XFS_BMBT_KTRACE_ARGBI
, line
,
145 (__psunsigned_t
)b
, i
, 0, 0,
151 * Add a trace buffer entry for arguments, for a buffer & 2 integer args.
154 xfs_bmbt_trace_argbii(
156 xfs_btree_cur_t
*cur
,
162 xfs_bmbt_trace_enter(func
, cur
, ARGS
, XFS_BMBT_KTRACE_ARGBII
, line
,
163 (__psunsigned_t
)b
, i0
, i1
, 0,
169 * Add a trace buffer entry for arguments, for 3 block-length args
170 * and an integer arg.
173 xfs_bmbt_trace_argfffi(
175 xfs_btree_cur_t
*cur
,
182 xfs_bmbt_trace_enter(func
, cur
, ARGS
, XFS_BMBT_KTRACE_ARGFFFI
, line
,
183 o
>> 32, (int)o
, b
>> 32, (int)b
,
184 i
>> 32, (int)i
, (int)j
, 0,
189 * Add a trace buffer entry for arguments, for one integer arg.
194 xfs_btree_cur_t
*cur
,
198 xfs_bmbt_trace_enter(func
, cur
, ARGS
, XFS_BMBT_KTRACE_ARGI
, line
,
205 * Add a trace buffer entry for arguments, for int, fsblock, key.
208 xfs_bmbt_trace_argifk(
210 xfs_btree_cur_t
*cur
,
220 o
= INT_GET(k
->br_startoff
, ARCH_CONVERT
);
221 xfs_bmbt_trace_enter(func
, cur
, ARGS
, XFS_BMBT_KTRACE_ARGIFK
, line
,
222 i
, d
>> 32, (int)d
, o
>> 32,
228 * Add a trace buffer entry for arguments, for int, fsblock, rec.
231 xfs_bmbt_trace_argifr(
233 xfs_btree_cur_t
*cur
,
246 xfs_bmbt_disk_get_all(r
, &s
);
247 o
= (xfs_dfiloff_t
)s
.br_startoff
;
248 b
= (xfs_dfsbno_t
)s
.br_startblock
;
250 xfs_bmbt_trace_enter(func
, cur
, ARGS
, XFS_BMBT_KTRACE_ARGIFR
, line
,
251 i
, d
>> 32, (int)d
, o
>> 32,
252 (int)o
, b
>> 32, (int)b
, c
>> 32,
257 * Add a trace buffer entry for arguments, for int, key.
260 xfs_bmbt_trace_argik(
262 xfs_btree_cur_t
*cur
,
269 o
= INT_GET(k
->br_startoff
, ARCH_CONVERT
);
270 xfs_bmbt_trace_enter(func
, cur
, ARGS
, XFS_BMBT_KTRACE_ARGIFK
, line
,
271 i
, o
>> 32, (int)o
, 0,
277 * Add a trace buffer entry for the cursor/operation.
280 xfs_bmbt_trace_cursor(
282 xfs_btree_cur_t
*cur
,
288 xfs_bmbt_set_all(&r
, &cur
->bc_rec
.b
);
289 xfs_bmbt_trace_enter(func
, cur
, s
, XFS_BMBT_KTRACE_CUR
, line
,
290 (cur
->bc_nlevels
<< 24) | (cur
->bc_private
.b
.flags
<< 16) |
291 cur
->bc_private
.b
.allocated
,
292 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
),
293 (unsigned long)cur
->bc_bufs
[0], (unsigned long)cur
->bc_bufs
[1],
294 (unsigned long)cur
->bc_bufs
[2], (unsigned long)cur
->bc_bufs
[3],
295 (cur
->bc_ptrs
[0] << 16) | cur
->bc_ptrs
[1],
296 (cur
->bc_ptrs
[2] << 16) | cur
->bc_ptrs
[3]);
299 #define XFS_BMBT_TRACE_ARGBI(c,b,i) \
300 xfs_bmbt_trace_argbi(fname, c, b, i, __LINE__)
301 #define XFS_BMBT_TRACE_ARGBII(c,b,i,j) \
302 xfs_bmbt_trace_argbii(fname, c, b, i, j, __LINE__)
303 #define XFS_BMBT_TRACE_ARGFFFI(c,o,b,i,j) \
304 xfs_bmbt_trace_argfffi(fname, c, o, b, i, j, __LINE__)
305 #define XFS_BMBT_TRACE_ARGI(c,i) \
306 xfs_bmbt_trace_argi(fname, c, i, __LINE__)
307 #define XFS_BMBT_TRACE_ARGIFK(c,i,f,k) \
308 xfs_bmbt_trace_argifk(fname, c, i, f, k, __LINE__)
309 #define XFS_BMBT_TRACE_ARGIFR(c,i,f,r) \
310 xfs_bmbt_trace_argifr(fname, c, i, f, r, __LINE__)
311 #define XFS_BMBT_TRACE_ARGIK(c,i,k) \
312 xfs_bmbt_trace_argik(fname, c, i, k, __LINE__)
313 #define XFS_BMBT_TRACE_CURSOR(c,s) \
314 xfs_bmbt_trace_cursor(fname, c, s, __LINE__)
316 #define XFS_BMBT_TRACE_ARGBI(c,b,i)
317 #define XFS_BMBT_TRACE_ARGBII(c,b,i,j)
318 #define XFS_BMBT_TRACE_ARGFFFI(c,o,b,i,j)
319 #define XFS_BMBT_TRACE_ARGI(c,i)
320 #define XFS_BMBT_TRACE_ARGIFK(c,i,f,k)
321 #define XFS_BMBT_TRACE_ARGIFR(c,i,f,r)
322 #define XFS_BMBT_TRACE_ARGIK(c,i,k)
323 #define XFS_BMBT_TRACE_CURSOR(c,s)
324 #endif /* XFS_BMBT_TRACE */
328 * Internal functions.
332 * Delete record pointed to by cur/level.
334 STATIC
int /* error */
336 xfs_btree_cur_t
*cur
,
338 int *stat
) /* success/failure */
340 xfs_bmbt_block_t
*block
; /* bmap btree block */
341 xfs_fsblock_t bno
; /* fs-relative block number */
342 xfs_buf_t
*bp
; /* buffer for block */
343 int error
; /* error return value */
344 #ifdef XFS_BMBT_TRACE
345 static char fname
[] = "xfs_bmbt_delrec";
347 int i
; /* loop counter */
348 int j
; /* temp state */
349 xfs_bmbt_key_t key
; /* bmap btree key */
350 xfs_bmbt_key_t
*kp
=NULL
; /* pointer to bmap btree key */
351 xfs_fsblock_t lbno
; /* left sibling block number */
352 xfs_buf_t
*lbp
; /* left buffer pointer */
353 xfs_bmbt_block_t
*left
; /* left btree block */
354 xfs_bmbt_key_t
*lkp
; /* left btree key */
355 xfs_bmbt_ptr_t
*lpp
; /* left address pointer */
356 int lrecs
=0; /* left record count */
357 xfs_bmbt_rec_t
*lrp
; /* left record pointer */
358 xfs_mount_t
*mp
; /* file system mount point */
359 xfs_bmbt_ptr_t
*pp
; /* pointer to bmap block addr */
360 int ptr
; /* key/record index */
361 xfs_fsblock_t rbno
; /* right sibling block number */
362 xfs_buf_t
*rbp
; /* right buffer pointer */
363 xfs_bmbt_block_t
*right
; /* right btree block */
364 xfs_bmbt_key_t
*rkp
; /* right btree key */
365 xfs_bmbt_rec_t
*rp
; /* pointer to bmap btree rec */
366 xfs_bmbt_ptr_t
*rpp
; /* right address pointer */
367 xfs_bmbt_block_t
*rrblock
; /* right-right btree block */
368 xfs_buf_t
*rrbp
; /* right-right buffer pointer */
369 int rrecs
=0; /* right record count */
370 xfs_bmbt_rec_t
*rrp
; /* right record pointer */
371 xfs_btree_cur_t
*tcur
; /* temporary btree cursor */
372 int numrecs
; /* temporary numrec count */
373 int numlrecs
, numrrecs
;
375 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
376 XFS_BMBT_TRACE_ARGI(cur
, level
);
377 ptr
= cur
->bc_ptrs
[level
];
378 tcur
= (xfs_btree_cur_t
*)0;
380 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
384 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
385 numrecs
= INT_GET(block
->bb_numrecs
, ARCH_CONVERT
);
387 if ((error
= xfs_btree_check_lblock(cur
, block
, level
, bp
))) {
388 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
393 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
397 XFS_STATS_INC(xs_bmbt_delrec
);
399 kp
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
400 pp
= XFS_BMAP_PTR_IADDR(block
, 1, cur
);
402 for (i
= ptr
; i
< numrecs
; i
++) {
403 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(pp
[i
], ARCH_CONVERT
), level
))) {
404 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
410 memmove(&kp
[ptr
- 1], &kp
[ptr
],
411 (numrecs
- ptr
) * sizeof(*kp
));
412 memmove(&pp
[ptr
- 1], &pp
[ptr
], /* INT_: direct copy */
413 (numrecs
- ptr
) * sizeof(*pp
));
414 xfs_bmbt_log_ptrs(cur
, bp
, ptr
, numrecs
- 1);
415 xfs_bmbt_log_keys(cur
, bp
, ptr
, numrecs
- 1);
418 rp
= XFS_BMAP_REC_IADDR(block
, 1, cur
);
420 memmove(&rp
[ptr
- 1], &rp
[ptr
],
421 (numrecs
- ptr
) * sizeof(*rp
));
422 xfs_bmbt_log_recs(cur
, bp
, ptr
, numrecs
- 1);
425 INT_SET(key
.br_startoff
, ARCH_CONVERT
, xfs_bmbt_disk_get_startoff(rp
));
430 INT_SET(block
->bb_numrecs
, ARCH_CONVERT
, numrecs
);
431 xfs_bmbt_log_block(cur
, bp
, XFS_BB_NUMRECS
);
433 * We're at the root level.
434 * First, shrink the root block in-memory.
435 * Try to get rid of the next level down.
436 * If we can't then there's nothing left to do.
438 if (level
== cur
->bc_nlevels
- 1) {
439 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, -1,
440 cur
->bc_private
.b
.whichfork
);
441 if ((error
= xfs_bmbt_killroot(cur
))) {
442 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
445 if (level
> 0 && (error
= xfs_bmbt_decrement(cur
, level
, &j
))) {
446 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
449 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
453 if (ptr
== 1 && (error
= xfs_bmbt_updkey(cur
, kp
, level
+ 1))) {
454 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
457 if (numrecs
>= XFS_BMAP_BLOCK_IMINRECS(level
, cur
)) {
458 if (level
> 0 && (error
= xfs_bmbt_decrement(cur
, level
, &j
))) {
459 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
462 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
466 rbno
= INT_GET(block
->bb_rightsib
, ARCH_CONVERT
);
467 lbno
= INT_GET(block
->bb_leftsib
, ARCH_CONVERT
);
469 * One child of root, need to get a chance to copy its contents
470 * into the root and delete it. Can't go up to next level,
471 * there's nothing to delete there.
473 if (lbno
== NULLFSBLOCK
&& rbno
== NULLFSBLOCK
&&
474 level
== cur
->bc_nlevels
- 2) {
475 if ((error
= xfs_bmbt_killroot(cur
))) {
476 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
479 if (level
> 0 && (error
= xfs_bmbt_decrement(cur
, level
, &i
))) {
480 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
483 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
487 ASSERT(rbno
!= NULLFSBLOCK
|| lbno
!= NULLFSBLOCK
);
488 if ((error
= xfs_btree_dup_cursor(cur
, &tcur
))) {
489 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
493 if (rbno
!= NULLFSBLOCK
) {
494 i
= xfs_btree_lastrec(tcur
, level
);
495 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
496 if ((error
= xfs_bmbt_increment(tcur
, level
, &i
))) {
497 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
500 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
501 i
= xfs_btree_lastrec(tcur
, level
);
502 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
503 rbp
= tcur
->bc_bufs
[level
];
504 right
= XFS_BUF_TO_BMBT_BLOCK(rbp
);
506 if ((error
= xfs_btree_check_lblock(cur
, right
, level
, rbp
))) {
507 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
511 bno
= INT_GET(right
->bb_leftsib
, ARCH_CONVERT
);
512 if (INT_GET(right
->bb_numrecs
, ARCH_CONVERT
) - 1 >=
513 XFS_BMAP_BLOCK_IMINRECS(level
, cur
)) {
514 if ((error
= xfs_bmbt_lshift(tcur
, level
, &i
))) {
515 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
519 ASSERT(INT_GET(block
->bb_numrecs
, ARCH_CONVERT
) >=
520 XFS_BMAP_BLOCK_IMINRECS(level
, tcur
));
521 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
524 if ((error
= xfs_bmbt_decrement(cur
,
526 XFS_BMBT_TRACE_CURSOR(cur
,
531 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
536 rrecs
= INT_GET(right
->bb_numrecs
, ARCH_CONVERT
);
537 if (lbno
!= NULLFSBLOCK
) {
538 i
= xfs_btree_firstrec(tcur
, level
);
539 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
540 if ((error
= xfs_bmbt_decrement(tcur
, level
, &i
))) {
541 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
544 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
547 if (lbno
!= NULLFSBLOCK
) {
548 i
= xfs_btree_firstrec(tcur
, level
);
549 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
551 * decrement to last in block
553 if ((error
= xfs_bmbt_decrement(tcur
, level
, &i
))) {
554 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
557 i
= xfs_btree_firstrec(tcur
, level
);
558 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
559 lbp
= tcur
->bc_bufs
[level
];
560 left
= XFS_BUF_TO_BMBT_BLOCK(lbp
);
562 if ((error
= xfs_btree_check_lblock(cur
, left
, level
, lbp
))) {
563 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
567 bno
= INT_GET(left
->bb_rightsib
, ARCH_CONVERT
);
568 if (INT_GET(left
->bb_numrecs
, ARCH_CONVERT
) - 1 >=
569 XFS_BMAP_BLOCK_IMINRECS(level
, cur
)) {
570 if ((error
= xfs_bmbt_rshift(tcur
, level
, &i
))) {
571 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
575 ASSERT(INT_GET(block
->bb_numrecs
, ARCH_CONVERT
) >=
576 XFS_BMAP_BLOCK_IMINRECS(level
, tcur
));
577 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
581 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
586 lrecs
= INT_GET(left
->bb_numrecs
, ARCH_CONVERT
);
588 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
591 ASSERT(bno
!= NULLFSBLOCK
);
592 if (lbno
!= NULLFSBLOCK
&&
593 lrecs
+ INT_GET(block
->bb_numrecs
, ARCH_CONVERT
) <= XFS_BMAP_BLOCK_IMAXRECS(level
, cur
)) {
597 if ((error
= xfs_btree_read_bufl(mp
, cur
->bc_tp
, lbno
, 0, &lbp
,
598 XFS_BMAP_BTREE_REF
))) {
599 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
602 left
= XFS_BUF_TO_BMBT_BLOCK(lbp
);
603 if ((error
= xfs_btree_check_lblock(cur
, left
, level
, lbp
))) {
604 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
607 } else if (rbno
!= NULLFSBLOCK
&&
608 rrecs
+ INT_GET(block
->bb_numrecs
, ARCH_CONVERT
) <=
609 XFS_BMAP_BLOCK_IMAXRECS(level
, cur
)) {
613 if ((error
= xfs_btree_read_bufl(mp
, cur
->bc_tp
, rbno
, 0, &rbp
,
614 XFS_BMAP_BTREE_REF
))) {
615 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
618 right
= XFS_BUF_TO_BMBT_BLOCK(rbp
);
619 if ((error
= xfs_btree_check_lblock(cur
, right
, level
, rbp
))) {
620 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
623 lrecs
= INT_GET(left
->bb_numrecs
, ARCH_CONVERT
);
625 if (level
> 0 && (error
= xfs_bmbt_decrement(cur
, level
, &i
))) {
626 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
629 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
633 numlrecs
= INT_GET(left
->bb_numrecs
, ARCH_CONVERT
);
634 numrrecs
= INT_GET(right
->bb_numrecs
, ARCH_CONVERT
);
636 lkp
= XFS_BMAP_KEY_IADDR(left
, numlrecs
+ 1, cur
);
637 lpp
= XFS_BMAP_PTR_IADDR(left
, numlrecs
+ 1, cur
);
638 rkp
= XFS_BMAP_KEY_IADDR(right
, 1, cur
);
639 rpp
= XFS_BMAP_PTR_IADDR(right
, 1, cur
);
641 for (i
= 0; i
< numrrecs
; i
++) {
642 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(rpp
[i
], ARCH_CONVERT
), level
))) {
643 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
648 memcpy(lkp
, rkp
, numrrecs
* sizeof(*lkp
));
649 memcpy(lpp
, rpp
, numrrecs
* sizeof(*lpp
));
650 xfs_bmbt_log_keys(cur
, lbp
, numlrecs
+ 1, numlrecs
+ numrrecs
);
651 xfs_bmbt_log_ptrs(cur
, lbp
, numlrecs
+ 1, numlrecs
+ numrrecs
);
653 lrp
= XFS_BMAP_REC_IADDR(left
, numlrecs
+ 1, cur
);
654 rrp
= XFS_BMAP_REC_IADDR(right
, 1, cur
);
655 memcpy(lrp
, rrp
, numrrecs
* sizeof(*lrp
));
656 xfs_bmbt_log_recs(cur
, lbp
, numlrecs
+ 1, numlrecs
+ numrrecs
);
658 INT_MOD(left
->bb_numrecs
, ARCH_CONVERT
, numrrecs
);
659 left
->bb_rightsib
= right
->bb_rightsib
; /* INT_: direct copy */
660 xfs_bmbt_log_block(cur
, lbp
, XFS_BB_RIGHTSIB
| XFS_BB_NUMRECS
);
661 if (INT_GET(left
->bb_rightsib
, ARCH_CONVERT
) != NULLDFSBNO
) {
662 if ((error
= xfs_btree_read_bufl(mp
, cur
->bc_tp
,
663 INT_GET(left
->bb_rightsib
, ARCH_CONVERT
),
664 0, &rrbp
, XFS_BMAP_BTREE_REF
))) {
665 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
668 rrblock
= XFS_BUF_TO_BMBT_BLOCK(rrbp
);
669 if ((error
= xfs_btree_check_lblock(cur
, rrblock
, level
, rrbp
))) {
670 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
673 INT_SET(rrblock
->bb_leftsib
, ARCH_CONVERT
, lbno
);
674 xfs_bmbt_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
676 xfs_bmap_add_free(XFS_DADDR_TO_FSB(mp
, XFS_BUF_ADDR(rbp
)), 1,
677 cur
->bc_private
.b
.flist
, mp
);
678 cur
->bc_private
.b
.ip
->i_d
.di_nblocks
--;
679 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
, XFS_ILOG_CORE
);
680 XFS_TRANS_MOD_DQUOT_BYINO(mp
, cur
->bc_tp
, cur
->bc_private
.b
.ip
,
681 XFS_TRANS_DQ_BCOUNT
, -1L);
682 xfs_trans_binval(cur
->bc_tp
, rbp
);
684 cur
->bc_bufs
[level
] = lbp
;
685 cur
->bc_ptrs
[level
] += lrecs
;
686 cur
->bc_ra
[level
] = 0;
687 } else if ((error
= xfs_bmbt_increment(cur
, level
+ 1, &i
))) {
688 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
692 cur
->bc_ptrs
[level
]--;
693 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
699 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
705 * Get the data from the pointed-to record.
709 xfs_btree_cur_t
*cur
,
716 xfs_bmbt_block_t
*block
;
724 block
= xfs_bmbt_get_block(cur
, 0, &bp
);
725 ptr
= cur
->bc_ptrs
[0];
727 if ((error
= xfs_btree_check_lblock(cur
, block
, 0, bp
)))
730 if (ptr
> INT_GET(block
->bb_numrecs
, ARCH_CONVERT
) || ptr
<= 0) {
734 rp
= XFS_BMAP_REC_IADDR(block
, ptr
, cur
);
735 *off
= xfs_bmbt_disk_get_startoff(rp
);
736 *bno
= xfs_bmbt_disk_get_startblock(rp
);
737 *len
= xfs_bmbt_disk_get_blockcount(rp
);
738 *state
= xfs_bmbt_disk_get_state(rp
);
745 * Insert one record/level. Return information to the caller
746 * allowing the next level up to proceed if necessary.
748 STATIC
int /* error */
750 xfs_btree_cur_t
*cur
,
753 xfs_bmbt_rec_t
*recp
,
754 xfs_btree_cur_t
**curp
,
755 int *stat
) /* no-go/done/continue */
757 xfs_bmbt_block_t
*block
; /* bmap btree block */
758 xfs_buf_t
*bp
; /* buffer for block */
759 int error
; /* error return value */
760 #ifdef XFS_BMBT_TRACE
761 static char fname
[] = "xfs_bmbt_insrec";
763 int i
; /* loop index */
764 xfs_bmbt_key_t key
; /* bmap btree key */
765 xfs_bmbt_key_t
*kp
=NULL
; /* pointer to bmap btree key */
766 int logflags
; /* inode logging flags */
767 xfs_fsblock_t nbno
; /* new block number */
768 struct xfs_btree_cur
*ncur
; /* new btree cursor */
769 xfs_bmbt_key_t nkey
; /* new btree key value */
770 xfs_bmbt_rec_t nrec
; /* new record count */
771 int optr
; /* old key/record index */
772 xfs_bmbt_ptr_t
*pp
; /* pointer to bmap block addr */
773 int ptr
; /* key/record index */
774 xfs_bmbt_rec_t
*rp
=NULL
; /* pointer to bmap btree rec */
777 ASSERT(level
< cur
->bc_nlevels
);
778 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
779 XFS_BMBT_TRACE_ARGIFR(cur
, level
, *bnop
, recp
);
780 ncur
= (xfs_btree_cur_t
*)0;
781 INT_SET(key
.br_startoff
, ARCH_CONVERT
,
782 xfs_bmbt_disk_get_startoff(recp
));
783 optr
= ptr
= cur
->bc_ptrs
[level
];
785 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
789 XFS_STATS_INC(xs_bmbt_insrec
);
790 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
791 numrecs
= INT_GET(block
->bb_numrecs
, ARCH_CONVERT
);
793 if ((error
= xfs_btree_check_lblock(cur
, block
, level
, bp
))) {
794 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
797 if (ptr
<= numrecs
) {
799 rp
= XFS_BMAP_REC_IADDR(block
, ptr
, cur
);
800 xfs_btree_check_rec(XFS_BTNUM_BMAP
, recp
, rp
);
802 kp
= XFS_BMAP_KEY_IADDR(block
, ptr
, cur
);
803 xfs_btree_check_key(XFS_BTNUM_BMAP
, &key
, kp
);
808 if (numrecs
== XFS_BMAP_BLOCK_IMAXRECS(level
, cur
)) {
809 if (numrecs
< XFS_BMAP_BLOCK_DMAXRECS(level
, cur
)) {
811 * A root block, that can be made bigger.
813 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, 1,
814 cur
->bc_private
.b
.whichfork
);
815 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
816 } else if (level
== cur
->bc_nlevels
- 1) {
817 if ((error
= xfs_bmbt_newroot(cur
, &logflags
, stat
)) ||
819 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
822 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
824 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
826 if ((error
= xfs_bmbt_rshift(cur
, level
, &i
))) {
827 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
833 if ((error
= xfs_bmbt_lshift(cur
, level
, &i
))) {
834 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
838 optr
= ptr
= cur
->bc_ptrs
[level
];
840 if ((error
= xfs_bmbt_split(cur
, level
,
843 XFS_BMBT_TRACE_CURSOR(cur
,
848 block
= xfs_bmbt_get_block(
852 xfs_btree_check_lblock(cur
,
853 block
, level
, bp
))) {
854 XFS_BMBT_TRACE_CURSOR(
859 ptr
= cur
->bc_ptrs
[level
];
860 xfs_bmbt_disk_set_allf(&nrec
,
861 nkey
.br_startoff
, 0, 0,
864 XFS_BMBT_TRACE_CURSOR(cur
,
873 numrecs
= INT_GET(block
->bb_numrecs
, ARCH_CONVERT
);
875 kp
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
876 pp
= XFS_BMAP_PTR_IADDR(block
, 1, cur
);
878 for (i
= numrecs
; i
>= ptr
; i
--) {
879 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(pp
[i
- 1], ARCH_CONVERT
),
881 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
886 memmove(&kp
[ptr
], &kp
[ptr
- 1],
887 (numrecs
- ptr
+ 1) * sizeof(*kp
));
888 memmove(&pp
[ptr
], &pp
[ptr
- 1], /* INT_: direct copy */
889 (numrecs
- ptr
+ 1) * sizeof(*pp
));
891 if ((error
= xfs_btree_check_lptr(cur
, (xfs_bmbt_ptr_t
)*bnop
,
893 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
898 INT_SET(pp
[ptr
- 1], ARCH_CONVERT
, *bnop
);
900 INT_SET(block
->bb_numrecs
, ARCH_CONVERT
, numrecs
);
901 xfs_bmbt_log_keys(cur
, bp
, ptr
, numrecs
);
902 xfs_bmbt_log_ptrs(cur
, bp
, ptr
, numrecs
);
904 rp
= XFS_BMAP_REC_IADDR(block
, 1, cur
);
905 memmove(&rp
[ptr
], &rp
[ptr
- 1],
906 (numrecs
- ptr
+ 1) * sizeof(*rp
));
909 INT_SET(block
->bb_numrecs
, ARCH_CONVERT
, numrecs
);
910 xfs_bmbt_log_recs(cur
, bp
, ptr
, numrecs
);
912 xfs_bmbt_log_block(cur
, bp
, XFS_BB_NUMRECS
);
916 xfs_btree_check_rec(XFS_BTNUM_BMAP
, rp
+ ptr
- 1,
919 xfs_btree_check_key(XFS_BTNUM_BMAP
, kp
+ ptr
- 1,
923 if (optr
== 1 && (error
= xfs_bmbt_updkey(cur
, &key
, level
+ 1))) {
924 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
928 if (nbno
!= NULLFSBLOCK
) {
932 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
939 xfs_btree_cur_t
*cur
)
941 xfs_bmbt_block_t
*block
;
942 xfs_bmbt_block_t
*cblock
;
949 #ifdef XFS_BMBT_TRACE
950 static char fname
[] = "xfs_bmbt_killroot";
959 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
960 level
= cur
->bc_nlevels
- 1;
963 * Don't deal with the root block needs to be a leaf case.
964 * We're just going to turn the thing back into extents anyway.
967 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
970 block
= xfs_bmbt_get_block(cur
, level
, &cbp
);
972 * Give up if the root has multiple children.
974 if (INT_GET(block
->bb_numrecs
, ARCH_CONVERT
) != 1) {
975 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
979 * Only do this if the next level will fit.
980 * Then the data must be copied up to the inode,
981 * instead of freeing the root you free the next level.
983 cbp
= cur
->bc_bufs
[level
- 1];
984 cblock
= XFS_BUF_TO_BMBT_BLOCK(cbp
);
985 if (INT_GET(cblock
->bb_numrecs
, ARCH_CONVERT
) > XFS_BMAP_BLOCK_DMAXRECS(level
, cur
)) {
986 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
989 ASSERT(INT_GET(cblock
->bb_leftsib
, ARCH_CONVERT
) == NULLDFSBNO
);
990 ASSERT(INT_GET(cblock
->bb_rightsib
, ARCH_CONVERT
) == NULLDFSBNO
);
991 ip
= cur
->bc_private
.b
.ip
;
992 ifp
= XFS_IFORK_PTR(ip
, cur
->bc_private
.b
.whichfork
);
993 ASSERT(XFS_BMAP_BLOCK_IMAXRECS(level
, cur
) ==
994 XFS_BMAP_BROOT_MAXRECS(ifp
->if_broot_bytes
));
995 i
= (int)(INT_GET(cblock
->bb_numrecs
, ARCH_CONVERT
) - XFS_BMAP_BLOCK_IMAXRECS(level
, cur
));
997 xfs_iroot_realloc(ip
, i
, cur
->bc_private
.b
.whichfork
);
998 block
= ifp
->if_broot
;
1000 INT_MOD(block
->bb_numrecs
, ARCH_CONVERT
, i
);
1001 ASSERT(INT_GET(block
->bb_numrecs
, ARCH_CONVERT
) == INT_GET(cblock
->bb_numrecs
, ARCH_CONVERT
));
1002 kp
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
1003 ckp
= XFS_BMAP_KEY_IADDR(cblock
, 1, cur
);
1004 memcpy(kp
, ckp
, INT_GET(block
->bb_numrecs
, ARCH_CONVERT
) * sizeof(*kp
));
1005 pp
= XFS_BMAP_PTR_IADDR(block
, 1, cur
);
1006 cpp
= XFS_BMAP_PTR_IADDR(cblock
, 1, cur
);
1008 for (i
= 0; i
< INT_GET(cblock
->bb_numrecs
, ARCH_CONVERT
); i
++) {
1009 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(cpp
[i
], ARCH_CONVERT
), level
- 1))) {
1010 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1015 memcpy(pp
, cpp
, INT_GET(block
->bb_numrecs
, ARCH_CONVERT
) * sizeof(*pp
));
1016 xfs_bmap_add_free(XFS_DADDR_TO_FSB(cur
->bc_mp
, XFS_BUF_ADDR(cbp
)), 1,
1017 cur
->bc_private
.b
.flist
, cur
->bc_mp
);
1018 ip
->i_d
.di_nblocks
--;
1019 XFS_TRANS_MOD_DQUOT_BYINO(cur
->bc_mp
, cur
->bc_tp
, ip
,
1020 XFS_TRANS_DQ_BCOUNT
, -1L);
1021 xfs_trans_binval(cur
->bc_tp
, cbp
);
1022 cur
->bc_bufs
[level
- 1] = NULL
;
1023 INT_MOD(block
->bb_level
, ARCH_CONVERT
, -1);
1024 xfs_trans_log_inode(cur
->bc_tp
, ip
,
1025 XFS_ILOG_CORE
| XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
));
1027 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1032 * Log key values from the btree block.
1036 xfs_btree_cur_t
*cur
,
1041 #ifdef XFS_BMBT_TRACE
1042 static char fname
[] = "xfs_bmbt_log_keys";
1046 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1047 XFS_BMBT_TRACE_ARGBII(cur
, bp
, kfirst
, klast
);
1050 xfs_bmbt_block_t
*block
;
1055 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
1056 kp
= XFS_BMAP_KEY_DADDR(block
, 1, cur
);
1057 first
= (int)((xfs_caddr_t
)&kp
[kfirst
- 1] - (xfs_caddr_t
)block
);
1058 last
= (int)(((xfs_caddr_t
)&kp
[klast
] - 1) - (xfs_caddr_t
)block
);
1059 xfs_trans_log_buf(tp
, bp
, first
, last
);
1063 ip
= cur
->bc_private
.b
.ip
;
1064 xfs_trans_log_inode(tp
, ip
,
1065 XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
));
1067 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1071 * Log pointer values from the btree block.
1075 xfs_btree_cur_t
*cur
,
1080 #ifdef XFS_BMBT_TRACE
1081 static char fname
[] = "xfs_bmbt_log_ptrs";
1085 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1086 XFS_BMBT_TRACE_ARGBII(cur
, bp
, pfirst
, plast
);
1089 xfs_bmbt_block_t
*block
;
1094 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
1095 pp
= XFS_BMAP_PTR_DADDR(block
, 1, cur
);
1096 first
= (int)((xfs_caddr_t
)&pp
[pfirst
- 1] - (xfs_caddr_t
)block
);
1097 last
= (int)(((xfs_caddr_t
)&pp
[plast
] - 1) - (xfs_caddr_t
)block
);
1098 xfs_trans_log_buf(tp
, bp
, first
, last
);
1102 ip
= cur
->bc_private
.b
.ip
;
1103 xfs_trans_log_inode(tp
, ip
,
1104 XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
));
1106 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1110 * Lookup the record. The cursor is made to point to it, based on dir.
1112 STATIC
int /* error */
1114 xfs_btree_cur_t
*cur
,
1116 int *stat
) /* success/failure */
1118 xfs_bmbt_block_t
*block
=NULL
;
1122 int error
; /* error return value */
1123 #ifdef XFS_BMBT_TRACE
1124 static char fname
[] = "xfs_bmbt_lookup";
1126 xfs_fsblock_t fsbno
=0;
1130 xfs_bmbt_key_t
*kkbase
=NULL
;
1131 xfs_bmbt_key_t
*kkp
;
1132 xfs_bmbt_rec_t
*krbase
=NULL
;
1133 xfs_bmbt_rec_t
*krp
;
1138 xfs_bmbt_irec_t
*rp
;
1139 xfs_fileoff_t startoff
;
1142 XFS_STATS_INC(xs_bmbt_lookup
);
1143 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1144 XFS_BMBT_TRACE_ARGI(cur
, (int)dir
);
1147 rp
= &cur
->bc_rec
.b
;
1148 for (level
= cur
->bc_nlevels
- 1, diff
= 1; level
>= 0; level
--) {
1149 if (level
< cur
->bc_nlevels
- 1) {
1150 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
1151 bp
= cur
->bc_bufs
[level
];
1152 if (bp
&& XFS_BUF_ADDR(bp
) != d
)
1153 bp
= (xfs_buf_t
*)0;
1155 if ((error
= xfs_btree_read_bufl(mp
, tp
, fsbno
,
1156 0, &bp
, XFS_BMAP_BTREE_REF
))) {
1157 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1160 xfs_btree_setbuf(cur
, level
, bp
);
1161 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
1162 if ((error
= xfs_btree_check_lblock(cur
, block
,
1164 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1168 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
1170 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
1175 kkbase
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
1177 krbase
= XFS_BMAP_REC_IADDR(block
, 1, cur
);
1179 if (!(high
= INT_GET(block
->bb_numrecs
, ARCH_CONVERT
))) {
1181 cur
->bc_ptrs
[0] = dir
!= XFS_LOOKUP_LE
;
1182 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1186 while (low
<= high
) {
1187 XFS_STATS_INC(xs_bmbt_compare
);
1188 keyno
= (low
+ high
) >> 1;
1190 kkp
= kkbase
+ keyno
- 1;
1191 startoff
= INT_GET(kkp
->br_startoff
, ARCH_CONVERT
);
1193 krp
= krbase
+ keyno
- 1;
1194 startoff
= xfs_bmbt_disk_get_startoff(krp
);
1196 diff
= (xfs_sfiloff_t
)
1197 (startoff
- rp
->br_startoff
);
1207 if (diff
> 0 && --keyno
< 1)
1209 pp
= XFS_BMAP_PTR_IADDR(block
, keyno
, cur
);
1211 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(*pp
, ARCH_CONVERT
), level
))) {
1212 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1216 fsbno
= INT_GET(*pp
, ARCH_CONVERT
);
1217 cur
->bc_ptrs
[level
] = keyno
;
1220 if (dir
!= XFS_LOOKUP_LE
&& diff
< 0) {
1223 * If ge search and we went off the end of the block, but it's
1224 * not the last block, we're in the wrong block.
1226 if (dir
== XFS_LOOKUP_GE
&& keyno
> INT_GET(block
->bb_numrecs
, ARCH_CONVERT
) &&
1227 INT_GET(block
->bb_rightsib
, ARCH_CONVERT
) != NULLDFSBNO
) {
1228 cur
->bc_ptrs
[0] = keyno
;
1229 if ((error
= xfs_bmbt_increment(cur
, 0, &i
))) {
1230 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1233 XFS_WANT_CORRUPTED_RETURN(i
== 1);
1234 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1239 else if (dir
== XFS_LOOKUP_LE
&& diff
> 0)
1241 cur
->bc_ptrs
[0] = keyno
;
1242 if (keyno
== 0 || keyno
> INT_GET(block
->bb_numrecs
, ARCH_CONVERT
)) {
1243 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1246 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1247 *stat
= ((dir
!= XFS_LOOKUP_EQ
) || (diff
== 0));
1253 * Move 1 record left from cur/level if possible.
1254 * Update cur to reflect the new path.
1256 STATIC
int /* error */
1258 xfs_btree_cur_t
*cur
,
1260 int *stat
) /* success/failure */
1262 int error
; /* error return value */
1263 #ifdef XFS_BMBT_TRACE
1264 static char fname
[] = "xfs_bmbt_lshift";
1267 int i
; /* loop counter */
1269 xfs_bmbt_key_t key
; /* bmap btree key */
1270 xfs_buf_t
*lbp
; /* left buffer pointer */
1271 xfs_bmbt_block_t
*left
; /* left btree block */
1272 xfs_bmbt_key_t
*lkp
=NULL
; /* left btree key */
1273 xfs_bmbt_ptr_t
*lpp
; /* left address pointer */
1274 int lrecs
; /* left record count */
1275 xfs_bmbt_rec_t
*lrp
=NULL
; /* left record pointer */
1276 xfs_mount_t
*mp
; /* file system mount point */
1277 xfs_buf_t
*rbp
; /* right buffer pointer */
1278 xfs_bmbt_block_t
*right
; /* right btree block */
1279 xfs_bmbt_key_t
*rkp
=NULL
; /* right btree key */
1280 xfs_bmbt_ptr_t
*rpp
=NULL
; /* right address pointer */
1281 xfs_bmbt_rec_t
*rrp
=NULL
; /* right record pointer */
1282 int rrecs
; /* right record count */
1284 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1285 XFS_BMBT_TRACE_ARGI(cur
, level
);
1286 if (level
== cur
->bc_nlevels
- 1) {
1287 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1291 rbp
= cur
->bc_bufs
[level
];
1292 right
= XFS_BUF_TO_BMBT_BLOCK(rbp
);
1294 if ((error
= xfs_btree_check_lblock(cur
, right
, level
, rbp
))) {
1295 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1299 if (INT_GET(right
->bb_leftsib
, ARCH_CONVERT
) == NULLDFSBNO
) {
1300 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1304 if (cur
->bc_ptrs
[level
] <= 1) {
1305 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1310 if ((error
= xfs_btree_read_bufl(mp
, cur
->bc_tp
, INT_GET(right
->bb_leftsib
, ARCH_CONVERT
), 0,
1311 &lbp
, XFS_BMAP_BTREE_REF
))) {
1312 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1315 left
= XFS_BUF_TO_BMBT_BLOCK(lbp
);
1316 if ((error
= xfs_btree_check_lblock(cur
, left
, level
, lbp
))) {
1317 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1320 if (INT_GET(left
->bb_numrecs
, ARCH_CONVERT
) == XFS_BMAP_BLOCK_IMAXRECS(level
, cur
)) {
1321 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1325 lrecs
= INT_GET(left
->bb_numrecs
, ARCH_CONVERT
) + 1;
1327 lkp
= XFS_BMAP_KEY_IADDR(left
, lrecs
, cur
);
1328 rkp
= XFS_BMAP_KEY_IADDR(right
, 1, cur
);
1330 xfs_bmbt_log_keys(cur
, lbp
, lrecs
, lrecs
);
1331 lpp
= XFS_BMAP_PTR_IADDR(left
, lrecs
, cur
);
1332 rpp
= XFS_BMAP_PTR_IADDR(right
, 1, cur
);
1334 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(*rpp
, ARCH_CONVERT
), level
))) {
1335 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1339 *lpp
= *rpp
; /* INT_: direct copy */
1340 xfs_bmbt_log_ptrs(cur
, lbp
, lrecs
, lrecs
);
1342 lrp
= XFS_BMAP_REC_IADDR(left
, lrecs
, cur
);
1343 rrp
= XFS_BMAP_REC_IADDR(right
, 1, cur
);
1345 xfs_bmbt_log_recs(cur
, lbp
, lrecs
, lrecs
);
1347 INT_SET(left
->bb_numrecs
, ARCH_CONVERT
, lrecs
);
1348 xfs_bmbt_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
1351 xfs_btree_check_key(XFS_BTNUM_BMAP
, lkp
- 1, lkp
);
1353 xfs_btree_check_rec(XFS_BTNUM_BMAP
, lrp
- 1, lrp
);
1355 rrecs
= INT_GET(right
->bb_numrecs
, ARCH_CONVERT
) - 1;
1356 INT_SET(right
->bb_numrecs
, ARCH_CONVERT
, rrecs
);
1357 xfs_bmbt_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
1360 for (i
= 0; i
< rrecs
; i
++) {
1361 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(rpp
[i
+ 1], ARCH_CONVERT
),
1363 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1368 memmove(rkp
, rkp
+ 1, rrecs
* sizeof(*rkp
));
1369 memmove(rpp
, rpp
+ 1, rrecs
* sizeof(*rpp
));
1370 xfs_bmbt_log_keys(cur
, rbp
, 1, rrecs
);
1371 xfs_bmbt_log_ptrs(cur
, rbp
, 1, rrecs
);
1373 memmove(rrp
, rrp
+ 1, rrecs
* sizeof(*rrp
));
1374 xfs_bmbt_log_recs(cur
, rbp
, 1, rrecs
);
1375 INT_SET(key
.br_startoff
, ARCH_CONVERT
,
1376 xfs_bmbt_disk_get_startoff(rrp
));
1379 if ((error
= xfs_bmbt_updkey(cur
, rkp
, level
+ 1))) {
1380 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1383 cur
->bc_ptrs
[level
]--;
1384 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1390 * Move 1 record right from cur/level if possible.
1391 * Update cur to reflect the new path.
1393 STATIC
int /* error */
1395 xfs_btree_cur_t
*cur
,
1397 int *stat
) /* success/failure */
1399 int error
; /* error return value */
1400 #ifdef XFS_BMBT_TRACE
1401 static char fname
[] = "xfs_bmbt_rshift";
1403 int i
; /* loop counter */
1404 xfs_bmbt_key_t key
; /* bmap btree key */
1405 xfs_buf_t
*lbp
; /* left buffer pointer */
1406 xfs_bmbt_block_t
*left
; /* left btree block */
1407 xfs_bmbt_key_t
*lkp
; /* left btree key */
1408 xfs_bmbt_ptr_t
*lpp
; /* left address pointer */
1409 xfs_bmbt_rec_t
*lrp
; /* left record pointer */
1410 xfs_mount_t
*mp
; /* file system mount point */
1411 xfs_buf_t
*rbp
; /* right buffer pointer */
1412 xfs_bmbt_block_t
*right
; /* right btree block */
1413 xfs_bmbt_key_t
*rkp
; /* right btree key */
1414 xfs_bmbt_ptr_t
*rpp
; /* right address pointer */
1415 xfs_bmbt_rec_t
*rrp
=NULL
; /* right record pointer */
1416 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
1418 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1419 XFS_BMBT_TRACE_ARGI(cur
, level
);
1420 if (level
== cur
->bc_nlevels
- 1) {
1421 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1425 lbp
= cur
->bc_bufs
[level
];
1426 left
= XFS_BUF_TO_BMBT_BLOCK(lbp
);
1428 if ((error
= xfs_btree_check_lblock(cur
, left
, level
, lbp
))) {
1429 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1433 if (INT_GET(left
->bb_rightsib
, ARCH_CONVERT
) == NULLDFSBNO
) {
1434 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1438 if (cur
->bc_ptrs
[level
] >= INT_GET(left
->bb_numrecs
, ARCH_CONVERT
)) {
1439 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1444 if ((error
= xfs_btree_read_bufl(mp
, cur
->bc_tp
, INT_GET(left
->bb_rightsib
, ARCH_CONVERT
), 0,
1445 &rbp
, XFS_BMAP_BTREE_REF
))) {
1446 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1449 right
= XFS_BUF_TO_BMBT_BLOCK(rbp
);
1450 if ((error
= xfs_btree_check_lblock(cur
, right
, level
, rbp
))) {
1451 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1454 if (INT_GET(right
->bb_numrecs
, ARCH_CONVERT
) == XFS_BMAP_BLOCK_IMAXRECS(level
, cur
)) {
1455 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1460 lkp
= XFS_BMAP_KEY_IADDR(left
, INT_GET(left
->bb_numrecs
, ARCH_CONVERT
), cur
);
1461 lpp
= XFS_BMAP_PTR_IADDR(left
, INT_GET(left
->bb_numrecs
, ARCH_CONVERT
), cur
);
1462 rkp
= XFS_BMAP_KEY_IADDR(right
, 1, cur
);
1463 rpp
= XFS_BMAP_PTR_IADDR(right
, 1, cur
);
1465 for (i
= INT_GET(right
->bb_numrecs
, ARCH_CONVERT
) - 1; i
>= 0; i
--) {
1466 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(rpp
[i
], ARCH_CONVERT
), level
))) {
1467 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1472 memmove(rkp
+ 1, rkp
, INT_GET(right
->bb_numrecs
, ARCH_CONVERT
) * sizeof(*rkp
));
1473 memmove(rpp
+ 1, rpp
, INT_GET(right
->bb_numrecs
, ARCH_CONVERT
) * sizeof(*rpp
));
1475 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(*lpp
, ARCH_CONVERT
), level
))) {
1476 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1481 *rpp
= *lpp
; /* INT_: direct copy */
1482 xfs_bmbt_log_keys(cur
, rbp
, 1, INT_GET(right
->bb_numrecs
, ARCH_CONVERT
) + 1);
1483 xfs_bmbt_log_ptrs(cur
, rbp
, 1, INT_GET(right
->bb_numrecs
, ARCH_CONVERT
) + 1);
1485 lrp
= XFS_BMAP_REC_IADDR(left
, INT_GET(left
->bb_numrecs
, ARCH_CONVERT
), cur
);
1486 rrp
= XFS_BMAP_REC_IADDR(right
, 1, cur
);
1487 memmove(rrp
+ 1, rrp
, INT_GET(right
->bb_numrecs
, ARCH_CONVERT
) * sizeof(*rrp
));
1489 xfs_bmbt_log_recs(cur
, rbp
, 1, INT_GET(right
->bb_numrecs
, ARCH_CONVERT
) + 1);
1490 INT_SET(key
.br_startoff
, ARCH_CONVERT
,
1491 xfs_bmbt_disk_get_startoff(rrp
));
1494 INT_MOD(left
->bb_numrecs
, ARCH_CONVERT
, -1);
1495 xfs_bmbt_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
1496 INT_MOD(right
->bb_numrecs
, ARCH_CONVERT
, +1);
1499 xfs_btree_check_key(XFS_BTNUM_BMAP
, rkp
, rkp
+ 1);
1501 xfs_btree_check_rec(XFS_BTNUM_BMAP
, rrp
, rrp
+ 1);
1503 xfs_bmbt_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
1504 if ((error
= xfs_btree_dup_cursor(cur
, &tcur
))) {
1505 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1508 i
= xfs_btree_lastrec(tcur
, level
);
1509 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1510 if ((error
= xfs_bmbt_increment(tcur
, level
, &i
))) {
1511 XFS_BMBT_TRACE_CURSOR(tcur
, ERROR
);
1514 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1515 if ((error
= xfs_bmbt_updkey(tcur
, rkp
, level
+ 1))) {
1516 XFS_BMBT_TRACE_CURSOR(tcur
, ERROR
);
1519 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
1520 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1524 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1526 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
1531 * Determine the extent state.
1540 ASSERT(blks
!= 0); /* saved for DMIG */
1541 return XFS_EXT_UNWRITTEN
;
1543 return XFS_EXT_NORM
;
1548 * Split cur/level block in half.
1549 * Return new block number and its first record (to be inserted into parent).
1551 STATIC
int /* error */
1553 xfs_btree_cur_t
*cur
,
1555 xfs_fsblock_t
*bnop
,
1556 xfs_bmbt_key_t
*keyp
,
1557 xfs_btree_cur_t
**curp
,
1558 int *stat
) /* success/failure */
1560 xfs_alloc_arg_t args
; /* block allocation args */
1561 int error
; /* error return value */
1562 #ifdef XFS_BMBT_TRACE
1563 static char fname
[] = "xfs_bmbt_split";
1565 int i
; /* loop counter */
1566 xfs_fsblock_t lbno
; /* left sibling block number */
1567 xfs_buf_t
*lbp
; /* left buffer pointer */
1568 xfs_bmbt_block_t
*left
; /* left btree block */
1569 xfs_bmbt_key_t
*lkp
; /* left btree key */
1570 xfs_bmbt_ptr_t
*lpp
; /* left address pointer */
1571 xfs_bmbt_rec_t
*lrp
; /* left record pointer */
1572 xfs_buf_t
*rbp
; /* right buffer pointer */
1573 xfs_bmbt_block_t
*right
; /* right btree block */
1574 xfs_bmbt_key_t
*rkp
; /* right btree key */
1575 xfs_bmbt_ptr_t
*rpp
; /* right address pointer */
1576 xfs_bmbt_block_t
*rrblock
; /* right-right btree block */
1577 xfs_buf_t
*rrbp
; /* right-right buffer pointer */
1578 xfs_bmbt_rec_t
*rrp
; /* right record pointer */
1580 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1581 XFS_BMBT_TRACE_ARGIFK(cur
, level
, *bnop
, keyp
);
1582 args
.tp
= cur
->bc_tp
;
1583 args
.mp
= cur
->bc_mp
;
1584 lbp
= cur
->bc_bufs
[level
];
1585 lbno
= XFS_DADDR_TO_FSB(args
.mp
, XFS_BUF_ADDR(lbp
));
1586 left
= XFS_BUF_TO_BMBT_BLOCK(lbp
);
1587 args
.fsbno
= cur
->bc_private
.b
.firstblock
;
1588 if (args
.fsbno
== NULLFSBLOCK
) {
1590 args
.type
= XFS_ALLOCTYPE_START_BNO
;
1591 } else if (cur
->bc_private
.b
.flist
->xbf_low
)
1592 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
1594 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
1595 args
.mod
= args
.minleft
= args
.alignment
= args
.total
= args
.isfl
=
1596 args
.userdata
= args
.minalignslop
= 0;
1597 args
.minlen
= args
.maxlen
= args
.prod
= 1;
1598 args
.wasdel
= cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
;
1599 if (!args
.wasdel
&& xfs_trans_get_block_res(args
.tp
) == 0) {
1600 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1601 return XFS_ERROR(ENOSPC
);
1603 if ((error
= xfs_alloc_vextent(&args
))) {
1604 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1607 if (args
.fsbno
== NULLFSBLOCK
) {
1608 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1612 ASSERT(args
.len
== 1);
1613 cur
->bc_private
.b
.firstblock
= args
.fsbno
;
1614 cur
->bc_private
.b
.allocated
++;
1615 cur
->bc_private
.b
.ip
->i_d
.di_nblocks
++;
1616 xfs_trans_log_inode(args
.tp
, cur
->bc_private
.b
.ip
, XFS_ILOG_CORE
);
1617 XFS_TRANS_MOD_DQUOT_BYINO(args
.mp
, args
.tp
, cur
->bc_private
.b
.ip
,
1618 XFS_TRANS_DQ_BCOUNT
, 1L);
1619 rbp
= xfs_btree_get_bufl(args
.mp
, args
.tp
, args
.fsbno
, 0);
1620 right
= XFS_BUF_TO_BMBT_BLOCK(rbp
);
1622 if ((error
= xfs_btree_check_lblock(cur
, left
, level
, rbp
))) {
1623 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1627 INT_SET(right
->bb_magic
, ARCH_CONVERT
, XFS_BMAP_MAGIC
);
1628 right
->bb_level
= left
->bb_level
; /* INT_: direct copy */
1629 INT_SET(right
->bb_numrecs
, ARCH_CONVERT
, (__uint16_t
)(INT_GET(left
->bb_numrecs
, ARCH_CONVERT
) / 2));
1630 if ((INT_GET(left
->bb_numrecs
, ARCH_CONVERT
) & 1) &&
1631 cur
->bc_ptrs
[level
] <= INT_GET(right
->bb_numrecs
, ARCH_CONVERT
) + 1)
1632 INT_MOD(right
->bb_numrecs
, ARCH_CONVERT
, +1);
1633 i
= INT_GET(left
->bb_numrecs
, ARCH_CONVERT
) - INT_GET(right
->bb_numrecs
, ARCH_CONVERT
) + 1;
1635 lkp
= XFS_BMAP_KEY_IADDR(left
, i
, cur
);
1636 lpp
= XFS_BMAP_PTR_IADDR(left
, i
, cur
);
1637 rkp
= XFS_BMAP_KEY_IADDR(right
, 1, cur
);
1638 rpp
= XFS_BMAP_PTR_IADDR(right
, 1, cur
);
1640 for (i
= 0; i
< INT_GET(right
->bb_numrecs
, ARCH_CONVERT
); i
++) {
1641 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(lpp
[i
], ARCH_CONVERT
), level
))) {
1642 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1647 memcpy(rkp
, lkp
, INT_GET(right
->bb_numrecs
, ARCH_CONVERT
) * sizeof(*rkp
));
1648 memcpy(rpp
, lpp
, INT_GET(right
->bb_numrecs
, ARCH_CONVERT
) * sizeof(*rpp
));
1649 xfs_bmbt_log_keys(cur
, rbp
, 1, INT_GET(right
->bb_numrecs
, ARCH_CONVERT
));
1650 xfs_bmbt_log_ptrs(cur
, rbp
, 1, INT_GET(right
->bb_numrecs
, ARCH_CONVERT
));
1651 keyp
->br_startoff
= INT_GET(rkp
->br_startoff
, ARCH_CONVERT
);
1653 lrp
= XFS_BMAP_REC_IADDR(left
, i
, cur
);
1654 rrp
= XFS_BMAP_REC_IADDR(right
, 1, cur
);
1655 memcpy(rrp
, lrp
, INT_GET(right
->bb_numrecs
, ARCH_CONVERT
) * sizeof(*rrp
));
1656 xfs_bmbt_log_recs(cur
, rbp
, 1, INT_GET(right
->bb_numrecs
, ARCH_CONVERT
));
1657 keyp
->br_startoff
= xfs_bmbt_disk_get_startoff(rrp
);
1659 INT_MOD(left
->bb_numrecs
, ARCH_CONVERT
, -(INT_GET(right
->bb_numrecs
, ARCH_CONVERT
)));
1660 right
->bb_rightsib
= left
->bb_rightsib
; /* INT_: direct copy */
1661 INT_SET(left
->bb_rightsib
, ARCH_CONVERT
, args
.fsbno
);
1662 INT_SET(right
->bb_leftsib
, ARCH_CONVERT
, lbno
);
1663 xfs_bmbt_log_block(cur
, rbp
, XFS_BB_ALL_BITS
);
1664 xfs_bmbt_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
1665 if (INT_GET(right
->bb_rightsib
, ARCH_CONVERT
) != NULLDFSBNO
) {
1666 if ((error
= xfs_btree_read_bufl(args
.mp
, args
.tp
,
1667 INT_GET(right
->bb_rightsib
, ARCH_CONVERT
), 0, &rrbp
,
1668 XFS_BMAP_BTREE_REF
))) {
1669 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1672 rrblock
= XFS_BUF_TO_BMBT_BLOCK(rrbp
);
1673 if ((error
= xfs_btree_check_lblock(cur
, rrblock
, level
, rrbp
))) {
1674 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1677 INT_SET(rrblock
->bb_leftsib
, ARCH_CONVERT
, args
.fsbno
);
1678 xfs_bmbt_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
1680 if (cur
->bc_ptrs
[level
] > INT_GET(left
->bb_numrecs
, ARCH_CONVERT
) + 1) {
1681 xfs_btree_setbuf(cur
, level
, rbp
);
1682 cur
->bc_ptrs
[level
] -= INT_GET(left
->bb_numrecs
, ARCH_CONVERT
);
1684 if (level
+ 1 < cur
->bc_nlevels
) {
1685 if ((error
= xfs_btree_dup_cursor(cur
, curp
))) {
1686 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1689 (*curp
)->bc_ptrs
[level
+ 1]++;
1692 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1699 * Update keys for the record.
1703 xfs_btree_cur_t
*cur
,
1704 xfs_bmbt_key_t
*keyp
, /* on-disk format */
1707 xfs_bmbt_block_t
*block
;
1712 #ifdef XFS_BMBT_TRACE
1713 static char fname
[] = "xfs_bmbt_updkey";
1719 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1720 XFS_BMBT_TRACE_ARGIK(cur
, level
, keyp
);
1721 for (ptr
= 1; ptr
== 1 && level
< cur
->bc_nlevels
; level
++) {
1722 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
1724 if ((error
= xfs_btree_check_lblock(cur
, block
, level
, bp
))) {
1725 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1729 ptr
= cur
->bc_ptrs
[level
];
1730 kp
= XFS_BMAP_KEY_IADDR(block
, ptr
, cur
);
1732 xfs_bmbt_log_keys(cur
, bp
, ptr
, ptr
);
1734 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1739 * Convert on-disk form of btree root to in-memory form.
1743 xfs_bmdr_block_t
*dblock
,
1745 xfs_bmbt_block_t
*rblock
,
1749 xfs_bmbt_key_t
*fkp
;
1750 xfs_bmbt_ptr_t
*fpp
;
1751 xfs_bmbt_key_t
*tkp
;
1752 xfs_bmbt_ptr_t
*tpp
;
1754 INT_SET(rblock
->bb_magic
, ARCH_CONVERT
, XFS_BMAP_MAGIC
);
1755 rblock
->bb_level
= dblock
->bb_level
; /* both in on-disk format */
1756 ASSERT(INT_GET(rblock
->bb_level
, ARCH_CONVERT
) > 0);
1757 rblock
->bb_numrecs
= dblock
->bb_numrecs
;/* both in on-disk format */
1758 INT_SET(rblock
->bb_leftsib
, ARCH_CONVERT
, NULLDFSBNO
);
1759 INT_SET(rblock
->bb_rightsib
, ARCH_CONVERT
, NULLDFSBNO
);
1760 dmxr
= (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen
, xfs_bmdr
, 0);
1761 fkp
= XFS_BTREE_KEY_ADDR(dblocklen
, xfs_bmdr
, dblock
, 1, dmxr
);
1762 tkp
= XFS_BMAP_BROOT_KEY_ADDR(rblock
, 1, rblocklen
);
1763 fpp
= XFS_BTREE_PTR_ADDR(dblocklen
, xfs_bmdr
, dblock
, 1, dmxr
);
1764 tpp
= XFS_BMAP_BROOT_PTR_ADDR(rblock
, 1, rblocklen
);
1765 dmxr
= INT_GET(dblock
->bb_numrecs
, ARCH_CONVERT
);
1766 memcpy(tkp
, fkp
, sizeof(*fkp
) * dmxr
);
1767 memcpy(tpp
, fpp
, sizeof(*fpp
) * dmxr
); /* INT_: direct copy */
1771 * Decrement cursor by one record at the level.
1772 * For nonzero levels the leaf-ward information is untouched.
1776 xfs_btree_cur_t
*cur
,
1778 int *stat
) /* success/failure */
1780 xfs_bmbt_block_t
*block
;
1782 int error
; /* error return value */
1783 #ifdef XFS_BMBT_TRACE
1784 static char fname
[] = "xfs_bmbt_decrement";
1786 xfs_fsblock_t fsbno
;
1791 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1792 XFS_BMBT_TRACE_ARGI(cur
, level
);
1793 ASSERT(level
< cur
->bc_nlevels
);
1794 if (level
< cur
->bc_nlevels
- 1)
1795 xfs_btree_readahead(cur
, level
, XFS_BTCUR_LEFTRA
);
1796 if (--cur
->bc_ptrs
[level
] > 0) {
1797 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1801 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
1803 if ((error
= xfs_btree_check_lblock(cur
, block
, level
, bp
))) {
1804 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1808 if (INT_GET(block
->bb_leftsib
, ARCH_CONVERT
) == NULLDFSBNO
) {
1809 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1813 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1814 if (--cur
->bc_ptrs
[lev
] > 0)
1816 if (lev
< cur
->bc_nlevels
- 1)
1817 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_LEFTRA
);
1819 if (lev
== cur
->bc_nlevels
) {
1820 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1826 for (block
= xfs_bmbt_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1827 fsbno
= INT_GET(*XFS_BMAP_PTR_IADDR(block
, cur
->bc_ptrs
[lev
], cur
), ARCH_CONVERT
);
1828 if ((error
= xfs_btree_read_bufl(mp
, tp
, fsbno
, 0, &bp
,
1829 XFS_BMAP_BTREE_REF
))) {
1830 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1834 xfs_btree_setbuf(cur
, lev
, bp
);
1835 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
1836 if ((error
= xfs_btree_check_lblock(cur
, block
, lev
, bp
))) {
1837 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1840 cur
->bc_ptrs
[lev
] = INT_GET(block
->bb_numrecs
, ARCH_CONVERT
);
1842 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1848 * Delete the record pointed to by cur.
1852 xfs_btree_cur_t
*cur
,
1853 int *stat
) /* success/failure */
1855 int error
; /* error return value */
1856 #ifdef XFS_BMBT_TRACE
1857 static char fname
[] = "xfs_bmbt_delete";
1862 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1863 for (level
= 0, i
= 2; i
== 2; level
++) {
1864 if ((error
= xfs_bmbt_delrec(cur
, level
, &i
))) {
1865 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1870 for (level
= 1; level
< cur
->bc_nlevels
; level
++) {
1871 if (cur
->bc_ptrs
[level
] == 0) {
1872 if ((error
= xfs_bmbt_decrement(cur
, level
,
1874 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1881 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1887 * Convert a compressed bmap extent record to an uncompressed form.
1888 * This code must be in sync with the routines xfs_bmbt_get_startoff,
1889 * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state.
1892 STATIC __inline__
void
1901 ext_flag
= (int)(l0
>> (64 - BMBT_EXNTFLAG_BITLEN
));
1902 s
->br_startoff
= ((xfs_fileoff_t
)l0
&
1903 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
1905 s
->br_startblock
= (((xfs_fsblock_t
)l0
& XFS_MASK64LO(9)) << 43) |
1906 (((xfs_fsblock_t
)l1
) >> 21);
1912 b
= (((xfs_dfsbno_t
)l0
& XFS_MASK64LO(9)) << 43) |
1913 (((xfs_dfsbno_t
)l1
) >> 21);
1914 ASSERT((b
>> 32) == 0 || ISNULLDSTARTBLOCK(b
));
1915 s
->br_startblock
= (xfs_fsblock_t
)b
;
1918 s
->br_startblock
= (xfs_fsblock_t
)(((xfs_dfsbno_t
)l1
) >> 21);
1920 #endif /* XFS_BIG_BLKNOS */
1921 s
->br_blockcount
= (xfs_filblks_t
)(l1
& XFS_MASK64LO(21));
1922 /* This is xfs_extent_state() in-line */
1924 ASSERT(s
->br_blockcount
!= 0); /* saved for DMIG */
1925 st
= XFS_EXT_UNWRITTEN
;
1936 __xfs_bmbt_get_all(r
->l0
, r
->l1
, s
);
1940 * Get the block pointer for the given level of the cursor.
1941 * Fill in the buffer pointer, if applicable.
1945 xfs_btree_cur_t
*cur
,
1950 xfs_bmbt_block_t
*rval
;
1952 if (level
< cur
->bc_nlevels
- 1) {
1953 *bpp
= cur
->bc_bufs
[level
];
1954 rval
= XFS_BUF_TO_BMBT_BLOCK(*bpp
);
1957 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
,
1958 cur
->bc_private
.b
.whichfork
);
1959 rval
= ifp
->if_broot
;
1965 * Extract the blockcount field from an in memory bmap extent record.
1968 xfs_bmbt_get_blockcount(
1971 return (xfs_filblks_t
)(r
->l1
& XFS_MASK64LO(21));
1975 * Extract the startblock field from an in memory bmap extent record.
1978 xfs_bmbt_get_startblock(
1982 return (((xfs_fsblock_t
)r
->l0
& XFS_MASK64LO(9)) << 43) |
1983 (((xfs_fsblock_t
)r
->l1
) >> 21);
1988 b
= (((xfs_dfsbno_t
)r
->l0
& XFS_MASK64LO(9)) << 43) |
1989 (((xfs_dfsbno_t
)r
->l1
) >> 21);
1990 ASSERT((b
>> 32) == 0 || ISNULLDSTARTBLOCK(b
));
1991 return (xfs_fsblock_t
)b
;
1993 return (xfs_fsblock_t
)(((xfs_dfsbno_t
)r
->l1
) >> 21);
1995 #endif /* XFS_BIG_BLKNOS */
1999 * Extract the startoff field from an in memory bmap extent record.
2002 xfs_bmbt_get_startoff(
2005 return ((xfs_fileoff_t
)r
->l0
&
2006 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
2015 ext_flag
= (int)((r
->l0
) >> (64 - BMBT_EXNTFLAG_BITLEN
));
2016 return xfs_extent_state(xfs_bmbt_get_blockcount(r
),
2020 #if __BYTE_ORDER != __BIG_ENDIAN
2021 /* Endian flipping versions of the bmbt extraction functions */
2023 xfs_bmbt_disk_get_all(
2029 l0
= INT_GET(r
->l0
, ARCH_CONVERT
);
2030 l1
= INT_GET(r
->l1
, ARCH_CONVERT
);
2032 __xfs_bmbt_get_all(l0
, l1
, s
);
2036 * Extract the blockcount field from an on disk bmap extent record.
2039 xfs_bmbt_disk_get_blockcount(
2042 return (xfs_filblks_t
)(INT_GET(r
->l1
, ARCH_CONVERT
) & XFS_MASK64LO(21));
2046 * Extract the startblock field from an on disk bmap extent record.
2049 xfs_bmbt_disk_get_startblock(
2053 return (((xfs_fsblock_t
)INT_GET(r
->l0
, ARCH_CONVERT
) & XFS_MASK64LO(9)) << 43) |
2054 (((xfs_fsblock_t
)INT_GET(r
->l1
, ARCH_CONVERT
)) >> 21);
2059 b
= (((xfs_dfsbno_t
)INT_GET(r
->l0
, ARCH_CONVERT
) & XFS_MASK64LO(9)) << 43) |
2060 (((xfs_dfsbno_t
)INT_GET(r
->l1
, ARCH_CONVERT
)) >> 21);
2061 ASSERT((b
>> 32) == 0 || ISNULLDSTARTBLOCK(b
));
2062 return (xfs_fsblock_t
)b
;
2064 return (xfs_fsblock_t
)(((xfs_dfsbno_t
)INT_GET(r
->l1
, ARCH_CONVERT
)) >> 21);
2066 #endif /* XFS_BIG_BLKNOS */
2070 * Extract the startoff field from a disk format bmap extent record.
2073 xfs_bmbt_disk_get_startoff(
2076 return ((xfs_fileoff_t
)INT_GET(r
->l0
, ARCH_CONVERT
) &
2077 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
2081 xfs_bmbt_disk_get_state(
2086 ext_flag
= (int)((INT_GET(r
->l0
, ARCH_CONVERT
)) >> (64 - BMBT_EXNTFLAG_BITLEN
));
2087 return xfs_extent_state(xfs_bmbt_disk_get_blockcount(r
),
2094 * Increment cursor by one record at the level.
2095 * For nonzero levels the leaf-ward information is untouched.
2099 xfs_btree_cur_t
*cur
,
2101 int *stat
) /* success/failure */
2103 xfs_bmbt_block_t
*block
;
2105 int error
; /* error return value */
2106 #ifdef XFS_BMBT_TRACE
2107 static char fname
[] = "xfs_bmbt_increment";
2109 xfs_fsblock_t fsbno
;
2114 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2115 XFS_BMBT_TRACE_ARGI(cur
, level
);
2116 ASSERT(level
< cur
->bc_nlevels
);
2117 if (level
< cur
->bc_nlevels
- 1)
2118 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
2119 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
2121 if ((error
= xfs_btree_check_lblock(cur
, block
, level
, bp
))) {
2122 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2126 if (++cur
->bc_ptrs
[level
] <= INT_GET(block
->bb_numrecs
, ARCH_CONVERT
)) {
2127 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2131 if (INT_GET(block
->bb_rightsib
, ARCH_CONVERT
) == NULLDFSBNO
) {
2132 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2136 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
2137 block
= xfs_bmbt_get_block(cur
, lev
, &bp
);
2139 if ((error
= xfs_btree_check_lblock(cur
, block
, lev
, bp
))) {
2140 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2144 if (++cur
->bc_ptrs
[lev
] <= INT_GET(block
->bb_numrecs
, ARCH_CONVERT
))
2146 if (lev
< cur
->bc_nlevels
- 1)
2147 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_RIGHTRA
);
2149 if (lev
== cur
->bc_nlevels
) {
2150 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2156 for (block
= xfs_bmbt_get_block(cur
, lev
, &bp
); lev
> level
; ) {
2157 fsbno
= INT_GET(*XFS_BMAP_PTR_IADDR(block
, cur
->bc_ptrs
[lev
], cur
), ARCH_CONVERT
);
2158 if ((error
= xfs_btree_read_bufl(mp
, tp
, fsbno
, 0, &bp
,
2159 XFS_BMAP_BTREE_REF
))) {
2160 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2164 xfs_btree_setbuf(cur
, lev
, bp
);
2165 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
2166 if ((error
= xfs_btree_check_lblock(cur
, block
, lev
, bp
))) {
2167 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2170 cur
->bc_ptrs
[lev
] = 1;
2172 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2178 * Insert the current record at the point referenced by cur.
2182 xfs_btree_cur_t
*cur
,
2183 int *stat
) /* success/failure */
2185 int error
; /* error return value */
2186 #ifdef XFS_BMBT_TRACE
2187 static char fname
[] = "xfs_bmbt_insert";
2192 xfs_btree_cur_t
*ncur
;
2193 xfs_bmbt_rec_t nrec
;
2194 xfs_btree_cur_t
*pcur
;
2196 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2199 xfs_bmbt_disk_set_all(&nrec
, &cur
->bc_rec
.b
);
2200 ncur
= (xfs_btree_cur_t
*)0;
2203 if ((error
= xfs_bmbt_insrec(pcur
, level
++, &nbno
, &nrec
, &ncur
,
2206 xfs_btree_del_cursor(pcur
, XFS_BTREE_ERROR
);
2207 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2210 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
2211 if (pcur
!= cur
&& (ncur
|| nbno
== NULLFSBLOCK
)) {
2212 cur
->bc_nlevels
= pcur
->bc_nlevels
;
2213 cur
->bc_private
.b
.allocated
+=
2214 pcur
->bc_private
.b
.allocated
;
2215 pcur
->bc_private
.b
.allocated
= 0;
2216 ASSERT((cur
->bc_private
.b
.firstblock
!= NULLFSBLOCK
) ||
2217 (cur
->bc_private
.b
.ip
->i_d
.di_flags
&
2218 XFS_DIFLAG_REALTIME
));
2219 cur
->bc_private
.b
.firstblock
=
2220 pcur
->bc_private
.b
.firstblock
;
2221 ASSERT(cur
->bc_private
.b
.flist
==
2222 pcur
->bc_private
.b
.flist
);
2223 xfs_btree_del_cursor(pcur
, XFS_BTREE_NOERROR
);
2227 ncur
= (xfs_btree_cur_t
*)0;
2229 } while (nbno
!= NULLFSBLOCK
);
2230 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2234 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2239 * Log fields from the btree block header.
2243 xfs_btree_cur_t
*cur
,
2248 #ifdef XFS_BMBT_TRACE
2249 static char fname
[] = "xfs_bmbt_log_block";
2253 static const short offsets
[] = {
2254 offsetof(xfs_bmbt_block_t
, bb_magic
),
2255 offsetof(xfs_bmbt_block_t
, bb_level
),
2256 offsetof(xfs_bmbt_block_t
, bb_numrecs
),
2257 offsetof(xfs_bmbt_block_t
, bb_leftsib
),
2258 offsetof(xfs_bmbt_block_t
, bb_rightsib
),
2259 sizeof(xfs_bmbt_block_t
)
2262 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2263 XFS_BMBT_TRACE_ARGBI(cur
, bp
, fields
);
2266 xfs_btree_offsets(fields
, offsets
, XFS_BB_NUM_BITS
, &first
,
2268 xfs_trans_log_buf(tp
, bp
, first
, last
);
2270 xfs_trans_log_inode(tp
, cur
->bc_private
.b
.ip
,
2271 XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
));
2272 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2276 * Log record values from the btree block.
2280 xfs_btree_cur_t
*cur
,
2285 xfs_bmbt_block_t
*block
;
2287 #ifdef XFS_BMBT_TRACE
2288 static char fname
[] = "xfs_bmbt_log_recs";
2294 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2295 XFS_BMBT_TRACE_ARGBII(cur
, bp
, rfirst
, rlast
);
2298 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
2299 rp
= XFS_BMAP_REC_DADDR(block
, 1, cur
);
2300 first
= (int)((xfs_caddr_t
)&rp
[rfirst
- 1] - (xfs_caddr_t
)block
);
2301 last
= (int)(((xfs_caddr_t
)&rp
[rlast
] - 1) - (xfs_caddr_t
)block
);
2302 xfs_trans_log_buf(tp
, bp
, first
, last
);
2303 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2308 xfs_btree_cur_t
*cur
,
2312 int *stat
) /* success/failure */
2314 cur
->bc_rec
.b
.br_startoff
= off
;
2315 cur
->bc_rec
.b
.br_startblock
= bno
;
2316 cur
->bc_rec
.b
.br_blockcount
= len
;
2317 return xfs_bmbt_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
2322 xfs_btree_cur_t
*cur
,
2326 int *stat
) /* success/failure */
2328 cur
->bc_rec
.b
.br_startoff
= off
;
2329 cur
->bc_rec
.b
.br_startblock
= bno
;
2330 cur
->bc_rec
.b
.br_blockcount
= len
;
2331 return xfs_bmbt_lookup(cur
, XFS_LOOKUP_GE
, stat
);
2336 xfs_btree_cur_t
*cur
,
2340 int *stat
) /* success/failure */
2342 cur
->bc_rec
.b
.br_startoff
= off
;
2343 cur
->bc_rec
.b
.br_startblock
= bno
;
2344 cur
->bc_rec
.b
.br_blockcount
= len
;
2345 return xfs_bmbt_lookup(cur
, XFS_LOOKUP_LE
, stat
);
2349 * Give the bmap btree a new root block. Copy the old broot contents
2350 * down into a real block and make the broot point to it.
2354 xfs_btree_cur_t
*cur
, /* btree cursor */
2355 int *logflags
, /* logging flags for inode */
2356 int *stat
) /* return status - 0 fail */
2358 xfs_alloc_arg_t args
; /* allocation arguments */
2359 xfs_bmbt_block_t
*block
; /* bmap btree block */
2360 xfs_buf_t
*bp
; /* buffer for block */
2361 xfs_bmbt_block_t
*cblock
; /* child btree block */
2362 xfs_bmbt_key_t
*ckp
; /* child key pointer */
2363 xfs_bmbt_ptr_t
*cpp
; /* child ptr pointer */
2364 int error
; /* error return code */
2365 #ifdef XFS_BMBT_TRACE
2366 static char fname
[] = "xfs_bmbt_newroot";
2369 int i
; /* loop counter */
2371 xfs_bmbt_key_t
*kp
; /* pointer to bmap btree key */
2372 int level
; /* btree level */
2373 xfs_bmbt_ptr_t
*pp
; /* pointer to bmap block addr */
2375 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2376 level
= cur
->bc_nlevels
- 1;
2377 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
2379 * Copy the root into a real block.
2381 args
.mp
= cur
->bc_mp
;
2382 pp
= XFS_BMAP_PTR_IADDR(block
, 1, cur
);
2383 args
.tp
= cur
->bc_tp
;
2384 args
.fsbno
= cur
->bc_private
.b
.firstblock
;
2385 args
.mod
= args
.minleft
= args
.alignment
= args
.total
= args
.isfl
=
2386 args
.userdata
= args
.minalignslop
= 0;
2387 args
.minlen
= args
.maxlen
= args
.prod
= 1;
2388 args
.wasdel
= cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
;
2389 if (args
.fsbno
== NULLFSBLOCK
) {
2391 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(*pp
, ARCH_CONVERT
), level
))) {
2392 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2396 args
.fsbno
= INT_GET(*pp
, ARCH_CONVERT
);
2397 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2398 } else if (args
.wasdel
)
2399 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
2401 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
2402 if ((error
= xfs_alloc_vextent(&args
))) {
2403 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2406 if (args
.fsbno
== NULLFSBLOCK
) {
2407 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2411 ASSERT(args
.len
== 1);
2412 cur
->bc_private
.b
.firstblock
= args
.fsbno
;
2413 cur
->bc_private
.b
.allocated
++;
2414 cur
->bc_private
.b
.ip
->i_d
.di_nblocks
++;
2415 XFS_TRANS_MOD_DQUOT_BYINO(args
.mp
, args
.tp
, cur
->bc_private
.b
.ip
,
2416 XFS_TRANS_DQ_BCOUNT
, 1L);
2417 bp
= xfs_btree_get_bufl(args
.mp
, cur
->bc_tp
, args
.fsbno
, 0);
2418 cblock
= XFS_BUF_TO_BMBT_BLOCK(bp
);
2420 INT_MOD(block
->bb_level
, ARCH_CONVERT
, +1);
2421 INT_SET(block
->bb_numrecs
, ARCH_CONVERT
, 1);
2423 cur
->bc_ptrs
[level
+ 1] = 1;
2424 kp
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
2425 ckp
= XFS_BMAP_KEY_IADDR(cblock
, 1, cur
);
2426 memcpy(ckp
, kp
, INT_GET(cblock
->bb_numrecs
, ARCH_CONVERT
) * sizeof(*kp
));
2427 cpp
= XFS_BMAP_PTR_IADDR(cblock
, 1, cur
);
2429 for (i
= 0; i
< INT_GET(cblock
->bb_numrecs
, ARCH_CONVERT
); i
++) {
2430 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(pp
[i
], ARCH_CONVERT
), level
))) {
2431 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2436 memcpy(cpp
, pp
, INT_GET(cblock
->bb_numrecs
, ARCH_CONVERT
) * sizeof(*pp
));
2438 if ((error
= xfs_btree_check_lptr(cur
, (xfs_bmbt_ptr_t
)args
.fsbno
,
2440 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2444 INT_SET(*pp
, ARCH_CONVERT
, args
.fsbno
);
2445 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, 1 - INT_GET(cblock
->bb_numrecs
, ARCH_CONVERT
),
2446 cur
->bc_private
.b
.whichfork
);
2447 xfs_btree_setbuf(cur
, level
, bp
);
2449 * Do all this logging at the end so that
2450 * the root is at the right level.
2452 xfs_bmbt_log_block(cur
, bp
, XFS_BB_ALL_BITS
);
2453 xfs_bmbt_log_keys(cur
, bp
, 1, INT_GET(cblock
->bb_numrecs
, ARCH_CONVERT
));
2454 xfs_bmbt_log_ptrs(cur
, bp
, 1, INT_GET(cblock
->bb_numrecs
, ARCH_CONVERT
));
2455 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2457 XFS_ILOG_CORE
| XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
);
2463 * Set all the fields in a bmap extent record from the uncompressed form.
2472 ASSERT((s
->br_state
== XFS_EXT_NORM
) ||
2473 (s
->br_state
== XFS_EXT_UNWRITTEN
));
2474 extent_flag
= (s
->br_state
== XFS_EXT_NORM
) ? 0 : 1;
2475 ASSERT((s
->br_startoff
& XFS_MASK64HI(9)) == 0);
2476 ASSERT((s
->br_blockcount
& XFS_MASK64HI(43)) == 0);
2478 ASSERT((s
->br_startblock
& XFS_MASK64HI(12)) == 0);
2479 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2480 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9) |
2481 ((xfs_bmbt_rec_base_t
)s
->br_startblock
>> 43);
2482 r
->l1
= ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2483 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2484 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2485 #else /* !XFS_BIG_BLKNOS */
2486 if (ISNULLSTARTBLOCK(s
->br_startblock
)) {
2487 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2488 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9) |
2489 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9);
2490 r
->l1
= XFS_MASK64HI(11) |
2491 ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2492 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2493 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2495 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2496 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9);
2497 r
->l1
= ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2498 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2499 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2501 #endif /* XFS_BIG_BLKNOS */
2505 * Set all the fields in a bmap extent record from the arguments.
2517 ASSERT((v
== XFS_EXT_NORM
) || (v
== XFS_EXT_UNWRITTEN
));
2518 extent_flag
= (v
== XFS_EXT_NORM
) ? 0 : 1;
2519 ASSERT((o
& XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN
)) == 0);
2520 ASSERT((c
& XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN
)) == 0);
2522 ASSERT((b
& XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN
)) == 0);
2523 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2524 ((xfs_bmbt_rec_base_t
)o
<< 9) |
2525 ((xfs_bmbt_rec_base_t
)b
>> 43);
2526 r
->l1
= ((xfs_bmbt_rec_base_t
)b
<< 21) |
2527 ((xfs_bmbt_rec_base_t
)c
&
2528 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2529 #else /* !XFS_BIG_BLKNOS */
2530 if (ISNULLSTARTBLOCK(b
)) {
2531 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2532 ((xfs_bmbt_rec_base_t
)o
<< 9) |
2533 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9);
2534 r
->l1
= XFS_MASK64HI(11) |
2535 ((xfs_bmbt_rec_base_t
)b
<< 21) |
2536 ((xfs_bmbt_rec_base_t
)c
&
2537 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2539 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2540 ((xfs_bmbt_rec_base_t
)o
<< 9);
2541 r
->l1
= ((xfs_bmbt_rec_base_t
)b
<< 21) |
2542 ((xfs_bmbt_rec_base_t
)c
&
2543 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2545 #endif /* XFS_BIG_BLKNOS */
2548 #if __BYTE_ORDER != __BIG_ENDIAN
2550 * Set all the fields in a bmap extent record from the uncompressed form.
2553 xfs_bmbt_disk_set_all(
2559 ASSERT((s
->br_state
== XFS_EXT_NORM
) ||
2560 (s
->br_state
== XFS_EXT_UNWRITTEN
));
2561 extent_flag
= (s
->br_state
== XFS_EXT_NORM
) ? 0 : 1;
2562 ASSERT((s
->br_startoff
& XFS_MASK64HI(9)) == 0);
2563 ASSERT((s
->br_blockcount
& XFS_MASK64HI(43)) == 0);
2565 ASSERT((s
->br_startblock
& XFS_MASK64HI(12)) == 0);
2566 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2567 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9) |
2568 ((xfs_bmbt_rec_base_t
)s
->br_startblock
>> 43));
2569 INT_SET(r
->l1
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2570 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2571 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2572 #else /* !XFS_BIG_BLKNOS */
2573 if (ISNULLSTARTBLOCK(s
->br_startblock
)) {
2574 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2575 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9) |
2576 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9));
2577 INT_SET(r
->l1
, ARCH_CONVERT
, XFS_MASK64HI(11) |
2578 ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2579 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2580 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2582 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2583 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9));
2584 INT_SET(r
->l1
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2585 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2586 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2588 #endif /* XFS_BIG_BLKNOS */
2592 * Set all the fields in a disk format bmap extent record from the arguments.
2595 xfs_bmbt_disk_set_allf(
2604 ASSERT((v
== XFS_EXT_NORM
) || (v
== XFS_EXT_UNWRITTEN
));
2605 extent_flag
= (v
== XFS_EXT_NORM
) ? 0 : 1;
2606 ASSERT((o
& XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN
)) == 0);
2607 ASSERT((c
& XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN
)) == 0);
2609 ASSERT((b
& XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN
)) == 0);
2610 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2611 ((xfs_bmbt_rec_base_t
)o
<< 9) |
2612 ((xfs_bmbt_rec_base_t
)b
>> 43));
2613 INT_SET(r
->l1
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)b
<< 21) |
2614 ((xfs_bmbt_rec_base_t
)c
&
2615 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2616 #else /* !XFS_BIG_BLKNOS */
2617 if (ISNULLSTARTBLOCK(b
)) {
2618 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2619 ((xfs_bmbt_rec_base_t
)o
<< 9) |
2620 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9));
2621 INT_SET(r
->l1
, ARCH_CONVERT
, XFS_MASK64HI(11) |
2622 ((xfs_bmbt_rec_base_t
)b
<< 21) |
2623 ((xfs_bmbt_rec_base_t
)c
&
2624 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2626 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2627 ((xfs_bmbt_rec_base_t
)o
<< 9));
2628 INT_SET(r
->l1
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)b
<< 21) |
2629 ((xfs_bmbt_rec_base_t
)c
&
2630 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2632 #endif /* XFS_BIG_BLKNOS */
2637 * Set the blockcount field in a bmap extent record.
2640 xfs_bmbt_set_blockcount(
2644 ASSERT((v
& XFS_MASK64HI(43)) == 0);
2645 r
->l1
= (r
->l1
& (xfs_bmbt_rec_base_t
)XFS_MASK64HI(43)) |
2646 (xfs_bmbt_rec_base_t
)(v
& XFS_MASK64LO(21));
2650 * Set the startblock field in a bmap extent record.
2653 xfs_bmbt_set_startblock(
2658 ASSERT((v
& XFS_MASK64HI(12)) == 0);
2659 r
->l0
= (r
->l0
& (xfs_bmbt_rec_base_t
)XFS_MASK64HI(55)) |
2660 (xfs_bmbt_rec_base_t
)(v
>> 43);
2661 r
->l1
= (r
->l1
& (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)) |
2662 (xfs_bmbt_rec_base_t
)(v
<< 21);
2663 #else /* !XFS_BIG_BLKNOS */
2664 if (ISNULLSTARTBLOCK(v
)) {
2665 r
->l0
|= (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9);
2666 r
->l1
= (xfs_bmbt_rec_base_t
)XFS_MASK64HI(11) |
2667 ((xfs_bmbt_rec_base_t
)v
<< 21) |
2668 (r
->l1
& (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2670 r
->l0
&= ~(xfs_bmbt_rec_base_t
)XFS_MASK64LO(9);
2671 r
->l1
= ((xfs_bmbt_rec_base_t
)v
<< 21) |
2672 (r
->l1
& (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2674 #endif /* XFS_BIG_BLKNOS */
2678 * Set the startoff field in a bmap extent record.
2681 xfs_bmbt_set_startoff(
2685 ASSERT((v
& XFS_MASK64HI(9)) == 0);
2686 r
->l0
= (r
->l0
& (xfs_bmbt_rec_base_t
) XFS_MASK64HI(1)) |
2687 ((xfs_bmbt_rec_base_t
)v
<< 9) |
2688 (r
->l0
& (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9));
2692 * Set the extent state field in a bmap extent record.
2699 ASSERT(v
== XFS_EXT_NORM
|| v
== XFS_EXT_UNWRITTEN
);
2700 if (v
== XFS_EXT_NORM
)
2701 r
->l0
&= XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN
);
2703 r
->l0
|= XFS_MASK64HI(BMBT_EXNTFLAG_BITLEN
);
2707 * Convert in-memory form of btree root to on-disk form.
2711 xfs_bmbt_block_t
*rblock
,
2713 xfs_bmdr_block_t
*dblock
,
2717 xfs_bmbt_key_t
*fkp
;
2718 xfs_bmbt_ptr_t
*fpp
;
2719 xfs_bmbt_key_t
*tkp
;
2720 xfs_bmbt_ptr_t
*tpp
;
2722 ASSERT(INT_GET(rblock
->bb_magic
, ARCH_CONVERT
) == XFS_BMAP_MAGIC
);
2723 ASSERT(INT_GET(rblock
->bb_leftsib
, ARCH_CONVERT
) == NULLDFSBNO
);
2724 ASSERT(INT_GET(rblock
->bb_rightsib
, ARCH_CONVERT
) == NULLDFSBNO
);
2725 ASSERT(INT_GET(rblock
->bb_level
, ARCH_CONVERT
) > 0);
2726 dblock
->bb_level
= rblock
->bb_level
; /* both in on-disk format */
2727 dblock
->bb_numrecs
= rblock
->bb_numrecs
;/* both in on-disk format */
2728 dmxr
= (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen
, xfs_bmdr
, 0);
2729 fkp
= XFS_BMAP_BROOT_KEY_ADDR(rblock
, 1, rblocklen
);
2730 tkp
= XFS_BTREE_KEY_ADDR(dblocklen
, xfs_bmdr
, dblock
, 1, dmxr
);
2731 fpp
= XFS_BMAP_BROOT_PTR_ADDR(rblock
, 1, rblocklen
);
2732 tpp
= XFS_BTREE_PTR_ADDR(dblocklen
, xfs_bmdr
, dblock
, 1, dmxr
);
2733 dmxr
= INT_GET(dblock
->bb_numrecs
, ARCH_CONVERT
);
2734 memcpy(tkp
, fkp
, sizeof(*fkp
) * dmxr
);
2735 memcpy(tpp
, fpp
, sizeof(*fpp
) * dmxr
); /* INT_: direct copy */
2739 * Update the record to the passed values.
2743 xfs_btree_cur_t
*cur
,
2749 xfs_bmbt_block_t
*block
;
2752 #ifdef XFS_BMBT_TRACE
2753 static char fname
[] = "xfs_bmbt_update";
2759 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2760 XFS_BMBT_TRACE_ARGFFFI(cur
, (xfs_dfiloff_t
)off
, (xfs_dfsbno_t
)bno
,
2761 (xfs_dfilblks_t
)len
, (int)state
);
2762 block
= xfs_bmbt_get_block(cur
, 0, &bp
);
2764 if ((error
= xfs_btree_check_lblock(cur
, block
, 0, bp
))) {
2765 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2769 ptr
= cur
->bc_ptrs
[0];
2770 rp
= XFS_BMAP_REC_IADDR(block
, ptr
, cur
);
2771 xfs_bmbt_disk_set_allf(rp
, off
, bno
, len
, state
);
2772 xfs_bmbt_log_recs(cur
, bp
, ptr
, ptr
);
2774 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2777 INT_SET(key
.br_startoff
, ARCH_CONVERT
, off
);
2778 if ((error
= xfs_bmbt_updkey(cur
, &key
, 1))) {
2779 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2782 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2787 * Check an extent list, which has just been read, for
2788 * any bit in the extent flag field. ASSERT on debug
2789 * kernels, as this condition should not occur.
2790 * Return an error condition (1) if any flags found,
2791 * otherwise return 0.
2795 xfs_check_nostate_extents(
2799 for (; num
> 0; num
--, ep
++) {
2801 (64 - BMBT_EXNTFLAG_BITLEN
)) != 0) {