Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_balloc.c revision 1.9
      1  1.9       mrg /*	$NetBSD: ext2fs_balloc.c,v 1.9 2001/05/30 11:57:19 mrg Exp $	*/
      2  1.1    bouyer 
      3  1.1    bouyer /*
      4  1.1    bouyer  * Copyright (c) 1997 Manuel Bouyer.
      5  1.1    bouyer  * Copyright (c) 1982, 1986, 1989, 1993
      6  1.1    bouyer  *	The Regents of the University of California.  All rights reserved.
      7  1.1    bouyer  *
      8  1.1    bouyer  * Redistribution and use in source and binary forms, with or without
      9  1.1    bouyer  * modification, are permitted provided that the following conditions
     10  1.1    bouyer  * are met:
     11  1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     12  1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     13  1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     15  1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     16  1.1    bouyer  * 3. All advertising materials mentioning features or use of this software
     17  1.1    bouyer  *    must display the following acknowledgement:
     18  1.1    bouyer  *	This product includes software developed by the University of
     19  1.1    bouyer  *	California, Berkeley and its contributors.
     20  1.1    bouyer  * 4. Neither the name of the University nor the names of its contributors
     21  1.1    bouyer  *    may be used to endorse or promote products derived from this software
     22  1.1    bouyer  *    without specific prior written permission.
     23  1.1    bouyer  *
     24  1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1    bouyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1    bouyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1    bouyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1    bouyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1    bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1    bouyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1    bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1    bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1    bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1    bouyer  * SUCH DAMAGE.
     35  1.1    bouyer  *
     36  1.1    bouyer  *	@(#)ffs_balloc.c	8.4 (Berkeley) 9/23/93
     37  1.1    bouyer  * Modified for ext2fs by Manuel Bouyer.
     38  1.1    bouyer  */
     39  1.1    bouyer 
     40  1.9       mrg #if defined(_KERNEL_OPT)
     41  1.7       chs #include "opt_uvmhist.h"
     42  1.7       chs #endif
     43  1.7       chs 
     44  1.1    bouyer #include <sys/param.h>
     45  1.1    bouyer #include <sys/systm.h>
     46  1.1    bouyer #include <sys/buf.h>
     47  1.1    bouyer #include <sys/proc.h>
     48  1.1    bouyer #include <sys/file.h>
     49  1.1    bouyer #include <sys/vnode.h>
     50  1.7       chs #include <sys/mount.h>
     51  1.7       chs 
     52  1.7       chs #include <uvm/uvm.h>
     53  1.1    bouyer 
     54  1.1    bouyer #include <ufs/ufs/quota.h>
     55  1.1    bouyer #include <ufs/ufs/inode.h>
     56  1.1    bouyer #include <ufs/ufs/ufs_extern.h>
     57  1.1    bouyer 
     58  1.1    bouyer #include <ufs/ext2fs/ext2fs.h>
     59  1.1    bouyer #include <ufs/ext2fs/ext2fs_extern.h>
     60  1.1    bouyer 
     61  1.1    bouyer /*
     62  1.1    bouyer  * Balloc defines the structure of file system storage
     63  1.1    bouyer  * by allocating the physical blocks on a device given
     64  1.1    bouyer  * the inode and the logical block number in a file.
     65  1.1    bouyer  */
     66  1.1    bouyer int
     67  1.1    bouyer ext2fs_balloc(ip, bn, size, cred, bpp, flags)
     68  1.4  augustss 	struct inode *ip;
     69  1.4  augustss 	ufs_daddr_t bn;
     70  1.1    bouyer 	int size;
     71  1.1    bouyer 	struct ucred *cred;
     72  1.1    bouyer 	struct buf **bpp;
     73  1.1    bouyer 	int flags;
     74  1.1    bouyer {
     75  1.4  augustss 	struct m_ext2fs *fs;
     76  1.4  augustss 	ufs_daddr_t nb;
     77  1.1    bouyer 	struct buf *bp, *nbp;
     78  1.1    bouyer 	struct vnode *vp = ITOV(ip);
     79  1.1    bouyer 	struct indir indirs[NIADDR + 2];
     80  1.3      fvdl 	ufs_daddr_t newb, lbn, *bap, pref;
     81  1.1    bouyer 	int num, i, error;
     82  1.3      fvdl 	u_int deallocated;
     83  1.3      fvdl 	ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
     84  1.5   mycroft 	int unwindidx = -1;
     85  1.7       chs 	UVMHIST_FUNC("ext2fs_balloc"); UVMHIST_CALLED(ubchist);
     86  1.1    bouyer 
     87  1.7       chs 	UVMHIST_LOG(ubchist, "bn 0x%x", bn,0,0,0);
     88  1.7       chs 
     89  1.7       chs 	if (bpp != NULL) {
     90  1.7       chs 		*bpp = NULL;
     91  1.7       chs 	}
     92  1.1    bouyer 	if (bn < 0)
     93  1.1    bouyer 		return (EFBIG);
     94  1.1    bouyer 	fs = ip->i_e2fs;
     95  1.1    bouyer 	lbn = bn;
     96  1.1    bouyer 
     97  1.1    bouyer 	/*
     98  1.1    bouyer 	 * The first NDADDR blocks are direct blocks
     99  1.1    bouyer 	 */
    100  1.1    bouyer 	if (bn < NDADDR) {
    101  1.2    bouyer 		nb = fs2h32(ip->i_e2fs_blocks[bn]);
    102  1.1    bouyer 		if (nb != 0) {
    103  1.7       chs 
    104  1.7       chs 			/*
    105  1.7       chs 			 * the block is already allocated, just read it.
    106  1.7       chs 			 */
    107  1.7       chs 
    108  1.7       chs 			if (bpp != NULL) {
    109  1.7       chs 				error = bread(vp, bn, fs->e2fs_bsize, NOCRED,
    110  1.7       chs 					      &bp);
    111  1.7       chs 				if (error) {
    112  1.7       chs 					brelse(bp);
    113  1.7       chs 					return (error);
    114  1.7       chs 				}
    115  1.7       chs 				*bpp = bp;
    116  1.1    bouyer 			}
    117  1.1    bouyer 			return (0);
    118  1.7       chs 		}
    119  1.7       chs 
    120  1.7       chs 		/*
    121  1.7       chs 		 * allocate a new direct block.
    122  1.7       chs 		 */
    123  1.7       chs 
    124  1.7       chs 		error = ext2fs_alloc(ip, bn,
    125  1.7       chs 		    ext2fs_blkpref(ip, bn, bn, &ip->i_e2fs_blocks[0]),
    126  1.7       chs 		    cred, &newb);
    127  1.7       chs 		if (error)
    128  1.7       chs 			return (error);
    129  1.7       chs 		ip->i_e2fs_last_lblk = lbn;
    130  1.7       chs 		ip->i_e2fs_last_blk = newb;
    131  1.7       chs 		ip->i_e2fs_blocks[bn] = h2fs32(newb);
    132  1.7       chs 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    133  1.7       chs 		if (bpp != NULL) {
    134  1.1    bouyer 			bp = getblk(vp, bn, fs->e2fs_bsize, 0, 0);
    135  1.1    bouyer 			bp->b_blkno = fsbtodb(fs, newb);
    136  1.1    bouyer 			if (flags & B_CLRBUF)
    137  1.1    bouyer 				clrbuf(bp);
    138  1.7       chs 			*bpp = bp;
    139  1.1    bouyer 		}
    140  1.1    bouyer 		return (0);
    141  1.1    bouyer 	}
    142  1.1    bouyer 	/*
    143  1.1    bouyer 	 * Determine the number of levels of indirection.
    144  1.1    bouyer 	 */
    145  1.1    bouyer 	pref = 0;
    146  1.1    bouyer 	if ((error = ufs_getlbns(vp, bn, indirs, &num)) != 0)
    147  1.1    bouyer 		return(error);
    148  1.1    bouyer #ifdef DIAGNOSTIC
    149  1.1    bouyer 	if (num < 1)
    150  1.1    bouyer 		panic ("ext2fs_balloc: ufs_getlbns returned indirect block\n");
    151  1.1    bouyer #endif
    152  1.1    bouyer 	/*
    153  1.1    bouyer 	 * Fetch the first indirect block allocating if necessary.
    154  1.1    bouyer 	 */
    155  1.1    bouyer 	--num;
    156  1.2    bouyer 	nb = fs2h32(ip->i_e2fs_blocks[NDADDR + indirs[0].in_off]);
    157  1.3      fvdl 	allocib = NULL;
    158  1.3      fvdl 	allocblk = allociblk;
    159  1.1    bouyer 	if (nb == 0) {
    160  1.3      fvdl 		pref = ext2fs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    161  1.5   mycroft 		error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
    162  1.1    bouyer 		if (error)
    163  1.1    bouyer 			return (error);
    164  1.1    bouyer 		nb = newb;
    165  1.3      fvdl 		*allocblk++ = nb;
    166  1.1    bouyer 		ip->i_e2fs_last_blk = newb;
    167  1.1    bouyer 		bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0);
    168  1.1    bouyer 		bp->b_blkno = fsbtodb(fs, newb);
    169  1.1    bouyer 		clrbuf(bp);
    170  1.1    bouyer 		/*
    171  1.1    bouyer 		 * Write synchronously so that indirect blocks
    172  1.1    bouyer 		 * never point at garbage.
    173  1.1    bouyer 		 */
    174  1.3      fvdl 		if ((error = bwrite(bp)) != 0)
    175  1.3      fvdl 			goto fail;
    176  1.5   mycroft 		unwindidx = 0;
    177  1.3      fvdl 		allocib = &ip->i_e2fs_blocks[NDADDR + indirs[0].in_off];
    178  1.3      fvdl 		*allocib = h2fs32(newb);
    179  1.1    bouyer 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    180  1.1    bouyer 	}
    181  1.1    bouyer 	/*
    182  1.1    bouyer 	 * Fetch through the indirect blocks, allocating as necessary.
    183  1.1    bouyer 	 */
    184  1.1    bouyer 	for (i = 1;;) {
    185  1.1    bouyer 		error = bread(vp,
    186  1.5   mycroft 		    indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp);
    187  1.1    bouyer 		if (error) {
    188  1.1    bouyer 			brelse(bp);
    189  1.3      fvdl 			goto fail;
    190  1.1    bouyer 		}
    191  1.3      fvdl 		bap = (ufs_daddr_t *)bp->b_data;
    192  1.2    bouyer 		nb = fs2h32(bap[indirs[i].in_off]);
    193  1.1    bouyer 		if (i == num)
    194  1.1    bouyer 			break;
    195  1.5   mycroft 		i++;
    196  1.1    bouyer 		if (nb != 0) {
    197  1.1    bouyer 			brelse(bp);
    198  1.1    bouyer 			continue;
    199  1.1    bouyer 		}
    200  1.3      fvdl 		pref = ext2fs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    201  1.5   mycroft 		error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
    202  1.1    bouyer 		if (error) {
    203  1.1    bouyer 			brelse(bp);
    204  1.3      fvdl 			goto fail;
    205  1.1    bouyer 		}
    206  1.1    bouyer 		nb = newb;
    207  1.3      fvdl 		*allocblk++ = nb;
    208  1.1    bouyer 		ip->i_e2fs_last_blk = newb;
    209  1.1    bouyer 		nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0);
    210  1.1    bouyer 		nbp->b_blkno = fsbtodb(fs, nb);
    211  1.1    bouyer 		clrbuf(nbp);
    212  1.1    bouyer 		/*
    213  1.1    bouyer 		 * Write synchronously so that indirect blocks
    214  1.1    bouyer 		 * never point at garbage.
    215  1.1    bouyer 		 */
    216  1.1    bouyer 		if ((error = bwrite(nbp)) != 0) {
    217  1.1    bouyer 			brelse(bp);
    218  1.3      fvdl 			goto fail;
    219  1.1    bouyer 		}
    220  1.5   mycroft 		if (unwindidx < 0)
    221  1.5   mycroft 			unwindidx = i - 1;
    222  1.2    bouyer 		bap[indirs[i - 1].in_off] = h2fs32(nb);
    223  1.1    bouyer 		/*
    224  1.1    bouyer 		 * If required, write synchronously, otherwise use
    225  1.1    bouyer 		 * delayed write.
    226  1.1    bouyer 		 */
    227  1.1    bouyer 		if (flags & B_SYNC) {
    228  1.1    bouyer 			bwrite(bp);
    229  1.1    bouyer 		} else {
    230  1.1    bouyer 			bdwrite(bp);
    231  1.1    bouyer 		}
    232  1.1    bouyer 	}
    233  1.1    bouyer 	/*
    234  1.1    bouyer 	 * Get the data block, allocating if necessary.
    235  1.1    bouyer 	 */
    236  1.1    bouyer 	if (nb == 0) {
    237  1.5   mycroft 		pref = ext2fs_blkpref(ip, lbn, indirs[num].in_off, &bap[0]);
    238  1.5   mycroft 		error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
    239  1.1    bouyer 		if (error) {
    240  1.1    bouyer 			brelse(bp);
    241  1.3      fvdl 			goto fail;
    242  1.1    bouyer 		}
    243  1.1    bouyer 		nb = newb;
    244  1.3      fvdl 		*allocblk++ = nb;
    245  1.1    bouyer 		ip->i_e2fs_last_lblk = lbn;
    246  1.1    bouyer 		ip->i_e2fs_last_blk = newb;
    247  1.5   mycroft 		bap[indirs[num].in_off] = h2fs32(nb);
    248  1.1    bouyer 		/*
    249  1.1    bouyer 		 * If required, write synchronously, otherwise use
    250  1.1    bouyer 		 * delayed write.
    251  1.1    bouyer 		 */
    252  1.1    bouyer 		if (flags & B_SYNC) {
    253  1.1    bouyer 			bwrite(bp);
    254  1.1    bouyer 		} else {
    255  1.1    bouyer 			bdwrite(bp);
    256  1.1    bouyer 		}
    257  1.7       chs 		if (bpp != NULL) {
    258  1.7       chs 			nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
    259  1.7       chs 			nbp->b_blkno = fsbtodb(fs, nb);
    260  1.7       chs 			if (flags & B_CLRBUF)
    261  1.7       chs 				clrbuf(nbp);
    262  1.7       chs 			*bpp = nbp;
    263  1.7       chs 		}
    264  1.1    bouyer 		return (0);
    265  1.1    bouyer 	}
    266  1.1    bouyer 	brelse(bp);
    267  1.7       chs 	if (bpp != NULL) {
    268  1.7       chs 		if (flags & B_CLRBUF) {
    269  1.7       chs 			error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED,
    270  1.7       chs 				      &nbp);
    271  1.7       chs 			if (error) {
    272  1.7       chs 				brelse(nbp);
    273  1.7       chs 				goto fail;
    274  1.7       chs 			}
    275  1.7       chs 		} else {
    276  1.7       chs 			nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
    277  1.7       chs 			nbp->b_blkno = fsbtodb(fs, nb);
    278  1.1    bouyer 		}
    279  1.7       chs 		*bpp = nbp;
    280  1.1    bouyer 	}
    281  1.1    bouyer 	return (0);
    282  1.3      fvdl fail:
    283  1.3      fvdl 	/*
    284  1.3      fvdl 	 * If we have failed part way through block allocation, we
    285  1.3      fvdl 	 * have to deallocate any indirect blocks that we have allocated.
    286  1.3      fvdl 	 */
    287  1.3      fvdl 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
    288  1.3      fvdl 		ext2fs_blkfree(ip, *blkp);
    289  1.3      fvdl 		deallocated += fs->e2fs_bsize;
    290  1.3      fvdl 	}
    291  1.5   mycroft 	if (unwindidx >= 0) {
    292  1.5   mycroft 		if (unwindidx == 0) {
    293  1.5   mycroft 			*allocib = 0;
    294  1.5   mycroft 		} else {
    295  1.5   mycroft 			int r;
    296  1.5   mycroft 
    297  1.5   mycroft 			r = bread(vp, indirs[unwindidx].in_lbn,
    298  1.5   mycroft 			    (int)fs->e2fs_bsize, NOCRED, &bp);
    299  1.5   mycroft 			if (r) {
    300  1.5   mycroft 				panic("Could not unwind indirect block, error %d", r);
    301  1.5   mycroft 				brelse(bp);
    302  1.5   mycroft 			} else {
    303  1.5   mycroft 				bap = (ufs_daddr_t *)bp->b_data;
    304  1.5   mycroft 				bap[indirs[unwindidx].in_off] = 0;
    305  1.5   mycroft 				if (flags & B_SYNC)
    306  1.5   mycroft 					bwrite(bp);
    307  1.5   mycroft 				else
    308  1.5   mycroft 					bdwrite(bp);
    309  1.5   mycroft 			}
    310  1.5   mycroft 		}
    311  1.5   mycroft 		for (i = unwindidx + 1; i <= num; i++) {
    312  1.5   mycroft 			bp = getblk(vp, indirs[i].in_lbn, (int)fs->e2fs_bsize,
    313  1.5   mycroft 			    0, 0);
    314  1.5   mycroft 			bp->b_flags |= B_INVAL;
    315  1.5   mycroft 			brelse(bp);
    316  1.5   mycroft 		}
    317  1.5   mycroft 	}
    318  1.3      fvdl 	if (deallocated) {
    319  1.3      fvdl 		ip->i_e2fs_nblock -= btodb(deallocated);
    320  1.3      fvdl 		ip->i_e2fs_flags |= IN_CHANGE | IN_UPDATE;
    321  1.3      fvdl 	}
    322  1.7       chs 	return error;
    323  1.7       chs }
    324  1.7       chs 
    325  1.7       chs int
    326  1.7       chs ext2fs_ballocn(v)
    327  1.7       chs 	void *v;
    328  1.7       chs {
    329  1.7       chs 	struct vop_ballocn_args /* {
    330  1.7       chs 		struct vnode *a_vp;
    331  1.7       chs 		off_t a_offset;
    332  1.7       chs 		off_t a_length;
    333  1.7       chs 		struct ucred *a_cred;
    334  1.7       chs 		int a_flags;
    335  1.7       chs 	} */ *ap = v;
    336  1.7       chs 	off_t off, len;
    337  1.7       chs 	struct vnode *vp = ap->a_vp;
    338  1.7       chs 	struct inode *ip = VTOI(vp);
    339  1.7       chs 	struct m_ext2fs *fs = ip->i_e2fs;
    340  1.7       chs 	int error, delta, bshift, bsize;
    341  1.7       chs 	UVMHIST_FUNC("ext2fs_ballocn"); UVMHIST_CALLED(ubchist);
    342  1.7       chs 
    343  1.7       chs 	bshift = fs->e2fs_bshift;
    344  1.7       chs 	bsize = 1 << bshift;
    345  1.7       chs 
    346  1.7       chs 	off = ap->a_offset;
    347  1.7       chs 	len = ap->a_length;
    348  1.7       chs 
    349  1.7       chs 	delta = off & (bsize - 1);
    350  1.7       chs 	off -= delta;
    351  1.7       chs 	len += delta;
    352  1.7       chs 
    353  1.7       chs 	while (len > 0) {
    354  1.7       chs 		bsize = min(bsize, len);
    355  1.7       chs 		UVMHIST_LOG(ubchist, "off 0x%x len 0x%x bsize 0x%x",
    356  1.7       chs 			    off, len, bsize, 0);
    357  1.7       chs 
    358  1.7       chs 		error = ext2fs_balloc(ip, lblkno(fs, off), bsize, ap->a_cred,
    359  1.7       chs 		    NULL, ap->a_flags);
    360  1.7       chs 		if (error) {
    361  1.7       chs 			UVMHIST_LOG(ubchist, "error %d", error, 0,0,0);
    362  1.7       chs 			return error;
    363  1.7       chs 		}
    364  1.7       chs 
    365  1.7       chs 		/*
    366  1.7       chs 		 * increase file size now, VOP_BALLOC() requires that
    367  1.7       chs 		 * EOF be up-to-date before each call.
    368  1.7       chs 		 */
    369  1.7       chs 
    370  1.7       chs 		if (ip->i_e2fs_size < off + bsize) {
    371  1.7       chs 			UVMHIST_LOG(ubchist, "old 0x%x new 0x%x",
    372  1.7       chs 				    ip->i_e2fs_size, off + bsize,0,0);
    373  1.7       chs 			ip->i_e2fs_size = off + bsize;
    374  1.7       chs 			if (vp->v_uvm.u_size < ip->i_e2fs_size) {
    375  1.7       chs 				uvm_vnp_setsize(vp, ip->i_e2fs_size);
    376  1.7       chs 			}
    377  1.7       chs 		}
    378  1.7       chs 
    379  1.7       chs 		off += bsize;
    380  1.7       chs 		len -= bsize;
    381  1.7       chs 	}
    382  1.7       chs 	return 0;
    383  1.7       chs }
    384  1.7       chs 
    385  1.7       chs /*
    386  1.7       chs  * allocate a range of blocks in a file.
    387  1.7       chs  * after this function returns, any page entirely contained within the range
    388  1.7       chs  * will map to invalid data and thus must be overwritten before it is made
    389  1.7       chs  * accessible to others.
    390  1.7       chs  */
    391  1.7       chs 
    392  1.7       chs int
    393  1.7       chs ext2fs_balloc_range(vp, off, len, cred, flags)
    394  1.7       chs 	struct vnode *vp;
    395  1.7       chs 	off_t off, len;
    396  1.7       chs 	struct ucred *cred;
    397  1.7       chs 	int flags;
    398  1.7       chs {
    399  1.8       chs 	off_t oldeof, eof, pagestart;
    400  1.7       chs 	struct uvm_object *uobj;
    401  1.8       chs 	int i, delta, error, npages;
    402  1.7       chs 	int bshift = vp->v_mount->mnt_fs_bshift;
    403  1.7       chs 	int bsize = 1 << bshift;
    404  1.7       chs 	int ppb = max(bsize >> PAGE_SHIFT, 1);
    405  1.8       chs 	struct vm_page *pgs[ppb];
    406  1.7       chs 	UVMHIST_FUNC("ext2fs_balloc_range"); UVMHIST_CALLED(ubchist);
    407  1.7       chs 	UVMHIST_LOG(ubchist, "vp %p off 0x%x len 0x%x u_size 0x%x",
    408  1.7       chs 		    vp, off, len, vp->v_uvm.u_size);
    409  1.7       chs 
    410  1.7       chs 	error = 0;
    411  1.7       chs 	uobj = &vp->v_uvm.u_obj;
    412  1.8       chs 	oldeof = vp->v_uvm.u_size;
    413  1.8       chs 	eof = max(oldeof, off + len);
    414  1.7       chs 	UVMHIST_LOG(ubchist, "new eof 0x%x", eof,0,0,0);
    415  1.8       chs 	pgs[0] = NULL;
    416  1.7       chs 
    417  1.7       chs 	/*
    418  1.8       chs 	 * cache the new range of the file.  this will create zeroed pages
    419  1.8       chs 	 * where the new block will be and keep them locked until the
    420  1.8       chs 	 * new block is allocated, so there will be no window where
    421  1.8       chs 	 * the old contents of the new block is visible to racing threads.
    422  1.7       chs 	 */
    423  1.7       chs 
    424  1.7       chs 	pagestart = trunc_page(off) & ~(bsize - 1);
    425  1.8       chs 	npages = min(ppb, (round_page(eof) - pagestart) >> PAGE_SHIFT);
    426  1.8       chs 	memset(pgs, 0, npages);
    427  1.8       chs 	simple_lock(&uobj->vmobjlock);
    428  1.8       chs 	error = VOP_GETPAGES(vp, pagestart, pgs, &npages, 0,
    429  1.8       chs 	    VM_PROT_READ, 0, PGO_SYNCIO | PGO_PASTEOF);
    430  1.8       chs 	if (error) {
    431  1.8       chs 		UVMHIST_LOG(ubchist, "getpages %d", error,0,0,0);
    432  1.8       chs 		goto errout;
    433  1.8       chs 	}
    434  1.8       chs 	for (i = 0; i < npages; i++) {
    435  1.8       chs 		UVMHIST_LOG(ubchist, "got pgs[%d] %p", i, pgs[i],0,0);
    436  1.8       chs 		KASSERT((pgs[i]->flags & PG_RELEASED) == 0);
    437  1.8       chs 		pgs[i]->flags &= ~PG_CLEAN;
    438  1.8       chs 		uvm_pageactivate(pgs[i]);
    439  1.7       chs 	}
    440  1.7       chs 
    441  1.7       chs 	/*
    442  1.7       chs 	 * adjust off to be block-aligned.
    443  1.7       chs 	 */
    444  1.7       chs 
    445  1.7       chs 	delta = off & (bsize - 1);
    446  1.7       chs 	off -= delta;
    447  1.7       chs 	len += delta;
    448  1.7       chs 
    449  1.7       chs 	/*
    450  1.7       chs 	 * now allocate the range.
    451  1.7       chs 	 */
    452  1.7       chs 
    453  1.7       chs 	lockmgr(&vp->v_glock, LK_EXCLUSIVE, NULL);
    454  1.7       chs 	error = VOP_BALLOCN(vp, off, len, cred, flags);
    455  1.7       chs 	UVMHIST_LOG(ubchist, "ballocn %d", error,0,0,0);
    456  1.7       chs 	lockmgr(&vp->v_glock, LK_RELEASE, NULL);
    457  1.7       chs 
    458  1.7       chs 	/*
    459  1.7       chs 	 * unbusy any pages we are holding.
    460  1.7       chs 	 */
    461  1.7       chs 
    462  1.7       chs errout:
    463  1.7       chs 	simple_lock(&uobj->vmobjlock);
    464  1.8       chs 	if (error) {
    465  1.8       chs 		(void) (uobj->pgops->pgo_flush)(uobj, oldeof, pagestart + ppb,
    466  1.8       chs 		    PGO_FREE);
    467  1.7       chs 	}
    468  1.8       chs 	if (pgs[0] != NULL) {
    469  1.8       chs 		uvm_page_unbusy(pgs, npages);
    470  1.7       chs 	}
    471  1.7       chs 	simple_unlock(&uobj->vmobjlock);
    472  1.5   mycroft 	return (error);
    473  1.1    bouyer }
    474