blob: 359cf0c3a6b434489d4b8067b9c98a9ca93d743d [file] [log] [blame]
Darrick J. Wong1946b912016-10-03 09:11:18 -07001/*
2 * Copyright (C) 2016 Oracle. All Rights Reserved.
3 *
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20#include "xfs.h"
21#include "xfs_fs.h"
22#include "xfs_shared.h"
23#include "xfs_format.h"
24#include "xfs_log_format.h"
25#include "xfs_trans_resv.h"
26#include "xfs_sb.h"
27#include "xfs_mount.h"
28#include "xfs_btree.h"
29#include "xfs_bmap.h"
30#include "xfs_refcount_btree.h"
31#include "xfs_alloc.h"
32#include "xfs_error.h"
33#include "xfs_trace.h"
34#include "xfs_cksum.h"
35#include "xfs_trans.h"
36#include "xfs_bit.h"
37
38static struct xfs_btree_cur *
39xfs_refcountbt_dup_cursor(
40 struct xfs_btree_cur *cur)
41{
42 return xfs_refcountbt_init_cursor(cur->bc_mp, cur->bc_tp,
43 cur->bc_private.a.agbp, cur->bc_private.a.agno,
44 cur->bc_private.a.dfops);
45}
46
47STATIC bool
48xfs_refcountbt_verify(
49 struct xfs_buf *bp)
50{
51 struct xfs_mount *mp = bp->b_target->bt_mount;
52 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
53 struct xfs_perag *pag = bp->b_pag;
54 unsigned int level;
55
56 if (block->bb_magic != cpu_to_be32(XFS_REFC_CRC_MAGIC))
57 return false;
58
59 if (!xfs_sb_version_hasreflink(&mp->m_sb))
60 return false;
61 if (!xfs_btree_sblock_v5hdr_verify(bp))
62 return false;
63
64 level = be16_to_cpu(block->bb_level);
65 if (pag && pag->pagf_init) {
66 if (level >= pag->pagf_refcount_level)
67 return false;
68 } else if (level >= mp->m_refc_maxlevels)
69 return false;
70
71 return xfs_btree_sblock_verify(bp, mp->m_refc_mxr[level != 0]);
72}
73
74STATIC void
75xfs_refcountbt_read_verify(
76 struct xfs_buf *bp)
77{
78 if (!xfs_btree_sblock_verify_crc(bp))
79 xfs_buf_ioerror(bp, -EFSBADCRC);
80 else if (!xfs_refcountbt_verify(bp))
81 xfs_buf_ioerror(bp, -EFSCORRUPTED);
82
83 if (bp->b_error) {
84 trace_xfs_btree_corrupt(bp, _RET_IP_);
85 xfs_verifier_error(bp);
86 }
87}
88
89STATIC void
90xfs_refcountbt_write_verify(
91 struct xfs_buf *bp)
92{
93 if (!xfs_refcountbt_verify(bp)) {
94 trace_xfs_btree_corrupt(bp, _RET_IP_);
95 xfs_buf_ioerror(bp, -EFSCORRUPTED);
96 xfs_verifier_error(bp);
97 return;
98 }
99 xfs_btree_sblock_calc_crc(bp);
100
101}
102
103const struct xfs_buf_ops xfs_refcountbt_buf_ops = {
104 .name = "xfs_refcountbt",
105 .verify_read = xfs_refcountbt_read_verify,
106 .verify_write = xfs_refcountbt_write_verify,
107};
108
109static const struct xfs_btree_ops xfs_refcountbt_ops = {
110 .rec_len = sizeof(struct xfs_refcount_rec),
111 .key_len = sizeof(struct xfs_refcount_key),
112
113 .dup_cursor = xfs_refcountbt_dup_cursor,
114 .buf_ops = &xfs_refcountbt_buf_ops,
115};
116
117/*
118 * Allocate a new refcount btree cursor.
119 */
120struct xfs_btree_cur *
121xfs_refcountbt_init_cursor(
122 struct xfs_mount *mp,
123 struct xfs_trans *tp,
124 struct xfs_buf *agbp,
125 xfs_agnumber_t agno,
126 struct xfs_defer_ops *dfops)
127{
128 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
129 struct xfs_btree_cur *cur;
130
131 ASSERT(agno != NULLAGNUMBER);
132 ASSERT(agno < mp->m_sb.sb_agcount);
133 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
134
135 cur->bc_tp = tp;
136 cur->bc_mp = mp;
137 cur->bc_btnum = XFS_BTNUM_REFC;
138 cur->bc_blocklog = mp->m_sb.sb_blocklog;
139 cur->bc_ops = &xfs_refcountbt_ops;
140
141 cur->bc_nlevels = be32_to_cpu(agf->agf_refcount_level);
142
143 cur->bc_private.a.agbp = agbp;
144 cur->bc_private.a.agno = agno;
145 cur->bc_private.a.dfops = dfops;
146 cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
147
148 cur->bc_private.a.priv.refc.nr_ops = 0;
149 cur->bc_private.a.priv.refc.shape_changes = 0;
150
151 return cur;
152}
153
154/*
155 * Calculate the number of records in a refcount btree block.
156 */
157int
158xfs_refcountbt_maxrecs(
159 struct xfs_mount *mp,
160 int blocklen,
161 bool leaf)
162{
163 blocklen -= XFS_REFCOUNT_BLOCK_LEN;
164
165 if (leaf)
166 return blocklen / sizeof(struct xfs_refcount_rec);
167 return blocklen / (sizeof(struct xfs_refcount_key) +
168 sizeof(xfs_refcount_ptr_t));
169}
170
171/* Compute the maximum height of a refcount btree. */
172void
173xfs_refcountbt_compute_maxlevels(
174 struct xfs_mount *mp)
175{
176 mp->m_refc_maxlevels = xfs_btree_compute_maxlevels(mp,
177 mp->m_refc_mnr, mp->m_sb.sb_agblocks);
178}