2 * linux/fs/ext2/balloc.c
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)
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
14 #include <linux/config.h>
16 #include <linux/quotaops.h>
17 #include <linux/sched.h>
18 #include <linux/buffer_head.h>
19 #include <linux/capability.h>
20 #include <linux/vs_base.h>
21 #include <linux/vs_dlimit.h>
24 * balloc.c contains the blocks allocation and deallocation routines
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.
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 ext2_read_super).
39 #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
41 struct ext2_group_desc * ext2_get_group_desc(struct super_block * sb,
42 unsigned int block_group,
43 struct buffer_head ** bh)
45 unsigned long group_desc;
47 struct ext2_group_desc * desc;
48 struct ext2_sb_info *sbi = EXT2_SB(sb);
50 if (block_group >= sbi->s_groups_count) {
51 ext2_error (sb, "ext2_get_group_desc",
52 "block_group >= groups_count - "
53 "block_group = %d, groups_count = %lu",
54 block_group, sbi->s_groups_count);
59 group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(sb);
60 offset = block_group & (EXT2_DESC_PER_BLOCK(sb) - 1);
61 if (!sbi->s_group_desc[group_desc]) {
62 ext2_error (sb, "ext2_get_group_desc",
63 "Group descriptor not loaded - "
64 "block_group = %d, group_desc = %lu, desc = %lu",
65 block_group, group_desc, offset);
69 desc = (struct ext2_group_desc *) sbi->s_group_desc[group_desc]->b_data;
71 *bh = sbi->s_group_desc[group_desc];
76 * Read the bitmap for a given block_group, reading into the specified
77 * slot in the superblock's bitmap cache.
79 * Return buffer_head on success or NULL in case of failure.
81 static struct buffer_head *
82 read_block_bitmap(struct super_block *sb, unsigned int block_group)
84 struct ext2_group_desc * desc;
85 struct buffer_head * bh = NULL;
87 desc = ext2_get_group_desc (sb, block_group, NULL);
90 bh = sb_bread(sb, le32_to_cpu(desc->bg_block_bitmap));
92 ext2_error (sb, "read_block_bitmap",
93 "Cannot read block bitmap - "
94 "block_group = %d, block_bitmap = %u",
95 block_group, le32_to_cpu(desc->bg_block_bitmap));
101 * Set sb->s_dirt here because the superblock was "logically" altered. We
102 * need to recalculate its free blocks count and flush it out.
104 static int reserve_blocks(struct super_block *sb, int count)
106 struct ext2_sb_info *sbi = EXT2_SB(sb);
107 struct ext2_super_block *es = sbi->s_es;
108 unsigned free_blocks;
109 unsigned root_blocks;
111 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
112 root_blocks = le32_to_cpu(es->s_r_blocks_count);
114 DLIMIT_ADJUST_BLOCK(sb, vx_current_xid(), &free_blocks, &root_blocks);
116 if (free_blocks < count)
119 if (free_blocks < root_blocks + count && !capable(CAP_SYS_RESOURCE) &&
120 sbi->s_resuid != current->fsuid &&
121 (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) {
123 * We are too close to reserve and we are not privileged.
124 * Can we allocate anything at all?
126 if (free_blocks > root_blocks)
127 count = free_blocks - root_blocks;
132 percpu_counter_mod(&sbi->s_freeblocks_counter, -count);
137 static void release_blocks(struct super_block *sb, int count)
140 struct ext2_sb_info *sbi = EXT2_SB(sb);
142 percpu_counter_mod(&sbi->s_freeblocks_counter, count);
147 static int group_reserve_blocks(struct ext2_sb_info *sbi, int group_no,
148 struct ext2_group_desc *desc, struct buffer_head *bh, int count)
150 unsigned free_blocks;
152 if (!desc->bg_free_blocks_count)
155 spin_lock(sb_bgl_lock(sbi, group_no));
156 free_blocks = le16_to_cpu(desc->bg_free_blocks_count);
157 if (free_blocks < count)
159 desc->bg_free_blocks_count = cpu_to_le16(free_blocks - count);
160 spin_unlock(sb_bgl_lock(sbi, group_no));
161 mark_buffer_dirty(bh);
165 static void group_release_blocks(struct super_block *sb, int group_no,
166 struct ext2_group_desc *desc, struct buffer_head *bh, int count)
169 struct ext2_sb_info *sbi = EXT2_SB(sb);
170 unsigned free_blocks;
172 spin_lock(sb_bgl_lock(sbi, group_no));
173 free_blocks = le16_to_cpu(desc->bg_free_blocks_count);
174 desc->bg_free_blocks_count = cpu_to_le16(free_blocks + count);
175 spin_unlock(sb_bgl_lock(sbi, group_no));
177 mark_buffer_dirty(bh);
181 /* Free given blocks, update quota and i_blocks field */
182 void ext2_free_blocks (struct inode * inode, unsigned long block,
185 struct buffer_head *bitmap_bh = NULL;
186 struct buffer_head * bh2;
187 unsigned long block_group;
190 unsigned long overflow;
191 struct super_block * sb = inode->i_sb;
192 struct ext2_sb_info * sbi = EXT2_SB(sb);
193 struct ext2_group_desc * desc;
194 struct ext2_super_block * es = sbi->s_es;
195 unsigned freed = 0, group_freed;
197 if (block < le32_to_cpu(es->s_first_data_block) ||
198 block + count < block ||
199 block + count > le32_to_cpu(es->s_blocks_count)) {
200 ext2_error (sb, "ext2_free_blocks",
201 "Freeing blocks not in datazone - "
202 "block = %lu, count = %lu", block, count);
206 ext2_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1);
210 block_group = (block - le32_to_cpu(es->s_first_data_block)) /
211 EXT2_BLOCKS_PER_GROUP(sb);
212 bit = (block - le32_to_cpu(es->s_first_data_block)) %
213 EXT2_BLOCKS_PER_GROUP(sb);
215 * Check to see if we are freeing blocks across a group
218 if (bit + count > EXT2_BLOCKS_PER_GROUP(sb)) {
219 overflow = bit + count - EXT2_BLOCKS_PER_GROUP(sb);
223 bitmap_bh = read_block_bitmap(sb, block_group);
227 desc = ext2_get_group_desc (sb, block_group, &bh2);
231 if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) ||
232 in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) ||
233 in_range (block, le32_to_cpu(desc->bg_inode_table),
234 sbi->s_itb_per_group) ||
235 in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table),
236 sbi->s_itb_per_group))
237 ext2_error (sb, "ext2_free_blocks",
238 "Freeing blocks in system zones - "
239 "Block = %lu, count = %lu",
242 for (i = 0, group_freed = 0; i < count; i++) {
243 if (!ext2_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
244 bit + i, bitmap_bh->b_data)) {
245 ext2_error(sb, __FUNCTION__,
246 "bit already cleared for block %lu", block + i);
252 mark_buffer_dirty(bitmap_bh);
253 if (sb->s_flags & MS_SYNCHRONOUS)
254 sync_dirty_buffer(bitmap_bh);
256 group_release_blocks(sb, block_group, desc, bh2, group_freed);
257 freed += group_freed;
266 DLIMIT_FREE_BLOCK(inode, freed);
267 release_blocks(sb, freed);
268 DQUOT_FREE_BLOCK(inode, freed);
271 static int grab_block(spinlock_t *lock, char *map, unsigned size, int goal)
276 if (!ext2_test_bit(goal, map))
282 * The goal was occupied; search forward for a free
283 * block within the next XX blocks.
285 * end_goal is more or less random, but it has to be
286 * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the
287 * next 64-bit boundary is simple..
289 k = (goal + 63) & ~63;
290 goal = ext2_find_next_zero_bit(map, k, goal);
294 * Search in the remainder of the current group.
298 p = map + (goal >> 3);
299 r = memscan(p, 0, (size - goal + 7) >> 3);
303 * We have succeeded in finding a free byte in the block
304 * bitmap. Now search backwards to find the start of this
305 * group of free blocks - won't take more than 7 iterations.
307 for (goal = k; goal && !ext2_test_bit (goal - 1, map); goal--)
312 k = ext2_find_next_zero_bit ((u32 *)map, size, goal);
319 if (ext2_set_bit_atomic(lock, goal, (void *) map))
325 * ext2_new_block uses a goal block to assist allocation. If the goal is
326 * free, or there is a free block within 32 blocks of the goal, that block
327 * is allocated. Otherwise a forward search is made for a free block; within
328 * each block group the search first looks for an entire free byte in the block
329 * bitmap, and then for any free bit if that fails.
330 * This function also updates quota and i_blocks field.
332 int ext2_new_block(struct inode *inode, unsigned long goal,
333 u32 *prealloc_count, u32 *prealloc_block, int *err)
335 struct buffer_head *bitmap_bh = NULL;
336 struct buffer_head *gdp_bh; /* bh2 */
337 struct ext2_group_desc *desc;
338 int group_no; /* i */
339 int ret_block; /* j */
340 int group_idx; /* k */
341 int target_block; /* tmp */
343 struct super_block *sb = inode->i_sb;
344 struct ext2_sb_info *sbi = EXT2_SB(sb);
345 struct ext2_super_block *es = sbi->s_es;
346 unsigned group_size = EXT2_BLOCKS_PER_GROUP(sb);
347 unsigned prealloc_goal = es->s_prealloc_blocks;
348 unsigned group_alloc = 0, es_alloc, dq_alloc;
349 int nr_scanned_groups;
351 if (!prealloc_goal--)
352 prealloc_goal = EXT2_DEFAULT_PREALLOC_BLOCKS - 1;
353 if (!prealloc_count || *prealloc_count)
356 if (DQUOT_ALLOC_BLOCK(inode, 1)) {
361 while (prealloc_goal && DQUOT_PREALLOC_BLOCK(inode, prealloc_goal))
364 dq_alloc = prealloc_goal + 1;
365 es_alloc = reserve_blocks(sb, dq_alloc);
370 if (DLIMIT_ALLOC_BLOCK(inode, es_alloc)) {
375 ext2_debug ("goal=%lu.\n", goal);
377 if (goal < le32_to_cpu(es->s_first_data_block) ||
378 goal >= le32_to_cpu(es->s_blocks_count))
379 goal = le32_to_cpu(es->s_first_data_block);
380 group_no = (goal - le32_to_cpu(es->s_first_data_block)) / group_size;
381 desc = ext2_get_group_desc (sb, group_no, &gdp_bh);
384 * gdp_bh may still be uninitialised. But group_release_blocks
385 * will not touch it because group_alloc is zero.
390 group_alloc = group_reserve_blocks(sbi, group_no, desc,
393 ret_block = ((goal - le32_to_cpu(es->s_first_data_block)) %
396 bitmap_bh = read_block_bitmap(sb, group_no);
400 ext2_debug("goal is at %d:%d.\n", group_no, ret_block);
402 ret_block = grab_block(sb_bgl_lock(sbi, group_no),
403 bitmap_bh->b_data, group_size, ret_block);
406 group_release_blocks(sb, group_no, desc, gdp_bh, group_alloc);
410 ext2_debug ("Bit not found in block group %d.\n", group_no);
413 * Now search the rest of the groups. We assume that
414 * i and desc correctly point to the last group visited.
416 nr_scanned_groups = 0;
418 for (group_idx = 0; !group_alloc &&
419 group_idx < sbi->s_groups_count; group_idx++) {
421 if (group_no >= sbi->s_groups_count)
423 desc = ext2_get_group_desc(sb, group_no, &gdp_bh);
426 group_alloc = group_reserve_blocks(sbi, group_no, desc,
434 bitmap_bh = read_block_bitmap(sb, group_no);
438 ret_block = grab_block(sb_bgl_lock(sbi, group_no), bitmap_bh->b_data,
442 * If a free block counter is corrupted we can loop inifintely.
446 if (nr_scanned_groups > 2 * sbi->s_groups_count) {
447 ext2_error(sb, "ext2_new_block",
448 "corrupted free blocks counters");
452 * Someone else grabbed the last free block in this blockgroup
453 * before us. Retry the scan.
455 group_release_blocks(sb, group_no, desc, gdp_bh, group_alloc);
461 ext2_debug("using block group %d(%d)\n",
462 group_no, desc->bg_free_blocks_count);
464 target_block = ret_block + group_no * group_size +
465 le32_to_cpu(es->s_first_data_block);
467 if (target_block == le32_to_cpu(desc->bg_block_bitmap) ||
468 target_block == le32_to_cpu(desc->bg_inode_bitmap) ||
469 in_range(target_block, le32_to_cpu(desc->bg_inode_table),
470 sbi->s_itb_per_group))
471 ext2_error (sb, "ext2_new_block",
472 "Allocating block in system zone - "
473 "block = %u", target_block);
475 if (target_block >= le32_to_cpu(es->s_blocks_count)) {
476 ext2_error (sb, "ext2_new_block",
477 "block(%d) >= blocks count(%d) - "
478 "block_group = %d, es == %p ", ret_block,
479 le32_to_cpu(es->s_blocks_count), group_no, es);
482 block = target_block;
484 /* OK, we _had_ allocated something */
485 ext2_debug("found bit %d\n", ret_block);
492 * Do block preallocation now if required.
494 write_lock(&EXT2_I(inode)->i_meta_lock);
495 if (group_alloc && !*prealloc_count) {
498 for (n = 0; n < group_alloc && ++ret_block < group_size; n++) {
499 if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group_no),
501 (void*) bitmap_bh->b_data))
504 *prealloc_block = block + 1;
510 write_unlock(&EXT2_I(inode)->i_meta_lock);
512 mark_buffer_dirty(bitmap_bh);
513 if (sb->s_flags & MS_SYNCHRONOUS)
514 sync_dirty_buffer(bitmap_bh);
516 ext2_debug ("allocating block %d. ", block);
520 group_release_blocks(sb, group_no, desc, gdp_bh, group_alloc);
521 DLIMIT_FREE_BLOCK(inode, es_alloc);
523 release_blocks(sb, es_alloc);
525 DQUOT_FREE_BLOCK(inode, dq_alloc);
535 unsigned long ext2_count_free_blocks (struct super_block * sb)
537 struct ext2_group_desc * desc;
538 unsigned long desc_count = 0;
541 unsigned long bitmap_count, x;
542 struct ext2_super_block *es;
545 es = EXT2_SB(sb)->s_es;
549 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
550 struct buffer_head *bitmap_bh;
551 desc = ext2_get_group_desc (sb, i, NULL);
554 desc_count += le16_to_cpu(desc->bg_free_blocks_count);
555 bitmap_bh = read_block_bitmap(sb, i);
559 x = ext2_count_free(bitmap_bh, sb->s_blocksize);
560 printk ("group %d: stored = %d, counted = %lu\n",
561 i, le16_to_cpu(desc->bg_free_blocks_count), x);
565 printk("ext2_count_free_blocks: stored = %lu, computed = %lu, %lu\n",
566 (long)le32_to_cpu(es->s_free_blocks_count),
567 desc_count, bitmap_count);
571 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
572 desc = ext2_get_group_desc (sb, i, NULL);
575 desc_count += le16_to_cpu(desc->bg_free_blocks_count);
582 block_in_use(unsigned long block, struct super_block *sb, unsigned char *map)
584 return ext2_test_bit ((block -
585 le32_to_cpu(EXT2_SB(sb)->s_es->s_first_data_block)) %
586 EXT2_BLOCKS_PER_GROUP(sb), map);
589 static inline int test_root(int a, int b)
598 static int ext2_group_sparse(int group)
602 return (test_root(group, 3) || test_root(group, 5) ||
603 test_root(group, 7));
607 * ext2_bg_has_super - number of blocks used by the superblock in group
608 * @sb: superblock for filesystem
609 * @group: group number to check
611 * Return the number of blocks used by the superblock (primary or backup)
612 * in this group. Currently this will be only 0 or 1.
614 int ext2_bg_has_super(struct super_block *sb, int group)
616 if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
617 !ext2_group_sparse(group))
623 * ext2_bg_num_gdb - number of blocks used by the group table in group
624 * @sb: superblock for filesystem
625 * @group: group number to check
627 * Return the number of blocks used by the group descriptor table
628 * (primary or backup) in this group. In the future there may be a
629 * different number of descriptor blocks in each group.
631 unsigned long ext2_bg_num_gdb(struct super_block *sb, int group)
633 if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
634 !ext2_group_sparse(group))
636 return EXT2_SB(sb)->s_gdb_count;