#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ext2fs_inode.c,v 1.91 2023/08/26 05:22:50 riastradh Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mount.h>
#include <sys/proc.h>
#include <sys/file.h>
#include <sys/buf.h>
#include <sys/vnode.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/trace.h>
#include <sys/resourcevar.h>
#include <sys/kauth.h>
#include <ufs/ufs/inode.h>
#include <ufs/ufs/ufsmount.h>
#include <ufs/ufs/ufs_extern.h>
#include <ufs/ext2fs/ext2fs.h>
#include <ufs/ext2fs/ext2fs_extern.h>
static int ext2fs_indirtrunc(struct inode *, daddr_t, daddr_t,
daddr_t, int, long *);
CTASSERT(EXT2FS_NDADDR == UFS_NDADDR);
CTASSERT(EXT2FS_NIADDR == UFS_NIADDR);
uint64_t
ext2fs_size(struct inode *ip)
{
uint64_t size = ip->i_e2fs_size;
if ((ip->i_e2fs_mode & IFMT) == IFREG)
size |= (uint64_t)ip->i_din.e2fs_din->e2di_size_high << 32;
return size;
}
int
ext2fs_setsize(struct inode *ip, uint64_t size)
{
if ((ip->i_e2fs_mode & IFMT) == IFREG ||
ip->i_e2fs_mode == 0) {
ip->i_din.e2fs_din->e2di_size_high = size >> 32;
if (size >= 0x80000000U) {
struct m_ext2fs *fs = ip->i_e2fs;
if (fs->e2fs.e2fs_rev <= E2FS_REV0) {
return EFBIG;
}
if (!EXT2F_HAS_ROCOMPAT_FEATURE(fs,
EXT2F_ROCOMPAT_LARGEFILE)) {
fs->e2fs.e2fs_features_rocompat |=
EXT2F_ROCOMPAT_LARGEFILE;
fs->e2fs_fmod = 1;
}
}
} else if (size >= 0x80000000U)
return EFBIG;
ip->i_e2fs_size = size;
return 0;
}
uint64_t
ext2fs_nblock(struct inode *ip)
{
uint64_t nblock = ip->i_e2fs_nblock;
struct m_ext2fs * const fs = ip->i_e2fs;
if (EXT2F_HAS_ROCOMPAT_FEATURE(fs, EXT2F_ROCOMPAT_HUGE_FILE)) {
nblock |= (uint64_t)ip->i_e2fs_nblock_high << 32;
if ((ip->i_e2fs_flags & EXT2_HUGE_FILE)) {
nblock = EXT2_FSBTODB(fs, nblock);
}
}
return nblock;
}
int
ext2fs_setnblock(struct inode *ip, uint64_t nblock)
{
struct m_ext2fs * const fs = ip->i_e2fs;
if (nblock <= 0xffffffffULL) {
CLR(ip->i_e2fs_flags, EXT2_HUGE_FILE);
ip->i_e2fs_nblock = nblock;
return 0;
}
if (!EXT2F_HAS_ROCOMPAT_FEATURE(fs, EXT2F_ROCOMPAT_HUGE_FILE))
return EFBIG;
if (nblock <= 0xffffffffffffULL) {
CLR(ip->i_e2fs_flags, EXT2_HUGE_FILE);
ip->i_e2fs_nblock = nblock & 0xffffffff;
ip->i_e2fs_nblock_high = (nblock >> 32) & 0xffff;
return 0;
}
if (EXT2_DBTOFSB(fs, nblock) <= 0xffffffffffffULL) {
SET(ip->i_e2fs_flags, EXT2_HUGE_FILE);
ip->i_e2fs_nblock = EXT2_DBTOFSB(fs, nblock) & 0xffffffff;
ip->i_e2fs_nblock_high = (EXT2_DBTOFSB(fs, nblock) >> 32) & 0xffff;
return 0;
}
return EFBIG;
}
int
ext2fs_inactive(void *v)
{
struct vop_inactive_v2_args
*ap = v;
struct vnode *vp = ap->a_vp;
struct inode *ip = VTOI(vp);
int error = 0;
if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0)
goto out;
error = 0;
if (ip->i_e2fs_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
if (ext2fs_size(ip) != 0) {
error = ext2fs_truncate(vp, (off_t)0, 0, NOCRED);
}
ip->i_e2fs_dtime = time_second;
ip->i_flag |= IN_CHANGE | IN_UPDATE;
ip->i_omode = 1;
}
if (ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) {
ext2fs_update(vp, NULL, NULL, 0);
}
out:
*ap->a_recycle = (ip->i_e2fs_dtime != 0);
return error;
}
int
ext2fs_update(struct vnode *vp, const struct timespec *acc,
const struct timespec *mod, int updflags)
{
struct m_ext2fs *fs;
struct buf *bp;
struct inode *ip;
int error;
void *cp;
int flags;
if (vp->v_mount->mnt_flag & MNT_RDONLY)
return 0;
ip = VTOI(vp);
EXT2FS_ITIMES(ip, acc, mod, NULL);
if (updflags & UPDATE_CLOSE)
flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
else
flags = ip->i_flag & IN_MODIFIED;
if (flags == 0)
return 0;
fs = ip->i_e2fs;
error = bread(ip->i_devvp,
EXT2_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)),
(int)fs->e2fs_bsize, B_MODIFY, &bp);
if (error) {
return error;
}
ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
cp = (char *)bp->b_data +
(ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE(fs));
e2fs_isave(ip->i_din.e2fs_din, (struct ext2fs_dinode *)cp, EXT2_DINODE_SIZE(fs));
if ((updflags & (UPDATE_WAIT|UPDATE_DIROP)) != 0 &&
(flags & IN_MODIFIED) != 0 &&
(vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
return bwrite(bp);
else {
bdwrite(bp);
return 0;
}
}
#define SINGLE 0
#define DOUBLE 1
#define TRIPLE 2
int
ext2fs_truncate(struct vnode *ovp, off_t length, int ioflag,
kauth_cred_t cred)
{
daddr_t lastblock;
struct inode *oip = VTOI(ovp);
daddr_t bn, lastiblock[EXT2FS_NIADDR], indir_lbn[EXT2FS_NIADDR];
int32_t oldblks[EXT2FS_NDADDR + EXT2FS_NIADDR], newblks[EXT2FS_NDADDR + EXT2FS_NIADDR];
struct m_ext2fs *fs;
int offset, size, level;
long count, blocksreleased = 0;
int i, nblocks;
int error, allerror = 0;
off_t osize;
int sync;
struct ufsmount *ump = oip->i_ump;
if (ovp->v_type == VCHR || ovp->v_type == VBLK ||
ovp->v_type == VFIFO || ovp->v_type == VSOCK) {
return 0;
}
if (length < 0)
return EINVAL;
if (ovp->v_type == VLNK &&
(ext2fs_size(oip) < ump->um_maxsymlinklen ||
(ump->um_maxsymlinklen == 0 && ext2fs_nblock(oip) == 0))) {
KDASSERT(length == 0);
memset((char *)&oip->i_din.e2fs_din->e2di_shortlink, 0,
(u_int)ext2fs_size(oip));
(void)ext2fs_setsize(oip, 0);
goto update;
}
if (ext2fs_size(oip) == length) {
uvm_vnp_setsize(ovp, length);
goto update;
}
fs = oip->i_e2fs;
if (length > ump->um_maxfilesize)
return EFBIG;
osize = ext2fs_size(oip);
if (osize < length) {
uvm_vnp_setwritesize(ovp, length);
error = ufs_balloc_range(ovp, length - 1, 1, cred,
ioflag & IO_SYNC ? B_SYNC : 0);
if (error) {
(void) ext2fs_truncate(ovp, osize, ioflag & IO_SYNC,
cred);
return error;
}
uvm_vnp_setsize(ovp, length);
KASSERT(error || ovp->v_size == ext2fs_size(oip));
goto update;
}
offset = ext2_blkoff(fs, length);
if (offset != 0) {
size = fs->e2fs_bsize;
ubc_zerorange(&ovp->v_uobj, length, size - offset,
UBC_VNODE_FLAGS(ovp));
}
(void)ext2fs_setsize(oip, length);
uvm_vnp_setsize(ovp, length);
lastblock = ext2_lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
lastiblock[SINGLE] = lastblock - EXT2FS_NDADDR;
lastiblock[DOUBLE] = lastiblock[SINGLE] - EXT2_NINDIR(fs);
lastiblock[TRIPLE] = lastiblock[DOUBLE] - EXT2_NINDIR(fs) * EXT2_NINDIR(fs);
nblocks = btodb(fs->e2fs_bsize);
memcpy((void *)oldblks, (void *)&oip->i_e2fs_blocks[0], sizeof oldblks);
sync = 0;
for (level = TRIPLE; level >= SINGLE; level--) {
if (lastiblock[level] < 0 && oldblks[EXT2FS_NDADDR + level] != 0) {
sync = 1;
oip->i_e2fs_blocks[EXT2FS_NDADDR + level] = 0;
lastiblock[level] = -1;
}
}
for (i = 0; i < EXT2FS_NDADDR; i++) {
if (i > lastblock && oldblks[i] != 0) {
sync = 1;
oip->i_e2fs_blocks[i] = 0;
}
}
oip->i_flag |= IN_CHANGE | IN_UPDATE;
if (sync) {
error = ext2fs_update(ovp, NULL, NULL, UPDATE_WAIT);
if (error && !allerror)
allerror = error;
}
memcpy((void *)newblks, (void *)&oip->i_e2fs_blocks[0], sizeof newblks);
memcpy((void *)&oip->i_e2fs_blocks[0], (void *)oldblks, sizeof oldblks);
(void)ext2fs_setsize(oip, osize);
error = vtruncbuf(ovp, lastblock + 1, 0, 0);
if (error && !allerror)
allerror = error;
indir_lbn[SINGLE] = -EXT2FS_NDADDR;
indir_lbn[DOUBLE] = indir_lbn[SINGLE] - EXT2_NINDIR(fs) -1;
indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - EXT2_NINDIR(fs) * EXT2_NINDIR(fs) - 1;
for (level = TRIPLE; level >= SINGLE; level--) {
bn = fs2h32(oip->i_e2fs_blocks[EXT2FS_NDADDR + level]);
if (bn != 0) {
error = ext2fs_indirtrunc(oip, indir_lbn[level],
EXT2_FSBTODB(fs, bn), lastiblock[level], level, &count);
if (error)
allerror = error;
blocksreleased += count;
if (lastiblock[level] < 0) {
oip->i_e2fs_blocks[EXT2FS_NDADDR + level] = 0;
ext2fs_blkfree(oip, bn);
blocksreleased += nblocks;
}
}
if (lastiblock[level] >= 0)
goto done;
}
for (i = EXT2FS_NDADDR - 1; i > lastblock; i--) {
bn = fs2h32(oip->i_e2fs_blocks[i]);
if (bn == 0)
continue;
oip->i_e2fs_blocks[i] = 0;
ext2fs_blkfree(oip, bn);
blocksreleased += btodb(fs->e2fs_bsize);
}
done:
#ifdef DIAGNOSTIC
for (level = SINGLE; level <= TRIPLE; level++)
if (newblks[EXT2FS_NDADDR + level] !=
oip->i_e2fs_blocks[EXT2FS_NDADDR + level])
panic("ext2fs_truncate1");
for (i = 0; i < EXT2FS_NDADDR; i++)
if (newblks[i] != oip->i_e2fs_blocks[i])
panic("ext2fs_truncate2");
if (length == 0 &&
(!LIST_EMPTY(&ovp->v_cleanblkhd) ||
!LIST_EMPTY(&ovp->v_dirtyblkhd)))
panic("ext2fs_truncate3");
#endif
(void)ext2fs_setsize(oip, length);
error = ext2fs_setnblock(oip, ext2fs_nblock(oip) - blocksreleased);
if (error != 0)
allerror = error;
oip->i_flag |= IN_CHANGE;
KASSERT(ovp->v_type != VREG || ovp->v_size == ext2fs_size(oip));
return allerror;
update:
oip->i_flag |= IN_CHANGE | IN_UPDATE;
return ext2fs_update(ovp, NULL, NULL, 0);
}
static int
ext2fs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn, daddr_t lastbn,
int level, long *countp)
{
int i;
struct buf *bp;
struct m_ext2fs *fs = ip->i_e2fs;
int32_t *bap;
struct vnode *vp;
daddr_t nb, nlbn, last;
int32_t *copy = NULL;
long blkcount, factor;
int nblocks, blocksreleased = 0;
int error = 0, allerror = 0;
factor = 1;
for (i = SINGLE; i < level; i++)
factor *= EXT2_NINDIR(fs);
last = lastbn;
if (lastbn > 0)
last /= factor;
nblocks = btodb(fs->e2fs_bsize);
vp = ITOV(ip);
bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0);
if (bp->b_oflags & (BO_DONE | BO_DELWRI)) {
trace(TR_BREADHIT, pack(vp, fs->e2fs_bsize), lbn);
} else {
trace(TR_BREADMISS, pack(vp, fs->e2fs_bsize), lbn);
curlwp->l_ru.ru_inblock++;
bp->b_flags |= B_READ;
if (bp->b_bcount > bp->b_bufsize)
panic("ext2fs_indirtrunc: bad buffer size");
bp->b_blkno = dbn;
VOP_STRATEGY(vp, bp);
error = biowait(bp);
}
if (error) {
brelse(bp, 0);
*countp = 0;
return error;
}
bap = (int32_t *)bp->b_data;
if (lastbn >= 0) {
copy = kmem_alloc(fs->e2fs_bsize, KM_SLEEP);
memcpy((void *)copy, (void *)bap, (u_int)fs->e2fs_bsize);
memset((void *)&bap[last + 1], 0,
(u_int)(EXT2_NINDIR(fs) - (last + 1)) * sizeof (uint32_t));
error = bwrite(bp);
if (error)
allerror = error;
bap = copy;
}
for (i = EXT2_NINDIR(fs) - 1,
nlbn = lbn + 1 - i * factor; i > last;
i--, nlbn += factor) {
nb = fs2h32(bap[i]);
if (nb == 0)
continue;
if (level > SINGLE) {
error = ext2fs_indirtrunc(ip, nlbn, EXT2_FSBTODB(fs, nb),
(daddr_t)-1, level - 1,
&blkcount);
if (error)
allerror = error;
blocksreleased += blkcount;
}
ext2fs_blkfree(ip, nb);
blocksreleased += nblocks;
}
if (level > SINGLE && lastbn >= 0) {
last = lastbn % factor;
nb = fs2h32(bap[i]);
if (nb != 0) {
error = ext2fs_indirtrunc(ip, nlbn, EXT2_FSBTODB(fs, nb),
last, level - 1, &blkcount);
if (error)
allerror = error;
blocksreleased += blkcount;
}
}
if (copy != NULL) {
kmem_free(copy, fs->e2fs_bsize);
} else {
brelse(bp, BC_INVAL);
}
*countp = blocksreleased;
return allerror;
}