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_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.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_trans.h"
33 #include "xfs_inode_item.h"
34 #include "xfs_alloc.h"
35 #include "xfs_btree.h"
36 #include "xfs_itable.h"
38 #include "xfs_error.h"
39 #include "xfs_quota.h"
40 #include "xfs_trace.h"
41 #include "xfs_cksum.h"
44 * Determine the extent state.
53 ASSERT(blks
!= 0); /* saved for DMIG */
54 return XFS_EXT_UNWRITTEN
;
60 * Convert on-disk form of btree root to in-memory form.
65 xfs_bmdr_block_t
*dblock
,
67 struct xfs_btree_block
*rblock
,
70 struct xfs_mount
*mp
= ip
->i_mount
;
77 if (xfs_sb_version_hascrc(&mp
->m_sb
))
78 xfs_btree_init_block_int(mp
, rblock
, XFS_BUF_DADDR_NULL
,
79 XFS_BMAP_CRC_MAGIC
, 0, 0, ip
->i_ino
,
80 XFS_BTREE_LONG_PTRS
| XFS_BTREE_CRC_BLOCKS
);
82 xfs_btree_init_block_int(mp
, rblock
, XFS_BUF_DADDR_NULL
,
83 XFS_BMAP_MAGIC
, 0, 0, ip
->i_ino
,
86 rblock
->bb_level
= dblock
->bb_level
;
87 ASSERT(be16_to_cpu(rblock
->bb_level
) > 0);
88 rblock
->bb_numrecs
= dblock
->bb_numrecs
;
89 dmxr
= xfs_bmdr_maxrecs(mp
, dblocklen
, 0);
90 fkp
= XFS_BMDR_KEY_ADDR(dblock
, 1);
91 tkp
= XFS_BMBT_KEY_ADDR(mp
, rblock
, 1);
92 fpp
= XFS_BMDR_PTR_ADDR(dblock
, 1, dmxr
);
93 tpp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, rblocklen
);
94 dmxr
= be16_to_cpu(dblock
->bb_numrecs
);
95 memcpy(tkp
, fkp
, sizeof(*fkp
) * dmxr
);
96 memcpy(tpp
, fpp
, sizeof(*fpp
) * dmxr
);
100 * Convert a compressed bmap extent record to an uncompressed form.
101 * This code must be in sync with the routines xfs_bmbt_get_startoff,
102 * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state.
113 ext_flag
= (int)(l0
>> (64 - BMBT_EXNTFLAG_BITLEN
));
114 s
->br_startoff
= ((xfs_fileoff_t
)l0
&
115 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
117 s
->br_startblock
= (((xfs_fsblock_t
)l0
& xfs_mask64lo(9)) << 43) |
118 (((xfs_fsblock_t
)l1
) >> 21);
124 b
= (((xfs_dfsbno_t
)l0
& xfs_mask64lo(9)) << 43) |
125 (((xfs_dfsbno_t
)l1
) >> 21);
126 ASSERT((b
>> 32) == 0 || isnulldstartblock(b
));
127 s
->br_startblock
= (xfs_fsblock_t
)b
;
130 s
->br_startblock
= (xfs_fsblock_t
)(((xfs_dfsbno_t
)l1
) >> 21);
132 #endif /* XFS_BIG_BLKNOS */
133 s
->br_blockcount
= (xfs_filblks_t
)(l1
& xfs_mask64lo(21));
134 /* This is xfs_extent_state() in-line */
136 ASSERT(s
->br_blockcount
!= 0); /* saved for DMIG */
137 st
= XFS_EXT_UNWRITTEN
;
145 xfs_bmbt_rec_host_t
*r
,
148 __xfs_bmbt_get_all(r
->l0
, r
->l1
, s
);
152 * Extract the blockcount field from an in memory bmap extent record.
155 xfs_bmbt_get_blockcount(
156 xfs_bmbt_rec_host_t
*r
)
158 return (xfs_filblks_t
)(r
->l1
& xfs_mask64lo(21));
162 * Extract the startblock field from an in memory bmap extent record.
165 xfs_bmbt_get_startblock(
166 xfs_bmbt_rec_host_t
*r
)
169 return (((xfs_fsblock_t
)r
->l0
& xfs_mask64lo(9)) << 43) |
170 (((xfs_fsblock_t
)r
->l1
) >> 21);
175 b
= (((xfs_dfsbno_t
)r
->l0
& xfs_mask64lo(9)) << 43) |
176 (((xfs_dfsbno_t
)r
->l1
) >> 21);
177 ASSERT((b
>> 32) == 0 || isnulldstartblock(b
));
178 return (xfs_fsblock_t
)b
;
180 return (xfs_fsblock_t
)(((xfs_dfsbno_t
)r
->l1
) >> 21);
182 #endif /* XFS_BIG_BLKNOS */
186 * Extract the startoff field from an in memory bmap extent record.
189 xfs_bmbt_get_startoff(
190 xfs_bmbt_rec_host_t
*r
)
192 return ((xfs_fileoff_t
)r
->l0
&
193 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
198 xfs_bmbt_rec_host_t
*r
)
202 ext_flag
= (int)((r
->l0
) >> (64 - BMBT_EXNTFLAG_BITLEN
));
203 return xfs_extent_state(xfs_bmbt_get_blockcount(r
),
208 * Extract the blockcount field from an on disk bmap extent record.
211 xfs_bmbt_disk_get_blockcount(
214 return (xfs_filblks_t
)(be64_to_cpu(r
->l1
) & xfs_mask64lo(21));
218 * Extract the startoff field from a disk format bmap extent record.
221 xfs_bmbt_disk_get_startoff(
224 return ((xfs_fileoff_t
)be64_to_cpu(r
->l0
) &
225 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN
)) >> 9;
230 * Set all the fields in a bmap extent record from the arguments.
234 xfs_bmbt_rec_host_t
*r
,
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);
247 ASSERT((startblock
& xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN
)) == 0);
249 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
250 ((xfs_bmbt_rec_base_t
)startoff
<< 9) |
251 ((xfs_bmbt_rec_base_t
)startblock
>> 43);
252 r
->l1
= ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
253 ((xfs_bmbt_rec_base_t
)blockcount
&
254 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21));
255 #else /* !XFS_BIG_BLKNOS */
256 if (isnullstartblock(startblock
)) {
257 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
258 ((xfs_bmbt_rec_base_t
)startoff
<< 9) |
259 (xfs_bmbt_rec_base_t
)xfs_mask64lo(9);
260 r
->l1
= xfs_mask64hi(11) |
261 ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
262 ((xfs_bmbt_rec_base_t
)blockcount
&
263 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21));
265 r
->l0
= ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
266 ((xfs_bmbt_rec_base_t
)startoff
<< 9);
267 r
->l1
= ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
268 ((xfs_bmbt_rec_base_t
)blockcount
&
269 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21));
271 #endif /* XFS_BIG_BLKNOS */
275 * Set all the fields in a bmap extent record from the uncompressed form.
279 xfs_bmbt_rec_host_t
*r
,
282 xfs_bmbt_set_allf(r
, s
->br_startoff
, s
->br_startblock
,
283 s
->br_blockcount
, s
->br_state
);
288 * Set all the fields in a disk format bmap extent record from the arguments.
291 xfs_bmbt_disk_set_allf(
293 xfs_fileoff_t startoff
,
294 xfs_fsblock_t startblock
,
295 xfs_filblks_t blockcount
,
298 int extent_flag
= (state
== XFS_EXT_NORM
) ? 0 : 1;
300 ASSERT(state
== XFS_EXT_NORM
|| state
== XFS_EXT_UNWRITTEN
);
301 ASSERT((startoff
& xfs_mask64hi(64-BMBT_STARTOFF_BITLEN
)) == 0);
302 ASSERT((blockcount
& xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN
)) == 0);
305 ASSERT((startblock
& xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN
)) == 0);
308 ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
309 ((xfs_bmbt_rec_base_t
)startoff
<< 9) |
310 ((xfs_bmbt_rec_base_t
)startblock
>> 43));
312 ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
313 ((xfs_bmbt_rec_base_t
)blockcount
&
314 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21)));
315 #else /* !XFS_BIG_BLKNOS */
316 if (isnullstartblock(startblock
)) {
318 ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
319 ((xfs_bmbt_rec_base_t
)startoff
<< 9) |
320 (xfs_bmbt_rec_base_t
)xfs_mask64lo(9));
321 r
->l1
= cpu_to_be64(xfs_mask64hi(11) |
322 ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
323 ((xfs_bmbt_rec_base_t
)blockcount
&
324 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21)));
327 ((xfs_bmbt_rec_base_t
)extent_flag
<< 63) |
328 ((xfs_bmbt_rec_base_t
)startoff
<< 9));
330 ((xfs_bmbt_rec_base_t
)startblock
<< 21) |
331 ((xfs_bmbt_rec_base_t
)blockcount
&
332 (xfs_bmbt_rec_base_t
)xfs_mask64lo(21)));
334 #endif /* XFS_BIG_BLKNOS */
338 * Set all the fields in a bmap extent record from the uncompressed form.
341 xfs_bmbt_disk_set_all(
345 xfs_bmbt_disk_set_allf(r
, s
->br_startoff
, s
->br_startblock
,
346 s
->br_blockcount
, s
->br_state
);
350 * Set the blockcount field in a bmap extent record.
353 xfs_bmbt_set_blockcount(
354 xfs_bmbt_rec_host_t
*r
,
357 ASSERT((v
& xfs_mask64hi(43)) == 0);
358 r
->l1
= (r
->l1
& (xfs_bmbt_rec_base_t
)xfs_mask64hi(43)) |
359 (xfs_bmbt_rec_base_t
)(v
& xfs_mask64lo(21));
363 * Set the startblock field in a bmap extent record.
366 xfs_bmbt_set_startblock(
367 xfs_bmbt_rec_host_t
*r
,
371 ASSERT((v
& xfs_mask64hi(12)) == 0);
372 r
->l0
= (r
->l0
& (xfs_bmbt_rec_base_t
)xfs_mask64hi(55)) |
373 (xfs_bmbt_rec_base_t
)(v
>> 43);
374 r
->l1
= (r
->l1
& (xfs_bmbt_rec_base_t
)xfs_mask64lo(21)) |
375 (xfs_bmbt_rec_base_t
)(v
<< 21);
376 #else /* !XFS_BIG_BLKNOS */
377 if (isnullstartblock(v
)) {
378 r
->l0
|= (xfs_bmbt_rec_base_t
)xfs_mask64lo(9);
379 r
->l1
= (xfs_bmbt_rec_base_t
)xfs_mask64hi(11) |
380 ((xfs_bmbt_rec_base_t
)v
<< 21) |
381 (r
->l1
& (xfs_bmbt_rec_base_t
)xfs_mask64lo(21));
383 r
->l0
&= ~(xfs_bmbt_rec_base_t
)xfs_mask64lo(9);
384 r
->l1
= ((xfs_bmbt_rec_base_t
)v
<< 21) |
385 (r
->l1
& (xfs_bmbt_rec_base_t
)xfs_mask64lo(21));
387 #endif /* XFS_BIG_BLKNOS */
391 * Set the startoff field in a bmap extent record.
394 xfs_bmbt_set_startoff(
395 xfs_bmbt_rec_host_t
*r
,
398 ASSERT((v
& xfs_mask64hi(9)) == 0);
399 r
->l0
= (r
->l0
& (xfs_bmbt_rec_base_t
) xfs_mask64hi(1)) |
400 ((xfs_bmbt_rec_base_t
)v
<< 9) |
401 (r
->l0
& (xfs_bmbt_rec_base_t
)xfs_mask64lo(9));
405 * Set the extent state field in a bmap extent record.
409 xfs_bmbt_rec_host_t
*r
,
412 ASSERT(v
== XFS_EXT_NORM
|| v
== XFS_EXT_UNWRITTEN
);
413 if (v
== XFS_EXT_NORM
)
414 r
->l0
&= xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN
);
416 r
->l0
|= xfs_mask64hi(BMBT_EXNTFLAG_BITLEN
);
420 * Convert in-memory form of btree root to on-disk form.
424 struct xfs_mount
*mp
,
425 struct xfs_btree_block
*rblock
,
427 xfs_bmdr_block_t
*dblock
,
436 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
437 ASSERT(rblock
->bb_magic
== cpu_to_be32(XFS_BMAP_CRC_MAGIC
));
438 ASSERT(uuid_equal(&rblock
->bb_u
.l
.bb_uuid
, &mp
->m_sb
.sb_uuid
));
439 ASSERT(rblock
->bb_u
.l
.bb_blkno
==
440 cpu_to_be64(XFS_BUF_DADDR_NULL
));
442 ASSERT(rblock
->bb_magic
== cpu_to_be32(XFS_BMAP_MAGIC
));
443 ASSERT(rblock
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLDFSBNO
));
444 ASSERT(rblock
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
));
445 ASSERT(rblock
->bb_level
!= 0);
446 dblock
->bb_level
= rblock
->bb_level
;
447 dblock
->bb_numrecs
= rblock
->bb_numrecs
;
448 dmxr
= xfs_bmdr_maxrecs(mp
, dblocklen
, 0);
449 fkp
= XFS_BMBT_KEY_ADDR(mp
, rblock
, 1);
450 tkp
= XFS_BMDR_KEY_ADDR(dblock
, 1);
451 fpp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, rblocklen
);
452 tpp
= XFS_BMDR_PTR_ADDR(dblock
, 1, dmxr
);
453 dmxr
= be16_to_cpu(dblock
->bb_numrecs
);
454 memcpy(tkp
, fkp
, sizeof(*fkp
) * dmxr
);
455 memcpy(tpp
, fpp
, sizeof(*fpp
) * dmxr
);
459 * Check extent records, which have just been read, for
460 * any bit in the extent flag field. ASSERT on debug
461 * kernels, as this condition should not occur.
462 * Return an error condition (1) if any flags found,
463 * otherwise return 0.
467 xfs_check_nostate_extents(
472 for (; num
> 0; num
--, idx
++) {
473 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
475 (64 - BMBT_EXNTFLAG_BITLEN
)) != 0) {
484 STATIC
struct xfs_btree_cur
*
486 struct xfs_btree_cur
*cur
)
488 struct xfs_btree_cur
*new;
490 new = xfs_bmbt_init_cursor(cur
->bc_mp
, cur
->bc_tp
,
491 cur
->bc_private
.b
.ip
, cur
->bc_private
.b
.whichfork
);
494 * Copy the firstblock, flist, and flags values,
495 * since init cursor doesn't get them.
497 new->bc_private
.b
.firstblock
= cur
->bc_private
.b
.firstblock
;
498 new->bc_private
.b
.flist
= cur
->bc_private
.b
.flist
;
499 new->bc_private
.b
.flags
= cur
->bc_private
.b
.flags
;
505 xfs_bmbt_update_cursor(
506 struct xfs_btree_cur
*src
,
507 struct xfs_btree_cur
*dst
)
509 ASSERT((dst
->bc_private
.b
.firstblock
!= NULLFSBLOCK
) ||
510 (dst
->bc_private
.b
.ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
));
511 ASSERT(dst
->bc_private
.b
.flist
== src
->bc_private
.b
.flist
);
513 dst
->bc_private
.b
.allocated
+= src
->bc_private
.b
.allocated
;
514 dst
->bc_private
.b
.firstblock
= src
->bc_private
.b
.firstblock
;
516 src
->bc_private
.b
.allocated
= 0;
520 xfs_bmbt_alloc_block(
521 struct xfs_btree_cur
*cur
,
522 union xfs_btree_ptr
*start
,
523 union xfs_btree_ptr
*new,
527 xfs_alloc_arg_t args
; /* block allocation args */
528 int error
; /* error return value */
530 memset(&args
, 0, sizeof(args
));
531 args
.tp
= cur
->bc_tp
;
532 args
.mp
= cur
->bc_mp
;
533 args
.fsbno
= cur
->bc_private
.b
.firstblock
;
534 args
.firstblock
= args
.fsbno
;
536 if (args
.fsbno
== NULLFSBLOCK
) {
537 args
.fsbno
= be64_to_cpu(start
->l
);
538 args
.type
= XFS_ALLOCTYPE_START_BNO
;
540 * Make sure there is sufficient room left in the AG to
541 * complete a full tree split for an extent insert. If
542 * we are converting the middle part of an extent then
543 * we may need space for two tree splits.
545 * We are relying on the caller to make the correct block
546 * reservation for this operation to succeed. If the
547 * reservation amount is insufficient then we may fail a
548 * block allocation here and corrupt the filesystem.
550 args
.minleft
= xfs_trans_get_block_res(args
.tp
);
551 } else if (cur
->bc_private
.b
.flist
->xbf_low
) {
552 args
.type
= XFS_ALLOCTYPE_START_BNO
;
554 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
557 args
.minlen
= args
.maxlen
= args
.prod
= 1;
558 args
.wasdel
= cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
;
559 if (!args
.wasdel
&& xfs_trans_get_block_res(args
.tp
) == 0) {
560 error
= XFS_ERROR(ENOSPC
);
563 error
= xfs_alloc_vextent(&args
);
567 if (args
.fsbno
== NULLFSBLOCK
&& args
.minleft
) {
569 * Could not find an AG with enough free space to satisfy
570 * a full btree split. Try again without minleft and if
571 * successful activate the lowspace algorithm.
574 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
576 error
= xfs_alloc_vextent(&args
);
579 cur
->bc_private
.b
.flist
->xbf_low
= 1;
581 if (args
.fsbno
== NULLFSBLOCK
) {
582 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
586 ASSERT(args
.len
== 1);
587 cur
->bc_private
.b
.firstblock
= args
.fsbno
;
588 cur
->bc_private
.b
.allocated
++;
589 cur
->bc_private
.b
.ip
->i_d
.di_nblocks
++;
590 xfs_trans_log_inode(args
.tp
, cur
->bc_private
.b
.ip
, XFS_ILOG_CORE
);
591 xfs_trans_mod_dquot_byino(args
.tp
, cur
->bc_private
.b
.ip
,
592 XFS_TRANS_DQ_BCOUNT
, 1L);
594 new->l
= cpu_to_be64(args
.fsbno
);
596 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
601 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
607 struct xfs_btree_cur
*cur
,
610 struct xfs_mount
*mp
= cur
->bc_mp
;
611 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
612 struct xfs_trans
*tp
= cur
->bc_tp
;
613 xfs_fsblock_t fsbno
= XFS_DADDR_TO_FSB(mp
, XFS_BUF_ADDR(bp
));
615 xfs_bmap_add_free(fsbno
, 1, cur
->bc_private
.b
.flist
, mp
);
616 ip
->i_d
.di_nblocks
--;
618 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
619 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
620 xfs_trans_binval(tp
, bp
);
625 xfs_bmbt_get_minrecs(
626 struct xfs_btree_cur
*cur
,
629 if (level
== cur
->bc_nlevels
- 1) {
630 struct xfs_ifork
*ifp
;
632 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
,
633 cur
->bc_private
.b
.whichfork
);
635 return xfs_bmbt_maxrecs(cur
->bc_mp
,
636 ifp
->if_broot_bytes
, level
== 0) / 2;
639 return cur
->bc_mp
->m_bmap_dmnr
[level
!= 0];
643 xfs_bmbt_get_maxrecs(
644 struct xfs_btree_cur
*cur
,
647 if (level
== cur
->bc_nlevels
- 1) {
648 struct xfs_ifork
*ifp
;
650 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
,
651 cur
->bc_private
.b
.whichfork
);
653 return xfs_bmbt_maxrecs(cur
->bc_mp
,
654 ifp
->if_broot_bytes
, level
== 0);
657 return cur
->bc_mp
->m_bmap_dmxr
[level
!= 0];
662 * Get the maximum records we could store in the on-disk format.
664 * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but
665 * for the root node this checks the available space in the dinode fork
666 * so that we can resize the in-memory buffer to match it. After a
667 * resize to the maximum size this function returns the same value
668 * as xfs_bmbt_get_maxrecs for the root node, too.
671 xfs_bmbt_get_dmaxrecs(
672 struct xfs_btree_cur
*cur
,
675 if (level
!= cur
->bc_nlevels
- 1)
676 return cur
->bc_mp
->m_bmap_dmxr
[level
!= 0];
677 return xfs_bmdr_maxrecs(cur
->bc_mp
, cur
->bc_private
.b
.forksize
,
682 xfs_bmbt_init_key_from_rec(
683 union xfs_btree_key
*key
,
684 union xfs_btree_rec
*rec
)
686 key
->bmbt
.br_startoff
=
687 cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec
->bmbt
));
691 xfs_bmbt_init_rec_from_key(
692 union xfs_btree_key
*key
,
693 union xfs_btree_rec
*rec
)
695 ASSERT(key
->bmbt
.br_startoff
!= 0);
697 xfs_bmbt_disk_set_allf(&rec
->bmbt
, be64_to_cpu(key
->bmbt
.br_startoff
),
702 xfs_bmbt_init_rec_from_cur(
703 struct xfs_btree_cur
*cur
,
704 union xfs_btree_rec
*rec
)
706 xfs_bmbt_disk_set_all(&rec
->bmbt
, &cur
->bc_rec
.b
);
710 xfs_bmbt_init_ptr_from_cur(
711 struct xfs_btree_cur
*cur
,
712 union xfs_btree_ptr
*ptr
)
719 struct xfs_btree_cur
*cur
,
720 union xfs_btree_key
*key
)
722 return (__int64_t
)be64_to_cpu(key
->bmbt
.br_startoff
) -
723 cur
->bc_rec
.b
.br_startoff
;
730 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
731 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
734 switch (block
->bb_magic
) {
735 case cpu_to_be32(XFS_BMAP_CRC_MAGIC
):
736 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
738 if (!uuid_equal(&block
->bb_u
.l
.bb_uuid
, &mp
->m_sb
.sb_uuid
))
740 if (be64_to_cpu(block
->bb_u
.l
.bb_blkno
) != bp
->b_bn
)
743 * XXX: need a better way of verifying the owner here. Right now
744 * just make sure there has been one set.
746 if (be64_to_cpu(block
->bb_u
.l
.bb_owner
) == 0)
749 case cpu_to_be32(XFS_BMAP_MAGIC
):
756 * numrecs and level verification.
758 * We don't know what fork we belong to, so just verify that the level
759 * is less than the maximum of the two. Later checks will be more
762 level
= be16_to_cpu(block
->bb_level
);
763 if (level
> max(mp
->m_bm_maxlevels
[0], mp
->m_bm_maxlevels
[1]))
765 if (be16_to_cpu(block
->bb_numrecs
) > mp
->m_bmap_dmxr
[level
!= 0])
768 /* sibling pointer verification */
769 if (!block
->bb_u
.l
.bb_leftsib
||
770 (block
->bb_u
.l
.bb_leftsib
!= cpu_to_be64(NULLDFSBNO
) &&
771 !XFS_FSB_SANITY_CHECK(mp
, be64_to_cpu(block
->bb_u
.l
.bb_leftsib
))))
773 if (!block
->bb_u
.l
.bb_rightsib
||
774 (block
->bb_u
.l
.bb_rightsib
!= cpu_to_be64(NULLDFSBNO
) &&
775 !XFS_FSB_SANITY_CHECK(mp
, be64_to_cpu(block
->bb_u
.l
.bb_rightsib
))))
782 xfs_bmbt_read_verify(
785 if (!(xfs_btree_lblock_verify_crc(bp
) &&
786 xfs_bmbt_verify(bp
))) {
787 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
788 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
,
789 bp
->b_target
->bt_mount
, bp
->b_addr
);
790 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
795 xfs_bmbt_write_verify(
798 if (!xfs_bmbt_verify(bp
)) {
799 xfs_warn(bp
->b_target
->bt_mount
, "bmbt daddr 0x%llx failed", bp
->b_bn
);
800 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
801 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
,
802 bp
->b_target
->bt_mount
, bp
->b_addr
);
803 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
806 xfs_btree_lblock_calc_crc(bp
);
809 const struct xfs_buf_ops xfs_bmbt_buf_ops
= {
810 .verify_read
= xfs_bmbt_read_verify
,
811 .verify_write
= xfs_bmbt_write_verify
,
815 #if defined(DEBUG) || defined(XFS_WARN)
817 xfs_bmbt_keys_inorder(
818 struct xfs_btree_cur
*cur
,
819 union xfs_btree_key
*k1
,
820 union xfs_btree_key
*k2
)
822 return be64_to_cpu(k1
->bmbt
.br_startoff
) <
823 be64_to_cpu(k2
->bmbt
.br_startoff
);
827 xfs_bmbt_recs_inorder(
828 struct xfs_btree_cur
*cur
,
829 union xfs_btree_rec
*r1
,
830 union xfs_btree_rec
*r2
)
832 return xfs_bmbt_disk_get_startoff(&r1
->bmbt
) +
833 xfs_bmbt_disk_get_blockcount(&r1
->bmbt
) <=
834 xfs_bmbt_disk_get_startoff(&r2
->bmbt
);
838 static const struct xfs_btree_ops xfs_bmbt_ops
= {
839 .rec_len
= sizeof(xfs_bmbt_rec_t
),
840 .key_len
= sizeof(xfs_bmbt_key_t
),
842 .dup_cursor
= xfs_bmbt_dup_cursor
,
843 .update_cursor
= xfs_bmbt_update_cursor
,
844 .alloc_block
= xfs_bmbt_alloc_block
,
845 .free_block
= xfs_bmbt_free_block
,
846 .get_maxrecs
= xfs_bmbt_get_maxrecs
,
847 .get_minrecs
= xfs_bmbt_get_minrecs
,
848 .get_dmaxrecs
= xfs_bmbt_get_dmaxrecs
,
849 .init_key_from_rec
= xfs_bmbt_init_key_from_rec
,
850 .init_rec_from_key
= xfs_bmbt_init_rec_from_key
,
851 .init_rec_from_cur
= xfs_bmbt_init_rec_from_cur
,
852 .init_ptr_from_cur
= xfs_bmbt_init_ptr_from_cur
,
853 .key_diff
= xfs_bmbt_key_diff
,
854 .buf_ops
= &xfs_bmbt_buf_ops
,
855 #if defined(DEBUG) || defined(XFS_WARN)
856 .keys_inorder
= xfs_bmbt_keys_inorder
,
857 .recs_inorder
= xfs_bmbt_recs_inorder
,
862 * Allocate a new bmap btree cursor.
864 struct xfs_btree_cur
* /* new bmap btree cursor */
865 xfs_bmbt_init_cursor(
866 struct xfs_mount
*mp
, /* file system mount point */
867 struct xfs_trans
*tp
, /* transaction pointer */
868 struct xfs_inode
*ip
, /* inode owning the btree */
869 int whichfork
) /* data or attr fork */
871 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
872 struct xfs_btree_cur
*cur
;
874 cur
= kmem_zone_zalloc(xfs_btree_cur_zone
, KM_SLEEP
);
878 cur
->bc_nlevels
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
879 cur
->bc_btnum
= XFS_BTNUM_BMAP
;
880 cur
->bc_blocklog
= mp
->m_sb
.sb_blocklog
;
882 cur
->bc_ops
= &xfs_bmbt_ops
;
883 cur
->bc_flags
= XFS_BTREE_LONG_PTRS
| XFS_BTREE_ROOT_IN_INODE
;
884 if (xfs_sb_version_hascrc(&mp
->m_sb
))
885 cur
->bc_flags
|= XFS_BTREE_CRC_BLOCKS
;
887 cur
->bc_private
.b
.forksize
= XFS_IFORK_SIZE(ip
, whichfork
);
888 cur
->bc_private
.b
.ip
= ip
;
889 cur
->bc_private
.b
.firstblock
= NULLFSBLOCK
;
890 cur
->bc_private
.b
.flist
= NULL
;
891 cur
->bc_private
.b
.allocated
= 0;
892 cur
->bc_private
.b
.flags
= 0;
893 cur
->bc_private
.b
.whichfork
= whichfork
;
899 * Calculate number of records in a bmap btree block.
903 struct xfs_mount
*mp
,
907 blocklen
-= XFS_BMBT_BLOCK_LEN(mp
);
910 return blocklen
/ sizeof(xfs_bmbt_rec_t
);
911 return blocklen
/ (sizeof(xfs_bmbt_key_t
) + sizeof(xfs_bmbt_ptr_t
));
915 * Calculate number of records in a bmap btree inode root.
919 struct xfs_mount
*mp
,
923 blocklen
-= sizeof(xfs_bmdr_block_t
);
926 return blocklen
/ sizeof(xfs_bmdr_rec_t
);
927 return blocklen
/ (sizeof(xfs_bmdr_key_t
) + sizeof(xfs_bmdr_ptr_t
));
931 * Change the owner of a btree format fork fo the inode passed in. Change it to
932 * the owner of that is passed in so that we can change owners before or after
933 * we switch forks between inodes. The operation that the caller is doing will
934 * determine whether is needs to change owner before or after the switch.
936 * For demand paged transactional modification, the fork switch should be done
937 * after reading in all the blocks, modifying them and pinning them in the
938 * transaction. For modification when the buffers are already pinned in memory,
939 * the fork switch can be done before changing the owner as we won't need to
940 * validate the owner until the btree buffers are unpinned and writes can occur
943 * For recovery based ownership change, there is no transactional context and
944 * so a buffer list must be supplied so that we can record the buffers that we
945 * modified for the caller to issue IO on.
948 xfs_bmbt_change_owner(
949 struct xfs_trans
*tp
,
950 struct xfs_inode
*ip
,
953 struct list_head
*buffer_list
)
955 struct xfs_btree_cur
*cur
;
958 ASSERT(tp
|| buffer_list
);
959 ASSERT(!(tp
&& buffer_list
));
960 if (whichfork
== XFS_DATA_FORK
)
961 ASSERT(ip
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
);
963 ASSERT(ip
->i_d
.di_aformat
== XFS_DINODE_FMT_BTREE
);
965 cur
= xfs_bmbt_init_cursor(ip
->i_mount
, tp
, ip
, whichfork
);
969 error
= xfs_btree_change_owner(cur
, new_owner
, buffer_list
);
970 xfs_btree_del_cursor(cur
, error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);