⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xfs_da_btree.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/* * Copyright (c) 2000-2005 Silicon Graphics, Inc. * All Rights Reserved. * * This program is free software; you can redistribute it and/or * modify it under the terms 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.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write the Free Software Foundation, * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */#include "xfs.h"#include "xfs_fs.h"#include "xfs_types.h"#include "xfs_bit.h"#include "xfs_log.h"#include "xfs_inum.h"#include "xfs_trans.h"#include "xfs_sb.h"#include "xfs_ag.h"#include "xfs_dir2.h"#include "xfs_dmapi.h"#include "xfs_mount.h"#include "xfs_da_btree.h"#include "xfs_bmap_btree.h"#include "xfs_alloc_btree.h"#include "xfs_ialloc_btree.h"#include "xfs_dir2_sf.h"#include "xfs_attr_sf.h"#include "xfs_dinode.h"#include "xfs_inode.h"#include "xfs_inode_item.h"#include "xfs_alloc.h"#include "xfs_btree.h"#include "xfs_bmap.h"#include "xfs_attr.h"#include "xfs_attr_leaf.h"#include "xfs_dir2_data.h"#include "xfs_dir2_leaf.h"#include "xfs_dir2_block.h"#include "xfs_dir2_node.h"#include "xfs_error.h"/* * xfs_da_btree.c * * Routines to implement directories as Btrees of hashed names. *//*======================================================================== * Function prototypes for the kernel. *========================================================================*//* * Routines used for growing the Btree. */STATIC int xfs_da_root_split(xfs_da_state_t *state,					    xfs_da_state_blk_t *existing_root,					    xfs_da_state_blk_t *new_child);STATIC int xfs_da_node_split(xfs_da_state_t *state,					    xfs_da_state_blk_t *existing_blk,					    xfs_da_state_blk_t *split_blk,					    xfs_da_state_blk_t *blk_to_add,					    int treelevel,					    int *result);STATIC void xfs_da_node_rebalance(xfs_da_state_t *state,					 xfs_da_state_blk_t *node_blk_1,					 xfs_da_state_blk_t *node_blk_2);STATIC void xfs_da_node_add(xfs_da_state_t *state,				   xfs_da_state_blk_t *old_node_blk,				   xfs_da_state_blk_t *new_node_blk);/* * Routines used for shrinking the Btree. */STATIC int xfs_da_root_join(xfs_da_state_t *state,					   xfs_da_state_blk_t *root_blk);STATIC int xfs_da_node_toosmall(xfs_da_state_t *state, int *retval);STATIC void xfs_da_node_remove(xfs_da_state_t *state,					      xfs_da_state_blk_t *drop_blk);STATIC void xfs_da_node_unbalance(xfs_da_state_t *state,					 xfs_da_state_blk_t *src_node_blk,					 xfs_da_state_blk_t *dst_node_blk);/* * Utility routines. */STATIC uint	xfs_da_node_lasthash(xfs_dabuf_t *bp, int *count);STATIC int	xfs_da_node_order(xfs_dabuf_t *node1_bp, xfs_dabuf_t *node2_bp);STATIC xfs_dabuf_t *xfs_da_buf_make(int nbuf, xfs_buf_t **bps, inst_t *ra);STATIC int	xfs_da_blk_unlink(xfs_da_state_t *state,				  xfs_da_state_blk_t *drop_blk,				  xfs_da_state_blk_t *save_blk);STATIC void	xfs_da_state_kill_altpath(xfs_da_state_t *state);/*======================================================================== * Routines used for growing the Btree. *========================================================================*//* * Create the initial contents of an intermediate node. */intxfs_da_node_create(xfs_da_args_t *args, xfs_dablk_t blkno, int level,				 xfs_dabuf_t **bpp, int whichfork){	xfs_da_intnode_t *node;	xfs_dabuf_t *bp;	int error;	xfs_trans_t *tp;	tp = args->trans;	error = xfs_da_get_buf(tp, args->dp, blkno, -1, &bp, whichfork);	if (error)		return(error);	ASSERT(bp != NULL);	node = bp->data;	node->hdr.info.forw = 0;	node->hdr.info.back = 0;	node->hdr.info.magic = cpu_to_be16(XFS_DA_NODE_MAGIC);	node->hdr.info.pad = 0;	node->hdr.count = 0;	node->hdr.level = cpu_to_be16(level);	xfs_da_log_buf(tp, bp,		XFS_DA_LOGRANGE(node, &node->hdr, sizeof(node->hdr)));	*bpp = bp;	return(0);}/* * Split a leaf node, rebalance, then possibly split * intermediate nodes, rebalance, etc. */int							/* error */xfs_da_split(xfs_da_state_t *state){	xfs_da_state_blk_t *oldblk, *newblk, *addblk;	xfs_da_intnode_t *node;	xfs_dabuf_t *bp;	int max, action, error, i;	/*	 * Walk back up the tree splitting/inserting/adjusting as necessary.	 * If we need to insert and there isn't room, split the node, then	 * decide which fragment to insert the new block from below into.	 * Note that we may split the root this way, but we need more fixup.	 */	max = state->path.active - 1;	ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH));	ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC ||	       state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);	addblk = &state->path.blk[max];		/* initial dummy value */	for (i = max; (i >= 0) && addblk; state->path.active--, i--) {		oldblk = &state->path.blk[i];		newblk = &state->altpath.blk[i];		/*		 * If a leaf node then		 *     Allocate a new leaf node, then rebalance across them.		 * else if an intermediate node then		 *     We split on the last layer, must we split the node?		 */		switch (oldblk->magic) {		case XFS_ATTR_LEAF_MAGIC:			error = xfs_attr_leaf_split(state, oldblk, newblk);			if ((error != 0) && (error != ENOSPC)) {				return(error);	/* GROT: attr is inconsistent */			}			if (!error) {				addblk = newblk;				break;			}			/*			 * Entry wouldn't fit, split the leaf again.			 */			state->extravalid = 1;			if (state->inleaf) {				state->extraafter = 0;	/* before newblk */				error = xfs_attr_leaf_split(state, oldblk,							    &state->extrablk);			} else {				state->extraafter = 1;	/* after newblk */				error = xfs_attr_leaf_split(state, newblk,							    &state->extrablk);			}			if (error)				return(error);	/* GROT: attr inconsistent */			addblk = newblk;			break;		case XFS_DIR2_LEAFN_MAGIC:			error = xfs_dir2_leafn_split(state, oldblk, newblk);			if (error)				return error;			addblk = newblk;			break;		case XFS_DA_NODE_MAGIC:			error = xfs_da_node_split(state, oldblk, newblk, addblk,							 max - i, &action);			xfs_da_buf_done(addblk->bp);			addblk->bp = NULL;			if (error)				return(error);	/* GROT: dir is inconsistent */			/*			 * Record the newly split block for the next time thru?			 */			if (action)				addblk = newblk;			else				addblk = NULL;			break;		}		/*		 * Update the btree to show the new hashval for this child.		 */		xfs_da_fixhashpath(state, &state->path);		/*		 * If we won't need this block again, it's getting dropped		 * from the active path by the loop control, so we need		 * to mark it done now.		 */		if (i > 0 || !addblk)			xfs_da_buf_done(oldblk->bp);	}	if (!addblk)		return(0);	/*	 * Split the root node.	 */	ASSERT(state->path.active == 0);	oldblk = &state->path.blk[0];	error = xfs_da_root_split(state, oldblk, addblk);	if (error) {		xfs_da_buf_done(oldblk->bp);		xfs_da_buf_done(addblk->bp);		addblk->bp = NULL;		return(error);	/* GROT: dir is inconsistent */	}	/*	 * Update pointers to the node which used to be block 0 and	 * just got bumped because of the addition of a new root node.	 * There might be three blocks involved if a double split occurred,	 * and the original block 0 could be at any position in the list.	 */	node = oldblk->bp->data;	if (node->hdr.info.forw) {		if (be32_to_cpu(node->hdr.info.forw) == addblk->blkno) {			bp = addblk->bp;		} else {			ASSERT(state->extravalid);			bp = state->extrablk.bp;		}		node = bp->data;		node->hdr.info.back = cpu_to_be32(oldblk->blkno);		xfs_da_log_buf(state->args->trans, bp,		    XFS_DA_LOGRANGE(node, &node->hdr.info,		    sizeof(node->hdr.info)));	}	node = oldblk->bp->data;	if (node->hdr.info.back) {		if (be32_to_cpu(node->hdr.info.back) == addblk->blkno) {			bp = addblk->bp;		} else {			ASSERT(state->extravalid);			bp = state->extrablk.bp;		}		node = bp->data;		node->hdr.info.forw = cpu_to_be32(oldblk->blkno);		xfs_da_log_buf(state->args->trans, bp,		    XFS_DA_LOGRANGE(node, &node->hdr.info,		    sizeof(node->hdr.info)));	}	xfs_da_buf_done(oldblk->bp);	xfs_da_buf_done(addblk->bp);	addblk->bp = NULL;	return(0);}/* * Split the root.  We have to create a new root and point to the two * parts (the split old root) that we just created.  Copy block zero to * the EOF, extending the inode in process. */STATIC int						/* error */xfs_da_root_split(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,				 xfs_da_state_blk_t *blk2){	xfs_da_intnode_t *node, *oldroot;	xfs_da_args_t *args;	xfs_dablk_t blkno;	xfs_dabuf_t *bp;	int error, size;	xfs_inode_t *dp;	xfs_trans_t *tp;	xfs_mount_t *mp;	xfs_dir2_leaf_t *leaf;	/*	 * Copy the existing (incorrect) block from the root node position	 * to a free space somewhere.	 */	args = state->args;	ASSERT(args != NULL);	error = xfs_da_grow_inode(args, &blkno);	if (error)		return(error);	dp = args->dp;	tp = args->trans;	mp = state->mp;	error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, args->whichfork);	if (error)		return(error);	ASSERT(bp != NULL);	node = bp->data;	oldroot = blk1->bp->data;	if (be16_to_cpu(oldroot->hdr.info.magic) == XFS_DA_NODE_MAGIC) {		size = (int)((char *)&oldroot->btree[be16_to_cpu(oldroot->hdr.count)] -			     (char *)oldroot);	} else {		ASSERT(be16_to_cpu(oldroot->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);		leaf = (xfs_dir2_leaf_t *)oldroot;		size = (int)((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] -			     (char *)leaf);	}	memcpy(node, oldroot, size);	xfs_da_log_buf(tp, bp, 0, size - 1);	xfs_da_buf_done(blk1->bp);	blk1->bp = bp;	blk1->blkno = blkno;	/*	 * Set up the new root node.	 */	error = xfs_da_node_create(args,		(args->whichfork == XFS_DATA_FORK) ? mp->m_dirleafblk : 0,		be16_to_cpu(node->hdr.level) + 1, &bp, args->whichfork);	if (error)		return(error);	node = bp->data;	node->btree[0].hashval = cpu_to_be32(blk1->hashval);	node->btree[0].before = cpu_to_be32(blk1->blkno);	node->btree[1].hashval = cpu_to_be32(blk2->hashval);	node->btree[1].before = cpu_to_be32(blk2->blkno);	node->hdr.count = cpu_to_be16(2);#ifdef DEBUG	if (be16_to_cpu(oldroot->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC) {		ASSERT(blk1->blkno >= mp->m_dirleafblk &&		       blk1->blkno < mp->m_dirfreeblk);		ASSERT(blk2->blkno >= mp->m_dirleafblk &&		       blk2->blkno < mp->m_dirfreeblk);	}#endif	/* Header is already logged by xfs_da_node_create */	xfs_da_log_buf(tp, bp,		XFS_DA_LOGRANGE(node, node->btree,			sizeof(xfs_da_node_entry_t) * 2));	xfs_da_buf_done(bp);	return(0);}/* * Split the node, rebalance, then add the new entry. */STATIC int						/* error */xfs_da_node_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,				 xfs_da_state_blk_t *newblk,				 xfs_da_state_blk_t *addblk,				 int treelevel, int *result){	xfs_da_intnode_t *node;	xfs_dablk_t blkno;	int newcount, error;	int useextra;	node = oldblk->bp->data;	ASSERT(be16_to_cpu(node->hdr.info.magic) == XFS_DA_NODE_MAGIC);	/*	 * With V2 dirs the extra block is data or freespace.	 */	useextra = state->extravalid && state->args->whichfork == XFS_ATTR_FORK;	newcount = 1 + useextra;	/*	 * Do we have to split the node?	 */	if ((be16_to_cpu(node->hdr.count) + newcount) > state->node_ents) {		/*		 * Allocate a new node, add to the doubly linked chain of		 * nodes, then move some of our excess entries into it.		 */		error = xfs_da_grow_inode(state->args, &blkno);		if (error)			return(error);	/* GROT: dir is inconsistent */		error = xfs_da_node_create(state->args, blkno, treelevel,					   &newblk->bp, state->args->whichfork);		if (error)			return(error);	/* GROT: dir is inconsistent */		newblk->blkno = blkno;		newblk->magic = XFS_DA_NODE_MAGIC;		xfs_da_node_rebalance(state, oldblk, newblk);		error = xfs_da_blk_link(state, oldblk, newblk);		if (error)			return(error);		*result = 1;	} else {		*result = 0;	}	/*	 * Insert the new entry(s) into the correct block	 * (updating last hashval in the process).	 *	 * xfs_da_node_add() inserts BEFORE the given index,	 * and as a result of using node_lookup_int() we always	 * point to a valid entry (not after one), but a split	 * operation always results in a new block whose hashvals	 * FOLLOW the current block.	 *	 * If we had double-split op below us, then add the extra block too.	 */	node = oldblk->bp->data;	if (oldblk->index <= be16_to_cpu(node->hdr.count)) {		oldblk->index++;		xfs_da_node_add(state, oldblk, addblk);		if (useextra) {			if (state->extraafter)				oldblk->index++;			xfs_da_node_add(state, oldblk, &state->extrablk);			state->extravalid = 0;		}	} else {		newblk->index++;		xfs_da_node_add(state, newblk, addblk);		if (useextra) {			if (state->extraafter)				newblk->index++;			xfs_da_node_add(state, newblk, &state->extrablk);			state->extravalid = 0;		}	}	return(0);}/* * Balance the btree elements between two intermediate nodes, * usually one full and one empty. * * NOTE: if blk2 is empty, then it will get the upper half of blk1. */STATIC voidxfs_da_node_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,				     xfs_da_state_blk_t *blk2){	xfs_da_intnode_t *node1, *node2, *tmpnode;	xfs_da_node_entry_t *btree_s, *btree_d;	int count, tmp;	xfs_trans_t *tp;

⌨️ 快捷键说明

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