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