Home | History | Annotate | Line # | Download | only in ffs
ffs_inode.c revision 1.91
      1 /*	$NetBSD: ffs_inode.c,v 1.91 2007/12/08 15:23:32 ad 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)ffs_inode.c	8.13 (Berkeley) 4/21/95
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: ffs_inode.c,v 1.91 2007/12/08 15:23:32 ad Exp $");
     36 
     37 #if defined(_KERNEL_OPT)
     38 #include "opt_ffs.h"
     39 #include "opt_quota.h"
     40 #endif
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/mount.h>
     45 #include <sys/proc.h>
     46 #include <sys/file.h>
     47 #include <sys/buf.h>
     48 #include <sys/vnode.h>
     49 #include <sys/kernel.h>
     50 #include <sys/malloc.h>
     51 #include <sys/trace.h>
     52 #include <sys/resourcevar.h>
     53 #include <sys/kauth.h>
     54 
     55 #include <ufs/ufs/quota.h>
     56 #include <ufs/ufs/inode.h>
     57 #include <ufs/ufs/ufsmount.h>
     58 #include <ufs/ufs/ufs_extern.h>
     59 #include <ufs/ufs/ufs_bswap.h>
     60 
     61 #include <ufs/ffs/fs.h>
     62 #include <ufs/ffs/ffs_extern.h>
     63 
     64 static int ffs_indirtrunc(struct inode *, daddr_t, daddr_t, daddr_t, int,
     65 			  int64_t *);
     66 
     67 /*
     68  * Update the access, modified, and inode change times as specified
     69  * by the IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively.
     70  * The IN_MODIFIED flag is used to specify that the inode needs to be
     71  * updated but that the times have already been set. The access
     72  * and modified times are taken from the second and third parameters;
     73  * the inode change time is always taken from the current time. If
     74  * UPDATE_WAIT flag is set, or UPDATE_DIROP is set and we are not doing
     75  * softupdates, then wait for the disk write of the inode to complete.
     76  */
     77 
     78 int
     79 ffs_update(struct vnode *vp, const struct timespec *acc,
     80     const struct timespec *mod, int updflags)
     81 {
     82 	struct fs *fs;
     83 	struct buf *bp;
     84 	struct inode *ip;
     85 	int error;
     86 	void *cp;
     87 	int waitfor, flags;
     88 
     89 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
     90 		return (0);
     91 	ip = VTOI(vp);
     92 	FFS_ITIMES(ip, acc, mod, NULL);
     93 	if (updflags & UPDATE_CLOSE)
     94 		flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
     95 	else
     96 		flags = ip->i_flag & IN_MODIFIED;
     97 	if (flags == 0)
     98 		return (0);
     99 	fs = ip->i_fs;
    100 
    101 	if ((flags & IN_MODIFIED) != 0 &&
    102 	    (vp->v_mount->mnt_flag & MNT_ASYNC) == 0) {
    103 		waitfor = updflags & UPDATE_WAIT;
    104 		if ((updflags & UPDATE_DIROP) && !DOINGSOFTDEP(vp))
    105 			waitfor |= UPDATE_WAIT;
    106 	} else
    107 		waitfor = 0;
    108 
    109 	/*
    110 	 * Ensure that uid and gid are correct. This is a temporary
    111 	 * fix until fsck has been changed to do the update.
    112 	 */
    113 	if (fs->fs_magic == FS_UFS1_MAGIC &&			/* XXX */
    114 	    fs->fs_old_inodefmt < FS_44INODEFMT) {		/* XXX */
    115 		ip->i_ffs1_ouid = ip->i_uid;	/* XXX */
    116 		ip->i_ffs1_ogid = ip->i_gid;	/* XXX */
    117 	}							/* XXX */
    118 	error = bread(ip->i_devvp,
    119 		      fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    120 		      (int)fs->fs_bsize, NOCRED, &bp);
    121 	if (error) {
    122 		brelse(bp, 0);
    123 		return (error);
    124 	}
    125 	ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
    126 	if (DOINGSOFTDEP(vp))
    127 		softdep_update_inodeblock(ip, bp, waitfor);
    128 	else if (ip->i_ffs_effnlink != ip->i_nlink)
    129 		panic("ffs_update: bad link cnt");
    130 	if (fs->fs_magic == FS_UFS1_MAGIC) {
    131 		cp = (char *)bp->b_data +
    132 		    (ino_to_fsbo(fs, ip->i_number) * DINODE1_SIZE);
    133 #ifdef FFS_EI
    134 		if (UFS_FSNEEDSWAP(fs))
    135 			ffs_dinode1_swap(ip->i_din.ffs1_din,
    136 			    (struct ufs1_dinode *)cp);
    137 		else
    138 #endif
    139 			memcpy(cp, ip->i_din.ffs1_din, DINODE1_SIZE);
    140 	} else {
    141 		cp = (char *)bp->b_data +
    142 		    (ino_to_fsbo(fs, ip->i_number) * DINODE2_SIZE);
    143 #ifdef FFS_EI
    144 		if (UFS_FSNEEDSWAP(fs))
    145 			ffs_dinode2_swap(ip->i_din.ffs2_din,
    146 			    (struct ufs2_dinode *)cp);
    147 		else
    148 #endif
    149 			memcpy(cp, ip->i_din.ffs2_din, DINODE2_SIZE);
    150 	}
    151 	if (waitfor) {
    152 		return (bwrite(bp));
    153 	} else {
    154 		bdwrite(bp);
    155 		return (0);
    156 	}
    157 }
    158 
    159 #define	SINGLE	0	/* index of single indirect block */
    160 #define	DOUBLE	1	/* index of double indirect block */
    161 #define	TRIPLE	2	/* index of triple indirect block */
    162 /*
    163  * Truncate the inode oip to at most length size, freeing the
    164  * disk blocks.
    165  */
    166 int
    167 ffs_truncate(struct vnode *ovp, off_t length, int ioflag, kauth_cred_t cred,
    168     struct lwp *l)
    169 {
    170 	daddr_t lastblock;
    171 	struct inode *oip = VTOI(ovp);
    172 	daddr_t bn, lastiblock[NIADDR], indir_lbn[NIADDR];
    173 	daddr_t blks[NDADDR + NIADDR];
    174 	struct fs *fs;
    175 	int offset, pgoffset, level;
    176 	int64_t count, blocksreleased = 0;
    177 	int i, aflag, nblocks;
    178 	int error, allerror = 0;
    179 	off_t osize;
    180 	int sync;
    181 	struct ufsmount *ump = oip->i_ump;
    182 
    183 	if (ovp->v_type == VCHR || ovp->v_type == VBLK ||
    184 	    ovp->v_type == VFIFO || ovp->v_type == VSOCK) {
    185 		KASSERT(oip->i_size == 0);
    186 		return 0;
    187 	}
    188 
    189 	if (length < 0)
    190 		return (EINVAL);
    191 
    192 	if (ovp->v_type == VLNK &&
    193 	    (oip->i_size < ump->um_maxsymlinklen ||
    194 	     (ump->um_maxsymlinklen == 0 && DIP(oip, blocks) == 0))) {
    195 		KDASSERT(length == 0);
    196 		memset(SHORTLINK(oip), 0, (size_t)oip->i_size);
    197 		oip->i_size = 0;
    198 		DIP_ASSIGN(oip, size, 0);
    199 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    200 		return (ffs_update(ovp, NULL, NULL, 0));
    201 	}
    202 	if (oip->i_size == length) {
    203 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    204 		return (ffs_update(ovp, NULL, NULL, 0));
    205 	}
    206 	fs = oip->i_fs;
    207 	if (length > ump->um_maxfilesize)
    208 		return (EFBIG);
    209 
    210 	if ((oip->i_flags & SF_SNAPSHOT) != 0)
    211 		ffs_snapremove(ovp);
    212 
    213 	osize = oip->i_size;
    214 	aflag = ioflag & IO_SYNC ? B_SYNC : 0;
    215 
    216 	/*
    217 	 * Lengthen the size of the file. We must ensure that the
    218 	 * last byte of the file is allocated. Since the smallest
    219 	 * value of osize is 0, length will be at least 1.
    220 	 */
    221 
    222 	if (osize < length) {
    223 		if (lblkno(fs, osize) < NDADDR &&
    224 		    lblkno(fs, osize) != lblkno(fs, length) &&
    225 		    blkroundup(fs, osize) != osize) {
    226 			off_t eob;
    227 
    228 			eob = blkroundup(fs, osize);
    229 			uvm_vnp_setwritesize(ovp, eob);
    230 			error = ufs_balloc_range(ovp, osize, eob - osize,
    231 			    cred, aflag);
    232 			if (error)
    233 				return error;
    234 			if (ioflag & IO_SYNC) {
    235 				simple_lock(&ovp->v_interlock);
    236 				VOP_PUTPAGES(ovp,
    237 				    trunc_page(osize & fs->fs_bmask),
    238 				    round_page(eob), PGO_CLEANIT | PGO_SYNCIO);
    239 			}
    240 		}
    241 		uvm_vnp_setwritesize(ovp, length);
    242 		error = ufs_balloc_range(ovp, length - 1, 1, cred, aflag);
    243 		if (error) {
    244 			(void) ffs_truncate(ovp, osize, ioflag & IO_SYNC,
    245 			    cred, l);
    246 			return (error);
    247 		}
    248 		uvm_vnp_setsize(ovp, length);
    249 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    250 		KASSERT(ovp->v_size == oip->i_size);
    251 		return (ffs_update(ovp, NULL, NULL, 0));
    252 	}
    253 
    254 	/*
    255 	 * When truncating a regular file down to a non-block-aligned size,
    256 	 * we must zero the part of last block which is past the new EOF.
    257 	 * We must synchronously flush the zeroed pages to disk
    258 	 * since the new pages will be invalidated as soon as we
    259 	 * inform the VM system of the new, smaller size.
    260 	 * We must do this before acquiring the GLOCK, since fetching
    261 	 * the pages will acquire the GLOCK internally.
    262 	 * So there is a window where another thread could see a whole
    263 	 * zeroed page past EOF, but that's life.
    264 	 */
    265 
    266 	offset = blkoff(fs, length);
    267 	pgoffset = length & PAGE_MASK;
    268 	if (ovp->v_type == VREG && (pgoffset != 0 || offset != 0) &&
    269 	    osize > length) {
    270 		daddr_t lbn;
    271 		voff_t eoz;
    272 		int size;
    273 
    274 		if (offset != 0) {
    275 			error = ufs_balloc_range(ovp, length - 1, 1, cred,
    276 			    aflag);
    277 			if (error)
    278 				return error;
    279 		}
    280 		lbn = lblkno(fs, length);
    281 		size = blksize(fs, oip, lbn);
    282 		eoz = MIN(MAX(lblktosize(fs, lbn) + size, round_page(pgoffset)),
    283 		    osize);
    284 		uvm_vnp_zerorange(ovp, length, eoz - length);
    285 		if (round_page(eoz) > round_page(length)) {
    286 			simple_lock(&ovp->v_interlock);
    287 			error = VOP_PUTPAGES(ovp, round_page(length),
    288 			    round_page(eoz),
    289 			    PGO_CLEANIT | PGO_DEACTIVATE |
    290 			    ((ioflag & IO_SYNC) ? PGO_SYNCIO : 0));
    291 			if (error)
    292 				return error;
    293 		}
    294 	}
    295 
    296 	genfs_node_wrlock(ovp);
    297 
    298 	if (DOINGSOFTDEP(ovp)) {
    299 		if (length > 0) {
    300 			/*
    301 			 * If a file is only partially truncated, then
    302 			 * we have to clean up the data structures
    303 			 * describing the allocation past the truncation
    304 			 * point. Finding and deallocating those structures
    305 			 * is a lot of work. Since partial truncation occurs
    306 			 * rarely, we solve the problem by syncing the file
    307 			 * so that it will have no data structures left.
    308 			 */
    309 			if ((error = VOP_FSYNC(ovp, cred, FSYNC_WAIT,
    310 			    0, 0)) != 0) {
    311 				genfs_node_unlock(ovp);
    312 				return (error);
    313 			}
    314 			mutex_enter(&ump->um_lock);
    315 			if (oip->i_flag & IN_SPACECOUNTED)
    316 				fs->fs_pendingblocks -= DIP(oip, blocks);
    317 			mutex_exit(&ump->um_lock);
    318 		} else {
    319 			uvm_vnp_setsize(ovp, length);
    320 #ifdef QUOTA
    321  			(void) chkdq(oip, -DIP(oip, blocks), NOCRED, 0);
    322 #endif
    323 			softdep_setup_freeblocks(oip, length, 0);
    324 			(void) vinvalbuf(ovp, 0, cred, l, 0, 0);
    325 			genfs_node_unlock(ovp);
    326 			oip->i_flag |= IN_CHANGE | IN_UPDATE;
    327 			return (ffs_update(ovp, NULL, NULL, 0));
    328 		}
    329 	}
    330 	oip->i_size = length;
    331 	DIP_ASSIGN(oip, size, length);
    332 	uvm_vnp_setsize(ovp, length);
    333 	/*
    334 	 * Calculate index into inode's block list of
    335 	 * last direct and indirect blocks (if any)
    336 	 * which we want to keep.  Lastblock is -1 when
    337 	 * the file is truncated to 0.
    338 	 */
    339 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
    340 	lastiblock[SINGLE] = lastblock - NDADDR;
    341 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    342 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    343 	nblocks = btodb(fs->fs_bsize);
    344 	/*
    345 	 * Update file and block pointers on disk before we start freeing
    346 	 * blocks.  If we crash before free'ing blocks below, the blocks
    347 	 * will be returned to the free list.  lastiblock values are also
    348 	 * normalized to -1 for calls to ffs_indirtrunc below.
    349 	 */
    350 	sync = 0;
    351 	for (level = TRIPLE; level >= SINGLE; level--) {
    352 		blks[NDADDR + level] = DIP(oip, ib[level]);
    353 		if (lastiblock[level] < 0 && blks[NDADDR + level] != 0) {
    354 			sync = 1;
    355 			DIP_ASSIGN(oip, ib[level], 0);
    356 			lastiblock[level] = -1;
    357 		}
    358 	}
    359 	for (i = 0; i < NDADDR; i++) {
    360 		blks[i] = DIP(oip, db[i]);
    361 		if (i > lastblock && blks[i] != 0) {
    362 			sync = 1;
    363 			DIP_ASSIGN(oip, db[i], 0);
    364 		}
    365 	}
    366 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
    367 	if (sync) {
    368 		error = ffs_update(ovp, NULL, NULL, UPDATE_WAIT);
    369 		if (error && !allerror)
    370 			allerror = error;
    371 	}
    372 
    373 	/*
    374 	 * Having written the new inode to disk, save its new configuration
    375 	 * and put back the old block pointers long enough to process them.
    376 	 * Note that we save the new block configuration so we can check it
    377 	 * when we are done.
    378 	 */
    379 	for (i = 0; i < NDADDR; i++) {
    380 		bn = DIP(oip, db[i]);
    381 		DIP_ASSIGN(oip, db[i], blks[i]);
    382 		blks[i] = bn;
    383 	}
    384 	for (i = 0; i < NIADDR; i++) {
    385 		bn = DIP(oip, ib[i]);
    386 		DIP_ASSIGN(oip, ib[i], blks[NDADDR + i]);
    387 		blks[NDADDR + i] = bn;
    388 	}
    389 
    390 	oip->i_size = osize;
    391 	DIP_ASSIGN(oip, size, osize);
    392 	error = vtruncbuf(ovp, lastblock + 1, 0, 0);
    393 	if (error && !allerror)
    394 		allerror = error;
    395 
    396 	/*
    397 	 * Indirect blocks first.
    398 	 */
    399 	indir_lbn[SINGLE] = -NDADDR;
    400 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
    401 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    402 	for (level = TRIPLE; level >= SINGLE; level--) {
    403 		if (oip->i_ump->um_fstype == UFS1)
    404 			bn = ufs_rw32(oip->i_ffs1_ib[level],UFS_FSNEEDSWAP(fs));
    405 		else
    406 			bn = ufs_rw64(oip->i_ffs2_ib[level],UFS_FSNEEDSWAP(fs));
    407 		if (bn != 0) {
    408 			error = ffs_indirtrunc(oip, indir_lbn[level],
    409 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
    410 			if (error)
    411 				allerror = error;
    412 			blocksreleased += count;
    413 			if (lastiblock[level] < 0) {
    414 				DIP_ASSIGN(oip, ib[level], 0);
    415 				ffs_blkfree(fs, oip->i_devvp, bn, fs->fs_bsize,
    416 				    oip->i_number);
    417 				blocksreleased += nblocks;
    418 			}
    419 		}
    420 		if (lastiblock[level] >= 0)
    421 			goto done;
    422 	}
    423 
    424 	/*
    425 	 * All whole direct blocks or frags.
    426 	 */
    427 	for (i = NDADDR - 1; i > lastblock; i--) {
    428 		long bsize;
    429 
    430 		if (oip->i_ump->um_fstype == UFS1)
    431 			bn = ufs_rw32(oip->i_ffs1_db[i], UFS_FSNEEDSWAP(fs));
    432 		else
    433 			bn = ufs_rw64(oip->i_ffs2_db[i], UFS_FSNEEDSWAP(fs));
    434 		if (bn == 0)
    435 			continue;
    436 		DIP_ASSIGN(oip, db[i], 0);
    437 		bsize = blksize(fs, oip, i);
    438 		ffs_blkfree(fs, oip->i_devvp, bn, bsize, oip->i_number);
    439 		blocksreleased += btodb(bsize);
    440 	}
    441 	if (lastblock < 0)
    442 		goto done;
    443 
    444 	/*
    445 	 * Finally, look for a change in size of the
    446 	 * last direct block; release any frags.
    447 	 */
    448 	if (oip->i_ump->um_fstype == UFS1)
    449 		bn = ufs_rw32(oip->i_ffs1_db[lastblock], UFS_FSNEEDSWAP(fs));
    450 	else
    451 		bn = ufs_rw64(oip->i_ffs2_db[lastblock], UFS_FSNEEDSWAP(fs));
    452 	if (bn != 0) {
    453 		long oldspace, newspace;
    454 
    455 		/*
    456 		 * Calculate amount of space we're giving
    457 		 * back as old block size minus new block size.
    458 		 */
    459 		oldspace = blksize(fs, oip, lastblock);
    460 		oip->i_size = length;
    461 		DIP_ASSIGN(oip, size, length);
    462 		newspace = blksize(fs, oip, lastblock);
    463 		if (newspace == 0)
    464 			panic("itrunc: newspace");
    465 		if (oldspace - newspace > 0) {
    466 			/*
    467 			 * Block number of space to be free'd is
    468 			 * the old block # plus the number of frags
    469 			 * required for the storage we're keeping.
    470 			 */
    471 			bn += numfrags(fs, newspace);
    472 			ffs_blkfree(fs, oip->i_devvp, bn, oldspace - newspace,
    473 			    oip->i_number);
    474 			blocksreleased += btodb(oldspace - newspace);
    475 		}
    476 	}
    477 
    478 done:
    479 #ifdef DIAGNOSTIC
    480 	for (level = SINGLE; level <= TRIPLE; level++)
    481 		if (blks[NDADDR + level] != DIP(oip, ib[level]))
    482 			panic("itrunc1");
    483 	for (i = 0; i < NDADDR; i++)
    484 		if (blks[i] != DIP(oip, db[i]))
    485 			panic("itrunc2");
    486 	if (length == 0 &&
    487 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
    488 		panic("itrunc3");
    489 #endif /* DIAGNOSTIC */
    490 	/*
    491 	 * Put back the real size.
    492 	 */
    493 	oip->i_size = length;
    494 	DIP_ASSIGN(oip, size, length);
    495 	DIP_ADD(oip, blocks, -blocksreleased);
    496 	genfs_node_unlock(ovp);
    497 	oip->i_flag |= IN_CHANGE;
    498 #ifdef QUOTA
    499 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
    500 #endif
    501 	KASSERT(ovp->v_type != VREG || ovp->v_size == oip->i_size);
    502 	return (allerror);
    503 }
    504 
    505 /*
    506  * Release blocks associated with the inode ip and stored in the indirect
    507  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    508  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    509  * and recursive calls to indirtrunc must be used to cleanse other indirect
    510  * blocks.
    511  *
    512  * NB: triple indirect blocks are untested.
    513  */
    514 static int
    515 ffs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn, daddr_t lastbn,
    516     int level, int64_t *countp)
    517 {
    518 	int i;
    519 	struct buf *bp;
    520 	struct fs *fs = ip->i_fs;
    521 	int32_t *bap1 = NULL;
    522 	int64_t *bap2 = NULL;
    523 	struct vnode *vp;
    524 	daddr_t nb, nlbn, last;
    525 	char *copy = NULL;
    526 	int64_t blkcount, factor, blocksreleased = 0;
    527 	int nblocks;
    528 	int error = 0, allerror = 0;
    529 #ifdef FFS_EI
    530 	const int needswap = UFS_FSNEEDSWAP(fs);
    531 #endif
    532 #define RBAP(ip, i) (((ip)->i_ump->um_fstype == UFS1) ? \
    533 	    ufs_rw32(bap1[i], needswap) : ufs_rw64(bap2[i], needswap))
    534 #define BAP_ASSIGN(ip, i, value)					\
    535 	do {								\
    536 		if ((ip)->i_ump->um_fstype == UFS1)			\
    537 			bap1[i] = (value);				\
    538 		else							\
    539 			bap2[i] = (value);				\
    540 	} while(0)
    541 
    542 	/*
    543 	 * Calculate index in current block of last
    544 	 * block to be kept.  -1 indicates the entire
    545 	 * block so we need not calculate the index.
    546 	 */
    547 	factor = 1;
    548 	for (i = SINGLE; i < level; i++)
    549 		factor *= NINDIR(fs);
    550 	last = lastbn;
    551 	if (lastbn > 0)
    552 		last /= factor;
    553 	nblocks = btodb(fs->fs_bsize);
    554 	/*
    555 	 * Get buffer of block pointers, zero those entries corresponding
    556 	 * to blocks to be free'd, and update on disk copy first.  Since
    557 	 * double(triple) indirect before single(double) indirect, calls
    558 	 * to bmap on these blocks will fail.  However, we already have
    559 	 * the on disk address, so we have to set the b_blkno field
    560 	 * explicitly instead of letting bread do everything for us.
    561 	 */
    562 	vp = ITOV(ip);
    563 	bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
    564 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
    565 		/* Braces must be here in case trace evaluates to nothing. */
    566 		trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
    567 	} else {
    568 		trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
    569 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
    570 		bp->b_flags |= B_READ;
    571 		if (bp->b_bcount > bp->b_bufsize)
    572 			panic("ffs_indirtrunc: bad buffer size");
    573 		bp->b_blkno = dbn;
    574 		BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
    575 		VOP_STRATEGY(vp, bp);
    576 		error = biowait(bp);
    577 	}
    578 	if (error) {
    579 		brelse(bp, 0);
    580 		*countp = 0;
    581 		return (error);
    582 	}
    583 
    584 	if (ip->i_ump->um_fstype == UFS1)
    585 		bap1 = (int32_t *)bp->b_data;
    586 	else
    587 		bap2 = (int64_t *)bp->b_data;
    588 	if (lastbn >= 0) {
    589 		copy = malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
    590 		memcpy((void *)copy, bp->b_data, (u_int)fs->fs_bsize);
    591 		for (i = last + 1; i < NINDIR(fs); i++)
    592 			BAP_ASSIGN(ip, i, 0);
    593 		error = bwrite(bp);
    594 		if (error)
    595 			allerror = error;
    596 		if (ip->i_ump->um_fstype == UFS1)
    597 			bap1 = (int32_t *)copy;
    598 		else
    599 			bap2 = (int64_t *)copy;
    600 	}
    601 
    602 	/*
    603 	 * Recursively free totally unused blocks.
    604 	 */
    605 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
    606 	    i--, nlbn += factor) {
    607 		nb = RBAP(ip, i);
    608 		if (nb == 0)
    609 			continue;
    610 		if (level > SINGLE) {
    611 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    612 					       (daddr_t)-1, level - 1,
    613 					       &blkcount);
    614 			if (error)
    615 				allerror = error;
    616 			blocksreleased += blkcount;
    617 		}
    618 		ffs_blkfree(fs, ip->i_devvp, nb, fs->fs_bsize, ip->i_number);
    619 		blocksreleased += nblocks;
    620 	}
    621 
    622 	/*
    623 	 * Recursively free last partial block.
    624 	 */
    625 	if (level > SINGLE && lastbn >= 0) {
    626 		last = lastbn % factor;
    627 		nb = RBAP(ip, i);
    628 		if (nb != 0) {
    629 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    630 					       last, level - 1, &blkcount);
    631 			if (error)
    632 				allerror = error;
    633 			blocksreleased += blkcount;
    634 		}
    635 	}
    636 
    637 	if (copy != NULL) {
    638 		FREE(copy, M_TEMP);
    639 	} else {
    640 		brelse(bp, BC_INVAL);
    641 	}
    642 
    643 	*countp = blocksreleased;
    644 	return (allerror);
    645 }
    646 
    647 void
    648 ffs_itimes(struct inode *ip, const struct timespec *acc,
    649     const struct timespec *mod, const struct timespec *cre)
    650 {
    651 	struct timespec now;
    652 
    653 	if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY))) {
    654 		return;
    655 	}
    656 
    657 	vfs_timestamp(&now);
    658 	if (ip->i_flag & IN_ACCESS) {
    659 		if (acc == NULL)
    660 			acc = &now;
    661 		DIP_ASSIGN(ip, atime, acc->tv_sec);
    662 		DIP_ASSIGN(ip, atimensec, acc->tv_nsec);
    663 	}
    664 	if (ip->i_flag & (IN_UPDATE | IN_MODIFY)) {
    665 		if ((ip->i_flags & SF_SNAPSHOT) == 0) {
    666 			if (mod == NULL)
    667 				mod = &now;
    668 			DIP_ASSIGN(ip, mtime, mod->tv_sec);
    669 			DIP_ASSIGN(ip, mtimensec, mod->tv_nsec);
    670 		}
    671 		ip->i_modrev++;
    672 	}
    673 	if (ip->i_flag & (IN_CHANGE | IN_MODIFY)) {
    674 		if (cre == NULL)
    675 			cre = &now;
    676 		DIP_ASSIGN(ip, ctime, cre->tv_sec);
    677 		DIP_ASSIGN(ip, ctimensec, cre->tv_nsec);
    678 	}
    679 	if (ip->i_flag & (IN_ACCESS | IN_MODIFY))
    680 		ip->i_flag |= IN_ACCESSED;
    681 	if (ip->i_flag & (IN_UPDATE | IN_CHANGE))
    682 		ip->i_flag |= IN_MODIFIED;
    683 	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY);
    684 }
    685