]>
Commit | Line | Data |
---|---|---|
37b3b4d6 | 1 | // SPDX-License-Identifier: GPL-2.0 |
2bd0ea18 | 2 | /* |
da23017d NS |
3 | * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
2bd0ea18 | 5 | */ |
9c799827 | 6 | #include "libxfs_priv.h" |
b626fb59 DC |
7 | #include "xfs_fs.h" |
8 | #include "xfs_shared.h" | |
9 | #include "xfs_format.h" | |
10 | #include "xfs_log_format.h" | |
11 | #include "xfs_trans_resv.h" | |
12 | #include "xfs_sb.h" | |
13 | #include "xfs_mount.h" | |
14 | #include "xfs_btree.h" | |
15 | #include "xfs_alloc_btree.h" | |
16 | #include "xfs_alloc.h" | |
17 | #include "xfs_trace.h" | |
18 | #include "xfs_cksum.h" | |
19 | #include "xfs_trans.h" | |
20 | ||
2bd0ea18 | 21 | |
b194c7d8 BN |
22 | STATIC struct xfs_btree_cur * |
23 | xfs_allocbt_dup_cursor( | |
24 | struct xfs_btree_cur *cur) | |
2bd0ea18 | 25 | { |
b194c7d8 BN |
26 | return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp, |
27 | cur->bc_private.a.agbp, cur->bc_private.a.agno, | |
28 | cur->bc_btnum); | |
2bd0ea18 NS |
29 | } |
30 | ||
2bd0ea18 | 31 | STATIC void |
b194c7d8 BN |
32 | xfs_allocbt_set_root( |
33 | struct xfs_btree_cur *cur, | |
34 | union xfs_btree_ptr *ptr, | |
35 | int inc) | |
2bd0ea18 | 36 | { |
b194c7d8 BN |
37 | struct xfs_buf *agbp = cur->bc_private.a.agbp; |
38 | struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp); | |
39 | xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno); | |
40 | int btnum = cur->bc_btnum; | |
56b2de80 | 41 | struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno); |
2bd0ea18 | 42 | |
b194c7d8 | 43 | ASSERT(ptr->s != 0); |
2bd0ea18 | 44 | |
b194c7d8 BN |
45 | agf->agf_roots[btnum] = ptr->s; |
46 | be32_add_cpu(&agf->agf_levels[btnum], inc); | |
56b2de80 DC |
47 | pag->pagf_levels[btnum] += inc; |
48 | xfs_perag_put(pag); | |
2bd0ea18 | 49 | |
b194c7d8 | 50 | xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS); |
2bd0ea18 NS |
51 | } |
52 | ||
b194c7d8 BN |
53 | STATIC int |
54 | xfs_allocbt_alloc_block( | |
55 | struct xfs_btree_cur *cur, | |
56 | union xfs_btree_ptr *start, | |
57 | union xfs_btree_ptr *new, | |
b194c7d8 | 58 | int *stat) |
2bd0ea18 | 59 | { |
b194c7d8 BN |
60 | int error; |
61 | xfs_agblock_t bno; | |
2bd0ea18 | 62 | |
b194c7d8 BN |
63 | /* Allocate the new block from the freelist. If we can't, give up. */ |
64 | error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp, | |
65 | &bno, 1); | |
97b3ffd0 | 66 | if (error) |
2bd0ea18 | 67 | return error; |
b194c7d8 BN |
68 | |
69 | if (bno == NULLAGBLOCK) { | |
2bd0ea18 NS |
70 | *stat = 0; |
71 | return 0; | |
72 | } | |
2bd0ea18 | 73 | |
a2ceac1f DC |
74 | xfs_extent_busy_reuse(cur->bc_mp, cur->bc_private.a.agno, bno, 1, false); |
75 | ||
b194c7d8 BN |
76 | xfs_trans_agbtree_delta(cur->bc_tp, 1); |
77 | new->s = cpu_to_be32(bno); | |
2bd0ea18 | 78 | |
2bd0ea18 NS |
79 | *stat = 1; |
80 | return 0; | |
2bd0ea18 NS |
81 | } |
82 | ||
b194c7d8 BN |
83 | STATIC int |
84 | xfs_allocbt_free_block( | |
85 | struct xfs_btree_cur *cur, | |
86 | struct xfs_buf *bp) | |
2bd0ea18 | 87 | { |
b194c7d8 | 88 | struct xfs_buf *agbp = cur->bc_private.a.agbp; |
ff105f75 | 89 | struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp); |
b194c7d8 BN |
90 | xfs_agblock_t bno; |
91 | int error; | |
2bd0ea18 | 92 | |
56b2de80 | 93 | bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp)); |
b194c7d8 | 94 | error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1); |
5e656dbb | 95 | if (error) |
2bd0ea18 | 96 | return error; |
2bd0ea18 | 97 | |
a2ceac1f DC |
98 | xfs_extent_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1, |
99 | XFS_EXTENT_BUSY_SKIP_DISCARD); | |
b194c7d8 | 100 | xfs_trans_agbtree_delta(cur->bc_tp, -1); |
2bd0ea18 NS |
101 | return 0; |
102 | } | |
103 | ||
104 | /* | |
b194c7d8 | 105 | * Update the longest extent in the AGF |
2bd0ea18 | 106 | */ |
b194c7d8 BN |
107 | STATIC void |
108 | xfs_allocbt_update_lastrec( | |
109 | struct xfs_btree_cur *cur, | |
110 | struct xfs_btree_block *block, | |
111 | union xfs_btree_rec *rec, | |
112 | int ptr, | |
113 | int reason) | |
2bd0ea18 | 114 | { |
b194c7d8 BN |
115 | struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp); |
116 | xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno); | |
56b2de80 | 117 | struct xfs_perag *pag; |
b194c7d8 BN |
118 | __be32 len; |
119 | int numrecs; | |
2bd0ea18 | 120 | |
b194c7d8 BN |
121 | ASSERT(cur->bc_btnum == XFS_BTNUM_CNT); |
122 | ||
123 | switch (reason) { | |
124 | case LASTREC_UPDATE: | |
125 | /* | |
126 | * If this is the last leaf block and it's the last record, | |
127 | * then update the size of the longest extent in the AG. | |
128 | */ | |
129 | if (ptr != xfs_btree_get_numrecs(block)) | |
130 | return; | |
131 | len = rec->alloc.ar_blockcount; | |
132 | break; | |
133 | case LASTREC_INSREC: | |
134 | if (be32_to_cpu(rec->alloc.ar_blockcount) <= | |
135 | be32_to_cpu(agf->agf_longest)) | |
136 | return; | |
137 | len = rec->alloc.ar_blockcount; | |
138 | break; | |
139 | case LASTREC_DELREC: | |
140 | numrecs = xfs_btree_get_numrecs(block); | |
141 | if (ptr <= numrecs) | |
142 | return; | |
143 | ASSERT(ptr == numrecs + 1); | |
2bd0ea18 | 144 | |
b194c7d8 BN |
145 | if (numrecs) { |
146 | xfs_alloc_rec_t *rrp; | |
147 | ||
b3563c19 | 148 | rrp = XFS_ALLOC_REC_ADDR(cur->bc_mp, block, numrecs); |
b194c7d8 BN |
149 | len = rrp->ar_blockcount; |
150 | } else { | |
151 | len = 0; | |
152 | } | |
153 | ||
154 | break; | |
155 | default: | |
156 | ASSERT(0); | |
157 | return; | |
2bd0ea18 | 158 | } |
b194c7d8 BN |
159 | |
160 | agf->agf_longest = len; | |
56b2de80 DC |
161 | pag = xfs_perag_get(cur->bc_mp, seqno); |
162 | pag->pagf_longest = be32_to_cpu(len); | |
163 | xfs_perag_put(pag); | |
b194c7d8 | 164 | xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST); |
2bd0ea18 NS |
165 | } |
166 | ||
b194c7d8 BN |
167 | STATIC int |
168 | xfs_allocbt_get_minrecs( | |
169 | struct xfs_btree_cur *cur, | |
170 | int level) | |
171 | { | |
172 | return cur->bc_mp->m_alloc_mnr[level != 0]; | |
173 | } | |
2bd0ea18 | 174 | |
b194c7d8 BN |
175 | STATIC int |
176 | xfs_allocbt_get_maxrecs( | |
177 | struct xfs_btree_cur *cur, | |
178 | int level) | |
2bd0ea18 | 179 | { |
b194c7d8 BN |
180 | return cur->bc_mp->m_alloc_mxr[level != 0]; |
181 | } | |
2bd0ea18 | 182 | |
b194c7d8 BN |
183 | STATIC void |
184 | xfs_allocbt_init_key_from_rec( | |
185 | union xfs_btree_key *key, | |
186 | union xfs_btree_rec *rec) | |
187 | { | |
b194c7d8 BN |
188 | key->alloc.ar_startblock = rec->alloc.ar_startblock; |
189 | key->alloc.ar_blockcount = rec->alloc.ar_blockcount; | |
2bd0ea18 NS |
190 | } |
191 | ||
46a1586d DW |
192 | STATIC void |
193 | xfs_bnobt_init_high_key_from_rec( | |
194 | union xfs_btree_key *key, | |
195 | union xfs_btree_rec *rec) | |
196 | { | |
197 | __u32 x; | |
198 | ||
199 | x = be32_to_cpu(rec->alloc.ar_startblock); | |
200 | x += be32_to_cpu(rec->alloc.ar_blockcount) - 1; | |
201 | key->alloc.ar_startblock = cpu_to_be32(x); | |
202 | key->alloc.ar_blockcount = 0; | |
203 | } | |
204 | ||
205 | STATIC void | |
206 | xfs_cntbt_init_high_key_from_rec( | |
207 | union xfs_btree_key *key, | |
208 | union xfs_btree_rec *rec) | |
209 | { | |
210 | key->alloc.ar_blockcount = rec->alloc.ar_blockcount; | |
211 | key->alloc.ar_startblock = 0; | |
212 | } | |
213 | ||
b194c7d8 BN |
214 | STATIC void |
215 | xfs_allocbt_init_rec_from_cur( | |
216 | struct xfs_btree_cur *cur, | |
217 | union xfs_btree_rec *rec) | |
2bd0ea18 | 218 | { |
b194c7d8 BN |
219 | rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock); |
220 | rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount); | |
221 | } | |
222 | ||
223 | STATIC void | |
224 | xfs_allocbt_init_ptr_from_cur( | |
225 | struct xfs_btree_cur *cur, | |
226 | union xfs_btree_ptr *ptr) | |
227 | { | |
228 | struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp); | |
2bd0ea18 | 229 | |
b194c7d8 | 230 | ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno)); |
b194c7d8 BN |
231 | |
232 | ptr->s = agf->agf_roots[cur->bc_btnum]; | |
233 | } | |
234 | ||
4a492e72 | 235 | STATIC int64_t |
46a1586d | 236 | xfs_bnobt_key_diff( |
b194c7d8 BN |
237 | struct xfs_btree_cur *cur, |
238 | union xfs_btree_key *key) | |
239 | { | |
240 | xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a; | |
241 | xfs_alloc_key_t *kp = &key->alloc; | |
b194c7d8 | 242 | |
4a492e72 | 243 | return (int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock; |
46a1586d DW |
244 | } |
245 | ||
4a492e72 | 246 | STATIC int64_t |
46a1586d DW |
247 | xfs_cntbt_key_diff( |
248 | struct xfs_btree_cur *cur, | |
249 | union xfs_btree_key *key) | |
250 | { | |
251 | xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a; | |
252 | xfs_alloc_key_t *kp = &key->alloc; | |
4a492e72 | 253 | int64_t diff; |
b194c7d8 | 254 | |
4a492e72 | 255 | diff = (int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount; |
b194c7d8 BN |
256 | if (diff) |
257 | return diff; | |
258 | ||
4a492e72 | 259 | return (int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock; |
2bd0ea18 NS |
260 | } |
261 | ||
4a492e72 | 262 | STATIC int64_t |
46a1586d DW |
263 | xfs_bnobt_diff_two_keys( |
264 | struct xfs_btree_cur *cur, | |
265 | union xfs_btree_key *k1, | |
266 | union xfs_btree_key *k2) | |
267 | { | |
4a492e72 | 268 | return (int64_t)be32_to_cpu(k1->alloc.ar_startblock) - |
46a1586d DW |
269 | be32_to_cpu(k2->alloc.ar_startblock); |
270 | } | |
271 | ||
4a492e72 | 272 | STATIC int64_t |
46a1586d DW |
273 | xfs_cntbt_diff_two_keys( |
274 | struct xfs_btree_cur *cur, | |
275 | union xfs_btree_key *k1, | |
276 | union xfs_btree_key *k2) | |
277 | { | |
4a492e72 | 278 | int64_t diff; |
46a1586d DW |
279 | |
280 | diff = be32_to_cpu(k1->alloc.ar_blockcount) - | |
281 | be32_to_cpu(k2->alloc.ar_blockcount); | |
282 | if (diff) | |
283 | return diff; | |
284 | ||
285 | return be32_to_cpu(k1->alloc.ar_startblock) - | |
286 | be32_to_cpu(k2->alloc.ar_startblock); | |
287 | } | |
288 | ||
bc01119d | 289 | static xfs_failaddr_t |
a2ceac1f DC |
290 | xfs_allocbt_verify( |
291 | struct xfs_buf *bp) | |
292 | { | |
293 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
294 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); | |
295 | struct xfs_perag *pag = bp->b_pag; | |
bc01119d | 296 | xfs_failaddr_t fa; |
a2ceac1f | 297 | unsigned int level; |
a2ceac1f DC |
298 | |
299 | /* | |
300 | * magic number and level verification | |
301 | * | |
5dfa5cd2 DC |
302 | * During growfs operations, we can't verify the exact level or owner as |
303 | * the perag is not fully initialised and hence not attached to the | |
304 | * buffer. In this case, check against the maximum tree depth. | |
c0adfb03 DC |
305 | * |
306 | * Similarly, during log recovery we will have a perag structure | |
307 | * attached, but the agf information will not yet have been initialised | |
308 | * from the on disk AGF. Again, we can only check against maximum limits | |
309 | * in this case. | |
a2ceac1f DC |
310 | */ |
311 | level = be16_to_cpu(block->bb_level); | |
c0adfb03 DC |
312 | switch (block->bb_magic) { |
313 | case cpu_to_be32(XFS_ABTB_CRC_MAGIC): | |
bc01119d DW |
314 | fa = xfs_btree_sblock_v5hdr_verify(bp); |
315 | if (fa) | |
316 | return fa; | |
5dfa5cd2 | 317 | /* fall through */ |
c0adfb03 DC |
318 | case cpu_to_be32(XFS_ABTB_MAGIC): |
319 | if (pag && pag->pagf_init) { | |
5dfa5cd2 | 320 | if (level >= pag->pagf_levels[XFS_BTNUM_BNOi]) |
bc01119d | 321 | return __this_address; |
5dfa5cd2 | 322 | } else if (level >= mp->m_ag_maxlevels) |
bc01119d | 323 | return __this_address; |
a2ceac1f | 324 | break; |
c0adfb03 | 325 | case cpu_to_be32(XFS_ABTC_CRC_MAGIC): |
bc01119d DW |
326 | fa = xfs_btree_sblock_v5hdr_verify(bp); |
327 | if (fa) | |
328 | return fa; | |
5dfa5cd2 | 329 | /* fall through */ |
c0adfb03 DC |
330 | case cpu_to_be32(XFS_ABTC_MAGIC): |
331 | if (pag && pag->pagf_init) { | |
5dfa5cd2 | 332 | if (level >= pag->pagf_levels[XFS_BTNUM_CNTi]) |
bc01119d | 333 | return __this_address; |
5dfa5cd2 | 334 | } else if (level >= mp->m_ag_maxlevels) |
bc01119d | 335 | return __this_address; |
a2ceac1f DC |
336 | break; |
337 | default: | |
bc01119d | 338 | return __this_address; |
a2ceac1f DC |
339 | } |
340 | ||
dbca0167 | 341 | return xfs_btree_sblock_verify(bp, mp->m_alloc_mxr[level != 0]); |
a2ceac1f DC |
342 | } |
343 | ||
344 | static void | |
345 | xfs_allocbt_read_verify( | |
346 | struct xfs_buf *bp) | |
347 | { | |
1e697959 DW |
348 | xfs_failaddr_t fa; |
349 | ||
45922933 | 350 | if (!xfs_btree_sblock_verify_crc(bp)) |
1e697959 DW |
351 | xfs_verifier_error(bp, -EFSBADCRC, __this_address); |
352 | else { | |
353 | fa = xfs_allocbt_verify(bp); | |
354 | if (fa) | |
355 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); | |
356 | } | |
45922933 | 357 | |
7e6c95f1 | 358 | if (bp->b_error) |
45922933 | 359 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
a2ceac1f DC |
360 | } |
361 | ||
362 | static void | |
363 | xfs_allocbt_write_verify( | |
364 | struct xfs_buf *bp) | |
365 | { | |
1e697959 DW |
366 | xfs_failaddr_t fa; |
367 | ||
368 | fa = xfs_allocbt_verify(bp); | |
369 | if (fa) { | |
5dfa5cd2 | 370 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
1e697959 | 371 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); |
dbf564d1 | 372 | return; |
5dfa5cd2 DC |
373 | } |
374 | xfs_btree_sblock_calc_crc(bp); | |
375 | ||
a2ceac1f DC |
376 | } |
377 | ||
378 | const struct xfs_buf_ops xfs_allocbt_buf_ops = { | |
a3fac935 | 379 | .name = "xfs_allocbt", |
a2ceac1f DC |
380 | .verify_read = xfs_allocbt_read_verify, |
381 | .verify_write = xfs_allocbt_write_verify, | |
95d9582b | 382 | .verify_struct = xfs_allocbt_verify, |
a2ceac1f DC |
383 | }; |
384 | ||
385 | ||
b194c7d8 | 386 | STATIC int |
46a1586d | 387 | xfs_bnobt_keys_inorder( |
b194c7d8 BN |
388 | struct xfs_btree_cur *cur, |
389 | union xfs_btree_key *k1, | |
390 | union xfs_btree_key *k2) | |
391 | { | |
46a1586d DW |
392 | return be32_to_cpu(k1->alloc.ar_startblock) < |
393 | be32_to_cpu(k2->alloc.ar_startblock); | |
b194c7d8 | 394 | } |
2bd0ea18 | 395 | |
b194c7d8 | 396 | STATIC int |
46a1586d | 397 | xfs_bnobt_recs_inorder( |
b194c7d8 BN |
398 | struct xfs_btree_cur *cur, |
399 | union xfs_btree_rec *r1, | |
400 | union xfs_btree_rec *r2) | |
401 | { | |
46a1586d DW |
402 | return be32_to_cpu(r1->alloc.ar_startblock) + |
403 | be32_to_cpu(r1->alloc.ar_blockcount) <= | |
404 | be32_to_cpu(r2->alloc.ar_startblock); | |
405 | } | |
406 | ||
407 | STATIC int | |
408 | xfs_cntbt_keys_inorder( | |
409 | struct xfs_btree_cur *cur, | |
410 | union xfs_btree_key *k1, | |
411 | union xfs_btree_key *k2) | |
412 | { | |
413 | return be32_to_cpu(k1->alloc.ar_blockcount) < | |
414 | be32_to_cpu(k2->alloc.ar_blockcount) || | |
415 | (k1->alloc.ar_blockcount == k2->alloc.ar_blockcount && | |
416 | be32_to_cpu(k1->alloc.ar_startblock) < | |
417 | be32_to_cpu(k2->alloc.ar_startblock)); | |
2bd0ea18 NS |
418 | } |
419 | ||
46a1586d DW |
420 | STATIC int |
421 | xfs_cntbt_recs_inorder( | |
422 | struct xfs_btree_cur *cur, | |
423 | union xfs_btree_rec *r1, | |
424 | union xfs_btree_rec *r2) | |
425 | { | |
426 | return be32_to_cpu(r1->alloc.ar_blockcount) < | |
427 | be32_to_cpu(r2->alloc.ar_blockcount) || | |
428 | (r1->alloc.ar_blockcount == r2->alloc.ar_blockcount && | |
429 | be32_to_cpu(r1->alloc.ar_startblock) < | |
430 | be32_to_cpu(r2->alloc.ar_startblock)); | |
431 | } | |
46a1586d DW |
432 | |
433 | static const struct xfs_btree_ops xfs_bnobt_ops = { | |
b194c7d8 BN |
434 | .rec_len = sizeof(xfs_alloc_rec_t), |
435 | .key_len = sizeof(xfs_alloc_key_t), | |
436 | ||
437 | .dup_cursor = xfs_allocbt_dup_cursor, | |
438 | .set_root = xfs_allocbt_set_root, | |
b194c7d8 BN |
439 | .alloc_block = xfs_allocbt_alloc_block, |
440 | .free_block = xfs_allocbt_free_block, | |
441 | .update_lastrec = xfs_allocbt_update_lastrec, | |
442 | .get_minrecs = xfs_allocbt_get_minrecs, | |
443 | .get_maxrecs = xfs_allocbt_get_maxrecs, | |
444 | .init_key_from_rec = xfs_allocbt_init_key_from_rec, | |
46a1586d | 445 | .init_high_key_from_rec = xfs_bnobt_init_high_key_from_rec, |
b194c7d8 BN |
446 | .init_rec_from_cur = xfs_allocbt_init_rec_from_cur, |
447 | .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur, | |
46a1586d | 448 | .key_diff = xfs_bnobt_key_diff, |
a2ceac1f | 449 | .buf_ops = &xfs_allocbt_buf_ops, |
46a1586d | 450 | .diff_two_keys = xfs_bnobt_diff_two_keys, |
46a1586d DW |
451 | .keys_inorder = xfs_bnobt_keys_inorder, |
452 | .recs_inorder = xfs_bnobt_recs_inorder, | |
46a1586d DW |
453 | }; |
454 | ||
455 | static const struct xfs_btree_ops xfs_cntbt_ops = { | |
456 | .rec_len = sizeof(xfs_alloc_rec_t), | |
457 | .key_len = sizeof(xfs_alloc_key_t), | |
458 | ||
459 | .dup_cursor = xfs_allocbt_dup_cursor, | |
460 | .set_root = xfs_allocbt_set_root, | |
461 | .alloc_block = xfs_allocbt_alloc_block, | |
462 | .free_block = xfs_allocbt_free_block, | |
463 | .update_lastrec = xfs_allocbt_update_lastrec, | |
464 | .get_minrecs = xfs_allocbt_get_minrecs, | |
465 | .get_maxrecs = xfs_allocbt_get_maxrecs, | |
466 | .init_key_from_rec = xfs_allocbt_init_key_from_rec, | |
467 | .init_high_key_from_rec = xfs_cntbt_init_high_key_from_rec, | |
468 | .init_rec_from_cur = xfs_allocbt_init_rec_from_cur, | |
469 | .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur, | |
470 | .key_diff = xfs_cntbt_key_diff, | |
471 | .buf_ops = &xfs_allocbt_buf_ops, | |
472 | .diff_two_keys = xfs_cntbt_diff_two_keys, | |
46a1586d DW |
473 | .keys_inorder = xfs_cntbt_keys_inorder, |
474 | .recs_inorder = xfs_cntbt_recs_inorder, | |
b194c7d8 | 475 | }; |
2bd0ea18 NS |
476 | |
477 | /* | |
b194c7d8 | 478 | * Allocate a new allocation btree cursor. |
2bd0ea18 | 479 | */ |
b194c7d8 BN |
480 | struct xfs_btree_cur * /* new alloc btree cursor */ |
481 | xfs_allocbt_init_cursor( | |
482 | struct xfs_mount *mp, /* file system mount point */ | |
483 | struct xfs_trans *tp, /* transaction pointer */ | |
484 | struct xfs_buf *agbp, /* buffer for agf structure */ | |
485 | xfs_agnumber_t agno, /* allocation group number */ | |
486 | xfs_btnum_t btnum) /* btree identifier */ | |
2bd0ea18 | 487 | { |
b194c7d8 BN |
488 | struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp); |
489 | struct xfs_btree_cur *cur; | |
2bd0ea18 | 490 | |
b194c7d8 | 491 | ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT); |
2bd0ea18 | 492 | |
762989ef | 493 | cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS); |
2bd0ea18 | 494 | |
b194c7d8 BN |
495 | cur->bc_tp = tp; |
496 | cur->bc_mp = mp; | |
b194c7d8 BN |
497 | cur->bc_btnum = btnum; |
498 | cur->bc_blocklog = mp->m_sb.sb_blocklog; | |
a2ceac1f DC |
499 | |
500 | if (btnum == XFS_BTNUM_CNT) { | |
46a1586d DW |
501 | cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_abtc_2); |
502 | cur->bc_ops = &xfs_cntbt_ops; | |
a2ceac1f | 503 | cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]); |
b194c7d8 | 504 | cur->bc_flags = XFS_BTREE_LASTREC_UPDATE; |
a2ceac1f | 505 | } else { |
46a1586d DW |
506 | cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_abtb_2); |
507 | cur->bc_ops = &xfs_bnobt_ops; | |
a2ceac1f DC |
508 | cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]); |
509 | } | |
b194c7d8 BN |
510 | |
511 | cur->bc_private.a.agbp = agbp; | |
512 | cur->bc_private.a.agno = agno; | |
513 | ||
5dfa5cd2 DC |
514 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
515 | cur->bc_flags |= XFS_BTREE_CRC_BLOCKS; | |
516 | ||
b194c7d8 | 517 | return cur; |
2bd0ea18 | 518 | } |
b3563c19 BN |
519 | |
520 | /* | |
521 | * Calculate number of records in an alloc btree block. | |
522 | */ | |
523 | int | |
524 | xfs_allocbt_maxrecs( | |
525 | struct xfs_mount *mp, | |
526 | int blocklen, | |
527 | int leaf) | |
528 | { | |
529 | blocklen -= XFS_ALLOC_BLOCK_LEN(mp); | |
530 | ||
531 | if (leaf) | |
532 | return blocklen / sizeof(xfs_alloc_rec_t); | |
533 | return blocklen / (sizeof(xfs_alloc_key_t) + sizeof(xfs_alloc_ptr_t)); | |
534 | } | |
5f728383 DW |
535 | |
536 | /* Calculate the freespace btree size for some records. */ | |
537 | xfs_extlen_t | |
538 | xfs_allocbt_calc_size( | |
539 | struct xfs_mount *mp, | |
540 | unsigned long long len) | |
541 | { | |
542 | return xfs_btree_calc_size(mp->m_alloc_mnr, len); | |
543 | } |