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