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 if (args
.fsbno
== NULLFSBLOCK
) {
1574 args
.type
= XFS_ALLOCTYPE_START_BNO
;
1575 } else if (cur
->bc_private
.b
.flist
->xbf_low
)
1576 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
1578 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
1579 args
.mod
= args
.minleft
= args
.alignment
= args
.total
= args
.isfl
=
1580 args
.userdata
= args
.minalignslop
= 0;
1581 args
.minlen
= args
.maxlen
= args
.prod
= 1;
1582 args
.wasdel
= cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
;
1583 if (!args
.wasdel
&& xfs_trans_get_block_res(args
.tp
) == 0) {
1584 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1585 return XFS_ERROR(ENOSPC
);
1587 if ((error
= xfs_alloc_vextent(&args
))) {
1588 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1591 if (args
.fsbno
== NULLFSBLOCK
) {
1592 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1596 ASSERT(args
.len
== 1);
1597 cur
->bc_private
.b
.firstblock
= args
.fsbno
;
1598 cur
->bc_private
.b
.allocated
++;
1599 cur
->bc_private
.b
.ip
->i_d
.di_nblocks
++;
1600 xfs_trans_log_inode(args
.tp
, cur
->bc_private
.b
.ip
, XFS_ILOG_CORE
);
1601 XFS_TRANS_MOD_DQUOT_BYINO(args
.mp
, args
.tp
, cur
->bc_private
.b
.ip
,
1602 XFS_TRANS_DQ_BCOUNT
, 1L);
1603 rbp
= xfs_btree_get_bufl(args
.mp
, args
.tp
, args
.fsbno
, 0);
1604 right
= XFS_BUF_TO_BMBT_BLOCK(rbp
);
1606 if ((error
= xfs_btree_check_lblock(cur
, left
, level
, rbp
))) {
1607 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1611 right
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
1612 right
->bb_level
= left
->bb_level
;
1613 right
->bb_numrecs
= cpu_to_be16(be16_to_cpu(left
->bb_numrecs
) / 2);
1614 if ((be16_to_cpu(left
->bb_numrecs
) & 1) &&
1615 cur
->bc_ptrs
[level
] <= be16_to_cpu(right
->bb_numrecs
) + 1)
1616 be16_add(&right
->bb_numrecs
, 1);
1617 i
= be16_to_cpu(left
->bb_numrecs
) - be16_to_cpu(right
->bb_numrecs
) + 1;
1619 lkp
= XFS_BMAP_KEY_IADDR(left
, i
, cur
);
1620 lpp
= XFS_BMAP_PTR_IADDR(left
, i
, cur
);
1621 rkp
= XFS_BMAP_KEY_IADDR(right
, 1, cur
);
1622 rpp
= XFS_BMAP_PTR_IADDR(right
, 1, cur
);
1624 for (i
= 0; i
< be16_to_cpu(right
->bb_numrecs
); i
++) {
1625 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(lpp
[i
], ARCH_CONVERT
), level
))) {
1626 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1631 memcpy(rkp
, lkp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rkp
));
1632 memcpy(rpp
, lpp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rpp
));
1633 xfs_bmbt_log_keys(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
1634 xfs_bmbt_log_ptrs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
1635 keyp
->br_startoff
= INT_GET(rkp
->br_startoff
, ARCH_CONVERT
);
1637 lrp
= XFS_BMAP_REC_IADDR(left
, i
, cur
);
1638 rrp
= XFS_BMAP_REC_IADDR(right
, 1, cur
);
1639 memcpy(rrp
, lrp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rrp
));
1640 xfs_bmbt_log_recs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
1641 keyp
->br_startoff
= xfs_bmbt_disk_get_startoff(rrp
);
1643 be16_add(&left
->bb_numrecs
, -(be16_to_cpu(right
->bb_numrecs
)));
1644 right
->bb_rightsib
= left
->bb_rightsib
;
1645 left
->bb_rightsib
= cpu_to_be64(args
.fsbno
);
1646 right
->bb_leftsib
= cpu_to_be64(lbno
);
1647 xfs_bmbt_log_block(cur
, rbp
, XFS_BB_ALL_BITS
);
1648 xfs_bmbt_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
1649 if (be64_to_cpu(right
->bb_rightsib
) != NULLDFSBNO
) {
1650 if ((error
= xfs_btree_read_bufl(args
.mp
, args
.tp
,
1651 be64_to_cpu(right
->bb_rightsib
), 0, &rrbp
,
1652 XFS_BMAP_BTREE_REF
))) {
1653 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1656 rrblock
= XFS_BUF_TO_BMBT_BLOCK(rrbp
);
1657 if ((error
= xfs_btree_check_lblock(cur
, rrblock
, level
, rrbp
))) {
1658 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1661 rrblock
->bb_leftsib
= cpu_to_be64(args
.fsbno
);
1662 xfs_bmbt_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
1664 if (cur
->bc_ptrs
[level
] > be16_to_cpu(left
->bb_numrecs
) + 1) {
1665 xfs_btree_setbuf(cur
, level
, rbp
);
1666 cur
->bc_ptrs
[level
] -= be16_to_cpu(left
->bb_numrecs
);
1668 if (level
+ 1 < cur
->bc_nlevels
) {
1669 if ((error
= xfs_btree_dup_cursor(cur
, curp
))) {
1670 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1673 (*curp
)->bc_ptrs
[level
+ 1]++;
1676 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1683 * Update keys for the record.
1687 xfs_btree_cur_t
*cur
,
1688 xfs_bmbt_key_t
*keyp
, /* on-disk format */
1691 xfs_bmbt_block_t
*block
;
1696 #ifdef XFS_BMBT_TRACE
1697 static char fname
[] = "xfs_bmbt_updkey";
1703 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1704 XFS_BMBT_TRACE_ARGIK(cur
, level
, keyp
);
1705 for (ptr
= 1; ptr
== 1 && level
< cur
->bc_nlevels
; level
++) {
1706 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
1708 if ((error
= xfs_btree_check_lblock(cur
, block
, level
, bp
))) {
1709 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1713 ptr
= cur
->bc_ptrs
[level
];
1714 kp
= XFS_BMAP_KEY_IADDR(block
, ptr
, cur
);
1716 xfs_bmbt_log_keys(cur
, bp
, ptr
, ptr
);
1718 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1723 * Convert on-disk form of btree root to in-memory form.
1727 xfs_bmdr_block_t
*dblock
,
1729 xfs_bmbt_block_t
*rblock
,
1733 xfs_bmbt_key_t
*fkp
;
1734 xfs_bmbt_ptr_t
*fpp
;
1735 xfs_bmbt_key_t
*tkp
;
1736 xfs_bmbt_ptr_t
*tpp
;
1738 rblock
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
1739 rblock
->bb_level
= dblock
->bb_level
;
1740 ASSERT(be16_to_cpu(rblock
->bb_level
) > 0);
1741 rblock
->bb_numrecs
= dblock
->bb_numrecs
;
1742 rblock
->bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
1743 rblock
->bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
1744 dmxr
= (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen
, xfs_bmdr
, 0);
1745 fkp
= XFS_BTREE_KEY_ADDR(dblocklen
, xfs_bmdr
, dblock
, 1, dmxr
);
1746 tkp
= XFS_BMAP_BROOT_KEY_ADDR(rblock
, 1, rblocklen
);
1747 fpp
= XFS_BTREE_PTR_ADDR(dblocklen
, xfs_bmdr
, dblock
, 1, dmxr
);
1748 tpp
= XFS_BMAP_BROOT_PTR_ADDR(rblock
, 1, rblocklen
);
1749 dmxr
= be16_to_cpu(dblock
->bb_numrecs
);
1750 memcpy(tkp
, fkp
, sizeof(*fkp
) * dmxr
);
1751 memcpy(tpp
, fpp
, sizeof(*fpp
) * dmxr
); /* INT_: direct copy */
1755 * Decrement cursor by one record at the level.
1756 * For nonzero levels the leaf-ward information is untouched.
1760 xfs_btree_cur_t
*cur
,
1762 int *stat
) /* success/failure */
1764 xfs_bmbt_block_t
*block
;
1766 int error
; /* error return value */
1767 #ifdef XFS_BMBT_TRACE
1768 static char fname
[] = "xfs_bmbt_decrement";
1770 xfs_fsblock_t fsbno
;
1775 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1776 XFS_BMBT_TRACE_ARGI(cur
, level
);
1777 ASSERT(level
< cur
->bc_nlevels
);
1778 if (level
< cur
->bc_nlevels
- 1)
1779 xfs_btree_readahead(cur
, level
, XFS_BTCUR_LEFTRA
);
1780 if (--cur
->bc_ptrs
[level
] > 0) {
1781 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1785 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
1787 if ((error
= xfs_btree_check_lblock(cur
, block
, level
, bp
))) {
1788 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1792 if (be64_to_cpu(block
->bb_leftsib
) == NULLDFSBNO
) {
1793 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1797 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1798 if (--cur
->bc_ptrs
[lev
] > 0)
1800 if (lev
< cur
->bc_nlevels
- 1)
1801 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_LEFTRA
);
1803 if (lev
== cur
->bc_nlevels
) {
1804 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1810 for (block
= xfs_bmbt_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1811 fsbno
= INT_GET(*XFS_BMAP_PTR_IADDR(block
, cur
->bc_ptrs
[lev
], cur
), ARCH_CONVERT
);
1812 if ((error
= xfs_btree_read_bufl(mp
, tp
, fsbno
, 0, &bp
,
1813 XFS_BMAP_BTREE_REF
))) {
1814 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1818 xfs_btree_setbuf(cur
, lev
, bp
);
1819 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
1820 if ((error
= xfs_btree_check_lblock(cur
, block
, lev
, bp
))) {
1821 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1824 cur
->bc_ptrs
[lev
] = be16_to_cpu(block
->bb_numrecs
);
1826 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1832 * Delete the record pointed to by cur.
1836 xfs_btree_cur_t
*cur
,
1837 int *stat
) /* success/failure */
1839 int error
; /* error return value */
1840 #ifdef XFS_BMBT_TRACE
1841 static char fname
[] = "xfs_bmbt_delete";
1846 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
1847 for (level
= 0, i
= 2; i
== 2; level
++) {
1848 if ((error
= xfs_bmbt_delrec(cur
, level
, &i
))) {
1849 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1854 for (level
= 1; level
< cur
->bc_nlevels
; level
++) {
1855 if (cur
->bc_ptrs
[level
] == 0) {
1856 if ((error
= xfs_bmbt_decrement(cur
, level
,
1858 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
1865 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
1871 * Convert a compressed bmap extent record to an uncompressed form.
1872 * This code must be in sync with the routines xfs_bmbt_get_startoff,
1873 * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state.
1876 STATIC __inline__
void
1885 ext_flag
= (int)(l0
>> (64 - BMBT_EXNTFLAG_BITLEN
));
1886 s
->br_startoff
= ((xfs_fileoff_t
)l0
&
1887 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
1889 s
->br_startblock
= (((xfs_fsblock_t
)l0
& XFS_MASK64LO(9)) << 43) |
1890 (((xfs_fsblock_t
)l1
) >> 21);
1896 b
= (((xfs_dfsbno_t
)l0
& XFS_MASK64LO(9)) << 43) |
1897 (((xfs_dfsbno_t
)l1
) >> 21);
1898 ASSERT((b
>> 32) == 0 || ISNULLDSTARTBLOCK(b
));
1899 s
->br_startblock
= (xfs_fsblock_t
)b
;
1902 s
->br_startblock
= (xfs_fsblock_t
)(((xfs_dfsbno_t
)l1
) >> 21);
1904 #endif /* XFS_BIG_BLKNOS */
1905 s
->br_blockcount
= (xfs_filblks_t
)(l1
& XFS_MASK64LO(21));
1906 /* This is xfs_extent_state() in-line */
1908 ASSERT(s
->br_blockcount
!= 0); /* saved for DMIG */
1909 st
= XFS_EXT_UNWRITTEN
;
1920 __xfs_bmbt_get_all(r
->l0
, r
->l1
, s
);
1924 * Get the block pointer for the given level of the cursor.
1925 * Fill in the buffer pointer, if applicable.
1929 xfs_btree_cur_t
*cur
,
1934 xfs_bmbt_block_t
*rval
;
1936 if (level
< cur
->bc_nlevels
- 1) {
1937 *bpp
= cur
->bc_bufs
[level
];
1938 rval
= XFS_BUF_TO_BMBT_BLOCK(*bpp
);
1941 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
,
1942 cur
->bc_private
.b
.whichfork
);
1943 rval
= ifp
->if_broot
;
1949 * Extract the blockcount field from an in memory bmap extent record.
1952 xfs_bmbt_get_blockcount(
1955 return (xfs_filblks_t
)(r
->l1
& XFS_MASK64LO(21));
1959 * Extract the startblock field from an in memory bmap extent record.
1962 xfs_bmbt_get_startblock(
1966 return (((xfs_fsblock_t
)r
->l0
& XFS_MASK64LO(9)) << 43) |
1967 (((xfs_fsblock_t
)r
->l1
) >> 21);
1972 b
= (((xfs_dfsbno_t
)r
->l0
& XFS_MASK64LO(9)) << 43) |
1973 (((xfs_dfsbno_t
)r
->l1
) >> 21);
1974 ASSERT((b
>> 32) == 0 || ISNULLDSTARTBLOCK(b
));
1975 return (xfs_fsblock_t
)b
;
1977 return (xfs_fsblock_t
)(((xfs_dfsbno_t
)r
->l1
) >> 21);
1979 #endif /* XFS_BIG_BLKNOS */
1983 * Extract the startoff field from an in memory bmap extent record.
1986 xfs_bmbt_get_startoff(
1989 return ((xfs_fileoff_t
)r
->l0
&
1990 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
1999 ext_flag
= (int)((r
->l0
) >> (64 - BMBT_EXNTFLAG_BITLEN
));
2000 return xfs_extent_state(xfs_bmbt_get_blockcount(r
),
2004 #ifndef XFS_NATIVE_HOST
2005 /* Endian flipping versions of the bmbt extraction functions */
2007 xfs_bmbt_disk_get_all(
2013 l0
= INT_GET(r
->l0
, ARCH_CONVERT
);
2014 l1
= INT_GET(r
->l1
, ARCH_CONVERT
);
2016 __xfs_bmbt_get_all(l0
, l1
, s
);
2020 * Extract the blockcount field from an on disk bmap extent record.
2023 xfs_bmbt_disk_get_blockcount(
2026 return (xfs_filblks_t
)(INT_GET(r
->l1
, ARCH_CONVERT
) & XFS_MASK64LO(21));
2030 * Extract the startblock field from an on disk bmap extent record.
2033 xfs_bmbt_disk_get_startblock(
2037 return (((xfs_fsblock_t
)INT_GET(r
->l0
, ARCH_CONVERT
) & XFS_MASK64LO(9)) << 43) |
2038 (((xfs_fsblock_t
)INT_GET(r
->l1
, ARCH_CONVERT
)) >> 21);
2043 b
= (((xfs_dfsbno_t
)INT_GET(r
->l0
, ARCH_CONVERT
) & XFS_MASK64LO(9)) << 43) |
2044 (((xfs_dfsbno_t
)INT_GET(r
->l1
, ARCH_CONVERT
)) >> 21);
2045 ASSERT((b
>> 32) == 0 || ISNULLDSTARTBLOCK(b
));
2046 return (xfs_fsblock_t
)b
;
2048 return (xfs_fsblock_t
)(((xfs_dfsbno_t
)INT_GET(r
->l1
, ARCH_CONVERT
)) >> 21);
2050 #endif /* XFS_BIG_BLKNOS */
2054 * Extract the startoff field from a disk format bmap extent record.
2057 xfs_bmbt_disk_get_startoff(
2060 return ((xfs_fileoff_t
)INT_GET(r
->l0
, ARCH_CONVERT
) &
2061 XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
2065 xfs_bmbt_disk_get_state(
2070 ext_flag
= (int)((INT_GET(r
->l0
, ARCH_CONVERT
)) >> (64 - BMBT_EXNTFLAG_BITLEN
));
2071 return xfs_extent_state(xfs_bmbt_disk_get_blockcount(r
),
2074 #endif /* XFS_NATIVE_HOST */
2078 * Increment cursor by one record at the level.
2079 * For nonzero levels the leaf-ward information is untouched.
2083 xfs_btree_cur_t
*cur
,
2085 int *stat
) /* success/failure */
2087 xfs_bmbt_block_t
*block
;
2089 int error
; /* error return value */
2090 #ifdef XFS_BMBT_TRACE
2091 static char fname
[] = "xfs_bmbt_increment";
2093 xfs_fsblock_t fsbno
;
2098 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2099 XFS_BMBT_TRACE_ARGI(cur
, level
);
2100 ASSERT(level
< cur
->bc_nlevels
);
2101 if (level
< cur
->bc_nlevels
- 1)
2102 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
2103 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
2105 if ((error
= xfs_btree_check_lblock(cur
, block
, level
, bp
))) {
2106 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2110 if (++cur
->bc_ptrs
[level
] <= be16_to_cpu(block
->bb_numrecs
)) {
2111 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2115 if (be64_to_cpu(block
->bb_rightsib
) == NULLDFSBNO
) {
2116 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2120 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
2121 block
= xfs_bmbt_get_block(cur
, lev
, &bp
);
2123 if ((error
= xfs_btree_check_lblock(cur
, block
, lev
, bp
))) {
2124 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2128 if (++cur
->bc_ptrs
[lev
] <= be16_to_cpu(block
->bb_numrecs
))
2130 if (lev
< cur
->bc_nlevels
- 1)
2131 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_RIGHTRA
);
2133 if (lev
== cur
->bc_nlevels
) {
2134 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2140 for (block
= xfs_bmbt_get_block(cur
, lev
, &bp
); lev
> level
; ) {
2141 fsbno
= INT_GET(*XFS_BMAP_PTR_IADDR(block
, cur
->bc_ptrs
[lev
], cur
), ARCH_CONVERT
);
2142 if ((error
= xfs_btree_read_bufl(mp
, tp
, fsbno
, 0, &bp
,
2143 XFS_BMAP_BTREE_REF
))) {
2144 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2148 xfs_btree_setbuf(cur
, lev
, bp
);
2149 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
2150 if ((error
= xfs_btree_check_lblock(cur
, block
, lev
, bp
))) {
2151 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2154 cur
->bc_ptrs
[lev
] = 1;
2156 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2162 * Insert the current record at the point referenced by cur.
2166 xfs_btree_cur_t
*cur
,
2167 int *stat
) /* success/failure */
2169 int error
; /* error return value */
2170 #ifdef XFS_BMBT_TRACE
2171 static char fname
[] = "xfs_bmbt_insert";
2176 xfs_btree_cur_t
*ncur
;
2177 xfs_bmbt_rec_t nrec
;
2178 xfs_btree_cur_t
*pcur
;
2180 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2183 xfs_bmbt_disk_set_all(&nrec
, &cur
->bc_rec
.b
);
2184 ncur
= (xfs_btree_cur_t
*)0;
2187 if ((error
= xfs_bmbt_insrec(pcur
, level
++, &nbno
, &nrec
, &ncur
,
2190 xfs_btree_del_cursor(pcur
, XFS_BTREE_ERROR
);
2191 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2194 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
2195 if (pcur
!= cur
&& (ncur
|| nbno
== NULLFSBLOCK
)) {
2196 cur
->bc_nlevels
= pcur
->bc_nlevels
;
2197 cur
->bc_private
.b
.allocated
+=
2198 pcur
->bc_private
.b
.allocated
;
2199 pcur
->bc_private
.b
.allocated
= 0;
2200 ASSERT((cur
->bc_private
.b
.firstblock
!= NULLFSBLOCK
) ||
2201 (cur
->bc_private
.b
.ip
->i_d
.di_flags
&
2202 XFS_DIFLAG_REALTIME
));
2203 cur
->bc_private
.b
.firstblock
=
2204 pcur
->bc_private
.b
.firstblock
;
2205 ASSERT(cur
->bc_private
.b
.flist
==
2206 pcur
->bc_private
.b
.flist
);
2207 xfs_btree_del_cursor(pcur
, XFS_BTREE_NOERROR
);
2211 ncur
= (xfs_btree_cur_t
*)0;
2213 } while (nbno
!= NULLFSBLOCK
);
2214 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2218 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2223 * Log fields from the btree block header.
2227 xfs_btree_cur_t
*cur
,
2232 #ifdef XFS_BMBT_TRACE
2233 static char fname
[] = "xfs_bmbt_log_block";
2237 static const short offsets
[] = {
2238 offsetof(xfs_bmbt_block_t
, bb_magic
),
2239 offsetof(xfs_bmbt_block_t
, bb_level
),
2240 offsetof(xfs_bmbt_block_t
, bb_numrecs
),
2241 offsetof(xfs_bmbt_block_t
, bb_leftsib
),
2242 offsetof(xfs_bmbt_block_t
, bb_rightsib
),
2243 sizeof(xfs_bmbt_block_t
)
2246 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2247 XFS_BMBT_TRACE_ARGBI(cur
, bp
, fields
);
2250 xfs_btree_offsets(fields
, offsets
, XFS_BB_NUM_BITS
, &first
,
2252 xfs_trans_log_buf(tp
, bp
, first
, last
);
2254 xfs_trans_log_inode(tp
, cur
->bc_private
.b
.ip
,
2255 XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
));
2256 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2260 * Log record values from the btree block.
2264 xfs_btree_cur_t
*cur
,
2269 xfs_bmbt_block_t
*block
;
2271 #ifdef XFS_BMBT_TRACE
2272 static char fname
[] = "xfs_bmbt_log_recs";
2278 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2279 XFS_BMBT_TRACE_ARGBII(cur
, bp
, rfirst
, rlast
);
2282 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
2283 rp
= XFS_BMAP_REC_DADDR(block
, 1, cur
);
2284 first
= (int)((xfs_caddr_t
)&rp
[rfirst
- 1] - (xfs_caddr_t
)block
);
2285 last
= (int)(((xfs_caddr_t
)&rp
[rlast
] - 1) - (xfs_caddr_t
)block
);
2286 xfs_trans_log_buf(tp
, bp
, first
, last
);
2287 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2292 xfs_btree_cur_t
*cur
,
2296 int *stat
) /* success/failure */
2298 cur
->bc_rec
.b
.br_startoff
= off
;
2299 cur
->bc_rec
.b
.br_startblock
= bno
;
2300 cur
->bc_rec
.b
.br_blockcount
= len
;
2301 return xfs_bmbt_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
2306 xfs_btree_cur_t
*cur
,
2310 int *stat
) /* success/failure */
2312 cur
->bc_rec
.b
.br_startoff
= off
;
2313 cur
->bc_rec
.b
.br_startblock
= bno
;
2314 cur
->bc_rec
.b
.br_blockcount
= len
;
2315 return xfs_bmbt_lookup(cur
, XFS_LOOKUP_GE
, stat
);
2319 * Give the bmap btree a new root block. Copy the old broot contents
2320 * down into a real block and make the broot point to it.
2324 xfs_btree_cur_t
*cur
, /* btree cursor */
2325 int *logflags
, /* logging flags for inode */
2326 int *stat
) /* return status - 0 fail */
2328 xfs_alloc_arg_t args
; /* allocation arguments */
2329 xfs_bmbt_block_t
*block
; /* bmap btree block */
2330 xfs_buf_t
*bp
; /* buffer for block */
2331 xfs_bmbt_block_t
*cblock
; /* child btree block */
2332 xfs_bmbt_key_t
*ckp
; /* child key pointer */
2333 xfs_bmbt_ptr_t
*cpp
; /* child ptr pointer */
2334 int error
; /* error return code */
2335 #ifdef XFS_BMBT_TRACE
2336 static char fname
[] = "xfs_bmbt_newroot";
2339 int i
; /* loop counter */
2341 xfs_bmbt_key_t
*kp
; /* pointer to bmap btree key */
2342 int level
; /* btree level */
2343 xfs_bmbt_ptr_t
*pp
; /* pointer to bmap block addr */
2345 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2346 level
= cur
->bc_nlevels
- 1;
2347 block
= xfs_bmbt_get_block(cur
, level
, &bp
);
2349 * Copy the root into a real block.
2351 args
.mp
= cur
->bc_mp
;
2352 pp
= XFS_BMAP_PTR_IADDR(block
, 1, cur
);
2353 args
.tp
= cur
->bc_tp
;
2354 args
.fsbno
= cur
->bc_private
.b
.firstblock
;
2355 args
.mod
= args
.minleft
= args
.alignment
= args
.total
= args
.isfl
=
2356 args
.userdata
= args
.minalignslop
= 0;
2357 args
.minlen
= args
.maxlen
= args
.prod
= 1;
2358 args
.wasdel
= cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
;
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
;
2368 } else if (args
.wasdel
)
2369 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
2371 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
2372 if ((error
= xfs_alloc_vextent(&args
))) {
2373 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2376 if (args
.fsbno
== NULLFSBLOCK
) {
2377 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2381 ASSERT(args
.len
== 1);
2382 cur
->bc_private
.b
.firstblock
= args
.fsbno
;
2383 cur
->bc_private
.b
.allocated
++;
2384 cur
->bc_private
.b
.ip
->i_d
.di_nblocks
++;
2385 XFS_TRANS_MOD_DQUOT_BYINO(args
.mp
, args
.tp
, cur
->bc_private
.b
.ip
,
2386 XFS_TRANS_DQ_BCOUNT
, 1L);
2387 bp
= xfs_btree_get_bufl(args
.mp
, cur
->bc_tp
, args
.fsbno
, 0);
2388 cblock
= XFS_BUF_TO_BMBT_BLOCK(bp
);
2390 be16_add(&block
->bb_level
, 1);
2391 block
->bb_numrecs
= cpu_to_be16(1);
2393 cur
->bc_ptrs
[level
+ 1] = 1;
2394 kp
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
2395 ckp
= XFS_BMAP_KEY_IADDR(cblock
, 1, cur
);
2396 memcpy(ckp
, kp
, be16_to_cpu(cblock
->bb_numrecs
) * sizeof(*kp
));
2397 cpp
= XFS_BMAP_PTR_IADDR(cblock
, 1, cur
);
2399 for (i
= 0; i
< be16_to_cpu(cblock
->bb_numrecs
); i
++) {
2400 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(pp
[i
], ARCH_CONVERT
), level
))) {
2401 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2406 memcpy(cpp
, pp
, be16_to_cpu(cblock
->bb_numrecs
) * sizeof(*pp
));
2408 if ((error
= xfs_btree_check_lptr(cur
, (xfs_bmbt_ptr_t
)args
.fsbno
,
2410 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2414 INT_SET(*pp
, ARCH_CONVERT
, args
.fsbno
);
2415 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, 1 - be16_to_cpu(cblock
->bb_numrecs
),
2416 cur
->bc_private
.b
.whichfork
);
2417 xfs_btree_setbuf(cur
, level
, bp
);
2419 * Do all this logging at the end so that
2420 * the root is at the right level.
2422 xfs_bmbt_log_block(cur
, bp
, XFS_BB_ALL_BITS
);
2423 xfs_bmbt_log_keys(cur
, bp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2424 xfs_bmbt_log_ptrs(cur
, bp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2425 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2427 XFS_ILOG_CORE
| XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
);
2433 * Set all the fields in a bmap extent record from the uncompressed form.
2442 ASSERT((s
->br_state
== XFS_EXT_NORM
) ||
2443 (s
->br_state
== XFS_EXT_UNWRITTEN
));
2444 extent_flag
= (s
->br_state
== XFS_EXT_NORM
) ? 0 : 1;
2445 ASSERT((s
->br_startoff
& XFS_MASK64HI(9)) == 0);
2446 ASSERT((s
->br_blockcount
& XFS_MASK64HI(43)) == 0);
2448 ASSERT((s
->br_startblock
& XFS_MASK64HI(12)) == 0);
2449 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2450 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9) |
2451 ((xfs_bmbt_rec_base_t
)s
->br_startblock
>> 43);
2452 r
->l1
= ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2453 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2454 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2455 #else /* !XFS_BIG_BLKNOS */
2456 if (ISNULLSTARTBLOCK(s
->br_startblock
)) {
2457 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2458 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9) |
2459 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9);
2460 r
->l1
= XFS_MASK64HI(11) |
2461 ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2462 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2463 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2465 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2466 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9);
2467 r
->l1
= ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2468 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2469 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2471 #endif /* XFS_BIG_BLKNOS */
2475 * Set all the fields in a bmap extent record from the arguments.
2487 ASSERT((v
== XFS_EXT_NORM
) || (v
== XFS_EXT_UNWRITTEN
));
2488 extent_flag
= (v
== XFS_EXT_NORM
) ? 0 : 1;
2489 ASSERT((o
& XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN
)) == 0);
2490 ASSERT((c
& XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN
)) == 0);
2492 ASSERT((b
& XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN
)) == 0);
2493 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2494 ((xfs_bmbt_rec_base_t
)o
<< 9) |
2495 ((xfs_bmbt_rec_base_t
)b
>> 43);
2496 r
->l1
= ((xfs_bmbt_rec_base_t
)b
<< 21) |
2497 ((xfs_bmbt_rec_base_t
)c
&
2498 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2499 #else /* !XFS_BIG_BLKNOS */
2500 if (ISNULLSTARTBLOCK(b
)) {
2501 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2502 ((xfs_bmbt_rec_base_t
)o
<< 9) |
2503 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9);
2504 r
->l1
= XFS_MASK64HI(11) |
2505 ((xfs_bmbt_rec_base_t
)b
<< 21) |
2506 ((xfs_bmbt_rec_base_t
)c
&
2507 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2509 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2510 ((xfs_bmbt_rec_base_t
)o
<< 9);
2511 r
->l1
= ((xfs_bmbt_rec_base_t
)b
<< 21) |
2512 ((xfs_bmbt_rec_base_t
)c
&
2513 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2515 #endif /* XFS_BIG_BLKNOS */
2518 #ifndef XFS_NATIVE_HOST
2520 * Set all the fields in a bmap extent record from the uncompressed form.
2523 xfs_bmbt_disk_set_all(
2529 ASSERT((s
->br_state
== XFS_EXT_NORM
) ||
2530 (s
->br_state
== XFS_EXT_UNWRITTEN
));
2531 extent_flag
= (s
->br_state
== XFS_EXT_NORM
) ? 0 : 1;
2532 ASSERT((s
->br_startoff
& XFS_MASK64HI(9)) == 0);
2533 ASSERT((s
->br_blockcount
& XFS_MASK64HI(43)) == 0);
2535 ASSERT((s
->br_startblock
& XFS_MASK64HI(12)) == 0);
2536 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2537 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9) |
2538 ((xfs_bmbt_rec_base_t
)s
->br_startblock
>> 43));
2539 INT_SET(r
->l1
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2540 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2541 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2542 #else /* !XFS_BIG_BLKNOS */
2543 if (ISNULLSTARTBLOCK(s
->br_startblock
)) {
2544 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2545 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9) |
2546 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9));
2547 INT_SET(r
->l1
, ARCH_CONVERT
, XFS_MASK64HI(11) |
2548 ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2549 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2550 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2552 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2553 ((xfs_bmbt_rec_base_t
)s
->br_startoff
<< 9));
2554 INT_SET(r
->l1
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)s
->br_startblock
<< 21) |
2555 ((xfs_bmbt_rec_base_t
)s
->br_blockcount
&
2556 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2558 #endif /* XFS_BIG_BLKNOS */
2562 * Set all the fields in a disk format bmap extent record from the arguments.
2565 xfs_bmbt_disk_set_allf(
2574 ASSERT((v
== XFS_EXT_NORM
) || (v
== XFS_EXT_UNWRITTEN
));
2575 extent_flag
= (v
== XFS_EXT_NORM
) ? 0 : 1;
2576 ASSERT((o
& XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN
)) == 0);
2577 ASSERT((c
& XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN
)) == 0);
2579 ASSERT((b
& XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN
)) == 0);
2580 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2581 ((xfs_bmbt_rec_base_t
)o
<< 9) |
2582 ((xfs_bmbt_rec_base_t
)b
>> 43));
2583 INT_SET(r
->l1
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)b
<< 21) |
2584 ((xfs_bmbt_rec_base_t
)c
&
2585 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2586 #else /* !XFS_BIG_BLKNOS */
2587 if (ISNULLSTARTBLOCK(b
)) {
2588 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2589 ((xfs_bmbt_rec_base_t
)o
<< 9) |
2590 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9));
2591 INT_SET(r
->l1
, ARCH_CONVERT
, XFS_MASK64HI(11) |
2592 ((xfs_bmbt_rec_base_t
)b
<< 21) |
2593 ((xfs_bmbt_rec_base_t
)c
&
2594 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2596 INT_SET(r
->l0
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
2597 ((xfs_bmbt_rec_base_t
)o
<< 9));
2598 INT_SET(r
->l1
, ARCH_CONVERT
, ((xfs_bmbt_rec_base_t
)b
<< 21) |
2599 ((xfs_bmbt_rec_base_t
)c
&
2600 (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)));
2602 #endif /* XFS_BIG_BLKNOS */
2604 #endif /* XFS_NATIVE_HOST */
2607 * Set the blockcount field in a bmap extent record.
2610 xfs_bmbt_set_blockcount(
2614 ASSERT((v
& XFS_MASK64HI(43)) == 0);
2615 r
->l1
= (r
->l1
& (xfs_bmbt_rec_base_t
)XFS_MASK64HI(43)) |
2616 (xfs_bmbt_rec_base_t
)(v
& XFS_MASK64LO(21));
2620 * Set the startblock field in a bmap extent record.
2623 xfs_bmbt_set_startblock(
2628 ASSERT((v
& XFS_MASK64HI(12)) == 0);
2629 r
->l0
= (r
->l0
& (xfs_bmbt_rec_base_t
)XFS_MASK64HI(55)) |
2630 (xfs_bmbt_rec_base_t
)(v
>> 43);
2631 r
->l1
= (r
->l1
& (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21)) |
2632 (xfs_bmbt_rec_base_t
)(v
<< 21);
2633 #else /* !XFS_BIG_BLKNOS */
2634 if (ISNULLSTARTBLOCK(v
)) {
2635 r
->l0
|= (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9);
2636 r
->l1
= (xfs_bmbt_rec_base_t
)XFS_MASK64HI(11) |
2637 ((xfs_bmbt_rec_base_t
)v
<< 21) |
2638 (r
->l1
& (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2640 r
->l0
&= ~(xfs_bmbt_rec_base_t
)XFS_MASK64LO(9);
2641 r
->l1
= ((xfs_bmbt_rec_base_t
)v
<< 21) |
2642 (r
->l1
& (xfs_bmbt_rec_base_t
)XFS_MASK64LO(21));
2644 #endif /* XFS_BIG_BLKNOS */
2648 * Set the startoff field in a bmap extent record.
2651 xfs_bmbt_set_startoff(
2655 ASSERT((v
& XFS_MASK64HI(9)) == 0);
2656 r
->l0
= (r
->l0
& (xfs_bmbt_rec_base_t
) XFS_MASK64HI(1)) |
2657 ((xfs_bmbt_rec_base_t
)v
<< 9) |
2658 (r
->l0
& (xfs_bmbt_rec_base_t
)XFS_MASK64LO(9));
2662 * Set the extent state field in a bmap extent record.
2669 ASSERT(v
== XFS_EXT_NORM
|| v
== XFS_EXT_UNWRITTEN
);
2670 if (v
== XFS_EXT_NORM
)
2671 r
->l0
&= XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN
);
2673 r
->l0
|= XFS_MASK64HI(BMBT_EXNTFLAG_BITLEN
);
2677 * Convert in-memory form of btree root to on-disk form.
2681 xfs_bmbt_block_t
*rblock
,
2683 xfs_bmdr_block_t
*dblock
,
2687 xfs_bmbt_key_t
*fkp
;
2688 xfs_bmbt_ptr_t
*fpp
;
2689 xfs_bmbt_key_t
*tkp
;
2690 xfs_bmbt_ptr_t
*tpp
;
2692 ASSERT(be32_to_cpu(rblock
->bb_magic
) == XFS_BMAP_MAGIC
);
2693 ASSERT(be64_to_cpu(rblock
->bb_leftsib
) == NULLDFSBNO
);
2694 ASSERT(be64_to_cpu(rblock
->bb_rightsib
) == NULLDFSBNO
);
2695 ASSERT(be16_to_cpu(rblock
->bb_level
) > 0);
2696 dblock
->bb_level
= rblock
->bb_level
;
2697 dblock
->bb_numrecs
= rblock
->bb_numrecs
;
2698 dmxr
= (int)XFS_BTREE_BLOCK_MAXRECS(dblocklen
, xfs_bmdr
, 0);
2699 fkp
= XFS_BMAP_BROOT_KEY_ADDR(rblock
, 1, rblocklen
);
2700 tkp
= XFS_BTREE_KEY_ADDR(dblocklen
, xfs_bmdr
, dblock
, 1, dmxr
);
2701 fpp
= XFS_BMAP_BROOT_PTR_ADDR(rblock
, 1, rblocklen
);
2702 tpp
= XFS_BTREE_PTR_ADDR(dblocklen
, xfs_bmdr
, dblock
, 1, dmxr
);
2703 dmxr
= be16_to_cpu(dblock
->bb_numrecs
);
2704 memcpy(tkp
, fkp
, sizeof(*fkp
) * dmxr
);
2705 memcpy(tpp
, fpp
, sizeof(*fpp
) * dmxr
); /* INT_: direct copy */
2709 * Update the record to the passed values.
2713 xfs_btree_cur_t
*cur
,
2719 xfs_bmbt_block_t
*block
;
2722 #ifdef XFS_BMBT_TRACE
2723 static char fname
[] = "xfs_bmbt_update";
2729 XFS_BMBT_TRACE_CURSOR(cur
, ENTRY
);
2730 XFS_BMBT_TRACE_ARGFFFI(cur
, (xfs_dfiloff_t
)off
, (xfs_dfsbno_t
)bno
,
2731 (xfs_dfilblks_t
)len
, (int)state
);
2732 block
= xfs_bmbt_get_block(cur
, 0, &bp
);
2734 if ((error
= xfs_btree_check_lblock(cur
, block
, 0, bp
))) {
2735 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2739 ptr
= cur
->bc_ptrs
[0];
2740 rp
= XFS_BMAP_REC_IADDR(block
, ptr
, cur
);
2741 xfs_bmbt_disk_set_allf(rp
, off
, bno
, len
, state
);
2742 xfs_bmbt_log_recs(cur
, bp
, ptr
, ptr
);
2744 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2747 INT_SET(key
.br_startoff
, ARCH_CONVERT
, off
);
2748 if ((error
= xfs_bmbt_updkey(cur
, &key
, 1))) {
2749 XFS_BMBT_TRACE_CURSOR(cur
, ERROR
);
2752 XFS_BMBT_TRACE_CURSOR(cur
, EXIT
);
2757 * Check an extent list, which has just been read, for
2758 * any bit in the extent flag field. ASSERT on debug
2759 * kernels, as this condition should not occur.
2760 * Return an error condition (1) if any flags found,
2761 * otherwise return 0.
2765 xfs_check_nostate_extents(
2769 for (; num
> 0; num
--, ep
++) {
2771 (64 - BMBT_EXNTFLAG_BITLEN
)) != 0) {