2018-06-06 10:42:14 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2005-11-02 11:58:39 +08:00
|
|
|
* Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
|
|
|
|
* All Rights Reserved.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
#include "xfs.h"
|
2005-11-02 11:38:42 +08:00
|
|
|
#include "xfs_fs.h"
|
2013-10-29 19:11:58 +08:00
|
|
|
#include "xfs_shared.h"
|
2013-10-23 07:51:50 +08:00
|
|
|
#include "xfs_format.h"
|
2013-10-23 07:50:10 +08:00
|
|
|
#include "xfs_log_format.h"
|
|
|
|
#include "xfs_trans_resv.h"
|
2005-04-17 06:20:36 +08:00
|
|
|
#include "xfs_sb.h"
|
|
|
|
#include "xfs_mount.h"
|
|
|
|
#include "xfs_btree.h"
|
2013-10-23 07:51:50 +08:00
|
|
|
#include "xfs_alloc_btree.h"
|
2005-04-17 06:20:36 +08:00
|
|
|
#include "xfs_alloc.h"
|
2012-04-29 18:39:43 +08:00
|
|
|
#include "xfs_extent_busy.h"
|
2005-04-17 06:20:36 +08:00
|
|
|
#include "xfs_error.h"
|
2009-12-15 07:14:59 +08:00
|
|
|
#include "xfs_trace.h"
|
2013-04-22 03:53:46 +08:00
|
|
|
#include "xfs_cksum.h"
|
2013-10-23 07:50:10 +08:00
|
|
|
#include "xfs_trans.h"
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
|
2008-10-30 13:56:32 +08:00
|
|
|
STATIC struct xfs_btree_cur *
|
|
|
|
xfs_allocbt_dup_cursor(
|
|
|
|
struct xfs_btree_cur *cur)
|
|
|
|
{
|
|
|
|
return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
|
|
|
|
cur->bc_private.a.agbp, cur->bc_private.a.agno,
|
|
|
|
cur->bc_btnum);
|
|
|
|
}
|
|
|
|
|
2008-10-30 13:57:16 +08:00
|
|
|
STATIC void
|
|
|
|
xfs_allocbt_set_root(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_ptr *ptr,
|
|
|
|
int inc)
|
|
|
|
{
|
|
|
|
struct xfs_buf *agbp = cur->bc_private.a.agbp;
|
|
|
|
struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
|
|
|
|
xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
|
|
|
|
int btnum = cur->bc_btnum;
|
2010-01-11 19:47:41 +08:00
|
|
|
struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno);
|
2008-10-30 13:57:16 +08:00
|
|
|
|
|
|
|
ASSERT(ptr->s != 0);
|
|
|
|
|
|
|
|
agf->agf_roots[btnum] = ptr->s;
|
|
|
|
be32_add_cpu(&agf->agf_levels[btnum], inc);
|
2010-01-11 19:47:41 +08:00
|
|
|
pag->pagf_levels[btnum] += inc;
|
|
|
|
xfs_perag_put(pag);
|
2008-10-30 13:57:16 +08:00
|
|
|
|
|
|
|
xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
|
|
|
|
}
|
|
|
|
|
2008-10-30 13:57:03 +08:00
|
|
|
STATIC int
|
|
|
|
xfs_allocbt_alloc_block(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_ptr *start,
|
|
|
|
union xfs_btree_ptr *new,
|
|
|
|
int *stat)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
xfs_agblock_t bno;
|
|
|
|
|
|
|
|
/* Allocate the new block from the freelist. If we can't, give up. */
|
|
|
|
error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
|
|
|
|
&bno, 1);
|
2018-03-07 09:03:30 +08:00
|
|
|
if (error)
|
2008-10-30 13:57:03 +08:00
|
|
|
return error;
|
|
|
|
|
|
|
|
if (bno == NULLAGBLOCK) {
|
|
|
|
*stat = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2011-04-25 03:06:16 +08:00
|
|
|
|
2012-04-29 18:41:10 +08:00
|
|
|
xfs_extent_busy_reuse(cur->bc_mp, cur->bc_private.a.agno, bno, 1, false);
|
2008-10-30 13:57:03 +08:00
|
|
|
|
|
|
|
xfs_trans_agbtree_delta(cur->bc_tp, 1);
|
|
|
|
new->s = cpu_to_be32(bno);
|
|
|
|
|
|
|
|
*stat = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-30 13:57:51 +08:00
|
|
|
STATIC int
|
|
|
|
xfs_allocbt_free_block(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
struct xfs_buf *bp)
|
|
|
|
{
|
|
|
|
struct xfs_buf *agbp = cur->bc_private.a.agbp;
|
|
|
|
struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
|
|
|
|
xfs_agblock_t bno;
|
|
|
|
int error;
|
|
|
|
|
2009-01-15 13:22:07 +08:00
|
|
|
bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
|
2008-10-30 13:57:51 +08:00
|
|
|
error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
2012-04-29 18:41:10 +08:00
|
|
|
xfs_extent_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1,
|
|
|
|
XFS_EXTENT_BUSY_SKIP_DISCARD);
|
2008-10-30 13:57:51 +08:00
|
|
|
xfs_trans_agbtree_delta(cur->bc_tp, -1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2008-10-30 13:56:32 +08:00
|
|
|
* Update the longest extent in the AGF
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2008-10-30 13:56:32 +08:00
|
|
|
STATIC void
|
|
|
|
xfs_allocbt_update_lastrec(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
struct xfs_btree_block *block,
|
|
|
|
union xfs_btree_rec *rec,
|
|
|
|
int ptr,
|
|
|
|
int reason)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-10-30 13:56:32 +08:00
|
|
|
struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
|
|
|
|
xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
|
2010-01-11 19:47:41 +08:00
|
|
|
struct xfs_perag *pag;
|
2008-10-30 13:56:32 +08:00
|
|
|
__be32 len;
|
2008-10-30 13:58:01 +08:00
|
|
|
int numrecs;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-10-30 13:56:32 +08:00
|
|
|
ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-10-30 13:56:32 +08:00
|
|
|
switch (reason) {
|
|
|
|
case LASTREC_UPDATE:
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2008-10-30 13:56:32 +08:00
|
|
|
* If this is the last leaf block and it's the last record,
|
|
|
|
* then update the size of the longest extent in the AG.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2008-10-30 13:56:32 +08:00
|
|
|
if (ptr != xfs_btree_get_numrecs(block))
|
|
|
|
return;
|
|
|
|
len = rec->alloc.ar_blockcount;
|
|
|
|
break;
|
2008-10-30 13:57:40 +08:00
|
|
|
case LASTREC_INSREC:
|
|
|
|
if (be32_to_cpu(rec->alloc.ar_blockcount) <=
|
|
|
|
be32_to_cpu(agf->agf_longest))
|
|
|
|
return;
|
|
|
|
len = rec->alloc.ar_blockcount;
|
2008-10-30 13:58:01 +08:00
|
|
|
break;
|
|
|
|
case LASTREC_DELREC:
|
|
|
|
numrecs = xfs_btree_get_numrecs(block);
|
|
|
|
if (ptr <= numrecs)
|
|
|
|
return;
|
|
|
|
ASSERT(ptr == numrecs + 1);
|
|
|
|
|
|
|
|
if (numrecs) {
|
|
|
|
xfs_alloc_rec_t *rrp;
|
|
|
|
|
2008-10-30 14:11:40 +08:00
|
|
|
rrp = XFS_ALLOC_REC_ADDR(cur->bc_mp, block, numrecs);
|
2008-10-30 13:58:01 +08:00
|
|
|
len = rrp->ar_blockcount;
|
|
|
|
} else {
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
|
2008-10-30 13:57:40 +08:00
|
|
|
break;
|
2008-10-30 13:56:32 +08:00
|
|
|
default:
|
|
|
|
ASSERT(0);
|
|
|
|
return;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2008-10-30 13:53:59 +08:00
|
|
|
|
2008-10-30 13:56:32 +08:00
|
|
|
agf->agf_longest = len;
|
2010-01-11 19:47:41 +08:00
|
|
|
pag = xfs_perag_get(cur->bc_mp, seqno);
|
|
|
|
pag->pagf_longest = be32_to_cpu(len);
|
|
|
|
xfs_perag_put(pag);
|
2008-10-30 13:56:32 +08:00
|
|
|
xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
|
2008-10-30 13:53:59 +08:00
|
|
|
}
|
|
|
|
|
2008-10-30 13:58:01 +08:00
|
|
|
STATIC int
|
|
|
|
xfs_allocbt_get_minrecs(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
int level)
|
|
|
|
{
|
|
|
|
return cur->bc_mp->m_alloc_mnr[level != 0];
|
|
|
|
}
|
|
|
|
|
2008-10-30 13:55:23 +08:00
|
|
|
STATIC int
|
|
|
|
xfs_allocbt_get_maxrecs(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
int level)
|
|
|
|
{
|
|
|
|
return cur->bc_mp->m_alloc_mxr[level != 0];
|
|
|
|
}
|
|
|
|
|
2008-10-30 13:56:09 +08:00
|
|
|
STATIC void
|
|
|
|
xfs_allocbt_init_key_from_rec(
|
|
|
|
union xfs_btree_key *key,
|
|
|
|
union xfs_btree_rec *rec)
|
|
|
|
{
|
|
|
|
key->alloc.ar_startblock = rec->alloc.ar_startblock;
|
|
|
|
key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
|
|
|
|
}
|
|
|
|
|
2017-03-29 05:56:34 +08:00
|
|
|
STATIC void
|
|
|
|
xfs_bnobt_init_high_key_from_rec(
|
|
|
|
union xfs_btree_key *key,
|
|
|
|
union xfs_btree_rec *rec)
|
|
|
|
{
|
|
|
|
__u32 x;
|
|
|
|
|
|
|
|
x = be32_to_cpu(rec->alloc.ar_startblock);
|
|
|
|
x += be32_to_cpu(rec->alloc.ar_blockcount) - 1;
|
|
|
|
key->alloc.ar_startblock = cpu_to_be32(x);
|
|
|
|
key->alloc.ar_blockcount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void
|
|
|
|
xfs_cntbt_init_high_key_from_rec(
|
|
|
|
union xfs_btree_key *key,
|
|
|
|
union xfs_btree_rec *rec)
|
|
|
|
{
|
|
|
|
key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
|
|
|
|
key->alloc.ar_startblock = 0;
|
|
|
|
}
|
|
|
|
|
2008-10-30 13:57:40 +08:00
|
|
|
STATIC void
|
|
|
|
xfs_allocbt_init_rec_from_cur(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_rec *rec)
|
|
|
|
{
|
|
|
|
rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
|
|
|
|
rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
|
|
|
|
}
|
|
|
|
|
2008-10-30 13:56:09 +08:00
|
|
|
STATIC void
|
|
|
|
xfs_allocbt_init_ptr_from_cur(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_ptr *ptr)
|
|
|
|
{
|
|
|
|
struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
|
|
|
|
|
|
|
|
ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
|
|
|
|
|
|
|
|
ptr->s = agf->agf_roots[cur->bc_btnum];
|
|
|
|
}
|
|
|
|
|
2017-06-17 02:00:05 +08:00
|
|
|
STATIC int64_t
|
2017-03-29 05:56:34 +08:00
|
|
|
xfs_bnobt_key_diff(
|
2008-10-30 13:56:09 +08:00
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_key *key)
|
|
|
|
{
|
|
|
|
xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
|
|
|
|
xfs_alloc_key_t *kp = &key->alloc;
|
|
|
|
|
2017-06-17 02:00:05 +08:00
|
|
|
return (int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
|
2017-03-29 05:56:34 +08:00
|
|
|
}
|
|
|
|
|
2017-06-17 02:00:05 +08:00
|
|
|
STATIC int64_t
|
2017-03-29 05:56:34 +08:00
|
|
|
xfs_cntbt_key_diff(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_key *key)
|
|
|
|
{
|
|
|
|
xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
|
|
|
|
xfs_alloc_key_t *kp = &key->alloc;
|
2017-06-17 02:00:05 +08:00
|
|
|
int64_t diff;
|
2008-10-30 13:56:09 +08:00
|
|
|
|
2017-06-17 02:00:05 +08:00
|
|
|
diff = (int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
|
2008-10-30 13:56:09 +08:00
|
|
|
if (diff)
|
|
|
|
return diff;
|
|
|
|
|
2017-06-17 02:00:05 +08:00
|
|
|
return (int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
|
2008-10-30 13:56:09 +08:00
|
|
|
}
|
|
|
|
|
2017-06-17 02:00:05 +08:00
|
|
|
STATIC int64_t
|
2017-03-29 05:56:34 +08:00
|
|
|
xfs_bnobt_diff_two_keys(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_key *k1,
|
|
|
|
union xfs_btree_key *k2)
|
|
|
|
{
|
2017-06-17 02:00:05 +08:00
|
|
|
return (int64_t)be32_to_cpu(k1->alloc.ar_startblock) -
|
2017-03-29 05:56:34 +08:00
|
|
|
be32_to_cpu(k2->alloc.ar_startblock);
|
|
|
|
}
|
|
|
|
|
2017-06-17 02:00:05 +08:00
|
|
|
STATIC int64_t
|
2017-03-29 05:56:34 +08:00
|
|
|
xfs_cntbt_diff_two_keys(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_key *k1,
|
|
|
|
union xfs_btree_key *k2)
|
|
|
|
{
|
2017-06-17 02:00:05 +08:00
|
|
|
int64_t diff;
|
2017-03-29 05:56:34 +08:00
|
|
|
|
|
|
|
diff = be32_to_cpu(k1->alloc.ar_blockcount) -
|
|
|
|
be32_to_cpu(k2->alloc.ar_blockcount);
|
|
|
|
if (diff)
|
|
|
|
return diff;
|
|
|
|
|
|
|
|
return be32_to_cpu(k1->alloc.ar_startblock) -
|
|
|
|
be32_to_cpu(k2->alloc.ar_startblock);
|
|
|
|
}
|
|
|
|
|
2018-01-09 02:51:03 +08:00
|
|
|
static xfs_failaddr_t
|
2012-11-14 14:52:32 +08:00
|
|
|
xfs_allocbt_verify(
|
2012-11-12 19:54:08 +08:00
|
|
|
struct xfs_buf *bp)
|
|
|
|
{
|
|
|
|
struct xfs_mount *mp = bp->b_target->bt_mount;
|
|
|
|
struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
|
|
|
|
struct xfs_perag *pag = bp->b_pag;
|
2018-01-09 02:51:03 +08:00
|
|
|
xfs_failaddr_t fa;
|
2012-11-12 19:54:08 +08:00
|
|
|
unsigned int level;
|
2019-02-08 02:45:47 +08:00
|
|
|
xfs_btnum_t btnum = XFS_BTNUM_BNOi;
|
|
|
|
|
|
|
|
if (!xfs_verify_magic(bp, block->bb_magic))
|
|
|
|
return __this_address;
|
|
|
|
|
|
|
|
if (xfs_sb_version_hascrc(&mp->m_sb)) {
|
|
|
|
fa = xfs_btree_sblock_v5hdr_verify(bp);
|
|
|
|
if (fa)
|
|
|
|
return fa;
|
|
|
|
}
|
2012-11-12 19:54:08 +08:00
|
|
|
|
|
|
|
/*
|
2019-02-08 02:45:47 +08:00
|
|
|
* The perag may not be attached during grow operations or fully
|
|
|
|
* initialized from the AGF during log recovery. Therefore we can only
|
|
|
|
* check against maximum tree depth from those contexts.
|
2013-04-22 03:53:46 +08:00
|
|
|
*
|
2019-02-08 02:45:47 +08:00
|
|
|
* Otherwise check against the per-tree limit. Peek at one of the
|
|
|
|
* verifier magic values to determine the type of tree we're verifying
|
|
|
|
* against.
|
2012-11-12 19:54:08 +08:00
|
|
|
*/
|
|
|
|
level = be16_to_cpu(block->bb_level);
|
2019-02-08 02:45:47 +08:00
|
|
|
if (bp->b_ops->magic[0] == cpu_to_be32(XFS_ABTC_MAGIC))
|
|
|
|
btnum = XFS_BTNUM_CNTi;
|
|
|
|
if (pag && pag->pagf_init) {
|
|
|
|
if (level >= pag->pagf_levels[btnum])
|
2018-01-09 02:51:03 +08:00
|
|
|
return __this_address;
|
2019-02-08 02:45:47 +08:00
|
|
|
} else if (level >= mp->m_ag_maxlevels)
|
2018-01-09 02:51:03 +08:00
|
|
|
return __this_address;
|
2012-11-12 19:54:08 +08:00
|
|
|
|
2016-01-04 13:13:21 +08:00
|
|
|
return xfs_btree_sblock_verify(bp, mp->m_alloc_mxr[level != 0]);
|
2012-11-14 14:52:32 +08:00
|
|
|
}
|
2012-11-12 19:54:08 +08:00
|
|
|
|
2012-11-14 14:52:32 +08:00
|
|
|
static void
|
2012-11-14 14:54:40 +08:00
|
|
|
xfs_allocbt_read_verify(
|
2012-11-14 14:52:32 +08:00
|
|
|
struct xfs_buf *bp)
|
|
|
|
{
|
2018-01-09 02:51:03 +08:00
|
|
|
xfs_failaddr_t fa;
|
|
|
|
|
2014-02-27 12:23:10 +08:00
|
|
|
if (!xfs_btree_sblock_verify_crc(bp))
|
2018-01-09 02:51:03 +08:00
|
|
|
xfs_verifier_error(bp, -EFSBADCRC, __this_address);
|
|
|
|
else {
|
|
|
|
fa = xfs_allocbt_verify(bp);
|
|
|
|
if (fa)
|
|
|
|
xfs_verifier_error(bp, -EFSCORRUPTED, fa);
|
|
|
|
}
|
2014-02-27 12:23:10 +08:00
|
|
|
|
2018-01-09 02:51:02 +08:00
|
|
|
if (bp->b_error)
|
2014-02-27 12:23:10 +08:00
|
|
|
trace_xfs_btree_corrupt(bp, _RET_IP_);
|
2012-11-14 14:52:32 +08:00
|
|
|
}
|
|
|
|
|
2012-11-14 14:54:40 +08:00
|
|
|
static void
|
|
|
|
xfs_allocbt_write_verify(
|
2012-11-14 14:52:32 +08:00
|
|
|
struct xfs_buf *bp)
|
|
|
|
{
|
2018-01-09 02:51:03 +08:00
|
|
|
xfs_failaddr_t fa;
|
|
|
|
|
|
|
|
fa = xfs_allocbt_verify(bp);
|
|
|
|
if (fa) {
|
2013-04-22 03:53:46 +08:00
|
|
|
trace_xfs_btree_corrupt(bp, _RET_IP_);
|
2018-01-09 02:51:03 +08:00
|
|
|
xfs_verifier_error(bp, -EFSCORRUPTED, fa);
|
2014-02-27 12:14:31 +08:00
|
|
|
return;
|
2013-04-22 03:53:46 +08:00
|
|
|
}
|
|
|
|
xfs_btree_sblock_calc_crc(bp);
|
|
|
|
|
2012-11-12 19:54:08 +08:00
|
|
|
}
|
|
|
|
|
2019-02-08 02:45:47 +08:00
|
|
|
const struct xfs_buf_ops xfs_bnobt_buf_ops = {
|
|
|
|
.name = "xfs_bnobt",
|
2019-02-08 02:45:47 +08:00
|
|
|
.magic = { cpu_to_be32(XFS_ABTB_MAGIC),
|
|
|
|
cpu_to_be32(XFS_ABTB_CRC_MAGIC) },
|
2012-11-14 14:54:40 +08:00
|
|
|
.verify_read = xfs_allocbt_read_verify,
|
|
|
|
.verify_write = xfs_allocbt_write_verify,
|
2018-01-09 02:51:08 +08:00
|
|
|
.verify_struct = xfs_allocbt_verify,
|
2012-11-14 14:54:40 +08:00
|
|
|
};
|
|
|
|
|
2019-02-08 02:45:47 +08:00
|
|
|
const struct xfs_buf_ops xfs_cntbt_buf_ops = {
|
|
|
|
.name = "xfs_cntbt",
|
2019-02-08 02:45:47 +08:00
|
|
|
.magic = { cpu_to_be32(XFS_ABTC_MAGIC),
|
|
|
|
cpu_to_be32(XFS_ABTC_CRC_MAGIC) },
|
2019-02-08 02:45:47 +08:00
|
|
|
.verify_read = xfs_allocbt_read_verify,
|
|
|
|
.verify_write = xfs_allocbt_write_verify,
|
|
|
|
.verify_struct = xfs_allocbt_verify,
|
|
|
|
};
|
2012-11-14 14:54:40 +08:00
|
|
|
|
2008-10-30 13:58:32 +08:00
|
|
|
STATIC int
|
2017-03-29 05:56:34 +08:00
|
|
|
xfs_bnobt_keys_inorder(
|
2008-10-30 13:58:32 +08:00
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_key *k1,
|
|
|
|
union xfs_btree_key *k2)
|
|
|
|
{
|
2017-03-29 05:56:34 +08:00
|
|
|
return be32_to_cpu(k1->alloc.ar_startblock) <
|
|
|
|
be32_to_cpu(k2->alloc.ar_startblock);
|
2008-10-30 13:58:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int
|
2017-03-29 05:56:34 +08:00
|
|
|
xfs_bnobt_recs_inorder(
|
2008-10-30 13:58:32 +08:00
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_rec *r1,
|
|
|
|
union xfs_btree_rec *r2)
|
|
|
|
{
|
2017-03-29 05:56:34 +08:00
|
|
|
return be32_to_cpu(r1->alloc.ar_startblock) +
|
|
|
|
be32_to_cpu(r1->alloc.ar_blockcount) <=
|
|
|
|
be32_to_cpu(r2->alloc.ar_startblock);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int
|
|
|
|
xfs_cntbt_keys_inorder(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_key *k1,
|
|
|
|
union xfs_btree_key *k2)
|
|
|
|
{
|
|
|
|
return be32_to_cpu(k1->alloc.ar_blockcount) <
|
|
|
|
be32_to_cpu(k2->alloc.ar_blockcount) ||
|
|
|
|
(k1->alloc.ar_blockcount == k2->alloc.ar_blockcount &&
|
|
|
|
be32_to_cpu(k1->alloc.ar_startblock) <
|
|
|
|
be32_to_cpu(k2->alloc.ar_startblock));
|
2008-10-30 13:58:32 +08:00
|
|
|
}
|
|
|
|
|
2017-03-29 05:56:34 +08:00
|
|
|
STATIC int
|
|
|
|
xfs_cntbt_recs_inorder(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_rec *r1,
|
|
|
|
union xfs_btree_rec *r2)
|
|
|
|
{
|
|
|
|
return be32_to_cpu(r1->alloc.ar_blockcount) <
|
|
|
|
be32_to_cpu(r2->alloc.ar_blockcount) ||
|
|
|
|
(r1->alloc.ar_blockcount == r2->alloc.ar_blockcount &&
|
|
|
|
be32_to_cpu(r1->alloc.ar_startblock) <
|
|
|
|
be32_to_cpu(r2->alloc.ar_startblock));
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct xfs_btree_ops xfs_bnobt_ops = {
|
2008-10-30 13:55:34 +08:00
|
|
|
.rec_len = sizeof(xfs_alloc_rec_t),
|
|
|
|
.key_len = sizeof(xfs_alloc_key_t),
|
|
|
|
|
2008-10-30 13:53:59 +08:00
|
|
|
.dup_cursor = xfs_allocbt_dup_cursor,
|
2008-10-30 13:57:16 +08:00
|
|
|
.set_root = xfs_allocbt_set_root,
|
2008-10-30 13:57:03 +08:00
|
|
|
.alloc_block = xfs_allocbt_alloc_block,
|
2008-10-30 13:57:51 +08:00
|
|
|
.free_block = xfs_allocbt_free_block,
|
2008-10-30 13:56:32 +08:00
|
|
|
.update_lastrec = xfs_allocbt_update_lastrec,
|
2008-10-30 13:58:01 +08:00
|
|
|
.get_minrecs = xfs_allocbt_get_minrecs,
|
2008-10-30 13:55:23 +08:00
|
|
|
.get_maxrecs = xfs_allocbt_get_maxrecs,
|
2008-10-30 13:56:09 +08:00
|
|
|
.init_key_from_rec = xfs_allocbt_init_key_from_rec,
|
2017-03-29 05:56:34 +08:00
|
|
|
.init_high_key_from_rec = xfs_bnobt_init_high_key_from_rec,
|
2008-10-30 13:57:40 +08:00
|
|
|
.init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
|
2008-10-30 13:56:09 +08:00
|
|
|
.init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
|
2017-03-29 05:56:34 +08:00
|
|
|
.key_diff = xfs_bnobt_key_diff,
|
2019-02-08 02:45:47 +08:00
|
|
|
.buf_ops = &xfs_bnobt_buf_ops,
|
2017-03-29 05:56:34 +08:00
|
|
|
.diff_two_keys = xfs_bnobt_diff_two_keys,
|
|
|
|
.keys_inorder = xfs_bnobt_keys_inorder,
|
|
|
|
.recs_inorder = xfs_bnobt_recs_inorder,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct xfs_btree_ops xfs_cntbt_ops = {
|
|
|
|
.rec_len = sizeof(xfs_alloc_rec_t),
|
|
|
|
.key_len = sizeof(xfs_alloc_key_t),
|
|
|
|
|
|
|
|
.dup_cursor = xfs_allocbt_dup_cursor,
|
|
|
|
.set_root = xfs_allocbt_set_root,
|
|
|
|
.alloc_block = xfs_allocbt_alloc_block,
|
|
|
|
.free_block = xfs_allocbt_free_block,
|
|
|
|
.update_lastrec = xfs_allocbt_update_lastrec,
|
|
|
|
.get_minrecs = xfs_allocbt_get_minrecs,
|
|
|
|
.get_maxrecs = xfs_allocbt_get_maxrecs,
|
|
|
|
.init_key_from_rec = xfs_allocbt_init_key_from_rec,
|
|
|
|
.init_high_key_from_rec = xfs_cntbt_init_high_key_from_rec,
|
|
|
|
.init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
|
|
|
|
.init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
|
|
|
|
.key_diff = xfs_cntbt_key_diff,
|
2019-02-08 02:45:47 +08:00
|
|
|
.buf_ops = &xfs_cntbt_buf_ops,
|
2017-03-29 05:56:34 +08:00
|
|
|
.diff_two_keys = xfs_cntbt_diff_two_keys,
|
|
|
|
.keys_inorder = xfs_cntbt_keys_inorder,
|
|
|
|
.recs_inorder = xfs_cntbt_recs_inorder,
|
2008-10-30 13:53:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a new allocation btree cursor.
|
|
|
|
*/
|
|
|
|
struct xfs_btree_cur * /* new alloc btree cursor */
|
|
|
|
xfs_allocbt_init_cursor(
|
|
|
|
struct xfs_mount *mp, /* file system mount point */
|
|
|
|
struct xfs_trans *tp, /* transaction pointer */
|
|
|
|
struct xfs_buf *agbp, /* buffer for agf structure */
|
|
|
|
xfs_agnumber_t agno, /* allocation group number */
|
|
|
|
xfs_btnum_t btnum) /* btree identifier */
|
|
|
|
{
|
|
|
|
struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
|
|
|
|
struct xfs_btree_cur *cur;
|
|
|
|
|
|
|
|
ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
|
|
|
|
|
2016-12-09 13:49:54 +08:00
|
|
|
cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
|
2008-10-30 13:53:59 +08:00
|
|
|
|
|
|
|
cur->bc_tp = tp;
|
|
|
|
cur->bc_mp = mp;
|
|
|
|
cur->bc_btnum = btnum;
|
|
|
|
cur->bc_blocklog = mp->m_sb.sb_blocklog;
|
2011-07-08 20:34:18 +08:00
|
|
|
|
|
|
|
if (btnum == XFS_BTNUM_CNT) {
|
2017-03-29 05:56:34 +08:00
|
|
|
cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_abtc_2);
|
|
|
|
cur->bc_ops = &xfs_cntbt_ops;
|
2011-07-08 20:34:18 +08:00
|
|
|
cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]);
|
2008-10-30 13:56:32 +08:00
|
|
|
cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
|
2011-07-08 20:34:18 +08:00
|
|
|
} else {
|
2017-03-29 05:56:34 +08:00
|
|
|
cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_abtb_2);
|
|
|
|
cur->bc_ops = &xfs_bnobt_ops;
|
2011-07-08 20:34:18 +08:00
|
|
|
cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]);
|
|
|
|
}
|
2008-10-30 13:53:59 +08:00
|
|
|
|
|
|
|
cur->bc_private.a.agbp = agbp;
|
|
|
|
cur->bc_private.a.agno = agno;
|
|
|
|
|
2013-04-22 03:53:46 +08:00
|
|
|
if (xfs_sb_version_hascrc(&mp->m_sb))
|
|
|
|
cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
|
|
|
|
|
2008-10-30 13:53:59 +08:00
|
|
|
return cur;
|
|
|
|
}
|
2008-10-30 14:11:19 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate number of records in an alloc btree block.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
xfs_allocbt_maxrecs(
|
|
|
|
struct xfs_mount *mp,
|
|
|
|
int blocklen,
|
|
|
|
int leaf)
|
|
|
|
{
|
2008-10-30 14:14:34 +08:00
|
|
|
blocklen -= XFS_ALLOC_BLOCK_LEN(mp);
|
2008-10-30 14:11:19 +08:00
|
|
|
|
|
|
|
if (leaf)
|
|
|
|
return blocklen / sizeof(xfs_alloc_rec_t);
|
|
|
|
return blocklen / (sizeof(xfs_alloc_key_t) + sizeof(xfs_alloc_ptr_t));
|
|
|
|
}
|
2018-05-10 01:02:01 +08:00
|
|
|
|
|
|
|
/* Calculate the freespace btree size for some records. */
|
|
|
|
xfs_extlen_t
|
|
|
|
xfs_allocbt_calc_size(
|
|
|
|
struct xfs_mount *mp,
|
|
|
|
unsigned long long len)
|
|
|
|
{
|
|
|
|
return xfs_btree_calc_size(mp->m_alloc_mnr, len);
|
|
|
|
}
|