xfs_ialloc_btree.c
来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 2,103 行 · 第 1/5 页
C
2,103 行
/* * Copyright (c) 2000-2001 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/ */#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_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_alloc.h"#include "xfs_error.h"/* * Inode allocation management for XFS. *//* * Prototypes for internal functions. */STATIC void xfs_inobt_log_block(xfs_trans_t *, xfs_buf_t *, int);STATIC void xfs_inobt_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);STATIC void xfs_inobt_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);STATIC void xfs_inobt_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int);STATIC int xfs_inobt_lshift(xfs_btree_cur_t *, int, int *);STATIC int xfs_inobt_newroot(xfs_btree_cur_t *, int *);STATIC int xfs_inobt_rshift(xfs_btree_cur_t *, int, int *);STATIC int xfs_inobt_split(xfs_btree_cur_t *, int, xfs_agblock_t *, xfs_inobt_key_t *, xfs_btree_cur_t **, int *);STATIC int xfs_inobt_updkey(xfs_btree_cur_t *, xfs_inobt_key_t *, int);/* * Internal functions. *//* * Single level of the xfs_inobt_delete record deletion routine. * Delete record pointed to by cur/level. * Remove the record from its block then rebalance the tree. * Return 0 for error, 1 for done, 2 to go on to the next level. */STATIC int /* error */xfs_inobt_delrec( xfs_btree_cur_t *cur, /* btree cursor */ int level, /* level removing record from */ int *stat) /* fail/done/go-on */{ xfs_buf_t *agbp; /* buffer for a.g. inode header */ xfs_mount_t *mp; /* mount structure */ xfs_agi_t *agi; /* allocation group inode header */ xfs_inobt_block_t *block; /* btree block record/key lives in */ xfs_agblock_t bno; /* btree block number */ xfs_buf_t *bp; /* buffer for block */ int error; /* error return value */ int i; /* loop index */ xfs_inobt_key_t key; /* kp points here if block is level 0 */ xfs_inobt_key_t *kp = NULL; /* pointer to btree keys */ xfs_agblock_t lbno; /* left block's block number */ xfs_buf_t *lbp; /* left block's buffer pointer */ xfs_inobt_block_t *left; /* left btree block */ xfs_inobt_key_t *lkp; /* left block key pointer */ xfs_inobt_ptr_t *lpp; /* left block address pointer */ int lrecs = 0; /* number of records in left block */ xfs_inobt_rec_t *lrp; /* left block record pointer */ xfs_inobt_ptr_t *pp = NULL; /* pointer to btree addresses */ int ptr; /* index in btree block for this rec */ xfs_agblock_t rbno; /* right block's block number */ xfs_buf_t *rbp; /* right block's buffer pointer */ xfs_inobt_block_t *right; /* right btree block */ xfs_inobt_key_t *rkp; /* right block key pointer */ xfs_inobt_rec_t *rp; /* pointer to btree records */ xfs_inobt_ptr_t *rpp; /* right block address pointer */ int rrecs = 0; /* number of records in right block */ int numrecs; xfs_inobt_rec_t *rrp; /* right block record pointer */ xfs_btree_cur_t *tcur; /* temporary btree cursor */ mp = cur->bc_mp; /* * Get the index of the entry being deleted, check for nothing there. */ ptr = cur->bc_ptrs[level]; if (ptr == 0) { *stat = 0; return 0; } /* * Get the buffer & block containing the record or key/ptr. */ bp = cur->bc_bufs[level]; block = XFS_BUF_TO_INOBT_BLOCK(bp);#ifdef DEBUG if ((error = xfs_btree_check_sblock(cur, block, level, bp))) return error;#endif /* * Fail if we're off the end of the block. */ numrecs = INT_GET(block->bb_numrecs, ARCH_CONVERT); if (ptr > numrecs) { *stat = 0; return 0; } /* * It's a nonleaf. Excise the key and ptr being deleted, by * sliding the entries past them down one. * Log the changed areas of the block. */ if (level > 0) { kp = XFS_INOBT_KEY_ADDR(block, 1, cur); pp = XFS_INOBT_PTR_ADDR(block, 1, cur);#ifdef DEBUG for (i = ptr; i < numrecs; i++) { if ((error = xfs_btree_check_sptr(cur, INT_GET(pp[i], ARCH_CONVERT), level))) return error; }#endif if (ptr < numrecs) { memmove(&kp[ptr - 1], &kp[ptr], (numrecs - ptr) * sizeof(*kp)); memmove(&pp[ptr - 1], &pp[ptr], (numrecs - ptr) * sizeof(*kp)); xfs_inobt_log_keys(cur, bp, ptr, numrecs - 1); xfs_inobt_log_ptrs(cur, bp, ptr, numrecs - 1); } } /* * It's a leaf. Excise the record being deleted, by sliding the * entries past it down one. Log the changed areas of the block. */ else { rp = XFS_INOBT_REC_ADDR(block, 1, cur); if (ptr < numrecs) { memmove(&rp[ptr - 1], &rp[ptr], (numrecs - ptr) * sizeof(*rp)); xfs_inobt_log_recs(cur, bp, ptr, numrecs - 1); } /* * If it's the first record in the block, we'll need a key * structure to pass up to the next level (updkey). */ if (ptr == 1) { key.ir_startino = rp->ir_startino; kp = &key; } } /* * Decrement and log the number of entries in the block. */ numrecs--; INT_SET(block->bb_numrecs, ARCH_CONVERT, numrecs); xfs_inobt_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS); /* * Is this the root level? If so, we're almost done. */ if (level == cur->bc_nlevels - 1) { /* * If this is the root level, * and there's only one entry left, * and it's NOT the leaf level, * then we can get rid of this level. */ if (numrecs == 1 && level > 0) { agbp = cur->bc_private.i.agbp; agi = XFS_BUF_TO_AGI(agbp); /* * pp is still set to the first pointer in the block. * Make it the new root of the btree. */ bno = INT_GET(agi->agi_root, ARCH_CONVERT); agi->agi_root = *pp; INT_MOD(agi->agi_level, ARCH_CONVERT, -1); /* * Free the block. */ if ((error = xfs_free_extent(cur->bc_tp, XFS_AGB_TO_FSB(mp, cur->bc_private.i.agno, bno), 1))) return error; xfs_trans_binval(cur->bc_tp, bp); xfs_ialloc_log_agi(cur->bc_tp, agbp, XFS_AGI_ROOT | XFS_AGI_LEVEL); /* * Update the cursor so there's one fewer level. */ cur->bc_bufs[level] = NULL; cur->bc_nlevels--; } else if (level > 0 && (error = xfs_inobt_decrement(cur, level, &i))) return error; *stat = 1; return 0; } /* * If we deleted the leftmost entry in the block, update the * key values above us in the tree. */ if (ptr == 1 && (error = xfs_inobt_updkey(cur, kp, level + 1))) return error; /* * If the number of records remaining in the block is at least * the minimum, we're done. */ if (numrecs >= XFS_INOBT_BLOCK_MINRECS(level, cur)) { if (level > 0 && (error = xfs_inobt_decrement(cur, level, &i))) return error; *stat = 1; return 0; } /* * Otherwise, we have to move some records around to keep the * tree balanced. Look at the left and right sibling blocks to * see if we can re-balance by moving only one record. */ rbno = INT_GET(block->bb_rightsib, ARCH_CONVERT); lbno = INT_GET(block->bb_leftsib, ARCH_CONVERT); bno = NULLAGBLOCK; ASSERT(rbno != NULLAGBLOCK || lbno != NULLAGBLOCK); /* * Duplicate the cursor so our btree manipulations here won't * disrupt the next level up. */ if ((error = xfs_btree_dup_cursor(cur, &tcur))) return error; /* * If there's a right sibling, see if it's ok to shift an entry * out of it. */ if (rbno != NULLAGBLOCK) { /* * Move the temp cursor to the last entry in the next block. * Actually any entry but the first would suffice. */ i = xfs_btree_lastrec(tcur, level); XFS_WANT_CORRUPTED_GOTO(i == 1, error0); if ((error = xfs_inobt_increment(tcur, level, &i))) goto error0; XFS_WANT_CORRUPTED_GOTO(i == 1, error0); i = xfs_btree_lastrec(tcur, level); XFS_WANT_CORRUPTED_GOTO(i == 1, error0); /* * Grab a pointer to the block. */ rbp = tcur->bc_bufs[level]; right = XFS_BUF_TO_INOBT_BLOCK(rbp);#ifdef DEBUG if ((error = xfs_btree_check_sblock(cur, right, level, rbp))) goto error0;#endif /* * Grab the current block number, for future use. */ bno = INT_GET(right->bb_leftsib, ARCH_CONVERT); /* * If right block is full enough so that removing one entry * won't make it too empty, and left-shifting an entry out * of right to us works, we're done. */ if (INT_GET(right->bb_numrecs, ARCH_CONVERT) - 1 >= XFS_INOBT_BLOCK_MINRECS(level, cur)) { if ((error = xfs_inobt_lshift(tcur, level, &i))) goto error0; if (i) { ASSERT(INT_GET(block->bb_numrecs, ARCH_CONVERT) >= XFS_INOBT_BLOCK_MINRECS(level, cur)); xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); if (level > 0 && (error = xfs_inobt_decrement(cur, level, &i))) return error; *stat = 1; return 0; } } /* * Otherwise, grab the number of records in right for * future reference, and fix up the temp cursor to point * to our block again (last record). */ rrecs = INT_GET(right->bb_numrecs, ARCH_CONVERT); if (lbno != NULLAGBLOCK) { xfs_btree_firstrec(tcur, level); if ((error = xfs_inobt_decrement(tcur, level, &i))) goto error0; } } /* * If there's a left sibling, see if it's ok to shift an entry * out of it. */ if (lbno != NULLAGBLOCK) { /* * Move the temp cursor to the first entry in the * previous block. */ xfs_btree_firstrec(tcur, level); if ((error = xfs_inobt_decrement(tcur, level, &i))) goto error0; xfs_btree_firstrec(tcur, level); /* * Grab a pointer to the block. */ lbp = tcur->bc_bufs[level]; left = XFS_BUF_TO_INOBT_BLOCK(lbp);#ifdef DEBUG if ((error = xfs_btree_check_sblock(cur, left, level, lbp))) goto error0;#endif /* * Grab the current block number, for future use. */ bno = INT_GET(left->bb_rightsib, ARCH_CONVERT); /* * If left block is full enough so that removing one entry * won't make it too empty, and right-shifting an entry out * of left to us works, we're done. */ if (INT_GET(left->bb_numrecs, ARCH_CONVERT) - 1 >= XFS_INOBT_BLOCK_MINRECS(level, cur)) { if ((error = xfs_inobt_rshift(tcur, level, &i))) goto error0; if (i) { ASSERT(INT_GET(block->bb_numrecs, ARCH_CONVERT) >= XFS_INOBT_BLOCK_MINRECS(level, cur)); xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); if (level == 0) cur->bc_ptrs[0]++; *stat = 1; return 0; } } /* * Otherwise, grab the number of records in right for * future reference. */ lrecs = INT_GET(left->bb_numrecs, ARCH_CONVERT); } /* * Delete the temp cursor, we're done with it. */ xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); /* * If here, we need to do a join to keep the tree balanced. */ ASSERT(bno != NULLAGBLOCK); /* * See if we can join with the left neighbor block. */ if (lbno != NULLAGBLOCK && lrecs + numrecs <= XFS_INOBT_BLOCK_MAXRECS(level, cur)) { /* * Set "right" to be the starting block, * "left" to be the left neighbor. */ rbno = bno; right = block; rrecs = INT_GET(right->bb_numrecs, ARCH_CONVERT); rbp = bp; if ((error = xfs_btree_read_bufs(mp, cur->bc_tp, cur->bc_private.i.agno, lbno, 0, &lbp, XFS_INO_BTREE_REF))) return error; left = XFS_BUF_TO_INOBT_BLOCK(lbp); lrecs = INT_GET(left->bb_numrecs, ARCH_CONVERT); if ((error = xfs_btree_check_sblock(cur, left, level, lbp))) return error; } /* * If that won't work, see if we can join with the right neighbor block. */ else if (rbno != NULLAGBLOCK && rrecs + numrecs <= XFS_INOBT_BLOCK_MAXRECS(level, cur)) { /* * Set "left" to be the starting block, * "right" to be the right neighbor. */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?