diff options
author | Mingming Cao <cmm@us.ibm.com> | 2006-09-27 04:49:32 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@g5.osdl.org> | 2006-09-27 11:26:09 -0400 |
commit | 36faadc144477b4929c8fe60b8053f4472eeb3d2 (patch) | |
tree | b329171a15b7caf363b0dec82ef7641fc43faced | |
parent | 321fb9e81821a81b2cda1328698b0c19d57c717f (diff) |
[PATCH] ext3: more comments about block allocation/reservation code
Signed-off-by: Mingming Cao <cmm@us.ibm.com>
Acked-by: Randy Dunlap <rdunlap@xenotime.net>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
-rw-r--r-- | fs/ext3/balloc.c | 292 |
1 files changed, 247 insertions, 45 deletions
diff --git a/fs/ext3/balloc.c b/fs/ext3/balloc.c index 2bd836f30ca6..f1897feb18f4 100644 --- a/fs/ext3/balloc.c +++ b/fs/ext3/balloc.c | |||
@@ -38,6 +38,13 @@ | |||
38 | 38 | ||
39 | #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1) | 39 | #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1) |
40 | 40 | ||
41 | /** | ||
42 | * ext3_get_group_desc() -- load group descriptor from disk | ||
43 | * @sb: super block | ||
44 | * @block_group: given block group | ||
45 | * @bh: pointer to the buffer head to store the block | ||
46 | * group descriptor | ||
47 | */ | ||
41 | struct ext3_group_desc * ext3_get_group_desc(struct super_block * sb, | 48 | struct ext3_group_desc * ext3_get_group_desc(struct super_block * sb, |
42 | unsigned int block_group, | 49 | unsigned int block_group, |
43 | struct buffer_head ** bh) | 50 | struct buffer_head ** bh) |
@@ -73,7 +80,11 @@ struct ext3_group_desc * ext3_get_group_desc(struct super_block * sb, | |||
73 | return desc + offset; | 80 | return desc + offset; |
74 | } | 81 | } |
75 | 82 | ||
76 | /* | 83 | /** |
84 | * read_block_bitmap() | ||
85 | * @sb: super block | ||
86 | * @block_group: given block group | ||
87 | * | ||
77 | * Read the bitmap for a given block_group, reading into the specified | 88 | * Read the bitmap for a given block_group, reading into the specified |
78 | * slot in the superblock's bitmap cache. | 89 | * slot in the superblock's bitmap cache. |
79 | * | 90 | * |
@@ -103,13 +114,20 @@ error_out: | |||
103 | * Operations include: | 114 | * Operations include: |
104 | * dump, find, add, remove, is_empty, find_next_reservable_window, etc. | 115 | * dump, find, add, remove, is_empty, find_next_reservable_window, etc. |
105 | * | 116 | * |
106 | * We use sorted double linked list for the per-filesystem reservation | 117 | * We use a red-black tree to represent per-filesystem reservation |
107 | * window list. (like in vm_region). | 118 | * windows. |
108 | * | 119 | * |
109 | * Initially, we keep those small operations in the abstract functions, | 120 | */ |
110 | * so later if we need a better searching tree than double linked-list, | 121 | |
111 | * we could easily switch to that without changing too much | 122 | /** |
112 | * code. | 123 | * __rsv_window_dump() -- Dump the filesystem block allocation reservation map |
124 | * @rb_root: root of per-filesystem reservation rb tree | ||
125 | * @verbose: verbose mode | ||
126 | * @fn: function which wishes to dump the reservation map | ||
127 | * | ||
128 | * If verbose is turned on, it will print the whole block reservation | ||
129 | * windows(start, end). Otherwise, it will only print out the "bad" windows, | ||
130 | * those windows that overlap with their immediate neighbors. | ||
113 | */ | 131 | */ |
114 | #if 1 | 132 | #if 1 |
115 | static void __rsv_window_dump(struct rb_root *root, int verbose, | 133 | static void __rsv_window_dump(struct rb_root *root, int verbose, |
@@ -161,6 +179,22 @@ restart: | |||
161 | #define rsv_window_dump(root, verbose) do {} while (0) | 179 | #define rsv_window_dump(root, verbose) do {} while (0) |
162 | #endif | 180 | #endif |
163 | 181 | ||
182 | /** | ||
183 | * goal_in_my_reservation() | ||
184 | * @rsv: inode's reservation window | ||
185 | * @grp_goal: given goal block relative to the allocation block group | ||
186 | * @group: the current allocation block group | ||
187 | * @sb: filesystem super block | ||
188 | * | ||
189 | * Test if the given goal block (group relative) is within the file's | ||
190 | * own block reservation window range. | ||
191 | * | ||
192 | * If the reservation window is outside the goal allocation group, return 0; | ||
193 | * grp_goal (given goal block) could be -1, which means no specific | ||
194 | * goal block. In this case, always return 1. | ||
195 | * If the goal block is within the reservation window, return 1; | ||
196 | * otherwise, return 0; | ||
197 | */ | ||
164 | static int | 198 | static int |
165 | goal_in_my_reservation(struct ext3_reserve_window *rsv, ext3_grpblk_t grp_goal, | 199 | goal_in_my_reservation(struct ext3_reserve_window *rsv, ext3_grpblk_t grp_goal, |
166 | unsigned int group, struct super_block * sb) | 200 | unsigned int group, struct super_block * sb) |
@@ -179,7 +213,11 @@ goal_in_my_reservation(struct ext3_reserve_window *rsv, ext3_grpblk_t grp_goal, | |||
179 | return 1; | 213 | return 1; |
180 | } | 214 | } |
181 | 215 | ||
182 | /* | 216 | /** |
217 | * search_reserve_window() | ||
218 | * @rb_root: root of reservation tree | ||
219 | * @goal: target allocation block | ||
220 | * | ||
183 | * Find the reserved window which includes the goal, or the previous one | 221 | * Find the reserved window which includes the goal, or the previous one |
184 | * if the goal is not in any window. | 222 | * if the goal is not in any window. |
185 | * Returns NULL if there are no windows or if all windows start after the goal. | 223 | * Returns NULL if there are no windows or if all windows start after the goal. |
@@ -216,6 +254,13 @@ search_reserve_window(struct rb_root *root, ext3_fsblk_t goal) | |||
216 | return rsv; | 254 | return rsv; |
217 | } | 255 | } |
218 | 256 | ||
257 | /** | ||
258 | * ext3_rsv_window_add() -- Insert a window to the block reservation rb tree. | ||
259 | * @sb: super block | ||
260 | * @rsv: reservation window to add | ||
261 | * | ||
262 | * Must be called with rsv_lock hold. | ||
263 | */ | ||
219 | void ext3_rsv_window_add(struct super_block *sb, | 264 | void ext3_rsv_window_add(struct super_block *sb, |
220 | struct ext3_reserve_window_node *rsv) | 265 | struct ext3_reserve_window_node *rsv) |
221 | { | 266 | { |
@@ -246,6 +291,15 @@ void ext3_rsv_window_add(struct super_block *sb, | |||
246 | rb_insert_color(node, root); | 291 | rb_insert_color(node, root); |
247 | } | 292 | } |
248 | 293 | ||
294 | /** | ||
295 | * ext3_rsv_window_remove() -- unlink a window from the reservation rb tree | ||
296 | * @sb: super block | ||
297 | * @rsv: reservation window to remove | ||
298 | * | ||
299 | * Mark the block reservation window as not allocated, and unlink it | ||
300 | * from the filesystem reservation window rb tree. Must be called with | ||
301 | * rsv_lock hold. | ||
302 | */ | ||
249 | static void rsv_window_remove(struct super_block *sb, | 303 | static void rsv_window_remove(struct super_block *sb, |
250 | struct ext3_reserve_window_node *rsv) | 304 | struct ext3_reserve_window_node *rsv) |
251 | { | 305 | { |
@@ -255,11 +309,39 @@ static void rsv_window_remove(struct super_block *sb, | |||
255 | rb_erase(&rsv->rsv_node, &EXT3_SB(sb)->s_rsv_window_root); | 309 | rb_erase(&rsv->rsv_node, &EXT3_SB(sb)->s_rsv_window_root); |
256 | } | 310 | } |
257 | 311 | ||
312 | /* | ||
313 | * rsv_is_empty() -- Check if the reservation window is allocated. | ||
314 | * @rsv: given reservation window to check | ||
315 | * | ||
316 | * returns 1 if the end block is EXT3_RESERVE_WINDOW_NOT_ALLOCATED. | ||
317 | */ | ||
258 | static inline int rsv_is_empty(struct ext3_reserve_window *rsv) | 318 | static inline int rsv_is_empty(struct ext3_reserve_window *rsv) |
259 | { | 319 | { |
260 | /* a valid reservation end block could not be 0 */ | 320 | /* a valid reservation end block could not be 0 */ |
261 | return (rsv->_rsv_end == EXT3_RESERVE_WINDOW_NOT_ALLOCATED); | 321 | return rsv->_rsv_end == EXT3_RESERVE_WINDOW_NOT_ALLOCATED; |
262 | } | 322 | } |
323 | |||
324 | /** | ||
325 | * ext3_init_block_alloc_info() | ||
326 | * @inode: file inode structure | ||
327 | * | ||
328 | * Allocate and initialize the reservation window structure, and | ||
329 | * link the window to the ext3 inode structure at last | ||
330 | * | ||
331 | * The reservation window structure is only dynamically allocated | ||
332 | * and linked to ext3 inode the first time the open file | ||
333 | * needs a new block. So, before every ext3_new_block(s) call, for | ||
334 | * regular files, we should check whether the reservation window | ||
335 | * structure exists or not. In the latter case, this function is called. | ||
336 | * Fail to do so will result in block reservation being turned off for that | ||
337 | * open file. | ||
338 | * | ||
339 | * This function is called from ext3_get_blocks_handle(), also called | ||
340 | * when setting the reservation window size through ioctl before the file | ||
341 | * is open for write (needs block allocation). | ||
342 | * | ||
343 | * Needs truncate_mutex protection prior to call this function. | ||
344 | */ | ||
263 | void ext3_init_block_alloc_info(struct inode *inode) | 345 | void ext3_init_block_alloc_info(struct inode *inode) |
264 | { | 346 | { |
265 | struct ext3_inode_info *ei = EXT3_I(inode); | 347 | struct ext3_inode_info *ei = EXT3_I(inode); |
@@ -289,6 +371,19 @@ void ext3_init_block_alloc_info(struct inode *inode) | |||
289 | ei->i_block_alloc_info = block_i; | 371 | ei->i_block_alloc_info = block_i; |
290 | } | 372 | } |
291 | 373 | ||
374 | /** | ||
375 | * ext3_discard_reservation() | ||
376 | * @inode: inode | ||
377 | * | ||
378 | * Discard(free) block reservation window on last file close, or truncate | ||
379 | * or at last iput(). | ||
380 | * | ||
381 | * It is being called in three cases: | ||
382 | * ext3_release_file(): last writer close the file | ||
383 | * ext3_clear_inode(): last iput(), when nobody link to this file. | ||
384 | * ext3_truncate(): when the block indirect map is about to change. | ||
385 | * | ||
386 | */ | ||
292 | void ext3_discard_reservation(struct inode *inode) | 387 | void ext3_discard_reservation(struct inode *inode) |
293 | { | 388 | { |
294 | struct ext3_inode_info *ei = EXT3_I(inode); | 389 | struct ext3_inode_info *ei = EXT3_I(inode); |
@@ -308,7 +403,14 @@ void ext3_discard_reservation(struct inode *inode) | |||
308 | } | 403 | } |
309 | } | 404 | } |
310 | 405 | ||
311 | /* Free given blocks, update quota and i_blocks field */ | 406 | /** |
407 | * ext3_free_blocks_sb() -- Free given blocks and update quota | ||
408 | * @handle: handle to this transaction | ||
409 | * @sb: super block | ||
410 | * @block: start physcial block to free | ||
411 | * @count: number of blocks to free | ||
412 | * @pdquot_freed_blocks: pointer to quota | ||
413 | */ | ||
312 | void ext3_free_blocks_sb(handle_t *handle, struct super_block *sb, | 414 | void ext3_free_blocks_sb(handle_t *handle, struct super_block *sb, |
313 | ext3_fsblk_t block, unsigned long count, | 415 | ext3_fsblk_t block, unsigned long count, |
314 | unsigned long *pdquot_freed_blocks) | 416 | unsigned long *pdquot_freed_blocks) |
@@ -492,7 +594,13 @@ error_return: | |||
492 | return; | 594 | return; |
493 | } | 595 | } |
494 | 596 | ||
495 | /* Free given blocks, update quota and i_blocks field */ | 597 | /** |
598 | * ext3_free_blocks() -- Free given blocks and update quota | ||
599 | * @handle: handle for this transaction | ||
600 | * @inode: inode | ||
601 | * @block: start physical block to free | ||
602 | * @count: number of blocks to count | ||
603 | */ | ||
496 | void ext3_free_blocks(handle_t *handle, struct inode *inode, | 604 | void ext3_free_blocks(handle_t *handle, struct inode *inode, |
497 | ext3_fsblk_t block, unsigned long count) | 605 | ext3_fsblk_t block, unsigned long count) |
498 | { | 606 | { |
@@ -510,7 +618,11 @@ void ext3_free_blocks(handle_t *handle, struct inode *inode, | |||
510 | return; | 618 | return; |
511 | } | 619 | } |
512 | 620 | ||
513 | /* | 621 | /** |
622 | * ext3_test_allocatable() | ||
623 | * @nr: given allocation block group | ||
624 | * @bh: bufferhead contains the bitmap of the given block group | ||
625 | * | ||
514 | * For ext3 allocations, we must not reuse any blocks which are | 626 | * For ext3 allocations, we must not reuse any blocks which are |
515 | * allocated in the bitmap buffer's "last committed data" copy. This | 627 | * allocated in the bitmap buffer's "last committed data" copy. This |
516 | * prevents deletes from freeing up the page for reuse until we have | 628 | * prevents deletes from freeing up the page for reuse until we have |
@@ -543,6 +655,16 @@ static int ext3_test_allocatable(ext3_grpblk_t nr, struct buffer_head *bh) | |||
543 | return ret; | 655 | return ret; |
544 | } | 656 | } |
545 | 657 | ||
658 | /** | ||
659 | * bitmap_search_next_usable_block() | ||
660 | * @start: the starting block (group relative) of the search | ||
661 | * @bh: bufferhead contains the block group bitmap | ||
662 | * @maxblocks: the ending block (group relative) of the reservation | ||
663 | * | ||
664 | * The bitmap search --- search forward alternately through the actual | ||
665 | * bitmap on disk and the last-committed copy in journal, until we find a | ||
666 | * bit free in both bitmaps. | ||
667 | */ | ||
546 | static ext3_grpblk_t | 668 | static ext3_grpblk_t |
547 | bitmap_search_next_usable_block(ext3_grpblk_t start, struct buffer_head *bh, | 669 | bitmap_search_next_usable_block(ext3_grpblk_t start, struct buffer_head *bh, |
548 | ext3_grpblk_t maxblocks) | 670 | ext3_grpblk_t maxblocks) |
@@ -550,11 +672,6 @@ bitmap_search_next_usable_block(ext3_grpblk_t start, struct buffer_head *bh, | |||
550 | ext3_grpblk_t next; | 672 | ext3_grpblk_t next; |
551 | struct journal_head *jh = bh2jh(bh); | 673 | struct journal_head *jh = bh2jh(bh); |
552 | 674 | ||
553 | /* | ||
554 | * The bitmap search --- search forward alternately through the actual | ||
555 | * bitmap and the last-committed copy until we find a bit free in | ||
556 | * both | ||
557 | */ | ||
558 | while (start < maxblocks) { | 675 | while (start < maxblocks) { |
559 | next = ext3_find_next_zero_bit(bh->b_data, maxblocks, start); | 676 | next = ext3_find_next_zero_bit(bh->b_data, maxblocks, start); |
560 | if (next >= maxblocks) | 677 | if (next >= maxblocks) |
@@ -570,8 +687,14 @@ bitmap_search_next_usable_block(ext3_grpblk_t start, struct buffer_head *bh, | |||
570 | return -1; | 687 | return -1; |
571 | } | 688 | } |
572 | 689 | ||
573 | /* | 690 | /** |
574 | * Find an allocatable block in a bitmap. We honour both the bitmap and | 691 | * find_next_usable_block() |
692 | * @start: the starting block (group relative) to find next | ||
693 | * allocatable block in bitmap. | ||
694 | * @bh: bufferhead contains the block group bitmap | ||
695 | * @maxblocks: the ending block (group relative) for the search | ||
696 | * | ||
697 | * Find an allocatable block in a bitmap. We honor both the bitmap and | ||
575 | * its last-committed copy (if that exists), and perform the "most | 698 | * its last-committed copy (if that exists), and perform the "most |
576 | * appropriate allocation" algorithm of looking for a free block near | 699 | * appropriate allocation" algorithm of looking for a free block near |
577 | * the initial goal; then for a free byte somewhere in the bitmap; then | 700 | * the initial goal; then for a free byte somewhere in the bitmap; then |
@@ -622,7 +745,11 @@ find_next_usable_block(ext3_grpblk_t start, struct buffer_head *bh, | |||
622 | return here; | 745 | return here; |
623 | } | 746 | } |
624 | 747 | ||
625 | /* | 748 | /** |
749 | * claim_block() | ||
750 | * @block: the free block (group relative) to allocate | ||
751 | * @bh: the bufferhead containts the block group bitmap | ||
752 | * | ||
626 | * We think we can allocate this block in this bitmap. Try to set the bit. | 753 | * We think we can allocate this block in this bitmap. Try to set the bit. |
627 | * If that succeeds then check that nobody has allocated and then freed the | 754 | * If that succeeds then check that nobody has allocated and then freed the |
628 | * block since we saw that is was not marked in b_committed_data. If it _was_ | 755 | * block since we saw that is was not marked in b_committed_data. If it _was_ |
@@ -648,7 +775,26 @@ claim_block(spinlock_t *lock, ext3_grpblk_t block, struct buffer_head *bh) | |||
648 | return ret; | 775 | return ret; |
649 | } | 776 | } |
650 | 777 | ||
651 | /* | 778 | /** |
779 | * ext3_try_to_allocate() | ||
780 | * @sb: superblock | ||
781 | * @handle: handle to this transaction | ||
782 | * @group: given allocation block group | ||
783 | * @bitmap_bh: bufferhead holds the block bitmap | ||
784 | * @grp_goal: given target block within the group | ||
785 | * @count: target number of blocks to allocate | ||
786 | * @my_rsv: reservation window | ||
787 | * | ||
788 | * Attempt to allocate blocks within a give range. Set the range of allocation | ||
789 | * first, then find the first free bit(s) from the bitmap (within the range), | ||
790 | * and at last, allocate the blocks by claiming the found free bit as allocated. | ||
791 | * | ||
792 | * To set the range of this allocation: | ||
793 | * if there is a reservation window, only try to allocate block(s) from the | ||
794 | * file's own reservation window; | ||
795 | * Otherwise, the allocation range starts from the give goal block, ends at | ||
796 | * the block group's last block. | ||
797 | * | ||
652 | * If we failed to allocate the desired block then we may end up crossing to a | 798 | * If we failed to allocate the desired block then we may end up crossing to a |
653 | * new bitmap. In that case we must release write access to the old one via | 799 | * new bitmap. In that case we must release write access to the old one via |
654 | * ext3_journal_release_buffer(), else we'll run out of credits. | 800 | * ext3_journal_release_buffer(), else we'll run out of credits. |
@@ -705,7 +851,8 @@ repeat: | |||
705 | } | 851 | } |
706 | start = grp_goal; | 852 | start = grp_goal; |
707 | 853 | ||
708 | if (!claim_block(sb_bgl_lock(EXT3_SB(sb), group), grp_goal, bitmap_bh)) { | 854 | if (!claim_block(sb_bgl_lock(EXT3_SB(sb), group), |
855 | grp_goal, bitmap_bh)) { | ||
709 | /* | 856 | /* |
710 | * The block was allocated by another thread, or it was | 857 | * The block was allocated by another thread, or it was |
711 | * allocated and then freed by another thread | 858 | * allocated and then freed by another thread |
@@ -720,7 +867,8 @@ repeat: | |||
720 | grp_goal++; | 867 | grp_goal++; |
721 | while (num < *count && grp_goal < end | 868 | while (num < *count && grp_goal < end |
722 | && ext3_test_allocatable(grp_goal, bitmap_bh) | 869 | && ext3_test_allocatable(grp_goal, bitmap_bh) |
723 | && claim_block(sb_bgl_lock(EXT3_SB(sb), group), grp_goal, bitmap_bh)) { | 870 | && claim_block(sb_bgl_lock(EXT3_SB(sb), group), |
871 | grp_goal, bitmap_bh)) { | ||
724 | num++; | 872 | num++; |
725 | grp_goal++; | 873 | grp_goal++; |
726 | } | 874 | } |
@@ -931,9 +1079,10 @@ static int alloc_new_reservation(struct ext3_reserve_window_node *my_rsv, | |||
931 | if ((my_rsv->rsv_alloc_hit > | 1079 | if ((my_rsv->rsv_alloc_hit > |
932 | (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) { | 1080 | (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) { |
933 | /* | 1081 | /* |
934 | * if we previously allocation hit ration is greater than half | 1082 | * if the previously allocation hit ratio is |
935 | * we double the size of reservation window next time | 1083 | * greater than 1/2, then we double the size of |
936 | * otherwise keep the same | 1084 | * the reservation window the next time, |
1085 | * otherwise we keep the same size window | ||
937 | */ | 1086 | */ |
938 | size = size * 2; | 1087 | size = size * 2; |
939 | if (size > EXT3_MAX_RESERVE_BLOCKS) | 1088 | if (size > EXT3_MAX_RESERVE_BLOCKS) |
@@ -1012,6 +1161,23 @@ retry: | |||
1012 | goto retry; | 1161 | goto retry; |
1013 | } | 1162 | } |
1014 | 1163 | ||
1164 | /** | ||
1165 | * try_to_extend_reservation() | ||
1166 | * @my_rsv: given reservation window | ||
1167 | * @sb: super block | ||
1168 | * @size: the delta to extend | ||
1169 | * | ||
1170 | * Attempt to expand the reservation window large enough to have | ||
1171 | * required number of free blocks | ||
1172 | * | ||
1173 | * Since ext3_try_to_allocate() will always allocate blocks within | ||
1174 | * the reservation window range, if the window size is too small, | ||
1175 | * multiple blocks allocation has to stop at the end of the reservation | ||
1176 | * window. To make this more efficient, given the total number of | ||
1177 | * blocks needed and the current size of the window, we try to | ||
1178 | * expand the reservation window size if necessary on a best-effort | ||
1179 | * basis before ext3_new_blocks() tries to allocate blocks, | ||
1180 | */ | ||
1015 | static void try_to_extend_reservation(struct ext3_reserve_window_node *my_rsv, | 1181 | static void try_to_extend_reservation(struct ext3_reserve_window_node *my_rsv, |
1016 | struct super_block *sb, int size) | 1182 | struct super_block *sb, int size) |
1017 | { | 1183 | { |
@@ -1037,7 +1203,17 @@ static void try_to_extend_reservation(struct ext3_reserve_window_node *my_rsv, | |||
1037 | spin_unlock(rsv_lock); | 1203 | spin_unlock(rsv_lock); |
1038 | } | 1204 | } |
1039 | 1205 | ||
1040 | /* | 1206 | /** |
1207 | * ext3_try_to_allocate_with_rsv() | ||
1208 | * @sb: superblock | ||
1209 | * @handle: handle to this transaction | ||
1210 | * @group: given allocation block group | ||
1211 | * @bitmap_bh: bufferhead holds the block bitmap | ||
1212 | * @grp_goal: given target block within the group | ||
1213 | * @count: target number of blocks to allocate | ||
1214 | * @my_rsv: reservation window | ||
1215 | * @errp: pointer to store the error code | ||
1216 | * | ||
1041 | * This is the main function used to allocate a new block and its reservation | 1217 | * This is the main function used to allocate a new block and its reservation |
1042 | * window. | 1218 | * window. |
1043 | * | 1219 | * |
@@ -1053,9 +1229,7 @@ static void try_to_extend_reservation(struct ext3_reserve_window_node *my_rsv, | |||
1053 | * reservation), and there are lots of free blocks, but they are all | 1229 | * reservation), and there are lots of free blocks, but they are all |
1054 | * being reserved. | 1230 | * being reserved. |
1055 | * | 1231 | * |
1056 | * We use a sorted double linked list for the per-filesystem reservation list. | 1232 | * We use a red-black tree for the per-filesystem reservation list. |
1057 | * The insert, remove and find a free space(non-reserved) operations for the | ||
1058 | * sorted double linked list should be fast. | ||
1059 | * | 1233 | * |
1060 | */ | 1234 | */ |
1061 | static ext3_grpblk_t | 1235 | static ext3_grpblk_t |
@@ -1120,7 +1294,8 @@ ext3_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle, | |||
1120 | */ | 1294 | */ |
1121 | while (1) { | 1295 | while (1) { |
1122 | if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) || | 1296 | if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) || |
1123 | !goal_in_my_reservation(&my_rsv->rsv_window, grp_goal, group, sb)) { | 1297 | !goal_in_my_reservation(&my_rsv->rsv_window, |
1298 | grp_goal, group, sb)) { | ||
1124 | if (my_rsv->rsv_goal_size < *count) | 1299 | if (my_rsv->rsv_goal_size < *count) |
1125 | my_rsv->rsv_goal_size = *count; | 1300 | my_rsv->rsv_goal_size = *count; |
1126 | ret = alloc_new_reservation(my_rsv, grp_goal, sb, | 1301 | ret = alloc_new_reservation(my_rsv, grp_goal, sb, |
@@ -1128,19 +1303,22 @@ ext3_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle, | |||
1128 | if (ret < 0) | 1303 | if (ret < 0) |
1129 | break; /* failed */ | 1304 | break; /* failed */ |
1130 | 1305 | ||
1131 | if (!goal_in_my_reservation(&my_rsv->rsv_window, grp_goal, group, sb)) | 1306 | if (!goal_in_my_reservation(&my_rsv->rsv_window, |
1307 | grp_goal, group, sb)) | ||
1132 | grp_goal = -1; | 1308 | grp_goal = -1; |
1133 | } else if (grp_goal > 0 && (my_rsv->rsv_end-grp_goal+1) < *count) | 1309 | } else if (grp_goal > 0 && |
1310 | (my_rsv->rsv_end-grp_goal+1) < *count) | ||
1134 | try_to_extend_reservation(my_rsv, sb, | 1311 | try_to_extend_reservation(my_rsv, sb, |
1135 | *count-my_rsv->rsv_end + grp_goal - 1); | 1312 | *count-my_rsv->rsv_end + grp_goal - 1); |
1136 | 1313 | ||
1137 | if ((my_rsv->rsv_start >= group_first_block + EXT3_BLOCKS_PER_GROUP(sb)) | 1314 | if ((my_rsv->rsv_start >= group_first_block + |
1315 | EXT3_BLOCKS_PER_GROUP(sb)) | ||
1138 | || (my_rsv->rsv_end < group_first_block)) { | 1316 | || (my_rsv->rsv_end < group_first_block)) { |
1139 | rsv_window_dump(&EXT3_SB(sb)->s_rsv_window_root, 1); | 1317 | rsv_window_dump(&EXT3_SB(sb)->s_rsv_window_root, 1); |
1140 | BUG(); | 1318 | BUG(); |
1141 | } | 1319 | } |
1142 | ret = ext3_try_to_allocate(sb, handle, group, bitmap_bh, grp_goal, | 1320 | ret = ext3_try_to_allocate(sb, handle, group, bitmap_bh, |
1143 | &num, &my_rsv->rsv_window); | 1321 | grp_goal, &num, &my_rsv->rsv_window); |
1144 | if (ret >= 0) { | 1322 | if (ret >= 0) { |
1145 | my_rsv->rsv_alloc_hit += num; | 1323 | my_rsv->rsv_alloc_hit += num; |
1146 | *count = num; | 1324 | *count = num; |
@@ -1165,6 +1343,12 @@ out: | |||
1165 | return ret; | 1343 | return ret; |
1166 | } | 1344 | } |
1167 | 1345 | ||
1346 | /** | ||
1347 | * ext3_has_free_blocks() | ||
1348 | * @sbi: in-core super block structure. | ||
1349 | * | ||
1350 | * Check if filesystem has at least 1 free block available for allocation. | ||
1351 | */ | ||
1168 | static int ext3_has_free_blocks(struct ext3_sb_info *sbi) | 1352 | static int ext3_has_free_blocks(struct ext3_sb_info *sbi) |
1169 | { | 1353 | { |
1170 | ext3_fsblk_t free_blocks, root_blocks; | 1354 | ext3_fsblk_t free_blocks, root_blocks; |
@@ -1179,11 +1363,17 @@ static int ext3_has_free_blocks(struct ext3_sb_info *sbi) | |||
1179 | return 1; | 1363 | return 1; |
1180 | } | 1364 | } |
1181 | 1365 | ||
1182 | /* | 1366 | /** |
1367 | * ext3_should_retry_alloc() | ||
1368 | * @sb: super block | ||
1369 | * @retries number of attemps has been made | ||
1370 | * | ||
1183 | * ext3_should_retry_alloc() is called when ENOSPC is returned, and if | 1371 | * ext3_should_retry_alloc() is called when ENOSPC is returned, and if |
1184 | * it is profitable to retry the operation, this function will wait | 1372 | * it is profitable to retry the operation, this function will wait |
1185 | * for the current or commiting transaction to complete, and then | 1373 | * for the current or commiting transaction to complete, and then |
1186 | * return TRUE. | 1374 | * return TRUE. |
1375 | * | ||
1376 | * if the total number of retries exceed three times, return FALSE. | ||
1187 | */ | 1377 | */ |
1188 | int ext3_should_retry_alloc(struct super_block *sb, int *retries) | 1378 | int ext3_should_retry_alloc(struct super_block *sb, int *retries) |
1189 | { | 1379 | { |
@@ -1195,13 +1385,19 @@ int ext3_should_retry_alloc(struct super_block *sb, int *retries) | |||
1195 | return journal_force_commit_nested(EXT3_SB(sb)->s_journal); | 1385 | return journal_force_commit_nested(EXT3_SB(sb)->s_journal); |
1196 | } | 1386 | } |
1197 | 1387 | ||
1198 | /* | 1388 | /** |
1199 | * ext3_new_block uses a goal block to assist allocation. If the goal is | 1389 | * ext3_new_blocks() -- core block(s) allocation function |
1200 | * free, or there is a free block within 32 blocks of the goal, that block | 1390 | * @handle: handle to this transaction |
1201 | * is allocated. Otherwise a forward search is made for a free block; within | 1391 | * @inode: file inode |
1202 | * each block group the search first looks for an entire free byte in the block | 1392 | * @goal: given target block(filesystem wide) |
1203 | * bitmap, and then for any free bit if that fails. | 1393 | * @count: target number of blocks to allocate |
1204 | * This function also updates quota and i_blocks field. | 1394 | * @errp: error code |
1395 | * | ||
1396 | * ext3_new_blocks uses a goal block to assist allocation. It tries to | ||
1397 | * allocate block(s) from the block group contains the goal block first. If that | ||
1398 | * fails, it will try to allocate block(s) from other block groups without | ||
1399 | * any specific goal block. | ||
1400 | * | ||
1205 | */ | 1401 | */ |
1206 | ext3_fsblk_t ext3_new_blocks(handle_t *handle, struct inode *inode, | 1402 | ext3_fsblk_t ext3_new_blocks(handle_t *handle, struct inode *inode, |
1207 | ext3_fsblk_t goal, unsigned long *count, int *errp) | 1403 | ext3_fsblk_t goal, unsigned long *count, int *errp) |
@@ -1432,7 +1628,7 @@ allocated: | |||
1432 | 1628 | ||
1433 | spin_lock(sb_bgl_lock(sbi, group_no)); | 1629 | spin_lock(sb_bgl_lock(sbi, group_no)); |
1434 | gdp->bg_free_blocks_count = | 1630 | gdp->bg_free_blocks_count = |
1435 | cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) - num); | 1631 | cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)-num); |
1436 | spin_unlock(sb_bgl_lock(sbi, group_no)); | 1632 | spin_unlock(sb_bgl_lock(sbi, group_no)); |
1437 | percpu_counter_mod(&sbi->s_freeblocks_counter, -num); | 1633 | percpu_counter_mod(&sbi->s_freeblocks_counter, -num); |
1438 | 1634 | ||
@@ -1475,6 +1671,12 @@ ext3_fsblk_t ext3_new_block(handle_t *handle, struct inode *inode, | |||
1475 | return ext3_new_blocks(handle, inode, goal, &count, errp); | 1671 | return ext3_new_blocks(handle, inode, goal, &count, errp); |
1476 | } | 1672 | } |
1477 | 1673 | ||
1674 | /** | ||
1675 | * ext3_count_free_blocks() -- count filesystem free blocks | ||
1676 | * @sb: superblock | ||
1677 | * | ||
1678 | * Adds up the number of free blocks from each block group. | ||
1679 | */ | ||
1478 | ext3_fsblk_t ext3_count_free_blocks(struct super_block *sb) | 1680 | ext3_fsblk_t ext3_count_free_blocks(struct super_block *sb) |
1479 | { | 1681 | { |
1480 | ext3_fsblk_t desc_count; | 1682 | ext3_fsblk_t desc_count; |