Home | History | Annotate | Line # | Download | only in ffs
ffs_inode.c revision 1.107
      1 /*	$NetBSD: ffs_inode.c,v 1.107 2011/06/16 09:21:03 hannken 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 2011/06/16 09:21:03 hannken 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/malloc.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 	KDASSERT(DIP(ip, nlink) == ip->i_nlink);
    160 	if (ip->i_mode) {
    161 		if (ip->i_nlink > 0) {
    162 			UFS_WAPBL_UNREGISTER_INODE(ip->i_ump->um_mountp,
    163 			    ip->i_number, ip->i_mode);
    164 		} else {
    165 			UFS_WAPBL_REGISTER_INODE(ip->i_ump->um_mountp,
    166 			    ip->i_number, ip->i_mode);
    167 		}
    168 	}
    169 	if (fs->fs_magic == FS_UFS1_MAGIC) {
    170 		cp = (char *)bp->b_data +
    171 		    (ino_to_fsbo(fs, ip->i_number) * DINODE1_SIZE);
    172 #ifdef FFS_EI
    173 		if (UFS_FSNEEDSWAP(fs))
    174 			ffs_dinode1_swap(ip->i_din.ffs1_din,
    175 			    (struct ufs1_dinode *)cp);
    176 		else
    177 #endif
    178 			memcpy(cp, ip->i_din.ffs1_din, DINODE1_SIZE);
    179 	} else {
    180 		cp = (char *)bp->b_data +
    181 		    (ino_to_fsbo(fs, ip->i_number) * DINODE2_SIZE);
    182 #ifdef FFS_EI
    183 		if (UFS_FSNEEDSWAP(fs))
    184 			ffs_dinode2_swap(ip->i_din.ffs2_din,
    185 			    (struct ufs2_dinode *)cp);
    186 		else
    187 #endif
    188 			memcpy(cp, ip->i_din.ffs2_din, DINODE2_SIZE);
    189 	}
    190 	if (waitfor) {
    191 		return (bwrite(bp));
    192 	} else {
    193 		bdwrite(bp);
    194 		return (0);
    195 	}
    196 }
    197 
    198 #define	SINGLE	0	/* index of single indirect block */
    199 #define	DOUBLE	1	/* index of double indirect block */
    200 #define	TRIPLE	2	/* index of triple indirect block */
    201 /*
    202  * Truncate the inode oip to at most length size, freeing the
    203  * disk blocks.
    204  */
    205 int
    206 ffs_truncate(struct vnode *ovp, off_t length, int ioflag, kauth_cred_t cred)
    207 {
    208 	daddr_t lastblock;
    209 	struct inode *oip = VTOI(ovp);
    210 	daddr_t bn, lastiblock[NIADDR], indir_lbn[NIADDR];
    211 	daddr_t blks[NDADDR + NIADDR];
    212 	struct fs *fs;
    213 	int offset, pgoffset, level;
    214 	int64_t count, blocksreleased = 0;
    215 	int i, aflag, nblocks;
    216 	int error, allerror = 0;
    217 	off_t osize;
    218 	int sync;
    219 	struct ufsmount *ump = oip->i_ump;
    220 
    221 	if (ovp->v_type == VCHR || ovp->v_type == VBLK ||
    222 	    ovp->v_type == VFIFO || ovp->v_type == VSOCK) {
    223 		KASSERT(oip->i_size == 0);
    224 		return 0;
    225 	}
    226 
    227 	if (length < 0)
    228 		return (EINVAL);
    229 
    230 	if (ovp->v_type == VLNK &&
    231 	    (oip->i_size < ump->um_maxsymlinklen ||
    232 	     (ump->um_maxsymlinklen == 0 && DIP(oip, blocks) == 0))) {
    233 		KDASSERT(length == 0);
    234 		memset(SHORTLINK(oip), 0, (size_t)oip->i_size);
    235 		oip->i_size = 0;
    236 		DIP_ASSIGN(oip, size, 0);
    237 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    238 		return (ffs_update(ovp, NULL, NULL, 0));
    239 	}
    240 	if (oip->i_size == length) {
    241 		/* still do a uvm_vnp_setsize() as writesize may be larger */
    242 		uvm_vnp_setsize(ovp, length);
    243 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    244 		return (ffs_update(ovp, NULL, NULL, 0));
    245 	}
    246 	fs = oip->i_fs;
    247 	if (length > ump->um_maxfilesize)
    248 		return (EFBIG);
    249 
    250 	if ((oip->i_flags & SF_SNAPSHOT) != 0)
    251 		ffs_snapremove(ovp);
    252 
    253 	osize = oip->i_size;
    254 	aflag = ioflag & IO_SYNC ? B_SYNC : 0;
    255 
    256 	/*
    257 	 * Lengthen the size of the file. We must ensure that the
    258 	 * last byte of the file is allocated. Since the smallest
    259 	 * value of osize is 0, length will be at least 1.
    260 	 */
    261 
    262 	if (osize < length) {
    263 		if (lblkno(fs, osize) < NDADDR &&
    264 		    lblkno(fs, osize) != lblkno(fs, length) &&
    265 		    blkroundup(fs, osize) != osize) {
    266 			off_t eob;
    267 
    268 			eob = blkroundup(fs, osize);
    269 			uvm_vnp_setwritesize(ovp, eob);
    270 			error = ufs_balloc_range(ovp, osize, eob - osize,
    271 			    cred, aflag);
    272 			if (error)
    273 				return error;
    274 			if (ioflag & IO_SYNC) {
    275 				mutex_enter(ovp->v_interlock);
    276 				VOP_PUTPAGES(ovp,
    277 				    trunc_page(osize & fs->fs_bmask),
    278 				    round_page(eob), PGO_CLEANIT | PGO_SYNCIO |
    279 				    PGO_JOURNALLOCKED);
    280 			}
    281 		}
    282 		uvm_vnp_setwritesize(ovp, length);
    283 		error = ufs_balloc_range(ovp, length - 1, 1, cred, aflag);
    284 		if (error) {
    285 			(void) ffs_truncate(ovp, osize, ioflag & IO_SYNC, cred);
    286 			return (error);
    287 		}
    288 		uvm_vnp_setsize(ovp, length);
    289 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    290 		KASSERT(ovp->v_size == oip->i_size);
    291 		return (ffs_update(ovp, NULL, NULL, 0));
    292 	}
    293 
    294 	/*
    295 	 * When truncating a regular file down to a non-block-aligned size,
    296 	 * we must zero the part of last block which is past the new EOF.
    297 	 * We must synchronously flush the zeroed pages to disk
    298 	 * since the new pages will be invalidated as soon as we
    299 	 * inform the VM system of the new, smaller size.
    300 	 * We must do this before acquiring the GLOCK, since fetching
    301 	 * the pages will acquire the GLOCK internally.
    302 	 * So there is a window where another thread could see a whole
    303 	 * zeroed page past EOF, but that's life.
    304 	 */
    305 
    306 	offset = blkoff(fs, length);
    307 	pgoffset = length & PAGE_MASK;
    308 	if (ovp->v_type == VREG && (pgoffset != 0 || offset != 0) &&
    309 	    osize > length) {
    310 		daddr_t lbn;
    311 		voff_t eoz;
    312 		int size;
    313 
    314 		if (offset != 0) {
    315 			error = ufs_balloc_range(ovp, length - 1, 1, cred,
    316 			    aflag);
    317 			if (error)
    318 				return error;
    319 		}
    320 		lbn = lblkno(fs, length);
    321 		size = blksize(fs, oip, lbn);
    322 		eoz = MIN(MAX(lblktosize(fs, lbn) + size, round_page(pgoffset)),
    323 		    osize);
    324 		ubc_zerorange(&ovp->v_uobj, length, eoz - length,
    325 		    UBC_UNMAP_FLAG(ovp));
    326 		if (round_page(eoz) > round_page(length)) {
    327 			mutex_enter(ovp->v_interlock);
    328 			error = VOP_PUTPAGES(ovp, round_page(length),
    329 			    round_page(eoz),
    330 			    PGO_CLEANIT | PGO_DEACTIVATE | PGO_JOURNALLOCKED |
    331 			    ((ioflag & IO_SYNC) ? PGO_SYNCIO : 0));
    332 			if (error)
    333 				return error;
    334 		}
    335 	}
    336 
    337 	genfs_node_wrlock(ovp);
    338 	oip->i_size = length;
    339 	DIP_ASSIGN(oip, size, length);
    340 	uvm_vnp_setsize(ovp, length);
    341 	/*
    342 	 * Calculate index into inode's block list of
    343 	 * last direct and indirect blocks (if any)
    344 	 * which we want to keep.  Lastblock is -1 when
    345 	 * the file is truncated to 0.
    346 	 */
    347 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
    348 	lastiblock[SINGLE] = lastblock - NDADDR;
    349 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    350 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    351 	nblocks = btodb(fs->fs_bsize);
    352 	/*
    353 	 * Update file and block pointers on disk before we start freeing
    354 	 * blocks.  If we crash before free'ing blocks below, the blocks
    355 	 * will be returned to the free list.  lastiblock values are also
    356 	 * normalized to -1 for calls to ffs_indirtrunc below.
    357 	 */
    358 	sync = 0;
    359 	for (level = TRIPLE; level >= SINGLE; level--) {
    360 		blks[NDADDR + level] = DIP(oip, ib[level]);
    361 		if (lastiblock[level] < 0 && blks[NDADDR + level] != 0) {
    362 			sync = 1;
    363 			DIP_ASSIGN(oip, ib[level], 0);
    364 			lastiblock[level] = -1;
    365 		}
    366 	}
    367 	for (i = 0; i < NDADDR; i++) {
    368 		blks[i] = DIP(oip, db[i]);
    369 		if (i > lastblock && blks[i] != 0) {
    370 			sync = 1;
    371 			DIP_ASSIGN(oip, db[i], 0);
    372 		}
    373 	}
    374 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
    375 	if (sync) {
    376 		error = ffs_update(ovp, NULL, NULL, UPDATE_WAIT);
    377 		if (error && !allerror)
    378 			allerror = error;
    379 	}
    380 
    381 	/*
    382 	 * Having written the new inode to disk, save its new configuration
    383 	 * and put back the old block pointers long enough to process them.
    384 	 * Note that we save the new block configuration so we can check it
    385 	 * when we are done.
    386 	 */
    387 	for (i = 0; i < NDADDR; i++) {
    388 		bn = DIP(oip, db[i]);
    389 		DIP_ASSIGN(oip, db[i], blks[i]);
    390 		blks[i] = bn;
    391 	}
    392 	for (i = 0; i < NIADDR; i++) {
    393 		bn = DIP(oip, ib[i]);
    394 		DIP_ASSIGN(oip, ib[i], blks[NDADDR + i]);
    395 		blks[NDADDR + i] = bn;
    396 	}
    397 
    398 	oip->i_size = osize;
    399 	DIP_ASSIGN(oip, size, osize);
    400 	error = vtruncbuf(ovp, lastblock + 1, 0, 0);
    401 	if (error && !allerror)
    402 		allerror = error;
    403 
    404 	/*
    405 	 * Indirect blocks first.
    406 	 */
    407 	indir_lbn[SINGLE] = -NDADDR;
    408 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
    409 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    410 	for (level = TRIPLE; level >= SINGLE; level--) {
    411 		if (oip->i_ump->um_fstype == UFS1)
    412 			bn = ufs_rw32(oip->i_ffs1_ib[level],UFS_FSNEEDSWAP(fs));
    413 		else
    414 			bn = ufs_rw64(oip->i_ffs2_ib[level],UFS_FSNEEDSWAP(fs));
    415 		if (bn != 0) {
    416 			error = ffs_indirtrunc(oip, indir_lbn[level],
    417 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
    418 			if (error)
    419 				allerror = error;
    420 			blocksreleased += count;
    421 			if (lastiblock[level] < 0) {
    422 				DIP_ASSIGN(oip, ib[level], 0);
    423 				if (oip->i_ump->um_mountp->mnt_wapbl) {
    424 					UFS_WAPBL_REGISTER_DEALLOCATION(
    425 					    oip->i_ump->um_mountp,
    426 					    fsbtodb(fs, bn), fs->fs_bsize);
    427 				} else
    428 					ffs_blkfree(fs, oip->i_devvp, bn,
    429 					    fs->fs_bsize, oip->i_number);
    430 				blocksreleased += nblocks;
    431 			}
    432 		}
    433 		if (lastiblock[level] >= 0)
    434 			goto done;
    435 	}
    436 
    437 	/*
    438 	 * All whole direct blocks or frags.
    439 	 */
    440 	for (i = NDADDR - 1; i > lastblock; i--) {
    441 		long bsize;
    442 
    443 		if (oip->i_ump->um_fstype == UFS1)
    444 			bn = ufs_rw32(oip->i_ffs1_db[i], UFS_FSNEEDSWAP(fs));
    445 		else
    446 			bn = ufs_rw64(oip->i_ffs2_db[i], UFS_FSNEEDSWAP(fs));
    447 		if (bn == 0)
    448 			continue;
    449 		DIP_ASSIGN(oip, db[i], 0);
    450 		bsize = blksize(fs, oip, i);
    451 		if ((oip->i_ump->um_mountp->mnt_wapbl) &&
    452 		    (ovp->v_type != VREG)) {
    453 			UFS_WAPBL_REGISTER_DEALLOCATION(oip->i_ump->um_mountp,
    454 			    fsbtodb(fs, bn), bsize);
    455 		} else
    456 			ffs_blkfree(fs, oip->i_devvp, bn, bsize, oip->i_number);
    457 		blocksreleased += btodb(bsize);
    458 	}
    459 	if (lastblock < 0)
    460 		goto done;
    461 
    462 	/*
    463 	 * Finally, look for a change in size of the
    464 	 * last direct block; release any frags.
    465 	 */
    466 	if (oip->i_ump->um_fstype == UFS1)
    467 		bn = ufs_rw32(oip->i_ffs1_db[lastblock], UFS_FSNEEDSWAP(fs));
    468 	else
    469 		bn = ufs_rw64(oip->i_ffs2_db[lastblock], UFS_FSNEEDSWAP(fs));
    470 	if (bn != 0) {
    471 		long oldspace, newspace;
    472 
    473 		/*
    474 		 * Calculate amount of space we're giving
    475 		 * back as old block size minus new block size.
    476 		 */
    477 		oldspace = blksize(fs, oip, lastblock);
    478 		oip->i_size = length;
    479 		DIP_ASSIGN(oip, size, length);
    480 		newspace = blksize(fs, oip, lastblock);
    481 		if (newspace == 0)
    482 			panic("itrunc: newspace");
    483 		if (oldspace - newspace > 0) {
    484 			/*
    485 			 * Block number of space to be free'd is
    486 			 * the old block # plus the number of frags
    487 			 * required for the storage we're keeping.
    488 			 */
    489 			bn += numfrags(fs, newspace);
    490 			if ((oip->i_ump->um_mountp->mnt_wapbl) &&
    491 			    (ovp->v_type != VREG)) {
    492 				UFS_WAPBL_REGISTER_DEALLOCATION(
    493 				    oip->i_ump->um_mountp, fsbtodb(fs, bn),
    494 				    oldspace - newspace);
    495 			} else
    496 				ffs_blkfree(fs, oip->i_devvp, bn,
    497 				    oldspace - newspace, oip->i_number);
    498 			blocksreleased += btodb(oldspace - newspace);
    499 		}
    500 	}
    501 
    502 done:
    503 #ifdef DIAGNOSTIC
    504 	for (level = SINGLE; level <= TRIPLE; level++)
    505 		if (blks[NDADDR + level] != DIP(oip, ib[level]))
    506 			panic("itrunc1");
    507 	for (i = 0; i < NDADDR; i++)
    508 		if (blks[i] != DIP(oip, db[i]))
    509 			panic("itrunc2");
    510 	if (length == 0 &&
    511 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
    512 		panic("itrunc3");
    513 #endif /* DIAGNOSTIC */
    514 	/*
    515 	 * Put back the real size.
    516 	 */
    517 	oip->i_size = length;
    518 	DIP_ASSIGN(oip, size, length);
    519 	DIP_ADD(oip, blocks, -blocksreleased);
    520 	genfs_node_unlock(ovp);
    521 	oip->i_flag |= IN_CHANGE;
    522 	UFS_WAPBL_UPDATE(ovp, NULL, NULL, 0);
    523 #if defined(QUOTA) || defined(QUOTA2)
    524 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
    525 #endif
    526 	KASSERT(ovp->v_type != VREG || ovp->v_size == oip->i_size);
    527 	return (allerror);
    528 }
    529 
    530 /*
    531  * Release blocks associated with the inode ip and stored in the indirect
    532  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    533  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    534  * and recursive calls to indirtrunc must be used to cleanse other indirect
    535  * blocks.
    536  *
    537  * NB: triple indirect blocks are untested.
    538  */
    539 static int
    540 ffs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn, daddr_t lastbn,
    541     int level, int64_t *countp)
    542 {
    543 	int i;
    544 	struct buf *bp;
    545 	struct fs *fs = ip->i_fs;
    546 	int32_t *bap1 = NULL;
    547 	int64_t *bap2 = NULL;
    548 	struct vnode *vp;
    549 	daddr_t nb, nlbn, last;
    550 	char *copy = NULL;
    551 	int64_t blkcount, factor, blocksreleased = 0;
    552 	int nblocks;
    553 	int error = 0, allerror = 0;
    554 #ifdef FFS_EI
    555 	const int needswap = UFS_FSNEEDSWAP(fs);
    556 #endif
    557 #define RBAP(ip, i) (((ip)->i_ump->um_fstype == UFS1) ? \
    558 	    ufs_rw32(bap1[i], needswap) : ufs_rw64(bap2[i], needswap))
    559 #define BAP_ASSIGN(ip, i, value)					\
    560 	do {								\
    561 		if ((ip)->i_ump->um_fstype == UFS1)			\
    562 			bap1[i] = (value);				\
    563 		else							\
    564 			bap2[i] = (value);				\
    565 	} while(0)
    566 
    567 	/*
    568 	 * Calculate index in current block of last
    569 	 * block to be kept.  -1 indicates the entire
    570 	 * block so we need not calculate the index.
    571 	 */
    572 	factor = 1;
    573 	for (i = SINGLE; i < level; i++)
    574 		factor *= NINDIR(fs);
    575 	last = lastbn;
    576 	if (lastbn > 0)
    577 		last /= factor;
    578 	nblocks = btodb(fs->fs_bsize);
    579 	/*
    580 	 * Get buffer of block pointers, zero those entries corresponding
    581 	 * to blocks to be free'd, and update on disk copy first.  Since
    582 	 * double(triple) indirect before single(double) indirect, calls
    583 	 * to bmap on these blocks will fail.  However, we already have
    584 	 * the on disk address, so we have to set the b_blkno field
    585 	 * explicitly instead of letting bread do everything for us.
    586 	 */
    587 	vp = ITOV(ip);
    588 	error = ffs_getblk(vp, lbn, FFS_NOBLK, fs->fs_bsize, false, &bp);
    589 	if (error) {
    590 		*countp = 0;
    591 		return error;
    592 	}
    593 	if (bp->b_oflags & (BO_DONE | BO_DELWRI)) {
    594 		/* Braces must be here in case trace evaluates to nothing. */
    595 		trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
    596 	} else {
    597 		trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
    598 		curlwp->l_ru.ru_inblock++;	/* pay for read */
    599 		bp->b_flags |= B_READ;
    600 		bp->b_flags &= ~B_COWDONE;	/* we change blkno below */
    601 		if (bp->b_bcount > bp->b_bufsize)
    602 			panic("ffs_indirtrunc: bad buffer size");
    603 		bp->b_blkno = dbn;
    604 		BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
    605 		VOP_STRATEGY(vp, bp);
    606 		error = biowait(bp);
    607 		if (error == 0)
    608 			error = fscow_run(bp, true);
    609 	}
    610 	if (error) {
    611 		brelse(bp, 0);
    612 		*countp = 0;
    613 		return (error);
    614 	}
    615 
    616 	if (ip->i_ump->um_fstype == UFS1)
    617 		bap1 = (int32_t *)bp->b_data;
    618 	else
    619 		bap2 = (int64_t *)bp->b_data;
    620 	if (lastbn >= 0) {
    621 		copy = malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
    622 		memcpy((void *)copy, bp->b_data, (u_int)fs->fs_bsize);
    623 		for (i = last + 1; i < NINDIR(fs); i++)
    624 			BAP_ASSIGN(ip, i, 0);
    625 		error = bwrite(bp);
    626 		if (error)
    627 			allerror = error;
    628 		if (ip->i_ump->um_fstype == UFS1)
    629 			bap1 = (int32_t *)copy;
    630 		else
    631 			bap2 = (int64_t *)copy;
    632 	}
    633 
    634 	/*
    635 	 * Recursively free totally unused blocks.
    636 	 */
    637 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
    638 	    i--, nlbn += factor) {
    639 		nb = RBAP(ip, i);
    640 		if (nb == 0)
    641 			continue;
    642 		if (level > SINGLE) {
    643 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    644 					       (daddr_t)-1, level - 1,
    645 					       &blkcount);
    646 			if (error)
    647 				allerror = error;
    648 			blocksreleased += blkcount;
    649 		}
    650 		if ((ip->i_ump->um_mountp->mnt_wapbl) &&
    651 		    ((level > SINGLE) || (ITOV(ip)->v_type != VREG))) {
    652 			UFS_WAPBL_REGISTER_DEALLOCATION(ip->i_ump->um_mountp,
    653 			    fsbtodb(fs, nb), fs->fs_bsize);
    654 		} else
    655 			ffs_blkfree(fs, ip->i_devvp, nb, fs->fs_bsize,
    656 			    ip->i_number);
    657 		blocksreleased += nblocks;
    658 	}
    659 
    660 	/*
    661 	 * Recursively free last partial block.
    662 	 */
    663 	if (level > SINGLE && lastbn >= 0) {
    664 		last = lastbn % factor;
    665 		nb = RBAP(ip, i);
    666 		if (nb != 0) {
    667 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    668 					       last, level - 1, &blkcount);
    669 			if (error)
    670 				allerror = error;
    671 			blocksreleased += blkcount;
    672 		}
    673 	}
    674 
    675 	if (copy != NULL) {
    676 		free(copy, M_TEMP);
    677 	} else {
    678 		brelse(bp, BC_INVAL);
    679 	}
    680 
    681 	*countp = blocksreleased;
    682 	return (allerror);
    683 }
    684 
    685 void
    686 ffs_itimes(struct inode *ip, const struct timespec *acc,
    687     const struct timespec *mod, const struct timespec *cre)
    688 {
    689 	struct timespec now;
    690 
    691 	if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY))) {
    692 		return;
    693 	}
    694 
    695 	vfs_timestamp(&now);
    696 	if (ip->i_flag & IN_ACCESS) {
    697 		if (acc == NULL)
    698 			acc = &now;
    699 		DIP_ASSIGN(ip, atime, acc->tv_sec);
    700 		DIP_ASSIGN(ip, atimensec, acc->tv_nsec);
    701 	}
    702 	if (ip->i_flag & (IN_UPDATE | IN_MODIFY)) {
    703 		if ((ip->i_flags & SF_SNAPSHOT) == 0) {
    704 			if (mod == NULL)
    705 				mod = &now;
    706 			DIP_ASSIGN(ip, mtime, mod->tv_sec);
    707 			DIP_ASSIGN(ip, mtimensec, mod->tv_nsec);
    708 		}
    709 		ip->i_modrev++;
    710 	}
    711 	if (ip->i_flag & (IN_CHANGE | IN_MODIFY)) {
    712 		if (cre == NULL)
    713 			cre = &now;
    714 		DIP_ASSIGN(ip, ctime, cre->tv_sec);
    715 		DIP_ASSIGN(ip, ctimensec, cre->tv_nsec);
    716 	}
    717 	if (ip->i_flag & (IN_ACCESS | IN_MODIFY))
    718 		ip->i_flag |= IN_ACCESSED;
    719 	if (ip->i_flag & (IN_UPDATE | IN_CHANGE))
    720 		ip->i_flag |= IN_MODIFIED;
    721 	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY);
    722 }
    723