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