Home | History | Annotate | Line # | Download | only in ffs
ffs_balloc.c revision 1.34
      1  1.34       agc /*	$NetBSD: ffs_balloc.c,v 1.34 2003/08/07 16:34:29 agc Exp $	*/
      2   1.2       cgd 
      3   1.1   mycroft /*
      4  1.33      fvdl  * Copyright (c) 2002 Networks Associates Technology, Inc.
      5  1.33      fvdl  * All rights reserved.
      6  1.33      fvdl  *
      7  1.33      fvdl  * This software was developed for the FreeBSD Project by Marshall
      8  1.33      fvdl  * Kirk McKusick and Network Associates Laboratories, the Security
      9  1.33      fvdl  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
     10  1.33      fvdl  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
     11  1.33      fvdl  * research program
     12  1.33      fvdl  *
     13   1.1   mycroft  * Copyright (c) 1982, 1986, 1989, 1993
     14   1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
     15   1.1   mycroft  *
     16   1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     17   1.1   mycroft  * modification, are permitted provided that the following conditions
     18   1.1   mycroft  * are met:
     19   1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     20   1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     21   1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     22   1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     23   1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     24  1.34       agc  * 3. Neither the name of the University nor the names of its contributors
     25   1.1   mycroft  *    may be used to endorse or promote products derived from this software
     26   1.1   mycroft  *    without specific prior written permission.
     27   1.1   mycroft  *
     28   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29   1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30   1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31   1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32   1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33   1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34   1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35   1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36   1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37   1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38   1.1   mycroft  * SUCH DAMAGE.
     39   1.1   mycroft  *
     40   1.8      fvdl  *	@(#)ffs_balloc.c	8.8 (Berkeley) 6/16/95
     41   1.1   mycroft  */
     42  1.28     lukem 
     43  1.28     lukem #include <sys/cdefs.h>
     44  1.34       agc __KERNEL_RCSID(0, "$NetBSD: ffs_balloc.c,v 1.34 2003/08/07 16:34:29 agc Exp $");
     45   1.7       mrg 
     46  1.24       mrg #if defined(_KERNEL_OPT)
     47  1.10    scottr #include "opt_quota.h"
     48  1.11    scottr #endif
     49   1.1   mycroft 
     50   1.1   mycroft #include <sys/param.h>
     51   1.1   mycroft #include <sys/systm.h>
     52   1.1   mycroft #include <sys/buf.h>
     53   1.1   mycroft #include <sys/file.h>
     54  1.15      fvdl #include <sys/mount.h>
     55   1.1   mycroft #include <sys/vnode.h>
     56   1.9    bouyer #include <sys/mount.h>
     57   1.6       mrg 
     58   1.1   mycroft #include <ufs/ufs/quota.h>
     59   1.9    bouyer #include <ufs/ufs/ufsmount.h>
     60   1.1   mycroft #include <ufs/ufs/inode.h>
     61   1.1   mycroft #include <ufs/ufs/ufs_extern.h>
     62   1.9    bouyer #include <ufs/ufs/ufs_bswap.h>
     63   1.1   mycroft 
     64   1.1   mycroft #include <ufs/ffs/fs.h>
     65   1.1   mycroft #include <ufs/ffs/ffs_extern.h>
     66   1.1   mycroft 
     67  1.23       chs #include <uvm/uvm.h>
     68  1.23       chs 
     69  1.33      fvdl static int ffs_balloc_ufs1(void *);
     70  1.33      fvdl static int ffs_balloc_ufs2(void *);
     71  1.33      fvdl 
     72   1.1   mycroft /*
     73   1.1   mycroft  * Balloc defines the structure of file system storage
     74   1.1   mycroft  * by allocating the physical blocks on a device given
     75   1.1   mycroft  * the inode and the logical block number in a file.
     76   1.1   mycroft  */
     77  1.33      fvdl 
     78   1.3  christos int
     79  1.15      fvdl ffs_balloc(v)
     80  1.15      fvdl 	void *v;
     81  1.15      fvdl {
     82  1.33      fvdl 	struct vop_balloc_args *ap = v;
     83  1.33      fvdl 
     84  1.33      fvdl 	if (VTOI(ap->a_vp)->i_fs->fs_magic == FS_UFS2_MAGIC)
     85  1.33      fvdl 		return ffs_balloc_ufs2(v);
     86  1.33      fvdl 	else
     87  1.33      fvdl 		return ffs_balloc_ufs1(v);
     88  1.33      fvdl }
     89  1.33      fvdl 
     90  1.33      fvdl static int
     91  1.33      fvdl ffs_balloc_ufs1(v)
     92  1.33      fvdl 	void *v;
     93  1.33      fvdl {
     94  1.15      fvdl 	struct vop_balloc_args /* {
     95  1.15      fvdl 		struct vnode *a_vp;
     96  1.25     lukem 		off_t a_startoffset;
     97  1.15      fvdl 		int a_size;
     98  1.15      fvdl 		struct ucred *a_cred;
     99  1.15      fvdl 		int a_flags;
    100  1.23       chs 		struct buf **a_bpp;
    101  1.15      fvdl 	} */ *ap = v;
    102  1.33      fvdl 	daddr_t lbn, lastlbn;
    103   1.1   mycroft 	int size;
    104   1.1   mycroft 	struct ucred *cred;
    105   1.1   mycroft 	int flags;
    106  1.33      fvdl 	int32_t nb;
    107   1.1   mycroft 	struct buf *bp, *nbp;
    108  1.15      fvdl 	struct vnode *vp = ap->a_vp;
    109  1.15      fvdl 	struct inode *ip = VTOI(vp);
    110  1.15      fvdl 	struct fs *fs = ip->i_fs;
    111   1.1   mycroft 	struct indir indirs[NIADDR + 2];
    112  1.31      fvdl 	daddr_t newb, pref;
    113  1.31      fvdl 	int32_t *bap;	/* XXX ondisk32 */
    114   1.8      fvdl 	int deallocated, osize, nsize, num, i, error;
    115  1.33      fvdl 	int32_t *blkp, *allocblk, allociblk[NIADDR + 1];
    116  1.33      fvdl 	int32_t *allocib;
    117  1.17      fvdl 	int unwindidx = -1;
    118  1.23       chs 	struct buf **bpp = ap->a_bpp;
    119  1.15      fvdl #ifdef FFS_EI
    120  1.15      fvdl 	const int needswap = UFS_FSNEEDSWAP(fs);
    121  1.15      fvdl #endif
    122  1.23       chs 	UVMHIST_FUNC("ffs_balloc"); UVMHIST_CALLED(ubchist);
    123   1.1   mycroft 
    124  1.15      fvdl 	lbn = lblkno(fs, ap->a_startoffset);
    125  1.15      fvdl 	size = blkoff(fs, ap->a_startoffset) + ap->a_size;
    126  1.15      fvdl 	if (size > fs->fs_bsize)
    127  1.15      fvdl 		panic("ffs_balloc: blk too big");
    128  1.23       chs 	if (bpp != NULL) {
    129  1.23       chs 		*bpp = NULL;
    130  1.23       chs 	}
    131  1.23       chs 	UVMHIST_LOG(ubchist, "vp %p lbn 0x%x size 0x%x", vp, lbn, size,0);
    132  1.23       chs 
    133  1.23       chs 	KASSERT(size <= fs->fs_bsize);
    134   1.8      fvdl 	if (lbn < 0)
    135   1.1   mycroft 		return (EFBIG);
    136  1.15      fvdl 	cred = ap->a_cred;
    137  1.15      fvdl 	flags = ap->a_flags;
    138   1.1   mycroft 
    139   1.1   mycroft 	/*
    140   1.1   mycroft 	 * If the next write will extend the file into a new block,
    141   1.1   mycroft 	 * and the file is currently composed of a fragment
    142   1.1   mycroft 	 * this fragment has to be extended to be a full block.
    143   1.1   mycroft 	 */
    144  1.23       chs 
    145  1.33      fvdl 	lastlbn = lblkno(fs, ip->i_size);
    146  1.33      fvdl 	if (lastlbn < NDADDR && lastlbn < lbn) {
    147  1.33      fvdl 		nb = lastlbn;
    148   1.1   mycroft 		osize = blksize(fs, ip, nb);
    149   1.1   mycroft 		if (osize < fs->fs_bsize && osize > 0) {
    150   1.1   mycroft 			error = ffs_realloccg(ip, nb,
    151  1.33      fvdl 				    ffs_blkpref_ufs1(ip, lastlbn, nb,
    152  1.33      fvdl 					&ip->i_ffs1_db[0]),
    153  1.33      fvdl 				    osize, (int)fs->fs_bsize, cred, bpp, &newb);
    154   1.1   mycroft 			if (error)
    155   1.1   mycroft 				return (error);
    156  1.15      fvdl 			if (DOINGSOFTDEP(vp))
    157  1.23       chs 				softdep_setup_allocdirect(ip, nb, newb,
    158  1.33      fvdl 				    ufs_rw32(ip->i_ffs1_db[nb], needswap),
    159  1.23       chs 				    fs->fs_bsize, osize, bpp ? *bpp : NULL);
    160  1.33      fvdl 			ip->i_size = lblktosize(fs, nb + 1);
    161  1.33      fvdl 			ip->i_ffs1_size = ip->i_size;
    162  1.33      fvdl 			uvm_vnp_setsize(vp, ip->i_ffs1_size);
    163  1.33      fvdl 			ip->i_ffs1_db[nb] = ufs_rw32((int32_t)newb, needswap);
    164   1.1   mycroft 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    165  1.23       chs 			if (bpp) {
    166  1.23       chs 				if (flags & B_SYNC)
    167  1.23       chs 					bwrite(*bpp);
    168  1.23       chs 				else
    169  1.23       chs 					bawrite(*bpp);
    170  1.23       chs 			}
    171   1.1   mycroft 		}
    172   1.1   mycroft 	}
    173  1.23       chs 
    174   1.1   mycroft 	/*
    175   1.1   mycroft 	 * The first NDADDR blocks are direct blocks
    176   1.1   mycroft 	 */
    177  1.23       chs 
    178   1.8      fvdl 	if (lbn < NDADDR) {
    179  1.33      fvdl 		nb = ufs_rw32(ip->i_ffs1_db[lbn], needswap);
    180  1.33      fvdl 		if (nb != 0 && ip->i_size >= lblktosize(fs, lbn + 1)) {
    181  1.23       chs 
    182  1.23       chs 			/*
    183  1.23       chs 			 * The block is an already-allocated direct block
    184  1.23       chs 			 * and the file already extends past this block,
    185  1.23       chs 			 * thus this must be a whole block.
    186  1.23       chs 			 * Just read the block (if requested).
    187  1.23       chs 			 */
    188  1.23       chs 
    189  1.23       chs 			if (bpp != NULL) {
    190  1.23       chs 				error = bread(vp, lbn, fs->fs_bsize, NOCRED,
    191  1.23       chs 					      bpp);
    192  1.23       chs 				if (error) {
    193  1.23       chs 					brelse(*bpp);
    194  1.23       chs 					return (error);
    195  1.23       chs 				}
    196   1.1   mycroft 			}
    197   1.1   mycroft 			return (0);
    198   1.1   mycroft 		}
    199   1.1   mycroft 		if (nb != 0) {
    200  1.23       chs 
    201   1.1   mycroft 			/*
    202   1.1   mycroft 			 * Consider need to reallocate a fragment.
    203   1.1   mycroft 			 */
    204  1.23       chs 
    205  1.33      fvdl 			osize = fragroundup(fs, blkoff(fs, ip->i_size));
    206   1.1   mycroft 			nsize = fragroundup(fs, size);
    207   1.1   mycroft 			if (nsize <= osize) {
    208  1.23       chs 
    209  1.23       chs 				/*
    210  1.23       chs 				 * The existing block is already
    211  1.23       chs 				 * at least as big as we want.
    212  1.23       chs 				 * Just read the block (if requested).
    213  1.23       chs 				 */
    214  1.23       chs 
    215  1.23       chs 				if (bpp != NULL) {
    216  1.23       chs 					error = bread(vp, lbn, osize, NOCRED,
    217  1.23       chs 						      bpp);
    218  1.23       chs 					if (error) {
    219  1.23       chs 						brelse(*bpp);
    220  1.23       chs 						return (error);
    221  1.23       chs 					}
    222   1.1   mycroft 				}
    223  1.23       chs 				return 0;
    224   1.1   mycroft 			} else {
    225  1.23       chs 
    226  1.23       chs 				/*
    227  1.23       chs 				 * The existing block is smaller than we want,
    228  1.23       chs 				 * grow it.
    229  1.23       chs 				 */
    230  1.23       chs 
    231   1.8      fvdl 				error = ffs_realloccg(ip, lbn,
    232  1.33      fvdl 				    ffs_blkpref_ufs1(ip, lbn, (int)lbn,
    233  1.33      fvdl 					&ip->i_ffs1_db[0]), osize, nsize, cred,
    234  1.23       chs 					bpp, &newb);
    235   1.1   mycroft 				if (error)
    236   1.1   mycroft 					return (error);
    237  1.15      fvdl 				if (DOINGSOFTDEP(vp))
    238  1.15      fvdl 					softdep_setup_allocdirect(ip, lbn,
    239  1.23       chs 					    newb, nb, nsize, osize,
    240  1.23       chs 					    bpp ? *bpp : NULL);
    241   1.1   mycroft 			}
    242   1.1   mycroft 		} else {
    243  1.23       chs 
    244  1.23       chs 			/*
    245  1.23       chs 			 * the block was not previously allocated,
    246  1.23       chs 			 * allocate a new block or fragment.
    247  1.23       chs 			 */
    248  1.23       chs 
    249  1.33      fvdl 			if (ip->i_size < lblktosize(fs, lbn + 1))
    250   1.1   mycroft 				nsize = fragroundup(fs, size);
    251   1.1   mycroft 			else
    252   1.1   mycroft 				nsize = fs->fs_bsize;
    253   1.8      fvdl 			error = ffs_alloc(ip, lbn,
    254  1.33      fvdl 			    ffs_blkpref_ufs1(ip, lbn, (int)lbn,
    255  1.33      fvdl 				&ip->i_ffs1_db[0]),
    256   1.8      fvdl 				nsize, cred, &newb);
    257   1.1   mycroft 			if (error)
    258   1.1   mycroft 				return (error);
    259  1.23       chs 			if (bpp != NULL) {
    260  1.23       chs 				bp = getblk(vp, lbn, nsize, 0, 0);
    261  1.23       chs 				bp->b_blkno = fsbtodb(fs, newb);
    262  1.23       chs 				if (flags & B_CLRBUF)
    263  1.23       chs 					clrbuf(bp);
    264  1.23       chs 				*bpp = bp;
    265  1.23       chs 			}
    266  1.23       chs 			if (DOINGSOFTDEP(vp)) {
    267  1.15      fvdl 				softdep_setup_allocdirect(ip, lbn, newb, 0,
    268  1.23       chs 				    nsize, 0, bpp ? *bpp : NULL);
    269  1.23       chs 			}
    270   1.1   mycroft 		}
    271  1.33      fvdl 		ip->i_ffs1_db[lbn] = ufs_rw32((int32_t)newb, needswap);
    272   1.1   mycroft 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    273   1.1   mycroft 		return (0);
    274   1.1   mycroft 	}
    275  1.29       chs 
    276   1.1   mycroft 	/*
    277   1.1   mycroft 	 * Determine the number of levels of indirection.
    278   1.1   mycroft 	 */
    279  1.29       chs 
    280   1.1   mycroft 	pref = 0;
    281   1.8      fvdl 	if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
    282  1.29       chs 		return (error);
    283  1.23       chs 
    284   1.1   mycroft 	/*
    285   1.1   mycroft 	 * Fetch the first indirect block allocating if necessary.
    286   1.1   mycroft 	 */
    287  1.29       chs 
    288   1.1   mycroft 	--num;
    289  1.33      fvdl 	nb = ufs_rw32(ip->i_ffs1_ib[indirs[0].in_off], needswap);
    290   1.8      fvdl 	allocib = NULL;
    291   1.8      fvdl 	allocblk = allociblk;
    292   1.1   mycroft 	if (nb == 0) {
    293  1.33      fvdl 		pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
    294  1.18   mycroft 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    295  1.18   mycroft 		    &newb);
    296   1.3  christos 		if (error)
    297  1.27       chs 			goto fail;
    298   1.1   mycroft 		nb = newb;
    299   1.8      fvdl 		*allocblk++ = nb;
    300   1.1   mycroft 		bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
    301   1.8      fvdl 		bp->b_blkno = fsbtodb(fs, nb);
    302   1.1   mycroft 		clrbuf(bp);
    303  1.15      fvdl 		if (DOINGSOFTDEP(vp)) {
    304  1.15      fvdl 			softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
    305  1.15      fvdl 			    newb, 0, fs->fs_bsize, 0, bp);
    306  1.15      fvdl 			bdwrite(bp);
    307  1.15      fvdl 		} else {
    308  1.29       chs 
    309  1.15      fvdl 			/*
    310  1.15      fvdl 			 * Write synchronously so that indirect blocks
    311  1.15      fvdl 			 * never point at garbage.
    312  1.15      fvdl 			 */
    313  1.29       chs 
    314  1.15      fvdl 			if ((error = bwrite(bp)) != 0)
    315  1.15      fvdl 				goto fail;
    316  1.15      fvdl 		}
    317  1.18   mycroft 		unwindidx = 0;
    318  1.33      fvdl 		allocib = &ip->i_ffs1_ib[indirs[0].in_off];
    319  1.33      fvdl 		*allocib = ufs_rw32(nb, needswap);
    320   1.1   mycroft 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    321   1.1   mycroft 	}
    322  1.29       chs 
    323   1.1   mycroft 	/*
    324   1.1   mycroft 	 * Fetch through the indirect blocks, allocating as necessary.
    325   1.1   mycroft 	 */
    326  1.29       chs 
    327   1.1   mycroft 	for (i = 1;;) {
    328   1.1   mycroft 		error = bread(vp,
    329   1.1   mycroft 		    indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
    330   1.1   mycroft 		if (error) {
    331   1.1   mycroft 			brelse(bp);
    332   1.8      fvdl 			goto fail;
    333   1.1   mycroft 		}
    334  1.31      fvdl 		bap = (int32_t *)bp->b_data;	/* XXX ondisk32 */
    335  1.15      fvdl 		nb = ufs_rw32(bap[indirs[i].in_off], needswap);
    336   1.1   mycroft 		if (i == num)
    337   1.1   mycroft 			break;
    338  1.18   mycroft 		i++;
    339   1.1   mycroft 		if (nb != 0) {
    340   1.1   mycroft 			brelse(bp);
    341   1.1   mycroft 			continue;
    342   1.1   mycroft 		}
    343   1.1   mycroft 		if (pref == 0)
    344  1.33      fvdl 			pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
    345   1.3  christos 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    346  1.18   mycroft 		    &newb);
    347   1.3  christos 		if (error) {
    348   1.1   mycroft 			brelse(bp);
    349   1.8      fvdl 			goto fail;
    350   1.1   mycroft 		}
    351   1.1   mycroft 		nb = newb;
    352   1.8      fvdl 		*allocblk++ = nb;
    353   1.1   mycroft 		nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
    354   1.1   mycroft 		nbp->b_blkno = fsbtodb(fs, nb);
    355   1.1   mycroft 		clrbuf(nbp);
    356  1.15      fvdl 		if (DOINGSOFTDEP(vp)) {
    357  1.15      fvdl 			softdep_setup_allocindir_meta(nbp, ip, bp,
    358  1.15      fvdl 			    indirs[i - 1].in_off, nb);
    359  1.15      fvdl 			bdwrite(nbp);
    360  1.15      fvdl 		} else {
    361  1.29       chs 
    362  1.15      fvdl 			/*
    363  1.15      fvdl 			 * Write synchronously so that indirect blocks
    364  1.15      fvdl 			 * never point at garbage.
    365  1.15      fvdl 			 */
    366  1.29       chs 
    367  1.15      fvdl 			if ((error = bwrite(nbp)) != 0) {
    368  1.15      fvdl 				brelse(bp);
    369  1.15      fvdl 				goto fail;
    370  1.15      fvdl 			}
    371   1.1   mycroft 		}
    372  1.18   mycroft 		if (unwindidx < 0)
    373  1.18   mycroft 			unwindidx = i - 1;
    374  1.33      fvdl 		bap[indirs[i - 1].in_off] = ufs_rw32(nb, needswap);
    375  1.29       chs 
    376   1.1   mycroft 		/*
    377   1.1   mycroft 		 * If required, write synchronously, otherwise use
    378   1.1   mycroft 		 * delayed write.
    379   1.1   mycroft 		 */
    380  1.29       chs 
    381   1.1   mycroft 		if (flags & B_SYNC) {
    382   1.1   mycroft 			bwrite(bp);
    383   1.1   mycroft 		} else {
    384   1.1   mycroft 			bdwrite(bp);
    385   1.1   mycroft 		}
    386   1.1   mycroft 	}
    387  1.29       chs 
    388   1.1   mycroft 	/*
    389   1.1   mycroft 	 * Get the data block, allocating if necessary.
    390   1.1   mycroft 	 */
    391  1.29       chs 
    392   1.1   mycroft 	if (nb == 0) {
    393  1.33      fvdl 		pref = ffs_blkpref_ufs1(ip, lbn, indirs[num].in_off, &bap[0]);
    394   1.3  christos 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    395  1.18   mycroft 		    &newb);
    396   1.3  christos 		if (error) {
    397   1.1   mycroft 			brelse(bp);
    398   1.8      fvdl 			goto fail;
    399   1.1   mycroft 		}
    400   1.1   mycroft 		nb = newb;
    401   1.8      fvdl 		*allocblk++ = nb;
    402  1.23       chs 		if (bpp != NULL) {
    403  1.23       chs 			nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    404  1.23       chs 			nbp->b_blkno = fsbtodb(fs, nb);
    405  1.23       chs 			if (flags & B_CLRBUF)
    406  1.23       chs 				clrbuf(nbp);
    407  1.23       chs 			*bpp = nbp;
    408  1.23       chs 		}
    409  1.15      fvdl 		if (DOINGSOFTDEP(vp))
    410  1.15      fvdl 			softdep_setup_allocindir_page(ip, lbn, bp,
    411  1.23       chs 			    indirs[num].in_off, nb, 0, bpp ? *bpp : NULL);
    412  1.33      fvdl 		bap[indirs[num].in_off] = ufs_rw32(nb, needswap);
    413  1.23       chs 		if (allocib == NULL && unwindidx < 0) {
    414  1.23       chs 			unwindidx = i - 1;
    415  1.23       chs 		}
    416  1.29       chs 
    417   1.1   mycroft 		/*
    418   1.1   mycroft 		 * If required, write synchronously, otherwise use
    419   1.1   mycroft 		 * delayed write.
    420   1.1   mycroft 		 */
    421  1.29       chs 
    422   1.1   mycroft 		if (flags & B_SYNC) {
    423   1.1   mycroft 			bwrite(bp);
    424   1.1   mycroft 		} else {
    425   1.1   mycroft 			bdwrite(bp);
    426   1.1   mycroft 		}
    427   1.1   mycroft 		return (0);
    428   1.1   mycroft 	}
    429   1.1   mycroft 	brelse(bp);
    430  1.23       chs 	if (bpp != NULL) {
    431  1.23       chs 		if (flags & B_CLRBUF) {
    432  1.23       chs 			error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
    433  1.23       chs 			if (error) {
    434  1.23       chs 				brelse(nbp);
    435  1.23       chs 				goto fail;
    436  1.23       chs 			}
    437  1.23       chs 		} else {
    438  1.23       chs 			nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    439  1.23       chs 			nbp->b_blkno = fsbtodb(fs, nb);
    440  1.23       chs 			clrbuf(nbp);
    441   1.1   mycroft 		}
    442  1.23       chs 		*bpp = nbp;
    443   1.1   mycroft 	}
    444   1.1   mycroft 	return (0);
    445  1.27       chs 
    446   1.8      fvdl fail:
    447  1.27       chs 	/*
    448  1.29       chs 	 * If we have failed part way through block allocation, we
    449  1.29       chs 	 * have to deallocate any indirect blocks that we have allocated.
    450  1.27       chs 	 */
    451  1.27       chs 
    452  1.29       chs 	if (unwindidx >= 0) {
    453  1.27       chs 
    454  1.29       chs 		/*
    455  1.29       chs 		 * First write out any buffers we've created to resolve their
    456  1.29       chs 		 * softdeps.  This must be done in reverse order of creation
    457  1.29       chs 		 * so that we resolve the dependencies in one pass.
    458  1.29       chs 		 * Write the cylinder group buffers for these buffers too.
    459  1.29       chs 		 */
    460  1.29       chs 
    461  1.29       chs 		for (i = num; i >= unwindidx; i--) {
    462  1.29       chs 			if (i == 0) {
    463  1.29       chs 				break;
    464  1.29       chs 			}
    465  1.29       chs 			bp = getblk(vp, indirs[i].in_lbn, (int)fs->fs_bsize, 0,
    466  1.29       chs 			    0);
    467  1.29       chs 			if (bp->b_flags & B_DELWRI) {
    468  1.29       chs 				nb = fsbtodb(fs, cgtod(fs, dtog(fs,
    469  1.30       chs 				    dbtofsb(fs, bp->b_blkno))));
    470  1.29       chs 				bwrite(bp);
    471  1.29       chs 				bp = getblk(ip->i_devvp, nb, (int)fs->fs_cgsize,
    472  1.29       chs 				    0, 0);
    473  1.29       chs 				if (bp->b_flags & B_DELWRI) {
    474  1.29       chs 					bwrite(bp);
    475  1.29       chs 				} else {
    476  1.29       chs 					bp->b_flags |= B_INVAL;
    477  1.29       chs 					brelse(bp);
    478  1.29       chs 				}
    479  1.29       chs 			} else {
    480  1.29       chs 				bp->b_flags |= B_INVAL;
    481  1.29       chs 				brelse(bp);
    482  1.29       chs 			}
    483  1.29       chs 		}
    484  1.29       chs 		if (unwindidx == 0) {
    485  1.29       chs 			ip->i_flag |= IN_MODIFIED | IN_CHANGE | IN_UPDATE;
    486  1.29       chs 			VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
    487  1.27       chs 		}
    488  1.27       chs 
    489  1.29       chs 		/*
    490  1.29       chs 		 * Now that any dependencies that we created have been
    491  1.29       chs 		 * resolved, we can undo the partial allocation.
    492  1.29       chs 		 */
    493  1.27       chs 
    494  1.18   mycroft 		if (unwindidx == 0) {
    495  1.18   mycroft 			*allocib = 0;
    496  1.29       chs 			ip->i_flag |= IN_MODIFIED | IN_CHANGE | IN_UPDATE;
    497  1.29       chs 			VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
    498  1.17      fvdl 		} else {
    499  1.18   mycroft 			int r;
    500  1.29       chs 
    501  1.29       chs 			r = bread(vp, indirs[unwindidx].in_lbn,
    502  1.18   mycroft 			    (int)fs->fs_bsize, NOCRED, &bp);
    503  1.18   mycroft 			if (r) {
    504  1.18   mycroft 				panic("Could not unwind indirect block, error %d", r);
    505  1.18   mycroft 				brelse(bp);
    506  1.18   mycroft 			} else {
    507  1.31      fvdl 				bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
    508  1.18   mycroft 				bap[indirs[unwindidx].in_off] = 0;
    509  1.29       chs 				bwrite(bp);
    510  1.18   mycroft 			}
    511  1.17      fvdl 		}
    512  1.19   mycroft 		for (i = unwindidx + 1; i <= num; i++) {
    513  1.19   mycroft 			bp = getblk(vp, indirs[i].in_lbn, (int)fs->fs_bsize, 0,
    514  1.19   mycroft 			    0);
    515  1.19   mycroft 			bp->b_flags |= B_INVAL;
    516  1.19   mycroft 			brelse(bp);
    517  1.19   mycroft 		}
    518  1.17      fvdl 	}
    519  1.29       chs 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
    520  1.29       chs 		ffs_blkfree(ip, *blkp, fs->fs_bsize);
    521  1.29       chs 		deallocated += fs->fs_bsize;
    522  1.29       chs 	}
    523   1.8      fvdl 	if (deallocated) {
    524   1.8      fvdl #ifdef QUOTA
    525   1.8      fvdl 		/*
    526   1.8      fvdl 		 * Restore user's disk quota because allocation failed.
    527   1.8      fvdl 		 */
    528  1.33      fvdl 		(void)chkdq(ip, -btodb(deallocated), cred, FORCE);
    529  1.33      fvdl #endif
    530  1.33      fvdl 		ip->i_ffs1_blocks -= btodb(deallocated);
    531  1.33      fvdl 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    532  1.33      fvdl 	}
    533  1.33      fvdl 	return (error);
    534  1.33      fvdl }
    535  1.33      fvdl 
    536  1.33      fvdl static int
    537  1.33      fvdl ffs_balloc_ufs2(v)
    538  1.33      fvdl 	void *v;
    539  1.33      fvdl {
    540  1.33      fvdl 	struct vop_balloc_args /* {
    541  1.33      fvdl 		struct vnode *a_vp;
    542  1.33      fvdl 		off_t a_startoffset;
    543  1.33      fvdl 		int a_size;
    544  1.33      fvdl 		struct ucred *a_cred;
    545  1.33      fvdl 		int a_flags;
    546  1.33      fvdl 		struct buf **a_bpp;
    547  1.33      fvdl 	} */ *ap = v;
    548  1.33      fvdl 	daddr_t lbn, lastlbn;
    549  1.33      fvdl 	int size;
    550  1.33      fvdl 	struct ucred *cred;
    551  1.33      fvdl 	int flags;
    552  1.33      fvdl 	struct buf *bp, *nbp;
    553  1.33      fvdl 	struct vnode *vp = ap->a_vp;
    554  1.33      fvdl 	struct inode *ip = VTOI(vp);
    555  1.33      fvdl 	struct fs *fs = ip->i_fs;
    556  1.33      fvdl 	struct indir indirs[NIADDR + 2];
    557  1.33      fvdl 	daddr_t newb, pref, nb;
    558  1.33      fvdl 	int64_t *bap;
    559  1.33      fvdl 	int deallocated, osize, nsize, num, i, error;
    560  1.33      fvdl 	daddr_t *blkp, *allocblk, allociblk[NIADDR + 1];
    561  1.33      fvdl 	int64_t *allocib;
    562  1.33      fvdl 	int unwindidx = -1;
    563  1.33      fvdl 	struct buf **bpp = ap->a_bpp;
    564  1.33      fvdl #ifdef FFS_EI
    565  1.33      fvdl 	const int needswap = UFS_FSNEEDSWAP(fs);
    566  1.33      fvdl #endif
    567  1.33      fvdl 	UVMHIST_FUNC("ffs_balloc"); UVMHIST_CALLED(ubchist);
    568  1.33      fvdl 
    569  1.33      fvdl 	lbn = lblkno(fs, ap->a_startoffset);
    570  1.33      fvdl 	size = blkoff(fs, ap->a_startoffset) + ap->a_size;
    571  1.33      fvdl 	if (size > fs->fs_bsize)
    572  1.33      fvdl 		panic("ffs_balloc: blk too big");
    573  1.33      fvdl 	if (bpp != NULL) {
    574  1.33      fvdl 		*bpp = NULL;
    575  1.33      fvdl 	}
    576  1.33      fvdl 	UVMHIST_LOG(ubchist, "vp %p lbn 0x%x size 0x%x", vp, lbn, size,0);
    577  1.33      fvdl 
    578  1.33      fvdl 	KASSERT(size <= fs->fs_bsize);
    579  1.33      fvdl 	if (lbn < 0)
    580  1.33      fvdl 		return (EFBIG);
    581  1.33      fvdl 	cred = ap->a_cred;
    582  1.33      fvdl 	flags = ap->a_flags;
    583  1.33      fvdl 
    584  1.33      fvdl #ifdef notyet
    585  1.33      fvdl 	/*
    586  1.33      fvdl 	 * Check for allocating external data.
    587  1.33      fvdl 	 */
    588  1.33      fvdl 	if (flags & IO_EXT) {
    589  1.33      fvdl 		if (lbn >= NXADDR)
    590  1.33      fvdl 			return (EFBIG);
    591  1.33      fvdl 		/*
    592  1.33      fvdl 		 * If the next write will extend the data into a new block,
    593  1.33      fvdl 		 * and the data is currently composed of a fragment
    594  1.33      fvdl 		 * this fragment has to be extended to be a full block.
    595  1.33      fvdl 		 */
    596  1.33      fvdl 		lastlbn = lblkno(fs, dp->di_extsize);
    597  1.33      fvdl 		if (lastlbn < lbn) {
    598  1.33      fvdl 			nb = lastlbn;
    599  1.33      fvdl 			osize = sblksize(fs, dp->di_extsize, nb);
    600  1.33      fvdl 			if (osize < fs->fs_bsize && osize > 0) {
    601  1.33      fvdl 				error = ffs_realloccg(ip, -1 - nb,
    602  1.33      fvdl 				    dp->di_extb[nb],
    603  1.33      fvdl 				    ffs_blkpref_ufs2(ip, lastlbn, (int)nb,
    604  1.33      fvdl 				    &dp->di_extb[0]), osize,
    605  1.33      fvdl 				    (int)fs->fs_bsize, cred, &bp);
    606  1.33      fvdl 				if (error)
    607  1.33      fvdl 					return (error);
    608  1.33      fvdl 				if (DOINGSOFTDEP(vp))
    609  1.33      fvdl 					softdep_setup_allocext(ip, nb,
    610  1.33      fvdl 					    dbtofsb(fs, bp->b_blkno),
    611  1.33      fvdl 					    dp->di_extb[nb],
    612  1.33      fvdl 					    fs->fs_bsize, osize, bp);
    613  1.33      fvdl 				dp->di_extsize = smalllblktosize(fs, nb + 1);
    614  1.33      fvdl 				dp->di_extb[nb] = dbtofsb(fs, bp->b_blkno);
    615  1.33      fvdl 				bp->b_xflags |= BX_ALTDATA;
    616  1.33      fvdl 				ip->i_flag |= IN_CHANGE | IN_UPDATE;
    617  1.33      fvdl 				if (flags & IO_SYNC)
    618  1.33      fvdl 					bwrite(bp);
    619  1.33      fvdl 				else
    620  1.33      fvdl 					bawrite(bp);
    621  1.33      fvdl 			}
    622  1.33      fvdl 		}
    623  1.33      fvdl 		/*
    624  1.33      fvdl 		 * All blocks are direct blocks
    625  1.33      fvdl 		 */
    626  1.33      fvdl 		if (flags & BA_METAONLY)
    627  1.33      fvdl 			panic("ffs_balloc_ufs2: BA_METAONLY for ext block");
    628  1.33      fvdl 		nb = dp->di_extb[lbn];
    629  1.33      fvdl 		if (nb != 0 && dp->di_extsize >= smalllblktosize(fs, lbn + 1)) {
    630  1.33      fvdl 			error = bread(vp, -1 - lbn, fs->fs_bsize, NOCRED, &bp);
    631  1.33      fvdl 			if (error) {
    632  1.33      fvdl 				brelse(bp);
    633  1.33      fvdl 				return (error);
    634  1.33      fvdl 			}
    635  1.33      fvdl 			bp->b_blkno = fsbtodb(fs, nb);
    636  1.33      fvdl 			bp->b_xflags |= BX_ALTDATA;
    637  1.33      fvdl 			*bpp = bp;
    638  1.33      fvdl 			return (0);
    639  1.33      fvdl 		}
    640  1.33      fvdl 		if (nb != 0) {
    641  1.33      fvdl 			/*
    642  1.33      fvdl 			 * Consider need to reallocate a fragment.
    643  1.33      fvdl 			 */
    644  1.33      fvdl 			osize = fragroundup(fs, blkoff(fs, dp->di_extsize));
    645  1.33      fvdl 			nsize = fragroundup(fs, size);
    646  1.33      fvdl 			if (nsize <= osize) {
    647  1.33      fvdl 				error = bread(vp, -1 - lbn, osize, NOCRED, &bp);
    648  1.33      fvdl 				if (error) {
    649  1.33      fvdl 					brelse(bp);
    650  1.33      fvdl 					return (error);
    651  1.33      fvdl 				}
    652  1.33      fvdl 				bp->b_blkno = fsbtodb(fs, nb);
    653  1.33      fvdl 				bp->b_xflags |= BX_ALTDATA;
    654  1.33      fvdl 			} else {
    655  1.33      fvdl 				error = ffs_realloccg(ip, -1 - lbn,
    656  1.33      fvdl 				    dp->di_extb[lbn],
    657  1.33      fvdl 				    ffs_blkpref_ufs2(ip, lbn, (int)lbn,
    658  1.33      fvdl 				    &dp->di_extb[0]), osize, nsize, cred, &bp);
    659  1.33      fvdl 				if (error)
    660  1.33      fvdl 					return (error);
    661  1.33      fvdl 				bp->b_xflags |= BX_ALTDATA;
    662  1.33      fvdl 				if (DOINGSOFTDEP(vp))
    663  1.33      fvdl 					softdep_setup_allocext(ip, lbn,
    664  1.33      fvdl 					    dbtofsb(fs, bp->b_blkno), nb,
    665  1.33      fvdl 					    nsize, osize, bp);
    666  1.33      fvdl 			}
    667  1.33      fvdl 		} else {
    668  1.33      fvdl 			if (dp->di_extsize < smalllblktosize(fs, lbn + 1))
    669  1.33      fvdl 				nsize = fragroundup(fs, size);
    670  1.33      fvdl 			else
    671  1.33      fvdl 				nsize = fs->fs_bsize;
    672  1.33      fvdl 			error = ffs_alloc(ip, lbn,
    673  1.33      fvdl 			   ffs_blkpref_ufs2(ip, lbn, (int)lbn, &dp->di_extb[0]),
    674  1.33      fvdl 			   nsize, cred, &newb);
    675  1.33      fvdl 			if (error)
    676  1.33      fvdl 				return (error);
    677  1.33      fvdl 			bp = getblk(vp, -1 - lbn, nsize, 0, 0);
    678  1.33      fvdl 			bp->b_blkno = fsbtodb(fs, newb);
    679  1.33      fvdl 			bp->b_xflags |= BX_ALTDATA;
    680  1.33      fvdl 			if (flags & BA_CLRBUF)
    681  1.33      fvdl 				vfs_bio_clrbuf(bp);
    682  1.33      fvdl 			if (DOINGSOFTDEP(vp))
    683  1.33      fvdl 				softdep_setup_allocext(ip, lbn, newb, 0,
    684  1.33      fvdl 				    nsize, 0, bp);
    685  1.33      fvdl 		}
    686  1.33      fvdl 		dp->di_extb[lbn] = dbtofsb(fs, bp->b_blkno);
    687  1.33      fvdl 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    688  1.33      fvdl 		*bpp = bp;
    689  1.33      fvdl 		return (0);
    690  1.33      fvdl 	}
    691  1.33      fvdl #endif
    692  1.33      fvdl 	/*
    693  1.33      fvdl 	 * If the next write will extend the file into a new block,
    694  1.33      fvdl 	 * and the file is currently composed of a fragment
    695  1.33      fvdl 	 * this fragment has to be extended to be a full block.
    696  1.33      fvdl 	 */
    697  1.33      fvdl 
    698  1.33      fvdl 	lastlbn = lblkno(fs, ip->i_size);
    699  1.33      fvdl 	if (lastlbn < NDADDR && lastlbn < lbn) {
    700  1.33      fvdl 		nb = lastlbn;
    701  1.33      fvdl 		osize = blksize(fs, ip, nb);
    702  1.33      fvdl 		if (osize < fs->fs_bsize && osize > 0) {
    703  1.33      fvdl 			error = ffs_realloccg(ip, nb,
    704  1.33      fvdl 				    ffs_blkpref_ufs2(ip, lastlbn, nb,
    705  1.33      fvdl 					&ip->i_ffs2_db[0]),
    706  1.33      fvdl 				    osize, (int)fs->fs_bsize, cred, bpp, &newb);
    707  1.33      fvdl 			if (error)
    708  1.33      fvdl 				return (error);
    709  1.33      fvdl 			if (DOINGSOFTDEP(vp))
    710  1.33      fvdl 				softdep_setup_allocdirect(ip, nb, newb,
    711  1.33      fvdl 				    ufs_rw64(ip->i_ffs2_db[nb], needswap),
    712  1.33      fvdl 				    fs->fs_bsize, osize, bpp ? *bpp : NULL);
    713  1.33      fvdl 			ip->i_size = lblktosize(fs, nb + 1);
    714  1.33      fvdl 			ip->i_ffs2_size = ip->i_size;
    715  1.33      fvdl 			uvm_vnp_setsize(vp, ip->i_size);
    716  1.33      fvdl 			ip->i_ffs2_db[nb] = ufs_rw64(newb, needswap);
    717  1.33      fvdl 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    718  1.33      fvdl 			if (bpp) {
    719  1.33      fvdl 				if (flags & B_SYNC)
    720  1.33      fvdl 					bwrite(*bpp);
    721  1.33      fvdl 				else
    722  1.33      fvdl 					bawrite(*bpp);
    723  1.33      fvdl 			}
    724  1.33      fvdl 		}
    725  1.33      fvdl 	}
    726  1.33      fvdl 
    727  1.33      fvdl 	/*
    728  1.33      fvdl 	 * The first NDADDR blocks are direct blocks
    729  1.33      fvdl 	 */
    730  1.33      fvdl 
    731  1.33      fvdl 	if (lbn < NDADDR) {
    732  1.33      fvdl 		nb = ufs_rw64(ip->i_ffs2_db[lbn], needswap);
    733  1.33      fvdl 		if (nb != 0 && ip->i_size >= lblktosize(fs, lbn + 1)) {
    734  1.33      fvdl 
    735  1.33      fvdl 			/*
    736  1.33      fvdl 			 * The block is an already-allocated direct block
    737  1.33      fvdl 			 * and the file already extends past this block,
    738  1.33      fvdl 			 * thus this must be a whole block.
    739  1.33      fvdl 			 * Just read the block (if requested).
    740  1.33      fvdl 			 */
    741  1.33      fvdl 
    742  1.33      fvdl 			if (bpp != NULL) {
    743  1.33      fvdl 				error = bread(vp, lbn, fs->fs_bsize, NOCRED,
    744  1.33      fvdl 					      bpp);
    745  1.33      fvdl 				if (error) {
    746  1.33      fvdl 					brelse(*bpp);
    747  1.33      fvdl 					return (error);
    748  1.33      fvdl 				}
    749  1.33      fvdl 			}
    750  1.33      fvdl 			return (0);
    751  1.33      fvdl 		}
    752  1.33      fvdl 		if (nb != 0) {
    753  1.33      fvdl 
    754  1.33      fvdl 			/*
    755  1.33      fvdl 			 * Consider need to reallocate a fragment.
    756  1.33      fvdl 			 */
    757  1.33      fvdl 
    758  1.33      fvdl 			osize = fragroundup(fs, blkoff(fs, ip->i_size));
    759  1.33      fvdl 			nsize = fragroundup(fs, size);
    760  1.33      fvdl 			if (nsize <= osize) {
    761  1.33      fvdl 
    762  1.33      fvdl 				/*
    763  1.33      fvdl 				 * The existing block is already
    764  1.33      fvdl 				 * at least as big as we want.
    765  1.33      fvdl 				 * Just read the block (if requested).
    766  1.33      fvdl 				 */
    767  1.33      fvdl 
    768  1.33      fvdl 				if (bpp != NULL) {
    769  1.33      fvdl 					error = bread(vp, lbn, osize, NOCRED,
    770  1.33      fvdl 						      bpp);
    771  1.33      fvdl 					if (error) {
    772  1.33      fvdl 						brelse(*bpp);
    773  1.33      fvdl 						return (error);
    774  1.33      fvdl 					}
    775  1.33      fvdl 				}
    776  1.33      fvdl 				return 0;
    777  1.33      fvdl 			} else {
    778  1.33      fvdl 
    779  1.33      fvdl 				/*
    780  1.33      fvdl 				 * The existing block is smaller than we want,
    781  1.33      fvdl 				 * grow it.
    782  1.33      fvdl 				 */
    783  1.33      fvdl 
    784  1.33      fvdl 				error = ffs_realloccg(ip, lbn,
    785  1.33      fvdl 				    ffs_blkpref_ufs2(ip, lbn, (int)lbn,
    786  1.33      fvdl 					&ip->i_ffs2_db[0]), osize, nsize, cred,
    787  1.33      fvdl 					bpp, &newb);
    788  1.33      fvdl 				if (error)
    789  1.33      fvdl 					return (error);
    790  1.33      fvdl 				if (DOINGSOFTDEP(vp))
    791  1.33      fvdl 					softdep_setup_allocdirect(ip, lbn,
    792  1.33      fvdl 					    newb, nb, nsize, osize,
    793  1.33      fvdl 					    bpp ? *bpp : NULL);
    794  1.33      fvdl 			}
    795  1.33      fvdl 		} else {
    796  1.33      fvdl 
    797  1.33      fvdl 			/*
    798  1.33      fvdl 			 * the block was not previously allocated,
    799  1.33      fvdl 			 * allocate a new block or fragment.
    800  1.33      fvdl 			 */
    801  1.33      fvdl 
    802  1.33      fvdl 			if (ip->i_size < lblktosize(fs, lbn + 1))
    803  1.33      fvdl 				nsize = fragroundup(fs, size);
    804  1.33      fvdl 			else
    805  1.33      fvdl 				nsize = fs->fs_bsize;
    806  1.33      fvdl 			error = ffs_alloc(ip, lbn,
    807  1.33      fvdl 			    ffs_blkpref_ufs2(ip, lbn, (int)lbn,
    808  1.33      fvdl 				&ip->i_ffs2_db[0]), nsize, cred, &newb);
    809  1.33      fvdl 			if (error)
    810  1.33      fvdl 				return (error);
    811  1.33      fvdl 			if (bpp != NULL) {
    812  1.33      fvdl 				bp = getblk(vp, lbn, nsize, 0, 0);
    813  1.33      fvdl 				bp->b_blkno = fsbtodb(fs, newb);
    814  1.33      fvdl 				if (flags & B_CLRBUF)
    815  1.33      fvdl 					clrbuf(bp);
    816  1.33      fvdl 				*bpp = bp;
    817  1.33      fvdl 			}
    818  1.33      fvdl 			if (DOINGSOFTDEP(vp)) {
    819  1.33      fvdl 				softdep_setup_allocdirect(ip, lbn, newb, 0,
    820  1.33      fvdl 				    nsize, 0, bpp ? *bpp : NULL);
    821  1.33      fvdl 			}
    822  1.33      fvdl 		}
    823  1.33      fvdl 		ip->i_ffs2_db[lbn] = ufs_rw64(newb, needswap);
    824  1.33      fvdl 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    825  1.33      fvdl 		return (0);
    826  1.33      fvdl 	}
    827  1.33      fvdl 
    828  1.33      fvdl 	/*
    829  1.33      fvdl 	 * Determine the number of levels of indirection.
    830  1.33      fvdl 	 */
    831  1.33      fvdl 
    832  1.33      fvdl 	pref = 0;
    833  1.33      fvdl 	if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
    834  1.33      fvdl 		return (error);
    835  1.33      fvdl 
    836  1.33      fvdl 	/*
    837  1.33      fvdl 	 * Fetch the first indirect block allocating if necessary.
    838  1.33      fvdl 	 */
    839  1.33      fvdl 
    840  1.33      fvdl 	--num;
    841  1.33      fvdl 	nb = ufs_rw64(ip->i_ffs2_ib[indirs[0].in_off], needswap);
    842  1.33      fvdl 	allocib = NULL;
    843  1.33      fvdl 	allocblk = allociblk;
    844  1.33      fvdl 	if (nb == 0) {
    845  1.33      fvdl 		pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
    846  1.33      fvdl 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    847  1.33      fvdl 		    &newb);
    848  1.33      fvdl 		if (error)
    849  1.33      fvdl 			goto fail;
    850  1.33      fvdl 		nb = newb;
    851  1.33      fvdl 		*allocblk++ = nb;
    852  1.33      fvdl 		bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
    853  1.33      fvdl 		bp->b_blkno = fsbtodb(fs, nb);
    854  1.33      fvdl 		clrbuf(bp);
    855  1.33      fvdl 		if (DOINGSOFTDEP(vp)) {
    856  1.33      fvdl 			softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
    857  1.33      fvdl 			    newb, 0, fs->fs_bsize, 0, bp);
    858  1.33      fvdl 			bdwrite(bp);
    859  1.33      fvdl 		} else {
    860  1.33      fvdl 
    861  1.33      fvdl 			/*
    862  1.33      fvdl 			 * Write synchronously so that indirect blocks
    863  1.33      fvdl 			 * never point at garbage.
    864  1.33      fvdl 			 */
    865  1.33      fvdl 
    866  1.33      fvdl 			if ((error = bwrite(bp)) != 0)
    867  1.33      fvdl 				goto fail;
    868  1.33      fvdl 		}
    869  1.33      fvdl 		unwindidx = 0;
    870  1.33      fvdl 		allocib = &ip->i_ffs2_ib[indirs[0].in_off];
    871  1.33      fvdl 		*allocib = ufs_rw64(nb, needswap);
    872  1.33      fvdl 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    873  1.33      fvdl 	}
    874  1.33      fvdl 
    875  1.33      fvdl 	/*
    876  1.33      fvdl 	 * Fetch through the indirect blocks, allocating as necessary.
    877  1.33      fvdl 	 */
    878  1.33      fvdl 
    879  1.33      fvdl 	for (i = 1;;) {
    880  1.33      fvdl 		error = bread(vp,
    881  1.33      fvdl 		    indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
    882  1.33      fvdl 		if (error) {
    883  1.33      fvdl 			brelse(bp);
    884  1.33      fvdl 			goto fail;
    885  1.33      fvdl 		}
    886  1.33      fvdl 		bap = (int64_t *)bp->b_data;
    887  1.33      fvdl 		nb = ufs_rw64(bap[indirs[i].in_off], needswap);
    888  1.33      fvdl 		if (i == num)
    889  1.33      fvdl 			break;
    890  1.33      fvdl 		i++;
    891  1.33      fvdl 		if (nb != 0) {
    892  1.33      fvdl 			brelse(bp);
    893  1.33      fvdl 			continue;
    894  1.33      fvdl 		}
    895  1.33      fvdl 		if (pref == 0)
    896  1.33      fvdl 			pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
    897  1.33      fvdl 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    898  1.33      fvdl 		    &newb);
    899  1.33      fvdl 		if (error) {
    900  1.33      fvdl 			brelse(bp);
    901  1.33      fvdl 			goto fail;
    902  1.33      fvdl 		}
    903  1.33      fvdl 		nb = newb;
    904  1.33      fvdl 		*allocblk++ = nb;
    905  1.33      fvdl 		nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
    906  1.33      fvdl 		nbp->b_blkno = fsbtodb(fs, nb);
    907  1.33      fvdl 		clrbuf(nbp);
    908  1.33      fvdl 		if (DOINGSOFTDEP(vp)) {
    909  1.33      fvdl 			softdep_setup_allocindir_meta(nbp, ip, bp,
    910  1.33      fvdl 			    indirs[i - 1].in_off, nb);
    911  1.33      fvdl 			bdwrite(nbp);
    912  1.33      fvdl 		} else {
    913  1.33      fvdl 
    914  1.33      fvdl 			/*
    915  1.33      fvdl 			 * Write synchronously so that indirect blocks
    916  1.33      fvdl 			 * never point at garbage.
    917  1.33      fvdl 			 */
    918  1.33      fvdl 
    919  1.33      fvdl 			if ((error = bwrite(nbp)) != 0) {
    920  1.33      fvdl 				brelse(bp);
    921  1.33      fvdl 				goto fail;
    922  1.33      fvdl 			}
    923  1.33      fvdl 		}
    924  1.33      fvdl 		if (unwindidx < 0)
    925  1.33      fvdl 			unwindidx = i - 1;
    926  1.33      fvdl 		bap[indirs[i - 1].in_off] = ufs_rw64(nb, needswap);
    927  1.33      fvdl 
    928  1.33      fvdl 		/*
    929  1.33      fvdl 		 * If required, write synchronously, otherwise use
    930  1.33      fvdl 		 * delayed write.
    931  1.33      fvdl 		 */
    932  1.33      fvdl 
    933  1.33      fvdl 		if (flags & B_SYNC) {
    934  1.33      fvdl 			bwrite(bp);
    935  1.33      fvdl 		} else {
    936  1.33      fvdl 			bdwrite(bp);
    937  1.33      fvdl 		}
    938  1.33      fvdl 	}
    939  1.33      fvdl 
    940  1.33      fvdl 	/*
    941  1.33      fvdl 	 * Get the data block, allocating if necessary.
    942  1.33      fvdl 	 */
    943  1.33      fvdl 
    944  1.33      fvdl 	if (nb == 0) {
    945  1.33      fvdl 		pref = ffs_blkpref_ufs2(ip, lbn, indirs[num].in_off, &bap[0]);
    946  1.33      fvdl 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    947  1.33      fvdl 		    &newb);
    948  1.33      fvdl 		if (error) {
    949  1.33      fvdl 			brelse(bp);
    950  1.33      fvdl 			goto fail;
    951  1.33      fvdl 		}
    952  1.33      fvdl 		nb = newb;
    953  1.33      fvdl 		*allocblk++ = nb;
    954  1.33      fvdl 		if (bpp != NULL) {
    955  1.33      fvdl 			nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    956  1.33      fvdl 			nbp->b_blkno = fsbtodb(fs, nb);
    957  1.33      fvdl 			if (flags & B_CLRBUF)
    958  1.33      fvdl 				clrbuf(nbp);
    959  1.33      fvdl 			*bpp = nbp;
    960  1.33      fvdl 		}
    961  1.33      fvdl 		if (DOINGSOFTDEP(vp))
    962  1.33      fvdl 			softdep_setup_allocindir_page(ip, lbn, bp,
    963  1.33      fvdl 			    indirs[num].in_off, nb, 0, bpp ? *bpp : NULL);
    964  1.33      fvdl 		bap[indirs[num].in_off] = ufs_rw64(nb, needswap);
    965  1.33      fvdl 		if (allocib == NULL && unwindidx < 0) {
    966  1.33      fvdl 			unwindidx = i - 1;
    967  1.33      fvdl 		}
    968  1.33      fvdl 
    969  1.33      fvdl 		/*
    970  1.33      fvdl 		 * If required, write synchronously, otherwise use
    971  1.33      fvdl 		 * delayed write.
    972  1.33      fvdl 		 */
    973  1.33      fvdl 
    974  1.33      fvdl 		if (flags & B_SYNC) {
    975  1.33      fvdl 			bwrite(bp);
    976  1.33      fvdl 		} else {
    977  1.33      fvdl 			bdwrite(bp);
    978  1.33      fvdl 		}
    979  1.33      fvdl 		return (0);
    980  1.33      fvdl 	}
    981  1.33      fvdl 	brelse(bp);
    982  1.33      fvdl 	if (bpp != NULL) {
    983  1.33      fvdl 		if (flags & B_CLRBUF) {
    984  1.33      fvdl 			error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
    985  1.33      fvdl 			if (error) {
    986  1.33      fvdl 				brelse(nbp);
    987  1.33      fvdl 				goto fail;
    988  1.33      fvdl 			}
    989  1.33      fvdl 		} else {
    990  1.33      fvdl 			nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    991  1.33      fvdl 			nbp->b_blkno = fsbtodb(fs, nb);
    992  1.33      fvdl 			clrbuf(nbp);
    993  1.33      fvdl 		}
    994  1.33      fvdl 		*bpp = nbp;
    995  1.33      fvdl 	}
    996  1.33      fvdl 	return (0);
    997  1.33      fvdl 
    998  1.33      fvdl fail:
    999  1.33      fvdl 	/*
   1000  1.33      fvdl 	 * If we have failed part way through block allocation, we
   1001  1.33      fvdl 	 * have to deallocate any indirect blocks that we have allocated.
   1002  1.33      fvdl 	 */
   1003  1.33      fvdl 
   1004  1.33      fvdl 	if (unwindidx >= 0) {
   1005  1.33      fvdl 
   1006  1.33      fvdl 		/*
   1007  1.33      fvdl 		 * First write out any buffers we've created to resolve their
   1008  1.33      fvdl 		 * softdeps.  This must be done in reverse order of creation
   1009  1.33      fvdl 		 * so that we resolve the dependencies in one pass.
   1010  1.33      fvdl 		 * Write the cylinder group buffers for these buffers too.
   1011  1.33      fvdl 		 */
   1012  1.33      fvdl 
   1013  1.33      fvdl 		for (i = num; i >= unwindidx; i--) {
   1014  1.33      fvdl 			if (i == 0) {
   1015  1.33      fvdl 				break;
   1016  1.33      fvdl 			}
   1017  1.33      fvdl 			bp = getblk(vp, indirs[i].in_lbn, (int)fs->fs_bsize, 0,
   1018  1.33      fvdl 			    0);
   1019  1.33      fvdl 			if (bp->b_flags & B_DELWRI) {
   1020  1.33      fvdl 				nb = fsbtodb(fs, cgtod(fs, dtog(fs,
   1021  1.33      fvdl 				    dbtofsb(fs, bp->b_blkno))));
   1022  1.33      fvdl 				bwrite(bp);
   1023  1.33      fvdl 				bp = getblk(ip->i_devvp, nb, (int)fs->fs_cgsize,
   1024  1.33      fvdl 				    0, 0);
   1025  1.33      fvdl 				if (bp->b_flags & B_DELWRI) {
   1026  1.33      fvdl 					bwrite(bp);
   1027  1.33      fvdl 				} else {
   1028  1.33      fvdl 					bp->b_flags |= B_INVAL;
   1029  1.33      fvdl 					brelse(bp);
   1030  1.33      fvdl 				}
   1031  1.33      fvdl 			} else {
   1032  1.33      fvdl 				bp->b_flags |= B_INVAL;
   1033  1.33      fvdl 				brelse(bp);
   1034  1.33      fvdl 			}
   1035  1.33      fvdl 		}
   1036  1.33      fvdl 		if (unwindidx == 0) {
   1037  1.33      fvdl 			ip->i_flag |= IN_MODIFIED | IN_CHANGE | IN_UPDATE;
   1038  1.33      fvdl 			VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
   1039  1.33      fvdl 		}
   1040  1.33      fvdl 
   1041  1.33      fvdl 		/*
   1042  1.33      fvdl 		 * Now that any dependencies that we created have been
   1043  1.33      fvdl 		 * resolved, we can undo the partial allocation.
   1044  1.33      fvdl 		 */
   1045  1.33      fvdl 
   1046  1.33      fvdl 		if (unwindidx == 0) {
   1047  1.33      fvdl 			*allocib = 0;
   1048  1.33      fvdl 			ip->i_flag |= IN_MODIFIED | IN_CHANGE | IN_UPDATE;
   1049  1.33      fvdl 			VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
   1050  1.33      fvdl 		} else {
   1051  1.33      fvdl 			int r;
   1052  1.33      fvdl 
   1053  1.33      fvdl 			r = bread(vp, indirs[unwindidx].in_lbn,
   1054  1.33      fvdl 			    (int)fs->fs_bsize, NOCRED, &bp);
   1055  1.33      fvdl 			if (r) {
   1056  1.33      fvdl 				panic("Could not unwind indirect block, error %d", r);
   1057  1.33      fvdl 				brelse(bp);
   1058  1.33      fvdl 			} else {
   1059  1.33      fvdl 				bap = (int64_t *)bp->b_data;
   1060  1.33      fvdl 				bap[indirs[unwindidx].in_off] = 0;
   1061  1.33      fvdl 				bwrite(bp);
   1062  1.33      fvdl 			}
   1063  1.33      fvdl 		}
   1064  1.33      fvdl 		for (i = unwindidx + 1; i <= num; i++) {
   1065  1.33      fvdl 			bp = getblk(vp, indirs[i].in_lbn, (int)fs->fs_bsize, 0,
   1066  1.33      fvdl 			    0);
   1067  1.33      fvdl 			bp->b_flags |= B_INVAL;
   1068  1.33      fvdl 			brelse(bp);
   1069  1.33      fvdl 		}
   1070  1.33      fvdl 	}
   1071  1.33      fvdl 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
   1072  1.33      fvdl 		ffs_blkfree(ip, *blkp, fs->fs_bsize);
   1073  1.33      fvdl 		deallocated += fs->fs_bsize;
   1074  1.33      fvdl 	}
   1075  1.33      fvdl 	if (deallocated) {
   1076  1.33      fvdl #ifdef QUOTA
   1077  1.33      fvdl 		/*
   1078  1.33      fvdl 		 * Restore user's disk quota because allocation failed.
   1079  1.33      fvdl 		 */
   1080  1.33      fvdl 		(void)chkdq(ip, -btodb(deallocated), cred, FORCE);
   1081   1.8      fvdl #endif
   1082  1.33      fvdl 		ip->i_ffs2_blocks -= btodb(deallocated);
   1083  1.13   mycroft 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
   1084   1.8      fvdl 	}
   1085   1.8      fvdl 	return (error);
   1086   1.1   mycroft }
   1087