/*
* Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it would be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "xfs.h"
#include "xfs_fs.h"
#include "xfs_types.h"
#include "xfs_bit.h"
#include "xfs_log.h"
#include "xfs_inum.h"
#include "xfs_trans.h"
#include "xfs_sb.h"
#include "xfs_ag.h"
#include "xfs_dir2.h"
#include "xfs_dmapi.h"
#include "xfs_mount.h"
#include "xfs_bmap_btree.h"
#include "xfs_alloc_btree.h"
#include "xfs_ialloc_btree.h"
#include "xfs_dir2_sf.h"
#include "xfs_attr_sf.h"
#include "xfs_dinode.h"
#include "xfs_inode.h"
#include "xfs_btree.h"
#include "xfs_ialloc.h"
#include "xfs_alloc.h"
#include "xfs_rtalloc.h"
#include "xfs_error.h"
#include "xfs_bmap.h"
/*
* Log specified fields for the inode given by bp and off.
*/
STATIC void
xfs_ialloc_log_di(
xfs_trans_t *tp, /* transaction pointer */
xfs_buf_t *bp, /* inode buffer */
int off, /* index of inode in buffer */
int fields) /* bitmask of fields to log */
{
int first; /* first byte number */
int ioffset; /* off in bytes */
int last; /* last byte number */
xfs_mount_t *mp; /* mount point structure */
static const short offsets[] = { /* field offsets */
/* keep in sync with bits */
offsetof(xfs_dinode_core_t, di_magic),
offsetof(xfs_dinode_core_t, di_mode),
offsetof(xfs_dinode_core_t, di_version),
offsetof(xfs_dinode_core_t, di_format),
offsetof(xfs_dinode_core_t, di_onlink),
offsetof(xfs_dinode_core_t, di_uid),
offsetof(xfs_dinode_core_t, di_gid),
offsetof(xfs_dinode_core_t, di_nlink),
offsetof(xfs_dinode_core_t, di_projid),
offsetof(xfs_dinode_core_t, di_pad),
offsetof(xfs_dinode_core_t, di_atime),
offsetof(xfs_dinode_core_t, di_mtime),
offsetof(xfs_dinode_core_t, di_ctime),
offsetof(xfs_dinode_core_t, di_size),
offsetof(xfs_dinode_core_t, di_nblocks),
offsetof(xfs_dinode_core_t, di_extsize),
offsetof(xfs_dinode_core_t, di_nextents),
offsetof(xfs_dinode_core_t, di_anextents),
offsetof(xfs_dinode_core_t, di_forkoff),
offsetof(xfs_dinode_core_t, di_aformat),
offsetof(xfs_dinode_core_t, di_dmevmask),
offsetof(xfs_dinode_core_t, di_dmstate),
offsetof(xfs_dinode_core_t, di_flags),
offsetof(xfs_dinode_core_t, di_gen),
offsetof(xfs_dinode_t, di_next_unlinked),
offsetof(xfs_dinode_t, di_u),
offsetof(xfs_dinode_t, di_a),
sizeof(xfs_dinode_t)
};
ASSERT(offsetof(xfs_dinode_t, di_core) == 0);
ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0);
mp = tp->t_mountp;
/*
* Get the inode-relative first and last bytes for these fields
*/
xfs_btree_offsets(fields, offsets, XFS_DI_NUM_BITS, &first, &last);
/*
* Convert to buffer offsets and log it.
*/
ioffset = off << mp->m_sb.sb_inodelog;
first += ioffset;
last += ioffset;
xfs_trans_log_buf(tp, bp, first, last);
}
/*
* Allocation group level functions.
*/
static inline int
xfs_ialloc_cluster_alignment(
xfs_alloc_arg_t *args)
{
if (xfs_sb_version_hasalign(&args->mp->m_sb) &&
args->mp->m_sb.sb_inoalignmt >=
XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp)))
return args->mp->m_sb.sb_inoalignmt;
return 1;
}
/*
* Lookup the record equal to ino in the btree given by cur.
*/
STATIC int /* error */
xfs_inobt_lookup_eq(
struct xfs_btree_cur *cur, /* btree cursor */
xfs_agino_t ino, /* starting inode of chunk */
__int32_t fcnt, /* free inode count */
xfs_inofree_t free, /* free inode mask */
int *stat) /* success/failure */
{
cur->bc_rec.i.ir_startino = ino;
cur->bc_rec.i.ir_freecount = fcnt;
cur->bc_rec.i.ir_free = free;
return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
}
/*
* Lookup the first record greater than or equal to ino
* in the btree given by cur.
*/
int /* error */
xfs_inobt_lookup_ge(
struct xfs_btree_cur *cur, /* btree cursor */
xfs_agino_t ino, /* starting inode of chunk */
__int32_t fcnt, /* free inode count */
xfs_inofree_t free, /* free inode mask */
int *stat) /* success/failure */
{
cur->bc_rec.i.ir_startino = ino;
cur->bc_rec.i.ir_freecount = fcnt;
cur->bc_rec.i.ir_free = free;
return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
}
/*
* Lookup the first record less than or equal to ino
* in the btree given by cur.
*/
int /* error */
xfs_inobt_lookup_le(
struct xfs_btree_cur *cur, /* btree cursor */
xfs_agino_t ino, /* starting inode of chunk */
__int32_t fcnt, /* free inode count */
xfs_inofree_t free, /* free inode mask */
int *stat) /* success/failure */
{
cur->bc_rec.i.ir_startino = ino;
cur->bc_rec.i.ir_freecount = fcnt;
cur->bc_rec.i.ir_free = free;
return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
}
/*
* Update the record referred to by cur to the value given
* by [ino, fcnt, free].
* This either works (return 0) or gets an EFSCORRUPTED error.
*/
STATIC int /* error */
xfs_inobt_update(
struct xfs_btree_cur *cur, /* btree cursor */
xfs_agino_t ino, /* starting inode of chunk */
__int32_t fcnt, /* free inode count */
xfs_inofree_t free) /* free inode mask */
{
union xfs_btree_rec rec;
rec.inobt.ir_startino = cpu_to_be32(ino);
rec.inobt.ir_freecount = cpu_to_be32(fcnt);
rec.inobt.ir_free = cpu_to_be64(free);
return xfs_btree_update(cur, &rec);
}
/*
* Get the data from the pointed-to record.
*/
int /* error */
xfs_inobt_get_rec(
struct xfs_btree_cur *cur, /* btree cursor */
xfs_agino_t *ino, /* output: starting inode of chunk */
__int32_t *fcnt, /* output: number of free inodes */
xfs_inofree_t *free, /* output: free inode mask */
int *stat) /* output: success/failure */
{
union xfs_btree_rec *rec;
int error;
error = xfs_btree_get_rec(cur, &rec, stat);
if (!error && *stat == 1) {
*ino = be32_to_cpu(rec->inobt.ir_startino);
*fcnt = be32_to_cpu(rec->inobt.ir_freecount);
*free = be64_to_cpu(rec->inobt.ir_free);
}
return error;
}
/*
* Allocate new inodes in the allocation group specified by agbp.
* Return 0 for success, else error code.
*/
STATIC int /* error code or 0 */
xfs_ialloc_ag_alloc(
xfs_trans_t *tp, /* transaction pointer */
xfs_buf_t *agbp, /* alloc group buffer */
int *alloc)
{
xfs_agi_t *agi; /* allocation group header */
xfs_alloc_arg_t args; /* allocation argument structure */
int blks_per_cluster; /*
|