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