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