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
18 #include "libxfs_priv.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_inode.h"
28 #include "xfs_trans.h"
29 #include "xfs_alloc.h"
30 #include "xfs_btree.h"
31 #include "xfs_bmap_btree.h"
33 #include "xfs_trace.h"
34 #include "xfs_cksum.h"
38 * Determine the extent state.
47 ASSERT(blks
!= 0); /* saved for DMIG */
48 return XFS_EXT_UNWRITTEN
;
54 * Convert on-disk form of btree root to in-memory form.
59 xfs_bmdr_block_t
*dblock
,
61 struct xfs_btree_block
*rblock
,
64 struct xfs_mount
*mp
= ip
->i_mount
;
71 xfs_btree_init_block_int(mp
, rblock
, XFS_BUF_DADDR_NULL
,
72 XFS_BTNUM_BMAP
, 0, 0, ip
->i_ino
,
74 rblock
->bb_level
= dblock
->bb_level
;
75 ASSERT(be16_to_cpu(rblock
->bb_level
) > 0);
76 rblock
->bb_numrecs
= dblock
->bb_numrecs
;
77 dmxr
= xfs_bmdr_maxrecs(dblocklen
, 0);
78 fkp
= XFS_BMDR_KEY_ADDR(dblock
, 1);
79 tkp
= XFS_BMBT_KEY_ADDR(mp
, rblock
, 1);
80 fpp
= XFS_BMDR_PTR_ADDR(dblock
, 1, dmxr
);
81 tpp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, rblocklen
);
82 dmxr
= be16_to_cpu(dblock
->bb_numrecs
);
83 memcpy(tkp
, fkp
, sizeof(*fkp
) * dmxr
);
84 memcpy(tpp
, fpp
, sizeof(*fpp
) * dmxr
);
88 * Convert a compressed bmap extent record to an uncompressed form.
89 * This code must be in sync with the routines xfs_bmbt_get_startoff,
90 * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state.
101 ext_flag
= (int)(l0
>> (64 - BMBT_EXNTFLAG_BITLEN
));
102 s
->br_startoff
= ((xfs_fileoff_t
)l0
&
103 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
104 s
->br_startblock
= (((xfs_fsblock_t
)l0
& xfs_mask64lo(9)) << 43) |
105 (((xfs_fsblock_t
)l1
) >> 21);
106 s
->br_blockcount
= (xfs_filblks_t
)(l1
& xfs_mask64lo(21));
107 /* This is xfs_extent_state() in-line */
109 ASSERT(s
->br_blockcount
!= 0); /* saved for DMIG */
110 st
= XFS_EXT_UNWRITTEN
;
118 xfs_bmbt_rec_host_t
*r
,
121 __xfs_bmbt_get_all(r
->l0
, r
->l1
, s
);
125 * Extract the blockcount field from an in memory bmap extent record.
128 xfs_bmbt_get_blockcount(
129 xfs_bmbt_rec_host_t
*r
)
131 return (xfs_filblks_t
)(r
->l1
& xfs_mask64lo(21));
135 * Extract the startblock field from an in memory bmap extent record.
138 xfs_bmbt_get_startblock(
139 xfs_bmbt_rec_host_t
*r
)
141 return (((xfs_fsblock_t
)r
->l0
& xfs_mask64lo(9)) << 43) |
142 (((xfs_fsblock_t
)r
->l1
) >> 21);
146 * Extract the startoff field from an in memory bmap extent record.
149 xfs_bmbt_get_startoff(
150 xfs_bmbt_rec_host_t
*r
)
152 return ((xfs_fileoff_t
)r
->l0
&
153 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
158 xfs_bmbt_rec_host_t
*r
)
162 ext_flag
= (int)((r
->l0
) >> (64 - BMBT_EXNTFLAG_BITLEN
));
163 return xfs_extent_state(xfs_bmbt_get_blockcount(r
),
168 * Extract the blockcount field from an on disk bmap extent record.
171 xfs_bmbt_disk_get_blockcount(
174 return (xfs_filblks_t
)(be64_to_cpu(r
->l1
) & xfs_mask64lo(21));
178 * Extract the startoff field from a disk format bmap extent record.
181 xfs_bmbt_disk_get_startoff(
184 return ((xfs_fileoff_t
)be64_to_cpu(r
->l0
) &
185 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
190 * Set all the fields in a bmap extent record from the arguments.
194 xfs_bmbt_rec_host_t
*r
,
195 xfs_fileoff_t startoff
,
196 xfs_fsblock_t startblock
,
197 xfs_filblks_t blockcount
,
200 int extent_flag
= (state
== XFS_EXT_NORM
) ? 0 : 1;
202 ASSERT(state
== XFS_EXT_NORM
|| state
== XFS_EXT_UNWRITTEN
);
203 ASSERT((startoff
& xfs_mask64hi(64-BMBT_STARTOFF_BITLEN
)) == 0);
204 ASSERT((blockcount
& xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN
)) == 0);
206 ASSERT((startblock
& xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN
)) == 0);
208 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
209 ((xfs_bmbt_rec_base_t
)startoff
<< 9) |
210 ((xfs_bmbt_rec_base_t
)startblock
>> 43);
211 r
->l1
= ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
212 ((xfs_bmbt_rec_base_t
)blockcount
&
213 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21));
217 * Set all the fields in a bmap extent record from the uncompressed form.
221 xfs_bmbt_rec_host_t
*r
,
224 xfs_bmbt_set_allf(r
, s
->br_startoff
, s
->br_startblock
,
225 s
->br_blockcount
, s
->br_state
);
230 * Set all the fields in a disk format bmap extent record from the arguments.
233 xfs_bmbt_disk_set_allf(
235 xfs_fileoff_t startoff
,
236 xfs_fsblock_t startblock
,
237 xfs_filblks_t blockcount
,
240 int extent_flag
= (state
== XFS_EXT_NORM
) ? 0 : 1;
242 ASSERT(state
== XFS_EXT_NORM
|| state
== XFS_EXT_UNWRITTEN
);
243 ASSERT((startoff
& xfs_mask64hi(64-BMBT_STARTOFF_BITLEN
)) == 0);
244 ASSERT((blockcount
& xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN
)) == 0);
245 ASSERT((startblock
& xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN
)) == 0);
248 ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
249 ((xfs_bmbt_rec_base_t
)startoff
<< 9) |
250 ((xfs_bmbt_rec_base_t
)startblock
>> 43));
252 ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
253 ((xfs_bmbt_rec_base_t
)blockcount
&
254 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21)));
258 * Set all the fields in a bmap extent record from the uncompressed form.
261 xfs_bmbt_disk_set_all(
265 xfs_bmbt_disk_set_allf(r
, s
->br_startoff
, s
->br_startblock
,
266 s
->br_blockcount
, s
->br_state
);
270 * Set the blockcount field in a bmap extent record.
273 xfs_bmbt_set_blockcount(
274 xfs_bmbt_rec_host_t
*r
,
277 ASSERT((v
& xfs_mask64hi(43)) == 0);
278 r
->l1
= (r
->l1
& (xfs_bmbt_rec_base_t
)xfs_mask64hi(43)) |
279 (xfs_bmbt_rec_base_t
)(v
& xfs_mask64lo(21));
283 * Set the startblock field in a bmap extent record.
286 xfs_bmbt_set_startblock(
287 xfs_bmbt_rec_host_t
*r
,
290 ASSERT((v
& xfs_mask64hi(12)) == 0);
291 r
->l0
= (r
->l0
& (xfs_bmbt_rec_base_t
)xfs_mask64hi(55)) |
292 (xfs_bmbt_rec_base_t
)(v
>> 43);
293 r
->l1
= (r
->l1
& (xfs_bmbt_rec_base_t
)xfs_mask64lo(21)) |
294 (xfs_bmbt_rec_base_t
)(v
<< 21);
298 * Set the startoff field in a bmap extent record.
301 xfs_bmbt_set_startoff(
302 xfs_bmbt_rec_host_t
*r
,
305 ASSERT((v
& xfs_mask64hi(9)) == 0);
306 r
->l0
= (r
->l0
& (xfs_bmbt_rec_base_t
) xfs_mask64hi(1)) |
307 ((xfs_bmbt_rec_base_t
)v
<< 9) |
308 (r
->l0
& (xfs_bmbt_rec_base_t
)xfs_mask64lo(9));
312 * Set the extent state field in a bmap extent record.
316 xfs_bmbt_rec_host_t
*r
,
319 ASSERT(v
== XFS_EXT_NORM
|| v
== XFS_EXT_UNWRITTEN
);
320 if (v
== XFS_EXT_NORM
)
321 r
->l0
&= xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN
);
323 r
->l0
|= xfs_mask64hi(BMBT_EXNTFLAG_BITLEN
);
327 * Convert in-memory form of btree root to on-disk form.
331 struct xfs_mount
*mp
,
332 struct xfs_btree_block
*rblock
,
334 xfs_bmdr_block_t
*dblock
,
343 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
344 ASSERT(rblock
->bb_magic
== cpu_to_be32(XFS_BMAP_CRC_MAGIC
));
345 ASSERT(uuid_equal(&rblock
->bb_u
.l
.bb_uuid
,
346 &mp
->m_sb
.sb_meta_uuid
));
347 ASSERT(rblock
->bb_u
.l
.bb_blkno
==
348 cpu_to_be64(XFS_BUF_DADDR_NULL
));
350 ASSERT(rblock
->bb_magic
== cpu_to_be32(XFS_BMAP_MAGIC
));
351 ASSERT(rblock
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLFSBLOCK
));
352 ASSERT(rblock
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLFSBLOCK
));
353 ASSERT(rblock
->bb_level
!= 0);
354 dblock
->bb_level
= rblock
->bb_level
;
355 dblock
->bb_numrecs
= rblock
->bb_numrecs
;
356 dmxr
= xfs_bmdr_maxrecs(dblocklen
, 0);
357 fkp
= XFS_BMBT_KEY_ADDR(mp
, rblock
, 1);
358 tkp
= XFS_BMDR_KEY_ADDR(dblock
, 1);
359 fpp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, rblocklen
);
360 tpp
= XFS_BMDR_PTR_ADDR(dblock
, 1, dmxr
);
361 dmxr
= be16_to_cpu(dblock
->bb_numrecs
);
362 memcpy(tkp
, fkp
, sizeof(*fkp
) * dmxr
);
363 memcpy(tpp
, fpp
, sizeof(*fpp
) * dmxr
);
366 STATIC
struct xfs_btree_cur
*
368 struct xfs_btree_cur
*cur
)
370 struct xfs_btree_cur
*new;
372 new = xfs_bmbt_init_cursor(cur
->bc_mp
, cur
->bc_tp
,
373 cur
->bc_private
.b
.ip
, cur
->bc_private
.b
.whichfork
);
376 * Copy the firstblock, dfops, and flags values,
377 * since init cursor doesn't get them.
379 new->bc_private
.b
.firstblock
= cur
->bc_private
.b
.firstblock
;
380 new->bc_private
.b
.dfops
= cur
->bc_private
.b
.dfops
;
381 new->bc_private
.b
.flags
= cur
->bc_private
.b
.flags
;
387 xfs_bmbt_update_cursor(
388 struct xfs_btree_cur
*src
,
389 struct xfs_btree_cur
*dst
)
391 ASSERT((dst
->bc_private
.b
.firstblock
!= NULLFSBLOCK
) ||
392 (dst
->bc_private
.b
.ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
));
393 ASSERT(dst
->bc_private
.b
.dfops
== src
->bc_private
.b
.dfops
);
395 dst
->bc_private
.b
.allocated
+= src
->bc_private
.b
.allocated
;
396 dst
->bc_private
.b
.firstblock
= src
->bc_private
.b
.firstblock
;
398 src
->bc_private
.b
.allocated
= 0;
402 xfs_bmbt_alloc_block(
403 struct xfs_btree_cur
*cur
,
404 union xfs_btree_ptr
*start
,
405 union xfs_btree_ptr
*new,
408 xfs_alloc_arg_t args
; /* block allocation args */
409 int error
; /* error return value */
411 memset(&args
, 0, sizeof(args
));
412 args
.tp
= cur
->bc_tp
;
413 args
.mp
= cur
->bc_mp
;
414 args
.fsbno
= cur
->bc_private
.b
.firstblock
;
415 args
.firstblock
= args
.fsbno
;
416 xfs_rmap_ino_bmbt_owner(&args
.oinfo
, cur
->bc_private
.b
.ip
->i_ino
,
417 cur
->bc_private
.b
.whichfork
);
419 if (args
.fsbno
== NULLFSBLOCK
) {
420 args
.fsbno
= be64_to_cpu(start
->l
);
421 args
.type
= XFS_ALLOCTYPE_START_BNO
;
423 * Make sure there is sufficient room left in the AG to
424 * complete a full tree split for an extent insert. If
425 * we are converting the middle part of an extent then
426 * we may need space for two tree splits.
428 * We are relying on the caller to make the correct block
429 * reservation for this operation to succeed. If the
430 * reservation amount is insufficient then we may fail a
431 * block allocation here and corrupt the filesystem.
433 args
.minleft
= args
.tp
->t_blk_res
;
434 } else if (cur
->bc_private
.b
.dfops
->dop_low
) {
435 args
.type
= XFS_ALLOCTYPE_START_BNO
;
437 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
440 args
.minlen
= args
.maxlen
= args
.prod
= 1;
441 args
.wasdel
= cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
;
442 if (!args
.wasdel
&& args
.tp
->t_blk_res
== 0) {
446 error
= xfs_alloc_vextent(&args
);
450 if (args
.fsbno
== NULLFSBLOCK
&& args
.minleft
) {
452 * Could not find an AG with enough free space to satisfy
453 * a full btree split. Try again and if
454 * successful activate the lowspace algorithm.
457 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
458 error
= xfs_alloc_vextent(&args
);
461 cur
->bc_private
.b
.dfops
->dop_low
= true;
463 if (WARN_ON_ONCE(args
.fsbno
== NULLFSBLOCK
)) {
464 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
468 ASSERT(args
.len
== 1);
469 cur
->bc_private
.b
.firstblock
= args
.fsbno
;
470 cur
->bc_private
.b
.allocated
++;
471 cur
->bc_private
.b
.ip
->i_d
.di_nblocks
++;
472 xfs_trans_log_inode(args
.tp
, cur
->bc_private
.b
.ip
, XFS_ILOG_CORE
);
473 xfs_trans_mod_dquot_byino(args
.tp
, cur
->bc_private
.b
.ip
,
474 XFS_TRANS_DQ_BCOUNT
, 1L);
476 new->l
= cpu_to_be64(args
.fsbno
);
478 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
483 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
489 struct xfs_btree_cur
*cur
,
492 struct xfs_mount
*mp
= cur
->bc_mp
;
493 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
494 struct xfs_trans
*tp
= cur
->bc_tp
;
495 xfs_fsblock_t fsbno
= XFS_DADDR_TO_FSB(mp
, XFS_BUF_ADDR(bp
));
496 struct xfs_owner_info oinfo
;
498 xfs_rmap_ino_bmbt_owner(&oinfo
, ip
->i_ino
, cur
->bc_private
.b
.whichfork
);
499 xfs_bmap_add_free(mp
, cur
->bc_private
.b
.dfops
, fsbno
, 1, &oinfo
);
500 ip
->i_d
.di_nblocks
--;
502 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
503 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
508 xfs_bmbt_get_minrecs(
509 struct xfs_btree_cur
*cur
,
512 if (level
== cur
->bc_nlevels
- 1) {
513 struct xfs_ifork
*ifp
;
515 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
,
516 cur
->bc_private
.b
.whichfork
);
518 return xfs_bmbt_maxrecs(cur
->bc_mp
,
519 ifp
->if_broot_bytes
, level
== 0) / 2;
522 return cur
->bc_mp
->m_bmap_dmnr
[level
!= 0];
526 xfs_bmbt_get_maxrecs(
527 struct xfs_btree_cur
*cur
,
530 if (level
== cur
->bc_nlevels
- 1) {
531 struct xfs_ifork
*ifp
;
533 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
,
534 cur
->bc_private
.b
.whichfork
);
536 return xfs_bmbt_maxrecs(cur
->bc_mp
,
537 ifp
->if_broot_bytes
, level
== 0);
540 return cur
->bc_mp
->m_bmap_dmxr
[level
!= 0];
545 * Get the maximum records we could store in the on-disk format.
547 * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but
548 * for the root node this checks the available space in the dinode fork
549 * so that we can resize the in-memory buffer to match it. After a
550 * resize to the maximum size this function returns the same value
551 * as xfs_bmbt_get_maxrecs for the root node, too.
554 xfs_bmbt_get_dmaxrecs(
555 struct xfs_btree_cur
*cur
,
558 if (level
!= cur
->bc_nlevels
- 1)
559 return cur
->bc_mp
->m_bmap_dmxr
[level
!= 0];
560 return xfs_bmdr_maxrecs(cur
->bc_private
.b
.forksize
, level
== 0);
564 xfs_bmbt_init_key_from_rec(
565 union xfs_btree_key
*key
,
566 union xfs_btree_rec
*rec
)
568 key
->bmbt
.br_startoff
=
569 cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec
->bmbt
));
573 xfs_bmbt_init_rec_from_cur(
574 struct xfs_btree_cur
*cur
,
575 union xfs_btree_rec
*rec
)
577 xfs_bmbt_disk_set_all(&rec
->bmbt
, &cur
->bc_rec
.b
);
581 xfs_bmbt_init_ptr_from_cur(
582 struct xfs_btree_cur
*cur
,
583 union xfs_btree_ptr
*ptr
)
590 struct xfs_btree_cur
*cur
,
591 union xfs_btree_key
*key
)
593 return (int64_t)be64_to_cpu(key
->bmbt
.br_startoff
) -
594 cur
->bc_rec
.b
.br_startoff
;
601 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
602 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
605 switch (block
->bb_magic
) {
606 case cpu_to_be32(XFS_BMAP_CRC_MAGIC
):
607 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
609 if (!uuid_equal(&block
->bb_u
.l
.bb_uuid
, &mp
->m_sb
.sb_meta_uuid
))
611 if (be64_to_cpu(block
->bb_u
.l
.bb_blkno
) != bp
->b_bn
)
614 * XXX: need a better way of verifying the owner here. Right now
615 * just make sure there has been one set.
617 if (be64_to_cpu(block
->bb_u
.l
.bb_owner
) == 0)
620 case cpu_to_be32(XFS_BMAP_MAGIC
):
627 * numrecs and level verification.
629 * We don't know what fork we belong to, so just verify that the level
630 * is less than the maximum of the two. Later checks will be more
633 level
= be16_to_cpu(block
->bb_level
);
634 if (level
> max(mp
->m_bm_maxlevels
[0], mp
->m_bm_maxlevels
[1]))
636 if (be16_to_cpu(block
->bb_numrecs
) > mp
->m_bmap_dmxr
[level
!= 0])
639 /* sibling pointer verification */
640 if (!block
->bb_u
.l
.bb_leftsib
||
641 (block
->bb_u
.l
.bb_leftsib
!= cpu_to_be64(NULLFSBLOCK
) &&
642 !XFS_FSB_SANITY_CHECK(mp
, be64_to_cpu(block
->bb_u
.l
.bb_leftsib
))))
644 if (!block
->bb_u
.l
.bb_rightsib
||
645 (block
->bb_u
.l
.bb_rightsib
!= cpu_to_be64(NULLFSBLOCK
) &&
646 !XFS_FSB_SANITY_CHECK(mp
, be64_to_cpu(block
->bb_u
.l
.bb_rightsib
))))
653 xfs_bmbt_read_verify(
656 if (!xfs_btree_lblock_verify_crc(bp
))
657 xfs_buf_ioerror(bp
, -EFSBADCRC
);
658 else if (!xfs_bmbt_verify(bp
))
659 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
662 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
663 xfs_verifier_error(bp
);
668 xfs_bmbt_write_verify(
671 if (!xfs_bmbt_verify(bp
)) {
672 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
673 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
674 xfs_verifier_error(bp
);
677 xfs_btree_lblock_calc_crc(bp
);
680 const struct xfs_buf_ops xfs_bmbt_buf_ops
= {
682 .verify_read
= xfs_bmbt_read_verify
,
683 .verify_write
= xfs_bmbt_write_verify
,
687 #if defined(DEBUG) || defined(XFS_WARN)
689 xfs_bmbt_keys_inorder(
690 struct xfs_btree_cur
*cur
,
691 union xfs_btree_key
*k1
,
692 union xfs_btree_key
*k2
)
694 return be64_to_cpu(k1
->bmbt
.br_startoff
) <
695 be64_to_cpu(k2
->bmbt
.br_startoff
);
699 xfs_bmbt_recs_inorder(
700 struct xfs_btree_cur
*cur
,
701 union xfs_btree_rec
*r1
,
702 union xfs_btree_rec
*r2
)
704 return xfs_bmbt_disk_get_startoff(&r1
->bmbt
) +
705 xfs_bmbt_disk_get_blockcount(&r1
->bmbt
) <=
706 xfs_bmbt_disk_get_startoff(&r2
->bmbt
);
710 static const struct xfs_btree_ops xfs_bmbt_ops
= {
711 .rec_len
= sizeof(xfs_bmbt_rec_t
),
712 .key_len
= sizeof(xfs_bmbt_key_t
),
714 .dup_cursor
= xfs_bmbt_dup_cursor
,
715 .update_cursor
= xfs_bmbt_update_cursor
,
716 .alloc_block
= xfs_bmbt_alloc_block
,
717 .free_block
= xfs_bmbt_free_block
,
718 .get_maxrecs
= xfs_bmbt_get_maxrecs
,
719 .get_minrecs
= xfs_bmbt_get_minrecs
,
720 .get_dmaxrecs
= xfs_bmbt_get_dmaxrecs
,
721 .init_key_from_rec
= xfs_bmbt_init_key_from_rec
,
722 .init_rec_from_cur
= xfs_bmbt_init_rec_from_cur
,
723 .init_ptr_from_cur
= xfs_bmbt_init_ptr_from_cur
,
724 .key_diff
= xfs_bmbt_key_diff
,
725 .buf_ops
= &xfs_bmbt_buf_ops
,
726 #if defined(DEBUG) || defined(XFS_WARN)
727 .keys_inorder
= xfs_bmbt_keys_inorder
,
728 .recs_inorder
= xfs_bmbt_recs_inorder
,
733 * Allocate a new bmap btree cursor.
735 struct xfs_btree_cur
* /* new bmap btree cursor */
736 xfs_bmbt_init_cursor(
737 struct xfs_mount
*mp
, /* file system mount point */
738 struct xfs_trans
*tp
, /* transaction pointer */
739 struct xfs_inode
*ip
, /* inode owning the btree */
740 int whichfork
) /* data or attr fork */
742 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
743 struct xfs_btree_cur
*cur
;
744 ASSERT(whichfork
!= XFS_COW_FORK
);
746 cur
= kmem_zone_zalloc(xfs_btree_cur_zone
, KM_NOFS
);
750 cur
->bc_nlevels
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
751 cur
->bc_btnum
= XFS_BTNUM_BMAP
;
752 cur
->bc_blocklog
= mp
->m_sb
.sb_blocklog
;
753 cur
->bc_statoff
= XFS_STATS_CALC_INDEX(xs_bmbt_2
);
755 cur
->bc_ops
= &xfs_bmbt_ops
;
756 cur
->bc_flags
= XFS_BTREE_LONG_PTRS
| XFS_BTREE_ROOT_IN_INODE
;
757 if (xfs_sb_version_hascrc(&mp
->m_sb
))
758 cur
->bc_flags
|= XFS_BTREE_CRC_BLOCKS
;
760 cur
->bc_private
.b
.forksize
= XFS_IFORK_SIZE(ip
, whichfork
);
761 cur
->bc_private
.b
.ip
= ip
;
762 cur
->bc_private
.b
.firstblock
= NULLFSBLOCK
;
763 cur
->bc_private
.b
.dfops
= NULL
;
764 cur
->bc_private
.b
.allocated
= 0;
765 cur
->bc_private
.b
.flags
= 0;
766 cur
->bc_private
.b
.whichfork
= whichfork
;
772 * Calculate number of records in a bmap btree block.
776 struct xfs_mount
*mp
,
780 blocklen
-= XFS_BMBT_BLOCK_LEN(mp
);
783 return blocklen
/ sizeof(xfs_bmbt_rec_t
);
784 return blocklen
/ (sizeof(xfs_bmbt_key_t
) + sizeof(xfs_bmbt_ptr_t
));
788 * Calculate number of records in a bmap btree inode root.
795 blocklen
-= sizeof(xfs_bmdr_block_t
);
798 return blocklen
/ sizeof(xfs_bmdr_rec_t
);
799 return blocklen
/ (sizeof(xfs_bmdr_key_t
) + sizeof(xfs_bmdr_ptr_t
));
803 * Change the owner of a btree format fork fo the inode passed in. Change it to
804 * the owner of that is passed in so that we can change owners before or after
805 * we switch forks between inodes. The operation that the caller is doing will
806 * determine whether is needs to change owner before or after the switch.
808 * For demand paged transactional modification, the fork switch should be done
809 * after reading in all the blocks, modifying them and pinning them in the
810 * transaction. For modification when the buffers are already pinned in memory,
811 * the fork switch can be done before changing the owner as we won't need to
812 * validate the owner until the btree buffers are unpinned and writes can occur
815 * For recovery based ownership change, there is no transactional context and
816 * so a buffer list must be supplied so that we can record the buffers that we
817 * modified for the caller to issue IO on.
820 xfs_bmbt_change_owner(
821 struct xfs_trans
*tp
,
822 struct xfs_inode
*ip
,
825 struct list_head
*buffer_list
)
827 struct xfs_btree_cur
*cur
;
830 ASSERT(tp
|| buffer_list
);
831 ASSERT(!(tp
&& buffer_list
));
832 if (whichfork
== XFS_DATA_FORK
)
833 ASSERT(ip
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
);
835 ASSERT(ip
->i_d
.di_aformat
== XFS_DINODE_FMT_BTREE
);
837 cur
= xfs_bmbt_init_cursor(ip
->i_mount
, tp
, ip
, whichfork
);
841 error
= xfs_btree_change_owner(cur
, new_owner
, buffer_list
);
842 xfs_btree_del_cursor(cur
, error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);