xref: /aosp_15_r20/external/e2fsprogs/lib/ext2fs/alloc.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * alloc.c --- allocate new inodes, blocks for ext2fs
3*6a54128fSAndroid Build Coastguard Worker  *
4*6a54128fSAndroid Build Coastguard Worker  * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
5*6a54128fSAndroid Build Coastguard Worker  *
6*6a54128fSAndroid Build Coastguard Worker  * %Begin-Header%
7*6a54128fSAndroid Build Coastguard Worker  * This file may be redistributed under the terms of the GNU Library
8*6a54128fSAndroid Build Coastguard Worker  * General Public License, version 2.
9*6a54128fSAndroid Build Coastguard Worker  * %End-Header%
10*6a54128fSAndroid Build Coastguard Worker  */
11*6a54128fSAndroid Build Coastguard Worker 
12*6a54128fSAndroid Build Coastguard Worker #include "config.h"
13*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
14*6a54128fSAndroid Build Coastguard Worker #if HAVE_UNISTD_H
15*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
16*6a54128fSAndroid Build Coastguard Worker #endif
17*6a54128fSAndroid Build Coastguard Worker #include <time.h>
18*6a54128fSAndroid Build Coastguard Worker #include <string.h>
19*6a54128fSAndroid Build Coastguard Worker #if HAVE_SYS_STAT_H
20*6a54128fSAndroid Build Coastguard Worker #include <sys/stat.h>
21*6a54128fSAndroid Build Coastguard Worker #endif
22*6a54128fSAndroid Build Coastguard Worker #if HAVE_SYS_TYPES_H
23*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
24*6a54128fSAndroid Build Coastguard Worker #endif
25*6a54128fSAndroid Build Coastguard Worker 
26*6a54128fSAndroid Build Coastguard Worker #include "ext2_fs.h"
27*6a54128fSAndroid Build Coastguard Worker #include "ext2fs.h"
28*6a54128fSAndroid Build Coastguard Worker 
29*6a54128fSAndroid Build Coastguard Worker #define min(a, b) ((a) < (b) ? (a) : (b))
30*6a54128fSAndroid Build Coastguard Worker 
31*6a54128fSAndroid Build Coastguard Worker #undef DEBUG
32*6a54128fSAndroid Build Coastguard Worker 
33*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
34*6a54128fSAndroid Build Coastguard Worker # define dbg_printf(f, a...)  do {printf(f, ## a); fflush(stdout); } while (0)
35*6a54128fSAndroid Build Coastguard Worker #else
36*6a54128fSAndroid Build Coastguard Worker # define dbg_printf(f, a...)
37*6a54128fSAndroid Build Coastguard Worker #endif
38*6a54128fSAndroid Build Coastguard Worker 
39*6a54128fSAndroid Build Coastguard Worker /*
40*6a54128fSAndroid Build Coastguard Worker  * Clear the uninit block bitmap flag if necessary
41*6a54128fSAndroid Build Coastguard Worker  */
ext2fs_clear_block_uninit(ext2_filsys fs,dgrp_t group)42*6a54128fSAndroid Build Coastguard Worker void ext2fs_clear_block_uninit(ext2_filsys fs, dgrp_t group)
43*6a54128fSAndroid Build Coastguard Worker {
44*6a54128fSAndroid Build Coastguard Worker 	if (group >= fs->group_desc_count ||
45*6a54128fSAndroid Build Coastguard Worker 	    !ext2fs_has_group_desc_csum(fs) ||
46*6a54128fSAndroid Build Coastguard Worker 	    !(ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT)))
47*6a54128fSAndroid Build Coastguard Worker 		return;
48*6a54128fSAndroid Build Coastguard Worker 
49*6a54128fSAndroid Build Coastguard Worker 	/* uninit block bitmaps are now initialized in read_bitmaps() */
50*6a54128fSAndroid Build Coastguard Worker 
51*6a54128fSAndroid Build Coastguard Worker 	ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
52*6a54128fSAndroid Build Coastguard Worker 	ext2fs_group_desc_csum_set(fs, group);
53*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_super_dirty(fs);
54*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_bb_dirty(fs);
55*6a54128fSAndroid Build Coastguard Worker }
56*6a54128fSAndroid Build Coastguard Worker 
57*6a54128fSAndroid Build Coastguard Worker /*
58*6a54128fSAndroid Build Coastguard Worker  * Check for uninit inode bitmaps and deal with them appropriately
59*6a54128fSAndroid Build Coastguard Worker  */
check_inode_uninit(ext2_filsys fs,ext2fs_inode_bitmap map,dgrp_t group)60*6a54128fSAndroid Build Coastguard Worker static void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map,
61*6a54128fSAndroid Build Coastguard Worker 			  dgrp_t group)
62*6a54128fSAndroid Build Coastguard Worker {
63*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t	i, ino;
64*6a54128fSAndroid Build Coastguard Worker 
65*6a54128fSAndroid Build Coastguard Worker 	if (group >= fs->group_desc_count ||
66*6a54128fSAndroid Build Coastguard Worker 	    !ext2fs_has_group_desc_csum(fs) ||
67*6a54128fSAndroid Build Coastguard Worker 	    !(ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
68*6a54128fSAndroid Build Coastguard Worker 		return;
69*6a54128fSAndroid Build Coastguard Worker 
70*6a54128fSAndroid Build Coastguard Worker 	ino = (group * fs->super->s_inodes_per_group) + 1;
71*6a54128fSAndroid Build Coastguard Worker 	for (i=0; i < fs->super->s_inodes_per_group; i++, ino++)
72*6a54128fSAndroid Build Coastguard Worker 		ext2fs_fast_unmark_inode_bitmap2(map, ino);
73*6a54128fSAndroid Build Coastguard Worker 
74*6a54128fSAndroid Build Coastguard Worker 	ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
75*6a54128fSAndroid Build Coastguard Worker 	/* Mimics what the kernel does */
76*6a54128fSAndroid Build Coastguard Worker 	ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
77*6a54128fSAndroid Build Coastguard Worker 	ext2fs_group_desc_csum_set(fs, group);
78*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_ib_dirty(fs);
79*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_super_dirty(fs);
80*6a54128fSAndroid Build Coastguard Worker }
81*6a54128fSAndroid Build Coastguard Worker 
82*6a54128fSAndroid Build Coastguard Worker /*
83*6a54128fSAndroid Build Coastguard Worker  * Right now, just search forward from the parent directory's block
84*6a54128fSAndroid Build Coastguard Worker  * group to find the next free inode.
85*6a54128fSAndroid Build Coastguard Worker  *
86*6a54128fSAndroid Build Coastguard Worker  * Should have a special policy for directories.
87*6a54128fSAndroid Build Coastguard Worker  */
ext2fs_new_inode(ext2_filsys fs,ext2_ino_t dir,int mode EXT2FS_ATTR ((unused)),ext2fs_inode_bitmap map,ext2_ino_t * ret)88*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
89*6a54128fSAndroid Build Coastguard Worker 			   int mode EXT2FS_ATTR((unused)),
90*6a54128fSAndroid Build Coastguard Worker 			   ext2fs_inode_bitmap map, ext2_ino_t *ret)
91*6a54128fSAndroid Build Coastguard Worker {
92*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t	start_inode = 0;
93*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t	i, ino_in_group, upto, first_zero;
94*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
95*6a54128fSAndroid Build Coastguard Worker 	dgrp_t		group;
96*6a54128fSAndroid Build Coastguard Worker 
97*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
98*6a54128fSAndroid Build Coastguard Worker 
99*6a54128fSAndroid Build Coastguard Worker 	if (!map)
100*6a54128fSAndroid Build Coastguard Worker 		map = fs->inode_map;
101*6a54128fSAndroid Build Coastguard Worker 	if (!map)
102*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_NO_INODE_BITMAP;
103*6a54128fSAndroid Build Coastguard Worker 
104*6a54128fSAndroid Build Coastguard Worker 	if (dir > 0) {
105*6a54128fSAndroid Build Coastguard Worker 		group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
106*6a54128fSAndroid Build Coastguard Worker 		start_inode = (group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
107*6a54128fSAndroid Build Coastguard Worker 	}
108*6a54128fSAndroid Build Coastguard Worker 	if (start_inode < EXT2_FIRST_INODE(fs->super))
109*6a54128fSAndroid Build Coastguard Worker 		start_inode = EXT2_FIRST_INODE(fs->super);
110*6a54128fSAndroid Build Coastguard Worker 	if (start_inode > fs->super->s_inodes_count)
111*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_INODE_ALLOC_FAIL;
112*6a54128fSAndroid Build Coastguard Worker 	i = start_inode;
113*6a54128fSAndroid Build Coastguard Worker 	do {
114*6a54128fSAndroid Build Coastguard Worker 		ino_in_group = (i - 1) % EXT2_INODES_PER_GROUP(fs->super);
115*6a54128fSAndroid Build Coastguard Worker 		group = (i - 1) / EXT2_INODES_PER_GROUP(fs->super);
116*6a54128fSAndroid Build Coastguard Worker 
117*6a54128fSAndroid Build Coastguard Worker 		check_inode_uninit(fs, map, group);
118*6a54128fSAndroid Build Coastguard Worker 		upto = i + (EXT2_INODES_PER_GROUP(fs->super) - ino_in_group);
119*6a54128fSAndroid Build Coastguard Worker 		if (i < start_inode && upto >= start_inode)
120*6a54128fSAndroid Build Coastguard Worker 			upto = start_inode - 1;
121*6a54128fSAndroid Build Coastguard Worker 		if (upto > fs->super->s_inodes_count)
122*6a54128fSAndroid Build Coastguard Worker 			upto = fs->super->s_inodes_count;
123*6a54128fSAndroid Build Coastguard Worker 
124*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_find_first_zero_inode_bitmap2(map, i, upto,
125*6a54128fSAndroid Build Coastguard Worker 							      &first_zero);
126*6a54128fSAndroid Build Coastguard Worker 		if (retval == 0) {
127*6a54128fSAndroid Build Coastguard Worker 			i = first_zero;
128*6a54128fSAndroid Build Coastguard Worker 			break;
129*6a54128fSAndroid Build Coastguard Worker 		}
130*6a54128fSAndroid Build Coastguard Worker 		if (retval != ENOENT)
131*6a54128fSAndroid Build Coastguard Worker 			return EXT2_ET_INODE_ALLOC_FAIL;
132*6a54128fSAndroid Build Coastguard Worker 		i = upto + 1;
133*6a54128fSAndroid Build Coastguard Worker 		if (i > fs->super->s_inodes_count)
134*6a54128fSAndroid Build Coastguard Worker 			i = EXT2_FIRST_INODE(fs->super);
135*6a54128fSAndroid Build Coastguard Worker 	} while (i != start_inode);
136*6a54128fSAndroid Build Coastguard Worker 
137*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_test_inode_bitmap2(map, i))
138*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_INODE_ALLOC_FAIL;
139*6a54128fSAndroid Build Coastguard Worker 	*ret = i;
140*6a54128fSAndroid Build Coastguard Worker 	return 0;
141*6a54128fSAndroid Build Coastguard Worker }
142*6a54128fSAndroid Build Coastguard Worker 
143*6a54128fSAndroid Build Coastguard Worker /*
144*6a54128fSAndroid Build Coastguard Worker  * Stupid algorithm --- we now just search forward starting from the
145*6a54128fSAndroid Build Coastguard Worker  * goal.  Should put in a smarter one someday....
146*6a54128fSAndroid Build Coastguard Worker  */
ext2fs_new_block3(ext2_filsys fs,blk64_t goal,ext2fs_block_bitmap map,blk64_t * ret,struct blk_alloc_ctx * ctx)147*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_new_block3(ext2_filsys fs, blk64_t goal,
148*6a54128fSAndroid Build Coastguard Worker 			    ext2fs_block_bitmap map, blk64_t *ret,
149*6a54128fSAndroid Build Coastguard Worker 			    struct blk_alloc_ctx *ctx)
150*6a54128fSAndroid Build Coastguard Worker {
151*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
152*6a54128fSAndroid Build Coastguard Worker 	blk64_t	b = 0;
153*6a54128fSAndroid Build Coastguard Worker 	errcode_t (*gab)(ext2_filsys fs, blk64_t goal, blk64_t *ret);
154*6a54128fSAndroid Build Coastguard Worker 	errcode_t (*gab2)(ext2_filsys, blk64_t, blk64_t *,
155*6a54128fSAndroid Build Coastguard Worker 			  struct blk_alloc_ctx *);
156*6a54128fSAndroid Build Coastguard Worker 
157*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
158*6a54128fSAndroid Build Coastguard Worker 
159*6a54128fSAndroid Build Coastguard Worker 	if (!map) {
160*6a54128fSAndroid Build Coastguard Worker 		/*
161*6a54128fSAndroid Build Coastguard Worker 		 * In case there are clients out there whose get_alloc_block
162*6a54128fSAndroid Build Coastguard Worker 		 * handlers call ext2fs_new_block2 with a NULL block map,
163*6a54128fSAndroid Build Coastguard Worker 		 * temporarily swap out the function pointer so that we don't
164*6a54128fSAndroid Build Coastguard Worker 		 * end up in an infinite loop.
165*6a54128fSAndroid Build Coastguard Worker 		 */
166*6a54128fSAndroid Build Coastguard Worker 		if (fs->get_alloc_block2) {
167*6a54128fSAndroid Build Coastguard Worker 			gab2 = fs->get_alloc_block2;
168*6a54128fSAndroid Build Coastguard Worker 			fs->get_alloc_block2 = NULL;
169*6a54128fSAndroid Build Coastguard Worker 			retval = gab2(fs, goal, &b, ctx);
170*6a54128fSAndroid Build Coastguard Worker 			fs->get_alloc_block2 = gab2;
171*6a54128fSAndroid Build Coastguard Worker 			goto allocated;
172*6a54128fSAndroid Build Coastguard Worker 		} else if (fs->get_alloc_block) {
173*6a54128fSAndroid Build Coastguard Worker 			gab = fs->get_alloc_block;
174*6a54128fSAndroid Build Coastguard Worker 			fs->get_alloc_block = NULL;
175*6a54128fSAndroid Build Coastguard Worker 			retval = gab(fs, goal, &b);
176*6a54128fSAndroid Build Coastguard Worker 			fs->get_alloc_block = gab;
177*6a54128fSAndroid Build Coastguard Worker 			goto allocated;
178*6a54128fSAndroid Build Coastguard Worker 		}
179*6a54128fSAndroid Build Coastguard Worker 	}
180*6a54128fSAndroid Build Coastguard Worker 	if (!map)
181*6a54128fSAndroid Build Coastguard Worker 		map = fs->block_map;
182*6a54128fSAndroid Build Coastguard Worker 	if (!map)
183*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_NO_BLOCK_BITMAP;
184*6a54128fSAndroid Build Coastguard Worker 	if (!goal || (goal >= ext2fs_blocks_count(fs->super)))
185*6a54128fSAndroid Build Coastguard Worker 		goal = fs->super->s_first_data_block;
186*6a54128fSAndroid Build Coastguard Worker 	goal &= ~EXT2FS_CLUSTER_MASK(fs);
187*6a54128fSAndroid Build Coastguard Worker 
188*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_find_first_zero_block_bitmap2(map,
189*6a54128fSAndroid Build Coastguard Worker 			goal, ext2fs_blocks_count(fs->super) - 1, &b);
190*6a54128fSAndroid Build Coastguard Worker 	if ((retval == ENOENT) && (goal != fs->super->s_first_data_block))
191*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_find_first_zero_block_bitmap2(map,
192*6a54128fSAndroid Build Coastguard Worker 			fs->super->s_first_data_block, goal - 1, &b);
193*6a54128fSAndroid Build Coastguard Worker allocated:
194*6a54128fSAndroid Build Coastguard Worker 	if (retval == ENOENT)
195*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_BLOCK_ALLOC_FAIL;
196*6a54128fSAndroid Build Coastguard Worker 	if (retval)
197*6a54128fSAndroid Build Coastguard Worker 		return retval;
198*6a54128fSAndroid Build Coastguard Worker 
199*6a54128fSAndroid Build Coastguard Worker 	ext2fs_clear_block_uninit(fs, ext2fs_group_of_blk2(fs, b));
200*6a54128fSAndroid Build Coastguard Worker 	*ret = b;
201*6a54128fSAndroid Build Coastguard Worker 	return 0;
202*6a54128fSAndroid Build Coastguard Worker }
203*6a54128fSAndroid Build Coastguard Worker 
ext2fs_new_block2(ext2_filsys fs,blk64_t goal,ext2fs_block_bitmap map,blk64_t * ret)204*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
205*6a54128fSAndroid Build Coastguard Worker 			   ext2fs_block_bitmap map, blk64_t *ret)
206*6a54128fSAndroid Build Coastguard Worker {
207*6a54128fSAndroid Build Coastguard Worker 	return ext2fs_new_block3(fs, goal, map, ret, NULL);
208*6a54128fSAndroid Build Coastguard Worker }
209*6a54128fSAndroid Build Coastguard Worker 
ext2fs_new_block(ext2_filsys fs,blk_t goal,ext2fs_block_bitmap map,blk_t * ret)210*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
211*6a54128fSAndroid Build Coastguard Worker 			   ext2fs_block_bitmap map, blk_t *ret)
212*6a54128fSAndroid Build Coastguard Worker {
213*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
214*6a54128fSAndroid Build Coastguard Worker 	blk64_t val;
215*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_new_block2(fs, goal, map, &val);
216*6a54128fSAndroid Build Coastguard Worker 	if (!retval)
217*6a54128fSAndroid Build Coastguard Worker 		*ret = (blk_t) val;
218*6a54128fSAndroid Build Coastguard Worker 	return retval;
219*6a54128fSAndroid Build Coastguard Worker }
220*6a54128fSAndroid Build Coastguard Worker 
221*6a54128fSAndroid Build Coastguard Worker /*
222*6a54128fSAndroid Build Coastguard Worker  * This function zeros out the allocated block, and updates all of the
223*6a54128fSAndroid Build Coastguard Worker  * appropriate filesystem records.
224*6a54128fSAndroid Build Coastguard Worker  */
ext2fs_alloc_block3(ext2_filsys fs,blk64_t goal,char * block_buf,blk64_t * ret,struct blk_alloc_ctx * ctx)225*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_alloc_block3(ext2_filsys fs, blk64_t goal, char *block_buf,
226*6a54128fSAndroid Build Coastguard Worker 			      blk64_t *ret, struct blk_alloc_ctx *ctx)
227*6a54128fSAndroid Build Coastguard Worker {
228*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
229*6a54128fSAndroid Build Coastguard Worker 	blk64_t		block;
230*6a54128fSAndroid Build Coastguard Worker 
231*6a54128fSAndroid Build Coastguard Worker 	if (fs->get_alloc_block2) {
232*6a54128fSAndroid Build Coastguard Worker 		retval = (fs->get_alloc_block2)(fs, goal, &block, ctx);
233*6a54128fSAndroid Build Coastguard Worker 		if (retval)
234*6a54128fSAndroid Build Coastguard Worker 			goto fail;
235*6a54128fSAndroid Build Coastguard Worker 	} else if (fs->get_alloc_block) {
236*6a54128fSAndroid Build Coastguard Worker 		retval = (fs->get_alloc_block)(fs, goal, &block);
237*6a54128fSAndroid Build Coastguard Worker 		if (retval)
238*6a54128fSAndroid Build Coastguard Worker 			goto fail;
239*6a54128fSAndroid Build Coastguard Worker 	} else {
240*6a54128fSAndroid Build Coastguard Worker 		if (!fs->block_map) {
241*6a54128fSAndroid Build Coastguard Worker 			retval = ext2fs_read_block_bitmap(fs);
242*6a54128fSAndroid Build Coastguard Worker 			if (retval)
243*6a54128fSAndroid Build Coastguard Worker 				goto fail;
244*6a54128fSAndroid Build Coastguard Worker 		}
245*6a54128fSAndroid Build Coastguard Worker 
246*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_new_block3(fs, goal, 0, &block, ctx);
247*6a54128fSAndroid Build Coastguard Worker 		if (retval)
248*6a54128fSAndroid Build Coastguard Worker 			goto fail;
249*6a54128fSAndroid Build Coastguard Worker 	}
250*6a54128fSAndroid Build Coastguard Worker 
251*6a54128fSAndroid Build Coastguard Worker 	if (block_buf) {
252*6a54128fSAndroid Build Coastguard Worker 		memset(block_buf, 0, fs->blocksize);
253*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_write_blk64(fs->io, block, 1, block_buf);
254*6a54128fSAndroid Build Coastguard Worker 	} else
255*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_zero_blocks2(fs, block, 1, NULL, NULL);
256*6a54128fSAndroid Build Coastguard Worker 	if (retval)
257*6a54128fSAndroid Build Coastguard Worker 		goto fail;
258*6a54128fSAndroid Build Coastguard Worker 
259*6a54128fSAndroid Build Coastguard Worker 	ext2fs_block_alloc_stats2(fs, block, +1);
260*6a54128fSAndroid Build Coastguard Worker 	*ret = block;
261*6a54128fSAndroid Build Coastguard Worker 
262*6a54128fSAndroid Build Coastguard Worker fail:
263*6a54128fSAndroid Build Coastguard Worker 	return retval;
264*6a54128fSAndroid Build Coastguard Worker }
265*6a54128fSAndroid Build Coastguard Worker 
ext2fs_alloc_block2(ext2_filsys fs,blk64_t goal,char * block_buf,blk64_t * ret)266*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_alloc_block2(ext2_filsys fs, blk64_t goal,
267*6a54128fSAndroid Build Coastguard Worker 			     char *block_buf, blk64_t *ret)
268*6a54128fSAndroid Build Coastguard Worker {
269*6a54128fSAndroid Build Coastguard Worker 	return ext2fs_alloc_block3(fs, goal, block_buf, ret, NULL);
270*6a54128fSAndroid Build Coastguard Worker }
271*6a54128fSAndroid Build Coastguard Worker 
ext2fs_alloc_block(ext2_filsys fs,blk_t goal,char * block_buf,blk_t * ret)272*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
273*6a54128fSAndroid Build Coastguard Worker 			     char *block_buf, blk_t *ret)
274*6a54128fSAndroid Build Coastguard Worker {
275*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
276*6a54128fSAndroid Build Coastguard Worker 	blk64_t ret64, goal64 = goal;
277*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_alloc_block3(fs, goal64, block_buf, &ret64, NULL);
278*6a54128fSAndroid Build Coastguard Worker 	if (!retval)
279*6a54128fSAndroid Build Coastguard Worker 		*ret = (blk_t)ret64;
280*6a54128fSAndroid Build Coastguard Worker         return retval;
281*6a54128fSAndroid Build Coastguard Worker }
282*6a54128fSAndroid Build Coastguard Worker 
ext2fs_get_free_blocks2(ext2_filsys fs,blk64_t start,blk64_t finish,int num,ext2fs_block_bitmap map,blk64_t * ret)283*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_get_free_blocks2(ext2_filsys fs, blk64_t start, blk64_t finish,
284*6a54128fSAndroid Build Coastguard Worker 				 int num, ext2fs_block_bitmap map, blk64_t *ret)
285*6a54128fSAndroid Build Coastguard Worker {
286*6a54128fSAndroid Build Coastguard Worker 	blk64_t	b = start;
287*6a54128fSAndroid Build Coastguard Worker 	int	c_ratio;
288*6a54128fSAndroid Build Coastguard Worker 
289*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
290*6a54128fSAndroid Build Coastguard Worker 
291*6a54128fSAndroid Build Coastguard Worker 	if (!map)
292*6a54128fSAndroid Build Coastguard Worker 		map = fs->block_map;
293*6a54128fSAndroid Build Coastguard Worker 	if (!map)
294*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_NO_BLOCK_BITMAP;
295*6a54128fSAndroid Build Coastguard Worker 	if (!b)
296*6a54128fSAndroid Build Coastguard Worker 		b = fs->super->s_first_data_block;
297*6a54128fSAndroid Build Coastguard Worker 	if (!finish)
298*6a54128fSAndroid Build Coastguard Worker 		finish = start;
299*6a54128fSAndroid Build Coastguard Worker 	if (!num)
300*6a54128fSAndroid Build Coastguard Worker 		num = 1;
301*6a54128fSAndroid Build Coastguard Worker 	c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
302*6a54128fSAndroid Build Coastguard Worker 	b &= ~(c_ratio - 1);
303*6a54128fSAndroid Build Coastguard Worker 	finish &= ~(c_ratio -1);
304*6a54128fSAndroid Build Coastguard Worker 	do {
305*6a54128fSAndroid Build Coastguard Worker 		if (b + num - 1 >= ext2fs_blocks_count(fs->super)) {
306*6a54128fSAndroid Build Coastguard Worker 			if (finish > start)
307*6a54128fSAndroid Build Coastguard Worker 				return EXT2_ET_BLOCK_ALLOC_FAIL;
308*6a54128fSAndroid Build Coastguard Worker 			b = fs->super->s_first_data_block;
309*6a54128fSAndroid Build Coastguard Worker 		}
310*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_fast_test_block_bitmap_range2(map, b, num)) {
311*6a54128fSAndroid Build Coastguard Worker 			*ret = b;
312*6a54128fSAndroid Build Coastguard Worker 			return 0;
313*6a54128fSAndroid Build Coastguard Worker 		}
314*6a54128fSAndroid Build Coastguard Worker 		b += c_ratio;
315*6a54128fSAndroid Build Coastguard Worker 	} while (b != finish);
316*6a54128fSAndroid Build Coastguard Worker 	return EXT2_ET_BLOCK_ALLOC_FAIL;
317*6a54128fSAndroid Build Coastguard Worker }
318*6a54128fSAndroid Build Coastguard Worker 
ext2fs_get_free_blocks(ext2_filsys fs,blk_t start,blk_t finish,int num,ext2fs_block_bitmap map,blk_t * ret)319*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
320*6a54128fSAndroid Build Coastguard Worker 				 int num, ext2fs_block_bitmap map, blk_t *ret)
321*6a54128fSAndroid Build Coastguard Worker {
322*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
323*6a54128fSAndroid Build Coastguard Worker 	blk64_t val;
324*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_free_blocks2(fs, start, finish, num, map, &val);
325*6a54128fSAndroid Build Coastguard Worker 	if(!retval)
326*6a54128fSAndroid Build Coastguard Worker 		*ret = (blk_t) val;
327*6a54128fSAndroid Build Coastguard Worker 	return retval;
328*6a54128fSAndroid Build Coastguard Worker }
329*6a54128fSAndroid Build Coastguard Worker 
ext2fs_set_alloc_block_callback(ext2_filsys fs,errcode_t (* func)(ext2_filsys fs,blk64_t goal,blk64_t * ret),errcode_t (** old)(ext2_filsys fs,blk64_t goal,blk64_t * ret))330*6a54128fSAndroid Build Coastguard Worker void ext2fs_set_alloc_block_callback(ext2_filsys fs,
331*6a54128fSAndroid Build Coastguard Worker 				     errcode_t (*func)(ext2_filsys fs,
332*6a54128fSAndroid Build Coastguard Worker 						       blk64_t goal,
333*6a54128fSAndroid Build Coastguard Worker 						       blk64_t *ret),
334*6a54128fSAndroid Build Coastguard Worker 				     errcode_t (**old)(ext2_filsys fs,
335*6a54128fSAndroid Build Coastguard Worker 						       blk64_t goal,
336*6a54128fSAndroid Build Coastguard Worker 						       blk64_t *ret))
337*6a54128fSAndroid Build Coastguard Worker {
338*6a54128fSAndroid Build Coastguard Worker 	if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
339*6a54128fSAndroid Build Coastguard Worker 		return;
340*6a54128fSAndroid Build Coastguard Worker 
341*6a54128fSAndroid Build Coastguard Worker 	if (old)
342*6a54128fSAndroid Build Coastguard Worker 		*old = fs->get_alloc_block;
343*6a54128fSAndroid Build Coastguard Worker 
344*6a54128fSAndroid Build Coastguard Worker 	fs->get_alloc_block = func;
345*6a54128fSAndroid Build Coastguard Worker }
346*6a54128fSAndroid Build Coastguard Worker 
ext2fs_find_inode_goal(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,blk64_t lblk)347*6a54128fSAndroid Build Coastguard Worker blk64_t ext2fs_find_inode_goal(ext2_filsys fs, ext2_ino_t ino,
348*6a54128fSAndroid Build Coastguard Worker 			       struct ext2_inode *inode, blk64_t lblk)
349*6a54128fSAndroid Build Coastguard Worker {
350*6a54128fSAndroid Build Coastguard Worker 	dgrp_t			group;
351*6a54128fSAndroid Build Coastguard Worker 	__u8			log_flex;
352*6a54128fSAndroid Build Coastguard Worker 	struct ext2fs_extent	extent;
353*6a54128fSAndroid Build Coastguard Worker 	ext2_extent_handle_t	handle = NULL;
354*6a54128fSAndroid Build Coastguard Worker 	errcode_t		err;
355*6a54128fSAndroid Build Coastguard Worker 
356*6a54128fSAndroid Build Coastguard Worker 	/* Make sure data stored in inode->i_block is neither fast symlink nor
357*6a54128fSAndroid Build Coastguard Worker 	 * inline data.
358*6a54128fSAndroid Build Coastguard Worker 	 */
359*6a54128fSAndroid Build Coastguard Worker 	if (inode == NULL || ext2fs_is_fast_symlink(inode) ||
360*6a54128fSAndroid Build Coastguard Worker 	    inode->i_flags & EXT4_INLINE_DATA_FL)
361*6a54128fSAndroid Build Coastguard Worker 		goto no_blocks;
362*6a54128fSAndroid Build Coastguard Worker 
363*6a54128fSAndroid Build Coastguard Worker 	if (inode->i_flags & EXT4_EXTENTS_FL) {
364*6a54128fSAndroid Build Coastguard Worker 		err = ext2fs_extent_open2(fs, ino, inode, &handle);
365*6a54128fSAndroid Build Coastguard Worker 		if (err)
366*6a54128fSAndroid Build Coastguard Worker 			goto no_blocks;
367*6a54128fSAndroid Build Coastguard Worker 		err = ext2fs_extent_goto2(handle, 0, lblk);
368*6a54128fSAndroid Build Coastguard Worker 		if (err)
369*6a54128fSAndroid Build Coastguard Worker 			goto no_blocks;
370*6a54128fSAndroid Build Coastguard Worker 		err = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT, &extent);
371*6a54128fSAndroid Build Coastguard Worker 		if (err)
372*6a54128fSAndroid Build Coastguard Worker 			goto no_blocks;
373*6a54128fSAndroid Build Coastguard Worker 		ext2fs_extent_free(handle);
374*6a54128fSAndroid Build Coastguard Worker 		return extent.e_pblk + (lblk - extent.e_lblk);
375*6a54128fSAndroid Build Coastguard Worker 	}
376*6a54128fSAndroid Build Coastguard Worker 
377*6a54128fSAndroid Build Coastguard Worker 	/* block mapped file; see if block zero is mapped? */
378*6a54128fSAndroid Build Coastguard Worker 	if (inode->i_block[0])
379*6a54128fSAndroid Build Coastguard Worker 		return inode->i_block[0];
380*6a54128fSAndroid Build Coastguard Worker 
381*6a54128fSAndroid Build Coastguard Worker no_blocks:
382*6a54128fSAndroid Build Coastguard Worker 	ext2fs_extent_free(handle);
383*6a54128fSAndroid Build Coastguard Worker 	log_flex = fs->super->s_log_groups_per_flex;
384*6a54128fSAndroid Build Coastguard Worker 	group = ext2fs_group_of_ino(fs, ino);
385*6a54128fSAndroid Build Coastguard Worker 	if (log_flex)
386*6a54128fSAndroid Build Coastguard Worker 		group = group & ~((1 << (log_flex)) - 1);
387*6a54128fSAndroid Build Coastguard Worker 	return ext2fs_group_first_block2(fs, group);
388*6a54128fSAndroid Build Coastguard Worker }
389*6a54128fSAndroid Build Coastguard Worker 
390*6a54128fSAndroid Build Coastguard Worker /*
391*6a54128fSAndroid Build Coastguard Worker  * Starting at _goal_, scan around the filesystem to find a run of free blocks
392*6a54128fSAndroid Build Coastguard Worker  * that's at least _len_ blocks long.  Possible flags:
393*6a54128fSAndroid Build Coastguard Worker  * - EXT2_NEWRANGE_EXACT_GOAL: The range of blocks must start at _goal_.
394*6a54128fSAndroid Build Coastguard Worker  * - EXT2_NEWRANGE_MIN_LENGTH: do not return a allocation shorter than _len_.
395*6a54128fSAndroid Build Coastguard Worker  * - EXT2_NEWRANGE_ZERO_BLOCKS: Zero blocks pblk to pblk+plen before returning.
396*6a54128fSAndroid Build Coastguard Worker  *
397*6a54128fSAndroid Build Coastguard Worker  * The starting block is returned in _pblk_ and the length is returned via
398*6a54128fSAndroid Build Coastguard Worker  * _plen_.  The blocks are not marked in the bitmap; the caller must mark
399*6a54128fSAndroid Build Coastguard Worker  * however much of the returned run they actually use, hopefully via
400*6a54128fSAndroid Build Coastguard Worker  * ext2fs_block_alloc_stats_range().
401*6a54128fSAndroid Build Coastguard Worker  *
402*6a54128fSAndroid Build Coastguard Worker  * This function can return a range that is longer than what was requested.
403*6a54128fSAndroid Build Coastguard Worker  */
ext2fs_new_range(ext2_filsys fs,int flags,blk64_t goal,blk64_t len,ext2fs_block_bitmap map,blk64_t * pblk,blk64_t * plen)404*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_new_range(ext2_filsys fs, int flags, blk64_t goal,
405*6a54128fSAndroid Build Coastguard Worker 			   blk64_t len, ext2fs_block_bitmap map, blk64_t *pblk,
406*6a54128fSAndroid Build Coastguard Worker 			   blk64_t *plen)
407*6a54128fSAndroid Build Coastguard Worker {
408*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
409*6a54128fSAndroid Build Coastguard Worker 	blk64_t start, end, b;
410*6a54128fSAndroid Build Coastguard Worker 	int looped = 0;
411*6a54128fSAndroid Build Coastguard Worker 	blk64_t max_blocks = ext2fs_blocks_count(fs->super);
412*6a54128fSAndroid Build Coastguard Worker 	errcode_t (*nrf)(ext2_filsys fs, int flags, blk64_t goal,
413*6a54128fSAndroid Build Coastguard Worker 			 blk64_t len, blk64_t *pblk, blk64_t *plen);
414*6a54128fSAndroid Build Coastguard Worker 
415*6a54128fSAndroid Build Coastguard Worker 	dbg_printf("%s: flags=0x%x goal=%llu len=%llu\n", __func__, flags,
416*6a54128fSAndroid Build Coastguard Worker 		   goal, len);
417*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
418*6a54128fSAndroid Build Coastguard Worker 	if (len == 0 || (flags & ~EXT2_NEWRANGE_ALL_FLAGS))
419*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_INVALID_ARGUMENT;
420*6a54128fSAndroid Build Coastguard Worker 
421*6a54128fSAndroid Build Coastguard Worker 	if (!map && fs->new_range) {
422*6a54128fSAndroid Build Coastguard Worker 		/*
423*6a54128fSAndroid Build Coastguard Worker 		 * In case there are clients out there whose new_range
424*6a54128fSAndroid Build Coastguard Worker 		 * handlers call ext2fs_new_range with a NULL block map,
425*6a54128fSAndroid Build Coastguard Worker 		 * temporarily swap out the function pointer so that we don't
426*6a54128fSAndroid Build Coastguard Worker 		 * end up in an infinite loop.
427*6a54128fSAndroid Build Coastguard Worker 		 */
428*6a54128fSAndroid Build Coastguard Worker 		nrf = fs->new_range;
429*6a54128fSAndroid Build Coastguard Worker 		fs->new_range = NULL;
430*6a54128fSAndroid Build Coastguard Worker 		retval = nrf(fs, flags, goal, len, pblk, plen);
431*6a54128fSAndroid Build Coastguard Worker 		fs->new_range = nrf;
432*6a54128fSAndroid Build Coastguard Worker 		if (retval)
433*6a54128fSAndroid Build Coastguard Worker 			return retval;
434*6a54128fSAndroid Build Coastguard Worker 		start = *pblk;
435*6a54128fSAndroid Build Coastguard Worker 		end = *pblk + *plen;
436*6a54128fSAndroid Build Coastguard Worker 		goto allocated;
437*6a54128fSAndroid Build Coastguard Worker 	}
438*6a54128fSAndroid Build Coastguard Worker 	if (!map)
439*6a54128fSAndroid Build Coastguard Worker 		map = fs->block_map;
440*6a54128fSAndroid Build Coastguard Worker 	if (!map)
441*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_NO_BLOCK_BITMAP;
442*6a54128fSAndroid Build Coastguard Worker 	if (!goal || goal >= ext2fs_blocks_count(fs->super))
443*6a54128fSAndroid Build Coastguard Worker 		goal = fs->super->s_first_data_block;
444*6a54128fSAndroid Build Coastguard Worker 
445*6a54128fSAndroid Build Coastguard Worker 	start = goal;
446*6a54128fSAndroid Build Coastguard Worker 	while (!looped || start <= goal) {
447*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_find_first_zero_block_bitmap2(map, start,
448*6a54128fSAndroid Build Coastguard Worker 							      max_blocks - 1,
449*6a54128fSAndroid Build Coastguard Worker 							      &start);
450*6a54128fSAndroid Build Coastguard Worker 		if (retval == ENOENT) {
451*6a54128fSAndroid Build Coastguard Worker 			/*
452*6a54128fSAndroid Build Coastguard Worker 			 * If there are no free blocks beyond the starting
453*6a54128fSAndroid Build Coastguard Worker 			 * point, try scanning the whole filesystem, unless the
454*6a54128fSAndroid Build Coastguard Worker 			 * user told us only to allocate from _goal_, or if
455*6a54128fSAndroid Build Coastguard Worker 			 * we're already scanning the whole filesystem.
456*6a54128fSAndroid Build Coastguard Worker 			 */
457*6a54128fSAndroid Build Coastguard Worker 			if (flags & EXT2_NEWRANGE_FIXED_GOAL ||
458*6a54128fSAndroid Build Coastguard Worker 			    start == fs->super->s_first_data_block)
459*6a54128fSAndroid Build Coastguard Worker 				goto fail;
460*6a54128fSAndroid Build Coastguard Worker 			start = fs->super->s_first_data_block;
461*6a54128fSAndroid Build Coastguard Worker 			continue;
462*6a54128fSAndroid Build Coastguard Worker 		} else if (retval)
463*6a54128fSAndroid Build Coastguard Worker 			goto errout;
464*6a54128fSAndroid Build Coastguard Worker 
465*6a54128fSAndroid Build Coastguard Worker 		if (flags & EXT2_NEWRANGE_FIXED_GOAL && start != goal)
466*6a54128fSAndroid Build Coastguard Worker 			goto fail;
467*6a54128fSAndroid Build Coastguard Worker 
468*6a54128fSAndroid Build Coastguard Worker 		b = min(start + len - 1, max_blocks - 1);
469*6a54128fSAndroid Build Coastguard Worker 		retval =  ext2fs_find_first_set_block_bitmap2(map, start, b,
470*6a54128fSAndroid Build Coastguard Worker 							      &end);
471*6a54128fSAndroid Build Coastguard Worker 		if (retval == ENOENT)
472*6a54128fSAndroid Build Coastguard Worker 			end = b + 1;
473*6a54128fSAndroid Build Coastguard Worker 		else if (retval)
474*6a54128fSAndroid Build Coastguard Worker 			goto errout;
475*6a54128fSAndroid Build Coastguard Worker 
476*6a54128fSAndroid Build Coastguard Worker 		if (!(flags & EXT2_NEWRANGE_MIN_LENGTH) ||
477*6a54128fSAndroid Build Coastguard Worker 		    (end - start) >= len) {
478*6a54128fSAndroid Build Coastguard Worker 			/* Success! */
479*6a54128fSAndroid Build Coastguard Worker 			*pblk = start;
480*6a54128fSAndroid Build Coastguard Worker 			*plen = end - start;
481*6a54128fSAndroid Build Coastguard Worker 			dbg_printf("%s: new_range goal=%llu--%llu "
482*6a54128fSAndroid Build Coastguard Worker 				   "blk=%llu--%llu %llu\n",
483*6a54128fSAndroid Build Coastguard Worker 				   __func__, goal, goal + len - 1,
484*6a54128fSAndroid Build Coastguard Worker 				   *pblk, *pblk + *plen - 1, *plen);
485*6a54128fSAndroid Build Coastguard Worker allocated:
486*6a54128fSAndroid Build Coastguard Worker 			for (b = start; b < end;
487*6a54128fSAndroid Build Coastguard Worker 			     b += fs->super->s_blocks_per_group)
488*6a54128fSAndroid Build Coastguard Worker 				ext2fs_clear_block_uninit(fs,
489*6a54128fSAndroid Build Coastguard Worker 						ext2fs_group_of_blk2(fs, b));
490*6a54128fSAndroid Build Coastguard Worker 			return 0;
491*6a54128fSAndroid Build Coastguard Worker 		}
492*6a54128fSAndroid Build Coastguard Worker 
493*6a54128fSAndroid Build Coastguard Worker 		if (flags & EXT2_NEWRANGE_FIXED_GOAL)
494*6a54128fSAndroid Build Coastguard Worker 			goto fail;
495*6a54128fSAndroid Build Coastguard Worker 		start = end;
496*6a54128fSAndroid Build Coastguard Worker 		if (start >= max_blocks) {
497*6a54128fSAndroid Build Coastguard Worker 			if (looped)
498*6a54128fSAndroid Build Coastguard Worker 				goto fail;
499*6a54128fSAndroid Build Coastguard Worker 			looped = 1;
500*6a54128fSAndroid Build Coastguard Worker 			start = fs->super->s_first_data_block;
501*6a54128fSAndroid Build Coastguard Worker 		}
502*6a54128fSAndroid Build Coastguard Worker 	}
503*6a54128fSAndroid Build Coastguard Worker 
504*6a54128fSAndroid Build Coastguard Worker fail:
505*6a54128fSAndroid Build Coastguard Worker 	retval = EXT2_ET_BLOCK_ALLOC_FAIL;
506*6a54128fSAndroid Build Coastguard Worker errout:
507*6a54128fSAndroid Build Coastguard Worker 	return retval;
508*6a54128fSAndroid Build Coastguard Worker }
509*6a54128fSAndroid Build Coastguard Worker 
ext2fs_set_new_range_callback(ext2_filsys fs,errcode_t (* func)(ext2_filsys fs,int flags,blk64_t goal,blk64_t len,blk64_t * pblk,blk64_t * plen),errcode_t (** old)(ext2_filsys fs,int flags,blk64_t goal,blk64_t len,blk64_t * pblk,blk64_t * plen))510*6a54128fSAndroid Build Coastguard Worker void ext2fs_set_new_range_callback(ext2_filsys fs,
511*6a54128fSAndroid Build Coastguard Worker 	errcode_t (*func)(ext2_filsys fs, int flags, blk64_t goal,
512*6a54128fSAndroid Build Coastguard Worker 			       blk64_t len, blk64_t *pblk, blk64_t *plen),
513*6a54128fSAndroid Build Coastguard Worker 	errcode_t (**old)(ext2_filsys fs, int flags, blk64_t goal,
514*6a54128fSAndroid Build Coastguard Worker 			       blk64_t len, blk64_t *pblk, blk64_t *plen))
515*6a54128fSAndroid Build Coastguard Worker {
516*6a54128fSAndroid Build Coastguard Worker 	if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
517*6a54128fSAndroid Build Coastguard Worker 		return;
518*6a54128fSAndroid Build Coastguard Worker 
519*6a54128fSAndroid Build Coastguard Worker 	if (old)
520*6a54128fSAndroid Build Coastguard Worker 		*old = fs->new_range;
521*6a54128fSAndroid Build Coastguard Worker 
522*6a54128fSAndroid Build Coastguard Worker 	fs->new_range = func;
523*6a54128fSAndroid Build Coastguard Worker }
524*6a54128fSAndroid Build Coastguard Worker 
ext2fs_alloc_range(ext2_filsys fs,int flags,blk64_t goal,blk_t len,blk64_t * ret)525*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_alloc_range(ext2_filsys fs, int flags, blk64_t goal,
526*6a54128fSAndroid Build Coastguard Worker 			     blk_t len, blk64_t *ret)
527*6a54128fSAndroid Build Coastguard Worker {
528*6a54128fSAndroid Build Coastguard Worker 	int newr_flags = EXT2_NEWRANGE_MIN_LENGTH;
529*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
530*6a54128fSAndroid Build Coastguard Worker 	blk64_t plen;
531*6a54128fSAndroid Build Coastguard Worker 
532*6a54128fSAndroid Build Coastguard Worker 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
533*6a54128fSAndroid Build Coastguard Worker 	if (len == 0 || (flags & ~EXT2_ALLOCRANGE_ALL_FLAGS))
534*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_INVALID_ARGUMENT;
535*6a54128fSAndroid Build Coastguard Worker 
536*6a54128fSAndroid Build Coastguard Worker 	if (flags & EXT2_ALLOCRANGE_FIXED_GOAL)
537*6a54128fSAndroid Build Coastguard Worker 		newr_flags |= EXT2_NEWRANGE_FIXED_GOAL;
538*6a54128fSAndroid Build Coastguard Worker 
539*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_new_range(fs, newr_flags, goal, len, NULL, ret, &plen);
540*6a54128fSAndroid Build Coastguard Worker 	if (retval)
541*6a54128fSAndroid Build Coastguard Worker 		return retval;
542*6a54128fSAndroid Build Coastguard Worker 
543*6a54128fSAndroid Build Coastguard Worker 	if (plen < len)
544*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_BLOCK_ALLOC_FAIL;
545*6a54128fSAndroid Build Coastguard Worker 
546*6a54128fSAndroid Build Coastguard Worker 	if (flags & EXT2_ALLOCRANGE_ZERO_BLOCKS) {
547*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_zero_blocks2(fs, *ret, len, NULL, NULL);
548*6a54128fSAndroid Build Coastguard Worker 		if (retval)
549*6a54128fSAndroid Build Coastguard Worker 			return retval;
550*6a54128fSAndroid Build Coastguard Worker 	}
551*6a54128fSAndroid Build Coastguard Worker 
552*6a54128fSAndroid Build Coastguard Worker 	ext2fs_block_alloc_stats_range(fs, *ret, len, +1);
553*6a54128fSAndroid Build Coastguard Worker 	return retval;
554*6a54128fSAndroid Build Coastguard Worker }
555