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