ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / ext3 / balloc.c
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
58         group_desc = block_group / EXT3_DESC_PER_BLOCK(sb);
59         desc = block_group % EXT3_DESC_PER_BLOCK(sb);
60         if (!EXT3_SB(sb)->s_group_desc[group_desc]) {
61                 ext3_error (sb, "ext3_get_group_desc",
62                             "Group descriptor not loaded - "
63                             "block_group = %d, group_desc = %lu, desc = %lu",
64                              block_group, group_desc, desc);
65                 return NULL;
66         }
67
68         gdp = (struct ext3_group_desc *) 
69               EXT3_SB(sb)->s_group_desc[group_desc]->b_data;
70         if (bh)
71                 *bh = EXT3_SB(sb)->s_group_desc[group_desc];
72         return gdp + desc;
73 }
74
75 /*
76  * Read the bitmap for a given block_group, reading into the specified 
77  * slot in the superblock's bitmap cache.
78  *
79  * Return buffer_head on success or NULL in case of failure.
80  */
81 static struct buffer_head *
82 read_block_bitmap(struct super_block *sb, unsigned int block_group)
83 {
84         struct ext3_group_desc * desc;
85         struct buffer_head * bh = NULL;
86
87         desc = ext3_get_group_desc (sb, block_group, NULL);
88         if (!desc)
89                 goto error_out;
90         bh = sb_bread(sb, le32_to_cpu(desc->bg_block_bitmap));
91         if (!bh)
92                 ext3_error (sb, "read_block_bitmap",
93                             "Cannot read block bitmap - "
94                             "block_group = %d, block_bitmap = %lu",
95                             block_group, (unsigned long) desc->bg_block_bitmap);
96 error_out:
97         return bh;
98 }
99
100 /* Free given blocks, update quota and i_blocks field */
101 void ext3_free_blocks (handle_t *handle, struct inode * inode,
102                         unsigned long block, unsigned long count)
103 {
104         struct buffer_head *bitmap_bh = NULL;
105         struct buffer_head *gd_bh;
106         unsigned long block_group;
107         unsigned long bit;
108         unsigned long i;
109         unsigned long overflow;
110         struct super_block * sb;
111         struct ext3_group_desc * gdp;
112         struct ext3_super_block * es;
113         struct ext3_sb_info *sbi;
114         int err = 0, ret;
115         int dquot_freed_blocks = 0;
116
117         sb = inode->i_sb;
118         if (!sb) {
119                 printk ("ext3_free_blocks: nonexistent device");
120                 return;
121         }
122         sbi = EXT3_SB(sb);
123         es = EXT3_SB(sb)->s_es;
124         if (block < le32_to_cpu(es->s_first_data_block) ||
125             block + count < block ||
126             block + count > le32_to_cpu(es->s_blocks_count)) {
127                 ext3_error (sb, "ext3_free_blocks",
128                             "Freeing blocks not in datazone - "
129                             "block = %lu, count = %lu", block, count);
130                 goto error_return;
131         }
132
133         ext3_debug ("freeing block %lu\n", block);
134
135 do_more:
136         overflow = 0;
137         block_group = (block - le32_to_cpu(es->s_first_data_block)) /
138                       EXT3_BLOCKS_PER_GROUP(sb);
139         bit = (block - le32_to_cpu(es->s_first_data_block)) %
140                       EXT3_BLOCKS_PER_GROUP(sb);
141         /*
142          * Check to see if we are freeing blocks across a group
143          * boundary.
144          */
145         if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
146                 overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
147                 count -= overflow;
148         }
149         brelse(bitmap_bh);
150         bitmap_bh = read_block_bitmap(sb, block_group);
151         if (!bitmap_bh)
152                 goto error_return;
153         gdp = ext3_get_group_desc (sb, block_group, &gd_bh);
154         if (!gdp)
155                 goto error_return;
156
157         if (in_range (le32_to_cpu(gdp->bg_block_bitmap), block, count) ||
158             in_range (le32_to_cpu(gdp->bg_inode_bitmap), block, count) ||
159             in_range (block, le32_to_cpu(gdp->bg_inode_table),
160                       EXT3_SB(sb)->s_itb_per_group) ||
161             in_range (block + count - 1, le32_to_cpu(gdp->bg_inode_table),
162                       EXT3_SB(sb)->s_itb_per_group))
163                 ext3_error (sb, "ext3_free_blocks",
164                             "Freeing blocks in system zones - "
165                             "Block = %lu, count = %lu",
166                             block, count);
167
168         /*
169          * We are about to start releasing blocks in the bitmap,
170          * so we need undo access.
171          */
172         /* @@@ check errors */
173         BUFFER_TRACE(bitmap_bh, "getting undo access");
174         err = ext3_journal_get_undo_access(handle, bitmap_bh, NULL);
175         if (err)
176                 goto error_return;
177
178         /*
179          * We are about to modify some metadata.  Call the journal APIs
180          * to unshare ->b_data if a currently-committing transaction is
181          * using it
182          */
183         BUFFER_TRACE(gd_bh, "get_write_access");
184         err = ext3_journal_get_write_access(handle, gd_bh);
185         if (err)
186                 goto error_return;
187
188         jbd_lock_bh_state(bitmap_bh);
189
190         for (i = 0; i < count; i++) {
191                 /*
192                  * An HJ special.  This is expensive...
193                  */
194 #ifdef CONFIG_JBD_DEBUG
195                 jbd_unlock_bh_state(bitmap_bh);
196                 {
197                         struct buffer_head *debug_bh;
198                         debug_bh = sb_find_get_block(sb, block + i);
199                         if (debug_bh) {
200                                 BUFFER_TRACE(debug_bh, "Deleted!");
201                                 if (!bh2jh(bitmap_bh)->b_committed_data)
202                                         BUFFER_TRACE(debug_bh,
203                                                 "No commited data in bitmap");
204                                 BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap");
205                                 __brelse(debug_bh);
206                         }
207                 }
208                 jbd_lock_bh_state(bitmap_bh);
209 #endif
210                 /* @@@ This prevents newly-allocated data from being
211                  * freed and then reallocated within the same
212                  * transaction. 
213                  * 
214                  * Ideally we would want to allow that to happen, but to
215                  * do so requires making journal_forget() capable of
216                  * revoking the queued write of a data block, which
217                  * implies blocking on the journal lock.  *forget()
218                  * cannot block due to truncate races.
219                  *
220                  * Eventually we can fix this by making journal_forget()
221                  * return a status indicating whether or not it was able
222                  * to revoke the buffer.  On successful revoke, it is
223                  * safe not to set the allocation bit in the committed
224                  * bitmap, because we know that there is no outstanding
225                  * activity on the buffer any more and so it is safe to
226                  * reallocate it.  
227                  */
228                 BUFFER_TRACE(bitmap_bh, "set in b_committed_data");
229                 J_ASSERT_BH(bitmap_bh,
230                                 bh2jh(bitmap_bh)->b_committed_data != NULL);
231                 ext3_set_bit_atomic(sb_bgl_lock(sbi, block_group), bit + i,
232                                 bh2jh(bitmap_bh)->b_committed_data);
233
234                 /*
235                  * We clear the bit in the bitmap after setting the committed
236                  * data bit, because this is the reverse order to that which
237                  * the allocator uses.
238                  */
239                 BUFFER_TRACE(bitmap_bh, "clear bit");
240                 if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
241                                                 bit + i, bitmap_bh->b_data)) {
242                         jbd_unlock_bh_state(bitmap_bh);
243                         ext3_error(sb, __FUNCTION__,
244                                 "bit already cleared for block %lu", block + i);
245                         jbd_lock_bh_state(bitmap_bh);
246                         BUFFER_TRACE(bitmap_bh, "bit already cleared");
247                 } else {
248                         dquot_freed_blocks++;
249                 }
250         }
251         jbd_unlock_bh_state(bitmap_bh);
252
253         spin_lock(sb_bgl_lock(sbi, block_group));
254         gdp->bg_free_blocks_count =
255                 cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) +
256                         dquot_freed_blocks);
257         spin_unlock(sb_bgl_lock(sbi, block_group));
258         percpu_counter_mod(&sbi->s_freeblocks_counter, count);
259
260         /* We dirtied the bitmap block */
261         BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
262         err = ext3_journal_dirty_metadata(handle, bitmap_bh);
263
264         /* And the group descriptor block */
265         BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
266         ret = ext3_journal_dirty_metadata(handle, gd_bh);
267         if (!err) err = ret;
268
269         if (overflow && !err) {
270                 block += count;
271                 count = overflow;
272                 goto do_more;
273         }
274         sb->s_dirt = 1;
275 error_return:
276         brelse(bitmap_bh);
277         ext3_std_error(sb, err);
278         if (dquot_freed_blocks)
279                 DQUOT_FREE_BLOCK(inode, dquot_freed_blocks);
280         return;
281 }
282
283 /*
284  * For ext3 allocations, we must not reuse any blocks which are
285  * allocated in the bitmap buffer's "last committed data" copy.  This
286  * prevents deletes from freeing up the page for reuse until we have
287  * committed the delete transaction.
288  *
289  * If we didn't do this, then deleting something and reallocating it as
290  * data would allow the old block to be overwritten before the
291  * transaction committed (because we force data to disk before commit).
292  * This would lead to corruption if we crashed between overwriting the
293  * data and committing the delete. 
294  *
295  * @@@ We may want to make this allocation behaviour conditional on
296  * data-writes at some point, and disable it for metadata allocations or
297  * sync-data inodes.
298  */
299 static inline int ext3_test_allocatable(int nr, struct buffer_head *bh)
300 {
301         int ret;
302         struct journal_head *jh = bh2jh(bh);
303
304         if (ext3_test_bit(nr, bh->b_data))
305                 return 0;
306
307         jbd_lock_bh_state(bh);
308         if (!jh->b_committed_data)
309                 ret = 1;
310         else
311                 ret = !ext3_test_bit(nr, jh->b_committed_data);
312         jbd_unlock_bh_state(bh);
313         return ret;
314 }
315
316 /*
317  * Find an allocatable block in a bitmap.  We honour both the bitmap and
318  * its last-committed copy (if that exists), and perform the "most
319  * appropriate allocation" algorithm of looking for a free block near
320  * the initial goal; then for a free byte somewhere in the bitmap; then
321  * for any free bit in the bitmap.
322  */
323 static int
324 find_next_usable_block(int start, struct buffer_head *bh, int maxblocks)
325 {
326         int here, next;
327         char *p, *r;
328         struct journal_head *jh = bh2jh(bh);
329
330         if (start > 0) {
331                 /*
332                  * The goal was occupied; search forward for a free 
333                  * block within the next XX blocks.
334                  *
335                  * end_goal is more or less random, but it has to be
336                  * less than EXT3_BLOCKS_PER_GROUP. Aligning up to the
337                  * next 64-bit boundary is simple..
338                  */
339                 int end_goal = (start + 63) & ~63;
340                 here = ext3_find_next_zero_bit(bh->b_data, end_goal, start);
341                 if (here < end_goal && ext3_test_allocatable(here, bh))
342                         return here;
343                 ext3_debug("Bit not found near goal\n");
344         }
345
346         here = start;
347         if (here < 0)
348                 here = 0;
349
350         p = ((char *)bh->b_data) + (here >> 3);
351         r = memscan(p, 0, (maxblocks - here + 7) >> 3);
352         next = (r - ((char *)bh->b_data)) << 3;
353
354         if (next < maxblocks && ext3_test_allocatable(next, bh))
355                 return next;
356
357         /*
358          * The bitmap search --- search forward alternately through the actual
359          * bitmap and the last-committed copy until we find a bit free in
360          * both
361          */
362         while (here < maxblocks) {
363                 next = ext3_find_next_zero_bit(bh->b_data, maxblocks, here);
364                 if (next >= maxblocks)
365                         return -1;
366                 if (ext3_test_allocatable(next, bh))
367                         return next;
368                 jbd_lock_bh_state(bh);
369                 if (jh->b_committed_data)
370                         here = ext3_find_next_zero_bit(jh->b_committed_data,
371                                                         maxblocks, next);
372                 jbd_unlock_bh_state(bh);
373         }
374         return -1;
375 }
376
377 /*
378  * We think we can allocate this block in this bitmap.  Try to set the bit.
379  * If that succeeds then check that nobody has allocated and then freed the
380  * block since we saw that is was not marked in b_committed_data.  If it _was_
381  * allocated and freed then clear the bit in the bitmap again and return
382  * zero (failure).
383  */
384 static inline int
385 claim_block(spinlock_t *lock, int block, struct buffer_head *bh)
386 {
387         struct journal_head *jh = bh2jh(bh);
388         int ret;
389
390         if (ext3_set_bit_atomic(lock, block, bh->b_data))
391                 return 0;
392         jbd_lock_bh_state(bh);
393         if (jh->b_committed_data && ext3_test_bit(block,jh->b_committed_data)) {
394                 ext3_clear_bit_atomic(lock, block, bh->b_data);
395                 ret = 0;
396         } else {
397                 ret = 1;
398         }
399         jbd_unlock_bh_state(bh);
400         return ret;
401 }
402
403 /*
404  * If we failed to allocate the desired block then we may end up crossing to a
405  * new bitmap.  In that case we must release write access to the old one via
406  * ext3_journal_release_buffer(), else we'll run out of credits.
407  */
408 static int
409 ext3_try_to_allocate(struct super_block *sb, handle_t *handle, int group,
410                 struct buffer_head *bitmap_bh, int goal, int *errp)
411 {
412         int i;
413         int fatal;
414         int credits = 0;
415
416         *errp = 0;
417
418         /*
419          * Make sure we use undo access for the bitmap, because it is critical
420          * that we do the frozen_data COW on bitmap buffers in all cases even
421          * if the buffer is in BJ_Forget state in the committing transaction.
422          */
423         BUFFER_TRACE(bitmap_bh, "get undo access for new block");
424         fatal = ext3_journal_get_undo_access(handle, bitmap_bh, &credits);
425         if (fatal) {
426                 *errp = fatal;
427                 goto fail;
428         }
429
430 repeat:
431         if (goal < 0 || !ext3_test_allocatable(goal, bitmap_bh)) {
432                 goal = find_next_usable_block(goal, bitmap_bh,
433                                         EXT3_BLOCKS_PER_GROUP(sb));
434                 if (goal < 0)
435                         goto fail_access;
436
437                 for (i = 0; i < 7 && goal > 0 &&
438                                 ext3_test_allocatable(goal - 1, bitmap_bh);
439                         i++, goal--);
440         }
441
442         if (!claim_block(sb_bgl_lock(EXT3_SB(sb), group), goal, bitmap_bh)) {
443                 /*
444                  * The block was allocated by another thread, or it was
445                  * allocated and then freed by another thread
446                  */
447                 goal++;
448                 if (goal >= EXT3_BLOCKS_PER_GROUP(sb))
449                         goto fail_access;
450                 goto repeat;
451         }
452
453         BUFFER_TRACE(bitmap_bh, "journal_dirty_metadata for bitmap block");
454         fatal = ext3_journal_dirty_metadata(handle, bitmap_bh);
455         if (fatal) {
456                 *errp = fatal;
457                 goto fail;
458         }
459         return goal;
460
461 fail_access:
462         BUFFER_TRACE(bitmap_bh, "journal_release_buffer");
463         ext3_journal_release_buffer(handle, bitmap_bh, credits);
464 fail:
465         return -1;
466 }
467
468 /*
469  * ext3_new_block uses a goal block to assist allocation.  If the goal is
470  * free, or there is a free block within 32 blocks of the goal, that block
471  * is allocated.  Otherwise a forward search is made for a free block; within 
472  * each block group the search first looks for an entire free byte in the block
473  * bitmap, and then for any free bit if that fails.
474  * This function also updates quota and i_blocks field.
475  */
476 int
477 ext3_new_block(handle_t *handle, struct inode *inode, unsigned long goal,
478                 u32 *prealloc_count, u32 *prealloc_block, int *errp)
479 {
480         struct buffer_head *bitmap_bh = NULL;   /* bh */
481         struct buffer_head *gdp_bh;             /* bh2 */
482         int group_no;                           /* i */
483         int ret_block;                          /* j */
484         int bgi;                                /* blockgroup iteration index */
485         int target_block;                       /* tmp */
486         int fatal = 0, err;
487         int performed_allocation = 0;
488         int free_blocks, root_blocks;
489         struct super_block *sb;
490         struct ext3_group_desc *gdp;
491         struct ext3_super_block *es;
492         struct ext3_sb_info *sbi;
493 #ifdef EXT3FS_DEBUG
494         static int goal_hits, goal_attempts;
495 #endif
496         *errp = -ENOSPC;
497         sb = inode->i_sb;
498         if (!sb) {
499                 printk("ext3_new_block: nonexistent device");
500                 return 0;
501         }
502
503         /*
504          * Check quota for allocation of this block.
505          */
506         if (DQUOT_ALLOC_BLOCK(inode, 1)) {
507                 *errp = -EDQUOT;
508                 return 0;
509         }
510
511         sbi = EXT3_SB(sb);
512         es = EXT3_SB(sb)->s_es;
513         ext3_debug("goal=%lu.\n", goal);
514
515         free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
516         root_blocks = le32_to_cpu(es->s_r_blocks_count);
517         if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) &&
518                 sbi->s_resuid != current->fsuid &&
519                 (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) {
520                 *errp = -ENOSPC;
521                 goto out;
522         }
523
524         /*
525          * First, test whether the goal block is free.
526          */
527         if (goal < le32_to_cpu(es->s_first_data_block) ||
528             goal >= le32_to_cpu(es->s_blocks_count))
529                 goal = le32_to_cpu(es->s_first_data_block);
530         group_no = (goal - le32_to_cpu(es->s_first_data_block)) /
531                         EXT3_BLOCKS_PER_GROUP(sb);
532         gdp = ext3_get_group_desc(sb, group_no, &gdp_bh);
533         if (!gdp)
534                 goto io_error;
535
536         free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
537         if (free_blocks > 0) {
538                 ret_block = ((goal - le32_to_cpu(es->s_first_data_block)) %
539                                 EXT3_BLOCKS_PER_GROUP(sb));
540                 bitmap_bh = read_block_bitmap(sb, group_no);
541                 if (!bitmap_bh)
542                         goto io_error;
543                 ret_block = ext3_try_to_allocate(sb, handle, group_no,
544                                         bitmap_bh, ret_block, &fatal);
545                 if (fatal)
546                         goto out;
547                 if (ret_block >= 0)
548                         goto allocated;
549         }
550
551         /*
552          * Now search the rest of the groups.  We assume that 
553          * i and gdp correctly point to the last group visited.
554          */
555         for (bgi = 0; bgi < EXT3_SB(sb)->s_groups_count; bgi++) {
556                 group_no++;
557                 if (group_no >= EXT3_SB(sb)->s_groups_count)
558                         group_no = 0;
559                 gdp = ext3_get_group_desc(sb, group_no, &gdp_bh);
560                 if (!gdp) {
561                         *errp = -EIO;
562                         goto out;
563                 }
564                 free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
565                 if (free_blocks <= 0)
566                         continue;
567
568                 brelse(bitmap_bh);
569                 bitmap_bh = read_block_bitmap(sb, group_no);
570                 if (!bitmap_bh)
571                         goto io_error;
572                 ret_block = ext3_try_to_allocate(sb, handle, group_no,
573                                                 bitmap_bh, -1, &fatal);
574                 if (fatal)
575                         goto out;
576                 if (ret_block >= 0) 
577                         goto allocated;
578         }
579
580         /* No space left on the device */
581         *errp = -ENOSPC;
582         goto out;
583
584 allocated:
585
586         ext3_debug("using block group %d(%d)\n",
587                         group_no, gdp->bg_free_blocks_count);
588
589         BUFFER_TRACE(gdp_bh, "get_write_access");
590         fatal = ext3_journal_get_write_access(handle, gdp_bh);
591         if (fatal)
592                 goto out;
593
594         target_block = ret_block + group_no * EXT3_BLOCKS_PER_GROUP(sb)
595                                 + le32_to_cpu(es->s_first_data_block);
596
597         if (target_block == le32_to_cpu(gdp->bg_block_bitmap) ||
598             target_block == le32_to_cpu(gdp->bg_inode_bitmap) ||
599             in_range(target_block, le32_to_cpu(gdp->bg_inode_table),
600                       EXT3_SB(sb)->s_itb_per_group))
601                 ext3_error(sb, "ext3_new_block",
602                             "Allocating block in system zone - "
603                             "block = %u", target_block);
604
605         performed_allocation = 1;
606
607 #ifdef CONFIG_JBD_DEBUG
608         {
609                 struct buffer_head *debug_bh;
610
611                 /* Record bitmap buffer state in the newly allocated block */
612                 debug_bh = sb_find_get_block(sb, target_block);
613                 if (debug_bh) {
614                         BUFFER_TRACE(debug_bh, "state when allocated");
615                         BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap state");
616                         brelse(debug_bh);
617                 }
618         }
619         jbd_lock_bh_state(bitmap_bh);
620         spin_lock(sb_bgl_lock(sbi, group_no));
621         if (buffer_jbd(bitmap_bh) && bh2jh(bitmap_bh)->b_committed_data) {
622                 if (ext3_test_bit(ret_block,
623                                 bh2jh(bitmap_bh)->b_committed_data)) {
624                         printk("%s: block was unexpectedly set in "
625                                 "b_committed_data\n", __FUNCTION__);
626                 }
627         }
628         ext3_debug("found bit %d\n", ret_block);
629         spin_unlock(sb_bgl_lock(sbi, group_no));
630         jbd_unlock_bh_state(bitmap_bh);
631 #endif
632
633         /* ret_block was blockgroup-relative.  Now it becomes fs-relative */
634         ret_block = target_block;
635
636         if (ret_block >= le32_to_cpu(es->s_blocks_count)) {
637                 ext3_error(sb, "ext3_new_block",
638                             "block(%d) >= blocks count(%d) - "
639                             "block_group = %d, es == %p ", ret_block,
640                         le32_to_cpu(es->s_blocks_count), group_no, es);
641                 goto out;
642         }
643
644         /*
645          * It is up to the caller to add the new buffer to a journal
646          * list of some description.  We don't know in advance whether
647          * the caller wants to use it as metadata or data.
648          */
649         ext3_debug("allocating block %d. Goal hits %d of %d.\n",
650                         ret_block, goal_hits, goal_attempts);
651
652         spin_lock(sb_bgl_lock(sbi, group_no));
653         gdp->bg_free_blocks_count =
654                         cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) - 1);
655         spin_unlock(sb_bgl_lock(sbi, group_no));
656         percpu_counter_mod(&sbi->s_freeblocks_counter, -1);
657
658         BUFFER_TRACE(gdp_bh, "journal_dirty_metadata for group descriptor");
659         err = ext3_journal_dirty_metadata(handle, gdp_bh);
660         if (!fatal)
661                 fatal = err;
662
663         sb->s_dirt = 1;
664         if (fatal)
665                 goto out;
666
667         *errp = 0;
668         brelse(bitmap_bh);
669         return ret_block;
670
671 io_error:
672         *errp = -EIO;
673 out:
674         if (fatal) {
675                 *errp = fatal;
676                 ext3_std_error(sb, fatal);
677         }
678         /*
679          * Undo the block allocation
680          */
681         if (!performed_allocation)
682                 DQUOT_FREE_BLOCK(inode, 1);
683         brelse(bitmap_bh);
684         return 0;
685 }
686
687 unsigned long ext3_count_free_blocks(struct super_block *sb)
688 {
689         unsigned long desc_count;
690         struct ext3_group_desc *gdp;
691         int i;
692 #ifdef EXT3FS_DEBUG
693         struct ext3_super_block *es;
694         unsigned long bitmap_count, x;
695         struct buffer_head *bitmap_bh = NULL;
696
697         lock_super(sb);
698         es = EXT3_SB(sb)->s_es;
699         desc_count = 0;
700         bitmap_count = 0;
701         gdp = NULL;
702         for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
703                 gdp = ext3_get_group_desc(sb, i, NULL);
704                 if (!gdp)
705                         continue;
706                 desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
707                 brelse(bitmap_bh);
708                 bitmap_bh = read_block_bitmap(sb, i);
709                 if (bitmap_bh == NULL)
710                         continue;
711
712                 x = ext3_count_free(bitmap_bh, sb->s_blocksize);
713                 printk("group %d: stored = %d, counted = %lu\n",
714                         i, le16_to_cpu(gdp->bg_free_blocks_count), x);
715                 bitmap_count += x;
716         }
717         brelse(bitmap_bh);
718         printk("ext3_count_free_blocks: stored = %u, computed = %lu, %lu\n",
719                le32_to_cpu(es->s_free_blocks_count), desc_count, bitmap_count);
720         unlock_super(sb);
721         return bitmap_count;
722 #else
723         desc_count = 0;
724         for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
725                 gdp = ext3_get_group_desc(sb, i, NULL);
726                 if (!gdp)
727                         continue;
728                 desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
729         }
730
731         return desc_count;
732 #endif
733 }
734
735 static inline int block_in_use(unsigned long block,
736                                 struct super_block * sb,
737                                 unsigned char * map)
738 {
739         return ext3_test_bit ((block -
740                 le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block)) %
741                          EXT3_BLOCKS_PER_GROUP(sb), map);
742 }
743
744 static inline int test_root(int a, int b)
745 {
746         if (a == 0)
747                 return 1;
748         while (1) {
749                 if (a == 1)
750                         return 1;
751                 if (a % b)
752                         return 0;
753                 a = a / b;
754         }
755 }
756
757 int ext3_group_sparse(int group)
758 {
759         return (test_root(group, 3) || test_root(group, 5) ||
760                 test_root(group, 7));
761 }
762
763 /**
764  *      ext3_bg_has_super - number of blocks used by the superblock in group
765  *      @sb: superblock for filesystem
766  *      @group: group number to check
767  *
768  *      Return the number of blocks used by the superblock (primary or backup)
769  *      in this group.  Currently this will be only 0 or 1.
770  */
771 int ext3_bg_has_super(struct super_block *sb, int group)
772 {
773         if (EXT3_HAS_RO_COMPAT_FEATURE(sb,EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
774             !ext3_group_sparse(group))
775                 return 0;
776         return 1;
777 }
778
779 /**
780  *      ext3_bg_num_gdb - number of blocks used by the group table in group
781  *      @sb: superblock for filesystem
782  *      @group: group number to check
783  *
784  *      Return the number of blocks used by the group descriptor table
785  *      (primary or backup) in this group.  In the future there may be a
786  *      different number of descriptor blocks in each group.
787  */
788 unsigned long ext3_bg_num_gdb(struct super_block *sb, int group)
789 {
790         if (EXT3_HAS_RO_COMPAT_FEATURE(sb,EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
791             !ext3_group_sparse(group))
792                 return 0;
793         return EXT3_SB(sb)->s_gdb_count;
794 }
795
796 #ifdef CONFIG_EXT3_CHECK
797 /* Called at mount-time, super-block is locked */
798 void ext3_check_blocks_bitmap (struct super_block * sb)
799 {
800         struct ext3_super_block *es;
801         unsigned long desc_count, bitmap_count, x, j;
802         unsigned long desc_blocks;
803         struct buffer_head *bitmap_bh = NULL;
804         struct ext3_group_desc *gdp;
805         int i;
806
807         es = EXT3_SB(sb)->s_es;
808         desc_count = 0;
809         bitmap_count = 0;
810         gdp = NULL;
811         for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
812                 gdp = ext3_get_group_desc (sb, i, NULL);
813                 if (!gdp)
814                         continue;
815                 desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
816                 brelse(bitmap_bh);
817                 bitmap_bh = read_block_bitmap(sb, i);
818                 if (bitmap_bh == NULL)
819                         continue;
820
821                 if (ext3_bg_has_super(sb, i) &&
822                                 !ext3_test_bit(0, bitmap_bh->b_data))
823                         ext3_error(sb, __FUNCTION__,
824                                    "Superblock in group %d is marked free", i);
825
826                 desc_blocks = ext3_bg_num_gdb(sb, i);
827                 for (j = 0; j < desc_blocks; j++)
828                         if (!ext3_test_bit(j + 1, bitmap_bh->b_data))
829                                 ext3_error(sb, __FUNCTION__,
830                                            "Descriptor block #%ld in group "
831                                            "%d is marked free", j, i);
832
833                 if (!block_in_use (le32_to_cpu(gdp->bg_block_bitmap),
834                                                 sb, bitmap_bh->b_data))
835                         ext3_error (sb, "ext3_check_blocks_bitmap",
836                                     "Block bitmap for group %d is marked free",
837                                     i);
838
839                 if (!block_in_use (le32_to_cpu(gdp->bg_inode_bitmap),
840                                                 sb, bitmap_bh->b_data))
841                         ext3_error (sb, "ext3_check_blocks_bitmap",
842                                     "Inode bitmap for group %d is marked free",
843                                     i);
844
845                 for (j = 0; j < EXT3_SB(sb)->s_itb_per_group; j++)
846                         if (!block_in_use (le32_to_cpu(gdp->bg_inode_table) + j,
847                                                         sb, bitmap_bh->b_data))
848                                 ext3_error (sb, "ext3_check_blocks_bitmap",
849                                             "Block #%d of the inode table in "
850                                             "group %d is marked free", j, i);
851
852                 x = ext3_count_free(bitmap_bh, sb->s_blocksize);
853                 if (le16_to_cpu(gdp->bg_free_blocks_count) != x)
854                         ext3_error (sb, "ext3_check_blocks_bitmap",
855                                     "Wrong free blocks count for group %d, "
856                                     "stored = %d, counted = %lu", i,
857                                     le16_to_cpu(gdp->bg_free_blocks_count), x);
858                 bitmap_count += x;
859         }
860         brelse(bitmap_bh);
861         if (le32_to_cpu(es->s_free_blocks_count) != bitmap_count)
862                 ext3_error (sb, "ext3_check_blocks_bitmap",
863                         "Wrong free blocks count in super block, "
864                         "stored = %lu, counted = %lu",
865                         (unsigned long)le32_to_cpu(es->s_free_blocks_count),
866                         bitmap_count);
867 }
868 #endif