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"
23 #include "xfs_trans.h"
26 #include "xfs_mount.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_alloc_btree.h"
29 #include "xfs_ialloc_btree.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
32 #include "xfs_inode_item.h"
33 #include "xfs_alloc.h"
34 #include "xfs_btree.h"
35 #include "xfs_itable.h"
37 #include "xfs_error.h"
38 #include "xfs_quota.h"
41 * Determine the extent state.
50 ASSERT(blks
!= 0); /* saved for DMIG */
51 return XFS_EXT_UNWRITTEN
;
57 * Convert on-disk form of btree root to in-memory form.
62 xfs_bmdr_block_t
*dblock
,
64 struct xfs_btree_block
*rblock
,
73 rblock
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
74 rblock
->bb_level
= dblock
->bb_level
;
75 ASSERT(be16_to_cpu(rblock
->bb_level
) > 0);
76 rblock
->bb_numrecs
= dblock
->bb_numrecs
;
77 rblock
->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
78 rblock
->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
79 dmxr
= xfs_bmdr_maxrecs(mp
, dblocklen
, 0);
80 fkp
= XFS_BMDR_KEY_ADDR(dblock
, 1);
81 tkp
= XFS_BMBT_KEY_ADDR(mp
, rblock
, 1);
82 fpp
= XFS_BMDR_PTR_ADDR(dblock
, 1, dmxr
);
83 tpp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, rblocklen
);
84 dmxr
= be16_to_cpu(dblock
->bb_numrecs
);
85 memcpy(tkp
, fkp
, sizeof(*fkp
) * dmxr
);
86 memcpy(tpp
, fpp
, sizeof(*fpp
) * dmxr
);
90 * Convert a compressed bmap extent record to an uncompressed form.
91 * This code must be in sync with the routines xfs_bmbt_get_startoff,
92 * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state.
103 ext_flag
= (int)(l0
>> (64 - BMBT_EXNTFLAG_BITLEN
));
104 s
->br_startoff
= ((xfs_fileoff_t
)l0
&
105 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
107 s
->br_startblock
= (((xfs_fsblock_t
)l0
& xfs_mask64lo(9)) << 43) |
108 (((xfs_fsblock_t
)l1
) >> 21);
114 b
= (((xfs_dfsbno_t
)l0
& xfs_mask64lo(9)) << 43) |
115 (((xfs_dfsbno_t
)l1
) >> 21);
116 ASSERT((b
>> 32) == 0 || isnulldstartblock(b
));
117 s
->br_startblock
= (xfs_fsblock_t
)b
;
120 s
->br_startblock
= (xfs_fsblock_t
)(((xfs_dfsbno_t
)l1
) >> 21);
122 #endif /* XFS_BIG_BLKNOS */
123 s
->br_blockcount
= (xfs_filblks_t
)(l1
& xfs_mask64lo(21));
124 /* This is xfs_extent_state() in-line */
126 ASSERT(s
->br_blockcount
!= 0); /* saved for DMIG */
127 st
= XFS_EXT_UNWRITTEN
;
135 xfs_bmbt_rec_host_t
*r
,
138 __xfs_bmbt_get_all(r
->l0
, r
->l1
, s
);
142 * Extract the blockcount field from an in memory bmap extent record.
145 xfs_bmbt_get_blockcount(
146 xfs_bmbt_rec_host_t
*r
)
148 return (xfs_filblks_t
)(r
->l1
& xfs_mask64lo(21));
152 * Extract the startblock field from an in memory bmap extent record.
155 xfs_bmbt_get_startblock(
156 xfs_bmbt_rec_host_t
*r
)
159 return (((xfs_fsblock_t
)r
->l0
& xfs_mask64lo(9)) << 43) |
160 (((xfs_fsblock_t
)r
->l1
) >> 21);
165 b
= (((xfs_dfsbno_t
)r
->l0
& xfs_mask64lo(9)) << 43) |
166 (((xfs_dfsbno_t
)r
->l1
) >> 21);
167 ASSERT((b
>> 32) == 0 || isnulldstartblock(b
));
168 return (xfs_fsblock_t
)b
;
170 return (xfs_fsblock_t
)(((xfs_dfsbno_t
)r
->l1
) >> 21);
172 #endif /* XFS_BIG_BLKNOS */
176 * Extract the startoff field from an in memory bmap extent record.
179 xfs_bmbt_get_startoff(
180 xfs_bmbt_rec_host_t
*r
)
182 return ((xfs_fileoff_t
)r
->l0
&
183 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
188 xfs_bmbt_rec_host_t
*r
)
192 ext_flag
= (int)((r
->l0
) >> (64 - BMBT_EXNTFLAG_BITLEN
));
193 return xfs_extent_state(xfs_bmbt_get_blockcount(r
),
198 * Extract the blockcount field from an on disk bmap extent record.
201 xfs_bmbt_disk_get_blockcount(
204 return (xfs_filblks_t
)(be64_to_cpu(r
->l1
) & xfs_mask64lo(21));
208 * Extract the startoff field from a disk format bmap extent record.
211 xfs_bmbt_disk_get_startoff(
214 return ((xfs_fileoff_t
)be64_to_cpu(r
->l0
) &
215 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
220 * Set all the fields in a bmap extent record from the arguments.
224 xfs_bmbt_rec_host_t
*r
,
225 xfs_fileoff_t startoff
,
226 xfs_fsblock_t startblock
,
227 xfs_filblks_t blockcount
,
230 int extent_flag
= (state
== XFS_EXT_NORM
) ? 0 : 1;
232 ASSERT(state
== XFS_EXT_NORM
|| state
== XFS_EXT_UNWRITTEN
);
233 ASSERT((startoff
& xfs_mask64hi(64-BMBT_STARTOFF_BITLEN
)) == 0);
234 ASSERT((blockcount
& xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN
)) == 0);
237 ASSERT((startblock
& xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN
)) == 0);
239 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
240 ((xfs_bmbt_rec_base_t
)startoff
<< 9) |
241 ((xfs_bmbt_rec_base_t
)startblock
>> 43);
242 r
->l1
= ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
243 ((xfs_bmbt_rec_base_t
)blockcount
&
244 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21));
245 #else /* !XFS_BIG_BLKNOS */
246 if (isnullstartblock(startblock
)) {
247 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
248 ((xfs_bmbt_rec_base_t
)startoff
<< 9) |
249 (xfs_bmbt_rec_base_t
)xfs_mask64lo(9);
250 r
->l1
= xfs_mask64hi(11) |
251 ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
252 ((xfs_bmbt_rec_base_t
)blockcount
&
253 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21));
255 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
256 ((xfs_bmbt_rec_base_t
)startoff
<< 9);
257 r
->l1
= ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
258 ((xfs_bmbt_rec_base_t
)blockcount
&
259 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21));
261 #endif /* XFS_BIG_BLKNOS */
265 * Set all the fields in a bmap extent record from the uncompressed form.
269 xfs_bmbt_rec_host_t
*r
,
272 xfs_bmbt_set_allf(r
, s
->br_startoff
, s
->br_startblock
,
273 s
->br_blockcount
, s
->br_state
);
278 * Set all the fields in a disk format bmap extent record from the arguments.
281 xfs_bmbt_disk_set_allf(
283 xfs_fileoff_t startoff
,
284 xfs_fsblock_t startblock
,
285 xfs_filblks_t blockcount
,
288 int extent_flag
= (state
== XFS_EXT_NORM
) ? 0 : 1;
290 ASSERT(state
== XFS_EXT_NORM
|| state
== XFS_EXT_UNWRITTEN
);
291 ASSERT((startoff
& xfs_mask64hi(64-BMBT_STARTOFF_BITLEN
)) == 0);
292 ASSERT((blockcount
& xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN
)) == 0);
295 ASSERT((startblock
& xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN
)) == 0);
298 ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
299 ((xfs_bmbt_rec_base_t
)startoff
<< 9) |
300 ((xfs_bmbt_rec_base_t
)startblock
>> 43));
302 ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
303 ((xfs_bmbt_rec_base_t
)blockcount
&
304 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21)));
305 #else /* !XFS_BIG_BLKNOS */
306 if (isnullstartblock(startblock
)) {
308 ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
309 ((xfs_bmbt_rec_base_t
)startoff
<< 9) |
310 (xfs_bmbt_rec_base_t
)xfs_mask64lo(9));
311 r
->l1
= cpu_to_be64(xfs_mask64hi(11) |
312 ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
313 ((xfs_bmbt_rec_base_t
)blockcount
&
314 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21)));
317 ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
318 ((xfs_bmbt_rec_base_t
)startoff
<< 9));
320 ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
321 ((xfs_bmbt_rec_base_t
)blockcount
&
322 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21)));
324 #endif /* XFS_BIG_BLKNOS */
328 * Set all the fields in a bmap extent record from the uncompressed form.
331 xfs_bmbt_disk_set_all(
335 xfs_bmbt_disk_set_allf(r
, s
->br_startoff
, s
->br_startblock
,
336 s
->br_blockcount
, s
->br_state
);
340 * Set the blockcount field in a bmap extent record.
343 xfs_bmbt_set_blockcount(
344 xfs_bmbt_rec_host_t
*r
,
347 ASSERT((v
& xfs_mask64hi(43)) == 0);
348 r
->l1
= (r
->l1
& (xfs_bmbt_rec_base_t
)xfs_mask64hi(43)) |
349 (xfs_bmbt_rec_base_t
)(v
& xfs_mask64lo(21));
353 * Set the startblock field in a bmap extent record.
356 xfs_bmbt_set_startblock(
357 xfs_bmbt_rec_host_t
*r
,
361 ASSERT((v
& xfs_mask64hi(12)) == 0);
362 r
->l0
= (r
->l0
& (xfs_bmbt_rec_base_t
)xfs_mask64hi(55)) |
363 (xfs_bmbt_rec_base_t
)(v
>> 43);
364 r
->l1
= (r
->l1
& (xfs_bmbt_rec_base_t
)xfs_mask64lo(21)) |
365 (xfs_bmbt_rec_base_t
)(v
<< 21);
366 #else /* !XFS_BIG_BLKNOS */
367 if (isnullstartblock(v
)) {
368 r
->l0
|= (xfs_bmbt_rec_base_t
)xfs_mask64lo(9);
369 r
->l1
= (xfs_bmbt_rec_base_t
)xfs_mask64hi(11) |
370 ((xfs_bmbt_rec_base_t
)v
<< 21) |
371 (r
->l1
& (xfs_bmbt_rec_base_t
)xfs_mask64lo(21));
373 r
->l0
&= ~(xfs_bmbt_rec_base_t
)xfs_mask64lo(9);
374 r
->l1
= ((xfs_bmbt_rec_base_t
)v
<< 21) |
375 (r
->l1
& (xfs_bmbt_rec_base_t
)xfs_mask64lo(21));
377 #endif /* XFS_BIG_BLKNOS */
381 * Set the startoff field in a bmap extent record.
384 xfs_bmbt_set_startoff(
385 xfs_bmbt_rec_host_t
*r
,
388 ASSERT((v
& xfs_mask64hi(9)) == 0);
389 r
->l0
= (r
->l0
& (xfs_bmbt_rec_base_t
) xfs_mask64hi(1)) |
390 ((xfs_bmbt_rec_base_t
)v
<< 9) |
391 (r
->l0
& (xfs_bmbt_rec_base_t
)xfs_mask64lo(9));
395 * Set the extent state field in a bmap extent record.
399 xfs_bmbt_rec_host_t
*r
,
402 ASSERT(v
== XFS_EXT_NORM
|| v
== XFS_EXT_UNWRITTEN
);
403 if (v
== XFS_EXT_NORM
)
404 r
->l0
&= xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN
);
406 r
->l0
|= xfs_mask64hi(BMBT_EXNTFLAG_BITLEN
);
410 * Convert in-memory form of btree root to on-disk form.
414 struct xfs_mount
*mp
,
415 struct xfs_btree_block
*rblock
,
417 xfs_bmdr_block_t
*dblock
,
426 ASSERT(rblock
->bb_magic
== cpu_to_be32(XFS_BMAP_MAGIC
));
427 ASSERT(rblock
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLDFSBNO
));
428 ASSERT(rblock
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
));
429 ASSERT(rblock
->bb_level
!= 0);
430 dblock
->bb_level
= rblock
->bb_level
;
431 dblock
->bb_numrecs
= rblock
->bb_numrecs
;
432 dmxr
= xfs_bmdr_maxrecs(mp
, dblocklen
, 0);
433 fkp
= XFS_BMBT_KEY_ADDR(mp
, rblock
, 1);
434 tkp
= XFS_BMDR_KEY_ADDR(dblock
, 1);
435 fpp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, rblocklen
);
436 tpp
= XFS_BMDR_PTR_ADDR(dblock
, 1, dmxr
);
437 dmxr
= be16_to_cpu(dblock
->bb_numrecs
);
438 memcpy(tkp
, fkp
, sizeof(*fkp
) * dmxr
);
439 memcpy(tpp
, fpp
, sizeof(*fpp
) * dmxr
);
443 * Check extent records, which have just been read, for
444 * any bit in the extent flag field. ASSERT on debug
445 * kernels, as this condition should not occur.
446 * Return an error condition (1) if any flags found,
447 * otherwise return 0.
451 xfs_check_nostate_extents(
456 for (; num
> 0; num
--, idx
++) {
457 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
459 (64 - BMBT_EXNTFLAG_BITLEN
)) != 0) {
468 STATIC
struct xfs_btree_cur
*
470 struct xfs_btree_cur
*cur
)
472 struct xfs_btree_cur
*new;
474 new = xfs_bmbt_init_cursor(cur
->bc_mp
, cur
->bc_tp
,
475 cur
->bc_private
.b
.ip
, cur
->bc_private
.b
.whichfork
);
478 * Copy the firstblock, flist, and flags values,
479 * since init cursor doesn't get them.
481 new->bc_private
.b
.firstblock
= cur
->bc_private
.b
.firstblock
;
482 new->bc_private
.b
.flist
= cur
->bc_private
.b
.flist
;
483 new->bc_private
.b
.flags
= cur
->bc_private
.b
.flags
;
489 xfs_bmbt_update_cursor(
490 struct xfs_btree_cur
*src
,
491 struct xfs_btree_cur
*dst
)
493 ASSERT((dst
->bc_private
.b
.firstblock
!= NULLFSBLOCK
) ||
494 (dst
->bc_private
.b
.ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
));
495 ASSERT(dst
->bc_private
.b
.flist
== src
->bc_private
.b
.flist
);
497 dst
->bc_private
.b
.allocated
+= src
->bc_private
.b
.allocated
;
498 dst
->bc_private
.b
.firstblock
= src
->bc_private
.b
.firstblock
;
500 src
->bc_private
.b
.allocated
= 0;
504 xfs_bmbt_alloc_block(
505 struct xfs_btree_cur
*cur
,
506 union xfs_btree_ptr
*start
,
507 union xfs_btree_ptr
*new,
511 xfs_alloc_arg_t args
; /* block allocation args */
512 int error
; /* error return value */
514 memset(&args
, 0, sizeof(args
));
515 args
.tp
= cur
->bc_tp
;
516 args
.mp
= cur
->bc_mp
;
517 args
.fsbno
= cur
->bc_private
.b
.firstblock
;
518 args
.firstblock
= args
.fsbno
;
520 if (args
.fsbno
== NULLFSBLOCK
) {
521 args
.fsbno
= be64_to_cpu(start
->l
);
522 args
.type
= XFS_ALLOCTYPE_START_BNO
;
524 * Make sure there is sufficient room left in the AG to
525 * complete a full tree split for an extent insert. If
526 * we are converting the middle part of an extent then
527 * we may need space for two tree splits.
529 * We are relying on the caller to make the correct block
530 * reservation for this operation to succeed. If the
531 * reservation amount is insufficient then we may fail a
532 * block allocation here and corrupt the filesystem.
534 args
.minleft
= xfs_trans_get_block_res(args
.tp
);
535 } else if (cur
->bc_private
.b
.flist
->xbf_low
) {
536 args
.type
= XFS_ALLOCTYPE_START_BNO
;
538 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
541 args
.minlen
= args
.maxlen
= args
.prod
= 1;
542 args
.wasdel
= cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
;
543 if (!args
.wasdel
&& xfs_trans_get_block_res(args
.tp
) == 0) {
544 error
= XFS_ERROR(ENOSPC
);
547 error
= xfs_alloc_vextent(&args
);
551 if (args
.fsbno
== NULLFSBLOCK
&& args
.minleft
) {
553 * Could not find an AG with enough free space to satisfy
554 * a full btree split. Try again without minleft and if
555 * successful activate the lowspace algorithm.
558 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
560 error
= xfs_alloc_vextent(&args
);
563 cur
->bc_private
.b
.flist
->xbf_low
= 1;
565 if (args
.fsbno
== NULLFSBLOCK
) {
566 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
570 ASSERT(args
.len
== 1);
571 cur
->bc_private
.b
.firstblock
= args
.fsbno
;
572 cur
->bc_private
.b
.allocated
++;
573 cur
->bc_private
.b
.ip
->i_d
.di_nblocks
++;
574 xfs_trans_log_inode(args
.tp
, cur
->bc_private
.b
.ip
, XFS_ILOG_CORE
);
575 xfs_trans_mod_dquot_byino(args
.tp
, cur
->bc_private
.b
.ip
,
576 XFS_TRANS_DQ_BCOUNT
, 1L);
578 new->l
= cpu_to_be64(args
.fsbno
);
580 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
585 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
591 struct xfs_btree_cur
*cur
,
594 struct xfs_mount
*mp
= cur
->bc_mp
;
595 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
596 struct xfs_trans
*tp
= cur
->bc_tp
;
597 xfs_fsblock_t fsbno
= XFS_DADDR_TO_FSB(mp
, XFS_BUF_ADDR(bp
));
599 xfs_bmap_add_free(fsbno
, 1, cur
->bc_private
.b
.flist
, mp
);
600 ip
->i_d
.di_nblocks
--;
602 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
603 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
604 xfs_trans_binval(tp
, bp
);
609 xfs_bmbt_get_minrecs(
610 struct xfs_btree_cur
*cur
,
613 if (level
== cur
->bc_nlevels
- 1) {
614 struct xfs_ifork
*ifp
;
616 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
,
617 cur
->bc_private
.b
.whichfork
);
619 return xfs_bmbt_maxrecs(cur
->bc_mp
,
620 ifp
->if_broot_bytes
, level
== 0) / 2;
623 return cur
->bc_mp
->m_bmap_dmnr
[level
!= 0];
627 xfs_bmbt_get_maxrecs(
628 struct xfs_btree_cur
*cur
,
631 if (level
== cur
->bc_nlevels
- 1) {
632 struct xfs_ifork
*ifp
;
634 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
,
635 cur
->bc_private
.b
.whichfork
);
637 return xfs_bmbt_maxrecs(cur
->bc_mp
,
638 ifp
->if_broot_bytes
, level
== 0);
641 return cur
->bc_mp
->m_bmap_dmxr
[level
!= 0];
646 * Get the maximum records we could store in the on-disk format.
648 * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but
649 * for the root node this checks the available space in the dinode fork
650 * so that we can resize the in-memory buffer to match it. After a
651 * resize to the maximum size this function returns the same value
652 * as xfs_bmbt_get_maxrecs for the root node, too.
655 xfs_bmbt_get_dmaxrecs(
656 struct xfs_btree_cur
*cur
,
659 if (level
!= cur
->bc_nlevels
- 1)
660 return cur
->bc_mp
->m_bmap_dmxr
[level
!= 0];
661 return xfs_bmdr_maxrecs(cur
->bc_mp
, cur
->bc_private
.b
.forksize
,
666 xfs_bmbt_init_key_from_rec(
667 union xfs_btree_key
*key
,
668 union xfs_btree_rec
*rec
)
670 key
->bmbt
.br_startoff
=
671 cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec
->bmbt
));
675 xfs_bmbt_init_rec_from_key(
676 union xfs_btree_key
*key
,
677 union xfs_btree_rec
*rec
)
679 ASSERT(key
->bmbt
.br_startoff
!= 0);
681 xfs_bmbt_disk_set_allf(&rec
->bmbt
, be64_to_cpu(key
->bmbt
.br_startoff
),
686 xfs_bmbt_init_rec_from_cur(
687 struct xfs_btree_cur
*cur
,
688 union xfs_btree_rec
*rec
)
690 xfs_bmbt_disk_set_all(&rec
->bmbt
, &cur
->bc_rec
.b
);
694 xfs_bmbt_init_ptr_from_cur(
695 struct xfs_btree_cur
*cur
,
696 union xfs_btree_ptr
*ptr
)
703 struct xfs_btree_cur
*cur
,
704 union xfs_btree_key
*key
)
706 return (__int64_t
)be64_to_cpu(key
->bmbt
.br_startoff
) -
707 cur
->bc_rec
.b
.br_startoff
;
712 xfs_bmbt_keys_inorder(
713 struct xfs_btree_cur
*cur
,
714 union xfs_btree_key
*k1
,
715 union xfs_btree_key
*k2
)
717 return be64_to_cpu(k1
->bmbt
.br_startoff
) <
718 be64_to_cpu(k2
->bmbt
.br_startoff
);
722 xfs_bmbt_recs_inorder(
723 struct xfs_btree_cur
*cur
,
724 union xfs_btree_rec
*r1
,
725 union xfs_btree_rec
*r2
)
727 return xfs_bmbt_disk_get_startoff(&r1
->bmbt
) +
728 xfs_bmbt_disk_get_blockcount(&r1
->bmbt
) <=
729 xfs_bmbt_disk_get_startoff(&r2
->bmbt
);
733 static const struct xfs_btree_ops xfs_bmbt_ops
= {
734 .rec_len
= sizeof(xfs_bmbt_rec_t
),
735 .key_len
= sizeof(xfs_bmbt_key_t
),
737 .dup_cursor
= xfs_bmbt_dup_cursor
,
738 .update_cursor
= xfs_bmbt_update_cursor
,
739 .alloc_block
= xfs_bmbt_alloc_block
,
740 .free_block
= xfs_bmbt_free_block
,
741 .get_maxrecs
= xfs_bmbt_get_maxrecs
,
742 .get_minrecs
= xfs_bmbt_get_minrecs
,
743 .get_dmaxrecs
= xfs_bmbt_get_dmaxrecs
,
744 .init_key_from_rec
= xfs_bmbt_init_key_from_rec
,
745 .init_rec_from_key
= xfs_bmbt_init_rec_from_key
,
746 .init_rec_from_cur
= xfs_bmbt_init_rec_from_cur
,
747 .init_ptr_from_cur
= xfs_bmbt_init_ptr_from_cur
,
748 .key_diff
= xfs_bmbt_key_diff
,
750 .keys_inorder
= xfs_bmbt_keys_inorder
,
751 .recs_inorder
= xfs_bmbt_recs_inorder
,
756 * Allocate a new bmap btree cursor.
758 struct xfs_btree_cur
* /* new bmap btree cursor */
759 xfs_bmbt_init_cursor(
760 struct xfs_mount
*mp
, /* file system mount point */
761 struct xfs_trans
*tp
, /* transaction pointer */
762 struct xfs_inode
*ip
, /* inode owning the btree */
763 int whichfork
) /* data or attr fork */
765 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
766 struct xfs_btree_cur
*cur
;
768 cur
= kmem_zone_zalloc(xfs_btree_cur_zone
, KM_SLEEP
);
772 cur
->bc_nlevels
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
773 cur
->bc_btnum
= XFS_BTNUM_BMAP
;
774 cur
->bc_blocklog
= mp
->m_sb
.sb_blocklog
;
776 cur
->bc_ops
= &xfs_bmbt_ops
;
777 cur
->bc_flags
= XFS_BTREE_LONG_PTRS
| XFS_BTREE_ROOT_IN_INODE
;
779 cur
->bc_private
.b
.forksize
= XFS_IFORK_SIZE(ip
, whichfork
);
780 cur
->bc_private
.b
.ip
= ip
;
781 cur
->bc_private
.b
.firstblock
= NULLFSBLOCK
;
782 cur
->bc_private
.b
.flist
= NULL
;
783 cur
->bc_private
.b
.allocated
= 0;
784 cur
->bc_private
.b
.flags
= 0;
785 cur
->bc_private
.b
.whichfork
= whichfork
;
791 * Calculate number of records in a bmap btree block.
795 struct xfs_mount
*mp
,
799 blocklen
-= XFS_BMBT_BLOCK_LEN(mp
);
802 return blocklen
/ sizeof(xfs_bmbt_rec_t
);
803 return blocklen
/ (sizeof(xfs_bmbt_key_t
) + sizeof(xfs_bmbt_ptr_t
));
807 * Calculate number of records in a bmap btree inode root.
811 struct xfs_mount
*mp
,
815 blocklen
-= sizeof(xfs_bmdr_block_t
);
818 return blocklen
/ sizeof(xfs_bmdr_rec_t
);
819 return blocklen
/ (sizeof(xfs_bmdr_key_t
) + sizeof(xfs_bmdr_ptr_t
));