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