2005-01-06 18:27:30 +00:00
|
|
|
/*-
|
1995-11-05 23:25:12 +00:00
|
|
|
* modified for Lites 1.1
|
|
|
|
*
|
|
|
|
* Aug 1995, Godmar Back (gback@cs.utah.edu)
|
|
|
|
* University of Utah, Department of Computer Science
|
|
|
|
*/
|
2005-01-06 18:27:30 +00:00
|
|
|
/*-
|
2017-11-20 19:43:44 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1995-11-05 23:25:12 +00:00
|
|
|
* 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.
|
2016-12-02 19:47:23 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1995-11-05 23:25:12 +00:00
|
|
|
* 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.4 (Berkeley) 9/23/93
|
2000-05-05 09:59:14 +00:00
|
|
|
* $FreeBSD$
|
1995-11-05 23:25:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2020-05-17 14:52:54 +00:00
|
|
|
#include <sys/endian.h>
|
2000-05-05 09:59:14 +00:00
|
|
|
#include <sys/bio.h>
|
1995-11-05 23:25:12 +00:00
|
|
|
#include <sys/buf.h>
|
2018-01-05 10:04:01 +00:00
|
|
|
#include <sys/limits.h>
|
1997-12-05 19:55:52 +00:00
|
|
|
#include <sys/lock.h>
|
2011-04-28 14:27:17 +00:00
|
|
|
#include <sys/mount.h>
|
1995-11-05 23:25:12 +00:00
|
|
|
#include <sys/vnode.h>
|
|
|
|
|
2013-06-16 16:10:45 +00:00
|
|
|
#include <fs/ext2fs/fs.h>
|
2010-01-14 14:30:54 +00:00
|
|
|
#include <fs/ext2fs/inode.h>
|
|
|
|
#include <fs/ext2fs/ext2fs.h>
|
2013-06-16 16:10:45 +00:00
|
|
|
#include <fs/ext2fs/ext2_dinode.h>
|
2010-01-14 14:30:54 +00:00
|
|
|
#include <fs/ext2fs/ext2_extern.h>
|
|
|
|
#include <fs/ext2fs/ext2_mount.h>
|
2013-06-16 16:10:45 +00:00
|
|
|
|
2017-10-17 20:45:44 +00:00
|
|
|
static int
|
|
|
|
ext2_ext_balloc(struct inode *ip, uint32_t lbn, int size,
|
|
|
|
struct ucred *cred, struct buf **bpp, int flags)
|
|
|
|
{
|
|
|
|
struct m_ext2fs *fs;
|
|
|
|
struct buf *bp = NULL;
|
|
|
|
struct vnode *vp = ITOV(ip);
|
2018-01-05 10:04:01 +00:00
|
|
|
daddr_t newblk;
|
2019-07-07 08:56:13 +00:00
|
|
|
int blks, error, allocated;
|
2017-10-17 20:45:44 +00:00
|
|
|
|
|
|
|
fs = ip->i_e2fs;
|
|
|
|
blks = howmany(size, fs->e2fs_bsize);
|
|
|
|
|
2018-01-05 10:04:01 +00:00
|
|
|
error = ext4_ext_get_blocks(ip, lbn, blks, cred, NULL, &allocated, &newblk);
|
2017-10-17 20:45:44 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
if (allocated) {
|
2019-07-07 08:56:13 +00:00
|
|
|
bp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
|
2017-10-17 20:45:44 +00:00
|
|
|
if(!bp)
|
|
|
|
return (EIO);
|
|
|
|
} else {
|
2019-07-07 08:56:13 +00:00
|
|
|
error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp);
|
2018-01-05 10:04:01 +00:00
|
|
|
if (error) {
|
|
|
|
return (error);
|
2017-10-17 20:45:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-07 08:56:13 +00:00
|
|
|
bp->b_blkno = fsbtodb(fs, newblk);
|
|
|
|
if (flags & BA_CLRBUF)
|
|
|
|
vfs_bio_clrbuf(bp);
|
|
|
|
|
2017-10-17 20:45:44 +00:00
|
|
|
*bpp = bp;
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1995-11-05 23:25:12 +00:00
|
|
|
/*
|
2013-06-11 02:47:07 +00:00
|
|
|
* Balloc defines the structure of filesystem storage
|
1995-11-05 23:25:12 +00:00
|
|
|
* by allocating the physical blocks on a device given
|
|
|
|
* the inode and the logical block number in a file.
|
|
|
|
*/
|
|
|
|
int
|
2013-06-23 02:44:42 +00:00
|
|
|
ext2_balloc(struct inode *ip, e2fs_lbn_t lbn, int size, struct ucred *cred,
|
2013-02-10 19:49:37 +00:00
|
|
|
struct buf **bpp, int flags)
|
1995-11-05 23:25:12 +00:00
|
|
|
{
|
2010-01-14 14:30:54 +00:00
|
|
|
struct m_ext2fs *fs;
|
|
|
|
struct ext2mount *ump;
|
1995-11-05 23:25:12 +00:00
|
|
|
struct buf *bp, *nbp;
|
|
|
|
struct vnode *vp = ITOV(ip);
|
2017-02-15 19:50:26 +00:00
|
|
|
struct indir indirs[EXT2_NIADDR + 2];
|
2013-08-13 15:40:43 +00:00
|
|
|
e4fs_daddr_t nb, newb;
|
|
|
|
e2fs_daddr_t *bap, pref;
|
2019-07-07 08:56:13 +00:00
|
|
|
int num, i, error;
|
2010-01-14 14:30:54 +00:00
|
|
|
|
1995-11-05 23:25:12 +00:00
|
|
|
*bpp = NULL;
|
2010-01-14 14:30:54 +00:00
|
|
|
if (lbn < 0)
|
1995-11-05 23:25:12 +00:00
|
|
|
return (EFBIG);
|
|
|
|
fs = ip->i_e2fs;
|
2010-01-14 14:30:54 +00:00
|
|
|
ump = ip->i_ump;
|
1995-11-05 23:25:12 +00:00
|
|
|
|
|
|
|
/*
|
2016-12-28 15:43:17 +00:00
|
|
|
* check if this is a sequential block allocation.
|
|
|
|
* If so, increment next_alloc fields to allow ext2_blkpref
|
1995-11-05 23:25:12 +00:00
|
|
|
* to make a good guess
|
|
|
|
*/
|
2014-02-28 21:25:32 +00:00
|
|
|
if (lbn == ip->i_next_alloc_block + 1) {
|
1995-11-05 23:25:12 +00:00
|
|
|
ip->i_next_alloc_block++;
|
|
|
|
ip->i_next_alloc_goal++;
|
|
|
|
}
|
2017-10-17 20:45:44 +00:00
|
|
|
|
|
|
|
if (ip->i_flag & IN_E4EXTENTS)
|
|
|
|
return (ext2_ext_balloc(ip, lbn, size, cred, bpp, flags));
|
|
|
|
|
1995-11-05 23:25:12 +00:00
|
|
|
/*
|
2017-02-15 19:50:26 +00:00
|
|
|
* The first EXT2_NDADDR blocks are direct blocks
|
1995-11-05 23:25:12 +00:00
|
|
|
*/
|
2017-02-15 19:50:26 +00:00
|
|
|
if (lbn < EXT2_NDADDR) {
|
2010-01-14 14:30:54 +00:00
|
|
|
nb = ip->i_db[lbn];
|
2016-12-28 15:43:17 +00:00
|
|
|
/*
|
|
|
|
* no new block is to be allocated, and no need to expand
|
|
|
|
* the file
|
|
|
|
*/
|
2019-07-07 08:56:13 +00:00
|
|
|
if (nb != 0) {
|
2010-01-14 14:30:54 +00:00
|
|
|
error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp);
|
1995-11-05 23:25:12 +00:00
|
|
|
if (error) {
|
|
|
|
return (error);
|
|
|
|
}
|
2001-04-25 10:33:09 +00:00
|
|
|
bp->b_blkno = fsbtodb(fs, nb);
|
2019-07-07 08:56:13 +00:00
|
|
|
if (ip->i_size >= (lbn + 1) * fs->e2fs_bsize) {
|
|
|
|
*bpp = bp;
|
|
|
|
return (0);
|
1995-11-05 23:25:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-01-14 14:30:54 +00:00
|
|
|
EXT2_LOCK(ump);
|
|
|
|
error = ext2_alloc(ip, lbn,
|
|
|
|
ext2_blkpref(ip, lbn, (int)lbn, &ip->i_db[0], 0),
|
2019-07-07 08:56:13 +00:00
|
|
|
fs->e2fs_bsize, cred, &newb);
|
1995-11-05 23:25:12 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
2018-01-05 10:04:01 +00:00
|
|
|
/*
|
|
|
|
* If the newly allocated block exceeds 32-bit limit,
|
|
|
|
* we can not use it in file block maps.
|
|
|
|
*/
|
|
|
|
if (newb > UINT_MAX)
|
|
|
|
return (EFBIG);
|
2019-07-07 08:56:13 +00:00
|
|
|
bp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
|
1995-11-05 23:25:12 +00:00
|
|
|
bp->b_blkno = fsbtodb(fs, newb);
|
2011-04-28 14:27:17 +00:00
|
|
|
if (flags & BA_CLRBUF)
|
1995-11-05 23:25:12 +00:00
|
|
|
vfs_bio_clrbuf(bp);
|
|
|
|
}
|
2010-01-14 14:30:54 +00:00
|
|
|
ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
|
1995-11-05 23:25:12 +00:00
|
|
|
ip->i_flag |= IN_CHANGE | IN_UPDATE;
|
|
|
|
*bpp = bp;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Determine the number of levels of indirection.
|
|
|
|
*/
|
|
|
|
pref = 0;
|
2010-01-14 14:30:54 +00:00
|
|
|
if ((error = ext2_getlbns(vp, lbn, indirs, &num)) != 0)
|
2011-12-16 15:47:43 +00:00
|
|
|
return (error);
|
2013-06-12 15:24:48 +00:00
|
|
|
#ifdef INVARIANTS
|
1995-11-05 23:25:12 +00:00
|
|
|
if (num < 1)
|
2016-12-28 15:43:17 +00:00
|
|
|
panic("ext2_balloc: ext2_getlbns returned indirect block");
|
1995-11-05 23:25:12 +00:00
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* Fetch the first indirect block allocating if necessary.
|
|
|
|
*/
|
|
|
|
--num;
|
|
|
|
nb = ip->i_ib[indirs[0].in_off];
|
|
|
|
if (nb == 0) {
|
2010-01-14 14:30:54 +00:00
|
|
|
EXT2_LOCK(ump);
|
2016-12-28 15:43:17 +00:00
|
|
|
pref = ext2_blkpref(ip, lbn, indirs[0].in_off +
|
|
|
|
EXT2_NDIR_BLOCKS, &ip->i_db[0], 0);
|
2014-02-28 21:25:32 +00:00
|
|
|
if ((error = ext2_alloc(ip, lbn, pref, fs->e2fs_bsize, cred,
|
2016-12-28 15:43:17 +00:00
|
|
|
&newb)))
|
1995-11-05 23:25:12 +00:00
|
|
|
return (error);
|
2018-01-05 10:04:01 +00:00
|
|
|
if (newb > UINT_MAX)
|
|
|
|
return (EFBIG);
|
1995-11-05 23:25:12 +00:00
|
|
|
nb = newb;
|
2010-01-14 14:30:54 +00:00
|
|
|
bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0, 0);
|
1995-11-05 23:25:12 +00:00
|
|
|
bp->b_blkno = fsbtodb(fs, newb);
|
|
|
|
vfs_bio_clrbuf(bp);
|
|
|
|
/*
|
|
|
|
* Write synchronously so that indirect blocks
|
|
|
|
* never point at garbage.
|
|
|
|
*/
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = bwrite(bp)) != 0) {
|
2010-01-14 14:30:54 +00:00
|
|
|
ext2_blkfree(ip, nb, fs->e2fs_bsize);
|
1995-11-05 23:25:12 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
ip->i_ib[indirs[0].in_off] = newb;
|
|
|
|
ip->i_flag |= IN_CHANGE | IN_UPDATE;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Fetch through the indirect blocks, allocating as necessary.
|
|
|
|
*/
|
|
|
|
for (i = 1;;) {
|
|
|
|
error = bread(vp,
|
2010-01-14 14:30:54 +00:00
|
|
|
indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp);
|
1995-11-05 23:25:12 +00:00
|
|
|
if (error) {
|
|
|
|
return (error);
|
|
|
|
}
|
2013-08-13 15:40:43 +00:00
|
|
|
bap = (e2fs_daddr_t *)bp->b_data;
|
2020-05-17 14:52:54 +00:00
|
|
|
nb = le32toh(bap[indirs[i].in_off]);
|
1995-11-05 23:25:12 +00:00
|
|
|
if (i == num)
|
|
|
|
break;
|
|
|
|
i += 1;
|
|
|
|
if (nb != 0) {
|
2010-01-14 14:30:54 +00:00
|
|
|
bqrelse(bp);
|
1995-11-05 23:25:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-01-14 14:30:54 +00:00
|
|
|
EXT2_LOCK(ump);
|
|
|
|
if (pref == 0)
|
1995-11-05 23:25:12 +00:00
|
|
|
pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap,
|
2016-12-28 15:43:17 +00:00
|
|
|
bp->b_lblkno);
|
|
|
|
error = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newb);
|
2010-01-14 14:30:54 +00:00
|
|
|
if (error) {
|
1995-11-05 23:25:12 +00:00
|
|
|
brelse(bp);
|
|
|
|
return (error);
|
|
|
|
}
|
2018-01-05 10:04:01 +00:00
|
|
|
if (newb > UINT_MAX)
|
|
|
|
return (EFBIG);
|
1995-11-05 23:25:12 +00:00
|
|
|
nb = newb;
|
2010-01-14 14:30:54 +00:00
|
|
|
nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0, 0);
|
1995-11-05 23:25:12 +00:00
|
|
|
nbp->b_blkno = fsbtodb(fs, nb);
|
|
|
|
vfs_bio_clrbuf(nbp);
|
|
|
|
/*
|
|
|
|
* Write synchronously so that indirect blocks
|
|
|
|
* never point at garbage.
|
|
|
|
*/
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = bwrite(nbp)) != 0) {
|
2010-01-14 14:30:54 +00:00
|
|
|
ext2_blkfree(ip, nb, fs->e2fs_bsize);
|
1995-11-05 23:25:12 +00:00
|
|
|
brelse(bp);
|
|
|
|
return (error);
|
|
|
|
}
|
2020-05-17 14:52:54 +00:00
|
|
|
bap[indirs[i - 1].in_off] = htole32(nb);
|
1995-11-05 23:25:12 +00:00
|
|
|
/*
|
|
|
|
* If required, write synchronously, otherwise use
|
|
|
|
* delayed write.
|
|
|
|
*/
|
2011-04-28 14:27:17 +00:00
|
|
|
if (flags & IO_SYNC) {
|
1995-11-05 23:25:12 +00:00
|
|
|
bwrite(bp);
|
|
|
|
} else {
|
2010-01-14 14:30:54 +00:00
|
|
|
if (bp->b_bufsize == fs->e2fs_bsize)
|
|
|
|
bp->b_flags |= B_CLUSTEROK;
|
1995-11-05 23:25:12 +00:00
|
|
|
bdwrite(bp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Get the data block, allocating if necessary.
|
|
|
|
*/
|
|
|
|
if (nb == 0) {
|
2010-01-14 14:30:54 +00:00
|
|
|
EXT2_LOCK(ump);
|
2016-12-28 15:43:17 +00:00
|
|
|
pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0],
|
|
|
|
bp->b_lblkno);
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = ext2_alloc(ip,
|
2010-01-14 14:30:54 +00:00
|
|
|
lbn, pref, (int)fs->e2fs_bsize, cred, &newb)) != 0) {
|
1995-11-05 23:25:12 +00:00
|
|
|
brelse(bp);
|
|
|
|
return (error);
|
|
|
|
}
|
2018-01-05 10:04:01 +00:00
|
|
|
if (newb > UINT_MAX)
|
|
|
|
return (EFBIG);
|
1995-11-05 23:25:12 +00:00
|
|
|
nb = newb;
|
2010-01-14 14:30:54 +00:00
|
|
|
nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
|
1995-11-05 23:25:12 +00:00
|
|
|
nbp->b_blkno = fsbtodb(fs, nb);
|
2011-04-28 14:27:17 +00:00
|
|
|
if (flags & BA_CLRBUF)
|
1995-11-05 23:25:12 +00:00
|
|
|
vfs_bio_clrbuf(nbp);
|
2020-05-17 14:52:54 +00:00
|
|
|
bap[indirs[i].in_off] = htole32(nb);
|
1995-11-05 23:25:12 +00:00
|
|
|
/*
|
|
|
|
* If required, write synchronously, otherwise use
|
|
|
|
* delayed write.
|
|
|
|
*/
|
2011-04-28 14:27:17 +00:00
|
|
|
if (flags & IO_SYNC) {
|
1995-11-05 23:25:12 +00:00
|
|
|
bwrite(bp);
|
|
|
|
} else {
|
2016-12-28 15:43:17 +00:00
|
|
|
if (bp->b_bufsize == fs->e2fs_bsize)
|
2010-01-14 14:30:54 +00:00
|
|
|
bp->b_flags |= B_CLUSTEROK;
|
1995-11-05 23:25:12 +00:00
|
|
|
bdwrite(bp);
|
|
|
|
}
|
|
|
|
*bpp = nbp;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
brelse(bp);
|
2011-04-28 14:27:17 +00:00
|
|
|
if (flags & BA_CLRBUF) {
|
|
|
|
int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
|
2016-12-28 15:43:17 +00:00
|
|
|
|
2011-04-28 14:27:17 +00:00
|
|
|
if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
|
|
|
|
error = cluster_read(vp, ip->i_size, lbn,
|
|
|
|
(int)fs->e2fs_bsize, NOCRED,
|
2013-03-14 20:28:26 +00:00
|
|
|
MAXBSIZE, seqcount, 0, &nbp);
|
2011-04-28 14:27:17 +00:00
|
|
|
} else {
|
|
|
|
error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED, &nbp);
|
|
|
|
}
|
1995-11-05 23:25:12 +00:00
|
|
|
if (error) {
|
|
|
|
brelse(nbp);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
} else {
|
2010-01-14 14:30:54 +00:00
|
|
|
nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
|
1995-11-05 23:25:12 +00:00
|
|
|
nbp->b_blkno = fsbtodb(fs, nb);
|
|
|
|
}
|
|
|
|
*bpp = nbp;
|
|
|
|
return (0);
|
|
|
|
}
|