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"
39 #include "xfs_trace.h"
42 * Determine the extent state.
51 ASSERT(blks
!= 0); /* saved for DMIG */
52 return XFS_EXT_UNWRITTEN
;
58 * Convert on-disk form of btree root to in-memory form.
63 xfs_bmdr_block_t
*dblock
,
65 struct xfs_btree_block
*rblock
,
74 rblock
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
75 rblock
->bb_level
= dblock
->bb_level
;
76 ASSERT(be16_to_cpu(rblock
->bb_level
) > 0);
77 rblock
->bb_numrecs
= dblock
->bb_numrecs
;
78 rblock
->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
79 rblock
->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
80 dmxr
= xfs_bmdr_maxrecs(mp
, dblocklen
, 0);
81 fkp
= XFS_BMDR_KEY_ADDR(dblock
, 1);
82 tkp
= XFS_BMBT_KEY_ADDR(mp
, rblock
, 1);
83 fpp
= XFS_BMDR_PTR_ADDR(dblock
, 1, dmxr
);
84 tpp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, rblocklen
);
85 dmxr
= be16_to_cpu(dblock
->bb_numrecs
);
86 memcpy(tkp
, fkp
, sizeof(*fkp
) * dmxr
);
87 memcpy(tpp
, fpp
, sizeof(*fpp
) * dmxr
);
91 * Convert a compressed bmap extent record to an uncompressed form.
92 * This code must be in sync with the routines xfs_bmbt_get_startoff,
93 * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state.
104 ext_flag
= (int)(l0
>> (64 - BMBT_EXNTFLAG_BITLEN
));
105 s
->br_startoff
= ((xfs_fileoff_t
)l0
&
106 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
108 s
->br_startblock
= (((xfs_fsblock_t
)l0
& xfs_mask64lo(9)) << 43) |
109 (((xfs_fsblock_t
)l1
) >> 21);
115 b
= (((xfs_dfsbno_t
)l0
& xfs_mask64lo(9)) << 43) |
116 (((xfs_dfsbno_t
)l1
) >> 21);
117 ASSERT((b
>> 32) == 0 || isnulldstartblock(b
));
118 s
->br_startblock
= (xfs_fsblock_t
)b
;
121 s
->br_startblock
= (xfs_fsblock_t
)(((xfs_dfsbno_t
)l1
) >> 21);
123 #endif /* XFS_BIG_BLKNOS */
124 s
->br_blockcount
= (xfs_filblks_t
)(l1
& xfs_mask64lo(21));
125 /* This is xfs_extent_state() in-line */
127 ASSERT(s
->br_blockcount
!= 0); /* saved for DMIG */
128 st
= XFS_EXT_UNWRITTEN
;
136 xfs_bmbt_rec_host_t
*r
,
139 __xfs_bmbt_get_all(r
->l0
, r
->l1
, s
);
143 * Extract the blockcount field from an in memory bmap extent record.
146 xfs_bmbt_get_blockcount(
147 xfs_bmbt_rec_host_t
*r
)
149 return (xfs_filblks_t
)(r
->l1
& xfs_mask64lo(21));
153 * Extract the startblock field from an in memory bmap extent record.
156 xfs_bmbt_get_startblock(
157 xfs_bmbt_rec_host_t
*r
)
160 return (((xfs_fsblock_t
)r
->l0
& xfs_mask64lo(9)) << 43) |
161 (((xfs_fsblock_t
)r
->l1
) >> 21);
166 b
= (((xfs_dfsbno_t
)r
->l0
& xfs_mask64lo(9)) << 43) |
167 (((xfs_dfsbno_t
)r
->l1
) >> 21);
168 ASSERT((b
>> 32) == 0 || isnulldstartblock(b
));
169 return (xfs_fsblock_t
)b
;
171 return (xfs_fsblock_t
)(((xfs_dfsbno_t
)r
->l1
) >> 21);
173 #endif /* XFS_BIG_BLKNOS */
177 * Extract the startoff field from an in memory bmap extent record.
180 xfs_bmbt_get_startoff(
181 xfs_bmbt_rec_host_t
*r
)
183 return ((xfs_fileoff_t
)r
->l0
&
184 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
189 xfs_bmbt_rec_host_t
*r
)
193 ext_flag
= (int)((r
->l0
) >> (64 - BMBT_EXNTFLAG_BITLEN
));
194 return xfs_extent_state(xfs_bmbt_get_blockcount(r
),
199 * Extract the blockcount field from an on disk bmap extent record.
202 xfs_bmbt_disk_get_blockcount(
205 return (xfs_filblks_t
)(be64_to_cpu(r
->l1
) & xfs_mask64lo(21));
209 * Extract the startoff field from a disk format bmap extent record.
212 xfs_bmbt_disk_get_startoff(
215 return ((xfs_fileoff_t
)be64_to_cpu(r
->l0
) &
216 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
221 * Set all the fields in a bmap extent record from the arguments.
225 xfs_bmbt_rec_host_t
*r
,
226 xfs_fileoff_t startoff
,
227 xfs_fsblock_t startblock
,
228 xfs_filblks_t blockcount
,
231 int extent_flag
= (state
== XFS_EXT_NORM
) ? 0 : 1;
233 ASSERT(state
== XFS_EXT_NORM
|| state
== XFS_EXT_UNWRITTEN
);
234 ASSERT((startoff
& xfs_mask64hi(64-BMBT_STARTOFF_BITLEN
)) == 0);
235 ASSERT((blockcount
& xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN
)) == 0);
238 ASSERT((startblock
& xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN
)) == 0);
240 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
241 ((xfs_bmbt_rec_base_t
)startoff
<< 9) |
242 ((xfs_bmbt_rec_base_t
)startblock
>> 43);
243 r
->l1
= ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
244 ((xfs_bmbt_rec_base_t
)blockcount
&
245 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21));
246 #else /* !XFS_BIG_BLKNOS */
247 if (isnullstartblock(startblock
)) {
248 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
249 ((xfs_bmbt_rec_base_t
)startoff
<< 9) |
250 (xfs_bmbt_rec_base_t
)xfs_mask64lo(9);
251 r
->l1
= xfs_mask64hi(11) |
252 ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
253 ((xfs_bmbt_rec_base_t
)blockcount
&
254 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21));
256 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
257 ((xfs_bmbt_rec_base_t
)startoff
<< 9);
258 r
->l1
= ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
259 ((xfs_bmbt_rec_base_t
)blockcount
&
260 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21));
262 #endif /* XFS_BIG_BLKNOS */
266 * Set all the fields in a bmap extent record from the uncompressed form.
270 xfs_bmbt_rec_host_t
*r
,
273 xfs_bmbt_set_allf(r
, s
->br_startoff
, s
->br_startblock
,
274 s
->br_blockcount
, s
->br_state
);
279 * Set all the fields in a disk format bmap extent record from the arguments.
282 xfs_bmbt_disk_set_allf(
284 xfs_fileoff_t startoff
,
285 xfs_fsblock_t startblock
,
286 xfs_filblks_t blockcount
,
289 int extent_flag
= (state
== XFS_EXT_NORM
) ? 0 : 1;
291 ASSERT(state
== XFS_EXT_NORM
|| state
== XFS_EXT_UNWRITTEN
);
292 ASSERT((startoff
& xfs_mask64hi(64-BMBT_STARTOFF_BITLEN
)) == 0);
293 ASSERT((blockcount
& xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN
)) == 0);
296 ASSERT((startblock
& xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN
)) == 0);
299 ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
300 ((xfs_bmbt_rec_base_t
)startoff
<< 9) |
301 ((xfs_bmbt_rec_base_t
)startblock
>> 43));
303 ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
304 ((xfs_bmbt_rec_base_t
)blockcount
&
305 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21)));
306 #else /* !XFS_BIG_BLKNOS */
307 if (isnullstartblock(startblock
)) {
309 ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
310 ((xfs_bmbt_rec_base_t
)startoff
<< 9) |
311 (xfs_bmbt_rec_base_t
)xfs_mask64lo(9));
312 r
->l1
= cpu_to_be64(xfs_mask64hi(11) |
313 ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
314 ((xfs_bmbt_rec_base_t
)blockcount
&
315 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21)));
318 ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
319 ((xfs_bmbt_rec_base_t
)startoff
<< 9));
321 ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
322 ((xfs_bmbt_rec_base_t
)blockcount
&
323 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21)));
325 #endif /* XFS_BIG_BLKNOS */
329 * Set all the fields in a bmap extent record from the uncompressed form.
332 xfs_bmbt_disk_set_all(
336 xfs_bmbt_disk_set_allf(r
, s
->br_startoff
, s
->br_startblock
,
337 s
->br_blockcount
, s
->br_state
);
341 * Set the blockcount field in a bmap extent record.
344 xfs_bmbt_set_blockcount(
345 xfs_bmbt_rec_host_t
*r
,
348 ASSERT((v
& xfs_mask64hi(43)) == 0);
349 r
->l1
= (r
->l1
& (xfs_bmbt_rec_base_t
)xfs_mask64hi(43)) |
350 (xfs_bmbt_rec_base_t
)(v
& xfs_mask64lo(21));
354 * Set the startblock field in a bmap extent record.
357 xfs_bmbt_set_startblock(
358 xfs_bmbt_rec_host_t
*r
,
362 ASSERT((v
& xfs_mask64hi(12)) == 0);
363 r
->l0
= (r
->l0
& (xfs_bmbt_rec_base_t
)xfs_mask64hi(55)) |
364 (xfs_bmbt_rec_base_t
)(v
>> 43);
365 r
->l1
= (r
->l1
& (xfs_bmbt_rec_base_t
)xfs_mask64lo(21)) |
366 (xfs_bmbt_rec_base_t
)(v
<< 21);
367 #else /* !XFS_BIG_BLKNOS */
368 if (isnullstartblock(v
)) {
369 r
->l0
|= (xfs_bmbt_rec_base_t
)xfs_mask64lo(9);
370 r
->l1
= (xfs_bmbt_rec_base_t
)xfs_mask64hi(11) |
371 ((xfs_bmbt_rec_base_t
)v
<< 21) |
372 (r
->l1
& (xfs_bmbt_rec_base_t
)xfs_mask64lo(21));
374 r
->l0
&= ~(xfs_bmbt_rec_base_t
)xfs_mask64lo(9);
375 r
->l1
= ((xfs_bmbt_rec_base_t
)v
<< 21) |
376 (r
->l1
& (xfs_bmbt_rec_base_t
)xfs_mask64lo(21));
378 #endif /* XFS_BIG_BLKNOS */
382 * Set the startoff field in a bmap extent record.
385 xfs_bmbt_set_startoff(
386 xfs_bmbt_rec_host_t
*r
,
389 ASSERT((v
& xfs_mask64hi(9)) == 0);
390 r
->l0
= (r
->l0
& (xfs_bmbt_rec_base_t
) xfs_mask64hi(1)) |
391 ((xfs_bmbt_rec_base_t
)v
<< 9) |
392 (r
->l0
& (xfs_bmbt_rec_base_t
)xfs_mask64lo(9));
396 * Set the extent state field in a bmap extent record.
400 xfs_bmbt_rec_host_t
*r
,
403 ASSERT(v
== XFS_EXT_NORM
|| v
== XFS_EXT_UNWRITTEN
);
404 if (v
== XFS_EXT_NORM
)
405 r
->l0
&= xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN
);
407 r
->l0
|= xfs_mask64hi(BMBT_EXNTFLAG_BITLEN
);
411 * Convert in-memory form of btree root to on-disk form.
415 struct xfs_mount
*mp
,
416 struct xfs_btree_block
*rblock
,
418 xfs_bmdr_block_t
*dblock
,
427 ASSERT(rblock
->bb_magic
== cpu_to_be32(XFS_BMAP_MAGIC
));
428 ASSERT(rblock
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLDFSBNO
));
429 ASSERT(rblock
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
));
430 ASSERT(rblock
->bb_level
!= 0);
431 dblock
->bb_level
= rblock
->bb_level
;
432 dblock
->bb_numrecs
= rblock
->bb_numrecs
;
433 dmxr
= xfs_bmdr_maxrecs(mp
, dblocklen
, 0);
434 fkp
= XFS_BMBT_KEY_ADDR(mp
, rblock
, 1);
435 tkp
= XFS_BMDR_KEY_ADDR(dblock
, 1);
436 fpp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, rblocklen
);
437 tpp
= XFS_BMDR_PTR_ADDR(dblock
, 1, dmxr
);
438 dmxr
= be16_to_cpu(dblock
->bb_numrecs
);
439 memcpy(tkp
, fkp
, sizeof(*fkp
) * dmxr
);
440 memcpy(tpp
, fpp
, sizeof(*fpp
) * dmxr
);
444 * Check extent records, which have just been read, for
445 * any bit in the extent flag field. ASSERT on debug
446 * kernels, as this condition should not occur.
447 * Return an error condition (1) if any flags found,
448 * otherwise return 0.
452 xfs_check_nostate_extents(
457 for (; num
> 0; num
--, idx
++) {
458 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
460 (64 - BMBT_EXNTFLAG_BITLEN
)) != 0) {
469 STATIC
struct xfs_btree_cur
*
471 struct xfs_btree_cur
*cur
)
473 struct xfs_btree_cur
*new;
475 new = xfs_bmbt_init_cursor(cur
->bc_mp
, cur
->bc_tp
,
476 cur
->bc_private
.b
.ip
, cur
->bc_private
.b
.whichfork
);
479 * Copy the firstblock, flist, and flags values,
480 * since init cursor doesn't get them.
482 new->bc_private
.b
.firstblock
= cur
->bc_private
.b
.firstblock
;
483 new->bc_private
.b
.flist
= cur
->bc_private
.b
.flist
;
484 new->bc_private
.b
.flags
= cur
->bc_private
.b
.flags
;
490 xfs_bmbt_update_cursor(
491 struct xfs_btree_cur
*src
,
492 struct xfs_btree_cur
*dst
)
494 ASSERT((dst
->bc_private
.b
.firstblock
!= NULLFSBLOCK
) ||
495 (dst
->bc_private
.b
.ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
));
496 ASSERT(dst
->bc_private
.b
.flist
== src
->bc_private
.b
.flist
);
498 dst
->bc_private
.b
.allocated
+= src
->bc_private
.b
.allocated
;
499 dst
->bc_private
.b
.firstblock
= src
->bc_private
.b
.firstblock
;
501 src
->bc_private
.b
.allocated
= 0;
505 xfs_bmbt_alloc_block(
506 struct xfs_btree_cur
*cur
,
507 union xfs_btree_ptr
*start
,
508 union xfs_btree_ptr
*new,
512 xfs_alloc_arg_t args
; /* block allocation args */
513 int error
; /* error return value */
515 memset(&args
, 0, sizeof(args
));
516 args
.tp
= cur
->bc_tp
;
517 args
.mp
= cur
->bc_mp
;
518 args
.fsbno
= cur
->bc_private
.b
.firstblock
;
519 args
.firstblock
= args
.fsbno
;
521 if (args
.fsbno
== NULLFSBLOCK
) {
522 args
.fsbno
= be64_to_cpu(start
->l
);
523 args
.type
= XFS_ALLOCTYPE_START_BNO
;
525 * Make sure there is sufficient room left in the AG to
526 * complete a full tree split for an extent insert. If
527 * we are converting the middle part of an extent then
528 * we may need space for two tree splits.
530 * We are relying on the caller to make the correct block
531 * reservation for this operation to succeed. If the
532 * reservation amount is insufficient then we may fail a
533 * block allocation here and corrupt the filesystem.
535 args
.minleft
= xfs_trans_get_block_res(args
.tp
);
536 } else if (cur
->bc_private
.b
.flist
->xbf_low
) {
537 args
.type
= XFS_ALLOCTYPE_START_BNO
;
539 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
542 args
.minlen
= args
.maxlen
= args
.prod
= 1;
543 args
.wasdel
= cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
;
544 if (!args
.wasdel
&& xfs_trans_get_block_res(args
.tp
) == 0) {
545 error
= XFS_ERROR(ENOSPC
);
548 error
= xfs_alloc_vextent(&args
);
552 if (args
.fsbno
== NULLFSBLOCK
&& args
.minleft
) {
554 * Could not find an AG with enough free space to satisfy
555 * a full btree split. Try again without minleft and if
556 * successful activate the lowspace algorithm.
559 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
561 error
= xfs_alloc_vextent(&args
);
564 cur
->bc_private
.b
.flist
->xbf_low
= 1;
566 if (args
.fsbno
== NULLFSBLOCK
) {
567 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
571 ASSERT(args
.len
== 1);
572 cur
->bc_private
.b
.firstblock
= args
.fsbno
;
573 cur
->bc_private
.b
.allocated
++;
574 cur
->bc_private
.b
.ip
->i_d
.di_nblocks
++;
575 xfs_trans_log_inode(args
.tp
, cur
->bc_private
.b
.ip
, XFS_ILOG_CORE
);
576 xfs_trans_mod_dquot_byino(args
.tp
, cur
->bc_private
.b
.ip
,
577 XFS_TRANS_DQ_BCOUNT
, 1L);
579 new->l
= cpu_to_be64(args
.fsbno
);
581 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
586 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
592 struct xfs_btree_cur
*cur
,
595 struct xfs_mount
*mp
= cur
->bc_mp
;
596 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
597 struct xfs_trans
*tp
= cur
->bc_tp
;
598 xfs_fsblock_t fsbno
= XFS_DADDR_TO_FSB(mp
, XFS_BUF_ADDR(bp
));
600 xfs_bmap_add_free(fsbno
, 1, cur
->bc_private
.b
.flist
, mp
);
601 ip
->i_d
.di_nblocks
--;
603 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
604 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
605 xfs_trans_binval(tp
, bp
);
610 xfs_bmbt_get_minrecs(
611 struct xfs_btree_cur
*cur
,
614 if (level
== cur
->bc_nlevels
- 1) {
615 struct xfs_ifork
*ifp
;
617 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
,
618 cur
->bc_private
.b
.whichfork
);
620 return xfs_bmbt_maxrecs(cur
->bc_mp
,
621 ifp
->if_broot_bytes
, level
== 0) / 2;
624 return cur
->bc_mp
->m_bmap_dmnr
[level
!= 0];
628 xfs_bmbt_get_maxrecs(
629 struct xfs_btree_cur
*cur
,
632 if (level
== cur
->bc_nlevels
- 1) {
633 struct xfs_ifork
*ifp
;
635 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
,
636 cur
->bc_private
.b
.whichfork
);
638 return xfs_bmbt_maxrecs(cur
->bc_mp
,
639 ifp
->if_broot_bytes
, level
== 0);
642 return cur
->bc_mp
->m_bmap_dmxr
[level
!= 0];
647 * Get the maximum records we could store in the on-disk format.
649 * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but
650 * for the root node this checks the available space in the dinode fork
651 * so that we can resize the in-memory buffer to match it. After a
652 * resize to the maximum size this function returns the same value
653 * as xfs_bmbt_get_maxrecs for the root node, too.
656 xfs_bmbt_get_dmaxrecs(
657 struct xfs_btree_cur
*cur
,
660 if (level
!= cur
->bc_nlevels
- 1)
661 return cur
->bc_mp
->m_bmap_dmxr
[level
!= 0];
662 return xfs_bmdr_maxrecs(cur
->bc_mp
, cur
->bc_private
.b
.forksize
,
667 xfs_bmbt_init_key_from_rec(
668 union xfs_btree_key
*key
,
669 union xfs_btree_rec
*rec
)
671 key
->bmbt
.br_startoff
=
672 cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec
->bmbt
));
676 xfs_bmbt_init_rec_from_key(
677 union xfs_btree_key
*key
,
678 union xfs_btree_rec
*rec
)
680 ASSERT(key
->bmbt
.br_startoff
!= 0);
682 xfs_bmbt_disk_set_allf(&rec
->bmbt
, be64_to_cpu(key
->bmbt
.br_startoff
),
687 xfs_bmbt_init_rec_from_cur(
688 struct xfs_btree_cur
*cur
,
689 union xfs_btree_rec
*rec
)
691 xfs_bmbt_disk_set_all(&rec
->bmbt
, &cur
->bc_rec
.b
);
695 xfs_bmbt_init_ptr_from_cur(
696 struct xfs_btree_cur
*cur
,
697 union xfs_btree_ptr
*ptr
)
704 struct xfs_btree_cur
*cur
,
705 union xfs_btree_key
*key
)
707 return (__int64_t
)be64_to_cpu(key
->bmbt
.br_startoff
) -
708 cur
->bc_rec
.b
.br_startoff
;
715 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
716 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
718 int lblock_ok
; /* block passes checks */
720 /* magic number and level verification.
722 * We don't know waht fork we belong to, so just verify that the level
723 * is less than the maximum of the two. Later checks will be more
726 level
= be16_to_cpu(block
->bb_level
);
727 lblock_ok
= block
->bb_magic
== cpu_to_be32(XFS_BMAP_MAGIC
) &&
728 level
< max(mp
->m_bm_maxlevels
[0], mp
->m_bm_maxlevels
[1]);
730 /* numrecs verification */
731 lblock_ok
= lblock_ok
&&
732 be16_to_cpu(block
->bb_numrecs
) <= mp
->m_bmap_dmxr
[level
!= 0];
734 /* sibling pointer verification */
735 lblock_ok
= lblock_ok
&&
736 block
->bb_u
.l
.bb_leftsib
&&
737 (block
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLDFSBNO
) ||
738 XFS_FSB_SANITY_CHECK(mp
,
739 be64_to_cpu(block
->bb_u
.l
.bb_leftsib
))) &&
740 block
->bb_u
.l
.bb_rightsib
&&
741 (block
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
) ||
742 XFS_FSB_SANITY_CHECK(mp
,
743 be64_to_cpu(block
->bb_u
.l
.bb_rightsib
)));
746 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
747 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, block
);
748 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
753 xfs_bmbt_read_verify(
760 xfs_bmbt_write_verify(
766 const struct xfs_buf_ops xfs_bmbt_buf_ops
= {
767 .verify_read
= xfs_bmbt_read_verify
,
768 .verify_write
= xfs_bmbt_write_verify
,
774 xfs_bmbt_keys_inorder(
775 struct xfs_btree_cur
*cur
,
776 union xfs_btree_key
*k1
,
777 union xfs_btree_key
*k2
)
779 return be64_to_cpu(k1
->bmbt
.br_startoff
) <
780 be64_to_cpu(k2
->bmbt
.br_startoff
);
784 xfs_bmbt_recs_inorder(
785 struct xfs_btree_cur
*cur
,
786 union xfs_btree_rec
*r1
,
787 union xfs_btree_rec
*r2
)
789 return xfs_bmbt_disk_get_startoff(&r1
->bmbt
) +
790 xfs_bmbt_disk_get_blockcount(&r1
->bmbt
) <=
791 xfs_bmbt_disk_get_startoff(&r2
->bmbt
);
795 static const struct xfs_btree_ops xfs_bmbt_ops
= {
796 .rec_len
= sizeof(xfs_bmbt_rec_t
),
797 .key_len
= sizeof(xfs_bmbt_key_t
),
799 .dup_cursor
= xfs_bmbt_dup_cursor
,
800 .update_cursor
= xfs_bmbt_update_cursor
,
801 .alloc_block
= xfs_bmbt_alloc_block
,
802 .free_block
= xfs_bmbt_free_block
,
803 .get_maxrecs
= xfs_bmbt_get_maxrecs
,
804 .get_minrecs
= xfs_bmbt_get_minrecs
,
805 .get_dmaxrecs
= xfs_bmbt_get_dmaxrecs
,
806 .init_key_from_rec
= xfs_bmbt_init_key_from_rec
,
807 .init_rec_from_key
= xfs_bmbt_init_rec_from_key
,
808 .init_rec_from_cur
= xfs_bmbt_init_rec_from_cur
,
809 .init_ptr_from_cur
= xfs_bmbt_init_ptr_from_cur
,
810 .key_diff
= xfs_bmbt_key_diff
,
811 .buf_ops
= &xfs_bmbt_buf_ops
,
813 .keys_inorder
= xfs_bmbt_keys_inorder
,
814 .recs_inorder
= xfs_bmbt_recs_inorder
,
819 * Allocate a new bmap btree cursor.
821 struct xfs_btree_cur
* /* new bmap btree cursor */
822 xfs_bmbt_init_cursor(
823 struct xfs_mount
*mp
, /* file system mount point */
824 struct xfs_trans
*tp
, /* transaction pointer */
825 struct xfs_inode
*ip
, /* inode owning the btree */
826 int whichfork
) /* data or attr fork */
828 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
829 struct xfs_btree_cur
*cur
;
831 cur
= kmem_zone_zalloc(xfs_btree_cur_zone
, KM_SLEEP
);
835 cur
->bc_nlevels
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
836 cur
->bc_btnum
= XFS_BTNUM_BMAP
;
837 cur
->bc_blocklog
= mp
->m_sb
.sb_blocklog
;
839 cur
->bc_ops
= &xfs_bmbt_ops
;
840 cur
->bc_flags
= XFS_BTREE_LONG_PTRS
| XFS_BTREE_ROOT_IN_INODE
;
842 cur
->bc_private
.b
.forksize
= XFS_IFORK_SIZE(ip
, whichfork
);
843 cur
->bc_private
.b
.ip
= ip
;
844 cur
->bc_private
.b
.firstblock
= NULLFSBLOCK
;
845 cur
->bc_private
.b
.flist
= NULL
;
846 cur
->bc_private
.b
.allocated
= 0;
847 cur
->bc_private
.b
.flags
= 0;
848 cur
->bc_private
.b
.whichfork
= whichfork
;
854 * Calculate number of records in a bmap btree block.
858 struct xfs_mount
*mp
,
862 blocklen
-= XFS_BMBT_BLOCK_LEN(mp
);
865 return blocklen
/ sizeof(xfs_bmbt_rec_t
);
866 return blocklen
/ (sizeof(xfs_bmbt_key_t
) + sizeof(xfs_bmbt_ptr_t
));
870 * Calculate number of records in a bmap btree inode root.
874 struct xfs_mount
*mp
,
878 blocklen
-= sizeof(xfs_bmdr_block_t
);
881 return blocklen
/ sizeof(xfs_bmdr_rec_t
);
882 return blocklen
/ (sizeof(xfs_bmdr_key_t
) + sizeof(xfs_bmdr_ptr_t
));