xref: /openbsd-src/sys/ufs/ext2fs/ext2fs_balloc.c (revision 570df5c46ec138c3c01140bbdb153f33c5d6314c)
1*570df5c4Scheloha /*	$OpenBSD: ext2fs_balloc.c,v 1.27 2019/07/19 00:24:31 cheloha Exp $	*/
21414b0faSart /*	$NetBSD: ext2fs_balloc.c,v 1.10 2001/07/04 21:16:01 chs Exp $	*/
35ac2d602Sdownsj 
45ac2d602Sdownsj /*
51f3ff51cSdownsj  * Copyright (c) 1997 Manuel Bouyer.
65ac2d602Sdownsj  * Copyright (c) 1982, 1986, 1989, 1993
75ac2d602Sdownsj  *	The Regents of the University of California.  All rights reserved.
85ac2d602Sdownsj  *
95ac2d602Sdownsj  * Redistribution and use in source and binary forms, with or without
105ac2d602Sdownsj  * modification, are permitted provided that the following conditions
115ac2d602Sdownsj  * are met:
125ac2d602Sdownsj  * 1. Redistributions of source code must retain the above copyright
135ac2d602Sdownsj  *    notice, this list of conditions and the following disclaimer.
145ac2d602Sdownsj  * 2. Redistributions in binary form must reproduce the above copyright
155ac2d602Sdownsj  *    notice, this list of conditions and the following disclaimer in the
165ac2d602Sdownsj  *    documentation and/or other materials provided with the distribution.
1729295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
185ac2d602Sdownsj  *    may be used to endorse or promote products derived from this software
195ac2d602Sdownsj  *    without specific prior written permission.
205ac2d602Sdownsj  *
215ac2d602Sdownsj  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
225ac2d602Sdownsj  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235ac2d602Sdownsj  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245ac2d602Sdownsj  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
255ac2d602Sdownsj  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265ac2d602Sdownsj  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275ac2d602Sdownsj  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285ac2d602Sdownsj  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295ac2d602Sdownsj  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305ac2d602Sdownsj  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315ac2d602Sdownsj  * SUCH DAMAGE.
325ac2d602Sdownsj  *
335ac2d602Sdownsj  *	@(#)ffs_balloc.c	8.4 (Berkeley) 9/23/93
341f3ff51cSdownsj  * Modified for ext2fs by Manuel Bouyer.
355ac2d602Sdownsj  */
365ac2d602Sdownsj 
375ac2d602Sdownsj #include <sys/param.h>
385ac2d602Sdownsj #include <sys/systm.h>
395ac2d602Sdownsj #include <sys/buf.h>
405ac2d602Sdownsj #include <sys/vnode.h>
415ac2d602Sdownsj 
425ac2d602Sdownsj #include <ufs/ufs/quota.h>
435ac2d602Sdownsj #include <ufs/ufs/inode.h>
445ac2d602Sdownsj #include <ufs/ufs/ufs_extern.h>
455ac2d602Sdownsj 
465ac2d602Sdownsj #include <ufs/ext2fs/ext2fs.h>
475ac2d602Sdownsj #include <ufs/ext2fs/ext2fs_extern.h>
485ac2d602Sdownsj 
495ac2d602Sdownsj /*
505ac2d602Sdownsj  * Balloc defines the structure of file system storage
515ac2d602Sdownsj  * by allocating the physical blocks on a device given
525ac2d602Sdownsj  * the inode and the logical block number in a file.
535ac2d602Sdownsj  */
545ac2d602Sdownsj int
ext2fs_buf_alloc(struct inode * ip,u_int32_t bn,int size,struct ucred * cred,struct buf ** bpp,int flags)555970a65fSpelikan ext2fs_buf_alloc(struct inode *ip, u_int32_t bn, int size, struct ucred *cred,
56b080ad39Scsapuntz     struct buf **bpp, int flags)
575ac2d602Sdownsj {
58b080ad39Scsapuntz 	struct m_ext2fs *fs;
595ac2d602Sdownsj 	struct buf *bp, *nbp;
605ac2d602Sdownsj 	struct vnode *vp = ITOV(ip);
615ac2d602Sdownsj 	struct indir indirs[NIADDR + 2];
625970a65fSpelikan 	u_int32_t nb, newb, *bap;
635ac2d602Sdownsj 	int num, i, error;
64bb9b8324Sart 	u_int deallocated;
655970a65fSpelikan 	u_int32_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
66bb9b8324Sart 	int unwindidx = -1;
671abdbfdeSderaadt 	daddr_t lbn, pref;
685ac2d602Sdownsj 
695ac2d602Sdownsj 	*bpp = NULL;
705ac2d602Sdownsj 	fs = ip->i_e2fs;
715ac2d602Sdownsj 	lbn = bn;
725ac2d602Sdownsj 
735ac2d602Sdownsj 	/*
745ac2d602Sdownsj 	 * The first NDADDR blocks are direct blocks
755ac2d602Sdownsj 	 */
765ac2d602Sdownsj 	if (bn < NDADDR) {
77f7dbefaaSpelikan 		nb = letoh32(ip->i_e2fs_blocks[bn]);
785ac2d602Sdownsj 		if (nb != 0) {
7993f62a9eStedu 			error = bread(vp, bn, fs->e2fs_bsize, &bp);
805ac2d602Sdownsj 			if (error) {
815ac2d602Sdownsj 				brelse(bp);
825ac2d602Sdownsj 				return (error);
835ac2d602Sdownsj 			}
845ac2d602Sdownsj 			*bpp = bp;
855ac2d602Sdownsj 			return (0);
86bb9b8324Sart 		}
87bb9b8324Sart 
88bb9b8324Sart 		/*
89bb9b8324Sart 		 * allocate a new direct block.
90bb9b8324Sart 		 */
915ac2d602Sdownsj 		error = ext2fs_alloc(ip, bn,
921414b0faSart 		    ext2fs_blkpref(ip, bn, (int)bn, &ip->i_e2fs_blocks[0]),
935ac2d602Sdownsj 		    cred, &newb);
945ac2d602Sdownsj 		if (error)
955ac2d602Sdownsj 			return (error);
965ac2d602Sdownsj 		ip->i_e2fs_last_lblk = lbn;
975ac2d602Sdownsj 		ip->i_e2fs_last_blk = newb;
98f7dbefaaSpelikan 		ip->i_e2fs_blocks[bn] = htole32(newb);
99bb9b8324Sart 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
100*570df5c4Scheloha 		bp = getblk(vp, bn, fs->e2fs_bsize, 0, INFSLP);
1015ac2d602Sdownsj 		bp->b_blkno = fsbtodb(fs, newb);
1025ac2d602Sdownsj 		if (flags & B_CLRBUF)
1035ac2d602Sdownsj 			clrbuf(bp);
1045ac2d602Sdownsj 		*bpp = bp;
1055ac2d602Sdownsj 		return (0);
1065ac2d602Sdownsj 	}
1075ac2d602Sdownsj 	/*
1085ac2d602Sdownsj 	 * Determine the number of levels of indirection.
1095ac2d602Sdownsj 	 */
1105ac2d602Sdownsj 	pref = 0;
1115ac2d602Sdownsj 	if ((error = ufs_getlbns(vp, bn, indirs, &num)) != 0)
1125ac2d602Sdownsj 		return(error);
1135ac2d602Sdownsj #ifdef DIAGNOSTIC
1145ac2d602Sdownsj 	if (num < 1)
11530ada397Smillert 		panic ("ext2fs_balloc: ufs_getlbns returned indirect block");
1165ac2d602Sdownsj #endif
1175ac2d602Sdownsj 	/*
1185ac2d602Sdownsj 	 * Fetch the first indirect block allocating if necessary.
1195ac2d602Sdownsj 	 */
1205ac2d602Sdownsj 	--num;
121f7dbefaaSpelikan 	nb = letoh32(ip->i_e2fs_blocks[NDADDR + indirs[0].in_off]);
122bb9b8324Sart 	allocib = NULL;
123bb9b8324Sart 	allocblk = allociblk;
1245ac2d602Sdownsj 	if (nb == 0) {
1255970a65fSpelikan 		pref = ext2fs_blkpref(ip, lbn, 0, NULL);
126bb9b8324Sart 		error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
1275ac2d602Sdownsj 		if (error)
1285ac2d602Sdownsj 			return (error);
1295ac2d602Sdownsj 		nb = newb;
130bb9b8324Sart 		*allocblk++ = nb;
1315ac2d602Sdownsj 		ip->i_e2fs_last_blk = newb;
132*570df5c4Scheloha 		bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, INFSLP);
1335ac2d602Sdownsj 		bp->b_blkno = fsbtodb(fs, newb);
1345ac2d602Sdownsj 		clrbuf(bp);
1355ac2d602Sdownsj 		/*
1365ac2d602Sdownsj 		 * Write synchronously so that indirect blocks
1375ac2d602Sdownsj 		 * never point at garbage.
1385ac2d602Sdownsj 		 */
139bb9b8324Sart 		if ((error = bwrite(bp)) != 0)
140bb9b8324Sart 			goto fail;
141bb9b8324Sart 		unwindidx = 0;
142bb9b8324Sart 		allocib = &ip->i_e2fs_blocks[NDADDR + indirs[0].in_off];
143f7dbefaaSpelikan 		*allocib = htole32(newb);
1445ac2d602Sdownsj 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
1455ac2d602Sdownsj 	}
1465ac2d602Sdownsj 	/*
1475ac2d602Sdownsj 	 * Fetch through the indirect blocks, allocating as necessary.
1485ac2d602Sdownsj 	 */
1495ac2d602Sdownsj 	for (i = 1;;) {
15093f62a9eStedu 		error = bread(vp, indirs[i].in_lbn, (int)fs->e2fs_bsize, &bp);
1515ac2d602Sdownsj 		if (error) {
1525ac2d602Sdownsj 			brelse(bp);
153bb9b8324Sart 			goto fail;
1545ac2d602Sdownsj 		}
1555970a65fSpelikan 		bap = (u_int32_t *)bp->b_data;
156f7dbefaaSpelikan 		nb = letoh32(bap[indirs[i].in_off]);
1575ac2d602Sdownsj 		if (i == num)
1585ac2d602Sdownsj 			break;
159bb9b8324Sart 		i++;
1605ac2d602Sdownsj 		if (nb != 0) {
1615ac2d602Sdownsj 			brelse(bp);
1625ac2d602Sdownsj 			continue;
1635ac2d602Sdownsj 		}
1645970a65fSpelikan 		pref = ext2fs_blkpref(ip, lbn, 0, NULL);
165bb9b8324Sart 		error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
1665ac2d602Sdownsj 		if (error) {
1675ac2d602Sdownsj 			brelse(bp);
168bb9b8324Sart 			goto fail;
1695ac2d602Sdownsj 		}
1705ac2d602Sdownsj 		nb = newb;
171bb9b8324Sart 		*allocblk++ = nb;
1725ac2d602Sdownsj 		ip->i_e2fs_last_blk = newb;
173*570df5c4Scheloha 		nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, INFSLP);
1745ac2d602Sdownsj 		nbp->b_blkno = fsbtodb(fs, nb);
1755ac2d602Sdownsj 		clrbuf(nbp);
1765ac2d602Sdownsj 		/*
1775ac2d602Sdownsj 		 * Write synchronously so that indirect blocks
1785ac2d602Sdownsj 		 * never point at garbage.
1795ac2d602Sdownsj 		 */
1805ac2d602Sdownsj 		if ((error = bwrite(nbp)) != 0) {
1815ac2d602Sdownsj 			brelse(bp);
182bb9b8324Sart 			goto fail;
1835ac2d602Sdownsj 		}
184bb9b8324Sart 		if (unwindidx < 0)
185bb9b8324Sart 			unwindidx = i - 1;
186f7dbefaaSpelikan 		bap[indirs[i - 1].in_off] = htole32(nb);
1875ac2d602Sdownsj 		/*
1885ac2d602Sdownsj 		 * If required, write synchronously, otherwise use
1895ac2d602Sdownsj 		 * delayed write.
1905ac2d602Sdownsj 		 */
1915ac2d602Sdownsj 		if (flags & B_SYNC) {
1925ac2d602Sdownsj 			bwrite(bp);
1935ac2d602Sdownsj 		} else {
1945ac2d602Sdownsj 			bdwrite(bp);
1955ac2d602Sdownsj 		}
1965ac2d602Sdownsj 	}
1975ac2d602Sdownsj 	/*
1985ac2d602Sdownsj 	 * Get the data block, allocating if necessary.
1995ac2d602Sdownsj 	 */
2005ac2d602Sdownsj 	if (nb == 0) {
2015970a65fSpelikan 		pref = ext2fs_blkpref(ip, lbn, indirs[num].in_off, bap);
202bb9b8324Sart 		error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
2035ac2d602Sdownsj 		if (error) {
2045ac2d602Sdownsj 			brelse(bp);
205bb9b8324Sart 			goto fail;
2065ac2d602Sdownsj 		}
2075ac2d602Sdownsj 		nb = newb;
208bb9b8324Sart 		*allocblk++ = nb;
2095ac2d602Sdownsj 		ip->i_e2fs_last_lblk = lbn;
2105ac2d602Sdownsj 		ip->i_e2fs_last_blk = newb;
211f7dbefaaSpelikan 		bap[indirs[num].in_off] = htole32(nb);
2125ac2d602Sdownsj 		/*
2135ac2d602Sdownsj 		 * If required, write synchronously, otherwise use
2145ac2d602Sdownsj 		 * delayed write.
2155ac2d602Sdownsj 		 */
2165ac2d602Sdownsj 		if (flags & B_SYNC) {
2175ac2d602Sdownsj 			bwrite(bp);
2185ac2d602Sdownsj 		} else {
2195ac2d602Sdownsj 			bdwrite(bp);
2205ac2d602Sdownsj 		}
221*570df5c4Scheloha 		nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, INFSLP);
222bb9b8324Sart 		nbp->b_blkno = fsbtodb(fs, nb);
223bb9b8324Sart 		if (flags & B_CLRBUF)
224bb9b8324Sart 			clrbuf(nbp);
2255ac2d602Sdownsj 		*bpp = nbp;
2265ac2d602Sdownsj 		return (0);
2275ac2d602Sdownsj 	}
2285ac2d602Sdownsj 	brelse(bp);
2295ac2d602Sdownsj 	if (flags & B_CLRBUF) {
23093f62a9eStedu 		error = bread(vp, lbn, (int)fs->e2fs_bsize, &nbp);
2315ac2d602Sdownsj 		if (error) {
2325ac2d602Sdownsj 			brelse(nbp);
233bb9b8324Sart 			goto fail;
2345ac2d602Sdownsj 		}
2355ac2d602Sdownsj 	} else {
236*570df5c4Scheloha 		nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, INFSLP);
2375ac2d602Sdownsj 		nbp->b_blkno = fsbtodb(fs, nb);
2385ac2d602Sdownsj 	}
2391414b0faSart 
2405ac2d602Sdownsj 	*bpp = nbp;
2415ac2d602Sdownsj 	return (0);
242bb9b8324Sart fail:
243bb9b8324Sart 	/*
244bb9b8324Sart 	 * If we have failed part way through block allocation, we
245bb9b8324Sart 	 * have to deallocate any indirect blocks that we have allocated.
246bb9b8324Sart 	 */
247bb9b8324Sart 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
248bb9b8324Sart 		ext2fs_blkfree(ip, *blkp);
249bb9b8324Sart 		deallocated += fs->e2fs_bsize;
250bb9b8324Sart 	}
251bb9b8324Sart 	if (unwindidx >= 0) {
252bb9b8324Sart 		if (unwindidx == 0) {
253bb9b8324Sart 			*allocib = 0;
254bb9b8324Sart 		} else {
255bb9b8324Sart 			int r;
256bb9b8324Sart 
257bb9b8324Sart 			r = bread(vp, indirs[unwindidx].in_lbn,
25893f62a9eStedu 			    (int)fs->e2fs_bsize, &bp);
259bb9b8324Sart 			if (r) {
260bb9b8324Sart 				panic("Could not unwind indirect block, error %d", r);
261bb9b8324Sart 			} else {
2625970a65fSpelikan 				bap = (u_int32_t *)bp->b_data;
263bb9b8324Sart 				bap[indirs[unwindidx].in_off] = 0;
264bb9b8324Sart 				if (flags & B_SYNC)
265bb9b8324Sart 					bwrite(bp);
266bb9b8324Sart 				else
267bb9b8324Sart 					bdwrite(bp);
268bb9b8324Sart 			}
269bb9b8324Sart 		}
270bb9b8324Sart 		for (i = unwindidx + 1; i <= num; i++) {
271bb9b8324Sart 			bp = getblk(vp, indirs[i].in_lbn, (int)fs->e2fs_bsize,
272*570df5c4Scheloha 			    0, INFSLP);
273bb9b8324Sart 			bp->b_flags |= B_INVAL;
274bb9b8324Sart 			brelse(bp);
275bb9b8324Sart 		}
276bb9b8324Sart 	}
277bb9b8324Sart 	if (deallocated) {
278bb9b8324Sart 		ip->i_e2fs_nblock -= btodb(deallocated);
279bb9b8324Sart 		ip->i_e2fs_flags |= IN_CHANGE | IN_UPDATE;
280bb9b8324Sart 	}
281bb9b8324Sart 	return error;
2825ac2d602Sdownsj }
283