/*
* Copyright (c) 2002 Networks Associates Technology, Inc.
* All rights reserved.
*
* This software was developed for the FreeBSD Project by Marshall
* Kirk McKusick and Network Associates Laboratories, the Security
* Research Division of Network Associates, Inc. under DARPA/SPAWAR
* contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
* research program
*
* Copyright (c) 1982, 1986, 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)ffs_balloc.c 8.8 (Berkeley) 6/16/95
*/
/*
* Balloc defines the structure of file system storage
* by allocating the physical blocks on a device given
* the inode and the logical block number in a file.
*/
int
ffs_balloc(struct vnode *vp, off_t off, int size, kauth_cred_t cred, int flags,
struct buf **bpp)
{
int error;
/*
* If the next write will extend the file into a new block,
* and the file is currently composed of a fragment
* this fragment has to be extended to be a full block.
*/
/*
* The first UFS_NDADDR blocks are direct blocks
*/
if (lbn < UFS_NDADDR) {
nb = ufs_rw32(ip->i_ffs1_db[lbn], needswap);
if (nb != 0 && ip->i_size >= ffs_lblktosize(fs, lbn + 1)) {
/*
* The block is an already-allocated direct block
* and the file already extends past this block,
* thus this must be a whole block.
* Just read the block (if requested).
*/
if (bpp != NULL) {
error = bread(vp, lbn, fs->fs_bsize,
B_MODIFY, bpp);
if (error) {
return (error);
}
}
return (0);
}
if (nb != 0) {
fail:
/*
* If we have failed part way through block allocation, we
* have to deallocate any indirect blocks that we have allocated.
*/
if (unwindidx >= 0) {
/*
* First write out any buffers we've created to resolve their
* softdeps. This must be done in reverse order of creation
* so that we resolve the dependencies in one pass.
* Write the cylinder group buffers for these buffers too.
*/
for (i = num; i >= unwindidx; i--) {
if (i == 0) {
break;
}
if (ffs_getblk(vp, indirs[i].in_lbn, FFS_NOBLK,
fs->fs_bsize, false, &bp) != 0)
continue;
if (bp->b_oflags & BO_DELWRI) {
nb = FFS_FSBTODB(fs, cgtod(fs, dtog(fs,
FFS_DBTOFSB(fs, bp->b_blkno))));
bwrite(bp);
if (ffs_getblk(ip->i_devvp, nb, FFS_NOBLK,
fs->fs_cgsize, false, &bp) != 0)
continue;
if (bp->b_oflags & BO_DELWRI) {
bwrite(bp);
} else {
brelse(bp, BC_INVAL);
}
} else {
brelse(bp, BC_INVAL);
}
}
/*
* Undo the partial allocation.
*/
if (unwindidx == 0) {
*allocib = 0;
ip->i_flag |= IN_CHANGE | IN_UPDATE;
} else {
int r;
/*
* Check for allocating external data.
*/
if (flags & IO_EXT) {
struct ufs2_dinode *dp = ip->i_din.ffs2_din;
if (lbn >= UFS_NXADDR)
return (EFBIG);
/*
* If the next write will extend the data into a new block,
* and the data is currently composed of a fragment
* this fragment has to be extended to be a full block.
*/
lastlbn = ffs_lblkno(fs, dp->di_extsize);
if (lastlbn < lbn) {
nb = lastlbn;
osize = ffs_sblksize(fs, dp->di_extsize, nb);
if (osize < fs->fs_bsize && osize > 0) {
mutex_enter(&ump->um_lock);
error = ffs_realloccg(ip, -1 - nb,
ffs_extb(fs, dp, nb),
ffs_blkpref_ufs2(ip, lastlbn, (int)nb,
flags, &dp->di_extb[0]),
osize, (int)fs->fs_bsize, flags, cred,
&bp, &newb);
if (error)
return (error);
dp->di_extsize = ffs_lblktosize(fs, nb + 1);
dp->di_extb[nb] = FFS_DBTOFSB(fs, bp->b_blkno);
ip->i_flag |= IN_CHANGE | IN_UPDATE;
if (flags & IO_SYNC)
bwrite(bp);
else
bawrite(bp);
}
}
/*
* All blocks are direct blocks
*/
nb = dp->di_extb[lbn];
if (nb != 0 && dp->di_extsize >= ffs_lblktosize(fs, lbn + 1)) {
error = bread(vp, -1 - lbn, fs->fs_bsize,
0, &bp);
if (error) {
return (error);
}
mutex_enter(bp->b_objlock);
bp->b_blkno = FFS_FSBTODB(fs, nb);
mutex_exit(bp->b_objlock);
*bpp = bp;
return (0);
}
if (nb != 0) {
/*
* Consider need to reallocate a fragment.
*/
osize = ffs_fragroundup(fs, ffs_blkoff(fs, dp->di_extsize));
nsize = ffs_fragroundup(fs, size);
if (nsize <= osize) {
error = bread(vp, -1 - lbn, osize,
0, &bp);
if (error) {
return (error);
}
mutex_enter(bp->b_objlock);
bp->b_blkno = FFS_FSBTODB(fs, nb);
mutex_exit(bp->b_objlock);
} else {
mutex_enter(&ump->um_lock);
error = ffs_realloccg(ip, -1 - lbn,
ffs_extb(fs, dp, lbn),
ffs_blkpref_ufs2(ip, lbn, (int)lbn, flags,
&dp->di_extb[0]),
osize, nsize, flags, cred, &bp, &newb);
if (error)
return (error);
}
} else {
if (dp->di_extsize < ffs_lblktosize(fs, lbn + 1))
nsize = ffs_fragroundup(fs, size);
else
nsize = fs->fs_bsize;
mutex_enter(&ump->um_lock);
error = ffs_alloc(ip, lbn,
ffs_blkpref_ufs2(ip, lbn, (int)lbn, flags,
&dp->di_extb[0]),
nsize, flags, cred, &newb);
if (error)
return (error);
error = ffs_getblk(vp, -1 - lbn, FFS_FSBTODB(fs, newb),
nsize, (flags & B_CLRBUF) != 0, &bp);
if (error)
return error;
}
dp->di_extb[lbn] = FFS_DBTOFSB(fs, bp->b_blkno);
ip->i_flag |= IN_CHANGE | IN_UPDATE;
*bpp = bp;
return (0);
}
/*
* If the next write will extend the file into a new block,
* and the file is currently composed of a fragment
* this fragment has to be extended to be a full block.
*/
/*
* The first UFS_NDADDR blocks are direct blocks
*/
if (lbn < UFS_NDADDR) {
nb = ufs_rw64(ip->i_ffs2_db[lbn], needswap);
if (nb != 0 && ip->i_size >= ffs_lblktosize(fs, lbn + 1)) {
/*
* The block is an already-allocated direct block
* and the file already extends past this block,
* thus this must be a whole block.
* Just read the block (if requested).
*/
if (bpp != NULL) {
error = bread(vp, lbn, fs->fs_bsize,
B_MODIFY, bpp);
if (error) {
return (error);
}
}
return (0);
}
if (nb != 0) {
fail:
/*
* If we have failed part way through block allocation, we
* have to deallocate any indirect blocks that we have allocated.
*/
if (unwindidx >= 0) {
/*
* First write out any buffers we've created to resolve their
* softdeps. This must be done in reverse order of creation
* so that we resolve the dependencies in one pass.
* Write the cylinder group buffers for these buffers too.
*/
for (i = num; i >= unwindidx; i--) {
if (i == 0) {
break;
}
if (ffs_getblk(vp, indirs[i].in_lbn, FFS_NOBLK,
fs->fs_bsize, false, &bp) != 0)
continue;
if (bp->b_oflags & BO_DELWRI) {
nb = FFS_FSBTODB(fs, cgtod(fs, dtog(fs,
FFS_DBTOFSB(fs, bp->b_blkno))));
bwrite(bp);
if (ffs_getblk(ip->i_devvp, nb, FFS_NOBLK,
fs->fs_cgsize, false, &bp) != 0)
continue;
if (bp->b_oflags & BO_DELWRI) {
bwrite(bp);
} else {
brelse(bp, BC_INVAL);
}
} else {
brelse(bp, BC_INVAL);
}
}
/*
* Now that any dependencies that we created have been
* resolved, we can undo the partial allocation.
*/
if (unwindidx == 0) {
*allocib = 0;
ip->i_flag |= IN_CHANGE | IN_UPDATE;
} else {
int r;
r = bread(vp, indirs[unwindidx].in_lbn,
(int)fs->fs_bsize, 0, &bp);
if (r) {
panic("Could not unwind indirect block, error %d", r);
} else {
bap = (int64_t *)bp->b_data;
bap[indirs[unwindidx].in_off] = 0;
bwrite(bp);
}
}
for (i = unwindidx + 1; i <= num; i++) {
if (ffs_getblk(vp, indirs[i].in_lbn, FFS_NOBLK,
fs->fs_bsize, false, &bp) == 0)
brelse(bp, BC_INVAL);
}
}
for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
ffs_blkfree(fs, ip->i_devvp, *blkp, fs->fs_bsize, ip->i_number);
deallocated += fs->fs_bsize;
}
if (deallocated) {
#if defined(QUOTA) || defined(QUOTA2)
/*
* Restore user's disk quota because allocation failed.
*/
(void)chkdq(ip, -btodb(deallocated), cred, FORCE);
#endif
ip->i_ffs2_blocks -= btodb(deallocated);
ip->i_flag |= IN_CHANGE | IN_UPDATE;
}