aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin J. Bligh <mbligh@mbligh.org>2007-10-17 02:30:46 -0400
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-10-17 11:43:02 -0400
commita686cd898bd999fd026a51e90fb0a3410d258ddb (patch)
tree3f0a91a70fe1a3addf9e99f3babed9e9baba4b99
parent369f2389e7d03022abdd25e298bffb9613cd0e54 (diff)
ext2 reservations
Val's cross-port of the ext3 reservations code into ext2. [mbligh@mbligh.org: Small type error for printk [akpm@linux-foundation.org: fix types, sync with ext3] [mbligh@mbligh.org: Bring ext2 reservations code in line with latest ext3] [akpm@linux-foundation.org: kill noisy printk] [akpm@linux-foundation.org: remember to dirty the gdp's block] [akpm@linux-foundation.org: cross-port the missed 5dea5176e5c32ef9f0d1a41d28427b3bf6881b3a] [akpm@linux-foundation.org: cross-port e6022603b9aa7d61d20b392e69edcdbbc1789969] [akpm@linux-foundation.org: Port the omitted 08fb306fe63d98eb86e3b16f4cc21816fa47f18e] [akpm@linux-foundation.org: Backport the missed 20acaa18d0c002fec180956f87adeb3f11f635a6] [akpm@linux-foundation.org: fixes] [cmm@us.ibm.com: fix reservation extension] [bunk@stusta.de: make ext2_get_blocks() static] [hugh@veritas.com: fix hang] [hugh@veritas.com: ext2_new_blocks should reset the reservation window size] [hugh@veritas.com: ext2 balloc: fix off-by-one against rsv_end] [hugh@veritas.com: grp_goal 0 is a genuine goal (unlike -1), so ext2_try_to_allocate_with_rsv should treat it as such] [hugh@veritas.com: rbtree usage cleanup] [pbadari@us.ibm.com: Fix for ext2 reservation] [bunk@kernel.org: remove fs/ext2/balloc.c:reserve_blocks()] [hugh@veritas.com: ext2 balloc: use io_error label] Cc: "Martin J. Bligh" <mbligh@mbligh.org> Cc: Valerie Henson <val_henson@linux.intel.com> Cc: Mingming Cao <cmm@us.ibm.com> Cc: Mel Gorman <mel@csn.ul.ie> Cc: Hugh Dickins <hugh@veritas.com> Signed-off-by: Adrian Bunk <bunk@stusta.de> Signed-off-by: Hugh Dickins <hugh@veritas.com> Signed-off-by: Badari Pulavarty <pbadari@us.ibm.com> Signed-off-by: Adrian Bunk <bunk@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--fs/ext2/balloc.c1305
-rw-r--r--fs/ext2/ext2.h36
-rw-r--r--fs/ext2/file.c7
-rw-r--r--fs/ext2/ialloc.c5
-rw-r--r--fs/ext2/inode.c524
-rw-r--r--fs/ext2/ioctl.c45
-rw-r--r--fs/ext2/super.c41
-rw-r--r--fs/ext2/xattr.c3
-rw-r--r--include/linux/ext2_fs.h23
-rw-r--r--include/linux/ext2_fs_sb.h50
10 files changed, 1554 insertions, 485 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
diff --git a/fs/ext2/ext2.h b/fs/ext2/ext2.h
index a08052d2c008..7730388c4931 100644
--- a/fs/ext2/ext2.h
+++ b/fs/ext2/ext2.h
@@ -33,22 +33,9 @@ struct ext2_inode_info {
33 */ 33 */
34 __u32 i_block_group; 34 __u32 i_block_group;
35 35
36 /* 36 /* block reservation info */
37 * i_next_alloc_block is the logical (file-relative) number of the 37 struct ext2_block_alloc_info *i_block_alloc_info;
38 * most-recently-allocated block in this file. Yes, it is misnamed.
39 * We use this for detecting linearly ascending allocation requests.
40 */
41 __u32 i_next_alloc_block;
42 38
43 /*
44 * i_next_alloc_goal is the *physical* companion to i_next_alloc_block.
45 * it the the physical block number of the block which was most-recently
46 * allocated to this file. This give us the goal (target) for the next
47 * allocation when we detect linearly ascending requests.
48 */
49 __u32 i_next_alloc_goal;
50 __u32 i_prealloc_block;
51 __u32 i_prealloc_count;
52 __u32 i_dir_start_lookup; 39 __u32 i_dir_start_lookup;
53#ifdef CONFIG_EXT2_FS_XATTR 40#ifdef CONFIG_EXT2_FS_XATTR
54 /* 41 /*
@@ -65,7 +52,16 @@ struct ext2_inode_info {
65 struct posix_acl *i_default_acl; 52 struct posix_acl *i_default_acl;
66#endif 53#endif
67 rwlock_t i_meta_lock; 54 rwlock_t i_meta_lock;
55
56 /*
57 * truncate_mutex is for serialising ext2_truncate() against
58 * ext2_getblock(). It also protects the internals of the inode's
59 * reservation data structures: ext2_reserve_window and
60 * ext2_reserve_window_node.
61 */
62 struct mutex truncate_mutex;
68 struct inode vfs_inode; 63 struct inode vfs_inode;
64 struct list_head i_orphan; /* unlinked but open inodes */
69}; 65};
70 66
71/* 67/*
@@ -91,8 +87,9 @@ static inline struct ext2_inode_info *EXT2_I(struct inode *inode)
91/* balloc.c */ 87/* balloc.c */
92extern int ext2_bg_has_super(struct super_block *sb, int group); 88extern int ext2_bg_has_super(struct super_block *sb, int group);
93extern unsigned long ext2_bg_num_gdb(struct super_block *sb, int group); 89extern unsigned long ext2_bg_num_gdb(struct super_block *sb, int group);
94extern int ext2_new_block (struct inode *, unsigned long, 90extern ext2_fsblk_t ext2_new_block(struct inode *, unsigned long, int *);
95 __u32 *, __u32 *, int *); 91extern ext2_fsblk_t ext2_new_blocks(struct inode *, unsigned long,
92 unsigned long *, int *);
96extern void ext2_free_blocks (struct inode *, unsigned long, 93extern void ext2_free_blocks (struct inode *, unsigned long,
97 unsigned long); 94 unsigned long);
98extern unsigned long ext2_count_free_blocks (struct super_block *); 95extern unsigned long ext2_count_free_blocks (struct super_block *);
@@ -101,6 +98,10 @@ extern void ext2_check_blocks_bitmap (struct super_block *);
101extern struct ext2_group_desc * ext2_get_group_desc(struct super_block * sb, 98extern struct ext2_group_desc * ext2_get_group_desc(struct super_block * sb,
102 unsigned int block_group, 99 unsigned int block_group,
103 struct buffer_head ** bh); 100 struct buffer_head ** bh);
101extern void ext2_discard_reservation (struct inode *);
102extern int ext2_should_retry_alloc(struct super_block *sb, int *retries);
103extern void ext2_init_block_alloc_info(struct inode *);
104extern void ext2_rsv_window_add(struct super_block *sb, struct ext2_reserve_window_node *rsv);
104 105
105/* dir.c */ 106/* dir.c */
106extern int ext2_add_link (struct dentry *, struct inode *); 107extern int ext2_add_link (struct dentry *, struct inode *);
@@ -128,7 +129,6 @@ extern int ext2_write_inode (struct inode *, int);
128extern void ext2_put_inode (struct inode *); 129extern void ext2_put_inode (struct inode *);
129extern void ext2_delete_inode (struct inode *); 130extern void ext2_delete_inode (struct inode *);
130extern int ext2_sync_inode (struct inode *); 131extern int ext2_sync_inode (struct inode *);
131extern void ext2_discard_prealloc (struct inode *);
132extern int ext2_get_block(struct inode *, sector_t, struct buffer_head *, int); 132extern int ext2_get_block(struct inode *, sector_t, struct buffer_head *, int);
133extern void ext2_truncate (struct inode *); 133extern void ext2_truncate (struct inode *);
134extern int ext2_setattr (struct dentry *, struct iattr *); 134extern int ext2_setattr (struct dentry *, struct iattr *);
diff --git a/fs/ext2/file.c b/fs/ext2/file.c
index ab7961260c49..c051798459a1 100644
--- a/fs/ext2/file.c
+++ b/fs/ext2/file.c
@@ -30,8 +30,11 @@
30 */ 30 */
31static int ext2_release_file (struct inode * inode, struct file * filp) 31static int ext2_release_file (struct inode * inode, struct file * filp)
32{ 32{
33 if (filp->f_mode & FMODE_WRITE) 33 if (filp->f_mode & FMODE_WRITE) {
34 ext2_discard_prealloc (inode); 34 mutex_lock(&EXT2_I(inode)->truncate_mutex);
35 ext2_discard_reservation(inode);
36 mutex_unlock(&EXT2_I(inode)->truncate_mutex);
37 }
35 return 0; 38 return 0;
36} 39}
37 40
diff --git a/fs/ext2/ialloc.c b/fs/ext2/ialloc.c
index 2625a00c4669..5deb8b74e649 100644
--- a/fs/ext2/ialloc.c
+++ b/fs/ext2/ialloc.c
@@ -581,11 +581,8 @@ got:
581 ei->i_file_acl = 0; 581 ei->i_file_acl = 0;
582 ei->i_dir_acl = 0; 582 ei->i_dir_acl = 0;
583 ei->i_dtime = 0; 583 ei->i_dtime = 0;
584 ei->i_block_alloc_info = NULL;
584 ei->i_block_group = group; 585 ei->i_block_group = group;
585 ei->i_next_alloc_block = 0;
586 ei->i_next_alloc_goal = 0;
587 ei->i_prealloc_block = 0;
588 ei->i_prealloc_count = 0;
589 ei->i_dir_start_lookup = 0; 586 ei->i_dir_start_lookup = 0;
590 ei->i_state = EXT2_STATE_NEW; 587 ei->i_state = EXT2_STATE_NEW;
591 ext2_set_inode_flags(inode); 588 ext2_set_inode_flags(inode);
diff --git a/fs/ext2/inode.c b/fs/ext2/inode.c
index 84818176fd9d..b1ab32ab5a77 100644
--- a/fs/ext2/inode.c
+++ b/fs/ext2/inode.c
@@ -54,19 +54,6 @@ static inline int ext2_inode_is_fast_symlink(struct inode *inode)
54} 54}
55 55
56/* 56/*
57 * Called at each iput().
58 *
59 * The inode may be "bad" if ext2_read_inode() saw an error from
60 * ext2_get_inode(), so we need to check that to avoid freeing random disk
61 * blocks.
62 */
63void ext2_put_inode(struct inode *inode)
64{
65 if (!is_bad_inode(inode))
66 ext2_discard_prealloc(inode);
67}
68
69/*
70 * Called at the last iput() if i_nlink is zero. 57 * Called at the last iput() if i_nlink is zero.
71 */ 58 */
72void ext2_delete_inode (struct inode * inode) 59void ext2_delete_inode (struct inode * inode)
@@ -89,61 +76,6 @@ no_delete:
89 clear_inode(inode); /* We must guarantee clearing of inode... */ 76 clear_inode(inode); /* We must guarantee clearing of inode... */
90} 77}
91 78
92void ext2_discard_prealloc (struct inode * inode)
93{
94#ifdef EXT2_PREALLOCATE
95 struct ext2_inode_info *ei = EXT2_I(inode);
96 write_lock(&ei->i_meta_lock);
97 if (ei->i_prealloc_count) {
98 unsigned short total = ei->i_prealloc_count;
99 unsigned long block = ei->i_prealloc_block;
100 ei->i_prealloc_count = 0;
101 ei->i_prealloc_block = 0;
102 write_unlock(&ei->i_meta_lock);
103 ext2_free_blocks (inode, block, total);
104 return;
105 } else
106 write_unlock(&ei->i_meta_lock);
107#endif
108}
109
110static int ext2_alloc_block (struct inode * inode, unsigned long goal, int *err)
111{
112#ifdef EXT2FS_DEBUG
113 static unsigned long alloc_hits, alloc_attempts;
114#endif
115 unsigned long result;
116
117
118#ifdef EXT2_PREALLOCATE
119 struct ext2_inode_info *ei = EXT2_I(inode);
120 write_lock(&ei->i_meta_lock);
121 if (ei->i_prealloc_count &&
122 (goal == ei->i_prealloc_block || goal + 1 == ei->i_prealloc_block))
123 {
124 result = ei->i_prealloc_block++;
125 ei->i_prealloc_count--;
126 write_unlock(&ei->i_meta_lock);
127 ext2_debug ("preallocation hit (%lu/%lu).\n",
128 ++alloc_hits, ++alloc_attempts);
129 } else {
130 write_unlock(&ei->i_meta_lock);
131 ext2_discard_prealloc (inode);
132 ext2_debug ("preallocation miss (%lu/%lu).\n",
133 alloc_hits, ++alloc_attempts);
134 if (S_ISREG(inode->i_mode))
135 result = ext2_new_block (inode, goal,
136 &ei->i_prealloc_count,
137 &ei->i_prealloc_block, err);
138 else
139 result = ext2_new_block(inode, goal, NULL, NULL, err);
140 }
141#else
142 result = ext2_new_block (inode, goal, 0, 0, err);
143#endif
144 return result;
145}
146
147typedef struct { 79typedef struct {
148 __le32 *p; 80 __le32 *p;
149 __le32 key; 81 __le32 key;
@@ -228,7 +160,8 @@ static int ext2_block_to_path(struct inode *inode,
228 ext2_warning (inode->i_sb, "ext2_block_to_path", "block > big"); 160 ext2_warning (inode->i_sb, "ext2_block_to_path", "block > big");
229 } 161 }
230 if (boundary) 162 if (boundary)
231 *boundary = (i_block & (ptrs - 1)) == (final - 1); 163 *boundary = final - 1 - (i_block & (ptrs - 1));
164
232 return n; 165 return n;
233} 166}
234 167
@@ -355,39 +288,129 @@ static unsigned long ext2_find_near(struct inode *inode, Indirect *ind)
355 * @block: block we want 288 * @block: block we want
356 * @chain: chain of indirect blocks 289 * @chain: chain of indirect blocks
357 * @partial: pointer to the last triple within a chain 290 * @partial: pointer to the last triple within a chain
358 * @goal: place to store the result.
359 * 291 *
360 * Normally this function find the prefered place for block allocation, 292 * Returns preferred place for a block (the goal).
361 * stores it in *@goal and returns zero. If the branch had been changed
362 * under us we return -EAGAIN.
363 */ 293 */
364 294
365static inline int ext2_find_goal(struct inode *inode, 295static inline int ext2_find_goal(struct inode *inode,
366 long block, 296 long block,
367 Indirect chain[4], 297 Indirect chain[4],
368 Indirect *partial, 298 Indirect *partial)
369 unsigned long *goal)
370{ 299{
371 struct ext2_inode_info *ei = EXT2_I(inode); 300 struct ext2_block_alloc_info *block_i;
372 write_lock(&ei->i_meta_lock); 301
373 if ((block == ei->i_next_alloc_block + 1) && ei->i_next_alloc_goal) { 302 block_i = EXT2_I(inode)->i_block_alloc_info;
374 ei->i_next_alloc_block++; 303
375 ei->i_next_alloc_goal++; 304 /*
376 } 305 * try the heuristic for sequential allocation,
377 if (verify_chain(chain, partial)) { 306 * failing that at least try to get decent locality.
378 /* 307 */
379 * try the heuristic for sequential allocation, 308 if (block_i && (block == block_i->last_alloc_logical_block + 1)
380 * failing that at least try to get decent locality. 309 && (block_i->last_alloc_physical_block != 0)) {
381 */ 310 return block_i->last_alloc_physical_block + 1;
382 if (block == ei->i_next_alloc_block)
383 *goal = ei->i_next_alloc_goal;
384 if (!*goal)
385 *goal = ext2_find_near(inode, partial);
386 write_unlock(&ei->i_meta_lock);
387 return 0;
388 } 311 }
389 write_unlock(&ei->i_meta_lock); 312
390 return -EAGAIN; 313 return ext2_find_near(inode, partial);
314}
315
316/**
317 * ext2_blks_to_allocate: Look up the block map and count the number
318 * of direct blocks need to be allocated for the given branch.
319 *
320 * @branch: chain of indirect blocks
321 * @k: number of blocks need for indirect blocks
322 * @blks: number of data blocks to be mapped.
323 * @blocks_to_boundary: the offset in the indirect block
324 *
325 * return the total number of blocks to be allocate, including the
326 * direct and indirect blocks.
327 */
328static int
329ext2_blks_to_allocate(Indirect * branch, int k, unsigned long blks,
330 int blocks_to_boundary)
331{
332 unsigned long count = 0;
333
334 /*
335 * Simple case, [t,d]Indirect block(s) has not allocated yet
336 * then it's clear blocks on that path have not allocated
337 */
338 if (k > 0) {
339 /* right now don't hanel cross boundary allocation */
340 if (blks < blocks_to_boundary + 1)
341 count += blks;
342 else
343 count += blocks_to_boundary + 1;
344 return count;
345 }
346
347 count++;
348 while (count < blks && count <= blocks_to_boundary
349 && le32_to_cpu(*(branch[0].p + count)) == 0) {
350 count++;
351 }
352 return count;
353}
354
355/**
356 * ext2_alloc_blocks: multiple allocate blocks needed for a branch
357 * @indirect_blks: the number of blocks need to allocate for indirect
358 * blocks
359 *
360 * @new_blocks: on return it will store the new block numbers for
361 * the indirect blocks(if needed) and the first direct block,
362 * @blks: on return it will store the total number of allocated
363 * direct blocks
364 */
365static int ext2_alloc_blocks(struct inode *inode,
366 ext2_fsblk_t goal, int indirect_blks, int blks,
367 ext2_fsblk_t new_blocks[4], int *err)
368{
369 int target, i;
370 unsigned long count = 0;
371 int index = 0;
372 ext2_fsblk_t current_block = 0;
373 int ret = 0;
374
375 /*
376 * Here we try to allocate the requested multiple blocks at once,
377 * on a best-effort basis.
378 * To build a branch, we should allocate blocks for
379 * the indirect blocks(if not allocated yet), and at least
380 * the first direct block of this branch. That's the
381 * minimum number of blocks need to allocate(required)
382 */
383 target = blks + indirect_blks;
384
385 while (1) {
386 count = target;
387 /* allocating blocks for indirect blocks and direct blocks */
388 current_block = ext2_new_blocks(inode,goal,&count,err);
389 if (*err)
390 goto failed_out;
391
392 target -= count;
393 /* allocate blocks for indirect blocks */
394 while (index < indirect_blks && count) {
395 new_blocks[index++] = current_block++;
396 count--;
397 }
398
399 if (count > 0)
400 break;
401 }
402
403 /* save the new block number for the first direct block */
404 new_blocks[index] = current_block;
405
406 /* total number of blocks allocated for direct blocks */
407 ret = count;
408 *err = 0;
409 return ret;
410failed_out:
411 for (i = 0; i <index; i++)
412 ext2_free_blocks(inode, new_blocks[i], 1);
413 return ret;
391} 414}
392 415
393/** 416/**
@@ -416,39 +439,49 @@ static inline int ext2_find_goal(struct inode *inode,
416 */ 439 */
417 440
418static int ext2_alloc_branch(struct inode *inode, 441static int ext2_alloc_branch(struct inode *inode,
419 int num, 442 int indirect_blks, int *blks, ext2_fsblk_t goal,
420 unsigned long goal, 443 int *offsets, Indirect *branch)
421 int *offsets,
422 Indirect *branch)
423{ 444{
424 int blocksize = inode->i_sb->s_blocksize; 445 int blocksize = inode->i_sb->s_blocksize;
425 int n = 0; 446 int i, n = 0;
426 int err; 447 int err = 0;
427 int i; 448 struct buffer_head *bh;
428 int parent = ext2_alloc_block(inode, goal, &err); 449 int num;
429 450 ext2_fsblk_t new_blocks[4];
430 branch[0].key = cpu_to_le32(parent); 451 ext2_fsblk_t current_block;
431 if (parent) for (n = 1; n < num; n++) { 452
432 struct buffer_head *bh; 453 num = ext2_alloc_blocks(inode, goal, indirect_blks,
433 /* Allocate the next block */ 454 *blks, new_blocks, &err);
434 int nr = ext2_alloc_block(inode, parent, &err); 455 if (err)
435 if (!nr) 456 return err;
436 break; 457
437 branch[n].key = cpu_to_le32(nr); 458 branch[0].key = cpu_to_le32(new_blocks[0]);
459 /*
460 * metadata blocks and data blocks are allocated.
461 */
462 for (n = 1; n <= indirect_blks; n++) {
438 /* 463 /*
439 * Get buffer_head for parent block, zero it out and set 464 * Get buffer_head for parent block, zero it out
440 * the pointer to new one, then send parent to disk. 465 * and set the pointer to new one, then send
466 * parent to disk.
441 */ 467 */
442 bh = sb_getblk(inode->i_sb, parent); 468 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
443 if (!bh) { 469 branch[n].bh = bh;
444 err = -EIO;
445 break;
446 }
447 lock_buffer(bh); 470 lock_buffer(bh);
448 memset(bh->b_data, 0, blocksize); 471 memset(bh->b_data, 0, blocksize);
449 branch[n].bh = bh;
450 branch[n].p = (__le32 *) bh->b_data + offsets[n]; 472 branch[n].p = (__le32 *) bh->b_data + offsets[n];
473 branch[n].key = cpu_to_le32(new_blocks[n]);
451 *branch[n].p = branch[n].key; 474 *branch[n].p = branch[n].key;
475 if ( n == indirect_blks) {
476 current_block = new_blocks[n];
477 /*
478 * End of chain, update the last new metablock of
479 * the chain to point to the new allocated
480 * data blocks numbers
481 */
482 for (i=1; i < num; i++)
483 *(branch[n].p + i) = cpu_to_le32(++current_block);
484 }
452 set_buffer_uptodate(bh); 485 set_buffer_uptodate(bh);
453 unlock_buffer(bh); 486 unlock_buffer(bh);
454 mark_buffer_dirty_inode(bh, inode); 487 mark_buffer_dirty_inode(bh, inode);
@@ -458,77 +491,68 @@ static int ext2_alloc_branch(struct inode *inode,
458 */ 491 */
459 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) 492 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
460 sync_dirty_buffer(bh); 493 sync_dirty_buffer(bh);
461 parent = nr;
462 } 494 }
463 if (n == num) 495 *blks = num;
464 return 0;
465
466 /* Allocation failed, free what we already allocated */
467 for (i = 1; i < n; i++)
468 bforget(branch[i].bh);
469 for (i = 0; i < n; i++)
470 ext2_free_blocks(inode, le32_to_cpu(branch[i].key), 1);
471 return err; 496 return err;
472} 497}
473 498
474/** 499/**
475 * ext2_splice_branch - splice the allocated branch onto inode. 500 * ext2_splice_branch - splice the allocated branch onto inode.
476 * @inode: owner 501 * @inode: owner
477 * @block: (logical) number of block we are adding 502 * @block: (logical) number of block we are adding
478 * @chain: chain of indirect blocks (with a missing link - see 503 * @chain: chain of indirect blocks (with a missing link - see
479 * ext2_alloc_branch) 504 * ext2_alloc_branch)
480 * @where: location of missing link 505 * @where: location of missing link
481 * @num: number of blocks we are adding 506 * @num: number of indirect blocks we are adding
507 * @blks: number of direct blocks we are adding
482 * 508 *
483 * This function verifies that chain (up to the missing link) had not 509 * This function fills the missing link and does all housekeeping needed in
484 * changed, fills the missing link and does all housekeeping needed in 510 * inode (->i_blocks, etc.). In case of success we end up with the full
485 * inode (->i_blocks, etc.). In case of success we end up with the full 511 * chain to new block and return 0.
486 * chain to new block and return 0. Otherwise (== chain had been changed)
487 * we free the new blocks (forgetting their buffer_heads, indeed) and
488 * return -EAGAIN.
489 */ 512 */
490 513static void ext2_splice_branch(struct inode *inode,
491static inline int ext2_splice_branch(struct inode *inode, 514 long block, Indirect *where, int num, int blks)
492 long block,
493 Indirect chain[4],
494 Indirect *where,
495 int num)
496{ 515{
497 struct ext2_inode_info *ei = EXT2_I(inode);
498 int i; 516 int i;
517 struct ext2_block_alloc_info *block_i;
518 ext2_fsblk_t current_block;
499 519
500 /* Verify that place we are splicing to is still there and vacant */ 520 block_i = EXT2_I(inode)->i_block_alloc_info;
501
502 write_lock(&ei->i_meta_lock);
503 if (!verify_chain(chain, where-1) || *where->p)
504 goto changed;
505 521
522 /* XXX LOCKING probably should have i_meta_lock ?*/
506 /* That's it */ 523 /* That's it */
507 524
508 *where->p = where->key; 525 *where->p = where->key;
509 ei->i_next_alloc_block = block;
510 ei->i_next_alloc_goal = le32_to_cpu(where[num-1].key);
511 526
512 write_unlock(&ei->i_meta_lock); 527 /*
528 * Update the host buffer_head or inode to point to more just allocated
529 * direct blocks blocks
530 */
531 if (num == 0 && blks > 1) {
532 current_block = le32_to_cpu(where->key) + 1;
533 for (i = 1; i < blks; i++)
534 *(where->p + i ) = cpu_to_le32(current_block++);
535 }
513 536
514 /* We are done with atomic stuff, now do the rest of housekeeping */ 537 /*
538 * update the most recently allocated logical & physical block
539 * in i_block_alloc_info, to assist find the proper goal block for next
540 * allocation
541 */
542 if (block_i) {
543 block_i->last_alloc_logical_block = block + blks - 1;
544 block_i->last_alloc_physical_block =
545 le32_to_cpu(where[num].key) + blks - 1;
546 }
515 547
516 inode->i_ctime = CURRENT_TIME_SEC; 548 /* We are done with atomic stuff, now do the rest of housekeeping */
517 549
518 /* had we spliced it onto indirect block? */ 550 /* had we spliced it onto indirect block? */
519 if (where->bh) 551 if (where->bh)
520 mark_buffer_dirty_inode(where->bh, inode); 552 mark_buffer_dirty_inode(where->bh, inode);
521 553
554 inode->i_ctime = CURRENT_TIME_SEC;
522 mark_inode_dirty(inode); 555 mark_inode_dirty(inode);
523 return 0;
524
525changed:
526 write_unlock(&ei->i_meta_lock);
527 for (i = 1; i < num; i++)
528 bforget(where[i].bh);
529 for (i = 0; i < num; i++)
530 ext2_free_blocks(inode, le32_to_cpu(where[i].key), 1);
531 return -EAGAIN;
532} 556}
533 557
534/* 558/*
@@ -542,64 +566,99 @@ changed:
542 * That has a nice additional property: no special recovery from the failed 566 * That has a nice additional property: no special recovery from the failed
543 * allocations is needed - we simply release blocks and do not touch anything 567 * allocations is needed - we simply release blocks and do not touch anything
544 * reachable from inode. 568 * reachable from inode.
569 *
570 * `handle' can be NULL if create == 0.
571 *
572 * The BKL may not be held on entry here. Be sure to take it early.
573 * return > 0, # of blocks mapped or allocated.
574 * return = 0, if plain lookup failed.
575 * return < 0, error case.
545 */ 576 */
546 577static int ext2_get_blocks(struct inode *inode,
547int ext2_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create) 578 sector_t iblock, unsigned long maxblocks,
579 struct buffer_head *bh_result,
580 int create)
548{ 581{
549 int err = -EIO; 582 int err = -EIO;
550 int offsets[4]; 583 int offsets[4];
551 Indirect chain[4]; 584 Indirect chain[4];
552 Indirect *partial; 585 Indirect *partial;
553 unsigned long goal; 586 ext2_fsblk_t goal;
554 int left; 587 int indirect_blks;
555 int boundary = 0; 588 int blocks_to_boundary = 0;
556 int depth = ext2_block_to_path(inode, iblock, offsets, &boundary); 589 int depth;
590 struct ext2_inode_info *ei = EXT2_I(inode);
591 int count = 0;
592 ext2_fsblk_t first_block = 0;
557 593
558 if (depth == 0) 594 depth = ext2_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
559 goto out;
560 595
596 if (depth == 0)
597 return (err);
561reread: 598reread:
562 partial = ext2_get_branch(inode, depth, offsets, chain, &err); 599 partial = ext2_get_branch(inode, depth, offsets, chain, &err);
563 600
564 /* Simplest case - block found, no allocation needed */ 601 /* Simplest case - block found, no allocation needed */
565 if (!partial) { 602 if (!partial) {
566got_it: 603 first_block = le32_to_cpu(chain[depth - 1].key);
567 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key)); 604 clear_buffer_new(bh_result); /* What's this do? */
568 if (boundary) 605 count++;
569 set_buffer_boundary(bh_result); 606 /*map more blocks*/
570 /* Clean up and exit */ 607 while (count < maxblocks && count <= blocks_to_boundary) {
571 partial = chain+depth-1; /* the whole chain */ 608 ext2_fsblk_t blk;
572 goto cleanup; 609
610 if (!verify_chain(chain, partial)) {
611 /*
612 * Indirect block might be removed by
613 * truncate while we were reading it.
614 * Handling of that case: forget what we've
615 * got now, go to reread.
616 */
617 count = 0;
618 goto changed;
619 }
620 blk = le32_to_cpu(*(chain[depth-1].p + count));
621 if (blk == first_block + count)
622 count++;
623 else
624 break;
625 }
626 goto got_it;
573 } 627 }
574 628
575 /* Next simple case - plain lookup or failed read of indirect block */ 629 /* Next simple case - plain lookup or failed read of indirect block */
576 if (!create || err == -EIO) { 630 if (!create || err == -EIO)
577cleanup: 631 goto cleanup;
578 while (partial > chain) { 632
579 brelse(partial->bh); 633 mutex_lock(&ei->truncate_mutex);
580 partial--;
581 }
582out:
583 return err;
584 }
585 634
586 /* 635 /*
587 * Indirect block might be removed by truncate while we were 636 * Okay, we need to do block allocation. Lazily initialize the block
588 * reading it. Handling of that case (forget what we've got and 637 * allocation info here if necessary
589 * reread) is taken out of the main path. 638 */
590 */ 639 if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info))
591 if (err == -EAGAIN) 640 ext2_init_block_alloc_info(inode);
592 goto changed;
593 641
594 goal = 0; 642 goal = ext2_find_goal(inode, iblock, chain, partial);
595 if (ext2_find_goal(inode, iblock, chain, partial, &goal) < 0)
596 goto changed;
597 643
598 left = (chain + depth) - partial; 644 /* the number of blocks need to allocate for [d,t]indirect blocks */
599 err = ext2_alloc_branch(inode, left, goal, 645 indirect_blks = (chain + depth) - partial - 1;
600 offsets+(partial-chain), partial); 646 /*
601 if (err) 647 * Next look up the indirect map to count the totoal number of
648 * direct blocks to allocate for this branch.
649 */
650 count = ext2_blks_to_allocate(partial, indirect_blks,
651 maxblocks, blocks_to_boundary);
652 /*
653 * XXX ???? Block out ext2_truncate while we alter the tree
654 */
655 err = ext2_alloc_branch(inode, indirect_blks, &count, goal,
656 offsets + (partial - chain), partial);
657
658 if (err) {
659 mutex_unlock(&ei->truncate_mutex);
602 goto cleanup; 660 goto cleanup;
661 }
603 662
604 if (ext2_use_xip(inode->i_sb)) { 663 if (ext2_use_xip(inode->i_sb)) {
605 /* 664 /*
@@ -607,16 +666,28 @@ out:
607 */ 666 */
608 err = ext2_clear_xip_target (inode, 667 err = ext2_clear_xip_target (inode,
609 le32_to_cpu(chain[depth-1].key)); 668 le32_to_cpu(chain[depth-1].key));
610 if (err) 669 if (err) {
670 mutex_unlock(&ei->truncate_mutex);
611 goto cleanup; 671 goto cleanup;
672 }
612 } 673 }
613 674
614 if (ext2_splice_branch(inode, iblock, chain, partial, left) < 0) 675 ext2_splice_branch(inode, iblock, partial, indirect_blks, count);
615 goto changed; 676 mutex_unlock(&ei->truncate_mutex);
616
617 set_buffer_new(bh_result); 677 set_buffer_new(bh_result);
618 goto got_it; 678got_it:
619 679 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
680 if (count > blocks_to_boundary)
681 set_buffer_boundary(bh_result);
682 err = count;
683 /* Clean up and exit */
684 partial = chain + depth - 1; /* the whole chain */
685cleanup:
686 while (partial > chain) {
687 brelse(partial->bh);
688 partial--;
689 }
690 return err;
620changed: 691changed:
621 while (partial > chain) { 692 while (partial > chain) {
622 brelse(partial->bh); 693 brelse(partial->bh);
@@ -625,6 +696,19 @@ changed:
625 goto reread; 696 goto reread;
626} 697}
627 698
699int ext2_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
700{
701 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
702 int ret = ext2_get_blocks(inode, iblock, max_blocks,
703 bh_result, create);
704 if (ret > 0) {
705 bh_result->b_size = (ret << inode->i_blkbits);
706 ret = 0;
707 }
708 return ret;
709
710}
711
628static int ext2_writepage(struct page *page, struct writeback_control *wbc) 712static int ext2_writepage(struct page *page, struct writeback_control *wbc)
629{ 713{
630 return block_write_full_page(page, ext2_get_block, wbc); 714 return block_write_full_page(page, ext2_get_block, wbc);
@@ -913,9 +997,10 @@ static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int de
913 ext2_free_data(inode, p, q); 997 ext2_free_data(inode, p, q);
914} 998}
915 999
916void ext2_truncate (struct inode * inode) 1000void ext2_truncate(struct inode *inode)
917{ 1001{
918 __le32 *i_data = EXT2_I(inode)->i_data; 1002 __le32 *i_data = EXT2_I(inode)->i_data;
1003 struct ext2_inode_info *ei = EXT2_I(inode);
919 int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb); 1004 int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
920 int offsets[4]; 1005 int offsets[4];
921 Indirect chain[4]; 1006 Indirect chain[4];
@@ -933,8 +1018,6 @@ void ext2_truncate (struct inode * inode)
933 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 1018 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
934 return; 1019 return;
935 1020
936 ext2_discard_prealloc(inode);
937
938 blocksize = inode->i_sb->s_blocksize; 1021 blocksize = inode->i_sb->s_blocksize;
939 iblock = (inode->i_size + blocksize-1) 1022 iblock = (inode->i_size + blocksize-1)
940 >> EXT2_BLOCK_SIZE_BITS(inode->i_sb); 1023 >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
@@ -952,6 +1035,12 @@ void ext2_truncate (struct inode * inode)
952 if (n == 0) 1035 if (n == 0)
953 return; 1036 return;
954 1037
1038 /*
1039 * From here we block out all ext2_get_block() callers who want to
1040 * modify the block allocation tree.
1041 */
1042 mutex_lock(&ei->truncate_mutex);
1043
955 if (n == 1) { 1044 if (n == 1) {
956 ext2_free_data(inode, i_data+offsets[0], 1045 ext2_free_data(inode, i_data+offsets[0],
957 i_data + EXT2_NDIR_BLOCKS); 1046 i_data + EXT2_NDIR_BLOCKS);
@@ -1004,6 +1093,10 @@ do_indirects:
1004 case EXT2_TIND_BLOCK: 1093 case EXT2_TIND_BLOCK:
1005 ; 1094 ;
1006 } 1095 }
1096
1097 ext2_discard_reservation(inode);
1098
1099 mutex_unlock(&ei->truncate_mutex);
1007 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC; 1100 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
1008 if (inode_needs_sync(inode)) { 1101 if (inode_needs_sync(inode)) {
1009 sync_mapping_buffers(inode->i_mapping); 1102 sync_mapping_buffers(inode->i_mapping);
@@ -1104,6 +1197,8 @@ void ext2_read_inode (struct inode * inode)
1104 ei->i_acl = EXT2_ACL_NOT_CACHED; 1197 ei->i_acl = EXT2_ACL_NOT_CACHED;
1105 ei->i_default_acl = EXT2_ACL_NOT_CACHED; 1198 ei->i_default_acl = EXT2_ACL_NOT_CACHED;
1106#endif 1199#endif
1200 ei->i_block_alloc_info = NULL;
1201
1107 if (IS_ERR(raw_inode)) 1202 if (IS_ERR(raw_inode))
1108 goto bad_inode; 1203 goto bad_inode;
1109 1204
@@ -1145,9 +1240,6 @@ void ext2_read_inode (struct inode * inode)
1145 ei->i_dtime = 0; 1240 ei->i_dtime = 0;
1146 inode->i_generation = le32_to_cpu(raw_inode->i_generation); 1241 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
1147 ei->i_state = 0; 1242 ei->i_state = 0;
1148 ei->i_next_alloc_block = 0;
1149 ei->i_next_alloc_goal = 0;
1150 ei->i_prealloc_count = 0;
1151 ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb); 1243 ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
1152 ei->i_dir_start_lookup = 0; 1244 ei->i_dir_start_lookup = 0;
1153 1245
diff --git a/fs/ext2/ioctl.c b/fs/ext2/ioctl.c
index 3bcd25422ee4..c2324d5fe4ac 100644
--- a/fs/ext2/ioctl.c
+++ b/fs/ext2/ioctl.c
@@ -22,6 +22,7 @@ int ext2_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
22{ 22{
23 struct ext2_inode_info *ei = EXT2_I(inode); 23 struct ext2_inode_info *ei = EXT2_I(inode);
24 unsigned int flags; 24 unsigned int flags;
25 unsigned short rsv_window_size;
25 26
26 ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg); 27 ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg);
27 28
@@ -83,6 +84,50 @@ int ext2_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
83 inode->i_ctime = CURRENT_TIME_SEC; 84 inode->i_ctime = CURRENT_TIME_SEC;
84 mark_inode_dirty(inode); 85 mark_inode_dirty(inode);
85 return 0; 86 return 0;
87 case EXT2_IOC_GETRSVSZ:
88 if (test_opt(inode->i_sb, RESERVATION)
89 && S_ISREG(inode->i_mode)
90 && ei->i_block_alloc_info) {
91 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
92 return put_user(rsv_window_size, (int __user *)arg);
93 }
94 return -ENOTTY;
95 case EXT2_IOC_SETRSVSZ: {
96
97 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
98 return -ENOTTY;
99
100 if (IS_RDONLY(inode))
101 return -EROFS;
102
103 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
104 return -EACCES;
105
106 if (get_user(rsv_window_size, (int __user *)arg))
107 return -EFAULT;
108
109 if (rsv_window_size > EXT2_MAX_RESERVE_BLOCKS)
110 rsv_window_size = EXT2_MAX_RESERVE_BLOCKS;
111
112 /*
113 * need to allocate reservation structure for this inode
114 * before set the window size
115 */
116 /*
117 * XXX What lock should protect the rsv_goal_size?
118 * Accessed in ext2_get_block only. ext3 uses i_truncate.
119 */
120 mutex_lock(&ei->truncate_mutex);
121 if (!ei->i_block_alloc_info)
122 ext2_init_block_alloc_info(inode);
123
124 if (ei->i_block_alloc_info){
125 struct ext2_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
126 rsv->rsv_goal_size = rsv_window_size;
127 }
128 mutex_unlock(&ei->truncate_mutex);
129 return 0;
130 }
86 default: 131 default:
87 return -ENOTTY; 132 return -ENOTTY;
88 } 133 }
diff --git a/fs/ext2/super.c b/fs/ext2/super.c
index 51b4c43b97e4..77bd5f9262f9 100644
--- a/fs/ext2/super.c
+++ b/fs/ext2/super.c
@@ -149,6 +149,7 @@ static struct inode *ext2_alloc_inode(struct super_block *sb)
149 ei->i_acl = EXT2_ACL_NOT_CACHED; 149 ei->i_acl = EXT2_ACL_NOT_CACHED;
150 ei->i_default_acl = EXT2_ACL_NOT_CACHED; 150 ei->i_default_acl = EXT2_ACL_NOT_CACHED;
151#endif 151#endif
152 ei->i_block_alloc_info = NULL;
152 ei->vfs_inode.i_version = 1; 153 ei->vfs_inode.i_version = 1;
153 return &ei->vfs_inode; 154 return &ei->vfs_inode;
154} 155}
@@ -166,6 +167,7 @@ static void init_once(struct kmem_cache * cachep, void *foo)
166#ifdef CONFIG_EXT2_FS_XATTR 167#ifdef CONFIG_EXT2_FS_XATTR
167 init_rwsem(&ei->xattr_sem); 168 init_rwsem(&ei->xattr_sem);
168#endif 169#endif
170 mutex_init(&ei->truncate_mutex);
169 inode_init_once(&ei->vfs_inode); 171 inode_init_once(&ei->vfs_inode);
170} 172}
171 173
@@ -188,6 +190,7 @@ static void destroy_inodecache(void)
188 190
189static void ext2_clear_inode(struct inode *inode) 191static void ext2_clear_inode(struct inode *inode)
190{ 192{
193 struct ext2_block_alloc_info *rsv = EXT2_I(inode)->i_block_alloc_info;
191#ifdef CONFIG_EXT2_FS_POSIX_ACL 194#ifdef CONFIG_EXT2_FS_POSIX_ACL
192 struct ext2_inode_info *ei = EXT2_I(inode); 195 struct ext2_inode_info *ei = EXT2_I(inode);
193 196
@@ -200,6 +203,10 @@ static void ext2_clear_inode(struct inode *inode)
200 ei->i_default_acl = EXT2_ACL_NOT_CACHED; 203 ei->i_default_acl = EXT2_ACL_NOT_CACHED;
201 } 204 }
202#endif 205#endif
206 ext2_discard_reservation(inode);
207 EXT2_I(inode)->i_block_alloc_info = NULL;
208 if (unlikely(rsv))
209 kfree(rsv);
203} 210}
204 211
205static int ext2_show_options(struct seq_file *seq, struct vfsmount *vfs) 212static int ext2_show_options(struct seq_file *seq, struct vfsmount *vfs)
@@ -291,7 +298,6 @@ static const struct super_operations ext2_sops = {
291 .destroy_inode = ext2_destroy_inode, 298 .destroy_inode = ext2_destroy_inode,
292 .read_inode = ext2_read_inode, 299 .read_inode = ext2_read_inode,
293 .write_inode = ext2_write_inode, 300 .write_inode = ext2_write_inode,
294 .put_inode = ext2_put_inode,
295 .delete_inode = ext2_delete_inode, 301 .delete_inode = ext2_delete_inode,
296 .put_super = ext2_put_super, 302 .put_super = ext2_put_super,
297 .write_super = ext2_write_super, 303 .write_super = ext2_write_super,
@@ -379,7 +385,7 @@ enum {
379 Opt_err_ro, Opt_nouid32, Opt_nocheck, Opt_debug, 385 Opt_err_ro, Opt_nouid32, Opt_nocheck, Opt_debug,
380 Opt_oldalloc, Opt_orlov, Opt_nobh, Opt_user_xattr, Opt_nouser_xattr, 386 Opt_oldalloc, Opt_orlov, Opt_nobh, Opt_user_xattr, Opt_nouser_xattr,
381 Opt_acl, Opt_noacl, Opt_xip, Opt_ignore, Opt_err, Opt_quota, 387 Opt_acl, Opt_noacl, Opt_xip, Opt_ignore, Opt_err, Opt_quota,
382 Opt_usrquota, Opt_grpquota 388 Opt_usrquota, Opt_grpquota, Opt_reservation, Opt_noreservation
383}; 389};
384 390
385static match_table_t tokens = { 391static match_table_t tokens = {
@@ -411,6 +417,8 @@ static match_table_t tokens = {
411 {Opt_ignore, "noquota"}, 417 {Opt_ignore, "noquota"},
412 {Opt_quota, "quota"}, 418 {Opt_quota, "quota"},
413 {Opt_usrquota, "usrquota"}, 419 {Opt_usrquota, "usrquota"},
420 {Opt_reservation, "reservation"},
421 {Opt_noreservation, "noreservation"},
414 {Opt_err, NULL} 422 {Opt_err, NULL}
415}; 423};
416 424
@@ -543,6 +551,14 @@ static int parse_options (char * options,
543 break; 551 break;
544#endif 552#endif
545 553
554 case Opt_reservation:
555 set_opt(sbi->s_mount_opt, RESERVATION);
556 printk("reservations ON\n");
557 break;
558 case Opt_noreservation:
559 clear_opt(sbi->s_mount_opt, RESERVATION);
560 printk("reservations OFF\n");
561 break;
546 case Opt_ignore: 562 case Opt_ignore:
547 break; 563 break;
548 default: 564 default:
@@ -784,6 +800,8 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
784 sbi->s_resuid = le16_to_cpu(es->s_def_resuid); 800 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
785 sbi->s_resgid = le16_to_cpu(es->s_def_resgid); 801 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
786 802
803 set_opt(sbi->s_mount_opt, RESERVATION);
804
787 if (!parse_options ((char *) data, sbi)) 805 if (!parse_options ((char *) data, sbi))
788 goto failed_mount; 806 goto failed_mount;
789 807
@@ -965,6 +983,21 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
965 get_random_bytes(&sbi->s_next_generation, sizeof(u32)); 983 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
966 spin_lock_init(&sbi->s_next_gen_lock); 984 spin_lock_init(&sbi->s_next_gen_lock);
967 985
986 /* per fileystem reservation list head & lock */
987 spin_lock_init(&sbi->s_rsv_window_lock);
988 sbi->s_rsv_window_root = RB_ROOT;
989 /*
990 * Add a single, static dummy reservation to the start of the
991 * reservation window list --- it gives us a placeholder for
992 * append-at-start-of-list which makes the allocation logic
993 * _much_ simpler.
994 */
995 sbi->s_rsv_window_head.rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
996 sbi->s_rsv_window_head.rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
997 sbi->s_rsv_window_head.rsv_alloc_hit = 0;
998 sbi->s_rsv_window_head.rsv_goal_size = 0;
999 ext2_rsv_window_add(sb, &sbi->s_rsv_window_head);
1000
968 err = percpu_counter_init(&sbi->s_freeblocks_counter, 1001 err = percpu_counter_init(&sbi->s_freeblocks_counter,
969 ext2_count_free_blocks(sb)); 1002 ext2_count_free_blocks(sb));
970 if (!err) { 1003 if (!err) {
@@ -1260,7 +1293,7 @@ static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data,
1260 1293
1261 tmp_bh.b_state = 0; 1294 tmp_bh.b_state = 0;
1262 err = ext2_get_block(inode, blk, &tmp_bh, 0); 1295 err = ext2_get_block(inode, blk, &tmp_bh, 0);
1263 if (err) 1296 if (err < 0)
1264 return err; 1297 return err;
1265 if (!buffer_mapped(&tmp_bh)) /* A hole? */ 1298 if (!buffer_mapped(&tmp_bh)) /* A hole? */
1266 memset(data, 0, tocopy); 1299 memset(data, 0, tocopy);
@@ -1299,7 +1332,7 @@ static ssize_t ext2_quota_write(struct super_block *sb, int type,
1299 1332
1300 tmp_bh.b_state = 0; 1333 tmp_bh.b_state = 0;
1301 err = ext2_get_block(inode, blk, &tmp_bh, 1); 1334 err = ext2_get_block(inode, blk, &tmp_bh, 1);
1302 if (err) 1335 if (err < 0)
1303 goto out; 1336 goto out;
1304 if (offset || tocopy != EXT2_BLOCK_SIZE(sb)) 1337 if (offset || tocopy != EXT2_BLOCK_SIZE(sb))
1305 bh = sb_bread(sb, tmp_bh.b_blocknr); 1338 bh = sb_bread(sb, tmp_bh.b_blocknr);
diff --git a/fs/ext2/xattr.c b/fs/ext2/xattr.c
index 247efd0b51d6..3e8683dbb13f 100644
--- a/fs/ext2/xattr.c
+++ b/fs/ext2/xattr.c
@@ -664,8 +664,7 @@ ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
664 s_first_data_block) + 664 s_first_data_block) +
665 EXT2_I(inode)->i_block_group * 665 EXT2_I(inode)->i_block_group *
666 EXT2_BLOCKS_PER_GROUP(sb); 666 EXT2_BLOCKS_PER_GROUP(sb);
667 int block = ext2_new_block(inode, goal, 667 int block = ext2_new_block(inode, goal, &error);
668 NULL, NULL, &error);
669 if (error) 668 if (error)
670 goto cleanup; 669 goto cleanup;
671 ea_idebug(inode, "creating block %d", block); 670 ea_idebug(inode, "creating block %d", block);
diff --git a/include/linux/ext2_fs.h b/include/linux/ext2_fs.h
index 153d755376a4..c77c3bbfe4bb 100644
--- a/include/linux/ext2_fs.h
+++ b/include/linux/ext2_fs.h
@@ -29,11 +29,12 @@
29#undef EXT2FS_DEBUG 29#undef EXT2FS_DEBUG
30 30
31/* 31/*
32 * Define EXT2_PREALLOCATE to preallocate data blocks for expanding files 32 * Define EXT2_RESERVATION to reserve data blocks for expanding files
33 */ 33 */
34#define EXT2_PREALLOCATE 34#define EXT2_DEFAULT_RESERVE_BLOCKS 8
35#define EXT2_DEFAULT_PREALLOC_BLOCKS 8 35/*max window size: 1024(direct blocks) + 3([t,d]indirect blocks) */
36 36#define EXT2_MAX_RESERVE_BLOCKS 1027
37#define EXT2_RESERVE_WINDOW_NOT_ALLOCATED 0
37/* 38/*
38 * The second extended file system version 39 * The second extended file system version
39 */ 40 */
@@ -200,6 +201,8 @@ struct ext2_group_desc
200#define EXT2_IOC_SETFLAGS FS_IOC_SETFLAGS 201#define EXT2_IOC_SETFLAGS FS_IOC_SETFLAGS
201#define EXT2_IOC_GETVERSION FS_IOC_GETVERSION 202#define EXT2_IOC_GETVERSION FS_IOC_GETVERSION
202#define EXT2_IOC_SETVERSION FS_IOC_SETVERSION 203#define EXT2_IOC_SETVERSION FS_IOC_SETVERSION
204#define EXT2_IOC_GETRSVSZ _IOR('f', 5, long)
205#define EXT2_IOC_SETRSVSZ _IOW('f', 6, long)
203 206
204/* 207/*
205 * ioctl commands in 32 bit emulation 208 * ioctl commands in 32 bit emulation
@@ -317,8 +320,9 @@ struct ext2_inode {
317#define EXT2_MOUNT_XATTR_USER 0x004000 /* Extended user attributes */ 320#define EXT2_MOUNT_XATTR_USER 0x004000 /* Extended user attributes */
318#define EXT2_MOUNT_POSIX_ACL 0x008000 /* POSIX Access Control Lists */ 321#define EXT2_MOUNT_POSIX_ACL 0x008000 /* POSIX Access Control Lists */
319#define EXT2_MOUNT_XIP 0x010000 /* Execute in place */ 322#define EXT2_MOUNT_XIP 0x010000 /* Execute in place */
320#define EXT2_MOUNT_USRQUOTA 0x020000 /* user quota */ 323#define EXT2_MOUNT_USRQUOTA 0x020000 /* user quota */
321#define EXT2_MOUNT_GRPQUOTA 0x040000 /* group quota */ 324#define EXT2_MOUNT_GRPQUOTA 0x040000 /* group quota */
325#define EXT2_MOUNT_RESERVATION 0x080000 /* Preallocation */
322 326
323 327
324#define clear_opt(o, opt) o &= ~EXT2_MOUNT_##opt 328#define clear_opt(o, opt) o &= ~EXT2_MOUNT_##opt
@@ -558,4 +562,11 @@ enum {
558#define EXT2_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT2_DIR_ROUND) & \ 562#define EXT2_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT2_DIR_ROUND) & \
559 ~EXT2_DIR_ROUND) 563 ~EXT2_DIR_ROUND)
560 564
565static inline ext2_fsblk_t
566ext2_group_first_block_no(struct super_block *sb, unsigned long group_no)
567{
568 return group_no * (ext2_fsblk_t)EXT2_BLOCKS_PER_GROUP(sb) +
569 le32_to_cpu(EXT2_SB(sb)->s_es->s_first_data_block);
570}
571
561#endif /* _LINUX_EXT2_FS_H */ 572#endif /* _LINUX_EXT2_FS_H */
diff --git a/include/linux/ext2_fs_sb.h b/include/linux/ext2_fs_sb.h
index 199ef37b0a19..f273415ab6f1 100644
--- a/include/linux/ext2_fs_sb.h
+++ b/include/linux/ext2_fs_sb.h
@@ -18,6 +18,52 @@
18 18
19#include <linux/blockgroup_lock.h> 19#include <linux/blockgroup_lock.h>
20#include <linux/percpu_counter.h> 20#include <linux/percpu_counter.h>
21#include <linux/rbtree.h>
22
23/* XXX Here for now... not interested in restructing headers JUST now */
24
25/* data type for block offset of block group */
26typedef int ext2_grpblk_t;
27
28/* data type for filesystem-wide blocks number */
29typedef unsigned long ext2_fsblk_t;
30
31#define E2FSBLK "%lu"
32
33struct ext2_reserve_window {
34 ext2_fsblk_t _rsv_start; /* First byte reserved */
35 ext2_fsblk_t _rsv_end; /* Last byte reserved or 0 */
36};
37
38struct ext2_reserve_window_node {
39 struct rb_node rsv_node;
40 __u32 rsv_goal_size;
41 __u32 rsv_alloc_hit;
42 struct ext2_reserve_window rsv_window;
43};
44
45struct ext2_block_alloc_info {
46 /* information about reservation window */
47 struct ext2_reserve_window_node rsv_window_node;
48 /*
49 * was i_next_alloc_block in ext2_inode_info
50 * is the logical (file-relative) number of the
51 * most-recently-allocated block in this file.
52 * We use this for detecting linearly ascending allocation requests.
53 */
54 __u32 last_alloc_logical_block;
55 /*
56 * Was i_next_alloc_goal in ext2_inode_info
57 * is the *physical* companion to i_next_alloc_block.
58 * it the the physical block number of the block which was most-recentl
59 * allocated to this file. This give us the goal (target) for the next
60 * allocation when we detect linearly ascending requests.
61 */
62 ext2_fsblk_t last_alloc_physical_block;
63};
64
65#define rsv_start rsv_window._rsv_start
66#define rsv_end rsv_window._rsv_end
21 67
22/* 68/*
23 * second extended-fs super-block data in memory 69 * second extended-fs super-block data in memory
@@ -56,6 +102,10 @@ struct ext2_sb_info {
56 struct percpu_counter s_freeinodes_counter; 102 struct percpu_counter s_freeinodes_counter;
57 struct percpu_counter s_dirs_counter; 103 struct percpu_counter s_dirs_counter;
58 struct blockgroup_lock s_blockgroup_lock; 104 struct blockgroup_lock s_blockgroup_lock;
105 /* root of the per fs reservation window tree */
106 spinlock_t s_rsv_window_lock;
107 struct rb_root s_rsv_window_root;
108 struct ext2_reserve_window_node s_rsv_window_head;
59}; 109};
60 110
61#endif /* _LINUX_EXT2_FS_SB */ 111#endif /* _LINUX_EXT2_FS_SB */