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