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