Home | History | Annotate | Line # | Download | only in ffs
ffs_balloc.c revision 1.23.2.5
      1  1.23.2.5   nathanw /*	$NetBSD: ffs_balloc.c,v 1.23.2.5 2001/10/08 20:11:51 nathanw Exp $	*/
      2       1.2       cgd 
      3       1.1   mycroft /*
      4       1.1   mycroft  * Copyright (c) 1982, 1986, 1989, 1993
      5       1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
      6       1.1   mycroft  *
      7       1.1   mycroft  * Redistribution and use in source and binary forms, with or without
      8       1.1   mycroft  * modification, are permitted provided that the following conditions
      9       1.1   mycroft  * are met:
     10       1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     11       1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     12       1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     14       1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     15       1.1   mycroft  * 3. All advertising materials mentioning features or use of this software
     16       1.1   mycroft  *    must display the following acknowledgement:
     17       1.1   mycroft  *	This product includes software developed by the University of
     18       1.1   mycroft  *	California, Berkeley and its contributors.
     19       1.1   mycroft  * 4. Neither the name of the University nor the names of its contributors
     20       1.1   mycroft  *    may be used to endorse or promote products derived from this software
     21       1.1   mycroft  *    without specific prior written permission.
     22       1.1   mycroft  *
     23       1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24       1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27       1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28       1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29       1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30       1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31       1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32       1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33       1.1   mycroft  * SUCH DAMAGE.
     34       1.1   mycroft  *
     35       1.8      fvdl  *	@(#)ffs_balloc.c	8.8 (Berkeley) 6/16/95
     36       1.1   mycroft  */
     37       1.7       mrg 
     38  1.23.2.2   nathanw #if defined(_KERNEL_OPT)
     39      1.10    scottr #include "opt_quota.h"
     40      1.11    scottr #endif
     41       1.1   mycroft 
     42       1.1   mycroft #include <sys/param.h>
     43       1.1   mycroft #include <sys/systm.h>
     44       1.1   mycroft #include <sys/buf.h>
     45  1.23.2.1   nathanw #include <sys/lwp.h>
     46       1.1   mycroft #include <sys/proc.h>
     47       1.1   mycroft #include <sys/file.h>
     48      1.15      fvdl #include <sys/mount.h>
     49       1.1   mycroft #include <sys/vnode.h>
     50       1.9    bouyer #include <sys/mount.h>
     51       1.6       mrg 
     52       1.1   mycroft #include <ufs/ufs/quota.h>
     53       1.9    bouyer #include <ufs/ufs/ufsmount.h>
     54       1.1   mycroft #include <ufs/ufs/inode.h>
     55       1.1   mycroft #include <ufs/ufs/ufs_extern.h>
     56       1.9    bouyer #include <ufs/ufs/ufs_bswap.h>
     57       1.1   mycroft 
     58       1.1   mycroft #include <ufs/ffs/fs.h>
     59       1.1   mycroft #include <ufs/ffs/ffs_extern.h>
     60       1.1   mycroft 
     61      1.23       chs #include <uvm/uvm.h>
     62      1.23       chs 
     63       1.1   mycroft /*
     64       1.1   mycroft  * Balloc defines the structure of file system storage
     65       1.1   mycroft  * by allocating the physical blocks on a device given
     66       1.1   mycroft  * the inode and the logical block number in a file.
     67       1.1   mycroft  */
     68       1.3  christos int
     69      1.15      fvdl ffs_balloc(v)
     70      1.15      fvdl 	void *v;
     71      1.15      fvdl {
     72      1.15      fvdl 	struct vop_balloc_args /* {
     73      1.15      fvdl 		struct vnode *a_vp;
     74  1.23.2.3   nathanw 		off_t a_startoffset;
     75      1.15      fvdl 		int a_size;
     76      1.15      fvdl 		struct ucred *a_cred;
     77      1.15      fvdl 		int a_flags;
     78      1.23       chs 		struct buf **a_bpp;
     79      1.15      fvdl 	} */ *ap = v;
     80      1.15      fvdl 	ufs_daddr_t lbn;
     81       1.1   mycroft 	int size;
     82       1.1   mycroft 	struct ucred *cred;
     83       1.1   mycroft 	int flags;
     84      1.15      fvdl 	ufs_daddr_t nb;
     85       1.1   mycroft 	struct buf *bp, *nbp;
     86      1.15      fvdl 	struct vnode *vp = ap->a_vp;
     87      1.15      fvdl 	struct inode *ip = VTOI(vp);
     88      1.15      fvdl 	struct fs *fs = ip->i_fs;
     89       1.1   mycroft 	struct indir indirs[NIADDR + 2];
     90       1.8      fvdl 	ufs_daddr_t newb, *bap, pref;
     91       1.8      fvdl 	int deallocated, osize, nsize, num, i, error;
     92       1.8      fvdl 	ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
     93      1.17      fvdl 	int unwindidx = -1;
     94      1.23       chs 	struct buf **bpp = ap->a_bpp;
     95  1.23.2.5   nathanw 	off_t off;
     96      1.15      fvdl #ifdef FFS_EI
     97      1.15      fvdl 	const int needswap = UFS_FSNEEDSWAP(fs);
     98      1.15      fvdl #endif
     99      1.23       chs 	UVMHIST_FUNC("ffs_balloc"); UVMHIST_CALLED(ubchist);
    100       1.1   mycroft 
    101      1.15      fvdl 	lbn = lblkno(fs, ap->a_startoffset);
    102      1.15      fvdl 	size = blkoff(fs, ap->a_startoffset) + ap->a_size;
    103      1.15      fvdl 	if (size > fs->fs_bsize)
    104      1.15      fvdl 		panic("ffs_balloc: blk too big");
    105      1.23       chs 	if (bpp != NULL) {
    106      1.23       chs 		*bpp = NULL;
    107      1.23       chs 	}
    108      1.23       chs 	UVMHIST_LOG(ubchist, "vp %p lbn 0x%x size 0x%x", vp, lbn, size,0);
    109      1.23       chs 
    110      1.23       chs 	KASSERT(size <= fs->fs_bsize);
    111       1.8      fvdl 	if (lbn < 0)
    112       1.1   mycroft 		return (EFBIG);
    113      1.15      fvdl 	cred = ap->a_cred;
    114      1.15      fvdl 	flags = ap->a_flags;
    115       1.1   mycroft 
    116       1.1   mycroft 	/*
    117       1.1   mycroft 	 * If the next write will extend the file into a new block,
    118       1.1   mycroft 	 * and the file is currently composed of a fragment
    119       1.1   mycroft 	 * this fragment has to be extended to be a full block.
    120       1.1   mycroft 	 */
    121      1.23       chs 
    122       1.4    bouyer 	nb = lblkno(fs, ip->i_ffs_size);
    123       1.8      fvdl 	if (nb < NDADDR && nb < lbn) {
    124       1.1   mycroft 		osize = blksize(fs, ip, nb);
    125       1.1   mycroft 		if (osize < fs->fs_bsize && osize > 0) {
    126       1.1   mycroft 			error = ffs_realloccg(ip, nb,
    127       1.4    bouyer 				ffs_blkpref(ip, nb, (int)nb, &ip->i_ffs_db[0]),
    128      1.23       chs 				osize, (int)fs->fs_bsize, cred, bpp, &newb);
    129       1.1   mycroft 			if (error)
    130       1.1   mycroft 				return (error);
    131      1.15      fvdl 			if (DOINGSOFTDEP(vp))
    132      1.23       chs 				softdep_setup_allocdirect(ip, nb, newb,
    133      1.15      fvdl 				    ufs_rw32(ip->i_ffs_db[nb], needswap),
    134      1.23       chs 				    fs->fs_bsize, osize, bpp ? *bpp : NULL);
    135      1.23       chs 			ip->i_ffs_size = lblktosize(fs, nb + 1);
    136       1.6       mrg 			uvm_vnp_setsize(vp, ip->i_ffs_size);
    137      1.23       chs 			ip->i_ffs_db[nb] = ufs_rw32(newb, needswap);
    138       1.1   mycroft 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    139      1.23       chs 			if (bpp) {
    140      1.23       chs 				if (flags & B_SYNC)
    141      1.23       chs 					bwrite(*bpp);
    142      1.23       chs 				else
    143      1.23       chs 					bawrite(*bpp);
    144      1.23       chs 			}
    145       1.1   mycroft 		}
    146       1.1   mycroft 	}
    147      1.23       chs 
    148       1.1   mycroft 	/*
    149       1.1   mycroft 	 * The first NDADDR blocks are direct blocks
    150       1.1   mycroft 	 */
    151      1.23       chs 
    152       1.8      fvdl 	if (lbn < NDADDR) {
    153      1.15      fvdl 		nb = ufs_rw32(ip->i_ffs_db[lbn], needswap);
    154      1.23       chs 		if (nb != 0 && ip->i_ffs_size >= lblktosize(fs, lbn + 1)) {
    155      1.23       chs 
    156      1.23       chs 			/*
    157      1.23       chs 			 * The block is an already-allocated direct block
    158      1.23       chs 			 * and the file already extends past this block,
    159      1.23       chs 			 * thus this must be a whole block.
    160      1.23       chs 			 * Just read the block (if requested).
    161      1.23       chs 			 */
    162      1.23       chs 
    163      1.23       chs 			if (bpp != NULL) {
    164      1.23       chs 				error = bread(vp, lbn, fs->fs_bsize, NOCRED,
    165      1.23       chs 					      bpp);
    166      1.23       chs 				if (error) {
    167      1.23       chs 					brelse(*bpp);
    168      1.23       chs 					return (error);
    169      1.23       chs 				}
    170       1.1   mycroft 			}
    171       1.1   mycroft 			return (0);
    172       1.1   mycroft 		}
    173       1.1   mycroft 		if (nb != 0) {
    174      1.23       chs 
    175       1.1   mycroft 			/*
    176       1.1   mycroft 			 * Consider need to reallocate a fragment.
    177       1.1   mycroft 			 */
    178      1.23       chs 
    179       1.4    bouyer 			osize = fragroundup(fs, blkoff(fs, ip->i_ffs_size));
    180       1.1   mycroft 			nsize = fragroundup(fs, size);
    181       1.1   mycroft 			if (nsize <= osize) {
    182      1.23       chs 
    183      1.23       chs 				/*
    184      1.23       chs 				 * The existing block is already
    185      1.23       chs 				 * at least as big as we want.
    186      1.23       chs 				 * Just read the block (if requested).
    187      1.23       chs 				 */
    188      1.23       chs 
    189      1.23       chs 				if (bpp != NULL) {
    190      1.23       chs 					error = bread(vp, lbn, osize, NOCRED,
    191      1.23       chs 						      bpp);
    192      1.23       chs 					if (error) {
    193      1.23       chs 						brelse(*bpp);
    194      1.23       chs 						return (error);
    195      1.23       chs 					}
    196       1.1   mycroft 				}
    197      1.23       chs 				return 0;
    198       1.1   mycroft 			} else {
    199      1.23       chs 
    200      1.23       chs 				/*
    201      1.23       chs 				 * The existing block is smaller than we want,
    202      1.23       chs 				 * grow it.
    203      1.23       chs 				 */
    204      1.23       chs 
    205       1.8      fvdl 				error = ffs_realloccg(ip, lbn,
    206       1.8      fvdl 				    ffs_blkpref(ip, lbn, (int)lbn,
    207       1.8      fvdl 					&ip->i_ffs_db[0]), osize, nsize, cred,
    208      1.23       chs 					bpp, &newb);
    209       1.1   mycroft 				if (error)
    210       1.1   mycroft 					return (error);
    211      1.15      fvdl 				if (DOINGSOFTDEP(vp))
    212      1.15      fvdl 					softdep_setup_allocdirect(ip, lbn,
    213      1.23       chs 					    newb, nb, nsize, osize,
    214      1.23       chs 					    bpp ? *bpp : NULL);
    215       1.1   mycroft 			}
    216       1.1   mycroft 		} else {
    217      1.23       chs 
    218      1.23       chs 			/*
    219      1.23       chs 			 * the block was not previously allocated,
    220      1.23       chs 			 * allocate a new block or fragment.
    221      1.23       chs 			 */
    222      1.23       chs 
    223      1.23       chs 			if (ip->i_ffs_size < lblktosize(fs, lbn + 1))
    224       1.1   mycroft 				nsize = fragroundup(fs, size);
    225       1.1   mycroft 			else
    226       1.1   mycroft 				nsize = fs->fs_bsize;
    227       1.8      fvdl 			error = ffs_alloc(ip, lbn,
    228       1.8      fvdl 			    ffs_blkpref(ip, lbn, (int)lbn, &ip->i_ffs_db[0]),
    229       1.8      fvdl 				nsize, cred, &newb);
    230       1.1   mycroft 			if (error)
    231       1.1   mycroft 				return (error);
    232      1.23       chs 			if (bpp != NULL) {
    233      1.23       chs 				bp = getblk(vp, lbn, nsize, 0, 0);
    234      1.23       chs 				bp->b_blkno = fsbtodb(fs, newb);
    235      1.23       chs 				if (flags & B_CLRBUF)
    236      1.23       chs 					clrbuf(bp);
    237      1.23       chs 				*bpp = bp;
    238      1.23       chs 			}
    239      1.23       chs 			if (DOINGSOFTDEP(vp)) {
    240      1.15      fvdl 				softdep_setup_allocdirect(ip, lbn, newb, 0,
    241      1.23       chs 				    nsize, 0, bpp ? *bpp : NULL);
    242      1.23       chs 			}
    243       1.1   mycroft 		}
    244      1.23       chs 		ip->i_ffs_db[lbn] = ufs_rw32(newb, needswap);
    245       1.1   mycroft 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    246       1.1   mycroft 		return (0);
    247       1.1   mycroft 	}
    248       1.1   mycroft 	/*
    249       1.1   mycroft 	 * Determine the number of levels of indirection.
    250       1.1   mycroft 	 */
    251       1.1   mycroft 	pref = 0;
    252       1.8      fvdl 	if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
    253       1.1   mycroft 		return(error);
    254      1.23       chs 
    255       1.1   mycroft #ifdef DIAGNOSTIC
    256       1.1   mycroft 	if (num < 1)
    257       1.1   mycroft 		panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
    258       1.1   mycroft #endif
    259       1.1   mycroft 	/*
    260       1.1   mycroft 	 * Fetch the first indirect block allocating if necessary.
    261       1.1   mycroft 	 */
    262       1.1   mycroft 	--num;
    263      1.15      fvdl 	nb = ufs_rw32(ip->i_ffs_ib[indirs[0].in_off], needswap);
    264       1.8      fvdl 	allocib = NULL;
    265       1.8      fvdl 	allocblk = allociblk;
    266       1.1   mycroft 	if (nb == 0) {
    267       1.8      fvdl 		pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    268      1.18   mycroft 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    269      1.18   mycroft 		    &newb);
    270       1.3  christos 		if (error)
    271  1.23.2.5   nathanw 			goto fail;
    272       1.1   mycroft 		nb = newb;
    273       1.8      fvdl 		*allocblk++ = nb;
    274       1.1   mycroft 		bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
    275       1.8      fvdl 		bp->b_blkno = fsbtodb(fs, nb);
    276       1.1   mycroft 		clrbuf(bp);
    277      1.15      fvdl 		if (DOINGSOFTDEP(vp)) {
    278      1.15      fvdl 			softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
    279      1.15      fvdl 			    newb, 0, fs->fs_bsize, 0, bp);
    280      1.15      fvdl 			bdwrite(bp);
    281      1.15      fvdl 		} else {
    282      1.15      fvdl 			/*
    283      1.15      fvdl 			 * Write synchronously so that indirect blocks
    284      1.15      fvdl 			 * never point at garbage.
    285      1.15      fvdl 			 */
    286      1.15      fvdl 			if ((error = bwrite(bp)) != 0)
    287      1.15      fvdl 				goto fail;
    288      1.15      fvdl 		}
    289      1.18   mycroft 		unwindidx = 0;
    290       1.8      fvdl 		allocib = &ip->i_ffs_ib[indirs[0].in_off];
    291      1.15      fvdl 		*allocib = ufs_rw32(nb, needswap);
    292       1.1   mycroft 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    293       1.1   mycroft 	}
    294       1.1   mycroft 	/*
    295       1.1   mycroft 	 * Fetch through the indirect blocks, allocating as necessary.
    296       1.1   mycroft 	 */
    297       1.1   mycroft 	for (i = 1;;) {
    298       1.1   mycroft 		error = bread(vp,
    299       1.1   mycroft 		    indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
    300       1.1   mycroft 		if (error) {
    301       1.1   mycroft 			brelse(bp);
    302       1.8      fvdl 			goto fail;
    303       1.1   mycroft 		}
    304       1.8      fvdl 		bap = (ufs_daddr_t *)bp->b_data;
    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.1   mycroft 			brelse(bp);
    311       1.1   mycroft 			continue;
    312       1.1   mycroft 		}
    313       1.1   mycroft 		if (pref == 0)
    314       1.8      fvdl 			pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    315       1.3  christos 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    316      1.18   mycroft 		    &newb);
    317       1.3  christos 		if (error) {
    318       1.1   mycroft 			brelse(bp);
    319       1.8      fvdl 			goto fail;
    320       1.1   mycroft 		}
    321       1.1   mycroft 		nb = newb;
    322       1.8      fvdl 		*allocblk++ = nb;
    323       1.1   mycroft 		nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
    324       1.1   mycroft 		nbp->b_blkno = fsbtodb(fs, nb);
    325       1.1   mycroft 		clrbuf(nbp);
    326      1.15      fvdl 		if (DOINGSOFTDEP(vp)) {
    327      1.15      fvdl 			softdep_setup_allocindir_meta(nbp, ip, bp,
    328      1.15      fvdl 			    indirs[i - 1].in_off, nb);
    329      1.15      fvdl 			bdwrite(nbp);
    330      1.15      fvdl 		} else {
    331      1.15      fvdl 			/*
    332      1.15      fvdl 			 * Write synchronously so that indirect blocks
    333      1.15      fvdl 			 * never point at garbage.
    334      1.15      fvdl 			 */
    335      1.15      fvdl 			if ((error = bwrite(nbp)) != 0) {
    336      1.15      fvdl 				brelse(bp);
    337      1.15      fvdl 				goto fail;
    338      1.15      fvdl 			}
    339       1.1   mycroft 		}
    340      1.18   mycroft 		if (unwindidx < 0)
    341      1.18   mycroft 			unwindidx = i - 1;
    342      1.15      fvdl 		bap[indirs[i - 1].in_off] = ufs_rw32(nb, needswap);
    343       1.1   mycroft 		/*
    344       1.1   mycroft 		 * If required, write synchronously, otherwise use
    345       1.1   mycroft 		 * delayed write.
    346       1.1   mycroft 		 */
    347       1.1   mycroft 		if (flags & B_SYNC) {
    348       1.1   mycroft 			bwrite(bp);
    349       1.1   mycroft 		} else {
    350       1.1   mycroft 			bdwrite(bp);
    351       1.1   mycroft 		}
    352       1.1   mycroft 	}
    353       1.1   mycroft 	/*
    354       1.1   mycroft 	 * Get the data block, allocating if necessary.
    355       1.1   mycroft 	 */
    356       1.1   mycroft 	if (nb == 0) {
    357      1.19   mycroft 		pref = ffs_blkpref(ip, lbn, indirs[num].in_off, &bap[0]);
    358       1.3  christos 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    359      1.18   mycroft 		    &newb);
    360       1.3  christos 		if (error) {
    361       1.1   mycroft 			brelse(bp);
    362       1.8      fvdl 			goto fail;
    363       1.1   mycroft 		}
    364       1.1   mycroft 		nb = newb;
    365       1.8      fvdl 		*allocblk++ = nb;
    366      1.23       chs 		if (bpp != NULL) {
    367      1.23       chs 			nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    368      1.23       chs 			nbp->b_blkno = fsbtodb(fs, nb);
    369      1.23       chs 			if (flags & B_CLRBUF)
    370      1.23       chs 				clrbuf(nbp);
    371      1.23       chs 			*bpp = nbp;
    372      1.23       chs 		}
    373      1.15      fvdl 		if (DOINGSOFTDEP(vp))
    374      1.15      fvdl 			softdep_setup_allocindir_page(ip, lbn, bp,
    375      1.23       chs 			    indirs[num].in_off, nb, 0, bpp ? *bpp : NULL);
    376      1.19   mycroft 		bap[indirs[num].in_off] = ufs_rw32(nb, needswap);
    377      1.23       chs 		if (allocib == NULL && unwindidx < 0) {
    378      1.23       chs 			unwindidx = i - 1;
    379      1.23       chs 		}
    380       1.1   mycroft 		/*
    381       1.1   mycroft 		 * If required, write synchronously, otherwise use
    382       1.1   mycroft 		 * delayed write.
    383       1.1   mycroft 		 */
    384       1.1   mycroft 		if (flags & B_SYNC) {
    385       1.1   mycroft 			bwrite(bp);
    386       1.1   mycroft 		} else {
    387       1.1   mycroft 			bdwrite(bp);
    388       1.1   mycroft 		}
    389       1.1   mycroft 		return (0);
    390       1.1   mycroft 	}
    391       1.1   mycroft 	brelse(bp);
    392      1.23       chs 	if (bpp != NULL) {
    393      1.23       chs 		if (flags & B_CLRBUF) {
    394      1.23       chs 			error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
    395      1.23       chs 			if (error) {
    396      1.23       chs 				brelse(nbp);
    397      1.23       chs 				goto fail;
    398      1.23       chs 			}
    399      1.23       chs 		} else {
    400      1.23       chs 			nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    401      1.23       chs 			nbp->b_blkno = fsbtodb(fs, nb);
    402      1.23       chs 			clrbuf(nbp);
    403       1.1   mycroft 		}
    404      1.23       chs 		*bpp = nbp;
    405       1.1   mycroft 	}
    406       1.1   mycroft 	return (0);
    407  1.23.2.5   nathanw 
    408       1.8      fvdl fail:
    409  1.23.2.5   nathanw 
    410  1.23.2.5   nathanw 	/*
    411  1.23.2.5   nathanw 	 * Restore the UVM state to what the rest of the FFS code is
    412  1.23.2.5   nathanw 	 * expecting.  Unbusy any pages that we allocated and left busy up in
    413  1.23.2.5   nathanw 	 * ufs_balloc_range().  the following VOP_FSYNC() will try to busy
    414  1.23.2.5   nathanw 	 * those pages again, which would deadlock if they are still busy
    415  1.23.2.5   nathanw 	 * from before.  After this we're back to a state where we can undo
    416  1.23.2.5   nathanw 	 * any partial allocation.
    417  1.23.2.5   nathanw 	 */
    418  1.23.2.5   nathanw 
    419  1.23.2.5   nathanw 	simple_lock(&vp->v_uobj.vmobjlock);
    420  1.23.2.5   nathanw 	for (off = ap->a_startoffset; off < ap->a_startoffset + fs->fs_bsize;
    421  1.23.2.5   nathanw 	     off += PAGE_SIZE) {
    422  1.23.2.5   nathanw 		struct vm_page *pg;
    423  1.23.2.5   nathanw 
    424  1.23.2.5   nathanw 		pg = uvm_pagelookup(&vp->v_uobj, off);
    425  1.23.2.5   nathanw 		if (pg == NULL) {
    426  1.23.2.5   nathanw 			break;
    427  1.23.2.5   nathanw 		}
    428  1.23.2.5   nathanw 		uvm_pageactivate(pg);
    429  1.23.2.5   nathanw 		KASSERT((pg->flags & PG_FAKE) == 0);
    430  1.23.2.5   nathanw 		pg->flags &= ~(PG_BUSY);
    431  1.23.2.5   nathanw 		UVM_PAGE_OWN(pg, NULL);
    432  1.23.2.5   nathanw 	}
    433  1.23.2.5   nathanw 	simple_unlock(&vp->v_uobj.vmobjlock);
    434  1.23.2.5   nathanw 
    435       1.8      fvdl 	/*
    436       1.8      fvdl 	 * If we have failed part way through block allocation, we
    437       1.8      fvdl 	 * have to deallocate any indirect blocks that we have allocated.
    438      1.16      fvdl 	 * We have to fsync the file before we start to get rid of all
    439      1.16      fvdl 	 * of its dependencies so that we do not leave them dangling.
    440      1.16      fvdl 	 * We have to sync it at the end so that the soft updates code
    441      1.16      fvdl 	 * does not find any untracked changes. Although this is really
    442      1.16      fvdl 	 * slow, running out of disk space is not expected to be a common
    443      1.16      fvdl 	 * occurence. The error return from fsync is ignored as we already
    444      1.16      fvdl 	 * have an error to return to the user.
    445       1.8      fvdl 	 */
    446  1.23.2.5   nathanw 
    447  1.23.2.1   nathanw 	(void) VOP_FSYNC(vp, cred, FSYNC_WAIT, 0, 0, curproc->l_proc);
    448       1.8      fvdl 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
    449       1.8      fvdl 		ffs_blkfree(ip, *blkp, fs->fs_bsize);
    450       1.8      fvdl 		deallocated += fs->fs_bsize;
    451       1.8      fvdl 	}
    452      1.18   mycroft 	if (unwindidx >= 0) {
    453      1.18   mycroft 		if (unwindidx == 0) {
    454      1.18   mycroft 			*allocib = 0;
    455      1.17      fvdl 		} else {
    456      1.18   mycroft 			int r;
    457      1.18   mycroft 
    458      1.18   mycroft 			r = bread(vp, indirs[unwindidx].in_lbn,
    459      1.18   mycroft 			    (int)fs->fs_bsize, NOCRED, &bp);
    460      1.18   mycroft 			if (r) {
    461      1.18   mycroft 				panic("Could not unwind indirect block, error %d", r);
    462      1.18   mycroft 				brelse(bp);
    463      1.18   mycroft 			} else {
    464      1.18   mycroft 				bap = (ufs_daddr_t *)bp->b_data;
    465      1.18   mycroft 				bap[indirs[unwindidx].in_off] = 0;
    466      1.18   mycroft 				if (flags & B_SYNC)
    467      1.18   mycroft 					bwrite(bp);
    468      1.18   mycroft 				else
    469      1.18   mycroft 					bdwrite(bp);
    470      1.18   mycroft 			}
    471      1.17      fvdl 		}
    472      1.19   mycroft 		for (i = unwindidx + 1; i <= num; i++) {
    473      1.19   mycroft 			bp = getblk(vp, indirs[i].in_lbn, (int)fs->fs_bsize, 0,
    474      1.19   mycroft 			    0);
    475      1.19   mycroft 			bp->b_flags |= B_INVAL;
    476      1.19   mycroft 			brelse(bp);
    477      1.19   mycroft 		}
    478      1.17      fvdl 	}
    479       1.8      fvdl 	if (deallocated) {
    480       1.8      fvdl #ifdef QUOTA
    481       1.8      fvdl 		/*
    482       1.8      fvdl 		 * Restore user's disk quota because allocation failed.
    483       1.8      fvdl 		 */
    484       1.8      fvdl 		(void)chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
    485       1.8      fvdl #endif
    486       1.8      fvdl 		ip->i_ffs_blocks -= btodb(deallocated);
    487      1.13   mycroft 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    488       1.8      fvdl 	}
    489  1.23.2.1   nathanw 	(void) VOP_FSYNC(vp, cred, FSYNC_WAIT, 0, 0, curproc->l_proc);
    490       1.8      fvdl 	return (error);
    491      1.23       chs }
    492      1.23       chs 
    493      1.23       chs 
    494      1.23       chs int
    495  1.23.2.4   nathanw ffs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
    496  1.23.2.4   nathanw     struct ucred *cred)
    497      1.23       chs {
    498      1.23       chs 	struct inode *ip = VTOI(vp);
    499      1.23       chs 	struct fs *fs = ip->i_fs;
    500      1.23       chs 	int error, delta, bshift, bsize;
    501  1.23.2.4   nathanw 	UVMHIST_FUNC("ffs_gop_alloc"); UVMHIST_CALLED(ubchist);
    502      1.23       chs 
    503      1.23       chs 	error = 0;
    504      1.23       chs 	bshift = fs->fs_bshift;
    505      1.23       chs 	bsize = 1 << bshift;
    506      1.23       chs 
    507      1.23       chs 	delta = off & (bsize - 1);
    508      1.23       chs 	off -= delta;
    509      1.23       chs 	len += delta;
    510      1.23       chs 
    511      1.23       chs 	while (len > 0) {
    512  1.23.2.4   nathanw 		bsize = MIN(bsize, len);
    513      1.23       chs 
    514  1.23.2.4   nathanw 		error = VOP_BALLOC(vp, off, bsize, cred, flags, NULL);
    515      1.23       chs 		if (error) {
    516      1.23       chs 			goto out;
    517      1.23       chs 		}
    518      1.23       chs 
    519      1.23       chs 		/*
    520      1.23       chs 		 * increase file size now, VOP_BALLOC() requires that
    521      1.23       chs 		 * EOF be up-to-date before each call.
    522      1.23       chs 		 */
    523      1.23       chs 
    524      1.23       chs 		if (ip->i_ffs_size < off + bsize) {
    525  1.23.2.4   nathanw 			UVMHIST_LOG(ubchist, "vp %p old 0x%x new 0x%x",
    526  1.23.2.4   nathanw 			    vp, ip->i_ffs_size, off + bsize, 0);
    527      1.23       chs 			ip->i_ffs_size = off + bsize;
    528      1.23       chs 		}
    529      1.23       chs 
    530      1.23       chs 		off += bsize;
    531      1.23       chs 		len -= bsize;
    532      1.23       chs 	}
    533      1.23       chs 
    534      1.23       chs out:
    535      1.23       chs 	return error;
    536       1.1   mycroft }
    537