xref: /openbmc/linux/fs/ext2/balloc.c (revision 7e24a55b2122746c2eef192296fc84624354f895)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/ext2/balloc.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 1992, 1993, 1994, 1995
61da177e4SLinus Torvalds  * Remy Card (card@masi.ibp.fr)
71da177e4SLinus Torvalds  * Laboratoire MASI - Institut Blaise Pascal
81da177e4SLinus Torvalds  * Universite Pierre et Marie Curie (Paris VI)
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993
111da177e4SLinus Torvalds  *  Big-endian to little-endian byte-swapping/bitmaps by
121da177e4SLinus Torvalds  *        David S. Miller (davem@caip.rutgers.edu), 1995
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #include "ext2.h"
161da177e4SLinus Torvalds #include <linux/quotaops.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/sched.h>
195b825c3aSIngo Molnar #include <linux/cred.h>
201da177e4SLinus Torvalds #include <linux/buffer_head.h>
2116f7e0feSRandy Dunlap #include <linux/capability.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds /*
241da177e4SLinus Torvalds  * balloc.c contains the blocks allocation and deallocation routines
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds /*
281da177e4SLinus Torvalds  * The free blocks are managed by bitmaps.  A file system contains several
291da177e4SLinus Torvalds  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
301da177e4SLinus Torvalds  * block for inodes, N blocks for the inode table and data blocks.
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  * The file system contains group descriptors which are located after the
331da177e4SLinus Torvalds  * super block.  Each descriptor contains the number of the bitmap block and
341da177e4SLinus Torvalds  * the free blocks count in the block.  The descriptors are loaded in memory
35e627432cSAneesh Kumar K.V  * when a file system is mounted (see ext2_fill_super).
361da177e4SLinus Torvalds  */
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds 
ext2_get_group_desc(struct super_block * sb,unsigned int block_group,struct buffer_head ** bh)391da177e4SLinus Torvalds struct ext2_group_desc * ext2_get_group_desc(struct super_block * sb,
401da177e4SLinus Torvalds 					     unsigned int block_group,
411da177e4SLinus Torvalds 					     struct buffer_head ** bh)
421da177e4SLinus Torvalds {
431da177e4SLinus Torvalds 	unsigned long group_desc;
441da177e4SLinus Torvalds 	unsigned long offset;
451da177e4SLinus Torvalds 	struct ext2_group_desc * desc;
461da177e4SLinus Torvalds 	struct ext2_sb_info *sbi = EXT2_SB(sb);
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 	if (block_group >= sbi->s_groups_count) {
49372d1f3eSDan Carpenter 		WARN(1, "block_group >= groups_count - "
501da177e4SLinus Torvalds 		     "block_group = %d, groups_count = %lu",
511da177e4SLinus Torvalds 		     block_group, sbi->s_groups_count);
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 		return NULL;
541da177e4SLinus Torvalds 	}
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(sb);
571da177e4SLinus Torvalds 	offset = block_group & (EXT2_DESC_PER_BLOCK(sb) - 1);
581da177e4SLinus Torvalds 	if (!sbi->s_group_desc[group_desc]) {
59372d1f3eSDan Carpenter 		WARN(1, "Group descriptor not loaded - "
601da177e4SLinus Torvalds 		     "block_group = %d, group_desc = %lu, desc = %lu",
611da177e4SLinus Torvalds 		      block_group, group_desc, offset);
621da177e4SLinus Torvalds 		return NULL;
631da177e4SLinus Torvalds 	}
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	desc = (struct ext2_group_desc *) sbi->s_group_desc[group_desc]->b_data;
661da177e4SLinus Torvalds 	if (bh)
671da177e4SLinus Torvalds 		*bh = sbi->s_group_desc[group_desc];
681da177e4SLinus Torvalds 	return desc + offset;
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds 
ext2_valid_block_bitmap(struct super_block * sb,struct ext2_group_desc * desc,unsigned int block_group,struct buffer_head * bh)7101584fa6SAneesh Kumar K.V static int ext2_valid_block_bitmap(struct super_block *sb,
7201584fa6SAneesh Kumar K.V 					struct ext2_group_desc *desc,
7301584fa6SAneesh Kumar K.V 					unsigned int block_group,
7401584fa6SAneesh Kumar K.V 					struct buffer_head *bh)
7501584fa6SAneesh Kumar K.V {
7601584fa6SAneesh Kumar K.V 	ext2_grpblk_t offset;
7701584fa6SAneesh Kumar K.V 	ext2_grpblk_t next_zero_bit;
7801584fa6SAneesh Kumar K.V 	ext2_fsblk_t bitmap_blk;
7901584fa6SAneesh Kumar K.V 	ext2_fsblk_t group_first_block;
80*8a31e8ffSJan Kara 	ext2_grpblk_t max_bit;
8101584fa6SAneesh Kumar K.V 
8201584fa6SAneesh Kumar K.V 	group_first_block = ext2_group_first_block_no(sb, block_group);
83*8a31e8ffSJan Kara 	max_bit = ext2_group_last_block_no(sb, block_group) - group_first_block;
8401584fa6SAneesh Kumar K.V 
8501584fa6SAneesh Kumar K.V 	/* check whether block bitmap block number is set */
8601584fa6SAneesh Kumar K.V 	bitmap_blk = le32_to_cpu(desc->bg_block_bitmap);
8701584fa6SAneesh Kumar K.V 	offset = bitmap_blk - group_first_block;
88*8a31e8ffSJan Kara 	if (offset < 0 || offset > max_bit ||
89*8a31e8ffSJan Kara 	    !ext2_test_bit(offset, bh->b_data))
9001584fa6SAneesh Kumar K.V 		/* bad block bitmap */
9101584fa6SAneesh Kumar K.V 		goto err_out;
9201584fa6SAneesh Kumar K.V 
9301584fa6SAneesh Kumar K.V 	/* check whether the inode bitmap block number is set */
9401584fa6SAneesh Kumar K.V 	bitmap_blk = le32_to_cpu(desc->bg_inode_bitmap);
9501584fa6SAneesh Kumar K.V 	offset = bitmap_blk - group_first_block;
96*8a31e8ffSJan Kara 	if (offset < 0 || offset > max_bit ||
97*8a31e8ffSJan Kara 	    !ext2_test_bit(offset, bh->b_data))
9801584fa6SAneesh Kumar K.V 		/* bad block bitmap */
9901584fa6SAneesh Kumar K.V 		goto err_out;
10001584fa6SAneesh Kumar K.V 
10101584fa6SAneesh Kumar K.V 	/* check whether the inode table block number is set */
10201584fa6SAneesh Kumar K.V 	bitmap_blk = le32_to_cpu(desc->bg_inode_table);
10301584fa6SAneesh Kumar K.V 	offset = bitmap_blk - group_first_block;
104*8a31e8ffSJan Kara 	if (offset < 0 || offset > max_bit ||
105*8a31e8ffSJan Kara 	    offset + EXT2_SB(sb)->s_itb_per_group - 1 > max_bit)
106*8a31e8ffSJan Kara 		goto err_out;
10701584fa6SAneesh Kumar K.V 	next_zero_bit = ext2_find_next_zero_bit(bh->b_data,
10801584fa6SAneesh Kumar K.V 				offset + EXT2_SB(sb)->s_itb_per_group,
10901584fa6SAneesh Kumar K.V 				offset);
11001584fa6SAneesh Kumar K.V 	if (next_zero_bit >= offset + EXT2_SB(sb)->s_itb_per_group)
11101584fa6SAneesh Kumar K.V 		/* good bitmap for inode tables */
11201584fa6SAneesh Kumar K.V 		return 1;
11301584fa6SAneesh Kumar K.V 
11401584fa6SAneesh Kumar K.V err_out:
115605afd60SHarvey Harrison 	ext2_error(sb, __func__,
11601584fa6SAneesh Kumar K.V 			"Invalid block bitmap - "
11701584fa6SAneesh Kumar K.V 			"block_group = %d, block = %lu",
11801584fa6SAneesh Kumar K.V 			block_group, bitmap_blk);
11901584fa6SAneesh Kumar K.V 	return 0;
12001584fa6SAneesh Kumar K.V }
12101584fa6SAneesh Kumar K.V 
1221da177e4SLinus Torvalds /*
12301584fa6SAneesh Kumar K.V  * Read the bitmap for a given block_group,and validate the
12401584fa6SAneesh Kumar K.V  * bits for block/inode/inode tables are set in the bitmaps
1251da177e4SLinus Torvalds  *
1261da177e4SLinus Torvalds  * Return buffer_head on success or NULL in case of failure.
1271da177e4SLinus Torvalds  */
1281da177e4SLinus Torvalds static struct buffer_head *
read_block_bitmap(struct super_block * sb,unsigned int block_group)1291da177e4SLinus Torvalds read_block_bitmap(struct super_block *sb, unsigned int block_group)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	struct ext2_group_desc * desc;
1321da177e4SLinus Torvalds 	struct buffer_head * bh = NULL;
13301584fa6SAneesh Kumar K.V 	ext2_fsblk_t bitmap_blk;
13428cf7559SZhang Yi 	int ret;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	desc = ext2_get_group_desc(sb, block_group, NULL);
1371da177e4SLinus Torvalds 	if (!desc)
13801584fa6SAneesh Kumar K.V 		return NULL;
13901584fa6SAneesh Kumar K.V 	bitmap_blk = le32_to_cpu(desc->bg_block_bitmap);
14001584fa6SAneesh Kumar K.V 	bh = sb_getblk(sb, bitmap_blk);
14101584fa6SAneesh Kumar K.V 	if (unlikely(!bh)) {
142605afd60SHarvey Harrison 		ext2_error(sb, __func__,
1431da177e4SLinus Torvalds 			    "Cannot read block bitmap - "
1441da177e4SLinus Torvalds 			    "block_group = %d, block_bitmap = %u",
1451da177e4SLinus Torvalds 			    block_group, le32_to_cpu(desc->bg_block_bitmap));
14601584fa6SAneesh Kumar K.V 		return NULL;
14701584fa6SAneesh Kumar K.V 	}
14828cf7559SZhang Yi 	ret = bh_read(bh, 0);
14928cf7559SZhang Yi 	if (ret > 0)
15001584fa6SAneesh Kumar K.V 		return bh;
15128cf7559SZhang Yi 	if (ret < 0) {
15201584fa6SAneesh Kumar K.V 		brelse(bh);
153605afd60SHarvey Harrison 		ext2_error(sb, __func__,
15401584fa6SAneesh Kumar K.V 			    "Cannot read block bitmap - "
15501584fa6SAneesh Kumar K.V 			    "block_group = %d, block_bitmap = %u",
15601584fa6SAneesh Kumar K.V 			    block_group, le32_to_cpu(desc->bg_block_bitmap));
15701584fa6SAneesh Kumar K.V 		return NULL;
15801584fa6SAneesh Kumar K.V 	}
15901584fa6SAneesh Kumar K.V 
1608b915825SAneesh Kumar K.V 	ext2_valid_block_bitmap(sb, desc, block_group, bh);
1618b915825SAneesh Kumar K.V 	/*
1628b915825SAneesh Kumar K.V 	 * file system mounted not to panic on error, continue with corrupt
1638b915825SAneesh Kumar K.V 	 * bitmap
1648b915825SAneesh Kumar K.V 	 */
1650b832a4bSLinus Torvalds 	return bh;
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
group_adjust_blocks(struct super_block * sb,int group_no,struct ext2_group_desc * desc,struct buffer_head * bh,int count)168a686cd89SMartin J. Bligh static void group_adjust_blocks(struct super_block *sb, int group_no,
1691da177e4SLinus Torvalds 	struct ext2_group_desc *desc, struct buffer_head *bh, int count)
1701da177e4SLinus Torvalds {
1711da177e4SLinus Torvalds 	if (count) {
1721da177e4SLinus Torvalds 		struct ext2_sb_info *sbi = EXT2_SB(sb);
1731da177e4SLinus Torvalds 		unsigned free_blocks;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 		spin_lock(sb_bgl_lock(sbi, group_no));
1761da177e4SLinus Torvalds 		free_blocks = le16_to_cpu(desc->bg_free_blocks_count);
1771da177e4SLinus Torvalds 		desc->bg_free_blocks_count = cpu_to_le16(free_blocks + count);
1781da177e4SLinus Torvalds 		spin_unlock(sb_bgl_lock(sbi, group_no));
1791da177e4SLinus Torvalds 		mark_buffer_dirty(bh);
1801da177e4SLinus Torvalds 	}
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds 
183a686cd89SMartin J. Bligh /*
184a686cd89SMartin J. Bligh  * The reservation window structure operations
185a686cd89SMartin J. Bligh  * --------------------------------------------
186a686cd89SMartin J. Bligh  * Operations include:
187a686cd89SMartin J. Bligh  * dump, find, add, remove, is_empty, find_next_reservable_window, etc.
188a686cd89SMartin J. Bligh  *
189a686cd89SMartin J. Bligh  * We use a red-black tree to represent per-filesystem reservation
190a686cd89SMartin J. Bligh  * windows.
191a686cd89SMartin J. Bligh  *
192a686cd89SMartin J. Bligh  */
193a686cd89SMartin J. Bligh 
194a686cd89SMartin J. Bligh /**
195a686cd89SMartin J. Bligh  * __rsv_window_dump() -- Dump the filesystem block allocation reservation map
196c53ec7bcSWang Hai  * @root:		root of per-filesystem reservation rb tree
197a686cd89SMartin J. Bligh  * @verbose:		verbose mode
198a686cd89SMartin J. Bligh  * @fn:			function which wishes to dump the reservation map
199a686cd89SMartin J. Bligh  *
200a686cd89SMartin J. Bligh  * If verbose is turned on, it will print the whole block reservation
201a686cd89SMartin J. Bligh  * windows(start, end). Otherwise, it will only print out the "bad" windows,
202a686cd89SMartin J. Bligh  * those windows that overlap with their immediate neighbors.
203a686cd89SMartin J. Bligh  */
204a686cd89SMartin J. Bligh #if 1
__rsv_window_dump(struct rb_root * root,int verbose,const char * fn)205a686cd89SMartin J. Bligh static void __rsv_window_dump(struct rb_root *root, int verbose,
206a686cd89SMartin J. Bligh 			      const char *fn)
207a686cd89SMartin J. Bligh {
208a686cd89SMartin J. Bligh 	struct rb_node *n;
209a686cd89SMartin J. Bligh 	struct ext2_reserve_window_node *rsv, *prev;
210a686cd89SMartin J. Bligh 	int bad;
211a686cd89SMartin J. Bligh 
212a686cd89SMartin J. Bligh restart:
213a686cd89SMartin J. Bligh 	n = rb_first(root);
214a686cd89SMartin J. Bligh 	bad = 0;
215a686cd89SMartin J. Bligh 	prev = NULL;
216a686cd89SMartin J. Bligh 
217a686cd89SMartin J. Bligh 	printk("Block Allocation Reservation Windows Map (%s):\n", fn);
218a686cd89SMartin J. Bligh 	while (n) {
219a686cd89SMartin J. Bligh 		rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
220a686cd89SMartin J. Bligh 		if (verbose)
221a686cd89SMartin J. Bligh 			printk("reservation window 0x%p "
222a686cd89SMartin J. Bligh 				"start: %lu, end: %lu\n",
223a686cd89SMartin J. Bligh 				rsv, rsv->rsv_start, rsv->rsv_end);
224a686cd89SMartin J. Bligh 		if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) {
225a686cd89SMartin J. Bligh 			printk("Bad reservation %p (start >= end)\n",
226a686cd89SMartin J. Bligh 			       rsv);
227a686cd89SMartin J. Bligh 			bad = 1;
228a686cd89SMartin J. Bligh 		}
229a686cd89SMartin J. Bligh 		if (prev && prev->rsv_end >= rsv->rsv_start) {
230a686cd89SMartin J. Bligh 			printk("Bad reservation %p (prev->end >= start)\n",
231a686cd89SMartin J. Bligh 			       rsv);
232a686cd89SMartin J. Bligh 			bad = 1;
233a686cd89SMartin J. Bligh 		}
234a686cd89SMartin J. Bligh 		if (bad) {
235a686cd89SMartin J. Bligh 			if (!verbose) {
236a686cd89SMartin J. Bligh 				printk("Restarting reservation walk in verbose mode\n");
237a686cd89SMartin J. Bligh 				verbose = 1;
238a686cd89SMartin J. Bligh 				goto restart;
239a686cd89SMartin J. Bligh 			}
240a686cd89SMartin J. Bligh 		}
241a686cd89SMartin J. Bligh 		n = rb_next(n);
242a686cd89SMartin J. Bligh 		prev = rsv;
243a686cd89SMartin J. Bligh 	}
244a686cd89SMartin J. Bligh 	printk("Window map complete.\n");
2452c11619aSJulia Lawall 	BUG_ON(bad);
246a686cd89SMartin J. Bligh }
247a686cd89SMartin J. Bligh #define rsv_window_dump(root, verbose) \
248605afd60SHarvey Harrison 	__rsv_window_dump((root), (verbose), __func__)
249a686cd89SMartin J. Bligh #else
250a686cd89SMartin J. Bligh #define rsv_window_dump(root, verbose) do {} while (0)
251a686cd89SMartin J. Bligh #endif
252a686cd89SMartin J. Bligh 
253a686cd89SMartin J. Bligh /**
254a686cd89SMartin J. Bligh  * goal_in_my_reservation()
255a686cd89SMartin J. Bligh  * @rsv:		inode's reservation window
256a686cd89SMartin J. Bligh  * @grp_goal:		given goal block relative to the allocation block group
257a686cd89SMartin J. Bligh  * @group:		the current allocation block group
258a686cd89SMartin J. Bligh  * @sb:			filesystem super block
259a686cd89SMartin J. Bligh  *
260a686cd89SMartin J. Bligh  * Test if the given goal block (group relative) is within the file's
261a686cd89SMartin J. Bligh  * own block reservation window range.
262a686cd89SMartin J. Bligh  *
263a686cd89SMartin J. Bligh  * If the reservation window is outside the goal allocation group, return 0;
264a686cd89SMartin J. Bligh  * grp_goal (given goal block) could be -1, which means no specific
265a686cd89SMartin J. Bligh  * goal block. In this case, always return 1.
266a686cd89SMartin J. Bligh  * If the goal block is within the reservation window, return 1;
267a686cd89SMartin J. Bligh  * otherwise, return 0;
268a686cd89SMartin J. Bligh  */
269a686cd89SMartin J. Bligh static int
goal_in_my_reservation(struct ext2_reserve_window * rsv,ext2_grpblk_t grp_goal,unsigned int group,struct super_block * sb)270a686cd89SMartin J. Bligh goal_in_my_reservation(struct ext2_reserve_window *rsv, ext2_grpblk_t grp_goal,
271a686cd89SMartin J. Bligh 			unsigned int group, struct super_block * sb)
272a686cd89SMartin J. Bligh {
273a686cd89SMartin J. Bligh 	ext2_fsblk_t group_first_block, group_last_block;
274a686cd89SMartin J. Bligh 
275a686cd89SMartin J. Bligh 	group_first_block = ext2_group_first_block_no(sb, group);
27690f3741cSChengguang Xu 	group_last_block = ext2_group_last_block_no(sb, group);
277a686cd89SMartin J. Bligh 
278a686cd89SMartin J. Bligh 	if ((rsv->_rsv_start > group_last_block) ||
279a686cd89SMartin J. Bligh 	    (rsv->_rsv_end < group_first_block))
280a686cd89SMartin J. Bligh 		return 0;
281a686cd89SMartin J. Bligh 	if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start)
282a686cd89SMartin J. Bligh 		|| (grp_goal + group_first_block > rsv->_rsv_end)))
283a686cd89SMartin J. Bligh 		return 0;
284a686cd89SMartin J. Bligh 	return 1;
285a686cd89SMartin J. Bligh }
286a686cd89SMartin J. Bligh 
287a686cd89SMartin J. Bligh /**
288a686cd89SMartin J. Bligh  * search_reserve_window()
289c53ec7bcSWang Hai  * @root:		root of reservation tree
290a686cd89SMartin J. Bligh  * @goal:		target allocation block
291a686cd89SMartin J. Bligh  *
292a686cd89SMartin J. Bligh  * Find the reserved window which includes the goal, or the previous one
293a686cd89SMartin J. Bligh  * if the goal is not in any window.
294a686cd89SMartin J. Bligh  * Returns NULL if there are no windows or if all windows start after the goal.
295a686cd89SMartin J. Bligh  */
296a686cd89SMartin J. Bligh static struct ext2_reserve_window_node *
search_reserve_window(struct rb_root * root,ext2_fsblk_t goal)297a686cd89SMartin J. Bligh search_reserve_window(struct rb_root *root, ext2_fsblk_t goal)
298a686cd89SMartin J. Bligh {
299a686cd89SMartin J. Bligh 	struct rb_node *n = root->rb_node;
300a686cd89SMartin J. Bligh 	struct ext2_reserve_window_node *rsv;
301a686cd89SMartin J. Bligh 
302a686cd89SMartin J. Bligh 	if (!n)
303a686cd89SMartin J. Bligh 		return NULL;
304a686cd89SMartin J. Bligh 
305a686cd89SMartin J. Bligh 	do {
306a686cd89SMartin J. Bligh 		rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
307a686cd89SMartin J. Bligh 
308a686cd89SMartin J. Bligh 		if (goal < rsv->rsv_start)
309a686cd89SMartin J. Bligh 			n = n->rb_left;
310a686cd89SMartin J. Bligh 		else if (goal > rsv->rsv_end)
311a686cd89SMartin J. Bligh 			n = n->rb_right;
312a686cd89SMartin J. Bligh 		else
313a686cd89SMartin J. Bligh 			return rsv;
314a686cd89SMartin J. Bligh 	} while (n);
315a686cd89SMartin J. Bligh 	/*
316a686cd89SMartin J. Bligh 	 * We've fallen off the end of the tree: the goal wasn't inside
317a686cd89SMartin J. Bligh 	 * any particular node.  OK, the previous node must be to one
318a686cd89SMartin J. Bligh 	 * side of the interval containing the goal.  If it's the RHS,
319a686cd89SMartin J. Bligh 	 * we need to back up one.
320a686cd89SMartin J. Bligh 	 */
321a686cd89SMartin J. Bligh 	if (rsv->rsv_start > goal) {
322a686cd89SMartin J. Bligh 		n = rb_prev(&rsv->rsv_node);
323a686cd89SMartin J. Bligh 		rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
324a686cd89SMartin J. Bligh 	}
325a686cd89SMartin J. Bligh 	return rsv;
326a686cd89SMartin J. Bligh }
327a686cd89SMartin J. Bligh 
328a686cd89SMartin J. Bligh /*
329a686cd89SMartin J. Bligh  * ext2_rsv_window_add() -- Insert a window to the block reservation rb tree.
330a686cd89SMartin J. Bligh  * @sb:			super block
331a686cd89SMartin J. Bligh  * @rsv:		reservation window to add
332a686cd89SMartin J. Bligh  *
333a686cd89SMartin J. Bligh  * Must be called with rsv_lock held.
334a686cd89SMartin J. Bligh  */
ext2_rsv_window_add(struct super_block * sb,struct ext2_reserve_window_node * rsv)335a686cd89SMartin J. Bligh void ext2_rsv_window_add(struct super_block *sb,
336a686cd89SMartin J. Bligh 		    struct ext2_reserve_window_node *rsv)
337a686cd89SMartin J. Bligh {
338a686cd89SMartin J. Bligh 	struct rb_root *root = &EXT2_SB(sb)->s_rsv_window_root;
339a686cd89SMartin J. Bligh 	struct rb_node *node = &rsv->rsv_node;
340a686cd89SMartin J. Bligh 	ext2_fsblk_t start = rsv->rsv_start;
341a686cd89SMartin J. Bligh 
342a686cd89SMartin J. Bligh 	struct rb_node ** p = &root->rb_node;
343a686cd89SMartin J. Bligh 	struct rb_node * parent = NULL;
344a686cd89SMartin J. Bligh 	struct ext2_reserve_window_node *this;
345a686cd89SMartin J. Bligh 
346a686cd89SMartin J. Bligh 	while (*p)
347a686cd89SMartin J. Bligh 	{
348a686cd89SMartin J. Bligh 		parent = *p;
349a686cd89SMartin J. Bligh 		this = rb_entry(parent, struct ext2_reserve_window_node, rsv_node);
350a686cd89SMartin J. Bligh 
351a686cd89SMartin J. Bligh 		if (start < this->rsv_start)
352a686cd89SMartin J. Bligh 			p = &(*p)->rb_left;
353a686cd89SMartin J. Bligh 		else if (start > this->rsv_end)
354a686cd89SMartin J. Bligh 			p = &(*p)->rb_right;
355a686cd89SMartin J. Bligh 		else {
356a686cd89SMartin J. Bligh 			rsv_window_dump(root, 1);
357a686cd89SMartin J. Bligh 			BUG();
358a686cd89SMartin J. Bligh 		}
359a686cd89SMartin J. Bligh 	}
360a686cd89SMartin J. Bligh 
361a686cd89SMartin J. Bligh 	rb_link_node(node, parent, p);
362a686cd89SMartin J. Bligh 	rb_insert_color(node, root);
363a686cd89SMartin J. Bligh }
364a686cd89SMartin J. Bligh 
365a686cd89SMartin J. Bligh /**
366a686cd89SMartin J. Bligh  * rsv_window_remove() -- unlink a window from the reservation rb tree
367a686cd89SMartin J. Bligh  * @sb:			super block
368a686cd89SMartin J. Bligh  * @rsv:		reservation window to remove
369a686cd89SMartin J. Bligh  *
370a686cd89SMartin J. Bligh  * Mark the block reservation window as not allocated, and unlink it
371a686cd89SMartin J. Bligh  * from the filesystem reservation window rb tree. Must be called with
372a686cd89SMartin J. Bligh  * rsv_lock held.
373a686cd89SMartin J. Bligh  */
rsv_window_remove(struct super_block * sb,struct ext2_reserve_window_node * rsv)374a686cd89SMartin J. Bligh static void rsv_window_remove(struct super_block *sb,
375a686cd89SMartin J. Bligh 			      struct ext2_reserve_window_node *rsv)
376a686cd89SMartin J. Bligh {
377a686cd89SMartin J. Bligh 	rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
378a686cd89SMartin J. Bligh 	rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
379a686cd89SMartin J. Bligh 	rsv->rsv_alloc_hit = 0;
380a686cd89SMartin J. Bligh 	rb_erase(&rsv->rsv_node, &EXT2_SB(sb)->s_rsv_window_root);
381a686cd89SMartin J. Bligh }
382a686cd89SMartin J. Bligh 
383a686cd89SMartin J. Bligh /*
384a686cd89SMartin J. Bligh  * rsv_is_empty() -- Check if the reservation window is allocated.
385a686cd89SMartin J. Bligh  * @rsv:		given reservation window to check
386a686cd89SMartin J. Bligh  *
387a686cd89SMartin J. Bligh  * returns 1 if the end block is EXT2_RESERVE_WINDOW_NOT_ALLOCATED.
388a686cd89SMartin J. Bligh  */
rsv_is_empty(struct ext2_reserve_window * rsv)389a686cd89SMartin J. Bligh static inline int rsv_is_empty(struct ext2_reserve_window *rsv)
390a686cd89SMartin J. Bligh {
391a686cd89SMartin J. Bligh 	/* a valid reservation end block could not be 0 */
392a686cd89SMartin J. Bligh 	return (rsv->_rsv_end == EXT2_RESERVE_WINDOW_NOT_ALLOCATED);
393a686cd89SMartin J. Bligh }
394a686cd89SMartin J. Bligh 
395a686cd89SMartin J. Bligh /**
396a686cd89SMartin J. Bligh  * ext2_init_block_alloc_info()
397a686cd89SMartin J. Bligh  * @inode:		file inode structure
398a686cd89SMartin J. Bligh  *
399a686cd89SMartin J. Bligh  * Allocate and initialize the  reservation window structure, and
400a686cd89SMartin J. Bligh  * link the window to the ext2 inode structure at last
401a686cd89SMartin J. Bligh  *
402a686cd89SMartin J. Bligh  * The reservation window structure is only dynamically allocated
403a686cd89SMartin J. Bligh  * and linked to ext2 inode the first time the open file
404a686cd89SMartin J. Bligh  * needs a new block. So, before every ext2_new_block(s) call, for
405a686cd89SMartin J. Bligh  * regular files, we should check whether the reservation window
406a686cd89SMartin J. Bligh  * structure exists or not. In the latter case, this function is called.
407a686cd89SMartin J. Bligh  * Fail to do so will result in block reservation being turned off for that
408a686cd89SMartin J. Bligh  * open file.
409a686cd89SMartin J. Bligh  *
410a686cd89SMartin J. Bligh  * This function is called from ext2_get_blocks_handle(), also called
411a686cd89SMartin J. Bligh  * when setting the reservation window size through ioctl before the file
412a686cd89SMartin J. Bligh  * is open for write (needs block allocation).
413a686cd89SMartin J. Bligh  *
414a686cd89SMartin J. Bligh  * Needs truncate_mutex protection prior to calling this function.
415a686cd89SMartin J. Bligh  */
ext2_init_block_alloc_info(struct inode * inode)416a686cd89SMartin J. Bligh void ext2_init_block_alloc_info(struct inode *inode)
417a686cd89SMartin J. Bligh {
418a686cd89SMartin J. Bligh 	struct ext2_inode_info *ei = EXT2_I(inode);
4196e3d6ca0SJulia Lawall 	struct ext2_block_alloc_info *block_i;
420a686cd89SMartin J. Bligh 	struct super_block *sb = inode->i_sb;
421a686cd89SMartin J. Bligh 
422a686cd89SMartin J. Bligh 	block_i = kmalloc(sizeof(*block_i), GFP_NOFS);
423a686cd89SMartin J. Bligh 	if (block_i) {
424a686cd89SMartin J. Bligh 		struct ext2_reserve_window_node *rsv = &block_i->rsv_window_node;
425a686cd89SMartin J. Bligh 
426a686cd89SMartin J. Bligh 		rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
427a686cd89SMartin J. Bligh 		rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
428a686cd89SMartin J. Bligh 
429a686cd89SMartin J. Bligh 	 	/*
430a686cd89SMartin J. Bligh 		 * if filesystem is mounted with NORESERVATION, the goal
431a686cd89SMartin J. Bligh 		 * reservation window size is set to zero to indicate
432a686cd89SMartin J. Bligh 		 * block reservation is off
433a686cd89SMartin J. Bligh 		 */
434a686cd89SMartin J. Bligh 		if (!test_opt(sb, RESERVATION))
435a686cd89SMartin J. Bligh 			rsv->rsv_goal_size = 0;
436a686cd89SMartin J. Bligh 		else
437a686cd89SMartin J. Bligh 			rsv->rsv_goal_size = EXT2_DEFAULT_RESERVE_BLOCKS;
438a686cd89SMartin J. Bligh 		rsv->rsv_alloc_hit = 0;
439a686cd89SMartin J. Bligh 		block_i->last_alloc_logical_block = 0;
440a686cd89SMartin J. Bligh 		block_i->last_alloc_physical_block = 0;
441a686cd89SMartin J. Bligh 	}
442a686cd89SMartin J. Bligh 	ei->i_block_alloc_info = block_i;
443a686cd89SMartin J. Bligh }
444a686cd89SMartin J. Bligh 
445a686cd89SMartin J. Bligh /**
446a686cd89SMartin J. Bligh  * ext2_discard_reservation()
447a686cd89SMartin J. Bligh  * @inode:		inode
448a686cd89SMartin J. Bligh  *
449a686cd89SMartin J. Bligh  * Discard(free) block reservation window on last file close, or truncate
450a686cd89SMartin J. Bligh  * or at last iput().
451a686cd89SMartin J. Bligh  *
452a686cd89SMartin J. Bligh  * It is being called in three cases:
453a686cd89SMartin J. Bligh  * 	ext2_release_file(): last writer closes the file
454a686cd89SMartin J. Bligh  * 	ext2_clear_inode(): last iput(), when nobody links to this file.
455a686cd89SMartin J. Bligh  * 	ext2_truncate(): when the block indirect map is about to change.
456a686cd89SMartin J. Bligh  */
ext2_discard_reservation(struct inode * inode)457a686cd89SMartin J. Bligh void ext2_discard_reservation(struct inode *inode)
458a686cd89SMartin J. Bligh {
459a686cd89SMartin J. Bligh 	struct ext2_inode_info *ei = EXT2_I(inode);
460a686cd89SMartin J. Bligh 	struct ext2_block_alloc_info *block_i = ei->i_block_alloc_info;
461a686cd89SMartin J. Bligh 	struct ext2_reserve_window_node *rsv;
462a686cd89SMartin J. Bligh 	spinlock_t *rsv_lock = &EXT2_SB(inode->i_sb)->s_rsv_window_lock;
463a686cd89SMartin J. Bligh 
464a686cd89SMartin J. Bligh 	if (!block_i)
465a686cd89SMartin J. Bligh 		return;
466a686cd89SMartin J. Bligh 
467a686cd89SMartin J. Bligh 	rsv = &block_i->rsv_window_node;
468a686cd89SMartin J. Bligh 	if (!rsv_is_empty(&rsv->rsv_window)) {
469a686cd89SMartin J. Bligh 		spin_lock(rsv_lock);
470a686cd89SMartin J. Bligh 		if (!rsv_is_empty(&rsv->rsv_window))
471a686cd89SMartin J. Bligh 			rsv_window_remove(inode->i_sb, rsv);
472a686cd89SMartin J. Bligh 		spin_unlock(rsv_lock);
473a686cd89SMartin J. Bligh 	}
474a686cd89SMartin J. Bligh }
475a686cd89SMartin J. Bligh 
476a686cd89SMartin J. Bligh /**
47703248766SWang Sheng-Hui  * ext2_free_blocks() -- Free given blocks and update quota and i_blocks
478a686cd89SMartin J. Bligh  * @inode:		inode
4794907cb7bSAnatol Pomozov  * @block:		start physical block to free
480a686cd89SMartin J. Bligh  * @count:		number of blocks to free
481a686cd89SMartin J. Bligh  */
ext2_free_blocks(struct inode * inode,ext2_fsblk_t block,unsigned long count)4822ebc736cSGeorg Ottinger void ext2_free_blocks(struct inode * inode, ext2_fsblk_t block,
4831da177e4SLinus Torvalds 		      unsigned long count)
4841da177e4SLinus Torvalds {
4851da177e4SLinus Torvalds 	struct buffer_head *bitmap_bh = NULL;
4861da177e4SLinus Torvalds 	struct buffer_head * bh2;
4871da177e4SLinus Torvalds 	unsigned long block_group;
4881da177e4SLinus Torvalds 	unsigned long bit;
4891da177e4SLinus Torvalds 	unsigned long i;
4901da177e4SLinus Torvalds 	unsigned long overflow;
4911da177e4SLinus Torvalds 	struct super_block * sb = inode->i_sb;
4921da177e4SLinus Torvalds 	struct ext2_sb_info * sbi = EXT2_SB(sb);
4931da177e4SLinus Torvalds 	struct ext2_group_desc * desc;
4941da177e4SLinus Torvalds 	struct ext2_super_block * es = sbi->s_es;
4951da177e4SLinus Torvalds 	unsigned freed = 0, group_freed;
4961da177e4SLinus Torvalds 
497b6aeffc5SChengguang Xu 	if (!ext2_data_block_valid(sbi, block, count)) {
4981da177e4SLinus Torvalds 		ext2_error (sb, "ext2_free_blocks",
4991da177e4SLinus Torvalds 			    "Freeing blocks not in datazone - "
5001da177e4SLinus Torvalds 			    "block = %lu, count = %lu", block, count);
5011da177e4SLinus Torvalds 		goto error_return;
5021da177e4SLinus Torvalds 	}
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	ext2_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1);
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds do_more:
5071da177e4SLinus Torvalds 	overflow = 0;
5081da177e4SLinus Torvalds 	block_group = (block - le32_to_cpu(es->s_first_data_block)) /
5091da177e4SLinus Torvalds 		      EXT2_BLOCKS_PER_GROUP(sb);
5101da177e4SLinus Torvalds 	bit = (block - le32_to_cpu(es->s_first_data_block)) %
5111da177e4SLinus Torvalds 		      EXT2_BLOCKS_PER_GROUP(sb);
5121da177e4SLinus Torvalds 	/*
5131da177e4SLinus Torvalds 	 * Check to see if we are freeing blocks across a group
5141da177e4SLinus Torvalds 	 * boundary.
5151da177e4SLinus Torvalds 	 */
5161da177e4SLinus Torvalds 	if (bit + count > EXT2_BLOCKS_PER_GROUP(sb)) {
5171da177e4SLinus Torvalds 		overflow = bit + count - EXT2_BLOCKS_PER_GROUP(sb);
5181da177e4SLinus Torvalds 		count -= overflow;
5191da177e4SLinus Torvalds 	}
5201da177e4SLinus Torvalds 	brelse(bitmap_bh);
5211da177e4SLinus Torvalds 	bitmap_bh = read_block_bitmap(sb, block_group);
5221da177e4SLinus Torvalds 	if (!bitmap_bh)
5231da177e4SLinus Torvalds 		goto error_return;
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	desc = ext2_get_group_desc (sb, block_group, &bh2);
5261da177e4SLinus Torvalds 	if (!desc)
5271da177e4SLinus Torvalds 		goto error_return;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) ||
5301da177e4SLinus Torvalds 	    in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) ||
5311da177e4SLinus Torvalds 	    in_range (block, le32_to_cpu(desc->bg_inode_table),
5321da177e4SLinus Torvalds 		      sbi->s_itb_per_group) ||
5331da177e4SLinus Torvalds 	    in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table),
5347f0adaecSAneesh Kumar K.V 		      sbi->s_itb_per_group)) {
5351da177e4SLinus Torvalds 		ext2_error (sb, "ext2_free_blocks",
5361da177e4SLinus Torvalds 			    "Freeing blocks in system zones - "
5371da177e4SLinus Torvalds 			    "Block = %lu, count = %lu",
5381da177e4SLinus Torvalds 			    block, count);
5397f0adaecSAneesh Kumar K.V 		goto error_return;
5407f0adaecSAneesh Kumar K.V 	}
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	for (i = 0, group_freed = 0; i < count; i++) {
5431da177e4SLinus Torvalds 		if (!ext2_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
5441da177e4SLinus Torvalds 						bit + i, bitmap_bh->b_data)) {
545605afd60SHarvey Harrison 			ext2_error(sb, __func__,
5461da177e4SLinus Torvalds 				"bit already cleared for block %lu", block + i);
5471da177e4SLinus Torvalds 		} else {
5481da177e4SLinus Torvalds 			group_freed++;
5491da177e4SLinus Torvalds 		}
5501da177e4SLinus Torvalds 	}
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	mark_buffer_dirty(bitmap_bh);
5531751e8a6SLinus Torvalds 	if (sb->s_flags & SB_SYNCHRONOUS)
5541da177e4SLinus Torvalds 		sync_dirty_buffer(bitmap_bh);
5551da177e4SLinus Torvalds 
556a686cd89SMartin J. Bligh 	group_adjust_blocks(sb, block_group, desc, bh2, group_freed);
5571da177e4SLinus Torvalds 	freed += group_freed;
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	if (overflow) {
5601da177e4SLinus Torvalds 		block += count;
5611da177e4SLinus Torvalds 		count = overflow;
5621da177e4SLinus Torvalds 		goto do_more;
5631da177e4SLinus Torvalds 	}
5641da177e4SLinus Torvalds error_return:
5651da177e4SLinus Torvalds 	brelse(bitmap_bh);
5668e3dffc6SWang Shilong 	if (freed) {
567712ddc52SWang Shilong 		percpu_counter_add(&sbi->s_freeblocks_counter, freed);
5683889717dSAl Viro 		dquot_free_block_nodirty(inode, freed);
5698e3dffc6SWang Shilong 		mark_inode_dirty(inode);
5708e3dffc6SWang Shilong 	}
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
573a686cd89SMartin J. Bligh /**
574a686cd89SMartin J. Bligh  * bitmap_search_next_usable_block()
575a686cd89SMartin J. Bligh  * @start:		the starting block (group relative) of the search
576a686cd89SMartin J. Bligh  * @bh:			bufferhead contains the block group bitmap
577a686cd89SMartin J. Bligh  * @maxblocks:		the ending block (group relative) of the reservation
578a686cd89SMartin J. Bligh  *
579a686cd89SMartin J. Bligh  * The bitmap search --- search forward through the actual bitmap on disk until
580a686cd89SMartin J. Bligh  * we find a bit free.
581a686cd89SMartin J. Bligh  */
582a686cd89SMartin J. Bligh static ext2_grpblk_t
bitmap_search_next_usable_block(ext2_grpblk_t start,struct buffer_head * bh,ext2_grpblk_t maxblocks)583a686cd89SMartin J. Bligh bitmap_search_next_usable_block(ext2_grpblk_t start, struct buffer_head *bh,
584a686cd89SMartin J. Bligh 					ext2_grpblk_t maxblocks)
5851da177e4SLinus Torvalds {
586a686cd89SMartin J. Bligh 	ext2_grpblk_t next;
587a686cd89SMartin J. Bligh 
588a686cd89SMartin J. Bligh 	next = ext2_find_next_zero_bit(bh->b_data, maxblocks, start);
589a686cd89SMartin J. Bligh 	if (next >= maxblocks)
590a686cd89SMartin J. Bligh 		return -1;
591a686cd89SMartin J. Bligh 	return next;
592a686cd89SMartin J. Bligh }
593a686cd89SMartin J. Bligh 
594a686cd89SMartin J. Bligh /**
595a686cd89SMartin J. Bligh  * find_next_usable_block()
596a686cd89SMartin J. Bligh  * @start:		the starting block (group relative) to find next
597a686cd89SMartin J. Bligh  * 			allocatable block in bitmap.
598a686cd89SMartin J. Bligh  * @bh:			bufferhead contains the block group bitmap
599a686cd89SMartin J. Bligh  * @maxblocks:		the ending block (group relative) for the search
600a686cd89SMartin J. Bligh  *
601a686cd89SMartin J. Bligh  * Find an allocatable block in a bitmap.  We perform the "most
602a686cd89SMartin J. Bligh  * appropriate allocation" algorithm of looking for a free block near
603a686cd89SMartin J. Bligh  * the initial goal; then for a free byte somewhere in the bitmap;
604a686cd89SMartin J. Bligh  * then for any free bit in the bitmap.
605a686cd89SMartin J. Bligh  */
606a686cd89SMartin J. Bligh static ext2_grpblk_t
find_next_usable_block(int start,struct buffer_head * bh,int maxblocks)607a686cd89SMartin J. Bligh find_next_usable_block(int start, struct buffer_head *bh, int maxblocks)
608a686cd89SMartin J. Bligh {
609a686cd89SMartin J. Bligh 	ext2_grpblk_t here, next;
6101da177e4SLinus Torvalds 	char *p, *r;
6111da177e4SLinus Torvalds 
612a686cd89SMartin J. Bligh 	if (start > 0) {
6131da177e4SLinus Torvalds 		/*
6141da177e4SLinus Torvalds 		 * The goal was occupied; search forward for a free
6151da177e4SLinus Torvalds 		 * block within the next XX blocks.
6161da177e4SLinus Torvalds 		 *
6171da177e4SLinus Torvalds 		 * end_goal is more or less random, but it has to be
6181da177e4SLinus Torvalds 		 * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the
6191da177e4SLinus Torvalds 		 * next 64-bit boundary is simple..
6201da177e4SLinus Torvalds 		 */
621a686cd89SMartin J. Bligh 		ext2_grpblk_t end_goal = (start + 63) & ~63;
622a686cd89SMartin J. Bligh 		if (end_goal > maxblocks)
623a686cd89SMartin J. Bligh 			end_goal = maxblocks;
624a686cd89SMartin J. Bligh 		here = ext2_find_next_zero_bit(bh->b_data, end_goal, start);
625a686cd89SMartin J. Bligh 		if (here < end_goal)
626a686cd89SMartin J. Bligh 			return here;
627a686cd89SMartin J. Bligh 		ext2_debug("Bit not found near goal\n");
6281da177e4SLinus Torvalds 	}
6291da177e4SLinus Torvalds 
630a686cd89SMartin J. Bligh 	here = start;
631a686cd89SMartin J. Bligh 	if (here < 0)
632a686cd89SMartin J. Bligh 		here = 0;
633a686cd89SMartin J. Bligh 
634a686cd89SMartin J. Bligh 	p = ((char *)bh->b_data) + (here >> 3);
635a686cd89SMartin J. Bligh 	r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3));
636a686cd89SMartin J. Bligh 	next = (r - ((char *)bh->b_data)) << 3;
637a686cd89SMartin J. Bligh 
638a686cd89SMartin J. Bligh 	if (next < maxblocks && next >= here)
639a686cd89SMartin J. Bligh 		return next;
640a686cd89SMartin J. Bligh 
641a686cd89SMartin J. Bligh 	here = bitmap_search_next_usable_block(here, bh, maxblocks);
642a686cd89SMartin J. Bligh 	return here;
643a686cd89SMartin J. Bligh }
644a686cd89SMartin J. Bligh 
64526f78b7aSNamhyung Kim /**
646a686cd89SMartin J. Bligh  * ext2_try_to_allocate()
647a686cd89SMartin J. Bligh  * @sb:			superblock
648a686cd89SMartin J. Bligh  * @group:		given allocation block group
649a686cd89SMartin J. Bligh  * @bitmap_bh:		bufferhead holds the block bitmap
650a686cd89SMartin J. Bligh  * @grp_goal:		given target block within the group
651a686cd89SMartin J. Bligh  * @count:		target number of blocks to allocate
652a686cd89SMartin J. Bligh  * @my_rsv:		reservation window
653a686cd89SMartin J. Bligh  *
654a686cd89SMartin J. Bligh  * Attempt to allocate blocks within a give range. Set the range of allocation
655a686cd89SMartin J. Bligh  * first, then find the first free bit(s) from the bitmap (within the range),
656a686cd89SMartin J. Bligh  * and at last, allocate the blocks by claiming the found free bit as allocated.
657a686cd89SMartin J. Bligh  *
658a686cd89SMartin J. Bligh  * To set the range of this allocation:
659a686cd89SMartin J. Bligh  * 	if there is a reservation window, only try to allocate block(s)
660a686cd89SMartin J. Bligh  * 	from the file's own reservation window;
661a686cd89SMartin J. Bligh  * 	Otherwise, the allocation range starts from the give goal block,
662a686cd89SMartin J. Bligh  * 	ends at the block group's last block.
663a686cd89SMartin J. Bligh  *
664a686cd89SMartin J. Bligh  * If we failed to allocate the desired block then we may end up crossing to a
665a686cd89SMartin J. Bligh  * new bitmap.
6661da177e4SLinus Torvalds  */
667a686cd89SMartin J. Bligh static int
ext2_try_to_allocate(struct super_block * sb,int group,struct buffer_head * bitmap_bh,ext2_grpblk_t grp_goal,unsigned long * count,struct ext2_reserve_window * my_rsv)668a686cd89SMartin J. Bligh ext2_try_to_allocate(struct super_block *sb, int group,
669a686cd89SMartin J. Bligh 			struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal,
670a686cd89SMartin J. Bligh 			unsigned long *count,
671a686cd89SMartin J. Bligh 			struct ext2_reserve_window *my_rsv)
672a686cd89SMartin J. Bligh {
67390f3741cSChengguang Xu 	ext2_fsblk_t group_first_block = ext2_group_first_block_no(sb, group);
67490f3741cSChengguang Xu 	ext2_fsblk_t group_last_block = ext2_group_last_block_no(sb, group);
675a686cd89SMartin J. Bligh 	ext2_grpblk_t start, end;
676a686cd89SMartin J. Bligh 	unsigned long num = 0;
677a686cd89SMartin J. Bligh 
678cf4eb321SJan Kara 	start = 0;
679cf4eb321SJan Kara 	end = group_last_block - group_first_block + 1;
680a686cd89SMartin J. Bligh 	/* we do allocation within the reservation window if we have a window */
681a686cd89SMartin J. Bligh 	if (my_rsv) {
682a686cd89SMartin J. Bligh 		if (my_rsv->_rsv_start >= group_first_block)
683a686cd89SMartin J. Bligh 			start = my_rsv->_rsv_start - group_first_block;
684cf4eb321SJan Kara 		if (my_rsv->_rsv_end < group_last_block)
685a686cd89SMartin J. Bligh 			end = my_rsv->_rsv_end - group_first_block + 1;
686cf4eb321SJan Kara 		if (grp_goal < start || grp_goal >= end)
687a686cd89SMartin J. Bligh 			grp_goal = -1;
688a686cd89SMartin J. Bligh 	}
689a686cd89SMartin J. Bligh 	BUG_ON(start > EXT2_BLOCKS_PER_GROUP(sb));
690a686cd89SMartin J. Bligh 
691a686cd89SMartin J. Bligh 	if (grp_goal < 0) {
692a686cd89SMartin J. Bligh 		grp_goal = find_next_usable_block(start, bitmap_bh, end);
693a686cd89SMartin J. Bligh 		if (grp_goal < 0)
694a686cd89SMartin J. Bligh 			goto fail_access;
695a686cd89SMartin J. Bligh 		if (!my_rsv) {
696a686cd89SMartin J. Bligh 			int i;
697a686cd89SMartin J. Bligh 
698a686cd89SMartin J. Bligh 			for (i = 0; i < 7 && grp_goal > start &&
699a686cd89SMartin J. Bligh 					!ext2_test_bit(grp_goal - 1,
700a686cd89SMartin J. Bligh 					     		bitmap_bh->b_data);
701a686cd89SMartin J. Bligh 			     		i++, grp_goal--)
7021da177e4SLinus Torvalds 				;
703a686cd89SMartin J. Bligh 		}
704a686cd89SMartin J. Bligh 	}
705a686cd89SMartin J. Bligh 
70644dd6161SChengguang Xu 	for (; num < *count && grp_goal < end; grp_goal++) {
70744dd6161SChengguang Xu 		if (ext2_set_bit_atomic(sb_bgl_lock(EXT2_SB(sb), group),
708a686cd89SMartin J. Bligh 					grp_goal, bitmap_bh->b_data)) {
70944dd6161SChengguang Xu 			if (num == 0)
71044dd6161SChengguang Xu 				continue;
71144dd6161SChengguang Xu 			break;
712a686cd89SMartin J. Bligh 		}
71344dd6161SChengguang Xu 		num++;
71444dd6161SChengguang Xu 	}
71544dd6161SChengguang Xu 
71644dd6161SChengguang Xu 	if (num == 0)
71744dd6161SChengguang Xu 		goto fail_access;
71844dd6161SChengguang Xu 
719a686cd89SMartin J. Bligh 	*count = num;
720a686cd89SMartin J. Bligh 	return grp_goal - num;
721a686cd89SMartin J. Bligh fail_access:
722a686cd89SMartin J. Bligh 	return -1;
7231da177e4SLinus Torvalds }
7241da177e4SLinus Torvalds 
725a686cd89SMartin J. Bligh /**
726df1ae36aSMatthew Wilcox (Oracle)  * find_next_reservable_window - Find a reservable space within the given range.
727df1ae36aSMatthew Wilcox (Oracle)  * @search_head: The list to search.
728df1ae36aSMatthew Wilcox (Oracle)  * @my_rsv: The reservation we're currently using.
729df1ae36aSMatthew Wilcox (Oracle)  * @sb: The super block.
730df1ae36aSMatthew Wilcox (Oracle)  * @start_block: The first block we consider to start the real search from
731df1ae36aSMatthew Wilcox (Oracle)  * @last_block: The maximum block number that our goal reservable space
732df1ae36aSMatthew Wilcox (Oracle)  *	could start from.
733a686cd89SMartin J. Bligh  *
734df1ae36aSMatthew Wilcox (Oracle)  * It does not allocate the reservation window: alloc_new_reservation()
735df1ae36aSMatthew Wilcox (Oracle)  * will do the work later.
736a686cd89SMartin J. Bligh  *
737df1ae36aSMatthew Wilcox (Oracle)  * We search the given range, rather than the whole reservation double
738df1ae36aSMatthew Wilcox (Oracle)  * linked list, (start_block, last_block) to find a free region that is
739df1ae36aSMatthew Wilcox (Oracle)  * of my size and has not been reserved.
740a686cd89SMartin J. Bligh  *
741df1ae36aSMatthew Wilcox (Oracle)  * @search_head is not necessarily the list head of the whole filesystem.
742df1ae36aSMatthew Wilcox (Oracle)  * We have both head and @start_block to assist the search for the
743df1ae36aSMatthew Wilcox (Oracle)  * reservable space. The list starts from head, but we will shift to
744df1ae36aSMatthew Wilcox (Oracle)  * the place where start_block is, then start from there, when looking
745df1ae36aSMatthew Wilcox (Oracle)  * for a reservable space.
746a686cd89SMartin J. Bligh  *
747df1ae36aSMatthew Wilcox (Oracle)  * @last_block is normally the last block in this group. The search will end
748df1ae36aSMatthew Wilcox (Oracle)  * when we found the start of next possible reservable space is out
749df1ae36aSMatthew Wilcox (Oracle)  * of this boundary.  This could handle the cross boundary reservation
750df1ae36aSMatthew Wilcox (Oracle)  * window request.
751a686cd89SMartin J. Bligh  *
752df1ae36aSMatthew Wilcox (Oracle)  * Return: -1 if we could not find a range of sufficient size.  If we could,
753df1ae36aSMatthew Wilcox (Oracle)  * return 0 and fill in @my_rsv with the range information.
754a686cd89SMartin J. Bligh  */
find_next_reservable_window(struct ext2_reserve_window_node * search_head,struct ext2_reserve_window_node * my_rsv,struct super_block * sb,ext2_fsblk_t start_block,ext2_fsblk_t last_block)755a686cd89SMartin J. Bligh static int find_next_reservable_window(
756a686cd89SMartin J. Bligh 				struct ext2_reserve_window_node *search_head,
757a686cd89SMartin J. Bligh 				struct ext2_reserve_window_node *my_rsv,
758a686cd89SMartin J. Bligh 				struct super_block * sb,
759a686cd89SMartin J. Bligh 				ext2_fsblk_t start_block,
760a686cd89SMartin J. Bligh 				ext2_fsblk_t last_block)
761a686cd89SMartin J. Bligh {
762a686cd89SMartin J. Bligh 	struct rb_node *next;
763a686cd89SMartin J. Bligh 	struct ext2_reserve_window_node *rsv, *prev;
764a686cd89SMartin J. Bligh 	ext2_fsblk_t cur;
765a686cd89SMartin J. Bligh 	int size = my_rsv->rsv_goal_size;
766a686cd89SMartin J. Bligh 
767a686cd89SMartin J. Bligh 	/* TODO: make the start of the reservation window byte-aligned */
768a686cd89SMartin J. Bligh 	/* cur = *start_block & ~7;*/
769a686cd89SMartin J. Bligh 	cur = start_block;
770a686cd89SMartin J. Bligh 	rsv = search_head;
771a686cd89SMartin J. Bligh 	if (!rsv)
7721da177e4SLinus Torvalds 		return -1;
773a686cd89SMartin J. Bligh 
774a686cd89SMartin J. Bligh 	while (1) {
775a686cd89SMartin J. Bligh 		if (cur <= rsv->rsv_end)
776a686cd89SMartin J. Bligh 			cur = rsv->rsv_end + 1;
777a686cd89SMartin J. Bligh 
778a686cd89SMartin J. Bligh 		/* TODO?
779a686cd89SMartin J. Bligh 		 * in the case we could not find a reservable space
780a686cd89SMartin J. Bligh 		 * that is what is expected, during the re-search, we could
781a686cd89SMartin J. Bligh 		 * remember what's the largest reservable space we could have
782a686cd89SMartin J. Bligh 		 * and return that one.
783a686cd89SMartin J. Bligh 		 *
784a686cd89SMartin J. Bligh 		 * For now it will fail if we could not find the reservable
785a686cd89SMartin J. Bligh 		 * space with expected-size (or more)...
786a686cd89SMartin J. Bligh 		 */
787a686cd89SMartin J. Bligh 		if (cur > last_block)
788a686cd89SMartin J. Bligh 			return -1;		/* fail */
789a686cd89SMartin J. Bligh 
790a686cd89SMartin J. Bligh 		prev = rsv;
791a686cd89SMartin J. Bligh 		next = rb_next(&rsv->rsv_node);
792a686cd89SMartin J. Bligh 		rsv = rb_entry(next,struct ext2_reserve_window_node,rsv_node);
793a686cd89SMartin J. Bligh 
794a686cd89SMartin J. Bligh 		/*
795a686cd89SMartin J. Bligh 		 * Reached the last reservation, we can just append to the
796a686cd89SMartin J. Bligh 		 * previous one.
797a686cd89SMartin J. Bligh 		 */
798a686cd89SMartin J. Bligh 		if (!next)
799a686cd89SMartin J. Bligh 			break;
800a686cd89SMartin J. Bligh 
801a686cd89SMartin J. Bligh 		if (cur + size <= rsv->rsv_start) {
802a686cd89SMartin J. Bligh 			/*
803a686cd89SMartin J. Bligh 			 * Found a reserveable space big enough.  We could
804a686cd89SMartin J. Bligh 			 * have a reservation across the group boundary here
805a686cd89SMartin J. Bligh 		 	 */
806a686cd89SMartin J. Bligh 			break;
807a686cd89SMartin J. Bligh 		}
808a686cd89SMartin J. Bligh 	}
809a686cd89SMartin J. Bligh 	/*
810a686cd89SMartin J. Bligh 	 * we come here either :
811a686cd89SMartin J. Bligh 	 * when we reach the end of the whole list,
812a686cd89SMartin J. Bligh 	 * and there is empty reservable space after last entry in the list.
813a686cd89SMartin J. Bligh 	 * append it to the end of the list.
814a686cd89SMartin J. Bligh 	 *
815a686cd89SMartin J. Bligh 	 * or we found one reservable space in the middle of the list,
816a686cd89SMartin J. Bligh 	 * return the reservation window that we could append to.
817a686cd89SMartin J. Bligh 	 * succeed.
818a686cd89SMartin J. Bligh 	 */
819a686cd89SMartin J. Bligh 
820a686cd89SMartin J. Bligh 	if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window)))
821a686cd89SMartin J. Bligh 		rsv_window_remove(sb, my_rsv);
822a686cd89SMartin J. Bligh 
823a686cd89SMartin J. Bligh 	/*
82425985edcSLucas De Marchi 	 * Let's book the whole available window for now.  We will check the
825a686cd89SMartin J. Bligh 	 * disk bitmap later and then, if there are free blocks then we adjust
826a686cd89SMartin J. Bligh 	 * the window size if it's larger than requested.
827a686cd89SMartin J. Bligh 	 * Otherwise, we will remove this node from the tree next time
828a686cd89SMartin J. Bligh 	 * call find_next_reservable_window.
829a686cd89SMartin J. Bligh 	 */
830a686cd89SMartin J. Bligh 	my_rsv->rsv_start = cur;
831a686cd89SMartin J. Bligh 	my_rsv->rsv_end = cur + size - 1;
832a686cd89SMartin J. Bligh 	my_rsv->rsv_alloc_hit = 0;
833a686cd89SMartin J. Bligh 
834a686cd89SMartin J. Bligh 	if (prev != my_rsv)
835a686cd89SMartin J. Bligh 		ext2_rsv_window_add(sb, my_rsv);
836a686cd89SMartin J. Bligh 
837a686cd89SMartin J. Bligh 	return 0;
838a686cd89SMartin J. Bligh }
839a686cd89SMartin J. Bligh 
840a686cd89SMartin J. Bligh /**
841df1ae36aSMatthew Wilcox (Oracle)  * alloc_new_reservation - Allocate a new reservation window.
842df1ae36aSMatthew Wilcox (Oracle)  * @my_rsv: The reservation we're currently using.
843df1ae36aSMatthew Wilcox (Oracle)  * @grp_goal: The goal block relative to the start of the group.
844df1ae36aSMatthew Wilcox (Oracle)  * @sb: The super block.
845df1ae36aSMatthew Wilcox (Oracle)  * @group: The group we are trying to allocate in.
846df1ae36aSMatthew Wilcox (Oracle)  * @bitmap_bh: The block group block bitmap.
847a686cd89SMartin J. Bligh  *
848df1ae36aSMatthew Wilcox (Oracle)  * To make a new reservation, we search part of the filesystem reservation
849df1ae36aSMatthew Wilcox (Oracle)  * list (the list inside the group). We try to allocate a new
850df1ae36aSMatthew Wilcox (Oracle)  * reservation window near @grp_goal, or the beginning of the
851df1ae36aSMatthew Wilcox (Oracle)  * group, if @grp_goal is negative.
852a686cd89SMartin J. Bligh  *
853df1ae36aSMatthew Wilcox (Oracle)  * We first find a reservable space after the goal, then from there,
854df1ae36aSMatthew Wilcox (Oracle)  * we check the bitmap for the first free block after it. If there is
855df1ae36aSMatthew Wilcox (Oracle)  * no free block until the end of group, then the whole group is full,
856df1ae36aSMatthew Wilcox (Oracle)  * we failed. Otherwise, check if the free block is inside the expected
857df1ae36aSMatthew Wilcox (Oracle)  * reservable space, if so, we succeed.
858a686cd89SMartin J. Bligh  *
859df1ae36aSMatthew Wilcox (Oracle)  * If the first free block is outside the reservable space, then start
860df1ae36aSMatthew Wilcox (Oracle)  * from the first free block, we search for next available space, and
861df1ae36aSMatthew Wilcox (Oracle)  * go on.
862a686cd89SMartin J. Bligh  *
863df1ae36aSMatthew Wilcox (Oracle)  * on succeed, a new reservation will be found and inserted into the
864df1ae36aSMatthew Wilcox (Oracle)  * list. It contains at least one free block, and it does not overlap
865df1ae36aSMatthew Wilcox (Oracle)  * with other reservation windows.
866a686cd89SMartin J. Bligh  *
867df1ae36aSMatthew Wilcox (Oracle)  * Return: 0 on success, -1 if we failed to find a reservation window
868df1ae36aSMatthew Wilcox (Oracle)  * in this group
869a686cd89SMartin J. Bligh  */
alloc_new_reservation(struct ext2_reserve_window_node * my_rsv,ext2_grpblk_t grp_goal,struct super_block * sb,unsigned int group,struct buffer_head * bitmap_bh)870a686cd89SMartin J. Bligh static int alloc_new_reservation(struct ext2_reserve_window_node *my_rsv,
871a686cd89SMartin J. Bligh 		ext2_grpblk_t grp_goal, struct super_block *sb,
872a686cd89SMartin J. Bligh 		unsigned int group, struct buffer_head *bitmap_bh)
873a686cd89SMartin J. Bligh {
874a686cd89SMartin J. Bligh 	struct ext2_reserve_window_node *search_head;
875a686cd89SMartin J. Bligh 	ext2_fsblk_t group_first_block, group_end_block, start_block;
876a686cd89SMartin J. Bligh 	ext2_grpblk_t first_free_block;
877a686cd89SMartin J. Bligh 	struct rb_root *fs_rsv_root = &EXT2_SB(sb)->s_rsv_window_root;
878a686cd89SMartin J. Bligh 	unsigned long size;
879a686cd89SMartin J. Bligh 	int ret;
880a686cd89SMartin J. Bligh 	spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock;
881a686cd89SMartin J. Bligh 
882a686cd89SMartin J. Bligh 	group_first_block = ext2_group_first_block_no(sb, group);
88390f3741cSChengguang Xu 	group_end_block = ext2_group_last_block_no(sb, group);
884a686cd89SMartin J. Bligh 
885a686cd89SMartin J. Bligh 	if (grp_goal < 0)
886a686cd89SMartin J. Bligh 		start_block = group_first_block;
887a686cd89SMartin J. Bligh 	else
888a686cd89SMartin J. Bligh 		start_block = grp_goal + group_first_block;
889a686cd89SMartin J. Bligh 
890a686cd89SMartin J. Bligh 	size = my_rsv->rsv_goal_size;
891a686cd89SMartin J. Bligh 
892a686cd89SMartin J. Bligh 	if (!rsv_is_empty(&my_rsv->rsv_window)) {
893a686cd89SMartin J. Bligh 		/*
894a686cd89SMartin J. Bligh 		 * if the old reservation is cross group boundary
895a686cd89SMartin J. Bligh 		 * and if the goal is inside the old reservation window,
896a686cd89SMartin J. Bligh 		 * we will come here when we just failed to allocate from
897a686cd89SMartin J. Bligh 		 * the first part of the window. We still have another part
898a686cd89SMartin J. Bligh 		 * that belongs to the next group. In this case, there is no
899a686cd89SMartin J. Bligh 		 * point to discard our window and try to allocate a new one
900a686cd89SMartin J. Bligh 		 * in this group(which will fail). we should
901a686cd89SMartin J. Bligh 		 * keep the reservation window, just simply move on.
902a686cd89SMartin J. Bligh 		 *
903a686cd89SMartin J. Bligh 		 * Maybe we could shift the start block of the reservation
904a686cd89SMartin J. Bligh 		 * window to the first block of next group.
905a686cd89SMartin J. Bligh 		 */
906a686cd89SMartin J. Bligh 
907a686cd89SMartin J. Bligh 		if ((my_rsv->rsv_start <= group_end_block) &&
908a686cd89SMartin J. Bligh 				(my_rsv->rsv_end > group_end_block) &&
909a686cd89SMartin J. Bligh 				(start_block >= my_rsv->rsv_start))
910a686cd89SMartin J. Bligh 			return -1;
911a686cd89SMartin J. Bligh 
912a686cd89SMartin J. Bligh 		if ((my_rsv->rsv_alloc_hit >
913a686cd89SMartin J. Bligh 		     (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) {
914a686cd89SMartin J. Bligh 			/*
915a686cd89SMartin J. Bligh 			 * if the previously allocation hit ratio is
916a686cd89SMartin J. Bligh 			 * greater than 1/2, then we double the size of
917a686cd89SMartin J. Bligh 			 * the reservation window the next time,
918a686cd89SMartin J. Bligh 			 * otherwise we keep the same size window
919a686cd89SMartin J. Bligh 			 */
920a686cd89SMartin J. Bligh 			size = size * 2;
921a686cd89SMartin J. Bligh 			if (size > EXT2_MAX_RESERVE_BLOCKS)
922a686cd89SMartin J. Bligh 				size = EXT2_MAX_RESERVE_BLOCKS;
923a686cd89SMartin J. Bligh 			my_rsv->rsv_goal_size= size;
924a686cd89SMartin J. Bligh 		}
925a686cd89SMartin J. Bligh 	}
926a686cd89SMartin J. Bligh 
927a686cd89SMartin J. Bligh 	spin_lock(rsv_lock);
928a686cd89SMartin J. Bligh 	/*
929a686cd89SMartin J. Bligh 	 * shift the search start to the window near the goal block
930a686cd89SMartin J. Bligh 	 */
931a686cd89SMartin J. Bligh 	search_head = search_reserve_window(fs_rsv_root, start_block);
932a686cd89SMartin J. Bligh 
933a686cd89SMartin J. Bligh 	/*
934a686cd89SMartin J. Bligh 	 * find_next_reservable_window() simply finds a reservable window
935a686cd89SMartin J. Bligh 	 * inside the given range(start_block, group_end_block).
936a686cd89SMartin J. Bligh 	 *
937a686cd89SMartin J. Bligh 	 * To make sure the reservation window has a free bit inside it, we
938a686cd89SMartin J. Bligh 	 * need to check the bitmap after we found a reservable window.
939a686cd89SMartin J. Bligh 	 */
940a686cd89SMartin J. Bligh retry:
941a686cd89SMartin J. Bligh 	ret = find_next_reservable_window(search_head, my_rsv, sb,
942a686cd89SMartin J. Bligh 						start_block, group_end_block);
943a686cd89SMartin J. Bligh 
944a686cd89SMartin J. Bligh 	if (ret == -1) {
945a686cd89SMartin J. Bligh 		if (!rsv_is_empty(&my_rsv->rsv_window))
946a686cd89SMartin J. Bligh 			rsv_window_remove(sb, my_rsv);
947a686cd89SMartin J. Bligh 		spin_unlock(rsv_lock);
948a686cd89SMartin J. Bligh 		return -1;
9491da177e4SLinus Torvalds 	}
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	/*
952a686cd89SMartin J. Bligh 	 * On success, find_next_reservable_window() returns the
953a686cd89SMartin J. Bligh 	 * reservation window where there is a reservable space after it.
954a686cd89SMartin J. Bligh 	 * Before we reserve this reservable space, we need
955a686cd89SMartin J. Bligh 	 * to make sure there is at least a free block inside this region.
956a686cd89SMartin J. Bligh 	 *
957a686cd89SMartin J. Bligh 	 * Search the first free bit on the block bitmap.  Search starts from
958a686cd89SMartin J. Bligh 	 * the start block of the reservable space we just found.
959a686cd89SMartin J. Bligh 	 */
960a686cd89SMartin J. Bligh 	spin_unlock(rsv_lock);
961a686cd89SMartin J. Bligh 	first_free_block = bitmap_search_next_usable_block(
962a686cd89SMartin J. Bligh 			my_rsv->rsv_start - group_first_block,
963a686cd89SMartin J. Bligh 			bitmap_bh, group_end_block - group_first_block + 1);
964a686cd89SMartin J. Bligh 
965a686cd89SMartin J. Bligh 	if (first_free_block < 0) {
966a686cd89SMartin J. Bligh 		/*
967a686cd89SMartin J. Bligh 		 * no free block left on the bitmap, no point
968a686cd89SMartin J. Bligh 		 * to reserve the space. return failed.
969a686cd89SMartin J. Bligh 		 */
970a686cd89SMartin J. Bligh 		spin_lock(rsv_lock);
971a686cd89SMartin J. Bligh 		if (!rsv_is_empty(&my_rsv->rsv_window))
972a686cd89SMartin J. Bligh 			rsv_window_remove(sb, my_rsv);
973a686cd89SMartin J. Bligh 		spin_unlock(rsv_lock);
974a686cd89SMartin J. Bligh 		return -1;		/* failed */
975a686cd89SMartin J. Bligh 	}
976a686cd89SMartin J. Bligh 
977a686cd89SMartin J. Bligh 	start_block = first_free_block + group_first_block;
978a686cd89SMartin J. Bligh 	/*
979a686cd89SMartin J. Bligh 	 * check if the first free block is within the
980a686cd89SMartin J. Bligh 	 * free space we just reserved
981a686cd89SMartin J. Bligh 	 */
982a686cd89SMartin J. Bligh 	if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end)
983a686cd89SMartin J. Bligh 		return 0;		/* success */
984a686cd89SMartin J. Bligh 	/*
985a686cd89SMartin J. Bligh 	 * if the first free bit we found is out of the reservable space
986a686cd89SMartin J. Bligh 	 * continue search for next reservable space,
987a686cd89SMartin J. Bligh 	 * start from where the free block is,
988a686cd89SMartin J. Bligh 	 * we also shift the list head to where we stopped last time
989a686cd89SMartin J. Bligh 	 */
990a686cd89SMartin J. Bligh 	search_head = my_rsv;
991a686cd89SMartin J. Bligh 	spin_lock(rsv_lock);
992a686cd89SMartin J. Bligh 	goto retry;
993a686cd89SMartin J. Bligh }
994a686cd89SMartin J. Bligh 
995a686cd89SMartin J. Bligh /**
996a686cd89SMartin J. Bligh  * try_to_extend_reservation()
997a686cd89SMartin J. Bligh  * @my_rsv:		given reservation window
998a686cd89SMartin J. Bligh  * @sb:			super block
999a686cd89SMartin J. Bligh  * @size:		the delta to extend
1000a686cd89SMartin J. Bligh  *
1001a686cd89SMartin J. Bligh  * Attempt to expand the reservation window large enough to have
1002a686cd89SMartin J. Bligh  * required number of free blocks
1003a686cd89SMartin J. Bligh  *
1004a686cd89SMartin J. Bligh  * Since ext2_try_to_allocate() will always allocate blocks within
1005a686cd89SMartin J. Bligh  * the reservation window range, if the window size is too small,
1006a686cd89SMartin J. Bligh  * multiple blocks allocation has to stop at the end of the reservation
1007a686cd89SMartin J. Bligh  * window. To make this more efficient, given the total number of
1008a686cd89SMartin J. Bligh  * blocks needed and the current size of the window, we try to
1009a686cd89SMartin J. Bligh  * expand the reservation window size if necessary on a best-effort
1010a686cd89SMartin J. Bligh  * basis before ext2_new_blocks() tries to allocate blocks.
1011a686cd89SMartin J. Bligh  */
try_to_extend_reservation(struct ext2_reserve_window_node * my_rsv,struct super_block * sb,int size)1012a686cd89SMartin J. Bligh static void try_to_extend_reservation(struct ext2_reserve_window_node *my_rsv,
1013a686cd89SMartin J. Bligh 			struct super_block *sb, int size)
1014a686cd89SMartin J. Bligh {
1015a686cd89SMartin J. Bligh 	struct ext2_reserve_window_node *next_rsv;
1016a686cd89SMartin J. Bligh 	struct rb_node *next;
1017a686cd89SMartin J. Bligh 	spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock;
1018a686cd89SMartin J. Bligh 
1019a686cd89SMartin J. Bligh 	if (!spin_trylock(rsv_lock))
1020a686cd89SMartin J. Bligh 		return;
1021a686cd89SMartin J. Bligh 
1022a686cd89SMartin J. Bligh 	next = rb_next(&my_rsv->rsv_node);
1023a686cd89SMartin J. Bligh 
1024a686cd89SMartin J. Bligh 	if (!next)
1025a686cd89SMartin J. Bligh 		my_rsv->rsv_end += size;
1026a686cd89SMartin J. Bligh 	else {
1027a686cd89SMartin J. Bligh 		next_rsv = rb_entry(next, struct ext2_reserve_window_node, rsv_node);
1028a686cd89SMartin J. Bligh 
1029a686cd89SMartin J. Bligh 		if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size)
1030a686cd89SMartin J. Bligh 			my_rsv->rsv_end += size;
1031a686cd89SMartin J. Bligh 		else
1032a686cd89SMartin J. Bligh 			my_rsv->rsv_end = next_rsv->rsv_start - 1;
1033a686cd89SMartin J. Bligh 	}
1034a686cd89SMartin J. Bligh 	spin_unlock(rsv_lock);
1035a686cd89SMartin J. Bligh }
1036a686cd89SMartin J. Bligh 
1037a686cd89SMartin J. Bligh /**
1038a686cd89SMartin J. Bligh  * ext2_try_to_allocate_with_rsv()
1039a686cd89SMartin J. Bligh  * @sb:			superblock
1040a686cd89SMartin J. Bligh  * @group:		given allocation block group
1041a686cd89SMartin J. Bligh  * @bitmap_bh:		bufferhead holds the block bitmap
1042a686cd89SMartin J. Bligh  * @grp_goal:		given target block within the group
1043a686cd89SMartin J. Bligh  * @count:		target number of blocks to allocate
1044a686cd89SMartin J. Bligh  * @my_rsv:		reservation window
1045a686cd89SMartin J. Bligh  *
1046a686cd89SMartin J. Bligh  * This is the main function used to allocate a new block and its reservation
1047a686cd89SMartin J. Bligh  * window.
1048a686cd89SMartin J. Bligh  *
1049a686cd89SMartin J. Bligh  * Each time when a new block allocation is need, first try to allocate from
1050a686cd89SMartin J. Bligh  * its own reservation.  If it does not have a reservation window, instead of
1051a686cd89SMartin J. Bligh  * looking for a free bit on bitmap first, then look up the reservation list to
1052a686cd89SMartin J. Bligh  * see if it is inside somebody else's reservation window, we try to allocate a
1053a686cd89SMartin J. Bligh  * reservation window for it starting from the goal first. Then do the block
1054a686cd89SMartin J. Bligh  * allocation within the reservation window.
1055a686cd89SMartin J. Bligh  *
1056a686cd89SMartin J. Bligh  * This will avoid keeping on searching the reservation list again and
1057a686cd89SMartin J. Bligh  * again when somebody is looking for a free block (without
1058a686cd89SMartin J. Bligh  * reservation), and there are lots of free blocks, but they are all
1059a686cd89SMartin J. Bligh  * being reserved.
1060a686cd89SMartin J. Bligh  *
1061a686cd89SMartin J. Bligh  * We use a red-black tree for the per-filesystem reservation list.
1062a686cd89SMartin J. Bligh  */
1063a686cd89SMartin J. Bligh static ext2_grpblk_t
ext2_try_to_allocate_with_rsv(struct super_block * sb,unsigned int group,struct buffer_head * bitmap_bh,ext2_grpblk_t grp_goal,struct ext2_reserve_window_node * my_rsv,unsigned long * count)1064a686cd89SMartin J. Bligh ext2_try_to_allocate_with_rsv(struct super_block *sb, unsigned int group,
1065a686cd89SMartin J. Bligh 			struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal,
1066a686cd89SMartin J. Bligh 			struct ext2_reserve_window_node * my_rsv,
1067a686cd89SMartin J. Bligh 			unsigned long *count)
1068a686cd89SMartin J. Bligh {
1069a686cd89SMartin J. Bligh 	ext2_fsblk_t group_first_block, group_last_block;
1070a686cd89SMartin J. Bligh 	ext2_grpblk_t ret = 0;
1071a686cd89SMartin J. Bligh 	unsigned long num = *count;
1072a686cd89SMartin J. Bligh 
1073a686cd89SMartin J. Bligh 	/*
1074a686cd89SMartin J. Bligh 	 * we don't deal with reservation when
1075a686cd89SMartin J. Bligh 	 * filesystem is mounted without reservation
1076a686cd89SMartin J. Bligh 	 * or the file is not a regular file
1077a686cd89SMartin J. Bligh 	 * or last attempt to allocate a block with reservation turned on failed
1078a686cd89SMartin J. Bligh 	 */
1079a686cd89SMartin J. Bligh 	if (my_rsv == NULL) {
1080a686cd89SMartin J. Bligh 		return ext2_try_to_allocate(sb, group, bitmap_bh,
1081a686cd89SMartin J. Bligh 						grp_goal, count, NULL);
1082a686cd89SMartin J. Bligh 	}
1083a686cd89SMartin J. Bligh 	/*
1084a686cd89SMartin J. Bligh 	 * grp_goal is a group relative block number (if there is a goal)
1085a686cd89SMartin J. Bligh 	 * 0 <= grp_goal < EXT2_BLOCKS_PER_GROUP(sb)
1086a686cd89SMartin J. Bligh 	 * first block is a filesystem wide block number
1087a686cd89SMartin J. Bligh 	 * first block is the block number of the first block in this group
1088a686cd89SMartin J. Bligh 	 */
1089a686cd89SMartin J. Bligh 	group_first_block = ext2_group_first_block_no(sb, group);
109090f3741cSChengguang Xu 	group_last_block = ext2_group_last_block_no(sb, group);
1091a686cd89SMartin J. Bligh 
1092a686cd89SMartin J. Bligh 	/*
1093a686cd89SMartin J. Bligh 	 * Basically we will allocate a new block from inode's reservation
1094a686cd89SMartin J. Bligh 	 * window.
1095a686cd89SMartin J. Bligh 	 *
1096a686cd89SMartin J. Bligh 	 * We need to allocate a new reservation window, if:
1097a686cd89SMartin J. Bligh 	 * a) inode does not have a reservation window; or
1098a686cd89SMartin J. Bligh 	 * b) last attempt to allocate a block from existing reservation
1099a686cd89SMartin J. Bligh 	 *    failed; or
1100a686cd89SMartin J. Bligh 	 * c) we come here with a goal and with a reservation window
1101a686cd89SMartin J. Bligh 	 *
1102a686cd89SMartin J. Bligh 	 * We do not need to allocate a new reservation window if we come here
1103a686cd89SMartin J. Bligh 	 * at the beginning with a goal and the goal is inside the window, or
1104a686cd89SMartin J. Bligh 	 * we don't have a goal but already have a reservation window.
1105a686cd89SMartin J. Bligh 	 * then we could go to allocate from the reservation window directly.
1106a686cd89SMartin J. Bligh 	 */
1107a686cd89SMartin J. Bligh 	while (1) {
1108a686cd89SMartin J. Bligh 		if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) ||
1109a686cd89SMartin J. Bligh 			!goal_in_my_reservation(&my_rsv->rsv_window,
1110a686cd89SMartin J. Bligh 						grp_goal, group, sb)) {
1111a686cd89SMartin J. Bligh 			if (my_rsv->rsv_goal_size < *count)
1112a686cd89SMartin J. Bligh 				my_rsv->rsv_goal_size = *count;
1113a686cd89SMartin J. Bligh 			ret = alloc_new_reservation(my_rsv, grp_goal, sb,
1114a686cd89SMartin J. Bligh 							group, bitmap_bh);
1115a686cd89SMartin J. Bligh 			if (ret < 0)
1116a686cd89SMartin J. Bligh 				break;			/* failed */
1117a686cd89SMartin J. Bligh 
1118a686cd89SMartin J. Bligh 			if (!goal_in_my_reservation(&my_rsv->rsv_window,
1119a686cd89SMartin J. Bligh 							grp_goal, group, sb))
1120a686cd89SMartin J. Bligh 				grp_goal = -1;
1121a686cd89SMartin J. Bligh 		} else if (grp_goal >= 0) {
1122a686cd89SMartin J. Bligh 			int curr = my_rsv->rsv_end -
1123a686cd89SMartin J. Bligh 					(grp_goal + group_first_block) + 1;
1124a686cd89SMartin J. Bligh 
1125a686cd89SMartin J. Bligh 			if (curr < *count)
1126a686cd89SMartin J. Bligh 				try_to_extend_reservation(my_rsv, sb,
1127a686cd89SMartin J. Bligh 							*count - curr);
1128a686cd89SMartin J. Bligh 		}
1129a686cd89SMartin J. Bligh 
1130a686cd89SMartin J. Bligh 		if ((my_rsv->rsv_start > group_last_block) ||
1131a686cd89SMartin J. Bligh 				(my_rsv->rsv_end < group_first_block)) {
11329bc6fc33SYe Bin 			ext2_error(sb, __func__,
11339bc6fc33SYe Bin 				   "Reservation out of group %u range goal %d fsb[%lu,%lu] rsv[%lu, %lu]",
11349bc6fc33SYe Bin 				   group, grp_goal, group_first_block,
11359bc6fc33SYe Bin 				   group_last_block, my_rsv->rsv_start,
11369bc6fc33SYe Bin 				   my_rsv->rsv_end);
1137a686cd89SMartin J. Bligh 			rsv_window_dump(&EXT2_SB(sb)->s_rsv_window_root, 1);
11389bc6fc33SYe Bin 			return -1;
1139a686cd89SMartin J. Bligh 		}
1140a686cd89SMartin J. Bligh 		ret = ext2_try_to_allocate(sb, group, bitmap_bh, grp_goal,
1141a686cd89SMartin J. Bligh 					   &num, &my_rsv->rsv_window);
1142a686cd89SMartin J. Bligh 		if (ret >= 0) {
1143a686cd89SMartin J. Bligh 			my_rsv->rsv_alloc_hit += num;
1144a686cd89SMartin J. Bligh 			*count = num;
1145a686cd89SMartin J. Bligh 			break;				/* succeed */
1146a686cd89SMartin J. Bligh 		}
1147a686cd89SMartin J. Bligh 		num = *count;
1148a686cd89SMartin J. Bligh 	}
1149a686cd89SMartin J. Bligh 	return ret;
1150a686cd89SMartin J. Bligh }
1151a686cd89SMartin J. Bligh 
1152a686cd89SMartin J. Bligh /**
1153a686cd89SMartin J. Bligh  * ext2_has_free_blocks()
1154a686cd89SMartin J. Bligh  * @sbi:		in-core super block structure.
1155a686cd89SMartin J. Bligh  *
1156a686cd89SMartin J. Bligh  * Check if filesystem has at least 1 free block available for allocation.
1157a686cd89SMartin J. Bligh  */
ext2_has_free_blocks(struct ext2_sb_info * sbi)1158a686cd89SMartin J. Bligh static int ext2_has_free_blocks(struct ext2_sb_info *sbi)
1159a686cd89SMartin J. Bligh {
1160a686cd89SMartin J. Bligh 	ext2_fsblk_t free_blocks, root_blocks;
1161a686cd89SMartin J. Bligh 
1162a686cd89SMartin J. Bligh 	free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
1163a686cd89SMartin J. Bligh 	root_blocks = le32_to_cpu(sbi->s_es->s_r_blocks_count);
1164a686cd89SMartin J. Bligh 	if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) &&
1165b8a9f9e1SEric W. Biederman 		!uid_eq(sbi->s_resuid, current_fsuid()) &&
1166b8a9f9e1SEric W. Biederman 		(gid_eq(sbi->s_resgid, GLOBAL_ROOT_GID) ||
1167b8a9f9e1SEric W. Biederman 		 !in_group_p (sbi->s_resgid))) {
1168a686cd89SMartin J. Bligh 		return 0;
1169a686cd89SMartin J. Bligh 	}
1170a686cd89SMartin J. Bligh 	return 1;
1171a686cd89SMartin J. Bligh }
1172a686cd89SMartin J. Bligh 
1173a686cd89SMartin J. Bligh /*
1174ff0031d8SCarlos Maiolino  * Returns 1 if the passed-in block region is valid; 0 if some part overlaps
11751fe03415SChengguang Xu  * with filesystem metadata blocks.
1176ff0031d8SCarlos Maiolino  */
ext2_data_block_valid(struct ext2_sb_info * sbi,ext2_fsblk_t start_blk,unsigned int count)1177ff0031d8SCarlos Maiolino int ext2_data_block_valid(struct ext2_sb_info *sbi, ext2_fsblk_t start_blk,
1178ff0031d8SCarlos Maiolino 			  unsigned int count)
1179ff0031d8SCarlos Maiolino {
1180ff0031d8SCarlos Maiolino 	if ((start_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
1181e5d39597SChengguang Xu 	    (start_blk + count - 1 < start_blk) ||
1182e5d39597SChengguang Xu 	    (start_blk + count - 1 >= le32_to_cpu(sbi->s_es->s_blocks_count)))
1183ff0031d8SCarlos Maiolino 		return 0;
1184ff0031d8SCarlos Maiolino 
1185ff0031d8SCarlos Maiolino 	/* Ensure we do not step over superblock */
1186ff0031d8SCarlos Maiolino 	if ((start_blk <= sbi->s_sb_block) &&
1187e5d39597SChengguang Xu 	    (start_blk + count - 1 >= sbi->s_sb_block))
1188ff0031d8SCarlos Maiolino 		return 0;
1189ff0031d8SCarlos Maiolino 
1190ff0031d8SCarlos Maiolino 	return 1;
1191ff0031d8SCarlos Maiolino }
1192ff0031d8SCarlos Maiolino 
1193ff0031d8SCarlos Maiolino /*
1194a686cd89SMartin J. Bligh  * ext2_new_blocks() -- core block(s) allocation function
1195a686cd89SMartin J. Bligh  * @inode:		file inode
1196a686cd89SMartin J. Bligh  * @goal:		given target block(filesystem wide)
1197a686cd89SMartin J. Bligh  * @count:		target number of blocks to allocate
1198a686cd89SMartin J. Bligh  * @errp:		error code
1199b450159dSYe Bin  * @flags:		allocate flags
1200a686cd89SMartin J. Bligh  *
1201a686cd89SMartin J. Bligh  * ext2_new_blocks uses a goal block to assist allocation.  If the goal is
12021da177e4SLinus Torvalds  * free, or there is a free block within 32 blocks of the goal, that block
12031da177e4SLinus Torvalds  * is allocated.  Otherwise a forward search is made for a free block; within
12041da177e4SLinus Torvalds  * each block group the search first looks for an entire free byte in the block
12051da177e4SLinus Torvalds  * bitmap, and then for any free bit if that fails.
12061da177e4SLinus Torvalds  * This function also updates quota and i_blocks field.
12071da177e4SLinus Torvalds  */
ext2_new_blocks(struct inode * inode,ext2_fsblk_t goal,unsigned long * count,int * errp,unsigned int flags)1208a686cd89SMartin J. Bligh ext2_fsblk_t ext2_new_blocks(struct inode *inode, ext2_fsblk_t goal,
1209b450159dSYe Bin 		    unsigned long *count, int *errp, unsigned int flags)
12101da177e4SLinus Torvalds {
12111da177e4SLinus Torvalds 	struct buffer_head *bitmap_bh = NULL;
1212a686cd89SMartin J. Bligh 	struct buffer_head *gdp_bh;
1213a686cd89SMartin J. Bligh 	int group_no;
1214a686cd89SMartin J. Bligh 	int goal_group;
1215a686cd89SMartin J. Bligh 	ext2_grpblk_t grp_target_blk;	/* blockgroup relative goal block */
1216a686cd89SMartin J. Bligh 	ext2_grpblk_t grp_alloc_blk;	/* blockgroup-relative allocated block*/
1217a686cd89SMartin J. Bligh 	ext2_fsblk_t ret_block;		/* filesyetem-wide allocated block */
1218a686cd89SMartin J. Bligh 	int bgi;			/* blockgroup iteration index */
1219a686cd89SMartin J. Bligh 	int performed_allocation = 0;
1220a686cd89SMartin J. Bligh 	ext2_grpblk_t free_blocks;	/* number of free blocks in a group */
1221a686cd89SMartin J. Bligh 	struct super_block *sb;
1222a686cd89SMartin J. Bligh 	struct ext2_group_desc *gdp;
1223a686cd89SMartin J. Bligh 	struct ext2_super_block *es;
1224a686cd89SMartin J. Bligh 	struct ext2_sb_info *sbi;
1225a686cd89SMartin J. Bligh 	struct ext2_reserve_window_node *my_rsv = NULL;
1226a686cd89SMartin J. Bligh 	struct ext2_block_alloc_info *block_i;
1227a686cd89SMartin J. Bligh 	unsigned short windowsz = 0;
1228a686cd89SMartin J. Bligh 	unsigned long ngroups;
1229a686cd89SMartin J. Bligh 	unsigned long num = *count;
12305dd4056dSChristoph Hellwig 	int ret;
12311da177e4SLinus Torvalds 
1232a686cd89SMartin J. Bligh 	*errp = -ENOSPC;
1233a686cd89SMartin J. Bligh 	sb = inode->i_sb;
12341da177e4SLinus Torvalds 
1235a686cd89SMartin J. Bligh 	/*
1236a686cd89SMartin J. Bligh 	 * Check quota for allocation of this block.
1237a686cd89SMartin J. Bligh 	 */
12385dd4056dSChristoph Hellwig 	ret = dquot_alloc_block(inode, num);
12395dd4056dSChristoph Hellwig 	if (ret) {
12405dd4056dSChristoph Hellwig 		*errp = ret;
1241a686cd89SMartin J. Bligh 		return 0;
1242a686cd89SMartin J. Bligh 	}
1243a686cd89SMartin J. Bligh 
1244a686cd89SMartin J. Bligh 	sbi = EXT2_SB(sb);
1245a686cd89SMartin J. Bligh 	es = EXT2_SB(sb)->s_es;
1246a686cd89SMartin J. Bligh 	ext2_debug("goal=%lu.\n", goal);
1247a686cd89SMartin J. Bligh 	/*
124883f99de1SYe Bin 	 * Allocate a block from reservation only when the filesystem is
124983f99de1SYe Bin 	 * mounted with reservation(default,-o reservation), and it's a regular
125083f99de1SYe Bin 	 * file, and the desired window size is greater than 0 (One could use
125183f99de1SYe Bin 	 * ioctl command EXT2_IOC_SETRSVSZ to set the window size to 0 to turn
125283f99de1SYe Bin 	 * off reservation on that particular file). Also do not use the
125383f99de1SYe Bin 	 * reservation window if the caller asked us not to do it.
1254a686cd89SMartin J. Bligh 	 */
1255a686cd89SMartin J. Bligh 	block_i = EXT2_I(inode)->i_block_alloc_info;
125683f99de1SYe Bin 	if (!(flags & EXT2_ALLOC_NORESERVE) && block_i) {
1257a686cd89SMartin J. Bligh 		windowsz = block_i->rsv_window_node.rsv_goal_size;
1258a686cd89SMartin J. Bligh 		if (windowsz > 0)
1259a686cd89SMartin J. Bligh 			my_rsv = &block_i->rsv_window_node;
1260a686cd89SMartin J. Bligh 	}
1261a686cd89SMartin J. Bligh 
1262a686cd89SMartin J. Bligh 	if (!ext2_has_free_blocks(sbi)) {
1263a686cd89SMartin J. Bligh 		*errp = -ENOSPC;
12641da177e4SLinus Torvalds 		goto out;
12651da177e4SLinus Torvalds 	}
12661da177e4SLinus Torvalds 
1267a686cd89SMartin J. Bligh 	/*
1268a686cd89SMartin J. Bligh 	 * First, test whether the goal block is free.
1269a686cd89SMartin J. Bligh 	 */
12701da177e4SLinus Torvalds 	if (goal < le32_to_cpu(es->s_first_data_block) ||
12711da177e4SLinus Torvalds 	    goal >= le32_to_cpu(es->s_blocks_count))
12721da177e4SLinus Torvalds 		goal = le32_to_cpu(es->s_first_data_block);
1273a686cd89SMartin J. Bligh 	group_no = (goal - le32_to_cpu(es->s_first_data_block)) /
1274a686cd89SMartin J. Bligh 			EXT2_BLOCKS_PER_GROUP(sb);
1275a686cd89SMartin J. Bligh 	goal_group = group_no;
1276a686cd89SMartin J. Bligh retry_alloc:
1277a686cd89SMartin J. Bligh 	gdp = ext2_get_group_desc(sb, group_no, &gdp_bh);
1278a686cd89SMartin J. Bligh 	if (!gdp)
12791da177e4SLinus Torvalds 		goto io_error;
12801da177e4SLinus Torvalds 
1281a686cd89SMartin J. Bligh 	free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
1282a686cd89SMartin J. Bligh 	/*
1283a686cd89SMartin J. Bligh 	 * if there is not enough free blocks to make a new resevation
1284a686cd89SMartin J. Bligh 	 * turn off reservation for this allocation
1285a686cd89SMartin J. Bligh 	 */
1286a686cd89SMartin J. Bligh 	if (my_rsv && (free_blocks < windowsz)
1287d707d31cSMingming Cao 		&& (free_blocks > 0)
1288a686cd89SMartin J. Bligh 		&& (rsv_is_empty(&my_rsv->rsv_window)))
1289a686cd89SMartin J. Bligh 		my_rsv = NULL;
1290a686cd89SMartin J. Bligh 
1291a686cd89SMartin J. Bligh 	if (free_blocks > 0) {
1292a686cd89SMartin J. Bligh 		grp_target_blk = ((goal - le32_to_cpu(es->s_first_data_block)) %
1293a686cd89SMartin J. Bligh 				EXT2_BLOCKS_PER_GROUP(sb));
1294ba1af2e4SChengguang Xu 		/*
1295ba1af2e4SChengguang Xu 		 * In case we retry allocation (due to fs reservation not
1296ba1af2e4SChengguang Xu 		 * working out or fs corruption), the bitmap_bh is non-null
1297ba1af2e4SChengguang Xu 		 * pointer and we have to release it before calling
1298ba1af2e4SChengguang Xu 		 * read_block_bitmap().
1299ba1af2e4SChengguang Xu 		 */
1300ba1af2e4SChengguang Xu 		brelse(bitmap_bh);
13011da177e4SLinus Torvalds 		bitmap_bh = read_block_bitmap(sb, group_no);
13021da177e4SLinus Torvalds 		if (!bitmap_bh)
13031da177e4SLinus Torvalds 			goto io_error;
1304a686cd89SMartin J. Bligh 		grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no,
1305a686cd89SMartin J. Bligh 					bitmap_bh, grp_target_blk,
1306a686cd89SMartin J. Bligh 					my_rsv, &num);
1307a686cd89SMartin J. Bligh 		if (grp_alloc_blk >= 0)
1308a686cd89SMartin J. Bligh 			goto allocated;
13091da177e4SLinus Torvalds 	}
13101da177e4SLinus Torvalds 
1311a686cd89SMartin J. Bligh 	ngroups = EXT2_SB(sb)->s_groups_count;
1312a686cd89SMartin J. Bligh 	smp_rmb();
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	/*
13151da177e4SLinus Torvalds 	 * Now search the rest of the groups.  We assume that
1316144704e5SAkinobu Mita 	 * group_no and gdp correctly point to the last group visited.
13171da177e4SLinus Torvalds 	 */
1318a686cd89SMartin J. Bligh 	for (bgi = 0; bgi < ngroups; bgi++) {
13191da177e4SLinus Torvalds 		group_no++;
1320a686cd89SMartin J. Bligh 		if (group_no >= ngroups)
13211da177e4SLinus Torvalds 			group_no = 0;
1322a686cd89SMartin J. Bligh 		gdp = ext2_get_group_desc(sb, group_no, &gdp_bh);
1323a686cd89SMartin J. Bligh 		if (!gdp)
13241da177e4SLinus Torvalds 			goto io_error;
1325a686cd89SMartin J. Bligh 
1326a686cd89SMartin J. Bligh 		free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
1327a686cd89SMartin J. Bligh 		/*
132846891532SJan Kara 		 * skip this group (and avoid loading bitmap) if there
132946891532SJan Kara 		 * are no free blocks
133046891532SJan Kara 		 */
133146891532SJan Kara 		if (!free_blocks)
133246891532SJan Kara 			continue;
133346891532SJan Kara 		/*
1334a686cd89SMartin J. Bligh 		 * skip this group if the number of
1335a686cd89SMartin J. Bligh 		 * free blocks is less than half of the reservation
1336a686cd89SMartin J. Bligh 		 * window size.
1337a686cd89SMartin J. Bligh 		 */
1338d707d31cSMingming Cao 		if (my_rsv && (free_blocks <= (windowsz/2)))
1339a686cd89SMartin J. Bligh 			continue;
1340a686cd89SMartin J. Bligh 
13411da177e4SLinus Torvalds 		brelse(bitmap_bh);
13421da177e4SLinus Torvalds 		bitmap_bh = read_block_bitmap(sb, group_no);
13431da177e4SLinus Torvalds 		if (!bitmap_bh)
13441da177e4SLinus Torvalds 			goto io_error;
13451da177e4SLinus Torvalds 		/*
1346a686cd89SMartin J. Bligh 		 * try to allocate block(s) from this group, without a goal(-1).
13471da177e4SLinus Torvalds 		 */
1348a686cd89SMartin J. Bligh 		grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no,
1349a686cd89SMartin J. Bligh 					bitmap_bh, -1, my_rsv, &num);
1350a686cd89SMartin J. Bligh 		if (grp_alloc_blk >= 0)
1351a686cd89SMartin J. Bligh 			goto allocated;
13521da177e4SLinus Torvalds 	}
13531da177e4SLinus Torvalds 	/*
135425985edcSLucas De Marchi 	 * We may end up a bogus earlier ENOSPC error due to
1355a686cd89SMartin J. Bligh 	 * filesystem is "full" of reservations, but
135625985edcSLucas De Marchi 	 * there maybe indeed free blocks available on disk
1357a686cd89SMartin J. Bligh 	 * In this case, we just forget about the reservations
1358a686cd89SMartin J. Bligh 	 * just do block allocation as without reservations.
13591da177e4SLinus Torvalds 	 */
1360a686cd89SMartin J. Bligh 	if (my_rsv) {
1361a686cd89SMartin J. Bligh 		my_rsv = NULL;
1362a686cd89SMartin J. Bligh 		windowsz = 0;
1363a686cd89SMartin J. Bligh 		group_no = goal_group;
1364a686cd89SMartin J. Bligh 		goto retry_alloc;
13651da177e4SLinus Torvalds 	}
1366a686cd89SMartin J. Bligh 	/* No space left on the device */
1367a686cd89SMartin J. Bligh 	*errp = -ENOSPC;
1368a686cd89SMartin J. Bligh 	goto out;
13691da177e4SLinus Torvalds 
1370a686cd89SMartin J. Bligh allocated:
1371a686cd89SMartin J. Bligh 
13721da177e4SLinus Torvalds 	ext2_debug("using block group %d(%d)\n",
1373a686cd89SMartin J. Bligh 			group_no, gdp->bg_free_blocks_count);
13741da177e4SLinus Torvalds 
1375a686cd89SMartin J. Bligh 	ret_block = grp_alloc_blk + ext2_group_first_block_no(sb, group_no);
13761da177e4SLinus Torvalds 
1377a686cd89SMartin J. Bligh 	if (in_range(le32_to_cpu(gdp->bg_block_bitmap), ret_block, num) ||
1378a686cd89SMartin J. Bligh 	    in_range(le32_to_cpu(gdp->bg_inode_bitmap), ret_block, num) ||
1379a686cd89SMartin J. Bligh 	    in_range(ret_block, le32_to_cpu(gdp->bg_inode_table),
1380a686cd89SMartin J. Bligh 		      EXT2_SB(sb)->s_itb_per_group) ||
1381a686cd89SMartin J. Bligh 	    in_range(ret_block + num - 1, le32_to_cpu(gdp->bg_inode_table),
13827f0adaecSAneesh Kumar K.V 		      EXT2_SB(sb)->s_itb_per_group)) {
1383a686cd89SMartin J. Bligh 		ext2_error(sb, "ext2_new_blocks",
13841da177e4SLinus Torvalds 			    "Allocating block in system zone - "
1385a686cd89SMartin J. Bligh 			    "blocks from "E2FSBLK", length %lu",
1386a686cd89SMartin J. Bligh 			    ret_block, num);
13878b915825SAneesh Kumar K.V 		/*
13888b915825SAneesh Kumar K.V 		 * ext2_try_to_allocate marked the blocks we allocated as in
13898b915825SAneesh Kumar K.V 		 * use.  So we may want to selectively mark some of the blocks
13908b915825SAneesh Kumar K.V 		 * as free
13918b915825SAneesh Kumar K.V 		 */
1392158be76cSChengguang Xu 		num = *count;
13938b915825SAneesh Kumar K.V 		goto retry_alloc;
13947f0adaecSAneesh Kumar K.V 	}
13951da177e4SLinus Torvalds 
1396a686cd89SMartin J. Bligh 	performed_allocation = 1;
1397a686cd89SMartin J. Bligh 
1398a686cd89SMartin J. Bligh 	if (ret_block + num - 1 >= le32_to_cpu(es->s_blocks_count)) {
1399a686cd89SMartin J. Bligh 		ext2_error(sb, "ext2_new_blocks",
1400a686cd89SMartin J. Bligh 			    "block("E2FSBLK") >= blocks count(%d) - "
14011da177e4SLinus Torvalds 			    "block_group = %d, es == %p ", ret_block,
14021da177e4SLinus Torvalds 			le32_to_cpu(es->s_blocks_count), group_no, es);
1403a686cd89SMartin J. Bligh 		goto out;
14041da177e4SLinus Torvalds 	}
14051da177e4SLinus Torvalds 
1406a686cd89SMartin J. Bligh 	group_adjust_blocks(sb, group_no, gdp, gdp_bh, -num);
1407a686cd89SMartin J. Bligh 	percpu_counter_sub(&sbi->s_freeblocks_counter, num);
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 	mark_buffer_dirty(bitmap_bh);
14101751e8a6SLinus Torvalds 	if (sb->s_flags & SB_SYNCHRONOUS)
14111da177e4SLinus Torvalds 		sync_dirty_buffer(bitmap_bh);
14121da177e4SLinus Torvalds 
1413a686cd89SMartin J. Bligh 	*errp = 0;
14141da177e4SLinus Torvalds 	brelse(bitmap_bh);
14158e3dffc6SWang Shilong 	if (num < *count) {
14163889717dSAl Viro 		dquot_free_block_nodirty(inode, *count-num);
14173889717dSAl Viro 		mark_inode_dirty(inode);
1418a686cd89SMartin J. Bligh 		*count = num;
14198e3dffc6SWang Shilong 	}
1420a686cd89SMartin J. Bligh 	return ret_block;
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds io_error:
1423a686cd89SMartin J. Bligh 	*errp = -EIO;
1424a686cd89SMartin J. Bligh out:
1425a686cd89SMartin J. Bligh 	/*
1426a686cd89SMartin J. Bligh 	 * Undo the block allocation
1427a686cd89SMartin J. Bligh 	 */
14283889717dSAl Viro 	if (!performed_allocation) {
14293889717dSAl Viro 		dquot_free_block_nodirty(inode, *count);
14303889717dSAl Viro 		mark_inode_dirty(inode);
14313889717dSAl Viro 	}
1432a686cd89SMartin J. Bligh 	brelse(bitmap_bh);
1433a686cd89SMartin J. Bligh 	return 0;
1434a686cd89SMartin J. Bligh }
1435a686cd89SMartin J. Bligh 
143621730eedSValerie Henson #ifdef EXT2FS_DEBUG
143721730eedSValerie Henson 
ext2_count_free(struct buffer_head * map,unsigned int numchars)143821730eedSValerie Henson unsigned long ext2_count_free(struct buffer_head *map, unsigned int numchars)
143921730eedSValerie Henson {
1440ecd0afa3SAkinobu Mita 	return numchars * BITS_PER_BYTE - memweight(map->b_data, numchars);
144121730eedSValerie Henson }
144221730eedSValerie Henson 
144321730eedSValerie Henson #endif  /*  EXT2FS_DEBUG  */
144421730eedSValerie Henson 
ext2_count_free_blocks(struct super_block * sb)14451da177e4SLinus Torvalds unsigned long ext2_count_free_blocks (struct super_block * sb)
14461da177e4SLinus Torvalds {
14471da177e4SLinus Torvalds 	struct ext2_group_desc * desc;
14481da177e4SLinus Torvalds 	unsigned long desc_count = 0;
14491da177e4SLinus Torvalds 	int i;
14501da177e4SLinus Torvalds #ifdef EXT2FS_DEBUG
14511da177e4SLinus Torvalds 	unsigned long bitmap_count, x;
14521da177e4SLinus Torvalds 	struct ext2_super_block *es;
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	es = EXT2_SB(sb)->s_es;
14551da177e4SLinus Torvalds 	desc_count = 0;
14561da177e4SLinus Torvalds 	bitmap_count = 0;
14571da177e4SLinus Torvalds 	desc = NULL;
14581da177e4SLinus Torvalds 	for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
14591da177e4SLinus Torvalds 		struct buffer_head *bitmap_bh;
14601da177e4SLinus Torvalds 		desc = ext2_get_group_desc (sb, i, NULL);
14611da177e4SLinus Torvalds 		if (!desc)
14621da177e4SLinus Torvalds 			continue;
14631da177e4SLinus Torvalds 		desc_count += le16_to_cpu(desc->bg_free_blocks_count);
14641da177e4SLinus Torvalds 		bitmap_bh = read_block_bitmap(sb, i);
14651da177e4SLinus Torvalds 		if (!bitmap_bh)
14661da177e4SLinus Torvalds 			continue;
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 		x = ext2_count_free(bitmap_bh, sb->s_blocksize);
14691da177e4SLinus Torvalds 		printk ("group %d: stored = %d, counted = %lu\n",
14701da177e4SLinus Torvalds 			i, le16_to_cpu(desc->bg_free_blocks_count), x);
14711da177e4SLinus Torvalds 		bitmap_count += x;
14721da177e4SLinus Torvalds 		brelse(bitmap_bh);
14731da177e4SLinus Torvalds 	}
14741da177e4SLinus Torvalds 	printk("ext2_count_free_blocks: stored = %lu, computed = %lu, %lu\n",
14751da177e4SLinus Torvalds 		(long)le32_to_cpu(es->s_free_blocks_count),
14761da177e4SLinus Torvalds 		desc_count, bitmap_count);
14771da177e4SLinus Torvalds 	return bitmap_count;
14781da177e4SLinus Torvalds #else
14791da177e4SLinus Torvalds 	for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
14801da177e4SLinus Torvalds 		desc = ext2_get_group_desc(sb, i, NULL);
14811da177e4SLinus Torvalds 		if (!desc)
14821da177e4SLinus Torvalds 			continue;
14831da177e4SLinus Torvalds 		desc_count += le16_to_cpu(desc->bg_free_blocks_count);
14841da177e4SLinus Torvalds 	}
14851da177e4SLinus Torvalds 	return desc_count;
14861da177e4SLinus Torvalds #endif
14871da177e4SLinus Torvalds }
14881da177e4SLinus Torvalds 
test_root(int a,int b)14891da177e4SLinus Torvalds static inline int test_root(int a, int b)
14901da177e4SLinus Torvalds {
14911da177e4SLinus Torvalds 	int num = b;
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds 	while (a > num)
14941da177e4SLinus Torvalds 		num *= b;
14951da177e4SLinus Torvalds 	return num == a;
14961da177e4SLinus Torvalds }
14971da177e4SLinus Torvalds 
ext2_group_sparse(int group)14981da177e4SLinus Torvalds static int ext2_group_sparse(int group)
14991da177e4SLinus Torvalds {
15001da177e4SLinus Torvalds 	if (group <= 1)
15011da177e4SLinus Torvalds 		return 1;
15021da177e4SLinus Torvalds 	return (test_root(group, 3) || test_root(group, 5) ||
15031da177e4SLinus Torvalds 		test_root(group, 7));
15041da177e4SLinus Torvalds }
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds /**
15071da177e4SLinus Torvalds  *	ext2_bg_has_super - number of blocks used by the superblock in group
15081da177e4SLinus Torvalds  *	@sb: superblock for filesystem
15091da177e4SLinus Torvalds  *	@group: group number to check
15101da177e4SLinus Torvalds  *
15111da177e4SLinus Torvalds  *	Return the number of blocks used by the superblock (primary or backup)
15121da177e4SLinus Torvalds  *	in this group.  Currently this will be only 0 or 1.
15131da177e4SLinus Torvalds  */
ext2_bg_has_super(struct super_block * sb,int group)15141da177e4SLinus Torvalds int ext2_bg_has_super(struct super_block *sb, int group)
15151da177e4SLinus Torvalds {
15161da177e4SLinus Torvalds 	if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
15171da177e4SLinus Torvalds 	    !ext2_group_sparse(group))
15181da177e4SLinus Torvalds 		return 0;
15191da177e4SLinus Torvalds 	return 1;
15201da177e4SLinus Torvalds }
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds /**
15231da177e4SLinus Torvalds  *	ext2_bg_num_gdb - number of blocks used by the group table in group
15241da177e4SLinus Torvalds  *	@sb: superblock for filesystem
15251da177e4SLinus Torvalds  *	@group: group number to check
15261da177e4SLinus Torvalds  *
15271da177e4SLinus Torvalds  *	Return the number of blocks used by the group descriptor table
15281da177e4SLinus Torvalds  *	(primary or backup) in this group.  In the future there may be a
15291da177e4SLinus Torvalds  *	different number of descriptor blocks in each group.
15301da177e4SLinus Torvalds  */
ext2_bg_num_gdb(struct super_block * sb,int group)15311da177e4SLinus Torvalds unsigned long ext2_bg_num_gdb(struct super_block *sb, int group)
15321da177e4SLinus Torvalds {
1533859cb936SAkinobu Mita 	return ext2_bg_has_super(sb, group) ? EXT2_SB(sb)->s_gdb_count : 0;
15341da177e4SLinus Torvalds }
15351da177e4SLinus Torvalds 
1536