]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_ialloc_btree.c
xfs: implement online get/set fs label
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_ialloc_btree.c
CommitLineData
2bd0ea18 1/*
da23017d
NS
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5000d01d 4 *
da23017d
NS
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
2bd0ea18 7 * published by the Free Software Foundation.
5000d01d 8 *
da23017d
NS
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.
5000d01d 13 *
da23017d
NS
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
2bd0ea18 17 */
9c799827 18#include "libxfs_priv.h"
b626fb59
DC
19#include "xfs_fs.h"
20#include "xfs_shared.h"
21#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
24#include "xfs_bit.h"
25#include "xfs_mount.h"
26#include "xfs_inode.h"
27#include "xfs_btree.h"
28#include "xfs_ialloc.h"
29#include "xfs_ialloc_btree.h"
30#include "xfs_alloc.h"
31#include "xfs_trace.h"
32#include "xfs_cksum.h"
33#include "xfs_trans.h"
85aec44f 34#include "xfs_rmap.h"
b626fb59 35
2bd0ea18 36
b194c7d8
BN
37STATIC int
38xfs_inobt_get_minrecs(
39 struct xfs_btree_cur *cur,
40 int level)
2bd0ea18 41{
b194c7d8 42 return cur->bc_mp->m_inobt_mnr[level != 0];
2bd0ea18
NS
43}
44
b194c7d8
BN
45STATIC struct xfs_btree_cur *
46xfs_inobt_dup_cursor(
47 struct xfs_btree_cur *cur)
2bd0ea18 48{
b194c7d8 49 return xfs_inobt_init_cursor(cur->bc_mp, cur->bc_tp,
70eb7337
BF
50 cur->bc_private.a.agbp, cur->bc_private.a.agno,
51 cur->bc_btnum);
2bd0ea18
NS
52}
53
2bd0ea18 54STATIC void
b194c7d8
BN
55xfs_inobt_set_root(
56 struct xfs_btree_cur *cur,
57 union xfs_btree_ptr *nptr,
58 int inc) /* level change */
2bd0ea18 59{
b194c7d8
BN
60 struct xfs_buf *agbp = cur->bc_private.a.agbp;
61 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
2bd0ea18 62
b194c7d8
BN
63 agi->agi_root = nptr->s;
64 be32_add_cpu(&agi->agi_level, inc);
65 xfs_ialloc_log_agi(cur->bc_tp, agbp, XFS_AGI_ROOT | XFS_AGI_LEVEL);
2bd0ea18
NS
66}
67
c0a4c227
BF
68STATIC void
69xfs_finobt_set_root(
70 struct xfs_btree_cur *cur,
71 union xfs_btree_ptr *nptr,
72 int inc) /* level change */
73{
74 struct xfs_buf *agbp = cur->bc_private.a.agbp;
75 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
76
77 agi->agi_free_root = nptr->s;
78 be32_add_cpu(&agi->agi_free_level, inc);
79 xfs_ialloc_log_agi(cur->bc_tp, agbp,
80 XFS_AGI_FREE_ROOT | XFS_AGI_FREE_LEVEL);
81}
82
b194c7d8 83STATIC int
74e502ac 84__xfs_inobt_alloc_block(
b194c7d8
BN
85 struct xfs_btree_cur *cur,
86 union xfs_btree_ptr *start,
87 union xfs_btree_ptr *new,
74e502ac
CH
88 int *stat,
89 enum xfs_ag_resv_type resv)
2bd0ea18 90{
b194c7d8
BN
91 xfs_alloc_arg_t args; /* block allocation args */
92 int error; /* error return value */
93 xfs_agblock_t sbno = be32_to_cpu(start->s);
2bd0ea18 94
b194c7d8 95 memset(&args, 0, sizeof(args));
2bd0ea18
NS
96 args.tp = cur->bc_tp;
97 args.mp = cur->bc_mp;
85aec44f 98 xfs_rmap_ag_owner(&args.oinfo, XFS_RMAP_OWN_INOBT);
b194c7d8
BN
99 args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.a.agno, sbno);
100 args.minlen = 1;
101 args.maxlen = 1;
102 args.prod = 1;
2bd0ea18 103 args.type = XFS_ALLOCTYPE_NEAR_BNO;
74e502ac 104 args.resv = resv;
b194c7d8
BN
105
106 error = xfs_alloc_vextent(&args);
97b3ffd0 107 if (error)
2bd0ea18 108 return error;
97b3ffd0 109
2bd0ea18
NS
110 if (args.fsbno == NULLFSBLOCK) {
111 *stat = 0;
112 return 0;
113 }
114 ASSERT(args.len == 1);
b194c7d8
BN
115
116 new->s = cpu_to_be32(XFS_FSB_TO_AGBNO(args.mp, args.fsbno));
2bd0ea18
NS
117 *stat = 1;
118 return 0;
119}
120
74e502ac
CH
121STATIC int
122xfs_inobt_alloc_block(
123 struct xfs_btree_cur *cur,
124 union xfs_btree_ptr *start,
125 union xfs_btree_ptr *new,
126 int *stat)
127{
128 return __xfs_inobt_alloc_block(cur, start, new, stat, XFS_AG_RESV_NONE);
129}
130
131STATIC int
132xfs_finobt_alloc_block(
133 struct xfs_btree_cur *cur,
134 union xfs_btree_ptr *start,
135 union xfs_btree_ptr *new,
136 int *stat)
137{
8504509d
BF
138 if (cur->bc_mp->m_inotbt_nores)
139 return xfs_inobt_alloc_block(cur, start, new, stat);
74e502ac
CH
140 return __xfs_inobt_alloc_block(cur, start, new, stat,
141 XFS_AG_RESV_METADATA);
142}
143
b194c7d8 144STATIC int
8504509d 145__xfs_inobt_free_block(
b194c7d8 146 struct xfs_btree_cur *cur,
8504509d
BF
147 struct xfs_buf *bp,
148 enum xfs_ag_resv_type resv)
2bd0ea18 149{
85aec44f
DW
150 struct xfs_owner_info oinfo;
151
152 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INOBT);
08caf14f 153 return xfs_free_extent(cur->bc_tp,
85aec44f 154 XFS_DADDR_TO_FSB(cur->bc_mp, XFS_BUF_ADDR(bp)), 1,
8504509d
BF
155 &oinfo, resv);
156}
157
158STATIC int
159xfs_inobt_free_block(
160 struct xfs_btree_cur *cur,
161 struct xfs_buf *bp)
162{
163 return __xfs_inobt_free_block(cur, bp, XFS_AG_RESV_NONE);
164}
165
166STATIC int
167xfs_finobt_free_block(
168 struct xfs_btree_cur *cur,
169 struct xfs_buf *bp)
170{
171 if (cur->bc_mp->m_inotbt_nores)
172 return xfs_inobt_free_block(cur, bp);
173 return __xfs_inobt_free_block(cur, bp, XFS_AG_RESV_METADATA);
2bd0ea18
NS
174}
175
b194c7d8
BN
176STATIC int
177xfs_inobt_get_maxrecs(
178 struct xfs_btree_cur *cur,
179 int level)
2bd0ea18 180{
b194c7d8
BN
181 return cur->bc_mp->m_inobt_mxr[level != 0];
182}
2bd0ea18 183
b194c7d8
BN
184STATIC void
185xfs_inobt_init_key_from_rec(
186 union xfs_btree_key *key,
187 union xfs_btree_rec *rec)
188{
189 key->inobt.ir_startino = rec->inobt.ir_startino;
190}
2bd0ea18 191
4e388bc9
DW
192STATIC void
193xfs_inobt_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->inobt.ir_startino);
200 x += XFS_INODES_PER_CHUNK - 1;
201 key->inobt.ir_startino = cpu_to_be32(x);
202}
203
b194c7d8
BN
204STATIC void
205xfs_inobt_init_rec_from_cur(
206 struct xfs_btree_cur *cur,
207 union xfs_btree_rec *rec)
208{
209 rec->inobt.ir_startino = cpu_to_be32(cur->bc_rec.i.ir_startino);
11640e30
BF
210 if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) {
211 rec->inobt.ir_u.sp.ir_holemask =
212 cpu_to_be16(cur->bc_rec.i.ir_holemask);
213 rec->inobt.ir_u.sp.ir_count = cur->bc_rec.i.ir_count;
214 rec->inobt.ir_u.sp.ir_freecount = cur->bc_rec.i.ir_freecount;
215 } else {
216 /* ir_holemask/ir_count not supported on-disk */
217 rec->inobt.ir_u.f.ir_freecount =
218 cpu_to_be32(cur->bc_rec.i.ir_freecount);
219 }
b194c7d8 220 rec->inobt.ir_free = cpu_to_be64(cur->bc_rec.i.ir_free);
2bd0ea18
NS
221}
222
223/*
56b2de80 224 * initial value of ptr for lookup
2bd0ea18 225 */
b194c7d8
BN
226STATIC void
227xfs_inobt_init_ptr_from_cur(
228 struct xfs_btree_cur *cur,
229 union xfs_btree_ptr *ptr)
2bd0ea18 230{
b194c7d8 231 struct xfs_agi *agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp);
2bd0ea18 232
b194c7d8 233 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agi->agi_seqno));
2bd0ea18 234
b194c7d8 235 ptr->s = agi->agi_root;
2bd0ea18
NS
236}
237
c0a4c227
BF
238STATIC void
239xfs_finobt_init_ptr_from_cur(
240 struct xfs_btree_cur *cur,
241 union xfs_btree_ptr *ptr)
242{
243 struct xfs_agi *agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp);
244
245 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agi->agi_seqno));
246 ptr->s = agi->agi_free_root;
247}
248
4a492e72 249STATIC int64_t
b194c7d8
BN
250xfs_inobt_key_diff(
251 struct xfs_btree_cur *cur,
252 union xfs_btree_key *key)
253{
4a492e72 254 return (int64_t)be32_to_cpu(key->inobt.ir_startino) -
b194c7d8
BN
255 cur->bc_rec.i.ir_startino;
256}
2bd0ea18 257
4e388bc9
DW
258STATIC int64_t
259xfs_inobt_diff_two_keys(
260 struct xfs_btree_cur *cur,
261 union xfs_btree_key *k1,
262 union xfs_btree_key *k2)
263{
264 return (int64_t)be32_to_cpu(k1->inobt.ir_startino) -
265 be32_to_cpu(k2->inobt.ir_startino);
266}
267
bc01119d 268static xfs_failaddr_t
a2ceac1f
DC
269xfs_inobt_verify(
270 struct xfs_buf *bp)
271{
272 struct xfs_mount *mp = bp->b_target->bt_mount;
273 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
bc01119d 274 xfs_failaddr_t fa;
a2ceac1f 275 unsigned int level;
a2ceac1f 276
5dfa5cd2
DC
277 /*
278 * During growfs operations, we can't verify the exact owner as the
279 * perag is not fully initialised and hence not attached to the buffer.
c0adfb03
DC
280 *
281 * Similarly, during log recovery we will have a perag structure
282 * attached, but the agi information will not yet have been initialised
283 * from the on disk AGI. We don't currently use any of this information,
284 * but beware of the landmine (i.e. need to check pag->pagi_init) if we
285 * ever do.
5dfa5cd2 286 */
c0adfb03
DC
287 switch (block->bb_magic) {
288 case cpu_to_be32(XFS_IBT_CRC_MAGIC):
c0a4c227 289 case cpu_to_be32(XFS_FIBT_CRC_MAGIC):
bc01119d
DW
290 fa = xfs_btree_sblock_v5hdr_verify(bp);
291 if (fa)
292 return fa;
5dfa5cd2 293 /* fall through */
c0adfb03 294 case cpu_to_be32(XFS_IBT_MAGIC):
c0a4c227 295 case cpu_to_be32(XFS_FIBT_MAGIC):
5dfa5cd2
DC
296 break;
297 default:
bc01119d 298 return NULL;
5dfa5cd2 299 }
a2ceac1f 300
dbca0167 301 /* level verification */
5dfa5cd2
DC
302 level = be16_to_cpu(block->bb_level);
303 if (level >= mp->m_in_maxlevels)
bc01119d 304 return __this_address;
5dfa5cd2 305
dbca0167 306 return xfs_btree_sblock_verify(bp, mp->m_inobt_mxr[level != 0]);
a2ceac1f
DC
307}
308
309static void
310xfs_inobt_read_verify(
311 struct xfs_buf *bp)
312{
1e697959
DW
313 xfs_failaddr_t fa;
314
45922933 315 if (!xfs_btree_sblock_verify_crc(bp))
1e697959
DW
316 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
317 else {
318 fa = xfs_inobt_verify(bp);
319 if (fa)
320 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
321 }
45922933 322
7e6c95f1 323 if (bp->b_error)
45922933 324 trace_xfs_btree_corrupt(bp, _RET_IP_);
a2ceac1f
DC
325}
326
327static void
328xfs_inobt_write_verify(
329 struct xfs_buf *bp)
330{
1e697959
DW
331 xfs_failaddr_t fa;
332
333 fa = xfs_inobt_verify(bp);
334 if (fa) {
5dfa5cd2 335 trace_xfs_btree_corrupt(bp, _RET_IP_);
1e697959 336 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
dbf564d1 337 return;
5dfa5cd2
DC
338 }
339 xfs_btree_sblock_calc_crc(bp);
340
a2ceac1f
DC
341}
342
343const struct xfs_buf_ops xfs_inobt_buf_ops = {
a3fac935 344 .name = "xfs_inobt",
a2ceac1f
DC
345 .verify_read = xfs_inobt_read_verify,
346 .verify_write = xfs_inobt_write_verify,
95d9582b 347 .verify_struct = xfs_inobt_verify,
a2ceac1f
DC
348};
349
b194c7d8
BN
350STATIC int
351xfs_inobt_keys_inorder(
352 struct xfs_btree_cur *cur,
353 union xfs_btree_key *k1,
354 union xfs_btree_key *k2)
355{
356 return be32_to_cpu(k1->inobt.ir_startino) <
357 be32_to_cpu(k2->inobt.ir_startino);
2bd0ea18
NS
358}
359
b194c7d8
BN
360STATIC int
361xfs_inobt_recs_inorder(
362 struct xfs_btree_cur *cur,
363 union xfs_btree_rec *r1,
364 union xfs_btree_rec *r2)
2bd0ea18 365{
b194c7d8
BN
366 return be32_to_cpu(r1->inobt.ir_startino) + XFS_INODES_PER_CHUNK <=
367 be32_to_cpu(r2->inobt.ir_startino);
368}
2bd0ea18 369
b194c7d8
BN
370static const struct xfs_btree_ops xfs_inobt_ops = {
371 .rec_len = sizeof(xfs_inobt_rec_t),
372 .key_len = sizeof(xfs_inobt_key_t),
373
374 .dup_cursor = xfs_inobt_dup_cursor,
375 .set_root = xfs_inobt_set_root,
b194c7d8
BN
376 .alloc_block = xfs_inobt_alloc_block,
377 .free_block = xfs_inobt_free_block,
378 .get_minrecs = xfs_inobt_get_minrecs,
379 .get_maxrecs = xfs_inobt_get_maxrecs,
380 .init_key_from_rec = xfs_inobt_init_key_from_rec,
4e388bc9 381 .init_high_key_from_rec = xfs_inobt_init_high_key_from_rec,
b194c7d8
BN
382 .init_rec_from_cur = xfs_inobt_init_rec_from_cur,
383 .init_ptr_from_cur = xfs_inobt_init_ptr_from_cur,
384 .key_diff = xfs_inobt_key_diff,
a2ceac1f 385 .buf_ops = &xfs_inobt_buf_ops,
4e388bc9 386 .diff_two_keys = xfs_inobt_diff_two_keys,
b194c7d8
BN
387 .keys_inorder = xfs_inobt_keys_inorder,
388 .recs_inorder = xfs_inobt_recs_inorder,
b194c7d8 389};
2bd0ea18 390
c0a4c227
BF
391static const struct xfs_btree_ops xfs_finobt_ops = {
392 .rec_len = sizeof(xfs_inobt_rec_t),
393 .key_len = sizeof(xfs_inobt_key_t),
394
395 .dup_cursor = xfs_inobt_dup_cursor,
396 .set_root = xfs_finobt_set_root,
74e502ac 397 .alloc_block = xfs_finobt_alloc_block,
8504509d 398 .free_block = xfs_finobt_free_block,
c0a4c227
BF
399 .get_minrecs = xfs_inobt_get_minrecs,
400 .get_maxrecs = xfs_inobt_get_maxrecs,
401 .init_key_from_rec = xfs_inobt_init_key_from_rec,
4e388bc9 402 .init_high_key_from_rec = xfs_inobt_init_high_key_from_rec,
c0a4c227
BF
403 .init_rec_from_cur = xfs_inobt_init_rec_from_cur,
404 .init_ptr_from_cur = xfs_finobt_init_ptr_from_cur,
405 .key_diff = xfs_inobt_key_diff,
406 .buf_ops = &xfs_inobt_buf_ops,
4e388bc9 407 .diff_two_keys = xfs_inobt_diff_two_keys,
c0a4c227
BF
408 .keys_inorder = xfs_inobt_keys_inorder,
409 .recs_inorder = xfs_inobt_recs_inorder,
c0a4c227
BF
410};
411
2bd0ea18 412/*
b194c7d8 413 * Allocate a new inode btree cursor.
2bd0ea18 414 */
b194c7d8
BN
415struct xfs_btree_cur * /* new inode btree cursor */
416xfs_inobt_init_cursor(
417 struct xfs_mount *mp, /* file system mount point */
418 struct xfs_trans *tp, /* transaction pointer */
419 struct xfs_buf *agbp, /* buffer for agi structure */
70eb7337
BF
420 xfs_agnumber_t agno, /* allocation group number */
421 xfs_btnum_t btnum) /* ialloc or free ino btree */
2bd0ea18 422{
b194c7d8
BN
423 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
424 struct xfs_btree_cur *cur;
2bd0ea18 425
762989ef 426 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
2bd0ea18 427
b194c7d8
BN
428 cur->bc_tp = tp;
429 cur->bc_mp = mp;
70eb7337 430 cur->bc_btnum = btnum;
c0a4c227
BF
431 if (btnum == XFS_BTNUM_INO) {
432 cur->bc_nlevels = be32_to_cpu(agi->agi_level);
433 cur->bc_ops = &xfs_inobt_ops;
5d8acc46 434 cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_ibt_2);
c0a4c227
BF
435 } else {
436 cur->bc_nlevels = be32_to_cpu(agi->agi_free_level);
437 cur->bc_ops = &xfs_finobt_ops;
5d8acc46 438 cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_fibt_2);
c0a4c227
BF
439 }
440
b194c7d8
BN
441 cur->bc_blocklog = mp->m_sb.sb_blocklog;
442
5dfa5cd2
DC
443 if (xfs_sb_version_hascrc(&mp->m_sb))
444 cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
b194c7d8
BN
445
446 cur->bc_private.a.agbp = agbp;
447 cur->bc_private.a.agno = agno;
448
449 return cur;
2bd0ea18 450}
b3563c19
BN
451
452/*
453 * Calculate number of records in an inobt btree block.
454 */
455int
456xfs_inobt_maxrecs(
457 struct xfs_mount *mp,
458 int blocklen,
459 int leaf)
460{
461 blocklen -= XFS_INOBT_BLOCK_LEN(mp);
462
463 if (leaf)
464 return blocklen / sizeof(xfs_inobt_rec_t);
465 return blocklen / (sizeof(xfs_inobt_key_t) + sizeof(xfs_inobt_ptr_t));
466}
2cf87710
BF
467
468/*
469 * Convert the inode record holemask to an inode allocation bitmap. The inode
470 * allocation bitmap is inode granularity and specifies whether an inode is
471 * physically allocated on disk (not whether the inode is considered allocated
472 * or free by the fs).
473 *
474 * A bit value of 1 means the inode is allocated, a value of 0 means it is free.
475 */
476uint64_t
477xfs_inobt_irec_to_allocmask(
478 struct xfs_inobt_rec_incore *rec)
479{
480 uint64_t bitmap = 0;
481 uint64_t inodespbit;
482 int nextbit;
483 uint allocbitmap;
484
485 /*
486 * The holemask has 16-bits for a 64 inode record. Therefore each
487 * holemask bit represents multiple inodes. Create a mask of bits to set
488 * in the allocmask for each holemask bit.
489 */
490 inodespbit = (1 << XFS_INODES_PER_HOLEMASK_BIT) - 1;
491
492 /*
493 * Allocated inodes are represented by 0 bits in holemask. Invert the 0
494 * bits to 1 and convert to a uint so we can use xfs_next_bit(). Mask
495 * anything beyond the 16 holemask bits since this casts to a larger
496 * type.
497 */
498 allocbitmap = ~rec->ir_holemask & ((1 << XFS_INOBT_HOLEMASK_BITS) - 1);
499
500 /*
501 * allocbitmap is the inverted holemask so every set bit represents
502 * allocated inodes. To expand from 16-bit holemask granularity to
503 * 64-bit (e.g., bit-per-inode), set inodespbit bits in the target
504 * bitmap for every holemask bit.
505 */
506 nextbit = xfs_next_bit(&allocbitmap, 1, 0);
507 while (nextbit != -1) {
508 ASSERT(nextbit < (sizeof(rec->ir_holemask) * NBBY));
509
510 bitmap |= (inodespbit <<
511 (nextbit * XFS_INODES_PER_HOLEMASK_BIT));
512
513 nextbit = xfs_next_bit(&allocbitmap, 1, nextbit + 1);
514 }
515
516 return bitmap;
517}
6f4c54a4
BF
518
519#if defined(DEBUG) || defined(XFS_WARN)
520/*
521 * Verify that an in-core inode record has a valid inode count.
522 */
523int
524xfs_inobt_rec_check_count(
525 struct xfs_mount *mp,
526 struct xfs_inobt_rec_incore *rec)
527{
528 int inocount = 0;
529 int nextbit = 0;
530 uint64_t allocbmap;
531 int wordsz;
532
533 wordsz = sizeof(allocbmap) / sizeof(unsigned int);
534 allocbmap = xfs_inobt_irec_to_allocmask(rec);
535
536 nextbit = xfs_next_bit((uint *) &allocbmap, wordsz, nextbit);
537 while (nextbit != -1) {
538 inocount++;
539 nextbit = xfs_next_bit((uint *) &allocbmap, wordsz,
540 nextbit + 1);
541 }
542
543 if (inocount != rec->ir_count)
544 return -EFSCORRUPTED;
545
546 return 0;
547}
548#endif /* DEBUG */
74e502ac
CH
549
550static xfs_extlen_t
551xfs_inobt_max_size(
552 struct xfs_mount *mp)
553{
554 /* Bail out if we're uninitialized, which can happen in mkfs. */
555 if (mp->m_inobt_mxr[0] == 0)
556 return 0;
557
1421de38 558 return xfs_btree_calc_size(mp->m_inobt_mnr,
74e502ac
CH
559 (uint64_t)mp->m_sb.sb_agblocks * mp->m_sb.sb_inopblock /
560 XFS_INODES_PER_CHUNK);
561}
562
563static int
564xfs_inobt_count_blocks(
565 struct xfs_mount *mp,
566 xfs_agnumber_t agno,
567 xfs_btnum_t btnum,
568 xfs_extlen_t *tree_blocks)
569{
570 struct xfs_buf *agbp;
571 struct xfs_btree_cur *cur;
572 int error;
573
574 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
575 if (error)
576 return error;
577
578 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno, btnum);
579 error = xfs_btree_count_blocks(cur, tree_blocks);
580 xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
581 xfs_buf_relse(agbp);
582
583 return error;
584}
585
586/*
587 * Figure out how many blocks to reserve and how many are used by this btree.
588 */
589int
590xfs_finobt_calc_reserves(
591 struct xfs_mount *mp,
592 xfs_agnumber_t agno,
593 xfs_extlen_t *ask,
594 xfs_extlen_t *used)
595{
596 xfs_extlen_t tree_len = 0;
597 int error;
598
599 if (!xfs_sb_version_hasfinobt(&mp->m_sb))
600 return 0;
601
602 error = xfs_inobt_count_blocks(mp, agno, XFS_BTNUM_FINO, &tree_len);
603 if (error)
604 return error;
605
606 *ask += xfs_inobt_max_size(mp);
607 *used += tree_len;
608 return 0;
609}
5f728383
DW
610
611/* Calculate the inobt btree size for some records. */
612xfs_extlen_t
613xfs_iallocbt_calc_size(
614 struct xfs_mount *mp,
615 unsigned long long len)
616{
617 return xfs_btree_calc_size(mp->m_inobt_mnr, len);
618}