Home | History | Annotate | Line # | Download | only in ffs
ffs_inode.c revision 1.130
      1 /*	$NetBSD: ffs_inode.c,v 1.130 2020/07/26 00:21:24 chs 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.130 2020/07/26 00:21:24 chs 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 		      FFS_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)),
    152 		      (int)fs->fs_bsize, B_MODIFY, &bp);
    153 	if (error) {
    154 		return (error);
    155 	}
    156 	ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
    157 	/* Keep unlinked inode list up to date */
    158 	KDASSERTMSG(DIP(ip, nlink) == ip->i_nlink,
    159 	    "DIP(ip, nlink) [%d] == ip->i_nlink [%d]",
    160 	    DIP(ip, nlink), ip->i_nlink);
    161 	if (ip->i_mode) {
    162 		if (ip->i_nlink > 0) {
    163 			UFS_WAPBL_UNREGISTER_INODE(ip->i_ump->um_mountp,
    164 			    ip->i_number, ip->i_mode);
    165 		} else {
    166 			UFS_WAPBL_REGISTER_INODE(ip->i_ump->um_mountp,
    167 			    ip->i_number, ip->i_mode);
    168 		}
    169 	}
    170 	if (fs->fs_magic == FS_UFS1_MAGIC) {
    171 		cp = (char *)bp->b_data +
    172 		    (ino_to_fsbo(fs, ip->i_number) * DINODE1_SIZE);
    173 #ifdef FFS_EI
    174 		if (UFS_FSNEEDSWAP(fs))
    175 			ffs_dinode1_swap(ip->i_din.ffs1_din,
    176 			    (struct ufs1_dinode *)cp);
    177 		else
    178 #endif
    179 			memcpy(cp, ip->i_din.ffs1_din, DINODE1_SIZE);
    180 	} else {
    181 		cp = (char *)bp->b_data +
    182 		    (ino_to_fsbo(fs, ip->i_number) * DINODE2_SIZE);
    183 #ifdef FFS_EI
    184 		if (UFS_FSNEEDSWAP(fs))
    185 			ffs_dinode2_swap(ip->i_din.ffs2_din,
    186 			    (struct ufs2_dinode *)cp);
    187 		else
    188 #endif
    189 			memcpy(cp, ip->i_din.ffs2_din, DINODE2_SIZE);
    190 	}
    191 	if (waitfor) {
    192 		return (bwrite(bp));
    193 	} else {
    194 		bdwrite(bp);
    195 		return (0);
    196 	}
    197 }
    198 
    199 #define	SINGLE	0	/* index of single indirect block */
    200 #define	DOUBLE	1	/* index of double indirect block */
    201 #define	TRIPLE	2	/* index of triple indirect block */
    202 /*
    203  * Truncate the inode oip to at most length size, freeing the
    204  * disk blocks.
    205  */
    206 int
    207 ffs_truncate(struct vnode *ovp, off_t length, int ioflag, kauth_cred_t cred)
    208 {
    209 	daddr_t lastblock;
    210 	struct inode *oip = VTOI(ovp);
    211 	daddr_t bn, lastiblock[UFS_NIADDR], indir_lbn[UFS_NIADDR];
    212 	daddr_t blks[UFS_NDADDR + UFS_NIADDR], oldblks[UFS_NDADDR + UFS_NIADDR];
    213 	struct fs *fs;
    214 	int extblocks;
    215 	int offset, pgoffset, level;
    216 	int64_t blocksreleased = 0, datablocks;
    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 	void *dcookie;
    223 
    224 	UFS_WAPBL_JLOCK_ASSERT(ump->um_mountp);
    225 
    226 	if (ovp->v_type == VCHR || ovp->v_type == VBLK ||
    227 	    ovp->v_type == VFIFO || ovp->v_type == VSOCK) {
    228 		KASSERT(oip->i_size == 0);
    229 		return 0;
    230 	}
    231 
    232 	if (length < 0)
    233 		return (EINVAL);
    234 
    235 	/*
    236 	 * Historically clients did not have to specify which data
    237 	 * they were truncating. So, if not specified, we assume
    238 	 * traditional behavior, e.g., just the normal data.
    239 	 */
    240 	if ((ioflag & (IO_EXT | IO_NORMAL)) == 0)
    241 		ioflag |= IO_NORMAL;
    242 
    243 	fs = oip->i_fs;
    244 #define i_din2 i_din.ffs2_din
    245 	extblocks = 0;
    246 	datablocks = DIP(oip, blocks);
    247 	if (fs->fs_magic == FS_UFS2_MAGIC && oip->i_din2->di_extsize > 0) {
    248 		extblocks = btodb(ffs_fragroundup(fs, oip->i_din2->di_extsize));
    249 		datablocks -= extblocks;
    250 	}
    251 	if ((ioflag & IO_EXT) && extblocks > 0) {
    252 		if (length != 0)
    253 			panic("ffs_truncate: partial trunc of extdata");
    254 		{
    255 #ifdef QUOTA
    256 			(void) chkdq(oip, -extblocks, NOCRED, FORCE);
    257 #endif
    258 			vinvalbuf(ovp, 0, cred, curlwp, 0, 0);
    259 			osize = oip->i_din2->di_extsize;
    260 			oip->i_din2->di_blocks -= extblocks;
    261 			oip->i_din2->di_extsize = 0;
    262 			for (i = 0; i < UFS_NXADDR; i++) {
    263 				oldblks[i] = oip->i_din2->di_extb[i];
    264 				oip->i_din2->di_extb[i] = 0;
    265 			}
    266 			oip->i_flag |= IN_CHANGE;
    267 			if ((error = ffs_update(ovp, NULL, NULL, 0)))
    268 				return (error);
    269 			for (i = 0; i < UFS_NXADDR; i++) {
    270 				if (oldblks[i] == 0)
    271 					continue;
    272 				ffs_blkfree(fs, oip->i_devvp, oldblks[i],
    273 				    ffs_sblksize(fs, osize, i), oip->i_number);
    274 			}
    275 			extblocks = 0;
    276 		}
    277 	}
    278 	if ((ioflag & IO_NORMAL) == 0)
    279 		return (0);
    280 	if (ovp->v_type == VLNK &&
    281 	    (oip->i_size < ump->um_maxsymlinklen ||
    282 	     (ump->um_maxsymlinklen == 0 && datablocks == 0))) {
    283 		KDASSERT(length == 0);
    284 		memset(SHORTLINK(oip), 0, (size_t)oip->i_size);
    285 		oip->i_size = 0;
    286 		DIP_ASSIGN(oip, size, 0);
    287 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    288 		return (ffs_update(ovp, NULL, NULL, 0));
    289 	}
    290 	if (oip->i_size == length) {
    291 		/* still do a uvm_vnp_setsize() as writesize may be larger */
    292 		uvm_vnp_setsize(ovp, length);
    293 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    294 		return (ffs_update(ovp, NULL, NULL, 0));
    295 	}
    296 	if (length > ump->um_maxfilesize)
    297 		return (EFBIG);
    298 
    299 	if ((oip->i_flags & SF_SNAPSHOT) != 0)
    300 		ffs_snapremove(ovp);
    301 
    302 	osize = oip->i_size;
    303 	aflag = ioflag & IO_SYNC ? B_SYNC : 0;
    304 
    305 	/*
    306 	 * Lengthen the size of the file. We must ensure that the
    307 	 * last byte of the file is allocated. Since the smallest
    308 	 * value of osize is 0, length will be at least 1.
    309 	 */
    310 
    311 	if (osize < length) {
    312 		if (ffs_lblkno(fs, osize) < UFS_NDADDR &&
    313 		    ffs_lblkno(fs, osize) != ffs_lblkno(fs, length) &&
    314 		    ffs_blkroundup(fs, osize) != osize) {
    315 			off_t eob;
    316 
    317 			eob = ffs_blkroundup(fs, osize);
    318 			uvm_vnp_setwritesize(ovp, eob);
    319 			error = ufs_balloc_range(ovp, osize, eob - osize,
    320 			    cred, aflag);
    321 			if (error) {
    322 				(void) ffs_truncate(ovp, osize,
    323 				    ioflag & IO_SYNC, cred);
    324 				return error;
    325 			}
    326 			if (ioflag & IO_SYNC) {
    327 				rw_enter(ovp->v_uobj.vmobjlock, RW_WRITER);
    328 				VOP_PUTPAGES(ovp,
    329 				    trunc_page(osize & fs->fs_bmask),
    330 				    round_page(eob), PGO_CLEANIT | PGO_SYNCIO |
    331 				    PGO_JOURNALLOCKED);
    332 			}
    333 		}
    334 		uvm_vnp_setwritesize(ovp, length);
    335 		error = ufs_balloc_range(ovp, length - 1, 1, cred, aflag);
    336 		if (error) {
    337 			(void) ffs_truncate(ovp, osize, ioflag & IO_SYNC, cred);
    338 			return (error);
    339 		}
    340 		uvm_vnp_setsize(ovp, length);
    341 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    342 		KASSERT(ovp->v_size == oip->i_size);
    343 		return (ffs_update(ovp, NULL, NULL, 0));
    344 	}
    345 
    346 	/*
    347 	 * When truncating a regular file down to a non-block-aligned size,
    348 	 * we must zero the part of last block which is past the new EOF.
    349 	 * We must synchronously flush the zeroed pages to disk
    350 	 * since the new pages will be invalidated as soon as we
    351 	 * inform the VM system of the new, smaller size.
    352 	 * We must do this before acquiring the GLOCK, since fetching
    353 	 * the pages will acquire the GLOCK internally.
    354 	 * So there is a window where another thread could see a whole
    355 	 * zeroed page past EOF, but that's life.
    356 	 */
    357 
    358 	offset = ffs_blkoff(fs, length);
    359 	pgoffset = length & PAGE_MASK;
    360 	if (ovp->v_type == VREG && (pgoffset != 0 || offset != 0) &&
    361 	    osize > length) {
    362 		daddr_t lbn;
    363 		voff_t eoz;
    364 		int size;
    365 
    366 		if (offset != 0) {
    367 			error = ufs_balloc_range(ovp, length - 1, 1, cred,
    368 			    aflag);
    369 			if (error)
    370 				return error;
    371 		}
    372 		lbn = ffs_lblkno(fs, length);
    373 		size = ffs_blksize(fs, oip, lbn);
    374 		eoz = MIN(MAX(ffs_lblktosize(fs, lbn) + size, round_page(pgoffset)),
    375 		    osize);
    376 		ubc_zerorange(&ovp->v_uobj, length, eoz - length,
    377 		    UBC_VNODE_FLAGS(ovp));
    378 		if (round_page(eoz) > round_page(length)) {
    379 			rw_enter(ovp->v_uobj.vmobjlock, RW_WRITER);
    380 			error = VOP_PUTPAGES(ovp, round_page(length),
    381 			    round_page(eoz),
    382 			    PGO_CLEANIT | PGO_DEACTIVATE | PGO_JOURNALLOCKED |
    383 			    ((ioflag & IO_SYNC) ? PGO_SYNCIO : 0));
    384 			if (error)
    385 				return error;
    386 		}
    387 	}
    388 
    389 	genfs_node_wrlock(ovp);
    390 	oip->i_size = length;
    391 	DIP_ASSIGN(oip, size, length);
    392 	uvm_vnp_setsize(ovp, length);
    393 	/*
    394 	 * Calculate index into inode's block list of
    395 	 * last direct and indirect blocks (if any)
    396 	 * which we want to keep.  Lastblock is -1 when
    397 	 * the file is truncated to 0.
    398 	 */
    399 	lastblock = ffs_lblkno(fs, length + fs->fs_bsize - 1) - 1;
    400 	lastiblock[SINGLE] = lastblock - UFS_NDADDR;
    401 	lastiblock[DOUBLE] = lastiblock[SINGLE] - FFS_NINDIR(fs);
    402 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - FFS_NINDIR(fs) * FFS_NINDIR(fs);
    403 	nblocks = btodb(fs->fs_bsize);
    404 	/*
    405 	 * Update file and block pointers on disk before we start freeing
    406 	 * blocks.  If we crash before free'ing blocks below, the blocks
    407 	 * will be returned to the free list.  lastiblock values are also
    408 	 * normalized to -1 for calls to ffs_indirtrunc below.
    409 	 */
    410 	sync = 0;
    411 	for (level = TRIPLE; level >= SINGLE; level--) {
    412 		blks[UFS_NDADDR + level] = DIP(oip, ib[level]);
    413 		if (lastiblock[level] < 0 && blks[UFS_NDADDR + level] != 0) {
    414 			sync = 1;
    415 			DIP_ASSIGN(oip, ib[level], 0);
    416 			lastiblock[level] = -1;
    417 		}
    418 	}
    419 	for (i = 0; i < UFS_NDADDR; i++) {
    420 		blks[i] = DIP(oip, db[i]);
    421 		if (i > lastblock && blks[i] != 0) {
    422 			sync = 1;
    423 			DIP_ASSIGN(oip, db[i], 0);
    424 		}
    425 	}
    426 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
    427 	if (sync) {
    428 		error = ffs_update(ovp, NULL, NULL, UPDATE_WAIT);
    429 		if (error && !allerror)
    430 			allerror = error;
    431 	}
    432 
    433 	/*
    434 	 * Having written the new inode to disk, save its new configuration
    435 	 * and put back the old block pointers long enough to process them.
    436 	 * Note that we save the new block configuration so we can check it
    437 	 * when we are done.
    438 	 */
    439 	for (i = 0; i < UFS_NDADDR; i++) {
    440 		bn = DIP(oip, db[i]);
    441 		DIP_ASSIGN(oip, db[i], blks[i]);
    442 		blks[i] = bn;
    443 	}
    444 	for (i = 0; i < UFS_NIADDR; i++) {
    445 		bn = DIP(oip, ib[i]);
    446 		DIP_ASSIGN(oip, ib[i], blks[UFS_NDADDR + i]);
    447 		blks[UFS_NDADDR + i] = bn;
    448 	}
    449 
    450 	oip->i_size = osize;
    451 	DIP_ASSIGN(oip, size, osize);
    452 	error = vtruncbuf(ovp, lastblock + 1, 0, 0);
    453 	if (error && !allerror)
    454 		allerror = error;
    455 
    456 	/*
    457 	 * Indirect blocks first.
    458 	 */
    459 	indir_lbn[SINGLE] = -UFS_NDADDR;
    460 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - FFS_NINDIR(fs) - 1;
    461 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - FFS_NINDIR(fs) * FFS_NINDIR(fs) - 1;
    462 	for (level = TRIPLE; level >= SINGLE; level--) {
    463 		bn = ffs_getib(fs, oip, level);
    464 		if (bn != 0) {
    465 			if (lastiblock[level] < 0 &&
    466 			    oip->i_ump->um_mountp->mnt_wapbl) {
    467 				error = UFS_WAPBL_REGISTER_DEALLOCATION(
    468 				    oip->i_ump->um_mountp,
    469 				    FFS_FSBTODB(fs, bn), fs->fs_bsize,
    470 				    &dcookie);
    471 				if (error)
    472 					goto out;
    473 			} else {
    474 				dcookie = NULL;
    475 			}
    476 
    477 			error = ffs_indirtrunc(oip, indir_lbn[level],
    478 			    FFS_FSBTODB(fs, bn), lastiblock[level], level,
    479 			    &blocksreleased);
    480 			if (error) {
    481 				if (dcookie) {
    482 					UFS_WAPBL_UNREGISTER_DEALLOCATION(
    483 					    oip->i_ump->um_mountp, dcookie);
    484 				}
    485 				goto out;
    486 			}
    487 
    488 			if (lastiblock[level] < 0) {
    489 				if (!dcookie)
    490 					ffs_blkfree(fs, oip->i_devvp, bn,
    491 					    fs->fs_bsize, oip->i_number);
    492 				DIP_ASSIGN(oip, ib[level], 0);
    493 				blocksreleased += nblocks;
    494 			}
    495 		}
    496 		if (lastiblock[level] >= 0)
    497 			goto done;
    498 	}
    499 
    500 	/*
    501 	 * All whole direct blocks or frags.
    502 	 */
    503 	for (i = UFS_NDADDR - 1; i > lastblock; i--) {
    504 		long bsize;
    505 
    506 		bn = ffs_getdb(fs, oip, i);
    507 		if (bn == 0)
    508 			continue;
    509 
    510 		bsize = ffs_blksize(fs, oip, i);
    511 		if ((oip->i_ump->um_mountp->mnt_wapbl) &&
    512 		    (ovp->v_type != VREG)) {
    513 			error = UFS_WAPBL_REGISTER_DEALLOCATION(
    514 			    oip->i_ump->um_mountp,
    515 			    FFS_FSBTODB(fs, bn), bsize, NULL);
    516 			if (error)
    517 				goto out;
    518 		} else
    519 			ffs_blkfree(fs, oip->i_devvp, bn, bsize, oip->i_number);
    520 		DIP_ASSIGN(oip, db[i], 0);
    521 		blocksreleased += btodb(bsize);
    522 	}
    523 	if (lastblock < 0)
    524 		goto done;
    525 
    526 	/*
    527 	 * Finally, look for a change in size of the
    528 	 * last direct block; release any frags.
    529 	 */
    530 	bn = ffs_getdb(fs, oip, lastblock);
    531 	if (bn != 0) {
    532 		long oldspace, newspace;
    533 
    534 		/*
    535 		 * Calculate amount of space we're giving
    536 		 * back as old block size minus new block size.
    537 		 */
    538 		oldspace = ffs_blksize(fs, oip, lastblock);
    539 		oip->i_size = length;
    540 		DIP_ASSIGN(oip, size, length);
    541 		newspace = ffs_blksize(fs, oip, lastblock);
    542 		if (newspace == 0)
    543 			panic("itrunc: newspace");
    544 		if (oldspace - newspace > 0) {
    545 			/*
    546 			 * Block number of space to be free'd is
    547 			 * the old block # plus the number of frags
    548 			 * required for the storage we're keeping.
    549 			 */
    550 			bn += ffs_numfrags(fs, newspace);
    551 			if ((oip->i_ump->um_mountp->mnt_wapbl) &&
    552 			    (ovp->v_type != VREG)) {
    553 				error = UFS_WAPBL_REGISTER_DEALLOCATION(
    554 				    oip->i_ump->um_mountp, FFS_FSBTODB(fs, bn),
    555 				    oldspace - newspace, NULL);
    556 				if (error)
    557 					goto out;
    558 			} else
    559 				ffs_blkfree(fs, oip->i_devvp, bn,
    560 				    oldspace - newspace, oip->i_number);
    561 			blocksreleased += btodb(oldspace - newspace);
    562 		}
    563 	}
    564 
    565 done:
    566 	for (level = SINGLE; level <= TRIPLE; level++)
    567 		KASSERTMSG((blks[UFS_NDADDR + level] == DIP(oip, ib[level])),
    568 		    "itrunc1 blk mismatch: %jx != %jx",
    569 		    (uintmax_t)blks[UFS_NDADDR + level],
    570 		    (uintmax_t)DIP(oip, ib[level]));
    571 	for (i = 0; i < UFS_NDADDR; i++)
    572 		KASSERTMSG((blks[i] == DIP(oip, db[i])),
    573 		    "itrunc2 blk mismatch: %jx != %jx",
    574 		    (uintmax_t)blks[i], (uintmax_t)DIP(oip, db[i]));
    575 	KASSERTMSG((length != 0 || extblocks || LIST_EMPTY(&ovp->v_cleanblkhd)),
    576 	    "itrunc3: zero length and nonempty cleanblkhd");
    577 	KASSERTMSG((length != 0 || extblocks || LIST_EMPTY(&ovp->v_dirtyblkhd)),
    578 	    "itrunc3: zero length and nonempty dirtyblkhd");
    579 
    580 out:
    581 	/*
    582 	 * Set length back to old size if deallocation failed. Some indirect
    583 	 * blocks were deallocated creating a hole, but that is okay.
    584 	 */
    585 	if (error == EAGAIN) {
    586 		if (!allerror)
    587 			allerror = error;
    588 		length = osize;
    589 		uvm_vnp_setsize(ovp, length);
    590 	}
    591 
    592 	/*
    593 	 * Put back the real size.
    594 	 */
    595 	oip->i_size = length;
    596 	DIP_ASSIGN(oip, size, length);
    597 	DIP_ADD(oip, blocks, -blocksreleased);
    598 	genfs_node_unlock(ovp);
    599 	oip->i_flag |= IN_CHANGE;
    600 	UFS_WAPBL_UPDATE(ovp, NULL, NULL, 0);
    601 #if defined(QUOTA) || defined(QUOTA2)
    602 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
    603 #endif
    604 	KASSERT(ovp->v_type != VREG || ovp->v_size == oip->i_size);
    605 	return (allerror);
    606 }
    607 
    608 /*
    609  * Release blocks associated with the inode ip and stored in the indirect
    610  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    611  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    612  * and recursive calls to indirtrunc must be used to cleanse other indirect
    613  * blocks.
    614  *
    615  * NB: triple indirect blocks are untested.
    616  */
    617 static int
    618 ffs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn, daddr_t lastbn,
    619     int level, int64_t *countp)
    620 {
    621 	int i;
    622 	struct buf *bp;
    623 	struct fs *fs = ip->i_fs;
    624 	int32_t *bap1 = NULL;
    625 	int64_t *bap2 = NULL;
    626 	struct vnode *vp;
    627 	daddr_t nb, nlbn, last;
    628 	char *copy = NULL;
    629 	int64_t factor;
    630 	int64_t nblocks;
    631 	int error = 0, allerror = 0;
    632 	const int needswap = UFS_FSNEEDSWAP(fs);
    633 	const int wapbl = (ip->i_ump->um_mountp->mnt_wapbl != NULL);
    634 	void *dcookie;
    635 
    636 #define RBAP(ip, i) (((ip)->i_ump->um_fstype == UFS1) ? \
    637 	    ufs_rw32(bap1[i], needswap) : ufs_rw64(bap2[i], needswap))
    638 #define BAP_ASSIGN(ip, i, value)					\
    639 	do {								\
    640 		if ((ip)->i_ump->um_fstype == UFS1)			\
    641 			bap1[i] = (value);				\
    642 		else							\
    643 			bap2[i] = (value);				\
    644 	} while(0)
    645 
    646 	/*
    647 	 * Calculate index in current block of last
    648 	 * block to be kept.  -1 indicates the entire
    649 	 * block so we need not calculate the index.
    650 	 */
    651 	factor = 1;
    652 	for (i = SINGLE; i < level; i++)
    653 		factor *= FFS_NINDIR(fs);
    654 	last = lastbn;
    655 	if (lastbn > 0)
    656 		last /= factor;
    657 	nblocks = btodb(fs->fs_bsize);
    658 	/*
    659 	 * Get buffer of block pointers, zero those entries corresponding
    660 	 * to blocks to be free'd, and update on disk copy first.  Since
    661 	 * double(triple) indirect before single(double) indirect, calls
    662 	 * to bmap on these blocks will fail.  However, we already have
    663 	 * the on disk address, so we have to set the b_blkno field
    664 	 * explicitly instead of letting bread do everything for us.
    665 	 */
    666 	vp = ITOV(ip);
    667 	error = ffs_getblk(vp, lbn, FFS_NOBLK, fs->fs_bsize, false, &bp);
    668 	if (error)
    669 		return error;
    670 
    671 	if (bp->b_oflags & (BO_DONE | BO_DELWRI)) {
    672 		/* Braces must be here in case trace evaluates to nothing. */
    673 		trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
    674 	} else {
    675 		trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
    676 		curlwp->l_ru.ru_inblock++;	/* pay for read */
    677 		bp->b_flags |= B_READ;
    678 		bp->b_flags &= ~B_COWDONE;	/* we change blkno below */
    679 		if (bp->b_bcount > bp->b_bufsize)
    680 			panic("ffs_indirtrunc: bad buffer size");
    681 		bp->b_blkno = dbn;
    682 		BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
    683 		VOP_STRATEGY(vp, bp);
    684 		error = biowait(bp);
    685 		if (error == 0)
    686 			error = fscow_run(bp, true);
    687 	}
    688 	if (error) {
    689 		brelse(bp, 0);
    690 		return error;
    691 	}
    692 
    693 	/*
    694 	 * Clear reference to blocks to be removed on disk, before actually
    695 	 * reclaiming them, so that fsck is more likely to be able to recover
    696 	 * the filesystem if system goes down during the truncate process.
    697 	 * This assumes the truncate process would not fail, contrary
    698 	 * to the wapbl case.
    699 	 */
    700 	if (ip->i_ump->um_fstype == UFS1)
    701 		bap1 = (int32_t *)bp->b_data;
    702 	else
    703 		bap2 = (int64_t *)bp->b_data;
    704 	if (lastbn >= 0 && !wapbl) {
    705 		copy = kmem_alloc(fs->fs_bsize, KM_SLEEP);
    706 		memcpy((void *)copy, bp->b_data, (u_int)fs->fs_bsize);
    707 		for (i = last + 1; i < FFS_NINDIR(fs); i++)
    708 			BAP_ASSIGN(ip, i, 0);
    709 		error = bwrite(bp);
    710 		if (error)
    711 			allerror = error;
    712 
    713 		if (ip->i_ump->um_fstype == UFS1)
    714 			bap1 = (int32_t *)copy;
    715 		else
    716 			bap2 = (int64_t *)copy;
    717 	}
    718 
    719 	/*
    720 	 * Recursively free totally unused blocks.
    721 	 */
    722 	for (i = FFS_NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
    723 	    i--, nlbn += factor) {
    724 		nb = RBAP(ip, i);
    725 		if (nb == 0)
    726 			continue;
    727 
    728 		if ((ip->i_ump->um_mountp->mnt_wapbl) &&
    729 		    ((level > SINGLE) || (ITOV(ip)->v_type != VREG))) {
    730 			error = UFS_WAPBL_REGISTER_DEALLOCATION(
    731 			    ip->i_ump->um_mountp,
    732 			    FFS_FSBTODB(fs, nb), fs->fs_bsize,
    733 			    &dcookie);
    734 			if (error)
    735 				goto out;
    736 		} else {
    737 			dcookie = NULL;
    738 		}
    739 
    740 		if (level > SINGLE) {
    741 			error = ffs_indirtrunc(ip, nlbn, FFS_FSBTODB(fs, nb),
    742 					       (daddr_t)-1, level - 1, countp);
    743 			if (error) {
    744 				if (dcookie) {
    745 					UFS_WAPBL_UNREGISTER_DEALLOCATION(
    746 					    ip->i_ump->um_mountp, dcookie);
    747 				}
    748 
    749 				goto out;
    750 			}
    751 		}
    752 
    753 		if (!dcookie)
    754 			ffs_blkfree(fs, ip->i_devvp, nb, fs->fs_bsize,
    755 			    ip->i_number);
    756 
    757 		BAP_ASSIGN(ip, i, 0);
    758 		*countp += nblocks;
    759 	}
    760 
    761 	/*
    762 	 * Recursively free blocks on the now last partial indirect block.
    763 	 */
    764 	if (level > SINGLE && lastbn >= 0) {
    765 		last = lastbn % factor;
    766 		nb = RBAP(ip, i);
    767 		if (nb != 0) {
    768 			error = ffs_indirtrunc(ip, nlbn, FFS_FSBTODB(fs, nb),
    769 					       last, level - 1, countp);
    770 			if (error)
    771 				goto out;
    772 		}
    773 	}
    774 
    775 out:
    776  	if (error && !allerror)
    777  		allerror = error;
    778 
    779  	if (copy != NULL) {
    780  		kmem_free(copy, fs->fs_bsize);
    781  	} else if (lastbn < 0 && error == 0) {
    782 		/* all freed, release without writing back */
    783 		brelse(bp, BC_INVAL);
    784 	} else if (wapbl) {
    785  		/* only partially freed, write the updated block */
    786  		error = bwrite(bp);
    787  		if (!allerror)
    788  			allerror = error;
    789 	}
    790 
    791 	return (allerror);
    792 }
    793 
    794 void
    795 ffs_itimes(struct inode *ip, const struct timespec *acc,
    796     const struct timespec *mod, const struct timespec *cre)
    797 {
    798 	struct timespec now;
    799 
    800 	if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY))) {
    801 		return;
    802 	}
    803 
    804 	vfs_timestamp(&now);
    805 	if (ip->i_flag & IN_ACCESS) {
    806 		if (acc == NULL)
    807 			acc = &now;
    808 		DIP_ASSIGN(ip, atime, acc->tv_sec);
    809 		DIP_ASSIGN(ip, atimensec, acc->tv_nsec);
    810 	}
    811 	if (ip->i_flag & (IN_UPDATE | IN_MODIFY)) {
    812 		if ((ip->i_flags & SF_SNAPSHOT) == 0) {
    813 			if (mod == NULL)
    814 				mod = &now;
    815 			DIP_ASSIGN(ip, mtime, mod->tv_sec);
    816 			DIP_ASSIGN(ip, mtimensec, mod->tv_nsec);
    817 		}
    818 		ip->i_modrev++;
    819 	}
    820 	if (ip->i_flag & (IN_CHANGE | IN_MODIFY)) {
    821 		if (cre == NULL)
    822 			cre = &now;
    823 		DIP_ASSIGN(ip, ctime, cre->tv_sec);
    824 		DIP_ASSIGN(ip, ctimensec, cre->tv_nsec);
    825 	}
    826 	if (ip->i_flag & (IN_ACCESS | IN_MODIFY))
    827 		ip->i_flag |= IN_ACCESSED;
    828 	if (ip->i_flag & (IN_UPDATE | IN_CHANGE))
    829 		ip->i_flag |= IN_MODIFIED;
    830 	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY);
    831 }
    832