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