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