14 #include <linux/time.h>
15 #include <linux/capability.h>
26 static unsigned ext4_num_base_meta_clusters(
struct super_block *
sb,
57 if (actual_group == block_group)
69 unsigned num_clusters;
70 int block_cluster = -1, inode_cluster = -1, itbl_cluster = -1,
i,
c;
73 struct ext4_sb_info *sbi =
EXT4_SB(sb);
78 num_clusters = ext4_num_base_meta_clusters(sb, block_group);
95 if (block_cluster < num_clusters)
97 else if (block_cluster == num_clusters) {
106 if (inode_cluster < num_clusters)
108 else if (inode_cluster == num_clusters) {
115 for (
i = 0;
i < sbi->s_itb_per_group;
i++) {
116 if (ext4_block_in_group(sb, itbl_blk +
i, block_group)) {
118 if ((c < num_clusters) || (c == inode_cluster) ||
119 (c == block_cluster) || (c == itbl_cluster))
121 if (c == num_clusters) {
130 if (block_cluster != -1)
132 if (inode_cluster != -1)
138 static unsigned int num_clusters_in_group(
struct super_block *sb,
143 if (block_group == ext4_get_groups_count(sb) - 1) {
150 blocks = ext4_blocks_count(
EXT4_SB(sb)->s_es) -
151 ext4_group_first_block_no(sb, block_group);
162 unsigned int bit, bit_max;
163 struct ext4_sb_info *sbi =
EXT4_SB(sb);
167 J_ASSERT_BH(bh, buffer_locked(bh));
172 ext4_error(sb,
"Checksum bad for group %u", block_group);
182 bit_max = ext4_num_base_meta_clusters(sb, block_group);
183 for (bit = 0; bit < bit_max; bit++)
186 start = ext4_group_first_block_no(sb, block_group);
193 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
197 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
202 sbi->s_itb_per_group; tmp++) {
203 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
225 return num_clusters_in_group(sb, block_group) -
249 struct buffer_head **bh)
251 unsigned int group_desc;
255 struct ext4_sb_info *sbi =
EXT4_SB(sb);
257 if (block_group >= ngroups) {
258 ext4_error(sb,
"block_group >= groups_count - block_group = %u,"
259 " groups_count = %u", block_group, ngroups);
264 group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb);
266 if (!sbi->s_group_desc[group_desc]) {
267 ext4_error(sb,
"Group descriptor not loaded - "
268 "block_group = %u, group_desc = %u, desc = %u",
269 block_group, group_desc, offset);
274 (
__u8 *)sbi->s_group_desc[group_desc]->b_data +
277 *bh = sbi->s_group_desc[group_desc];
287 unsigned int block_group,
288 struct buffer_head *bh)
304 group_first_block = ext4_group_first_block_no(sb, block_group);
308 offset = blk - group_first_block;
315 offset = blk - group_first_block;
322 offset = blk - group_first_block;
324 offset +
EXT4_SB(sb)->s_itb_per_group,
326 if (next_zero_bit < offset +
EXT4_SB(sb)->s_itb_per_group)
334 unsigned int block_group,
335 struct buffer_head *bh)
339 if (buffer_verified(bh))
342 ext4_lock_group(sb, block_group);
343 blk = ext4_valid_block_bitmap(sb, desc, block_group, bh);
345 ext4_unlock_group(sb, block_group);
346 ext4_error(sb,
"bg %u: block %llu: invalid block bitmap",
352 ext4_unlock_group(sb, block_group);
353 ext4_error(sb,
"bg %u: bad block bitmap checksum", block_group);
356 set_buffer_verified(bh);
357 ext4_unlock_group(sb, block_group);
374 struct buffer_head *bh;
381 bh = sb_getblk(sb, bitmap_blk);
383 ext4_error(sb,
"Cannot get buffer for block bitmap - "
384 "block_group = %u, block_bitmap = %llu",
385 block_group, bitmap_blk);
389 if (bitmap_uptodate(bh))
393 if (bitmap_uptodate(bh)) {
397 ext4_lock_group(sb, block_group);
400 set_bitmap_uptodate(bh);
401 set_buffer_uptodate(bh);
402 ext4_unlock_group(sb, block_group);
406 ext4_unlock_group(sb, block_group);
407 if (buffer_uptodate(bh)) {
412 set_bitmap_uptodate(bh);
420 trace_ext4_read_block_bitmap_load(sb, block_group);
432 struct buffer_head *bh)
442 if (!buffer_uptodate(bh)) {
443 ext4_error(sb,
"Cannot read block bitmap - "
444 "block_group = %u, block_bitmap = %llu",
445 block_group, (
unsigned long long) bh->b_blocknr);
448 clear_buffer_new(bh);
457 struct buffer_head *bh;
476 static int ext4_has_free_clusters(
struct ext4_sb_info *sbi,
479 s64 free_clusters, dirty_clusters, root_clusters;
483 free_clusters = percpu_counter_read_positive(fcc);
484 dirty_clusters = percpu_counter_read_positive(dcc);
485 root_clusters =
EXT4_B2C(sbi, ext4_r_blocks_count(sbi->s_es));
487 if (free_clusters - (nclusters + root_clusters + dirty_clusters) <
488 EXT4_FREECLUSTERS_WATERMARK) {
489 free_clusters =
EXT4_C2B(sbi, percpu_counter_sum_positive(fcc));
490 dirty_clusters = percpu_counter_sum_positive(dcc);
495 if (free_clusters >= ((root_clusters + nclusters) + dirty_clusters))
504 if (free_clusters >= (nclusters + dirty_clusters))
512 s64 nclusters,
unsigned int flags)
514 if (ext4_has_free_clusters(sbi, nclusters, flags)) {
515 percpu_counter_add(&sbi->s_dirtyclusters_counter, nclusters);
535 if (!ext4_has_free_clusters(
EXT4_SB(sb), 1, 0) ||
540 jbd_debug(1,
"%s: retrying operation after ENOSPC\n", sb->
s_id);
559 unsigned long *
count,
int *errp)
564 memset(&ar, 0,
sizeof(ar));
568 ar.
len = count ? *count : 1;
579 ext4_test_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED)) {
580 spin_lock(&
EXT4_I(inode)->i_block_reservation_lock);
581 EXT4_I(inode)->i_allocated_meta_blocks += ar.
len;
582 spin_unlock(&
EXT4_I(inode)->i_block_reservation_lock);
583 dquot_alloc_block_nofail(inode,
605 struct buffer_head *bitmap_bh =
NULL;
612 for (i = 0; i < ngroups; i++) {
619 if (bitmap_bh ==
NULL)
630 ", computed = %llu, %llu\n",
632 desc_count, bitmap_count);
636 for (i = 0; i < ngroups; i++) {
662 return (test_root(group, 7) || test_root(group, 5) ||
663 test_root(group, 3));
678 !ext4_group_sparse(group))
683 static unsigned long ext4_bg_num_gdb_meta(
struct super_block *sb,
690 if (group == first || group == first + 1 || group == last)
695 static unsigned long ext4_bg_num_gdb_nometa(
struct super_block *sb,
704 return EXT4_SB(sb)->s_gdb_count;
718 unsigned long first_meta_bg =
723 metagroup < first_meta_bg)
724 return ext4_bg_num_gdb_nometa(sb, group);
726 return ext4_bg_num_gdb_meta(sb,group);
734 static unsigned ext4_num_base_meta_clusters(
struct super_block *sb,
737 struct ext4_sb_info *sbi =
EXT4_SB(sb);
744 block_group <
le32_to_cpu(sbi->s_es->s_first_meta_bg) *
745 sbi->s_desc_per_block) {
748 num +=
le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
767 int flex_size = ext4_flex_bg_size(
EXT4_SB(inode->
i_sb));
781 block_group &= ~(flex_size-1);
785 bg_start = ext4_group_first_block_no(inode->
i_sb, block_group);
786 last_block = ext4_blocks_count(
EXT4_SB(inode->
i_sb)->s_es) - 1;
799 colour = (
current->pid % 16) * ((last_block - bg_start) / 16);
800 return bg_start + colour;