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"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir_sf.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_inode_item.h"
40 #include "xfs_alloc.h"
41 #include "xfs_btree.h"
42 #include "xfs_ialloc.h"
43 #include "xfs_itable.h"
45 #include "xfs_error.h"
46 #include "xfs_quota.h"
48 #if defined(XFS_BMBT_TRACE)
49 ktrace_t
*xfs_bmbt_trace_buf
;
53 * Prototypes for internal btree functions.
57 STATIC
int xfs_bmbt_killroot(xfs_btree_cur_t
*);
58 STATIC
void xfs_bmbt_log_keys(xfs_btree_cur_t
*, xfs_buf_t
*, int, int);
59 STATIC
void xfs_bmbt_log_ptrs(xfs_btree_cur_t
*, xfs_buf_t
*, int, int);
60 STATIC
int xfs_bmbt_lshift(xfs_btree_cur_t
*, int, int *);
61 STATIC
int xfs_bmbt_rshift(xfs_btree_cur_t
*, int, int *);
62 STATIC
int xfs_bmbt_split(xfs_btree_cur_t
*, int, xfs_fsblock_t
*,
63 xfs_bmbt_key_t
*, xfs_btree_cur_t
**, int *);
64 STATIC
int xfs_bmbt_updkey(xfs_btree_cur_t
*, xfs_bmbt_key_t
*, int);
67 #if defined(XFS_BMBT_TRACE)
69 static char ARGS
[] = "args";
70 static char ENTRY
[] = "entry";
71 static char ERROR
[] = "error";
73 static char EXIT
[] = "exit";
76 * Add a trace buffer entry for the arguments given to the routine,
101 ip
= cur
->bc_private
.b
.ip
;
102 whichfork
= cur
->bc_private
.b
.whichfork
;
103 ktrace_enter(xfs_bmbt_trace_buf
,
104 (void *)((__psint_t
)type
| (whichfork
<< 8) | (line
<< 16)),
105 (void *)func
, (void *)s
, (void *)ip
, (void *)cur
,
106 (void *)a0
, (void *)a1
, (void *)a2
, (void *)a3
,
107 (void *)a4
, (void *)a5
, (void *)a6
, (void *)a7
,
108 (void *)a8
, (void *)a9
, (void *)a10
);
109 ASSERT(ip
->i_btrace
);
110 ktrace_enter(ip
->i_btrace
,
111 (void *)((__psint_t
)type
| (whichfork
<< 8) | (line
<< 16)),
112 (void *)func
, (void *)s
, (void *)ip
, (void *)cur
,
113 (void *)a0
, (void *)a1
, (void *)a2
, (void *)a3
,
114 (void *)a4
, (void *)a5
, (void *)a6
, (void *)a7
,
115 (void *)a8
, (void *)a9
, (void *)a10
);
118 * Add a trace buffer entry for arguments, for a buffer & 1 integer arg.
121 xfs_bmbt_trace_argbi(
123 xfs_btree_cur_t
*cur
,
128 xfs_bmbt_trace_enter(func
, cur
, ARGS
, XFS_BMBT_KTRACE_ARGBI
, line
,
129 (__psunsigned_t
)b
, i
, 0, 0,
135 * Add a trace buffer entry for arguments, for a buffer & 2 integer args.
138 xfs_bmbt_trace_argbii(
140 xfs_btree_cur_t
*cur
,
146 xfs_bmbt_trace_enter(func
, cur
, ARGS
, XFS_BMBT_KTRACE_ARGBII
, line
,
147 (__psunsigned_t
)b
, i0
, i1
, 0,
153 * Add a trace buffer entry for arguments, for 3 block-length args
154 * and an integer arg.
157 xfs_bmbt_trace_argfffi(
159 xfs_btree_cur_t
*cur
,
166 xfs_bmbt_trace_enter(func
, cur
, ARGS
, XFS_BMBT_KTRACE_ARGFFFI
, line
,
167 o
>> 32, (int)o
, b
>> 32, (int)b
,
168 i
>> 32, (int)i
, (int)j
, 0,
173 * Add a trace buffer entry for arguments, for one integer arg.
178 xfs_btree_cur_t
*cur
,
182 xfs_bmbt_trace_enter(func
, cur
, ARGS
, XFS_BMBT_KTRACE_ARGI
, line
,
189 * Add a trace buffer entry for arguments, for int, fsblock, key.
192 xfs_bmbt_trace_argifk(
194 xfs_btree_cur_t
*cur
,
204 o
= INT_GET(k
->br_startoff
, ARCH_CONVERT
);
205 xfs_bmbt_trace_enter(func
, cur
, ARGS
, XFS_BMBT_KTRACE_ARGIFK
, line
,
206 i
, d
>> 32, (int)d
, o
>> 32,
212 * Add a trace buffer entry for arguments, for int, fsblock, rec.
215 xfs_bmbt_trace_argifr(
217 xfs_btree_cur_t
*cur
,
230 xfs_bmbt_disk_get_all(r
, &s
);
231 o
= (xfs_dfiloff_t
)s
.br_startoff
;
232 b
= (xfs_dfsbno_t
)s
.br_startblock
;
234 xfs_bmbt_trace_enter(func
, cur
, ARGS
, XFS_BMBT_KTRACE_ARGIFR
, line
,
235 i
, d
>> 32, (int)d
, o
>> 32,
236 (int)o
, b
>> 32, (int)b
, c
>> 32,
241 * Add a trace buffer entry for arguments, for int, key.
244 xfs_bmbt_trace_argik(
246 xfs_btree_cur_t
*cur
,
253 o
= INT_GET(k
->br_startoff
, ARCH_CONVERT
);
254 xfs_bmbt_trace_enter(func
, cur
, ARGS
, XFS_BMBT_KTRACE_ARGIFK
, line
,
255 i
, o
>> 32, (int)o
, 0,
261 * Add a trace buffer entry for the cursor/operation.
264 xfs_bmbt_trace_cursor(
266 xfs_btree_cur_t
*cur
,
272 xfs_bmbt_set_all(&r
, &cur
->bc_rec
.b
);
273 xfs_bmbt_trace_enter(func
, cur
, s
, XFS_BMBT_KTRACE_CUR
, line
,
274 (cur
->bc_nlevels
<< 24) | (cur
->bc_private
.b
.flags
<< 16) |
275 cur
->bc_private
.b
.allocated
,
276 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
),
277 (unsigned long)cur
->bc_bufs
[0], (unsigned long)cur
->bc_bufs
[1],
278 (unsigned long)cur
->bc_bufs
[2], (unsigned long)cur
->bc_bufs
[3],
279 (cur
->bc_ptrs
[0] << 16) | cur
->bc_ptrs
[1],
280 (cur
->bc_ptrs
[2] << 16) | cur
->bc_ptrs
[3]);
283 #define XFS_BMBT_TRACE_ARGBI(c,b,i) \
284 xfs_bmbt_trace_argbi(fname, c, b, i, __LINE__)
285 #define XFS_BMBT_TRACE_ARGBII(c,b,i,j) \
286 xfs_bmbt_trace_argbii(fname, c, b, i, j, __LINE__)
287 #define XFS_BMBT_TRACE_ARGFFFI(c,o,b,i,j) \
288 xfs_bmbt_trace_argfffi(fname, c, o, b, i, j, __LINE__)
289 #define XFS_BMBT_TRACE_ARGI(c,i) \
290 xfs_bmbt_trace_argi(fname, c, i, __LINE__)
291 #define XFS_BMBT_TRACE_ARGIFK(c,i,f,k) \
292 xfs_bmbt_trace_argifk(fname, c, i, f, k, __LINE__)
293 #define XFS_BMBT_TRACE_ARGIFR(c,i,f,r) \
294 xfs_bmbt_trace_argifr(fname, c, i, f, r, __LINE__)
295 #define XFS_BMBT_TRACE_ARGIK(c,i,k) \
296 xfs_bmbt_trace_argik(fname, c, i, k, __LINE__)
297 #define XFS_BMBT_TRACE_CURSOR(c,s) \
298 xfs_bmbt_trace_cursor(fname, c, s, __LINE__)
300 #define XFS_BMBT_TRACE_ARGBI(c,b,i)
301 #define XFS_BMBT_TRACE_ARGBII(c,b,i,j)
302 #define XFS_BMBT_TRACE_ARGFFFI(c,o,b,i,j)
303 #define XFS_BMBT_TRACE_ARGI(c,i)
304 #define XFS_BMBT_TRACE_ARGIFK(c,i,f,k)
305 #define XFS_BMBT_TRACE_ARGIFR(c,i,f,r)
306 #define XFS_BMBT_TRACE_ARGIK(c,i,k)
307 #define XFS_BMBT_TRACE_CURSOR(c,s)
308 #endif /* XFS_BMBT_TRACE */
312 * Internal functions.
316 * Delete record pointed to by cur/level.
318 STATIC
int /* error */
320 xfs_btree_cur_t
*cur
,
322 int *stat
) /* success/failure */
324 xfs_bmbt_block_t
*block
; /* bmap btree block */
325 xfs_fsblock_t bno
; /* fs-relative block number */
326 xfs_buf_t
*bp
; /* buffer for block */
327 int error
; /* error return value */
328 #ifdef XFS_BMBT_TRACE
329 static char fname
[] = "xfs_bmbt_delrec";
331 int i
; /* loop counter */
332 int j
; /* temp state */
333 xfs_bmbt_key_t key
; /* bmap btree key */
334 xfs_bmbt_key_t
*kp
=NULL
; /* pointer to bmap btree key */
335 xfs_fsblock_t lbno
; /* left sibling block number */
336 xfs_buf_t
*lbp
; /* left buffer pointer */
337 xfs_bmbt_block_t
*left
; /* left btree block */
338 xfs_bmbt_key_t
*lkp
; /* left btree key */
339 xfs_bmbt_ptr_t
*lpp
; /* left address pointer */
340 int lrecs
=0; /* left record count */
341 xfs_bmbt_rec_t
*lrp
; /* left record pointer */
342 xfs_mount_t
*mp
; /* file system mount point */
343 xfs_bmbt_ptr_t
*pp
; /* pointer to bmap block addr */
344 int ptr
; /* key/record index */
345 xfs_fsblock_t rbno
; /* right sibling block number */
346 xfs_buf_t
*rbp
; /* right buffer pointer */
347 xfs_bmbt_block_t
*right
; /* right btree block */
348 xfs_bmbt_key_t
*rkp
; /* right btree key */
349 xfs_bmbt_rec_t
*rp
; /* pointer to bmap btree rec */
350 xfs_bmbt_ptr_t
*rpp
; /* right address pointer */
351 xfs_bmbt_block_t
*rrblock
; /* right-right btree block */
352 xfs_buf_t
*rrbp
; /* right-right buffer pointer */
353 int rrecs
=0; /* right record count */
354 xfs_bmbt_rec_t
*rrp
; /* right record pointer */
355 xfs_btree_cur_t
*tcur
; /* temporary btree cursor */
356 int numrecs
; /* temporary numrec count */
357 int numlrecs
, numrrecs
;
359 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
360 XFS_BMBT_TRACE_ARGI(cur
, level
);
361 ptr
= cur
->bc_ptrs
[level
];
362 tcur
= (xfs_btree_cur_t
*)0;
364 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
368 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
369 numrecs
= be16_to_cpu(block
->bb_numrecs
);
371 if ((error
= xfs_btree_check_lblock(cur
, block
, level
, bp
))) {
372 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
377 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
381 XFS_STATS_INC(xs_bmbt_delrec
);
383 kp
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
384 pp
= XFS_BMAP_PTR_IADDR(block
, 1, cur
);
386 for (i
= ptr
; i
< numrecs
; i
++) {
387 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(pp
[i
], ARCH_CONVERT
), level
))) {
388 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
394 memmove(&kp
[ptr
- 1], &kp
[ptr
],
395 (numrecs
- ptr
) * sizeof(*kp
));
396 memmove(&pp
[ptr
- 1], &pp
[ptr
], /* INT_: direct copy */
397 (numrecs
- ptr
) * sizeof(*pp
));
398 xfs_bmbt_log_ptrs(cur
, bp
, ptr
, numrecs
- 1);
399 xfs_bmbt_log_keys(cur
, bp
, ptr
, numrecs
- 1);
402 rp
= XFS_BMAP_REC_IADDR(block
, 1, cur
);
404 memmove(&rp
[ptr
- 1], &rp
[ptr
],
405 (numrecs
- ptr
) * sizeof(*rp
));
406 xfs_bmbt_log_recs(cur
, bp
, ptr
, numrecs
- 1);
409 INT_SET(key
.br_startoff
, ARCH_CONVERT
, xfs_bmbt_disk_get_startoff(rp
));
414 block
->bb_numrecs
= cpu_to_be16(numrecs
);
415 xfs_bmbt_log_block(cur
, bp
, XFS_BB_NUMRECS
);
417 * We're at the root level.
418 * First, shrink the root block in-memory.
419 * Try to get rid of the next level down.
420 * If we can't then there's nothing left to do.
422 if (level
== cur
->bc_nlevels
- 1) {
423 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, -1,
424 cur
->bc_private
.b
.whichfork
);
425 if ((error
= xfs_bmbt_killroot(cur
))) {
426 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
429 if (level
> 0 && (error
= xfs_bmbt_decrement(cur
, level
, &j
))) {
430 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
433 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
437 if (ptr
== 1 && (error
= xfs_bmbt_updkey(cur
, kp
, level
+ 1))) {
438 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
441 if (numrecs
>= XFS_BMAP_BLOCK_IMINRECS(level
, cur
)) {
442 if (level
> 0 && (error
= xfs_bmbt_decrement(cur
, level
, &j
))) {
443 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
446 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
450 rbno
= be64_to_cpu(block
->bb_rightsib
);
451 lbno
= be64_to_cpu(block
->bb_leftsib
);
453 * One child of root, need to get a chance to copy its contents
454 * into the root and delete it. Can't go up to next level,
455 * there's nothing to delete there.
457 if (lbno
== NULLFSBLOCK
&& rbno
== NULLFSBLOCK
&&
458 level
== cur
->bc_nlevels
- 2) {
459 if ((error
= xfs_bmbt_killroot(cur
))) {
460 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
463 if (level
> 0 && (error
= xfs_bmbt_decrement(cur
, level
, &i
))) {
464 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
467 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
471 ASSERT(rbno
!= NULLFSBLOCK
|| lbno
!= NULLFSBLOCK
);
472 if ((error
= xfs_btree_dup_cursor(cur
, &tcur
))) {
473 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
477 if (rbno
!= NULLFSBLOCK
) {
478 i
= xfs_btree_lastrec(tcur
, level
);
479 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
480 if ((error
= xfs_bmbt_increment(tcur
, level
, &i
))) {
481 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
484 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
485 i
= xfs_btree_lastrec(tcur
, level
);
486 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
487 rbp
= tcur
->bc_bufs
[level
];
488 right
= XFS_BUF_TO_BMBT_BLOCK(rbp
);
490 if ((error
= xfs_btree_check_lblock(cur
, right
, level
, rbp
))) {
491 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
495 bno
= be64_to_cpu(right
->bb_leftsib
);
496 if (be16_to_cpu(right
->bb_numrecs
) - 1 >=
497 XFS_BMAP_BLOCK_IMINRECS(level
, cur
)) {
498 if ((error
= xfs_bmbt_lshift(tcur
, level
, &i
))) {
499 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
503 ASSERT(be16_to_cpu(block
->bb_numrecs
) >=
504 XFS_BMAP_BLOCK_IMINRECS(level
, tcur
));
505 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
508 if ((error
= xfs_bmbt_decrement(cur
,
510 XFS_BMBT_TRACE_CURSOR(cur
,
515 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
520 rrecs
= be16_to_cpu(right
->bb_numrecs
);
521 if (lbno
!= NULLFSBLOCK
) {
522 i
= xfs_btree_firstrec(tcur
, level
);
523 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
524 if ((error
= xfs_bmbt_decrement(tcur
, level
, &i
))) {
525 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
528 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
531 if (lbno
!= NULLFSBLOCK
) {
532 i
= xfs_btree_firstrec(tcur
, level
);
533 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
535 * decrement to last in block
537 if ((error
= xfs_bmbt_decrement(tcur
, level
, &i
))) {
538 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
541 i
= xfs_btree_firstrec(tcur
, level
);
542 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
543 lbp
= tcur
->bc_bufs
[level
];
544 left
= XFS_BUF_TO_BMBT_BLOCK(lbp
);
546 if ((error
= xfs_btree_check_lblock(cur
, left
, level
, lbp
))) {
547 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
551 bno
= be64_to_cpu(left
->bb_rightsib
);
552 if (be16_to_cpu(left
->bb_numrecs
) - 1 >=
553 XFS_BMAP_BLOCK_IMINRECS(level
, cur
)) {
554 if ((error
= xfs_bmbt_rshift(tcur
, level
, &i
))) {
555 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
559 ASSERT(be16_to_cpu(block
->bb_numrecs
) >=
560 XFS_BMAP_BLOCK_IMINRECS(level
, tcur
));
561 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
565 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
570 lrecs
= be16_to_cpu(left
->bb_numrecs
);
572 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
575 ASSERT(bno
!= NULLFSBLOCK
);
576 if (lbno
!= NULLFSBLOCK
&&
577 lrecs
+ be16_to_cpu(block
->bb_numrecs
) <= XFS_BMAP_BLOCK_IMAXRECS(level
, cur
)) {
581 if ((error
= xfs_btree_read_bufl(mp
, cur
->bc_tp
, lbno
, 0, &lbp
,
582 XFS_BMAP_BTREE_REF
))) {
583 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
586 left
= XFS_BUF_TO_BMBT_BLOCK(lbp
);
587 if ((error
= xfs_btree_check_lblock(cur
, left
, level
, lbp
))) {
588 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
591 } else if (rbno
!= NULLFSBLOCK
&&
592 rrecs
+ be16_to_cpu(block
->bb_numrecs
) <=
593 XFS_BMAP_BLOCK_IMAXRECS(level
, cur
)) {
597 if ((error
= xfs_btree_read_bufl(mp
, cur
->bc_tp
, rbno
, 0, &rbp
,
598 XFS_BMAP_BTREE_REF
))) {
599 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
602 right
= XFS_BUF_TO_BMBT_BLOCK(rbp
);
603 if ((error
= xfs_btree_check_lblock(cur
, right
, level
, rbp
))) {
604 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
607 lrecs
= be16_to_cpu(left
->bb_numrecs
);
609 if (level
> 0 && (error
= xfs_bmbt_decrement(cur
, level
, &i
))) {
610 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
613 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
617 numlrecs
= be16_to_cpu(left
->bb_numrecs
);
618 numrrecs
= be16_to_cpu(right
->bb_numrecs
);
620 lkp
= XFS_BMAP_KEY_IADDR(left
, numlrecs
+ 1, cur
);
621 lpp
= XFS_BMAP_PTR_IADDR(left
, numlrecs
+ 1, cur
);
622 rkp
= XFS_BMAP_KEY_IADDR(right
, 1, cur
);
623 rpp
= XFS_BMAP_PTR_IADDR(right
, 1, cur
);
625 for (i
= 0; i
< numrrecs
; i
++) {
626 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(rpp
[i
], ARCH_CONVERT
), level
))) {
627 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
632 memcpy(lkp
, rkp
, numrrecs
* sizeof(*lkp
));
633 memcpy(lpp
, rpp
, numrrecs
* sizeof(*lpp
));
634 xfs_bmbt_log_keys(cur
, lbp
, numlrecs
+ 1, numlrecs
+ numrrecs
);
635 xfs_bmbt_log_ptrs(cur
, lbp
, numlrecs
+ 1, numlrecs
+ numrrecs
);
637 lrp
= XFS_BMAP_REC_IADDR(left
, numlrecs
+ 1, cur
);
638 rrp
= XFS_BMAP_REC_IADDR(right
, 1, cur
);
639 memcpy(lrp
, rrp
, numrrecs
* sizeof(*lrp
));
640 xfs_bmbt_log_recs(cur
, lbp
, numlrecs
+ 1, numlrecs
+ numrrecs
);
642 be16_add(&left
->bb_numrecs
, numrrecs
);
643 left
->bb_rightsib
= right
->bb_rightsib
;
644 xfs_bmbt_log_block(cur
, lbp
, XFS_BB_RIGHTSIB
| XFS_BB_NUMRECS
);
645 if (be64_to_cpu(left
->bb_rightsib
) != NULLDFSBNO
) {
646 if ((error
= xfs_btree_read_bufl(mp
, cur
->bc_tp
,
647 be64_to_cpu(left
->bb_rightsib
),
648 0, &rrbp
, XFS_BMAP_BTREE_REF
))) {
649 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
652 rrblock
= XFS_BUF_TO_BMBT_BLOCK(rrbp
);
653 if ((error
= xfs_btree_check_lblock(cur
, rrblock
, level
, rrbp
))) {
654 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
657 rrblock
->bb_leftsib
= cpu_to_be64(lbno
);
658 xfs_bmbt_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
660 xfs_bmap_add_free(XFS_DADDR_TO_FSB(mp
, XFS_BUF_ADDR(rbp
)), 1,
661 cur
->bc_private
.b
.flist
, mp
);
662 cur
->bc_private
.b
.ip
->i_d
.di_nblocks
--;
663 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
, XFS_ILOG_CORE
);
664 XFS_TRANS_MOD_DQUOT_BYINO(mp
, cur
->bc_tp
, cur
->bc_private
.b
.ip
,
665 XFS_TRANS_DQ_BCOUNT
, -1L);
666 xfs_trans_binval(cur
->bc_tp
, rbp
);
668 cur
->bc_bufs
[level
] = lbp
;
669 cur
->bc_ptrs
[level
] += lrecs
;
670 cur
->bc_ra
[level
] = 0;
671 } else if ((error
= xfs_bmbt_increment(cur
, level
+ 1, &i
))) {
672 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
676 cur
->bc_ptrs
[level
]--;
677 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
683 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
689 * Get the data from the pointed-to record.
693 xfs_btree_cur_t
*cur
,
700 xfs_bmbt_block_t
*block
;
708 block
= xfs_bmbt_get_block(cur
, 0, &bp
);
709 ptr
= cur
->bc_ptrs
[0];
711 if ((error
= xfs_btree_check_lblock(cur
, block
, 0, bp
)))
714 if (ptr
> be16_to_cpu(block
->bb_numrecs
) || ptr
<= 0) {
718 rp
= XFS_BMAP_REC_IADDR(block
, ptr
, cur
);
719 *off
= xfs_bmbt_disk_get_startoff(rp
);
720 *bno
= xfs_bmbt_disk_get_startblock(rp
);
721 *len
= xfs_bmbt_disk_get_blockcount(rp
);
722 *state
= xfs_bmbt_disk_get_state(rp
);
729 * Insert one record/level. Return information to the caller
730 * allowing the next level up to proceed if necessary.
732 STATIC
int /* error */
734 xfs_btree_cur_t
*cur
,
737 xfs_bmbt_rec_t
*recp
,
738 xfs_btree_cur_t
**curp
,
739 int *stat
) /* no-go/done/continue */
741 xfs_bmbt_block_t
*block
; /* bmap btree block */
742 xfs_buf_t
*bp
; /* buffer for block */
743 int error
; /* error return value */
744 #ifdef XFS_BMBT_TRACE
745 static char fname
[] = "xfs_bmbt_insrec";
747 int i
; /* loop index */
748 xfs_bmbt_key_t key
; /* bmap btree key */
749 xfs_bmbt_key_t
*kp
=NULL
; /* pointer to bmap btree key */
750 int logflags
; /* inode logging flags */
751 xfs_fsblock_t nbno
; /* new block number */
752 struct xfs_btree_cur
*ncur
; /* new btree cursor */
753 xfs_bmbt_key_t nkey
; /* new btree key value */
754 xfs_bmbt_rec_t nrec
; /* new record count */
755 int optr
; /* old key/record index */
756 xfs_bmbt_ptr_t
*pp
; /* pointer to bmap block addr */
757 int ptr
; /* key/record index */
758 xfs_bmbt_rec_t
*rp
=NULL
; /* pointer to bmap btree rec */
761 ASSERT(level
< cur
->bc_nlevels
);
762 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
763 XFS_BMBT_TRACE_ARGIFR(cur
, level
, *bnop
, recp
);
764 ncur
= (xfs_btree_cur_t
*)0;
765 INT_SET(key
.br_startoff
, ARCH_CONVERT
,
766 xfs_bmbt_disk_get_startoff(recp
));
767 optr
= ptr
= cur
->bc_ptrs
[level
];
769 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
773 XFS_STATS_INC(xs_bmbt_insrec
);
774 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
775 numrecs
= be16_to_cpu(block
->bb_numrecs
);
777 if ((error
= xfs_btree_check_lblock(cur
, block
, level
, bp
))) {
778 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
781 if (ptr
<= numrecs
) {
783 rp
= XFS_BMAP_REC_IADDR(block
, ptr
, cur
);
784 xfs_btree_check_rec(XFS_BTNUM_BMAP
, recp
, rp
);
786 kp
= XFS_BMAP_KEY_IADDR(block
, ptr
, cur
);
787 xfs_btree_check_key(XFS_BTNUM_BMAP
, &key
, kp
);
792 if (numrecs
== XFS_BMAP_BLOCK_IMAXRECS(level
, cur
)) {
793 if (numrecs
< XFS_BMAP_BLOCK_DMAXRECS(level
, cur
)) {
795 * A root block, that can be made bigger.
797 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, 1,
798 cur
->bc_private
.b
.whichfork
);
799 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
800 } else if (level
== cur
->bc_nlevels
- 1) {
801 if ((error
= xfs_bmbt_newroot(cur
, &logflags
, stat
)) ||
803 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
806 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
808 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
810 if ((error
= xfs_bmbt_rshift(cur
, level
, &i
))) {
811 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
817 if ((error
= xfs_bmbt_lshift(cur
, level
, &i
))) {
818 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
822 optr
= ptr
= cur
->bc_ptrs
[level
];
824 if ((error
= xfs_bmbt_split(cur
, level
,
827 XFS_BMBT_TRACE_CURSOR(cur
,
832 block
= xfs_bmbt_get_block(
836 xfs_btree_check_lblock(cur
,
837 block
, level
, bp
))) {
838 XFS_BMBT_TRACE_CURSOR(
843 ptr
= cur
->bc_ptrs
[level
];
844 xfs_bmbt_disk_set_allf(&nrec
,
845 nkey
.br_startoff
, 0, 0,
848 XFS_BMBT_TRACE_CURSOR(cur
,
857 numrecs
= be16_to_cpu(block
->bb_numrecs
);
859 kp
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
860 pp
= XFS_BMAP_PTR_IADDR(block
, 1, cur
);
862 for (i
= numrecs
; i
>= ptr
; i
--) {
863 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(pp
[i
- 1], ARCH_CONVERT
),
865 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
870 memmove(&kp
[ptr
], &kp
[ptr
- 1],
871 (numrecs
- ptr
+ 1) * sizeof(*kp
));
872 memmove(&pp
[ptr
], &pp
[ptr
- 1], /* INT_: direct copy */
873 (numrecs
- ptr
+ 1) * sizeof(*pp
));
875 if ((error
= xfs_btree_check_lptr(cur
, (xfs_bmbt_ptr_t
)*bnop
,
877 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
882 INT_SET(pp
[ptr
- 1], ARCH_CONVERT
, *bnop
);
884 block
->bb_numrecs
= cpu_to_be16(numrecs
);
885 xfs_bmbt_log_keys(cur
, bp
, ptr
, numrecs
);
886 xfs_bmbt_log_ptrs(cur
, bp
, ptr
, numrecs
);
888 rp
= XFS_BMAP_REC_IADDR(block
, 1, cur
);
889 memmove(&rp
[ptr
], &rp
[ptr
- 1],
890 (numrecs
- ptr
+ 1) * sizeof(*rp
));
893 block
->bb_numrecs
= cpu_to_be16(numrecs
);
894 xfs_bmbt_log_recs(cur
, bp
, ptr
, numrecs
);
896 xfs_bmbt_log_block(cur
, bp
, XFS_BB_NUMRECS
);
900 xfs_btree_check_rec(XFS_BTNUM_BMAP
, rp
+ ptr
- 1,
903 xfs_btree_check_key(XFS_BTNUM_BMAP
, kp
+ ptr
- 1,
907 if (optr
== 1 && (error
= xfs_bmbt_updkey(cur
, &key
, level
+ 1))) {
908 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
912 if (nbno
!= NULLFSBLOCK
) {
916 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
923 xfs_btree_cur_t
*cur
)
925 xfs_bmbt_block_t
*block
;
926 xfs_bmbt_block_t
*cblock
;
933 #ifdef XFS_BMBT_TRACE
934 static char fname
[] = "xfs_bmbt_killroot";
943 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
944 level
= cur
->bc_nlevels
- 1;
947 * Don't deal with the root block needs to be a leaf case.
948 * We're just going to turn the thing back into extents anyway.
951 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
954 block
= xfs_bmbt_get_block(cur
, level
, &cbp
);
956 * Give up if the root has multiple children.
958 if (be16_to_cpu(block
->bb_numrecs
) != 1) {
959 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
963 * Only do this if the next level will fit.
964 * Then the data must be copied up to the inode,
965 * instead of freeing the root you free the next level.
967 cbp
= cur
->bc_bufs
[level
- 1];
968 cblock
= XFS_BUF_TO_BMBT_BLOCK(cbp
);
969 if (be16_to_cpu(cblock
->bb_numrecs
) > XFS_BMAP_BLOCK_DMAXRECS(level
, cur
)) {
970 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
973 ASSERT(be64_to_cpu(cblock
->bb_leftsib
) == NULLDFSBNO
);
974 ASSERT(be64_to_cpu(cblock
->bb_rightsib
) == NULLDFSBNO
);
975 ip
= cur
->bc_private
.b
.ip
;
976 ifp
= XFS_IFORK_PTR(ip
, cur
->bc_private
.b
.whichfork
);
977 ASSERT(XFS_BMAP_BLOCK_IMAXRECS(level
, cur
) ==
978 XFS_BMAP_BROOT_MAXRECS(ifp
->if_broot_bytes
));
979 i
= (int)(be16_to_cpu(cblock
->bb_numrecs
) - XFS_BMAP_BLOCK_IMAXRECS(level
, cur
));
981 xfs_iroot_realloc(ip
, i
, cur
->bc_private
.b
.whichfork
);
982 block
= ifp
->if_broot
;
984 be16_add(&block
->bb_numrecs
, i
);
985 ASSERT(block
->bb_numrecs
== cblock
->bb_numrecs
);
986 kp
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
987 ckp
= XFS_BMAP_KEY_IADDR(cblock
, 1, cur
);
988 memcpy(kp
, ckp
, be16_to_cpu(block
->bb_numrecs
) * sizeof(*kp
));
989 pp
= XFS_BMAP_PTR_IADDR(block
, 1, cur
);
990 cpp
= XFS_BMAP_PTR_IADDR(cblock
, 1, cur
);
992 for (i
= 0; i
< be16_to_cpu(cblock
->bb_numrecs
); i
++) {
993 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(cpp
[i
], ARCH_CONVERT
), level
- 1))) {
994 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
999 memcpy(pp
, cpp
, be16_to_cpu(block
->bb_numrecs
) * sizeof(*pp
));
1000 xfs_bmap_add_free(XFS_DADDR_TO_FSB(cur
->bc_mp
, XFS_BUF_ADDR(cbp
)), 1,
1001 cur
->bc_private
.b
.flist
, cur
->bc_mp
);
1002 ip
->i_d
.di_nblocks
--;
1003 XFS_TRANS_MOD_DQUOT_BYINO(cur
->bc_mp
, cur
->bc_tp
, ip
,
1004 XFS_TRANS_DQ_BCOUNT
, -1L);
1005 xfs_trans_binval(cur
->bc_tp
, cbp
);
1006 cur
->bc_bufs
[level
- 1] = NULL
;
1007 be16_add(&block
->bb_level
, -1);
1008 xfs_trans_log_inode(cur
->bc_tp
, ip
,
1009 XFS_ILOG_CORE
| XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
));
1011 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1016 * Log key values from the btree block.
1020 xfs_btree_cur_t
*cur
,
1025 #ifdef XFS_BMBT_TRACE
1026 static char fname
[] = "xfs_bmbt_log_keys";
1030 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1031 XFS_BMBT_TRACE_ARGBII(cur
, bp
, kfirst
, klast
);
1034 xfs_bmbt_block_t
*block
;
1039 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
1040 kp
= XFS_BMAP_KEY_DADDR(block
, 1, cur
);
1041 first
= (int)((xfs_caddr_t
)&kp
[kfirst
- 1] - (xfs_caddr_t
)block
);
1042 last
= (int)(((xfs_caddr_t
)&kp
[klast
] - 1) - (xfs_caddr_t
)block
);
1043 xfs_trans_log_buf(tp
, bp
, first
, last
);
1047 ip
= cur
->bc_private
.b
.ip
;
1048 xfs_trans_log_inode(tp
, ip
,
1049 XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
));
1051 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1055 * Log pointer values from the btree block.
1059 xfs_btree_cur_t
*cur
,
1064 #ifdef XFS_BMBT_TRACE
1065 static char fname
[] = "xfs_bmbt_log_ptrs";
1069 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1070 XFS_BMBT_TRACE_ARGBII(cur
, bp
, pfirst
, plast
);
1073 xfs_bmbt_block_t
*block
;
1078 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
1079 pp
= XFS_BMAP_PTR_DADDR(block
, 1, cur
);
1080 first
= (int)((xfs_caddr_t
)&pp
[pfirst
- 1] - (xfs_caddr_t
)block
);
1081 last
= (int)(((xfs_caddr_t
)&pp
[plast
] - 1) - (xfs_caddr_t
)block
);
1082 xfs_trans_log_buf(tp
, bp
, first
, last
);
1086 ip
= cur
->bc_private
.b
.ip
;
1087 xfs_trans_log_inode(tp
, ip
,
1088 XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
));
1090 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1094 * Lookup the record. The cursor is made to point to it, based on dir.
1096 STATIC
int /* error */
1098 xfs_btree_cur_t
*cur
,
1100 int *stat
) /* success/failure */
1102 xfs_bmbt_block_t
*block
=NULL
;
1106 int error
; /* error return value */
1107 #ifdef XFS_BMBT_TRACE
1108 static char fname
[] = "xfs_bmbt_lookup";
1110 xfs_fsblock_t fsbno
=0;
1114 xfs_bmbt_key_t
*kkbase
=NULL
;
1115 xfs_bmbt_key_t
*kkp
;
1116 xfs_bmbt_rec_t
*krbase
=NULL
;
1117 xfs_bmbt_rec_t
*krp
;
1122 xfs_bmbt_irec_t
*rp
;
1123 xfs_fileoff_t startoff
;
1126 XFS_STATS_INC(xs_bmbt_lookup
);
1127 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1128 XFS_BMBT_TRACE_ARGI(cur
, (int)dir
);
1131 rp
= &cur
->bc_rec
.b
;
1132 for (level
= cur
->bc_nlevels
- 1, diff
= 1; level
>= 0; level
--) {
1133 if (level
< cur
->bc_nlevels
- 1) {
1134 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
1135 bp
= cur
->bc_bufs
[level
];
1136 if (bp
&& XFS_BUF_ADDR(bp
) != d
)
1137 bp
= (xfs_buf_t
*)0;
1139 if ((error
= xfs_btree_read_bufl(mp
, tp
, fsbno
,
1140 0, &bp
, XFS_BMAP_BTREE_REF
))) {
1141 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1144 xfs_btree_setbuf(cur
, level
, bp
);
1145 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
1146 if ((error
= xfs_btree_check_lblock(cur
, block
,
1148 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1152 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
1154 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
1159 kkbase
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
1161 krbase
= XFS_BMAP_REC_IADDR(block
, 1, cur
);
1163 if (!(high
= be16_to_cpu(block
->bb_numrecs
))) {
1165 cur
->bc_ptrs
[0] = dir
!= XFS_LOOKUP_LE
;
1166 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1170 while (low
<= high
) {
1171 XFS_STATS_INC(xs_bmbt_compare
);
1172 keyno
= (low
+ high
) >> 1;
1174 kkp
= kkbase
+ keyno
- 1;
1175 startoff
= INT_GET(kkp
->br_startoff
, ARCH_CONVERT
);
1177 krp
= krbase
+ keyno
- 1;
1178 startoff
= xfs_bmbt_disk_get_startoff(krp
);
1180 diff
= (xfs_sfiloff_t
)
1181 (startoff
- rp
->br_startoff
);
1191 if (diff
> 0 && --keyno
< 1)
1193 pp
= XFS_BMAP_PTR_IADDR(block
, keyno
, cur
);
1195 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(*pp
, ARCH_CONVERT
), level
))) {
1196 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1200 fsbno
= INT_GET(*pp
, ARCH_CONVERT
);
1201 cur
->bc_ptrs
[level
] = keyno
;
1204 if (dir
!= XFS_LOOKUP_LE
&& diff
< 0) {
1207 * If ge search and we went off the end of the block, but it's
1208 * not the last block, we're in the wrong block.
1210 if (dir
== XFS_LOOKUP_GE
&& keyno
> be16_to_cpu(block
->bb_numrecs
) &&
1211 be64_to_cpu(block
->bb_rightsib
) != NULLDFSBNO
) {
1212 cur
->bc_ptrs
[0] = keyno
;
1213 if ((error
= xfs_bmbt_increment(cur
, 0, &i
))) {
1214 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1217 XFS_WANT_CORRUPTED_RETURN(i
== 1);
1218 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1223 else if (dir
== XFS_LOOKUP_LE
&& diff
> 0)
1225 cur
->bc_ptrs
[0] = keyno
;
1226 if (keyno
== 0 || keyno
> be16_to_cpu(block
->bb_numrecs
)) {
1227 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1230 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1231 *stat
= ((dir
!= XFS_LOOKUP_EQ
) || (diff
== 0));
1237 * Move 1 record left from cur/level if possible.
1238 * Update cur to reflect the new path.
1240 STATIC
int /* error */
1242 xfs_btree_cur_t
*cur
,
1244 int *stat
) /* success/failure */
1246 int error
; /* error return value */
1247 #ifdef XFS_BMBT_TRACE
1248 static char fname
[] = "xfs_bmbt_lshift";
1251 int i
; /* loop counter */
1253 xfs_bmbt_key_t key
; /* bmap btree key */
1254 xfs_buf_t
*lbp
; /* left buffer pointer */
1255 xfs_bmbt_block_t
*left
; /* left btree block */
1256 xfs_bmbt_key_t
*lkp
=NULL
; /* left btree key */
1257 xfs_bmbt_ptr_t
*lpp
; /* left address pointer */
1258 int lrecs
; /* left record count */
1259 xfs_bmbt_rec_t
*lrp
=NULL
; /* left record pointer */
1260 xfs_mount_t
*mp
; /* file system mount point */
1261 xfs_buf_t
*rbp
; /* right buffer pointer */
1262 xfs_bmbt_block_t
*right
; /* right btree block */
1263 xfs_bmbt_key_t
*rkp
=NULL
; /* right btree key */
1264 xfs_bmbt_ptr_t
*rpp
=NULL
; /* right address pointer */
1265 xfs_bmbt_rec_t
*rrp
=NULL
; /* right record pointer */
1266 int rrecs
; /* right record count */
1268 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1269 XFS_BMBT_TRACE_ARGI(cur
, level
);
1270 if (level
== cur
->bc_nlevels
- 1) {
1271 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1275 rbp
= cur
->bc_bufs
[level
];
1276 right
= XFS_BUF_TO_BMBT_BLOCK(rbp
);
1278 if ((error
= xfs_btree_check_lblock(cur
, right
, level
, rbp
))) {
1279 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1283 if (be64_to_cpu(right
->bb_leftsib
) == NULLDFSBNO
) {
1284 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1288 if (cur
->bc_ptrs
[level
] <= 1) {
1289 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1294 if ((error
= xfs_btree_read_bufl(mp
, cur
->bc_tp
, be64_to_cpu(right
->bb_leftsib
), 0,
1295 &lbp
, XFS_BMAP_BTREE_REF
))) {
1296 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1299 left
= XFS_BUF_TO_BMBT_BLOCK(lbp
);
1300 if ((error
= xfs_btree_check_lblock(cur
, left
, level
, lbp
))) {
1301 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1304 if (be16_to_cpu(left
->bb_numrecs
) == XFS_BMAP_BLOCK_IMAXRECS(level
, cur
)) {
1305 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1309 lrecs
= be16_to_cpu(left
->bb_numrecs
) + 1;
1311 lkp
= XFS_BMAP_KEY_IADDR(left
, lrecs
, cur
);
1312 rkp
= XFS_BMAP_KEY_IADDR(right
, 1, cur
);
1314 xfs_bmbt_log_keys(cur
, lbp
, lrecs
, lrecs
);
1315 lpp
= XFS_BMAP_PTR_IADDR(left
, lrecs
, cur
);
1316 rpp
= XFS_BMAP_PTR_IADDR(right
, 1, cur
);
1318 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(*rpp
, ARCH_CONVERT
), level
))) {
1319 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1323 *lpp
= *rpp
; /* INT_: direct copy */
1324 xfs_bmbt_log_ptrs(cur
, lbp
, lrecs
, lrecs
);
1326 lrp
= XFS_BMAP_REC_IADDR(left
, lrecs
, cur
);
1327 rrp
= XFS_BMAP_REC_IADDR(right
, 1, cur
);
1329 xfs_bmbt_log_recs(cur
, lbp
, lrecs
, lrecs
);
1331 left
->bb_numrecs
= cpu_to_be16(lrecs
);
1332 xfs_bmbt_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
1335 xfs_btree_check_key(XFS_BTNUM_BMAP
, lkp
- 1, lkp
);
1337 xfs_btree_check_rec(XFS_BTNUM_BMAP
, lrp
- 1, lrp
);
1339 rrecs
= be16_to_cpu(right
->bb_numrecs
) - 1;
1340 right
->bb_numrecs
= cpu_to_be16(rrecs
);
1341 xfs_bmbt_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
1344 for (i
= 0; i
< rrecs
; i
++) {
1345 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(rpp
[i
+ 1], ARCH_CONVERT
),
1347 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1352 memmove(rkp
, rkp
+ 1, rrecs
* sizeof(*rkp
));
1353 memmove(rpp
, rpp
+ 1, rrecs
* sizeof(*rpp
));
1354 xfs_bmbt_log_keys(cur
, rbp
, 1, rrecs
);
1355 xfs_bmbt_log_ptrs(cur
, rbp
, 1, rrecs
);
1357 memmove(rrp
, rrp
+ 1, rrecs
* sizeof(*rrp
));
1358 xfs_bmbt_log_recs(cur
, rbp
, 1, rrecs
);
1359 INT_SET(key
.br_startoff
, ARCH_CONVERT
,
1360 xfs_bmbt_disk_get_startoff(rrp
));
1363 if ((error
= xfs_bmbt_updkey(cur
, rkp
, level
+ 1))) {
1364 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1367 cur
->bc_ptrs
[level
]--;
1368 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1374 * Move 1 record right from cur/level if possible.
1375 * Update cur to reflect the new path.
1377 STATIC
int /* error */
1379 xfs_btree_cur_t
*cur
,
1381 int *stat
) /* success/failure */
1383 int error
; /* error return value */
1384 #ifdef XFS_BMBT_TRACE
1385 static char fname
[] = "xfs_bmbt_rshift";
1387 int i
; /* loop counter */
1388 xfs_bmbt_key_t key
; /* bmap btree key */
1389 xfs_buf_t
*lbp
; /* left buffer pointer */
1390 xfs_bmbt_block_t
*left
; /* left btree block */
1391 xfs_bmbt_key_t
*lkp
; /* left btree key */
1392 xfs_bmbt_ptr_t
*lpp
; /* left address pointer */
1393 xfs_bmbt_rec_t
*lrp
; /* left record pointer */
1394 xfs_mount_t
*mp
; /* file system mount point */
1395 xfs_buf_t
*rbp
; /* right buffer pointer */
1396 xfs_bmbt_block_t
*right
; /* right btree block */
1397 xfs_bmbt_key_t
*rkp
; /* right btree key */
1398 xfs_bmbt_ptr_t
*rpp
; /* right address pointer */
1399 xfs_bmbt_rec_t
*rrp
=NULL
; /* right record pointer */
1400 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
1402 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1403 XFS_BMBT_TRACE_ARGI(cur
, level
);
1404 if (level
== cur
->bc_nlevels
- 1) {
1405 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1409 lbp
= cur
->bc_bufs
[level
];
1410 left
= XFS_BUF_TO_BMBT_BLOCK(lbp
);
1412 if ((error
= xfs_btree_check_lblock(cur
, left
, level
, lbp
))) {
1413 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1417 if (be64_to_cpu(left
->bb_rightsib
) == NULLDFSBNO
) {
1418 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1422 if (cur
->bc_ptrs
[level
] >= be16_to_cpu(left
->bb_numrecs
)) {
1423 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1428 if ((error
= xfs_btree_read_bufl(mp
, cur
->bc_tp
, be64_to_cpu(left
->bb_rightsib
), 0,
1429 &rbp
, XFS_BMAP_BTREE_REF
))) {
1430 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1433 right
= XFS_BUF_TO_BMBT_BLOCK(rbp
);
1434 if ((error
= xfs_btree_check_lblock(cur
, right
, level
, rbp
))) {
1435 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1438 if (be16_to_cpu(right
->bb_numrecs
) == XFS_BMAP_BLOCK_IMAXRECS(level
, cur
)) {
1439 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1444 lkp
= XFS_BMAP_KEY_IADDR(left
, be16_to_cpu(left
->bb_numrecs
), cur
);
1445 lpp
= XFS_BMAP_PTR_IADDR(left
, be16_to_cpu(left
->bb_numrecs
), cur
);
1446 rkp
= XFS_BMAP_KEY_IADDR(right
, 1, cur
);
1447 rpp
= XFS_BMAP_PTR_IADDR(right
, 1, cur
);
1449 for (i
= be16_to_cpu(right
->bb_numrecs
) - 1; i
>= 0; i
--) {
1450 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(rpp
[i
], ARCH_CONVERT
), level
))) {
1451 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1456 memmove(rkp
+ 1, rkp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rkp
));
1457 memmove(rpp
+ 1, rpp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rpp
));
1459 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(*lpp
, ARCH_CONVERT
), level
))) {
1460 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1465 *rpp
= *lpp
; /* INT_: direct copy */
1466 xfs_bmbt_log_keys(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
) + 1);
1467 xfs_bmbt_log_ptrs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
) + 1);
1469 lrp
= XFS_BMAP_REC_IADDR(left
, be16_to_cpu(left
->bb_numrecs
), cur
);
1470 rrp
= XFS_BMAP_REC_IADDR(right
, 1, cur
);
1471 memmove(rrp
+ 1, rrp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rrp
));
1473 xfs_bmbt_log_recs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
) + 1);
1474 INT_SET(key
.br_startoff
, ARCH_CONVERT
,
1475 xfs_bmbt_disk_get_startoff(rrp
));
1478 be16_add(&left
->bb_numrecs
, -1);
1479 xfs_bmbt_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
1480 be16_add(&right
->bb_numrecs
, 1);
1483 xfs_btree_check_key(XFS_BTNUM_BMAP
, rkp
, rkp
+ 1);
1485 xfs_btree_check_rec(XFS_BTNUM_BMAP
, rrp
, rrp
+ 1);
1487 xfs_bmbt_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
1488 if ((error
= xfs_btree_dup_cursor(cur
, &tcur
))) {
1489 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1492 i
= xfs_btree_lastrec(tcur
, level
);
1493 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1494 if ((error
= xfs_bmbt_increment(tcur
, level
, &i
))) {
1495 XFS_BMBT_TRACE_CURSOR(tcur
, ERROR
);
1498 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1499 if ((error
= xfs_bmbt_updkey(tcur
, rkp
, level
+ 1))) {
1500 XFS_BMBT_TRACE_CURSOR(tcur
, ERROR
);
1503 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
1504 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1508 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1510 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
1515 * Determine the extent state.
1524 ASSERT(blks
!= 0); /* saved for DMIG */
1525 return XFS_EXT_UNWRITTEN
;
1527 return XFS_EXT_NORM
;
1532 * Split cur/level block in half.
1533 * Return new block number and its first record (to be inserted into parent).
1535 STATIC
int /* error */
1537 xfs_btree_cur_t
*cur
,
1539 xfs_fsblock_t
*bnop
,
1540 xfs_bmbt_key_t
*keyp
,
1541 xfs_btree_cur_t
**curp
,
1542 int *stat
) /* success/failure */
1544 xfs_alloc_arg_t args
; /* block allocation args */
1545 int error
; /* error return value */
1546 #ifdef XFS_BMBT_TRACE
1547 static char fname
[] = "xfs_bmbt_split";
1549 int i
; /* loop counter */
1550 xfs_fsblock_t lbno
; /* left sibling block number */
1551 xfs_buf_t
*lbp
; /* left buffer pointer */
1552 xfs_bmbt_block_t
*left
; /* left btree block */
1553 xfs_bmbt_key_t
*lkp
; /* left btree key */
1554 xfs_bmbt_ptr_t
*lpp
; /* left address pointer */
1555 xfs_bmbt_rec_t
*lrp
; /* left record pointer */
1556 xfs_buf_t
*rbp
; /* right buffer pointer */
1557 xfs_bmbt_block_t
*right
; /* right btree block */
1558 xfs_bmbt_key_t
*rkp
; /* right btree key */
1559 xfs_bmbt_ptr_t
*rpp
; /* right address pointer */
1560 xfs_bmbt_block_t
*rrblock
; /* right-right btree block */
1561 xfs_buf_t
*rrbp
; /* right-right buffer pointer */
1562 xfs_bmbt_rec_t
*rrp
; /* right record pointer */
1564 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1565 XFS_BMBT_TRACE_ARGIFK(cur
, level
, *bnop
, keyp
);
1566 args
.tp
= cur
->bc_tp
;
1567 args
.mp
= cur
->bc_mp
;
1568 lbp
= cur
->bc_bufs
[level
];
1569 lbno
= XFS_DADDR_TO_FSB(args
.mp
, XFS_BUF_ADDR(lbp
));
1570 left
= XFS_BUF_TO_BMBT_BLOCK(lbp
);
1571 args
.fsbno
= cur
->bc_private
.b
.firstblock
;
1572 args
.firstblock
= args
.fsbno
;
1573 if (args
.fsbno
== NULLFSBLOCK
) {
1575 args
.type
= XFS_ALLOCTYPE_START_BNO
;
1577 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
1578 args
.mod
= args
.minleft
= args
.alignment
= args
.total
= args
.isfl
=
1579 args
.userdata
= args
.minalignslop
= 0;
1580 args
.minlen
= args
.maxlen
= args
.prod
= 1;
1581 args
.wasdel
= cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
;
1582 if (!args
.wasdel
&& xfs_trans_get_block_res(args
.tp
) == 0) {
1583 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1584 return XFS_ERROR(ENOSPC
);
1586 if ((error
= xfs_alloc_vextent(&args
))) {
1587 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1590 if (args
.fsbno
== NULLFSBLOCK
) {
1591 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1595 ASSERT(args
.len
== 1);
1596 cur
->bc_private
.b
.firstblock
= args
.fsbno
;
1597 cur
->bc_private
.b
.allocated
++;
1598 cur
->bc_private
.b
.ip
->i_d
.di_nblocks
++;
1599 xfs_trans_log_inode(args
.tp
, cur
->bc_private
.b
.ip
, XFS_ILOG_CORE
);
1600 XFS_TRANS_MOD_DQUOT_BYINO(args
.mp
, args
.tp
, cur
->bc_private
.b
.ip
,
1601 XFS_TRANS_DQ_BCOUNT
, 1L);
1602 rbp
= xfs_btree_get_bufl(args
.mp
, args
.tp
, args
.fsbno
, 0);
1603 right
= XFS_BUF_TO_BMBT_BLOCK(rbp
);
1605 if ((error
= xfs_btree_check_lblock(cur
, left
, level
, rbp
))) {
1606 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1610 right
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
1611 right
->bb_level
= left
->bb_level
;
1612 right
->bb_numrecs
= cpu_to_be16(be16_to_cpu(left
->bb_numrecs
) / 2);
1613 if ((be16_to_cpu(left
->bb_numrecs
) & 1) &&
1614 cur
->bc_ptrs
[level
] <= be16_to_cpu(right
->bb_numrecs
) + 1)
1615 be16_add(&right
->bb_numrecs
, 1);
1616 i
= be16_to_cpu(left
->bb_numrecs
) - be16_to_cpu(right
->bb_numrecs
) + 1;
1618 lkp
= XFS_BMAP_KEY_IADDR(left
, i
, cur
);
1619 lpp
= XFS_BMAP_PTR_IADDR(left
, i
, cur
);
1620 rkp
= XFS_BMAP_KEY_IADDR(right
, 1, cur
);
1621 rpp
= XFS_BMAP_PTR_IADDR(right
, 1, cur
);
1623 for (i
= 0; i
< be16_to_cpu(right
->bb_numrecs
); i
++) {
1624 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(lpp
[i
], ARCH_CONVERT
), level
))) {
1625 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1630 memcpy(rkp
, lkp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rkp
));
1631 memcpy(rpp
, lpp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rpp
));
1632 xfs_bmbt_log_keys(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
1633 xfs_bmbt_log_ptrs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
1634 keyp
->br_startoff
= INT_GET(rkp
->br_startoff
, ARCH_CONVERT
);
1636 lrp
= XFS_BMAP_REC_IADDR(left
, i
, cur
);
1637 rrp
= XFS_BMAP_REC_IADDR(right
, 1, cur
);
1638 memcpy(rrp
, lrp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rrp
));
1639 xfs_bmbt_log_recs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
1640 keyp
->br_startoff
= xfs_bmbt_disk_get_startoff(rrp
);
1642 be16_add(&left
->bb_numrecs
, -(be16_to_cpu(right
->bb_numrecs
)));
1643 right
->bb_rightsib
= left
->bb_rightsib
;
1644 left
->bb_rightsib
= cpu_to_be64(args
.fsbno
);
1645 right
->bb_leftsib
= cpu_to_be64(lbno
);
1646 xfs_bmbt_log_block(cur
, rbp
, XFS_BB_ALL_BITS
);
1647 xfs_bmbt_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
1648 if (be64_to_cpu(right
->bb_rightsib
) != NULLDFSBNO
) {
1649 if ((error
= xfs_btree_read_bufl(args
.mp
, args
.tp
,
1650 be64_to_cpu(right
->bb_rightsib
), 0, &rrbp
,
1651 XFS_BMAP_BTREE_REF
))) {
1652 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1655 rrblock
= XFS_BUF_TO_BMBT_BLOCK(rrbp
);
1656 if ((error
= xfs_btree_check_lblock(cur
, rrblock
, level
, rrbp
))) {
1657 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1660 rrblock
->bb_leftsib
= cpu_to_be64(args
.fsbno
);
1661 xfs_bmbt_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
1663 if (cur
->bc_ptrs
[level
] > be16_to_cpu(left
->bb_numrecs
) + 1) {
1664 xfs_btree_setbuf(cur
, level
, rbp
);
1665 cur
->bc_ptrs
[level
] -= be16_to_cpu(left
->bb_numrecs
);
1667 if (level
+ 1 < cur
->bc_nlevels
) {
1668 if ((error
= xfs_btree_dup_cursor(cur
, curp
))) {
1669 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1672 (*curp
)->bc_ptrs
[level
+ 1]++;
1675 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1682 * Update keys for the record.
1686 xfs_btree_cur_t
*cur
,
1687 xfs_bmbt_key_t
*keyp
, /* on-disk format */
1690 xfs_bmbt_block_t
*block
;
1695 #ifdef XFS_BMBT_TRACE
1696 static char fname
[] = "xfs_bmbt_updkey";
1702 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1703 XFS_BMBT_TRACE_ARGIK(cur
, level
, keyp
);
1704 for (ptr
= 1; ptr
== 1 && level
< cur
->bc_nlevels
; level
++) {
1705 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
1707 if ((error
= xfs_btree_check_lblock(cur
, block
, level
, bp
))) {
1708 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1712 ptr
= cur
->bc_ptrs
[level
];
1713 kp
= XFS_BMAP_KEY_IADDR(block
, ptr
, cur
);
1715 xfs_bmbt_log_keys(cur
, bp
, ptr
, ptr
);
1717 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1722 * Convert on-disk form of btree root to in-memory form.
1726 xfs_bmdr_block_t
*dblock
,
1728 xfs_bmbt_block_t
*rblock
,
1732 xfs_bmbt_key_t
*fkp
;
1733 xfs_bmbt_ptr_t
*fpp
;
1734 xfs_bmbt_key_t
*tkp
;
1735 xfs_bmbt_ptr_t
*tpp
;
1737 rblock
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
1738 rblock
->bb_level
= dblock
->bb_level
;
1739 ASSERT(be16_to_cpu(rblock
->bb_level
) > 0);
1740 rblock
->bb_numrecs
= dblock
->bb_numrecs
;
1741 rblock
->bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
1742 rblock
->bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
1743 dmxr
= (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen
, xfs_bmdr
, 0);
1744 fkp
= XFS_BTREE_KEY_ADDR(dblocklen
, xfs_bmdr
, dblock
, 1, dmxr
);
1745 tkp
= XFS_BMAP_BROOT_KEY_ADDR(rblock
, 1, rblocklen
);
1746 fpp
= XFS_BTREE_PTR_ADDR(dblocklen
, xfs_bmdr
, dblock
, 1, dmxr
);
1747 tpp
= XFS_BMAP_BROOT_PTR_ADDR(rblock
, 1, rblocklen
);
1748 dmxr
= be16_to_cpu(dblock
->bb_numrecs
);
1749 memcpy(tkp
, fkp
, sizeof(*fkp
) * dmxr
);
1750 memcpy(tpp
, fpp
, sizeof(*fpp
) * dmxr
); /* INT_: direct copy */
1754 * Decrement cursor by one record at the level.
1755 * For nonzero levels the leaf-ward information is untouched.
1759 xfs_btree_cur_t
*cur
,
1761 int *stat
) /* success/failure */
1763 xfs_bmbt_block_t
*block
;
1765 int error
; /* error return value */
1766 #ifdef XFS_BMBT_TRACE
1767 static char fname
[] = "xfs_bmbt_decrement";
1769 xfs_fsblock_t fsbno
;
1774 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1775 XFS_BMBT_TRACE_ARGI(cur
, level
);
1776 ASSERT(level
< cur
->bc_nlevels
);
1777 if (level
< cur
->bc_nlevels
- 1)
1778 xfs_btree_readahead(cur
, level
, XFS_BTCUR_LEFTRA
);
1779 if (--cur
->bc_ptrs
[level
] > 0) {
1780 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1784 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
1786 if ((error
= xfs_btree_check_lblock(cur
, block
, level
, bp
))) {
1787 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1791 if (be64_to_cpu(block
->bb_leftsib
) == NULLDFSBNO
) {
1792 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1796 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1797 if (--cur
->bc_ptrs
[lev
] > 0)
1799 if (lev
< cur
->bc_nlevels
- 1)
1800 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_LEFTRA
);
1802 if (lev
== cur
->bc_nlevels
) {
1803 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1809 for (block
= xfs_bmbt_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1810 fsbno
= INT_GET(*XFS_BMAP_PTR_IADDR(block
, cur
->bc_ptrs
[lev
], cur
), ARCH_CONVERT
);
1811 if ((error
= xfs_btree_read_bufl(mp
, tp
, fsbno
, 0, &bp
,
1812 XFS_BMAP_BTREE_REF
))) {
1813 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1817 xfs_btree_setbuf(cur
, lev
, bp
);
1818 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
1819 if ((error
= xfs_btree_check_lblock(cur
, block
, lev
, bp
))) {
1820 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1823 cur
->bc_ptrs
[lev
] = be16_to_cpu(block
->bb_numrecs
);
1825 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1831 * Delete the record pointed to by cur.
1835 xfs_btree_cur_t
*cur
,
1836 int *stat
) /* success/failure */
1838 int error
; /* error return value */
1839 #ifdef XFS_BMBT_TRACE
1840 static char fname
[] = "xfs_bmbt_delete";
1845 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1846 for (level
= 0, i
= 2; i
== 2; level
++) {
1847 if ((error
= xfs_bmbt_delrec(cur
, level
, &i
))) {
1848 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1853 for (level
= 1; level
< cur
->bc_nlevels
; level
++) {
1854 if (cur
->bc_ptrs
[level
] == 0) {
1855 if ((error
= xfs_bmbt_decrement(cur
, level
,
1857 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1864 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1870 * Convert a compressed bmap extent record to an uncompressed form.
1871 * This code must be in sync with the routines xfs_bmbt_get_startoff,
1872 * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state.
1875 STATIC __inline__
void
1884 ext_flag
= (int)(l0
>> (64 - BMBT_EXNTFLAG_BITLEN
));
1885 s
->br_startoff
= ((xfs_fileoff_t
)l0
&
1886 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
1888 s
->br_startblock
= (((xfs_fsblock_t
)l0
& XFS_MASK64LO(9)) << 43) |
1889 (((xfs_fsblock_t
)l1
) >> 21);
1895 b
= (((xfs_dfsbno_t
)l0
& XFS_MASK64LO(9)) << 43) |
1896 (((xfs_dfsbno_t
)l1
) >> 21);
1897 ASSERT((b
>> 32) == 0 || ISNULLDSTARTBLOCK(b
));
1898 s
->br_startblock
= (xfs_fsblock_t
)b
;
1901 s
->br_startblock
= (xfs_fsblock_t
)(((xfs_dfsbno_t
)l1
) >> 21);
1903 #endif /* XFS_BIG_BLKNOS */
1904 s
->br_blockcount
= (xfs_filblks_t
)(l1
& XFS_MASK64LO(21));
1905 /* This is xfs_extent_state() in-line */
1907 ASSERT(s
->br_blockcount
!= 0); /* saved for DMIG */
1908 st
= XFS_EXT_UNWRITTEN
;
1919 __xfs_bmbt_get_all(r
->l0
, r
->l1
, s
);
1923 * Get the block pointer for the given level of the cursor.
1924 * Fill in the buffer pointer, if applicable.
1928 xfs_btree_cur_t
*cur
,
1933 xfs_bmbt_block_t
*rval
;
1935 if (level
< cur
->bc_nlevels
- 1) {
1936 *bpp
= cur
->bc_bufs
[level
];
1937 rval
= XFS_BUF_TO_BMBT_BLOCK(*bpp
);
1940 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
,
1941 cur
->bc_private
.b
.whichfork
);
1942 rval
= ifp
->if_broot
;
1948 * Extract the blockcount field from an in memory bmap extent record.
1951 xfs_bmbt_get_blockcount(
1954 return (xfs_filblks_t
)(r
->l1
& XFS_MASK64LO(21));
1958 * Extract the startblock field from an in memory bmap extent record.
1961 xfs_bmbt_get_startblock(
1965 return (((xfs_fsblock_t
)r
->l0
& XFS_MASK64LO(9)) << 43) |
1966 (((xfs_fsblock_t
)r
->l1
) >> 21);
1971 b
= (((xfs_dfsbno_t
)r
->l0
& XFS_MASK64LO(9)) << 43) |
1972 (((xfs_dfsbno_t
)r
->l1
) >> 21);
1973 ASSERT((b
>> 32) == 0 || ISNULLDSTARTBLOCK(b
));
1974 return (xfs_fsblock_t
)b
;
1976 return (xfs_fsblock_t
)(((xfs_dfsbno_t
)r
->l1
) >> 21);
1978 #endif /* XFS_BIG_BLKNOS */
1982 * Extract the startoff field from an in memory bmap extent record.
1985 xfs_bmbt_get_startoff(
1988 return ((xfs_fileoff_t
)r
->l0
&
1989 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
1998 ext_flag
= (int)((r
->l0
) >> (64 - BMBT_EXNTFLAG_BITLEN
));
1999 return xfs_extent_state(xfs_bmbt_get_blockcount(r
),
2003 #ifndef XFS_NATIVE_HOST
2004 /* Endian flipping versions of the bmbt extraction functions */
2006 xfs_bmbt_disk_get_all(
2012 l0
= INT_GET(r
->l0
, ARCH_CONVERT
);
2013 l1
= INT_GET(r
->l1
, ARCH_CONVERT
);
2015 __xfs_bmbt_get_all(l0
, l1
, s
);
2019 * Extract the blockcount field from an on disk bmap extent record.
2022 xfs_bmbt_disk_get_blockcount(
2025 return (xfs_filblks_t
)(INT_GET(r
->l1
, ARCH_CONVERT
) & XFS_MASK64LO(21));
2029 * Extract the startblock field from an on disk bmap extent record.
2032 xfs_bmbt_disk_get_startblock(
2036 return (((xfs_fsblock_t
)INT_GET(r
->l0
, ARCH_CONVERT
) & XFS_MASK64LO(9)) << 43) |
2037 (((xfs_fsblock_t
)INT_GET(r
->l1
, ARCH_CONVERT
)) >> 21);
2042 b
= (((xfs_dfsbno_t
)INT_GET(r
->l0
, ARCH_CONVERT
) & XFS_MASK64LO(9)) << 43) |
2043 (((xfs_dfsbno_t
)INT_GET(r
->l1
, ARCH_CONVERT
)) >> 21);
2044 ASSERT((b
>> 32) == 0 || ISNULLDSTARTBLOCK(b
));
2045 return (xfs_fsblock_t
)b
;
2047 return (xfs_fsblock_t
)(((xfs_dfsbno_t
)INT_GET(r
->l1
, ARCH_CONVERT
)) >> 21);
2049 #endif /* XFS_BIG_BLKNOS */
2053 * Extract the startoff field from a disk format bmap extent record.
2056 xfs_bmbt_disk_get_startoff(
2059 return ((xfs_fileoff_t
)INT_GET(r
->l0
, ARCH_CONVERT
) &
2060 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
2064 xfs_bmbt_disk_get_state(
2069 ext_flag
= (int)((INT_GET(r
->l0
, ARCH_CONVERT
)) >> (64 - BMBT_EXNTFLAG_BITLEN
));
2070 return xfs_extent_state(xfs_bmbt_disk_get_blockcount(r
),
2073 #endif /* XFS_NATIVE_HOST */
2077 * Increment cursor by one record at the level.
2078 * For nonzero levels the leaf-ward information is untouched.
2082 xfs_btree_cur_t
*cur
,
2084 int *stat
) /* success/failure */
2086 xfs_bmbt_block_t
*block
;
2088 int error
; /* error return value */
2089 #ifdef XFS_BMBT_TRACE
2090 static char fname
[] = "xfs_bmbt_increment";
2092 xfs_fsblock_t fsbno
;
2097 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2098 XFS_BMBT_TRACE_ARGI(cur
, level
);
2099 ASSERT(level
< cur
->bc_nlevels
);
2100 if (level
< cur
->bc_nlevels
- 1)
2101 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
2102 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
2104 if ((error
= xfs_btree_check_lblock(cur
, block
, level
, bp
))) {
2105 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2109 if (++cur
->bc_ptrs
[level
] <= be16_to_cpu(block
->bb_numrecs
)) {
2110 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2114 if (be64_to_cpu(block
->bb_rightsib
) == NULLDFSBNO
) {
2115 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2119 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
2120 block
= xfs_bmbt_get_block(cur
, lev
, &bp
);
2122 if ((error
= xfs_btree_check_lblock(cur
, block
, lev
, bp
))) {
2123 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2127 if (++cur
->bc_ptrs
[lev
] <= be16_to_cpu(block
->bb_numrecs
))
2129 if (lev
< cur
->bc_nlevels
- 1)
2130 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_RIGHTRA
);
2132 if (lev
== cur
->bc_nlevels
) {
2133 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2139 for (block
= xfs_bmbt_get_block(cur
, lev
, &bp
); lev
> level
; ) {
2140 fsbno
= INT_GET(*XFS_BMAP_PTR_IADDR(block
, cur
->bc_ptrs
[lev
], cur
), ARCH_CONVERT
);
2141 if ((error
= xfs_btree_read_bufl(mp
, tp
, fsbno
, 0, &bp
,
2142 XFS_BMAP_BTREE_REF
))) {
2143 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2147 xfs_btree_setbuf(cur
, lev
, bp
);
2148 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
2149 if ((error
= xfs_btree_check_lblock(cur
, block
, lev
, bp
))) {
2150 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2153 cur
->bc_ptrs
[lev
] = 1;
2155 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2161 * Insert the current record at the point referenced by cur.
2165 xfs_btree_cur_t
*cur
,
2166 int *stat
) /* success/failure */
2168 int error
; /* error return value */
2169 #ifdef XFS_BMBT_TRACE
2170 static char fname
[] = "xfs_bmbt_insert";
2175 xfs_btree_cur_t
*ncur
;
2176 xfs_bmbt_rec_t nrec
;
2177 xfs_btree_cur_t
*pcur
;
2179 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2182 xfs_bmbt_disk_set_all(&nrec
, &cur
->bc_rec
.b
);
2183 ncur
= (xfs_btree_cur_t
*)0;
2186 if ((error
= xfs_bmbt_insrec(pcur
, level
++, &nbno
, &nrec
, &ncur
,
2189 xfs_btree_del_cursor(pcur
, XFS_BTREE_ERROR
);
2190 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2193 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
2194 if (pcur
!= cur
&& (ncur
|| nbno
== NULLFSBLOCK
)) {
2195 cur
->bc_nlevels
= pcur
->bc_nlevels
;
2196 cur
->bc_private
.b
.allocated
+=
2197 pcur
->bc_private
.b
.allocated
;
2198 pcur
->bc_private
.b
.allocated
= 0;
2199 ASSERT((cur
->bc_private
.b
.firstblock
!= NULLFSBLOCK
) ||
2200 (cur
->bc_private
.b
.ip
->i_d
.di_flags
&
2201 XFS_DIFLAG_REALTIME
));
2202 cur
->bc_private
.b
.firstblock
=
2203 pcur
->bc_private
.b
.firstblock
;
2204 ASSERT(cur
->bc_private
.b
.flist
==
2205 pcur
->bc_private
.b
.flist
);
2206 xfs_btree_del_cursor(pcur
, XFS_BTREE_NOERROR
);
2210 ncur
= (xfs_btree_cur_t
*)0;
2212 } while (nbno
!= NULLFSBLOCK
);
2213 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2217 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2222 * Log fields from the btree block header.
2226 xfs_btree_cur_t
*cur
,
2231 #ifdef XFS_BMBT_TRACE
2232 static char fname
[] = "xfs_bmbt_log_block";
2236 static const short offsets
[] = {
2237 offsetof(xfs_bmbt_block_t
, bb_magic
),
2238 offsetof(xfs_bmbt_block_t
, bb_level
),
2239 offsetof(xfs_bmbt_block_t
, bb_numrecs
),
2240 offsetof(xfs_bmbt_block_t
, bb_leftsib
),
2241 offsetof(xfs_bmbt_block_t
, bb_rightsib
),
2242 sizeof(xfs_bmbt_block_t
)
2245 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2246 XFS_BMBT_TRACE_ARGBI(cur
, bp
, fields
);
2249 xfs_btree_offsets(fields
, offsets
, XFS_BB_NUM_BITS
, &first
,
2251 xfs_trans_log_buf(tp
, bp
, first
, last
);
2253 xfs_trans_log_inode(tp
, cur
->bc_private
.b
.ip
,
2254 XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
));
2255 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2259 * Log record values from the btree block.
2263 xfs_btree_cur_t
*cur
,
2268 xfs_bmbt_block_t
*block
;
2270 #ifdef XFS_BMBT_TRACE
2271 static char fname
[] = "xfs_bmbt_log_recs";
2277 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2278 XFS_BMBT_TRACE_ARGBII(cur
, bp
, rfirst
, rlast
);
2281 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
2282 rp
= XFS_BMAP_REC_DADDR(block
, 1, cur
);
2283 first
= (int)((xfs_caddr_t
)&rp
[rfirst
- 1] - (xfs_caddr_t
)block
);
2284 last
= (int)(((xfs_caddr_t
)&rp
[rlast
] - 1) - (xfs_caddr_t
)block
);
2285 xfs_trans_log_buf(tp
, bp
, first
, last
);
2286 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2291 xfs_btree_cur_t
*cur
,
2295 int *stat
) /* success/failure */
2297 cur
->bc_rec
.b
.br_startoff
= off
;
2298 cur
->bc_rec
.b
.br_startblock
= bno
;
2299 cur
->bc_rec
.b
.br_blockcount
= len
;
2300 return xfs_bmbt_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
2305 xfs_btree_cur_t
*cur
,
2309 int *stat
) /* success/failure */
2311 cur
->bc_rec
.b
.br_startoff
= off
;
2312 cur
->bc_rec
.b
.br_startblock
= bno
;
2313 cur
->bc_rec
.b
.br_blockcount
= len
;
2314 return xfs_bmbt_lookup(cur
, XFS_LOOKUP_GE
, stat
);
2318 * Give the bmap btree a new root block. Copy the old broot contents
2319 * down into a real block and make the broot point to it.
2323 xfs_btree_cur_t
*cur
, /* btree cursor */
2324 int *logflags
, /* logging flags for inode */
2325 int *stat
) /* return status - 0 fail */
2327 xfs_alloc_arg_t args
; /* allocation arguments */
2328 xfs_bmbt_block_t
*block
; /* bmap btree block */
2329 xfs_buf_t
*bp
; /* buffer for block */
2330 xfs_bmbt_block_t
*cblock
; /* child btree block */
2331 xfs_bmbt_key_t
*ckp
; /* child key pointer */
2332 xfs_bmbt_ptr_t
*cpp
; /* child ptr pointer */
2333 int error
; /* error return code */
2334 #ifdef XFS_BMBT_TRACE
2335 static char fname
[] = "xfs_bmbt_newroot";
2338 int i
; /* loop counter */
2340 xfs_bmbt_key_t
*kp
; /* pointer to bmap btree key */
2341 int level
; /* btree level */
2342 xfs_bmbt_ptr_t
*pp
; /* pointer to bmap block addr */
2344 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2345 level
= cur
->bc_nlevels
- 1;
2346 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
2348 * Copy the root into a real block.
2350 args
.mp
= cur
->bc_mp
;
2351 pp
= XFS_BMAP_PTR_IADDR(block
, 1, cur
);
2352 args
.tp
= cur
->bc_tp
;
2353 args
.fsbno
= cur
->bc_private
.b
.firstblock
;
2354 args
.mod
= args
.minleft
= args
.alignment
= args
.total
= args
.isfl
=
2355 args
.userdata
= args
.minalignslop
= 0;
2356 args
.minlen
= args
.maxlen
= args
.prod
= 1;
2357 args
.wasdel
= cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
;
2358 args
.firstblock
= args
.fsbno
;
2359 if (args
.fsbno
== NULLFSBLOCK
) {
2361 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(*pp
, ARCH_CONVERT
), level
))) {
2362 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2366 args
.fsbno
= INT_GET(*pp
, ARCH_CONVERT
);
2367 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2369 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
2370 if ((error
= xfs_alloc_vextent(&args
))) {
2371 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2374 if (args
.fsbno
== NULLFSBLOCK
) {
2375 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2379 ASSERT(args
.len
== 1);
2380 cur
->bc_private
.b
.firstblock
= args
.fsbno
;
2381 cur
->bc_private
.b
.allocated
++;
2382 cur
->bc_private
.b
.ip
->i_d
.di_nblocks
++;
2383 XFS_TRANS_MOD_DQUOT_BYINO(args
.mp
, args
.tp
, cur
->bc_private
.b
.ip
,
2384 XFS_TRANS_DQ_BCOUNT
, 1L);
2385 bp
= xfs_btree_get_bufl(args
.mp
, cur
->bc_tp
, args
.fsbno
, 0);
2386 cblock
= XFS_BUF_TO_BMBT_BLOCK(bp
);
2388 be16_add(&block
->bb_level
, 1);
2389 block
->bb_numrecs
= cpu_to_be16(1);
2391 cur
->bc_ptrs
[level
+ 1] = 1;
2392 kp
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
2393 ckp
= XFS_BMAP_KEY_IADDR(cblock
, 1, cur
);
2394 memcpy(ckp
, kp
, be16_to_cpu(cblock
->bb_numrecs
) * sizeof(*kp
));
2395 cpp
= XFS_BMAP_PTR_IADDR(cblock
, 1, cur
);
2397 for (i
= 0; i
< be16_to_cpu(cblock
->bb_numrecs
); i
++) {
2398 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(pp
[i
], ARCH_CONVERT
), level
))) {
2399 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2404 memcpy(cpp
, pp
, be16_to_cpu(cblock
->bb_numrecs
) * sizeof(*pp
));
2406 if ((error
= xfs_btree_check_lptr(cur
, (xfs_bmbt_ptr_t
)args
.fsbno
,
2408 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2412 INT_SET(*pp
, ARCH_CONVERT
, args
.fsbno
);
2413 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, 1 - be16_to_cpu(cblock
->bb_numrecs
),
2414 cur
->bc_private
.b
.whichfork
);
2415 xfs_btree_setbuf(cur
, level
, bp
);
2417 * Do all this logging at the end so that
2418 * the root is at the right level.
2420 xfs_bmbt_log_block(cur
, bp
, XFS_BB_ALL_BITS
);
2421 xfs_bmbt_log_keys(cur
, bp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2422 xfs_bmbt_log_ptrs(cur
, bp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2423 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2425 XFS_ILOG_CORE
| XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
);
2431 * Set all the fields in a bmap extent record from the uncompressed form.
2440 ASSERT((s
->br_state
== XFS_EXT_NORM
) ||
2441 (s
->br_state
== XFS_EXT_UNWRITTEN
));
2442 extent_flag
= (s
->br_state
== XFS_EXT_NORM
) ? 0 : 1;
2443 ASSERT((s
->br_startoff
& XFS_MASK64HI(9)) == 0);
2444 ASSERT((s
->br_blockcount
& XFS_MASK64HI(43)) == 0);
2446 ASSERT((s
->br_startblock
& XFS_MASK64HI(12)) == 0);
2447 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2448 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9) |
2449 ((xfs_bmbt_rec_base_t
)s
->br_startblock
>> 43);
2450 r
->l1
= ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2451 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2452 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2453 #else /* !XFS_BIG_BLKNOS */
2454 if (ISNULLSTARTBLOCK(s
->br_startblock
)) {
2455 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2456 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9) |
2457 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9);
2458 r
->l1
= XFS_MASK64HI(11) |
2459 ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2460 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2461 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2463 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2464 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9);
2465 r
->l1
= ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2466 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2467 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2469 #endif /* XFS_BIG_BLKNOS */
2473 * Set all the fields in a bmap extent record from the arguments.
2485 ASSERT((v
== XFS_EXT_NORM
) || (v
== XFS_EXT_UNWRITTEN
));
2486 extent_flag
= (v
== XFS_EXT_NORM
) ? 0 : 1;
2487 ASSERT((o
& XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN
)) == 0);
2488 ASSERT((c
& XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN
)) == 0);
2490 ASSERT((b
& XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN
)) == 0);
2491 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2492 ((xfs_bmbt_rec_base_t
)o
<< 9) |
2493 ((xfs_bmbt_rec_base_t
)b
>> 43);
2494 r
->l1
= ((xfs_bmbt_rec_base_t
)b
<< 21) |
2495 ((xfs_bmbt_rec_base_t
)c
&
2496 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2497 #else /* !XFS_BIG_BLKNOS */
2498 if (ISNULLSTARTBLOCK(b
)) {
2499 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2500 ((xfs_bmbt_rec_base_t
)o
<< 9) |
2501 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9);
2502 r
->l1
= XFS_MASK64HI(11) |
2503 ((xfs_bmbt_rec_base_t
)b
<< 21) |
2504 ((xfs_bmbt_rec_base_t
)c
&
2505 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2507 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2508 ((xfs_bmbt_rec_base_t
)o
<< 9);
2509 r
->l1
= ((xfs_bmbt_rec_base_t
)b
<< 21) |
2510 ((xfs_bmbt_rec_base_t
)c
&
2511 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2513 #endif /* XFS_BIG_BLKNOS */
2516 #ifndef XFS_NATIVE_HOST
2518 * Set all the fields in a bmap extent record from the uncompressed form.
2521 xfs_bmbt_disk_set_all(
2527 ASSERT((s
->br_state
== XFS_EXT_NORM
) ||
2528 (s
->br_state
== XFS_EXT_UNWRITTEN
));
2529 extent_flag
= (s
->br_state
== XFS_EXT_NORM
) ? 0 : 1;
2530 ASSERT((s
->br_startoff
& XFS_MASK64HI(9)) == 0);
2531 ASSERT((s
->br_blockcount
& XFS_MASK64HI(43)) == 0);
2533 ASSERT((s
->br_startblock
& XFS_MASK64HI(12)) == 0);
2534 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2535 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9) |
2536 ((xfs_bmbt_rec_base_t
)s
->br_startblock
>> 43));
2537 INT_SET(r
->l1
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2538 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2539 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2540 #else /* !XFS_BIG_BLKNOS */
2541 if (ISNULLSTARTBLOCK(s
->br_startblock
)) {
2542 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2543 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9) |
2544 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9));
2545 INT_SET(r
->l1
, ARCH_CONVERT
, XFS_MASK64HI(11) |
2546 ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2547 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2548 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2550 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2551 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9));
2552 INT_SET(r
->l1
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2553 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2554 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2556 #endif /* XFS_BIG_BLKNOS */
2560 * Set all the fields in a disk format bmap extent record from the arguments.
2563 xfs_bmbt_disk_set_allf(
2572 ASSERT((v
== XFS_EXT_NORM
) || (v
== XFS_EXT_UNWRITTEN
));
2573 extent_flag
= (v
== XFS_EXT_NORM
) ? 0 : 1;
2574 ASSERT((o
& XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN
)) == 0);
2575 ASSERT((c
& XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN
)) == 0);
2577 ASSERT((b
& XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN
)) == 0);
2578 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2579 ((xfs_bmbt_rec_base_t
)o
<< 9) |
2580 ((xfs_bmbt_rec_base_t
)b
>> 43));
2581 INT_SET(r
->l1
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)b
<< 21) |
2582 ((xfs_bmbt_rec_base_t
)c
&
2583 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2584 #else /* !XFS_BIG_BLKNOS */
2585 if (ISNULLSTARTBLOCK(b
)) {
2586 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2587 ((xfs_bmbt_rec_base_t
)o
<< 9) |
2588 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9));
2589 INT_SET(r
->l1
, ARCH_CONVERT
, XFS_MASK64HI(11) |
2590 ((xfs_bmbt_rec_base_t
)b
<< 21) |
2591 ((xfs_bmbt_rec_base_t
)c
&
2592 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2594 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2595 ((xfs_bmbt_rec_base_t
)o
<< 9));
2596 INT_SET(r
->l1
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)b
<< 21) |
2597 ((xfs_bmbt_rec_base_t
)c
&
2598 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2600 #endif /* XFS_BIG_BLKNOS */
2602 #endif /* XFS_NATIVE_HOST */
2605 * Set the blockcount field in a bmap extent record.
2608 xfs_bmbt_set_blockcount(
2612 ASSERT((v
& XFS_MASK64HI(43)) == 0);
2613 r
->l1
= (r
->l1
& (xfs_bmbt_rec_base_t
)XFS_MASK64HI(43)) |
2614 (xfs_bmbt_rec_base_t
)(v
& XFS_MASK64LO(21));
2618 * Set the startblock field in a bmap extent record.
2621 xfs_bmbt_set_startblock(
2626 ASSERT((v
& XFS_MASK64HI(12)) == 0);
2627 r
->l0
= (r
->l0
& (xfs_bmbt_rec_base_t
)XFS_MASK64HI(55)) |
2628 (xfs_bmbt_rec_base_t
)(v
>> 43);
2629 r
->l1
= (r
->l1
& (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)) |
2630 (xfs_bmbt_rec_base_t
)(v
<< 21);
2631 #else /* !XFS_BIG_BLKNOS */
2632 if (ISNULLSTARTBLOCK(v
)) {
2633 r
->l0
|= (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9);
2634 r
->l1
= (xfs_bmbt_rec_base_t
)XFS_MASK64HI(11) |
2635 ((xfs_bmbt_rec_base_t
)v
<< 21) |
2636 (r
->l1
& (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2638 r
->l0
&= ~(xfs_bmbt_rec_base_t
)XFS_MASK64LO(9);
2639 r
->l1
= ((xfs_bmbt_rec_base_t
)v
<< 21) |
2640 (r
->l1
& (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2642 #endif /* XFS_BIG_BLKNOS */
2646 * Set the startoff field in a bmap extent record.
2649 xfs_bmbt_set_startoff(
2653 ASSERT((v
& XFS_MASK64HI(9)) == 0);
2654 r
->l0
= (r
->l0
& (xfs_bmbt_rec_base_t
) XFS_MASK64HI(1)) |
2655 ((xfs_bmbt_rec_base_t
)v
<< 9) |
2656 (r
->l0
& (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9));
2660 * Set the extent state field in a bmap extent record.
2667 ASSERT(v
== XFS_EXT_NORM
|| v
== XFS_EXT_UNWRITTEN
);
2668 if (v
== XFS_EXT_NORM
)
2669 r
->l0
&= XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN
);
2671 r
->l0
|= XFS_MASK64HI(BMBT_EXNTFLAG_BITLEN
);
2675 * Convert in-memory form of btree root to on-disk form.
2679 xfs_bmbt_block_t
*rblock
,
2681 xfs_bmdr_block_t
*dblock
,
2685 xfs_bmbt_key_t
*fkp
;
2686 xfs_bmbt_ptr_t
*fpp
;
2687 xfs_bmbt_key_t
*tkp
;
2688 xfs_bmbt_ptr_t
*tpp
;
2690 ASSERT(be32_to_cpu(rblock
->bb_magic
) == XFS_BMAP_MAGIC
);
2691 ASSERT(be64_to_cpu(rblock
->bb_leftsib
) == NULLDFSBNO
);
2692 ASSERT(be64_to_cpu(rblock
->bb_rightsib
) == NULLDFSBNO
);
2693 ASSERT(be16_to_cpu(rblock
->bb_level
) > 0);
2694 dblock
->bb_level
= rblock
->bb_level
;
2695 dblock
->bb_numrecs
= rblock
->bb_numrecs
;
2696 dmxr
= (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen
, xfs_bmdr
, 0);
2697 fkp
= XFS_BMAP_BROOT_KEY_ADDR(rblock
, 1, rblocklen
);
2698 tkp
= XFS_BTREE_KEY_ADDR(dblocklen
, xfs_bmdr
, dblock
, 1, dmxr
);
2699 fpp
= XFS_BMAP_BROOT_PTR_ADDR(rblock
, 1, rblocklen
);
2700 tpp
= XFS_BTREE_PTR_ADDR(dblocklen
, xfs_bmdr
, dblock
, 1, dmxr
);
2701 dmxr
= be16_to_cpu(dblock
->bb_numrecs
);
2702 memcpy(tkp
, fkp
, sizeof(*fkp
) * dmxr
);
2703 memcpy(tpp
, fpp
, sizeof(*fpp
) * dmxr
); /* INT_: direct copy */
2707 * Update the record to the passed values.
2711 xfs_btree_cur_t
*cur
,
2717 xfs_bmbt_block_t
*block
;
2720 #ifdef XFS_BMBT_TRACE
2721 static char fname
[] = "xfs_bmbt_update";
2727 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2728 XFS_BMBT_TRACE_ARGFFFI(cur
, (xfs_dfiloff_t
)off
, (xfs_dfsbno_t
)bno
,
2729 (xfs_dfilblks_t
)len
, (int)state
);
2730 block
= xfs_bmbt_get_block(cur
, 0, &bp
);
2732 if ((error
= xfs_btree_check_lblock(cur
, block
, 0, bp
))) {
2733 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2737 ptr
= cur
->bc_ptrs
[0];
2738 rp
= XFS_BMAP_REC_IADDR(block
, ptr
, cur
);
2739 xfs_bmbt_disk_set_allf(rp
, off
, bno
, len
, state
);
2740 xfs_bmbt_log_recs(cur
, bp
, ptr
, ptr
);
2742 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2745 INT_SET(key
.br_startoff
, ARCH_CONVERT
, off
);
2746 if ((error
= xfs_bmbt_updkey(cur
, &key
, 1))) {
2747 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2750 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2755 * Check extent records, which have just been read, for
2756 * any bit in the extent flag field. ASSERT on debug
2757 * kernels, as this condition should not occur.
2758 * Return an error condition (1) if any flags found,
2759 * otherwise return 0.
2763 xfs_check_nostate_extents(
2770 for (; num
> 0; num
--, idx
++) {
2771 ep
= xfs_iext_get_ext(ifp
, idx
);
2773 (64 - BMBT_EXNTFLAG_BITLEN
)) != 0) {