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