2 * linux/fs/ext3/balloc.c
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)
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
14 #include <linux/config.h>
15 #include <linux/time.h>
17 #include <linux/jbd.h>
18 #include <linux/ext3_fs.h>
19 #include <linux/ext3_jbd.h>
20 #include <linux/quotaops.h>
21 #include <linux/buffer_head.h>
24 * balloc.c contains the blocks allocation and deallocation routines
28 * The free blocks are managed by bitmaps. A file system contains several
29 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
30 * block for inodes, N blocks for the inode table and data blocks.
32 * The file system contains group descriptors which are located after the
33 * super block. Each descriptor contains the number of the bitmap block and
34 * the free blocks count in the block. The descriptors are loaded in memory
35 * when a file system is mounted (see ext3_read_super).
39 #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
41 struct ext3_group_desc * ext3_get_group_desc(struct super_block * sb,
42 unsigned int block_group,
43 struct buffer_head ** bh)
45 unsigned long group_desc;
47 struct ext3_group_desc * gdp;
49 if (block_group >= EXT3_SB(sb)->s_groups_count) {
50 ext3_error (sb, "ext3_get_group_desc",
51 "block_group >= groups_count - "
52 "block_group = %d, groups_count = %lu",
53 block_group, EXT3_SB(sb)->s_groups_count);
58 group_desc = block_group / EXT3_DESC_PER_BLOCK(sb);
59 desc = block_group % EXT3_DESC_PER_BLOCK(sb);
60 if (!EXT3_SB(sb)->s_group_desc[group_desc]) {
61 ext3_error (sb, "ext3_get_group_desc",
62 "Group descriptor not loaded - "
63 "block_group = %d, group_desc = %lu, desc = %lu",
64 block_group, group_desc, desc);
68 gdp = (struct ext3_group_desc *)
69 EXT3_SB(sb)->s_group_desc[group_desc]->b_data;
71 *bh = EXT3_SB(sb)->s_group_desc[group_desc];
76 * Read the bitmap for a given block_group, reading into the specified
77 * slot in the superblock's bitmap cache.
79 * Return buffer_head on success or NULL in case of failure.
81 static struct buffer_head *
82 read_block_bitmap(struct super_block *sb, unsigned int block_group)
84 struct ext3_group_desc * desc;
85 struct buffer_head * bh = NULL;
87 desc = ext3_get_group_desc (sb, block_group, NULL);
90 bh = sb_bread(sb, le32_to_cpu(desc->bg_block_bitmap));
92 ext3_error (sb, "read_block_bitmap",
93 "Cannot read block bitmap - "
94 "block_group = %d, block_bitmap = %lu",
95 block_group, (unsigned long) desc->bg_block_bitmap);
100 * The reservation window structure operations
101 * --------------------------------------------
102 * Operations include:
103 * dump, find, add, remove, is_empty, find_next_reservable_window, etc.
105 * We use sorted double linked list for the per-filesystem reservation
106 * window list. (like in vm_region).
108 * Initially, we keep those small operations in the abstract functions,
109 * so later if we need a better searching tree than double linked-list,
110 * we could easily switch to that without changing too much
113 static inline void rsv_window_dump(struct reserve_window *head, char *fn)
115 struct reserve_window *rsv;
117 printk("Block Allocation Reservation Windows Map (%s):\n", fn);
118 list_for_each_entry(rsv, &head->rsv_list, rsv_list) {
119 printk("reservation window 0x%p start: %d, end: %d\n",
120 rsv, rsv->rsv_start, rsv->rsv_end);
125 goal_in_my_reservation(struct reserve_window *rsv, int goal,
126 unsigned int group, struct super_block * sb)
128 unsigned long group_first_block, group_last_block;
130 group_first_block = le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block) +
131 group * EXT3_BLOCKS_PER_GROUP(sb);
132 group_last_block = group_first_block + EXT3_BLOCKS_PER_GROUP(sb) - 1;
134 if ((rsv->rsv_start > group_last_block) ||
135 (rsv->rsv_end < group_first_block))
137 if ((goal >= 0) && ((goal + group_first_block < rsv->rsv_start)
138 || (goal + group_first_block > rsv->rsv_end)))
143 static inline void rsv_window_add(struct reserve_window *rsv,
144 struct reserve_window *prev)
146 /* insert the new reservation window after the head */
147 list_add(&rsv->rsv_list, &prev->rsv_list);
150 static inline void rsv_window_remove(struct reserve_window *rsv)
154 rsv->rsv_alloc_hit = 0;
155 list_del(&rsv->rsv_list);
156 INIT_LIST_HEAD(&rsv->rsv_list);
159 static inline int rsv_is_empty(struct reserve_window *rsv)
161 /* a valid reservation end block could not be 0 */
162 return (rsv->rsv_end == 0);
165 void ext3_discard_reservation(struct inode *inode)
167 struct ext3_inode_info *ei = EXT3_I(inode);
168 struct reserve_window *rsv = &ei->i_rsv_window;
169 spinlock_t *rsv_lock = &EXT3_SB(inode->i_sb)->s_rsv_window_lock;
171 if (!rsv_is_empty(rsv)) {
173 rsv_window_remove(rsv);
174 spin_unlock(rsv_lock);
178 /* Free given blocks, update quota and i_blocks field */
179 void ext3_free_blocks(handle_t *handle, struct inode *inode,
180 unsigned long block, unsigned long count)
182 struct buffer_head *bitmap_bh = NULL;
183 struct buffer_head *gd_bh;
184 unsigned long block_group;
187 unsigned long overflow;
188 struct super_block * sb;
189 struct ext3_group_desc * gdp;
190 struct ext3_super_block * es;
191 struct ext3_sb_info *sbi;
193 int dquot_freed_blocks = 0;
197 printk ("ext3_free_blocks: nonexistent device");
201 es = EXT3_SB(sb)->s_es;
202 if (block < le32_to_cpu(es->s_first_data_block) ||
203 block + count < block ||
204 block + count > le32_to_cpu(es->s_blocks_count)) {
205 ext3_error (sb, "ext3_free_blocks",
206 "Freeing blocks not in datazone - "
207 "block = %lu, count = %lu", block, count);
211 ext3_debug ("freeing block %lu\n", block);
215 block_group = (block - le32_to_cpu(es->s_first_data_block)) /
216 EXT3_BLOCKS_PER_GROUP(sb);
217 bit = (block - le32_to_cpu(es->s_first_data_block)) %
218 EXT3_BLOCKS_PER_GROUP(sb);
220 * Check to see if we are freeing blocks across a group
223 if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
224 overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
228 bitmap_bh = read_block_bitmap(sb, block_group);
231 gdp = ext3_get_group_desc (sb, block_group, &gd_bh);
235 if (in_range (le32_to_cpu(gdp->bg_block_bitmap), block, count) ||
236 in_range (le32_to_cpu(gdp->bg_inode_bitmap), block, count) ||
237 in_range (block, le32_to_cpu(gdp->bg_inode_table),
238 EXT3_SB(sb)->s_itb_per_group) ||
239 in_range (block + count - 1, le32_to_cpu(gdp->bg_inode_table),
240 EXT3_SB(sb)->s_itb_per_group))
241 ext3_error (sb, "ext3_free_blocks",
242 "Freeing blocks in system zones - "
243 "Block = %lu, count = %lu",
247 * We are about to start releasing blocks in the bitmap,
248 * so we need undo access.
250 /* @@@ check errors */
251 BUFFER_TRACE(bitmap_bh, "getting undo access");
252 err = ext3_journal_get_undo_access(handle, bitmap_bh, NULL);
257 * We are about to modify some metadata. Call the journal APIs
258 * to unshare ->b_data if a currently-committing transaction is
261 BUFFER_TRACE(gd_bh, "get_write_access");
262 err = ext3_journal_get_write_access(handle, gd_bh);
266 jbd_lock_bh_state(bitmap_bh);
268 for (i = 0; i < count; i++) {
270 * An HJ special. This is expensive...
272 #ifdef CONFIG_JBD_DEBUG
273 jbd_unlock_bh_state(bitmap_bh);
275 struct buffer_head *debug_bh;
276 debug_bh = sb_find_get_block(sb, block + i);
278 BUFFER_TRACE(debug_bh, "Deleted!");
279 if (!bh2jh(bitmap_bh)->b_committed_data)
280 BUFFER_TRACE(debug_bh,
281 "No commited data in bitmap");
282 BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap");
286 jbd_lock_bh_state(bitmap_bh);
288 /* @@@ This prevents newly-allocated data from being
289 * freed and then reallocated within the same
292 * Ideally we would want to allow that to happen, but to
293 * do so requires making journal_forget() capable of
294 * revoking the queued write of a data block, which
295 * implies blocking on the journal lock. *forget()
296 * cannot block due to truncate races.
298 * Eventually we can fix this by making journal_forget()
299 * return a status indicating whether or not it was able
300 * to revoke the buffer. On successful revoke, it is
301 * safe not to set the allocation bit in the committed
302 * bitmap, because we know that there is no outstanding
303 * activity on the buffer any more and so it is safe to
306 BUFFER_TRACE(bitmap_bh, "set in b_committed_data");
307 J_ASSERT_BH(bitmap_bh,
308 bh2jh(bitmap_bh)->b_committed_data != NULL);
309 ext3_set_bit_atomic(sb_bgl_lock(sbi, block_group), bit + i,
310 bh2jh(bitmap_bh)->b_committed_data);
313 * We clear the bit in the bitmap after setting the committed
314 * data bit, because this is the reverse order to that which
315 * the allocator uses.
317 BUFFER_TRACE(bitmap_bh, "clear bit");
318 if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
319 bit + i, bitmap_bh->b_data)) {
320 jbd_unlock_bh_state(bitmap_bh);
321 ext3_error(sb, __FUNCTION__,
322 "bit already cleared for block %lu", block + i);
323 jbd_lock_bh_state(bitmap_bh);
324 BUFFER_TRACE(bitmap_bh, "bit already cleared");
326 dquot_freed_blocks++;
329 jbd_unlock_bh_state(bitmap_bh);
331 spin_lock(sb_bgl_lock(sbi, block_group));
332 gdp->bg_free_blocks_count =
333 cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) +
335 spin_unlock(sb_bgl_lock(sbi, block_group));
336 percpu_counter_mod(&sbi->s_freeblocks_counter, count);
338 /* We dirtied the bitmap block */
339 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
340 err = ext3_journal_dirty_metadata(handle, bitmap_bh);
342 /* And the group descriptor block */
343 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
344 ret = ext3_journal_dirty_metadata(handle, gd_bh);
347 if (overflow && !err) {
355 ext3_std_error(sb, err);
356 if (dquot_freed_blocks && !(EXT3_I(inode)->i_state & EXT3_STATE_RESIZE))
357 DQUOT_FREE_BLOCK(inode, dquot_freed_blocks);
362 * For ext3 allocations, we must not reuse any blocks which are
363 * allocated in the bitmap buffer's "last committed data" copy. This
364 * prevents deletes from freeing up the page for reuse until we have
365 * committed the delete transaction.
367 * If we didn't do this, then deleting something and reallocating it as
368 * data would allow the old block to be overwritten before the
369 * transaction committed (because we force data to disk before commit).
370 * This would lead to corruption if we crashed between overwriting the
371 * data and committing the delete.
373 * @@@ We may want to make this allocation behaviour conditional on
374 * data-writes at some point, and disable it for metadata allocations or
377 static int ext3_test_allocatable(int nr, struct buffer_head *bh)
380 struct journal_head *jh = bh2jh(bh);
382 if (ext3_test_bit(nr, bh->b_data))
385 jbd_lock_bh_state(bh);
386 if (!jh->b_committed_data)
389 ret = !ext3_test_bit(nr, jh->b_committed_data);
390 jbd_unlock_bh_state(bh);
395 bitmap_search_next_usable_block(int start, struct buffer_head *bh,
399 struct journal_head *jh = bh2jh(bh);
402 * The bitmap search --- search forward alternately through the actual
403 * bitmap and the last-committed copy until we find a bit free in
406 while (start < maxblocks) {
407 next = ext3_find_next_zero_bit(bh->b_data, maxblocks, start);
408 if (next >= maxblocks)
410 if (ext3_test_allocatable(next, bh))
412 jbd_lock_bh_state(bh);
413 if (jh->b_committed_data)
414 start = ext3_find_next_zero_bit(jh->b_committed_data,
416 jbd_unlock_bh_state(bh);
422 * Find an allocatable block in a bitmap. We honour both the bitmap and
423 * its last-committed copy (if that exists), and perform the "most
424 * appropriate allocation" algorithm of looking for a free block near
425 * the initial goal; then for a free byte somewhere in the bitmap; then
426 * for any free bit in the bitmap.
429 find_next_usable_block(int start, struct buffer_head *bh, int maxblocks)
436 * The goal was occupied; search forward for a free
437 * block within the next XX blocks.
439 * end_goal is more or less random, but it has to be
440 * less than EXT3_BLOCKS_PER_GROUP. Aligning up to the
441 * next 64-bit boundary is simple..
443 int end_goal = (start + 63) & ~63;
444 if (end_goal > maxblocks)
445 end_goal = maxblocks;
446 here = ext3_find_next_zero_bit(bh->b_data, end_goal, start);
447 if (here < end_goal && ext3_test_allocatable(here, bh))
449 ext3_debug("Bit not found near goal\n");
456 p = ((char *)bh->b_data) + (here >> 3);
457 r = memscan(p, 0, (maxblocks - here + 7) >> 3);
458 next = (r - ((char *)bh->b_data)) << 3;
460 if (next < maxblocks && next >= start && ext3_test_allocatable(next, bh))
464 * The bitmap search --- search forward alternately through the actual
465 * bitmap and the last-committed copy until we find a bit free in
468 here = bitmap_search_next_usable_block(here, bh, maxblocks);
473 * We think we can allocate this block in this bitmap. Try to set the bit.
474 * If that succeeds then check that nobody has allocated and then freed the
475 * block since we saw that is was not marked in b_committed_data. If it _was_
476 * allocated and freed then clear the bit in the bitmap again and return
480 claim_block(spinlock_t *lock, int block, struct buffer_head *bh)
482 struct journal_head *jh = bh2jh(bh);
485 if (ext3_set_bit_atomic(lock, block, bh->b_data))
487 jbd_lock_bh_state(bh);
488 if (jh->b_committed_data && ext3_test_bit(block,jh->b_committed_data)) {
489 ext3_clear_bit_atomic(lock, block, bh->b_data);
494 jbd_unlock_bh_state(bh);
499 * If we failed to allocate the desired block then we may end up crossing to a
500 * new bitmap. In that case we must release write access to the old one via
501 * ext3_journal_release_buffer(), else we'll run out of credits.
504 ext3_try_to_allocate(struct super_block *sb, handle_t *handle, int group,
505 struct buffer_head *bitmap_bh, int goal, struct reserve_window *my_rsv)
507 int group_first_block, start, end;
509 /* we do allocation within the reservation window if we have a window */
512 le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block) +
513 group * EXT3_BLOCKS_PER_GROUP(sb);
514 if (my_rsv->rsv_start >= group_first_block)
515 start = my_rsv->rsv_start - group_first_block;
517 /* reservation window cross group boundary */
519 end = my_rsv->rsv_end - group_first_block + 1;
520 if (end > EXT3_BLOCKS_PER_GROUP(sb))
521 /* reservation window crosses group boundary */
522 end = EXT3_BLOCKS_PER_GROUP(sb);
523 if ((start <= goal) && (goal < end))
532 end = EXT3_BLOCKS_PER_GROUP(sb);
535 BUG_ON(start > EXT3_BLOCKS_PER_GROUP(sb));
538 if (goal < 0 || !ext3_test_allocatable(goal, bitmap_bh)) {
539 goal = find_next_usable_block(start, bitmap_bh, end);
545 for (i = 0; i < 7 && goal > start &&
546 ext3_test_allocatable(goal - 1,
554 if (!claim_block(sb_bgl_lock(EXT3_SB(sb), group), goal, bitmap_bh)) {
556 * The block was allocated by another thread, or it was
557 * allocated and then freed by another thread
566 my_rsv->rsv_alloc_hit++;
573 * find_next_reservable_window():
574 * find a reservable space within the given range
575 * It does not allocate the reservation window for now
576 * alloc_new_reservation() will do the work later.
578 * @search_head: the head of the searching list;
579 * This is not necessary the list head of the whole filesystem
581 * we have both head and start_block to assist the search
582 * for the reservable space. The list start from head,
583 * but we will shift to the place where start_block is,
584 * then start from there, we looking for a resevable space.
586 * @fs_rsv_head: per-filesystem reervation list head.
588 * @size: the target new reservation window size
589 * @group_first_block: the first block we consider to start
590 * the real search from
593 * the maxium block number that our goal reservable space
594 * could start from. This is normally the last block in this
595 * group. The search will end when we found the start of next
596 * possiblereservable space is out of this boundary.
597 * This could handle the cross bounday reservation window request.
599 * basically we search from the given range, rather than the whole
600 * reservation double linked list, (start_block, last_block)
601 * to find a free region that of of my size and has not
604 * on succeed, it returns the reservation window to be append to.
605 * failed, return NULL.
608 struct reserve_window *find_next_reservable_window(
609 struct reserve_window *search_head,
610 struct reserve_window *fs_rsv_head,
611 unsigned long size, int *start_block,
614 struct reserve_window *rsv;
617 /* TODO:make the start of the reservation window byte alligned */
618 /*cur = *start_block & 8;*/
620 rsv = list_entry(search_head->rsv_list.next,
621 struct reserve_window, rsv_list);
622 while (rsv != fs_rsv_head) {
623 if (cur + size <= rsv->rsv_start) {
625 * Found a reserveable space big enough. We could
626 * have a reservation across the group boundary here
630 if (cur <= rsv->rsv_end)
631 cur = rsv->rsv_end + 1;
634 * in the case we could not find a reservable space
635 * that is what is expected, during the re-search, we could
636 * remember what's the largest reservable space we could have
637 * and return that one.
639 * For now it will fail if we could not find the reservable
640 * space with expected-size (or more)...
642 rsv = list_entry(rsv->rsv_list.next,
643 struct reserve_window, rsv_list);
644 if (cur > last_block)
645 return NULL; /* fail */
648 * we come here either :
649 * when we rearch to the end of the whole list,
650 * and there is empty reservable space after last entry in the list.
651 * append it to the end of the list.
653 * or we found one reservable space in the middle of the list,
654 * return the reservation window that we could append to.
658 return list_entry(rsv->rsv_list.prev, struct reserve_window, rsv_list);
662 * alloc_new_reservation()--allocate a new reservation window
663 * if there is an existing reservation, discard it first
664 * then allocate the new one from there
665 * otherwise allocate the new reservation from the given
666 * start block, or the beginning of the group, if a goal
669 * To make a new reservation, we search part of the filesystem
670 * reservation list(the list that inside the group).
672 * If we have a old reservation, the search goal is the end of
673 * last reservation. If we do not have a old reservatio, then we
674 * start from a given goal, or the first block of the group, if
675 * the goal is not given.
677 * We first find a reservable space after the goal, then from
678 * there,we check the bitmap for the first free block after
679 * it. If there is no free block until the end of group, then the
680 * whole group is full, we failed. Otherwise, check if the free
681 * block is inside the expected reservable space, if so, we
683 * If the first free block is outside the reseravle space, then
684 * start from the first free block, we search for next avalibale
687 * on succeed, a new reservation will be found and inserted into the list
688 * It contains at least one free block, and it is not overlap with other
689 * reservation window.
691 * failed: we failed to found a reservation window in this group
693 * @rsv: the reservation
695 * @goal: The goal. It is where the search for a
696 * free reservable space should start from.
697 * if we have a old reservation, start_block is the end of
698 * old reservation. Otherwise,
699 * if we have a goal(goal >0 ), then start from there,
700 * no goal(goal = -1), we start from the first block
703 * @sb: the super block
704 * @group: the group we are trying to do allocate in
705 * @bitmap_bh: the block group block bitmap
707 static int alloc_new_reservation(struct reserve_window *my_rsv,
708 int goal, struct super_block *sb,
709 unsigned int group, struct buffer_head *bitmap_bh)
711 struct reserve_window *search_head;
712 int group_first_block, group_end_block, start_block;
713 int first_free_block;
714 int reservable_space_start;
715 struct reserve_window *prev_rsv;
716 struct reserve_window *fs_rsv_head = &EXT3_SB(sb)->s_rsv_window_head;
719 group_first_block = le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block) +
720 group * EXT3_BLOCKS_PER_GROUP(sb);
721 group_end_block = group_first_block + EXT3_BLOCKS_PER_GROUP(sb) - 1;
724 start_block = group_first_block;
726 start_block = goal + group_first_block;
728 size = atomic_read(&my_rsv->rsv_goal_size);
729 /* if we have a old reservation, start the search from the old rsv */
730 if (!rsv_is_empty(my_rsv)) {
732 * if the old reservation is cross group boundary
733 * we will come here when we just failed to allocate from
734 * the first part of the window. We still have another part
735 * that belongs to the next group. In this case, there is no
736 * point to discard our window and try to allocate a new one
737 * in this group(which will fail). we should
738 * keep the reservation window, just simply move on.
740 * Maybe we could shift the start block of the reservation
741 * window to the first block of next group.
744 if ((my_rsv->rsv_start <= group_end_block) &&
745 (my_rsv->rsv_end > group_end_block))
748 /* remember where we are before we discard the old one */
749 if (my_rsv->rsv_end + 1 > start_block)
750 start_block = my_rsv->rsv_end + 1;
751 search_head = my_rsv;
752 if ((my_rsv->rsv_alloc_hit > (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) {
754 * if we previously allocation hit ration is greater than half
755 * we double the size of reservation window next time
756 * otherwise keep the same
759 if (size > EXT3_MAX_RESERVE_BLOCKS)
760 size = EXT3_MAX_RESERVE_BLOCKS;
761 atomic_set(&my_rsv->rsv_goal_size, size);
766 * we don't have a reservation,
767 * we set our goal(start_block) and
768 * the list head for the search
770 search_head = fs_rsv_head;
774 * find_next_reservable_window() simply find a reservable window
775 * inside the given range(start_block, group_end_block).
777 * To make sure the reservation window has a free bit inside it, we
778 * need to check the bitmap after we found a reservable window.
781 prev_rsv = find_next_reservable_window(search_head, fs_rsv_head, size,
782 &start_block, group_end_block);
783 if (prev_rsv == NULL)
785 reservable_space_start = start_block;
787 * On success, find_next_reservable_window() returns the
788 * reservation window where there is a reservable space after it.
789 * Before we reserve this reservable space, we need
790 * to make sure there is at least a free block inside this region.
792 * searching the first free bit on the block bitmap and copy of
793 * last committed bitmap alternatively, until we found a allocatable
794 * block. Search start from the start block of the reservable space
797 first_free_block = bitmap_search_next_usable_block(
798 reservable_space_start - group_first_block,
799 bitmap_bh, group_end_block - group_first_block + 1);
801 if (first_free_block < 0) {
803 * no free block left on the bitmap, no point
804 * to reserve the space. return failed.
808 start_block = first_free_block + group_first_block;
810 * check if the first free block is within the
811 * free space we just found
813 if ((start_block >= reservable_space_start) &&
814 (start_block < reservable_space_start + size))
815 goto found_rsv_window;
817 * if the first free bit we found is out of the reservable space
818 * this means there is no free block on the reservable space
819 * we should continue search for next reservable space,
820 * start from where the free block is,
821 * we also shift the list head to where we stopped last time
823 search_head = prev_rsv;
828 * great! the reservable space contains some free blocks.
829 * if the search returns that we should add the new
830 * window just next to where the old window, we don't
831 * need to remove the old window first then add it to the
832 * same place, just update the new start and new end.
834 if (my_rsv != prev_rsv) {
835 if (!rsv_is_empty(my_rsv))
836 rsv_window_remove(my_rsv);
837 rsv_window_add(my_rsv, prev_rsv);
839 my_rsv->rsv_start = reservable_space_start;
840 my_rsv->rsv_end = my_rsv->rsv_start + size - 1;
841 return 0; /* succeed */
843 return -1; /* failed */
847 * This is the main function used to allocate a new block and its reservation
850 * Each time when a new block allocation is need, first try to allocate from
851 * its own reservation. If it does not have a reservation window, instead of
852 * looking for a free bit on bitmap first, then look up the reservation list to
853 * see if it is inside somebody else's reservation window, we try to allocate a
854 * reservation window for it start from the goal first. Then do the block
855 * allocation within the reservation window.
857 * This will aviod keep searching the reservation list again and again when
858 * someboday is looking for a free block(without reservation), and there are
859 * lots of free blocks, but they are all being reserved
861 * We use a sorted double linked list for the per-filesystem reservation list.
862 * The insert, remove and find a free space(non-reserved) operations for the
863 * sorted double linked list should be fast.
867 ext3_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle,
868 unsigned int group, struct buffer_head *bitmap_bh,
869 int goal, struct reserve_window * my_rsv,
872 spinlock_t *rsv_lock;
873 unsigned long group_first_block;
881 * Make sure we use undo access for the bitmap, because it is critical
882 * that we do the frozen_data COW on bitmap buffers in all cases even
883 * if the buffer is in BJ_Forget state in the committing transaction.
885 BUFFER_TRACE(bitmap_bh, "get undo access for new block");
886 fatal = ext3_journal_get_undo_access(handle, bitmap_bh, &credits);
893 * we don't deal with reservation when
894 * filesystem is mounted without reservation
895 * or the file is not a regular file
896 * of last attemp of allocating a block with reservation turn on failed
898 if (my_rsv == NULL ) {
899 ret = ext3_try_to_allocate(sb, handle, group, bitmap_bh, goal, NULL);
902 rsv_lock = &EXT3_SB(sb)->s_rsv_window_lock;
904 * goal is a group relative block number (if there is a goal)
905 * 0 < goal < EXT3_BLOCKS_PER_GROUP(sb)
906 * first block is a filesystem wide block number
907 * first block is the block number of the first block in this group
909 group_first_block = le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block) +
910 group * EXT3_BLOCKS_PER_GROUP(sb);
913 * Basically we will allocate a new block from inode's reservation
916 * We need to allocate a new reservation window, if:
917 * a) inode does not have a reservation window; or
918 * b) last attemp of allocating a block from existing reservation
920 * c) we come here with a goal and with a reservation window
922 * We do not need to allocate a new reservation window if we come here
923 * at the beginning with a goal and the goal is inside the window, or
924 * or we don't have a goal but already have a reservation window.
925 * then we could go to allocate from the reservation window directly.
928 if (rsv_is_empty(my_rsv) || (ret < 0) ||
929 !goal_in_my_reservation(my_rsv, goal, group, sb)) {
931 ret = alloc_new_reservation(my_rsv, goal, sb,
933 spin_unlock(rsv_lock);
937 if (!goal_in_my_reservation(my_rsv, goal, group, sb))
940 if ((my_rsv->rsv_start >= group_first_block + EXT3_BLOCKS_PER_GROUP(sb))
941 || (my_rsv->rsv_end < group_first_block))
943 ret = ext3_try_to_allocate(sb, handle, group, bitmap_bh, goal,
950 BUFFER_TRACE(bitmap_bh, "journal_dirty_metadata for "
952 fatal = ext3_journal_dirty_metadata(handle, bitmap_bh);
960 BUFFER_TRACE(bitmap_bh, "journal_release_buffer");
961 ext3_journal_release_buffer(handle, bitmap_bh, credits);
965 static int ext3_has_free_blocks(struct ext3_sb_info *sbi)
967 int free_blocks, root_blocks;
969 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
970 root_blocks = le32_to_cpu(sbi->s_es->s_r_blocks_count);
971 if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) &&
972 sbi->s_resuid != current->fsuid &&
973 (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) {
980 * ext3_should_retry_alloc() is called when ENOSPC is returned, and if
981 * it is profitable to retry the operation, this function will wait
982 * for the current or commiting transaction to complete, and then
985 int ext3_should_retry_alloc(struct super_block *sb, int *retries)
987 if (!ext3_has_free_blocks(EXT3_SB(sb)) || (*retries)++ > 3)
990 jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
992 return journal_force_commit_nested(EXT3_SB(sb)->s_journal);
996 * ext3_new_block uses a goal block to assist allocation. If the goal is
997 * free, or there is a free block within 32 blocks of the goal, that block
998 * is allocated. Otherwise a forward search is made for a free block; within
999 * each block group the search first looks for an entire free byte in the block
1000 * bitmap, and then for any free bit if that fails.
1001 * This function also updates quota and i_blocks field.
1003 int ext3_new_block(handle_t *handle, struct inode *inode,
1004 unsigned long goal, int *errp)
1006 struct buffer_head *bitmap_bh = NULL;
1007 struct buffer_head *gdp_bh;
1011 int bgi; /* blockgroup iteration index */
1014 int performed_allocation = 0;
1016 struct super_block *sb;
1017 struct ext3_group_desc *gdp;
1018 struct ext3_super_block *es;
1019 struct ext3_sb_info *sbi;
1020 struct reserve_window *my_rsv = NULL;
1022 static int goal_hits, goal_attempts;
1027 printk("ext3_new_block: nonexistent device");
1032 * Check quota for allocation of this block.
1034 if (DQUOT_ALLOC_BLOCK(inode, 1)) {
1040 es = EXT3_SB(sb)->s_es;
1041 ext3_debug("goal=%lu.\n", goal);
1042 if (test_opt(sb, RESERVATION) && S_ISREG(inode->i_mode))
1043 my_rsv = &EXT3_I(inode)->i_rsv_window;
1044 if (!ext3_has_free_blocks(sbi)) {
1050 * First, test whether the goal block is free.
1052 if (goal < le32_to_cpu(es->s_first_data_block) ||
1053 goal >= le32_to_cpu(es->s_blocks_count))
1054 goal = le32_to_cpu(es->s_first_data_block);
1055 group_no = (goal - le32_to_cpu(es->s_first_data_block)) /
1056 EXT3_BLOCKS_PER_GROUP(sb);
1057 gdp = ext3_get_group_desc(sb, group_no, &gdp_bh);
1061 goal_group = group_no;
1063 free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
1064 if (free_blocks > 0) {
1065 ret_block = ((goal - le32_to_cpu(es->s_first_data_block)) %
1066 EXT3_BLOCKS_PER_GROUP(sb));
1067 bitmap_bh = read_block_bitmap(sb, group_no);
1070 ret_block = ext3_try_to_allocate_with_rsv(sb, handle, group_no,
1071 bitmap_bh, ret_block, my_rsv, &fatal);
1079 * Now search the rest of the groups. We assume that
1080 * i and gdp correctly point to the last group visited.
1082 for (bgi = 0; bgi < EXT3_SB(sb)->s_groups_count; bgi++) {
1084 if (group_no >= EXT3_SB(sb)->s_groups_count)
1086 gdp = ext3_get_group_desc(sb, group_no, &gdp_bh);
1091 free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
1092 if (free_blocks <= 0)
1096 bitmap_bh = read_block_bitmap(sb, group_no);
1099 ret_block = ext3_try_to_allocate_with_rsv(sb, handle, group_no,
1100 bitmap_bh, -1, my_rsv, &fatal);
1107 * We may end up a bogus ealier ENOSPC error due to
1108 * filesystem is "full" of reservations, but
1109 * there maybe indeed free blocks avaliable on disk
1110 * In this case, we just forget about the reservations
1111 * just do block allocation as without reservations.
1115 group_no = goal_group;
1118 /* No space left on the device */
1124 ext3_debug("using block group %d(%d)\n",
1125 group_no, gdp->bg_free_blocks_count);
1127 BUFFER_TRACE(gdp_bh, "get_write_access");
1128 fatal = ext3_journal_get_write_access(handle, gdp_bh);
1132 target_block = ret_block + group_no * EXT3_BLOCKS_PER_GROUP(sb)
1133 + le32_to_cpu(es->s_first_data_block);
1135 if (target_block == le32_to_cpu(gdp->bg_block_bitmap) ||
1136 target_block == le32_to_cpu(gdp->bg_inode_bitmap) ||
1137 in_range(target_block, le32_to_cpu(gdp->bg_inode_table),
1138 EXT3_SB(sb)->s_itb_per_group))
1139 ext3_error(sb, "ext3_new_block",
1140 "Allocating block in system zone - "
1141 "block = %u", target_block);
1143 performed_allocation = 1;
1145 #ifdef CONFIG_JBD_DEBUG
1147 struct buffer_head *debug_bh;
1149 /* Record bitmap buffer state in the newly allocated block */
1150 debug_bh = sb_find_get_block(sb, target_block);
1152 BUFFER_TRACE(debug_bh, "state when allocated");
1153 BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap state");
1157 jbd_lock_bh_state(bitmap_bh);
1158 spin_lock(sb_bgl_lock(sbi, group_no));
1159 if (buffer_jbd(bitmap_bh) && bh2jh(bitmap_bh)->b_committed_data) {
1160 if (ext3_test_bit(ret_block,
1161 bh2jh(bitmap_bh)->b_committed_data)) {
1162 printk("%s: block was unexpectedly set in "
1163 "b_committed_data\n", __FUNCTION__);
1166 ext3_debug("found bit %d\n", ret_block);
1167 spin_unlock(sb_bgl_lock(sbi, group_no));
1168 jbd_unlock_bh_state(bitmap_bh);
1171 /* ret_block was blockgroup-relative. Now it becomes fs-relative */
1172 ret_block = target_block;
1174 if (ret_block >= le32_to_cpu(es->s_blocks_count)) {
1175 ext3_error(sb, "ext3_new_block",
1176 "block(%d) >= blocks count(%d) - "
1177 "block_group = %d, es == %p ", ret_block,
1178 le32_to_cpu(es->s_blocks_count), group_no, es);
1183 * It is up to the caller to add the new buffer to a journal
1184 * list of some description. We don't know in advance whether
1185 * the caller wants to use it as metadata or data.
1187 ext3_debug("allocating block %d. Goal hits %d of %d.\n",
1188 ret_block, goal_hits, goal_attempts);
1190 spin_lock(sb_bgl_lock(sbi, group_no));
1191 gdp->bg_free_blocks_count =
1192 cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) - 1);
1193 spin_unlock(sb_bgl_lock(sbi, group_no));
1194 percpu_counter_mod(&sbi->s_freeblocks_counter, -1);
1196 BUFFER_TRACE(gdp_bh, "journal_dirty_metadata for group descriptor");
1197 err = ext3_journal_dirty_metadata(handle, gdp_bh);
1214 ext3_std_error(sb, fatal);
1217 * Undo the block allocation
1219 if (!performed_allocation)
1220 DQUOT_FREE_BLOCK(inode, 1);
1225 unsigned long ext3_count_free_blocks(struct super_block *sb)
1227 unsigned long desc_count;
1228 struct ext3_group_desc *gdp;
1231 struct ext3_super_block *es;
1232 unsigned long bitmap_count, x;
1233 struct buffer_head *bitmap_bh = NULL;
1236 es = EXT3_SB(sb)->s_es;
1240 for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
1241 gdp = ext3_get_group_desc(sb, i, NULL);
1244 desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
1246 bitmap_bh = read_block_bitmap(sb, i);
1247 if (bitmap_bh == NULL)
1250 x = ext3_count_free(bitmap_bh, sb->s_blocksize);
1251 printk("group %d: stored = %d, counted = %lu\n",
1252 i, le16_to_cpu(gdp->bg_free_blocks_count), x);
1256 printk("ext3_count_free_blocks: stored = %u, computed = %lu, %lu\n",
1257 le32_to_cpu(es->s_free_blocks_count), desc_count, bitmap_count);
1259 return bitmap_count;
1262 for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
1263 gdp = ext3_get_group_desc(sb, i, NULL);
1266 desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
1273 static inline int block_in_use(unsigned long block,
1274 struct super_block * sb,
1275 unsigned char * map)
1277 return ext3_test_bit ((block -
1278 le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block)) %
1279 EXT3_BLOCKS_PER_GROUP(sb), map);
1282 static inline int test_root(int a, int b)
1295 int ext3_group_sparse(int group)
1297 return (test_root(group, 3) || test_root(group, 5) ||
1298 test_root(group, 7));
1302 * ext3_bg_has_super - number of blocks used by the superblock in group
1303 * @sb: superblock for filesystem
1304 * @group: group number to check
1306 * Return the number of blocks used by the superblock (primary or backup)
1307 * in this group. Currently this will be only 0 or 1.
1309 int ext3_bg_has_super(struct super_block *sb, int group)
1311 if (EXT3_HAS_RO_COMPAT_FEATURE(sb,EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
1312 !ext3_group_sparse(group))
1318 * ext3_bg_num_gdb - number of blocks used by the group table in group
1319 * @sb: superblock for filesystem
1320 * @group: group number to check
1322 * Return the number of blocks used by the group descriptor table
1323 * (primary or backup) in this group. In the future there may be a
1324 * different number of descriptor blocks in each group.
1326 unsigned long ext3_bg_num_gdb(struct super_block *sb, int group)
1328 if (EXT3_HAS_RO_COMPAT_FEATURE(sb,EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
1329 !ext3_group_sparse(group))
1331 return EXT3_SB(sb)->s_gdb_count;
1334 #ifdef CONFIG_EXT3_CHECK
1335 /* Called at mount-time, super-block is locked */
1336 void ext3_check_blocks_bitmap (struct super_block * sb)
1338 struct ext3_super_block *es;
1339 unsigned long desc_count, bitmap_count, x, j;
1340 unsigned long desc_blocks;
1341 struct buffer_head *bitmap_bh = NULL;
1342 struct ext3_group_desc *gdp;
1345 es = EXT3_SB(sb)->s_es;
1349 for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
1350 gdp = ext3_get_group_desc (sb, i, NULL);
1353 desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
1355 bitmap_bh = read_block_bitmap(sb, i);
1356 if (bitmap_bh == NULL)
1359 if (ext3_bg_has_super(sb, i) &&
1360 !ext3_test_bit(0, bitmap_bh->b_data))
1361 ext3_error(sb, __FUNCTION__,
1362 "Superblock in group %d is marked free", i);
1364 desc_blocks = ext3_bg_num_gdb(sb, i);
1365 for (j = 0; j < desc_blocks; j++)
1366 if (!ext3_test_bit(j + 1, bitmap_bh->b_data))
1367 ext3_error(sb, __FUNCTION__,
1368 "Descriptor block #%ld in group "
1369 "%d is marked free", j, i);
1371 if (!block_in_use (le32_to_cpu(gdp->bg_block_bitmap),
1372 sb, bitmap_bh->b_data))
1373 ext3_error (sb, "ext3_check_blocks_bitmap",
1374 "Block bitmap for group %d is marked free",
1377 if (!block_in_use (le32_to_cpu(gdp->bg_inode_bitmap),
1378 sb, bitmap_bh->b_data))
1379 ext3_error (sb, "ext3_check_blocks_bitmap",
1380 "Inode bitmap for group %d is marked free",
1383 for (j = 0; j < EXT3_SB(sb)->s_itb_per_group; j++)
1384 if (!block_in_use (le32_to_cpu(gdp->bg_inode_table) + j,
1385 sb, bitmap_bh->b_data))
1386 ext3_error (sb, "ext3_check_blocks_bitmap",
1387 "Block #%d of the inode table in "
1388 "group %d is marked free", j, i);
1390 x = ext3_count_free(bitmap_bh, sb->s_blocksize);
1391 if (le16_to_cpu(gdp->bg_free_blocks_count) != x)
1392 ext3_error (sb, "ext3_check_blocks_bitmap",
1393 "Wrong free blocks count for group %d, "
1394 "stored = %d, counted = %lu", i,
1395 le16_to_cpu(gdp->bg_free_blocks_count), x);
1399 if (le32_to_cpu(es->s_free_blocks_count) != bitmap_count)
1400 ext3_error (sb, "ext3_check_blocks_bitmap",
1401 "Wrong free blocks count in super block, "
1402 "stored = %lu, counted = %lu",
1403 (unsigned long)le32_to_cpu(es->s_free_blocks_count),