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