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