xref: /linux/fs/ext2/balloc.c (revision 98366c20a275e957416e9516db5dcb7195b4e101)
1 /*
2  *  linux/fs/ext2/balloc.c
3  *
4  * Copyright (C) 1992, 1993, 1994, 1995
5  * Remy Card (card@masi.ibp.fr)
6  * Laboratoire MASI - Institut Blaise Pascal
7  * Universite Pierre et Marie Curie (Paris VI)
8  *
9  *  Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993
10  *  Big-endian to little-endian byte-swapping/bitmaps by
11  *        David S. Miller (davem@caip.rutgers.edu), 1995
12  */
13 
14 #include "ext2.h"
15 #include <linux/quotaops.h>
16 #include <linux/sched.h>
17 #include <linux/buffer_head.h>
18 #include <linux/capability.h>
19 
20 /*
21  * balloc.c contains the blocks allocation and deallocation routines
22  */
23 
24 /*
25  * The free blocks are managed by bitmaps.  A file system contains several
26  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
27  * block for inodes, N blocks for the inode table and data blocks.
28  *
29  * The file system contains group descriptors which are located after the
30  * super block.  Each descriptor contains the number of the bitmap block and
31  * the free blocks count in the block.  The descriptors are loaded in memory
32  * when a file system is mounted (see ext2_fill_super).
33  */
34 
35 
36 #define in_range(b, first, len)	((b) >= (first) && (b) <= (first) + (len) - 1)
37 
38 struct ext2_group_desc * ext2_get_group_desc(struct super_block * sb,
39 					     unsigned int block_group,
40 					     struct buffer_head ** bh)
41 {
42 	unsigned long group_desc;
43 	unsigned long offset;
44 	struct ext2_group_desc * desc;
45 	struct ext2_sb_info *sbi = EXT2_SB(sb);
46 
47 	if (block_group >= sbi->s_groups_count) {
48 		ext2_error (sb, "ext2_get_group_desc",
49 			    "block_group >= groups_count - "
50 			    "block_group = %d, groups_count = %lu",
51 			    block_group, sbi->s_groups_count);
52 
53 		return NULL;
54 	}
55 
56 	group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(sb);
57 	offset = block_group & (EXT2_DESC_PER_BLOCK(sb) - 1);
58 	if (!sbi->s_group_desc[group_desc]) {
59 		ext2_error (sb, "ext2_get_group_desc",
60 			    "Group descriptor not loaded - "
61 			    "block_group = %d, group_desc = %lu, desc = %lu",
62 			     block_group, group_desc, offset);
63 		return NULL;
64 	}
65 
66 	desc = (struct ext2_group_desc *) sbi->s_group_desc[group_desc]->b_data;
67 	if (bh)
68 		*bh = sbi->s_group_desc[group_desc];
69 	return desc + offset;
70 }
71 
72 static inline int
73 block_in_use(unsigned long block, struct super_block *sb, unsigned char *map)
74 {
75 	return ext2_test_bit ((block -
76 		le32_to_cpu(EXT2_SB(sb)->s_es->s_first_data_block)) %
77 			 EXT2_BLOCKS_PER_GROUP(sb), map);
78 }
79 
80 /*
81  * Read the bitmap for a given block_group, reading into the specified
82  * slot in the superblock's bitmap cache.
83  *
84  * Return buffer_head on success or NULL in case of failure.
85  */
86 static struct buffer_head *
87 read_block_bitmap(struct super_block *sb, unsigned int block_group)
88 {
89 	int i;
90 	struct ext2_group_desc * desc;
91 	struct buffer_head * bh = NULL;
92 	unsigned int bitmap_blk;
93 
94 	desc = ext2_get_group_desc (sb, block_group, NULL);
95 	if (!desc)
96 		return NULL;
97 	bitmap_blk = le32_to_cpu(desc->bg_block_bitmap);
98 	bh = sb_bread(sb, bitmap_blk);
99 	if (!bh)
100 		ext2_error (sb, __FUNCTION__,
101 			    "Cannot read block bitmap - "
102 			    "block_group = %d, block_bitmap = %u",
103 			    block_group, le32_to_cpu(desc->bg_block_bitmap));
104 
105 	/* check whether block bitmap block number is set */
106 	if (!block_in_use(bitmap_blk, sb, bh->b_data)) {
107 		/* bad block bitmap */
108 		goto error_out;
109 	}
110 	/* check whether the inode bitmap block number is set */
111 	bitmap_blk = le32_to_cpu(desc->bg_inode_bitmap);
112 	if (!block_in_use(bitmap_blk, sb, bh->b_data)) {
113 		/* bad block bitmap */
114 		goto error_out;
115 	}
116 	/* check whether the inode table block number is set */
117 	bitmap_blk = le32_to_cpu(desc->bg_inode_table);
118 	for (i = 0; i < EXT2_SB(sb)->s_itb_per_group; i++, bitmap_blk++) {
119 		if (!block_in_use(bitmap_blk, sb, bh->b_data)) {
120 			/* bad block bitmap */
121 			goto error_out;
122 		}
123 	}
124 
125 	return bh;
126 
127 error_out:
128 	brelse(bh);
129 	ext2_error(sb, __FUNCTION__,
130 			"Invalid block bitmap - "
131 			"block_group = %d, block = %u",
132 			block_group, bitmap_blk);
133 	return NULL;
134 }
135 
136 static void release_blocks(struct super_block *sb, int count)
137 {
138 	if (count) {
139 		struct ext2_sb_info *sbi = EXT2_SB(sb);
140 
141 		percpu_counter_add(&sbi->s_freeblocks_counter, count);
142 		sb->s_dirt = 1;
143 	}
144 }
145 
146 static void group_adjust_blocks(struct super_block *sb, int group_no,
147 	struct ext2_group_desc *desc, struct buffer_head *bh, int count)
148 {
149 	if (count) {
150 		struct ext2_sb_info *sbi = EXT2_SB(sb);
151 		unsigned free_blocks;
152 
153 		spin_lock(sb_bgl_lock(sbi, group_no));
154 		free_blocks = le16_to_cpu(desc->bg_free_blocks_count);
155 		desc->bg_free_blocks_count = cpu_to_le16(free_blocks + count);
156 		spin_unlock(sb_bgl_lock(sbi, group_no));
157 		sb->s_dirt = 1;
158 		mark_buffer_dirty(bh);
159 	}
160 }
161 
162 /*
163  * The reservation window structure operations
164  * --------------------------------------------
165  * Operations include:
166  * dump, find, add, remove, is_empty, find_next_reservable_window, etc.
167  *
168  * We use a red-black tree to represent per-filesystem reservation
169  * windows.
170  *
171  */
172 
173 /**
174  * __rsv_window_dump() -- Dump the filesystem block allocation reservation map
175  * @rb_root:		root of per-filesystem reservation rb tree
176  * @verbose:		verbose mode
177  * @fn:			function which wishes to dump the reservation map
178  *
179  * If verbose is turned on, it will print the whole block reservation
180  * windows(start, end). Otherwise, it will only print out the "bad" windows,
181  * those windows that overlap with their immediate neighbors.
182  */
183 #if 1
184 static void __rsv_window_dump(struct rb_root *root, int verbose,
185 			      const char *fn)
186 {
187 	struct rb_node *n;
188 	struct ext2_reserve_window_node *rsv, *prev;
189 	int bad;
190 
191 restart:
192 	n = rb_first(root);
193 	bad = 0;
194 	prev = NULL;
195 
196 	printk("Block Allocation Reservation Windows Map (%s):\n", fn);
197 	while (n) {
198 		rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
199 		if (verbose)
200 			printk("reservation window 0x%p "
201 				"start: %lu, end: %lu\n",
202 				rsv, rsv->rsv_start, rsv->rsv_end);
203 		if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) {
204 			printk("Bad reservation %p (start >= end)\n",
205 			       rsv);
206 			bad = 1;
207 		}
208 		if (prev && prev->rsv_end >= rsv->rsv_start) {
209 			printk("Bad reservation %p (prev->end >= start)\n",
210 			       rsv);
211 			bad = 1;
212 		}
213 		if (bad) {
214 			if (!verbose) {
215 				printk("Restarting reservation walk in verbose mode\n");
216 				verbose = 1;
217 				goto restart;
218 			}
219 		}
220 		n = rb_next(n);
221 		prev = rsv;
222 	}
223 	printk("Window map complete.\n");
224 	if (bad)
225 		BUG();
226 }
227 #define rsv_window_dump(root, verbose) \
228 	__rsv_window_dump((root), (verbose), __FUNCTION__)
229 #else
230 #define rsv_window_dump(root, verbose) do {} while (0)
231 #endif
232 
233 /**
234  * goal_in_my_reservation()
235  * @rsv:		inode's reservation window
236  * @grp_goal:		given goal block relative to the allocation block group
237  * @group:		the current allocation block group
238  * @sb:			filesystem super block
239  *
240  * Test if the given goal block (group relative) is within the file's
241  * own block reservation window range.
242  *
243  * If the reservation window is outside the goal allocation group, return 0;
244  * grp_goal (given goal block) could be -1, which means no specific
245  * goal block. In this case, always return 1.
246  * If the goal block is within the reservation window, return 1;
247  * otherwise, return 0;
248  */
249 static int
250 goal_in_my_reservation(struct ext2_reserve_window *rsv, ext2_grpblk_t grp_goal,
251 			unsigned int group, struct super_block * sb)
252 {
253 	ext2_fsblk_t group_first_block, group_last_block;
254 
255 	group_first_block = ext2_group_first_block_no(sb, group);
256 	group_last_block = group_first_block + EXT2_BLOCKS_PER_GROUP(sb) - 1;
257 
258 	if ((rsv->_rsv_start > group_last_block) ||
259 	    (rsv->_rsv_end < group_first_block))
260 		return 0;
261 	if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start)
262 		|| (grp_goal + group_first_block > rsv->_rsv_end)))
263 		return 0;
264 	return 1;
265 }
266 
267 /**
268  * search_reserve_window()
269  * @rb_root:		root of reservation tree
270  * @goal:		target allocation block
271  *
272  * Find the reserved window which includes the goal, or the previous one
273  * if the goal is not in any window.
274  * Returns NULL if there are no windows or if all windows start after the goal.
275  */
276 static struct ext2_reserve_window_node *
277 search_reserve_window(struct rb_root *root, ext2_fsblk_t goal)
278 {
279 	struct rb_node *n = root->rb_node;
280 	struct ext2_reserve_window_node *rsv;
281 
282 	if (!n)
283 		return NULL;
284 
285 	do {
286 		rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
287 
288 		if (goal < rsv->rsv_start)
289 			n = n->rb_left;
290 		else if (goal > rsv->rsv_end)
291 			n = n->rb_right;
292 		else
293 			return rsv;
294 	} while (n);
295 	/*
296 	 * We've fallen off the end of the tree: the goal wasn't inside
297 	 * any particular node.  OK, the previous node must be to one
298 	 * side of the interval containing the goal.  If it's the RHS,
299 	 * we need to back up one.
300 	 */
301 	if (rsv->rsv_start > goal) {
302 		n = rb_prev(&rsv->rsv_node);
303 		rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
304 	}
305 	return rsv;
306 }
307 
308 /*
309  * ext2_rsv_window_add() -- Insert a window to the block reservation rb tree.
310  * @sb:			super block
311  * @rsv:		reservation window to add
312  *
313  * Must be called with rsv_lock held.
314  */
315 void ext2_rsv_window_add(struct super_block *sb,
316 		    struct ext2_reserve_window_node *rsv)
317 {
318 	struct rb_root *root = &EXT2_SB(sb)->s_rsv_window_root;
319 	struct rb_node *node = &rsv->rsv_node;
320 	ext2_fsblk_t start = rsv->rsv_start;
321 
322 	struct rb_node ** p = &root->rb_node;
323 	struct rb_node * parent = NULL;
324 	struct ext2_reserve_window_node *this;
325 
326 	while (*p)
327 	{
328 		parent = *p;
329 		this = rb_entry(parent, struct ext2_reserve_window_node, rsv_node);
330 
331 		if (start < this->rsv_start)
332 			p = &(*p)->rb_left;
333 		else if (start > this->rsv_end)
334 			p = &(*p)->rb_right;
335 		else {
336 			rsv_window_dump(root, 1);
337 			BUG();
338 		}
339 	}
340 
341 	rb_link_node(node, parent, p);
342 	rb_insert_color(node, root);
343 }
344 
345 /**
346  * rsv_window_remove() -- unlink a window from the reservation rb tree
347  * @sb:			super block
348  * @rsv:		reservation window to remove
349  *
350  * Mark the block reservation window as not allocated, and unlink it
351  * from the filesystem reservation window rb tree. Must be called with
352  * rsv_lock held.
353  */
354 static void rsv_window_remove(struct super_block *sb,
355 			      struct ext2_reserve_window_node *rsv)
356 {
357 	rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
358 	rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
359 	rsv->rsv_alloc_hit = 0;
360 	rb_erase(&rsv->rsv_node, &EXT2_SB(sb)->s_rsv_window_root);
361 }
362 
363 /*
364  * rsv_is_empty() -- Check if the reservation window is allocated.
365  * @rsv:		given reservation window to check
366  *
367  * returns 1 if the end block is EXT2_RESERVE_WINDOW_NOT_ALLOCATED.
368  */
369 static inline int rsv_is_empty(struct ext2_reserve_window *rsv)
370 {
371 	/* a valid reservation end block could not be 0 */
372 	return (rsv->_rsv_end == EXT2_RESERVE_WINDOW_NOT_ALLOCATED);
373 }
374 
375 /**
376  * ext2_init_block_alloc_info()
377  * @inode:		file inode structure
378  *
379  * Allocate and initialize the  reservation window structure, and
380  * link the window to the ext2 inode structure at last
381  *
382  * The reservation window structure is only dynamically allocated
383  * and linked to ext2 inode the first time the open file
384  * needs a new block. So, before every ext2_new_block(s) call, for
385  * regular files, we should check whether the reservation window
386  * structure exists or not. In the latter case, this function is called.
387  * Fail to do so will result in block reservation being turned off for that
388  * open file.
389  *
390  * This function is called from ext2_get_blocks_handle(), also called
391  * when setting the reservation window size through ioctl before the file
392  * is open for write (needs block allocation).
393  *
394  * Needs truncate_mutex protection prior to calling this function.
395  */
396 void ext2_init_block_alloc_info(struct inode *inode)
397 {
398 	struct ext2_inode_info *ei = EXT2_I(inode);
399 	struct ext2_block_alloc_info *block_i = ei->i_block_alloc_info;
400 	struct super_block *sb = inode->i_sb;
401 
402 	block_i = kmalloc(sizeof(*block_i), GFP_NOFS);
403 	if (block_i) {
404 		struct ext2_reserve_window_node *rsv = &block_i->rsv_window_node;
405 
406 		rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
407 		rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
408 
409 	 	/*
410 		 * if filesystem is mounted with NORESERVATION, the goal
411 		 * reservation window size is set to zero to indicate
412 		 * block reservation is off
413 		 */
414 		if (!test_opt(sb, RESERVATION))
415 			rsv->rsv_goal_size = 0;
416 		else
417 			rsv->rsv_goal_size = EXT2_DEFAULT_RESERVE_BLOCKS;
418 		rsv->rsv_alloc_hit = 0;
419 		block_i->last_alloc_logical_block = 0;
420 		block_i->last_alloc_physical_block = 0;
421 	}
422 	ei->i_block_alloc_info = block_i;
423 }
424 
425 /**
426  * ext2_discard_reservation()
427  * @inode:		inode
428  *
429  * Discard(free) block reservation window on last file close, or truncate
430  * or at last iput().
431  *
432  * It is being called in three cases:
433  * 	ext2_release_file(): last writer closes the file
434  * 	ext2_clear_inode(): last iput(), when nobody links to this file.
435  * 	ext2_truncate(): when the block indirect map is about to change.
436  */
437 void ext2_discard_reservation(struct inode *inode)
438 {
439 	struct ext2_inode_info *ei = EXT2_I(inode);
440 	struct ext2_block_alloc_info *block_i = ei->i_block_alloc_info;
441 	struct ext2_reserve_window_node *rsv;
442 	spinlock_t *rsv_lock = &EXT2_SB(inode->i_sb)->s_rsv_window_lock;
443 
444 	if (!block_i)
445 		return;
446 
447 	rsv = &block_i->rsv_window_node;
448 	if (!rsv_is_empty(&rsv->rsv_window)) {
449 		spin_lock(rsv_lock);
450 		if (!rsv_is_empty(&rsv->rsv_window))
451 			rsv_window_remove(inode->i_sb, rsv);
452 		spin_unlock(rsv_lock);
453 	}
454 }
455 
456 /**
457  * ext2_free_blocks_sb() -- Free given blocks and update quota and i_blocks
458  * @inode:		inode
459  * @block:		start physcial block to free
460  * @count:		number of blocks to free
461  */
462 void ext2_free_blocks (struct inode * inode, unsigned long block,
463 		       unsigned long count)
464 {
465 	struct buffer_head *bitmap_bh = NULL;
466 	struct buffer_head * bh2;
467 	unsigned long block_group;
468 	unsigned long bit;
469 	unsigned long i;
470 	unsigned long overflow;
471 	struct super_block * sb = inode->i_sb;
472 	struct ext2_sb_info * sbi = EXT2_SB(sb);
473 	struct ext2_group_desc * desc;
474 	struct ext2_super_block * es = sbi->s_es;
475 	unsigned freed = 0, group_freed;
476 
477 	if (block < le32_to_cpu(es->s_first_data_block) ||
478 	    block + count < block ||
479 	    block + count > le32_to_cpu(es->s_blocks_count)) {
480 		ext2_error (sb, "ext2_free_blocks",
481 			    "Freeing blocks not in datazone - "
482 			    "block = %lu, count = %lu", block, count);
483 		goto error_return;
484 	}
485 
486 	ext2_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1);
487 
488 do_more:
489 	overflow = 0;
490 	block_group = (block - le32_to_cpu(es->s_first_data_block)) /
491 		      EXT2_BLOCKS_PER_GROUP(sb);
492 	bit = (block - le32_to_cpu(es->s_first_data_block)) %
493 		      EXT2_BLOCKS_PER_GROUP(sb);
494 	/*
495 	 * Check to see if we are freeing blocks across a group
496 	 * boundary.
497 	 */
498 	if (bit + count > EXT2_BLOCKS_PER_GROUP(sb)) {
499 		overflow = bit + count - EXT2_BLOCKS_PER_GROUP(sb);
500 		count -= overflow;
501 	}
502 	brelse(bitmap_bh);
503 	bitmap_bh = read_block_bitmap(sb, block_group);
504 	if (!bitmap_bh)
505 		goto error_return;
506 
507 	desc = ext2_get_group_desc (sb, block_group, &bh2);
508 	if (!desc)
509 		goto error_return;
510 
511 	if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) ||
512 	    in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) ||
513 	    in_range (block, le32_to_cpu(desc->bg_inode_table),
514 		      sbi->s_itb_per_group) ||
515 	    in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table),
516 		      sbi->s_itb_per_group))
517 		ext2_error (sb, "ext2_free_blocks",
518 			    "Freeing blocks in system zones - "
519 			    "Block = %lu, count = %lu",
520 			    block, count);
521 
522 	for (i = 0, group_freed = 0; i < count; i++) {
523 		if (!ext2_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
524 						bit + i, bitmap_bh->b_data)) {
525 			ext2_error(sb, __FUNCTION__,
526 				"bit already cleared for block %lu", block + i);
527 		} else {
528 			group_freed++;
529 		}
530 	}
531 
532 	mark_buffer_dirty(bitmap_bh);
533 	if (sb->s_flags & MS_SYNCHRONOUS)
534 		sync_dirty_buffer(bitmap_bh);
535 
536 	group_adjust_blocks(sb, block_group, desc, bh2, group_freed);
537 	freed += group_freed;
538 
539 	if (overflow) {
540 		block += count;
541 		count = overflow;
542 		goto do_more;
543 	}
544 error_return:
545 	brelse(bitmap_bh);
546 	release_blocks(sb, freed);
547 	DQUOT_FREE_BLOCK(inode, freed);
548 }
549 
550 /**
551  * bitmap_search_next_usable_block()
552  * @start:		the starting block (group relative) of the search
553  * @bh:			bufferhead contains the block group bitmap
554  * @maxblocks:		the ending block (group relative) of the reservation
555  *
556  * The bitmap search --- search forward through the actual bitmap on disk until
557  * we find a bit free.
558  */
559 static ext2_grpblk_t
560 bitmap_search_next_usable_block(ext2_grpblk_t start, struct buffer_head *bh,
561 					ext2_grpblk_t maxblocks)
562 {
563 	ext2_grpblk_t next;
564 
565 	next = ext2_find_next_zero_bit(bh->b_data, maxblocks, start);
566 	if (next >= maxblocks)
567 		return -1;
568 	return next;
569 }
570 
571 /**
572  * find_next_usable_block()
573  * @start:		the starting block (group relative) to find next
574  * 			allocatable block in bitmap.
575  * @bh:			bufferhead contains the block group bitmap
576  * @maxblocks:		the ending block (group relative) for the search
577  *
578  * Find an allocatable block in a bitmap.  We perform the "most
579  * appropriate allocation" algorithm of looking for a free block near
580  * the initial goal; then for a free byte somewhere in the bitmap;
581  * then for any free bit in the bitmap.
582  */
583 static ext2_grpblk_t
584 find_next_usable_block(int start, struct buffer_head *bh, int maxblocks)
585 {
586 	ext2_grpblk_t here, next;
587 	char *p, *r;
588 
589 	if (start > 0) {
590 		/*
591 		 * The goal was occupied; search forward for a free
592 		 * block within the next XX blocks.
593 		 *
594 		 * end_goal is more or less random, but it has to be
595 		 * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the
596 		 * next 64-bit boundary is simple..
597 		 */
598 		ext2_grpblk_t end_goal = (start + 63) & ~63;
599 		if (end_goal > maxblocks)
600 			end_goal = maxblocks;
601 		here = ext2_find_next_zero_bit(bh->b_data, end_goal, start);
602 		if (here < end_goal)
603 			return here;
604 		ext2_debug("Bit not found near goal\n");
605 	}
606 
607 	here = start;
608 	if (here < 0)
609 		here = 0;
610 
611 	p = ((char *)bh->b_data) + (here >> 3);
612 	r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3));
613 	next = (r - ((char *)bh->b_data)) << 3;
614 
615 	if (next < maxblocks && next >= here)
616 		return next;
617 
618 	here = bitmap_search_next_usable_block(here, bh, maxblocks);
619 	return here;
620 }
621 
622 /*
623  * ext2_try_to_allocate()
624  * @sb:			superblock
625  * @handle:		handle to this transaction
626  * @group:		given allocation block group
627  * @bitmap_bh:		bufferhead holds the block bitmap
628  * @grp_goal:		given target block within the group
629  * @count:		target number of blocks to allocate
630  * @my_rsv:		reservation window
631  *
632  * Attempt to allocate blocks within a give range. Set the range of allocation
633  * first, then find the first free bit(s) from the bitmap (within the range),
634  * and at last, allocate the blocks by claiming the found free bit as allocated.
635  *
636  * To set the range of this allocation:
637  * 	if there is a reservation window, only try to allocate block(s)
638  * 	from the file's own reservation window;
639  * 	Otherwise, the allocation range starts from the give goal block,
640  * 	ends at the block group's last block.
641  *
642  * If we failed to allocate the desired block then we may end up crossing to a
643  * new bitmap.
644  */
645 static int
646 ext2_try_to_allocate(struct super_block *sb, int group,
647 			struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal,
648 			unsigned long *count,
649 			struct ext2_reserve_window *my_rsv)
650 {
651 	ext2_fsblk_t group_first_block;
652        	ext2_grpblk_t start, end;
653 	unsigned long num = 0;
654 
655 	/* we do allocation within the reservation window if we have a window */
656 	if (my_rsv) {
657 		group_first_block = ext2_group_first_block_no(sb, group);
658 		if (my_rsv->_rsv_start >= group_first_block)
659 			start = my_rsv->_rsv_start - group_first_block;
660 		else
661 			/* reservation window cross group boundary */
662 			start = 0;
663 		end = my_rsv->_rsv_end - group_first_block + 1;
664 		if (end > EXT2_BLOCKS_PER_GROUP(sb))
665 			/* reservation window crosses group boundary */
666 			end = EXT2_BLOCKS_PER_GROUP(sb);
667 		if ((start <= grp_goal) && (grp_goal < end))
668 			start = grp_goal;
669 		else
670 			grp_goal = -1;
671 	} else {
672 		if (grp_goal > 0)
673 			start = grp_goal;
674 		else
675 			start = 0;
676 		end = EXT2_BLOCKS_PER_GROUP(sb);
677 	}
678 
679 	BUG_ON(start > EXT2_BLOCKS_PER_GROUP(sb));
680 
681 repeat:
682 	if (grp_goal < 0) {
683 		grp_goal = find_next_usable_block(start, bitmap_bh, end);
684 		if (grp_goal < 0)
685 			goto fail_access;
686 		if (!my_rsv) {
687 			int i;
688 
689 			for (i = 0; i < 7 && grp_goal > start &&
690 					!ext2_test_bit(grp_goal - 1,
691 					     		bitmap_bh->b_data);
692 			     		i++, grp_goal--)
693 				;
694 		}
695 	}
696 	start = grp_goal;
697 
698 	if (ext2_set_bit_atomic(sb_bgl_lock(EXT2_SB(sb), group), grp_goal,
699 			       				bitmap_bh->b_data)) {
700 		/*
701 		 * The block was allocated by another thread, or it was
702 		 * allocated and then freed by another thread
703 		 */
704 		start++;
705 		grp_goal++;
706 		if (start >= end)
707 			goto fail_access;
708 		goto repeat;
709 	}
710 	num++;
711 	grp_goal++;
712 	while (num < *count && grp_goal < end
713 		&& !ext2_set_bit_atomic(sb_bgl_lock(EXT2_SB(sb), group),
714 					grp_goal, bitmap_bh->b_data)) {
715 		num++;
716 		grp_goal++;
717 	}
718 	*count = num;
719 	return grp_goal - num;
720 fail_access:
721 	*count = num;
722 	return -1;
723 }
724 
725 /**
726  * 	find_next_reservable_window():
727  *		find a reservable space within the given range.
728  *		It does not allocate the reservation window for now:
729  *		alloc_new_reservation() will do the work later.
730  *
731  * 	@search_head: the head of the searching list;
732  *		This is not necessarily the list head of the whole filesystem
733  *
734  *		We have both head and start_block to assist the search
735  *		for the reservable space. The list starts from head,
736  *		but we will shift to the place where start_block is,
737  *		then start from there, when looking for a reservable space.
738  *
739  * 	@size: the target new reservation window size
740  *
741  * 	@group_first_block: the first block we consider to start
742  *			the real search from
743  *
744  * 	@last_block:
745  *		the maximum block number that our goal reservable space
746  *		could start from. This is normally the last block in this
747  *		group. The search will end when we found the start of next
748  *		possible reservable space is out of this boundary.
749  *		This could handle the cross boundary reservation window
750  *		request.
751  *
752  * 	basically we search from the given range, rather than the whole
753  * 	reservation double linked list, (start_block, last_block)
754  * 	to find a free region that is of my size and has not
755  * 	been reserved.
756  *
757  */
758 static int find_next_reservable_window(
759 				struct ext2_reserve_window_node *search_head,
760 				struct ext2_reserve_window_node *my_rsv,
761 				struct super_block * sb,
762 				ext2_fsblk_t start_block,
763 				ext2_fsblk_t last_block)
764 {
765 	struct rb_node *next;
766 	struct ext2_reserve_window_node *rsv, *prev;
767 	ext2_fsblk_t cur;
768 	int size = my_rsv->rsv_goal_size;
769 
770 	/* TODO: make the start of the reservation window byte-aligned */
771 	/* cur = *start_block & ~7;*/
772 	cur = start_block;
773 	rsv = search_head;
774 	if (!rsv)
775 		return -1;
776 
777 	while (1) {
778 		if (cur <= rsv->rsv_end)
779 			cur = rsv->rsv_end + 1;
780 
781 		/* TODO?
782 		 * in the case we could not find a reservable space
783 		 * that is what is expected, during the re-search, we could
784 		 * remember what's the largest reservable space we could have
785 		 * and return that one.
786 		 *
787 		 * For now it will fail if we could not find the reservable
788 		 * space with expected-size (or more)...
789 		 */
790 		if (cur > last_block)
791 			return -1;		/* fail */
792 
793 		prev = rsv;
794 		next = rb_next(&rsv->rsv_node);
795 		rsv = rb_entry(next,struct ext2_reserve_window_node,rsv_node);
796 
797 		/*
798 		 * Reached the last reservation, we can just append to the
799 		 * previous one.
800 		 */
801 		if (!next)
802 			break;
803 
804 		if (cur + size <= rsv->rsv_start) {
805 			/*
806 			 * Found a reserveable space big enough.  We could
807 			 * have a reservation across the group boundary here
808 		 	 */
809 			break;
810 		}
811 	}
812 	/*
813 	 * we come here either :
814 	 * when we reach the end of the whole list,
815 	 * and there is empty reservable space after last entry in the list.
816 	 * append it to the end of the list.
817 	 *
818 	 * or we found one reservable space in the middle of the list,
819 	 * return the reservation window that we could append to.
820 	 * succeed.
821 	 */
822 
823 	if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window)))
824 		rsv_window_remove(sb, my_rsv);
825 
826 	/*
827 	 * Let's book the whole avaliable window for now.  We will check the
828 	 * disk bitmap later and then, if there are free blocks then we adjust
829 	 * the window size if it's larger than requested.
830 	 * Otherwise, we will remove this node from the tree next time
831 	 * call find_next_reservable_window.
832 	 */
833 	my_rsv->rsv_start = cur;
834 	my_rsv->rsv_end = cur + size - 1;
835 	my_rsv->rsv_alloc_hit = 0;
836 
837 	if (prev != my_rsv)
838 		ext2_rsv_window_add(sb, my_rsv);
839 
840 	return 0;
841 }
842 
843 /**
844  * 	alloc_new_reservation()--allocate a new reservation window
845  *
846  *		To make a new reservation, we search part of the filesystem
847  *		reservation list (the list that inside the group). We try to
848  *		allocate a new reservation window near the allocation goal,
849  *		or the beginning of the group, if there is no goal.
850  *
851  *		We first find a reservable space after the goal, then from
852  *		there, we check the bitmap for the first free block after
853  *		it. If there is no free block until the end of group, then the
854  *		whole group is full, we failed. Otherwise, check if the free
855  *		block is inside the expected reservable space, if so, we
856  *		succeed.
857  *		If the first free block is outside the reservable space, then
858  *		start from the first free block, we search for next available
859  *		space, and go on.
860  *
861  *	on succeed, a new reservation will be found and inserted into the list
862  *	It contains at least one free block, and it does not overlap with other
863  *	reservation windows.
864  *
865  *	failed: we failed to find a reservation window in this group
866  *
867  *	@rsv: the reservation
868  *
869  *	@grp_goal: The goal (group-relative).  It is where the search for a
870  *		free reservable space should start from.
871  *		if we have a goal(goal >0 ), then start from there,
872  *		no goal(goal = -1), we start from the first block
873  *		of the group.
874  *
875  *	@sb: the super block
876  *	@group: the group we are trying to allocate in
877  *	@bitmap_bh: the block group block bitmap
878  *
879  */
880 static int alloc_new_reservation(struct ext2_reserve_window_node *my_rsv,
881 		ext2_grpblk_t grp_goal, struct super_block *sb,
882 		unsigned int group, struct buffer_head *bitmap_bh)
883 {
884 	struct ext2_reserve_window_node *search_head;
885 	ext2_fsblk_t group_first_block, group_end_block, start_block;
886 	ext2_grpblk_t first_free_block;
887 	struct rb_root *fs_rsv_root = &EXT2_SB(sb)->s_rsv_window_root;
888 	unsigned long size;
889 	int ret;
890 	spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock;
891 
892 	group_first_block = ext2_group_first_block_no(sb, group);
893 	group_end_block = group_first_block + (EXT2_BLOCKS_PER_GROUP(sb) - 1);
894 
895 	if (grp_goal < 0)
896 		start_block = group_first_block;
897 	else
898 		start_block = grp_goal + group_first_block;
899 
900 	size = my_rsv->rsv_goal_size;
901 
902 	if (!rsv_is_empty(&my_rsv->rsv_window)) {
903 		/*
904 		 * if the old reservation is cross group boundary
905 		 * and if the goal is inside the old reservation window,
906 		 * we will come here when we just failed to allocate from
907 		 * the first part of the window. We still have another part
908 		 * that belongs to the next group. In this case, there is no
909 		 * point to discard our window and try to allocate a new one
910 		 * in this group(which will fail). we should
911 		 * keep the reservation window, just simply move on.
912 		 *
913 		 * Maybe we could shift the start block of the reservation
914 		 * window to the first block of next group.
915 		 */
916 
917 		if ((my_rsv->rsv_start <= group_end_block) &&
918 				(my_rsv->rsv_end > group_end_block) &&
919 				(start_block >= my_rsv->rsv_start))
920 			return -1;
921 
922 		if ((my_rsv->rsv_alloc_hit >
923 		     (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) {
924 			/*
925 			 * if the previously allocation hit ratio is
926 			 * greater than 1/2, then we double the size of
927 			 * the reservation window the next time,
928 			 * otherwise we keep the same size window
929 			 */
930 			size = size * 2;
931 			if (size > EXT2_MAX_RESERVE_BLOCKS)
932 				size = EXT2_MAX_RESERVE_BLOCKS;
933 			my_rsv->rsv_goal_size= size;
934 		}
935 	}
936 
937 	spin_lock(rsv_lock);
938 	/*
939 	 * shift the search start to the window near the goal block
940 	 */
941 	search_head = search_reserve_window(fs_rsv_root, start_block);
942 
943 	/*
944 	 * find_next_reservable_window() simply finds a reservable window
945 	 * inside the given range(start_block, group_end_block).
946 	 *
947 	 * To make sure the reservation window has a free bit inside it, we
948 	 * need to check the bitmap after we found a reservable window.
949 	 */
950 retry:
951 	ret = find_next_reservable_window(search_head, my_rsv, sb,
952 						start_block, group_end_block);
953 
954 	if (ret == -1) {
955 		if (!rsv_is_empty(&my_rsv->rsv_window))
956 			rsv_window_remove(sb, my_rsv);
957 		spin_unlock(rsv_lock);
958 		return -1;
959 	}
960 
961 	/*
962 	 * On success, find_next_reservable_window() returns the
963 	 * reservation window where there is a reservable space after it.
964 	 * Before we reserve this reservable space, we need
965 	 * to make sure there is at least a free block inside this region.
966 	 *
967 	 * Search the first free bit on the block bitmap.  Search starts from
968 	 * the start block of the reservable space we just found.
969 	 */
970 	spin_unlock(rsv_lock);
971 	first_free_block = bitmap_search_next_usable_block(
972 			my_rsv->rsv_start - group_first_block,
973 			bitmap_bh, group_end_block - group_first_block + 1);
974 
975 	if (first_free_block < 0) {
976 		/*
977 		 * no free block left on the bitmap, no point
978 		 * to reserve the space. return failed.
979 		 */
980 		spin_lock(rsv_lock);
981 		if (!rsv_is_empty(&my_rsv->rsv_window))
982 			rsv_window_remove(sb, my_rsv);
983 		spin_unlock(rsv_lock);
984 		return -1;		/* failed */
985 	}
986 
987 	start_block = first_free_block + group_first_block;
988 	/*
989 	 * check if the first free block is within the
990 	 * free space we just reserved
991 	 */
992 	if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end)
993 		return 0;		/* success */
994 	/*
995 	 * if the first free bit we found is out of the reservable space
996 	 * continue search for next reservable space,
997 	 * start from where the free block is,
998 	 * we also shift the list head to where we stopped last time
999 	 */
1000 	search_head = my_rsv;
1001 	spin_lock(rsv_lock);
1002 	goto retry;
1003 }
1004 
1005 /**
1006  * try_to_extend_reservation()
1007  * @my_rsv:		given reservation window
1008  * @sb:			super block
1009  * @size:		the delta to extend
1010  *
1011  * Attempt to expand the reservation window large enough to have
1012  * required number of free blocks
1013  *
1014  * Since ext2_try_to_allocate() will always allocate blocks within
1015  * the reservation window range, if the window size is too small,
1016  * multiple blocks allocation has to stop at the end of the reservation
1017  * window. To make this more efficient, given the total number of
1018  * blocks needed and the current size of the window, we try to
1019  * expand the reservation window size if necessary on a best-effort
1020  * basis before ext2_new_blocks() tries to allocate blocks.
1021  */
1022 static void try_to_extend_reservation(struct ext2_reserve_window_node *my_rsv,
1023 			struct super_block *sb, int size)
1024 {
1025 	struct ext2_reserve_window_node *next_rsv;
1026 	struct rb_node *next;
1027 	spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock;
1028 
1029 	if (!spin_trylock(rsv_lock))
1030 		return;
1031 
1032 	next = rb_next(&my_rsv->rsv_node);
1033 
1034 	if (!next)
1035 		my_rsv->rsv_end += size;
1036 	else {
1037 		next_rsv = rb_entry(next, struct ext2_reserve_window_node, rsv_node);
1038 
1039 		if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size)
1040 			my_rsv->rsv_end += size;
1041 		else
1042 			my_rsv->rsv_end = next_rsv->rsv_start - 1;
1043 	}
1044 	spin_unlock(rsv_lock);
1045 }
1046 
1047 /**
1048  * ext2_try_to_allocate_with_rsv()
1049  * @sb:			superblock
1050  * @group:		given allocation block group
1051  * @bitmap_bh:		bufferhead holds the block bitmap
1052  * @grp_goal:		given target block within the group
1053  * @count:		target number of blocks to allocate
1054  * @my_rsv:		reservation window
1055  *
1056  * This is the main function used to allocate a new block and its reservation
1057  * window.
1058  *
1059  * Each time when a new block allocation is need, first try to allocate from
1060  * its own reservation.  If it does not have a reservation window, instead of
1061  * looking for a free bit on bitmap first, then look up the reservation list to
1062  * see if it is inside somebody else's reservation window, we try to allocate a
1063  * reservation window for it starting from the goal first. Then do the block
1064  * allocation within the reservation window.
1065  *
1066  * This will avoid keeping on searching the reservation list again and
1067  * again when somebody is looking for a free block (without
1068  * reservation), and there are lots of free blocks, but they are all
1069  * being reserved.
1070  *
1071  * We use a red-black tree for the per-filesystem reservation list.
1072  */
1073 static ext2_grpblk_t
1074 ext2_try_to_allocate_with_rsv(struct super_block *sb, unsigned int group,
1075 			struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal,
1076 			struct ext2_reserve_window_node * my_rsv,
1077 			unsigned long *count)
1078 {
1079 	ext2_fsblk_t group_first_block, group_last_block;
1080 	ext2_grpblk_t ret = 0;
1081 	unsigned long num = *count;
1082 
1083 	/*
1084 	 * we don't deal with reservation when
1085 	 * filesystem is mounted without reservation
1086 	 * or the file is not a regular file
1087 	 * or last attempt to allocate a block with reservation turned on failed
1088 	 */
1089 	if (my_rsv == NULL) {
1090 		return ext2_try_to_allocate(sb, group, bitmap_bh,
1091 						grp_goal, count, NULL);
1092 	}
1093 	/*
1094 	 * grp_goal is a group relative block number (if there is a goal)
1095 	 * 0 <= grp_goal < EXT2_BLOCKS_PER_GROUP(sb)
1096 	 * first block is a filesystem wide block number
1097 	 * first block is the block number of the first block in this group
1098 	 */
1099 	group_first_block = ext2_group_first_block_no(sb, group);
1100 	group_last_block = group_first_block + (EXT2_BLOCKS_PER_GROUP(sb) - 1);
1101 
1102 	/*
1103 	 * Basically we will allocate a new block from inode's reservation
1104 	 * window.
1105 	 *
1106 	 * We need to allocate a new reservation window, if:
1107 	 * a) inode does not have a reservation window; or
1108 	 * b) last attempt to allocate a block from existing reservation
1109 	 *    failed; or
1110 	 * c) we come here with a goal and with a reservation window
1111 	 *
1112 	 * We do not need to allocate a new reservation window if we come here
1113 	 * at the beginning with a goal and the goal is inside the window, or
1114 	 * we don't have a goal but already have a reservation window.
1115 	 * then we could go to allocate from the reservation window directly.
1116 	 */
1117 	while (1) {
1118 		if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) ||
1119 			!goal_in_my_reservation(&my_rsv->rsv_window,
1120 						grp_goal, group, sb)) {
1121 			if (my_rsv->rsv_goal_size < *count)
1122 				my_rsv->rsv_goal_size = *count;
1123 			ret = alloc_new_reservation(my_rsv, grp_goal, sb,
1124 							group, bitmap_bh);
1125 			if (ret < 0)
1126 				break;			/* failed */
1127 
1128 			if (!goal_in_my_reservation(&my_rsv->rsv_window,
1129 							grp_goal, group, sb))
1130 				grp_goal = -1;
1131 		} else if (grp_goal >= 0) {
1132 			int curr = my_rsv->rsv_end -
1133 					(grp_goal + group_first_block) + 1;
1134 
1135 			if (curr < *count)
1136 				try_to_extend_reservation(my_rsv, sb,
1137 							*count - curr);
1138 		}
1139 
1140 		if ((my_rsv->rsv_start > group_last_block) ||
1141 				(my_rsv->rsv_end < group_first_block)) {
1142 			rsv_window_dump(&EXT2_SB(sb)->s_rsv_window_root, 1);
1143 			BUG();
1144 		}
1145 		ret = ext2_try_to_allocate(sb, group, bitmap_bh, grp_goal,
1146 					   &num, &my_rsv->rsv_window);
1147 		if (ret >= 0) {
1148 			my_rsv->rsv_alloc_hit += num;
1149 			*count = num;
1150 			break;				/* succeed */
1151 		}
1152 		num = *count;
1153 	}
1154 	return ret;
1155 }
1156 
1157 /**
1158  * ext2_has_free_blocks()
1159  * @sbi:		in-core super block structure.
1160  *
1161  * Check if filesystem has at least 1 free block available for allocation.
1162  */
1163 static int ext2_has_free_blocks(struct ext2_sb_info *sbi)
1164 {
1165 	ext2_fsblk_t free_blocks, root_blocks;
1166 
1167 	free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
1168 	root_blocks = le32_to_cpu(sbi->s_es->s_r_blocks_count);
1169 	if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) &&
1170 		sbi->s_resuid != current->fsuid &&
1171 		(sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) {
1172 		return 0;
1173 	}
1174 	return 1;
1175 }
1176 
1177 /*
1178  * ext2_new_blocks() -- core block(s) allocation function
1179  * @inode:		file inode
1180  * @goal:		given target block(filesystem wide)
1181  * @count:		target number of blocks to allocate
1182  * @errp:		error code
1183  *
1184  * ext2_new_blocks uses a goal block to assist allocation.  If the goal is
1185  * free, or there is a free block within 32 blocks of the goal, that block
1186  * is allocated.  Otherwise a forward search is made for a free block; within
1187  * each block group the search first looks for an entire free byte in the block
1188  * bitmap, and then for any free bit if that fails.
1189  * This function also updates quota and i_blocks field.
1190  */
1191 ext2_fsblk_t ext2_new_blocks(struct inode *inode, ext2_fsblk_t goal,
1192 		    unsigned long *count, int *errp)
1193 {
1194 	struct buffer_head *bitmap_bh = NULL;
1195 	struct buffer_head *gdp_bh;
1196 	int group_no;
1197 	int goal_group;
1198 	ext2_grpblk_t grp_target_blk;	/* blockgroup relative goal block */
1199 	ext2_grpblk_t grp_alloc_blk;	/* blockgroup-relative allocated block*/
1200 	ext2_fsblk_t ret_block;		/* filesyetem-wide allocated block */
1201 	int bgi;			/* blockgroup iteration index */
1202 	int performed_allocation = 0;
1203 	ext2_grpblk_t free_blocks;	/* number of free blocks in a group */
1204 	struct super_block *sb;
1205 	struct ext2_group_desc *gdp;
1206 	struct ext2_super_block *es;
1207 	struct ext2_sb_info *sbi;
1208 	struct ext2_reserve_window_node *my_rsv = NULL;
1209 	struct ext2_block_alloc_info *block_i;
1210 	unsigned short windowsz = 0;
1211 	unsigned long ngroups;
1212 	unsigned long num = *count;
1213 
1214 	*errp = -ENOSPC;
1215 	sb = inode->i_sb;
1216 	if (!sb) {
1217 		printk("ext2_new_blocks: nonexistent device");
1218 		return 0;
1219 	}
1220 
1221 	/*
1222 	 * Check quota for allocation of this block.
1223 	 */
1224 	if (DQUOT_ALLOC_BLOCK(inode, num)) {
1225 		*errp = -EDQUOT;
1226 		return 0;
1227 	}
1228 
1229 	sbi = EXT2_SB(sb);
1230 	es = EXT2_SB(sb)->s_es;
1231 	ext2_debug("goal=%lu.\n", goal);
1232 	/*
1233 	 * Allocate a block from reservation only when
1234 	 * filesystem is mounted with reservation(default,-o reservation), and
1235 	 * it's a regular file, and
1236 	 * the desired window size is greater than 0 (One could use ioctl
1237 	 * command EXT2_IOC_SETRSVSZ to set the window size to 0 to turn off
1238 	 * reservation on that particular file)
1239 	 */
1240 	block_i = EXT2_I(inode)->i_block_alloc_info;
1241 	if (block_i) {
1242 		windowsz = block_i->rsv_window_node.rsv_goal_size;
1243 		if (windowsz > 0)
1244 			my_rsv = &block_i->rsv_window_node;
1245 	}
1246 
1247 	if (!ext2_has_free_blocks(sbi)) {
1248 		*errp = -ENOSPC;
1249 		goto out;
1250 	}
1251 
1252 	/*
1253 	 * First, test whether the goal block is free.
1254 	 */
1255 	if (goal < le32_to_cpu(es->s_first_data_block) ||
1256 	    goal >= le32_to_cpu(es->s_blocks_count))
1257 		goal = le32_to_cpu(es->s_first_data_block);
1258 	group_no = (goal - le32_to_cpu(es->s_first_data_block)) /
1259 			EXT2_BLOCKS_PER_GROUP(sb);
1260 	goal_group = group_no;
1261 retry_alloc:
1262 	gdp = ext2_get_group_desc(sb, group_no, &gdp_bh);
1263 	if (!gdp)
1264 		goto io_error;
1265 
1266 	free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
1267 	/*
1268 	 * if there is not enough free blocks to make a new resevation
1269 	 * turn off reservation for this allocation
1270 	 */
1271 	if (my_rsv && (free_blocks < windowsz)
1272 		&& (rsv_is_empty(&my_rsv->rsv_window)))
1273 		my_rsv = NULL;
1274 
1275 	if (free_blocks > 0) {
1276 		grp_target_blk = ((goal - le32_to_cpu(es->s_first_data_block)) %
1277 				EXT2_BLOCKS_PER_GROUP(sb));
1278 		bitmap_bh = read_block_bitmap(sb, group_no);
1279 		if (!bitmap_bh)
1280 			goto io_error;
1281 		grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no,
1282 					bitmap_bh, grp_target_blk,
1283 					my_rsv, &num);
1284 		if (grp_alloc_blk >= 0)
1285 			goto allocated;
1286 	}
1287 
1288 	ngroups = EXT2_SB(sb)->s_groups_count;
1289 	smp_rmb();
1290 
1291 	/*
1292 	 * Now search the rest of the groups.  We assume that
1293 	 * i and gdp correctly point to the last group visited.
1294 	 */
1295 	for (bgi = 0; bgi < ngroups; bgi++) {
1296 		group_no++;
1297 		if (group_no >= ngroups)
1298 			group_no = 0;
1299 		gdp = ext2_get_group_desc(sb, group_no, &gdp_bh);
1300 		if (!gdp)
1301 			goto io_error;
1302 
1303 		free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
1304 		/*
1305 		 * skip this group if the number of
1306 		 * free blocks is less than half of the reservation
1307 		 * window size.
1308 		 */
1309 		if (free_blocks <= (windowsz/2))
1310 			continue;
1311 
1312 		brelse(bitmap_bh);
1313 		bitmap_bh = read_block_bitmap(sb, group_no);
1314 		if (!bitmap_bh)
1315 			goto io_error;
1316 		/*
1317 		 * try to allocate block(s) from this group, without a goal(-1).
1318 		 */
1319 		grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no,
1320 					bitmap_bh, -1, my_rsv, &num);
1321 		if (grp_alloc_blk >= 0)
1322 			goto allocated;
1323 	}
1324 	/*
1325 	 * We may end up a bogus ealier ENOSPC error due to
1326 	 * filesystem is "full" of reservations, but
1327 	 * there maybe indeed free blocks avaliable on disk
1328 	 * In this case, we just forget about the reservations
1329 	 * just do block allocation as without reservations.
1330 	 */
1331 	if (my_rsv) {
1332 		my_rsv = NULL;
1333 		windowsz = 0;
1334 		group_no = goal_group;
1335 		goto retry_alloc;
1336 	}
1337 	/* No space left on the device */
1338 	*errp = -ENOSPC;
1339 	goto out;
1340 
1341 allocated:
1342 
1343 	ext2_debug("using block group %d(%d)\n",
1344 			group_no, gdp->bg_free_blocks_count);
1345 
1346 	ret_block = grp_alloc_blk + ext2_group_first_block_no(sb, group_no);
1347 
1348 	if (in_range(le32_to_cpu(gdp->bg_block_bitmap), ret_block, num) ||
1349 	    in_range(le32_to_cpu(gdp->bg_inode_bitmap), ret_block, num) ||
1350 	    in_range(ret_block, le32_to_cpu(gdp->bg_inode_table),
1351 		      EXT2_SB(sb)->s_itb_per_group) ||
1352 	    in_range(ret_block + num - 1, le32_to_cpu(gdp->bg_inode_table),
1353 		      EXT2_SB(sb)->s_itb_per_group))
1354 		ext2_error(sb, "ext2_new_blocks",
1355 			    "Allocating block in system zone - "
1356 			    "blocks from "E2FSBLK", length %lu",
1357 			    ret_block, num);
1358 
1359 	performed_allocation = 1;
1360 
1361 	if (ret_block + num - 1 >= le32_to_cpu(es->s_blocks_count)) {
1362 		ext2_error(sb, "ext2_new_blocks",
1363 			    "block("E2FSBLK") >= blocks count(%d) - "
1364 			    "block_group = %d, es == %p ", ret_block,
1365 			le32_to_cpu(es->s_blocks_count), group_no, es);
1366 		goto out;
1367 	}
1368 
1369 	group_adjust_blocks(sb, group_no, gdp, gdp_bh, -num);
1370 	percpu_counter_sub(&sbi->s_freeblocks_counter, num);
1371 
1372 	mark_buffer_dirty(bitmap_bh);
1373 	if (sb->s_flags & MS_SYNCHRONOUS)
1374 		sync_dirty_buffer(bitmap_bh);
1375 
1376 	*errp = 0;
1377 	brelse(bitmap_bh);
1378 	DQUOT_FREE_BLOCK(inode, *count-num);
1379 	*count = num;
1380 	return ret_block;
1381 
1382 io_error:
1383 	*errp = -EIO;
1384 out:
1385 	/*
1386 	 * Undo the block allocation
1387 	 */
1388 	if (!performed_allocation)
1389 		DQUOT_FREE_BLOCK(inode, *count);
1390 	brelse(bitmap_bh);
1391 	return 0;
1392 }
1393 
1394 ext2_fsblk_t ext2_new_block(struct inode *inode, unsigned long goal, int *errp)
1395 {
1396 	unsigned long count = 1;
1397 
1398 	return ext2_new_blocks(inode, goal, &count, errp);
1399 }
1400 
1401 #ifdef EXT2FS_DEBUG
1402 
1403 static const int nibblemap[] = {4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0};
1404 
1405 unsigned long ext2_count_free (struct buffer_head * map, unsigned int numchars)
1406 {
1407 	unsigned int i;
1408 	unsigned long sum = 0;
1409 
1410 	if (!map)
1411 		return (0);
1412 	for (i = 0; i < numchars; i++)
1413 		sum += nibblemap[map->b_data[i] & 0xf] +
1414 			nibblemap[(map->b_data[i] >> 4) & 0xf];
1415 	return (sum);
1416 }
1417 
1418 #endif  /*  EXT2FS_DEBUG  */
1419 
1420 unsigned long ext2_count_free_blocks (struct super_block * sb)
1421 {
1422 	struct ext2_group_desc * desc;
1423 	unsigned long desc_count = 0;
1424 	int i;
1425 #ifdef EXT2FS_DEBUG
1426 	unsigned long bitmap_count, x;
1427 	struct ext2_super_block *es;
1428 
1429 	es = EXT2_SB(sb)->s_es;
1430 	desc_count = 0;
1431 	bitmap_count = 0;
1432 	desc = NULL;
1433 	for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
1434 		struct buffer_head *bitmap_bh;
1435 		desc = ext2_get_group_desc (sb, i, NULL);
1436 		if (!desc)
1437 			continue;
1438 		desc_count += le16_to_cpu(desc->bg_free_blocks_count);
1439 		bitmap_bh = read_block_bitmap(sb, i);
1440 		if (!bitmap_bh)
1441 			continue;
1442 
1443 		x = ext2_count_free(bitmap_bh, sb->s_blocksize);
1444 		printk ("group %d: stored = %d, counted = %lu\n",
1445 			i, le16_to_cpu(desc->bg_free_blocks_count), x);
1446 		bitmap_count += x;
1447 		brelse(bitmap_bh);
1448 	}
1449 	printk("ext2_count_free_blocks: stored = %lu, computed = %lu, %lu\n",
1450 		(long)le32_to_cpu(es->s_free_blocks_count),
1451 		desc_count, bitmap_count);
1452 	return bitmap_count;
1453 #else
1454         for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
1455                 desc = ext2_get_group_desc (sb, i, NULL);
1456                 if (!desc)
1457                         continue;
1458                 desc_count += le16_to_cpu(desc->bg_free_blocks_count);
1459 	}
1460 	return desc_count;
1461 #endif
1462 }
1463 
1464 
1465 static inline int test_root(int a, int b)
1466 {
1467 	int num = b;
1468 
1469 	while (a > num)
1470 		num *= b;
1471 	return num == a;
1472 }
1473 
1474 static int ext2_group_sparse(int group)
1475 {
1476 	if (group <= 1)
1477 		return 1;
1478 	return (test_root(group, 3) || test_root(group, 5) ||
1479 		test_root(group, 7));
1480 }
1481 
1482 /**
1483  *	ext2_bg_has_super - number of blocks used by the superblock in group
1484  *	@sb: superblock for filesystem
1485  *	@group: group number to check
1486  *
1487  *	Return the number of blocks used by the superblock (primary or backup)
1488  *	in this group.  Currently this will be only 0 or 1.
1489  */
1490 int ext2_bg_has_super(struct super_block *sb, int group)
1491 {
1492 	if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
1493 	    !ext2_group_sparse(group))
1494 		return 0;
1495 	return 1;
1496 }
1497 
1498 /**
1499  *	ext2_bg_num_gdb - number of blocks used by the group table in group
1500  *	@sb: superblock for filesystem
1501  *	@group: group number to check
1502  *
1503  *	Return the number of blocks used by the group descriptor table
1504  *	(primary or backup) in this group.  In the future there may be a
1505  *	different number of descriptor blocks in each group.
1506  */
1507 unsigned long ext2_bg_num_gdb(struct super_block *sb, int group)
1508 {
1509 	if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
1510 	    !ext2_group_sparse(group))
1511 		return 0;
1512 	return EXT2_SB(sb)->s_gdb_count;
1513 }
1514 
1515