Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_inode.c revision 1.12
      1 /*	$NetBSD: ext2fs_inode.c,v 1.12 1999/03/05 21:09:49 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Manuel Bouyer.
      5  * Copyright (c) 1982, 1986, 1989, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)ffs_inode.c	8.8 (Berkeley) 10/19/94
     37  * Modified for ext2fs by Manuel Bouyer.
     38  */
     39 
     40 #if defined(_KERNEL) && !defined(_LKM)
     41 #include "opt_uvm.h"
     42 #endif
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/mount.h>
     47 #include <sys/proc.h>
     48 #include <sys/file.h>
     49 #include <sys/buf.h>
     50 #include <sys/vnode.h>
     51 #include <sys/kernel.h>
     52 #include <sys/malloc.h>
     53 #include <sys/trace.h>
     54 #include <sys/resourcevar.h>
     55 
     56 #include <vm/vm.h>
     57 #if defined(UVM)
     58 #include <uvm/uvm_extern.h>
     59 #endif
     60 
     61 #include <ufs/ufs/quota.h>
     62 #include <ufs/ufs/inode.h>
     63 #include <ufs/ufs/ufsmount.h>
     64 #include <ufs/ufs/ufs_extern.h>
     65 
     66 #include <ufs/ext2fs/ext2fs.h>
     67 #include <ufs/ext2fs/ext2fs_extern.h>
     68 
     69 static int ext2fs_indirtrunc __P((struct inode *, ufs_daddr_t, ufs_daddr_t,
     70 				  ufs_daddr_t, int, long *));
     71 
     72 /*
     73  * Last reference to an inode.  If necessary, write or delete it.
     74  */
     75 int
     76 ext2fs_inactive(v)
     77 	void *v;
     78 {
     79 	struct vop_inactive_args /* {
     80 		struct vnode *a_vp;
     81 		struct proc *a_p;
     82 	} */ *ap = v;
     83 	struct vnode *vp = ap->a_vp;
     84 	struct inode *ip = VTOI(vp);
     85 	struct proc *p = ap->a_p;
     86 	struct timespec ts;
     87 	int error = 0;
     88 	extern int prtactive;
     89 
     90 	if (prtactive && vp->v_usecount != 0)
     91 		vprint("ext2fs_inactive: pushing active", vp);
     92 	/* Get rid of inodes related to stale file handles. */
     93 	if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0)
     94 		goto out;
     95 
     96 	error = 0;
     97 	if (ip->i_e2fs_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
     98 		error = VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, NULL);
     99 		TIMEVAL_TO_TIMESPEC(&time, &ts);
    100 		ip->i_e2fs_dtime = ts.tv_sec;
    101 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    102 		VOP_VFREE(vp, ip->i_number, ip->i_e2fs_mode);
    103 	}
    104 	if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE))
    105 		VOP_UPDATE(vp, NULL, NULL, 0);
    106 out:
    107 	VOP_UNLOCK(vp, 0);
    108 	/*
    109 	 * If we are done with the inode, reclaim it
    110 	 * so that it can be reused immediately.
    111 	 */
    112 	if (ip->i_e2fs_dtime != 0)
    113 		vrecycle(vp, (struct simplelock *)0, p);
    114 	return (error);
    115 }
    116 
    117 
    118 /*
    119  * Update the access, modified, and inode change times as specified by the
    120  * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
    121  * used to specify that the inode needs to be updated but that the times have
    122  * already been set. The access and modified times are taken from the second
    123  * and third parameters; the inode change time is always taken from the current
    124  * time. If waitfor is set, then wait for the disk write of the inode to
    125  * complete.
    126  */
    127 int
    128 ext2fs_update(v)
    129 	void *v;
    130 {
    131 	struct vop_update_args /* {
    132 		struct vnode *a_vp;
    133 		struct timespec *a_access;
    134 		struct timespec *a_modify;
    135 		int a_waitfor;
    136 	} */ *ap = v;
    137 	register struct m_ext2fs *fs;
    138 	struct buf *bp;
    139 	struct inode *ip;
    140 	int error;
    141 	struct timespec ts;
    142 	caddr_t cp;
    143 
    144 	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
    145 		return (0);
    146 	ip = VTOI(ap->a_vp);
    147 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    148 	EXT2FS_ITIMES(ip,
    149 	    ap->a_access ? ap->a_access : &ts,
    150 	    ap->a_modify ? ap->a_modify : &ts, &ts);
    151 	if ((ip->i_flag & IN_MODIFIED) == 0)
    152 		return (0);
    153 	ip->i_flag &= ~IN_MODIFIED;
    154 	fs = ip->i_e2fs;
    155 	error = bread(ip->i_devvp,
    156 			  fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    157 			  (int)fs->e2fs_bsize, NOCRED, &bp);
    158 	if (error) {
    159 		brelse(bp);
    160 		return (error);
    161 	}
    162 	cp = (caddr_t)bp->b_data +
    163 	    (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE);
    164 	e2fs_isave(&ip->i_din.e2fs_din, (struct ext2fs_dinode *)cp);
    165 	if (ap->a_waitfor && (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
    166 		return (bwrite(bp));
    167 	else {
    168 		bdwrite(bp);
    169 		return (0);
    170 	}
    171 }
    172 
    173 #define	SINGLE	0	/* index of single indirect block */
    174 #define	DOUBLE	1	/* index of double indirect block */
    175 #define	TRIPLE	2	/* index of triple indirect block */
    176 /*
    177  * Truncate the inode oip to at most length size, freeing the
    178  * disk blocks.
    179  */
    180 int
    181 ext2fs_truncate(v)
    182 	void *v;
    183 {
    184 	struct vop_truncate_args /* {
    185 		struct vnode *a_vp;
    186 		off_t a_length;
    187 		int a_flags;
    188 		struct ucred *a_cred;
    189 		struct proc *a_p;
    190 	} */ *ap = v;
    191 	register struct vnode *ovp = ap->a_vp;
    192 	register ufs_daddr_t lastblock;
    193 	register struct inode *oip;
    194 	ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
    195 	ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
    196 	off_t length = ap->a_length;
    197 	register struct m_ext2fs *fs;
    198 	struct buf *bp;
    199 	int offset, size, level;
    200 	long count, nblocks, vflags, blocksreleased = 0;
    201 	register int i;
    202 	int aflags, error, allerror;
    203 	off_t osize;
    204 
    205 	if (length < 0)
    206 		return (EINVAL);
    207 
    208 	oip = VTOI(ovp);
    209 	if (ovp->v_type == VLNK &&
    210 		(oip->i_e2fs_size < ovp->v_mount->mnt_maxsymlinklen ||
    211 		 (ovp->v_mount->mnt_maxsymlinklen == 0 &&
    212 		  oip->i_e2fs_nblock == 0))) {
    213 #ifdef DIAGNOSTIC
    214 		if (length != 0)
    215 			panic("ext2fs_truncate: partial truncate of symlink");
    216 #endif
    217 		memset((char *)&oip->i_din.e2fs_din.e2di_shortlink, 0,
    218 			(u_int)oip->i_e2fs_size);
    219 		oip->i_e2fs_size = 0;
    220 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    221 		return (VOP_UPDATE(ovp, NULL, NULL, 1));
    222 	}
    223 	if (oip->i_e2fs_size == length) {
    224 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    225 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
    226 	}
    227 	fs = oip->i_e2fs;
    228 	osize = oip->i_e2fs_size;
    229 	/*
    230 	 * Lengthen the size of the file. We must ensure that the
    231 	 * last byte of the file is allocated. Since the smallest
    232 	 * value of osize is 0, length will be at least 1.
    233 	 */
    234 	if (osize < length) {
    235 #if 0 /* XXX */
    236 		if (length > fs->fs_maxfilesize)
    237 			return (EFBIG);
    238 #endif
    239 		offset = blkoff(fs, length - 1);
    240 		lbn = lblkno(fs, length - 1);
    241 		aflags = B_CLRBUF;
    242 		if (ap->a_flags & IO_SYNC)
    243 			aflags |= B_SYNC;
    244 		error = ext2fs_balloc(oip, lbn, offset + 1, ap->a_cred, &bp,
    245 				   aflags);
    246 		if (error)
    247 			return (error);
    248 		oip->i_e2fs_size = length;
    249 #if defined(UVM)
    250 		uvm_vnp_setsize(ovp, length);
    251 		(void) uvm_vnp_uncache(ovp);
    252 #else
    253 		vnode_pager_setsize(ovp, length);
    254 		(void) vnode_pager_uncache(ovp);
    255 #endif
    256 		if (aflags & B_SYNC)
    257 			bwrite(bp);
    258 		else
    259 			bawrite(bp);
    260 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    261 		return (VOP_UPDATE(ovp, NULL, NULL, 1));
    262 	}
    263 	/*
    264 	 * Shorten the size of the file. If the file is not being
    265 	 * truncated to a block boundry, the contents of the
    266 	 * partial block following the end of the file must be
    267 	 * zero'ed in case it ever become accessable again because
    268 	 * of subsequent file growth.
    269 	 */
    270 	offset = blkoff(fs, length);
    271 	if (offset == 0) {
    272 		oip->i_e2fs_size = length;
    273 	} else {
    274 		lbn = lblkno(fs, length);
    275 		aflags = B_CLRBUF;
    276 		if (ap->a_flags & IO_SYNC)
    277 			aflags |= B_SYNC;
    278 		error = ext2fs_balloc(oip, lbn, offset, ap->a_cred, &bp, aflags);
    279 		if (error)
    280 			return (error);
    281 		oip->i_e2fs_size = length;
    282 		size = fs->e2fs_bsize;
    283 #if defined(UVM)
    284 		(void) uvm_vnp_uncache(ovp);
    285 #else
    286 		(void) vnode_pager_uncache(ovp);
    287 #endif
    288 		memset((char *)bp->b_data + offset, 0,  (u_int)(size - offset));
    289 		allocbuf(bp, size);
    290 		if (aflags & B_SYNC)
    291 			bwrite(bp);
    292 		else
    293 			bawrite(bp);
    294 	}
    295 #if defined(UVM)
    296 	uvm_vnp_setsize(ovp, length);
    297 #else
    298 	vnode_pager_setsize(ovp, length);
    299 #endif
    300 	/*
    301 	 * Calculate index into inode's block list of
    302 	 * last direct and indirect blocks (if any)
    303 	 * which we want to keep.  Lastblock is -1 when
    304 	 * the file is truncated to 0.
    305 	 */
    306 	lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
    307 	lastiblock[SINGLE] = lastblock - NDADDR;
    308 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    309 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    310 	nblocks = btodb(fs->e2fs_bsize);
    311 	/*
    312 	 * Update file and block pointers on disk before we start freeing
    313 	 * blocks.  If we crash before free'ing blocks below, the blocks
    314 	 * will be returned to the free list.  lastiblock values are also
    315 	 * normalized to -1 for calls to ext2fs_indirtrunc below.
    316 	 */
    317 	memcpy((caddr_t)oldblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof oldblks);
    318 	for (level = TRIPLE; level >= SINGLE; level--)
    319 		if (lastiblock[level] < 0) {
    320 			oip->i_e2fs_blocks[NDADDR + level] = 0;
    321 			lastiblock[level] = -1;
    322 		}
    323 	for (i = NDADDR - 1; i > lastblock; i--)
    324 		oip->i_e2fs_blocks[i] = 0;
    325 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
    326 	if ((error = VOP_UPDATE(ovp, NULL, NULL, 1)) != 0)
    327 		allerror = error;
    328 	/*
    329 	 * Having written the new inode to disk, save its new configuration
    330 	 * and put back the old block pointers long enough to process them.
    331 	 * Note that we save the new block configuration so we can check it
    332 	 * when we are done.
    333 	 */
    334 	memcpy((caddr_t)newblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof newblks);
    335 	memcpy((caddr_t)&oip->i_e2fs_blocks[0], (caddr_t)oldblks, sizeof oldblks);
    336 	oip->i_e2fs_size = osize;
    337 	vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
    338 	allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0);
    339 
    340 	/*
    341 	 * Indirect blocks first.
    342 	 */
    343 	indir_lbn[SINGLE] = -NDADDR;
    344 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1;
    345 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    346 	for (level = TRIPLE; level >= SINGLE; level--) {
    347 		bn = fs2h32(oip->i_e2fs_blocks[NDADDR + level]);
    348 		if (bn != 0) {
    349 			error = ext2fs_indirtrunc(oip, indir_lbn[level],
    350 				fsbtodb(fs, bn), lastiblock[level], level, &count);
    351 			if (error)
    352 				allerror = error;
    353 			blocksreleased += count;
    354 			if (lastiblock[level] < 0) {
    355 				oip->i_e2fs_blocks[NDADDR + level] = 0;
    356 				ext2fs_blkfree(oip, bn);
    357 				blocksreleased += nblocks;
    358 			}
    359 		}
    360 		if (lastiblock[level] >= 0)
    361 			goto done;
    362 	}
    363 
    364 	/*
    365 	 * All whole direct blocks or frags.
    366 	 */
    367 	for (i = NDADDR - 1; i > lastblock; i--) {
    368 		bn = fs2h32(oip->i_e2fs_blocks[i]);
    369 		if (bn == 0)
    370 			continue;
    371 		oip->i_e2fs_blocks[i] = 0;
    372 		ext2fs_blkfree(oip, bn);
    373 		blocksreleased += btodb(fs->e2fs_bsize);
    374 	}
    375 	if (lastblock < 0)
    376 		goto done;
    377 
    378 done:
    379 #ifdef DIAGNOSTIC
    380 	for (level = SINGLE; level <= TRIPLE; level++)
    381 		if (newblks[NDADDR + level] !=
    382 			fs2h32(oip->i_e2fs_blocks[NDADDR + level]))
    383 			panic("itrunc1");
    384 	for (i = 0; i < NDADDR; i++)
    385 		if (newblks[i] != fs2h32(oip->i_e2fs_blocks[i]))
    386 			panic("itrunc2");
    387 	if (length == 0 &&
    388 		(ovp->v_dirtyblkhd.lh_first || ovp->v_cleanblkhd.lh_first))
    389 		panic("itrunc3");
    390 #endif /* DIAGNOSTIC */
    391 	/*
    392 	 * Put back the real size.
    393 	 */
    394 	oip->i_e2fs_size = length;
    395 	oip->i_e2fs_nblock -= blocksreleased;
    396 	if (oip->i_e2fs_nblock < 0)			/* sanity */
    397 		oip->i_e2fs_nblock = 0;
    398 	oip->i_flag |= IN_CHANGE;
    399 	return (allerror);
    400 }
    401 
    402 /*
    403  * Release blocks associated with the inode ip and stored in the indirect
    404  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    405  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    406  * and recursive calls to indirtrunc must be used to cleanse other indirect
    407  * blocks.
    408  *
    409  * NB: triple indirect blocks are untested.
    410  */
    411 static int
    412 ext2fs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
    413 	register struct inode *ip;
    414 	ufs_daddr_t lbn, lastbn;
    415 	ufs_daddr_t dbn;
    416 	int level;
    417 	long *countp;
    418 {
    419 	register int i;
    420 	struct buf *bp;
    421 	register struct m_ext2fs *fs = ip->i_e2fs;
    422 	register ufs_daddr_t *bap;
    423 	struct vnode *vp;
    424 	ufs_daddr_t *copy = NULL, nb, nlbn, last;
    425 	long blkcount, factor;
    426 	int nblocks, blocksreleased = 0;
    427 	int error = 0, allerror = 0;
    428 
    429 	/*
    430 	 * Calculate index in current block of last
    431 	 * block to be kept.  -1 indicates the entire
    432 	 * block so we need not calculate the index.
    433 	 */
    434 	factor = 1;
    435 	for (i = SINGLE; i < level; i++)
    436 		factor *= NINDIR(fs);
    437 	last = lastbn;
    438 	if (lastbn > 0)
    439 		last /= factor;
    440 	nblocks = btodb(fs->e2fs_bsize);
    441 	/*
    442 	 * Get buffer of block pointers, zero those entries corresponding
    443 	 * to blocks to be free'd, and update on disk copy first.  Since
    444 	 * double(triple) indirect before single(double) indirect, calls
    445 	 * to bmap on these blocks will fail.  However, we already have
    446 	 * the on disk address, so we have to set the b_blkno field
    447 	 * explicitly instead of letting bread do everything for us.
    448 	 */
    449 	vp = ITOV(ip);
    450 	bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0);
    451 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
    452 		/* Braces must be here in case trace evaluates to nothing. */
    453 		trace(TR_BREADHIT, pack(vp, fs->e2fs_bsize), lbn);
    454 	} else {
    455 		trace(TR_BREADMISS, pack(vp, fs->e2fs_bsize), lbn);
    456 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
    457 		bp->b_flags |= B_READ;
    458 		if (bp->b_bcount > bp->b_bufsize)
    459 			panic("ext2fs_indirtrunc: bad buffer size");
    460 		bp->b_blkno = dbn;
    461 		VOP_STRATEGY(bp);
    462 		error = biowait(bp);
    463 	}
    464 	if (error) {
    465 		brelse(bp);
    466 		*countp = 0;
    467 		return (error);
    468 	}
    469 
    470 	bap = (ufs_daddr_t *)bp->b_data;
    471 	if (lastbn != -1) {
    472 		MALLOC(copy, ufs_daddr_t *, fs->e2fs_bsize, M_TEMP, M_WAITOK);
    473 		memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->e2fs_bsize);
    474 		memset((caddr_t)&bap[last + 1], 0,
    475 			(u_int)(NINDIR(fs) - (last + 1)) * sizeof (u_int32_t));
    476 		error = bwrite(bp);
    477 		if (error)
    478 			allerror = error;
    479 		bap = copy;
    480 	}
    481 
    482 	/*
    483 	 * Recursively free totally unused blocks.
    484 	 */
    485 	for (i = NINDIR(fs) - 1,
    486 		nlbn = lbn + 1 - i * factor; i > last;
    487 		i--, nlbn += factor) {
    488 		nb = fs2h32(bap[i]);
    489 		if (nb == 0)
    490 			continue;
    491 		if (level > SINGLE) {
    492 			error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    493 						   (ufs_daddr_t)-1, level - 1,
    494 						   &blkcount);
    495 			if (error)
    496 				allerror = error;
    497 			blocksreleased += blkcount;
    498 		}
    499 		ext2fs_blkfree(ip, nb);
    500 		blocksreleased += nblocks;
    501 	}
    502 
    503 	/*
    504 	 * Recursively free last partial block.
    505 	 */
    506 	if (level > SINGLE && lastbn >= 0) {
    507 		last = lastbn % factor;
    508 		nb = fs2h32(bap[i]);
    509 		if (nb != 0) {
    510 			error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    511 						   last, level - 1, &blkcount);
    512 			if (error)
    513 				allerror = error;
    514 			blocksreleased += blkcount;
    515 		}
    516 	}
    517 
    518 	if (copy != NULL) {
    519 		FREE(copy, M_TEMP);
    520 	} else {
    521 		bp->b_flags |= B_INVAL;
    522 		brelse(bp);
    523 	}
    524 
    525 	*countp = blocksreleased;
    526 	return (allerror);
    527 }
    528