#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bio.h>
#include <sys/buf.h>
#include <sys/lock.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/vnode.h>
#include <sys/vmmeter.h>
#include <ufs/ufs/quota.h>
#include <ufs/ufs/inode.h>
#include <ufs/ufs/ufs_extern.h>
#include <ufs/ufs/extattr.h>
#include <ufs/ufs/ufsmount.h>
#include <ufs/ffs/fs.h>
#include <ufs/ffs/ffs_extern.h>
int
ffs_balloc_ufs1(struct vnode *vp, off_t startoffset, int size,
struct ucred *cred, int flags, struct buf **bpp)
{
struct inode *ip;
struct ufs1_dinode *dp;
ufs_lbn_t lbn, lastlbn;
struct fs *fs;
ufs1_daddr_t nb;
struct buf *bp, *nbp;
struct mount *mp;
struct ufsmount *ump;
struct indir indirs[UFS_NIADDR + 2];
int deallocated, osize, nsize, num, i, error;
ufs2_daddr_t newb;
ufs1_daddr_t *bap, pref;
ufs1_daddr_t *allocib, *blkp, *allocblk, allociblk[UFS_NIADDR + 1];
ufs2_daddr_t *lbns_remfree, lbns[UFS_NIADDR + 1];
int unwindidx = -1;
int saved_inbdflush;
int gbflags, reclaimed;
ip = VTOI(vp);
dp = ip->i_din1;
fs = ITOFS(ip);
mp = ITOVFS(ip);
ump = ITOUMP(ip);
lbn = lblkno(fs, startoffset);
size = blkoff(fs, startoffset) + size;
reclaimed = 0;
if (size > fs->fs_bsize)
panic("ffs_balloc_ufs1: blk too big");
*bpp = NULL;
if (flags & IO_EXT)
return (EOPNOTSUPP);
if (lbn < 0)
return (EFBIG);
gbflags = (flags & BA_UNMAPPED) != 0 ? GB_UNMAPPED : 0;
vn_seqc_write_begin(vp);
lastlbn = lblkno(fs, ip->i_size);
if (lastlbn < UFS_NDADDR && lastlbn < lbn) {
nb = lastlbn;
osize = blksize(fs, ip, nb);
if (osize < fs->fs_bsize && osize > 0) {
UFS_LOCK(ump);
error = ffs_realloccg(ip, nb, dp->di_db[nb],
ffs_blkpref_ufs1(ip, lastlbn, (int)nb,
&dp->di_db[0]), osize, (int)fs->fs_bsize, flags,
cred, &bp);
if (error)
goto done;
if (DOINGSOFTDEP(vp))
softdep_setup_allocdirect(ip, nb,
dbtofsb(fs, bp->b_blkno), dp->di_db[nb],
fs->fs_bsize, osize, bp);
ip->i_size = smalllblktosize(fs, nb + 1);
dp->di_size = ip->i_size;
dp->di_db[nb] = dbtofsb(fs, bp->b_blkno);
UFS_INODE_SET_FLAG(ip,
IN_SIZEMOD | IN_CHANGE | IN_UPDATE | IN_IBLKDATA);
if (flags & IO_SYNC)
bwrite(bp);
else if (DOINGASYNC(vp))
bdwrite(bp);
else
bawrite(bp);
}
}
if (lbn < UFS_NDADDR) {
if (flags & BA_METAONLY)
panic("ffs_balloc_ufs1: BA_METAONLY for direct block");
nb = dp->di_db[lbn];
if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
if ((flags & BA_CLRBUF) != 0) {
error = bread(vp, lbn, fs->fs_bsize, NOCRED,
&bp);
if (error != 0)
goto done;
} else {
bp = getblk(vp, lbn, fs->fs_bsize, 0, 0,
gbflags);
if (bp == NULL) {
error = EIO;
goto done;
}
vfs_bio_clrbuf(bp);
}
bp->b_blkno = fsbtodb(fs, nb);
*bpp = bp;
error = 0;
goto done;
}
if (nb != 0) {
osize = fragroundup(fs, blkoff(fs, ip->i_size));
nsize = fragroundup(fs, size);
if (nsize <= osize) {
error = bread(vp, lbn, osize, NOCRED, &bp);
if (error)
goto done;
bp->b_blkno = fsbtodb(fs, nb);
} else {
UFS_LOCK(ump);
error = ffs_realloccg(ip, lbn, dp->di_db[lbn],
ffs_blkpref_ufs1(ip, lbn, (int)lbn,
&dp->di_db[0]), osize, nsize, flags,
cred, &bp);
if (error)
goto done;
if (DOINGSOFTDEP(vp))
softdep_setup_allocdirect(ip, lbn,
dbtofsb(fs, bp->b_blkno), nb,
nsize, osize, bp);
}
} else {
if (ip->i_size < smalllblktosize(fs, lbn + 1))
nsize = fragroundup(fs, size);
else
nsize = fs->fs_bsize;
UFS_LOCK(ump);
error = ffs_alloc(ip, lbn,
ffs_blkpref_ufs1(ip, lbn, (int)lbn, &dp->di_db[0]),
nsize, flags, cred, &newb);
if (error)
goto done;
bp = getblk(vp, lbn, nsize, 0, 0, gbflags);
bp->b_blkno = fsbtodb(fs, newb);
if (flags & BA_CLRBUF)
vfs_bio_clrbuf(bp);
if (DOINGSOFTDEP(vp))
softdep_setup_allocdirect(ip, lbn, newb, 0,
nsize, 0, bp);
}
dp->di_db[lbn] = dbtofsb(fs, bp->b_blkno);
UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE | IN_IBLKDATA);
*bpp = bp;
error = 0;
goto done;
}
pref = 0;
if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
goto done;
#ifdef INVARIANTS
if (num < 1)
panic ("ffs_balloc_ufs1: ufs_getlbns returned indirect block");
#endif
saved_inbdflush = curthread_pflags_set(TDP_INBDFLUSH);
--num;
nb = dp->di_ib[indirs[0].in_off];
allocib = NULL;
allocblk = allociblk;
lbns_remfree = lbns;
if (nb == 0) {
UFS_LOCK(ump);
pref = ffs_blkpref_ufs1(ip, lbn, -indirs[0].in_off - 1,
(ufs1_daddr_t *)0);
if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
flags, cred, &newb)) != 0) {
curthread_pflags_restore(saved_inbdflush);
goto done;
}
pref = newb + fs->fs_frag;
nb = newb;
MPASS(allocblk < allociblk + nitems(allociblk));
MPASS(lbns_remfree < lbns + nitems(lbns));
*allocblk++ = nb;
*lbns_remfree++ = indirs[1].in_lbn;
bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0, gbflags);
bp->b_blkno = fsbtodb(fs, nb);
vfs_bio_clrbuf(bp);
if (DOINGSOFTDEP(vp)) {
softdep_setup_allocdirect(ip,
UFS_NDADDR + indirs[0].in_off, newb, 0,
fs->fs_bsize, 0, bp);
bdwrite(bp);
} else if ((flags & IO_SYNC) == 0 && DOINGASYNC(vp)) {
if (bp->b_bufsize == fs->fs_bsize)
bp->b_flags |= B_CLUSTEROK;
bdwrite(bp);
} else {
if ((error = bwrite(bp)) != 0)
goto fail;
}
allocib = &dp->di_ib[indirs[0].in_off];
*allocib = nb;
UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE | IN_IBLKDATA);
}
retry:
for (i = 1;;) {
error = bread(vp,
indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
if (error) {
goto fail;
}
bap = (ufs1_daddr_t *)bp->b_data;
nb = bap[indirs[i].in_off];
if ((error = UFS_CHECK_BLKNO(mp, ip->i_number, nb,
fs->fs_bsize)) != 0) {
brelse(bp);
goto fail;
}
if (i == num)
break;
i += 1;
if (nb != 0) {
bqrelse(bp);
continue;
}
UFS_LOCK(ump);
if (pref == 0)
pref = ffs_blkpref_ufs1(ip, lbn, i - num - 1,
(ufs1_daddr_t *)0);
if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
flags | IO_BUFLOCKED, cred, &newb)) != 0) {
brelse(bp);
UFS_LOCK(ump);
if (DOINGSOFTDEP(vp) && ++reclaimed == 1) {
softdep_request_cleanup(fs, vp, cred,
FLUSH_BLOCKS_WAIT);
UFS_UNLOCK(ump);
goto retry;
}
if (!ffs_fsfail_cleanup_locked(ump, error) &&
ppsratecheck(&ump->um_last_fullmsg,
&ump->um_secs_fullmsg, 1)) {
UFS_UNLOCK(ump);
ffs_fserr(fs, ip->i_number, "filesystem full");
uprintf("\n%s: write failed, filesystem "
"is full\n", fs->fs_fsmnt);
} else {
UFS_UNLOCK(ump);
}
goto fail;
}
pref = newb + fs->fs_frag;
nb = newb;
MPASS(allocblk < allociblk + nitems(allociblk));
MPASS(lbns_remfree < lbns + nitems(lbns));
*allocblk++ = nb;
*lbns_remfree++ = indirs[i].in_lbn;
nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0, 0);
nbp->b_blkno = fsbtodb(fs, nb);
vfs_bio_clrbuf(nbp);
if (DOINGSOFTDEP(vp)) {
softdep_setup_allocindir_meta(nbp, ip, bp,
indirs[i - 1].in_off, nb);
bdwrite(nbp);
} else if ((flags & IO_SYNC) == 0 && DOINGASYNC(vp)) {
if (nbp->b_bufsize == fs->fs_bsize)
nbp->b_flags |= B_CLUSTEROK;
bdwrite(nbp);
} else {
if ((error = bwrite(nbp)) != 0) {
brelse(bp);
goto fail;
}
}
bap[indirs[i - 1].in_off] = nb;
if (allocib == NULL && unwindidx < 0)
unwindidx = i - 1;
if (flags & IO_SYNC) {
bwrite(bp);
} else {
if (bp->b_bufsize == fs->fs_bsize)
bp->b_flags |= B_CLUSTEROK;
bdwrite(bp);
}
}
if (flags & BA_METAONLY) {
curthread_pflags_restore(saved_inbdflush);
*bpp = bp;
error = 0;
goto done;
}
if (nb == 0) {
UFS_LOCK(ump);
if (pref == 0 || (lbn > UFS_NDADDR && fs->fs_metaspace != 0))
pref = ffs_blkpref_ufs1(ip, lbn, indirs[i].in_off,
&bap[0]);
error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
flags | IO_BUFLOCKED, cred, &newb);
if (error) {
brelse(bp);
UFS_LOCK(ump);
if (DOINGSOFTDEP(vp) && ++reclaimed == 1) {
softdep_request_cleanup(fs, vp, cred,
FLUSH_BLOCKS_WAIT);
UFS_UNLOCK(ump);
goto retry;
}
if (!ffs_fsfail_cleanup_locked(ump, error) &&
ppsratecheck(&ump->um_last_fullmsg,
&ump->um_secs_fullmsg, 1)) {
UFS_UNLOCK(ump);
ffs_fserr(fs, ip->i_number, "filesystem full");
uprintf("\n%s: write failed, filesystem "
"is full\n", fs->fs_fsmnt);
} else {
UFS_UNLOCK(ump);
}
goto fail;
}
nb = newb;
MPASS(allocblk < allociblk + nitems(allociblk));
MPASS(lbns_remfree < lbns + nitems(lbns));
*allocblk++ = nb;
*lbns_remfree++ = lbn;
nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, gbflags);
nbp->b_blkno = fsbtodb(fs, nb);
if (flags & BA_CLRBUF)
vfs_bio_clrbuf(nbp);
if (DOINGSOFTDEP(vp))
softdep_setup_allocindir_page(ip, lbn, bp,
indirs[i].in_off, nb, 0, nbp);
bap[indirs[i].in_off] = nb;
if (flags & IO_SYNC) {
bwrite(bp);
} else {
if (bp->b_bufsize == fs->fs_bsize)
bp->b_flags |= B_CLUSTEROK;
bdwrite(bp);
}
curthread_pflags_restore(saved_inbdflush);
*bpp = nbp;
error = 0;
goto done;
}
brelse(bp);
if (flags & BA_CLRBUF) {
int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
if (seqcount != 0 &&
(vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0 &&
!(vm_page_count_severe() || buf_dirty_count_severe())) {
error = cluster_read(vp, ip->i_size, lbn,
(int)fs->fs_bsize, NOCRED,
MAXBSIZE, seqcount, gbflags, &nbp);
} else {
error = bread_gb(vp, lbn, (int)fs->fs_bsize, NOCRED,
gbflags, &nbp);
}
if (error) {
brelse(nbp);
goto fail;
}
} else {
nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, gbflags);
nbp->b_blkno = fsbtodb(fs, nb);
}
curthread_pflags_restore(saved_inbdflush);
*bpp = nbp;
error = 0;
goto done;
fail:
curthread_pflags_restore(saved_inbdflush);
if (allocblk == allociblk && allocib == NULL && unwindidx == -1)
goto done;
(void) ffs_syncvnode(vp, MNT_WAIT, 0);
for (deallocated = 0, blkp = allociblk, lbns_remfree = lbns;
blkp < allocblk; blkp++, lbns_remfree++) {
bp = getblk(vp, *lbns_remfree, fs->fs_bsize, 0, 0,
GB_NOCREAT | GB_UNMAPPED);
if (bp != NULL) {
KASSERT(bp->b_blkno == fsbtodb(fs, *blkp),
("mismatch1 l %jd %jd b %ju %ju",
(intmax_t)bp->b_lblkno, (uintmax_t)*lbns_remfree,
(uintmax_t)bp->b_blkno,
(uintmax_t)fsbtodb(fs, *blkp)));
bp->b_flags |= B_INVAL | B_RELBUF | B_NOCACHE;
bp->b_flags &= ~(B_ASYNC | B_CACHE);
brelse(bp);
}
deallocated += fs->fs_bsize;
}
if (allocib != NULL) {
*allocib = 0;
} else if (unwindidx >= 0) {
int r;
r = bread(vp, indirs[unwindidx].in_lbn,
(int)fs->fs_bsize, NOCRED, &bp);
if (r) {
panic("Could not unwind indirect block, error %d", r);
brelse(bp);
} else {
bap = (ufs1_daddr_t *)bp->b_data;
bap[indirs[unwindidx].in_off] = 0;
if (flags & IO_SYNC) {
bwrite(bp);
} else {
if (bp->b_bufsize == fs->fs_bsize)
bp->b_flags |= B_CLUSTEROK;
bdwrite(bp);
}
}
}
if (deallocated) {
#ifdef QUOTA
(void) chkdq(ip, -btodb(deallocated), cred, FORCE);
#endif
dp->di_blocks -= btodb(deallocated);
UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
}
(void) ffs_syncvnode(vp, MNT_WAIT, 0);
for (blkp = allociblk; blkp < allocblk; blkp++) {
#ifdef INVARIANTS
if (blkp == allociblk)
lbns_remfree = lbns;
bp = getblk(vp, *lbns_remfree, fs->fs_bsize, 0, 0,
GB_NOCREAT | GB_UNMAPPED);
if (bp != NULL) {
panic("zombie1 %jd %ju %ju",
(intmax_t)bp->b_lblkno, (uintmax_t)bp->b_blkno,
(uintmax_t)fsbtodb(fs, *blkp));
}
lbns_remfree++;
#endif
ffs_blkfree(ump, fs, ump->um_devvp, *blkp, fs->fs_bsize,
ip->i_number, vp->v_type, NULL, SINGLETON_KEY);
}
done:
vn_seqc_write_end(vp);
return (error);
}
int
ffs_balloc_ufs2(struct vnode *vp, off_t startoffset, int size,
struct ucred *cred, int flags, struct buf **bpp)
{
struct inode *ip;
struct ufs2_dinode *dp;
ufs_lbn_t lbn, lastlbn;
struct fs *fs;
struct buf *bp, *nbp;
struct mount *mp;
struct ufsmount *ump;
struct indir indirs[UFS_NIADDR + 2];
ufs2_daddr_t nb, newb, *bap, pref;
ufs2_daddr_t *allocib, *blkp, *allocblk, allociblk[UFS_NIADDR + 1];
ufs2_daddr_t *lbns_remfree, lbns[UFS_NIADDR + 1];
int deallocated, osize, nsize, num, i, error;
int unwindidx = -1;
int saved_inbdflush;
int gbflags, gbwflag, reclaimed;
ip = VTOI(vp);
dp = ip->i_din2;
fs = ITOFS(ip);
mp = ITOVFS(ip);
ump = ITOUMP(ip);
lbn = lblkno(fs, startoffset);
size = blkoff(fs, startoffset) + size;
reclaimed = 0;
if (size > fs->fs_bsize)
panic("ffs_balloc_ufs2: blk too big");
*bpp = NULL;
if (lbn < 0)
return (EFBIG);
gbflags = (flags & BA_UNMAPPED) != 0 ? GB_UNMAPPED : 0;
#ifdef WITNESS
gbwflag = IS_SNAPSHOT(ip) ? GB_NOWITNESS : 0;
gbflags |= gbwflag;
#else
gbwflag = 0;
#endif
vn_seqc_write_begin(vp);
if (flags & IO_EXT) {
if (lbn >= UFS_NXADDR) {
error = EFBIG;
goto done;
}
lastlbn = lblkno(fs, dp->di_extsize);
if (lastlbn < lbn) {
nb = lastlbn;
osize = sblksize(fs, dp->di_extsize, nb);
if (osize < fs->fs_bsize && osize > 0) {
UFS_LOCK(ump);
error = ffs_realloccg(ip, -1 - nb,
dp->di_extb[nb],
ffs_blkpref_ufs2(ip, lastlbn, (int)nb,
&dp->di_extb[0]), osize,
(int)fs->fs_bsize, flags, cred, &bp);
if (error)
goto done;
if (DOINGSOFTDEP(vp))
softdep_setup_allocext(ip, nb,
dbtofsb(fs, bp->b_blkno),
dp->di_extb[nb],
fs->fs_bsize, osize, bp);
dp->di_extsize = smalllblktosize(fs, nb + 1);
dp->di_extb[nb] = dbtofsb(fs, bp->b_blkno);
bp->b_xflags |= BX_ALTDATA;
UFS_INODE_SET_FLAG(ip,
IN_SIZEMOD | IN_CHANGE | IN_IBLKDATA);
if (flags & IO_SYNC)
bwrite(bp);
else
bawrite(bp);
}
}
if (flags & BA_METAONLY)
panic("ffs_balloc_ufs2: BA_METAONLY for ext block");
nb = dp->di_extb[lbn];
if (nb != 0 && dp->di_extsize >= smalllblktosize(fs, lbn + 1)) {
error = bread_gb(vp, -1 - lbn, fs->fs_bsize, NOCRED,
gbflags, &bp);
if (error)
goto done;
bp->b_blkno = fsbtodb(fs, nb);
bp->b_xflags |= BX_ALTDATA;
*bpp = bp;
goto done;
}
if (nb != 0) {
osize = fragroundup(fs, blkoff(fs, dp->di_extsize));
nsize = fragroundup(fs, size);
if (nsize <= osize) {
error = bread_gb(vp, -1 - lbn, osize, NOCRED,
gbflags, &bp);
if (error)
goto done;
bp->b_blkno = fsbtodb(fs, nb);
bp->b_xflags |= BX_ALTDATA;
} else {
UFS_LOCK(ump);
error = ffs_realloccg(ip, -1 - lbn,
dp->di_extb[lbn],
ffs_blkpref_ufs2(ip, lbn, (int)lbn,
&dp->di_extb[0]), osize, nsize, flags,
cred, &bp);
if (error)
goto done;
bp->b_xflags |= BX_ALTDATA;
if (DOINGSOFTDEP(vp))
softdep_setup_allocext(ip, lbn,
dbtofsb(fs, bp->b_blkno), nb,
nsize, osize, bp);
}
} else {
if (dp->di_extsize < smalllblktosize(fs, lbn + 1))
nsize = fragroundup(fs, size);
else
nsize = fs->fs_bsize;
UFS_LOCK(ump);
error = ffs_alloc(ip, lbn,
ffs_blkpref_ufs2(ip, lbn, (int)lbn, &dp->di_extb[0]),
nsize, flags, cred, &newb);
if (error)
goto done;
bp = getblk(vp, -1 - lbn, nsize, 0, 0, gbflags);
bp->b_blkno = fsbtodb(fs, newb);
bp->b_xflags |= BX_ALTDATA;
if (flags & BA_CLRBUF)
vfs_bio_clrbuf(bp);
if (DOINGSOFTDEP(vp))
softdep_setup_allocext(ip, lbn, newb, 0,
nsize, 0, bp);
}
dp->di_extb[lbn] = dbtofsb(fs, bp->b_blkno);
UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_IBLKDATA);
*bpp = bp;
error = 0;
goto done;
}
lastlbn = lblkno(fs, ip->i_size);
if (lastlbn < UFS_NDADDR && lastlbn < lbn) {
nb = lastlbn;
osize = blksize(fs, ip, nb);
if (osize < fs->fs_bsize && osize > 0) {
UFS_LOCK(ump);
error = ffs_realloccg(ip, nb, dp->di_db[nb],
ffs_blkpref_ufs2(ip, lastlbn, (int)nb,
&dp->di_db[0]), osize, (int)fs->fs_bsize,
flags, cred, &bp);
if (error)
goto done;
if (DOINGSOFTDEP(vp))
softdep_setup_allocdirect(ip, nb,
dbtofsb(fs, bp->b_blkno),
dp->di_db[nb],
fs->fs_bsize, osize, bp);
ip->i_size = smalllblktosize(fs, nb + 1);
dp->di_size = ip->i_size;
dp->di_db[nb] = dbtofsb(fs, bp->b_blkno);
UFS_INODE_SET_FLAG(ip,
IN_SIZEMOD |IN_CHANGE | IN_UPDATE | IN_IBLKDATA);
if (flags & IO_SYNC)
bwrite(bp);
else
bawrite(bp);
}
}
if (lbn < UFS_NDADDR) {
if (flags & BA_METAONLY)
panic("ffs_balloc_ufs2: BA_METAONLY for direct block");
nb = dp->di_db[lbn];
if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
if ((flags & BA_CLRBUF) != 0) {
error = bread_gb(vp, lbn, fs->fs_bsize, NOCRED,
gbflags, &bp);
if (error != 0)
goto done;
} else {
bp = getblk(vp, lbn, fs->fs_bsize, 0, 0,
gbflags);
if (bp == NULL) {
error = EIO;
goto done;
}
vfs_bio_clrbuf(bp);
}
bp->b_blkno = fsbtodb(fs, nb);
*bpp = bp;
error = 0;
goto done;
}
if (nb != 0) {
osize = fragroundup(fs, blkoff(fs, ip->i_size));
nsize = fragroundup(fs, size);
if (nsize <= osize) {
error = bread_gb(vp, lbn, osize, NOCRED,
gbflags, &bp);
if (error)
goto done;
bp->b_blkno = fsbtodb(fs, nb);
} else {
UFS_LOCK(ump);
error = ffs_realloccg(ip, lbn, dp->di_db[lbn],
ffs_blkpref_ufs2(ip, lbn, (int)lbn,
&dp->di_db[0]), osize, nsize, flags,
cred, &bp);
if (error)
goto done;
if (DOINGSOFTDEP(vp))
softdep_setup_allocdirect(ip, lbn,
dbtofsb(fs, bp->b_blkno), nb,
nsize, osize, bp);
}
} else {
if (ip->i_size < smalllblktosize(fs, lbn + 1))
nsize = fragroundup(fs, size);
else
nsize = fs->fs_bsize;
UFS_LOCK(ump);
error = ffs_alloc(ip, lbn,
ffs_blkpref_ufs2(ip, lbn, (int)lbn,
&dp->di_db[0]), nsize, flags, cred, &newb);
if (error)
goto done;
bp = getblk(vp, lbn, nsize, 0, 0, gbflags);
bp->b_blkno = fsbtodb(fs, newb);
if (flags & BA_CLRBUF)
vfs_bio_clrbuf(bp);
if (DOINGSOFTDEP(vp))
softdep_setup_allocdirect(ip, lbn, newb, 0,
nsize, 0, bp);
}
dp->di_db[lbn] = dbtofsb(fs, bp->b_blkno);
UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE | IN_IBLKDATA);
*bpp = bp;
error = 0;
goto done;
}
pref = 0;
if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
goto done;
#ifdef INVARIANTS
if (num < 1)
panic ("ffs_balloc_ufs2: ufs_getlbns returned indirect block");
#endif
saved_inbdflush = curthread_pflags_set(TDP_INBDFLUSH);
--num;
nb = dp->di_ib[indirs[0].in_off];
allocib = NULL;
allocblk = allociblk;
lbns_remfree = lbns;
if (nb == 0) {
UFS_LOCK(ump);
pref = ffs_blkpref_ufs2(ip, lbn, -indirs[0].in_off - 1,
(ufs2_daddr_t *)0);
if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
flags, cred, &newb)) != 0) {
curthread_pflags_restore(saved_inbdflush);
goto done;
}
pref = newb + fs->fs_frag;
nb = newb;
MPASS(allocblk < allociblk + nitems(allociblk));
MPASS(lbns_remfree < lbns + nitems(lbns));
*allocblk++ = nb;
*lbns_remfree++ = indirs[1].in_lbn;
bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0,
GB_UNMAPPED | gbwflag);
bp->b_blkno = fsbtodb(fs, nb);
vfs_bio_clrbuf(bp);
if (DOINGSOFTDEP(vp)) {
softdep_setup_allocdirect(ip,
UFS_NDADDR + indirs[0].in_off, newb, 0,
fs->fs_bsize, 0, bp);
bdwrite(bp);
} else if ((flags & IO_SYNC) == 0 && DOINGASYNC(vp)) {
if (bp->b_bufsize == fs->fs_bsize)
bp->b_flags |= B_CLUSTEROK;
bdwrite(bp);
} else {
if ((error = bwrite(bp)) != 0)
goto fail;
}
allocib = &dp->di_ib[indirs[0].in_off];
*allocib = nb;
UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE | IN_IBLKDATA);
}
retry:
for (i = 1;;) {
error = bread_gb(vp, indirs[i].in_lbn, (int)fs->fs_bsize,
NOCRED, gbwflag, &bp);
if (error) {
goto fail;
}
bap = (ufs2_daddr_t *)bp->b_data;
nb = bap[indirs[i].in_off];
if ((error = UFS_CHECK_BLKNO(mp, ip->i_number, nb,
fs->fs_bsize)) != 0) {
brelse(bp);
goto fail;
}
if (i == num)
break;
i += 1;
if (nb != 0) {
bqrelse(bp);
continue;
}
UFS_LOCK(ump);
if (pref == 0)
pref = ffs_blkpref_ufs2(ip, lbn, i - num - 1,
(ufs2_daddr_t *)0);
if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
flags | IO_BUFLOCKED, cred, &newb)) != 0) {
brelse(bp);
UFS_LOCK(ump);
if (DOINGSOFTDEP(vp) && ++reclaimed == 1) {
softdep_request_cleanup(fs, vp, cred,
FLUSH_BLOCKS_WAIT);
UFS_UNLOCK(ump);
goto retry;
}
if (!ffs_fsfail_cleanup_locked(ump, error) &&
ppsratecheck(&ump->um_last_fullmsg,
&ump->um_secs_fullmsg, 1)) {
UFS_UNLOCK(ump);
ffs_fserr(fs, ip->i_number, "filesystem full");
uprintf("\n%s: write failed, filesystem "
"is full\n", fs->fs_fsmnt);
} else {
UFS_UNLOCK(ump);
}
goto fail;
}
pref = newb + fs->fs_frag;
nb = newb;
MPASS(allocblk < allociblk + nitems(allociblk));
MPASS(lbns_remfree < lbns + nitems(lbns));
*allocblk++ = nb;
*lbns_remfree++ = indirs[i].in_lbn;
nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0,
GB_UNMAPPED);
nbp->b_blkno = fsbtodb(fs, nb);
vfs_bio_clrbuf(nbp);
if (DOINGSOFTDEP(vp)) {
softdep_setup_allocindir_meta(nbp, ip, bp,
indirs[i - 1].in_off, nb);
bdwrite(nbp);
} else if ((flags & IO_SYNC) == 0 && DOINGASYNC(vp)) {
if (nbp->b_bufsize == fs->fs_bsize)
nbp->b_flags |= B_CLUSTEROK;
bdwrite(nbp);
} else {
if ((error = bwrite(nbp)) != 0) {
brelse(bp);
goto fail;
}
}
bap[indirs[i - 1].in_off] = nb;
if (allocib == NULL && unwindidx < 0)
unwindidx = i - 1;
if (flags & IO_SYNC) {
bwrite(bp);
} else {
if (bp->b_bufsize == fs->fs_bsize)
bp->b_flags |= B_CLUSTEROK;
bdwrite(bp);
}
}
if (flags & BA_METAONLY) {
curthread_pflags_restore(saved_inbdflush);
*bpp = bp;
error = 0;
goto done;
}
if (nb == 0) {
UFS_LOCK(ump);
if (pref == 0 || (lbn > UFS_NDADDR && fs->fs_metaspace != 0))
pref = ffs_blkpref_ufs2(ip, lbn, indirs[i].in_off,
&bap[0]);
error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
flags | IO_BUFLOCKED, cred, &newb);
if (error) {
brelse(bp);
UFS_LOCK(ump);
if (DOINGSOFTDEP(vp) && ++reclaimed == 1) {
softdep_request_cleanup(fs, vp, cred,
FLUSH_BLOCKS_WAIT);
UFS_UNLOCK(ump);
goto retry;
}
if (!ffs_fsfail_cleanup_locked(ump, error) &&
ppsratecheck(&ump->um_last_fullmsg,
&ump->um_secs_fullmsg, 1)) {
UFS_UNLOCK(ump);
ffs_fserr(fs, ip->i_number, "filesystem full");
uprintf("\n%s: write failed, filesystem "
"is full\n", fs->fs_fsmnt);
} else {
UFS_UNLOCK(ump);
}
goto fail;
}
nb = newb;
MPASS(allocblk < allociblk + nitems(allociblk));
MPASS(lbns_remfree < lbns + nitems(lbns));
*allocblk++ = nb;
*lbns_remfree++ = lbn;
nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, gbflags);
nbp->b_blkno = fsbtodb(fs, nb);
if (flags & BA_CLRBUF)
vfs_bio_clrbuf(nbp);
if (DOINGSOFTDEP(vp))
softdep_setup_allocindir_page(ip, lbn, bp,
indirs[i].in_off, nb, 0, nbp);
bap[indirs[i].in_off] = nb;
if (flags & IO_SYNC) {
bwrite(bp);
} else {
if (bp->b_bufsize == fs->fs_bsize)
bp->b_flags |= B_CLUSTEROK;
bdwrite(bp);
}
curthread_pflags_restore(saved_inbdflush);
*bpp = nbp;
error = 0;
goto done;
}
brelse(bp);
if (flags & BA_CLRBUF) {
int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
if (seqcount != 0 &&
(vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0 &&
!(vm_page_count_severe() || buf_dirty_count_severe())) {
error = cluster_read(vp, ip->i_size, lbn,
(int)fs->fs_bsize, NOCRED,
MAXBSIZE, seqcount, gbflags, &nbp);
} else {
error = bread_gb(vp, lbn, (int)fs->fs_bsize,
NOCRED, gbflags, &nbp);
}
if (error) {
brelse(nbp);
goto fail;
}
} else {
nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, gbflags);
nbp->b_blkno = fsbtodb(fs, nb);
}
curthread_pflags_restore(saved_inbdflush);
*bpp = nbp;
error = 0;
goto done;
fail:
curthread_pflags_restore(saved_inbdflush);
if (allocblk == allociblk && allocib == NULL && unwindidx == -1)
goto done;
(void) ffs_syncvnode(vp, MNT_WAIT, 0);
for (deallocated = 0, blkp = allociblk, lbns_remfree = lbns;
blkp < allocblk; blkp++, lbns_remfree++) {
bp = getblk(vp, *lbns_remfree, fs->fs_bsize, 0, 0,
GB_NOCREAT | GB_UNMAPPED | gbwflag);
if (bp != NULL) {
KASSERT(bp->b_blkno == fsbtodb(fs, *blkp),
("mismatch2 l %jd %jd b %ju %ju",
(intmax_t)bp->b_lblkno, (uintmax_t)*lbns_remfree,
(uintmax_t)bp->b_blkno,
(uintmax_t)fsbtodb(fs, *blkp)));
bp->b_flags |= B_INVAL | B_RELBUF | B_NOCACHE;
bp->b_flags &= ~(B_ASYNC | B_CACHE);
brelse(bp);
}
deallocated += fs->fs_bsize;
}
if (allocib != NULL) {
*allocib = 0;
} else if (unwindidx >= 0) {
int r;
r = bread_gb(vp, indirs[unwindidx].in_lbn,
(int)fs->fs_bsize, NOCRED, gbwflag, &bp);
if (r) {
panic("Could not unwind indirect block, error %d", r);
brelse(bp);
} else {
bap = (ufs2_daddr_t *)bp->b_data;
bap[indirs[unwindidx].in_off] = 0;
if (flags & IO_SYNC) {
bwrite(bp);
} else {
if (bp->b_bufsize == fs->fs_bsize)
bp->b_flags |= B_CLUSTEROK;
bdwrite(bp);
}
}
}
if (deallocated) {
#ifdef QUOTA
(void) chkdq(ip, -btodb(deallocated), cred, FORCE);
#endif
dp->di_blocks -= btodb(deallocated);
UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
}
(void) ffs_syncvnode(vp, MNT_WAIT, 0);
for (blkp = allociblk; blkp < allocblk; blkp++) {
#ifdef INVARIANTS
if (blkp == allociblk)
lbns_remfree = lbns;
bp = getblk(vp, *lbns_remfree, fs->fs_bsize, 0, 0,
GB_NOCREAT | GB_UNMAPPED | gbwflag);
if (bp != NULL) {
panic("zombie2 %jd %ju %ju",
(intmax_t)bp->b_lblkno, (uintmax_t)bp->b_blkno,
(uintmax_t)fsbtodb(fs, *blkp));
}
lbns_remfree++;
#endif
ffs_blkfree(ump, fs, ump->um_devvp, *blkp, fs->fs_bsize,
ip->i_number, vp->v_type, NULL, SINGLETON_KEY);
}
done:
vn_seqc_write_end(vp);
return (error);
}