xfs_btree.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 950 行 · 第 1/2 页

C
950
字号
/* * Copyright (c) 2000-2002 Silicon Graphics, Inc.  All Rights Reserved. * * This program is free software; you can redistribute it and/or modify it * under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * This program is distributed in the hope that it would be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * Further, this software is distributed without any warranty that it is * free of the rightful claim of any third person regarding infringement * or the like.  Any license provided herein, whether implied or * otherwise, applies only to this software file.  Patent licenses, if * any, provided herein do not apply to combinations of this program with * other software, or any other product whatsoever. * * You should have received a copy of the GNU General Public License along * with this program; if not, write the Free Software Foundation, Inc., 59 * Temple Place - Suite 330, Boston MA 02111-1307, USA. * * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, * Mountain View, CA  94043, or: * * http://www.sgi.com * * For further information regarding this notice, see: * * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ *//* * This file contains common code for the space manager's btree implementations. */#include "xfs.h"#include "xfs_macros.h"#include "xfs_types.h"#include "xfs_inum.h"#include "xfs_log.h"#include "xfs_trans.h"#include "xfs_sb.h"#include "xfs_ag.h"#include "xfs_dir.h"#include "xfs_dir2.h"#include "xfs_dmapi.h"#include "xfs_mount.h"#include "xfs_alloc_btree.h"#include "xfs_bmap_btree.h"#include "xfs_ialloc_btree.h"#include "xfs_btree.h"#include "xfs_ialloc.h"#include "xfs_attr_sf.h"#include "xfs_dir_sf.h"#include "xfs_dir2_sf.h"#include "xfs_dinode.h"#include "xfs_inode.h"#include "xfs_bit.h"#include "xfs_error.h"/* * Cursor allocation zone. */kmem_zone_t	*xfs_btree_cur_zone;/* * Btree magic numbers. */const __uint32_t xfs_magics[XFS_BTNUM_MAX] ={	XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC};/* * Prototypes for internal routines. *//* * Checking routine: return maxrecs for the block. */STATIC int				/* number of records fitting in block */xfs_btree_maxrecs(	xfs_btree_cur_t		*cur,	/* btree cursor */	xfs_btree_block_t	*block);/* generic btree block pointer *//* * Internal routines. *//* * Checking routine: return maxrecs for the block. */STATIC int				/* number of records fitting in block */xfs_btree_maxrecs(	xfs_btree_cur_t		*cur,	/* btree cursor */	xfs_btree_block_t	*block)	/* generic btree block pointer */{	switch (cur->bc_btnum) {	case XFS_BTNUM_BNO:	case XFS_BTNUM_CNT:		return (int)XFS_ALLOC_BLOCK_MAXRECS(INT_GET(block->bb_h.bb_level, ARCH_CONVERT), cur);	case XFS_BTNUM_BMAP:		return (int)XFS_BMAP_BLOCK_IMAXRECS(INT_GET(block->bb_h.bb_level, ARCH_CONVERT), cur);	case XFS_BTNUM_INO:		return (int)XFS_INOBT_BLOCK_MAXRECS(INT_GET(block->bb_h.bb_level, ARCH_CONVERT), cur);	default:		ASSERT(0);		return 0;	}}/* * External routines. */#ifdef DEBUG/* * Debug routine: check that block header is ok. */voidxfs_btree_check_block(	xfs_btree_cur_t		*cur,	/* btree cursor */	xfs_btree_block_t	*block,	/* generic btree block pointer */	int			level,	/* level of the btree block */	xfs_buf_t		*bp)	/* buffer containing block, if any */{	if (XFS_BTREE_LONG_PTRS(cur->bc_btnum))		xfs_btree_check_lblock(cur, (xfs_btree_lblock_t *)block, level,			bp);	else		xfs_btree_check_sblock(cur, (xfs_btree_sblock_t *)block, level,			bp);}/* * Debug routine: check that keys are in the right order. */voidxfs_btree_check_key(	xfs_btnum_t	btnum,		/* btree identifier */	void		*ak1,		/* pointer to left (lower) key */	void		*ak2)		/* pointer to right (higher) key */{	switch (btnum) {	case XFS_BTNUM_BNO: {		xfs_alloc_key_t	*k1;		xfs_alloc_key_t	*k2;		k1 = ak1;		k2 = ak2;		ASSERT(INT_GET(k1->ar_startblock, ARCH_CONVERT) < INT_GET(k2->ar_startblock, ARCH_CONVERT));		break;	    }	case XFS_BTNUM_CNT: {		xfs_alloc_key_t	*k1;		xfs_alloc_key_t	*k2;		k1 = ak1;		k2 = ak2;		ASSERT(INT_GET(k1->ar_blockcount, ARCH_CONVERT) < INT_GET(k2->ar_blockcount, ARCH_CONVERT) ||		       (INT_GET(k1->ar_blockcount, ARCH_CONVERT) == INT_GET(k2->ar_blockcount, ARCH_CONVERT) &&			INT_GET(k1->ar_startblock, ARCH_CONVERT) < INT_GET(k2->ar_startblock, ARCH_CONVERT)));		break;	    }	case XFS_BTNUM_BMAP: {		xfs_bmbt_key_t	*k1;		xfs_bmbt_key_t	*k2;		k1 = ak1;		k2 = ak2;		ASSERT(INT_GET(k1->br_startoff, ARCH_CONVERT) < INT_GET(k2->br_startoff, ARCH_CONVERT));		break;	    }	case XFS_BTNUM_INO: {		xfs_inobt_key_t	*k1;		xfs_inobt_key_t	*k2;		k1 = ak1;		k2 = ak2;		ASSERT(INT_GET(k1->ir_startino, ARCH_CONVERT) < INT_GET(k2->ir_startino, ARCH_CONVERT));		break;	    }	default:		ASSERT(0);	}}#endif	/* DEBUG *//* * Checking routine: check that long form block header is ok. *//* ARGSUSED */int					/* error (0 or EFSCORRUPTED) */xfs_btree_check_lblock(	xfs_btree_cur_t		*cur,	/* btree cursor */	xfs_btree_lblock_t	*block,	/* btree long form block pointer */	int			level,	/* level of the btree block */	xfs_buf_t		*bp)	/* buffer for block, if any */{	int			lblock_ok; /* block passes checks */	xfs_mount_t		*mp;	/* file system mount point */	mp = cur->bc_mp;	lblock_ok =		INT_GET(block->bb_magic, ARCH_CONVERT) == xfs_magics[cur->bc_btnum] &&		INT_GET(block->bb_level, ARCH_CONVERT) == level &&		INT_GET(block->bb_numrecs, ARCH_CONVERT) <=			xfs_btree_maxrecs(cur, (xfs_btree_block_t *)block) &&		!INT_ISZERO(block->bb_leftsib, ARCH_CONVERT) &&		(INT_GET(block->bb_leftsib, ARCH_CONVERT) == NULLDFSBNO ||		 XFS_FSB_SANITY_CHECK(mp, INT_GET(block->bb_leftsib, ARCH_CONVERT))) &&		!INT_ISZERO(block->bb_rightsib, ARCH_CONVERT) &&		(INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLDFSBNO ||		 XFS_FSB_SANITY_CHECK(mp, INT_GET(block->bb_rightsib, ARCH_CONVERT)));	if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp, XFS_ERRTAG_BTREE_CHECK_LBLOCK,			XFS_RANDOM_BTREE_CHECK_LBLOCK))) {		if (bp)			xfs_buftrace("LBTREE ERROR", bp);		XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW,				 mp);		return XFS_ERROR(EFSCORRUPTED);	}	return 0;}/* * Checking routine: check that (long) pointer is ok. */int					/* error (0 or EFSCORRUPTED) */xfs_btree_check_lptr(	xfs_btree_cur_t	*cur,		/* btree cursor */	xfs_dfsbno_t	ptr,		/* btree block disk address */	int		level)		/* btree block level */{	xfs_mount_t	*mp;		/* file system mount point */	mp = cur->bc_mp;	XFS_WANT_CORRUPTED_RETURN(		level > 0 &&		ptr != NULLDFSBNO &&		XFS_FSB_SANITY_CHECK(mp, ptr));	return 0;}#ifdef DEBUG/* * Debug routine: check that records are in the right order. */voidxfs_btree_check_rec(	xfs_btnum_t	btnum,		/* btree identifier */	void		*ar1,		/* pointer to left (lower) record */	void		*ar2)		/* pointer to right (higher) record */{	switch (btnum) {	case XFS_BTNUM_BNO: {		xfs_alloc_rec_t	*r1;		xfs_alloc_rec_t	*r2;		r1 = ar1;		r2 = ar2;		ASSERT(INT_GET(r1->ar_startblock, ARCH_CONVERT) + INT_GET(r1->ar_blockcount, ARCH_CONVERT) <=		       INT_GET(r2->ar_startblock, ARCH_CONVERT));		break;	    }	case XFS_BTNUM_CNT: {		xfs_alloc_rec_t	*r1;		xfs_alloc_rec_t	*r2;		r1 = ar1;		r2 = ar2;		ASSERT(INT_GET(r1->ar_blockcount, ARCH_CONVERT) < INT_GET(r2->ar_blockcount, ARCH_CONVERT) ||		       (INT_GET(r1->ar_blockcount, ARCH_CONVERT) == INT_GET(r2->ar_blockcount, ARCH_CONVERT) &&			INT_GET(r1->ar_startblock, ARCH_CONVERT) < INT_GET(r2->ar_startblock, ARCH_CONVERT)));		break;	    }	case XFS_BTNUM_BMAP: {		xfs_bmbt_rec_t	*r1;		xfs_bmbt_rec_t	*r2;		r1 = ar1;		r2 = ar2;		ASSERT(xfs_bmbt_disk_get_startoff(r1) +		       xfs_bmbt_disk_get_blockcount(r1) <=		       xfs_bmbt_disk_get_startoff(r2));		break;	    }	case XFS_BTNUM_INO: {		xfs_inobt_rec_t	*r1;		xfs_inobt_rec_t	*r2;		r1 = ar1;		r2 = ar2;		ASSERT(INT_GET(r1->ir_startino, ARCH_CONVERT) + XFS_INODES_PER_CHUNK <=		       INT_GET(r2->ir_startino, ARCH_CONVERT));		break;	    }	default:		ASSERT(0);	}}#endif	/* DEBUG *//* * Checking routine: check that block header is ok. *//* ARGSUSED */int					/* error (0 or EFSCORRUPTED) */xfs_btree_check_sblock(	xfs_btree_cur_t		*cur,	/* btree cursor */	xfs_btree_sblock_t	*block,	/* btree short form block pointer */	int			level,	/* level of the btree block */	xfs_buf_t		*bp)	/* buffer containing block */{	xfs_buf_t		*agbp;	/* buffer for ag. freespace struct */	xfs_agf_t		*agf;	/* ag. freespace structure */	xfs_agblock_t		agflen;	/* native ag. freespace length */	int			sblock_ok; /* block passes checks */	agbp = cur->bc_private.a.agbp;	agf = XFS_BUF_TO_AGF(agbp);	agflen = INT_GET(agf->agf_length, ARCH_CONVERT);	sblock_ok =		INT_GET(block->bb_magic, ARCH_CONVERT) == xfs_magics[cur->bc_btnum] &&		INT_GET(block->bb_level, ARCH_CONVERT) == level &&		INT_GET(block->bb_numrecs, ARCH_CONVERT) <=			xfs_btree_maxrecs(cur, (xfs_btree_block_t *)block) &&		(INT_GET(block->bb_leftsib, ARCH_CONVERT) == NULLAGBLOCK ||		 INT_GET(block->bb_leftsib, ARCH_CONVERT) < agflen) &&		!INT_ISZERO(block->bb_leftsib, ARCH_CONVERT) &&		(INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK ||		 INT_GET(block->bb_rightsib, ARCH_CONVERT) < agflen) &&		!INT_ISZERO(block->bb_rightsib, ARCH_CONVERT);	if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp,			XFS_ERRTAG_BTREE_CHECK_SBLOCK,			XFS_RANDOM_BTREE_CHECK_SBLOCK))) {		if (bp)			xfs_buftrace("SBTREE ERROR", bp);		XFS_ERROR_REPORT("xfs_btree_check_sblock", XFS_ERRLEVEL_LOW,				 cur->bc_mp);		return XFS_ERROR(EFSCORRUPTED);	}	return 0;}/* * Checking routine: check that (short) pointer is ok. */int					/* error (0 or EFSCORRUPTED) */xfs_btree_check_sptr(	xfs_btree_cur_t	*cur,		/* btree cursor */	xfs_agblock_t	ptr,		/* btree block disk address */	int		level)		/* btree block level */{	xfs_buf_t	*agbp;		/* buffer for ag. freespace struct */	xfs_agf_t	*agf;		/* ag. freespace structure */	agbp = cur->bc_private.a.agbp;	agf = XFS_BUF_TO_AGF(agbp);	XFS_WANT_CORRUPTED_RETURN(		level > 0 &&		ptr != NULLAGBLOCK && ptr != 0 &&		ptr < INT_GET(agf->agf_length, ARCH_CONVERT));	return 0;}/* * Delete the btree cursor. */voidxfs_btree_del_cursor(	xfs_btree_cur_t	*cur,		/* btree cursor */	int		error)		/* del because of error */{	int		i;		/* btree level */	/*	 * Clear the buffer pointers, and release the buffers.	 * If we're doing this in the face of an error, we	 * need to make sure to inspect all of the entries	 * in the bc_bufs array for buffers to be unlocked.	 * This is because some of the btree code works from	 * level n down to 0, and if we get an error along	 * the way we won't have initialized all the entries	 * down to 0.	 */	for (i = 0; i < cur->bc_nlevels; i++) {		if (cur->bc_bufs[i])			xfs_btree_setbuf(cur, i, NULL);		else if (!error)			break;	}	/*	 * Can't free a bmap cursor without having dealt with the	 * allocated indirect blocks' accounting.	 */	ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||	       cur->bc_private.b.allocated == 0);	/*	 * Free the cursor.	 */	kmem_zone_free(xfs_btree_cur_zone, cur);}/* * Duplicate the btree cursor. * Allocate a new one, copy the record, re-get the buffers. */int					/* error */xfs_btree_dup_cursor(	xfs_btree_cur_t	*cur,		/* input cursor */	xfs_btree_cur_t	**ncur)		/* output cursor */{	xfs_buf_t	*bp;		/* btree block's buffer pointer */	int		error;		/* error return value */	int		i;		/* level number of btree block */	xfs_mount_t	*mp;		/* mount structure for filesystem */	xfs_btree_cur_t	*new;		/* new cursor value */	xfs_trans_t	*tp;		/* transaction pointer, can be NULL */	tp = cur->bc_tp;	mp = cur->bc_mp;	/*	 * Allocate a new cursor like the old one.	 */	new = xfs_btree_init_cursor(mp, tp, cur->bc_private.a.agbp,		cur->bc_private.a.agno, cur->bc_btnum, cur->bc_private.b.ip,		cur->bc_private.b.whichfork);	/*	 * Copy the record currently in the cursor.	 */	new->bc_rec = cur->bc_rec;	/*	 * For each level current, re-get the buffer and copy the ptr value.	 */	for (i = 0; i < new->bc_nlevels; i++) {		new->bc_ptrs[i] = cur->bc_ptrs[i];		new->bc_ra[i] = cur->bc_ra[i];		if ((bp = cur->bc_bufs[i])) {			if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,				XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) {				xfs_btree_del_cursor(new, error);				*ncur = NULL;				return error;			}			new->bc_bufs[i] = bp;			ASSERT(bp);			ASSERT(!XFS_BUF_GETERROR(bp));		} else			new->bc_bufs[i] = NULL;	}	/*	 * For bmap btrees, copy the firstblock, flist, and flags values,	 * since init cursor doesn't get them.	 */	if (new->bc_btnum == XFS_BTNUM_BMAP) {		new->bc_private.b.firstblock = cur->bc_private.b.firstblock;		new->bc_private.b.flist = cur->bc_private.b.flist;		new->bc_private.b.flags = cur->bc_private.b.flags;	}	*ncur = new;	return 0;}/* * Change the cursor to point to the first record at the given level. * Other levels are unaffected. */int					/* success=1, failure=0 */xfs_btree_firstrec(	xfs_btree_cur_t		*cur,	/* btree cursor */	int			level)	/* level to change */{

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?