xref: /netbsd-src/sys/ufs/ext2fs/ext2fs_balloc.c (revision 9fc453562f6ebe8eabdfd51e21ae0a0058906d4f)
1*9fc45356Sriastradh /*	$NetBSD: ext2fs_balloc.c,v 1.43 2020/09/05 16:30:13 riastradh Exp $	*/
2aad01611Sagc 
3aad01611Sagc /*
4aad01611Sagc  * Copyright (c) 1982, 1986, 1989, 1993
5aad01611Sagc  *	The Regents of the University of California.  All rights reserved.
6aad01611Sagc  *
7aad01611Sagc  * Redistribution and use in source and binary forms, with or without
8aad01611Sagc  * modification, are permitted provided that the following conditions
9aad01611Sagc  * are met:
10aad01611Sagc  * 1. Redistributions of source code must retain the above copyright
11aad01611Sagc  *    notice, this list of conditions and the following disclaimer.
12aad01611Sagc  * 2. Redistributions in binary form must reproduce the above copyright
13aad01611Sagc  *    notice, this list of conditions and the following disclaimer in the
14aad01611Sagc  *    documentation and/or other materials provided with the distribution.
15aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
16aad01611Sagc  *    may be used to endorse or promote products derived from this software
17aad01611Sagc  *    without specific prior written permission.
18aad01611Sagc  *
19aad01611Sagc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20aad01611Sagc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21aad01611Sagc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22aad01611Sagc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23aad01611Sagc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24aad01611Sagc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25aad01611Sagc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26aad01611Sagc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27aad01611Sagc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28aad01611Sagc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29aad01611Sagc  * SUCH DAMAGE.
30aad01611Sagc  *
31aad01611Sagc  *	@(#)ffs_balloc.c	8.4 (Berkeley) 9/23/93
32aad01611Sagc  * Modified for ext2fs by Manuel Bouyer.
33aad01611Sagc  */
349e3c291dSbouyer 
359e3c291dSbouyer /*
369e3c291dSbouyer  * Copyright (c) 1997 Manuel Bouyer.
379e3c291dSbouyer  *
389e3c291dSbouyer  * Redistribution and use in source and binary forms, with or without
399e3c291dSbouyer  * modification, are permitted provided that the following conditions
409e3c291dSbouyer  * are met:
419e3c291dSbouyer  * 1. Redistributions of source code must retain the above copyright
429e3c291dSbouyer  *    notice, this list of conditions and the following disclaimer.
439e3c291dSbouyer  * 2. Redistributions in binary form must reproduce the above copyright
449e3c291dSbouyer  *    notice, this list of conditions and the following disclaimer in the
459e3c291dSbouyer  *    documentation and/or other materials provided with the distribution.
469e3c291dSbouyer  *
4735decc6eSbouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
4835decc6eSbouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
4935decc6eSbouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
5035decc6eSbouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
5135decc6eSbouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
5235decc6eSbouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5335decc6eSbouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5435decc6eSbouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5535decc6eSbouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
5635decc6eSbouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
579e3c291dSbouyer  *
589e3c291dSbouyer  *	@(#)ffs_balloc.c	8.4 (Berkeley) 9/23/93
599e3c291dSbouyer  * Modified for ext2fs by Manuel Bouyer.
609e3c291dSbouyer  */
619e3c291dSbouyer 
62ec624546Slukem #include <sys/cdefs.h>
63*9fc45356Sriastradh __KERNEL_RCSID(0, "$NetBSD: ext2fs_balloc.c,v 1.43 2020/09/05 16:30:13 riastradh Exp $");
64ec624546Slukem 
6567afbd62Smrg #if defined(_KERNEL_OPT)
66aeda8d3bSchs #include "opt_uvmhist.h"
67aeda8d3bSchs #endif
68aeda8d3bSchs 
699e3c291dSbouyer #include <sys/param.h>
709e3c291dSbouyer #include <sys/systm.h>
719e3c291dSbouyer #include <sys/buf.h>
729e3c291dSbouyer #include <sys/proc.h>
739e3c291dSbouyer #include <sys/file.h>
749e3c291dSbouyer #include <sys/vnode.h>
75aeda8d3bSchs #include <sys/mount.h>
76fc9422c9Selad #include <sys/kauth.h>
77aeda8d3bSchs 
78*9fc45356Sriastradh #ifdef UVMHIST
79aeda8d3bSchs #include <uvm/uvm.h>
80*9fc45356Sriastradh #endif
81*9fc45356Sriastradh #include <uvm/uvm_stat.h>
829e3c291dSbouyer 
839e3c291dSbouyer #include <ufs/ufs/inode.h>
849e3c291dSbouyer #include <ufs/ufs/ufs_extern.h>
859e3c291dSbouyer 
869e3c291dSbouyer #include <ufs/ext2fs/ext2fs.h>
879e3c291dSbouyer #include <ufs/ext2fs/ext2fs_extern.h>
889e3c291dSbouyer 
899e3c291dSbouyer /*
909e3c291dSbouyer  * Balloc defines the structure of file system storage
919e3c291dSbouyer  * by allocating the physical blocks on a device given
929e3c291dSbouyer  * the inode and the logical block number in a file.
939e3c291dSbouyer  */
949e3c291dSbouyer int
ext2fs_balloc(struct inode * ip,daddr_t bn,int size,kauth_cred_t cred,struct buf ** bpp,int flags)95168cd830Schristos ext2fs_balloc(struct inode *ip, daddr_t bn, int size,
964d595fd7Schristos     kauth_cred_t cred, struct buf **bpp, int flags)
979e3c291dSbouyer {
98169ac5b3Saugustss 	struct m_ext2fs *fs;
99a3ff3a30Sfvdl 	daddr_t nb;
1009e3c291dSbouyer 	struct buf *bp, *nbp;
1019e3c291dSbouyer 	struct vnode *vp = ITOV(ip);
102dcd34a91Sdholland 	struct indir indirs[EXT2FS_NIADDR + 2];
103a3ff3a30Sfvdl 	daddr_t newb, lbn, pref;
104a3ff3a30Sfvdl 	int32_t *bap;	/* XXX ondisk32 */
1059e3c291dSbouyer 	int num, i, error;
106e5bc90f4Sfvdl 	u_int deallocated;
107dcd34a91Sdholland 	daddr_t *blkp, *allocblk, allociblk[EXT2FS_NIADDR + 1];
108a3ff3a30Sfvdl 	int32_t *allocib;	/* XXX ondisk32 */
109dad58e31Smycroft 	int unwindidx = -1;
110aeda8d3bSchs 	UVMHIST_FUNC("ext2fs_balloc"); UVMHIST_CALLED(ubchist);
1119e3c291dSbouyer 
112aeda8d3bSchs 	UVMHIST_LOG(ubchist, "bn 0x%x", bn, 0, 0, 0);
113aeda8d3bSchs 
114aeda8d3bSchs 	if (bpp != NULL) {
1159e3c291dSbouyer 		*bpp = NULL;
116aeda8d3bSchs 	}
1179e3c291dSbouyer 	if (bn < 0)
11801d303e7Schristos 		return EFBIG;
1199e3c291dSbouyer 	fs = ip->i_e2fs;
1209e3c291dSbouyer 	lbn = bn;
1219e3c291dSbouyer 
1229e3c291dSbouyer 	/*
123dcd34a91Sdholland 	 * The first EXT2FS_NDADDR blocks are direct blocks
1249e3c291dSbouyer 	 */
125dcd34a91Sdholland 	if (bn < EXT2FS_NDADDR) {
126a3ff3a30Sfvdl 		/* XXX ondisk32 */
1276ab3092bSbouyer 		nb = fs2h32(ip->i_e2fs_blocks[bn]);
1289e3c291dSbouyer 		if (nb != 0) {
129aeda8d3bSchs 
130aeda8d3bSchs 			/*
131aeda8d3bSchs 			 * the block is already allocated, just read it.
132aeda8d3bSchs 			 */
133aeda8d3bSchs 
134aeda8d3bSchs 			if (bpp != NULL) {
1356e392401Smaxv 				error = bread(vp, bn, fs->e2fs_bsize,
1365d2bff06Shannken 					      B_MODIFY, &bp);
1379e3c291dSbouyer 				if (error) {
13801d303e7Schristos 					return error;
1399e3c291dSbouyer 				}
1409e3c291dSbouyer 				*bpp = bp;
141aeda8d3bSchs 			}
14201d303e7Schristos 			return 0;
143aeda8d3bSchs 		}
144aeda8d3bSchs 
145aeda8d3bSchs 		/*
146aeda8d3bSchs 		 * allocate a new direct block.
147aeda8d3bSchs 		 */
148aeda8d3bSchs 
1499e3c291dSbouyer 		error = ext2fs_alloc(ip, bn,
150aeda8d3bSchs 		    ext2fs_blkpref(ip, bn, bn, &ip->i_e2fs_blocks[0]),
1519e3c291dSbouyer 		    cred, &newb);
1529e3c291dSbouyer 		if (error)
15301d303e7Schristos 			return error;
1549e3c291dSbouyer 		ip->i_e2fs_last_lblk = lbn;
1559e3c291dSbouyer 		ip->i_e2fs_last_blk = newb;
156a3ff3a30Sfvdl 		/* XXX ondisk32 */
157a3ff3a30Sfvdl 		ip->i_e2fs_blocks[bn] = h2fs32((int32_t)newb);
158aeda8d3bSchs 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
159aeda8d3bSchs 		if (bpp != NULL) {
1609e3c291dSbouyer 			bp = getblk(vp, bn, fs->e2fs_bsize, 0, 0);
1612737439dSdholland 			bp->b_blkno = EXT2_FSBTODB(fs, newb);
1629e3c291dSbouyer 			if (flags & B_CLRBUF)
1639e3c291dSbouyer 				clrbuf(bp);
1649e3c291dSbouyer 			*bpp = bp;
165aeda8d3bSchs 		}
16601d303e7Schristos 		return 0;
1679e3c291dSbouyer 	}
1689e3c291dSbouyer 	/*
1699e3c291dSbouyer 	 * Determine the number of levels of indirection.
1709e3c291dSbouyer 	 */
1719e3c291dSbouyer 	pref = 0;
1729e3c291dSbouyer 	if ((error = ufs_getlbns(vp, bn, indirs, &num)) != 0)
17301d303e7Schristos 		return error;
1749e3c291dSbouyer #ifdef DIAGNOSTIC
1759e3c291dSbouyer 	if (num < 1)
17601d303e7Schristos 		panic("%s: ufs_getlbns returned indirect block\n", __func__);
1779e3c291dSbouyer #endif
1789e3c291dSbouyer 	/*
1799e3c291dSbouyer 	 * Fetch the first indirect block allocating if necessary.
1809e3c291dSbouyer 	 */
1819e3c291dSbouyer 	--num;
182a3ff3a30Sfvdl 	/* XXX ondisk32 */
183dcd34a91Sdholland 	nb = fs2h32(ip->i_e2fs_blocks[EXT2FS_NDADDR + indirs[0].in_off]);
184e5bc90f4Sfvdl 	allocib = NULL;
185e5bc90f4Sfvdl 	allocblk = allociblk;
1869e3c291dSbouyer 	if (nb == 0) {
187a3ff3a30Sfvdl 		pref = ext2fs_blkpref(ip, lbn, 0, (int32_t *)0);
188dad58e31Smycroft 		error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
1899e3c291dSbouyer 		if (error)
19001d303e7Schristos 			return error;
1919e3c291dSbouyer 		nb = newb;
192e5bc90f4Sfvdl 		*allocblk++ = nb;
1939e3c291dSbouyer 		ip->i_e2fs_last_blk = newb;
1949e3c291dSbouyer 		bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0);
1952737439dSdholland 		bp->b_blkno = EXT2_FSBTODB(fs, newb);
1969e3c291dSbouyer 		clrbuf(bp);
1979e3c291dSbouyer 		/*
1989e3c291dSbouyer 		 * Write synchronously so that indirect blocks
1999e3c291dSbouyer 		 * never point at garbage.
2009e3c291dSbouyer 		 */
201e5bc90f4Sfvdl 		if ((error = bwrite(bp)) != 0)
202e5bc90f4Sfvdl 			goto fail;
203dad58e31Smycroft 		unwindidx = 0;
204dcd34a91Sdholland 		allocib = &ip->i_e2fs_blocks[EXT2FS_NDADDR + indirs[0].in_off];
205a3ff3a30Sfvdl 		/* XXX ondisk32 */
206a3ff3a30Sfvdl 		*allocib = h2fs32((int32_t)newb);
2079e3c291dSbouyer 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
2089e3c291dSbouyer 	}
2099e3c291dSbouyer 	/*
2109e3c291dSbouyer 	 * Fetch through the indirect blocks, allocating as necessary.
2119e3c291dSbouyer 	 */
2129e3c291dSbouyer 	for (i = 1;;) {
2139e3c291dSbouyer 		error = bread(vp,
2146e392401Smaxv 		    indirs[i].in_lbn, (int)fs->e2fs_bsize, 0, &bp);
2159e3c291dSbouyer 		if (error) {
216e5bc90f4Sfvdl 			goto fail;
2179e3c291dSbouyer 		}
218a3ff3a30Sfvdl 		bap = (int32_t *)bp->b_data;	/* XXX ondisk32 */
2196ab3092bSbouyer 		nb = fs2h32(bap[indirs[i].in_off]);
2209e3c291dSbouyer 		if (i == num)
2219e3c291dSbouyer 			break;
222dad58e31Smycroft 		i++;
2239e3c291dSbouyer 		if (nb != 0) {
2245c3b2b3fSad 			brelse(bp, 0);
2259e3c291dSbouyer 			continue;
2269e3c291dSbouyer 		}
227a3ff3a30Sfvdl 		pref = ext2fs_blkpref(ip, lbn, 0, (int32_t *)0);
228dad58e31Smycroft 		error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
2299e3c291dSbouyer 		if (error) {
2305c3b2b3fSad 			brelse(bp, 0);
231e5bc90f4Sfvdl 			goto fail;
2329e3c291dSbouyer 		}
2339e3c291dSbouyer 		nb = newb;
234e5bc90f4Sfvdl 		*allocblk++ = nb;
2359e3c291dSbouyer 		ip->i_e2fs_last_blk = newb;
2369e3c291dSbouyer 		nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0);
2372737439dSdholland 		nbp->b_blkno = EXT2_FSBTODB(fs, nb);
2389e3c291dSbouyer 		clrbuf(nbp);
2399e3c291dSbouyer 		/*
2409e3c291dSbouyer 		 * Write synchronously so that indirect blocks
2419e3c291dSbouyer 		 * never point at garbage.
2429e3c291dSbouyer 		 */
2439e3c291dSbouyer 		if ((error = bwrite(nbp)) != 0) {
2445c3b2b3fSad 			brelse(bp, 0);
245e5bc90f4Sfvdl 			goto fail;
2469e3c291dSbouyer 		}
247dad58e31Smycroft 		if (unwindidx < 0)
248dad58e31Smycroft 			unwindidx = i - 1;
249a3ff3a30Sfvdl 		/* XXX ondisk32 */
250a3ff3a30Sfvdl 		bap[indirs[i - 1].in_off] = h2fs32((int32_t)nb);
2519e3c291dSbouyer 		/*
2529e3c291dSbouyer 		 * If required, write synchronously, otherwise use
2539e3c291dSbouyer 		 * delayed write.
2549e3c291dSbouyer 		 */
2559e3c291dSbouyer 		if (flags & B_SYNC) {
2569e3c291dSbouyer 			bwrite(bp);
2579e3c291dSbouyer 		} else {
2589e3c291dSbouyer 			bdwrite(bp);
2599e3c291dSbouyer 		}
2609e3c291dSbouyer 	}
2619e3c291dSbouyer 	/*
2629e3c291dSbouyer 	 * Get the data block, allocating if necessary.
2639e3c291dSbouyer 	 */
2649e3c291dSbouyer 	if (nb == 0) {
265dad58e31Smycroft 		pref = ext2fs_blkpref(ip, lbn, indirs[num].in_off, &bap[0]);
266dad58e31Smycroft 		error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
2679e3c291dSbouyer 		if (error) {
2685c3b2b3fSad 			brelse(bp, 0);
269e5bc90f4Sfvdl 			goto fail;
2709e3c291dSbouyer 		}
2719e3c291dSbouyer 		nb = newb;
272e5bc90f4Sfvdl 		*allocblk++ = nb;
2739e3c291dSbouyer 		ip->i_e2fs_last_lblk = lbn;
2749e3c291dSbouyer 		ip->i_e2fs_last_blk = newb;
275a3ff3a30Sfvdl 		/* XXX ondisk32 */
276a3ff3a30Sfvdl 		bap[indirs[num].in_off] = h2fs32((int32_t)nb);
2779e3c291dSbouyer 		/*
2789e3c291dSbouyer 		 * If required, write synchronously, otherwise use
2799e3c291dSbouyer 		 * delayed write.
2809e3c291dSbouyer 		 */
2819e3c291dSbouyer 		if (flags & B_SYNC) {
2829e3c291dSbouyer 			bwrite(bp);
2839e3c291dSbouyer 		} else {
2849e3c291dSbouyer 			bdwrite(bp);
2859e3c291dSbouyer 		}
286aeda8d3bSchs 		if (bpp != NULL) {
287aeda8d3bSchs 			nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
2882737439dSdholland 			nbp->b_blkno = EXT2_FSBTODB(fs, nb);
289aeda8d3bSchs 			if (flags & B_CLRBUF)
290aeda8d3bSchs 				clrbuf(nbp);
2919e3c291dSbouyer 			*bpp = nbp;
292aeda8d3bSchs 		}
29301d303e7Schristos 		return 0;
2949e3c291dSbouyer 	}
2955c3b2b3fSad 	brelse(bp, 0);
296aeda8d3bSchs 	if (bpp != NULL) {
2979e3c291dSbouyer 		if (flags & B_CLRBUF) {
2986e392401Smaxv 			error = bread(vp, lbn, (int)fs->e2fs_bsize,
2995d2bff06Shannken 				      B_MODIFY, &nbp);
3009e3c291dSbouyer 			if (error) {
301dad58e31Smycroft 				goto fail;
3029e3c291dSbouyer 			}
3039e3c291dSbouyer 		} else {
3049e3c291dSbouyer 			nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
3052737439dSdholland 			nbp->b_blkno = EXT2_FSBTODB(fs, nb);
3069e3c291dSbouyer 		}
3079e3c291dSbouyer 		*bpp = nbp;
308aeda8d3bSchs 	}
30901d303e7Schristos 	return 0;
310e5bc90f4Sfvdl fail:
311e5bc90f4Sfvdl 	/*
312e5bc90f4Sfvdl 	 * If we have failed part way through block allocation, we
313e5bc90f4Sfvdl 	 * have to deallocate any indirect blocks that we have allocated.
314e5bc90f4Sfvdl 	 */
315e5bc90f4Sfvdl 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
316e5bc90f4Sfvdl 		ext2fs_blkfree(ip, *blkp);
317e5bc90f4Sfvdl 		deallocated += fs->e2fs_bsize;
318e5bc90f4Sfvdl 	}
319dad58e31Smycroft 	if (unwindidx >= 0) {
320dad58e31Smycroft 		if (unwindidx == 0) {
321e5bc90f4Sfvdl 			*allocib = 0;
322dad58e31Smycroft 		} else {
323dad58e31Smycroft 			int r;
324dad58e31Smycroft 
325dad58e31Smycroft 			r = bread(vp, indirs[unwindidx].in_lbn,
3266e392401Smaxv 			    (int)fs->e2fs_bsize, B_MODIFY, &bp);
327dad58e31Smycroft 			if (r) {
32801d303e7Schristos 				panic("%s: Could not unwind indirect block, "
32901d303e7Schristos 				    "error %d", __func__, r);
330dad58e31Smycroft 			} else {
331a3ff3a30Sfvdl 				bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
332dad58e31Smycroft 				bap[indirs[unwindidx].in_off] = 0;
333dad58e31Smycroft 				if (flags & B_SYNC)
334dad58e31Smycroft 					bwrite(bp);
335dad58e31Smycroft 				else
336dad58e31Smycroft 					bdwrite(bp);
337dad58e31Smycroft 			}
338dad58e31Smycroft 		}
339dad58e31Smycroft 		for (i = unwindidx + 1; i <= num; i++) {
340dad58e31Smycroft 			bp = getblk(vp, indirs[i].in_lbn, (int)fs->e2fs_bsize,
341dad58e31Smycroft 			    0, 0);
3425c3b2b3fSad 			brelse(bp, BC_INVAL);
343dad58e31Smycroft 		}
344dad58e31Smycroft 	}
345e5bc90f4Sfvdl 	if (deallocated) {
3464ca1f268Sjakllsch 		ext2fs_setnblock(ip, ext2fs_nblock(ip) - btodb(deallocated));
347e5bc90f4Sfvdl 		ip->i_e2fs_flags |= IN_CHANGE | IN_UPDATE;
348e5bc90f4Sfvdl 	}
349aeda8d3bSchs 	return error;
350aeda8d3bSchs }
351aeda8d3bSchs 
352aeda8d3bSchs int
ext2fs_gop_alloc(struct vnode * vp,off_t off,off_t len,int flags,kauth_cred_t cred)35364c6d1d2Schs ext2fs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
354fc9422c9Selad     kauth_cred_t cred)
355aeda8d3bSchs {
356aeda8d3bSchs 	struct inode *ip = VTOI(vp);
357aeda8d3bSchs 	struct m_ext2fs *fs = ip->i_e2fs;
358aeda8d3bSchs 	int error, delta, bshift, bsize;
35964c6d1d2Schs 	UVMHIST_FUNC("ext2fs_gop_alloc"); UVMHIST_CALLED(ubchist);
360aeda8d3bSchs 
361aeda8d3bSchs 	bshift = fs->e2fs_bshift;
362aeda8d3bSchs 	bsize = 1 << bshift;
363aeda8d3bSchs 
364aeda8d3bSchs 	delta = off & (bsize - 1);
365aeda8d3bSchs 	off -= delta;
366aeda8d3bSchs 	len += delta;
367aeda8d3bSchs 
368aeda8d3bSchs 	while (len > 0) {
369d1579b2dSriastradh 		bsize = uimin(bsize, len);
370aeda8d3bSchs 		UVMHIST_LOG(ubchist, "off 0x%x len 0x%x bsize 0x%x",
371aeda8d3bSchs 			    off, len, bsize, 0);
372aeda8d3bSchs 
373e1610ba4Sdholland 		error = ext2fs_balloc(ip, ext2_lblkno(fs, off), bsize, cred,
37464c6d1d2Schs 		    NULL, flags);
375aeda8d3bSchs 		if (error) {
376aeda8d3bSchs 			UVMHIST_LOG(ubchist, "error %d", error, 0, 0, 0);
377aeda8d3bSchs 			return error;
378aeda8d3bSchs 		}
379aeda8d3bSchs 
380aeda8d3bSchs 		/*
381a748ea88Syamt 		 * increase file size now, ext2fs_balloc() requires that
382aeda8d3bSchs 		 * EOF be up-to-date before each call.
383aeda8d3bSchs 		 */
384aeda8d3bSchs 
3855387f721Sws 		if (ext2fs_size(ip) < off + bsize) {
3865387f721Sws 			UVMHIST_LOG(ubchist, "old 0x%lx%8lx new 0x%lx%8lx",
3875387f721Sws 			    /* Note that arguments are always cast to u_long. */
3885387f721Sws 				    ext2fs_size(ip) >> 32,
3895387f721Sws 				    ext2fs_size(ip) & 0xffffffff,
3905387f721Sws 				    (off + bsize) >> 32,
3915387f721Sws 				    (off + bsize) & 0xffffffff);
3925387f721Sws 			error = ext2fs_setsize(ip, off + bsize);
3935387f721Sws 			if (error) {
39401d303e7Schristos 				UVMHIST_LOG(ubchist, "error %d",
39501d303e7Schristos 				    error, 0, 0, 0);
3965387f721Sws 				return error;
3975387f721Sws 			}
398aeda8d3bSchs 		}
399aeda8d3bSchs 
400aeda8d3bSchs 		off += bsize;
401aeda8d3bSchs 		len -= bsize;
402aeda8d3bSchs 	}
403aeda8d3bSchs 	return 0;
404aeda8d3bSchs }
405