#ident "$Revision: 1.20 $"
/*
* Free space allocation for xFS.
*/
#include <sys/param.h>
#ifdef SIM
#define _KERNEL
#endif
#include <sys/buf.h>
#ifdef SIM
#undef _KERNEL
#endif
#include <sys/vnode.h>
#include <sys/uuid.h>
#include <sys/debug.h>
#include <stddef.h>
#ifdef SIM
#include <stdlib.h>
#include <bstring.h>
#else
#include <sys/systm.h>
#endif
#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_mount.h"
#include "xfs_alloc_btree.h"
#include "xfs_ialloc.h"
#include "xfs_bmap_btree.h"
#include "xfs_btree.h"
#include "xfs_alloc.h"
#ifdef SIM
#include "sim.h"
#endif
#if !defined(SIM) || !defined(XFSDEBUG)
#define kmem_check() /* dummy for memory-allocation checking */
#endif
/*
* Prototypes for internal functions.
*/
/*
* Compute best start block and diff for "near" allocations.
* freelen >= wantlen already checked by caller.
*/
STATIC xfs_agblock_t /* difference value (absolute) */
xfs_alloc_compute_diff(
xfs_agblock_t wantbno, /* target starting block */
xfs_extlen_t wantlen, /* target length */
xfs_agblock_t freebno, /* freespace's starting block */
xfs_extlen_t freelen, /* freespace's length */
xfs_agblock_t *newbnop); /* result: best start block from free */
/*
* Fix up the length, based on mod and prod.
* len should be k * prod + mod for some k.
* If len is too small it is returned unchanged.
*/
STATIC xfs_extlen_t /* new length to use instead of len */
xfs_alloc_fix_len(
xfs_extlen_t mod, /* mod value to fix length with */
xfs_extlen_t prod, /* product value to fix length with */
xfs_extlen_t len); /* input length */
/*
* Read in the allocation group header (free/alloc section).
*/
STATIC buf_t * /* buffer for the ag freelist header */
xfs_alloc_read_agf(
xfs_mount_t *mp, /* mount point structure */
xfs_trans_t *tp, /* transaction pointer */
xfs_agnumber_t agno, /* allocation group number */
int flags); /* XFS_ALLOC_FLAG_... */
/*
* Prototypes for per-ag allocation routines
*/
/*
* Allocate a variable extent in the allocation group agno.
* Type and bno are used to determine where in the allocation group the
* extent will start.
* Extent's length (returned in *len) will be between minlen and maxlen,
* and of the form k * prod + mod unless there's nothing that large.
* Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
*/
STATIC xfs_agblock_t /* starting block number of allocated extent */
xfs_alloc_ag_vextent(
xfs_trans_t *tp, /* transaction pointer */
buf_t *agbuf, /* buffer for a.g. freelist header */
xfs_agnumber_t agno, /* allocation group number */
xfs_agblock_t bno, /* allocation-group relative block number */
xfs_extlen_t minlen, /* mininum size of extent */
xfs_extlen_t maxlen, /* maximum size of extent */
xfs_extlen_t *len, /* output: size of extent */
xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE_... */
int wasdel, /* set if allocation was previously delayed */
xfs_extlen_t mod, /* mod value for extent size */
xfs_extlen_t prod); /* prod value for extent size */
/*
* Allocate a variable extent at exactly agno/bno.
* Extent's length (returned in *len) will be between minlen and maxlen,
* and of the form k * prod + mod unless there's nothing that large.
* Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
*/
STATIC xfs_agblock_t /* starting block number of allocated extent */
xfs_alloc_ag_vextent_exact(
xfs_trans_t *tp, /* transaction pointer */
buf_t *agbuf, /* buffer for a.g. freelist header */
xfs_agnumber_t agno, /* allocation group number */
xfs_agblock_t bno, /* allocation-group relative block number */
xfs_extlen_t minlen, /* mininum size of extent */
xfs_extlen_t maxlen, /* maximum size of extent */
xfs_extlen_t *len, /* output: size of extent */
xfs_extlen_t mod, /* mod value for extent size */
xfs_extlen_t prod); /* prod value for extent size */
/*
* Allocate a variable extent near bno in the allocation group agno.
* Extent's length (returned in *len) will be between minlen and maxlen,
* and of the form k * prod + mod unless there's nothing that large.
* Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
*/
STATIC xfs_agblock_t /* starting block number of allocated extent */
xfs_alloc_ag_vextent_near(
xfs_trans_t *tp, /* transaction pointer */
buf_t *agbuf, /* buffer for a.g. freelist header */
xfs_agnumber_t agno, /* allocation group number */
xfs_agblock_t bno, /* allocation-group relative block number */
xfs_extlen_t minlen, /* mininum size of extent */
xfs_extlen_t maxlen, /* maximum size of extent */
xfs_extlen_t *len, /* output: size of extent */
xfs_extlen_t mod, /* mod value for extent size */
xfs_extlen_t prod); /* prod value for extent size */
/*
* Allocate a variable extent anywhere in the allocation group agno.
* Extent's length (returned in *len) will be between minlen and maxlen,
* and of the form k * prod + mod unless there's nothing that large.
* Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
*/
STATIC xfs_agblock_t /* starting block number of allocated extent */
xfs_alloc_ag_vextent_size(
xfs_trans_t *tp, /* transaction pointer */
buf_t *agbuf, /* buffer for a.g. freelist header */
xfs_agnumber_t agno, /* allocation group number */
xfs_extlen_t minlen, /* mininum size of extent */
xfs_extlen_t maxlen, /* maximum size of extent */
xfs_extlen_t *len, /* output: size of extent */
xfs_extlen_t mod, /* mod value for extent size */
xfs_extlen_t prod); /* prod value for extent size */
/*
* Free the extent starting at agno/bno for length.
*/
STATIC int /* return success/failure, will be void */
xfs_free_ag_extent(
xfs_trans_t *tp, /* transaction pointer */
buf_t *agbuf, /* buffer for a.g. freelist header */
xfs_agnumber_t agno, /* allocation group number */
xfs_agblock_t bno, /* starting block number */
xfs_extlen_t len); /* length of extent */
/*
* Internal functions.
*/
/*
* Compute best start block and diff for "near" allocations.
* freelen >= wantlen already checked by caller.
*/
STATIC xfs_agblock_t /* difference value (absolute) */
xfs_alloc_compute_diff(
xfs_agblock_t wantbno, /* target starting block */
xfs_extlen_t wantlen, /* target length */
xfs_agblock_t freebno, /* freespace's starting block */
xfs_extlen_t freelen, /* freespace's length */
xfs_agblock_t *newbnop) /* result: best start block from free */
{
xfs_agblock_t freeend; /* end of freespace extent */
xfs_agblock_t newbno; /* return block number */
xfs_agblock_t wantend; /* end of target extent */
freeend = freebno + freelen;
wantend = wantbno + wantlen;
if (freebno >= wantbno)
newbno = freebno;
else if (freeend >= wantend)
newbno = wantbno;
else
newbno = freeend - wantlen;
*newbnop = newbno;
return newbno <= wantbno ? wantbno - newbno : newbno - wantbno;
}
/*
* Fix up the length, based on mod and prod.
* len should be k * prod + mod for some k.
* If len is too small it is returned unchanged.
*/
STATIC xfs_extlen_t /* new length to use instead of len */
xfs_alloc_fix_len(
xfs_extlen_t mod, /* mod value to fix length with */
xfs_extlen_t prod, /* product value to fix length with */
xfs_extlen_t len) /* input length */
{
xfs_extlen_t k;
ASSERT(mod < prod);
if (prod <= 1 || len < mod || (mod == 0 && len < prod))
return len;
k = len % prod;
if (k == mod)
return len;
if (k > mod)
len -= k - mod;
else
len -= prod - (mod - k);
return len;
}
/*
* Read in the allocation group header (free/alloc section).
*/
STATIC buf_t * /* buffer for the ag freelist header */
xfs_alloc_read_agf(
xfs_mount_t *mp, /* mount point structure */
xfs_trans_t *tp, /* transaction pointer */
xfs_agnumber_t agno, /* allocation group number */
int flags) /* XFS_ALLOC_FLAG_... */
{
daddr_t d; /* disk block address */
xfs_sb_t *sbp; /* superblock structure */
ASSERT(agno != NULLAGNUMBER);
sbp = &mp->m_sb;
d = xfs_ag_daddr(sbp, agno, XFS_AGF_DADDR);
return xfs_trans_read_buf(tp, mp->m_dev, d, 1,
(flags & XFS_ALLOC_FLAG_TRYLOCK) ? BUF_TRYLOCK : 0U);
}
/*
* Allocation group level functions.
*/
/*
* Allocate a variable extent in the allocation group agno.
* Type and bno are used to determine where in the allocation group the
* extent will start.
* Extent's length (returned in *len) will be between minlen and maxlen,
* and of the form k * prod + mod unless there's nothing that large.
* Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
*/
STATIC xfs_agblock_t /* starting block number of allocated extent */
xfs_alloc_ag_vextent(
xfs_trans_t *tp, /* transaction pointer */
buf_t *agbuf, /* buffer for a.g. freelist header */
xfs_agnumber_t agno, /* allocation group number */
xfs_agblock_t bno, /* allocation-group relative block number */
xfs_extlen_t minlen, /* mininum size of extent */
xfs_extlen_t maxlen, /* maximum size of extent */
xfs_extlen_t *len, /* output: size of extent */
xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE_... */
int wasdel, /* set if allocation was previously delayed */
xfs_extlen_t mod, /* mod value for extent size */
xfs_extlen_t prod) /* prod value for extent size */
{
xfs_agf_t *agf; /* allocation group freelist header */
xfs_agblock_t r; /* return value (starting block) */
ASSERT(minlen > 0 && maxlen > 0 && minlen <= maxlen);
ASSERT(mod < prod);
/*
* Branch to correct routine based on the type.
*/
switch (type) {
case XFS_ALLOCTYPE_THIS_AG:
r = xfs_alloc_ag_vextent_size(tp, agbuf, agno, minlen, maxlen,
len, mod, prod);
break;
case XFS_ALLOCTYPE_NEAR_BNO:
r = xfs_alloc_ag_vextent_near(tp, agbuf, agno, bno, minlen,
maxlen, len, mod, prod);
break;
case XFS_ALLOCTYPE_THIS_BNO:
r = xfs_alloc_ag_vextent_exact(tp, agbuf, agno, bno, minlen,
maxlen, len, mod, prod);
break;
default:
ASSERT(0);
/* NOTREACHED */
}
/*
* If the allocation worked, need to change the agf structure
* (and log it), and the superblock.
*/
if (r != NULLAGBLOCK) {
int slen = (int)*len;
ASSERT(*len >= minlen && *len <= maxlen);
agf = xfs_buf_to_agf(agbuf);
agf->agf_freeblks -= *len;
xfs_alloc_log_agf(tp, agbuf, XFS_AGF_FREEBLKS);
if (wasdel)
xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FDBLOCKS, -slen);
else
xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, -slen);
}
return r;
}
/*
* Allocate a variable extent at exactly agno/bno.
* Extent's length (returned in *len) will be between minlen and maxlen,
* and of the form k * prod + mod unless there's nothing that large.
* Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
*/
STATIC xfs_agblock_t /* starting block number of allocated extent */
xfs_alloc_ag_vextent_exact(
xfs_trans_t *tp, /* transaction pointer */
buf_t *agbuf, /* buffer for a.g. freelist header */
xfs_agnumber_t agno, /* allocation group number */
xfs_agblock_t bno, /* allocation-group relative block number */
xfs_extlen_t minlen, /* mininum size of extent */
xfs_extlen_t maxlen, /* maximum size of extent */
xfs_extlen_t *len, /* output: size of extent */
xfs_extlen_t mod, /* mod value for extent size */
xfs_extlen_t prod) /* prod value for extent size */
{
xfs_btree_cur_t *bno_cur; /* by block-number btree cursor */
xfs_btree_cur_t *cnt_cur; /* by count btree cursor */
xfs_agblock_t end; /* end of allocated extent */
xfs_agblock_t fbno; /* start block of found extent */
xfs_agblock_t fend; /* end block of found extent */
xfs_extlen_t flen; /* length of found extent */
xfs_agblock_t maxend; /* end of maximal extent */
xfs_agblock_t minend; /* end of minimal extent */
xfs_mount_t *mp; /* mount point structure for filesystem */
xfs_extlen_t rlen; /* requested extent length */
/*
* Allocate/initialize a cursor for the by-number freespace btree.
*/
mp = tp->t_mountp;
bno_cur = xfs_btree_init_cursor(mp, tp, agbuf, agno, XFS_BTNUM_BNO, 0);
/*
* Lookup bno and minlen in the btree (minlen is irrelevant, really).
* Look for the closest free block <= bno, it must contain bno
* if any free block does.
*/
if (!xfs_alloc_lookup_le(bno_cur, bno, minlen)) {
/*
* Didn't find it, return null.
*/
xfs_btree_del_cursor(bno_cur);
return NULLAGBLOCK;
}
/*
* Grab the freespace record.
*/
xfs_alloc_get_rec(bno_cur, &fbno, &flen);
ASSERT(fbno <= bno);
minend = bno + minlen;
maxend = bno + maxlen;
fend = fbno + flen;
/*
* Give up if the freespace isn't long enough for the minimum request.
*/
if (fend < minend) {
xfs_btree_del_cursor(bno_cur);
return NULLAGBLOCK;
}
/*
* End of extent will be smaller of the freespace end and the
* maximal requested end.
*/
end = fend < maxend ? fend : maxend;
/*
* Fix the length according to mod and prod if given.
*/
rlen = xfs_alloc_fix_len(mod, prod, end - bno);
end = bno + rlen;
/*
* Give up if the resuling extent is too small.
*/
if (end < minend) {
xfs_btree_del_cursor(bno_cur);
return NULLAGBLOCK;
}
/*
* We are allocating bno for rlen [bno .. end)
*/
/*
* Allocate/initialize a cursor for the by-size btree.
*/
cnt_cur = xfs_btree_init_cursor(mp, tp, agbuf, agno, XFS_BTNUM_CNT, 0);
/*
* Look up the previously found extent.
*/
{
int i;
i = xfs_alloc_lookup_eq(cnt_cur, fbno, flen);
ASSERT(i == 1);
}
/*
* Delete the extent from the by-size btree.
*/
xfs_alloc_delete(cnt_cur);
/*
* If the found freespace starts left of the allocation, add back the
* leftover freespace to the by-size btree.
*/
if (fbno < bno) {
xfs_alloc_lookup_eq(cnt_cur, fbno, bno - fbno);
xfs_alloc_insert(cnt_cur);
}
/*
* If the found freespace ends right of the allocation, add back the
* leftover freespace to the by-size btree.
*/
if (fend > end) {
xfs_alloc_lookup_eq(cnt_cur, end, fend - end);
xfs_alloc_insert(cnt_cur);
}
xfs_alloc_rcheck(cnt_cur);
xfs_alloc_kcheck(cnt_cur);
xfs_btree_del_cursor(cnt_cur);
/*
* If the found freespace matches the allocation, just delete it
* from the by-bno btree.
*/
if (fbno == bno && fend == end)
xfs_alloc_delete(bno_cur);
/*
* If the found freespace starts at the same block but is longer,
* just update the by-bno btree entry to be shorter.
*/
else if (fbno == bno)
xfs_alloc_update(bno_cur, end, fend - end);
else {
/*
* If the found freespace starts left of the allocation,
* update the length of that by-bno entry.
*/
xfs_alloc_update(bno_cur, fbno, bno - fbno);
/*
* ... and if the found freespace ends right of the
* allocation, add another btree entry with the leftover space.
*/
if (fend > end) {
xfs_alloc_lookup_eq(bno_cur, end, fend - end);
xfs_alloc_insert(bno_cur);
}
}
*len = end - bno;
xfs_alloc_rcheck(bno_cur);
xfs_alloc_kcheck(bno_cur);
xfs_btree_del_cursor(bno_cur);
return bno;
}
/*
* Allocate a variable extent near bno in the allocation group agno.
* Extent's length (returned in *len) will be between minlen and maxlen,
* and of the form k * prod + mod unless there's nothing that large.
* Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
*/
STATIC xfs_agblock_t /* starting block number of allocated extent */
xfs_alloc_ag_vextent_near(
xfs_trans_t *tp, /* transaction pointer */
buf_t *agbuf, /* buffer for a.g. freelist header */
xfs_agnumber_t agno, /* allocation group number */
xfs_agblock_t bno, /* allocation-group relative block number */
xfs_extlen_t minlen, /* mininum size of extent */
xfs_extlen_t maxlen, /* maximum size of extent */
xfs_extlen_t *len, /* output: size of extent */
xfs_extlen_t mod, /* mod value for extent size */
xfs_extlen_t prod) /* prod value for extent size */
{
xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
xfs_agblock_t gtbno; /* start bno of right side entry */
xfs_extlen_t gtdiff; /* difference to right side entry */
xfs_agblock_t gtend; /* end bno of right side entry */
xfs_extlen_t gtlen; /* length of right side entry */
xfs_agblock_t gtnew; /* useful start bno of right side */
xfs_agblock_t gtnewend; /* useful end bno of right side */
int i; /* result code, temporary */
xfs_agblock_t ltbno; /* start bno of left side entry */
xfs_extlen_t ltdiff; /* difference to left side entry */
xfs_agblock_t ltend; /* end bno of left side entry */
xfs_extlen_t ltlen; /* length of left side entry */
xfs_agblock_t ltnew; /* useful start bno of left side */
xfs_agblock_t ltnewend; /* useful end bno of left side */
xfs_mount_t *mp; /* mount point structure for filesys */
xfs_extlen_t rlen; /* length of allocated extent */
mp = tp->t_mountp;
/*
* Get a cursor for the by-size btree.
*/
cnt_cur = xfs_btree_init_cursor(mp, tp, agbuf, agno, XFS_BTNUM_CNT, 0);
ltlen = 0;
/*
* See if there are any free extents as big as maxlen.
*/
if (!xfs_alloc_lookup_ge(cnt_cur, 0, maxlen)) {
/*
* Nothing as big as maxlen.
* Is there anything at all in the tree?
* If so get the biggest extent.
*/
if (xfs_alloc_decrement(cnt_cur, 0))
xfs_alloc_get_rec(cnt_cur, <bno, <len);
/*
* If nothing, or what we got is too small, give up.
*/
if (ltlen < minlen) {
xfs_btree_del_cursor(cnt_cur);
return NULLAGBLOCK;
}
}
/*
* First algorithm.
* If the requested extent is large wrt the freespaces available
* in this a.g., then the cursor will be pointing to a btree entry
* near the right edge of the tree. If it's in the last btree leaf
* block, then we just examine all the entries in that block
* that are big enough, and pick the best one.
*/
if (xfs_btree_islastblock(cnt_cur, 0)) {
xfs_extlen_t bdiff;
int besti;
xfs_agblock_t bnew;
/*
* Start from the entry that lookup found, sequence through
* all larger free blocks.
*/
i = cnt_cur->bc_ptrs[0];
besti = 0;
bdiff = ltdiff = (xfs_extlen_t)0;
do {
/*
* For each entry, decide if it's better than
* the previous best entry.
*/
xfs_alloc_get_rec(cnt_cur, <bno, <len);
rlen = ltlen < maxlen ? ltlen : maxlen;
rlen = xfs_alloc_fix_len(mod, prod, rlen);
if (rlen < minlen)
ltdiff = 1; /* just skip it */
else {
ltdiff = xfs_alloc_compute_diff(bno, rlen,
ltbno, ltlen, <new);
if (!besti || ltdiff < bdiff) {
bdiff = ltdiff;
bnew = ltnew;
besti = i;
}
}
i++;
} while (ltdiff > 0 && xfs_alloc_increment(cnt_cur, 0));
/*
* Point at the best entry, and retrieve it again.
*/
cnt_cur->bc_ptrs[0] = besti;
xfs_alloc_get_rec(cnt_cur, <bno, <len);
ltend = ltbno + ltlen;
rlen = ltlen < maxlen ? ltlen : maxlen;
rlen = xfs_alloc_fix_len(mod, prod, rlen);
/*
* Delete that entry from the by-size tree.
*/
xfs_alloc_delete(cnt_cur);
/*
* We are allocating starting at bnew for rlen blocks.
*/
ltnew = bnew;
ltnewend = bnew + rlen;
*len = rlen;
/*
* Set up a cursor for the by-bno tree.
*/
bno_cur_lt = xfs_btree_init_cursor(mp, tp, agbuf, agno,
XFS_BTNUM_BNO, 0);
/*
* Find the entry we used.
*/
i = xfs_alloc_lookup_eq(bno_cur_lt, ltbno, ltlen);
ASSERT(i == 1);
/*
* Freespace properly contains allocated space.
*/
if (ltbno < ltnew && ltend > ltnewend) {
/*
* Insert two leftover entries into the cnt tree.
* Update the bno entry, and add a new one for the
* new (leftover on the right) freespace.
*/
xfs_alloc_lookup_eq(cnt_cur, ltbno, ltnew - ltbno);
xfs_alloc_insert(cnt_cur);
xfs_alloc_update(bno_cur_lt, ltbno, ltnew - ltbno);
xfs_alloc_lookup_eq(cnt_cur, ltnewend, ltend - ltnewend);
xfs_alloc_insert(cnt_cur);
xfs_alloc_lookup_eq(bno_cur_lt, ltnewend, ltend - ltnewend);
xfs_alloc_insert(bno_cur_lt);
/*
* Freespace contains allocated space, matches at left side.
*/
} else if (ltend > ltnewend) {
/*
* Insert the leftover entry for the cnt tree.
* Update the bno entry to have just the leftover space.
*/
xfs_alloc_lookup_eq(cnt_cur, ltnewend, ltend - ltnewend);
xfs_alloc_insert(cnt_cur);
xfs_alloc_update(bno_cur_lt, ltnewend, ltend - ltnewend);
/*
* Freespace contains allocated space, matches at right side.
*/
} else if (ltbno < ltnew) {
/*
* Insert the leftover entry for the cnt tree.
* Update the bno entry to have just the leftover space.
*/
xfs_alloc_lookup_eq(cnt_cur, ltbno, ltnew - ltbno);
xfs_alloc_insert(cnt_cur);
xfs_alloc_update(bno_cur_lt, ltbno, ltnew - ltbno);
/*
* Freespace same size as allocated.
*/
} else {
/*
* Just delete the bno tree entry.
*/
xfs_alloc_delete(bno_cur_lt);
}
xfs_alloc_rcheck(cnt_cur);
xfs_alloc_kcheck(cnt_cur);
xfs_btree_del_cursor(cnt_cur);
xfs_alloc_rcheck(bno_cur_lt);
xfs_alloc_kcheck(bno_cur_lt);
xfs_btree_del_cursor(bno_cur_lt);
return ltnew;
}
/*
* Second algorithm.
* Search in the by-bno tree to the left and to the right
* simultaneously, until in each case we find a space big enough,
* or run into the edge of the tree. When we run into the edge,
* we deallocate that cursor.
* If both searches succeed, we compare the two spaces and pick
* the better one.
*/
/*
* Allocate and initialize the cursor for the leftward search.
*/
bno_cur_lt = xfs_btree_init_cursor(mp, tp, agbuf, agno,
XFS_BTNUM_BNO, 0);
/*
* Lookup <= bno to find the leftward search's starting point.
*/
if (!xfs_alloc_lookup_le(bno_cur_lt, bno, maxlen)) {
/*
* Didn't find anything; use this cursor for the rightward
* search.
*/
bno_cur_gt = bno_cur_lt;
bno_cur_lt = 0;
} else {
/*
* Found something. Duplicate the cursor for the rightward
* search.
*/
bno_cur_gt = xfs_btree_dup_cursor(bno_cur_lt);
}
/*
* Increment the cursor, so we will point at the entry just right
* of the leftward entry if any, or to the leftmost entry.
*/
if (!xfs_alloc_increment(bno_cur_gt, 0)) {
/*
* It failed, there are no rightward entries.
*/
xfs_btree_del_cursor(bno_cur_gt);
bno_cur_gt = 0;
}
/*
* Loop going left with the leftward cursor, right with the
* rightward cursor, until either both directions give up or
* we find an entry at least as big as minlen.
*/
do {
if (bno_cur_lt) {
xfs_alloc_get_rec(bno_cur_lt, <bno, <len);
if (ltlen >= minlen)
break;
if (!xfs_alloc_decrement(bno_cur_lt, 0)) {
xfs_btree_del_cursor(bno_cur_lt);
bno_cur_lt = 0;
}
}
if (bno_cur_gt) {
xfs_alloc_get_rec(bno_cur_gt, >bno, >len);
if (gtlen >= minlen)
break;
if (!xfs_alloc_increment(bno_cur_gt, 0)) {
xfs_btree_del_cursor(bno_cur_gt);
bno_cur_gt = 0;
}
}
} while (bno_cur_lt || bno_cur_gt);
/*
* We have to find something as big as minlen, we've already checked.
*/
ASSERT(bno_cur_lt || bno_cur_gt);
/*
* Got both cursors still active, need to find better entry.
*/
if (bno_cur_lt && bno_cur_gt) {
/*
* Left side is long enough, look for a right side entry.
*/
if (ltlen >= minlen) {
/*
* Fix up the length.
*/
rlen = ltlen < maxlen ? ltlen : maxlen;
rlen = xfs_alloc_fix_len(mod, prod, rlen);
ltdiff = xfs_alloc_compute_diff(bno, rlen, ltbno,
ltlen, <new);
/*
* Not perfect.
*/
if (ltdiff) {
/*
* Look until we find a better one, run out of
* space, or run off the end.
*/
while (bno_cur_lt && bno_cur_gt) {
xfs_alloc_get_rec(bno_cur_gt, >bno,
>len);
/*
* The left one is clearly better.
*/
if (gtbno >= bno + ltdiff) {
xfs_btree_del_cursor(bno_cur_gt);
bno_cur_gt = 0;
/*
* Do a fix_len and compare the
* differences.
*/
} else if (gtlen >= minlen) {
rlen = gtlen < maxlen ?
gtlen : maxlen;
rlen = xfs_alloc_fix_len(mod,
prod, rlen);
gtdiff =
xfs_alloc_compute_diff(bno,
rlen, gtbno, gtlen, >new);
/*
* Right side is better.
*/
if (gtdiff < ltdiff) {
xfs_btree_del_cursor(bno_cur_lt);
bno_cur_lt = 0;
}
/*
* Fell off the right end.
*/
} else if (!xfs_alloc_increment(bno_cur_gt, 0)) {
xfs_btree_del_cursor(bno_cur_gt);
bno_cur_gt = 0;
}
}
/*
* The left side is perfect, trash the right side.
*/
} else {
xfs_btree_del_cursor(bno_cur_gt);
bno_cur_gt = 0;
}
/*
* It's the right side that was found first, look left.
*/
} else {
/*
* Fix up the length.
*/
rlen = gtlen < maxlen ? gtlen : maxlen;
rlen = xfs_alloc_fix_len(mod, prod, rlen);
gtdiff = xfs_alloc_compute_diff(bno, rlen, gtbno, gtlen, >new);
/*
* Right side entry isn't perfect.
*/
if (gtdiff) {
/*
* Look until we find a better one, run out of
* space, or run off the end.
*/
while (bno_cur_lt && bno_cur_gt) {
xfs_alloc_get_rec(bno_cur_lt, <bno, <len);
/*
* The right one is clearly better.
*/
if (ltbno <= bno - gtdiff) {
xfs_btree_del_cursor(bno_cur_lt);
bno_cur_lt = 0;
/*
* Do a fix_len and compare the
* differences.
*/
} else if (ltlen >= minlen) {
rlen = ltlen < maxlen ? ltlen : maxlen;
rlen = xfs_alloc_fix_len(mod, prod, rlen);
ltdiff = xfs_alloc_compute_diff(bno, rlen, ltbno, ltlen, <new);
/*
* Left side is better.
*/
if (ltdiff < gtdiff) {
xfs_btree_del_cursor(bno_cur_gt);
bno_cur_gt = 0;
}
/*
* Fell off the left end.
*/
} else if (!xfs_alloc_decrement(bno_cur_lt, 0)) {
xfs_btree_del_cursor(bno_cur_lt);
bno_cur_lt = 0;
}
}
/*
* The right side is perfect, trash the left side.
*/
} else {
xfs_btree_del_cursor(bno_cur_lt);
bno_cur_lt = 0;
}
}
}
/*
* At this point we have selected a freespace entry, either to the
* left or to the right.
*/
/*
* On the left side.
*/
if (bno_cur_lt) {
/*
* Fix up the length and compute the useful address.
*/
ltend = ltbno + ltlen;
rlen = ltlen < maxlen ? ltlen : maxlen;
rlen = xfs_alloc_fix_len(mod, prod, rlen);
ltdiff = xfs_alloc_compute_diff(bno, rlen, ltbno, ltlen, <new);
ltnewend = ltnew + rlen;
*len = rlen;
i = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen);
ASSERT(i == 1);
xfs_alloc_delete(cnt_cur);
/*
* Freespace properly contains allocated space.
*/
if (ltbno < ltnew && ltend > ltnewend) {
xfs_alloc_lookup_eq(cnt_cur, ltbno, ltnew - ltbno);
xfs_alloc_insert(cnt_cur);
xfs_alloc_update(bno_cur_lt, ltbno, ltnew - ltbno);
xfs_alloc_lookup_eq(cnt_cur, ltnewend, ltend - ltnewend);
xfs_alloc_insert(cnt_cur);
xfs_alloc_lookup_eq(bno_cur_lt, ltnewend, ltend - ltnewend);
xfs_alloc_insert(bno_cur_lt);
/*
* Freespace contains allocated space, matches at left side.
*/
} else if (ltend > ltnewend) {
xfs_alloc_lookup_eq(cnt_cur, ltnewend, ltend - ltnewend);
xfs_alloc_insert(cnt_cur);
xfs_alloc_update(bno_cur_lt, ltnewend, ltend - ltnewend);
/*
* Freespace contains allocated space, matches at right side.
*/
} else if (ltbno < ltnew) {
xfs_alloc_lookup_eq(cnt_cur, ltbno, ltnew - ltbno);
xfs_alloc_insert(cnt_cur);
xfs_alloc_update(bno_cur_lt, ltbno, ltnew - ltbno);
/*
* Freespace same size as allocated.
*/
} else
xfs_alloc_delete(bno_cur_lt);
xfs_alloc_rcheck(cnt_cur);
xfs_alloc_kcheck(cnt_cur);
xfs_btree_del_cursor(cnt_cur);
xfs_alloc_rcheck(bno_cur_lt);
xfs_alloc_kcheck(bno_cur_lt);
xfs_btree_del_cursor(bno_cur_lt);
return ltnew;
/*
* On the right side.
*/
} else {
/*
* Fix up the length and compute the useful address.
*/
gtend = gtbno + gtlen;
rlen = gtlen < maxlen ? gtlen : maxlen;
rlen = xfs_alloc_fix_len(mod, prod, rlen);
gtdiff = xfs_alloc_compute_diff(bno, rlen, gtbno, gtlen, >new);
gtnewend = gtnew + rlen;
*len = rlen;
i = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen);
ASSERT(i == 1);
xfs_alloc_delete(cnt_cur);
/*
* Other cases can't occur since gtbno > bno.
*/
/*
* Freespace contains allocated space, matches at left side.
*/
if (gtend > gtnewend) {
xfs_alloc_lookup_eq(cnt_cur, gtnewend, gtend - gtnewend);
xfs_alloc_insert(cnt_cur);
xfs_alloc_update(bno_cur_gt, gtnewend, gtend - gtnewend);
/*
* Freespace same size as allocated.
*/
} else
xfs_alloc_delete(bno_cur_gt);
xfs_alloc_rcheck(cnt_cur);
xfs_alloc_kcheck(cnt_cur);
xfs_btree_del_cursor(cnt_cur);
xfs_alloc_rcheck(bno_cur_gt);
xfs_alloc_kcheck(bno_cur_gt);
xfs_btree_del_cursor(bno_cur_gt);
return gtnew;
}
}
/*
* Allocate a variable extent anywhere in the allocation group agno.
* Extent's length (returned in *len) will be between minlen and maxlen,
* and of the form k * prod + mod unless there's nothing that large.
* Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
*/
STATIC xfs_agblock_t /* starting block number of allocated extent */
xfs_alloc_ag_vextent_size(
xfs_trans_t *tp, /* transaction pointer */
buf_t *agbuf, /* buffer for a.g. freelist header */
xfs_agnumber_t agno, /* allocation group number */
xfs_extlen_t minlen, /* mininum size of extent */
xfs_extlen_t maxlen, /* maximum size of extent */
xfs_extlen_t *len, /* output: size of extent */
xfs_extlen_t mod, /* mod value for extent size */
xfs_extlen_t prod) /* prod value for extent size */
{
xfs_btree_cur_t *bno_cur;
xfs_btree_cur_t *cnt_cur;
xfs_agblock_t fbno;
xfs_extlen_t flen = 0;
int i;
xfs_extlen_t k;
xfs_mount_t *mp;
xfs_extlen_t rlen;
mp = tp->t_mountp;
cnt_cur = xfs_btree_init_cursor(mp, tp, agbuf, agno, XFS_BTNUM_CNT, 0);
if (!xfs_alloc_lookup_ge(cnt_cur, 0, maxlen)) {
if (xfs_alloc_decrement(cnt_cur, 0))
xfs_alloc_get_rec(cnt_cur, &fbno, &flen);
if (flen < minlen) {
xfs_btree_del_cursor(cnt_cur);
return NULLAGBLOCK;
}
rlen = flen;
} else {
xfs_alloc_get_rec(cnt_cur, &fbno, &flen);
rlen = maxlen;
}
rlen = xfs_alloc_fix_len(mod, prod, rlen);
if (rlen < minlen) {
xfs_btree_del_cursor(cnt_cur);
return NULLAGBLOCK;
}
xfs_alloc_delete(cnt_cur);
bno_cur = xfs_btree_init_cursor(mp, tp, agbuf, agno, XFS_BTNUM_BNO, 0);
i = xfs_alloc_lookup_eq(bno_cur, fbno, flen);
ASSERT(i == 1);
if (rlen < flen) {
xfs_alloc_lookup_eq(cnt_cur, fbno + rlen, flen - rlen);
xfs_alloc_insert(cnt_cur);
xfs_alloc_update(bno_cur, fbno + rlen, flen - rlen);
} else
xfs_alloc_delete(bno_cur);
*len = rlen;
xfs_alloc_rcheck(bno_cur);
xfs_alloc_kcheck(bno_cur);
xfs_btree_del_cursor(bno_cur);
xfs_alloc_rcheck(cnt_cur);
xfs_alloc_kcheck(cnt_cur);
xfs_btree_del_cursor(cnt_cur);
return fbno;
}
/*
* Free the extent starting at agno/bno for length.
*/
STATIC int /* return success/failure, will be void */
xfs_free_ag_extent(
xfs_trans_t *tp, /* transaction pointer */
buf_t *agbuf, /* buffer for a.g. freelist header */
xfs_agnumber_t agno, /* allocation group number */
xfs_agblock_t bno, /* starting block number */
xfs_extlen_t len) /* length of extent */
{
xfs_agf_t *agf;
xfs_btree_cur_t *bno_cur;
xfs_btree_cur_t *cnt_cur;
xfs_extlen_t flen = len;
xfs_agblock_t gtbno;
xfs_extlen_t gtlen;
int haveleft;
int haveright;
int i;
xfs_agblock_t ltbno;
xfs_extlen_t ltlen;
xfs_mount_t *mp;
xfs_agblock_t nbno;
xfs_extlen_t nlen;
mp = tp->t_mountp;
bno_cur = xfs_btree_init_cursor(mp, tp, agbuf, agno, XFS_BTNUM_BNO, 0);
if (haveleft = xfs_alloc_lookup_le(bno_cur, bno, len)) {
xfs_alloc_get_rec(bno_cur, <bno, <len);
if (ltbno + ltlen < bno)
haveleft = 0;
else if (ltbno + ltlen > bno) {
xfs_btree_del_cursor(bno_cur);
return 0;
}
}
if (haveright = xfs_alloc_increment(bno_cur, 0)) {
xfs_alloc_get_rec(bno_cur, >bno, >len);
if (bno + len < gtbno)
haveright = 0;
else if (gtbno < bno + len) {
xfs_btree_del_cursor(bno_cur);
return 0;
}
}
cnt_cur = xfs_btree_init_cursor(mp, tp, agbuf, agno, XFS_BTNUM_CNT, 0);
if (haveleft && haveright) {
i = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen);
ASSERT(i == 1);
xfs_alloc_delete(cnt_cur);
i = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen);
ASSERT(i == 1);
xfs_alloc_delete(cnt_cur);
xfs_alloc_delete(bno_cur);
xfs_alloc_decrement(bno_cur, 0);
#ifdef XFSDEBUG
{ xfs_agblock_t xxbno; xfs_extlen_t xxlen;
xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen);
ASSERT(xxbno == ltbno);
ASSERT(xxlen == ltlen);
}
#endif
nbno = ltbno;
nlen = len + ltlen + gtlen;
xfs_alloc_update(bno_cur, nbno, nlen);
} else if (haveleft) {
i = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen);
ASSERT(i == 1);
xfs_alloc_delete(cnt_cur);
xfs_alloc_decrement(bno_cur, 0);
nbno = ltbno;
nlen = len + ltlen;
xfs_alloc_update(bno_cur, nbno, nlen);
} else if (haveright) {
i = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen);
ASSERT(i == 1);
xfs_alloc_delete(cnt_cur);
nbno = bno;
nlen = len + gtlen;
xfs_alloc_update(bno_cur, nbno, nlen);
} else {
nbno = bno;
nlen = len;
xfs_alloc_insert(bno_cur);
}
xfs_alloc_rcheck(bno_cur);
xfs_alloc_kcheck(bno_cur);
xfs_btree_del_cursor(bno_cur);
xfs_alloc_lookup_eq(cnt_cur, nbno, nlen);
xfs_alloc_insert(cnt_cur);
xfs_alloc_rcheck(cnt_cur);
xfs_alloc_kcheck(cnt_cur);
agf = xfs_buf_to_agf(agbuf);
agf->agf_freeblks += flen;
xfs_alloc_log_agf(tp, agbuf, XFS_AGF_FREEBLKS);
xfs_btree_del_cursor(cnt_cur);
xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (int)flen);
return 1;
}
/*
* Visible (exported) allocation/free functions.
* Some of these are used just by xfs_alloc_btree.c and this file.
*/
/*
* Return the number of free blocks left in the allocation group.
*/
xfs_extlen_t /* number of remaining free blocks */
xfs_alloc_ag_freeblks(
xfs_mount_t *mp, /* file system mount structure */
xfs_trans_t *tp, /* transaction pointer */
xfs_agnumber_t agno, /* allocation group number */
int flags) /* XFS_ALLOC_FLAG_... */
{
buf_t *agbuf;
xfs_agf_t *agf;
xfs_extlen_t freeblks;
agbuf = xfs_alloc_read_agf(mp, tp, agno, flags);
agf = xfs_buf_to_agf(agbuf);
freeblks = agf->agf_freeblks;
xfs_trans_brelse(tp, agbuf);
return freeblks;
}
/*
* Allocate an extent (fixed-size).
* Return the extent's starting block, NULLFSBLOCK on failure.
*/
xfs_fsblock_t /* extent's starting block */
xfs_alloc_extent(
xfs_trans_t *tp, /* transaction pointer */
xfs_fsblock_t bno, /* requested starting block */
xfs_extlen_t len, /* requested length */
xfs_alloctype_t type, /* allocation type, see above defn */
xfs_extlen_t total, /* total blocks needed in transaction */
int wasdel) /* extent was previously del-alloced */
{
xfs_extlen_t rlen;
xfs_fsblock_t rval;
rval = xfs_alloc_vextent(tp, bno, len, len, &rlen, type, total, wasdel,
0, 1);
if (rval != NULLFSBLOCK)
ASSERT(rlen == len);
return rval;
}
/*
* Decide whether to use this allocation group for this allocation.
* If so, fix up the btree freelist's size.
* This is external so mkfs can call it, too.
*/
buf_t * /* buffer for the a.g. freelist header */
xfs_alloc_fix_freelist(
xfs_trans_t *tp, /* transaction pointer */
xfs_agnumber_t agno, /* allocation group number */
xfs_extlen_t minlen, /* minimum extent length, else reject */
xfs_extlen_t total, /* total free blocks, else reject */
int flags) /* XFS_ALLOC_FLAG_... */
{
xfs_agblock_t agbno;
buf_t *agbuf;
xfs_agf_t *agf;
xfs_agblock_t bno;
buf_t *buf;
xfs_extlen_t i;
xfs_mount_t *mp;
xfs_extlen_t need;
mp = tp->t_mountp;
agbuf = xfs_alloc_read_agf(mp, tp, agno, flags);
if (!agbuf)
return agbuf;
agf = xfs_buf_to_agf(agbuf);
need = XFS_MIN_FREELIST(agf);
if (total && agf->agf_freecount < need)
total += need - agf->agf_freecount;
if (minlen > agf->agf_longest || total > agf->agf_freeblks) {
xfs_trans_brelse(tp, agbuf);
return NULL;
}
while (agf->agf_freecount > need) {
bno = xfs_alloc_get_freelist(tp, agbuf, &buf);
xfs_free_ag_extent(tp, agbuf, agno, bno, 1);
}
while (agf->agf_freecount < need) {
i = need - agf->agf_freecount;
agbno = xfs_alloc_ag_vextent(tp, agbuf, agno, 0, 1, i, &i,
XFS_ALLOCTYPE_THIS_AG, 0, 0, 1);
if (agbno == NULLAGBLOCK)
break;
for (bno = agbno + i - 1; bno >= agbno; bno--) {
buf = xfs_btree_get_bufs(mp, tp, agno, bno, 0);
xfs_alloc_put_freelist(tp, agbuf, buf);
}
}
return agbuf;
}
/*
* Get a block from the freelist.
* Returns with the buffer for the block gotten.
*/
xfs_agblock_t /* block address retrieved from freelist */
xfs_alloc_get_freelist(
xfs_trans_t *tp, /* transaction pointer */
buf_t *agbuf, /* buffer containing the agf structure */
buf_t **bufp) /* out: buffer pointer for the free block */
{
xfs_agf_t *agf;
xfs_agblock_t bno;
xfs_btree_sblock_t *block;
buf_t *buf;
xfs_mount_t *mp;
mp = tp->t_mountp;
agf = xfs_buf_to_agf(agbuf);
bno = agf->agf_freelist;
if (bno == NULLAGBLOCK)
return NULLAGBLOCK;
buf = xfs_btree_read_bufs(mp, tp, agf->agf_seqno, bno, 0);
block = xfs_buf_to_sblock(buf);
agf->agf_freecount--;
agf->agf_freelist = *(xfs_agblock_t *)block;
xfs_alloc_log_agf(tp, agbuf, XFS_AGF_FREELIST | XFS_AGF_FREECOUNT);
*bufp = buf;
kmem_check();
return bno;
}
/*
* Log the given fields from the agf structure.
*/
void
xfs_alloc_log_agf(
xfs_trans_t *tp, /* transaction pointer */
buf_t *buf, /* buffer for a.g. freelist header */
int fields) /* mask of fields to be logged (XFS_AGF_...) */
{
int first;
int last;
static const int offsets[] = {
offsetof(xfs_agf_t, agf_magicnum),
offsetof(xfs_agf_t, agf_versionnum),
offsetof(xfs_agf_t, agf_seqno),
offsetof(xfs_agf_t, agf_length),
offsetof(xfs_agf_t, agf_roots[0]),
offsetof(xfs_agf_t, agf_levels[0]),
offsetof(xfs_agf_t, agf_freelist),
offsetof(xfs_agf_t, agf_freecount),
offsetof(xfs_agf_t, agf_freeblks),
offsetof(xfs_agf_t, agf_longest),
sizeof(xfs_agf_t)
};
xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
xfs_trans_log_buf(tp, buf, (uint)first, (uint)last);
kmem_check();
}
/*
* Find the next freelist block number.
*/
xfs_agblock_t /* a.g.-relative block number for btree list */
xfs_alloc_next_free(
xfs_mount_t *mp, /* file system mount structure */
xfs_trans_t *tp, /* transaction pointer */
buf_t *agbuf, /* buffer for a.g. freelist header */
xfs_agblock_t bno) /* current freelist block number */
{
xfs_agf_t *agf;
buf_t *buf;
agf = xfs_buf_to_agf(agbuf);
buf = xfs_btree_read_bufs(mp, tp, agf->agf_seqno, bno, 0);
bno = *(xfs_agblock_t *)buf->b_un.b_addr;
xfs_trans_brelse(tp, buf);
return bno;
}
/*
* Put the buffer on the freelist for the allocation group.
*/
void
xfs_alloc_put_freelist(
xfs_trans_t *tp, /* transaction pointer */
buf_t *agbuf, /* buffer for a.g. freelist header */
buf_t *buf) /* buffer for the block being freed */
{
xfs_agf_t *agf;
xfs_btree_sblock_t *block;
xfs_agblock_t bno;
xfs_mount_t *mp;
xfs_sb_t *sbp;
mp = tp->t_mountp;
sbp = &mp->m_sb;
agf = xfs_buf_to_agf(agbuf);
block = xfs_buf_to_sblock(buf);
/*
* Point the new block to the old head of the list.
*/
*(xfs_agblock_t *)block = agf->agf_freelist;
xfs_trans_log_buf(tp, buf, 0, (int)sizeof(xfs_agblock_t) - 1);
bno = xfs_daddr_to_agbno(sbp, buf->b_blkno);
agf->agf_freelist = bno;
agf->agf_freecount++;
xfs_alloc_log_agf(tp, agbuf, XFS_AGF_FREELIST | XFS_AGF_FREECOUNT);
kmem_check();
}
/*
* Allocate an extent (variable-size).
*/
xfs_fsblock_t /* extent's starting block, or NULLFSBLOCK */
xfs_alloc_vextent(
xfs_trans_t *tp, /* transaction pointer */
xfs_fsblock_t bno, /* requested starting block */
xfs_extlen_t minlen, /* minimum requested length */
xfs_extlen_t maxlen, /* maximum requested length */
xfs_extlen_t *len, /* output: actual allocated length */
xfs_alloctype_t type, /* allocation type, see above definition */
xfs_extlen_t total, /* total blocks needed in transaction */
int wasdel, /* extent was previously delayed-allocated */
xfs_extlen_t mod, /* length should be k * prod + mod unless */
xfs_extlen_t prod) /* there's nothing as big as mod */
{
xfs_agblock_t agbno;
buf_t *agbuf;
xfs_agnumber_t agno;
xfs_agblock_t agsize;
int flags;
xfs_mount_t *mp;
xfs_alloctype_t ntype;
xfs_sb_t *sbp;
xfs_agnumber_t tagno;
ntype = type;
mp = tp->t_mountp;
sbp = &mp->m_sb;
agsize = sbp->sb_agblocks;
/*
* These should really be asserts, left this way for now just
* for the benefit of xfs_test.
*/
if (xfs_fsb_to_agno(sbp, bno) >= sbp->sb_agcount ||
xfs_fsb_to_agbno(sbp, bno) >= sbp->sb_agblocks || minlen > maxlen ||
minlen > agsize || len == 0 || mod >= prod)
return NULLFSBLOCK;
if (maxlen > agsize)
maxlen = agsize;
agbno = NULLAGBLOCK;
ntype = XFS_ALLOCTYPE_THIS_AG;
switch (type) {
case XFS_ALLOCTYPE_THIS_AG:
case XFS_ALLOCTYPE_NEAR_BNO:
case XFS_ALLOCTYPE_THIS_BNO:
agno = xfs_fsb_to_agno(sbp, bno);
agbuf = xfs_alloc_fix_freelist(tp, agno, minlen, total, 1);
if (!agbuf)
break;
agbno = xfs_fsb_to_agbno(sbp, bno);
agbno = xfs_alloc_ag_vextent(tp, agbuf, agno, agbno, minlen,
maxlen, len, type, wasdel, mod, prod);
break;
case XFS_ALLOCTYPE_START_BNO:
agbno = xfs_fsb_to_agbno(sbp, bno);
ntype = XFS_ALLOCTYPE_NEAR_BNO;
/* FALLTHROUGH */
case XFS_ALLOCTYPE_ANY_AG:
case XFS_ALLOCTYPE_START_AG:
flags = XFS_ALLOC_FLAG_TRYLOCK;
if (type == XFS_ALLOCTYPE_ANY_AG)
tagno = agno = mp->m_agrotor;
else
tagno = agno = xfs_fsb_to_agno(sbp, bno);
for (;;) {
agbuf = xfs_alloc_fix_freelist(tp, tagno, minlen,
total, flags);
if (agbuf) {
agbno = xfs_alloc_ag_vextent(tp, agbuf, tagno,
agbno, minlen, maxlen, len, ntype,
wasdel, mod, prod);
break;
}
if (tagno == agno && type == XFS_ALLOCTYPE_START_BNO)
ntype = XFS_ALLOCTYPE_THIS_AG;
if (++tagno == sbp->sb_agcount)
tagno = 0;
if (tagno == agno) {
if (!flags)
break;
flags = 0;
if (type == XFS_ALLOCTYPE_START_BNO)
ntype = XFS_ALLOCTYPE_NEAR_BNO;
}
}
agno = tagno;
mp->m_agrotor = (agno + 1) % sbp->sb_agcount;
break;
default:
ASSERT(0);
/* NOTREACHED */
}
if (agbno == NULLAGBLOCK)
return NULLFSBLOCK;
else
return xfs_agb_to_fsb(sbp, agno, agbno);
}
/*
* Free an extent.
*/
int /* success/failure; will become void */
xfs_free_extent(
xfs_trans_t *tp, /* transaction pointer */
xfs_fsblock_t bno, /* starting block number of extent */
xfs_extlen_t len) /* length of extent */
{
xfs_agblock_t agbno;
buf_t *agbuf;
xfs_agnumber_t agno;
int i;
xfs_mount_t *mp;
xfs_sb_t *sbp;
mp = tp->t_mountp;
sbp = &mp->m_sb;
ASSERT(len != 0);
agno = xfs_fsb_to_agno(sbp, bno);
ASSERT(agno < sbp->sb_agcount);
agbno = xfs_fsb_to_agbno(sbp, bno);
ASSERT(agbno < sbp->sb_agblocks);
agbuf = xfs_alloc_fix_freelist(tp, agno, 0, 0, 1);
ASSERT(agbuf != NULL);
i = xfs_free_ag_extent(tp, agbuf, agno, agbno, len);
return i;
}