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