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