Home | History | Annotate | Line # | Download | only in ffs
ffs_balloc.c revision 1.13.2.5
      1  1.13.2.5       chs /*	$NetBSD: ffs_balloc.c,v 1.13.2.5 1999/05/30 15:01:26 chs 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.11    scottr #if defined(_KERNEL) && !defined(_LKM)
     39      1.10    scottr #include "opt_quota.h"
     40       1.7       mrg #include "opt_uvm.h"
     41      1.11    scottr #endif
     42       1.1   mycroft 
     43       1.1   mycroft #include <sys/param.h>
     44       1.1   mycroft #include <sys/systm.h>
     45       1.1   mycroft #include <sys/buf.h>
     46       1.1   mycroft #include <sys/proc.h>
     47       1.1   mycroft #include <sys/file.h>
     48       1.1   mycroft #include <sys/vnode.h>
     49       1.9    bouyer #include <sys/mount.h>
     50       1.1   mycroft 
     51       1.1   mycroft #include <vm/vm.h>
     52       1.1   mycroft 
     53       1.6       mrg #if defined(UVM)
     54  1.13.2.1       chs #include <uvm/uvm.h>
     55       1.6       mrg #endif
     56       1.6       mrg 
     57       1.1   mycroft #include <ufs/ufs/quota.h>
     58       1.9    bouyer #include <ufs/ufs/ufsmount.h>
     59       1.1   mycroft #include <ufs/ufs/inode.h>
     60       1.1   mycroft #include <ufs/ufs/ufs_extern.h>
     61       1.9    bouyer #include <ufs/ufs/ufs_bswap.h>
     62       1.1   mycroft 
     63       1.1   mycroft #include <ufs/ffs/fs.h>
     64       1.1   mycroft #include <ufs/ffs/ffs_extern.h>
     65       1.1   mycroft 
     66       1.1   mycroft /*
     67       1.1   mycroft  * Balloc defines the structure of file system storage
     68       1.1   mycroft  * by allocating the physical blocks on a device given
     69       1.1   mycroft  * the inode and the logical block number in a file.
     70       1.1   mycroft  */
     71       1.3  christos int
     72  1.13.2.5       chs ffs_balloc(ip, lbn, size, cred, bpp, blknop, flags)
     73  1.13.2.2       chs 	struct inode *ip;
     74  1.13.2.2       chs 	ufs_daddr_t lbn;
     75       1.1   mycroft 	int size;
     76       1.1   mycroft 	struct ucred *cred;
     77       1.1   mycroft 	struct buf **bpp;
     78  1.13.2.2       chs 	daddr_t *blknop;
     79       1.1   mycroft 	int flags;
     80       1.1   mycroft {
     81  1.13.2.2       chs 	struct fs *fs;
     82  1.13.2.2       chs 	ufs_daddr_t nb;
     83       1.1   mycroft 	struct buf *bp, *nbp;
     84       1.1   mycroft 	struct vnode *vp = ITOV(ip);
     85       1.1   mycroft 	struct indir indirs[NIADDR + 2];
     86       1.8      fvdl 	ufs_daddr_t newb, *bap, pref;
     87       1.8      fvdl 	int deallocated, osize, nsize, num, i, error;
     88       1.8      fvdl 	ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
     89       1.1   mycroft 
     90  1.13.2.1       chs 	if (bpp != NULL) {
     91  1.13.2.1       chs 		*bpp = NULL;
     92  1.13.2.1       chs 	}
     93  1.13.2.2       chs 	if (blknop != NULL) {
     94  1.13.2.2       chs 		*blknop = (daddr_t)-1;
     95  1.13.2.2       chs 	}
     96  1.13.2.1       chs 
     97       1.8      fvdl 	if (lbn < 0)
     98       1.1   mycroft 		return (EFBIG);
     99       1.1   mycroft 	fs = ip->i_fs;
    100       1.1   mycroft 
    101       1.1   mycroft 	/*
    102  1.13.2.1       chs 	 * If the file currently ends with a fragment and
    103  1.13.2.1       chs 	 * the block we're allocating now is after the current EOF,
    104       1.1   mycroft 	 * this fragment has to be extended to be a full block.
    105       1.1   mycroft 	 */
    106       1.4    bouyer 	nb = lblkno(fs, ip->i_ffs_size);
    107       1.8      fvdl 	if (nb < NDADDR && nb < lbn) {
    108       1.1   mycroft 		osize = blksize(fs, ip, nb);
    109       1.1   mycroft 		if (osize < fs->fs_bsize && osize > 0) {
    110       1.1   mycroft 			error = ffs_realloccg(ip, nb,
    111       1.4    bouyer 				ffs_blkpref(ip, nb, (int)nb, &ip->i_ffs_db[0]),
    112  1.13.2.1       chs 				osize, (int)fs->fs_bsize, cred, bpp, &newb);
    113       1.1   mycroft 			if (error)
    114       1.1   mycroft 				return (error);
    115  1.13.2.1       chs 			ip->i_ffs_size = lblktosize(fs, nb + 1);
    116       1.6       mrg #if defined(UVM)
    117       1.6       mrg 			uvm_vnp_setsize(vp, ip->i_ffs_size);
    118       1.6       mrg #else
    119       1.5  drochner 			vnode_pager_setsize(vp, ip->i_ffs_size);
    120       1.6       mrg #endif
    121  1.13.2.1       chs 			ip->i_ffs_db[nb] = ufs_rw32(newb,
    122      1.12    kleink 			    UFS_MPNEEDSWAP(vp->v_mount));
    123       1.1   mycroft 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    124  1.13.2.1       chs 
    125  1.13.2.1       chs 			if (bpp) {
    126  1.13.2.1       chs 				if (flags & B_SYNC)
    127  1.13.2.1       chs 					bwrite(*bpp);
    128  1.13.2.1       chs 				else
    129  1.13.2.1       chs 					bawrite(*bpp);
    130  1.13.2.1       chs 			}
    131  1.13.2.1       chs 			else {
    132  1.13.2.1       chs 				/*
    133  1.13.2.1       chs 				 * XXX the data in the frag might be
    134  1.13.2.1       chs 				 * moving to a new disk location.
    135  1.13.2.1       chs 				 * we need to flush pages to the
    136  1.13.2.1       chs 				 * new disk locations.
    137  1.13.2.1       chs 				 * XXX we could do this in realloccg
    138  1.13.2.1       chs 				 * except for the sync flag.
    139  1.13.2.1       chs 				 */
    140  1.13.2.1       chs 				(vp->v_uvm.u_obj.pgops->pgo_flush)
    141  1.13.2.1       chs 					(&vp->v_uvm.u_obj, lblktosize(fs, nb),
    142  1.13.2.1       chs 					 lblktosize(fs, nb + 1),
    143  1.13.2.1       chs 					 flags & B_SYNC ? PGO_SYNCIO : 0);
    144  1.13.2.1       chs 			}
    145       1.1   mycroft 		}
    146       1.1   mycroft 	}
    147       1.1   mycroft 	/*
    148       1.1   mycroft 	 * The first NDADDR blocks are direct blocks
    149       1.1   mycroft 	 */
    150       1.8      fvdl 	if (lbn < NDADDR) {
    151  1.13.2.1       chs 
    152       1.9    bouyer 		nb = ufs_rw32(ip->i_ffs_db[lbn], UFS_MPNEEDSWAP(vp->v_mount));
    153  1.13.2.1       chs 		if (nb != 0 && ip->i_ffs_size >= lblktosize(fs, lbn + 1)) {
    154  1.13.2.1       chs 
    155  1.13.2.1       chs 			/*
    156  1.13.2.1       chs 			 * the block is an already-allocated direct block
    157  1.13.2.1       chs 			 * and the file already extends past this block,
    158  1.13.2.1       chs 			 * thus this must be a whole block.
    159  1.13.2.1       chs 			 * just read the block (if requested).
    160  1.13.2.1       chs 			 */
    161  1.13.2.1       chs 
    162  1.13.2.1       chs 			if (bpp != NULL) {
    163  1.13.2.1       chs 				error = bread(vp, lbn, fs->fs_bsize, NOCRED,
    164  1.13.2.1       chs 					      &bp);
    165  1.13.2.1       chs 				if (error) {
    166  1.13.2.1       chs 					brelse(bp);
    167  1.13.2.1       chs 					return (error);
    168  1.13.2.1       chs 				}
    169  1.13.2.1       chs 				*bpp = bp;
    170       1.1   mycroft 			}
    171  1.13.2.5       chs 			if (blknop) {
    172  1.13.2.5       chs 				*blknop = fsbtodb(fs, nb);
    173  1.13.2.5       chs 			}
    174       1.1   mycroft 			return (0);
    175       1.1   mycroft 		}
    176       1.1   mycroft 		if (nb != 0) {
    177       1.1   mycroft 			/*
    178       1.1   mycroft 			 * Consider need to reallocate a fragment.
    179       1.1   mycroft 			 */
    180       1.4    bouyer 			osize = fragroundup(fs, blkoff(fs, ip->i_ffs_size));
    181       1.1   mycroft 			nsize = fragroundup(fs, size);
    182       1.1   mycroft 			if (nsize <= osize) {
    183  1.13.2.1       chs 
    184  1.13.2.1       chs 				/*
    185  1.13.2.1       chs 				 * the existing block is already
    186  1.13.2.1       chs 				 * at least as big as we want.
    187  1.13.2.1       chs 				 * just read the block (if requested).
    188  1.13.2.1       chs 				 */
    189  1.13.2.1       chs 
    190  1.13.2.3       chs 				if (bpp != NULL) {
    191  1.13.2.3       chs 					error = bread(vp, lbn, osize, NOCRED,
    192  1.13.2.3       chs 						      &bp);
    193  1.13.2.3       chs 					if (error) {
    194  1.13.2.3       chs 						brelse(bp);
    195  1.13.2.3       chs 						return (error);
    196  1.13.2.3       chs 					}
    197  1.13.2.3       chs 					*bpp = bp;
    198  1.13.2.3       chs 				}
    199  1.13.2.5       chs 				if (blknop) {
    200  1.13.2.5       chs 					*blknop = fsbtodb(fs, nb);
    201  1.13.2.5       chs 				}
    202  1.13.2.3       chs 				return 0;
    203       1.1   mycroft 			} else {
    204  1.13.2.1       chs 
    205  1.13.2.1       chs 				/*
    206  1.13.2.1       chs 				 * the existing block is smaller than we want,
    207  1.13.2.1       chs 				 * grow it.
    208  1.13.2.1       chs 				 */
    209  1.13.2.1       chs 
    210       1.8      fvdl 				error = ffs_realloccg(ip, lbn,
    211       1.8      fvdl 				    ffs_blkpref(ip, lbn, (int)lbn,
    212       1.8      fvdl 					&ip->i_ffs_db[0]), osize, nsize, cred,
    213  1.13.2.1       chs 					bpp, &newb);
    214       1.1   mycroft 				if (error)
    215       1.1   mycroft 					return (error);
    216       1.1   mycroft 			}
    217       1.1   mycroft 		} else {
    218  1.13.2.1       chs 
    219  1.13.2.1       chs 			/*
    220  1.13.2.1       chs 			 * the block was not previously allocated,
    221  1.13.2.1       chs 			 * allocate a new block or fragment.
    222  1.13.2.1       chs 			 */
    223  1.13.2.1       chs 
    224  1.13.2.1       chs 			if (ip->i_ffs_size < lblktosize(fs, lbn + 1))
    225       1.1   mycroft 				nsize = fragroundup(fs, size);
    226       1.1   mycroft 			else
    227       1.1   mycroft 				nsize = fs->fs_bsize;
    228       1.8      fvdl 			error = ffs_alloc(ip, lbn,
    229       1.8      fvdl 			    ffs_blkpref(ip, lbn, (int)lbn, &ip->i_ffs_db[0]),
    230       1.8      fvdl 				nsize, cred, &newb);
    231       1.1   mycroft 			if (error)
    232       1.1   mycroft 				return (error);
    233  1.13.2.1       chs 			if (bpp != NULL) {
    234  1.13.2.1       chs 				bp = getblk(vp, lbn, nsize, 0, 0);
    235  1.13.2.1       chs 				bp->b_blkno = fsbtodb(fs, newb);
    236  1.13.2.1       chs 				if (flags & B_CLRBUF)
    237  1.13.2.1       chs 					clrbuf(bp);
    238  1.13.2.1       chs 				*bpp = bp;
    239  1.13.2.1       chs 			}
    240  1.13.2.4       chs 		}
    241  1.13.2.4       chs 		ip->i_ffs_db[lbn] = ufs_rw32(newb, UFS_MPNEEDSWAP(vp->v_mount));
    242  1.13.2.4       chs 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    243  1.13.2.4       chs 
    244  1.13.2.4       chs 		if (blknop != NULL) {
    245  1.13.2.4       chs 			*blknop = fsbtodb(fs, newb);
    246  1.13.2.4       chs 		}
    247       1.1   mycroft 		return (0);
    248       1.1   mycroft 	}
    249  1.13.2.5       chs 
    250       1.1   mycroft 	/*
    251       1.1   mycroft 	 * Determine the number of levels of indirection.
    252       1.1   mycroft 	 */
    253  1.13.2.5       chs 
    254       1.1   mycroft 	pref = 0;
    255       1.8      fvdl 	if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
    256       1.1   mycroft 		return(error);
    257       1.1   mycroft #ifdef DIAGNOSTIC
    258       1.1   mycroft 	if (num < 1)
    259       1.1   mycroft 		panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
    260       1.1   mycroft #endif
    261       1.1   mycroft 	/*
    262       1.1   mycroft 	 * Fetch the first indirect block allocating if necessary.
    263       1.1   mycroft 	 */
    264       1.1   mycroft 	--num;
    265       1.9    bouyer 	nb = ufs_rw32(ip->i_ffs_ib[indirs[0].in_off],
    266      1.12    kleink 	    UFS_MPNEEDSWAP(vp->v_mount));
    267       1.8      fvdl 	allocib = NULL;
    268       1.8      fvdl 	allocblk = allociblk;
    269       1.1   mycroft 	if (nb == 0) {
    270       1.8      fvdl 		pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    271       1.9    bouyer 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
    272       1.9    bouyer 			cred, &newb);
    273       1.3  christos 		if (error)
    274       1.1   mycroft 			return (error);
    275       1.1   mycroft 		nb = newb;
    276       1.8      fvdl 		*allocblk++ = nb;
    277       1.1   mycroft 		bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
    278       1.8      fvdl 		bp->b_blkno = fsbtodb(fs, nb);
    279       1.1   mycroft 		clrbuf(bp);
    280       1.1   mycroft 		/*
    281       1.1   mycroft 		 * Write synchronously so that indirect blocks
    282       1.1   mycroft 		 * never point at garbage.
    283       1.1   mycroft 		 */
    284       1.8      fvdl 		if ((error = bwrite(bp)) != 0)
    285       1.8      fvdl 			goto fail;
    286       1.8      fvdl 		allocib = &ip->i_ffs_ib[indirs[0].in_off];
    287       1.9    bouyer 		*allocib = ufs_rw32(nb, UFS_MPNEEDSWAP(vp->v_mount));
    288       1.1   mycroft 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    289       1.1   mycroft 	}
    290       1.1   mycroft 	/*
    291       1.1   mycroft 	 * Fetch through the indirect blocks, allocating as necessary.
    292       1.1   mycroft 	 */
    293       1.1   mycroft 	for (i = 1;;) {
    294       1.1   mycroft 		error = bread(vp,
    295       1.1   mycroft 		    indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
    296       1.1   mycroft 		if (error) {
    297       1.1   mycroft 			brelse(bp);
    298       1.8      fvdl 			goto fail;
    299       1.1   mycroft 		}
    300       1.8      fvdl 		bap = (ufs_daddr_t *)bp->b_data;
    301      1.12    kleink 		nb = ufs_rw32(bap[indirs[i].in_off],
    302      1.12    kleink 		    UFS_MPNEEDSWAP(vp->v_mount));
    303       1.1   mycroft 		if (i == num)
    304       1.1   mycroft 			break;
    305       1.1   mycroft 		i += 1;
    306       1.1   mycroft 		if (nb != 0) {
    307       1.1   mycroft 			brelse(bp);
    308       1.1   mycroft 			continue;
    309       1.1   mycroft 		}
    310       1.1   mycroft 		if (pref == 0)
    311       1.8      fvdl 			pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    312       1.3  christos 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    313       1.3  christos 				  &newb);
    314       1.3  christos 		if (error) {
    315       1.1   mycroft 			brelse(bp);
    316       1.8      fvdl 			goto fail;
    317       1.1   mycroft 		}
    318       1.1   mycroft 		nb = newb;
    319       1.8      fvdl 		*allocblk++ = nb;
    320       1.1   mycroft 		nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
    321       1.1   mycroft 		nbp->b_blkno = fsbtodb(fs, nb);
    322       1.1   mycroft 		clrbuf(nbp);
    323       1.1   mycroft 		/*
    324       1.1   mycroft 		 * Write synchronously so that indirect blocks
    325       1.1   mycroft 		 * never point at garbage.
    326       1.1   mycroft 		 */
    327       1.3  christos 		if ((error = bwrite(nbp)) != 0) {
    328       1.1   mycroft 			brelse(bp);
    329       1.8      fvdl 			goto fail;
    330       1.1   mycroft 		}
    331       1.9    bouyer 		bap[indirs[i - 1].in_off] = ufs_rw32(nb,
    332      1.12    kleink 		    UFS_MPNEEDSWAP(vp->v_mount));
    333       1.1   mycroft 		/*
    334       1.1   mycroft 		 * If required, write synchronously, otherwise use
    335       1.1   mycroft 		 * delayed write.
    336       1.1   mycroft 		 */
    337       1.1   mycroft 		if (flags & B_SYNC) {
    338       1.1   mycroft 			bwrite(bp);
    339       1.1   mycroft 		} else {
    340       1.1   mycroft 			bdwrite(bp);
    341       1.1   mycroft 		}
    342       1.1   mycroft 	}
    343       1.1   mycroft 	/*
    344       1.1   mycroft 	 * Get the data block, allocating if necessary.
    345       1.1   mycroft 	 */
    346       1.1   mycroft 	if (nb == 0) {
    347       1.1   mycroft 		pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
    348       1.3  christos 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    349       1.3  christos 				  &newb);
    350       1.3  christos 		if (error) {
    351       1.1   mycroft 			brelse(bp);
    352       1.8      fvdl 			goto fail;
    353       1.1   mycroft 		}
    354       1.1   mycroft 		nb = newb;
    355       1.8      fvdl 		*allocblk++ = nb;
    356      1.12    kleink 		bap[indirs[i].in_off] = ufs_rw32(nb,
    357      1.12    kleink 		    UFS_MPNEEDSWAP(vp->v_mount));
    358       1.1   mycroft 		/*
    359       1.1   mycroft 		 * If required, write synchronously, otherwise use
    360       1.1   mycroft 		 * delayed write.
    361       1.1   mycroft 		 */
    362       1.1   mycroft 		if (flags & B_SYNC) {
    363       1.1   mycroft 			bwrite(bp);
    364       1.1   mycroft 		} else {
    365       1.1   mycroft 			bdwrite(bp);
    366       1.1   mycroft 		}
    367  1.13.2.1       chs 		if (bpp != NULL) {
    368  1.13.2.1       chs 			nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    369  1.13.2.1       chs 			nbp->b_blkno = fsbtodb(fs, nb);
    370  1.13.2.1       chs 			if (flags & B_CLRBUF)
    371  1.13.2.1       chs 				clrbuf(nbp);
    372  1.13.2.1       chs 			*bpp = nbp;
    373  1.13.2.1       chs 		}
    374  1.13.2.2       chs 		if (blknop != NULL) {
    375  1.13.2.2       chs 			*blknop = fsbtodb(fs, nb);
    376  1.13.2.2       chs 		}
    377       1.1   mycroft 		return (0);
    378       1.1   mycroft 	}
    379  1.13.2.1       chs 
    380       1.1   mycroft 	brelse(bp);
    381  1.13.2.1       chs 
    382  1.13.2.1       chs 	if (bpp != NULL) {
    383  1.13.2.1       chs 		if (flags & B_CLRBUF) {
    384  1.13.2.1       chs 			error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
    385  1.13.2.1       chs 			if (error) {
    386  1.13.2.1       chs 				brelse(nbp);
    387  1.13.2.1       chs 				goto fail;
    388  1.13.2.1       chs 			}
    389  1.13.2.1       chs 		} else {
    390  1.13.2.1       chs 			nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    391  1.13.2.1       chs 			nbp->b_blkno = fsbtodb(fs, nb);
    392  1.13.2.1       chs 			clrbuf(nbp);
    393       1.1   mycroft 		}
    394  1.13.2.1       chs 		*bpp = nbp;
    395       1.1   mycroft 	}
    396  1.13.2.2       chs 	if (blknop != NULL) {
    397  1.13.2.2       chs 		*blknop = fsbtodb(fs, nb);
    398  1.13.2.2       chs 	}
    399       1.1   mycroft 	return (0);
    400       1.8      fvdl fail:
    401       1.8      fvdl 	/*
    402       1.8      fvdl 	 * If we have failed part way through block allocation, we
    403       1.8      fvdl 	 * have to deallocate any indirect blocks that we have allocated.
    404       1.8      fvdl 	 */
    405       1.8      fvdl 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
    406       1.8      fvdl 		ffs_blkfree(ip, *blkp, fs->fs_bsize);
    407       1.8      fvdl 		deallocated += fs->fs_bsize;
    408       1.8      fvdl 	}
    409       1.8      fvdl 	if (allocib != NULL)
    410       1.8      fvdl 		*allocib = 0;
    411       1.8      fvdl 	if (deallocated) {
    412       1.8      fvdl #ifdef QUOTA
    413       1.8      fvdl 		/*
    414       1.8      fvdl 		 * Restore user's disk quota because allocation failed.
    415       1.8      fvdl 		 */
    416       1.8      fvdl 		(void)chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
    417       1.8      fvdl #endif
    418       1.8      fvdl 		ip->i_ffs_blocks -= btodb(deallocated);
    419      1.13   mycroft 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    420       1.8      fvdl 	}
    421       1.8      fvdl 	return (error);
    422  1.13.2.2       chs }
    423  1.13.2.2       chs 
    424  1.13.2.2       chs int
    425  1.13.2.2       chs ffs_balloc_range(ip, off, len, cred, flags)
    426  1.13.2.2       chs 	struct inode *ip;
    427  1.13.2.5       chs 	off_t off, len;
    428  1.13.2.2       chs 	struct ucred *cred;
    429  1.13.2.2       chs 	int flags;
    430  1.13.2.2       chs {
    431  1.13.2.2       chs 	struct fs *fs = ip->i_fs;
    432  1.13.2.2       chs 	int lbn, bsize, delta, error;
    433  1.13.2.2       chs 	off_t pagestart, pageend;
    434  1.13.2.2       chs 
    435  1.13.2.2       chs 	/*
    436  1.13.2.2       chs 	 * pagestart and pageend describe the range of pages that are
    437  1.13.2.2       chs 	 * completely covered by the range of blocks being allocated.
    438  1.13.2.2       chs 	 */
    439  1.13.2.2       chs 
    440  1.13.2.2       chs 	pagestart = round_page(off);
    441  1.13.2.2       chs 	pageend = trunc_page(off + len);
    442  1.13.2.2       chs 
    443  1.13.2.5       chs 	/*
    444  1.13.2.5       chs 	 * adjust off to be block-aligned.
    445  1.13.2.5       chs 	 */
    446  1.13.2.5       chs 
    447  1.13.2.5       chs 	delta = off - lblktosize(fs, lblkno(fs, off));
    448  1.13.2.5       chs 	off -= delta;
    449  1.13.2.5       chs 	len += delta;
    450  1.13.2.5       chs 
    451  1.13.2.2       chs 	while (len > 0) {
    452  1.13.2.2       chs 		lbn = lblkno(fs, off);
    453  1.13.2.5       chs 		bsize = min(fs->fs_bsize, len);
    454  1.13.2.2       chs 
    455  1.13.2.5       chs 		if ((error = ffs_balloc(ip, lbn, bsize, cred, NULL, NULL,
    456  1.13.2.5       chs 					flags))) {
    457  1.13.2.2       chs 			return error;
    458  1.13.2.4       chs 		}
    459  1.13.2.4       chs 
    460  1.13.2.4       chs 		/*
    461  1.13.2.4       chs 		 * bump file size now.
    462  1.13.2.4       chs 		 * ffs_balloc() needs to know in the case where we loop here.
    463  1.13.2.4       chs 		 */
    464  1.13.2.4       chs 
    465  1.13.2.4       chs 		if (ip->i_ffs_size < lblktosize(fs, lbn) + bsize) {
    466  1.13.2.4       chs 			ip->i_ffs_size = lblktosize(fs, lbn) + bsize;
    467  1.13.2.4       chs 			uvm_vnp_setsize(ip->i_vnode, ip->i_ffs_size);
    468  1.13.2.2       chs 		}
    469  1.13.2.2       chs 
    470  1.13.2.5       chs 		len -= bsize;
    471  1.13.2.5       chs 		off += bsize;
    472  1.13.2.2       chs 	}
    473  1.13.2.2       chs 	return 0;
    474       1.1   mycroft }
    475