aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ext2/balloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ext2/balloc.c')
-rw-r--r--fs/ext2/balloc.c1305
1 files changed, 1072 insertions, 233 deletions
diff --git a/fs/ext2/balloc.c b/fs/ext2/balloc.c
index ffaa6d845442..18a42de25b55 100644
--- a/fs/ext2/balloc.c
+++ b/fs/ext2/balloc.c
@@ -133,41 +133,6 @@ error_out:
133 return NULL; 133 return NULL;
134} 134}
135 135
136/*
137 * Set sb->s_dirt here because the superblock was "logically" altered. We
138 * need to recalculate its free blocks count and flush it out.
139 */
140static int reserve_blocks(struct super_block *sb, int count)
141{
142 struct ext2_sb_info *sbi = EXT2_SB(sb);
143 struct ext2_super_block *es = sbi->s_es;
144 unsigned free_blocks;
145 unsigned root_blocks;
146
147 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
148 root_blocks = le32_to_cpu(es->s_r_blocks_count);
149
150 if (free_blocks < count)
151 count = free_blocks;
152
153 if (free_blocks < root_blocks + count && !capable(CAP_SYS_RESOURCE) &&
154 sbi->s_resuid != current->fsuid &&
155 (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) {
156 /*
157 * We are too close to reserve and we are not privileged.
158 * Can we allocate anything at all?
159 */
160 if (free_blocks > root_blocks)
161 count = free_blocks - root_blocks;
162 else
163 return 0;
164 }
165
166 percpu_counter_sub(&sbi->s_freeblocks_counter, count);
167 sb->s_dirt = 1;
168 return count;
169}
170
171static void release_blocks(struct super_block *sb, int count) 136static void release_blocks(struct super_block *sb, int count)
172{ 137{
173 if (count) { 138 if (count) {
@@ -178,25 +143,7 @@ static void release_blocks(struct super_block *sb, int count)
178 } 143 }
179} 144}
180 145
181static int group_reserve_blocks(struct ext2_sb_info *sbi, int group_no, 146static void group_adjust_blocks(struct super_block *sb, int group_no,
182 struct ext2_group_desc *desc, struct buffer_head *bh, int count)
183{
184 unsigned free_blocks;
185
186 if (!desc->bg_free_blocks_count)
187 return 0;
188
189 spin_lock(sb_bgl_lock(sbi, group_no));
190 free_blocks = le16_to_cpu(desc->bg_free_blocks_count);
191 if (free_blocks < count)
192 count = free_blocks;
193 desc->bg_free_blocks_count = cpu_to_le16(free_blocks - count);
194 spin_unlock(sb_bgl_lock(sbi, group_no));
195 mark_buffer_dirty(bh);
196 return count;
197}
198
199static void group_release_blocks(struct super_block *sb, int group_no,
200 struct ext2_group_desc *desc, struct buffer_head *bh, int count) 147 struct ext2_group_desc *desc, struct buffer_head *bh, int count)
201{ 148{
202 if (count) { 149 if (count) {
@@ -212,7 +159,306 @@ static void group_release_blocks(struct super_block *sb, int group_no,
212 } 159 }
213} 160}
214 161
215/* Free given blocks, update quota and i_blocks field */ 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
184static 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
191restart:
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 */
249static int
250goal_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 */
276static struct ext2_reserve_window_node *
277search_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 */
315void 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 */
354static 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 */
369static 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 */
396void 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 */
437void 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 */
216void ext2_free_blocks (struct inode * inode, unsigned long block, 462void ext2_free_blocks (struct inode * inode, unsigned long block,
217 unsigned long count) 463 unsigned long count)
218{ 464{
@@ -287,7 +533,7 @@ do_more:
287 if (sb->s_flags & MS_SYNCHRONOUS) 533 if (sb->s_flags & MS_SYNCHRONOUS)
288 sync_dirty_buffer(bitmap_bh); 534 sync_dirty_buffer(bitmap_bh);
289 535
290 group_release_blocks(sb, block_group, desc, bh2, group_freed); 536 group_adjust_blocks(sb, block_group, desc, bh2, group_freed);
291 freed += group_freed; 537 freed += group_freed;
292 538
293 if (overflow) { 539 if (overflow) {
@@ -301,16 +547,46 @@ error_return:
301 DQUOT_FREE_BLOCK(inode, freed); 547 DQUOT_FREE_BLOCK(inode, freed);
302} 548}
303 549
304static int grab_block(spinlock_t *lock, char *map, unsigned size, int goal) 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 */
559static ext2_grpblk_t
560bitmap_search_next_usable_block(ext2_grpblk_t start, struct buffer_head *bh,
561 ext2_grpblk_t maxblocks)
305{ 562{
306 int k; 563 ext2_grpblk_t next;
307 char *p, *r;
308 564
309 if (!ext2_test_bit(goal, map)) 565 next = ext2_find_next_zero_bit(bh->b_data, maxblocks, start);
310 goto got_it; 566 if (next >= maxblocks)
567 return -1;
568 return next;
569}
311 570
312repeat: 571/**
313 if (goal) { 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 */
583static ext2_grpblk_t
584find_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) {
314 /* 590 /*
315 * The goal was occupied; search forward for a free 591 * The goal was occupied; search forward for a free
316 * block within the next XX blocks. 592 * block within the next XX blocks.
@@ -319,244 +595,807 @@ repeat:
319 * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the 595 * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the
320 * next 64-bit boundary is simple.. 596 * next 64-bit boundary is simple..
321 */ 597 */
322 k = (goal + 63) & ~63; 598 ext2_grpblk_t end_goal = (start + 63) & ~63;
323 goal = ext2_find_next_zero_bit(map, k, goal); 599 if (end_goal > maxblocks)
324 if (goal < k) 600 end_goal = maxblocks;
325 goto got_it; 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 */
645static int
646ext2_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
681repeat:
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;
720fail_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 */
758static 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
326 /* 797 /*
327 * Search in the remainder of the current group. 798 * Reached the last reservation, we can just append to the
799 * previous one.
328 */ 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 }
329 } 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 */
330 822
331 p = map + (goal >> 3); 823 if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window)))
332 r = memscan(p, 0, (size - goal + 7) >> 3); 824 rsv_window_remove(sb, my_rsv);
333 k = (r - map) << 3; 825
334 if (k < size) { 826 /*
335 /* 827 * Let's book the whole avaliable window for now. We will check the
336 * We have succeeded in finding a free byte in the block 828 * disk bitmap later and then, if there are free blocks then we adjust
337 * bitmap. Now search backwards to find the start of this 829 * the window size if it's larger than requested.
338 * group of free blocks - won't take more than 7 iterations. 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 */
880static 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.
339 */ 915 */
340 for (goal = k; goal && !ext2_test_bit (goal - 1, map); goal--) 916
341 ; 917 if ((my_rsv->rsv_start <= group_end_block) &&
342 goto got_it; 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 }
343 } 935 }
344 936
345 k = ext2_find_next_zero_bit ((u32 *)map, size, goal); 937 spin_lock(rsv_lock);
346 if (k < size) { 938 /*
347 goal = k; 939 * shift the search start to the window near the goal block
348 goto got_it; 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 */
950retry:
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;
349 } 959 }
350 return -1; 960
351got_it: 961 /*
352 if (ext2_set_bit_atomic(lock, goal, (void *) map)) 962 * On success, find_next_reservable_window() returns the
353 goto repeat; 963 * reservation window where there is a reservable space after it.
354 return goal; 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 */
1022static 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 */
1073static ext2_grpblk_t
1074ext2_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 */
1163static 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;
355} 1175}
356 1176
357/* 1177/*
358 * ext2_new_block uses a goal block to assist allocation. If the goal is 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
359 * free, or there is a free block within 32 blocks of the goal, that block 1185 * free, or there is a free block within 32 blocks of the goal, that block
360 * is allocated. Otherwise a forward search is made for a free block; within 1186 * is allocated. Otherwise a forward search is made for a free block; within
361 * each block group the search first looks for an entire free byte in the block 1187 * each block group the search first looks for an entire free byte in the block
362 * bitmap, and then for any free bit if that fails. 1188 * bitmap, and then for any free bit if that fails.
363 * This function also updates quota and i_blocks field. 1189 * This function also updates quota and i_blocks field.
364 */ 1190 */
365int ext2_new_block(struct inode *inode, unsigned long goal, 1191ext2_fsblk_t ext2_new_blocks(struct inode *inode, ext2_fsblk_t goal,
366 u32 *prealloc_count, u32 *prealloc_block, int *err) 1192 unsigned long *count, int *errp)
367{ 1193{
368 struct buffer_head *bitmap_bh = NULL; 1194 struct buffer_head *bitmap_bh = NULL;
369 struct buffer_head *gdp_bh; /* bh2 */ 1195 struct buffer_head *gdp_bh;
370 struct ext2_group_desc *desc; 1196 int group_no;
371 int group_no; /* i */ 1197 int goal_group;
372 int ret_block; /* j */ 1198 ext2_grpblk_t grp_target_blk; /* blockgroup relative goal block */
373 int group_idx; /* k */ 1199 ext2_grpblk_t grp_alloc_blk; /* blockgroup-relative allocated block*/
374 int target_block; /* tmp */ 1200 ext2_fsblk_t ret_block; /* filesyetem-wide allocated block */
375 int block = 0; 1201 int bgi; /* blockgroup iteration index */
376 struct super_block *sb = inode->i_sb; 1202 int performed_allocation = 0;
377 struct ext2_sb_info *sbi = EXT2_SB(sb); 1203 ext2_grpblk_t free_blocks; /* number of free blocks in a group */
378 struct ext2_super_block *es = sbi->s_es; 1204 struct super_block *sb;
379 unsigned group_size = EXT2_BLOCKS_PER_GROUP(sb); 1205 struct ext2_group_desc *gdp;
380 unsigned prealloc_goal = es->s_prealloc_blocks; 1206 struct ext2_super_block *es;
381 unsigned group_alloc = 0, es_alloc, dq_alloc; 1207 struct ext2_sb_info *sbi;
382 int nr_scanned_groups; 1208 struct ext2_reserve_window_node *my_rsv = NULL;
383 1209 struct ext2_block_alloc_info *block_i;
384 if (!prealloc_goal--) 1210 unsigned short windowsz = 0;
385 prealloc_goal = EXT2_DEFAULT_PREALLOC_BLOCKS - 1; 1211 unsigned long ngroups;
386 if (!prealloc_count || *prealloc_count) 1212 unsigned long num = *count;
387 prealloc_goal = 0; 1213
388 1214 *errp = -ENOSPC;
389 if (DQUOT_ALLOC_BLOCK(inode, 1)) { 1215 sb = inode->i_sb;
390 *err = -EDQUOT; 1216 if (!sb) {
391 goto out; 1217 printk("ext2_new_blocks: nonexistent device");
1218 return 0;
392 } 1219 }
393 1220
394 while (prealloc_goal && DQUOT_PREALLOC_BLOCK(inode, prealloc_goal)) 1221 /*
395 prealloc_goal--; 1222 * Check quota for allocation of this block.
1223 */
1224 if (DQUOT_ALLOC_BLOCK(inode, num)) {
1225 *errp = -EDQUOT;
1226 return 0;
1227 }
396 1228
397 dq_alloc = prealloc_goal + 1; 1229 sbi = EXT2_SB(sb);
398 es_alloc = reserve_blocks(sb, dq_alloc); 1230 es = EXT2_SB(sb)->s_es;
399 if (!es_alloc) { 1231 ext2_debug("goal=%lu.\n", goal);
400 *err = -ENOSPC; 1232 /*
401 goto out_dquot; 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;
402 } 1245 }
403 1246
404 ext2_debug ("goal=%lu.\n", goal); 1247 if (!ext2_has_free_blocks(sbi)) {
1248 *errp = -ENOSPC;
1249 goto out;
1250 }
405 1251
1252 /*
1253 * First, test whether the goal block is free.
1254 */
406 if (goal < le32_to_cpu(es->s_first_data_block) || 1255 if (goal < le32_to_cpu(es->s_first_data_block) ||
407 goal >= le32_to_cpu(es->s_blocks_count)) 1256 goal >= le32_to_cpu(es->s_blocks_count))
408 goal = le32_to_cpu(es->s_first_data_block); 1257 goal = le32_to_cpu(es->s_first_data_block);
409 group_no = (goal - le32_to_cpu(es->s_first_data_block)) / group_size; 1258 group_no = (goal - le32_to_cpu(es->s_first_data_block)) /
410 desc = ext2_get_group_desc (sb, group_no, &gdp_bh); 1259 EXT2_BLOCKS_PER_GROUP(sb);
411 if (!desc) { 1260 goal_group = group_no;
412 /* 1261retry_alloc:
413 * gdp_bh may still be uninitialised. But group_release_blocks 1262 gdp = ext2_get_group_desc(sb, group_no, &gdp_bh);
414 * will not touch it because group_alloc is zero. 1263 if (!gdp)
415 */
416 goto io_error; 1264 goto io_error;
417 }
418 1265
419 group_alloc = group_reserve_blocks(sbi, group_no, desc, 1266 free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
420 gdp_bh, es_alloc); 1267 /*
421 if (group_alloc) { 1268 * if there is not enough free blocks to make a new resevation
422 ret_block = ((goal - le32_to_cpu(es->s_first_data_block)) % 1269 * turn off reservation for this allocation
423 group_size); 1270 */
424 brelse(bitmap_bh); 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));
425 bitmap_bh = read_block_bitmap(sb, group_no); 1278 bitmap_bh = read_block_bitmap(sb, group_no);
426 if (!bitmap_bh) 1279 if (!bitmap_bh)
427 goto io_error; 1280 goto io_error;
428 1281 grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no,
429 ext2_debug("goal is at %d:%d.\n", group_no, ret_block); 1282 bitmap_bh, grp_target_blk,
430 1283 my_rsv, &num);
431 ret_block = grab_block(sb_bgl_lock(sbi, group_no), 1284 if (grp_alloc_blk >= 0)
432 bitmap_bh->b_data, group_size, ret_block); 1285 goto allocated;
433 if (ret_block >= 0)
434 goto got_block;
435 group_release_blocks(sb, group_no, desc, gdp_bh, group_alloc);
436 group_alloc = 0;
437 } 1286 }
438 1287
439 ext2_debug ("Bit not found in block group %d.\n", group_no); 1288 ngroups = EXT2_SB(sb)->s_groups_count;
1289 smp_rmb();
440 1290
441 /* 1291 /*
442 * Now search the rest of the groups. We assume that 1292 * Now search the rest of the groups. We assume that
443 * i and desc correctly point to the last group visited. 1293 * i and gdp correctly point to the last group visited.
444 */ 1294 */
445 nr_scanned_groups = 0; 1295 for (bgi = 0; bgi < ngroups; bgi++) {
446retry:
447 for (group_idx = 0; !group_alloc &&
448 group_idx < sbi->s_groups_count; group_idx++) {
449 group_no++; 1296 group_no++;
450 if (group_no >= sbi->s_groups_count) 1297 if (group_no >= ngroups)
451 group_no = 0; 1298 group_no = 0;
452 desc = ext2_get_group_desc(sb, group_no, &gdp_bh); 1299 gdp = ext2_get_group_desc(sb, group_no, &gdp_bh);
453 if (!desc) 1300 if (!gdp)
454 goto io_error; 1301 goto io_error;
455 group_alloc = group_reserve_blocks(sbi, group_no, desc,
456 gdp_bh, es_alloc);
457 }
458 if (!group_alloc) {
459 *err = -ENOSPC;
460 goto out_release;
461 }
462 brelse(bitmap_bh);
463 bitmap_bh = read_block_bitmap(sb, group_no);
464 if (!bitmap_bh)
465 goto io_error;
466 1302
467 ret_block = grab_block(sb_bgl_lock(sbi, group_no), bitmap_bh->b_data, 1303 free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
468 group_size, 0);
469 if (ret_block < 0) {
470 /* 1304 /*
471 * If a free block counter is corrupted we can loop inifintely. 1305 * skip this group if the number of
472 * Detect that here. 1306 * free blocks is less than half of the reservation
1307 * window size.
473 */ 1308 */
474 nr_scanned_groups++; 1309 if (free_blocks <= (windowsz/2))
475 if (nr_scanned_groups > 2 * sbi->s_groups_count) { 1310 continue;
476 ext2_error(sb, "ext2_new_block", 1311
477 "corrupted free blocks counters"); 1312 brelse(bitmap_bh);
1313 bitmap_bh = read_block_bitmap(sb, group_no);
1314 if (!bitmap_bh)
478 goto io_error; 1315 goto io_error;
479 }
480 /* 1316 /*
481 * Someone else grabbed the last free block in this blockgroup 1317 * try to allocate block(s) from this group, without a goal(-1).
482 * before us. Retry the scan.
483 */ 1318 */
484 group_release_blocks(sb, group_no, desc, gdp_bh, group_alloc); 1319 grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no,
485 group_alloc = 0; 1320 bitmap_bh, -1, my_rsv, &num);
486 goto retry; 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;
487 } 1336 }
1337 /* No space left on the device */
1338 *errp = -ENOSPC;
1339 goto out;
1340
1341allocated:
488 1342
489got_block:
490 ext2_debug("using block group %d(%d)\n", 1343 ext2_debug("using block group %d(%d)\n",
491 group_no, desc->bg_free_blocks_count); 1344 group_no, gdp->bg_free_blocks_count);
492 1345
493 target_block = ret_block + group_no * group_size + 1346 ret_block = grp_alloc_blk + ext2_group_first_block_no(sb, group_no);
494 le32_to_cpu(es->s_first_data_block);
495 1347
496 if (target_block == le32_to_cpu(desc->bg_block_bitmap) || 1348 if (in_range(le32_to_cpu(gdp->bg_block_bitmap), ret_block, num) ||
497 target_block == le32_to_cpu(desc->bg_inode_bitmap) || 1349 in_range(le32_to_cpu(gdp->bg_inode_bitmap), ret_block, num) ||
498 in_range(target_block, le32_to_cpu(desc->bg_inode_table), 1350 in_range(ret_block, le32_to_cpu(gdp->bg_inode_table),
499 sbi->s_itb_per_group)) 1351 EXT2_SB(sb)->s_itb_per_group) ||
500 ext2_error (sb, "ext2_new_block", 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",
501 "Allocating block in system zone - " 1355 "Allocating block in system zone - "
502 "block = %u", target_block); 1356 "blocks from "E2FSBLK", length %lu",
1357 ret_block, num);
503 1358
504 if (target_block >= le32_to_cpu(es->s_blocks_count)) { 1359 performed_allocation = 1;
505 ext2_error (sb, "ext2_new_block", 1360
506 "block(%d) >= blocks count(%d) - " 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) - "
507 "block_group = %d, es == %p ", ret_block, 1364 "block_group = %d, es == %p ", ret_block,
508 le32_to_cpu(es->s_blocks_count), group_no, es); 1365 le32_to_cpu(es->s_blocks_count), group_no, es);
509 goto io_error; 1366 goto out;
510 } 1367 }
511 block = target_block;
512
513 /* OK, we _had_ allocated something */
514 ext2_debug("found bit %d\n", ret_block);
515
516 dq_alloc--;
517 es_alloc--;
518 group_alloc--;
519 1368
520 /* 1369 group_adjust_blocks(sb, group_no, gdp, gdp_bh, -num);
521 * Do block preallocation now if required. 1370 percpu_counter_sub(&sbi->s_freeblocks_counter, num);
522 */
523 write_lock(&EXT2_I(inode)->i_meta_lock);
524 if (group_alloc && !*prealloc_count) {
525 unsigned n;
526
527 for (n = 0; n < group_alloc && ++ret_block < group_size; n++) {
528 if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group_no),
529 ret_block,
530 (void*) bitmap_bh->b_data))
531 break;
532 }
533 *prealloc_block = block + 1;
534 *prealloc_count = n;
535 es_alloc -= n;
536 dq_alloc -= n;
537 group_alloc -= n;
538 }
539 write_unlock(&EXT2_I(inode)->i_meta_lock);
540 1371
541 mark_buffer_dirty(bitmap_bh); 1372 mark_buffer_dirty(bitmap_bh);
542 if (sb->s_flags & MS_SYNCHRONOUS) 1373 if (sb->s_flags & MS_SYNCHRONOUS)
543 sync_dirty_buffer(bitmap_bh); 1374 sync_dirty_buffer(bitmap_bh);
544 1375
545 ext2_debug ("allocating block %d. ", block); 1376 *errp = 0;
1377 brelse(bitmap_bh);
1378 DQUOT_FREE_BLOCK(inode, *count-num);
1379 *count = num;
1380 return ret_block;
546 1381
547 *err = 0; 1382io_error:
548out_release: 1383 *errp = -EIO;
549 group_release_blocks(sb, group_no, desc, gdp_bh, group_alloc);
550 release_blocks(sb, es_alloc);
551out_dquot:
552 DQUOT_FREE_BLOCK(inode, dq_alloc);
553out: 1384out:
1385 /*
1386 * Undo the block allocation
1387 */
1388 if (!performed_allocation)
1389 DQUOT_FREE_BLOCK(inode, *count);
554 brelse(bitmap_bh); 1390 brelse(bitmap_bh);
555 return block; 1391 return 0;
1392}
556 1393
557io_error: 1394ext2_fsblk_t ext2_new_block(struct inode *inode, unsigned long goal, int *errp)
558 *err = -EIO; 1395{
559 goto out_release; 1396 unsigned long count = 1;
1397
1398 return ext2_new_blocks(inode, goal, &count, errp);
560} 1399}
561 1400
562#ifdef EXT2FS_DEBUG 1401#ifdef EXT2FS_DEBUG