Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_inode.c revision 1.29
      1 /*	$NetBSD: ext2fs_inode.c,v 1.29 2003/01/24 21:55:20 fvdl 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 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: ext2fs_inode.c,v 1.29 2003/01/24 21:55:20 fvdl Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/mount.h>
     46 #include <sys/proc.h>
     47 #include <sys/file.h>
     48 #include <sys/buf.h>
     49 #include <sys/vnode.h>
     50 #include <sys/kernel.h>
     51 #include <sys/malloc.h>
     52 #include <sys/trace.h>
     53 #include <sys/resourcevar.h>
     54 
     55 #include <ufs/ufs/inode.h>
     56 #include <ufs/ufs/ufsmount.h>
     57 #include <ufs/ufs/ufs_extern.h>
     58 
     59 #include <ufs/ext2fs/ext2fs.h>
     60 #include <ufs/ext2fs/ext2fs_extern.h>
     61 
     62 extern int prtactive;
     63 
     64 static int ext2fs_indirtrunc __P((struct inode *, daddr_t, daddr_t,
     65 				  daddr_t, int, long *));
     66 
     67 /*
     68  * Last reference to an inode.  If necessary, write or delete it.
     69  */
     70 int
     71 ext2fs_inactive(v)
     72 	void *v;
     73 {
     74 	struct vop_inactive_args /* {
     75 		struct vnode *a_vp;
     76 		struct proc *a_p;
     77 	} */ *ap = v;
     78 	struct vnode *vp = ap->a_vp;
     79 	struct inode *ip = VTOI(vp);
     80 	struct proc *p = ap->a_p;
     81 	struct timespec ts;
     82 	int error = 0;
     83 
     84 	if (prtactive && vp->v_usecount != 0)
     85 		vprint("ext2fs_inactive: pushing active", vp);
     86 	/* Get rid of inodes related to stale file handles. */
     87 	if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0)
     88 		goto out;
     89 
     90 	error = 0;
     91 	if (ip->i_e2fs_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
     92 		if (ip->i_e2fs_size != 0) {
     93 			error = VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, NULL);
     94 		}
     95 		TIMEVAL_TO_TIMESPEC(&time, &ts);
     96 		ip->i_e2fs_dtime = ts.tv_sec;
     97 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
     98 		VOP_VFREE(vp, ip->i_number, ip->i_e2fs_mode);
     99 	}
    100 	if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED))
    101 		VOP_UPDATE(vp, NULL, NULL, 0);
    102 out:
    103 	VOP_UNLOCK(vp, 0);
    104 	/*
    105 	 * If we are done with the inode, reclaim it
    106 	 * so that it can be reused immediately.
    107 	 */
    108 	if (ip->i_e2fs_dtime != 0)
    109 		vrecycle(vp, NULL, p);
    110 	return (error);
    111 }
    112 
    113 
    114 /*
    115  * Update the access, modified, and inode change times as specified by the
    116  * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
    117  * used to specify that the inode needs to be updated but that the times have
    118  * already been set. The access and modified times are taken from the second
    119  * and third parameters; the inode change time is always taken from the current
    120  * time. If UPDATE_WAIT or UPDATE_DIROP is set, then wait for the disk
    121  * write of the inode to complete.
    122  */
    123 int
    124 ext2fs_update(v)
    125 	void *v;
    126 {
    127 	struct vop_update_args /* {
    128 		struct vnode *a_vp;
    129 		struct timespec *a_access;
    130 		struct timespec *a_modify;
    131 		int a_flags;
    132 	} */ *ap = v;
    133 	struct m_ext2fs *fs;
    134 	struct buf *bp;
    135 	struct inode *ip;
    136 	int error;
    137 	struct timespec ts;
    138 	caddr_t cp;
    139 	int flags;
    140 
    141 	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
    142 		return (0);
    143 	ip = VTOI(ap->a_vp);
    144 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    145 	EXT2FS_ITIMES(ip,
    146 	    ap->a_access ? ap->a_access : &ts,
    147 	    ap->a_modify ? ap->a_modify : &ts, &ts);
    148 	flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
    149 	if (flags == 0)
    150 		return (0);
    151 	fs = ip->i_e2fs;
    152 
    153 	error = bread(ip->i_devvp,
    154 			  fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    155 			  (int)fs->e2fs_bsize, NOCRED, &bp);
    156 	if (error) {
    157 		brelse(bp);
    158 		return (error);
    159 	}
    160 	ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
    161 	cp = (caddr_t)bp->b_data +
    162 	    (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE);
    163 	e2fs_isave(&ip->i_din.e2fs_din, (struct ext2fs_dinode *)cp);
    164 	if ((ap->a_flags & (UPDATE_WAIT|UPDATE_DIROP)) != 0 &&
    165 	    (flags & IN_MODIFIED) != 0 &&
    166 	    (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
    167 		return (bwrite(bp));
    168 	else {
    169 		bdwrite(bp);
    170 		return (0);
    171 	}
    172 }
    173 
    174 #define	SINGLE	0	/* index of single indirect block */
    175 #define	DOUBLE	1	/* index of double indirect block */
    176 #define	TRIPLE	2	/* index of triple indirect block */
    177 /*
    178  * Truncate the inode oip to at most length size, freeing the
    179  * disk blocks.
    180  */
    181 int
    182 ext2fs_truncate(v)
    183 	void *v;
    184 {
    185 	struct vop_truncate_args /* {
    186 		struct vnode *a_vp;
    187 		off_t a_length;
    188 		int a_flags;
    189 		struct ucred *a_cred;
    190 		struct proc *a_p;
    191 	} */ *ap = v;
    192 	struct vnode *ovp = ap->a_vp;
    193 	daddr_t lastblock;
    194 	struct inode *oip;
    195 	daddr_t bn, lastiblock[NIADDR], indir_lbn[NIADDR];
    196 	daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
    197 	off_t length = ap->a_length;
    198 	struct m_ext2fs *fs;
    199 	int offset, size, level;
    200 	long count, nblocks, blocksreleased = 0;
    201 	int i;
    202 	int error, allerror = 0;
    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, UPDATE_WAIT));
    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 		ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
    240 		    ap->a_flags & IO_SYNC ? B_SYNC : 0);
    241 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    242 		return (VOP_UPDATE(ovp, NULL, NULL, 1));
    243 	}
    244 	/*
    245 	 * Shorten the size of the file. If the file is not being
    246 	 * truncated to a block boundry, the contents of the
    247 	 * partial block following the end of the file must be
    248 	 * zero'ed in case it ever become accessible again because
    249 	 * of subsequent file growth.
    250 	 */
    251 	offset = blkoff(fs, length);
    252 	if (offset != 0) {
    253 		size = fs->e2fs_bsize;
    254 
    255 		/* XXXUBC we should handle more than just VREG */
    256 		uvm_vnp_zerorange(ovp, length, size - offset);
    257 	}
    258 	oip->i_e2fs_size = length;
    259 	uvm_vnp_setsize(ovp, length);
    260 
    261 	/*
    262 	 * Calculate index into inode's block list of
    263 	 * last direct and indirect blocks (if any)
    264 	 * which we want to keep.  Lastblock is -1 when
    265 	 * the file is truncated to 0.
    266 	 */
    267 	lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
    268 	lastiblock[SINGLE] = lastblock - NDADDR;
    269 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    270 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    271 	nblocks = btodb(fs->e2fs_bsize);
    272 	/*
    273 	 * Update file and block pointers on disk before we start freeing
    274 	 * blocks.  If we crash before free'ing blocks below, the blocks
    275 	 * will be returned to the free list.  lastiblock values are also
    276 	 * normalized to -1 for calls to ext2fs_indirtrunc below.
    277 	 */
    278 	memcpy((caddr_t)oldblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof oldblks);
    279 	for (level = TRIPLE; level >= SINGLE; level--)
    280 		if (lastiblock[level] < 0) {
    281 			oip->i_e2fs_blocks[NDADDR + level] = 0;
    282 			lastiblock[level] = -1;
    283 		}
    284 	for (i = NDADDR - 1; i > lastblock; i--)
    285 		oip->i_e2fs_blocks[i] = 0;
    286 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
    287 	error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
    288 	if (error && !allerror)
    289 		allerror = error;
    290 
    291 	/*
    292 	 * Having written the new inode to disk, save its new configuration
    293 	 * and put back the old block pointers long enough to process them.
    294 	 * Note that we save the new block configuration so we can check it
    295 	 * when we are done.
    296 	 */
    297 
    298 	memcpy((caddr_t)newblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof newblks);
    299 	memcpy((caddr_t)&oip->i_e2fs_blocks[0], (caddr_t)oldblks, sizeof oldblks);
    300 	oip->i_e2fs_size = osize;
    301 	error = vtruncbuf(ovp, lastblock + 1, 0, 0);
    302 	if (error && !allerror)
    303 		allerror = error;
    304 
    305 	/*
    306 	 * Indirect blocks first.
    307 	 */
    308 	indir_lbn[SINGLE] = -NDADDR;
    309 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1;
    310 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    311 	for (level = TRIPLE; level >= SINGLE; level--) {
    312 		/* XXX ondisk32 */
    313 		bn = fs2h32(oip->i_e2fs_blocks[NDADDR + level]);
    314 		if (bn != 0) {
    315 			error = ext2fs_indirtrunc(oip, indir_lbn[level],
    316 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
    317 			if (error)
    318 				allerror = error;
    319 			blocksreleased += count;
    320 			if (lastiblock[level] < 0) {
    321 				oip->i_e2fs_blocks[NDADDR + level] = 0;
    322 				ext2fs_blkfree(oip, bn);
    323 				blocksreleased += nblocks;
    324 			}
    325 		}
    326 		if (lastiblock[level] >= 0)
    327 			goto done;
    328 	}
    329 
    330 	/*
    331 	 * All whole direct blocks or frags.
    332 	 */
    333 	for (i = NDADDR - 1; i > lastblock; i--) {
    334 		/* XXX ondisk32 */
    335 		bn = fs2h32(oip->i_e2fs_blocks[i]);
    336 		if (bn == 0)
    337 			continue;
    338 		oip->i_e2fs_blocks[i] = 0;
    339 		ext2fs_blkfree(oip, bn);
    340 		blocksreleased += btodb(fs->e2fs_bsize);
    341 	}
    342 
    343 done:
    344 #ifdef DIAGNOSTIC
    345 	for (level = SINGLE; level <= TRIPLE; level++)
    346 		if (newblks[NDADDR + level] !=
    347 		    oip->i_e2fs_blocks[NDADDR + level])
    348 			panic("ext2fs_truncate1");
    349 	for (i = 0; i < NDADDR; i++)
    350 		if (newblks[i] != oip->i_e2fs_blocks[i])
    351 			panic("ext2fs_truncate2");
    352 	if (length == 0 &&
    353 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) ||
    354 	     !LIST_EMPTY(&ovp->v_dirtyblkhd)))
    355 		panic("ext2fs_truncate3");
    356 #endif /* DIAGNOSTIC */
    357 	/*
    358 	 * Put back the real size.
    359 	 */
    360 	oip->i_e2fs_size = length;
    361 	oip->i_e2fs_nblock -= blocksreleased;
    362 	oip->i_flag |= IN_CHANGE;
    363 	return (allerror);
    364 }
    365 
    366 /*
    367  * Release blocks associated with the inode ip and stored in the indirect
    368  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    369  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    370  * and recursive calls to indirtrunc must be used to cleanse other indirect
    371  * blocks.
    372  *
    373  * NB: triple indirect blocks are untested.
    374  */
    375 static int
    376 ext2fs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
    377 	struct inode *ip;
    378 	daddr_t lbn, lastbn;
    379 	daddr_t dbn;
    380 	int level;
    381 	long *countp;
    382 {
    383 	int i;
    384 	struct buf *bp;
    385 	struct m_ext2fs *fs = ip->i_e2fs;
    386 	int32_t *bap;	/* XXX ondisk32 */
    387 	struct vnode *vp;
    388 	daddr_t nb, nlbn, last;
    389 	int32_t *copy = NULL;	/* XXX ondisk32 */
    390 	long blkcount, factor;
    391 	int nblocks, blocksreleased = 0;
    392 	int error = 0, allerror = 0;
    393 
    394 	/*
    395 	 * Calculate index in current block of last
    396 	 * block to be kept.  -1 indicates the entire
    397 	 * block so we need not calculate the index.
    398 	 */
    399 	factor = 1;
    400 	for (i = SINGLE; i < level; i++)
    401 		factor *= NINDIR(fs);
    402 	last = lastbn;
    403 	if (lastbn > 0)
    404 		last /= factor;
    405 	nblocks = btodb(fs->e2fs_bsize);
    406 	/*
    407 	 * Get buffer of block pointers, zero those entries corresponding
    408 	 * to blocks to be free'd, and update on disk copy first.  Since
    409 	 * double(triple) indirect before single(double) indirect, calls
    410 	 * to bmap on these blocks will fail.  However, we already have
    411 	 * the on disk address, so we have to set the b_blkno field
    412 	 * explicitly instead of letting bread do everything for us.
    413 	 */
    414 	vp = ITOV(ip);
    415 	bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0);
    416 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
    417 		/* Braces must be here in case trace evaluates to nothing. */
    418 		trace(TR_BREADHIT, pack(vp, fs->e2fs_bsize), lbn);
    419 	} else {
    420 		trace(TR_BREADMISS, pack(vp, fs->e2fs_bsize), lbn);
    421 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
    422 		bp->b_flags |= B_READ;
    423 		if (bp->b_bcount > bp->b_bufsize)
    424 			panic("ext2fs_indirtrunc: bad buffer size");
    425 		bp->b_blkno = dbn;
    426 		VOP_STRATEGY(bp);
    427 		error = biowait(bp);
    428 	}
    429 	if (error) {
    430 		brelse(bp);
    431 		*countp = 0;
    432 		return (error);
    433 	}
    434 
    435 	bap = (int32_t *)bp->b_data;	/* XXX ondisk32 */
    436 	if (lastbn >= 0) {
    437 		/* XXX ondisk32 */
    438 		MALLOC(copy, int32_t *, fs->e2fs_bsize, M_TEMP, M_WAITOK);
    439 		memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->e2fs_bsize);
    440 		memset((caddr_t)&bap[last + 1], 0,
    441 			(u_int)(NINDIR(fs) - (last + 1)) * sizeof (u_int32_t));
    442 		error = bwrite(bp);
    443 		if (error)
    444 			allerror = error;
    445 		bap = copy;
    446 	}
    447 
    448 	/*
    449 	 * Recursively free totally unused blocks.
    450 	 */
    451 	for (i = NINDIR(fs) - 1,
    452 		nlbn = lbn + 1 - i * factor; i > last;
    453 		i--, nlbn += factor) {
    454 		/* XXX ondisk32 */
    455 		nb = fs2h32(bap[i]);
    456 		if (nb == 0)
    457 			continue;
    458 		if (level > SINGLE) {
    459 			error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    460 						   (daddr_t)-1, level - 1,
    461 						   &blkcount);
    462 			if (error)
    463 				allerror = error;
    464 			blocksreleased += blkcount;
    465 		}
    466 		ext2fs_blkfree(ip, nb);
    467 		blocksreleased += nblocks;
    468 	}
    469 
    470 	/*
    471 	 * Recursively free last partial block.
    472 	 */
    473 	if (level > SINGLE && lastbn >= 0) {
    474 		last = lastbn % factor;
    475 		/* XXX ondisk32 */
    476 		nb = fs2h32(bap[i]);
    477 		if (nb != 0) {
    478 			error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    479 						   last, level - 1, &blkcount);
    480 			if (error)
    481 				allerror = error;
    482 			blocksreleased += blkcount;
    483 		}
    484 	}
    485 
    486 	if (copy != NULL) {
    487 		FREE(copy, M_TEMP);
    488 	} else {
    489 		bp->b_flags |= B_INVAL;
    490 		brelse(bp);
    491 	}
    492 
    493 	*countp = blocksreleased;
    494 	return (allerror);
    495 }
    496