Linux Audio

Check our new training course

Loading...
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (c) 2000-2003,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_trans.h"
 16#include "xfs_alloc.h"
 17#include "xfs_btree.h"
 18#include "xfs_btree_staging.h"
 19#include "xfs_bmap_btree.h"
 20#include "xfs_bmap.h"
 21#include "xfs_error.h"
 22#include "xfs_quota.h"
 23#include "xfs_trace.h"
 24#include "xfs_rmap.h"
 25#include "xfs_ag.h"
 26
 27static struct kmem_cache	*xfs_bmbt_cur_cache;
 28
 29void
 30xfs_bmbt_init_block(
 31	struct xfs_inode		*ip,
 32	struct xfs_btree_block		*buf,
 33	struct xfs_buf			*bp,
 34	__u16				level,
 35	__u16				numrecs)
 36{
 37	if (bp)
 38		xfs_btree_init_buf(ip->i_mount, bp, &xfs_bmbt_ops, level,
 39				numrecs, ip->i_ino);
 40	else
 41		xfs_btree_init_block(ip->i_mount, buf, &xfs_bmbt_ops, level,
 42				numrecs, ip->i_ino);
 43}
 44
 45/*
 46 * Convert on-disk form of btree root to in-memory form.
 47 */
 48void
 49xfs_bmdr_to_bmbt(
 50	struct xfs_inode	*ip,
 51	xfs_bmdr_block_t	*dblock,
 52	int			dblocklen,
 53	struct xfs_btree_block	*rblock,
 54	int			rblocklen)
 55{
 56	struct xfs_mount	*mp = ip->i_mount;
 57	int			dmxr;
 58	xfs_bmbt_key_t		*fkp;
 59	__be64			*fpp;
 60	xfs_bmbt_key_t		*tkp;
 61	__be64			*tpp;
 62
 63	xfs_bmbt_init_block(ip, rblock, NULL, 0, 0);
 
 
 64	rblock->bb_level = dblock->bb_level;
 65	ASSERT(be16_to_cpu(rblock->bb_level) > 0);
 66	rblock->bb_numrecs = dblock->bb_numrecs;
 67	dmxr = xfs_bmdr_maxrecs(dblocklen, 0);
 68	fkp = XFS_BMDR_KEY_ADDR(dblock, 1);
 69	tkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1);
 70	fpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr);
 71	tpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
 72	dmxr = be16_to_cpu(dblock->bb_numrecs);
 73	memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
 74	memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
 75}
 76
 77void
 78xfs_bmbt_disk_get_all(
 79	const struct xfs_bmbt_rec *rec,
 80	struct xfs_bmbt_irec	*irec)
 81{
 82	uint64_t		l0 = get_unaligned_be64(&rec->l0);
 83	uint64_t		l1 = get_unaligned_be64(&rec->l1);
 84
 85	irec->br_startoff = (l0 & xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
 86	irec->br_startblock = ((l0 & xfs_mask64lo(9)) << 43) | (l1 >> 21);
 87	irec->br_blockcount = l1 & xfs_mask64lo(21);
 88	if (l0 >> (64 - BMBT_EXNTFLAG_BITLEN))
 89		irec->br_state = XFS_EXT_UNWRITTEN;
 90	else
 91		irec->br_state = XFS_EXT_NORM;
 92}
 93
 94/*
 95 * Extract the blockcount field from an on disk bmap extent record.
 96 */
 97xfs_filblks_t
 98xfs_bmbt_disk_get_blockcount(
 99	const struct xfs_bmbt_rec	*r)
100{
101	return (xfs_filblks_t)(be64_to_cpu(r->l1) & xfs_mask64lo(21));
102}
103
104/*
105 * Extract the startoff field from a disk format bmap extent record.
106 */
107xfs_fileoff_t
108xfs_bmbt_disk_get_startoff(
109	const struct xfs_bmbt_rec	*r)
110{
111	return ((xfs_fileoff_t)be64_to_cpu(r->l0) &
112		 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
113}
114
115/*
116 * Set all the fields in a bmap extent record from the uncompressed form.
117 */
118void
119xfs_bmbt_disk_set_all(
120	struct xfs_bmbt_rec	*r,
121	struct xfs_bmbt_irec	*s)
122{
123	int			extent_flag = (s->br_state != XFS_EXT_NORM);
124
125	ASSERT(s->br_state == XFS_EXT_NORM || s->br_state == XFS_EXT_UNWRITTEN);
126	ASSERT(!(s->br_startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)));
127	ASSERT(!(s->br_blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)));
128	ASSERT(!(s->br_startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)));
129
130	put_unaligned_be64(
131		((xfs_bmbt_rec_base_t)extent_flag << 63) |
132		 ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
133		 ((xfs_bmbt_rec_base_t)s->br_startblock >> 43), &r->l0);
134	put_unaligned_be64(
135		((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
136		 ((xfs_bmbt_rec_base_t)s->br_blockcount &
137		  (xfs_bmbt_rec_base_t)xfs_mask64lo(21)), &r->l1);
138}
139
140/*
141 * Convert in-memory form of btree root to on-disk form.
142 */
143void
144xfs_bmbt_to_bmdr(
145	struct xfs_mount	*mp,
146	struct xfs_btree_block	*rblock,
147	int			rblocklen,
148	xfs_bmdr_block_t	*dblock,
149	int			dblocklen)
150{
151	int			dmxr;
152	xfs_bmbt_key_t		*fkp;
153	__be64			*fpp;
154	xfs_bmbt_key_t		*tkp;
155	__be64			*tpp;
156
157	if (xfs_has_crc(mp)) {
158		ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_CRC_MAGIC));
159		ASSERT(uuid_equal(&rblock->bb_u.l.bb_uuid,
160		       &mp->m_sb.sb_meta_uuid));
161		ASSERT(rblock->bb_u.l.bb_blkno ==
162		       cpu_to_be64(XFS_BUF_DADDR_NULL));
163	} else
164		ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_MAGIC));
165	ASSERT(rblock->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK));
166	ASSERT(rblock->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK));
167	ASSERT(rblock->bb_level != 0);
168	dblock->bb_level = rblock->bb_level;
169	dblock->bb_numrecs = rblock->bb_numrecs;
170	dmxr = xfs_bmdr_maxrecs(dblocklen, 0);
171	fkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1);
172	tkp = XFS_BMDR_KEY_ADDR(dblock, 1);
173	fpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
174	tpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr);
175	dmxr = be16_to_cpu(dblock->bb_numrecs);
176	memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
177	memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
178}
179
180STATIC struct xfs_btree_cur *
181xfs_bmbt_dup_cursor(
182	struct xfs_btree_cur	*cur)
183{
184	struct xfs_btree_cur	*new;
185
186	new = xfs_bmbt_init_cursor(cur->bc_mp, cur->bc_tp,
187			cur->bc_ino.ip, cur->bc_ino.whichfork);
188	new->bc_flags |= (cur->bc_flags &
189		(XFS_BTREE_BMBT_INVALID_OWNER | XFS_BTREE_BMBT_WASDEL));
 
 
 
 
 
190	return new;
191}
192
193STATIC void
194xfs_bmbt_update_cursor(
195	struct xfs_btree_cur	*src,
196	struct xfs_btree_cur	*dst)
197{
198	ASSERT((dst->bc_tp->t_highest_agno != NULLAGNUMBER) ||
199	       (dst->bc_ino.ip->i_diflags & XFS_DIFLAG_REALTIME));
200
201	dst->bc_bmap.allocated += src->bc_bmap.allocated;
202	dst->bc_tp->t_highest_agno = src->bc_tp->t_highest_agno;
203
204	src->bc_bmap.allocated = 0;
205}
206
207STATIC int
208xfs_bmbt_alloc_block(
209	struct xfs_btree_cur		*cur,
210	const union xfs_btree_ptr	*start,
211	union xfs_btree_ptr		*new,
212	int				*stat)
213{
214	struct xfs_alloc_arg	args;
215	int			error;
216
217	memset(&args, 0, sizeof(args));
218	args.tp = cur->bc_tp;
219	args.mp = cur->bc_mp;
 
220	xfs_rmap_ino_bmbt_owner(&args.oinfo, cur->bc_ino.ip->i_ino,
221			cur->bc_ino.whichfork);
222	args.minlen = args.maxlen = args.prod = 1;
223	args.wasdel = cur->bc_flags & XFS_BTREE_BMBT_WASDEL;
224	if (!args.wasdel && args.tp->t_blk_res == 0)
225		return -ENOSPC;
226
227	/*
228	 * If we are coming here from something like unwritten extent
229	 * conversion, there has been no data extent allocation already done, so
230	 * we have to ensure that we attempt to locate the entire set of bmbt
231	 * allocations in the same AG, as xfs_bmapi_write() would have reserved.
232	 */
233	if (cur->bc_tp->t_highest_agno == NULLAGNUMBER)
234		args.minleft = xfs_bmapi_minleft(cur->bc_tp, cur->bc_ino.ip,
235					cur->bc_ino.whichfork);
 
 
 
 
 
 
 
 
 
 
 
236
237	error = xfs_alloc_vextent_start_ag(&args, be64_to_cpu(start->l));
 
 
 
 
 
 
238	if (error)
239		return error;
240
241	if (args.fsbno == NULLFSBLOCK && args.minleft) {
242		/*
243		 * Could not find an AG with enough free space to satisfy
244		 * a full btree split.  Try again and if
245		 * successful activate the lowspace algorithm.
246		 */
247		args.minleft = 0;
248		error = xfs_alloc_vextent_start_ag(&args, 0);
 
249		if (error)
250			return error;
251		cur->bc_tp->t_flags |= XFS_TRANS_LOWMODE;
252	}
253	if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
254		*stat = 0;
255		return 0;
256	}
257
258	ASSERT(args.len == 1);
259	cur->bc_bmap.allocated++;
260	cur->bc_ino.ip->i_nblocks++;
 
261	xfs_trans_log_inode(args.tp, cur->bc_ino.ip, XFS_ILOG_CORE);
262	xfs_trans_mod_dquot_byino(args.tp, cur->bc_ino.ip,
263			XFS_TRANS_DQ_BCOUNT, 1L);
264
265	new->l = cpu_to_be64(args.fsbno);
266
267	*stat = 1;
268	return 0;
 
 
 
269}
270
271STATIC int
272xfs_bmbt_free_block(
273	struct xfs_btree_cur	*cur,
274	struct xfs_buf		*bp)
275{
276	struct xfs_mount	*mp = cur->bc_mp;
277	struct xfs_inode	*ip = cur->bc_ino.ip;
278	struct xfs_trans	*tp = cur->bc_tp;
279	xfs_fsblock_t		fsbno = XFS_DADDR_TO_FSB(mp, xfs_buf_daddr(bp));
280	struct xfs_owner_info	oinfo;
281	int			error;
282
283	xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, cur->bc_ino.whichfork);
284	error = xfs_free_extent_later(cur->bc_tp, fsbno, 1, &oinfo,
285			XFS_AG_RESV_NONE, false);
286	if (error)
287		return error;
288
289	ip->i_nblocks--;
290	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
291	xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
292	return 0;
293}
294
295STATIC int
296xfs_bmbt_get_minrecs(
297	struct xfs_btree_cur	*cur,
298	int			level)
299{
300	if (level == cur->bc_nlevels - 1) {
301		struct xfs_ifork	*ifp = xfs_btree_ifork_ptr(cur);
 
 
 
302
303		return xfs_bmbt_maxrecs(cur->bc_mp,
304					ifp->if_broot_bytes, level == 0) / 2;
305	}
306
307	return cur->bc_mp->m_bmap_dmnr[level != 0];
308}
309
310int
311xfs_bmbt_get_maxrecs(
312	struct xfs_btree_cur	*cur,
313	int			level)
314{
315	if (level == cur->bc_nlevels - 1) {
316		struct xfs_ifork	*ifp = xfs_btree_ifork_ptr(cur);
 
 
 
317
318		return xfs_bmbt_maxrecs(cur->bc_mp,
319					ifp->if_broot_bytes, level == 0);
320	}
321
322	return cur->bc_mp->m_bmap_dmxr[level != 0];
323
324}
325
326/*
327 * Get the maximum records we could store in the on-disk format.
328 *
329 * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but
330 * for the root node this checks the available space in the dinode fork
331 * so that we can resize the in-memory buffer to match it.  After a
332 * resize to the maximum size this function returns the same value
333 * as xfs_bmbt_get_maxrecs for the root node, too.
334 */
335STATIC int
336xfs_bmbt_get_dmaxrecs(
337	struct xfs_btree_cur	*cur,
338	int			level)
339{
340	if (level != cur->bc_nlevels - 1)
341		return cur->bc_mp->m_bmap_dmxr[level != 0];
342	return xfs_bmdr_maxrecs(cur->bc_ino.forksize, level == 0);
343}
344
345STATIC void
346xfs_bmbt_init_key_from_rec(
347	union xfs_btree_key		*key,
348	const union xfs_btree_rec	*rec)
349{
350	key->bmbt.br_startoff =
351		cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt));
352}
353
354STATIC void
355xfs_bmbt_init_high_key_from_rec(
356	union xfs_btree_key		*key,
357	const union xfs_btree_rec	*rec)
358{
359	key->bmbt.br_startoff = cpu_to_be64(
360			xfs_bmbt_disk_get_startoff(&rec->bmbt) +
361			xfs_bmbt_disk_get_blockcount(&rec->bmbt) - 1);
362}
363
364STATIC void
365xfs_bmbt_init_rec_from_cur(
366	struct xfs_btree_cur	*cur,
367	union xfs_btree_rec	*rec)
368{
369	xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b);
370}
371
 
 
 
 
 
 
 
 
372STATIC int64_t
373xfs_bmbt_key_diff(
374	struct xfs_btree_cur		*cur,
375	const union xfs_btree_key	*key)
376{
377	return (int64_t)be64_to_cpu(key->bmbt.br_startoff) -
378				      cur->bc_rec.b.br_startoff;
379}
380
381STATIC int64_t
382xfs_bmbt_diff_two_keys(
383	struct xfs_btree_cur		*cur,
384	const union xfs_btree_key	*k1,
385	const union xfs_btree_key	*k2,
386	const union xfs_btree_key	*mask)
387{
388	uint64_t			a = be64_to_cpu(k1->bmbt.br_startoff);
389	uint64_t			b = be64_to_cpu(k2->bmbt.br_startoff);
390
391	ASSERT(!mask || mask->bmbt.br_startoff);
392
393	/*
394	 * Note: This routine previously casted a and b to int64 and subtracted
395	 * them to generate a result.  This lead to problems if b was the
396	 * "maximum" key value (all ones) being signed incorrectly, hence this
397	 * somewhat less efficient version.
398	 */
399	if (a > b)
400		return 1;
401	if (b > a)
402		return -1;
403	return 0;
404}
405
406static xfs_failaddr_t
407xfs_bmbt_verify(
408	struct xfs_buf		*bp)
409{
410	struct xfs_mount	*mp = bp->b_mount;
411	struct xfs_btree_block	*block = XFS_BUF_TO_BLOCK(bp);
412	xfs_failaddr_t		fa;
413	unsigned int		level;
414
415	if (!xfs_verify_magic(bp, block->bb_magic))
416		return __this_address;
417
418	if (xfs_has_crc(mp)) {
419		/*
420		 * XXX: need a better way of verifying the owner here. Right now
421		 * just make sure there has been one set.
422		 */
423		fa = xfs_btree_fsblock_v5hdr_verify(bp, XFS_RMAP_OWN_UNKNOWN);
424		if (fa)
425			return fa;
426	}
427
428	/*
429	 * numrecs and level verification.
430	 *
431	 * We don't know what fork we belong to, so just verify that the level
432	 * is less than the maximum of the two. Later checks will be more
433	 * precise.
434	 */
435	level = be16_to_cpu(block->bb_level);
436	if (level > max(mp->m_bm_maxlevels[0], mp->m_bm_maxlevels[1]))
437		return __this_address;
438
439	return xfs_btree_fsblock_verify(bp, mp->m_bmap_dmxr[level != 0]);
440}
441
442static void
443xfs_bmbt_read_verify(
444	struct xfs_buf	*bp)
445{
446	xfs_failaddr_t	fa;
447
448	if (!xfs_btree_fsblock_verify_crc(bp))
449		xfs_verifier_error(bp, -EFSBADCRC, __this_address);
450	else {
451		fa = xfs_bmbt_verify(bp);
452		if (fa)
453			xfs_verifier_error(bp, -EFSCORRUPTED, fa);
454	}
455
456	if (bp->b_error)
457		trace_xfs_btree_corrupt(bp, _RET_IP_);
458}
459
460static void
461xfs_bmbt_write_verify(
462	struct xfs_buf	*bp)
463{
464	xfs_failaddr_t	fa;
465
466	fa = xfs_bmbt_verify(bp);
467	if (fa) {
468		trace_xfs_btree_corrupt(bp, _RET_IP_);
469		xfs_verifier_error(bp, -EFSCORRUPTED, fa);
470		return;
471	}
472	xfs_btree_fsblock_calc_crc(bp);
473}
474
475const struct xfs_buf_ops xfs_bmbt_buf_ops = {
476	.name = "xfs_bmbt",
477	.magic = { cpu_to_be32(XFS_BMAP_MAGIC),
478		   cpu_to_be32(XFS_BMAP_CRC_MAGIC) },
479	.verify_read = xfs_bmbt_read_verify,
480	.verify_write = xfs_bmbt_write_verify,
481	.verify_struct = xfs_bmbt_verify,
482};
483
484
485STATIC int
486xfs_bmbt_keys_inorder(
487	struct xfs_btree_cur		*cur,
488	const union xfs_btree_key	*k1,
489	const union xfs_btree_key	*k2)
490{
491	return be64_to_cpu(k1->bmbt.br_startoff) <
492		be64_to_cpu(k2->bmbt.br_startoff);
493}
494
495STATIC int
496xfs_bmbt_recs_inorder(
497	struct xfs_btree_cur		*cur,
498	const union xfs_btree_rec	*r1,
499	const union xfs_btree_rec	*r2)
500{
501	return xfs_bmbt_disk_get_startoff(&r1->bmbt) +
502		xfs_bmbt_disk_get_blockcount(&r1->bmbt) <=
503		xfs_bmbt_disk_get_startoff(&r2->bmbt);
504}
505
506STATIC enum xbtree_key_contig
507xfs_bmbt_keys_contiguous(
508	struct xfs_btree_cur		*cur,
509	const union xfs_btree_key	*key1,
510	const union xfs_btree_key	*key2,
511	const union xfs_btree_key	*mask)
512{
513	ASSERT(!mask || mask->bmbt.br_startoff);
514
515	return xbtree_key_contig(be64_to_cpu(key1->bmbt.br_startoff),
516				 be64_to_cpu(key2->bmbt.br_startoff));
517}
518
519const struct xfs_btree_ops xfs_bmbt_ops = {
520	.name			= "bmap",
521	.type			= XFS_BTREE_TYPE_INODE,
522
523	.rec_len		= sizeof(xfs_bmbt_rec_t),
524	.key_len		= sizeof(xfs_bmbt_key_t),
525	.ptr_len		= XFS_BTREE_LONG_PTR_LEN,
526
527	.lru_refs		= XFS_BMAP_BTREE_REF,
528	.statoff		= XFS_STATS_CALC_INDEX(xs_bmbt_2),
529
530	.dup_cursor		= xfs_bmbt_dup_cursor,
531	.update_cursor		= xfs_bmbt_update_cursor,
532	.alloc_block		= xfs_bmbt_alloc_block,
533	.free_block		= xfs_bmbt_free_block,
534	.get_maxrecs		= xfs_bmbt_get_maxrecs,
535	.get_minrecs		= xfs_bmbt_get_minrecs,
536	.get_dmaxrecs		= xfs_bmbt_get_dmaxrecs,
537	.init_key_from_rec	= xfs_bmbt_init_key_from_rec,
538	.init_high_key_from_rec	= xfs_bmbt_init_high_key_from_rec,
539	.init_rec_from_cur	= xfs_bmbt_init_rec_from_cur,
 
540	.key_diff		= xfs_bmbt_key_diff,
541	.diff_two_keys		= xfs_bmbt_diff_two_keys,
542	.buf_ops		= &xfs_bmbt_buf_ops,
543	.keys_inorder		= xfs_bmbt_keys_inorder,
544	.recs_inorder		= xfs_bmbt_recs_inorder,
545	.keys_contiguous	= xfs_bmbt_keys_contiguous,
546};
547
548/*
549 * Create a new bmap btree cursor.
550 *
551 * For staging cursors -1 in passed in whichfork.
552 */
553struct xfs_btree_cur *
554xfs_bmbt_init_cursor(
555	struct xfs_mount	*mp,
556	struct xfs_trans	*tp,
557	struct xfs_inode	*ip,
558	int			whichfork)
559{
 
560	struct xfs_btree_cur	*cur;
561	unsigned int		maxlevels;
562
563	ASSERT(whichfork != XFS_COW_FORK);
564
565	/*
566	 * The Data fork always has larger maxlevel, so use that for staging
567	 * cursors.
568	 */
569	switch (whichfork) {
570	case XFS_STAGING_FORK:
571		maxlevels = mp->m_bm_maxlevels[XFS_DATA_FORK];
572		break;
573	default:
574		maxlevels = mp->m_bm_maxlevels[whichfork];
575		break;
576	}
577	cur = xfs_btree_alloc_cursor(mp, tp, &xfs_bmbt_ops, maxlevels,
578			xfs_bmbt_cur_cache);
 
579	cur->bc_ino.ip = ip;
 
 
580	cur->bc_ino.whichfork = whichfork;
581	cur->bc_bmap.allocated = 0;
582	if (whichfork != XFS_STAGING_FORK) {
583		struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
584
585		cur->bc_nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1;
586		cur->bc_ino.forksize = xfs_inode_fork_size(ip, whichfork);
587	}
588	return cur;
589}
590
591/* Calculate number of records in a block mapping btree block. */
592static inline unsigned int
593xfs_bmbt_block_maxrecs(
594	unsigned int		blocklen,
595	bool			leaf)
596{
597	if (leaf)
598		return blocklen / sizeof(xfs_bmbt_rec_t);
599	return blocklen / (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t));
600}
601
602/*
603 * Swap in the new inode fork root.  Once we pass this point the newly rebuilt
604 * mappings are in place and we have to kill off any old btree blocks.
605 */
606void
607xfs_bmbt_commit_staged_btree(
608	struct xfs_btree_cur	*cur,
609	struct xfs_trans	*tp,
610	int			whichfork)
611{
612	struct xbtree_ifakeroot	*ifake = cur->bc_ino.ifake;
613	struct xfs_ifork	*ifp;
614	static const short	brootflag[2] = {XFS_ILOG_DBROOT, XFS_ILOG_ABROOT};
615	static const short	extflag[2] = {XFS_ILOG_DEXT, XFS_ILOG_AEXT};
616	int			flags = XFS_ILOG_CORE;
617
618	ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
619	ASSERT(whichfork != XFS_COW_FORK);
620
621	/*
622	 * Free any resources hanging off the real fork, then shallow-copy the
623	 * staging fork's contents into the real fork to transfer everything
624	 * we just built.
625	 */
626	ifp = xfs_ifork_ptr(cur->bc_ino.ip, whichfork);
627	xfs_idestroy_fork(ifp);
628	memcpy(ifp, ifake->if_fork, sizeof(struct xfs_ifork));
629
630	switch (ifp->if_format) {
631	case XFS_DINODE_FMT_EXTENTS:
632		flags |= extflag[whichfork];
633		break;
634	case XFS_DINODE_FMT_BTREE:
635		flags |= brootflag[whichfork];
636		break;
637	default:
638		ASSERT(0);
639		break;
640	}
641	xfs_trans_log_inode(tp, cur->bc_ino.ip, flags);
642	xfs_btree_commit_ifakeroot(cur, tp, whichfork);
643}
644
645/*
646 * Calculate number of records in a bmap btree block.
647 */
648int
649xfs_bmbt_maxrecs(
650	struct xfs_mount	*mp,
651	int			blocklen,
652	int			leaf)
653{
654	blocklen -= XFS_BMBT_BLOCK_LEN(mp);
655	return xfs_bmbt_block_maxrecs(blocklen, leaf);
656}
657
658/*
659 * Calculate the maximum possible height of the btree that the on-disk format
660 * supports. This is used for sizing structures large enough to support every
661 * possible configuration of a filesystem that might get mounted.
662 */
663unsigned int
664xfs_bmbt_maxlevels_ondisk(void)
665{
666	unsigned int		minrecs[2];
667	unsigned int		blocklen;
668
669	blocklen = min(XFS_MIN_BLOCKSIZE - XFS_BTREE_SBLOCK_LEN,
670		       XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN);
671
672	minrecs[0] = xfs_bmbt_block_maxrecs(blocklen, true) / 2;
673	minrecs[1] = xfs_bmbt_block_maxrecs(blocklen, false) / 2;
674
675	/* One extra level for the inode root. */
676	return xfs_btree_compute_maxlevels(minrecs,
677			XFS_MAX_EXTCNT_DATA_FORK_LARGE) + 1;
678}
679
680/*
681 * Calculate number of records in a bmap btree inode root.
682 */
683int
684xfs_bmdr_maxrecs(
685	int			blocklen,
686	int			leaf)
687{
688	blocklen -= sizeof(xfs_bmdr_block_t);
689
690	if (leaf)
691		return blocklen / sizeof(xfs_bmdr_rec_t);
692	return blocklen / (sizeof(xfs_bmdr_key_t) + sizeof(xfs_bmdr_ptr_t));
693}
694
695/*
696 * Change the owner of a btree format fork fo the inode passed in. Change it to
697 * the owner of that is passed in so that we can change owners before or after
698 * we switch forks between inodes. The operation that the caller is doing will
699 * determine whether is needs to change owner before or after the switch.
700 *
701 * For demand paged transactional modification, the fork switch should be done
702 * after reading in all the blocks, modifying them and pinning them in the
703 * transaction. For modification when the buffers are already pinned in memory,
704 * the fork switch can be done before changing the owner as we won't need to
705 * validate the owner until the btree buffers are unpinned and writes can occur
706 * again.
707 *
708 * For recovery based ownership change, there is no transactional context and
709 * so a buffer list must be supplied so that we can record the buffers that we
710 * modified for the caller to issue IO on.
711 */
712int
713xfs_bmbt_change_owner(
714	struct xfs_trans	*tp,
715	struct xfs_inode	*ip,
716	int			whichfork,
717	xfs_ino_t		new_owner,
718	struct list_head	*buffer_list)
719{
720	struct xfs_btree_cur	*cur;
721	int			error;
722
723	ASSERT(tp || buffer_list);
724	ASSERT(!(tp && buffer_list));
725	ASSERT(xfs_ifork_ptr(ip, whichfork)->if_format == XFS_DINODE_FMT_BTREE);
726
727	cur = xfs_bmbt_init_cursor(ip->i_mount, tp, ip, whichfork);
728	cur->bc_flags |= XFS_BTREE_BMBT_INVALID_OWNER;
 
 
729
730	error = xfs_btree_change_owner(cur, new_owner, buffer_list);
731	xfs_btree_del_cursor(cur, error);
732	return error;
733}
734
735/* Calculate the bmap btree size for some records. */
736unsigned long long
737xfs_bmbt_calc_size(
738	struct xfs_mount	*mp,
739	unsigned long long	len)
740{
741	return xfs_btree_calc_size(mp->m_bmap_dmnr, len);
742}
743
744int __init
745xfs_bmbt_init_cur_cache(void)
746{
747	xfs_bmbt_cur_cache = kmem_cache_create("xfs_bmbt_cur",
748			xfs_btree_cur_sizeof(xfs_bmbt_maxlevels_ondisk()),
749			0, 0, NULL);
750
751	if (!xfs_bmbt_cur_cache)
752		return -ENOMEM;
753	return 0;
754}
755
756void
757xfs_bmbt_destroy_cur_cache(void)
758{
759	kmem_cache_destroy(xfs_bmbt_cur_cache);
760	xfs_bmbt_cur_cache = NULL;
761}
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (c) 2000-2003,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_trans.h"
 16#include "xfs_alloc.h"
 17#include "xfs_btree.h"
 
 18#include "xfs_bmap_btree.h"
 19#include "xfs_bmap.h"
 20#include "xfs_error.h"
 21#include "xfs_quota.h"
 22#include "xfs_trace.h"
 23#include "xfs_rmap.h"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 24
 25/*
 26 * Convert on-disk form of btree root to in-memory form.
 27 */
 28void
 29xfs_bmdr_to_bmbt(
 30	struct xfs_inode	*ip,
 31	xfs_bmdr_block_t	*dblock,
 32	int			dblocklen,
 33	struct xfs_btree_block	*rblock,
 34	int			rblocklen)
 35{
 36	struct xfs_mount	*mp = ip->i_mount;
 37	int			dmxr;
 38	xfs_bmbt_key_t		*fkp;
 39	__be64			*fpp;
 40	xfs_bmbt_key_t		*tkp;
 41	__be64			*tpp;
 42
 43	xfs_btree_init_block_int(mp, rblock, XFS_BUF_DADDR_NULL,
 44				 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
 45				 XFS_BTREE_LONG_PTRS);
 46	rblock->bb_level = dblock->bb_level;
 47	ASSERT(be16_to_cpu(rblock->bb_level) > 0);
 48	rblock->bb_numrecs = dblock->bb_numrecs;
 49	dmxr = xfs_bmdr_maxrecs(dblocklen, 0);
 50	fkp = XFS_BMDR_KEY_ADDR(dblock, 1);
 51	tkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1);
 52	fpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr);
 53	tpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
 54	dmxr = be16_to_cpu(dblock->bb_numrecs);
 55	memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
 56	memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
 57}
 58
 59void
 60xfs_bmbt_disk_get_all(
 61	struct xfs_bmbt_rec	*rec,
 62	struct xfs_bmbt_irec	*irec)
 63{
 64	uint64_t		l0 = get_unaligned_be64(&rec->l0);
 65	uint64_t		l1 = get_unaligned_be64(&rec->l1);
 66
 67	irec->br_startoff = (l0 & xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
 68	irec->br_startblock = ((l0 & xfs_mask64lo(9)) << 43) | (l1 >> 21);
 69	irec->br_blockcount = l1 & xfs_mask64lo(21);
 70	if (l0 >> (64 - BMBT_EXNTFLAG_BITLEN))
 71		irec->br_state = XFS_EXT_UNWRITTEN;
 72	else
 73		irec->br_state = XFS_EXT_NORM;
 74}
 75
 76/*
 77 * Extract the blockcount field from an on disk bmap extent record.
 78 */
 79xfs_filblks_t
 80xfs_bmbt_disk_get_blockcount(
 81	xfs_bmbt_rec_t	*r)
 82{
 83	return (xfs_filblks_t)(be64_to_cpu(r->l1) & xfs_mask64lo(21));
 84}
 85
 86/*
 87 * Extract the startoff field from a disk format bmap extent record.
 88 */
 89xfs_fileoff_t
 90xfs_bmbt_disk_get_startoff(
 91	xfs_bmbt_rec_t	*r)
 92{
 93	return ((xfs_fileoff_t)be64_to_cpu(r->l0) &
 94		 xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
 95}
 96
 97/*
 98 * Set all the fields in a bmap extent record from the uncompressed form.
 99 */
100void
101xfs_bmbt_disk_set_all(
102	struct xfs_bmbt_rec	*r,
103	struct xfs_bmbt_irec	*s)
104{
105	int			extent_flag = (s->br_state != XFS_EXT_NORM);
106
107	ASSERT(s->br_state == XFS_EXT_NORM || s->br_state == XFS_EXT_UNWRITTEN);
108	ASSERT(!(s->br_startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)));
109	ASSERT(!(s->br_blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)));
110	ASSERT(!(s->br_startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)));
111
112	put_unaligned_be64(
113		((xfs_bmbt_rec_base_t)extent_flag << 63) |
114		 ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
115		 ((xfs_bmbt_rec_base_t)s->br_startblock >> 43), &r->l0);
116	put_unaligned_be64(
117		((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
118		 ((xfs_bmbt_rec_base_t)s->br_blockcount &
119		  (xfs_bmbt_rec_base_t)xfs_mask64lo(21)), &r->l1);
120}
121
122/*
123 * Convert in-memory form of btree root to on-disk form.
124 */
125void
126xfs_bmbt_to_bmdr(
127	struct xfs_mount	*mp,
128	struct xfs_btree_block	*rblock,
129	int			rblocklen,
130	xfs_bmdr_block_t	*dblock,
131	int			dblocklen)
132{
133	int			dmxr;
134	xfs_bmbt_key_t		*fkp;
135	__be64			*fpp;
136	xfs_bmbt_key_t		*tkp;
137	__be64			*tpp;
138
139	if (xfs_sb_version_hascrc(&mp->m_sb)) {
140		ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_CRC_MAGIC));
141		ASSERT(uuid_equal(&rblock->bb_u.l.bb_uuid,
142		       &mp->m_sb.sb_meta_uuid));
143		ASSERT(rblock->bb_u.l.bb_blkno ==
144		       cpu_to_be64(XFS_BUF_DADDR_NULL));
145	} else
146		ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_MAGIC));
147	ASSERT(rblock->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK));
148	ASSERT(rblock->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK));
149	ASSERT(rblock->bb_level != 0);
150	dblock->bb_level = rblock->bb_level;
151	dblock->bb_numrecs = rblock->bb_numrecs;
152	dmxr = xfs_bmdr_maxrecs(dblocklen, 0);
153	fkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1);
154	tkp = XFS_BMDR_KEY_ADDR(dblock, 1);
155	fpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
156	tpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr);
157	dmxr = be16_to_cpu(dblock->bb_numrecs);
158	memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
159	memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
160}
161
162STATIC struct xfs_btree_cur *
163xfs_bmbt_dup_cursor(
164	struct xfs_btree_cur	*cur)
165{
166	struct xfs_btree_cur	*new;
167
168	new = xfs_bmbt_init_cursor(cur->bc_mp, cur->bc_tp,
169			cur->bc_ino.ip, cur->bc_ino.whichfork);
170
171	/*
172	 * Copy the firstblock, dfops, and flags values,
173	 * since init cursor doesn't get them.
174	 */
175	new->bc_ino.flags = cur->bc_ino.flags;
176
177	return new;
178}
179
180STATIC void
181xfs_bmbt_update_cursor(
182	struct xfs_btree_cur	*src,
183	struct xfs_btree_cur	*dst)
184{
185	ASSERT((dst->bc_tp->t_firstblock != NULLFSBLOCK) ||
186	       (dst->bc_ino.ip->i_d.di_flags & XFS_DIFLAG_REALTIME));
187
188	dst->bc_ino.allocated += src->bc_ino.allocated;
189	dst->bc_tp->t_firstblock = src->bc_tp->t_firstblock;
190
191	src->bc_ino.allocated = 0;
192}
193
194STATIC int
195xfs_bmbt_alloc_block(
196	struct xfs_btree_cur	*cur,
197	union xfs_btree_ptr	*start,
198	union xfs_btree_ptr	*new,
199	int			*stat)
200{
201	xfs_alloc_arg_t		args;		/* block allocation args */
202	int			error;		/* error return value */
203
204	memset(&args, 0, sizeof(args));
205	args.tp = cur->bc_tp;
206	args.mp = cur->bc_mp;
207	args.fsbno = cur->bc_tp->t_firstblock;
208	xfs_rmap_ino_bmbt_owner(&args.oinfo, cur->bc_ino.ip->i_ino,
209			cur->bc_ino.whichfork);
 
 
 
 
210
211	if (args.fsbno == NULLFSBLOCK) {
212		args.fsbno = be64_to_cpu(start->l);
213		args.type = XFS_ALLOCTYPE_START_BNO;
214		/*
215		 * Make sure there is sufficient room left in the AG to
216		 * complete a full tree split for an extent insert.  If
217		 * we are converting the middle part of an extent then
218		 * we may need space for two tree splits.
219		 *
220		 * We are relying on the caller to make the correct block
221		 * reservation for this operation to succeed.  If the
222		 * reservation amount is insufficient then we may fail a
223		 * block allocation here and corrupt the filesystem.
224		 */
225		args.minleft = args.tp->t_blk_res;
226	} else if (cur->bc_tp->t_flags & XFS_TRANS_LOWMODE) {
227		args.type = XFS_ALLOCTYPE_START_BNO;
228	} else {
229		args.type = XFS_ALLOCTYPE_NEAR_BNO;
230	}
231
232	args.minlen = args.maxlen = args.prod = 1;
233	args.wasdel = cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL;
234	if (!args.wasdel && args.tp->t_blk_res == 0) {
235		error = -ENOSPC;
236		goto error0;
237	}
238	error = xfs_alloc_vextent(&args);
239	if (error)
240		goto error0;
241
242	if (args.fsbno == NULLFSBLOCK && args.minleft) {
243		/*
244		 * Could not find an AG with enough free space to satisfy
245		 * a full btree split.  Try again and if
246		 * successful activate the lowspace algorithm.
247		 */
248		args.fsbno = 0;
249		args.type = XFS_ALLOCTYPE_FIRST_AG;
250		error = xfs_alloc_vextent(&args);
251		if (error)
252			goto error0;
253		cur->bc_tp->t_flags |= XFS_TRANS_LOWMODE;
254	}
255	if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
256		*stat = 0;
257		return 0;
258	}
259
260	ASSERT(args.len == 1);
261	cur->bc_tp->t_firstblock = args.fsbno;
262	cur->bc_ino.allocated++;
263	cur->bc_ino.ip->i_d.di_nblocks++;
264	xfs_trans_log_inode(args.tp, cur->bc_ino.ip, XFS_ILOG_CORE);
265	xfs_trans_mod_dquot_byino(args.tp, cur->bc_ino.ip,
266			XFS_TRANS_DQ_BCOUNT, 1L);
267
268	new->l = cpu_to_be64(args.fsbno);
269
270	*stat = 1;
271	return 0;
272
273 error0:
274	return error;
275}
276
277STATIC int
278xfs_bmbt_free_block(
279	struct xfs_btree_cur	*cur,
280	struct xfs_buf		*bp)
281{
282	struct xfs_mount	*mp = cur->bc_mp;
283	struct xfs_inode	*ip = cur->bc_ino.ip;
284	struct xfs_trans	*tp = cur->bc_tp;
285	xfs_fsblock_t		fsbno = XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(bp));
286	struct xfs_owner_info	oinfo;
 
287
288	xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, cur->bc_ino.whichfork);
289	xfs_bmap_add_free(cur->bc_tp, fsbno, 1, &oinfo);
290	ip->i_d.di_nblocks--;
 
 
291
 
292	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
293	xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
294	return 0;
295}
296
297STATIC int
298xfs_bmbt_get_minrecs(
299	struct xfs_btree_cur	*cur,
300	int			level)
301{
302	if (level == cur->bc_nlevels - 1) {
303		struct xfs_ifork	*ifp;
304
305		ifp = XFS_IFORK_PTR(cur->bc_ino.ip,
306				    cur->bc_ino.whichfork);
307
308		return xfs_bmbt_maxrecs(cur->bc_mp,
309					ifp->if_broot_bytes, level == 0) / 2;
310	}
311
312	return cur->bc_mp->m_bmap_dmnr[level != 0];
313}
314
315int
316xfs_bmbt_get_maxrecs(
317	struct xfs_btree_cur	*cur,
318	int			level)
319{
320	if (level == cur->bc_nlevels - 1) {
321		struct xfs_ifork	*ifp;
322
323		ifp = XFS_IFORK_PTR(cur->bc_ino.ip,
324				    cur->bc_ino.whichfork);
325
326		return xfs_bmbt_maxrecs(cur->bc_mp,
327					ifp->if_broot_bytes, level == 0);
328	}
329
330	return cur->bc_mp->m_bmap_dmxr[level != 0];
331
332}
333
334/*
335 * Get the maximum records we could store in the on-disk format.
336 *
337 * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but
338 * for the root node this checks the available space in the dinode fork
339 * so that we can resize the in-memory buffer to match it.  After a
340 * resize to the maximum size this function returns the same value
341 * as xfs_bmbt_get_maxrecs for the root node, too.
342 */
343STATIC int
344xfs_bmbt_get_dmaxrecs(
345	struct xfs_btree_cur	*cur,
346	int			level)
347{
348	if (level != cur->bc_nlevels - 1)
349		return cur->bc_mp->m_bmap_dmxr[level != 0];
350	return xfs_bmdr_maxrecs(cur->bc_ino.forksize, level == 0);
351}
352
353STATIC void
354xfs_bmbt_init_key_from_rec(
355	union xfs_btree_key	*key,
356	union xfs_btree_rec	*rec)
357{
358	key->bmbt.br_startoff =
359		cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt));
360}
361
362STATIC void
363xfs_bmbt_init_high_key_from_rec(
364	union xfs_btree_key	*key,
365	union xfs_btree_rec	*rec)
366{
367	key->bmbt.br_startoff = cpu_to_be64(
368			xfs_bmbt_disk_get_startoff(&rec->bmbt) +
369			xfs_bmbt_disk_get_blockcount(&rec->bmbt) - 1);
370}
371
372STATIC void
373xfs_bmbt_init_rec_from_cur(
374	struct xfs_btree_cur	*cur,
375	union xfs_btree_rec	*rec)
376{
377	xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b);
378}
379
380STATIC void
381xfs_bmbt_init_ptr_from_cur(
382	struct xfs_btree_cur	*cur,
383	union xfs_btree_ptr	*ptr)
384{
385	ptr->l = 0;
386}
387
388STATIC int64_t
389xfs_bmbt_key_diff(
390	struct xfs_btree_cur	*cur,
391	union xfs_btree_key	*key)
392{
393	return (int64_t)be64_to_cpu(key->bmbt.br_startoff) -
394				      cur->bc_rec.b.br_startoff;
395}
396
397STATIC int64_t
398xfs_bmbt_diff_two_keys(
399	struct xfs_btree_cur	*cur,
400	union xfs_btree_key	*k1,
401	union xfs_btree_key	*k2)
 
402{
403	uint64_t		a = be64_to_cpu(k1->bmbt.br_startoff);
404	uint64_t		b = be64_to_cpu(k2->bmbt.br_startoff);
 
 
405
406	/*
407	 * Note: This routine previously casted a and b to int64 and subtracted
408	 * them to generate a result.  This lead to problems if b was the
409	 * "maximum" key value (all ones) being signed incorrectly, hence this
410	 * somewhat less efficient version.
411	 */
412	if (a > b)
413		return 1;
414	if (b > a)
415		return -1;
416	return 0;
417}
418
419static xfs_failaddr_t
420xfs_bmbt_verify(
421	struct xfs_buf		*bp)
422{
423	struct xfs_mount	*mp = bp->b_mount;
424	struct xfs_btree_block	*block = XFS_BUF_TO_BLOCK(bp);
425	xfs_failaddr_t		fa;
426	unsigned int		level;
427
428	if (!xfs_verify_magic(bp, block->bb_magic))
429		return __this_address;
430
431	if (xfs_sb_version_hascrc(&mp->m_sb)) {
432		/*
433		 * XXX: need a better way of verifying the owner here. Right now
434		 * just make sure there has been one set.
435		 */
436		fa = xfs_btree_lblock_v5hdr_verify(bp, XFS_RMAP_OWN_UNKNOWN);
437		if (fa)
438			return fa;
439	}
440
441	/*
442	 * numrecs and level verification.
443	 *
444	 * We don't know what fork we belong to, so just verify that the level
445	 * is less than the maximum of the two. Later checks will be more
446	 * precise.
447	 */
448	level = be16_to_cpu(block->bb_level);
449	if (level > max(mp->m_bm_maxlevels[0], mp->m_bm_maxlevels[1]))
450		return __this_address;
451
452	return xfs_btree_lblock_verify(bp, mp->m_bmap_dmxr[level != 0]);
453}
454
455static void
456xfs_bmbt_read_verify(
457	struct xfs_buf	*bp)
458{
459	xfs_failaddr_t	fa;
460
461	if (!xfs_btree_lblock_verify_crc(bp))
462		xfs_verifier_error(bp, -EFSBADCRC, __this_address);
463	else {
464		fa = xfs_bmbt_verify(bp);
465		if (fa)
466			xfs_verifier_error(bp, -EFSCORRUPTED, fa);
467	}
468
469	if (bp->b_error)
470		trace_xfs_btree_corrupt(bp, _RET_IP_);
471}
472
473static void
474xfs_bmbt_write_verify(
475	struct xfs_buf	*bp)
476{
477	xfs_failaddr_t	fa;
478
479	fa = xfs_bmbt_verify(bp);
480	if (fa) {
481		trace_xfs_btree_corrupt(bp, _RET_IP_);
482		xfs_verifier_error(bp, -EFSCORRUPTED, fa);
483		return;
484	}
485	xfs_btree_lblock_calc_crc(bp);
486}
487
488const struct xfs_buf_ops xfs_bmbt_buf_ops = {
489	.name = "xfs_bmbt",
490	.magic = { cpu_to_be32(XFS_BMAP_MAGIC),
491		   cpu_to_be32(XFS_BMAP_CRC_MAGIC) },
492	.verify_read = xfs_bmbt_read_verify,
493	.verify_write = xfs_bmbt_write_verify,
494	.verify_struct = xfs_bmbt_verify,
495};
496
497
498STATIC int
499xfs_bmbt_keys_inorder(
500	struct xfs_btree_cur	*cur,
501	union xfs_btree_key	*k1,
502	union xfs_btree_key	*k2)
503{
504	return be64_to_cpu(k1->bmbt.br_startoff) <
505		be64_to_cpu(k2->bmbt.br_startoff);
506}
507
508STATIC int
509xfs_bmbt_recs_inorder(
510	struct xfs_btree_cur	*cur,
511	union xfs_btree_rec	*r1,
512	union xfs_btree_rec	*r2)
513{
514	return xfs_bmbt_disk_get_startoff(&r1->bmbt) +
515		xfs_bmbt_disk_get_blockcount(&r1->bmbt) <=
516		xfs_bmbt_disk_get_startoff(&r2->bmbt);
517}
518
519static const struct xfs_btree_ops xfs_bmbt_ops = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
520	.rec_len		= sizeof(xfs_bmbt_rec_t),
521	.key_len		= sizeof(xfs_bmbt_key_t),
 
 
 
 
522
523	.dup_cursor		= xfs_bmbt_dup_cursor,
524	.update_cursor		= xfs_bmbt_update_cursor,
525	.alloc_block		= xfs_bmbt_alloc_block,
526	.free_block		= xfs_bmbt_free_block,
527	.get_maxrecs		= xfs_bmbt_get_maxrecs,
528	.get_minrecs		= xfs_bmbt_get_minrecs,
529	.get_dmaxrecs		= xfs_bmbt_get_dmaxrecs,
530	.init_key_from_rec	= xfs_bmbt_init_key_from_rec,
531	.init_high_key_from_rec	= xfs_bmbt_init_high_key_from_rec,
532	.init_rec_from_cur	= xfs_bmbt_init_rec_from_cur,
533	.init_ptr_from_cur	= xfs_bmbt_init_ptr_from_cur,
534	.key_diff		= xfs_bmbt_key_diff,
535	.diff_two_keys		= xfs_bmbt_diff_two_keys,
536	.buf_ops		= &xfs_bmbt_buf_ops,
537	.keys_inorder		= xfs_bmbt_keys_inorder,
538	.recs_inorder		= xfs_bmbt_recs_inorder,
 
539};
540
541/*
542 * Allocate a new bmap btree cursor.
 
 
543 */
544struct xfs_btree_cur *				/* new bmap btree cursor */
545xfs_bmbt_init_cursor(
546	struct xfs_mount	*mp,		/* file system mount point */
547	struct xfs_trans	*tp,		/* transaction pointer */
548	struct xfs_inode	*ip,		/* inode owning the btree */
549	int			whichfork)	/* data or attr fork */
550{
551	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, whichfork);
552	struct xfs_btree_cur	*cur;
 
 
553	ASSERT(whichfork != XFS_COW_FORK);
554
555	cur = kmem_cache_zalloc(xfs_btree_cur_zone, GFP_NOFS | __GFP_NOFAIL);
556
557	cur->bc_tp = tp;
558	cur->bc_mp = mp;
559	cur->bc_nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1;
560	cur->bc_btnum = XFS_BTNUM_BMAP;
561	cur->bc_blocklog = mp->m_sb.sb_blocklog;
562	cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_bmbt_2);
563
564	cur->bc_ops = &xfs_bmbt_ops;
565	cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE;
566	if (xfs_sb_version_hascrc(&mp->m_sb))
567		cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
568
569	cur->bc_ino.forksize = XFS_IFORK_SIZE(ip, whichfork);
570	cur->bc_ino.ip = ip;
571	cur->bc_ino.allocated = 0;
572	cur->bc_ino.flags = 0;
573	cur->bc_ino.whichfork = whichfork;
 
 
 
574
 
 
 
575	return cur;
576}
577
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
578/*
579 * Calculate number of records in a bmap btree block.
580 */
581int
582xfs_bmbt_maxrecs(
583	struct xfs_mount	*mp,
584	int			blocklen,
585	int			leaf)
586{
587	blocklen -= XFS_BMBT_BLOCK_LEN(mp);
 
 
 
 
 
 
 
 
 
 
 
 
 
588
589	if (leaf)
590		return blocklen / sizeof(xfs_bmbt_rec_t);
591	return blocklen / (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t));
 
 
 
 
 
 
592}
593
594/*
595 * Calculate number of records in a bmap btree inode root.
596 */
597int
598xfs_bmdr_maxrecs(
599	int			blocklen,
600	int			leaf)
601{
602	blocklen -= sizeof(xfs_bmdr_block_t);
603
604	if (leaf)
605		return blocklen / sizeof(xfs_bmdr_rec_t);
606	return blocklen / (sizeof(xfs_bmdr_key_t) + sizeof(xfs_bmdr_ptr_t));
607}
608
609/*
610 * Change the owner of a btree format fork fo the inode passed in. Change it to
611 * the owner of that is passed in so that we can change owners before or after
612 * we switch forks between inodes. The operation that the caller is doing will
613 * determine whether is needs to change owner before or after the switch.
614 *
615 * For demand paged transactional modification, the fork switch should be done
616 * after reading in all the blocks, modifying them and pinning them in the
617 * transaction. For modification when the buffers are already pinned in memory,
618 * the fork switch can be done before changing the owner as we won't need to
619 * validate the owner until the btree buffers are unpinned and writes can occur
620 * again.
621 *
622 * For recovery based ownership change, there is no transactional context and
623 * so a buffer list must be supplied so that we can record the buffers that we
624 * modified for the caller to issue IO on.
625 */
626int
627xfs_bmbt_change_owner(
628	struct xfs_trans	*tp,
629	struct xfs_inode	*ip,
630	int			whichfork,
631	xfs_ino_t		new_owner,
632	struct list_head	*buffer_list)
633{
634	struct xfs_btree_cur	*cur;
635	int			error;
636
637	ASSERT(tp || buffer_list);
638	ASSERT(!(tp && buffer_list));
639	ASSERT(XFS_IFORK_PTR(ip, whichfork)->if_format == XFS_DINODE_FMT_BTREE);
640
641	cur = xfs_bmbt_init_cursor(ip->i_mount, tp, ip, whichfork);
642	if (!cur)
643		return -ENOMEM;
644	cur->bc_ino.flags |= XFS_BTCUR_BMBT_INVALID_OWNER;
645
646	error = xfs_btree_change_owner(cur, new_owner, buffer_list);
647	xfs_btree_del_cursor(cur, error);
648	return error;
649}
650
651/* Calculate the bmap btree size for some records. */
652unsigned long long
653xfs_bmbt_calc_size(
654	struct xfs_mount	*mp,
655	unsigned long long	len)
656{
657	return xfs_btree_calc_size(mp->m_bmap_dmnr, len);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
658}