#include <rev_endian_fs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/proc.h>
#include <sys/file.h>
#include <sys/vnode.h>
#include <sys/ubc.h>
#if REV_ENDIAN_FS
#include <sys/mount.h>
#endif
#include <sys/vm.h>
#include <ufs/ufs/quota.h>
#include <ufs/ufs/inode.h>
#include <ufs/ufs/ufs_extern.h>
#include <ufs/ffs/fs.h>
#include <ufs/ffs/ffs_extern.h>
#if REV_ENDIAN_FS
#include <ufs/ufs/ufs_byte_order.h>
#include <architecture/byte_order.h>
#endif
ffs_balloc(ip, lbn, size, cred, bpp, flags, blk_alloc)
register struct inode *ip;
register ufs_daddr_t lbn;
int size;
struct ucred *cred;
struct buf **bpp;
int flags;
int * blk_alloc;
{
register struct fs *fs;
register ufs_daddr_t nb;
struct buf *bp, *nbp;
struct vnode *vp = ITOV(ip);
struct indir indirs[NIADDR + 2];
ufs_daddr_t newb, *bap, pref;
int deallocated, osize, nsize, num, i, error;
ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
int devBlockSize=0;
int alloc_buffer = 1;
#if REV_ENDIAN_FS
struct mount *mp=vp->v_mount;
int rev_endian=(mp->mnt_flag & MNT_REVEND);
#endif
*bpp = NULL;
if (lbn < 0)
return (EFBIG);
fs = ip->i_fs;
if (flags & B_NOBUFF)
alloc_buffer = 0;
if (blk_alloc)
*blk_alloc = 0;
nb = lblkno(fs, ip->i_size);
if (nb < NDADDR && nb < lbn) {
osize = blksize(fs, ip, nb);
if (osize < fs->fs_bsize && osize > 0) {
error = ffs_realloccg(ip, nb,
ffs_blkpref(ip, nb, (int)nb, &ip->i_db[0]),
osize, (int)fs->fs_bsize, cred, &bp);
if (error)
return (error);
ip->i_size = (nb + 1) * fs->fs_bsize;
if (UBCISVALID(vp))
ubc_setsize(vp, (off_t)ip->i_size);
ip->i_db[nb] = dbtofsb(fs, bp->b_blkno);
ip->i_flag |= IN_CHANGE | IN_UPDATE;
if ((flags & B_SYNC) || (!alloc_buffer)) {
if (!alloc_buffer)
SET(bp->b_flags, B_INVAL);
bwrite(bp);
} else
bawrite(bp);
}
}
if (lbn < NDADDR) {
nb = ip->i_db[lbn];
if (nb != 0 && ip->i_size >= (lbn + 1) * fs->fs_bsize) {
if (alloc_buffer) {
error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp);
if (error) {
brelse(bp);
return (error);
}
*bpp = bp;
}
return (0);
}
if (nb != 0) {
osize = fragroundup(fs, blkoff(fs, ip->i_size));
nsize = fragroundup(fs, size);
if (nsize <= osize) {
if (alloc_buffer) {
error = bread(vp, lbn, osize, NOCRED, &bp);
if (error) {
brelse(bp);
return (error);
}
ip->i_flag |= IN_CHANGE | IN_UPDATE;
*bpp = bp;
return (0);
}
else {
ip->i_flag |= IN_CHANGE | IN_UPDATE;
return (0);
}
} else {
error = ffs_realloccg(ip, lbn,
ffs_blkpref(ip, lbn, (int)lbn,
&ip->i_db[0]), osize, nsize, cred, &bp);
if (error)
return (error);
ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
ip->i_flag |= IN_CHANGE | IN_UPDATE;
if(!alloc_buffer) {
SET(bp->b_flags, B_INVAL);
bwrite(bp);
} else
*bpp = bp;
return (0);
}
} else {
if (ip->i_size < (lbn + 1) * fs->fs_bsize)
nsize = fragroundup(fs, size);
else
nsize = fs->fs_bsize;
error = ffs_alloc(ip, lbn,
ffs_blkpref(ip, lbn, (int)lbn, &ip->i_db[0]),
nsize, cred, &newb);
if (error)
return (error);
if (alloc_buffer) {
bp = getblk(vp, lbn, nsize, 0, 0, BLK_WRITE);
bp->b_blkno = fsbtodb(fs, newb);
if (flags & B_CLRBUF)
clrbuf(bp);
}
ip->i_db[lbn] = newb;
ip->i_flag |= IN_CHANGE | IN_UPDATE;
if (blk_alloc) {
*blk_alloc = nsize;
}
if (alloc_buffer)
*bpp = bp;
return (0);
}
}
pref = 0;
if (error = ufs_getlbns(vp, lbn, indirs, &num))
return(error);
#if DIAGNOSTIC
if (num < 1)
panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
#endif
--num;
nb = ip->i_ib[indirs[0].in_off];
allocib = NULL;
allocblk = allociblk;
if (nb == 0) {
pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
if (error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
cred, &newb))
return (error);
nb = newb;
*allocblk++ = nb;
bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0, BLK_META);
bp->b_blkno = fsbtodb(fs, nb);
clrbuf(bp);
if (error = bwrite(bp))
goto fail;
allocib = &ip->i_ib[indirs[0].in_off];
*allocib = nb;
ip->i_flag |= IN_CHANGE | IN_UPDATE;
}
for (i = 1;;) {
error = meta_bread(vp,
indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
if (error) {
brelse(bp);
goto fail;
}
bap = (ufs_daddr_t *)bp->b_data;
#if REV_ENDIAN_FS
if (rev_endian)
nb = NXSwapLong(bap[indirs[i].in_off]);
else {
#endif
nb = bap[indirs[i].in_off];
#if REV_ENDIAN_FS
}
#endif
if (i == num)
break;
i += 1;
if (nb != 0) {
brelse(bp);
continue;
}
if (pref == 0)
pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
if (error =
ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred, &newb)) {
brelse(bp);
goto fail;
}
nb = newb;
*allocblk++ = nb;
nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0, BLK_META);
nbp->b_blkno = fsbtodb(fs, nb);
clrbuf(nbp);
if (error = bwrite(nbp)) {
brelse(bp);
goto fail;
}
#if REV_ENDIAN_FS
if (rev_endian)
bap[indirs[i - 1].in_off] = NXSwapLong(nb);
else {
#endif
bap[indirs[i - 1].in_off] = nb;
#if REV_ENDIAN_FS
}
#endif
if (flags & B_SYNC) {
bwrite(bp);
} else {
bdwrite(bp);
}
}
if (nb == 0) {
pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
if (error = ffs_alloc(ip,
lbn, pref, (int)fs->fs_bsize, cred, &newb)) {
brelse(bp);
goto fail;
}
nb = newb;
*allocblk++ = nb;
#if REV_ENDIAN_FS
if (rev_endian)
bap[indirs[i].in_off] = NXSwapLong(nb);
else {
#endif
bap[indirs[i].in_off] = nb;
#if REV_ENDIAN_FS
}
#endif
if ((flags & B_SYNC)) {
bwrite(bp);
} else {
bdwrite(bp);
}
if(alloc_buffer ) {
nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, BLK_WRITE);
nbp->b_blkno = fsbtodb(fs, nb);
if (flags & B_CLRBUF)
clrbuf(nbp);
}
if (blk_alloc) {
*blk_alloc = fs->fs_bsize;
}
if(alloc_buffer)
*bpp = nbp;
return (0);
}
brelse(bp);
if (alloc_buffer) {
if (flags & B_CLRBUF) {
error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
if (error) {
brelse(nbp);
goto fail;
}
} else {
nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, BLK_WRITE);
nbp->b_blkno = fsbtodb(fs, nb);
}
*bpp = nbp;
}
return (0);
fail:
for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
ffs_blkfree(ip, *blkp, fs->fs_bsize);
deallocated += fs->fs_bsize;
}
if (allocib != NULL)
*allocib = 0;
if (deallocated) {
VOP_DEVBLOCKSIZE(ip->i_devvp,&devBlockSize);
#if QUOTA
(void) chkdq(ip, (long)-btodb(deallocated, devBlockSize), cred, FORCE);
#endif
ip->i_blocks -= btodb(deallocated, devBlockSize);
ip->i_flag |= IN_CHANGE | IN_UPDATE;
}
return (error);
}
ffs_blkalloc(ip, lbn, size, cred, flags)
register struct inode *ip;
ufs_daddr_t lbn;
int size;
struct ucred *cred;
int flags;
{
register struct fs *fs;
register ufs_daddr_t nb;
struct buf *bp, *nbp;
struct vnode *vp = ITOV(ip);
struct indir indirs[NIADDR + 2];
ufs_daddr_t newb, *bap, pref;
int deallocated, osize, nsize, num, i, error;
ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
int devBlockSize=0;
#if REV_ENDIAN_FS
struct mount *mp=vp->v_mount;
int rev_endian=(mp->mnt_flag & MNT_REVEND);
#endif
fs = ip->i_fs;
if(size > fs->fs_bsize)
panic("ffs_blkalloc: too large for allocation\n");
nb = lblkno(fs, ip->i_size);
if (nb < NDADDR && nb < lbn) {
panic("ffs_blkalloc():cannot extend file: i_size %d, lbn %d\n", ip->i_size, lbn);
}
if (lbn < NDADDR) {
nb = ip->i_db[lbn];
if (nb != 0 && ip->i_size >= (lbn + 1) * fs->fs_bsize) {
return (0);
}
if (nb != 0) {
osize = fragroundup(fs, blkoff(fs, ip->i_size));
nsize = fragroundup(fs, size);
if (nsize > osize) {
panic("ffs_allocblk: trying to extend
a fragment \n");
}
return(0);
} else {
if (ip->i_size < (lbn + 1) * fs->fs_bsize)
nsize = fragroundup(fs, size);
else
nsize = fs->fs_bsize;
error = ffs_alloc(ip, lbn,
ffs_blkpref(ip, lbn, (int)lbn, &ip->i_db[0]),
nsize, cred, &newb);
if (error)
return (error);
ip->i_db[lbn] = newb;
ip->i_flag |= IN_CHANGE | IN_UPDATE;
return (0);
}
}
pref = 0;
if (error = ufs_getlbns(vp, lbn, indirs, &num))
return(error);
if(num == 0) {
panic("ffs_blkalloc: file with direct blocks only\n");
}
--num;
nb = ip->i_ib[indirs[0].in_off];
allocib = NULL;
allocblk = allociblk;
if (nb == 0) {
pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
if (error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
cred, &newb))
return (error);
nb = newb;
*allocblk++ = nb;
bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0, BLK_META);
bp->b_blkno = fsbtodb(fs, nb);
clrbuf(bp);
if (error = bwrite(bp))
goto fail;
allocib = &ip->i_ib[indirs[0].in_off];
*allocib = nb;
ip->i_flag |= IN_CHANGE | IN_UPDATE;
}
for (i = 1;;) {
error = meta_bread(vp,
indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
if (error) {
brelse(bp);
goto fail;
}
bap = (ufs_daddr_t *)bp->b_data;
#if REV_ENDIAN_FS
if (rev_endian)
nb = NXSwapLong(bap[indirs[i].in_off]);
else {
#endif
nb = bap[indirs[i].in_off];
#if REV_ENDIAN_FS
}
#endif
if (i == num)
break;
i += 1;
if (nb != 0) {
brelse(bp);
continue;
}
if (pref == 0)
pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
if (error =
ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred, &newb)) {
brelse(bp);
goto fail;
}
nb = newb;
*allocblk++ = nb;
nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0, BLK_META);
nbp->b_blkno = fsbtodb(fs, nb);
clrbuf(nbp);
if (error = bwrite(nbp)) {
brelse(bp);
goto fail;
}
#if REV_ENDIAN_FS
if (rev_endian)
bap[indirs[i - 1].in_off] = NXSwapLong(nb);
else {
#endif
bap[indirs[i - 1].in_off] = nb;
#if REV_ENDIAN_FS
}
#endif
if (flags & B_SYNC) {
bwrite(bp);
} else {
bdwrite(bp);
}
}
if (nb == 0) {
pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
if (error = ffs_alloc(ip,
lbn, pref, (int)fs->fs_bsize, cred, &newb)) {
brelse(bp);
goto fail;
}
nb = newb;
*allocblk++ = nb;
#if REV_ENDIAN_FS
if (rev_endian)
bap[indirs[i].in_off] = NXSwapLong(nb);
else {
#endif
bap[indirs[i].in_off] = nb;
#if REV_ENDIAN_FS
}
#endif
if (flags & B_SYNC) {
bwrite(bp);
} else {
bdwrite(bp);
}
return (0);
}
brelse(bp);
return (0);
fail:
for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
ffs_blkfree(ip, *blkp, fs->fs_bsize);
deallocated += fs->fs_bsize;
}
if (allocib != NULL)
*allocib = 0;
if (deallocated) {
VOP_DEVBLOCKSIZE(ip->i_devvp,&devBlockSize);
#if QUOTA
(void) chkdq(ip, (long)-btodb(deallocated, devBlockSize), cred, FORCE);
#endif
ip->i_blocks -= btodb(deallocated, devBlockSize);
ip->i_flag |= IN_CHANGE | IN_UPDATE;
}
return (error);
}