Home | History | Annotate | Line # | Download | only in ffs
ffs_inode.c revision 1.18
      1 /*	$NetBSD: ffs_inode.c,v 1.18 1998/03/01 02:23:14 fvdl 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 
     63 #include <ufs/ffs/fs.h>
     64 #include <ufs/ffs/ffs_extern.h>
     65 
     66 static int ffs_indirtrunc __P((struct inode *, ufs_daddr_t, ufs_daddr_t,
     67 			       ufs_daddr_t, int, long *));
     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 	FFS_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.ffs_din.di_ouid = ip->i_ffs_uid;	/* XXX */
    111 		ip->i_din.ffs_din.di_ogid = ip->i_ffs_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.ffs_din;
    122 	if (ap->a_waitfor && (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
    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 ufs_daddr_t lastblock;
    150 	register struct inode *oip;
    151 	ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
    152 	ufs_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_ffs_size < ovp->v_mount->mnt_maxsymlinklen ||
    169 	     (ovp->v_mount->mnt_maxsymlinklen == 0 &&
    170 	      oip->i_din.ffs_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_ffs_shortlink, (u_int)oip->i_ffs_size);
    176 		oip->i_ffs_size = 0;
    177 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    178 		return (VOP_UPDATE(ovp, &ts, &ts, 1));
    179 	}
    180 	if (oip->i_ffs_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 	fs = oip->i_fs;
    189 	osize = oip->i_ffs_size;
    190 	ovp->v_lasta = ovp->v_clen = ovp->v_cstart = ovp->v_lastw = 0;
    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_ffs_size = length;
    209 #if defined(UVM)
    210 		uvm_vnp_setsize(ovp, length);
    211 		(void) uvm_vnp_uncache(ovp);
    212 #else
    213 		vnode_pager_setsize(ovp, length);
    214 		(void) vnode_pager_uncache(ovp);
    215 #endif
    216 		if (aflags & B_SYNC)
    217 			bwrite(bp);
    218 		else
    219 			bawrite(bp);
    220 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    221 		return (VOP_UPDATE(ovp, &ts, &ts, 1));
    222 	}
    223 	/*
    224 	 * Shorten the size of the file. If the file is not being
    225 	 * truncated to a block boundry, the contents of the
    226 	 * partial block following the end of the file must be
    227 	 * zero'ed in case it ever become accessable again because
    228 	 * of subsequent file growth.
    229 	 */
    230 	offset = blkoff(fs, length);
    231 	if (offset == 0) {
    232 		oip->i_ffs_size = length;
    233 	} else {
    234 		lbn = lblkno(fs, length);
    235 		aflags = B_CLRBUF;
    236 		if (ap->a_flags & IO_SYNC)
    237 			aflags |= B_SYNC;
    238 		error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp, aflags);
    239 		if (error)
    240 			return (error);
    241 		oip->i_ffs_size = length;
    242 		size = blksize(fs, oip, lbn);
    243 #if defined(UVM)
    244 		(void) uvm_vnp_uncache(ovp);
    245 #else
    246 		(void) vnode_pager_uncache(ovp);
    247 #endif
    248 		bzero((char *)bp->b_data + offset, (u_int)(size - offset));
    249 		allocbuf(bp, size);
    250 		if (aflags & B_SYNC)
    251 			bwrite(bp);
    252 		else
    253 			bawrite(bp);
    254 	}
    255 #if defined(UVM)
    256 	uvm_vnp_setsize(ovp, length);
    257 #else
    258 	vnode_pager_setsize(ovp, length);
    259 #endif
    260 	/*
    261 	 * Calculate index into inode's block list of
    262 	 * last direct and indirect blocks (if any)
    263 	 * which we want to keep.  Lastblock is -1 when
    264 	 * the file is truncated to 0.
    265 	 */
    266 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
    267 	lastiblock[SINGLE] = lastblock - NDADDR;
    268 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    269 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    270 	nblocks = btodb(fs->fs_bsize);
    271 	/*
    272 	 * Update file and block pointers on disk before we start freeing
    273 	 * blocks.  If we crash before free'ing blocks below, the blocks
    274 	 * will be returned to the free list.  lastiblock values are also
    275 	 * normalized to -1 for calls to ffs_indirtrunc below.
    276 	 */
    277 	bcopy((caddr_t)&oip->i_ffs_db[0], (caddr_t)oldblks, sizeof oldblks);
    278 	for (level = TRIPLE; level >= SINGLE; level--)
    279 		if (lastiblock[level] < 0) {
    280 			oip->i_ffs_ib[level] = 0;
    281 			lastiblock[level] = -1;
    282 		}
    283 	for (i = NDADDR - 1; i > lastblock; i--)
    284 		oip->i_ffs_db[i] = 0;
    285 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
    286 	if ((error = VOP_UPDATE(ovp, &ts, &ts, 1)) != 0)
    287 		allerror = error;
    288 	/*
    289 	 * Having written the new inode to disk, save its new configuration
    290 	 * and put back the old block pointers long enough to process them.
    291 	 * Note that we save the new block configuration so we can check it
    292 	 * when we are done.
    293 	 */
    294 	bcopy((caddr_t)&oip->i_ffs_db[0], (caddr_t)newblks, sizeof newblks);
    295 	bcopy((caddr_t)oldblks, (caddr_t)&oip->i_ffs_db[0], sizeof oldblks);
    296 	oip->i_ffs_size = osize;
    297 	vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
    298 	allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0);
    299 
    300 	/*
    301 	 * Indirect blocks first.
    302 	 */
    303 	indir_lbn[SINGLE] = -NDADDR;
    304 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
    305 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    306 	for (level = TRIPLE; level >= SINGLE; level--) {
    307 		bn = oip->i_ffs_ib[level];
    308 		if (bn != 0) {
    309 			error = ffs_indirtrunc(oip, indir_lbn[level],
    310 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
    311 			if (error)
    312 				allerror = error;
    313 			blocksreleased += count;
    314 			if (lastiblock[level] < 0) {
    315 				oip->i_ffs_ib[level] = 0;
    316 				ffs_blkfree(oip, bn, fs->fs_bsize);
    317 				blocksreleased += nblocks;
    318 			}
    319 		}
    320 		if (lastiblock[level] >= 0)
    321 			goto done;
    322 	}
    323 
    324 	/*
    325 	 * All whole direct blocks or frags.
    326 	 */
    327 	for (i = NDADDR - 1; i > lastblock; i--) {
    328 		register long bsize;
    329 
    330 		bn = oip->i_ffs_db[i];
    331 		if (bn == 0)
    332 			continue;
    333 		oip->i_ffs_db[i] = 0;
    334 		bsize = blksize(fs, oip, i);
    335 		ffs_blkfree(oip, bn, bsize);
    336 		blocksreleased += btodb(bsize);
    337 	}
    338 	if (lastblock < 0)
    339 		goto done;
    340 
    341 	/*
    342 	 * Finally, look for a change in size of the
    343 	 * last direct block; release any frags.
    344 	 */
    345 	bn = oip->i_ffs_db[lastblock];
    346 	if (bn != 0) {
    347 		long oldspace, newspace;
    348 
    349 		/*
    350 		 * Calculate amount of space we're giving
    351 		 * back as old block size minus new block size.
    352 		 */
    353 		oldspace = blksize(fs, oip, lastblock);
    354 		oip->i_ffs_size = length;
    355 		newspace = blksize(fs, oip, lastblock);
    356 		if (newspace == 0)
    357 			panic("itrunc: newspace");
    358 		if (oldspace - newspace > 0) {
    359 			/*
    360 			 * Block number of space to be free'd is
    361 			 * the old block # plus the number of frags
    362 			 * required for the storage we're keeping.
    363 			 */
    364 			bn += numfrags(fs, newspace);
    365 			ffs_blkfree(oip, bn, oldspace - newspace);
    366 			blocksreleased += btodb(oldspace - newspace);
    367 		}
    368 	}
    369 done:
    370 #ifdef DIAGNOSTIC
    371 	for (level = SINGLE; level <= TRIPLE; level++)
    372 		if (newblks[NDADDR + level] != oip->i_ffs_ib[level])
    373 			panic("itrunc1");
    374 	for (i = 0; i < NDADDR; i++)
    375 		if (newblks[i] != oip->i_ffs_db[i])
    376 			panic("itrunc2");
    377 	if (length == 0 &&
    378 	    (ovp->v_dirtyblkhd.lh_first || ovp->v_cleanblkhd.lh_first))
    379 		panic("itrunc3");
    380 #endif /* DIAGNOSTIC */
    381 	/*
    382 	 * Put back the real size.
    383 	 */
    384 	oip->i_ffs_size = length;
    385 	oip->i_ffs_blocks -= blocksreleased;
    386 	if (oip->i_ffs_blocks < 0)			/* sanity */
    387 		oip->i_ffs_blocks = 0;
    388 	oip->i_flag |= IN_CHANGE;
    389 #ifdef QUOTA
    390 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
    391 #endif
    392 	return (allerror);
    393 }
    394 
    395 /*
    396  * Release blocks associated with the inode ip and stored in the indirect
    397  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    398  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    399  * and recursive calls to indirtrunc must be used to cleanse other indirect
    400  * blocks.
    401  *
    402  * NB: triple indirect blocks are untested.
    403  */
    404 static int
    405 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
    406 	register struct inode *ip;
    407 	ufs_daddr_t lbn, lastbn;
    408 	ufs_daddr_t dbn;
    409 	int level;
    410 	long *countp;
    411 {
    412 	register int i;
    413 	struct buf *bp;
    414 	register struct fs *fs = ip->i_fs;
    415 	register ufs_daddr_t *bap;
    416 	struct vnode *vp;
    417 	ufs_daddr_t *copy = NULL, nb, nlbn, last;
    418 	long blkcount, factor;
    419 	int nblocks, blocksreleased = 0;
    420 	int error = 0, allerror = 0;
    421 
    422 	/*
    423 	 * Calculate index in current block of last
    424 	 * block to be kept.  -1 indicates the entire
    425 	 * block so we need not calculate the index.
    426 	 */
    427 	factor = 1;
    428 	for (i = SINGLE; i < level; i++)
    429 		factor *= NINDIR(fs);
    430 	last = lastbn;
    431 	if (lastbn > 0)
    432 		last /= factor;
    433 	nblocks = btodb(fs->fs_bsize);
    434 	/*
    435 	 * Get buffer of block pointers, zero those entries corresponding
    436 	 * to blocks to be free'd, and update on disk copy first.  Since
    437 	 * double(triple) indirect before single(double) indirect, calls
    438 	 * to bmap on these blocks will fail.  However, we already have
    439 	 * the on disk address, so we have to set the b_blkno field
    440 	 * explicitly instead of letting bread do everything for us.
    441 	 */
    442 	vp = ITOV(ip);
    443 	bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
    444 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
    445 		/* Braces must be here in case trace evaluates to nothing. */
    446 		trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
    447 	} else {
    448 		trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
    449 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
    450 		bp->b_flags |= B_READ;
    451 		if (bp->b_bcount > bp->b_bufsize)
    452 			panic("ffs_indirtrunc: bad buffer size");
    453 		bp->b_blkno = dbn;
    454 		VOP_STRATEGY(bp);
    455 		error = biowait(bp);
    456 	}
    457 	if (error) {
    458 		brelse(bp);
    459 		*countp = 0;
    460 		return (error);
    461 	}
    462 
    463 	bap = (ufs_daddr_t *)bp->b_data;
    464 	if (lastbn != -1) {
    465 		MALLOC(copy, ufs_daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
    466 		bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
    467 		bzero((caddr_t)&bap[last + 1],
    468 		  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
    469 		error = bwrite(bp);
    470 		if (error)
    471 			allerror = error;
    472 		bap = copy;
    473 	}
    474 
    475 	/*
    476 	 * Recursively free totally unused blocks.
    477 	 */
    478 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
    479 	    i--, nlbn += factor) {
    480 		nb = bap[i];
    481 		if (nb == 0)
    482 			continue;
    483 		if (level > SINGLE) {
    484 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    485 					       (ufs_daddr_t)-1, level - 1,
    486 					       &blkcount);
    487 			if (error)
    488 				allerror = error;
    489 			blocksreleased += blkcount;
    490 		}
    491 		ffs_blkfree(ip, nb, fs->fs_bsize);
    492 		blocksreleased += nblocks;
    493 	}
    494 
    495 	/*
    496 	 * Recursively free last partial block.
    497 	 */
    498 	if (level > SINGLE && lastbn >= 0) {
    499 		last = lastbn % factor;
    500 		nb = bap[i];
    501 		if (nb != 0) {
    502 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    503 					       last, level - 1, &blkcount);
    504 			if (error)
    505 				allerror = error;
    506 			blocksreleased += blkcount;
    507 		}
    508 	}
    509 
    510 	if (copy != NULL) {
    511 		FREE(copy, M_TEMP);
    512 	} else {
    513 		bp->b_flags |= B_INVAL;
    514 		brelse(bp);
    515 	}
    516 
    517 	*countp = blocksreleased;
    518 	return (allerror);
    519 }
    520