2005-04-17 02:20:36 +04:00
|
|
|
/*
|
2005-11-02 06:58:39 +03:00
|
|
|
* Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
|
|
|
|
* All Rights Reserved.
|
2005-04-17 02:20:36 +04:00
|
|
|
*
|
2005-11-02 06:58:39 +03:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
2005-04-17 02:20:36 +04:00
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
2005-11-02 06:58:39 +03:00
|
|
|
* 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.
|
2005-04-17 02:20:36 +04:00
|
|
|
*
|
2005-11-02 06:58:39 +03:00
|
|
|
* 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
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
|
|
|
#include "xfs.h"
|
2005-11-02 06:38:42 +03:00
|
|
|
#include "xfs_fs.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
#include "xfs_types.h"
|
2005-11-02 06:38:42 +03:00
|
|
|
#include "xfs_bit.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
#include "xfs_log.h"
|
2005-11-02 06:38:42 +03:00
|
|
|
#include "xfs_inum.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
#include "xfs_trans.h"
|
|
|
|
#include "xfs_sb.h"
|
|
|
|
#include "xfs_ag.h"
|
2005-11-02 06:38:42 +03:00
|
|
|
#include "xfs_dir2.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
#include "xfs_dmapi.h"
|
|
|
|
#include "xfs_mount.h"
|
2005-11-02 06:38:42 +03:00
|
|
|
#include "xfs_bmap_btree.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
#include "xfs_alloc_btree.h"
|
|
|
|
#include "xfs_ialloc_btree.h"
|
2005-11-02 06:38:42 +03:00
|
|
|
#include "xfs_dir2_sf.h"
|
|
|
|
#include "xfs_attr_sf.h"
|
|
|
|
#include "xfs_dinode.h"
|
|
|
|
#include "xfs_inode.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
#include "xfs_btree.h"
|
2008-10-30 08:57:03 +03:00
|
|
|
#include "xfs_btree_trace.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
#include "xfs_ialloc.h"
|
|
|
|
#include "xfs_alloc.h"
|
|
|
|
#include "xfs_error.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prototypes for internal functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC void xfs_alloc_log_block(xfs_trans_t *, xfs_buf_t *, int);
|
|
|
|
STATIC void xfs_alloc_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
|
|
|
|
STATIC void xfs_alloc_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
|
|
|
|
STATIC void xfs_alloc_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Internal functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Single level of the xfs_alloc_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_alloc_delrec(
|
|
|
|
xfs_btree_cur_t *cur, /* btree cursor */
|
|
|
|
int level, /* level removing record from */
|
|
|
|
int *stat) /* fail/done/go-on */
|
|
|
|
{
|
|
|
|
xfs_agf_t *agf; /* allocation group freelist header */
|
|
|
|
xfs_alloc_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_alloc_key_t key; /* kp points here if block is level 0 */
|
|
|
|
xfs_agblock_t lbno; /* left block's block number */
|
|
|
|
xfs_buf_t *lbp; /* left block's buffer pointer */
|
|
|
|
xfs_alloc_block_t *left; /* left btree block */
|
|
|
|
xfs_alloc_key_t *lkp=NULL; /* left block key pointer */
|
|
|
|
xfs_alloc_ptr_t *lpp=NULL; /* left block address pointer */
|
|
|
|
int lrecs=0; /* number of records in left block */
|
|
|
|
xfs_alloc_rec_t *lrp; /* left block record pointer */
|
|
|
|
xfs_mount_t *mp; /* mount structure */
|
|
|
|
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_alloc_block_t *right; /* right btree block */
|
|
|
|
xfs_alloc_key_t *rkp; /* right block key pointer */
|
|
|
|
xfs_alloc_ptr_t *rpp; /* right block address pointer */
|
|
|
|
int rrecs=0; /* number of records in right block */
|
2006-09-28 05:05:40 +04:00
|
|
|
int numrecs;
|
2005-04-17 02:20:36 +04:00
|
|
|
xfs_alloc_rec_t *rrp; /* right block record pointer */
|
|
|
|
xfs_btree_cur_t *tcur; /* temporary btree cursor */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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_ALLOC_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.
|
|
|
|
*/
|
2006-09-28 05:05:40 +04:00
|
|
|
numrecs = be16_to_cpu(block->bb_numrecs);
|
|
|
|
if (ptr > numrecs) {
|
2005-04-17 02:20:36 +04:00
|
|
|
*stat = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
XFS_STATS_INC(xs_abt_delrec);
|
|
|
|
/*
|
|
|
|
* 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) {
|
|
|
|
lkp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
|
|
|
|
lpp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
|
|
|
|
#ifdef DEBUG
|
2006-09-28 05:05:40 +04:00
|
|
|
for (i = ptr; i < numrecs; i++) {
|
2005-11-02 07:11:25 +03:00
|
|
|
if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
|
2005-04-17 02:20:36 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
#endif
|
2006-09-28 05:05:40 +04:00
|
|
|
if (ptr < numrecs) {
|
2005-04-17 02:20:36 +04:00
|
|
|
memmove(&lkp[ptr - 1], &lkp[ptr],
|
2006-09-28 05:05:40 +04:00
|
|
|
(numrecs - ptr) * sizeof(*lkp));
|
2005-04-17 02:20:36 +04:00
|
|
|
memmove(&lpp[ptr - 1], &lpp[ptr],
|
2006-09-28 05:05:40 +04:00
|
|
|
(numrecs - ptr) * sizeof(*lpp));
|
|
|
|
xfs_alloc_log_ptrs(cur, bp, ptr, numrecs - 1);
|
|
|
|
xfs_alloc_log_keys(cur, bp, ptr, numrecs - 1);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* 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 {
|
|
|
|
lrp = XFS_ALLOC_REC_ADDR(block, 1, cur);
|
2006-09-28 05:05:40 +04:00
|
|
|
if (ptr < numrecs) {
|
2005-04-17 02:20:36 +04:00
|
|
|
memmove(&lrp[ptr - 1], &lrp[ptr],
|
2006-09-28 05:05:40 +04:00
|
|
|
(numrecs - ptr) * sizeof(*lrp));
|
|
|
|
xfs_alloc_log_recs(cur, bp, ptr, numrecs - 1);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* 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) {
|
2005-11-02 07:11:25 +03:00
|
|
|
key.ar_startblock = lrp->ar_startblock;
|
|
|
|
key.ar_blockcount = lrp->ar_blockcount;
|
2005-04-17 02:20:36 +04:00
|
|
|
lkp = &key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Decrement and log the number of entries in the block.
|
|
|
|
*/
|
2006-09-28 05:05:40 +04:00
|
|
|
numrecs--;
|
|
|
|
block->bb_numrecs = cpu_to_be16(numrecs);
|
2005-04-17 02:20:36 +04:00
|
|
|
xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
|
|
|
|
/*
|
|
|
|
* See if the longest free extent in the allocation group was
|
|
|
|
* changed by this operation. True if it's the by-size btree, and
|
|
|
|
* this is the leaf level, and there is no right sibling block,
|
|
|
|
* and this was the last record.
|
|
|
|
*/
|
|
|
|
agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
|
|
|
|
mp = cur->bc_mp;
|
|
|
|
|
|
|
|
if (level == 0 &&
|
|
|
|
cur->bc_btnum == XFS_BTNUM_CNT &&
|
2005-11-02 07:11:25 +03:00
|
|
|
be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
|
2006-09-28 05:05:40 +04:00
|
|
|
ptr > numrecs) {
|
|
|
|
ASSERT(ptr == numrecs + 1);
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* There are still records in the block. Grab the size
|
|
|
|
* from the last one.
|
|
|
|
*/
|
2006-09-28 05:05:40 +04:00
|
|
|
if (numrecs) {
|
|
|
|
rrp = XFS_ALLOC_REC_ADDR(block, numrecs, cur);
|
2005-11-02 07:11:25 +03:00
|
|
|
agf->agf_longest = rrp->ar_blockcount;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* No free extents left.
|
|
|
|
*/
|
|
|
|
else
|
|
|
|
agf->agf_longest = 0;
|
2005-11-02 07:11:25 +03:00
|
|
|
mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest =
|
|
|
|
be32_to_cpu(agf->agf_longest);
|
2005-04-17 02:20:36 +04:00
|
|
|
xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
|
|
|
|
XFS_AGF_LONGEST);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2006-09-28 05:05:40 +04:00
|
|
|
if (numrecs == 1 && level > 0) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* lpp is still set to the first pointer in the block.
|
|
|
|
* Make it the new root of the btree.
|
|
|
|
*/
|
2005-11-02 07:11:25 +03:00
|
|
|
bno = be32_to_cpu(agf->agf_roots[cur->bc_btnum]);
|
|
|
|
agf->agf_roots[cur->bc_btnum] = *lpp;
|
2008-02-14 02:03:29 +03:00
|
|
|
be32_add_cpu(&agf->agf_levels[cur->bc_btnum], -1);
|
2005-11-02 07:11:25 +03:00
|
|
|
mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_levels[cur->bc_btnum]--;
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Put this buffer/block on the ag's freelist.
|
|
|
|
*/
|
[XFS] Lazy Superblock Counters
When we have a couple of hundred transactions on the fly at once, they all
typically modify the on disk superblock in some way.
create/unclink/mkdir/rmdir modify inode counts, allocation/freeing modify
free block counts.
When these counts are modified in a transaction, they must eventually lock
the superblock buffer and apply the mods. The buffer then remains locked
until the transaction is committed into the incore log buffer. The result
of this is that with enough transactions on the fly the incore superblock
buffer becomes a bottleneck.
The result of contention on the incore superblock buffer is that
transaction rates fall - the more pressure that is put on the superblock
buffer, the slower things go.
The key to removing the contention is to not require the superblock fields
in question to be locked. We do that by not marking the superblock dirty
in the transaction. IOWs, we modify the incore superblock but do not
modify the cached superblock buffer. In short, we do not log superblock
modifications to critical fields in the superblock on every transaction.
In fact we only do it just before we write the superblock to disk every
sync period or just before unmount.
This creates an interesting problem - if we don't log or write out the
fields in every transaction, then how do the values get recovered after a
crash? the answer is simple - we keep enough duplicate, logged information
in other structures that we can reconstruct the correct count after log
recovery has been performed.
It is the AGF and AGI structures that contain the duplicate information;
after recovery, we walk every AGI and AGF and sum their individual
counters to get the correct value, and we do a transaction into the log to
correct them. An optimisation of this is that if we have a clean unmount
record, we know the value in the superblock is correct, so we can avoid
the summation walk under normal conditions and so mount/recovery times do
not change under normal operation.
One wrinkle that was discovered during development was that the blocks
used in the freespace btrees are never accounted for in the AGF counters.
This was once a valid optimisation to make; when the filesystem is full,
the free space btrees are empty and consume no space. Hence when it
matters, the "accounting" is correct. But that means the when we do the
AGF summations, we would not have a correct count and xfs_check would
complain. Hence a new counter was added to track the number of blocks used
by the free space btrees. This is an *on-disk format change*.
As a result of this, lazy superblock counters are a mkfs option and at the
moment on linux there is no way to convert an old filesystem. This is
possible - xfs_db can be used to twiddle the right bits and then
xfs_repair will do the format conversion for you. Similarly, you can
convert backwards as well. At some point we'll add functionality to
xfs_admin to do the bit twiddling easily....
SGI-PV: 964999
SGI-Modid: xfs-linux-melb:xfs-kern:28652a
Signed-off-by: David Chinner <dgc@sgi.com>
Signed-off-by: Christoph Hellwig <hch@infradead.org>
Signed-off-by: Tim Shimmin <tes@sgi.com>
2007-05-24 09:26:31 +04:00
|
|
|
error = xfs_alloc_put_freelist(cur->bc_tp,
|
|
|
|
cur->bc_private.a.agbp, NULL, bno, 1);
|
|
|
|
if (error)
|
2005-04-17 02:20:36 +04:00
|
|
|
return error;
|
|
|
|
/*
|
|
|
|
* Since blocks move to the free list without the
|
|
|
|
* coordination used in xfs_bmap_finish, we can't allow
|
|
|
|
* block to be available for reallocation and
|
|
|
|
* non-transaction writing (user data) until we know
|
|
|
|
* that the transaction that moved it to the free list
|
|
|
|
* is permanently on disk. We track the blocks by
|
|
|
|
* declaring these blocks as "busy"; the busy list is
|
|
|
|
* maintained on a per-ag basis and each transaction
|
|
|
|
* records which entries should be removed when the
|
|
|
|
* iclog commits to disk. If a busy block is
|
|
|
|
* allocated, the iclog is pushed up to the LSN
|
|
|
|
* that freed the block.
|
|
|
|
*/
|
|
|
|
xfs_alloc_mark_busy(cur->bc_tp,
|
2005-11-02 07:11:25 +03:00
|
|
|
be32_to_cpu(agf->agf_seqno), bno, 1);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
xfs_trans_agbtree_delta(cur->bc_tp, -1);
|
|
|
|
xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
|
|
|
|
XFS_AGF_ROOTS | XFS_AGF_LEVELS);
|
|
|
|
/*
|
|
|
|
* Update the cursor so there's one fewer level.
|
|
|
|
*/
|
|
|
|
xfs_btree_setbuf(cur, level, NULL);
|
|
|
|
cur->bc_nlevels--;
|
|
|
|
} else if (level > 0 &&
|
2008-10-30 08:55:58 +03:00
|
|
|
(error = xfs_btree_decrement(cur, level, &i)))
|
2005-04-17 02:20:36 +04:00
|
|
|
return error;
|
|
|
|
*stat = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If we deleted the leftmost entry in the block, update the
|
|
|
|
* key values above us in the tree.
|
|
|
|
*/
|
2008-10-30 08:56:22 +03:00
|
|
|
if (ptr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)lkp, level + 1)))
|
2005-04-17 02:20:36 +04:00
|
|
|
return error;
|
|
|
|
/*
|
|
|
|
* If the number of records remaining in the block is at least
|
|
|
|
* the minimum, we're done.
|
|
|
|
*/
|
2006-09-28 05:05:40 +04:00
|
|
|
if (numrecs >= XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
|
2008-10-30 08:55:58 +03:00
|
|
|
if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
|
2005-04-17 02:20:36 +04:00
|
|
|
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.
|
|
|
|
*/
|
2005-11-02 07:11:25 +03:00
|
|
|
rbno = be32_to_cpu(block->bb_rightsib);
|
|
|
|
lbno = be32_to_cpu(block->bb_leftsib);
|
2005-04-17 02:20:36 +04:00
|
|
|
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);
|
2008-10-30 08:55:45 +03:00
|
|
|
if ((error = xfs_btree_increment(tcur, level, &i)))
|
2005-04-17 02:20:36 +04:00
|
|
|
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_ALLOC_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.
|
|
|
|
*/
|
2005-11-02 07:11:25 +03:00
|
|
|
bno = be32_to_cpu(right->bb_leftsib);
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2005-11-02 07:11:25 +03:00
|
|
|
if (be16_to_cpu(right->bb_numrecs) - 1 >=
|
2005-04-17 02:20:36 +04:00
|
|
|
XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
|
2008-10-30 08:56:53 +03:00
|
|
|
if ((error = xfs_btree_lshift(tcur, level, &i)))
|
2005-04-17 02:20:36 +04:00
|
|
|
goto error0;
|
|
|
|
if (i) {
|
2005-11-02 07:11:25 +03:00
|
|
|
ASSERT(be16_to_cpu(block->bb_numrecs) >=
|
2005-04-17 02:20:36 +04:00
|
|
|
XFS_ALLOC_BLOCK_MINRECS(level, cur));
|
|
|
|
xfs_btree_del_cursor(tcur,
|
|
|
|
XFS_BTREE_NOERROR);
|
|
|
|
if (level > 0 &&
|
2008-10-30 08:55:58 +03:00
|
|
|
(error = xfs_btree_decrement(cur, level,
|
2005-04-17 02:20:36 +04:00
|
|
|
&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).
|
|
|
|
*/
|
2005-11-02 07:11:25 +03:00
|
|
|
rrecs = be16_to_cpu(right->bb_numrecs);
|
2005-04-17 02:20:36 +04:00
|
|
|
if (lbno != NULLAGBLOCK) {
|
|
|
|
i = xfs_btree_firstrec(tcur, level);
|
|
|
|
XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
|
2008-10-30 08:55:58 +03:00
|
|
|
if ((error = xfs_btree_decrement(tcur, level, &i)))
|
2005-04-17 02:20:36 +04:00
|
|
|
goto error0;
|
|
|
|
XFS_WANT_CORRUPTED_GOTO(i == 1, 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.
|
|
|
|
*/
|
|
|
|
i = xfs_btree_firstrec(tcur, level);
|
|
|
|
XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
|
2008-10-30 08:55:58 +03:00
|
|
|
if ((error = xfs_btree_decrement(tcur, level, &i)))
|
2005-04-17 02:20:36 +04:00
|
|
|
goto error0;
|
|
|
|
XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
|
|
|
|
xfs_btree_firstrec(tcur, level);
|
|
|
|
/*
|
|
|
|
* Grab a pointer to the block.
|
|
|
|
*/
|
|
|
|
lbp = tcur->bc_bufs[level];
|
|
|
|
left = XFS_BUF_TO_ALLOC_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.
|
|
|
|
*/
|
2005-11-02 07:11:25 +03:00
|
|
|
bno = be32_to_cpu(left->bb_rightsib);
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2005-11-02 07:11:25 +03:00
|
|
|
if (be16_to_cpu(left->bb_numrecs) - 1 >=
|
2005-04-17 02:20:36 +04:00
|
|
|
XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
|
2008-10-30 08:56:43 +03:00
|
|
|
if ((error = xfs_btree_rshift(tcur, level, &i)))
|
2005-04-17 02:20:36 +04:00
|
|
|
goto error0;
|
|
|
|
if (i) {
|
2005-11-02 07:11:25 +03:00
|
|
|
ASSERT(be16_to_cpu(block->bb_numrecs) >=
|
2005-04-17 02:20:36 +04:00
|
|
|
XFS_ALLOC_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.
|
|
|
|
*/
|
2005-11-02 07:11:25 +03:00
|
|
|
lrecs = be16_to_cpu(left->bb_numrecs);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* 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 &&
|
2006-09-28 05:05:40 +04:00
|
|
|
lrecs + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Set "right" to be the starting block,
|
|
|
|
* "left" to be the left neighbor.
|
|
|
|
*/
|
|
|
|
rbno = bno;
|
|
|
|
right = block;
|
2006-09-28 05:05:40 +04:00
|
|
|
rrecs = be16_to_cpu(right->bb_numrecs);
|
2005-04-17 02:20:36 +04:00
|
|
|
rbp = bp;
|
|
|
|
if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
|
|
|
|
cur->bc_private.a.agno, lbno, 0, &lbp,
|
|
|
|
XFS_ALLOC_BTREE_REF)))
|
|
|
|
return error;
|
|
|
|
left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
|
2006-09-28 05:05:40 +04:00
|
|
|
lrecs = be16_to_cpu(left->bb_numrecs);
|
2005-04-17 02:20:36 +04:00
|
|
|
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 &&
|
2006-09-28 05:05:40 +04:00
|
|
|
rrecs + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Set "left" to be the starting block,
|
|
|
|
* "right" to be the right neighbor.
|
|
|
|
*/
|
|
|
|
lbno = bno;
|
|
|
|
left = block;
|
2006-09-28 05:05:40 +04:00
|
|
|
lrecs = be16_to_cpu(left->bb_numrecs);
|
2005-04-17 02:20:36 +04:00
|
|
|
lbp = bp;
|
|
|
|
if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
|
|
|
|
cur->bc_private.a.agno, rbno, 0, &rbp,
|
|
|
|
XFS_ALLOC_BTREE_REF)))
|
|
|
|
return error;
|
|
|
|
right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
|
2006-09-28 05:05:40 +04:00
|
|
|
rrecs = be16_to_cpu(right->bb_numrecs);
|
2005-04-17 02:20:36 +04:00
|
|
|
if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Otherwise, we can't fix the imbalance.
|
|
|
|
* Just return. This is probably a logic error, but it's not fatal.
|
|
|
|
*/
|
|
|
|
else {
|
2008-10-30 08:55:58 +03:00
|
|
|
if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
|
2005-04-17 02:20:36 +04:00
|
|
|
return error;
|
|
|
|
*stat = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We're now going to join "left" and "right" by moving all the stuff
|
|
|
|
* in "right" to "left" and deleting "right".
|
|
|
|
*/
|
|
|
|
if (level > 0) {
|
|
|
|
/*
|
|
|
|
* It's a non-leaf. Move keys and pointers.
|
|
|
|
*/
|
2006-09-28 05:05:40 +04:00
|
|
|
lkp = XFS_ALLOC_KEY_ADDR(left, lrecs + 1, cur);
|
|
|
|
lpp = XFS_ALLOC_PTR_ADDR(left, lrecs + 1, cur);
|
2005-04-17 02:20:36 +04:00
|
|
|
rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
|
|
|
|
rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
|
|
|
|
#ifdef DEBUG
|
2006-09-28 05:05:40 +04:00
|
|
|
for (i = 0; i < rrecs; i++) {
|
2005-11-02 07:11:25 +03:00
|
|
|
if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level)))
|
2005-04-17 02:20:36 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
#endif
|
2006-09-28 05:05:40 +04:00
|
|
|
memcpy(lkp, rkp, rrecs * sizeof(*lkp));
|
|
|
|
memcpy(lpp, rpp, rrecs * sizeof(*lpp));
|
|
|
|
xfs_alloc_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
|
|
|
|
xfs_alloc_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
|
2005-04-17 02:20:36 +04:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* It's a leaf. Move records.
|
|
|
|
*/
|
2006-09-28 05:05:40 +04:00
|
|
|
lrp = XFS_ALLOC_REC_ADDR(left, lrecs + 1, cur);
|
2005-04-17 02:20:36 +04:00
|
|
|
rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
|
2006-09-28 05:05:40 +04:00
|
|
|
memcpy(lrp, rrp, rrecs * sizeof(*lrp));
|
|
|
|
xfs_alloc_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If we joined with the left neighbor, set the buffer in the
|
|
|
|
* cursor to the left block, and fix up the index.
|
|
|
|
*/
|
|
|
|
if (bp != lbp) {
|
|
|
|
xfs_btree_setbuf(cur, level, lbp);
|
2006-09-28 05:05:40 +04:00
|
|
|
cur->bc_ptrs[level] += lrecs;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If we joined with the right neighbor and there's a level above
|
|
|
|
* us, increment the cursor at that level.
|
|
|
|
*/
|
|
|
|
else if (level + 1 < cur->bc_nlevels &&
|
2008-10-30 08:55:45 +03:00
|
|
|
(error = xfs_btree_increment(cur, level + 1, &i)))
|
2005-04-17 02:20:36 +04:00
|
|
|
return error;
|
|
|
|
/*
|
|
|
|
* Fix up the number of records in the surviving block.
|
|
|
|
*/
|
2006-09-28 05:05:40 +04:00
|
|
|
lrecs += rrecs;
|
|
|
|
left->bb_numrecs = cpu_to_be16(lrecs);
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Fix up the right block pointer in the surviving block, and log it.
|
|
|
|
*/
|
2005-11-02 07:11:25 +03:00
|
|
|
left->bb_rightsib = right->bb_rightsib;
|
2005-04-17 02:20:36 +04:00
|
|
|
xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
|
|
|
|
/*
|
|
|
|
* If there is a right sibling now, make it point to the
|
|
|
|
* remaining block.
|
|
|
|
*/
|
2005-11-02 07:11:25 +03:00
|
|
|
if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
|
2005-04-17 02:20:36 +04:00
|
|
|
xfs_alloc_block_t *rrblock;
|
|
|
|
xfs_buf_t *rrbp;
|
|
|
|
|
|
|
|
if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
|
2005-11-02 07:11:25 +03:00
|
|
|
cur->bc_private.a.agno, be32_to_cpu(left->bb_rightsib), 0,
|
2005-04-17 02:20:36 +04:00
|
|
|
&rrbp, XFS_ALLOC_BTREE_REF)))
|
|
|
|
return error;
|
|
|
|
rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
|
|
|
|
if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
|
|
|
|
return error;
|
2005-11-02 07:11:25 +03:00
|
|
|
rrblock->bb_leftsib = cpu_to_be32(lbno);
|
2005-04-17 02:20:36 +04:00
|
|
|
xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Free the deleting block by putting it on the freelist.
|
|
|
|
*/
|
[XFS] Lazy Superblock Counters
When we have a couple of hundred transactions on the fly at once, they all
typically modify the on disk superblock in some way.
create/unclink/mkdir/rmdir modify inode counts, allocation/freeing modify
free block counts.
When these counts are modified in a transaction, they must eventually lock
the superblock buffer and apply the mods. The buffer then remains locked
until the transaction is committed into the incore log buffer. The result
of this is that with enough transactions on the fly the incore superblock
buffer becomes a bottleneck.
The result of contention on the incore superblock buffer is that
transaction rates fall - the more pressure that is put on the superblock
buffer, the slower things go.
The key to removing the contention is to not require the superblock fields
in question to be locked. We do that by not marking the superblock dirty
in the transaction. IOWs, we modify the incore superblock but do not
modify the cached superblock buffer. In short, we do not log superblock
modifications to critical fields in the superblock on every transaction.
In fact we only do it just before we write the superblock to disk every
sync period or just before unmount.
This creates an interesting problem - if we don't log or write out the
fields in every transaction, then how do the values get recovered after a
crash? the answer is simple - we keep enough duplicate, logged information
in other structures that we can reconstruct the correct count after log
recovery has been performed.
It is the AGF and AGI structures that contain the duplicate information;
after recovery, we walk every AGI and AGF and sum their individual
counters to get the correct value, and we do a transaction into the log to
correct them. An optimisation of this is that if we have a clean unmount
record, we know the value in the superblock is correct, so we can avoid
the summation walk under normal conditions and so mount/recovery times do
not change under normal operation.
One wrinkle that was discovered during development was that the blocks
used in the freespace btrees are never accounted for in the AGF counters.
This was once a valid optimisation to make; when the filesystem is full,
the free space btrees are empty and consume no space. Hence when it
matters, the "accounting" is correct. But that means the when we do the
AGF summations, we would not have a correct count and xfs_check would
complain. Hence a new counter was added to track the number of blocks used
by the free space btrees. This is an *on-disk format change*.
As a result of this, lazy superblock counters are a mkfs option and at the
moment on linux there is no way to convert an old filesystem. This is
possible - xfs_db can be used to twiddle the right bits and then
xfs_repair will do the format conversion for you. Similarly, you can
convert backwards as well. At some point we'll add functionality to
xfs_admin to do the bit twiddling easily....
SGI-PV: 964999
SGI-Modid: xfs-linux-melb:xfs-kern:28652a
Signed-off-by: David Chinner <dgc@sgi.com>
Signed-off-by: Christoph Hellwig <hch@infradead.org>
Signed-off-by: Tim Shimmin <tes@sgi.com>
2007-05-24 09:26:31 +04:00
|
|
|
error = xfs_alloc_put_freelist(cur->bc_tp,
|
|
|
|
cur->bc_private.a.agbp, NULL, rbno, 1);
|
|
|
|
if (error)
|
2005-04-17 02:20:36 +04:00
|
|
|
return error;
|
|
|
|
/*
|
|
|
|
* Since blocks move to the free list without the coordination
|
|
|
|
* used in xfs_bmap_finish, we can't allow block to be available
|
|
|
|
* for reallocation and non-transaction writing (user data)
|
|
|
|
* until we know that the transaction that moved it to the free
|
|
|
|
* list is permanently on disk. We track the blocks by declaring
|
|
|
|
* these blocks as "busy"; the busy list is maintained on a
|
|
|
|
* per-ag basis and each transaction records which entries
|
|
|
|
* should be removed when the iclog commits to disk. If a
|
|
|
|
* busy block is allocated, the iclog is pushed up to the
|
|
|
|
* LSN that freed the block.
|
|
|
|
*/
|
2005-11-02 07:11:25 +03:00
|
|
|
xfs_alloc_mark_busy(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
|
2005-04-17 02:20:36 +04:00
|
|
|
xfs_trans_agbtree_delta(cur->bc_tp, -1);
|
2005-11-02 07:11:25 +03:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Adjust the current level's cursor so that we're left referring
|
|
|
|
* to the right node, after we're done.
|
|
|
|
* If this leaves the ptr value 0 our caller will fix it up.
|
|
|
|
*/
|
|
|
|
if (level > 0)
|
|
|
|
cur->bc_ptrs[level]--;
|
|
|
|
/*
|
|
|
|
* Return value means the next level up has something to do.
|
|
|
|
*/
|
|
|
|
*stat = 2;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error0:
|
|
|
|
xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Log header fields from a btree block.
|
|
|
|
*/
|
|
|
|
STATIC void
|
|
|
|
xfs_alloc_log_block(
|
|
|
|
xfs_trans_t *tp, /* transaction pointer */
|
|
|
|
xfs_buf_t *bp, /* buffer containing btree block */
|
|
|
|
int fields) /* mask of fields: XFS_BB_... */
|
|
|
|
{
|
|
|
|
int first; /* first byte offset logged */
|
|
|
|
int last; /* last byte offset logged */
|
|
|
|
static const short offsets[] = { /* table of offsets */
|
|
|
|
offsetof(xfs_alloc_block_t, bb_magic),
|
|
|
|
offsetof(xfs_alloc_block_t, bb_level),
|
|
|
|
offsetof(xfs_alloc_block_t, bb_numrecs),
|
|
|
|
offsetof(xfs_alloc_block_t, bb_leftsib),
|
|
|
|
offsetof(xfs_alloc_block_t, bb_rightsib),
|
|
|
|
sizeof(xfs_alloc_block_t)
|
|
|
|
};
|
|
|
|
|
|
|
|
xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
|
|
|
|
xfs_trans_log_buf(tp, bp, first, last);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Log keys from a btree block (nonleaf).
|
|
|
|
*/
|
|
|
|
STATIC void
|
|
|
|
xfs_alloc_log_keys(
|
|
|
|
xfs_btree_cur_t *cur, /* btree cursor */
|
|
|
|
xfs_buf_t *bp, /* buffer containing btree block */
|
|
|
|
int kfirst, /* index of first key to log */
|
|
|
|
int klast) /* index of last key to log */
|
|
|
|
{
|
|
|
|
xfs_alloc_block_t *block; /* btree block to log from */
|
|
|
|
int first; /* first byte offset logged */
|
|
|
|
xfs_alloc_key_t *kp; /* key pointer in btree block */
|
|
|
|
int last; /* last byte offset logged */
|
|
|
|
|
|
|
|
block = XFS_BUF_TO_ALLOC_BLOCK(bp);
|
|
|
|
kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
|
|
|
|
first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
|
|
|
|
last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
|
|
|
|
xfs_trans_log_buf(cur->bc_tp, bp, first, last);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Log block pointer fields from a btree block (nonleaf).
|
|
|
|
*/
|
|
|
|
STATIC void
|
|
|
|
xfs_alloc_log_ptrs(
|
|
|
|
xfs_btree_cur_t *cur, /* btree cursor */
|
|
|
|
xfs_buf_t *bp, /* buffer containing btree block */
|
|
|
|
int pfirst, /* index of first pointer to log */
|
|
|
|
int plast) /* index of last pointer to log */
|
|
|
|
{
|
|
|
|
xfs_alloc_block_t *block; /* btree block to log from */
|
|
|
|
int first; /* first byte offset logged */
|
|
|
|
int last; /* last byte offset logged */
|
|
|
|
xfs_alloc_ptr_t *pp; /* block-pointer pointer in btree blk */
|
|
|
|
|
|
|
|
block = XFS_BUF_TO_ALLOC_BLOCK(bp);
|
|
|
|
pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
|
|
|
|
first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
|
|
|
|
last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
|
|
|
|
xfs_trans_log_buf(cur->bc_tp, bp, first, last);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Log records from a btree block (leaf).
|
|
|
|
*/
|
|
|
|
STATIC void
|
|
|
|
xfs_alloc_log_recs(
|
|
|
|
xfs_btree_cur_t *cur, /* btree cursor */
|
|
|
|
xfs_buf_t *bp, /* buffer containing btree block */
|
|
|
|
int rfirst, /* index of first record to log */
|
|
|
|
int rlast) /* index of last record to log */
|
|
|
|
{
|
|
|
|
xfs_alloc_block_t *block; /* btree block to log from */
|
|
|
|
int first; /* first byte offset logged */
|
|
|
|
int last; /* last byte offset logged */
|
|
|
|
xfs_alloc_rec_t *rp; /* record pointer for btree block */
|
|
|
|
|
|
|
|
|
|
|
|
block = XFS_BUF_TO_ALLOC_BLOCK(bp);
|
|
|
|
rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
xfs_agf_t *agf;
|
|
|
|
xfs_alloc_rec_t *p;
|
|
|
|
|
|
|
|
agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
|
|
|
|
for (p = &rp[rfirst - 1]; p <= &rp[rlast - 1]; p++)
|
2005-11-02 07:11:25 +03:00
|
|
|
ASSERT(be32_to_cpu(p->ar_startblock) +
|
|
|
|
be32_to_cpu(p->ar_blockcount) <=
|
|
|
|
be32_to_cpu(agf->agf_length));
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
|
|
|
|
last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
|
|
|
|
xfs_trans_log_buf(cur->bc_tp, bp, first, last);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Externally visible routines.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete the record pointed to by cur.
|
|
|
|
* The cursor refers to the place where the record was (could be inserted)
|
|
|
|
* when the operation returns.
|
|
|
|
*/
|
|
|
|
int /* error */
|
|
|
|
xfs_alloc_delete(
|
|
|
|
xfs_btree_cur_t *cur, /* btree cursor */
|
|
|
|
int *stat) /* success/failure */
|
|
|
|
{
|
|
|
|
int error; /* error return value */
|
|
|
|
int i; /* result code */
|
|
|
|
int level; /* btree level */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Go up the tree, starting at leaf level.
|
|
|
|
* If 2 is returned then a join was done; go to the next level.
|
|
|
|
* Otherwise we are done.
|
|
|
|
*/
|
|
|
|
for (level = 0, i = 2; i == 2; level++) {
|
|
|
|
if ((error = xfs_alloc_delrec(cur, level, &i)))
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
if (i == 0) {
|
|
|
|
for (level = 1; level < cur->bc_nlevels; level++) {
|
|
|
|
if (cur->bc_ptrs[level] == 0) {
|
2008-10-30 08:55:58 +03:00
|
|
|
if ((error = xfs_btree_decrement(cur, level, &i)))
|
2005-04-17 02:20:36 +04:00
|
|
|
return error;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*stat = i;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the data from the pointed-to record.
|
|
|
|
*/
|
|
|
|
int /* error */
|
|
|
|
xfs_alloc_get_rec(
|
|
|
|
xfs_btree_cur_t *cur, /* btree cursor */
|
|
|
|
xfs_agblock_t *bno, /* output: starting block of extent */
|
|
|
|
xfs_extlen_t *len, /* output: length of extent */
|
|
|
|
int *stat) /* output: success/failure */
|
|
|
|
{
|
|
|
|
xfs_alloc_block_t *block; /* btree block */
|
|
|
|
#ifdef DEBUG
|
|
|
|
int error; /* error return value */
|
|
|
|
#endif
|
|
|
|
int ptr; /* record number */
|
|
|
|
|
|
|
|
ptr = cur->bc_ptrs[0];
|
|
|
|
block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
|
|
|
|
#ifdef DEBUG
|
|
|
|
if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
|
|
|
|
return error;
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* Off the right end or left end, return failure.
|
|
|
|
*/
|
2005-11-02 07:11:25 +03:00
|
|
|
if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) {
|
2005-04-17 02:20:36 +04:00
|
|
|
*stat = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Point to the record and extract its data.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
xfs_alloc_rec_t *rec; /* record data */
|
|
|
|
|
|
|
|
rec = XFS_ALLOC_REC_ADDR(block, ptr, cur);
|
2005-11-02 07:11:25 +03:00
|
|
|
*bno = be32_to_cpu(rec->ar_startblock);
|
|
|
|
*len = be32_to_cpu(rec->ar_blockcount);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
*stat = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-30 08:56:32 +03:00
|
|
|
STATIC struct xfs_btree_cur *
|
|
|
|
xfs_allocbt_dup_cursor(
|
|
|
|
struct xfs_btree_cur *cur)
|
|
|
|
{
|
|
|
|
return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
|
|
|
|
cur->bc_private.a.agbp, cur->bc_private.a.agno,
|
|
|
|
cur->bc_btnum);
|
|
|
|
}
|
|
|
|
|
2008-10-30 08:57:16 +03:00
|
|
|
STATIC void
|
|
|
|
xfs_allocbt_set_root(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_ptr *ptr,
|
|
|
|
int inc)
|
|
|
|
{
|
|
|
|
struct xfs_buf *agbp = cur->bc_private.a.agbp;
|
|
|
|
struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
|
|
|
|
xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
|
|
|
|
int btnum = cur->bc_btnum;
|
|
|
|
|
|
|
|
ASSERT(ptr->s != 0);
|
|
|
|
|
|
|
|
agf->agf_roots[btnum] = ptr->s;
|
|
|
|
be32_add_cpu(&agf->agf_levels[btnum], inc);
|
|
|
|
cur->bc_mp->m_perag[seqno].pagf_levels[btnum] += inc;
|
|
|
|
|
|
|
|
xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
|
|
|
|
}
|
|
|
|
|
2008-10-30 08:57:03 +03:00
|
|
|
STATIC int
|
|
|
|
xfs_allocbt_alloc_block(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_ptr *start,
|
|
|
|
union xfs_btree_ptr *new,
|
|
|
|
int length,
|
|
|
|
int *stat)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
xfs_agblock_t bno;
|
|
|
|
|
|
|
|
XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
|
|
|
|
|
|
|
|
/* Allocate the new block from the freelist. If we can't, give up. */
|
|
|
|
error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
|
|
|
|
&bno, 1);
|
|
|
|
if (error) {
|
|
|
|
XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bno == NULLAGBLOCK) {
|
|
|
|
XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
|
|
|
|
*stat = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
xfs_trans_agbtree_delta(cur->bc_tp, 1);
|
|
|
|
new->s = cpu_to_be32(bno);
|
|
|
|
|
|
|
|
XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
|
|
|
|
*stat = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-30 08:57:51 +03:00
|
|
|
STATIC int
|
|
|
|
xfs_allocbt_free_block(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
struct xfs_buf *bp)
|
|
|
|
{
|
|
|
|
struct xfs_buf *agbp = cur->bc_private.a.agbp;
|
|
|
|
struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
|
|
|
|
xfs_agblock_t bno;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
bno = XFS_DADDR_TO_AGBNO(cur->bc_mp, XFS_BUF_ADDR(bp));
|
|
|
|
error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since blocks move to the free list without the coordination used in
|
|
|
|
* xfs_bmap_finish, we can't allow block to be available for
|
|
|
|
* reallocation and non-transaction writing (user data) until we know
|
|
|
|
* that the transaction that moved it to the free list is permanently
|
|
|
|
* on disk. We track the blocks by declaring these blocks as "busy";
|
|
|
|
* the busy list is maintained on a per-ag basis and each transaction
|
|
|
|
* records which entries should be removed when the iclog commits to
|
|
|
|
* disk. If a busy block is allocated, the iclog is pushed up to the
|
|
|
|
* LSN that freed the block.
|
|
|
|
*/
|
|
|
|
xfs_alloc_mark_busy(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
|
|
|
|
xfs_trans_agbtree_delta(cur->bc_tp, -1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
2008-10-30 08:56:32 +03:00
|
|
|
* Update the longest extent in the AGF
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2008-10-30 08:56:32 +03:00
|
|
|
STATIC void
|
|
|
|
xfs_allocbt_update_lastrec(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
struct xfs_btree_block *block,
|
|
|
|
union xfs_btree_rec *rec,
|
|
|
|
int ptr,
|
|
|
|
int reason)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2008-10-30 08:56:32 +03:00
|
|
|
struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
|
|
|
|
xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
|
|
|
|
__be32 len;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2008-10-30 08:56:32 +03:00
|
|
|
ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2008-10-30 08:56:32 +03:00
|
|
|
switch (reason) {
|
|
|
|
case LASTREC_UPDATE:
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
2008-10-30 08:56:32 +03:00
|
|
|
* If this is the last leaf block and it's the last record,
|
|
|
|
* then update the size of the longest extent in the AG.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2008-10-30 08:56:32 +03:00
|
|
|
if (ptr != xfs_btree_get_numrecs(block))
|
|
|
|
return;
|
|
|
|
len = rec->alloc.ar_blockcount;
|
|
|
|
break;
|
2008-10-30 08:57:40 +03:00
|
|
|
case LASTREC_INSREC:
|
|
|
|
if (be32_to_cpu(rec->alloc.ar_blockcount) <=
|
|
|
|
be32_to_cpu(agf->agf_longest))
|
|
|
|
return;
|
|
|
|
len = rec->alloc.ar_blockcount;
|
|
|
|
break;
|
2008-10-30 08:56:32 +03:00
|
|
|
default:
|
|
|
|
ASSERT(0);
|
|
|
|
return;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2008-10-30 08:53:59 +03:00
|
|
|
|
2008-10-30 08:56:32 +03:00
|
|
|
agf->agf_longest = len;
|
|
|
|
cur->bc_mp->m_perag[seqno].pagf_longest = be32_to_cpu(len);
|
|
|
|
xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
|
2008-10-30 08:53:59 +03:00
|
|
|
}
|
|
|
|
|
2008-10-30 08:55:23 +03:00
|
|
|
STATIC int
|
|
|
|
xfs_allocbt_get_maxrecs(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
int level)
|
|
|
|
{
|
|
|
|
return cur->bc_mp->m_alloc_mxr[level != 0];
|
|
|
|
}
|
|
|
|
|
2008-10-30 08:56:09 +03:00
|
|
|
STATIC void
|
|
|
|
xfs_allocbt_init_key_from_rec(
|
|
|
|
union xfs_btree_key *key,
|
|
|
|
union xfs_btree_rec *rec)
|
|
|
|
{
|
|
|
|
ASSERT(rec->alloc.ar_startblock != 0);
|
|
|
|
|
|
|
|
key->alloc.ar_startblock = rec->alloc.ar_startblock;
|
|
|
|
key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
|
|
|
|
}
|
|
|
|
|
2008-10-30 08:57:40 +03:00
|
|
|
STATIC void
|
|
|
|
xfs_allocbt_init_rec_from_key(
|
|
|
|
union xfs_btree_key *key,
|
|
|
|
union xfs_btree_rec *rec)
|
|
|
|
{
|
|
|
|
ASSERT(key->alloc.ar_startblock != 0);
|
|
|
|
|
|
|
|
rec->alloc.ar_startblock = key->alloc.ar_startblock;
|
|
|
|
rec->alloc.ar_blockcount = key->alloc.ar_blockcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void
|
|
|
|
xfs_allocbt_init_rec_from_cur(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_rec *rec)
|
|
|
|
{
|
|
|
|
ASSERT(cur->bc_rec.a.ar_startblock != 0);
|
|
|
|
|
|
|
|
rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
|
|
|
|
rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
|
|
|
|
}
|
|
|
|
|
2008-10-30 08:56:09 +03:00
|
|
|
STATIC void
|
|
|
|
xfs_allocbt_init_ptr_from_cur(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_ptr *ptr)
|
|
|
|
{
|
|
|
|
struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
|
|
|
|
|
|
|
|
ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
|
|
|
|
ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
|
|
|
|
|
|
|
|
ptr->s = agf->agf_roots[cur->bc_btnum];
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC __int64_t
|
|
|
|
xfs_allocbt_key_diff(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_key *key)
|
|
|
|
{
|
|
|
|
xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
|
|
|
|
xfs_alloc_key_t *kp = &key->alloc;
|
|
|
|
__int64_t diff;
|
|
|
|
|
|
|
|
if (cur->bc_btnum == XFS_BTNUM_BNO) {
|
|
|
|
return (__int64_t)be32_to_cpu(kp->ar_startblock) -
|
|
|
|
rec->ar_startblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
|
|
|
|
if (diff)
|
|
|
|
return diff;
|
|
|
|
|
|
|
|
return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
|
|
|
|
}
|
|
|
|
|
2008-10-30 08:55:13 +03:00
|
|
|
#ifdef XFS_BTREE_TRACE
|
|
|
|
ktrace_t *xfs_allocbt_trace_buf;
|
|
|
|
|
|
|
|
STATIC void
|
|
|
|
xfs_allocbt_trace_enter(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
const char *func,
|
|
|
|
char *s,
|
|
|
|
int type,
|
|
|
|
int line,
|
|
|
|
__psunsigned_t a0,
|
|
|
|
__psunsigned_t a1,
|
|
|
|
__psunsigned_t a2,
|
|
|
|
__psunsigned_t a3,
|
|
|
|
__psunsigned_t a4,
|
|
|
|
__psunsigned_t a5,
|
|
|
|
__psunsigned_t a6,
|
|
|
|
__psunsigned_t a7,
|
|
|
|
__psunsigned_t a8,
|
|
|
|
__psunsigned_t a9,
|
|
|
|
__psunsigned_t a10)
|
|
|
|
{
|
|
|
|
ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
|
|
|
|
(void *)func, (void *)s, NULL, (void *)cur,
|
|
|
|
(void *)a0, (void *)a1, (void *)a2, (void *)a3,
|
|
|
|
(void *)a4, (void *)a5, (void *)a6, (void *)a7,
|
|
|
|
(void *)a8, (void *)a9, (void *)a10);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void
|
|
|
|
xfs_allocbt_trace_cursor(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
__uint32_t *s0,
|
|
|
|
__uint64_t *l0,
|
|
|
|
__uint64_t *l1)
|
|
|
|
{
|
|
|
|
*s0 = cur->bc_private.a.agno;
|
|
|
|
*l0 = cur->bc_rec.a.ar_startblock;
|
|
|
|
*l1 = cur->bc_rec.a.ar_blockcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void
|
|
|
|
xfs_allocbt_trace_key(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_key *key,
|
|
|
|
__uint64_t *l0,
|
|
|
|
__uint64_t *l1)
|
|
|
|
{
|
|
|
|
*l0 = be32_to_cpu(key->alloc.ar_startblock);
|
|
|
|
*l1 = be32_to_cpu(key->alloc.ar_blockcount);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void
|
|
|
|
xfs_allocbt_trace_record(
|
|
|
|
struct xfs_btree_cur *cur,
|
|
|
|
union xfs_btree_rec *rec,
|
|
|
|
__uint64_t *l0,
|
|
|
|
__uint64_t *l1,
|
|
|
|
__uint64_t *l2)
|
|
|
|
{
|
|
|
|
*l0 = be32_to_cpu(rec->alloc.ar_startblock);
|
|
|
|
*l1 = be32_to_cpu(rec->alloc.ar_blockcount);
|
|
|
|
*l2 = 0;
|
|
|
|
}
|
|
|
|
#endif /* XFS_BTREE_TRACE */
|
|
|
|
|
2008-10-30 08:53:59 +03:00
|
|
|
static const struct xfs_btree_ops xfs_allocbt_ops = {
|
2008-10-30 08:55:34 +03:00
|
|
|
.rec_len = sizeof(xfs_alloc_rec_t),
|
|
|
|
.key_len = sizeof(xfs_alloc_key_t),
|
|
|
|
|
2008-10-30 08:53:59 +03:00
|
|
|
.dup_cursor = xfs_allocbt_dup_cursor,
|
2008-10-30 08:57:16 +03:00
|
|
|
.set_root = xfs_allocbt_set_root,
|
2008-10-30 08:57:03 +03:00
|
|
|
.alloc_block = xfs_allocbt_alloc_block,
|
2008-10-30 08:57:51 +03:00
|
|
|
.free_block = xfs_allocbt_free_block,
|
2008-10-30 08:56:32 +03:00
|
|
|
.update_lastrec = xfs_allocbt_update_lastrec,
|
2008-10-30 08:55:23 +03:00
|
|
|
.get_maxrecs = xfs_allocbt_get_maxrecs,
|
2008-10-30 08:56:09 +03:00
|
|
|
.init_key_from_rec = xfs_allocbt_init_key_from_rec,
|
2008-10-30 08:57:40 +03:00
|
|
|
.init_rec_from_key = xfs_allocbt_init_rec_from_key,
|
|
|
|
.init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
|
2008-10-30 08:56:09 +03:00
|
|
|
.init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
|
|
|
|
.key_diff = xfs_allocbt_key_diff,
|
2008-10-30 08:55:13 +03:00
|
|
|
|
|
|
|
#ifdef XFS_BTREE_TRACE
|
|
|
|
.trace_enter = xfs_allocbt_trace_enter,
|
|
|
|
.trace_cursor = xfs_allocbt_trace_cursor,
|
|
|
|
.trace_key = xfs_allocbt_trace_key,
|
|
|
|
.trace_record = xfs_allocbt_trace_record,
|
|
|
|
#endif
|
2008-10-30 08:53:59 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a new allocation btree cursor.
|
|
|
|
*/
|
|
|
|
struct xfs_btree_cur * /* new alloc btree cursor */
|
|
|
|
xfs_allocbt_init_cursor(
|
|
|
|
struct xfs_mount *mp, /* file system mount point */
|
|
|
|
struct xfs_trans *tp, /* transaction pointer */
|
|
|
|
struct xfs_buf *agbp, /* buffer for agf structure */
|
|
|
|
xfs_agnumber_t agno, /* allocation group number */
|
|
|
|
xfs_btnum_t btnum) /* btree identifier */
|
|
|
|
{
|
|
|
|
struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
|
|
|
|
struct xfs_btree_cur *cur;
|
|
|
|
|
|
|
|
ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
|
|
|
|
|
|
|
|
cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
|
|
|
|
|
|
|
|
cur->bc_tp = tp;
|
|
|
|
cur->bc_mp = mp;
|
|
|
|
cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
|
|
|
|
cur->bc_btnum = btnum;
|
|
|
|
cur->bc_blocklog = mp->m_sb.sb_blocklog;
|
|
|
|
|
|
|
|
cur->bc_ops = &xfs_allocbt_ops;
|
2008-10-30 08:56:32 +03:00
|
|
|
if (btnum == XFS_BTNUM_CNT)
|
|
|
|
cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
|
2008-10-30 08:53:59 +03:00
|
|
|
|
|
|
|
cur->bc_private.a.agbp = agbp;
|
|
|
|
cur->bc_private.a.agno = agno;
|
|
|
|
|
|
|
|
return cur;
|
|
|
|
}
|