]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_ialloc_btree.c
1dbb536089107995be448c621faaafaaa6187889
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_ialloc_btree.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6 #include "libxfs_priv.h"
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_bit.h"
13 #include "xfs_mount.h"
14 #include "xfs_btree.h"
15 #include "xfs_btree_staging.h"
16 #include "xfs_ialloc.h"
17 #include "xfs_ialloc_btree.h"
18 #include "xfs_alloc.h"
19 #include "xfs_trace.h"
20 #include "xfs_trans.h"
21 #include "xfs_rmap.h"
22 #include "xfs_ag.h"
23
24 static struct kmem_cache *xfs_inobt_cur_cache;
25
26 STATIC int
27 xfs_inobt_get_minrecs(
28 struct xfs_btree_cur *cur,
29 int level)
30 {
31 return M_IGEO(cur->bc_mp)->inobt_mnr[level != 0];
32 }
33
34 STATIC struct xfs_btree_cur *
35 xfs_inobt_dup_cursor(
36 struct xfs_btree_cur *cur)
37 {
38 return xfs_inobt_init_cursor(cur->bc_mp, cur->bc_tp,
39 cur->bc_ag.agbp, cur->bc_ag.pag, cur->bc_btnum);
40 }
41
42 STATIC void
43 xfs_inobt_set_root(
44 struct xfs_btree_cur *cur,
45 const union xfs_btree_ptr *nptr,
46 int inc) /* level change */
47 {
48 struct xfs_buf *agbp = cur->bc_ag.agbp;
49 struct xfs_agi *agi = agbp->b_addr;
50
51 agi->agi_root = nptr->s;
52 be32_add_cpu(&agi->agi_level, inc);
53 xfs_ialloc_log_agi(cur->bc_tp, agbp, XFS_AGI_ROOT | XFS_AGI_LEVEL);
54 }
55
56 STATIC void
57 xfs_finobt_set_root(
58 struct xfs_btree_cur *cur,
59 const union xfs_btree_ptr *nptr,
60 int inc) /* level change */
61 {
62 struct xfs_buf *agbp = cur->bc_ag.agbp;
63 struct xfs_agi *agi = agbp->b_addr;
64
65 agi->agi_free_root = nptr->s;
66 be32_add_cpu(&agi->agi_free_level, inc);
67 xfs_ialloc_log_agi(cur->bc_tp, agbp,
68 XFS_AGI_FREE_ROOT | XFS_AGI_FREE_LEVEL);
69 }
70
71 /* Update the inode btree block counter for this btree. */
72 static inline void
73 xfs_inobt_mod_blockcount(
74 struct xfs_btree_cur *cur,
75 int howmuch)
76 {
77 struct xfs_buf *agbp = cur->bc_ag.agbp;
78 struct xfs_agi *agi = agbp->b_addr;
79
80 if (!xfs_has_inobtcounts(cur->bc_mp))
81 return;
82
83 if (cur->bc_btnum == XFS_BTNUM_FINO)
84 be32_add_cpu(&agi->agi_fblocks, howmuch);
85 else if (cur->bc_btnum == XFS_BTNUM_INO)
86 be32_add_cpu(&agi->agi_iblocks, howmuch);
87 xfs_ialloc_log_agi(cur->bc_tp, agbp, XFS_AGI_IBLOCKS);
88 }
89
90 STATIC int
91 __xfs_inobt_alloc_block(
92 struct xfs_btree_cur *cur,
93 const union xfs_btree_ptr *start,
94 union xfs_btree_ptr *new,
95 int *stat,
96 enum xfs_ag_resv_type resv)
97 {
98 xfs_alloc_arg_t args; /* block allocation args */
99 int error; /* error return value */
100 xfs_agblock_t sbno = be32_to_cpu(start->s);
101
102 memset(&args, 0, sizeof(args));
103 args.tp = cur->bc_tp;
104 args.mp = cur->bc_mp;
105 args.oinfo = XFS_RMAP_OINFO_INOBT;
106 args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_ag.pag->pag_agno, sbno);
107 args.minlen = 1;
108 args.maxlen = 1;
109 args.prod = 1;
110 args.type = XFS_ALLOCTYPE_NEAR_BNO;
111 args.resv = resv;
112
113 error = xfs_alloc_vextent(&args);
114 if (error)
115 return error;
116
117 if (args.fsbno == NULLFSBLOCK) {
118 *stat = 0;
119 return 0;
120 }
121 ASSERT(args.len == 1);
122
123 new->s = cpu_to_be32(XFS_FSB_TO_AGBNO(args.mp, args.fsbno));
124 *stat = 1;
125 xfs_inobt_mod_blockcount(cur, 1);
126 return 0;
127 }
128
129 STATIC int
130 xfs_inobt_alloc_block(
131 struct xfs_btree_cur *cur,
132 const union xfs_btree_ptr *start,
133 union xfs_btree_ptr *new,
134 int *stat)
135 {
136 return __xfs_inobt_alloc_block(cur, start, new, stat, XFS_AG_RESV_NONE);
137 }
138
139 STATIC int
140 xfs_finobt_alloc_block(
141 struct xfs_btree_cur *cur,
142 const union xfs_btree_ptr *start,
143 union xfs_btree_ptr *new,
144 int *stat)
145 {
146 if (cur->bc_mp->m_finobt_nores)
147 return xfs_inobt_alloc_block(cur, start, new, stat);
148 return __xfs_inobt_alloc_block(cur, start, new, stat,
149 XFS_AG_RESV_METADATA);
150 }
151
152 STATIC int
153 __xfs_inobt_free_block(
154 struct xfs_btree_cur *cur,
155 struct xfs_buf *bp,
156 enum xfs_ag_resv_type resv)
157 {
158 xfs_inobt_mod_blockcount(cur, -1);
159 return xfs_free_extent(cur->bc_tp,
160 XFS_DADDR_TO_FSB(cur->bc_mp, xfs_buf_daddr(bp)), 1,
161 &XFS_RMAP_OINFO_INOBT, resv);
162 }
163
164 STATIC int
165 xfs_inobt_free_block(
166 struct xfs_btree_cur *cur,
167 struct xfs_buf *bp)
168 {
169 return __xfs_inobt_free_block(cur, bp, XFS_AG_RESV_NONE);
170 }
171
172 STATIC int
173 xfs_finobt_free_block(
174 struct xfs_btree_cur *cur,
175 struct xfs_buf *bp)
176 {
177 if (cur->bc_mp->m_finobt_nores)
178 return xfs_inobt_free_block(cur, bp);
179 return __xfs_inobt_free_block(cur, bp, XFS_AG_RESV_METADATA);
180 }
181
182 STATIC int
183 xfs_inobt_get_maxrecs(
184 struct xfs_btree_cur *cur,
185 int level)
186 {
187 return M_IGEO(cur->bc_mp)->inobt_mxr[level != 0];
188 }
189
190 STATIC void
191 xfs_inobt_init_key_from_rec(
192 union xfs_btree_key *key,
193 const union xfs_btree_rec *rec)
194 {
195 key->inobt.ir_startino = rec->inobt.ir_startino;
196 }
197
198 STATIC void
199 xfs_inobt_init_high_key_from_rec(
200 union xfs_btree_key *key,
201 const union xfs_btree_rec *rec)
202 {
203 __u32 x;
204
205 x = be32_to_cpu(rec->inobt.ir_startino);
206 x += XFS_INODES_PER_CHUNK - 1;
207 key->inobt.ir_startino = cpu_to_be32(x);
208 }
209
210 STATIC void
211 xfs_inobt_init_rec_from_cur(
212 struct xfs_btree_cur *cur,
213 union xfs_btree_rec *rec)
214 {
215 rec->inobt.ir_startino = cpu_to_be32(cur->bc_rec.i.ir_startino);
216 if (xfs_has_sparseinodes(cur->bc_mp)) {
217 rec->inobt.ir_u.sp.ir_holemask =
218 cpu_to_be16(cur->bc_rec.i.ir_holemask);
219 rec->inobt.ir_u.sp.ir_count = cur->bc_rec.i.ir_count;
220 rec->inobt.ir_u.sp.ir_freecount = cur->bc_rec.i.ir_freecount;
221 } else {
222 /* ir_holemask/ir_count not supported on-disk */
223 rec->inobt.ir_u.f.ir_freecount =
224 cpu_to_be32(cur->bc_rec.i.ir_freecount);
225 }
226 rec->inobt.ir_free = cpu_to_be64(cur->bc_rec.i.ir_free);
227 }
228
229 /*
230 * initial value of ptr for lookup
231 */
232 STATIC void
233 xfs_inobt_init_ptr_from_cur(
234 struct xfs_btree_cur *cur,
235 union xfs_btree_ptr *ptr)
236 {
237 struct xfs_agi *agi = cur->bc_ag.agbp->b_addr;
238
239 ASSERT(cur->bc_ag.pag->pag_agno == be32_to_cpu(agi->agi_seqno));
240
241 ptr->s = agi->agi_root;
242 }
243
244 STATIC void
245 xfs_finobt_init_ptr_from_cur(
246 struct xfs_btree_cur *cur,
247 union xfs_btree_ptr *ptr)
248 {
249 struct xfs_agi *agi = cur->bc_ag.agbp->b_addr;
250
251 ASSERT(cur->bc_ag.pag->pag_agno == be32_to_cpu(agi->agi_seqno));
252 ptr->s = agi->agi_free_root;
253 }
254
255 STATIC int64_t
256 xfs_inobt_key_diff(
257 struct xfs_btree_cur *cur,
258 const union xfs_btree_key *key)
259 {
260 return (int64_t)be32_to_cpu(key->inobt.ir_startino) -
261 cur->bc_rec.i.ir_startino;
262 }
263
264 STATIC int64_t
265 xfs_inobt_diff_two_keys(
266 struct xfs_btree_cur *cur,
267 const union xfs_btree_key *k1,
268 const union xfs_btree_key *k2)
269 {
270 return (int64_t)be32_to_cpu(k1->inobt.ir_startino) -
271 be32_to_cpu(k2->inobt.ir_startino);
272 }
273
274 static xfs_failaddr_t
275 xfs_inobt_verify(
276 struct xfs_buf *bp)
277 {
278 struct xfs_mount *mp = bp->b_mount;
279 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
280 xfs_failaddr_t fa;
281 unsigned int level;
282
283 if (!xfs_verify_magic(bp, block->bb_magic))
284 return __this_address;
285
286 /*
287 * During growfs operations, we can't verify the exact owner as the
288 * perag is not fully initialised and hence not attached to the buffer.
289 *
290 * Similarly, during log recovery we will have a perag structure
291 * attached, but the agi information will not yet have been initialised
292 * from the on disk AGI. We don't currently use any of this information,
293 * but beware of the landmine (i.e. need to check pag->pagi_init) if we
294 * ever do.
295 */
296 if (xfs_has_crc(mp)) {
297 fa = xfs_btree_sblock_v5hdr_verify(bp);
298 if (fa)
299 return fa;
300 }
301
302 /* level verification */
303 level = be16_to_cpu(block->bb_level);
304 if (level >= M_IGEO(mp)->inobt_maxlevels)
305 return __this_address;
306
307 return xfs_btree_sblock_verify(bp,
308 M_IGEO(mp)->inobt_mxr[level != 0]);
309 }
310
311 static void
312 xfs_inobt_read_verify(
313 struct xfs_buf *bp)
314 {
315 xfs_failaddr_t fa;
316
317 if (!xfs_btree_sblock_verify_crc(bp))
318 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
319 else {
320 fa = xfs_inobt_verify(bp);
321 if (fa)
322 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
323 }
324
325 if (bp->b_error)
326 trace_xfs_btree_corrupt(bp, _RET_IP_);
327 }
328
329 static void
330 xfs_inobt_write_verify(
331 struct xfs_buf *bp)
332 {
333 xfs_failaddr_t fa;
334
335 fa = xfs_inobt_verify(bp);
336 if (fa) {
337 trace_xfs_btree_corrupt(bp, _RET_IP_);
338 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
339 return;
340 }
341 xfs_btree_sblock_calc_crc(bp);
342
343 }
344
345 const struct xfs_buf_ops xfs_inobt_buf_ops = {
346 .name = "xfs_inobt",
347 .magic = { cpu_to_be32(XFS_IBT_MAGIC), cpu_to_be32(XFS_IBT_CRC_MAGIC) },
348 .verify_read = xfs_inobt_read_verify,
349 .verify_write = xfs_inobt_write_verify,
350 .verify_struct = xfs_inobt_verify,
351 };
352
353 const struct xfs_buf_ops xfs_finobt_buf_ops = {
354 .name = "xfs_finobt",
355 .magic = { cpu_to_be32(XFS_FIBT_MAGIC),
356 cpu_to_be32(XFS_FIBT_CRC_MAGIC) },
357 .verify_read = xfs_inobt_read_verify,
358 .verify_write = xfs_inobt_write_verify,
359 .verify_struct = xfs_inobt_verify,
360 };
361
362 STATIC int
363 xfs_inobt_keys_inorder(
364 struct xfs_btree_cur *cur,
365 const union xfs_btree_key *k1,
366 const union xfs_btree_key *k2)
367 {
368 return be32_to_cpu(k1->inobt.ir_startino) <
369 be32_to_cpu(k2->inobt.ir_startino);
370 }
371
372 STATIC int
373 xfs_inobt_recs_inorder(
374 struct xfs_btree_cur *cur,
375 const union xfs_btree_rec *r1,
376 const union xfs_btree_rec *r2)
377 {
378 return be32_to_cpu(r1->inobt.ir_startino) + XFS_INODES_PER_CHUNK <=
379 be32_to_cpu(r2->inobt.ir_startino);
380 }
381
382 static const struct xfs_btree_ops xfs_inobt_ops = {
383 .rec_len = sizeof(xfs_inobt_rec_t),
384 .key_len = sizeof(xfs_inobt_key_t),
385
386 .dup_cursor = xfs_inobt_dup_cursor,
387 .set_root = xfs_inobt_set_root,
388 .alloc_block = xfs_inobt_alloc_block,
389 .free_block = xfs_inobt_free_block,
390 .get_minrecs = xfs_inobt_get_minrecs,
391 .get_maxrecs = xfs_inobt_get_maxrecs,
392 .init_key_from_rec = xfs_inobt_init_key_from_rec,
393 .init_high_key_from_rec = xfs_inobt_init_high_key_from_rec,
394 .init_rec_from_cur = xfs_inobt_init_rec_from_cur,
395 .init_ptr_from_cur = xfs_inobt_init_ptr_from_cur,
396 .key_diff = xfs_inobt_key_diff,
397 .buf_ops = &xfs_inobt_buf_ops,
398 .diff_two_keys = xfs_inobt_diff_two_keys,
399 .keys_inorder = xfs_inobt_keys_inorder,
400 .recs_inorder = xfs_inobt_recs_inorder,
401 };
402
403 static const struct xfs_btree_ops xfs_finobt_ops = {
404 .rec_len = sizeof(xfs_inobt_rec_t),
405 .key_len = sizeof(xfs_inobt_key_t),
406
407 .dup_cursor = xfs_inobt_dup_cursor,
408 .set_root = xfs_finobt_set_root,
409 .alloc_block = xfs_finobt_alloc_block,
410 .free_block = xfs_finobt_free_block,
411 .get_minrecs = xfs_inobt_get_minrecs,
412 .get_maxrecs = xfs_inobt_get_maxrecs,
413 .init_key_from_rec = xfs_inobt_init_key_from_rec,
414 .init_high_key_from_rec = xfs_inobt_init_high_key_from_rec,
415 .init_rec_from_cur = xfs_inobt_init_rec_from_cur,
416 .init_ptr_from_cur = xfs_finobt_init_ptr_from_cur,
417 .key_diff = xfs_inobt_key_diff,
418 .buf_ops = &xfs_finobt_buf_ops,
419 .diff_two_keys = xfs_inobt_diff_two_keys,
420 .keys_inorder = xfs_inobt_keys_inorder,
421 .recs_inorder = xfs_inobt_recs_inorder,
422 };
423
424 /*
425 * Initialize a new inode btree cursor.
426 */
427 static struct xfs_btree_cur *
428 xfs_inobt_init_common(
429 struct xfs_mount *mp, /* file system mount point */
430 struct xfs_trans *tp, /* transaction pointer */
431 struct xfs_perag *pag,
432 xfs_btnum_t btnum) /* ialloc or free ino btree */
433 {
434 struct xfs_btree_cur *cur;
435
436 cur = xfs_btree_alloc_cursor(mp, tp, btnum,
437 M_IGEO(mp)->inobt_maxlevels, xfs_inobt_cur_cache);
438 if (btnum == XFS_BTNUM_INO) {
439 cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_ibt_2);
440 cur->bc_ops = &xfs_inobt_ops;
441 } else {
442 cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_fibt_2);
443 cur->bc_ops = &xfs_finobt_ops;
444 }
445
446 if (xfs_has_crc(mp))
447 cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
448
449 /* take a reference for the cursor */
450 atomic_inc(&pag->pag_ref);
451 cur->bc_ag.pag = pag;
452 return cur;
453 }
454
455 /* Create an inode btree cursor. */
456 struct xfs_btree_cur *
457 xfs_inobt_init_cursor(
458 struct xfs_mount *mp,
459 struct xfs_trans *tp,
460 struct xfs_buf *agbp,
461 struct xfs_perag *pag,
462 xfs_btnum_t btnum)
463 {
464 struct xfs_btree_cur *cur;
465 struct xfs_agi *agi = agbp->b_addr;
466
467 cur = xfs_inobt_init_common(mp, tp, pag, btnum);
468 if (btnum == XFS_BTNUM_INO)
469 cur->bc_nlevels = be32_to_cpu(agi->agi_level);
470 else
471 cur->bc_nlevels = be32_to_cpu(agi->agi_free_level);
472 cur->bc_ag.agbp = agbp;
473 return cur;
474 }
475
476 /* Create an inode btree cursor with a fake root for staging. */
477 struct xfs_btree_cur *
478 xfs_inobt_stage_cursor(
479 struct xfs_mount *mp,
480 struct xbtree_afakeroot *afake,
481 struct xfs_perag *pag,
482 xfs_btnum_t btnum)
483 {
484 struct xfs_btree_cur *cur;
485
486 cur = xfs_inobt_init_common(mp, NULL, pag, btnum);
487 xfs_btree_stage_afakeroot(cur, afake);
488 return cur;
489 }
490
491 /*
492 * Install a new inobt btree root. Caller is responsible for invalidating
493 * and freeing the old btree blocks.
494 */
495 void
496 xfs_inobt_commit_staged_btree(
497 struct xfs_btree_cur *cur,
498 struct xfs_trans *tp,
499 struct xfs_buf *agbp)
500 {
501 struct xfs_agi *agi = agbp->b_addr;
502 struct xbtree_afakeroot *afake = cur->bc_ag.afake;
503 int fields;
504
505 ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
506
507 if (cur->bc_btnum == XFS_BTNUM_INO) {
508 fields = XFS_AGI_ROOT | XFS_AGI_LEVEL;
509 agi->agi_root = cpu_to_be32(afake->af_root);
510 agi->agi_level = cpu_to_be32(afake->af_levels);
511 if (xfs_has_inobtcounts(cur->bc_mp)) {
512 agi->agi_iblocks = cpu_to_be32(afake->af_blocks);
513 fields |= XFS_AGI_IBLOCKS;
514 }
515 xfs_ialloc_log_agi(tp, agbp, fields);
516 xfs_btree_commit_afakeroot(cur, tp, agbp, &xfs_inobt_ops);
517 } else {
518 fields = XFS_AGI_FREE_ROOT | XFS_AGI_FREE_LEVEL;
519 agi->agi_free_root = cpu_to_be32(afake->af_root);
520 agi->agi_free_level = cpu_to_be32(afake->af_levels);
521 if (xfs_has_inobtcounts(cur->bc_mp)) {
522 agi->agi_fblocks = cpu_to_be32(afake->af_blocks);
523 fields |= XFS_AGI_IBLOCKS;
524 }
525 xfs_ialloc_log_agi(tp, agbp, fields);
526 xfs_btree_commit_afakeroot(cur, tp, agbp, &xfs_finobt_ops);
527 }
528 }
529
530 /* Calculate number of records in an inode btree block. */
531 static inline unsigned int
532 xfs_inobt_block_maxrecs(
533 unsigned int blocklen,
534 bool leaf)
535 {
536 if (leaf)
537 return blocklen / sizeof(xfs_inobt_rec_t);
538 return blocklen / (sizeof(xfs_inobt_key_t) + sizeof(xfs_inobt_ptr_t));
539 }
540
541 /*
542 * Calculate number of records in an inobt btree block.
543 */
544 int
545 xfs_inobt_maxrecs(
546 struct xfs_mount *mp,
547 int blocklen,
548 int leaf)
549 {
550 blocklen -= XFS_INOBT_BLOCK_LEN(mp);
551 return xfs_inobt_block_maxrecs(blocklen, leaf);
552 }
553
554 /*
555 * Maximum number of inode btree records per AG. Pretend that we can fill an
556 * entire AG completely full of inodes except for the AG headers.
557 */
558 #define XFS_MAX_INODE_RECORDS \
559 ((XFS_MAX_AG_BYTES - (4 * BBSIZE)) / XFS_DINODE_MIN_SIZE) / \
560 XFS_INODES_PER_CHUNK
561
562 /* Compute the max possible height for the inode btree. */
563 static inline unsigned int
564 xfs_inobt_maxlevels_ondisk(void)
565 {
566 unsigned int minrecs[2];
567 unsigned int blocklen;
568
569 blocklen = min(XFS_MIN_BLOCKSIZE - XFS_BTREE_SBLOCK_LEN,
570 XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN);
571
572 minrecs[0] = xfs_inobt_block_maxrecs(blocklen, true) / 2;
573 minrecs[1] = xfs_inobt_block_maxrecs(blocklen, false) / 2;
574
575 return xfs_btree_compute_maxlevels(minrecs, XFS_MAX_INODE_RECORDS);
576 }
577
578 /* Compute the max possible height for the free inode btree. */
579 static inline unsigned int
580 xfs_finobt_maxlevels_ondisk(void)
581 {
582 unsigned int minrecs[2];
583 unsigned int blocklen;
584
585 blocklen = XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN;
586
587 minrecs[0] = xfs_inobt_block_maxrecs(blocklen, true) / 2;
588 minrecs[1] = xfs_inobt_block_maxrecs(blocklen, false) / 2;
589
590 return xfs_btree_compute_maxlevels(minrecs, XFS_MAX_INODE_RECORDS);
591 }
592
593 /* Compute the max possible height for either inode btree. */
594 unsigned int
595 xfs_iallocbt_maxlevels_ondisk(void)
596 {
597 return max(xfs_inobt_maxlevels_ondisk(),
598 xfs_finobt_maxlevels_ondisk());
599 }
600
601 /*
602 * Convert the inode record holemask to an inode allocation bitmap. The inode
603 * allocation bitmap is inode granularity and specifies whether an inode is
604 * physically allocated on disk (not whether the inode is considered allocated
605 * or free by the fs).
606 *
607 * A bit value of 1 means the inode is allocated, a value of 0 means it is free.
608 */
609 uint64_t
610 xfs_inobt_irec_to_allocmask(
611 struct xfs_inobt_rec_incore *rec)
612 {
613 uint64_t bitmap = 0;
614 uint64_t inodespbit;
615 int nextbit;
616 uint allocbitmap;
617
618 /*
619 * The holemask has 16-bits for a 64 inode record. Therefore each
620 * holemask bit represents multiple inodes. Create a mask of bits to set
621 * in the allocmask for each holemask bit.
622 */
623 inodespbit = (1 << XFS_INODES_PER_HOLEMASK_BIT) - 1;
624
625 /*
626 * Allocated inodes are represented by 0 bits in holemask. Invert the 0
627 * bits to 1 and convert to a uint so we can use xfs_next_bit(). Mask
628 * anything beyond the 16 holemask bits since this casts to a larger
629 * type.
630 */
631 allocbitmap = ~rec->ir_holemask & ((1 << XFS_INOBT_HOLEMASK_BITS) - 1);
632
633 /*
634 * allocbitmap is the inverted holemask so every set bit represents
635 * allocated inodes. To expand from 16-bit holemask granularity to
636 * 64-bit (e.g., bit-per-inode), set inodespbit bits in the target
637 * bitmap for every holemask bit.
638 */
639 nextbit = xfs_next_bit(&allocbitmap, 1, 0);
640 while (nextbit != -1) {
641 ASSERT(nextbit < (sizeof(rec->ir_holemask) * NBBY));
642
643 bitmap |= (inodespbit <<
644 (nextbit * XFS_INODES_PER_HOLEMASK_BIT));
645
646 nextbit = xfs_next_bit(&allocbitmap, 1, nextbit + 1);
647 }
648
649 return bitmap;
650 }
651
652 #if defined(DEBUG) || defined(XFS_WARN)
653 /*
654 * Verify that an in-core inode record has a valid inode count.
655 */
656 int
657 xfs_inobt_rec_check_count(
658 struct xfs_mount *mp,
659 struct xfs_inobt_rec_incore *rec)
660 {
661 int inocount = 0;
662 int nextbit = 0;
663 uint64_t allocbmap;
664 int wordsz;
665
666 wordsz = sizeof(allocbmap) / sizeof(unsigned int);
667 allocbmap = xfs_inobt_irec_to_allocmask(rec);
668
669 nextbit = xfs_next_bit((uint *) &allocbmap, wordsz, nextbit);
670 while (nextbit != -1) {
671 inocount++;
672 nextbit = xfs_next_bit((uint *) &allocbmap, wordsz,
673 nextbit + 1);
674 }
675
676 if (inocount != rec->ir_count)
677 return -EFSCORRUPTED;
678
679 return 0;
680 }
681 #endif /* DEBUG */
682
683 static xfs_extlen_t
684 xfs_inobt_max_size(
685 struct xfs_mount *mp,
686 xfs_agnumber_t agno)
687 {
688 xfs_agblock_t agblocks = xfs_ag_block_count(mp, agno);
689
690 /* Bail out if we're uninitialized, which can happen in mkfs. */
691 if (M_IGEO(mp)->inobt_mxr[0] == 0)
692 return 0;
693
694 /*
695 * The log is permanently allocated, so the space it occupies will
696 * never be available for the kinds of things that would require btree
697 * expansion. We therefore can pretend the space isn't there.
698 */
699 if (mp->m_sb.sb_logstart &&
700 XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart) == agno)
701 agblocks -= mp->m_sb.sb_logblocks;
702
703 return xfs_btree_calc_size(M_IGEO(mp)->inobt_mnr,
704 (uint64_t)agblocks * mp->m_sb.sb_inopblock /
705 XFS_INODES_PER_CHUNK);
706 }
707
708 /* Read AGI and create inobt cursor. */
709 int
710 xfs_inobt_cur(
711 struct xfs_mount *mp,
712 struct xfs_trans *tp,
713 struct xfs_perag *pag,
714 xfs_btnum_t which,
715 struct xfs_btree_cur **curpp,
716 struct xfs_buf **agi_bpp)
717 {
718 struct xfs_btree_cur *cur;
719 int error;
720
721 ASSERT(*agi_bpp == NULL);
722 ASSERT(*curpp == NULL);
723
724 error = xfs_ialloc_read_agi(mp, tp, pag->pag_agno, agi_bpp);
725 if (error)
726 return error;
727
728 cur = xfs_inobt_init_cursor(mp, tp, *agi_bpp, pag, which);
729 *curpp = cur;
730 return 0;
731 }
732
733 static int
734 xfs_inobt_count_blocks(
735 struct xfs_mount *mp,
736 struct xfs_trans *tp,
737 struct xfs_perag *pag,
738 xfs_btnum_t btnum,
739 xfs_extlen_t *tree_blocks)
740 {
741 struct xfs_buf *agbp = NULL;
742 struct xfs_btree_cur *cur = NULL;
743 int error;
744
745 error = xfs_inobt_cur(mp, tp, pag, btnum, &cur, &agbp);
746 if (error)
747 return error;
748
749 error = xfs_btree_count_blocks(cur, tree_blocks);
750 xfs_btree_del_cursor(cur, error);
751 xfs_trans_brelse(tp, agbp);
752
753 return error;
754 }
755
756 /* Read finobt block count from AGI header. */
757 static int
758 xfs_finobt_read_blocks(
759 struct xfs_mount *mp,
760 struct xfs_trans *tp,
761 struct xfs_perag *pag,
762 xfs_extlen_t *tree_blocks)
763 {
764 struct xfs_buf *agbp;
765 struct xfs_agi *agi;
766 int error;
767
768 error = xfs_ialloc_read_agi(mp, tp, pag->pag_agno, &agbp);
769 if (error)
770 return error;
771
772 agi = agbp->b_addr;
773 *tree_blocks = be32_to_cpu(agi->agi_fblocks);
774 xfs_trans_brelse(tp, agbp);
775 return 0;
776 }
777
778 /*
779 * Figure out how many blocks to reserve and how many are used by this btree.
780 */
781 int
782 xfs_finobt_calc_reserves(
783 struct xfs_mount *mp,
784 struct xfs_trans *tp,
785 struct xfs_perag *pag,
786 xfs_extlen_t *ask,
787 xfs_extlen_t *used)
788 {
789 xfs_extlen_t tree_len = 0;
790 int error;
791
792 if (!xfs_has_finobt(mp))
793 return 0;
794
795 if (xfs_has_inobtcounts(mp))
796 error = xfs_finobt_read_blocks(mp, tp, pag, &tree_len);
797 else
798 error = xfs_inobt_count_blocks(mp, tp, pag, XFS_BTNUM_FINO,
799 &tree_len);
800 if (error)
801 return error;
802
803 *ask += xfs_inobt_max_size(mp, pag->pag_agno);
804 *used += tree_len;
805 return 0;
806 }
807
808 /* Calculate the inobt btree size for some records. */
809 xfs_extlen_t
810 xfs_iallocbt_calc_size(
811 struct xfs_mount *mp,
812 unsigned long long len)
813 {
814 return xfs_btree_calc_size(M_IGEO(mp)->inobt_mnr, len);
815 }
816
817 int __init
818 xfs_inobt_init_cur_cache(void)
819 {
820 xfs_inobt_cur_cache = kmem_cache_create("xfs_inobt_cur",
821 xfs_btree_cur_sizeof(xfs_inobt_maxlevels_ondisk()),
822 0, 0, NULL);
823
824 if (!xfs_inobt_cur_cache)
825 return -ENOMEM;
826 return 0;
827 }
828
829 void
830 xfs_inobt_destroy_cur_cache(void)
831 {
832 kmem_cache_destroy(xfs_inobt_cur_cache);
833 xfs_inobt_cur_cache = NULL;
834 }