xref: /openbmc/linux/fs/ext2/balloc.c (revision f9bff0e3)
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;
8001584fa6SAneesh Kumar K.V 
8101584fa6SAneesh Kumar K.V 	group_first_block = ext2_group_first_block_no(sb, block_group);
8201584fa6SAneesh Kumar K.V 
8301584fa6SAneesh Kumar K.V 	/* check whether block bitmap block number is set */
8401584fa6SAneesh Kumar K.V 	bitmap_blk = le32_to_cpu(desc->bg_block_bitmap);
8501584fa6SAneesh Kumar K.V 	offset = bitmap_blk - group_first_block;
8601584fa6SAneesh Kumar K.V 	if (!ext2_test_bit(offset, bh->b_data))
8701584fa6SAneesh Kumar K.V 		/* bad block bitmap */
8801584fa6SAneesh Kumar K.V 		goto err_out;
8901584fa6SAneesh Kumar K.V 
9001584fa6SAneesh Kumar K.V 	/* check whether the inode bitmap block number is set */
9101584fa6SAneesh Kumar K.V 	bitmap_blk = le32_to_cpu(desc->bg_inode_bitmap);
9201584fa6SAneesh Kumar K.V 	offset = bitmap_blk - group_first_block;
9301584fa6SAneesh Kumar K.V 	if (!ext2_test_bit(offset, bh->b_data))
9401584fa6SAneesh Kumar K.V 		/* bad block bitmap */
9501584fa6SAneesh Kumar K.V 		goto err_out;
9601584fa6SAneesh Kumar K.V 
9701584fa6SAneesh Kumar K.V 	/* check whether the inode table block number is set */
9801584fa6SAneesh Kumar K.V 	bitmap_blk = le32_to_cpu(desc->bg_inode_table);
9901584fa6SAneesh Kumar K.V 	offset = bitmap_blk - group_first_block;
10001584fa6SAneesh Kumar K.V 	next_zero_bit = ext2_find_next_zero_bit(bh->b_data,
10101584fa6SAneesh Kumar K.V 				offset + EXT2_SB(sb)->s_itb_per_group,
10201584fa6SAneesh Kumar K.V 				offset);
10301584fa6SAneesh Kumar K.V 	if (next_zero_bit >= offset + EXT2_SB(sb)->s_itb_per_group)
10401584fa6SAneesh Kumar K.V 		/* good bitmap for inode tables */
10501584fa6SAneesh Kumar K.V 		return 1;
10601584fa6SAneesh Kumar K.V 
10701584fa6SAneesh Kumar K.V err_out:
108605afd60SHarvey Harrison 	ext2_error(sb, __func__,
10901584fa6SAneesh Kumar K.V 			"Invalid block bitmap - "
11001584fa6SAneesh Kumar K.V 			"block_group = %d, block = %lu",
11101584fa6SAneesh Kumar K.V 			block_group, bitmap_blk);
11201584fa6SAneesh Kumar K.V 	return 0;
11301584fa6SAneesh Kumar K.V }
11401584fa6SAneesh Kumar K.V 
1151da177e4SLinus Torvalds /*
11601584fa6SAneesh Kumar K.V  * Read the bitmap for a given block_group,and validate the
11701584fa6SAneesh Kumar K.V  * bits for block/inode/inode tables are set in the bitmaps
1181da177e4SLinus Torvalds  *
1191da177e4SLinus Torvalds  * Return buffer_head on success or NULL in case of failure.
1201da177e4SLinus Torvalds  */
1211da177e4SLinus Torvalds static struct buffer_head *
read_block_bitmap(struct super_block * sb,unsigned int block_group)1221da177e4SLinus Torvalds read_block_bitmap(struct super_block *sb, unsigned int block_group)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds 	struct ext2_group_desc * desc;
1251da177e4SLinus Torvalds 	struct buffer_head * bh = NULL;
12601584fa6SAneesh Kumar K.V 	ext2_fsblk_t bitmap_blk;
127*28cf7559SZhang Yi 	int ret;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	desc = ext2_get_group_desc(sb, block_group, NULL);
1301da177e4SLinus Torvalds 	if (!desc)
13101584fa6SAneesh Kumar K.V 		return NULL;
13201584fa6SAneesh Kumar K.V 	bitmap_blk = le32_to_cpu(desc->bg_block_bitmap);
13301584fa6SAneesh Kumar K.V 	bh = sb_getblk(sb, bitmap_blk);
13401584fa6SAneesh Kumar K.V 	if (unlikely(!bh)) {
135605afd60SHarvey Harrison 		ext2_error(sb, __func__,
1361da177e4SLinus Torvalds 			    "Cannot read block bitmap - "
1371da177e4SLinus Torvalds 			    "block_group = %d, block_bitmap = %u",
1381da177e4SLinus Torvalds 			    block_group, le32_to_cpu(desc->bg_block_bitmap));
13901584fa6SAneesh Kumar K.V 		return NULL;
14001584fa6SAneesh Kumar K.V 	}
141*28cf7559SZhang Yi 	ret = bh_read(bh, 0);
142*28cf7559SZhang Yi 	if (ret > 0)
14301584fa6SAneesh Kumar K.V 		return bh;
144*28cf7559SZhang Yi 	if (ret < 0) {
14501584fa6SAneesh Kumar K.V 		brelse(bh);
146605afd60SHarvey Harrison 		ext2_error(sb, __func__,
14701584fa6SAneesh Kumar K.V 			    "Cannot read block bitmap - "
14801584fa6SAneesh Kumar K.V 			    "block_group = %d, block_bitmap = %u",
14901584fa6SAneesh Kumar K.V 			    block_group, le32_to_cpu(desc->bg_block_bitmap));
15001584fa6SAneesh Kumar K.V 		return NULL;
15101584fa6SAneesh Kumar K.V 	}
15201584fa6SAneesh Kumar K.V 
1538b915825SAneesh Kumar K.V 	ext2_valid_block_bitmap(sb, desc, block_group, bh);
1548b915825SAneesh Kumar K.V 	/*
1558b915825SAneesh Kumar K.V 	 * file system mounted not to panic on error, continue with corrupt
1568b915825SAneesh Kumar K.V 	 * bitmap
1578b915825SAneesh Kumar K.V 	 */
1580b832a4bSLinus Torvalds 	return bh;
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
group_adjust_blocks(struct super_block * sb,int group_no,struct ext2_group_desc * desc,struct buffer_head * bh,int count)161a686cd89SMartin J. Bligh static void group_adjust_blocks(struct super_block *sb, int group_no,
1621da177e4SLinus Torvalds 	struct ext2_group_desc *desc, struct buffer_head *bh, int count)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds 	if (count) {
1651da177e4SLinus Torvalds 		struct ext2_sb_info *sbi = EXT2_SB(sb);
1661da177e4SLinus Torvalds 		unsigned free_blocks;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 		spin_lock(sb_bgl_lock(sbi, group_no));
1691da177e4SLinus Torvalds 		free_blocks = le16_to_cpu(desc->bg_free_blocks_count);
1701da177e4SLinus Torvalds 		desc->bg_free_blocks_count = cpu_to_le16(free_blocks + count);
1711da177e4SLinus Torvalds 		spin_unlock(sb_bgl_lock(sbi, group_no));
1721da177e4SLinus Torvalds 		mark_buffer_dirty(bh);
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds 
176a686cd89SMartin J. Bligh /*
177a686cd89SMartin J. Bligh  * The reservation window structure operations
178a686cd89SMartin J. Bligh  * --------------------------------------------
179a686cd89SMartin J. Bligh  * Operations include:
180a686cd89SMartin J. Bligh  * dump, find, add, remove, is_empty, find_next_reservable_window, etc.
181a686cd89SMartin J. Bligh  *
182a686cd89SMartin J. Bligh  * We use a red-black tree to represent per-filesystem reservation
183a686cd89SMartin J. Bligh  * windows.
184a686cd89SMartin J. Bligh  *
185a686cd89SMartin J. Bligh  */
186a686cd89SMartin J. Bligh 
187a686cd89SMartin J. Bligh /**
188a686cd89SMartin J. Bligh  * __rsv_window_dump() -- Dump the filesystem block allocation reservation map
189c53ec7bcSWang Hai  * @root:		root of per-filesystem reservation rb tree
190a686cd89SMartin J. Bligh  * @verbose:		verbose mode
191a686cd89SMartin J. Bligh  * @fn:			function which wishes to dump the reservation map
192a686cd89SMartin J. Bligh  *
193a686cd89SMartin J. Bligh  * If verbose is turned on, it will print the whole block reservation
194a686cd89SMartin J. Bligh  * windows(start, end). Otherwise, it will only print out the "bad" windows,
195a686cd89SMartin J. Bligh  * those windows that overlap with their immediate neighbors.
196a686cd89SMartin J. Bligh  */
197a686cd89SMartin J. Bligh #if 1
__rsv_window_dump(struct rb_root * root,int verbose,const char * fn)198a686cd89SMartin J. Bligh static void __rsv_window_dump(struct rb_root *root, int verbose,
199a686cd89SMartin J. Bligh 			      const char *fn)
200a686cd89SMartin J. Bligh {
201a686cd89SMartin J. Bligh 	struct rb_node *n;
202a686cd89SMartin J. Bligh 	struct ext2_reserve_window_node *rsv, *prev;
203a686cd89SMartin J. Bligh 	int bad;
204a686cd89SMartin J. Bligh 
205a686cd89SMartin J. Bligh restart:
206a686cd89SMartin J. Bligh 	n = rb_first(root);
207a686cd89SMartin J. Bligh 	bad = 0;
208a686cd89SMartin J. Bligh 	prev = NULL;
209a686cd89SMartin J. Bligh 
210a686cd89SMartin J. Bligh 	printk("Block Allocation Reservation Windows Map (%s):\n", fn);
211a686cd89SMartin J. Bligh 	while (n) {
212a686cd89SMartin J. Bligh 		rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
213a686cd89SMartin J. Bligh 		if (verbose)
214a686cd89SMartin J. Bligh 			printk("reservation window 0x%p "
215a686cd89SMartin J. Bligh 				"start: %lu, end: %lu\n",
216a686cd89SMartin J. Bligh 				rsv, rsv->rsv_start, rsv->rsv_end);
217a686cd89SMartin J. Bligh 		if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) {
218a686cd89SMartin J. Bligh 			printk("Bad reservation %p (start >= end)\n",
219a686cd89SMartin J. Bligh 			       rsv);
220a686cd89SMartin J. Bligh 			bad = 1;
221a686cd89SMartin J. Bligh 		}
222a686cd89SMartin J. Bligh 		if (prev && prev->rsv_end >= rsv->rsv_start) {
223a686cd89SMartin J. Bligh 			printk("Bad reservation %p (prev->end >= start)\n",
224a686cd89SMartin J. Bligh 			       rsv);
225a686cd89SMartin J. Bligh 			bad = 1;
226a686cd89SMartin J. Bligh 		}
227a686cd89SMartin J. Bligh 		if (bad) {
228a686cd89SMartin J. Bligh 			if (!verbose) {
229a686cd89SMartin J. Bligh 				printk("Restarting reservation walk in verbose mode\n");
230a686cd89SMartin J. Bligh 				verbose = 1;
231a686cd89SMartin J. Bligh 				goto restart;
232a686cd89SMartin J. Bligh 			}
233a686cd89SMartin J. Bligh 		}
234a686cd89SMartin J. Bligh 		n = rb_next(n);
235a686cd89SMartin J. Bligh 		prev = rsv;
236a686cd89SMartin J. Bligh 	}
237a686cd89SMartin J. Bligh 	printk("Window map complete.\n");
2382c11619aSJulia Lawall 	BUG_ON(bad);
239a686cd89SMartin J. Bligh }
240a686cd89SMartin J. Bligh #define rsv_window_dump(root, verbose) \
241605afd60SHarvey Harrison 	__rsv_window_dump((root), (verbose), __func__)
242a686cd89SMartin J. Bligh #else
243a686cd89SMartin J. Bligh #define rsv_window_dump(root, verbose) do {} while (0)
244a686cd89SMartin J. Bligh #endif
245a686cd89SMartin J. Bligh 
246a686cd89SMartin J. Bligh /**
247a686cd89SMartin J. Bligh  * goal_in_my_reservation()
248a686cd89SMartin J. Bligh  * @rsv:		inode's reservation window
249a686cd89SMartin J. Bligh  * @grp_goal:		given goal block relative to the allocation block group
250a686cd89SMartin J. Bligh  * @group:		the current allocation block group
251a686cd89SMartin J. Bligh  * @sb:			filesystem super block
252a686cd89SMartin J. Bligh  *
253a686cd89SMartin J. Bligh  * Test if the given goal block (group relative) is within the file's
254a686cd89SMartin J. Bligh  * own block reservation window range.
255a686cd89SMartin J. Bligh  *
256a686cd89SMartin J. Bligh  * If the reservation window is outside the goal allocation group, return 0;
257a686cd89SMartin J. Bligh  * grp_goal (given goal block) could be -1, which means no specific
258a686cd89SMartin J. Bligh  * goal block. In this case, always return 1.
259a686cd89SMartin J. Bligh  * If the goal block is within the reservation window, return 1;
260a686cd89SMartin J. Bligh  * otherwise, return 0;
261a686cd89SMartin J. Bligh  */
262a686cd89SMartin 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)263a686cd89SMartin J. Bligh goal_in_my_reservation(struct ext2_reserve_window *rsv, ext2_grpblk_t grp_goal,
264a686cd89SMartin J. Bligh 			unsigned int group, struct super_block * sb)
265a686cd89SMartin J. Bligh {
266a686cd89SMartin J. Bligh 	ext2_fsblk_t group_first_block, group_last_block;
267a686cd89SMartin J. Bligh 
268a686cd89SMartin J. Bligh 	group_first_block = ext2_group_first_block_no(sb, group);
26990f3741cSChengguang Xu 	group_last_block = ext2_group_last_block_no(sb, group);
270a686cd89SMartin J. Bligh 
271a686cd89SMartin J. Bligh 	if ((rsv->_rsv_start > group_last_block) ||
272a686cd89SMartin J. Bligh 	    (rsv->_rsv_end < group_first_block))
273a686cd89SMartin J. Bligh 		return 0;
274a686cd89SMartin J. Bligh 	if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start)
275a686cd89SMartin J. Bligh 		|| (grp_goal + group_first_block > rsv->_rsv_end)))
276a686cd89SMartin J. Bligh 		return 0;
277a686cd89SMartin J. Bligh 	return 1;
278a686cd89SMartin J. Bligh }
279a686cd89SMartin J. Bligh 
280a686cd89SMartin J. Bligh /**
281a686cd89SMartin J. Bligh  * search_reserve_window()
282c53ec7bcSWang Hai  * @root:		root of reservation tree
283a686cd89SMartin J. Bligh  * @goal:		target allocation block
284a686cd89SMartin J. Bligh  *
285a686cd89SMartin J. Bligh  * Find the reserved window which includes the goal, or the previous one
286a686cd89SMartin J. Bligh  * if the goal is not in any window.
287a686cd89SMartin J. Bligh  * Returns NULL if there are no windows or if all windows start after the goal.
288a686cd89SMartin J. Bligh  */
289a686cd89SMartin J. Bligh static struct ext2_reserve_window_node *
search_reserve_window(struct rb_root * root,ext2_fsblk_t goal)290a686cd89SMartin J. Bligh search_reserve_window(struct rb_root *root, ext2_fsblk_t goal)
291a686cd89SMartin J. Bligh {
292a686cd89SMartin J. Bligh 	struct rb_node *n = root->rb_node;
293a686cd89SMartin J. Bligh 	struct ext2_reserve_window_node *rsv;
294a686cd89SMartin J. Bligh 
295a686cd89SMartin J. Bligh 	if (!n)
296a686cd89SMartin J. Bligh 		return NULL;
297a686cd89SMartin J. Bligh 
298a686cd89SMartin J. Bligh 	do {
299a686cd89SMartin J. Bligh 		rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
300a686cd89SMartin J. Bligh 
301a686cd89SMartin J. Bligh 		if (goal < rsv->rsv_start)
302a686cd89SMartin J. Bligh 			n = n->rb_left;
303a686cd89SMartin J. Bligh 		else if (goal > rsv->rsv_end)
304a686cd89SMartin J. Bligh 			n = n->rb_right;
305a686cd89SMartin J. Bligh 		else
306a686cd89SMartin J. Bligh 			return rsv;
307a686cd89SMartin J. Bligh 	} while (n);
308a686cd89SMartin J. Bligh 	/*
309a686cd89SMartin J. Bligh 	 * We've fallen off the end of the tree: the goal wasn't inside
310a686cd89SMartin J. Bligh 	 * any particular node.  OK, the previous node must be to one
311a686cd89SMartin J. Bligh 	 * side of the interval containing the goal.  If it's the RHS,
312a686cd89SMartin J. Bligh 	 * we need to back up one.
313a686cd89SMartin J. Bligh 	 */
314a686cd89SMartin J. Bligh 	if (rsv->rsv_start > goal) {
315a686cd89SMartin J. Bligh 		n = rb_prev(&rsv->rsv_node);
316a686cd89SMartin J. Bligh 		rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
317a686cd89SMartin J. Bligh 	}
318a686cd89SMartin J. Bligh 	return rsv;
319a686cd89SMartin J. Bligh }
320a686cd89SMartin J. Bligh 
321a686cd89SMartin J. Bligh /*
322a686cd89SMartin J. Bligh  * ext2_rsv_window_add() -- Insert a window to the block reservation rb tree.
323a686cd89SMartin J. Bligh  * @sb:			super block
324a686cd89SMartin J. Bligh  * @rsv:		reservation window to add
325a686cd89SMartin J. Bligh  *
326a686cd89SMartin J. Bligh  * Must be called with rsv_lock held.
327a686cd89SMartin J. Bligh  */
ext2_rsv_window_add(struct super_block * sb,struct ext2_reserve_window_node * rsv)328a686cd89SMartin J. Bligh void ext2_rsv_window_add(struct super_block *sb,
329a686cd89SMartin J. Bligh 		    struct ext2_reserve_window_node *rsv)
330a686cd89SMartin J. Bligh {
331a686cd89SMartin J. Bligh 	struct rb_root *root = &EXT2_SB(sb)->s_rsv_window_root;
332a686cd89SMartin J. Bligh 	struct rb_node *node = &rsv->rsv_node;
333a686cd89SMartin J. Bligh 	ext2_fsblk_t start = rsv->rsv_start;
334a686cd89SMartin J. Bligh 
335a686cd89SMartin J. Bligh 	struct rb_node ** p = &root->rb_node;
336a686cd89SMartin J. Bligh 	struct rb_node * parent = NULL;
337a686cd89SMartin J. Bligh 	struct ext2_reserve_window_node *this;
338a686cd89SMartin J. Bligh 
339a686cd89SMartin J. Bligh 	while (*p)
340a686cd89SMartin J. Bligh 	{
341a686cd89SMartin J. Bligh 		parent = *p;
342a686cd89SMartin J. Bligh 		this = rb_entry(parent, struct ext2_reserve_window_node, rsv_node);
343a686cd89SMartin J. Bligh 
344a686cd89SMartin J. Bligh 		if (start < this->rsv_start)
345a686cd89SMartin J. Bligh 			p = &(*p)->rb_left;
346a686cd89SMartin J. Bligh 		else if (start > this->rsv_end)
347a686cd89SMartin J. Bligh 			p = &(*p)->rb_right;
348a686cd89SMartin J. Bligh 		else {
349a686cd89SMartin J. Bligh 			rsv_window_dump(root, 1);
350a686cd89SMartin J. Bligh 			BUG();
351a686cd89SMartin J. Bligh 		}
352a686cd89SMartin J. Bligh 	}
353a686cd89SMartin J. Bligh 
354a686cd89SMartin J. Bligh 	rb_link_node(node, parent, p);
355a686cd89SMartin J. Bligh 	rb_insert_color(node, root);
356a686cd89SMartin J. Bligh }
357a686cd89SMartin J. Bligh 
358a686cd89SMartin J. Bligh /**
359a686cd89SMartin J. Bligh  * rsv_window_remove() -- unlink a window from the reservation rb tree
360a686cd89SMartin J. Bligh  * @sb:			super block
361a686cd89SMartin J. Bligh  * @rsv:		reservation window to remove
362a686cd89SMartin J. Bligh  *
363a686cd89SMartin J. Bligh  * Mark the block reservation window as not allocated, and unlink it
364a686cd89SMartin J. Bligh  * from the filesystem reservation window rb tree. Must be called with
365a686cd89SMartin J. Bligh  * rsv_lock held.
366a686cd89SMartin J. Bligh  */
rsv_window_remove(struct super_block * sb,struct ext2_reserve_window_node * rsv)367a686cd89SMartin J. Bligh static void rsv_window_remove(struct super_block *sb,
368a686cd89SMartin J. Bligh 			      struct ext2_reserve_window_node *rsv)
369a686cd89SMartin J. Bligh {
370a686cd89SMartin J. Bligh 	rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
371a686cd89SMartin J. Bligh 	rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
372a686cd89SMartin J. Bligh 	rsv->rsv_alloc_hit = 0;
373a686cd89SMartin J. Bligh 	rb_erase(&rsv->rsv_node, &EXT2_SB(sb)->s_rsv_window_root);
374a686cd89SMartin J. Bligh }
375a686cd89SMartin J. Bligh 
376a686cd89SMartin J. Bligh /*
377a686cd89SMartin J. Bligh  * rsv_is_empty() -- Check if the reservation window is allocated.
378a686cd89SMartin J. Bligh  * @rsv:		given reservation window to check
379a686cd89SMartin J. Bligh  *
380a686cd89SMartin J. Bligh  * returns 1 if the end block is EXT2_RESERVE_WINDOW_NOT_ALLOCATED.
381a686cd89SMartin J. Bligh  */
rsv_is_empty(struct ext2_reserve_window * rsv)382a686cd89SMartin J. Bligh static inline int rsv_is_empty(struct ext2_reserve_window *rsv)
383a686cd89SMartin J. Bligh {
384a686cd89SMartin J. Bligh 	/* a valid reservation end block could not be 0 */
385a686cd89SMartin J. Bligh 	return (rsv->_rsv_end == EXT2_RESERVE_WINDOW_NOT_ALLOCATED);
386a686cd89SMartin J. Bligh }
387a686cd89SMartin J. Bligh 
388a686cd89SMartin J. Bligh /**
389a686cd89SMartin J. Bligh  * ext2_init_block_alloc_info()
390a686cd89SMartin J. Bligh  * @inode:		file inode structure
391a686cd89SMartin J. Bligh  *
392a686cd89SMartin J. Bligh  * Allocate and initialize the  reservation window structure, and
393a686cd89SMartin J. Bligh  * link the window to the ext2 inode structure at last
394a686cd89SMartin J. Bligh  *
395a686cd89SMartin J. Bligh  * The reservation window structure is only dynamically allocated
396a686cd89SMartin J. Bligh  * and linked to ext2 inode the first time the open file
397a686cd89SMartin J. Bligh  * needs a new block. So, before every ext2_new_block(s) call, for
398a686cd89SMartin J. Bligh  * regular files, we should check whether the reservation window
399a686cd89SMartin J. Bligh  * structure exists or not. In the latter case, this function is called.
400a686cd89SMartin J. Bligh  * Fail to do so will result in block reservation being turned off for that
401a686cd89SMartin J. Bligh  * open file.
402a686cd89SMartin J. Bligh  *
403a686cd89SMartin J. Bligh  * This function is called from ext2_get_blocks_handle(), also called
404a686cd89SMartin J. Bligh  * when setting the reservation window size through ioctl before the file
405a686cd89SMartin J. Bligh  * is open for write (needs block allocation).
406a686cd89SMartin J. Bligh  *
407a686cd89SMartin J. Bligh  * Needs truncate_mutex protection prior to calling this function.
408a686cd89SMartin J. Bligh  */
ext2_init_block_alloc_info(struct inode * inode)409a686cd89SMartin J. Bligh void ext2_init_block_alloc_info(struct inode *inode)
410a686cd89SMartin J. Bligh {
411a686cd89SMartin J. Bligh 	struct ext2_inode_info *ei = EXT2_I(inode);
4126e3d6ca0SJulia Lawall 	struct ext2_block_alloc_info *block_i;
413a686cd89SMartin J. Bligh 	struct super_block *sb = inode->i_sb;
414a686cd89SMartin J. Bligh 
415a686cd89SMartin J. Bligh 	block_i = kmalloc(sizeof(*block_i), GFP_NOFS);
416a686cd89SMartin J. Bligh 	if (block_i) {
417a686cd89SMartin J. Bligh 		struct ext2_reserve_window_node *rsv = &block_i->rsv_window_node;
418a686cd89SMartin J. Bligh 
419a686cd89SMartin J. Bligh 		rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
420a686cd89SMartin J. Bligh 		rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
421a686cd89SMartin J. Bligh 
422a686cd89SMartin J. Bligh 	 	/*
423a686cd89SMartin J. Bligh 		 * if filesystem is mounted with NORESERVATION, the goal
424a686cd89SMartin J. Bligh 		 * reservation window size is set to zero to indicate
425a686cd89SMartin J. Bligh 		 * block reservation is off
426a686cd89SMartin J. Bligh 		 */
427a686cd89SMartin J. Bligh 		if (!test_opt(sb, RESERVATION))
428a686cd89SMartin J. Bligh 			rsv->rsv_goal_size = 0;
429a686cd89SMartin J. Bligh 		else
430a686cd89SMartin J. Bligh 			rsv->rsv_goal_size = EXT2_DEFAULT_RESERVE_BLOCKS;
431a686cd89SMartin J. Bligh 		rsv->rsv_alloc_hit = 0;
432a686cd89SMartin J. Bligh 		block_i->last_alloc_logical_block = 0;
433a686cd89SMartin J. Bligh 		block_i->last_alloc_physical_block = 0;
434a686cd89SMartin J. Bligh 	}
435a686cd89SMartin J. Bligh 	ei->i_block_alloc_info = block_i;
436a686cd89SMartin J. Bligh }
437a686cd89SMartin J. Bligh 
438a686cd89SMartin J. Bligh /**
439a686cd89SMartin J. Bligh  * ext2_discard_reservation()
440a686cd89SMartin J. Bligh  * @inode:		inode
441a686cd89SMartin J. Bligh  *
442a686cd89SMartin J. Bligh  * Discard(free) block reservation window on last file close, or truncate
443a686cd89SMartin J. Bligh  * or at last iput().
444a686cd89SMartin J. Bligh  *
445a686cd89SMartin J. Bligh  * It is being called in three cases:
446a686cd89SMartin J. Bligh  * 	ext2_release_file(): last writer closes the file
447a686cd89SMartin J. Bligh  * 	ext2_clear_inode(): last iput(), when nobody links to this file.
448a686cd89SMartin J. Bligh  * 	ext2_truncate(): when the block indirect map is about to change.
449a686cd89SMartin J. Bligh  */
ext2_discard_reservation(struct inode * inode)450a686cd89SMartin J. Bligh void ext2_discard_reservation(struct inode *inode)
451a686cd89SMartin J. Bligh {
452a686cd89SMartin J. Bligh 	struct ext2_inode_info *ei = EXT2_I(inode);
453a686cd89SMartin J. Bligh 	struct ext2_block_alloc_info *block_i = ei->i_block_alloc_info;
454a686cd89SMartin J. Bligh 	struct ext2_reserve_window_node *rsv;
455a686cd89SMartin J. Bligh 	spinlock_t *rsv_lock = &EXT2_SB(inode->i_sb)->s_rsv_window_lock;
456a686cd89SMartin J. Bligh 
457a686cd89SMartin J. Bligh 	if (!block_i)
458a686cd89SMartin J. Bligh 		return;
459a686cd89SMartin J. Bligh 
460a686cd89SMartin J. Bligh 	rsv = &block_i->rsv_window_node;
461a686cd89SMartin J. Bligh 	if (!rsv_is_empty(&rsv->rsv_window)) {
462a686cd89SMartin J. Bligh 		spin_lock(rsv_lock);
463a686cd89SMartin J. Bligh 		if (!rsv_is_empty(&rsv->rsv_window))
464a686cd89SMartin J. Bligh 			rsv_window_remove(inode->i_sb, rsv);
465a686cd89SMartin J. Bligh 		spin_unlock(rsv_lock);
466a686cd89SMartin J. Bligh 	}
467a686cd89SMartin J. Bligh }
468a686cd89SMartin J. Bligh 
469a686cd89SMartin J. Bligh /**
47003248766SWang Sheng-Hui  * ext2_free_blocks() -- Free given blocks and update quota and i_blocks
471a686cd89SMartin J. Bligh  * @inode:		inode
4724907cb7bSAnatol Pomozov  * @block:		start physical block to free
473a686cd89SMartin J. Bligh  * @count:		number of blocks to free
474a686cd89SMartin J. Bligh  */
ext2_free_blocks(struct inode * inode,ext2_fsblk_t block,unsigned long count)4751da177e4SLinus Torvalds void ext2_free_blocks(struct inode * inode, ext2_fsblk_t block,
4761da177e4SLinus Torvalds 		      unsigned long count)
4771da177e4SLinus Torvalds {
4781da177e4SLinus Torvalds 	struct buffer_head *bitmap_bh = NULL;
4791da177e4SLinus Torvalds 	struct buffer_head * bh2;
4801da177e4SLinus Torvalds 	unsigned long block_group;
4811da177e4SLinus Torvalds 	unsigned long bit;
4821da177e4SLinus Torvalds 	unsigned long i;
4831da177e4SLinus Torvalds 	unsigned long overflow;
4841da177e4SLinus Torvalds 	struct super_block * sb = inode->i_sb;
4851da177e4SLinus Torvalds 	struct ext2_sb_info * sbi = EXT2_SB(sb);
4861da177e4SLinus Torvalds 	struct ext2_group_desc * desc;
4871da177e4SLinus Torvalds 	struct ext2_super_block * es = sbi->s_es;
4881da177e4SLinus Torvalds 	unsigned freed = 0, group_freed;
4891da177e4SLinus Torvalds 
490b6aeffc5SChengguang Xu 	if (!ext2_data_block_valid(sbi, block, count)) {
4911da177e4SLinus Torvalds 		ext2_error (sb, "ext2_free_blocks",
4921da177e4SLinus Torvalds 			    "Freeing blocks not in datazone - "
4931da177e4SLinus Torvalds 			    "block = %lu, count = %lu", block, count);
4941da177e4SLinus Torvalds 		goto error_return;
4951da177e4SLinus Torvalds 	}
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	ext2_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1);
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds do_more:
5001da177e4SLinus Torvalds 	overflow = 0;
5011da177e4SLinus Torvalds 	block_group = (block - le32_to_cpu(es->s_first_data_block)) /
5021da177e4SLinus Torvalds 		      EXT2_BLOCKS_PER_GROUP(sb);
5031da177e4SLinus Torvalds 	bit = (block - le32_to_cpu(es->s_first_data_block)) %
5041da177e4SLinus Torvalds 		      EXT2_BLOCKS_PER_GROUP(sb);
5051da177e4SLinus Torvalds 	/*
5061da177e4SLinus Torvalds 	 * Check to see if we are freeing blocks across a group
5071da177e4SLinus Torvalds 	 * boundary.
5081da177e4SLinus Torvalds 	 */
5091da177e4SLinus Torvalds 	if (bit + count > EXT2_BLOCKS_PER_GROUP(sb)) {
5101da177e4SLinus Torvalds 		overflow = bit + count - EXT2_BLOCKS_PER_GROUP(sb);
5111da177e4SLinus Torvalds 		count -= overflow;
5121da177e4SLinus Torvalds 	}
5131da177e4SLinus Torvalds 	brelse(bitmap_bh);
5141da177e4SLinus Torvalds 	bitmap_bh = read_block_bitmap(sb, block_group);
5151da177e4SLinus Torvalds 	if (!bitmap_bh)
5161da177e4SLinus Torvalds 		goto error_return;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	desc = ext2_get_group_desc (sb, block_group, &bh2);
5191da177e4SLinus Torvalds 	if (!desc)
5201da177e4SLinus Torvalds 		goto error_return;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) ||
5231da177e4SLinus Torvalds 	    in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) ||
5241da177e4SLinus Torvalds 	    in_range (block, le32_to_cpu(desc->bg_inode_table),
5251da177e4SLinus Torvalds 		      sbi->s_itb_per_group) ||
5261da177e4SLinus Torvalds 	    in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table),
5277f0adaecSAneesh Kumar K.V 		      sbi->s_itb_per_group)) {
5281da177e4SLinus Torvalds 		ext2_error (sb, "ext2_free_blocks",
5291da177e4SLinus Torvalds 			    "Freeing blocks in system zones - "
5301da177e4SLinus Torvalds 			    "Block = %lu, count = %lu",
5311da177e4SLinus Torvalds 			    block, count);
5327f0adaecSAneesh Kumar K.V 		goto error_return;
5337f0adaecSAneesh Kumar K.V 	}
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	for (i = 0, group_freed = 0; i < count; i++) {
5361da177e4SLinus Torvalds 		if (!ext2_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
5371da177e4SLinus Torvalds 						bit + i, bitmap_bh->b_data)) {
538605afd60SHarvey Harrison 			ext2_error(sb, __func__,
5391da177e4SLinus Torvalds 				"bit already cleared for block %lu", block + i);
5401da177e4SLinus Torvalds 		} else {
5411da177e4SLinus Torvalds 			group_freed++;
5421da177e4SLinus Torvalds 		}
5431da177e4SLinus Torvalds 	}
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	mark_buffer_dirty(bitmap_bh);
5461751e8a6SLinus Torvalds 	if (sb->s_flags & SB_SYNCHRONOUS)
5471da177e4SLinus Torvalds 		sync_dirty_buffer(bitmap_bh);
5481da177e4SLinus Torvalds 
549a686cd89SMartin J. Bligh 	group_adjust_blocks(sb, block_group, desc, bh2, group_freed);
5501da177e4SLinus Torvalds 	freed += group_freed;
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	if (overflow) {
5531da177e4SLinus Torvalds 		block += count;
5541da177e4SLinus Torvalds 		count = overflow;
5551da177e4SLinus Torvalds 		goto do_more;
5561da177e4SLinus Torvalds 	}
5571da177e4SLinus Torvalds error_return:
5581da177e4SLinus Torvalds 	brelse(bitmap_bh);
5598e3dffc6SWang Shilong 	if (freed) {
560712ddc52SWang Shilong 		percpu_counter_add(&sbi->s_freeblocks_counter, freed);
5613889717dSAl Viro 		dquot_free_block_nodirty(inode, freed);
5628e3dffc6SWang Shilong 		mark_inode_dirty(inode);
5638e3dffc6SWang Shilong 	}
5641da177e4SLinus Torvalds }
5651da177e4SLinus Torvalds 
566a686cd89SMartin J. Bligh /**
567a686cd89SMartin J. Bligh  * bitmap_search_next_usable_block()
568a686cd89SMartin J. Bligh  * @start:		the starting block (group relative) of the search
569a686cd89SMartin J. Bligh  * @bh:			bufferhead contains the block group bitmap
570a686cd89SMartin J. Bligh  * @maxblocks:		the ending block (group relative) of the reservation
571a686cd89SMartin J. Bligh  *
572a686cd89SMartin J. Bligh  * The bitmap search --- search forward through the actual bitmap on disk until
573a686cd89SMartin J. Bligh  * we find a bit free.
574a686cd89SMartin J. Bligh  */
575a686cd89SMartin J. Bligh static ext2_grpblk_t
bitmap_search_next_usable_block(ext2_grpblk_t start,struct buffer_head * bh,ext2_grpblk_t maxblocks)576a686cd89SMartin J. Bligh bitmap_search_next_usable_block(ext2_grpblk_t start, struct buffer_head *bh,
577a686cd89SMartin J. Bligh 					ext2_grpblk_t maxblocks)
5781da177e4SLinus Torvalds {
579a686cd89SMartin J. Bligh 	ext2_grpblk_t next;
580a686cd89SMartin J. Bligh 
581a686cd89SMartin J. Bligh 	next = ext2_find_next_zero_bit(bh->b_data, maxblocks, start);
582a686cd89SMartin J. Bligh 	if (next >= maxblocks)
583a686cd89SMartin J. Bligh 		return -1;
584a686cd89SMartin J. Bligh 	return next;
585a686cd89SMartin J. Bligh }
586a686cd89SMartin J. Bligh 
587a686cd89SMartin J. Bligh /**
588a686cd89SMartin J. Bligh  * find_next_usable_block()
589a686cd89SMartin J. Bligh  * @start:		the starting block (group relative) to find next
590a686cd89SMartin J. Bligh  * 			allocatable block in bitmap.
591a686cd89SMartin J. Bligh  * @bh:			bufferhead contains the block group bitmap
592a686cd89SMartin J. Bligh  * @maxblocks:		the ending block (group relative) for the search
593a686cd89SMartin J. Bligh  *
594a686cd89SMartin J. Bligh  * Find an allocatable block in a bitmap.  We perform the "most
595a686cd89SMartin J. Bligh  * appropriate allocation" algorithm of looking for a free block near
596a686cd89SMartin J. Bligh  * the initial goal; then for a free byte somewhere in the bitmap;
597a686cd89SMartin J. Bligh  * then for any free bit in the bitmap.
598a686cd89SMartin J. Bligh  */
599a686cd89SMartin J. Bligh static ext2_grpblk_t
find_next_usable_block(int start,struct buffer_head * bh,int maxblocks)600a686cd89SMartin J. Bligh find_next_usable_block(int start, struct buffer_head *bh, int maxblocks)
601a686cd89SMartin J. Bligh {
602a686cd89SMartin J. Bligh 	ext2_grpblk_t here, next;
6031da177e4SLinus Torvalds 	char *p, *r;
6041da177e4SLinus Torvalds 
605a686cd89SMartin J. Bligh 	if (start > 0) {
6061da177e4SLinus Torvalds 		/*
6071da177e4SLinus Torvalds 		 * The goal was occupied; search forward for a free
6081da177e4SLinus Torvalds 		 * block within the next XX blocks.
6091da177e4SLinus Torvalds 		 *
6101da177e4SLinus Torvalds 		 * end_goal is more or less random, but it has to be
6111da177e4SLinus Torvalds 		 * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the
6121da177e4SLinus Torvalds 		 * next 64-bit boundary is simple..
6131da177e4SLinus Torvalds 		 */
614a686cd89SMartin J. Bligh 		ext2_grpblk_t end_goal = (start + 63) & ~63;
615a686cd89SMartin J. Bligh 		if (end_goal > maxblocks)
616a686cd89SMartin J. Bligh 			end_goal = maxblocks;
617a686cd89SMartin J. Bligh 		here = ext2_find_next_zero_bit(bh->b_data, end_goal, start);
618a686cd89SMartin J. Bligh 		if (here < end_goal)
619a686cd89SMartin J. Bligh 			return here;
620a686cd89SMartin J. Bligh 		ext2_debug("Bit not found near goal\n");
6211da177e4SLinus Torvalds 	}
6221da177e4SLinus Torvalds 
623a686cd89SMartin J. Bligh 	here = start;
624a686cd89SMartin J. Bligh 	if (here < 0)
625a686cd89SMartin J. Bligh 		here = 0;
626a686cd89SMartin J. Bligh 
627a686cd89SMartin J. Bligh 	p = ((char *)bh->b_data) + (here >> 3);
628a686cd89SMartin J. Bligh 	r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3));
629a686cd89SMartin J. Bligh 	next = (r - ((char *)bh->b_data)) << 3;
630a686cd89SMartin J. Bligh 
631a686cd89SMartin J. Bligh 	if (next < maxblocks && next >= here)
632a686cd89SMartin J. Bligh 		return next;
633a686cd89SMartin J. Bligh 
634a686cd89SMartin J. Bligh 	here = bitmap_search_next_usable_block(here, bh, maxblocks);
635a686cd89SMartin J. Bligh 	return here;
636a686cd89SMartin J. Bligh }
637a686cd89SMartin J. Bligh 
63826f78b7aSNamhyung Kim /**
639a686cd89SMartin J. Bligh  * ext2_try_to_allocate()
640a686cd89SMartin J. Bligh  * @sb:			superblock
641a686cd89SMartin J. Bligh  * @group:		given allocation block group
642a686cd89SMartin J. Bligh  * @bitmap_bh:		bufferhead holds the block bitmap
643a686cd89SMartin J. Bligh  * @grp_goal:		given target block within the group
644a686cd89SMartin J. Bligh  * @count:		target number of blocks to allocate
645a686cd89SMartin J. Bligh  * @my_rsv:		reservation window
646a686cd89SMartin J. Bligh  *
647a686cd89SMartin J. Bligh  * Attempt to allocate blocks within a give range. Set the range of allocation
648a686cd89SMartin J. Bligh  * first, then find the first free bit(s) from the bitmap (within the range),
649a686cd89SMartin J. Bligh  * and at last, allocate the blocks by claiming the found free bit as allocated.
650a686cd89SMartin J. Bligh  *
651a686cd89SMartin J. Bligh  * To set the range of this allocation:
652a686cd89SMartin J. Bligh  * 	if there is a reservation window, only try to allocate block(s)
653a686cd89SMartin J. Bligh  * 	from the file's own reservation window;
654a686cd89SMartin J. Bligh  * 	Otherwise, the allocation range starts from the give goal block,
655a686cd89SMartin J. Bligh  * 	ends at the block group's last block.
656a686cd89SMartin J. Bligh  *
657a686cd89SMartin J. Bligh  * If we failed to allocate the desired block then we may end up crossing to a
658a686cd89SMartin J. Bligh  * new bitmap.
6591da177e4SLinus Torvalds  */
660a686cd89SMartin 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)661a686cd89SMartin J. Bligh ext2_try_to_allocate(struct super_block *sb, int group,
662a686cd89SMartin J. Bligh 			struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal,
663a686cd89SMartin J. Bligh 			unsigned long *count,
664a686cd89SMartin J. Bligh 			struct ext2_reserve_window *my_rsv)
665a686cd89SMartin J. Bligh {
66690f3741cSChengguang Xu 	ext2_fsblk_t group_first_block = ext2_group_first_block_no(sb, group);
66790f3741cSChengguang Xu 	ext2_fsblk_t group_last_block = ext2_group_last_block_no(sb, group);
668a686cd89SMartin J. Bligh 	ext2_grpblk_t start, end;
669a686cd89SMartin J. Bligh 	unsigned long num = 0;
670a686cd89SMartin J. Bligh 
671cf4eb321SJan Kara 	start = 0;
672cf4eb321SJan Kara 	end = group_last_block - group_first_block + 1;
673a686cd89SMartin J. Bligh 	/* we do allocation within the reservation window if we have a window */
674a686cd89SMartin J. Bligh 	if (my_rsv) {
675a686cd89SMartin J. Bligh 		if (my_rsv->_rsv_start >= group_first_block)
676a686cd89SMartin J. Bligh 			start = my_rsv->_rsv_start - group_first_block;
677cf4eb321SJan Kara 		if (my_rsv->_rsv_end < group_last_block)
678a686cd89SMartin J. Bligh 			end = my_rsv->_rsv_end - group_first_block + 1;
679cf4eb321SJan Kara 		if (grp_goal < start || grp_goal >= end)
680a686cd89SMartin J. Bligh 			grp_goal = -1;
681a686cd89SMartin J. Bligh 	}
682a686cd89SMartin J. Bligh 	BUG_ON(start > EXT2_BLOCKS_PER_GROUP(sb));
683a686cd89SMartin J. Bligh 
684a686cd89SMartin J. Bligh 	if (grp_goal < 0) {
685a686cd89SMartin J. Bligh 		grp_goal = find_next_usable_block(start, bitmap_bh, end);
686a686cd89SMartin J. Bligh 		if (grp_goal < 0)
687a686cd89SMartin J. Bligh 			goto fail_access;
688a686cd89SMartin J. Bligh 		if (!my_rsv) {
689a686cd89SMartin J. Bligh 			int i;
690a686cd89SMartin J. Bligh 
691a686cd89SMartin J. Bligh 			for (i = 0; i < 7 && grp_goal > start &&
692a686cd89SMartin J. Bligh 					!ext2_test_bit(grp_goal - 1,
693a686cd89SMartin J. Bligh 					     		bitmap_bh->b_data);
694a686cd89SMartin J. Bligh 			     		i++, grp_goal--)
6951da177e4SLinus Torvalds 				;
696a686cd89SMartin J. Bligh 		}
697a686cd89SMartin J. Bligh 	}
698a686cd89SMartin J. Bligh 
69944dd6161SChengguang Xu 	for (; num < *count && grp_goal < end; grp_goal++) {
70044dd6161SChengguang Xu 		if (ext2_set_bit_atomic(sb_bgl_lock(EXT2_SB(sb), group),
701a686cd89SMartin J. Bligh 					grp_goal, bitmap_bh->b_data)) {
70244dd6161SChengguang Xu 			if (num == 0)
70344dd6161SChengguang Xu 				continue;
70444dd6161SChengguang Xu 			break;
705a686cd89SMartin J. Bligh 		}
70644dd6161SChengguang Xu 		num++;
70744dd6161SChengguang Xu 	}
70844dd6161SChengguang Xu 
70944dd6161SChengguang Xu 	if (num == 0)
71044dd6161SChengguang Xu 		goto fail_access;
71144dd6161SChengguang Xu 
712a686cd89SMartin J. Bligh 	*count = num;
713a686cd89SMartin J. Bligh 	return grp_goal - num;
714a686cd89SMartin J. Bligh fail_access:
715a686cd89SMartin J. Bligh 	return -1;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
718a686cd89SMartin J. Bligh /**
719a686cd89SMartin J. Bligh  * find_next_reservable_window - Find a reservable space within the given range.
720a686cd89SMartin J. Bligh  * @search_head: The list to search.
721a686cd89SMartin J. Bligh  * @my_rsv: The reservation we're currently using.
722a686cd89SMartin J. Bligh  * @sb: The super block.
723a686cd89SMartin J. Bligh  * @start_block: The first block we consider to start the real search from
724a686cd89SMartin J. Bligh  * @last_block: The maximum block number that our goal reservable space
725a686cd89SMartin J. Bligh  *	could start from.
726a686cd89SMartin J. Bligh  *
727a686cd89SMartin J. Bligh  * It does not allocate the reservation window: alloc_new_reservation()
728a686cd89SMartin J. Bligh  * will do the work later.
729a686cd89SMartin J. Bligh  *
730a686cd89SMartin J. Bligh  * We search the given range, rather than the whole reservation double
731a686cd89SMartin J. Bligh  * linked list, (start_block, last_block) to find a free region that is
732355b9aaeSChengguang Xu  * of my size and has not been reserved.
733a686cd89SMartin J. Bligh  *
734355b9aaeSChengguang Xu  * @search_head is not necessarily the list head of the whole filesystem.
735a686cd89SMartin J. Bligh  * We have both head and @start_block to assist the search for the
736a686cd89SMartin J. Bligh  * reservable space. The list starts from head, but we will shift to
737a686cd89SMartin J. Bligh  * the place where start_block is, then start from there, when looking
738a686cd89SMartin J. Bligh  * for a reservable space.
739a686cd89SMartin J. Bligh  *
740a686cd89SMartin J. Bligh  * @last_block is normally the last block in this group. The search will end
741a686cd89SMartin J. Bligh  * when we found the start of next possible reservable space is out
742a686cd89SMartin J. Bligh  * of this boundary.  This could handle the cross boundary reservation
743a686cd89SMartin J. Bligh  * window request.
744a686cd89SMartin J. Bligh  *
745a686cd89SMartin J. Bligh  * Return: -1 if we could not find a range of sufficient size.  If we could,
746a686cd89SMartin J. Bligh  * return 0 and fill in @my_rsv with the range information.
747a686cd89SMartin 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)748a686cd89SMartin J. Bligh static int find_next_reservable_window(
749a686cd89SMartin J. Bligh 				struct ext2_reserve_window_node *search_head,
750a686cd89SMartin J. Bligh 				struct ext2_reserve_window_node *my_rsv,
751a686cd89SMartin J. Bligh 				struct super_block * sb,
752a686cd89SMartin J. Bligh 				ext2_fsblk_t start_block,
753a686cd89SMartin J. Bligh 				ext2_fsblk_t last_block)
754a686cd89SMartin J. Bligh {
755a686cd89SMartin J. Bligh 	struct rb_node *next;
756a686cd89SMartin J. Bligh 	struct ext2_reserve_window_node *rsv, *prev;
757a686cd89SMartin J. Bligh 	ext2_fsblk_t cur;
758a686cd89SMartin J. Bligh 	int size = my_rsv->rsv_goal_size;
759a686cd89SMartin J. Bligh 
760a686cd89SMartin J. Bligh 	/* TODO: make the start of the reservation window byte-aligned */
761a686cd89SMartin J. Bligh 	/* cur = *start_block & ~7;*/
762a686cd89SMartin J. Bligh 	cur = start_block;
763a686cd89SMartin J. Bligh 	rsv = search_head;
764a686cd89SMartin J. Bligh 	if (!rsv)
765a686cd89SMartin J. Bligh 		return -1;
766a686cd89SMartin J. Bligh 
7671da177e4SLinus Torvalds 	while (1) {
768a686cd89SMartin J. Bligh 		if (cur <= rsv->rsv_end)
769a686cd89SMartin J. Bligh 			cur = rsv->rsv_end + 1;
770a686cd89SMartin J. Bligh 
771a686cd89SMartin J. Bligh 		/* TODO?
772a686cd89SMartin J. Bligh 		 * in the case we could not find a reservable space
773a686cd89SMartin J. Bligh 		 * that is what is expected, during the re-search, we could
774a686cd89SMartin J. Bligh 		 * remember what's the largest reservable space we could have
775a686cd89SMartin J. Bligh 		 * and return that one.
776a686cd89SMartin J. Bligh 		 *
777a686cd89SMartin J. Bligh 		 * For now it will fail if we could not find the reservable
778a686cd89SMartin J. Bligh 		 * space with expected-size (or more)...
779a686cd89SMartin J. Bligh 		 */
780a686cd89SMartin J. Bligh 		if (cur > last_block)
781a686cd89SMartin J. Bligh 			return -1;		/* fail */
782a686cd89SMartin J. Bligh 
783a686cd89SMartin J. Bligh 		prev = rsv;
784a686cd89SMartin J. Bligh 		next = rb_next(&rsv->rsv_node);
785a686cd89SMartin J. Bligh 		rsv = rb_entry(next,struct ext2_reserve_window_node,rsv_node);
786a686cd89SMartin J. Bligh 
787a686cd89SMartin J. Bligh 		/*
788a686cd89SMartin J. Bligh 		 * Reached the last reservation, we can just append to the
789a686cd89SMartin J. Bligh 		 * previous one.
790a686cd89SMartin J. Bligh 		 */
791a686cd89SMartin J. Bligh 		if (!next)
792a686cd89SMartin J. Bligh 			break;
793a686cd89SMartin J. Bligh 
794a686cd89SMartin J. Bligh 		if (cur + size <= rsv->rsv_start) {
795a686cd89SMartin J. Bligh 			/*
796a686cd89SMartin J. Bligh 			 * Found a reserveable space big enough.  We could
797a686cd89SMartin J. Bligh 			 * have a reservation across the group boundary here
798a686cd89SMartin J. Bligh 		 	 */
799a686cd89SMartin J. Bligh 			break;
800a686cd89SMartin J. Bligh 		}
801a686cd89SMartin J. Bligh 	}
802a686cd89SMartin J. Bligh 	/*
803a686cd89SMartin J. Bligh 	 * we come here either :
804a686cd89SMartin J. Bligh 	 * when we reach the end of the whole list,
805a686cd89SMartin J. Bligh 	 * and there is empty reservable space after last entry in the list.
806a686cd89SMartin J. Bligh 	 * append it to the end of the list.
807a686cd89SMartin J. Bligh 	 *
808a686cd89SMartin J. Bligh 	 * or we found one reservable space in the middle of the list,
809a686cd89SMartin J. Bligh 	 * return the reservation window that we could append to.
810a686cd89SMartin J. Bligh 	 * succeed.
811a686cd89SMartin J. Bligh 	 */
812a686cd89SMartin J. Bligh 
813a686cd89SMartin J. Bligh 	if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window)))
814a686cd89SMartin J. Bligh 		rsv_window_remove(sb, my_rsv);
815a686cd89SMartin J. Bligh 
816a686cd89SMartin J. Bligh 	/*
817a686cd89SMartin J. Bligh 	 * Let's book the whole available window for now.  We will check the
818a686cd89SMartin J. Bligh 	 * disk bitmap later and then, if there are free blocks then we adjust
81925985edcSLucas De Marchi 	 * the window size if it's larger than requested.
820a686cd89SMartin J. Bligh 	 * Otherwise, we will remove this node from the tree next time
821a686cd89SMartin J. Bligh 	 * call find_next_reservable_window.
822a686cd89SMartin J. Bligh 	 */
823a686cd89SMartin J. Bligh 	my_rsv->rsv_start = cur;
824a686cd89SMartin J. Bligh 	my_rsv->rsv_end = cur + size - 1;
825a686cd89SMartin J. Bligh 	my_rsv->rsv_alloc_hit = 0;
826a686cd89SMartin J. Bligh 
827a686cd89SMartin J. Bligh 	if (prev != my_rsv)
828a686cd89SMartin J. Bligh 		ext2_rsv_window_add(sb, my_rsv);
829a686cd89SMartin J. Bligh 
830a686cd89SMartin J. Bligh 	return 0;
831a686cd89SMartin J. Bligh }
832a686cd89SMartin J. Bligh 
833a686cd89SMartin J. Bligh /**
834a686cd89SMartin J. Bligh  * alloc_new_reservation - Allocate a new reservation window.
835a686cd89SMartin J. Bligh  * @my_rsv: The reservation we're currently using.
836a686cd89SMartin J. Bligh  * @grp_goal: The goal block relative to the start of the group.
837a686cd89SMartin J. Bligh  * @sb: The super block.
838a686cd89SMartin J. Bligh  * @group: The group we are trying to allocate in.
839a686cd89SMartin J. Bligh  * @bitmap_bh: The block group block bitmap.
840a686cd89SMartin J. Bligh  *
841a686cd89SMartin J. Bligh  * To make a new reservation, we search part of the filesystem reservation
842a686cd89SMartin J. Bligh  * list (the list inside the group). We try to allocate a new
843a686cd89SMartin J. Bligh  * reservation window near @grp_goal, or the beginning of the
844a686cd89SMartin J. Bligh  * group, if @grp_goal is negative.
845a686cd89SMartin J. Bligh  *
846a686cd89SMartin J. Bligh  * We first find a reservable space after the goal, then from there,
847a686cd89SMartin J. Bligh  * we check the bitmap for the first free block after it. If there is
848a686cd89SMartin J. Bligh  * no free block until the end of group, then the whole group is full,
849a686cd89SMartin J. Bligh  * we failed. Otherwise, check if the free block is inside the expected
850a686cd89SMartin J. Bligh  * reservable space, if so, we succeed.
851a686cd89SMartin J. Bligh  *
852a686cd89SMartin J. Bligh  * If the first free block is outside the reservable space, then start
853a686cd89SMartin J. Bligh  * from the first free block, we search for next available space, and
854a686cd89SMartin J. Bligh  * go on.
855a686cd89SMartin J. Bligh  *
856a686cd89SMartin J. Bligh  * on succeed, a new reservation will be found and inserted into the
857a686cd89SMartin J. Bligh  * list. It contains at least one free block, and it does not overlap
858a686cd89SMartin J. Bligh  * with other reservation windows.
859c53ec7bcSWang Hai  *
860a686cd89SMartin J. Bligh  * Return: 0 on success, -1 if we failed to find a reservation window
861a686cd89SMartin J. Bligh  * in this group
862a686cd89SMartin 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)863a686cd89SMartin J. Bligh static int alloc_new_reservation(struct ext2_reserve_window_node *my_rsv,
864a686cd89SMartin J. Bligh 		ext2_grpblk_t grp_goal, struct super_block *sb,
865a686cd89SMartin J. Bligh 		unsigned int group, struct buffer_head *bitmap_bh)
866a686cd89SMartin J. Bligh {
867a686cd89SMartin J. Bligh 	struct ext2_reserve_window_node *search_head;
868a686cd89SMartin J. Bligh 	ext2_fsblk_t group_first_block, group_end_block, start_block;
869a686cd89SMartin J. Bligh 	ext2_grpblk_t first_free_block;
870a686cd89SMartin J. Bligh 	struct rb_root *fs_rsv_root = &EXT2_SB(sb)->s_rsv_window_root;
871a686cd89SMartin J. Bligh 	unsigned long size;
872a686cd89SMartin J. Bligh 	int ret;
873a686cd89SMartin J. Bligh 	spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock;
874a686cd89SMartin J. Bligh 
875a686cd89SMartin J. Bligh 	group_first_block = ext2_group_first_block_no(sb, group);
876a686cd89SMartin J. Bligh 	group_end_block = ext2_group_last_block_no(sb, group);
877a686cd89SMartin J. Bligh 
878a686cd89SMartin J. Bligh 	if (grp_goal < 0)
879a686cd89SMartin J. Bligh 		start_block = group_first_block;
880a686cd89SMartin J. Bligh 	else
881a686cd89SMartin J. Bligh 		start_block = grp_goal + group_first_block;
882a686cd89SMartin J. Bligh 
883a686cd89SMartin J. Bligh 	size = my_rsv->rsv_goal_size;
884a686cd89SMartin J. Bligh 
88590f3741cSChengguang Xu 	if (!rsv_is_empty(&my_rsv->rsv_window)) {
886a686cd89SMartin J. Bligh 		/*
887a686cd89SMartin J. Bligh 		 * if the old reservation is cross group boundary
888a686cd89SMartin J. Bligh 		 * and if the goal is inside the old reservation window,
889a686cd89SMartin J. Bligh 		 * we will come here when we just failed to allocate from
890a686cd89SMartin J. Bligh 		 * the first part of the window. We still have another part
891a686cd89SMartin J. Bligh 		 * that belongs to the next group. In this case, there is no
892a686cd89SMartin J. Bligh 		 * point to discard our window and try to allocate a new one
893a686cd89SMartin J. Bligh 		 * in this group(which will fail). we should
894a686cd89SMartin J. Bligh 		 * keep the reservation window, just simply move on.
895a686cd89SMartin J. Bligh 		 *
896a686cd89SMartin J. Bligh 		 * Maybe we could shift the start block of the reservation
897a686cd89SMartin J. Bligh 		 * window to the first block of next group.
898a686cd89SMartin J. Bligh 		 */
899a686cd89SMartin J. Bligh 
900a686cd89SMartin J. Bligh 		if ((my_rsv->rsv_start <= group_end_block) &&
901a686cd89SMartin J. Bligh 				(my_rsv->rsv_end > group_end_block) &&
902a686cd89SMartin J. Bligh 				(start_block >= my_rsv->rsv_start))
903a686cd89SMartin J. Bligh 			return -1;
904a686cd89SMartin J. Bligh 
905a686cd89SMartin J. Bligh 		if ((my_rsv->rsv_alloc_hit >
906a686cd89SMartin J. Bligh 		     (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) {
907a686cd89SMartin J. Bligh 			/*
908a686cd89SMartin J. Bligh 			 * if the previously allocation hit ratio is
909a686cd89SMartin J. Bligh 			 * greater than 1/2, then we double the size of
910a686cd89SMartin J. Bligh 			 * the reservation window the next time,
911a686cd89SMartin J. Bligh 			 * otherwise we keep the same size window
912a686cd89SMartin J. Bligh 			 */
913a686cd89SMartin J. Bligh 			size = size * 2;
914a686cd89SMartin J. Bligh 			if (size > EXT2_MAX_RESERVE_BLOCKS)
915a686cd89SMartin J. Bligh 				size = EXT2_MAX_RESERVE_BLOCKS;
916a686cd89SMartin J. Bligh 			my_rsv->rsv_goal_size= size;
917a686cd89SMartin J. Bligh 		}
918a686cd89SMartin J. Bligh 	}
919a686cd89SMartin J. Bligh 
920a686cd89SMartin J. Bligh 	spin_lock(rsv_lock);
921a686cd89SMartin J. Bligh 	/*
922a686cd89SMartin J. Bligh 	 * shift the search start to the window near the goal block
923a686cd89SMartin J. Bligh 	 */
924a686cd89SMartin J. Bligh 	search_head = search_reserve_window(fs_rsv_root, start_block);
925a686cd89SMartin J. Bligh 
926a686cd89SMartin J. Bligh 	/*
927a686cd89SMartin J. Bligh 	 * find_next_reservable_window() simply finds a reservable window
928a686cd89SMartin J. Bligh 	 * inside the given range(start_block, group_end_block).
929a686cd89SMartin J. Bligh 	 *
930a686cd89SMartin J. Bligh 	 * To make sure the reservation window has a free bit inside it, we
931a686cd89SMartin J. Bligh 	 * need to check the bitmap after we found a reservable window.
932a686cd89SMartin J. Bligh 	 */
933a686cd89SMartin J. Bligh retry:
934a686cd89SMartin J. Bligh 	ret = find_next_reservable_window(search_head, my_rsv, sb,
935a686cd89SMartin J. Bligh 						start_block, group_end_block);
936a686cd89SMartin J. Bligh 
937a686cd89SMartin J. Bligh 	if (ret == -1) {
938a686cd89SMartin J. Bligh 		if (!rsv_is_empty(&my_rsv->rsv_window))
939a686cd89SMartin J. Bligh 			rsv_window_remove(sb, my_rsv);
940a686cd89SMartin J. Bligh 		spin_unlock(rsv_lock);
941a686cd89SMartin J. Bligh 		return -1;
942a686cd89SMartin J. Bligh 	}
943a686cd89SMartin J. Bligh 
944a686cd89SMartin J. Bligh 	/*
945a686cd89SMartin J. Bligh 	 * On success, find_next_reservable_window() returns the
946a686cd89SMartin J. Bligh 	 * reservation window where there is a reservable space after it.
947a686cd89SMartin J. Bligh 	 * Before we reserve this reservable space, we need
948a686cd89SMartin J. Bligh 	 * to make sure there is at least a free block inside this region.
949a686cd89SMartin J. Bligh 	 *
950a686cd89SMartin J. Bligh 	 * Search the first free bit on the block bitmap.  Search starts from
9511da177e4SLinus Torvalds 	 * the start block of the reservable space we just found.
9521da177e4SLinus Torvalds 	 */
9531da177e4SLinus Torvalds 	spin_unlock(rsv_lock);
954a686cd89SMartin J. Bligh 	first_free_block = bitmap_search_next_usable_block(
955a686cd89SMartin J. Bligh 			my_rsv->rsv_start - group_first_block,
956a686cd89SMartin J. Bligh 			bitmap_bh, group_end_block - group_first_block + 1);
957a686cd89SMartin J. Bligh 
958a686cd89SMartin J. Bligh 	if (first_free_block < 0) {
959a686cd89SMartin J. Bligh 		/*
960a686cd89SMartin J. Bligh 		 * no free block left on the bitmap, no point
961a686cd89SMartin J. Bligh 		 * to reserve the space. return failed.
962a686cd89SMartin J. Bligh 		 */
963a686cd89SMartin J. Bligh 		spin_lock(rsv_lock);
964a686cd89SMartin J. Bligh 		if (!rsv_is_empty(&my_rsv->rsv_window))
965a686cd89SMartin J. Bligh 			rsv_window_remove(sb, my_rsv);
966a686cd89SMartin J. Bligh 		spin_unlock(rsv_lock);
967a686cd89SMartin J. Bligh 		return -1;		/* failed */
968a686cd89SMartin J. Bligh 	}
969a686cd89SMartin J. Bligh 
970a686cd89SMartin J. Bligh 	start_block = first_free_block + group_first_block;
971a686cd89SMartin J. Bligh 	/*
972a686cd89SMartin J. Bligh 	 * check if the first free block is within the
973a686cd89SMartin J. Bligh 	 * free space we just reserved
974a686cd89SMartin J. Bligh 	 */
975a686cd89SMartin J. Bligh 	if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end)
976a686cd89SMartin J. Bligh 		return 0;		/* success */
977a686cd89SMartin J. Bligh 	/*
978a686cd89SMartin J. Bligh 	 * if the first free bit we found is out of the reservable space
979a686cd89SMartin J. Bligh 	 * continue search for next reservable space,
980a686cd89SMartin J. Bligh 	 * start from where the free block is,
981a686cd89SMartin J. Bligh 	 * we also shift the list head to where we stopped last time
982a686cd89SMartin J. Bligh 	 */
983a686cd89SMartin J. Bligh 	search_head = my_rsv;
984a686cd89SMartin J. Bligh 	spin_lock(rsv_lock);
985a686cd89SMartin J. Bligh 	goto retry;
986a686cd89SMartin J. Bligh }
987a686cd89SMartin J. Bligh 
988a686cd89SMartin J. Bligh /**
989a686cd89SMartin J. Bligh  * try_to_extend_reservation()
990a686cd89SMartin J. Bligh  * @my_rsv:		given reservation window
991a686cd89SMartin J. Bligh  * @sb:			super block
992a686cd89SMartin J. Bligh  * @size:		the delta to extend
993a686cd89SMartin J. Bligh  *
994a686cd89SMartin J. Bligh  * Attempt to expand the reservation window large enough to have
995a686cd89SMartin J. Bligh  * required number of free blocks
996a686cd89SMartin J. Bligh  *
997a686cd89SMartin J. Bligh  * Since ext2_try_to_allocate() will always allocate blocks within
998a686cd89SMartin J. Bligh  * the reservation window range, if the window size is too small,
999a686cd89SMartin J. Bligh  * multiple blocks allocation has to stop at the end of the reservation
1000a686cd89SMartin J. Bligh  * window. To make this more efficient, given the total number of
1001a686cd89SMartin J. Bligh  * blocks needed and the current size of the window, we try to
1002a686cd89SMartin J. Bligh  * expand the reservation window size if necessary on a best-effort
1003a686cd89SMartin J. Bligh  * basis before ext2_new_blocks() tries to allocate blocks.
1004a686cd89SMartin J. Bligh  */
try_to_extend_reservation(struct ext2_reserve_window_node * my_rsv,struct super_block * sb,int size)1005a686cd89SMartin J. Bligh static void try_to_extend_reservation(struct ext2_reserve_window_node *my_rsv,
1006a686cd89SMartin J. Bligh 			struct super_block *sb, int size)
1007a686cd89SMartin J. Bligh {
1008a686cd89SMartin J. Bligh 	struct ext2_reserve_window_node *next_rsv;
1009a686cd89SMartin J. Bligh 	struct rb_node *next;
1010a686cd89SMartin J. Bligh 	spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock;
1011a686cd89SMartin J. Bligh 
1012a686cd89SMartin J. Bligh 	if (!spin_trylock(rsv_lock))
1013a686cd89SMartin J. Bligh 		return;
1014a686cd89SMartin J. Bligh 
1015a686cd89SMartin J. Bligh 	next = rb_next(&my_rsv->rsv_node);
1016a686cd89SMartin J. Bligh 
1017a686cd89SMartin J. Bligh 	if (!next)
1018a686cd89SMartin J. Bligh 		my_rsv->rsv_end += size;
1019a686cd89SMartin J. Bligh 	else {
1020a686cd89SMartin J. Bligh 		next_rsv = rb_entry(next, struct ext2_reserve_window_node, rsv_node);
1021a686cd89SMartin J. Bligh 
1022a686cd89SMartin J. Bligh 		if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size)
1023a686cd89SMartin J. Bligh 			my_rsv->rsv_end += size;
1024a686cd89SMartin J. Bligh 		else
1025a686cd89SMartin J. Bligh 			my_rsv->rsv_end = next_rsv->rsv_start - 1;
1026a686cd89SMartin J. Bligh 	}
1027a686cd89SMartin J. Bligh 	spin_unlock(rsv_lock);
1028a686cd89SMartin J. Bligh }
1029a686cd89SMartin J. Bligh 
1030a686cd89SMartin J. Bligh /**
1031a686cd89SMartin J. Bligh  * ext2_try_to_allocate_with_rsv()
1032a686cd89SMartin J. Bligh  * @sb:			superblock
1033a686cd89SMartin J. Bligh  * @group:		given allocation block group
1034a686cd89SMartin J. Bligh  * @bitmap_bh:		bufferhead holds the block bitmap
1035a686cd89SMartin J. Bligh  * @grp_goal:		given target block within the group
1036a686cd89SMartin J. Bligh  * @count:		target number of blocks to allocate
1037a686cd89SMartin J. Bligh  * @my_rsv:		reservation window
1038a686cd89SMartin J. Bligh  *
1039a686cd89SMartin J. Bligh  * This is the main function used to allocate a new block and its reservation
1040a686cd89SMartin J. Bligh  * window.
1041a686cd89SMartin J. Bligh  *
1042a686cd89SMartin J. Bligh  * Each time when a new block allocation is need, first try to allocate from
1043a686cd89SMartin J. Bligh  * its own reservation.  If it does not have a reservation window, instead of
1044a686cd89SMartin J. Bligh  * looking for a free bit on bitmap first, then look up the reservation list to
1045a686cd89SMartin J. Bligh  * see if it is inside somebody else's reservation window, we try to allocate a
1046a686cd89SMartin J. Bligh  * reservation window for it starting from the goal first. Then do the block
1047a686cd89SMartin J. Bligh  * allocation within the reservation window.
1048a686cd89SMartin J. Bligh  *
1049a686cd89SMartin J. Bligh  * This will avoid keeping on searching the reservation list again and
1050a686cd89SMartin J. Bligh  * again when somebody is looking for a free block (without
1051a686cd89SMartin J. Bligh  * reservation), and there are lots of free blocks, but they are all
1052a686cd89SMartin J. Bligh  * being reserved.
1053a686cd89SMartin J. Bligh  *
1054a686cd89SMartin J. Bligh  * We use a red-black tree for the per-filesystem reservation list.
1055a686cd89SMartin J. Bligh  */
1056a686cd89SMartin 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)1057a686cd89SMartin J. Bligh ext2_try_to_allocate_with_rsv(struct super_block *sb, unsigned int group,
1058a686cd89SMartin J. Bligh 			struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal,
1059a686cd89SMartin J. Bligh 			struct ext2_reserve_window_node * my_rsv,
1060a686cd89SMartin J. Bligh 			unsigned long *count)
1061a686cd89SMartin J. Bligh {
1062a686cd89SMartin J. Bligh 	ext2_fsblk_t group_first_block, group_last_block;
1063a686cd89SMartin J. Bligh 	ext2_grpblk_t ret = 0;
1064a686cd89SMartin J. Bligh 	unsigned long num = *count;
1065a686cd89SMartin J. Bligh 
1066a686cd89SMartin J. Bligh 	/*
1067a686cd89SMartin J. Bligh 	 * we don't deal with reservation when
1068a686cd89SMartin J. Bligh 	 * filesystem is mounted without reservation
1069a686cd89SMartin J. Bligh 	 * or the file is not a regular file
1070a686cd89SMartin J. Bligh 	 * or last attempt to allocate a block with reservation turned on failed
1071a686cd89SMartin J. Bligh 	 */
1072a686cd89SMartin J. Bligh 	if (my_rsv == NULL) {
1073a686cd89SMartin J. Bligh 		return ext2_try_to_allocate(sb, group, bitmap_bh,
1074a686cd89SMartin J. Bligh 						grp_goal, count, NULL);
1075a686cd89SMartin J. Bligh 	}
1076a686cd89SMartin J. Bligh 	/*
1077a686cd89SMartin J. Bligh 	 * grp_goal is a group relative block number (if there is a goal)
1078a686cd89SMartin J. Bligh 	 * 0 <= grp_goal < EXT2_BLOCKS_PER_GROUP(sb)
1079a686cd89SMartin J. Bligh 	 * first block is a filesystem wide block number
1080a686cd89SMartin J. Bligh 	 * first block is the block number of the first block in this group
1081a686cd89SMartin J. Bligh 	 */
1082a686cd89SMartin J. Bligh 	group_first_block = ext2_group_first_block_no(sb, group);
1083a686cd89SMartin J. Bligh 	group_last_block = ext2_group_last_block_no(sb, group);
1084a686cd89SMartin J. Bligh 
1085a686cd89SMartin J. Bligh 	/*
1086a686cd89SMartin J. Bligh 	 * Basically we will allocate a new block from inode's reservation
1087a686cd89SMartin J. Bligh 	 * window.
1088a686cd89SMartin J. Bligh 	 *
1089a686cd89SMartin J. Bligh 	 * We need to allocate a new reservation window, if:
1090a686cd89SMartin J. Bligh 	 * a) inode does not have a reservation window; or
1091a686cd89SMartin J. Bligh 	 * b) last attempt to allocate a block from existing reservation
109290f3741cSChengguang Xu 	 *    failed; or
1093a686cd89SMartin J. Bligh 	 * c) we come here with a goal and with a reservation window
1094a686cd89SMartin J. Bligh 	 *
1095a686cd89SMartin J. Bligh 	 * We do not need to allocate a new reservation window if we come here
1096a686cd89SMartin J. Bligh 	 * at the beginning with a goal and the goal is inside the window, or
1097a686cd89SMartin J. Bligh 	 * we don't have a goal but already have a reservation window.
1098a686cd89SMartin J. Bligh 	 * then we could go to allocate from the reservation window directly.
1099a686cd89SMartin J. Bligh 	 */
1100a686cd89SMartin J. Bligh 	while (1) {
1101a686cd89SMartin J. Bligh 		if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) ||
1102a686cd89SMartin J. Bligh 			!goal_in_my_reservation(&my_rsv->rsv_window,
1103a686cd89SMartin J. Bligh 						grp_goal, group, sb)) {
1104a686cd89SMartin J. Bligh 			if (my_rsv->rsv_goal_size < *count)
1105a686cd89SMartin J. Bligh 				my_rsv->rsv_goal_size = *count;
1106a686cd89SMartin J. Bligh 			ret = alloc_new_reservation(my_rsv, grp_goal, sb,
1107a686cd89SMartin J. Bligh 							group, bitmap_bh);
1108a686cd89SMartin J. Bligh 			if (ret < 0)
1109a686cd89SMartin J. Bligh 				break;			/* failed */
1110a686cd89SMartin J. Bligh 
1111a686cd89SMartin J. Bligh 			if (!goal_in_my_reservation(&my_rsv->rsv_window,
1112a686cd89SMartin J. Bligh 							grp_goal, group, sb))
1113a686cd89SMartin J. Bligh 				grp_goal = -1;
1114a686cd89SMartin J. Bligh 		} else if (grp_goal >= 0) {
1115a686cd89SMartin J. Bligh 			int curr = my_rsv->rsv_end -
1116a686cd89SMartin J. Bligh 					(grp_goal + group_first_block) + 1;
1117a686cd89SMartin J. Bligh 
1118a686cd89SMartin J. Bligh 			if (curr < *count)
1119a686cd89SMartin J. Bligh 				try_to_extend_reservation(my_rsv, sb,
1120a686cd89SMartin J. Bligh 							*count - curr);
1121a686cd89SMartin J. Bligh 		}
1122a686cd89SMartin J. Bligh 
1123a686cd89SMartin J. Bligh 		if ((my_rsv->rsv_start > group_last_block) ||
1124a686cd89SMartin J. Bligh 				(my_rsv->rsv_end < group_first_block)) {
1125a686cd89SMartin J. Bligh 			ext2_error(sb, __func__,
1126a686cd89SMartin J. Bligh 				   "Reservation out of group %u range goal %d fsb[%lu,%lu] rsv[%lu, %lu]",
1127a686cd89SMartin J. Bligh 				   group, grp_goal, group_first_block,
1128a686cd89SMartin J. Bligh 				   group_last_block, my_rsv->rsv_start,
1129a686cd89SMartin J. Bligh 				   my_rsv->rsv_end);
1130a686cd89SMartin J. Bligh 			rsv_window_dump(&EXT2_SB(sb)->s_rsv_window_root, 1);
1131a686cd89SMartin J. Bligh 			return -1;
1132a686cd89SMartin J. Bligh 		}
1133a686cd89SMartin J. Bligh 		ret = ext2_try_to_allocate(sb, group, bitmap_bh, grp_goal,
1134a686cd89SMartin J. Bligh 					   &num, &my_rsv->rsv_window);
1135a686cd89SMartin J. Bligh 		if (ret >= 0) {
1136a686cd89SMartin J. Bligh 			my_rsv->rsv_alloc_hit += num;
1137a686cd89SMartin J. Bligh 			*count = num;
1138a686cd89SMartin J. Bligh 			break;				/* succeed */
1139a686cd89SMartin J. Bligh 		}
1140a686cd89SMartin J. Bligh 		num = *count;
1141a686cd89SMartin J. Bligh 	}
1142a686cd89SMartin J. Bligh 	return ret;
1143a686cd89SMartin J. Bligh }
1144a686cd89SMartin J. Bligh 
1145a686cd89SMartin J. Bligh /**
1146a686cd89SMartin J. Bligh  * ext2_has_free_blocks()
1147a686cd89SMartin J. Bligh  * @sbi:		in-core super block structure.
1148a686cd89SMartin J. Bligh  *
1149a686cd89SMartin J. Bligh  * Check if filesystem has at least 1 free block available for allocation.
1150a686cd89SMartin J. Bligh  */
ext2_has_free_blocks(struct ext2_sb_info * sbi)1151a686cd89SMartin J. Bligh static int ext2_has_free_blocks(struct ext2_sb_info *sbi)
1152a686cd89SMartin J. Bligh {
1153a686cd89SMartin J. Bligh 	ext2_fsblk_t free_blocks, root_blocks;
1154a686cd89SMartin J. Bligh 
1155a686cd89SMartin J. Bligh 	free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
1156a686cd89SMartin J. Bligh 	root_blocks = le32_to_cpu(sbi->s_es->s_r_blocks_count);
1157a686cd89SMartin J. Bligh 	if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) &&
1158a686cd89SMartin J. Bligh 		!uid_eq(sbi->s_resuid, current_fsuid()) &&
1159a686cd89SMartin J. Bligh 		(gid_eq(sbi->s_resgid, GLOBAL_ROOT_GID) ||
1160a686cd89SMartin J. Bligh 		 !in_group_p (sbi->s_resgid))) {
1161a686cd89SMartin J. Bligh 		return 0;
1162b8a9f9e1SEric W. Biederman 	}
1163b8a9f9e1SEric W. Biederman 	return 1;
1164b8a9f9e1SEric W. Biederman }
1165a686cd89SMartin J. Bligh 
1166a686cd89SMartin J. Bligh /*
1167a686cd89SMartin J. Bligh  * Returns 1 if the passed-in block region is valid; 0 if some part overlaps
1168a686cd89SMartin J. Bligh  * with filesystem metadata blocks.
1169a686cd89SMartin J. Bligh  */
ext2_data_block_valid(struct ext2_sb_info * sbi,ext2_fsblk_t start_blk,unsigned int count)1170a686cd89SMartin J. Bligh int ext2_data_block_valid(struct ext2_sb_info *sbi, ext2_fsblk_t start_blk,
1171ff0031d8SCarlos Maiolino 			  unsigned int count)
11721fe03415SChengguang Xu {
1173ff0031d8SCarlos Maiolino 	if ((start_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
1174ff0031d8SCarlos Maiolino 	    (start_blk + count - 1 < start_blk) ||
1175ff0031d8SCarlos Maiolino 	    (start_blk + count - 1 >= le32_to_cpu(sbi->s_es->s_blocks_count)))
1176ff0031d8SCarlos Maiolino 		return 0;
1177ff0031d8SCarlos Maiolino 
1178e5d39597SChengguang Xu 	/* Ensure we do not step over superblock */
1179e5d39597SChengguang Xu 	if ((start_blk <= sbi->s_sb_block) &&
1180ff0031d8SCarlos Maiolino 	    (start_blk + count - 1 >= sbi->s_sb_block))
1181ff0031d8SCarlos Maiolino 		return 0;
1182ff0031d8SCarlos Maiolino 
1183ff0031d8SCarlos Maiolino 	return 1;
1184e5d39597SChengguang Xu }
1185ff0031d8SCarlos Maiolino 
1186ff0031d8SCarlos Maiolino /*
1187ff0031d8SCarlos Maiolino  * ext2_new_blocks() -- core block(s) allocation function
1188ff0031d8SCarlos Maiolino  * @inode:		file inode
1189ff0031d8SCarlos Maiolino  * @goal:		given target block(filesystem wide)
1190ff0031d8SCarlos Maiolino  * @count:		target number of blocks to allocate
1191a686cd89SMartin J. Bligh  * @errp:		error code
1192a686cd89SMartin J. Bligh  * @flags:		allocate flags
1193a686cd89SMartin J. Bligh  *
1194a686cd89SMartin J. Bligh  * ext2_new_blocks uses a goal block to assist allocation.  If the goal is
1195a686cd89SMartin J. Bligh  * free, or there is a free block within 32 blocks of the goal, that block
1196a686cd89SMartin J. Bligh  * is allocated.  Otherwise a forward search is made for a free block; within
1197a686cd89SMartin J. Bligh  * each block group the search first looks for an entire free byte in the block
11981da177e4SLinus Torvalds  * bitmap, and then for any free bit if that fails.
11991da177e4SLinus Torvalds  * This function also updates quota and i_blocks field.
12001da177e4SLinus Torvalds  */
ext2_new_blocks(struct inode * inode,ext2_fsblk_t goal,unsigned long * count,int * errp,unsigned int flags)12011da177e4SLinus Torvalds ext2_fsblk_t ext2_new_blocks(struct inode *inode, ext2_fsblk_t goal,
12021da177e4SLinus Torvalds 		    unsigned long *count, int *errp, unsigned int flags)
12031da177e4SLinus Torvalds {
1204a686cd89SMartin J. Bligh 	struct buffer_head *bitmap_bh = NULL;
1205a686cd89SMartin J. Bligh 	struct buffer_head *gdp_bh;
12061da177e4SLinus Torvalds 	int group_no;
12071da177e4SLinus Torvalds 	int goal_group;
1208a686cd89SMartin J. Bligh 	ext2_grpblk_t grp_target_blk;	/* blockgroup relative goal block */
1209a686cd89SMartin J. Bligh 	ext2_grpblk_t grp_alloc_blk;	/* blockgroup-relative allocated block*/
1210a686cd89SMartin J. Bligh 	ext2_fsblk_t ret_block;		/* filesyetem-wide allocated block */
1211a686cd89SMartin J. Bligh 	int bgi;			/* blockgroup iteration index */
1212a686cd89SMartin J. Bligh 	int performed_allocation = 0;
1213a686cd89SMartin J. Bligh 	ext2_grpblk_t free_blocks;	/* number of free blocks in a group */
1214a686cd89SMartin J. Bligh 	struct super_block *sb;
1215a686cd89SMartin J. Bligh 	struct ext2_group_desc *gdp;
1216a686cd89SMartin J. Bligh 	struct ext2_super_block *es;
1217a686cd89SMartin J. Bligh 	struct ext2_sb_info *sbi;
1218a686cd89SMartin J. Bligh 	struct ext2_reserve_window_node *my_rsv = NULL;
1219a686cd89SMartin J. Bligh 	struct ext2_block_alloc_info *block_i;
1220a686cd89SMartin J. Bligh 	unsigned short windowsz = 0;
1221a686cd89SMartin J. Bligh 	unsigned long ngroups;
1222a686cd89SMartin J. Bligh 	unsigned long num = *count;
1223a686cd89SMartin J. Bligh 	int ret;
1224a686cd89SMartin J. Bligh 
1225a686cd89SMartin J. Bligh 	*errp = -ENOSPC;
12265dd4056dSChristoph Hellwig 	sb = inode->i_sb;
12271da177e4SLinus Torvalds 
1228a686cd89SMartin J. Bligh 	/*
1229a686cd89SMartin J. Bligh 	 * Check quota for allocation of this block.
12301da177e4SLinus Torvalds 	 */
1231a686cd89SMartin J. Bligh 	ret = dquot_alloc_block(inode, num);
1232a686cd89SMartin J. Bligh 	if (ret) {
1233a686cd89SMartin J. Bligh 		*errp = ret;
12345dd4056dSChristoph Hellwig 		return 0;
12355dd4056dSChristoph Hellwig 	}
12365dd4056dSChristoph Hellwig 
1237a686cd89SMartin J. Bligh 	sbi = EXT2_SB(sb);
1238a686cd89SMartin J. Bligh 	es = EXT2_SB(sb)->s_es;
1239a686cd89SMartin J. Bligh 	ext2_debug("goal=%lu.\n", goal);
1240a686cd89SMartin J. Bligh 	/*
1241a686cd89SMartin J. Bligh 	 * Allocate a block from reservation only when the filesystem is
1242a686cd89SMartin J. Bligh 	 * mounted with reservation(default,-o reservation), and it's a regular
1243a686cd89SMartin J. Bligh 	 * file, and the desired window size is greater than 0 (One could use
1244a686cd89SMartin J. Bligh 	 * ioctl command EXT2_IOC_SETRSVSZ to set the window size to 0 to turn
1245a686cd89SMartin J. Bligh 	 * off reservation on that particular file). Also do not use the
1246a686cd89SMartin J. Bligh 	 * reservation window if the caller asked us not to do it.
1247a686cd89SMartin J. Bligh 	 */
1248a686cd89SMartin J. Bligh 	block_i = EXT2_I(inode)->i_block_alloc_info;
1249a686cd89SMartin J. Bligh 	if (!(flags & EXT2_ALLOC_NORESERVE) && block_i) {
1250a686cd89SMartin J. Bligh 		windowsz = block_i->rsv_window_node.rsv_goal_size;
1251a686cd89SMartin J. Bligh 		if (windowsz > 0)
1252a686cd89SMartin J. Bligh 			my_rsv = &block_i->rsv_window_node;
1253a686cd89SMartin J. Bligh 	}
1254a686cd89SMartin J. Bligh 
1255a686cd89SMartin J. Bligh 	if (!ext2_has_free_blocks(sbi)) {
1256a686cd89SMartin J. Bligh 		*errp = -ENOSPC;
1257a686cd89SMartin J. Bligh 		goto out;
1258a686cd89SMartin J. Bligh 	}
1259a686cd89SMartin J. Bligh 
12601da177e4SLinus Torvalds 	/*
12611da177e4SLinus Torvalds 	 * First, test whether the goal block is free.
12621da177e4SLinus Torvalds 	 */
1263a686cd89SMartin J. Bligh 	if (goal < le32_to_cpu(es->s_first_data_block) ||
1264a686cd89SMartin J. Bligh 	    goal >= le32_to_cpu(es->s_blocks_count))
1265a686cd89SMartin J. Bligh 		goal = le32_to_cpu(es->s_first_data_block);
12661da177e4SLinus Torvalds 	group_no = (goal - le32_to_cpu(es->s_first_data_block)) /
12671da177e4SLinus Torvalds 			EXT2_BLOCKS_PER_GROUP(sb);
12681da177e4SLinus Torvalds 	goal_group = group_no;
1269a686cd89SMartin J. Bligh retry_alloc:
1270a686cd89SMartin J. Bligh 	gdp = ext2_get_group_desc(sb, group_no, &gdp_bh);
1271a686cd89SMartin J. Bligh 	if (!gdp)
1272a686cd89SMartin J. Bligh 		goto io_error;
1273a686cd89SMartin J. Bligh 
1274a686cd89SMartin J. Bligh 	free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
12751da177e4SLinus Torvalds 	/*
12761da177e4SLinus Torvalds 	 * if there is not enough free blocks to make a new resevation
1277a686cd89SMartin J. Bligh 	 * turn off reservation for this allocation
1278a686cd89SMartin J. Bligh 	 */
1279a686cd89SMartin J. Bligh 	if (my_rsv && (free_blocks < windowsz)
1280a686cd89SMartin J. Bligh 		&& (free_blocks > 0)
1281a686cd89SMartin J. Bligh 		&& (rsv_is_empty(&my_rsv->rsv_window)))
1282a686cd89SMartin J. Bligh 		my_rsv = NULL;
1283d707d31cSMingming Cao 
1284a686cd89SMartin J. Bligh 	if (free_blocks > 0) {
1285a686cd89SMartin J. Bligh 		grp_target_blk = ((goal - le32_to_cpu(es->s_first_data_block)) %
1286a686cd89SMartin J. Bligh 				EXT2_BLOCKS_PER_GROUP(sb));
1287a686cd89SMartin J. Bligh 		/*
1288a686cd89SMartin J. Bligh 		 * In case we retry allocation (due to fs reservation not
1289a686cd89SMartin J. Bligh 		 * working out or fs corruption), the bitmap_bh is non-null
1290ba1af2e4SChengguang Xu 		 * pointer and we have to release it before calling
1291ba1af2e4SChengguang Xu 		 * read_block_bitmap().
1292ba1af2e4SChengguang Xu 		 */
1293ba1af2e4SChengguang Xu 		brelse(bitmap_bh);
1294ba1af2e4SChengguang Xu 		bitmap_bh = read_block_bitmap(sb, group_no);
1295ba1af2e4SChengguang Xu 		if (!bitmap_bh)
1296ba1af2e4SChengguang Xu 			goto io_error;
12971da177e4SLinus Torvalds 		grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no,
12981da177e4SLinus Torvalds 					bitmap_bh, grp_target_blk,
12991da177e4SLinus Torvalds 					my_rsv, &num);
1300a686cd89SMartin J. Bligh 		if (grp_alloc_blk >= 0)
1301a686cd89SMartin J. Bligh 			goto allocated;
1302a686cd89SMartin J. Bligh 	}
1303a686cd89SMartin J. Bligh 
1304a686cd89SMartin J. Bligh 	ngroups = EXT2_SB(sb)->s_groups_count;
13051da177e4SLinus Torvalds 	smp_rmb();
13061da177e4SLinus Torvalds 
1307a686cd89SMartin J. Bligh 	/*
1308a686cd89SMartin J. Bligh 	 * Now search the rest of the groups.  We assume that
13091da177e4SLinus Torvalds 	 * group_no and gdp correctly point to the last group visited.
13101da177e4SLinus Torvalds 	 */
13111da177e4SLinus Torvalds 	for (bgi = 0; bgi < ngroups; bgi++) {
1312144704e5SAkinobu Mita 		group_no++;
13131da177e4SLinus Torvalds 		if (group_no >= ngroups)
1314a686cd89SMartin J. Bligh 			group_no = 0;
13151da177e4SLinus Torvalds 		gdp = ext2_get_group_desc(sb, group_no, &gdp_bh);
1316a686cd89SMartin J. Bligh 		if (!gdp)
13171da177e4SLinus Torvalds 			goto io_error;
1318a686cd89SMartin J. Bligh 
1319a686cd89SMartin J. Bligh 		free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
13201da177e4SLinus Torvalds 		/*
1321a686cd89SMartin J. Bligh 		 * skip this group (and avoid loading bitmap) if there
1322a686cd89SMartin J. Bligh 		 * are no free blocks
1323a686cd89SMartin J. Bligh 		 */
132446891532SJan Kara 		if (!free_blocks)
132546891532SJan Kara 			continue;
132646891532SJan Kara 		/*
132746891532SJan Kara 		 * skip this group if the number of
132846891532SJan Kara 		 * free blocks is less than half of the reservation
132946891532SJan Kara 		 * window size.
1330a686cd89SMartin J. Bligh 		 */
1331a686cd89SMartin J. Bligh 		if (my_rsv && (free_blocks <= (windowsz/2)))
1332a686cd89SMartin J. Bligh 			continue;
1333a686cd89SMartin J. Bligh 
1334d707d31cSMingming Cao 		brelse(bitmap_bh);
1335a686cd89SMartin J. Bligh 		bitmap_bh = read_block_bitmap(sb, group_no);
1336a686cd89SMartin J. Bligh 		if (!bitmap_bh)
13371da177e4SLinus Torvalds 			goto io_error;
13381da177e4SLinus Torvalds 		/*
13391da177e4SLinus Torvalds 		 * try to allocate block(s) from this group, without a goal(-1).
13401da177e4SLinus Torvalds 		 */
13411da177e4SLinus Torvalds 		grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no,
1342a686cd89SMartin J. Bligh 					bitmap_bh, -1, my_rsv, &num);
13431da177e4SLinus Torvalds 		if (grp_alloc_blk >= 0)
1344a686cd89SMartin J. Bligh 			goto allocated;
1345a686cd89SMartin J. Bligh 	}
1346a686cd89SMartin J. Bligh 	/*
1347a686cd89SMartin J. Bligh 	 * We may end up a bogus earlier ENOSPC error due to
13481da177e4SLinus Torvalds 	 * filesystem is "full" of reservations, but
13491da177e4SLinus Torvalds 	 * there maybe indeed free blocks available on disk
135025985edcSLucas De Marchi 	 * In this case, we just forget about the reservations
1351a686cd89SMartin J. Bligh 	 * just do block allocation as without reservations.
135225985edcSLucas De Marchi 	 */
1353a686cd89SMartin J. Bligh 	if (my_rsv) {
1354a686cd89SMartin J. Bligh 		my_rsv = NULL;
13551da177e4SLinus Torvalds 		windowsz = 0;
1356a686cd89SMartin J. Bligh 		group_no = goal_group;
1357a686cd89SMartin J. Bligh 		goto retry_alloc;
1358a686cd89SMartin J. Bligh 	}
1359a686cd89SMartin J. Bligh 	/* No space left on the device */
1360a686cd89SMartin J. Bligh 	*errp = -ENOSPC;
13611da177e4SLinus Torvalds 	goto out;
1362a686cd89SMartin J. Bligh 
1363a686cd89SMartin J. Bligh allocated:
1364a686cd89SMartin J. Bligh 
13651da177e4SLinus Torvalds 	ext2_debug("using block group %d(%d)\n",
1366a686cd89SMartin J. Bligh 			group_no, gdp->bg_free_blocks_count);
1367a686cd89SMartin J. Bligh 
13681da177e4SLinus Torvalds 	ret_block = grp_alloc_blk + ext2_group_first_block_no(sb, group_no);
1369a686cd89SMartin J. Bligh 
13701da177e4SLinus Torvalds 	if (in_range(le32_to_cpu(gdp->bg_block_bitmap), ret_block, num) ||
1371a686cd89SMartin J. Bligh 	    in_range(le32_to_cpu(gdp->bg_inode_bitmap), ret_block, num) ||
13721da177e4SLinus Torvalds 	    in_range(ret_block, le32_to_cpu(gdp->bg_inode_table),
1373a686cd89SMartin J. Bligh 		      EXT2_SB(sb)->s_itb_per_group) ||
1374a686cd89SMartin J. Bligh 	    in_range(ret_block + num - 1, le32_to_cpu(gdp->bg_inode_table),
1375a686cd89SMartin J. Bligh 		      EXT2_SB(sb)->s_itb_per_group)) {
1376a686cd89SMartin J. Bligh 		ext2_error(sb, "ext2_new_blocks",
1377a686cd89SMartin J. Bligh 			    "Allocating block in system zone - "
13787f0adaecSAneesh Kumar K.V 			    "blocks from "E2FSBLK", length %lu",
1379a686cd89SMartin J. Bligh 			    ret_block, num);
13801da177e4SLinus Torvalds 		/*
1381a686cd89SMartin J. Bligh 		 * ext2_try_to_allocate marked the blocks we allocated as in
1382a686cd89SMartin J. Bligh 		 * use.  So we may want to selectively mark some of the blocks
13838b915825SAneesh Kumar K.V 		 * as free
13848b915825SAneesh Kumar K.V 		 */
13858b915825SAneesh Kumar K.V 		num = *count;
13868b915825SAneesh Kumar K.V 		goto retry_alloc;
13878b915825SAneesh Kumar K.V 	}
1388158be76cSChengguang Xu 
13898b915825SAneesh Kumar K.V 	performed_allocation = 1;
13907f0adaecSAneesh Kumar K.V 
13911da177e4SLinus Torvalds 	if (ret_block + num - 1 >= le32_to_cpu(es->s_blocks_count)) {
1392a686cd89SMartin J. Bligh 		ext2_error(sb, "ext2_new_blocks",
1393a686cd89SMartin J. Bligh 			    "block("E2FSBLK") >= blocks count(%d) - "
1394a686cd89SMartin J. Bligh 			    "block_group = %d, es == %p ", ret_block,
1395a686cd89SMartin J. Bligh 			le32_to_cpu(es->s_blocks_count), group_no, es);
1396a686cd89SMartin J. Bligh 		goto out;
13971da177e4SLinus Torvalds 	}
13981da177e4SLinus Torvalds 
1399a686cd89SMartin J. Bligh 	group_adjust_blocks(sb, group_no, gdp, gdp_bh, -num);
14001da177e4SLinus Torvalds 	percpu_counter_sub(&sbi->s_freeblocks_counter, num);
14011da177e4SLinus Torvalds 
1402a686cd89SMartin J. Bligh 	mark_buffer_dirty(bitmap_bh);
1403a686cd89SMartin J. Bligh 	if (sb->s_flags & SB_SYNCHRONOUS)
14041da177e4SLinus Torvalds 		sync_dirty_buffer(bitmap_bh);
14051da177e4SLinus Torvalds 
14061751e8a6SLinus Torvalds 	*errp = 0;
14071da177e4SLinus Torvalds 	brelse(bitmap_bh);
14081da177e4SLinus Torvalds 	if (num < *count) {
1409a686cd89SMartin J. Bligh 		dquot_free_block_nodirty(inode, *count-num);
14101da177e4SLinus Torvalds 		mark_inode_dirty(inode);
14118e3dffc6SWang Shilong 		*count = num;
14123889717dSAl Viro 	}
14133889717dSAl Viro 	return ret_block;
1414a686cd89SMartin J. Bligh 
14158e3dffc6SWang Shilong io_error:
1416a686cd89SMartin J. Bligh 	*errp = -EIO;
14171da177e4SLinus Torvalds out:
14181da177e4SLinus Torvalds 	/*
1419a686cd89SMartin J. Bligh 	 * Undo the block allocation
1420a686cd89SMartin J. Bligh 	 */
1421a686cd89SMartin J. Bligh 	if (!performed_allocation) {
1422a686cd89SMartin J. Bligh 		dquot_free_block_nodirty(inode, *count);
1423a686cd89SMartin J. Bligh 		mark_inode_dirty(inode);
14243889717dSAl Viro 	}
14253889717dSAl Viro 	brelse(bitmap_bh);
14263889717dSAl Viro 	return 0;
14273889717dSAl Viro }
1428a686cd89SMartin J. Bligh 
1429a686cd89SMartin J. Bligh #ifdef EXT2FS_DEBUG
1430a686cd89SMartin J. Bligh 
ext2_count_free(struct buffer_head * map,unsigned int numchars)1431a686cd89SMartin J. Bligh unsigned long ext2_count_free(struct buffer_head *map, unsigned int numchars)
1432a686cd89SMartin J. Bligh {
1433a686cd89SMartin J. Bligh 	return numchars * BITS_PER_BYTE - memweight(map->b_data, numchars);
1434a686cd89SMartin J. Bligh }
1435a686cd89SMartin J. Bligh 
1436a686cd89SMartin J. Bligh #endif  /*  EXT2FS_DEBUG  */
14371da177e4SLinus Torvalds 
ext2_count_free_blocks(struct super_block * sb)14381da177e4SLinus Torvalds unsigned long ext2_count_free_blocks (struct super_block * sb)
143921730eedSValerie Henson {
144021730eedSValerie Henson 	struct ext2_group_desc * desc;
144121730eedSValerie Henson 	unsigned long desc_count = 0;
144221730eedSValerie Henson 	int i;
1443ecd0afa3SAkinobu Mita #ifdef EXT2FS_DEBUG
144421730eedSValerie Henson 	unsigned long bitmap_count, x;
144521730eedSValerie Henson 	struct ext2_super_block *es;
144621730eedSValerie Henson 
144721730eedSValerie Henson 	es = EXT2_SB(sb)->s_es;
14481da177e4SLinus Torvalds 	desc_count = 0;
14491da177e4SLinus Torvalds 	bitmap_count = 0;
14501da177e4SLinus Torvalds 	desc = NULL;
14511da177e4SLinus Torvalds 	for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
14521da177e4SLinus Torvalds 		struct buffer_head *bitmap_bh;
14531da177e4SLinus Torvalds 		desc = ext2_get_group_desc (sb, i, NULL);
14541da177e4SLinus Torvalds 		if (!desc)
14551da177e4SLinus Torvalds 			continue;
14561da177e4SLinus Torvalds 		desc_count += le16_to_cpu(desc->bg_free_blocks_count);
14571da177e4SLinus Torvalds 		bitmap_bh = read_block_bitmap(sb, i);
14581da177e4SLinus Torvalds 		if (!bitmap_bh)
14591da177e4SLinus Torvalds 			continue;
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 		x = ext2_count_free(bitmap_bh, sb->s_blocksize);
14621da177e4SLinus Torvalds 		printk ("group %d: stored = %d, counted = %lu\n",
14631da177e4SLinus Torvalds 			i, le16_to_cpu(desc->bg_free_blocks_count), x);
14641da177e4SLinus Torvalds 		bitmap_count += x;
14651da177e4SLinus Torvalds 		brelse(bitmap_bh);
14661da177e4SLinus Torvalds 	}
14671da177e4SLinus Torvalds 	printk("ext2_count_free_blocks: stored = %lu, computed = %lu, %lu\n",
14681da177e4SLinus Torvalds 		(long)le32_to_cpu(es->s_free_blocks_count),
14691da177e4SLinus Torvalds 		desc_count, bitmap_count);
14701da177e4SLinus Torvalds 	return bitmap_count;
14711da177e4SLinus Torvalds #else
14721da177e4SLinus Torvalds 	for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
14731da177e4SLinus Torvalds 		desc = ext2_get_group_desc(sb, i, NULL);
14741da177e4SLinus Torvalds 		if (!desc)
14751da177e4SLinus Torvalds 			continue;
14761da177e4SLinus Torvalds 		desc_count += le16_to_cpu(desc->bg_free_blocks_count);
14771da177e4SLinus Torvalds 	}
14781da177e4SLinus Torvalds 	return desc_count;
14791da177e4SLinus Torvalds #endif
14801da177e4SLinus Torvalds }
14811da177e4SLinus Torvalds 
test_root(int a,int b)14821da177e4SLinus Torvalds static inline int test_root(int a, int b)
14831da177e4SLinus Torvalds {
14841da177e4SLinus Torvalds 	int num = b;
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds 	while (a > num)
14871da177e4SLinus Torvalds 		num *= b;
14881da177e4SLinus Torvalds 	return num == a;
14891da177e4SLinus Torvalds }
14901da177e4SLinus Torvalds 
ext2_group_sparse(int group)14911da177e4SLinus Torvalds static int ext2_group_sparse(int group)
14921da177e4SLinus Torvalds {
14931da177e4SLinus Torvalds 	if (group <= 1)
14941da177e4SLinus Torvalds 		return 1;
14951da177e4SLinus Torvalds 	return (test_root(group, 3) || test_root(group, 5) ||
14961da177e4SLinus Torvalds 		test_root(group, 7));
14971da177e4SLinus Torvalds }
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds /**
15001da177e4SLinus Torvalds  *	ext2_bg_has_super - number of blocks used by the superblock in group
15011da177e4SLinus Torvalds  *	@sb: superblock for filesystem
15021da177e4SLinus Torvalds  *	@group: group number to check
15031da177e4SLinus Torvalds  *
15041da177e4SLinus Torvalds  *	Return the number of blocks used by the superblock (primary or backup)
15051da177e4SLinus Torvalds  *	in this group.  Currently this will be only 0 or 1.
15061da177e4SLinus Torvalds  */
ext2_bg_has_super(struct super_block * sb,int group)15071da177e4SLinus Torvalds int ext2_bg_has_super(struct super_block *sb, int group)
15081da177e4SLinus Torvalds {
15091da177e4SLinus Torvalds 	if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
15101da177e4SLinus Torvalds 	    !ext2_group_sparse(group))
15111da177e4SLinus Torvalds 		return 0;
15121da177e4SLinus Torvalds 	return 1;
15131da177e4SLinus Torvalds }
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds /**
15161da177e4SLinus Torvalds  *	ext2_bg_num_gdb - number of blocks used by the group table in group
15171da177e4SLinus Torvalds  *	@sb: superblock for filesystem
15181da177e4SLinus Torvalds  *	@group: group number to check
15191da177e4SLinus Torvalds  *
15201da177e4SLinus Torvalds  *	Return the number of blocks used by the group descriptor table
15211da177e4SLinus Torvalds  *	(primary or backup) in this group.  In the future there may be a
15221da177e4SLinus Torvalds  *	different number of descriptor blocks in each group.
15231da177e4SLinus Torvalds  */
ext2_bg_num_gdb(struct super_block * sb,int group)15241da177e4SLinus Torvalds unsigned long ext2_bg_num_gdb(struct super_block *sb, int group)
15251da177e4SLinus Torvalds {
15261da177e4SLinus Torvalds 	return ext2_bg_has_super(sb, group) ? EXT2_SB(sb)->s_gdb_count : 0;
15271da177e4SLinus Torvalds }
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds