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