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(cur
, INT_GET(pp
[i
], ARCH_CONVERT
), 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(cur
, INT_GET(rpp
[i
], ARCH_CONVERT
), 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(cur
, INT_GET(pp
[i
- 1], ARCH_CONVERT
),
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
, (xfs_bmbt_ptr_t
)*bnop
,
875 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
880 INT_SET(pp
[ptr
- 1], ARCH_CONVERT
, *bnop
);
882 block
->bb_numrecs
= cpu_to_be16(numrecs
);
883 xfs_bmbt_log_keys(cur
, bp
, ptr
, numrecs
);
884 xfs_bmbt_log_ptrs(cur
, bp
, ptr
, numrecs
);
886 rp
= XFS_BMAP_REC_IADDR(block
, 1, cur
);
887 memmove(&rp
[ptr
], &rp
[ptr
- 1],
888 (numrecs
- ptr
+ 1) * sizeof(*rp
));
891 block
->bb_numrecs
= cpu_to_be16(numrecs
);
892 xfs_bmbt_log_recs(cur
, bp
, ptr
, numrecs
);
894 xfs_bmbt_log_block(cur
, bp
, XFS_BB_NUMRECS
);
898 xfs_btree_check_rec(XFS_BTNUM_BMAP
, rp
+ ptr
- 1,
901 xfs_btree_check_key(XFS_BTNUM_BMAP
, kp
+ ptr
- 1,
905 if (optr
== 1 && (error
= xfs_bmbt_updkey(cur
, &key
, level
+ 1))) {
906 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
910 if (nbno
!= NULLFSBLOCK
) {
914 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
921 xfs_btree_cur_t
*cur
)
923 xfs_bmbt_block_t
*block
;
924 xfs_bmbt_block_t
*cblock
;
931 #ifdef XFS_BMBT_TRACE
932 static char fname
[] = "xfs_bmbt_killroot";
941 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
942 level
= cur
->bc_nlevels
- 1;
945 * Don't deal with the root block needs to be a leaf case.
946 * We're just going to turn the thing back into extents anyway.
949 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
952 block
= xfs_bmbt_get_block(cur
, level
, &cbp
);
954 * Give up if the root has multiple children.
956 if (be16_to_cpu(block
->bb_numrecs
) != 1) {
957 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
961 * Only do this if the next level will fit.
962 * Then the data must be copied up to the inode,
963 * instead of freeing the root you free the next level.
965 cbp
= cur
->bc_bufs
[level
- 1];
966 cblock
= XFS_BUF_TO_BMBT_BLOCK(cbp
);
967 if (be16_to_cpu(cblock
->bb_numrecs
) > XFS_BMAP_BLOCK_DMAXRECS(level
, cur
)) {
968 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
971 ASSERT(be64_to_cpu(cblock
->bb_leftsib
) == NULLDFSBNO
);
972 ASSERT(be64_to_cpu(cblock
->bb_rightsib
) == NULLDFSBNO
);
973 ip
= cur
->bc_private
.b
.ip
;
974 ifp
= XFS_IFORK_PTR(ip
, cur
->bc_private
.b
.whichfork
);
975 ASSERT(XFS_BMAP_BLOCK_IMAXRECS(level
, cur
) ==
976 XFS_BMAP_BROOT_MAXRECS(ifp
->if_broot_bytes
));
977 i
= (int)(be16_to_cpu(cblock
->bb_numrecs
) - XFS_BMAP_BLOCK_IMAXRECS(level
, cur
));
979 xfs_iroot_realloc(ip
, i
, cur
->bc_private
.b
.whichfork
);
980 block
= ifp
->if_broot
;
982 be16_add(&block
->bb_numrecs
, i
);
983 ASSERT(block
->bb_numrecs
== cblock
->bb_numrecs
);
984 kp
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
985 ckp
= XFS_BMAP_KEY_IADDR(cblock
, 1, cur
);
986 memcpy(kp
, ckp
, be16_to_cpu(block
->bb_numrecs
) * sizeof(*kp
));
987 pp
= XFS_BMAP_PTR_IADDR(block
, 1, cur
);
988 cpp
= XFS_BMAP_PTR_IADDR(cblock
, 1, cur
);
990 for (i
= 0; i
< be16_to_cpu(cblock
->bb_numrecs
); i
++) {
991 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(cpp
[i
], ARCH_CONVERT
), level
- 1))) {
992 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
997 memcpy(pp
, cpp
, be16_to_cpu(block
->bb_numrecs
) * sizeof(*pp
));
998 xfs_bmap_add_free(XFS_DADDR_TO_FSB(cur
->bc_mp
, XFS_BUF_ADDR(cbp
)), 1,
999 cur
->bc_private
.b
.flist
, cur
->bc_mp
);
1000 ip
->i_d
.di_nblocks
--;
1001 XFS_TRANS_MOD_DQUOT_BYINO(cur
->bc_mp
, cur
->bc_tp
, ip
,
1002 XFS_TRANS_DQ_BCOUNT
, -1L);
1003 xfs_trans_binval(cur
->bc_tp
, cbp
);
1004 cur
->bc_bufs
[level
- 1] = NULL
;
1005 be16_add(&block
->bb_level
, -1);
1006 xfs_trans_log_inode(cur
->bc_tp
, ip
,
1007 XFS_ILOG_CORE
| XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
));
1009 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1014 * Log key values from the btree block.
1018 xfs_btree_cur_t
*cur
,
1023 #ifdef XFS_BMBT_TRACE
1024 static char fname
[] = "xfs_bmbt_log_keys";
1028 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1029 XFS_BMBT_TRACE_ARGBII(cur
, bp
, kfirst
, klast
);
1032 xfs_bmbt_block_t
*block
;
1037 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
1038 kp
= XFS_BMAP_KEY_DADDR(block
, 1, cur
);
1039 first
= (int)((xfs_caddr_t
)&kp
[kfirst
- 1] - (xfs_caddr_t
)block
);
1040 last
= (int)(((xfs_caddr_t
)&kp
[klast
] - 1) - (xfs_caddr_t
)block
);
1041 xfs_trans_log_buf(tp
, bp
, first
, last
);
1045 ip
= cur
->bc_private
.b
.ip
;
1046 xfs_trans_log_inode(tp
, ip
,
1047 XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
));
1049 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1053 * Log pointer values from the btree block.
1057 xfs_btree_cur_t
*cur
,
1062 #ifdef XFS_BMBT_TRACE
1063 static char fname
[] = "xfs_bmbt_log_ptrs";
1067 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1068 XFS_BMBT_TRACE_ARGBII(cur
, bp
, pfirst
, plast
);
1071 xfs_bmbt_block_t
*block
;
1076 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
1077 pp
= XFS_BMAP_PTR_DADDR(block
, 1, cur
);
1078 first
= (int)((xfs_caddr_t
)&pp
[pfirst
- 1] - (xfs_caddr_t
)block
);
1079 last
= (int)(((xfs_caddr_t
)&pp
[plast
] - 1) - (xfs_caddr_t
)block
);
1080 xfs_trans_log_buf(tp
, bp
, first
, last
);
1084 ip
= cur
->bc_private
.b
.ip
;
1085 xfs_trans_log_inode(tp
, ip
,
1086 XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
));
1088 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1092 * Lookup the record. The cursor is made to point to it, based on dir.
1094 STATIC
int /* error */
1096 xfs_btree_cur_t
*cur
,
1098 int *stat
) /* success/failure */
1100 xfs_bmbt_block_t
*block
=NULL
;
1104 int error
; /* error return value */
1105 #ifdef XFS_BMBT_TRACE
1106 static char fname
[] = "xfs_bmbt_lookup";
1108 xfs_fsblock_t fsbno
=0;
1112 xfs_bmbt_key_t
*kkbase
=NULL
;
1113 xfs_bmbt_key_t
*kkp
;
1114 xfs_bmbt_rec_t
*krbase
=NULL
;
1115 xfs_bmbt_rec_t
*krp
;
1120 xfs_bmbt_irec_t
*rp
;
1121 xfs_fileoff_t startoff
;
1124 XFS_STATS_INC(xs_bmbt_lookup
);
1125 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1126 XFS_BMBT_TRACE_ARGI(cur
, (int)dir
);
1129 rp
= &cur
->bc_rec
.b
;
1130 for (level
= cur
->bc_nlevels
- 1, diff
= 1; level
>= 0; level
--) {
1131 if (level
< cur
->bc_nlevels
- 1) {
1132 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
1133 bp
= cur
->bc_bufs
[level
];
1134 if (bp
&& XFS_BUF_ADDR(bp
) != d
)
1135 bp
= (xfs_buf_t
*)0;
1137 if ((error
= xfs_btree_read_bufl(mp
, tp
, fsbno
,
1138 0, &bp
, XFS_BMAP_BTREE_REF
))) {
1139 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1142 xfs_btree_setbuf(cur
, level
, bp
);
1143 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
1144 if ((error
= xfs_btree_check_lblock(cur
, block
,
1146 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1150 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
1152 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
1157 kkbase
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
1159 krbase
= XFS_BMAP_REC_IADDR(block
, 1, cur
);
1161 if (!(high
= be16_to_cpu(block
->bb_numrecs
))) {
1163 cur
->bc_ptrs
[0] = dir
!= XFS_LOOKUP_LE
;
1164 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1168 while (low
<= high
) {
1169 XFS_STATS_INC(xs_bmbt_compare
);
1170 keyno
= (low
+ high
) >> 1;
1172 kkp
= kkbase
+ keyno
- 1;
1173 startoff
= INT_GET(kkp
->br_startoff
, ARCH_CONVERT
);
1175 krp
= krbase
+ keyno
- 1;
1176 startoff
= xfs_bmbt_disk_get_startoff(krp
);
1178 diff
= (xfs_sfiloff_t
)
1179 (startoff
- rp
->br_startoff
);
1189 if (diff
> 0 && --keyno
< 1)
1191 pp
= XFS_BMAP_PTR_IADDR(block
, keyno
, cur
);
1193 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(*pp
, ARCH_CONVERT
), level
))) {
1194 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1198 fsbno
= INT_GET(*pp
, ARCH_CONVERT
);
1199 cur
->bc_ptrs
[level
] = keyno
;
1202 if (dir
!= XFS_LOOKUP_LE
&& diff
< 0) {
1205 * If ge search and we went off the end of the block, but it's
1206 * not the last block, we're in the wrong block.
1208 if (dir
== XFS_LOOKUP_GE
&& keyno
> be16_to_cpu(block
->bb_numrecs
) &&
1209 be64_to_cpu(block
->bb_rightsib
) != NULLDFSBNO
) {
1210 cur
->bc_ptrs
[0] = keyno
;
1211 if ((error
= xfs_bmbt_increment(cur
, 0, &i
))) {
1212 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1215 XFS_WANT_CORRUPTED_RETURN(i
== 1);
1216 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1221 else if (dir
== XFS_LOOKUP_LE
&& diff
> 0)
1223 cur
->bc_ptrs
[0] = keyno
;
1224 if (keyno
== 0 || keyno
> be16_to_cpu(block
->bb_numrecs
)) {
1225 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1228 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1229 *stat
= ((dir
!= XFS_LOOKUP_EQ
) || (diff
== 0));
1235 * Move 1 record left from cur/level if possible.
1236 * Update cur to reflect the new path.
1238 STATIC
int /* error */
1240 xfs_btree_cur_t
*cur
,
1242 int *stat
) /* success/failure */
1244 int error
; /* error return value */
1245 #ifdef XFS_BMBT_TRACE
1246 static char fname
[] = "xfs_bmbt_lshift";
1249 int i
; /* loop counter */
1251 xfs_bmbt_key_t key
; /* bmap btree key */
1252 xfs_buf_t
*lbp
; /* left buffer pointer */
1253 xfs_bmbt_block_t
*left
; /* left btree block */
1254 xfs_bmbt_key_t
*lkp
=NULL
; /* left btree key */
1255 xfs_bmbt_ptr_t
*lpp
; /* left address pointer */
1256 int lrecs
; /* left record count */
1257 xfs_bmbt_rec_t
*lrp
=NULL
; /* left record pointer */
1258 xfs_mount_t
*mp
; /* file system mount point */
1259 xfs_buf_t
*rbp
; /* right buffer pointer */
1260 xfs_bmbt_block_t
*right
; /* right btree block */
1261 xfs_bmbt_key_t
*rkp
=NULL
; /* right btree key */
1262 xfs_bmbt_ptr_t
*rpp
=NULL
; /* right address pointer */
1263 xfs_bmbt_rec_t
*rrp
=NULL
; /* right record pointer */
1264 int rrecs
; /* right record count */
1266 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1267 XFS_BMBT_TRACE_ARGI(cur
, level
);
1268 if (level
== cur
->bc_nlevels
- 1) {
1269 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1273 rbp
= cur
->bc_bufs
[level
];
1274 right
= XFS_BUF_TO_BMBT_BLOCK(rbp
);
1276 if ((error
= xfs_btree_check_lblock(cur
, right
, level
, rbp
))) {
1277 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1281 if (be64_to_cpu(right
->bb_leftsib
) == NULLDFSBNO
) {
1282 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1286 if (cur
->bc_ptrs
[level
] <= 1) {
1287 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1292 if ((error
= xfs_btree_read_bufl(mp
, cur
->bc_tp
, be64_to_cpu(right
->bb_leftsib
), 0,
1293 &lbp
, XFS_BMAP_BTREE_REF
))) {
1294 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1297 left
= XFS_BUF_TO_BMBT_BLOCK(lbp
);
1298 if ((error
= xfs_btree_check_lblock(cur
, left
, level
, lbp
))) {
1299 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1302 if (be16_to_cpu(left
->bb_numrecs
) == XFS_BMAP_BLOCK_IMAXRECS(level
, cur
)) {
1303 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1307 lrecs
= be16_to_cpu(left
->bb_numrecs
) + 1;
1309 lkp
= XFS_BMAP_KEY_IADDR(left
, lrecs
, cur
);
1310 rkp
= XFS_BMAP_KEY_IADDR(right
, 1, cur
);
1312 xfs_bmbt_log_keys(cur
, lbp
, lrecs
, lrecs
);
1313 lpp
= XFS_BMAP_PTR_IADDR(left
, lrecs
, cur
);
1314 rpp
= XFS_BMAP_PTR_IADDR(right
, 1, cur
);
1316 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(*rpp
, ARCH_CONVERT
), level
))) {
1317 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1321 *lpp
= *rpp
; /* INT_: direct copy */
1322 xfs_bmbt_log_ptrs(cur
, lbp
, lrecs
, lrecs
);
1324 lrp
= XFS_BMAP_REC_IADDR(left
, lrecs
, cur
);
1325 rrp
= XFS_BMAP_REC_IADDR(right
, 1, cur
);
1327 xfs_bmbt_log_recs(cur
, lbp
, lrecs
, lrecs
);
1329 left
->bb_numrecs
= cpu_to_be16(lrecs
);
1330 xfs_bmbt_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
1333 xfs_btree_check_key(XFS_BTNUM_BMAP
, lkp
- 1, lkp
);
1335 xfs_btree_check_rec(XFS_BTNUM_BMAP
, lrp
- 1, lrp
);
1337 rrecs
= be16_to_cpu(right
->bb_numrecs
) - 1;
1338 right
->bb_numrecs
= cpu_to_be16(rrecs
);
1339 xfs_bmbt_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
1342 for (i
= 0; i
< rrecs
; i
++) {
1343 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(rpp
[i
+ 1], ARCH_CONVERT
),
1345 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1350 memmove(rkp
, rkp
+ 1, rrecs
* sizeof(*rkp
));
1351 memmove(rpp
, rpp
+ 1, rrecs
* sizeof(*rpp
));
1352 xfs_bmbt_log_keys(cur
, rbp
, 1, rrecs
);
1353 xfs_bmbt_log_ptrs(cur
, rbp
, 1, rrecs
);
1355 memmove(rrp
, rrp
+ 1, rrecs
* sizeof(*rrp
));
1356 xfs_bmbt_log_recs(cur
, rbp
, 1, rrecs
);
1357 INT_SET(key
.br_startoff
, ARCH_CONVERT
,
1358 xfs_bmbt_disk_get_startoff(rrp
));
1361 if ((error
= xfs_bmbt_updkey(cur
, rkp
, level
+ 1))) {
1362 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1365 cur
->bc_ptrs
[level
]--;
1366 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1372 * Move 1 record right from cur/level if possible.
1373 * Update cur to reflect the new path.
1375 STATIC
int /* error */
1377 xfs_btree_cur_t
*cur
,
1379 int *stat
) /* success/failure */
1381 int error
; /* error return value */
1382 #ifdef XFS_BMBT_TRACE
1383 static char fname
[] = "xfs_bmbt_rshift";
1385 int i
; /* loop counter */
1386 xfs_bmbt_key_t key
; /* bmap btree key */
1387 xfs_buf_t
*lbp
; /* left buffer pointer */
1388 xfs_bmbt_block_t
*left
; /* left btree block */
1389 xfs_bmbt_key_t
*lkp
; /* left btree key */
1390 xfs_bmbt_ptr_t
*lpp
; /* left address pointer */
1391 xfs_bmbt_rec_t
*lrp
; /* left record pointer */
1392 xfs_mount_t
*mp
; /* file system mount point */
1393 xfs_buf_t
*rbp
; /* right buffer pointer */
1394 xfs_bmbt_block_t
*right
; /* right btree block */
1395 xfs_bmbt_key_t
*rkp
; /* right btree key */
1396 xfs_bmbt_ptr_t
*rpp
; /* right address pointer */
1397 xfs_bmbt_rec_t
*rrp
=NULL
; /* right record pointer */
1398 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
1400 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1401 XFS_BMBT_TRACE_ARGI(cur
, level
);
1402 if (level
== cur
->bc_nlevels
- 1) {
1403 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1407 lbp
= cur
->bc_bufs
[level
];
1408 left
= XFS_BUF_TO_BMBT_BLOCK(lbp
);
1410 if ((error
= xfs_btree_check_lblock(cur
, left
, level
, lbp
))) {
1411 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1415 if (be64_to_cpu(left
->bb_rightsib
) == NULLDFSBNO
) {
1416 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1420 if (cur
->bc_ptrs
[level
] >= be16_to_cpu(left
->bb_numrecs
)) {
1421 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1426 if ((error
= xfs_btree_read_bufl(mp
, cur
->bc_tp
, be64_to_cpu(left
->bb_rightsib
), 0,
1427 &rbp
, XFS_BMAP_BTREE_REF
))) {
1428 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1431 right
= XFS_BUF_TO_BMBT_BLOCK(rbp
);
1432 if ((error
= xfs_btree_check_lblock(cur
, right
, level
, rbp
))) {
1433 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1436 if (be16_to_cpu(right
->bb_numrecs
) == XFS_BMAP_BLOCK_IMAXRECS(level
, cur
)) {
1437 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1442 lkp
= XFS_BMAP_KEY_IADDR(left
, be16_to_cpu(left
->bb_numrecs
), cur
);
1443 lpp
= XFS_BMAP_PTR_IADDR(left
, be16_to_cpu(left
->bb_numrecs
), cur
);
1444 rkp
= XFS_BMAP_KEY_IADDR(right
, 1, cur
);
1445 rpp
= XFS_BMAP_PTR_IADDR(right
, 1, cur
);
1447 for (i
= be16_to_cpu(right
->bb_numrecs
) - 1; i
>= 0; i
--) {
1448 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(rpp
[i
], ARCH_CONVERT
), level
))) {
1449 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1454 memmove(rkp
+ 1, rkp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rkp
));
1455 memmove(rpp
+ 1, rpp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rpp
));
1457 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(*lpp
, ARCH_CONVERT
), level
))) {
1458 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1463 *rpp
= *lpp
; /* INT_: direct copy */
1464 xfs_bmbt_log_keys(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
) + 1);
1465 xfs_bmbt_log_ptrs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
) + 1);
1467 lrp
= XFS_BMAP_REC_IADDR(left
, be16_to_cpu(left
->bb_numrecs
), cur
);
1468 rrp
= XFS_BMAP_REC_IADDR(right
, 1, cur
);
1469 memmove(rrp
+ 1, rrp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rrp
));
1471 xfs_bmbt_log_recs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
) + 1);
1472 INT_SET(key
.br_startoff
, ARCH_CONVERT
,
1473 xfs_bmbt_disk_get_startoff(rrp
));
1476 be16_add(&left
->bb_numrecs
, -1);
1477 xfs_bmbt_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
1478 be16_add(&right
->bb_numrecs
, 1);
1481 xfs_btree_check_key(XFS_BTNUM_BMAP
, rkp
, rkp
+ 1);
1483 xfs_btree_check_rec(XFS_BTNUM_BMAP
, rrp
, rrp
+ 1);
1485 xfs_bmbt_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
1486 if ((error
= xfs_btree_dup_cursor(cur
, &tcur
))) {
1487 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1490 i
= xfs_btree_lastrec(tcur
, level
);
1491 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1492 if ((error
= xfs_bmbt_increment(tcur
, level
, &i
))) {
1493 XFS_BMBT_TRACE_CURSOR(tcur
, ERROR
);
1496 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1497 if ((error
= xfs_bmbt_updkey(tcur
, rkp
, level
+ 1))) {
1498 XFS_BMBT_TRACE_CURSOR(tcur
, ERROR
);
1501 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
1502 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1506 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1508 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
1513 * Determine the extent state.
1522 ASSERT(blks
!= 0); /* saved for DMIG */
1523 return XFS_EXT_UNWRITTEN
;
1525 return XFS_EXT_NORM
;
1530 * Split cur/level block in half.
1531 * Return new block number and its first record (to be inserted into parent).
1533 STATIC
int /* error */
1535 xfs_btree_cur_t
*cur
,
1537 xfs_fsblock_t
*bnop
,
1538 xfs_bmbt_key_t
*keyp
,
1539 xfs_btree_cur_t
**curp
,
1540 int *stat
) /* success/failure */
1542 xfs_alloc_arg_t args
; /* block allocation args */
1543 int error
; /* error return value */
1544 #ifdef XFS_BMBT_TRACE
1545 static char fname
[] = "xfs_bmbt_split";
1547 int i
; /* loop counter */
1548 xfs_fsblock_t lbno
; /* left sibling block number */
1549 xfs_buf_t
*lbp
; /* left buffer pointer */
1550 xfs_bmbt_block_t
*left
; /* left btree block */
1551 xfs_bmbt_key_t
*lkp
; /* left btree key */
1552 xfs_bmbt_ptr_t
*lpp
; /* left address pointer */
1553 xfs_bmbt_rec_t
*lrp
; /* left record pointer */
1554 xfs_buf_t
*rbp
; /* right buffer pointer */
1555 xfs_bmbt_block_t
*right
; /* right btree block */
1556 xfs_bmbt_key_t
*rkp
; /* right btree key */
1557 xfs_bmbt_ptr_t
*rpp
; /* right address pointer */
1558 xfs_bmbt_block_t
*rrblock
; /* right-right btree block */
1559 xfs_buf_t
*rrbp
; /* right-right buffer pointer */
1560 xfs_bmbt_rec_t
*rrp
; /* right record pointer */
1562 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1563 XFS_BMBT_TRACE_ARGIFK(cur
, level
, *bnop
, keyp
);
1564 args
.tp
= cur
->bc_tp
;
1565 args
.mp
= cur
->bc_mp
;
1566 lbp
= cur
->bc_bufs
[level
];
1567 lbno
= XFS_DADDR_TO_FSB(args
.mp
, XFS_BUF_ADDR(lbp
));
1568 left
= XFS_BUF_TO_BMBT_BLOCK(lbp
);
1569 args
.fsbno
= cur
->bc_private
.b
.firstblock
;
1570 args
.firstblock
= args
.fsbno
;
1571 if (args
.fsbno
== NULLFSBLOCK
) {
1573 args
.type
= XFS_ALLOCTYPE_START_BNO
;
1575 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
1576 args
.mod
= args
.minleft
= args
.alignment
= args
.total
= args
.isfl
=
1577 args
.userdata
= args
.minalignslop
= 0;
1578 args
.minlen
= args
.maxlen
= args
.prod
= 1;
1579 args
.wasdel
= cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
;
1580 if (!args
.wasdel
&& xfs_trans_get_block_res(args
.tp
) == 0) {
1581 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1582 return XFS_ERROR(ENOSPC
);
1584 if ((error
= xfs_alloc_vextent(&args
))) {
1585 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1588 if (args
.fsbno
== NULLFSBLOCK
) {
1589 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1593 ASSERT(args
.len
== 1);
1594 cur
->bc_private
.b
.firstblock
= args
.fsbno
;
1595 cur
->bc_private
.b
.allocated
++;
1596 cur
->bc_private
.b
.ip
->i_d
.di_nblocks
++;
1597 xfs_trans_log_inode(args
.tp
, cur
->bc_private
.b
.ip
, XFS_ILOG_CORE
);
1598 XFS_TRANS_MOD_DQUOT_BYINO(args
.mp
, args
.tp
, cur
->bc_private
.b
.ip
,
1599 XFS_TRANS_DQ_BCOUNT
, 1L);
1600 rbp
= xfs_btree_get_bufl(args
.mp
, args
.tp
, args
.fsbno
, 0);
1601 right
= XFS_BUF_TO_BMBT_BLOCK(rbp
);
1603 if ((error
= xfs_btree_check_lblock(cur
, left
, level
, rbp
))) {
1604 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1608 right
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
1609 right
->bb_level
= left
->bb_level
;
1610 right
->bb_numrecs
= cpu_to_be16(be16_to_cpu(left
->bb_numrecs
) / 2);
1611 if ((be16_to_cpu(left
->bb_numrecs
) & 1) &&
1612 cur
->bc_ptrs
[level
] <= be16_to_cpu(right
->bb_numrecs
) + 1)
1613 be16_add(&right
->bb_numrecs
, 1);
1614 i
= be16_to_cpu(left
->bb_numrecs
) - be16_to_cpu(right
->bb_numrecs
) + 1;
1616 lkp
= XFS_BMAP_KEY_IADDR(left
, i
, cur
);
1617 lpp
= XFS_BMAP_PTR_IADDR(left
, i
, cur
);
1618 rkp
= XFS_BMAP_KEY_IADDR(right
, 1, cur
);
1619 rpp
= XFS_BMAP_PTR_IADDR(right
, 1, cur
);
1621 for (i
= 0; i
< be16_to_cpu(right
->bb_numrecs
); i
++) {
1622 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(lpp
[i
], ARCH_CONVERT
), level
))) {
1623 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1628 memcpy(rkp
, lkp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rkp
));
1629 memcpy(rpp
, lpp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rpp
));
1630 xfs_bmbt_log_keys(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
1631 xfs_bmbt_log_ptrs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
1632 keyp
->br_startoff
= INT_GET(rkp
->br_startoff
, ARCH_CONVERT
);
1634 lrp
= XFS_BMAP_REC_IADDR(left
, i
, cur
);
1635 rrp
= XFS_BMAP_REC_IADDR(right
, 1, cur
);
1636 memcpy(rrp
, lrp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rrp
));
1637 xfs_bmbt_log_recs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
1638 keyp
->br_startoff
= xfs_bmbt_disk_get_startoff(rrp
);
1640 be16_add(&left
->bb_numrecs
, -(be16_to_cpu(right
->bb_numrecs
)));
1641 right
->bb_rightsib
= left
->bb_rightsib
;
1642 left
->bb_rightsib
= cpu_to_be64(args
.fsbno
);
1643 right
->bb_leftsib
= cpu_to_be64(lbno
);
1644 xfs_bmbt_log_block(cur
, rbp
, XFS_BB_ALL_BITS
);
1645 xfs_bmbt_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
1646 if (be64_to_cpu(right
->bb_rightsib
) != NULLDFSBNO
) {
1647 if ((error
= xfs_btree_read_bufl(args
.mp
, args
.tp
,
1648 be64_to_cpu(right
->bb_rightsib
), 0, &rrbp
,
1649 XFS_BMAP_BTREE_REF
))) {
1650 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1653 rrblock
= XFS_BUF_TO_BMBT_BLOCK(rrbp
);
1654 if ((error
= xfs_btree_check_lblock(cur
, rrblock
, level
, rrbp
))) {
1655 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1658 rrblock
->bb_leftsib
= cpu_to_be64(args
.fsbno
);
1659 xfs_bmbt_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
1661 if (cur
->bc_ptrs
[level
] > be16_to_cpu(left
->bb_numrecs
) + 1) {
1662 xfs_btree_setbuf(cur
, level
, rbp
);
1663 cur
->bc_ptrs
[level
] -= be16_to_cpu(left
->bb_numrecs
);
1665 if (level
+ 1 < cur
->bc_nlevels
) {
1666 if ((error
= xfs_btree_dup_cursor(cur
, curp
))) {
1667 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1670 (*curp
)->bc_ptrs
[level
+ 1]++;
1673 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1680 * Update keys for the record.
1684 xfs_btree_cur_t
*cur
,
1685 xfs_bmbt_key_t
*keyp
, /* on-disk format */
1688 xfs_bmbt_block_t
*block
;
1693 #ifdef XFS_BMBT_TRACE
1694 static char fname
[] = "xfs_bmbt_updkey";
1700 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1701 XFS_BMBT_TRACE_ARGIK(cur
, level
, keyp
);
1702 for (ptr
= 1; ptr
== 1 && level
< cur
->bc_nlevels
; level
++) {
1703 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
1705 if ((error
= xfs_btree_check_lblock(cur
, block
, level
, bp
))) {
1706 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1710 ptr
= cur
->bc_ptrs
[level
];
1711 kp
= XFS_BMAP_KEY_IADDR(block
, ptr
, cur
);
1713 xfs_bmbt_log_keys(cur
, bp
, ptr
, ptr
);
1715 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1720 * Convert on-disk form of btree root to in-memory form.
1724 xfs_bmdr_block_t
*dblock
,
1726 xfs_bmbt_block_t
*rblock
,
1730 xfs_bmbt_key_t
*fkp
;
1731 xfs_bmbt_ptr_t
*fpp
;
1732 xfs_bmbt_key_t
*tkp
;
1733 xfs_bmbt_ptr_t
*tpp
;
1735 rblock
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
1736 rblock
->bb_level
= dblock
->bb_level
;
1737 ASSERT(be16_to_cpu(rblock
->bb_level
) > 0);
1738 rblock
->bb_numrecs
= dblock
->bb_numrecs
;
1739 rblock
->bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
1740 rblock
->bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
1741 dmxr
= (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen
, xfs_bmdr
, 0);
1742 fkp
= XFS_BTREE_KEY_ADDR(dblocklen
, xfs_bmdr
, dblock
, 1, dmxr
);
1743 tkp
= XFS_BMAP_BROOT_KEY_ADDR(rblock
, 1, rblocklen
);
1744 fpp
= XFS_BTREE_PTR_ADDR(dblocklen
, xfs_bmdr
, dblock
, 1, dmxr
);
1745 tpp
= XFS_BMAP_BROOT_PTR_ADDR(rblock
, 1, rblocklen
);
1746 dmxr
= be16_to_cpu(dblock
->bb_numrecs
);
1747 memcpy(tkp
, fkp
, sizeof(*fkp
) * dmxr
);
1748 memcpy(tpp
, fpp
, sizeof(*fpp
) * dmxr
); /* INT_: direct copy */
1752 * Decrement cursor by one record at the level.
1753 * For nonzero levels the leaf-ward information is untouched.
1757 xfs_btree_cur_t
*cur
,
1759 int *stat
) /* success/failure */
1761 xfs_bmbt_block_t
*block
;
1763 int error
; /* error return value */
1764 #ifdef XFS_BMBT_TRACE
1765 static char fname
[] = "xfs_bmbt_decrement";
1767 xfs_fsblock_t fsbno
;
1772 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1773 XFS_BMBT_TRACE_ARGI(cur
, level
);
1774 ASSERT(level
< cur
->bc_nlevels
);
1775 if (level
< cur
->bc_nlevels
- 1)
1776 xfs_btree_readahead(cur
, level
, XFS_BTCUR_LEFTRA
);
1777 if (--cur
->bc_ptrs
[level
] > 0) {
1778 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1782 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
1784 if ((error
= xfs_btree_check_lblock(cur
, block
, level
, bp
))) {
1785 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1789 if (be64_to_cpu(block
->bb_leftsib
) == NULLDFSBNO
) {
1790 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1794 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1795 if (--cur
->bc_ptrs
[lev
] > 0)
1797 if (lev
< cur
->bc_nlevels
- 1)
1798 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_LEFTRA
);
1800 if (lev
== cur
->bc_nlevels
) {
1801 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1807 for (block
= xfs_bmbt_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1808 fsbno
= INT_GET(*XFS_BMAP_PTR_IADDR(block
, cur
->bc_ptrs
[lev
], cur
), ARCH_CONVERT
);
1809 if ((error
= xfs_btree_read_bufl(mp
, tp
, fsbno
, 0, &bp
,
1810 XFS_BMAP_BTREE_REF
))) {
1811 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1815 xfs_btree_setbuf(cur
, lev
, bp
);
1816 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
1817 if ((error
= xfs_btree_check_lblock(cur
, block
, lev
, bp
))) {
1818 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1821 cur
->bc_ptrs
[lev
] = be16_to_cpu(block
->bb_numrecs
);
1823 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1829 * Delete the record pointed to by cur.
1833 xfs_btree_cur_t
*cur
,
1834 int *stat
) /* success/failure */
1836 int error
; /* error return value */
1837 #ifdef XFS_BMBT_TRACE
1838 static char fname
[] = "xfs_bmbt_delete";
1843 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1844 for (level
= 0, i
= 2; i
== 2; level
++) {
1845 if ((error
= xfs_bmbt_delrec(cur
, level
, &i
))) {
1846 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1851 for (level
= 1; level
< cur
->bc_nlevels
; level
++) {
1852 if (cur
->bc_ptrs
[level
] == 0) {
1853 if ((error
= xfs_bmbt_decrement(cur
, level
,
1855 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1862 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1868 * Convert a compressed bmap extent record to an uncompressed form.
1869 * This code must be in sync with the routines xfs_bmbt_get_startoff,
1870 * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state.
1873 STATIC __inline__
void
1882 ext_flag
= (int)(l0
>> (64 - BMBT_EXNTFLAG_BITLEN
));
1883 s
->br_startoff
= ((xfs_fileoff_t
)l0
&
1884 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
1886 s
->br_startblock
= (((xfs_fsblock_t
)l0
& XFS_MASK64LO(9)) << 43) |
1887 (((xfs_fsblock_t
)l1
) >> 21);
1893 b
= (((xfs_dfsbno_t
)l0
& XFS_MASK64LO(9)) << 43) |
1894 (((xfs_dfsbno_t
)l1
) >> 21);
1895 ASSERT((b
>> 32) == 0 || ISNULLDSTARTBLOCK(b
));
1896 s
->br_startblock
= (xfs_fsblock_t
)b
;
1899 s
->br_startblock
= (xfs_fsblock_t
)(((xfs_dfsbno_t
)l1
) >> 21);
1901 #endif /* XFS_BIG_BLKNOS */
1902 s
->br_blockcount
= (xfs_filblks_t
)(l1
& XFS_MASK64LO(21));
1903 /* This is xfs_extent_state() in-line */
1905 ASSERT(s
->br_blockcount
!= 0); /* saved for DMIG */
1906 st
= XFS_EXT_UNWRITTEN
;
1917 __xfs_bmbt_get_all(r
->l0
, r
->l1
, s
);
1921 * Get the block pointer for the given level of the cursor.
1922 * Fill in the buffer pointer, if applicable.
1926 xfs_btree_cur_t
*cur
,
1931 xfs_bmbt_block_t
*rval
;
1933 if (level
< cur
->bc_nlevels
- 1) {
1934 *bpp
= cur
->bc_bufs
[level
];
1935 rval
= XFS_BUF_TO_BMBT_BLOCK(*bpp
);
1938 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
,
1939 cur
->bc_private
.b
.whichfork
);
1940 rval
= ifp
->if_broot
;
1946 * Extract the blockcount field from an in memory bmap extent record.
1949 xfs_bmbt_get_blockcount(
1952 return (xfs_filblks_t
)(r
->l1
& XFS_MASK64LO(21));
1956 * Extract the startblock field from an in memory bmap extent record.
1959 xfs_bmbt_get_startblock(
1963 return (((xfs_fsblock_t
)r
->l0
& XFS_MASK64LO(9)) << 43) |
1964 (((xfs_fsblock_t
)r
->l1
) >> 21);
1969 b
= (((xfs_dfsbno_t
)r
->l0
& XFS_MASK64LO(9)) << 43) |
1970 (((xfs_dfsbno_t
)r
->l1
) >> 21);
1971 ASSERT((b
>> 32) == 0 || ISNULLDSTARTBLOCK(b
));
1972 return (xfs_fsblock_t
)b
;
1974 return (xfs_fsblock_t
)(((xfs_dfsbno_t
)r
->l1
) >> 21);
1976 #endif /* XFS_BIG_BLKNOS */
1980 * Extract the startoff field from an in memory bmap extent record.
1983 xfs_bmbt_get_startoff(
1986 return ((xfs_fileoff_t
)r
->l0
&
1987 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
1996 ext_flag
= (int)((r
->l0
) >> (64 - BMBT_EXNTFLAG_BITLEN
));
1997 return xfs_extent_state(xfs_bmbt_get_blockcount(r
),
2001 #ifndef XFS_NATIVE_HOST
2002 /* Endian flipping versions of the bmbt extraction functions */
2004 xfs_bmbt_disk_get_all(
2010 l0
= INT_GET(r
->l0
, ARCH_CONVERT
);
2011 l1
= INT_GET(r
->l1
, ARCH_CONVERT
);
2013 __xfs_bmbt_get_all(l0
, l1
, s
);
2017 * Extract the blockcount field from an on disk bmap extent record.
2020 xfs_bmbt_disk_get_blockcount(
2023 return (xfs_filblks_t
)(INT_GET(r
->l1
, ARCH_CONVERT
) & XFS_MASK64LO(21));
2027 * Extract the startblock field from an on disk bmap extent record.
2030 xfs_bmbt_disk_get_startblock(
2034 return (((xfs_fsblock_t
)INT_GET(r
->l0
, ARCH_CONVERT
) & XFS_MASK64LO(9)) << 43) |
2035 (((xfs_fsblock_t
)INT_GET(r
->l1
, ARCH_CONVERT
)) >> 21);
2040 b
= (((xfs_dfsbno_t
)INT_GET(r
->l0
, ARCH_CONVERT
) & XFS_MASK64LO(9)) << 43) |
2041 (((xfs_dfsbno_t
)INT_GET(r
->l1
, ARCH_CONVERT
)) >> 21);
2042 ASSERT((b
>> 32) == 0 || ISNULLDSTARTBLOCK(b
));
2043 return (xfs_fsblock_t
)b
;
2045 return (xfs_fsblock_t
)(((xfs_dfsbno_t
)INT_GET(r
->l1
, ARCH_CONVERT
)) >> 21);
2047 #endif /* XFS_BIG_BLKNOS */
2051 * Extract the startoff field from a disk format bmap extent record.
2054 xfs_bmbt_disk_get_startoff(
2057 return ((xfs_fileoff_t
)INT_GET(r
->l0
, ARCH_CONVERT
) &
2058 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
2062 xfs_bmbt_disk_get_state(
2067 ext_flag
= (int)((INT_GET(r
->l0
, ARCH_CONVERT
)) >> (64 - BMBT_EXNTFLAG_BITLEN
));
2068 return xfs_extent_state(xfs_bmbt_disk_get_blockcount(r
),
2071 #endif /* XFS_NATIVE_HOST */
2075 * Increment cursor by one record at the level.
2076 * For nonzero levels the leaf-ward information is untouched.
2080 xfs_btree_cur_t
*cur
,
2082 int *stat
) /* success/failure */
2084 xfs_bmbt_block_t
*block
;
2086 int error
; /* error return value */
2087 #ifdef XFS_BMBT_TRACE
2088 static char fname
[] = "xfs_bmbt_increment";
2090 xfs_fsblock_t fsbno
;
2095 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2096 XFS_BMBT_TRACE_ARGI(cur
, level
);
2097 ASSERT(level
< cur
->bc_nlevels
);
2098 if (level
< cur
->bc_nlevels
- 1)
2099 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
2100 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
2102 if ((error
= xfs_btree_check_lblock(cur
, block
, level
, bp
))) {
2103 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2107 if (++cur
->bc_ptrs
[level
] <= be16_to_cpu(block
->bb_numrecs
)) {
2108 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2112 if (be64_to_cpu(block
->bb_rightsib
) == NULLDFSBNO
) {
2113 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2117 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
2118 block
= xfs_bmbt_get_block(cur
, lev
, &bp
);
2120 if ((error
= xfs_btree_check_lblock(cur
, block
, lev
, bp
))) {
2121 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2125 if (++cur
->bc_ptrs
[lev
] <= be16_to_cpu(block
->bb_numrecs
))
2127 if (lev
< cur
->bc_nlevels
- 1)
2128 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_RIGHTRA
);
2130 if (lev
== cur
->bc_nlevels
) {
2131 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2137 for (block
= xfs_bmbt_get_block(cur
, lev
, &bp
); lev
> level
; ) {
2138 fsbno
= INT_GET(*XFS_BMAP_PTR_IADDR(block
, cur
->bc_ptrs
[lev
], cur
), ARCH_CONVERT
);
2139 if ((error
= xfs_btree_read_bufl(mp
, tp
, fsbno
, 0, &bp
,
2140 XFS_BMAP_BTREE_REF
))) {
2141 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2145 xfs_btree_setbuf(cur
, lev
, bp
);
2146 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
2147 if ((error
= xfs_btree_check_lblock(cur
, block
, lev
, bp
))) {
2148 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2151 cur
->bc_ptrs
[lev
] = 1;
2153 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2159 * Insert the current record at the point referenced by cur.
2163 xfs_btree_cur_t
*cur
,
2164 int *stat
) /* success/failure */
2166 int error
; /* error return value */
2167 #ifdef XFS_BMBT_TRACE
2168 static char fname
[] = "xfs_bmbt_insert";
2173 xfs_btree_cur_t
*ncur
;
2174 xfs_bmbt_rec_t nrec
;
2175 xfs_btree_cur_t
*pcur
;
2177 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2180 xfs_bmbt_disk_set_all(&nrec
, &cur
->bc_rec
.b
);
2181 ncur
= (xfs_btree_cur_t
*)0;
2184 if ((error
= xfs_bmbt_insrec(pcur
, level
++, &nbno
, &nrec
, &ncur
,
2187 xfs_btree_del_cursor(pcur
, XFS_BTREE_ERROR
);
2188 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2191 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
2192 if (pcur
!= cur
&& (ncur
|| nbno
== NULLFSBLOCK
)) {
2193 cur
->bc_nlevels
= pcur
->bc_nlevels
;
2194 cur
->bc_private
.b
.allocated
+=
2195 pcur
->bc_private
.b
.allocated
;
2196 pcur
->bc_private
.b
.allocated
= 0;
2197 ASSERT((cur
->bc_private
.b
.firstblock
!= NULLFSBLOCK
) ||
2198 (cur
->bc_private
.b
.ip
->i_d
.di_flags
&
2199 XFS_DIFLAG_REALTIME
));
2200 cur
->bc_private
.b
.firstblock
=
2201 pcur
->bc_private
.b
.firstblock
;
2202 ASSERT(cur
->bc_private
.b
.flist
==
2203 pcur
->bc_private
.b
.flist
);
2204 xfs_btree_del_cursor(pcur
, XFS_BTREE_NOERROR
);
2208 ncur
= (xfs_btree_cur_t
*)0;
2210 } while (nbno
!= NULLFSBLOCK
);
2211 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2215 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2220 * Log fields from the btree block header.
2224 xfs_btree_cur_t
*cur
,
2229 #ifdef XFS_BMBT_TRACE
2230 static char fname
[] = "xfs_bmbt_log_block";
2234 static const short offsets
[] = {
2235 offsetof(xfs_bmbt_block_t
, bb_magic
),
2236 offsetof(xfs_bmbt_block_t
, bb_level
),
2237 offsetof(xfs_bmbt_block_t
, bb_numrecs
),
2238 offsetof(xfs_bmbt_block_t
, bb_leftsib
),
2239 offsetof(xfs_bmbt_block_t
, bb_rightsib
),
2240 sizeof(xfs_bmbt_block_t
)
2243 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2244 XFS_BMBT_TRACE_ARGBI(cur
, bp
, fields
);
2247 xfs_btree_offsets(fields
, offsets
, XFS_BB_NUM_BITS
, &first
,
2249 xfs_trans_log_buf(tp
, bp
, first
, last
);
2251 xfs_trans_log_inode(tp
, cur
->bc_private
.b
.ip
,
2252 XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
));
2253 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2257 * Log record values from the btree block.
2261 xfs_btree_cur_t
*cur
,
2266 xfs_bmbt_block_t
*block
;
2268 #ifdef XFS_BMBT_TRACE
2269 static char fname
[] = "xfs_bmbt_log_recs";
2275 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2276 XFS_BMBT_TRACE_ARGBII(cur
, bp
, rfirst
, rlast
);
2279 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
2280 rp
= XFS_BMAP_REC_DADDR(block
, 1, cur
);
2281 first
= (int)((xfs_caddr_t
)&rp
[rfirst
- 1] - (xfs_caddr_t
)block
);
2282 last
= (int)(((xfs_caddr_t
)&rp
[rlast
] - 1) - (xfs_caddr_t
)block
);
2283 xfs_trans_log_buf(tp
, bp
, first
, last
);
2284 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2289 xfs_btree_cur_t
*cur
,
2293 int *stat
) /* success/failure */
2295 cur
->bc_rec
.b
.br_startoff
= off
;
2296 cur
->bc_rec
.b
.br_startblock
= bno
;
2297 cur
->bc_rec
.b
.br_blockcount
= len
;
2298 return xfs_bmbt_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
2303 xfs_btree_cur_t
*cur
,
2307 int *stat
) /* success/failure */
2309 cur
->bc_rec
.b
.br_startoff
= off
;
2310 cur
->bc_rec
.b
.br_startblock
= bno
;
2311 cur
->bc_rec
.b
.br_blockcount
= len
;
2312 return xfs_bmbt_lookup(cur
, XFS_LOOKUP_GE
, stat
);
2316 * Give the bmap btree a new root block. Copy the old broot contents
2317 * down into a real block and make the broot point to it.
2321 xfs_btree_cur_t
*cur
, /* btree cursor */
2322 int *logflags
, /* logging flags for inode */
2323 int *stat
) /* return status - 0 fail */
2325 xfs_alloc_arg_t args
; /* allocation arguments */
2326 xfs_bmbt_block_t
*block
; /* bmap btree block */
2327 xfs_buf_t
*bp
; /* buffer for block */
2328 xfs_bmbt_block_t
*cblock
; /* child btree block */
2329 xfs_bmbt_key_t
*ckp
; /* child key pointer */
2330 xfs_bmbt_ptr_t
*cpp
; /* child ptr pointer */
2331 int error
; /* error return code */
2332 #ifdef XFS_BMBT_TRACE
2333 static char fname
[] = "xfs_bmbt_newroot";
2336 int i
; /* loop counter */
2338 xfs_bmbt_key_t
*kp
; /* pointer to bmap btree key */
2339 int level
; /* btree level */
2340 xfs_bmbt_ptr_t
*pp
; /* pointer to bmap block addr */
2342 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2343 level
= cur
->bc_nlevels
- 1;
2344 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
2346 * Copy the root into a real block.
2348 args
.mp
= cur
->bc_mp
;
2349 pp
= XFS_BMAP_PTR_IADDR(block
, 1, cur
);
2350 args
.tp
= cur
->bc_tp
;
2351 args
.fsbno
= cur
->bc_private
.b
.firstblock
;
2352 args
.mod
= args
.minleft
= args
.alignment
= args
.total
= args
.isfl
=
2353 args
.userdata
= args
.minalignslop
= 0;
2354 args
.minlen
= args
.maxlen
= args
.prod
= 1;
2355 args
.wasdel
= cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
;
2356 args
.firstblock
= args
.fsbno
;
2357 if (args
.fsbno
== NULLFSBLOCK
) {
2359 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(*pp
, ARCH_CONVERT
), level
))) {
2360 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2364 args
.fsbno
= INT_GET(*pp
, ARCH_CONVERT
);
2365 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2367 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
2368 if ((error
= xfs_alloc_vextent(&args
))) {
2369 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2372 if (args
.fsbno
== NULLFSBLOCK
) {
2373 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2377 ASSERT(args
.len
== 1);
2378 cur
->bc_private
.b
.firstblock
= args
.fsbno
;
2379 cur
->bc_private
.b
.allocated
++;
2380 cur
->bc_private
.b
.ip
->i_d
.di_nblocks
++;
2381 XFS_TRANS_MOD_DQUOT_BYINO(args
.mp
, args
.tp
, cur
->bc_private
.b
.ip
,
2382 XFS_TRANS_DQ_BCOUNT
, 1L);
2383 bp
= xfs_btree_get_bufl(args
.mp
, cur
->bc_tp
, args
.fsbno
, 0);
2384 cblock
= XFS_BUF_TO_BMBT_BLOCK(bp
);
2386 be16_add(&block
->bb_level
, 1);
2387 block
->bb_numrecs
= cpu_to_be16(1);
2389 cur
->bc_ptrs
[level
+ 1] = 1;
2390 kp
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
2391 ckp
= XFS_BMAP_KEY_IADDR(cblock
, 1, cur
);
2392 memcpy(ckp
, kp
, be16_to_cpu(cblock
->bb_numrecs
) * sizeof(*kp
));
2393 cpp
= XFS_BMAP_PTR_IADDR(cblock
, 1, cur
);
2395 for (i
= 0; i
< be16_to_cpu(cblock
->bb_numrecs
); i
++) {
2396 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(pp
[i
], ARCH_CONVERT
), level
))) {
2397 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2402 memcpy(cpp
, pp
, be16_to_cpu(cblock
->bb_numrecs
) * sizeof(*pp
));
2404 if ((error
= xfs_btree_check_lptr(cur
, (xfs_bmbt_ptr_t
)args
.fsbno
,
2406 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2410 INT_SET(*pp
, ARCH_CONVERT
, args
.fsbno
);
2411 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, 1 - be16_to_cpu(cblock
->bb_numrecs
),
2412 cur
->bc_private
.b
.whichfork
);
2413 xfs_btree_setbuf(cur
, level
, bp
);
2415 * Do all this logging at the end so that
2416 * the root is at the right level.
2418 xfs_bmbt_log_block(cur
, bp
, XFS_BB_ALL_BITS
);
2419 xfs_bmbt_log_keys(cur
, bp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2420 xfs_bmbt_log_ptrs(cur
, bp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2421 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2423 XFS_ILOG_CORE
| XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
);
2429 * Set all the fields in a bmap extent record from the uncompressed form.
2438 ASSERT((s
->br_state
== XFS_EXT_NORM
) ||
2439 (s
->br_state
== XFS_EXT_UNWRITTEN
));
2440 extent_flag
= (s
->br_state
== XFS_EXT_NORM
) ? 0 : 1;
2441 ASSERT((s
->br_startoff
& XFS_MASK64HI(9)) == 0);
2442 ASSERT((s
->br_blockcount
& XFS_MASK64HI(43)) == 0);
2444 ASSERT((s
->br_startblock
& XFS_MASK64HI(12)) == 0);
2445 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2446 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9) |
2447 ((xfs_bmbt_rec_base_t
)s
->br_startblock
>> 43);
2448 r
->l1
= ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2449 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2450 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2451 #else /* !XFS_BIG_BLKNOS */
2452 if (ISNULLSTARTBLOCK(s
->br_startblock
)) {
2453 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2454 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9) |
2455 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9);
2456 r
->l1
= XFS_MASK64HI(11) |
2457 ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2458 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2459 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2461 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2462 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9);
2463 r
->l1
= ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2464 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2465 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2467 #endif /* XFS_BIG_BLKNOS */
2471 * Set all the fields in a bmap extent record from the arguments.
2483 ASSERT((v
== XFS_EXT_NORM
) || (v
== XFS_EXT_UNWRITTEN
));
2484 extent_flag
= (v
== XFS_EXT_NORM
) ? 0 : 1;
2485 ASSERT((o
& XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN
)) == 0);
2486 ASSERT((c
& XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN
)) == 0);
2488 ASSERT((b
& XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN
)) == 0);
2489 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2490 ((xfs_bmbt_rec_base_t
)o
<< 9) |
2491 ((xfs_bmbt_rec_base_t
)b
>> 43);
2492 r
->l1
= ((xfs_bmbt_rec_base_t
)b
<< 21) |
2493 ((xfs_bmbt_rec_base_t
)c
&
2494 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2495 #else /* !XFS_BIG_BLKNOS */
2496 if (ISNULLSTARTBLOCK(b
)) {
2497 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2498 ((xfs_bmbt_rec_base_t
)o
<< 9) |
2499 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9);
2500 r
->l1
= XFS_MASK64HI(11) |
2501 ((xfs_bmbt_rec_base_t
)b
<< 21) |
2502 ((xfs_bmbt_rec_base_t
)c
&
2503 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2505 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2506 ((xfs_bmbt_rec_base_t
)o
<< 9);
2507 r
->l1
= ((xfs_bmbt_rec_base_t
)b
<< 21) |
2508 ((xfs_bmbt_rec_base_t
)c
&
2509 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2511 #endif /* XFS_BIG_BLKNOS */
2514 #ifndef XFS_NATIVE_HOST
2516 * Set all the fields in a bmap extent record from the uncompressed form.
2519 xfs_bmbt_disk_set_all(
2525 ASSERT((s
->br_state
== XFS_EXT_NORM
) ||
2526 (s
->br_state
== XFS_EXT_UNWRITTEN
));
2527 extent_flag
= (s
->br_state
== XFS_EXT_NORM
) ? 0 : 1;
2528 ASSERT((s
->br_startoff
& XFS_MASK64HI(9)) == 0);
2529 ASSERT((s
->br_blockcount
& XFS_MASK64HI(43)) == 0);
2531 ASSERT((s
->br_startblock
& XFS_MASK64HI(12)) == 0);
2532 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2533 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9) |
2534 ((xfs_bmbt_rec_base_t
)s
->br_startblock
>> 43));
2535 INT_SET(r
->l1
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2536 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2537 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2538 #else /* !XFS_BIG_BLKNOS */
2539 if (ISNULLSTARTBLOCK(s
->br_startblock
)) {
2540 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2541 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9) |
2542 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9));
2543 INT_SET(r
->l1
, ARCH_CONVERT
, XFS_MASK64HI(11) |
2544 ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2545 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2546 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2548 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2549 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9));
2550 INT_SET(r
->l1
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2551 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2552 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2554 #endif /* XFS_BIG_BLKNOS */
2558 * Set all the fields in a disk format bmap extent record from the arguments.
2561 xfs_bmbt_disk_set_allf(
2570 ASSERT((v
== XFS_EXT_NORM
) || (v
== XFS_EXT_UNWRITTEN
));
2571 extent_flag
= (v
== XFS_EXT_NORM
) ? 0 : 1;
2572 ASSERT((o
& XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN
)) == 0);
2573 ASSERT((c
& XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN
)) == 0);
2575 ASSERT((b
& XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN
)) == 0);
2576 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2577 ((xfs_bmbt_rec_base_t
)o
<< 9) |
2578 ((xfs_bmbt_rec_base_t
)b
>> 43));
2579 INT_SET(r
->l1
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)b
<< 21) |
2580 ((xfs_bmbt_rec_base_t
)c
&
2581 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2582 #else /* !XFS_BIG_BLKNOS */
2583 if (ISNULLSTARTBLOCK(b
)) {
2584 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2585 ((xfs_bmbt_rec_base_t
)o
<< 9) |
2586 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9));
2587 INT_SET(r
->l1
, ARCH_CONVERT
, XFS_MASK64HI(11) |
2588 ((xfs_bmbt_rec_base_t
)b
<< 21) |
2589 ((xfs_bmbt_rec_base_t
)c
&
2590 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2592 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2593 ((xfs_bmbt_rec_base_t
)o
<< 9));
2594 INT_SET(r
->l1
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)b
<< 21) |
2595 ((xfs_bmbt_rec_base_t
)c
&
2596 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2598 #endif /* XFS_BIG_BLKNOS */
2600 #endif /* XFS_NATIVE_HOST */
2603 * Set the blockcount field in a bmap extent record.
2606 xfs_bmbt_set_blockcount(
2610 ASSERT((v
& XFS_MASK64HI(43)) == 0);
2611 r
->l1
= (r
->l1
& (xfs_bmbt_rec_base_t
)XFS_MASK64HI(43)) |
2612 (xfs_bmbt_rec_base_t
)(v
& XFS_MASK64LO(21));
2616 * Set the startblock field in a bmap extent record.
2619 xfs_bmbt_set_startblock(
2624 ASSERT((v
& XFS_MASK64HI(12)) == 0);
2625 r
->l0
= (r
->l0
& (xfs_bmbt_rec_base_t
)XFS_MASK64HI(55)) |
2626 (xfs_bmbt_rec_base_t
)(v
>> 43);
2627 r
->l1
= (r
->l1
& (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)) |
2628 (xfs_bmbt_rec_base_t
)(v
<< 21);
2629 #else /* !XFS_BIG_BLKNOS */
2630 if (ISNULLSTARTBLOCK(v
)) {
2631 r
->l0
|= (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9);
2632 r
->l1
= (xfs_bmbt_rec_base_t
)XFS_MASK64HI(11) |
2633 ((xfs_bmbt_rec_base_t
)v
<< 21) |
2634 (r
->l1
& (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2636 r
->l0
&= ~(xfs_bmbt_rec_base_t
)XFS_MASK64LO(9);
2637 r
->l1
= ((xfs_bmbt_rec_base_t
)v
<< 21) |
2638 (r
->l1
& (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2640 #endif /* XFS_BIG_BLKNOS */
2644 * Set the startoff field in a bmap extent record.
2647 xfs_bmbt_set_startoff(
2651 ASSERT((v
& XFS_MASK64HI(9)) == 0);
2652 r
->l0
= (r
->l0
& (xfs_bmbt_rec_base_t
) XFS_MASK64HI(1)) |
2653 ((xfs_bmbt_rec_base_t
)v
<< 9) |
2654 (r
->l0
& (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9));
2658 * Set the extent state field in a bmap extent record.
2665 ASSERT(v
== XFS_EXT_NORM
|| v
== XFS_EXT_UNWRITTEN
);
2666 if (v
== XFS_EXT_NORM
)
2667 r
->l0
&= XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN
);
2669 r
->l0
|= XFS_MASK64HI(BMBT_EXNTFLAG_BITLEN
);
2673 * Convert in-memory form of btree root to on-disk form.
2677 xfs_bmbt_block_t
*rblock
,
2679 xfs_bmdr_block_t
*dblock
,
2683 xfs_bmbt_key_t
*fkp
;
2684 xfs_bmbt_ptr_t
*fpp
;
2685 xfs_bmbt_key_t
*tkp
;
2686 xfs_bmbt_ptr_t
*tpp
;
2688 ASSERT(be32_to_cpu(rblock
->bb_magic
) == XFS_BMAP_MAGIC
);
2689 ASSERT(be64_to_cpu(rblock
->bb_leftsib
) == NULLDFSBNO
);
2690 ASSERT(be64_to_cpu(rblock
->bb_rightsib
) == NULLDFSBNO
);
2691 ASSERT(be16_to_cpu(rblock
->bb_level
) > 0);
2692 dblock
->bb_level
= rblock
->bb_level
;
2693 dblock
->bb_numrecs
= rblock
->bb_numrecs
;
2694 dmxr
= (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen
, xfs_bmdr
, 0);
2695 fkp
= XFS_BMAP_BROOT_KEY_ADDR(rblock
, 1, rblocklen
);
2696 tkp
= XFS_BTREE_KEY_ADDR(dblocklen
, xfs_bmdr
, dblock
, 1, dmxr
);
2697 fpp
= XFS_BMAP_BROOT_PTR_ADDR(rblock
, 1, rblocklen
);
2698 tpp
= XFS_BTREE_PTR_ADDR(dblocklen
, xfs_bmdr
, dblock
, 1, dmxr
);
2699 dmxr
= be16_to_cpu(dblock
->bb_numrecs
);
2700 memcpy(tkp
, fkp
, sizeof(*fkp
) * dmxr
);
2701 memcpy(tpp
, fpp
, sizeof(*fpp
) * dmxr
); /* INT_: direct copy */
2705 * Update the record to the passed values.
2709 xfs_btree_cur_t
*cur
,
2715 xfs_bmbt_block_t
*block
;
2718 #ifdef XFS_BMBT_TRACE
2719 static char fname
[] = "xfs_bmbt_update";
2725 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2726 XFS_BMBT_TRACE_ARGFFFI(cur
, (xfs_dfiloff_t
)off
, (xfs_dfsbno_t
)bno
,
2727 (xfs_dfilblks_t
)len
, (int)state
);
2728 block
= xfs_bmbt_get_block(cur
, 0, &bp
);
2730 if ((error
= xfs_btree_check_lblock(cur
, block
, 0, bp
))) {
2731 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2735 ptr
= cur
->bc_ptrs
[0];
2736 rp
= XFS_BMAP_REC_IADDR(block
, ptr
, cur
);
2737 xfs_bmbt_disk_set_allf(rp
, off
, bno
, len
, state
);
2738 xfs_bmbt_log_recs(cur
, bp
, ptr
, ptr
);
2740 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2743 INT_SET(key
.br_startoff
, ARCH_CONVERT
, off
);
2744 if ((error
= xfs_bmbt_updkey(cur
, &key
, 1))) {
2745 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2748 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2753 * Check extent records, which have just been read, for
2754 * any bit in the extent flag field. ASSERT on debug
2755 * kernels, as this condition should not occur.
2756 * Return an error condition (1) if any flags found,
2757 * otherwise return 0.
2761 xfs_check_nostate_extents(
2768 for (; num
> 0; num
--, idx
++) {
2769 ep
= xfs_iext_get_ext(ifp
, idx
);
2771 (64 - BMBT_EXNTFLAG_BITLEN
)) != 0) {