Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2 | * linux/fs/ext4/resize.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 4 | * Support for resizing an ext4 filesystem while it is mounted. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 2001, 2002 Andreas Dilger <adilger@clusterfs.com> |
| 7 | * |
| 8 | * This could probably be made into a module, because it is not often in use. |
| 9 | */ |
| 10 | |
| 11 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 12 | #define EXT4FS_DEBUG |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 13 | |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/smp_lock.h> |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 16 | #include <linux/ext4_jbd2.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 17 | |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/slab.h> |
| 20 | |
| 21 | |
| 22 | #define outside(b, first, last) ((b) < (first) || (b) >= (last)) |
| 23 | #define inside(b, first, last) ((b) >= (first) && (b) < (last)) |
| 24 | |
| 25 | static int verify_group_input(struct super_block *sb, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 26 | struct ext4_new_group_data *input) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 27 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 28 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 29 | struct ext4_super_block *es = sbi->s_es; |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 30 | ext4_fsblk_t start = ext4_blocks_count(es); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 31 | ext4_fsblk_t end = start + input->blocks_count; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 32 | unsigned group = input->group; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 33 | ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group; |
| 34 | unsigned overhead = ext4_bg_has_super(sb, group) ? |
| 35 | (1 + ext4_bg_num_gdb(sb, group) + |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 36 | le16_to_cpu(es->s_reserved_gdt_blocks)) : 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 37 | ext4_fsblk_t metaend = start + overhead; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 38 | struct buffer_head *bh = NULL; |
Mingming Cao | 3a5b2ec | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 39 | ext4_grpblk_t free_blocks_count, offset; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 40 | int err = -EINVAL; |
| 41 | |
| 42 | input->free_blocks_count = free_blocks_count = |
| 43 | input->blocks_count - 2 - overhead - sbi->s_itb_per_group; |
| 44 | |
| 45 | if (test_opt(sb, DEBUG)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 46 | printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks " |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 47 | "(%d free, %u reserved)\n", |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 48 | ext4_bg_has_super(sb, input->group) ? "normal" : |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 49 | "no-super", input->group, input->blocks_count, |
| 50 | free_blocks_count, input->reserved_blocks); |
| 51 | |
Mingming Cao | 3a5b2ec | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 52 | ext4_get_group_no_and_offset(sb, start, NULL, &offset); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 53 | if (group != sbi->s_groups_count) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 54 | ext4_warning(sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 55 | "Cannot add at group %u (only %lu groups)", |
| 56 | input->group, sbi->s_groups_count); |
Mingming Cao | 3a5b2ec | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 57 | else if (offset != 0) |
| 58 | ext4_warning(sb, __FUNCTION__, "Last group not full"); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 59 | else if (input->reserved_blocks > input->blocks_count / 5) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 60 | ext4_warning(sb, __FUNCTION__, "Reserved blocks too high (%u)", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 61 | input->reserved_blocks); |
| 62 | else if (free_blocks_count < 0) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 63 | ext4_warning(sb, __FUNCTION__, "Bad blocks count %u", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 64 | input->blocks_count); |
| 65 | else if (!(bh = sb_bread(sb, end - 1))) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 66 | ext4_warning(sb, __FUNCTION__, |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 67 | "Cannot read last block (%llu)", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 68 | end - 1); |
| 69 | else if (outside(input->block_bitmap, start, end)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 70 | ext4_warning(sb, __FUNCTION__, |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 71 | "Block bitmap not in group (block %llu)", |
Randy Dunlap | 1939e49 | 2006-10-28 10:38:26 -0700 | [diff] [blame] | 72 | (unsigned long long)input->block_bitmap); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 73 | else if (outside(input->inode_bitmap, start, end)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 74 | ext4_warning(sb, __FUNCTION__, |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 75 | "Inode bitmap not in group (block %llu)", |
Randy Dunlap | 1939e49 | 2006-10-28 10:38:26 -0700 | [diff] [blame] | 76 | (unsigned long long)input->inode_bitmap); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 77 | else if (outside(input->inode_table, start, end) || |
| 78 | outside(itend - 1, start, end)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 79 | ext4_warning(sb, __FUNCTION__, |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 80 | "Inode table not in group (blocks %llu-%llu)", |
Randy Dunlap | 1939e49 | 2006-10-28 10:38:26 -0700 | [diff] [blame] | 81 | (unsigned long long)input->inode_table, itend - 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 82 | else if (input->inode_bitmap == input->block_bitmap) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 83 | ext4_warning(sb, __FUNCTION__, |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 84 | "Block bitmap same as inode bitmap (%llu)", |
Randy Dunlap | 1939e49 | 2006-10-28 10:38:26 -0700 | [diff] [blame] | 85 | (unsigned long long)input->block_bitmap); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 86 | else if (inside(input->block_bitmap, input->inode_table, itend)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 87 | ext4_warning(sb, __FUNCTION__, |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 88 | "Block bitmap (%llu) in inode table (%llu-%llu)", |
Randy Dunlap | 1939e49 | 2006-10-28 10:38:26 -0700 | [diff] [blame] | 89 | (unsigned long long)input->block_bitmap, |
| 90 | (unsigned long long)input->inode_table, itend - 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 91 | else if (inside(input->inode_bitmap, input->inode_table, itend)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 92 | ext4_warning(sb, __FUNCTION__, |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 93 | "Inode bitmap (%llu) in inode table (%llu-%llu)", |
Randy Dunlap | 1939e49 | 2006-10-28 10:38:26 -0700 | [diff] [blame] | 94 | (unsigned long long)input->inode_bitmap, |
| 95 | (unsigned long long)input->inode_table, itend - 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 96 | else if (inside(input->block_bitmap, start, metaend)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 97 | ext4_warning(sb, __FUNCTION__, |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 98 | "Block bitmap (%llu) in GDT table" |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 99 | " (%llu-%llu)", |
Randy Dunlap | 1939e49 | 2006-10-28 10:38:26 -0700 | [diff] [blame] | 100 | (unsigned long long)input->block_bitmap, |
| 101 | start, metaend - 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 102 | else if (inside(input->inode_bitmap, start, metaend)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 103 | ext4_warning(sb, __FUNCTION__, |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 104 | "Inode bitmap (%llu) in GDT table" |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 105 | " (%llu-%llu)", |
Randy Dunlap | 1939e49 | 2006-10-28 10:38:26 -0700 | [diff] [blame] | 106 | (unsigned long long)input->inode_bitmap, |
| 107 | start, metaend - 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 108 | else if (inside(input->inode_table, start, metaend) || |
| 109 | inside(itend - 1, start, metaend)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 110 | ext4_warning(sb, __FUNCTION__, |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 111 | "Inode table (%llu-%llu) overlaps" |
| 112 | "GDT table (%llu-%llu)", |
Randy Dunlap | 1939e49 | 2006-10-28 10:38:26 -0700 | [diff] [blame] | 113 | (unsigned long long)input->inode_table, |
| 114 | itend - 1, start, metaend - 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 115 | else |
| 116 | err = 0; |
| 117 | brelse(bh); |
| 118 | |
| 119 | return err; |
| 120 | } |
| 121 | |
| 122 | static struct buffer_head *bclean(handle_t *handle, struct super_block *sb, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 123 | ext4_fsblk_t blk) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 124 | { |
| 125 | struct buffer_head *bh; |
| 126 | int err; |
| 127 | |
| 128 | bh = sb_getblk(sb, blk); |
| 129 | if (!bh) |
| 130 | return ERR_PTR(-EIO); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 131 | if ((err = ext4_journal_get_write_access(handle, bh))) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 132 | brelse(bh); |
| 133 | bh = ERR_PTR(err); |
| 134 | } else { |
| 135 | lock_buffer(bh); |
| 136 | memset(bh->b_data, 0, sb->s_blocksize); |
| 137 | set_buffer_uptodate(bh); |
| 138 | unlock_buffer(bh); |
| 139 | } |
| 140 | |
| 141 | return bh; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * To avoid calling the atomic setbit hundreds or thousands of times, we only |
| 146 | * need to use it within a single byte (to ensure we get endianness right). |
| 147 | * We can use memset for the rest of the bitmap as there are no other users. |
| 148 | */ |
| 149 | static void mark_bitmap_end(int start_bit, int end_bit, char *bitmap) |
| 150 | { |
| 151 | int i; |
| 152 | |
| 153 | if (start_bit >= end_bit) |
| 154 | return; |
| 155 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 156 | ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 157 | for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 158 | ext4_set_bit(i, bitmap); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 159 | if (i < end_bit) |
| 160 | memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Set up the block and inode bitmaps, and the inode table for the new group. |
| 165 | * This doesn't need to be part of the main transaction, since we are only |
| 166 | * changing blocks outside the actual filesystem. We still do journaling to |
| 167 | * ensure the recovery is correct in case of a failure just after resize. |
| 168 | * If any part of this fails, we simply abort the resize. |
| 169 | */ |
| 170 | static int setup_new_group_blocks(struct super_block *sb, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 171 | struct ext4_new_group_data *input) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 172 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 173 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 174 | ext4_fsblk_t start = ext4_group_first_block_no(sb, input->group); |
| 175 | int reserved_gdb = ext4_bg_has_super(sb, input->group) ? |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 176 | le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 177 | unsigned long gdblocks = ext4_bg_num_gdb(sb, input->group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 178 | struct buffer_head *bh; |
| 179 | handle_t *handle; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 180 | ext4_fsblk_t block; |
| 181 | ext4_grpblk_t bit; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 182 | int i; |
| 183 | int err = 0, err2; |
| 184 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 185 | handle = ext4_journal_start_sb(sb, reserved_gdb + gdblocks + |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 186 | 2 + sbi->s_itb_per_group); |
| 187 | if (IS_ERR(handle)) |
| 188 | return PTR_ERR(handle); |
| 189 | |
| 190 | lock_super(sb); |
| 191 | if (input->group != sbi->s_groups_count) { |
| 192 | err = -EBUSY; |
| 193 | goto exit_journal; |
| 194 | } |
| 195 | |
| 196 | if (IS_ERR(bh = bclean(handle, sb, input->block_bitmap))) { |
| 197 | err = PTR_ERR(bh); |
| 198 | goto exit_journal; |
| 199 | } |
| 200 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 201 | if (ext4_bg_has_super(sb, input->group)) { |
| 202 | ext4_debug("mark backup superblock %#04lx (+0)\n", start); |
| 203 | ext4_set_bit(0, bh->b_data); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* Copy all of the GDT blocks into the backup in this group */ |
| 207 | for (i = 0, bit = 1, block = start + 1; |
| 208 | i < gdblocks; i++, block++, bit++) { |
| 209 | struct buffer_head *gdb; |
| 210 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 211 | ext4_debug("update backup group %#04lx (+%d)\n", block, bit); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 212 | |
| 213 | gdb = sb_getblk(sb, block); |
| 214 | if (!gdb) { |
| 215 | err = -EIO; |
| 216 | goto exit_bh; |
| 217 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 218 | if ((err = ext4_journal_get_write_access(handle, gdb))) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 219 | brelse(gdb); |
| 220 | goto exit_bh; |
| 221 | } |
| 222 | lock_buffer(bh); |
| 223 | memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, bh->b_size); |
| 224 | set_buffer_uptodate(gdb); |
| 225 | unlock_buffer(bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 226 | ext4_journal_dirty_metadata(handle, gdb); |
| 227 | ext4_set_bit(bit, bh->b_data); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 228 | brelse(gdb); |
| 229 | } |
| 230 | |
| 231 | /* Zero out all of the reserved backup group descriptor table blocks */ |
| 232 | for (i = 0, bit = gdblocks + 1, block = start + bit; |
| 233 | i < reserved_gdb; i++, block++, bit++) { |
| 234 | struct buffer_head *gdb; |
| 235 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 236 | ext4_debug("clear reserved block %#04lx (+%d)\n", block, bit); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 237 | |
| 238 | if (IS_ERR(gdb = bclean(handle, sb, block))) { |
| 239 | err = PTR_ERR(bh); |
| 240 | goto exit_bh; |
| 241 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 242 | ext4_journal_dirty_metadata(handle, gdb); |
| 243 | ext4_set_bit(bit, bh->b_data); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 244 | brelse(gdb); |
| 245 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 246 | ext4_debug("mark block bitmap %#04x (+%ld)\n", input->block_bitmap, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 247 | input->block_bitmap - start); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 248 | ext4_set_bit(input->block_bitmap - start, bh->b_data); |
| 249 | ext4_debug("mark inode bitmap %#04x (+%ld)\n", input->inode_bitmap, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 250 | input->inode_bitmap - start); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 251 | ext4_set_bit(input->inode_bitmap - start, bh->b_data); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 252 | |
| 253 | /* Zero out all of the inode table blocks */ |
| 254 | for (i = 0, block = input->inode_table, bit = block - start; |
| 255 | i < sbi->s_itb_per_group; i++, bit++, block++) { |
| 256 | struct buffer_head *it; |
| 257 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 258 | ext4_debug("clear inode block %#04lx (+%d)\n", block, bit); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 259 | if (IS_ERR(it = bclean(handle, sb, block))) { |
| 260 | err = PTR_ERR(it); |
| 261 | goto exit_bh; |
| 262 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 263 | ext4_journal_dirty_metadata(handle, it); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 264 | brelse(it); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 265 | ext4_set_bit(bit, bh->b_data); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 266 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 267 | mark_bitmap_end(input->blocks_count, EXT4_BLOCKS_PER_GROUP(sb), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 268 | bh->b_data); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 269 | ext4_journal_dirty_metadata(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 270 | brelse(bh); |
| 271 | |
| 272 | /* Mark unused entries in inode bitmap used */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 273 | ext4_debug("clear inode bitmap %#04x (+%ld)\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 274 | input->inode_bitmap, input->inode_bitmap - start); |
| 275 | if (IS_ERR(bh = bclean(handle, sb, input->inode_bitmap))) { |
| 276 | err = PTR_ERR(bh); |
| 277 | goto exit_journal; |
| 278 | } |
| 279 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 280 | mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 281 | bh->b_data); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 282 | ext4_journal_dirty_metadata(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 283 | exit_bh: |
| 284 | brelse(bh); |
| 285 | |
| 286 | exit_journal: |
| 287 | unlock_super(sb); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 288 | if ((err2 = ext4_journal_stop(handle)) && !err) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 289 | err = err2; |
| 290 | |
| 291 | return err; |
| 292 | } |
| 293 | |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 294 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 295 | /* |
| 296 | * Iterate through the groups which hold BACKUP superblock/GDT copies in an |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 297 | * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 298 | * calling this for the first time. In a sparse filesystem it will be the |
| 299 | * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ... |
| 300 | * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ... |
| 301 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 302 | static unsigned ext4_list_backups(struct super_block *sb, unsigned *three, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 303 | unsigned *five, unsigned *seven) |
| 304 | { |
| 305 | unsigned *min = three; |
| 306 | int mult = 3; |
| 307 | unsigned ret; |
| 308 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 309 | if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 310 | EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 311 | ret = *min; |
| 312 | *min += 1; |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | if (*five < *min) { |
| 317 | min = five; |
| 318 | mult = 5; |
| 319 | } |
| 320 | if (*seven < *min) { |
| 321 | min = seven; |
| 322 | mult = 7; |
| 323 | } |
| 324 | |
| 325 | ret = *min; |
| 326 | *min *= mult; |
| 327 | |
| 328 | return ret; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Check that all of the backup GDT blocks are held in the primary GDT block. |
| 333 | * It is assumed that they are stored in group order. Returns the number of |
| 334 | * groups in current filesystem that have BACKUPS, or -ve error code. |
| 335 | */ |
| 336 | static int verify_reserved_gdb(struct super_block *sb, |
| 337 | struct buffer_head *primary) |
| 338 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 339 | const ext4_fsblk_t blk = primary->b_blocknr; |
| 340 | const unsigned long end = EXT4_SB(sb)->s_groups_count; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 341 | unsigned three = 1; |
| 342 | unsigned five = 5; |
| 343 | unsigned seven = 7; |
| 344 | unsigned grp; |
| 345 | __le32 *p = (__le32 *)primary->b_data; |
| 346 | int gdbackups = 0; |
| 347 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 348 | while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) { |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 349 | if (le32_to_cpu(*p++) != |
| 350 | grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 351 | ext4_warning(sb, __FUNCTION__, |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 352 | "reserved GDT %llu" |
| 353 | " missing grp %d (%llu)", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 354 | blk, grp, |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 355 | grp * |
| 356 | (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) + |
| 357 | blk); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 358 | return -EINVAL; |
| 359 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 360 | if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 361 | return -EFBIG; |
| 362 | } |
| 363 | |
| 364 | return gdbackups; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * Called when we need to bring a reserved group descriptor table block into |
| 369 | * use from the resize inode. The primary copy of the new GDT block currently |
| 370 | * is an indirect block (under the double indirect block in the resize inode). |
| 371 | * The new backup GDT blocks will be stored as leaf blocks in this indirect |
| 372 | * block, in group order. Even though we know all the block numbers we need, |
| 373 | * we check to ensure that the resize inode has actually reserved these blocks. |
| 374 | * |
| 375 | * Don't need to update the block bitmaps because the blocks are still in use. |
| 376 | * |
| 377 | * We get all of the error cases out of the way, so that we are sure to not |
| 378 | * fail once we start modifying the data on disk, because JBD has no rollback. |
| 379 | */ |
| 380 | static int add_new_gdb(handle_t *handle, struct inode *inode, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 381 | struct ext4_new_group_data *input, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 382 | struct buffer_head **primary) |
| 383 | { |
| 384 | struct super_block *sb = inode->i_sb; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 385 | struct ext4_super_block *es = EXT4_SB(sb)->s_es; |
| 386 | unsigned long gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb); |
| 387 | ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 388 | struct buffer_head **o_group_desc, **n_group_desc; |
| 389 | struct buffer_head *dind; |
| 390 | int gdbackups; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 391 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 392 | __le32 *data; |
| 393 | int err; |
| 394 | |
| 395 | if (test_opt(sb, DEBUG)) |
| 396 | printk(KERN_DEBUG |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 397 | "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 398 | gdb_num); |
| 399 | |
| 400 | /* |
| 401 | * If we are not using the primary superblock/GDT copy don't resize, |
| 402 | * because the user tools have no way of handling this. Probably a |
| 403 | * bad time to do it anyways. |
| 404 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 405 | if (EXT4_SB(sb)->s_sbh->b_blocknr != |
| 406 | le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) { |
| 407 | ext4_warning(sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 408 | "won't resize using backup superblock at %llu", |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 409 | (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 410 | return -EPERM; |
| 411 | } |
| 412 | |
| 413 | *primary = sb_bread(sb, gdblock); |
| 414 | if (!*primary) |
| 415 | return -EIO; |
| 416 | |
| 417 | if ((gdbackups = verify_reserved_gdb(sb, *primary)) < 0) { |
| 418 | err = gdbackups; |
| 419 | goto exit_bh; |
| 420 | } |
| 421 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 422 | data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 423 | dind = sb_bread(sb, le32_to_cpu(*data)); |
| 424 | if (!dind) { |
| 425 | err = -EIO; |
| 426 | goto exit_bh; |
| 427 | } |
| 428 | |
| 429 | data = (__le32 *)dind->b_data; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 430 | if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) { |
| 431 | ext4_warning(sb, __FUNCTION__, |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 432 | "new group %u GDT block %llu not reserved", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 433 | input->group, gdblock); |
| 434 | err = -EINVAL; |
| 435 | goto exit_dind; |
| 436 | } |
| 437 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 438 | if ((err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh))) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 439 | goto exit_dind; |
| 440 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 441 | if ((err = ext4_journal_get_write_access(handle, *primary))) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 442 | goto exit_sbh; |
| 443 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 444 | if ((err = ext4_journal_get_write_access(handle, dind))) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 445 | goto exit_primary; |
| 446 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 447 | /* ext4_reserve_inode_write() gets a reference on the iloc */ |
| 448 | if ((err = ext4_reserve_inode_write(handle, inode, &iloc))) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 449 | goto exit_dindj; |
| 450 | |
| 451 | n_group_desc = kmalloc((gdb_num + 1) * sizeof(struct buffer_head *), |
| 452 | GFP_KERNEL); |
| 453 | if (!n_group_desc) { |
| 454 | err = -ENOMEM; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 455 | ext4_warning (sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 456 | "not enough memory for %lu groups", gdb_num + 1); |
| 457 | goto exit_inode; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Finally, we have all of the possible failures behind us... |
| 462 | * |
| 463 | * Remove new GDT block from inode double-indirect block and clear out |
| 464 | * the new GDT block for use (which also "frees" the backup GDT blocks |
| 465 | * from the reserved inode). We don't need to change the bitmaps for |
| 466 | * these blocks, because they are marked as in-use from being in the |
| 467 | * reserved inode, and will become GDT blocks (primary and backup). |
| 468 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 469 | data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0; |
| 470 | ext4_journal_dirty_metadata(handle, dind); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 471 | brelse(dind); |
| 472 | inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 473 | ext4_mark_iloc_dirty(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 474 | memset((*primary)->b_data, 0, sb->s_blocksize); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 475 | ext4_journal_dirty_metadata(handle, *primary); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 476 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 477 | o_group_desc = EXT4_SB(sb)->s_group_desc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 478 | memcpy(n_group_desc, o_group_desc, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 479 | EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 480 | n_group_desc[gdb_num] = *primary; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 481 | EXT4_SB(sb)->s_group_desc = n_group_desc; |
| 482 | EXT4_SB(sb)->s_gdb_count++; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 483 | kfree(o_group_desc); |
| 484 | |
| 485 | es->s_reserved_gdt_blocks = |
| 486 | cpu_to_le16(le16_to_cpu(es->s_reserved_gdt_blocks) - 1); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 487 | ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 488 | |
| 489 | return 0; |
| 490 | |
| 491 | exit_inode: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 492 | //ext4_journal_release_buffer(handle, iloc.bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 493 | brelse(iloc.bh); |
| 494 | exit_dindj: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 495 | //ext4_journal_release_buffer(handle, dind); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 496 | exit_primary: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 497 | //ext4_journal_release_buffer(handle, *primary); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 498 | exit_sbh: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 499 | //ext4_journal_release_buffer(handle, *primary); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 500 | exit_dind: |
| 501 | brelse(dind); |
| 502 | exit_bh: |
| 503 | brelse(*primary); |
| 504 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 505 | ext4_debug("leaving with error %d\n", err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 506 | return err; |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Called when we are adding a new group which has a backup copy of each of |
| 511 | * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks. |
| 512 | * We need to add these reserved backup GDT blocks to the resize inode, so |
| 513 | * that they are kept for future resizing and not allocated to files. |
| 514 | * |
| 515 | * Each reserved backup GDT block will go into a different indirect block. |
| 516 | * The indirect blocks are actually the primary reserved GDT blocks, |
| 517 | * so we know in advance what their block numbers are. We only get the |
| 518 | * double-indirect block to verify it is pointing to the primary reserved |
| 519 | * GDT blocks so we don't overwrite a data block by accident. The reserved |
| 520 | * backup GDT blocks are stored in their reserved primary GDT block. |
| 521 | */ |
| 522 | static int reserve_backup_gdb(handle_t *handle, struct inode *inode, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 523 | struct ext4_new_group_data *input) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 524 | { |
| 525 | struct super_block *sb = inode->i_sb; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 526 | int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 527 | struct buffer_head **primary; |
| 528 | struct buffer_head *dind; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 529 | struct ext4_iloc iloc; |
| 530 | ext4_fsblk_t blk; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 531 | __le32 *data, *end; |
| 532 | int gdbackups = 0; |
| 533 | int res, i; |
| 534 | int err; |
| 535 | |
| 536 | primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_KERNEL); |
| 537 | if (!primary) |
| 538 | return -ENOMEM; |
| 539 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 540 | data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 541 | dind = sb_bread(sb, le32_to_cpu(*data)); |
| 542 | if (!dind) { |
| 543 | err = -EIO; |
| 544 | goto exit_free; |
| 545 | } |
| 546 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 547 | blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count; |
| 548 | data = (__le32 *)dind->b_data + EXT4_SB(sb)->s_gdb_count; |
| 549 | end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 550 | |
| 551 | /* Get each reserved primary GDT block and verify it holds backups */ |
| 552 | for (res = 0; res < reserved_gdb; res++, blk++) { |
| 553 | if (le32_to_cpu(*data) != blk) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 554 | ext4_warning(sb, __FUNCTION__, |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 555 | "reserved block %llu" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 556 | " not at offset %ld", |
| 557 | blk, |
| 558 | (long)(data - (__le32 *)dind->b_data)); |
| 559 | err = -EINVAL; |
| 560 | goto exit_bh; |
| 561 | } |
| 562 | primary[res] = sb_bread(sb, blk); |
| 563 | if (!primary[res]) { |
| 564 | err = -EIO; |
| 565 | goto exit_bh; |
| 566 | } |
| 567 | if ((gdbackups = verify_reserved_gdb(sb, primary[res])) < 0) { |
| 568 | brelse(primary[res]); |
| 569 | err = gdbackups; |
| 570 | goto exit_bh; |
| 571 | } |
| 572 | if (++data >= end) |
| 573 | data = (__le32 *)dind->b_data; |
| 574 | } |
| 575 | |
| 576 | for (i = 0; i < reserved_gdb; i++) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 577 | if ((err = ext4_journal_get_write_access(handle, primary[i]))) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 578 | /* |
| 579 | int j; |
| 580 | for (j = 0; j < i; j++) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 581 | ext4_journal_release_buffer(handle, primary[j]); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 582 | */ |
| 583 | goto exit_bh; |
| 584 | } |
| 585 | } |
| 586 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 587 | if ((err = ext4_reserve_inode_write(handle, inode, &iloc))) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 588 | goto exit_bh; |
| 589 | |
| 590 | /* |
| 591 | * Finally we can add each of the reserved backup GDT blocks from |
| 592 | * the new group to its reserved primary GDT block. |
| 593 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 594 | blk = input->group * EXT4_BLOCKS_PER_GROUP(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 595 | for (i = 0; i < reserved_gdb; i++) { |
| 596 | int err2; |
| 597 | data = (__le32 *)primary[i]->b_data; |
| 598 | /* printk("reserving backup %lu[%u] = %lu\n", |
| 599 | primary[i]->b_blocknr, gdbackups, |
| 600 | blk + primary[i]->b_blocknr); */ |
| 601 | data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 602 | err2 = ext4_journal_dirty_metadata(handle, primary[i]); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 603 | if (!err) |
| 604 | err = err2; |
| 605 | } |
| 606 | inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 607 | ext4_mark_iloc_dirty(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 608 | |
| 609 | exit_bh: |
| 610 | while (--res >= 0) |
| 611 | brelse(primary[res]); |
| 612 | brelse(dind); |
| 613 | |
| 614 | exit_free: |
| 615 | kfree(primary); |
| 616 | |
| 617 | return err; |
| 618 | } |
| 619 | |
| 620 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 621 | * Update the backup copies of the ext4 metadata. These don't need to be part |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 622 | * of the main resize transaction, because e2fsck will re-write them if there |
| 623 | * is a problem (basically only OOM will cause a problem). However, we |
| 624 | * _should_ update the backups if possible, in case the primary gets trashed |
| 625 | * for some reason and we need to run e2fsck from a backup superblock. The |
| 626 | * important part is that the new block and inode counts are in the backup |
| 627 | * superblocks, and the location of the new group metadata in the GDT backups. |
| 628 | * |
| 629 | * We do not need lock_super() for this, because these blocks are not |
| 630 | * otherwise touched by the filesystem code when it is mounted. We don't |
| 631 | * need to worry about last changing from sbi->s_groups_count, because the |
| 632 | * worst that can happen is that we do not copy the full number of backups |
| 633 | * at this time. The resize which changed s_groups_count will backup again. |
| 634 | */ |
| 635 | static void update_backups(struct super_block *sb, |
| 636 | int blk_off, char *data, int size) |
| 637 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 638 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 639 | const unsigned long last = sbi->s_groups_count; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 640 | const int bpg = EXT4_BLOCKS_PER_GROUP(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 641 | unsigned three = 1; |
| 642 | unsigned five = 5; |
| 643 | unsigned seven = 7; |
| 644 | unsigned group; |
| 645 | int rest = sb->s_blocksize - size; |
| 646 | handle_t *handle; |
| 647 | int err = 0, err2; |
| 648 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 649 | handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 650 | if (IS_ERR(handle)) { |
| 651 | group = 1; |
| 652 | err = PTR_ERR(handle); |
| 653 | goto exit_err; |
| 654 | } |
| 655 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 656 | while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 657 | struct buffer_head *bh; |
| 658 | |
| 659 | /* Out of journal space, and can't get more - abort - so sad */ |
| 660 | if (handle->h_buffer_credits == 0 && |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 661 | ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) && |
| 662 | (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA))) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 663 | break; |
| 664 | |
| 665 | bh = sb_getblk(sb, group * bpg + blk_off); |
| 666 | if (!bh) { |
| 667 | err = -EIO; |
| 668 | break; |
| 669 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 670 | ext4_debug("update metadata backup %#04lx\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 671 | (unsigned long)bh->b_blocknr); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 672 | if ((err = ext4_journal_get_write_access(handle, bh))) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 673 | break; |
| 674 | lock_buffer(bh); |
| 675 | memcpy(bh->b_data, data, size); |
| 676 | if (rest) |
| 677 | memset(bh->b_data + size, 0, rest); |
| 678 | set_buffer_uptodate(bh); |
| 679 | unlock_buffer(bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 680 | ext4_journal_dirty_metadata(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 681 | brelse(bh); |
| 682 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 683 | if ((err2 = ext4_journal_stop(handle)) && !err) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 684 | err = err2; |
| 685 | |
| 686 | /* |
| 687 | * Ugh! Need to have e2fsck write the backup copies. It is too |
| 688 | * late to revert the resize, we shouldn't fail just because of |
| 689 | * the backup copies (they are only needed in case of corruption). |
| 690 | * |
| 691 | * However, if we got here we have a journal problem too, so we |
| 692 | * can't really start a transaction to mark the superblock. |
| 693 | * Chicken out and just set the flag on the hope it will be written |
| 694 | * to disk, and if not - we will simply wait until next fsck. |
| 695 | */ |
| 696 | exit_err: |
| 697 | if (err) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 698 | ext4_warning(sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 699 | "can't update backup for group %d (err %d), " |
| 700 | "forcing fsck on next reboot", group, err); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 701 | sbi->s_mount_state &= ~EXT4_VALID_FS; |
| 702 | sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 703 | mark_buffer_dirty(sbi->s_sbh); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | /* Add group descriptor data to an existing or new group descriptor block. |
| 708 | * Ensure we handle all possible error conditions _before_ we start modifying |
| 709 | * the filesystem, because we cannot abort the transaction and not have it |
| 710 | * write the data to disk. |
| 711 | * |
| 712 | * If we are on a GDT block boundary, we need to get the reserved GDT block. |
| 713 | * Otherwise, we may need to add backup GDT blocks for a sparse group. |
| 714 | * |
| 715 | * We only need to hold the superblock lock while we are actually adding |
| 716 | * in the new group's counts to the superblock. Prior to that we have |
| 717 | * not really "added" the group at all. We re-check that we are still |
| 718 | * adding in the last group in case things have changed since verifying. |
| 719 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 720 | int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 721 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 722 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 723 | struct ext4_super_block *es = sbi->s_es; |
| 724 | int reserved_gdb = ext4_bg_has_super(sb, input->group) ? |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 725 | le16_to_cpu(es->s_reserved_gdt_blocks) : 0; |
| 726 | struct buffer_head *primary = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 727 | struct ext4_group_desc *gdp; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 728 | struct inode *inode = NULL; |
| 729 | handle_t *handle; |
| 730 | int gdb_off, gdb_num; |
| 731 | int err, err2; |
| 732 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 733 | gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb); |
| 734 | gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 735 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 736 | if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 737 | EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) { |
| 738 | ext4_warning(sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 739 | "Can't resize non-sparse filesystem further"); |
| 740 | return -EPERM; |
| 741 | } |
| 742 | |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 743 | if (ext4_blocks_count(es) + input->blocks_count < |
| 744 | ext4_blocks_count(es)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 745 | ext4_warning(sb, __FUNCTION__, "blocks_count overflow\n"); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 746 | return -EINVAL; |
| 747 | } |
| 748 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 749 | if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) < |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 750 | le32_to_cpu(es->s_inodes_count)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 751 | ext4_warning(sb, __FUNCTION__, "inodes_count overflow\n"); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 752 | return -EINVAL; |
| 753 | } |
| 754 | |
| 755 | if (reserved_gdb || gdb_off == 0) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 756 | if (!EXT4_HAS_COMPAT_FEATURE(sb, |
| 757 | EXT4_FEATURE_COMPAT_RESIZE_INODE)){ |
| 758 | ext4_warning(sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 759 | "No reserved GDT blocks, can't resize"); |
| 760 | return -EPERM; |
| 761 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 762 | inode = iget(sb, EXT4_RESIZE_INO); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 763 | if (!inode || is_bad_inode(inode)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 764 | ext4_warning(sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 765 | "Error opening resize inode"); |
| 766 | iput(inode); |
| 767 | return -ENOENT; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | if ((err = verify_group_input(sb, input))) |
| 772 | goto exit_put; |
| 773 | |
| 774 | if ((err = setup_new_group_blocks(sb, input))) |
| 775 | goto exit_put; |
| 776 | |
| 777 | /* |
| 778 | * We will always be modifying at least the superblock and a GDT |
| 779 | * block. If we are adding a group past the last current GDT block, |
| 780 | * we will also modify the inode and the dindirect block. If we |
| 781 | * are adding a group with superblock/GDT backups we will also |
| 782 | * modify each of the reserved GDT dindirect blocks. |
| 783 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 784 | handle = ext4_journal_start_sb(sb, |
| 785 | ext4_bg_has_super(sb, input->group) ? |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 786 | 3 + reserved_gdb : 4); |
| 787 | if (IS_ERR(handle)) { |
| 788 | err = PTR_ERR(handle); |
| 789 | goto exit_put; |
| 790 | } |
| 791 | |
| 792 | lock_super(sb); |
| 793 | if (input->group != sbi->s_groups_count) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 794 | ext4_warning(sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 795 | "multiple resizers run on filesystem!"); |
| 796 | err = -EBUSY; |
| 797 | goto exit_journal; |
| 798 | } |
| 799 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 800 | if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh))) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 801 | goto exit_journal; |
| 802 | |
| 803 | /* |
| 804 | * We will only either add reserved group blocks to a backup group |
| 805 | * or remove reserved blocks for the first group in a new group block. |
| 806 | * Doing both would be mean more complex code, and sane people don't |
| 807 | * use non-sparse filesystems anymore. This is already checked above. |
| 808 | */ |
| 809 | if (gdb_off) { |
| 810 | primary = sbi->s_group_desc[gdb_num]; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 811 | if ((err = ext4_journal_get_write_access(handle, primary))) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 812 | goto exit_journal; |
| 813 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 814 | if (reserved_gdb && ext4_bg_num_gdb(sb, input->group) && |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 815 | (err = reserve_backup_gdb(handle, inode, input))) |
| 816 | goto exit_journal; |
| 817 | } else if ((err = add_new_gdb(handle, inode, input, &primary))) |
| 818 | goto exit_journal; |
| 819 | |
| 820 | /* |
| 821 | * OK, now we've set up the new group. Time to make it active. |
| 822 | * |
| 823 | * Current kernels don't lock all allocations via lock_super(), |
| 824 | * so we have to be safe wrt. concurrent accesses the group |
| 825 | * data. So we need to be careful to set all of the relevant |
| 826 | * group descriptor data etc. *before* we enable the group. |
| 827 | * |
| 828 | * The key field here is sbi->s_groups_count: as long as |
| 829 | * that retains its old value, nobody is going to access the new |
| 830 | * group. |
| 831 | * |
| 832 | * So first we update all the descriptor metadata for the new |
| 833 | * group; then we update the total disk blocks count; then we |
| 834 | * update the groups count to enable the group; then finally we |
| 835 | * update the free space counts so that the system can start |
| 836 | * using the new disk blocks. |
| 837 | */ |
| 838 | |
| 839 | /* Update group descriptor block for new group */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 840 | gdp = (struct ext4_group_desc *)primary->b_data + gdb_off; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 841 | |
Alexandre Ratchov | 8fadc14 | 2006-10-11 01:21:15 -0700 | [diff] [blame] | 842 | ext4_block_bitmap_set(sb, gdp, input->block_bitmap); /* LV FIXME */ |
| 843 | ext4_inode_bitmap_set(sb, gdp, input->inode_bitmap); /* LV FIXME */ |
| 844 | ext4_inode_table_set(sb, gdp, input->inode_table); /* LV FIXME */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 845 | gdp->bg_free_blocks_count = cpu_to_le16(input->free_blocks_count); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 846 | gdp->bg_free_inodes_count = cpu_to_le16(EXT4_INODES_PER_GROUP(sb)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 847 | |
| 848 | /* |
| 849 | * Make the new blocks and inodes valid next. We do this before |
| 850 | * increasing the group count so that once the group is enabled, |
| 851 | * all of its blocks and inodes are already valid. |
| 852 | * |
| 853 | * We always allocate group-by-group, then block-by-block or |
| 854 | * inode-by-inode within a group, so enabling these |
| 855 | * blocks/inodes before the group is live won't actually let us |
| 856 | * allocate the new space yet. |
| 857 | */ |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 858 | ext4_blocks_count_set(es, ext4_blocks_count(es) + |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 859 | input->blocks_count); |
| 860 | es->s_inodes_count = cpu_to_le32(le32_to_cpu(es->s_inodes_count) + |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 861 | EXT4_INODES_PER_GROUP(sb)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 862 | |
| 863 | /* |
| 864 | * We need to protect s_groups_count against other CPUs seeing |
| 865 | * inconsistent state in the superblock. |
| 866 | * |
| 867 | * The precise rules we use are: |
| 868 | * |
| 869 | * * Writers of s_groups_count *must* hold lock_super |
| 870 | * AND |
| 871 | * * Writers must perform a smp_wmb() after updating all dependent |
| 872 | * data and before modifying the groups count |
| 873 | * |
| 874 | * * Readers must hold lock_super() over the access |
| 875 | * OR |
| 876 | * * Readers must perform an smp_rmb() after reading the groups count |
| 877 | * and before reading any dependent data. |
| 878 | * |
| 879 | * NB. These rules can be relaxed when checking the group count |
| 880 | * while freeing data, as we can only allocate from a block |
| 881 | * group after serialising against the group count, and we can |
| 882 | * only then free after serialising in turn against that |
| 883 | * allocation. |
| 884 | */ |
| 885 | smp_wmb(); |
| 886 | |
| 887 | /* Update the global fs size fields */ |
| 888 | sbi->s_groups_count++; |
| 889 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 890 | ext4_journal_dirty_metadata(handle, primary); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 891 | |
| 892 | /* Update the reserved block counts only once the new group is |
| 893 | * active. */ |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 894 | ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) + |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 895 | input->reserved_blocks); |
| 896 | |
| 897 | /* Update the free space counts */ |
| 898 | percpu_counter_mod(&sbi->s_freeblocks_counter, |
| 899 | input->free_blocks_count); |
| 900 | percpu_counter_mod(&sbi->s_freeinodes_counter, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 901 | EXT4_INODES_PER_GROUP(sb)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 902 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 903 | ext4_journal_dirty_metadata(handle, sbi->s_sbh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 904 | sb->s_dirt = 1; |
| 905 | |
| 906 | exit_journal: |
| 907 | unlock_super(sb); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 908 | if ((err2 = ext4_journal_stop(handle)) && !err) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 909 | err = err2; |
| 910 | if (!err) { |
| 911 | update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 912 | sizeof(struct ext4_super_block)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 913 | update_backups(sb, primary->b_blocknr, primary->b_data, |
| 914 | primary->b_size); |
| 915 | } |
| 916 | exit_put: |
| 917 | iput(inode); |
| 918 | return err; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 919 | } /* ext4_group_add */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 920 | |
| 921 | /* Extend the filesystem to the new number of blocks specified. This entry |
| 922 | * point is only used to extend the current filesystem to the end of the last |
| 923 | * existing group. It can be accessed via ioctl, or by "remount,resize=<size>" |
| 924 | * for emergencies (because it has no dependencies on reserved blocks). |
| 925 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 926 | * If we _really_ wanted, we could use default values to call ext4_group_add() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 927 | * allow the "remount" trick to work for arbitrary resizing, assuming enough |
| 928 | * GDT blocks are reserved to grow to the desired size. |
| 929 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 930 | int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es, |
| 931 | ext4_fsblk_t n_blocks_count) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 932 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 933 | ext4_fsblk_t o_blocks_count; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 934 | unsigned long o_groups_count; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 935 | ext4_grpblk_t last; |
| 936 | ext4_grpblk_t add; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 937 | struct buffer_head * bh; |
| 938 | handle_t *handle; |
| 939 | int err; |
| 940 | unsigned long freed_blocks; |
| 941 | |
| 942 | /* We don't need to worry about locking wrt other resizers just |
| 943 | * yet: we're going to revalidate es->s_blocks_count after |
| 944 | * taking lock_super() below. */ |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 945 | o_blocks_count = ext4_blocks_count(es); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 946 | o_groups_count = EXT4_SB(sb)->s_groups_count; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 947 | |
| 948 | if (test_opt(sb, DEBUG)) |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 949 | printk(KERN_DEBUG "EXT4-fs: extending last group from %llu uto %llu blocks\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 950 | o_blocks_count, n_blocks_count); |
| 951 | |
| 952 | if (n_blocks_count == 0 || n_blocks_count == o_blocks_count) |
| 953 | return 0; |
| 954 | |
| 955 | if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 956 | printk(KERN_ERR "EXT4-fs: filesystem on %s:" |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 957 | " too large to resize to %llu blocks safely\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 958 | sb->s_id, n_blocks_count); |
| 959 | if (sizeof(sector_t) < 8) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 960 | ext4_warning(sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 961 | "CONFIG_LBD not enabled\n"); |
| 962 | return -EINVAL; |
| 963 | } |
| 964 | |
| 965 | if (n_blocks_count < o_blocks_count) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 966 | ext4_warning(sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 967 | "can't shrink FS - resize aborted"); |
| 968 | return -EBUSY; |
| 969 | } |
| 970 | |
| 971 | /* Handle the remaining blocks in the last group only. */ |
Mingming Cao | 3a5b2ec | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 972 | ext4_get_group_no_and_offset(sb, o_blocks_count, NULL, &last); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 973 | |
| 974 | if (last == 0) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 975 | ext4_warning(sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 976 | "need to use ext2online to resize further"); |
| 977 | return -EPERM; |
| 978 | } |
| 979 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 980 | add = EXT4_BLOCKS_PER_GROUP(sb) - last; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 981 | |
| 982 | if (o_blocks_count + add < o_blocks_count) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 983 | ext4_warning(sb, __FUNCTION__, "blocks_count overflow"); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 984 | return -EINVAL; |
| 985 | } |
| 986 | |
| 987 | if (o_blocks_count + add > n_blocks_count) |
| 988 | add = n_blocks_count - o_blocks_count; |
| 989 | |
| 990 | if (o_blocks_count + add < n_blocks_count) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 991 | ext4_warning(sb, __FUNCTION__, |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 992 | "will only finish group (%llu" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 993 | " blocks, %u new)", |
| 994 | o_blocks_count + add, add); |
| 995 | |
| 996 | /* See if the device is actually as big as what was requested */ |
| 997 | bh = sb_bread(sb, o_blocks_count + add -1); |
| 998 | if (!bh) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 999 | ext4_warning(sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1000 | "can't read last block, resize aborted"); |
| 1001 | return -ENOSPC; |
| 1002 | } |
| 1003 | brelse(bh); |
| 1004 | |
| 1005 | /* We will update the superblock, one block bitmap, and |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1006 | * one group descriptor via ext4_free_blocks(). |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1007 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1008 | handle = ext4_journal_start_sb(sb, 3); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1009 | if (IS_ERR(handle)) { |
| 1010 | err = PTR_ERR(handle); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1011 | ext4_warning(sb, __FUNCTION__, "error %d on journal start",err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1012 | goto exit_put; |
| 1013 | } |
| 1014 | |
| 1015 | lock_super(sb); |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 1016 | if (o_blocks_count != ext4_blocks_count(es)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1017 | ext4_warning(sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1018 | "multiple resizers run on filesystem!"); |
| 1019 | unlock_super(sb); |
| 1020 | err = -EBUSY; |
| 1021 | goto exit_put; |
| 1022 | } |
| 1023 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1024 | if ((err = ext4_journal_get_write_access(handle, |
| 1025 | EXT4_SB(sb)->s_sbh))) { |
| 1026 | ext4_warning(sb, __FUNCTION__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1027 | "error %d on journal write access", err); |
| 1028 | unlock_super(sb); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1029 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1030 | goto exit_put; |
| 1031 | } |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 1032 | ext4_blocks_count_set(es, o_blocks_count + add); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1033 | ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1034 | sb->s_dirt = 1; |
| 1035 | unlock_super(sb); |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1036 | ext4_debug("freeing blocks %lu through %llu\n", o_blocks_count, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1037 | o_blocks_count + add); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1038 | ext4_free_blocks_sb(handle, sb, o_blocks_count, add, &freed_blocks); |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1039 | ext4_debug("freed blocks %llu through %llu\n", o_blocks_count, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1040 | o_blocks_count + add); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1041 | if ((err = ext4_journal_stop(handle))) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1042 | goto exit_put; |
| 1043 | if (test_opt(sb, DEBUG)) |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 1044 | printk(KERN_DEBUG "EXT4-fs: extended group to %llu blocks\n", |
| 1045 | ext4_blocks_count(es)); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1046 | update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es, |
| 1047 | sizeof(struct ext4_super_block)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1048 | exit_put: |
| 1049 | return err; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1050 | } /* ext4_group_extend */ |