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