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
|
|
|
/*-
|
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.
|
|
|
|
* 4. 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.
|
|
|
|
*
|
2002-03-23 13:41:52 +00:00
|
|
|
* @(#)ffs_inode.c 8.5 (Berkeley) 12/30/93
|
2000-03-20 10:44:49 +00:00
|
|
|
* $FreeBSD$
|
1995-11-05 23:25:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/mount.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>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
|
|
|
|
#include <vm/vm.h>
|
1995-12-10 14:52:10 +00:00
|
|
|
#include <vm/vm_extern.h>
|
1995-11-05 23:25:12 +00:00
|
|
|
|
2010-01-14 14:30:54 +00:00
|
|
|
#include <fs/ext2fs/inode.h>
|
|
|
|
#include <fs/ext2fs/ext2_mount.h>
|
|
|
|
#include <fs/ext2fs/ext2fs.h>
|
|
|
|
#include <fs/ext2fs/fs.h>
|
|
|
|
#include <fs/ext2fs/ext2_extern.h>
|
1995-11-05 23:25:12 +00:00
|
|
|
|
2002-05-18 19:12:38 +00:00
|
|
|
static int ext2_indirtrunc(struct inode *, int32_t, int32_t, int32_t, int,
|
2002-03-19 22:40:48 +00:00
|
|
|
long *);
|
1995-11-05 23:25:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the access, modified, and inode change times as specified by the
|
1998-07-03 22:17:03 +00:00
|
|
|
* IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively. Write the inode
|
|
|
|
* to disk if the IN_MODIFIED flag is set (it may be set initially, or by
|
|
|
|
* the timestamp update). The IN_LAZYMOD flag is set to force a write
|
|
|
|
* later if not now. If we write now, then clear both IN_MODIFIED and
|
|
|
|
* IN_LAZYMOD to reflect the presumably successful write, and if waitfor is
|
|
|
|
* set, then wait for the write to complete.
|
1995-11-05 23:25:12 +00:00
|
|
|
*/
|
|
|
|
int
|
1999-01-07 16:14:19 +00:00
|
|
|
ext2_update(vp, waitfor)
|
1997-10-16 20:32:40 +00:00
|
|
|
struct vnode *vp;
|
|
|
|
int waitfor;
|
1995-11-05 23:25:12 +00:00
|
|
|
{
|
2010-01-14 14:30:54 +00:00
|
|
|
struct m_ext2fs *fs;
|
1995-11-05 23:25:12 +00:00
|
|
|
struct buf *bp;
|
|
|
|
struct inode *ip;
|
|
|
|
int error;
|
|
|
|
|
2010-01-14 14:30:54 +00:00
|
|
|
ASSERT_VOP_ELOCKED(vp, "ext2_update");
|
2002-05-16 19:08:03 +00:00
|
|
|
ext2_itimes(vp);
|
1997-10-16 20:32:40 +00:00
|
|
|
ip = VTOI(vp);
|
2010-01-14 14:30:54 +00:00
|
|
|
if ((ip->i_flag & IN_MODIFIED) == 0 && waitfor == 0)
|
1995-11-05 23:25:12 +00:00
|
|
|
return (0);
|
2010-01-14 14:30:54 +00:00
|
|
|
ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED);
|
1995-11-05 23:25:12 +00:00
|
|
|
fs = ip->i_e2fs;
|
2010-01-14 14:30:54 +00:00
|
|
|
if(fs->e2fs_ronly)
|
|
|
|
return (0);
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = bread(ip->i_devvp,
|
1995-11-05 23:25:12 +00:00
|
|
|
fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
|
2010-01-14 14:30:54 +00:00
|
|
|
(int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
|
1995-11-05 23:25:12 +00:00
|
|
|
brelse(bp);
|
|
|
|
return (error);
|
|
|
|
}
|
2010-01-14 14:30:54 +00:00
|
|
|
ext2_i2ei(ip, (struct ext2fs_dinode *)((char *)bp->b_data +
|
2009-01-18 14:04:56 +00:00
|
|
|
EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ip->i_number)));
|
2011-04-28 14:27:17 +00:00
|
|
|
if (waitfor && !DOINGASYNC(vp))
|
1995-11-05 23:25:12 +00:00
|
|
|
return (bwrite(bp));
|
|
|
|
else {
|
|
|
|
bdwrite(bp);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SINGLE 0 /* index of single indirect block */
|
|
|
|
#define DOUBLE 1 /* index of double indirect block */
|
|
|
|
#define TRIPLE 2 /* index of triple indirect block */
|
|
|
|
/*
|
|
|
|
* Truncate the inode oip to at most length size, freeing the
|
|
|
|
* disk blocks.
|
|
|
|
*/
|
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
ext2_truncate(vp, length, flags, cred, td)
|
VFS mega cleanup commit (x/N)
1. Add new file "sys/kern/vfs_default.c" where default actions for
VOPs go. Implement proper defaults for ABORTOP, BWRITE, LEASE,
POLL, REVOKE and STRATEGY. Various stuff spread over the entire
tree belongs here.
2. Change VOP_BLKATOFF to a normal function in cd9660.
3. Kill VOP_BLKATOFF, VOP_TRUNCATE, VOP_VFREE, VOP_VALLOC. These
are private interface functions between UFS and the underlying
storage manager layer (FFS/LFS/MFS/EXT2FS). The functions now
live in struct ufsmount instead.
4. Remove a kludge of VOP_ functions in all filesystems, that did
nothing but obscure the simplicity and break the expandability.
If a filesystem doesn't implement VOP_FOO, it shouldn't have an
entry for it in its vnops table. The system will try to DTRT
if it is not implemented. There are still some cruft left, but
the bulk of it is done.
5. Fix another VCALL in vfs_cache.c (thanks Bruce!)
1997-10-16 10:50:27 +00:00
|
|
|
struct vnode *vp;
|
|
|
|
off_t length;
|
|
|
|
int flags;
|
|
|
|
struct ucred *cred;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td;
|
1995-11-05 23:25:12 +00:00
|
|
|
{
|
2002-05-16 19:43:28 +00:00
|
|
|
struct vnode *ovp = vp;
|
2002-05-18 19:12:38 +00:00
|
|
|
int32_t lastblock;
|
2002-05-16 19:43:28 +00:00
|
|
|
struct inode *oip;
|
2002-05-18 19:12:38 +00:00
|
|
|
int32_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
|
|
|
|
int32_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
|
2010-01-14 14:30:54 +00:00
|
|
|
struct bufobj *bo;
|
|
|
|
struct m_ext2fs *fs;
|
1995-11-05 23:25:12 +00:00
|
|
|
struct buf *bp;
|
|
|
|
int offset, size, level;
|
1998-10-29 09:30:52 +00:00
|
|
|
long count, nblocks, blocksreleased = 0;
|
2011-04-28 14:27:17 +00:00
|
|
|
int error, i, allerror;
|
1995-11-05 23:25:12 +00:00
|
|
|
off_t osize;
|
|
|
|
|
|
|
|
oip = VTOI(ovp);
|
2010-01-14 14:30:54 +00:00
|
|
|
bo = &ovp->v_bufobj;
|
|
|
|
|
|
|
|
ASSERT_VOP_LOCKED(vp, "ext2_truncate");
|
|
|
|
|
|
|
|
if (length < 0)
|
|
|
|
return (EINVAL);
|
|
|
|
|
1995-11-05 23:25:12 +00:00
|
|
|
if (ovp->v_type == VLNK &&
|
|
|
|
oip->i_size < ovp->v_mount->mnt_maxsymlinklen) {
|
2005-12-05 11:58:35 +00:00
|
|
|
#ifdef DIAGNOSTIC
|
1995-11-05 23:25:12 +00:00
|
|
|
if (length != 0)
|
|
|
|
panic("ext2_truncate: partial truncate of symlink");
|
|
|
|
#endif
|
|
|
|
bzero((char *)&oip->i_shortlink, (u_int)oip->i_size);
|
|
|
|
oip->i_size = 0;
|
|
|
|
oip->i_flag |= IN_CHANGE | IN_UPDATE;
|
2002-05-16 19:08:03 +00:00
|
|
|
return (ext2_update(ovp, 1));
|
1995-11-05 23:25:12 +00:00
|
|
|
}
|
|
|
|
if (oip->i_size == length) {
|
|
|
|
oip->i_flag |= IN_CHANGE | IN_UPDATE;
|
2002-05-16 19:08:03 +00:00
|
|
|
return (ext2_update(ovp, 0));
|
1995-11-05 23:25:12 +00:00
|
|
|
}
|
|
|
|
fs = oip->i_e2fs;
|
|
|
|
osize = oip->i_size;
|
|
|
|
/*
|
|
|
|
* Lengthen the size of the file. We must ensure that the
|
|
|
|
* last byte of the file is allocated. Since the smallest
|
2010-01-14 14:30:54 +00:00
|
|
|
* value of osize is 0, length will be at least 1.
|
1995-11-05 23:25:12 +00:00
|
|
|
*/
|
|
|
|
if (osize < length) {
|
2010-01-14 14:30:54 +00:00
|
|
|
if (length > oip->i_e2fs->e2fs_maxfilesize)
|
2004-02-18 14:08:25 +00:00
|
|
|
return (EFBIG);
|
2010-01-14 14:30:54 +00:00
|
|
|
vnode_pager_setsize(ovp, length);
|
1995-11-05 23:25:12 +00:00
|
|
|
offset = blkoff(fs, length - 1);
|
|
|
|
lbn = lblkno(fs, length - 1);
|
2011-04-28 14:27:17 +00:00
|
|
|
flags |= BA_CLRBUF;
|
|
|
|
error = ext2_balloc(oip, lbn, offset + 1, cred, &bp, flags);
|
2010-01-14 14:30:54 +00:00
|
|
|
if (error) {
|
|
|
|
vnode_pager_setsize(vp, osize);
|
1995-11-05 23:25:12 +00:00
|
|
|
return (error);
|
2010-01-14 14:30:54 +00:00
|
|
|
}
|
1995-11-05 23:25:12 +00:00
|
|
|
oip->i_size = length;
|
2010-01-14 14:30:54 +00:00
|
|
|
if (bp->b_bufsize == fs->e2fs_bsize)
|
|
|
|
bp->b_flags |= B_CLUSTEROK;
|
2011-04-28 14:27:17 +00:00
|
|
|
if (flags & IO_SYNC)
|
1995-11-05 23:25:12 +00:00
|
|
|
bwrite(bp);
|
2011-04-28 14:27:17 +00:00
|
|
|
else if (DOINGASYNC(ovp))
|
2010-01-14 14:30:54 +00:00
|
|
|
bdwrite(bp);
|
1995-11-05 23:25:12 +00:00
|
|
|
else
|
|
|
|
bawrite(bp);
|
|
|
|
oip->i_flag |= IN_CHANGE | IN_UPDATE;
|
2002-05-16 19:08:03 +00:00
|
|
|
return (ext2_update(ovp, 1));
|
1995-11-05 23:25:12 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Shorten the size of the file. If the file is not being
|
|
|
|
* truncated to a block boundry, the contents of the
|
|
|
|
* partial block following the end of the file must be
|
2000-06-14 17:53:40 +00:00
|
|
|
* zero'ed in case it ever become accessible again because
|
1995-11-05 23:25:12 +00:00
|
|
|
* of subsequent file growth.
|
|
|
|
*/
|
|
|
|
/* I don't understand the comment above */
|
|
|
|
offset = blkoff(fs, length);
|
|
|
|
if (offset == 0) {
|
|
|
|
oip->i_size = length;
|
|
|
|
} else {
|
|
|
|
lbn = lblkno(fs, length);
|
2011-04-28 14:27:17 +00:00
|
|
|
flags |= BA_CLRBUF;
|
|
|
|
error = ext2_balloc(oip, lbn, offset, cred, &bp, flags);
|
2010-01-14 14:30:54 +00:00
|
|
|
if (error)
|
1995-11-05 23:25:12 +00:00
|
|
|
return (error);
|
|
|
|
oip->i_size = length;
|
|
|
|
size = blksize(fs, oip, lbn);
|
|
|
|
bzero((char *)bp->b_data + offset, (u_int)(size - offset));
|
|
|
|
allocbuf(bp, size);
|
2010-01-14 14:30:54 +00:00
|
|
|
if (bp->b_bufsize == fs->e2fs_bsize)
|
|
|
|
bp->b_flags |= B_CLUSTEROK;
|
2011-04-28 14:27:17 +00:00
|
|
|
if (flags & IO_SYNC)
|
1995-11-05 23:25:12 +00:00
|
|
|
bwrite(bp);
|
2011-04-28 14:27:17 +00:00
|
|
|
else if (DOINGASYNC(ovp))
|
2010-01-14 14:30:54 +00:00
|
|
|
bdwrite(bp);
|
1995-11-05 23:25:12 +00:00
|
|
|
else
|
|
|
|
bawrite(bp);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Calculate index into inode's block list of
|
|
|
|
* last direct and indirect blocks (if any)
|
|
|
|
* which we want to keep. Lastblock is -1 when
|
|
|
|
* the file is truncated to 0.
|
|
|
|
*/
|
2010-01-14 14:30:54 +00:00
|
|
|
lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
|
1995-11-05 23:25:12 +00:00
|
|
|
lastiblock[SINGLE] = lastblock - NDADDR;
|
|
|
|
lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
|
|
|
|
lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
|
2010-01-14 14:30:54 +00:00
|
|
|
nblocks = btodb(fs->e2fs_bsize);
|
1995-11-05 23:25:12 +00:00
|
|
|
/*
|
|
|
|
* Update file and block pointers on disk before we start freeing
|
|
|
|
* blocks. If we crash before free'ing blocks below, the blocks
|
|
|
|
* will be returned to the free list. lastiblock values are also
|
|
|
|
* normalized to -1 for calls to ext2_indirtrunc below.
|
|
|
|
*/
|
2011-12-16 15:47:43 +00:00
|
|
|
bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof(oldblks));
|
1995-11-05 23:25:12 +00:00
|
|
|
for (level = TRIPLE; level >= SINGLE; level--)
|
|
|
|
if (lastiblock[level] < 0) {
|
|
|
|
oip->i_ib[level] = 0;
|
|
|
|
lastiblock[level] = -1;
|
|
|
|
}
|
|
|
|
for (i = NDADDR - 1; i > lastblock; i--)
|
|
|
|
oip->i_db[i] = 0;
|
|
|
|
oip->i_flag |= IN_CHANGE | IN_UPDATE;
|
2002-05-16 19:08:03 +00:00
|
|
|
allerror = ext2_update(ovp, 1);
|
1998-10-29 09:44:12 +00:00
|
|
|
|
1995-11-05 23:25:12 +00:00
|
|
|
/*
|
|
|
|
* Having written the new inode to disk, save its new configuration
|
|
|
|
* and put back the old block pointers long enough to process them.
|
|
|
|
* Note that we save the new block configuration so we can check it
|
|
|
|
* when we are done.
|
|
|
|
*/
|
2011-12-16 15:47:43 +00:00
|
|
|
bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof(newblks));
|
|
|
|
bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof(oldblks));
|
1995-11-05 23:25:12 +00:00
|
|
|
oip->i_size = osize;
|
2012-04-23 13:21:28 +00:00
|
|
|
error = vtruncbuf(ovp, cred, length, (int)fs->e2fs_bsize);
|
1998-10-29 09:30:52 +00:00
|
|
|
if (error && (allerror == 0))
|
|
|
|
allerror = error;
|
2010-01-14 14:30:54 +00:00
|
|
|
vnode_pager_setsize(ovp, length);
|
|
|
|
|
1995-11-05 23:25:12 +00:00
|
|
|
/*
|
|
|
|
* Indirect blocks first.
|
|
|
|
*/
|
|
|
|
indir_lbn[SINGLE] = -NDADDR;
|
|
|
|
indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
|
|
|
|
indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
|
|
|
|
for (level = TRIPLE; level >= SINGLE; level--) {
|
|
|
|
bn = oip->i_ib[level];
|
|
|
|
if (bn != 0) {
|
|
|
|
error = ext2_indirtrunc(oip, indir_lbn[level],
|
|
|
|
fsbtodb(fs, bn), lastiblock[level], level, &count);
|
|
|
|
if (error)
|
|
|
|
allerror = error;
|
|
|
|
blocksreleased += count;
|
|
|
|
if (lastiblock[level] < 0) {
|
|
|
|
oip->i_ib[level] = 0;
|
2010-01-14 14:30:54 +00:00
|
|
|
ext2_blkfree(oip, bn, fs->e2fs_fsize);
|
1995-11-05 23:25:12 +00:00
|
|
|
blocksreleased += nblocks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lastiblock[level] >= 0)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All whole direct blocks or frags.
|
|
|
|
*/
|
|
|
|
for (i = NDADDR - 1; i > lastblock; i--) {
|
2002-05-16 19:43:28 +00:00
|
|
|
long bsize;
|
1995-11-05 23:25:12 +00:00
|
|
|
|
|
|
|
bn = oip->i_db[i];
|
|
|
|
if (bn == 0)
|
|
|
|
continue;
|
|
|
|
oip->i_db[i] = 0;
|
|
|
|
bsize = blksize(fs, oip, i);
|
|
|
|
ext2_blkfree(oip, bn, bsize);
|
|
|
|
blocksreleased += btodb(bsize);
|
|
|
|
}
|
|
|
|
if (lastblock < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finally, look for a change in size of the
|
|
|
|
* last direct block; release any frags.
|
|
|
|
*/
|
|
|
|
bn = oip->i_db[lastblock];
|
|
|
|
if (bn != 0) {
|
|
|
|
long oldspace, newspace;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate amount of space we're giving
|
|
|
|
* back as old block size minus new block size.
|
|
|
|
*/
|
|
|
|
oldspace = blksize(fs, oip, lastblock);
|
|
|
|
oip->i_size = length;
|
|
|
|
newspace = blksize(fs, oip, lastblock);
|
|
|
|
if (newspace == 0)
|
|
|
|
panic("itrunc: newspace");
|
|
|
|
if (oldspace - newspace > 0) {
|
|
|
|
/*
|
|
|
|
* Block number of space to be free'd is
|
|
|
|
* the old block # plus the number of frags
|
|
|
|
* required for the storage we're keeping.
|
|
|
|
*/
|
|
|
|
bn += numfrags(fs, newspace);
|
|
|
|
ext2_blkfree(oip, bn, oldspace - newspace);
|
|
|
|
blocksreleased += btodb(oldspace - newspace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
2005-12-05 11:58:35 +00:00
|
|
|
#ifdef DIAGNOSTIC
|
1995-11-05 23:25:12 +00:00
|
|
|
for (level = SINGLE; level <= TRIPLE; level++)
|
|
|
|
if (newblks[NDADDR + level] != oip->i_ib[level])
|
|
|
|
panic("itrunc1");
|
|
|
|
for (i = 0; i < NDADDR; i++)
|
|
|
|
if (newblks[i] != oip->i_db[i])
|
|
|
|
panic("itrunc2");
|
2010-01-14 14:30:54 +00:00
|
|
|
BO_LOCK(bo);
|
|
|
|
if (length == 0 && (bo->bo_dirty.bv_cnt != 0 ||
|
|
|
|
bo->bo_clean.bv_cnt != 0))
|
1995-11-05 23:25:12 +00:00
|
|
|
panic("itrunc3");
|
2010-01-14 14:30:54 +00:00
|
|
|
BO_UNLOCK(bo);
|
1995-11-05 23:25:12 +00:00
|
|
|
#endif /* DIAGNOSTIC */
|
|
|
|
/*
|
|
|
|
* Put back the real size.
|
|
|
|
*/
|
|
|
|
oip->i_size = length;
|
|
|
|
oip->i_blocks -= blocksreleased;
|
|
|
|
if (oip->i_blocks < 0) /* sanity */
|
|
|
|
oip->i_blocks = 0;
|
|
|
|
oip->i_flag |= IN_CHANGE;
|
1996-01-19 04:00:31 +00:00
|
|
|
vnode_pager_setsize(ovp, length);
|
1995-11-05 23:25:12 +00:00
|
|
|
return (allerror);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release blocks associated with the inode ip and stored in the indirect
|
|
|
|
* block bn. Blocks are free'd in LIFO order up to (but not including)
|
|
|
|
* lastbn. If level is greater than SINGLE, the block is an indirect block
|
|
|
|
* and recursive calls to indirtrunc must be used to cleanse other indirect
|
|
|
|
* blocks.
|
|
|
|
*
|
|
|
|
* NB: triple indirect blocks are untested.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
ext2_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
|
2002-05-16 19:43:28 +00:00
|
|
|
struct inode *ip;
|
2002-05-18 19:12:38 +00:00
|
|
|
int32_t lbn, lastbn;
|
|
|
|
int32_t dbn;
|
1995-11-05 23:25:12 +00:00
|
|
|
int level;
|
|
|
|
long *countp;
|
|
|
|
{
|
|
|
|
struct buf *bp;
|
2010-01-14 14:30:54 +00:00
|
|
|
struct m_ext2fs *fs = ip->i_e2fs;
|
1995-11-05 23:25:12 +00:00
|
|
|
struct vnode *vp;
|
2002-05-18 19:12:38 +00:00
|
|
|
int32_t *bap, *copy, nb, nlbn, last;
|
1995-11-05 23:25:12 +00:00
|
|
|
long blkcount, factor;
|
2002-05-16 19:43:28 +00:00
|
|
|
int i, nblocks, blocksreleased = 0;
|
1995-11-05 23:25:12 +00:00
|
|
|
int error = 0, allerror = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate index in current block of last
|
|
|
|
* block to be kept. -1 indicates the entire
|
|
|
|
* block so we need not calculate the index.
|
|
|
|
*/
|
|
|
|
factor = 1;
|
|
|
|
for (i = SINGLE; i < level; i++)
|
|
|
|
factor *= NINDIR(fs);
|
|
|
|
last = lastbn;
|
|
|
|
if (lastbn > 0)
|
|
|
|
last /= factor;
|
2010-01-14 14:30:54 +00:00
|
|
|
nblocks = btodb(fs->e2fs_bsize);
|
1995-11-05 23:25:12 +00:00
|
|
|
/*
|
|
|
|
* Get buffer of block pointers, zero those entries corresponding
|
|
|
|
* to blocks to be free'd, and update on disk copy first. Since
|
|
|
|
* double(triple) indirect before single(double) indirect, calls
|
|
|
|
* to bmap on these blocks will fail. However, we already have
|
|
|
|
* the on disk address, so we have to set the b_blkno field
|
|
|
|
* explicitly instead of letting bread do everything for us.
|
|
|
|
*/
|
|
|
|
vp = ITOV(ip);
|
2010-01-14 14:30:54 +00:00
|
|
|
bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0, 0);
|
1995-11-05 23:25:12 +00:00
|
|
|
if (bp->b_flags & (B_DONE | B_DELWRI)) {
|
|
|
|
} else {
|
2000-03-20 10:44:49 +00:00
|
|
|
bp->b_iocmd = BIO_READ;
|
1995-11-05 23:25:12 +00:00
|
|
|
if (bp->b_bcount > bp->b_bufsize)
|
|
|
|
panic("ext2_indirtrunc: bad buffer size");
|
|
|
|
bp->b_blkno = dbn;
|
|
|
|
vfs_busy_pages(bp, 0);
|
2003-10-18 14:10:28 +00:00
|
|
|
bp->b_iooffset = dbtob(bp->b_blkno);
|
2004-10-24 20:03:41 +00:00
|
|
|
bstrategy(bp);
|
2000-04-29 16:25:22 +00:00
|
|
|
error = bufwait(bp);
|
1995-11-05 23:25:12 +00:00
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
brelse(bp);
|
|
|
|
*countp = 0;
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2002-05-18 19:12:38 +00:00
|
|
|
bap = (int32_t *)bp->b_data;
|
2010-01-14 14:30:54 +00:00
|
|
|
copy = malloc(fs->e2fs_bsize, M_TEMP, M_WAITOK);
|
|
|
|
bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->e2fs_bsize);
|
1995-11-05 23:25:12 +00:00
|
|
|
bzero((caddr_t)&bap[last + 1],
|
2011-12-16 15:47:43 +00:00
|
|
|
(u_int)(NINDIR(fs) - (last + 1)) * sizeof(int32_t));
|
1995-11-05 23:25:12 +00:00
|
|
|
if (last == -1)
|
|
|
|
bp->b_flags |= B_INVAL;
|
|
|
|
error = bwrite(bp);
|
|
|
|
if (error)
|
|
|
|
allerror = error;
|
|
|
|
bap = copy;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recursively free totally unused blocks.
|
|
|
|
*/
|
|
|
|
for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
|
|
|
|
i--, nlbn += factor) {
|
|
|
|
nb = bap[i];
|
|
|
|
if (nb == 0)
|
|
|
|
continue;
|
|
|
|
if (level > SINGLE) {
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = ext2_indirtrunc(ip, nlbn,
|
2002-05-18 19:12:38 +00:00
|
|
|
fsbtodb(fs, nb), (int32_t)-1, level - 1, &blkcount)) != 0)
|
1995-11-05 23:25:12 +00:00
|
|
|
allerror = error;
|
|
|
|
blocksreleased += blkcount;
|
|
|
|
}
|
2010-01-14 14:30:54 +00:00
|
|
|
ext2_blkfree(ip, nb, fs->e2fs_bsize);
|
1995-11-05 23:25:12 +00:00
|
|
|
blocksreleased += nblocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recursively free last partial block.
|
|
|
|
*/
|
|
|
|
if (level > SINGLE && lastbn >= 0) {
|
|
|
|
last = lastbn % factor;
|
|
|
|
nb = bap[i];
|
|
|
|
if (nb != 0) {
|
1999-01-27 21:50:00 +00:00
|
|
|
if ((error = ext2_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
|
|
|
|
last, level - 1, &blkcount)) != 0)
|
1995-11-05 23:25:12 +00:00
|
|
|
allerror = error;
|
|
|
|
blocksreleased += blkcount;
|
|
|
|
}
|
|
|
|
}
|
2008-10-23 15:53:51 +00:00
|
|
|
free(copy, M_TEMP);
|
1995-11-05 23:25:12 +00:00
|
|
|
*countp = blocksreleased;
|
|
|
|
return (allerror);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* discard preallocated blocks
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ext2_inactive(ap)
|
|
|
|
struct vop_inactive_args /* {
|
|
|
|
struct vnode *a_vp;
|
2002-05-16 19:08:03 +00:00
|
|
|
struct thread *a_td;
|
1995-11-05 23:25:12 +00:00
|
|
|
} */ *ap;
|
|
|
|
{
|
2002-05-16 19:08:03 +00:00
|
|
|
struct vnode *vp = ap->a_vp;
|
|
|
|
struct inode *ip = VTOI(vp);
|
|
|
|
struct thread *td = ap->a_td;
|
|
|
|
int mode, error = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ignore inodes related to stale file handles.
|
|
|
|
*/
|
|
|
|
if (ip->i_mode == 0)
|
|
|
|
goto out;
|
|
|
|
if (ip->i_nlink <= 0) {
|
|
|
|
error = ext2_truncate(vp, (off_t)0, 0, NOCRED, td);
|
|
|
|
ip->i_rdev = 0;
|
|
|
|
mode = ip->i_mode;
|
|
|
|
ip->i_mode = 0;
|
|
|
|
ip->i_flag |= IN_CHANGE | IN_UPDATE;
|
|
|
|
ext2_vfree(vp, ip->i_number, mode);
|
|
|
|
}
|
2008-09-16 11:09:26 +00:00
|
|
|
if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE))
|
|
|
|
ext2_update(vp, 0);
|
2002-05-16 19:08:03 +00:00
|
|
|
out:
|
|
|
|
/*
|
|
|
|
* If we are done with the inode, reclaim it
|
|
|
|
* so that it can be reused immediately.
|
|
|
|
*/
|
|
|
|
if (ip->i_mode == 0)
|
2005-01-28 13:08:21 +00:00
|
|
|
vrecycle(vp, td);
|
2002-05-16 19:08:03 +00:00
|
|
|
return (error);
|
1995-11-05 23:25:12 +00:00
|
|
|
}
|
|
|
|
|
2002-05-16 19:08:03 +00:00
|
|
|
/*
|
|
|
|
* Reclaim an inode so that it can be used for other purposes.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ext2_reclaim(ap)
|
|
|
|
struct vop_reclaim_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
struct thread *a_td;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
struct inode *ip;
|
|
|
|
struct vnode *vp = ap->a_vp;
|
|
|
|
|
|
|
|
ip = VTOI(vp);
|
|
|
|
if (ip->i_flag & IN_LAZYMOD) {
|
|
|
|
ip->i_flag |= IN_MODIFIED;
|
|
|
|
ext2_update(vp, 0);
|
|
|
|
}
|
2005-03-14 13:30:06 +00:00
|
|
|
vfs_hash_remove(vp);
|
2008-10-23 15:53:51 +00:00
|
|
|
free(vp->v_data, M_EXT2NODE);
|
2002-05-16 19:08:03 +00:00
|
|
|
vp->v_data = 0;
|
2005-01-28 14:42:17 +00:00
|
|
|
vnode_destroy_vobject(vp);
|
2002-05-16 19:08:03 +00:00
|
|
|
return (0);
|
|
|
|
}
|