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