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