blob: 4fe49c3661b286990b76b2bc939325804565f9b6 [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/resize.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
Mingming Cao617ba132006-10-11 01:20:53 -07004 * Support for resizing an ext4 filesystem while it is mounted.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005 *
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 Cao617ba132006-10-11 01:20:53 -070012#define EXT4FS_DEBUG
Dave Kleikampac27a0e2006-10-11 01:20:50 -070013
14#include <linux/sched.h>
15#include <linux/smp_lock.h>
Mingming Caodab291a2006-10-11 01:21:01 -070016#include <linux/ext4_jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070017
18#include <linux/errno.h>
19#include <linux/slab.h>
20
21
22#define outside(b, first, last) ((b) < (first) || (b) >= (last))
23#define inside(b, first, last) ((b) >= (first) && (b) < (last))
24
25static int verify_group_input(struct super_block *sb,
Mingming Cao617ba132006-10-11 01:20:53 -070026 struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070027{
Mingming Cao617ba132006-10-11 01:20:53 -070028 struct ext4_sb_info *sbi = EXT4_SB(sb);
29 struct ext4_super_block *es = sbi->s_es;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070030 ext4_fsblk_t start = ext4_blocks_count(es);
Mingming Cao617ba132006-10-11 01:20:53 -070031 ext4_fsblk_t end = start + input->blocks_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070032 unsigned group = input->group;
Mingming Cao617ba132006-10-11 01:20:53 -070033 ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group;
34 unsigned overhead = ext4_bg_has_super(sb, group) ?
35 (1 + ext4_bg_num_gdb(sb, group) +
Dave Kleikampac27a0e2006-10-11 01:20:50 -070036 le16_to_cpu(es->s_reserved_gdt_blocks)) : 0;
Mingming Cao617ba132006-10-11 01:20:53 -070037 ext4_fsblk_t metaend = start + overhead;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070038 struct buffer_head *bh = NULL;
Mingming Cao3a5b2ec2006-10-11 01:21:05 -070039 ext4_grpblk_t free_blocks_count, offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070040 int err = -EINVAL;
41
42 input->free_blocks_count = free_blocks_count =
43 input->blocks_count - 2 - overhead - sbi->s_itb_per_group;
44
45 if (test_opt(sb, DEBUG))
Mingming Cao617ba132006-10-11 01:20:53 -070046 printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks "
Dave Kleikampac27a0e2006-10-11 01:20:50 -070047 "(%d free, %u reserved)\n",
Mingming Cao617ba132006-10-11 01:20:53 -070048 ext4_bg_has_super(sb, input->group) ? "normal" :
Dave Kleikampac27a0e2006-10-11 01:20:50 -070049 "no-super", input->group, input->blocks_count,
50 free_blocks_count, input->reserved_blocks);
51
Mingming Cao3a5b2ec2006-10-11 01:21:05 -070052 ext4_get_group_no_and_offset(sb, start, NULL, &offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070053 if (group != sbi->s_groups_count)
Mingming Cao617ba132006-10-11 01:20:53 -070054 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070055 "Cannot add at group %u (only %lu groups)",
56 input->group, sbi->s_groups_count);
Mingming Cao3a5b2ec2006-10-11 01:21:05 -070057 else if (offset != 0)
58 ext4_warning(sb, __FUNCTION__, "Last group not full");
Dave Kleikampac27a0e2006-10-11 01:20:50 -070059 else if (input->reserved_blocks > input->blocks_count / 5)
Mingming Cao617ba132006-10-11 01:20:53 -070060 ext4_warning(sb, __FUNCTION__, "Reserved blocks too high (%u)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -070061 input->reserved_blocks);
62 else if (free_blocks_count < 0)
Mingming Cao617ba132006-10-11 01:20:53 -070063 ext4_warning(sb, __FUNCTION__, "Bad blocks count %u",
Dave Kleikampac27a0e2006-10-11 01:20:50 -070064 input->blocks_count);
65 else if (!(bh = sb_bread(sb, end - 1)))
Mingming Cao617ba132006-10-11 01:20:53 -070066 ext4_warning(sb, __FUNCTION__,
Mingming Cao2ae02102006-10-11 01:21:11 -070067 "Cannot read last block (%llu)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -070068 end - 1);
69 else if (outside(input->block_bitmap, start, end))
Mingming Cao617ba132006-10-11 01:20:53 -070070 ext4_warning(sb, __FUNCTION__,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070071 "Block bitmap not in group (block %llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -070072 (unsigned long long)input->block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070073 else if (outside(input->inode_bitmap, start, end))
Mingming Cao617ba132006-10-11 01:20:53 -070074 ext4_warning(sb, __FUNCTION__,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070075 "Inode bitmap not in group (block %llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -070076 (unsigned long long)input->inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070077 else if (outside(input->inode_table, start, end) ||
78 outside(itend - 1, start, end))
Mingming Cao617ba132006-10-11 01:20:53 -070079 ext4_warning(sb, __FUNCTION__,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070080 "Inode table not in group (blocks %llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -070081 (unsigned long long)input->inode_table, itend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070082 else if (input->inode_bitmap == input->block_bitmap)
Mingming Cao617ba132006-10-11 01:20:53 -070083 ext4_warning(sb, __FUNCTION__,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070084 "Block bitmap same as inode bitmap (%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -070085 (unsigned long long)input->block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070086 else if (inside(input->block_bitmap, input->inode_table, itend))
Mingming Cao617ba132006-10-11 01:20:53 -070087 ext4_warning(sb, __FUNCTION__,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070088 "Block bitmap (%llu) in inode table (%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -070089 (unsigned long long)input->block_bitmap,
90 (unsigned long long)input->inode_table, itend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070091 else if (inside(input->inode_bitmap, input->inode_table, itend))
Mingming Cao617ba132006-10-11 01:20:53 -070092 ext4_warning(sb, __FUNCTION__,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070093 "Inode bitmap (%llu) in inode table (%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -070094 (unsigned long long)input->inode_bitmap,
95 (unsigned long long)input->inode_table, itend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070096 else if (inside(input->block_bitmap, start, metaend))
Mingming Cao617ba132006-10-11 01:20:53 -070097 ext4_warning(sb, __FUNCTION__,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070098 "Block bitmap (%llu) in GDT table"
Mingming Cao2ae02102006-10-11 01:21:11 -070099 " (%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700100 (unsigned long long)input->block_bitmap,
101 start, metaend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700102 else if (inside(input->inode_bitmap, start, metaend))
Mingming Cao617ba132006-10-11 01:20:53 -0700103 ext4_warning(sb, __FUNCTION__,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700104 "Inode bitmap (%llu) in GDT table"
Mingming Cao2ae02102006-10-11 01:21:11 -0700105 " (%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700106 (unsigned long long)input->inode_bitmap,
107 start, metaend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700108 else if (inside(input->inode_table, start, metaend) ||
109 inside(itend - 1, start, metaend))
Mingming Cao617ba132006-10-11 01:20:53 -0700110 ext4_warning(sb, __FUNCTION__,
Mingming Cao2ae02102006-10-11 01:21:11 -0700111 "Inode table (%llu-%llu) overlaps"
112 "GDT table (%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700113 (unsigned long long)input->inode_table,
114 itend - 1, start, metaend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700115 else
116 err = 0;
117 brelse(bh);
118
119 return err;
120}
121
122static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
Mingming Cao617ba132006-10-11 01:20:53 -0700123 ext4_fsblk_t blk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700124{
125 struct buffer_head *bh;
126 int err;
127
128 bh = sb_getblk(sb, blk);
129 if (!bh)
130 return ERR_PTR(-EIO);
Mingming Cao617ba132006-10-11 01:20:53 -0700131 if ((err = ext4_journal_get_write_access(handle, bh))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700132 brelse(bh);
133 bh = ERR_PTR(err);
134 } else {
135 lock_buffer(bh);
136 memset(bh->b_data, 0, sb->s_blocksize);
137 set_buffer_uptodate(bh);
138 unlock_buffer(bh);
139 }
140
141 return bh;
142}
143
144/*
145 * To avoid calling the atomic setbit hundreds or thousands of times, we only
146 * need to use it within a single byte (to ensure we get endianness right).
147 * We can use memset for the rest of the bitmap as there are no other users.
148 */
149static void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
150{
151 int i;
152
153 if (start_bit >= end_bit)
154 return;
155
Mingming Cao617ba132006-10-11 01:20:53 -0700156 ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700157 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
Mingming Cao617ba132006-10-11 01:20:53 -0700158 ext4_set_bit(i, bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700159 if (i < end_bit)
160 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
161}
162
163/*
164 * Set up the block and inode bitmaps, and the inode table for the new group.
165 * This doesn't need to be part of the main transaction, since we are only
166 * changing blocks outside the actual filesystem. We still do journaling to
167 * ensure the recovery is correct in case of a failure just after resize.
168 * If any part of this fails, we simply abort the resize.
169 */
170static int setup_new_group_blocks(struct super_block *sb,
Mingming Cao617ba132006-10-11 01:20:53 -0700171 struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700172{
Mingming Cao617ba132006-10-11 01:20:53 -0700173 struct ext4_sb_info *sbi = EXT4_SB(sb);
174 ext4_fsblk_t start = ext4_group_first_block_no(sb, input->group);
175 int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700176 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700177 unsigned long gdblocks = ext4_bg_num_gdb(sb, input->group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700178 struct buffer_head *bh;
179 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -0700180 ext4_fsblk_t block;
181 ext4_grpblk_t bit;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700182 int i;
183 int err = 0, err2;
184
Mingming Cao617ba132006-10-11 01:20:53 -0700185 handle = ext4_journal_start_sb(sb, reserved_gdb + gdblocks +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700186 2 + sbi->s_itb_per_group);
187 if (IS_ERR(handle))
188 return PTR_ERR(handle);
189
190 lock_super(sb);
191 if (input->group != sbi->s_groups_count) {
192 err = -EBUSY;
193 goto exit_journal;
194 }
195
196 if (IS_ERR(bh = bclean(handle, sb, input->block_bitmap))) {
197 err = PTR_ERR(bh);
198 goto exit_journal;
199 }
200
Mingming Cao617ba132006-10-11 01:20:53 -0700201 if (ext4_bg_has_super(sb, input->group)) {
202 ext4_debug("mark backup superblock %#04lx (+0)\n", start);
203 ext4_set_bit(0, bh->b_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700204 }
205
206 /* Copy all of the GDT blocks into the backup in this group */
207 for (i = 0, bit = 1, block = start + 1;
208 i < gdblocks; i++, block++, bit++) {
209 struct buffer_head *gdb;
210
Mingming Cao617ba132006-10-11 01:20:53 -0700211 ext4_debug("update backup group %#04lx (+%d)\n", block, bit);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700212
213 gdb = sb_getblk(sb, block);
214 if (!gdb) {
215 err = -EIO;
216 goto exit_bh;
217 }
Mingming Cao617ba132006-10-11 01:20:53 -0700218 if ((err = ext4_journal_get_write_access(handle, gdb))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700219 brelse(gdb);
220 goto exit_bh;
221 }
222 lock_buffer(bh);
223 memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, bh->b_size);
224 set_buffer_uptodate(gdb);
225 unlock_buffer(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700226 ext4_journal_dirty_metadata(handle, gdb);
227 ext4_set_bit(bit, bh->b_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700228 brelse(gdb);
229 }
230
231 /* Zero out all of the reserved backup group descriptor table blocks */
232 for (i = 0, bit = gdblocks + 1, block = start + bit;
233 i < reserved_gdb; i++, block++, bit++) {
234 struct buffer_head *gdb;
235
Mingming Cao617ba132006-10-11 01:20:53 -0700236 ext4_debug("clear reserved block %#04lx (+%d)\n", block, bit);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700237
238 if (IS_ERR(gdb = bclean(handle, sb, block))) {
239 err = PTR_ERR(bh);
240 goto exit_bh;
241 }
Mingming Cao617ba132006-10-11 01:20:53 -0700242 ext4_journal_dirty_metadata(handle, gdb);
243 ext4_set_bit(bit, bh->b_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700244 brelse(gdb);
245 }
Mingming Cao617ba132006-10-11 01:20:53 -0700246 ext4_debug("mark block bitmap %#04x (+%ld)\n", input->block_bitmap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700247 input->block_bitmap - start);
Mingming Cao617ba132006-10-11 01:20:53 -0700248 ext4_set_bit(input->block_bitmap - start, bh->b_data);
249 ext4_debug("mark inode bitmap %#04x (+%ld)\n", input->inode_bitmap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700250 input->inode_bitmap - start);
Mingming Cao617ba132006-10-11 01:20:53 -0700251 ext4_set_bit(input->inode_bitmap - start, bh->b_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700252
253 /* Zero out all of the inode table blocks */
254 for (i = 0, block = input->inode_table, bit = block - start;
255 i < sbi->s_itb_per_group; i++, bit++, block++) {
256 struct buffer_head *it;
257
Mingming Cao617ba132006-10-11 01:20:53 -0700258 ext4_debug("clear inode block %#04lx (+%d)\n", block, bit);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700259 if (IS_ERR(it = bclean(handle, sb, block))) {
260 err = PTR_ERR(it);
261 goto exit_bh;
262 }
Mingming Cao617ba132006-10-11 01:20:53 -0700263 ext4_journal_dirty_metadata(handle, it);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700264 brelse(it);
Mingming Cao617ba132006-10-11 01:20:53 -0700265 ext4_set_bit(bit, bh->b_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266 }
Mingming Cao617ba132006-10-11 01:20:53 -0700267 mark_bitmap_end(input->blocks_count, EXT4_BLOCKS_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700268 bh->b_data);
Mingming Cao617ba132006-10-11 01:20:53 -0700269 ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700270 brelse(bh);
271
272 /* Mark unused entries in inode bitmap used */
Mingming Cao617ba132006-10-11 01:20:53 -0700273 ext4_debug("clear inode bitmap %#04x (+%ld)\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700274 input->inode_bitmap, input->inode_bitmap - start);
275 if (IS_ERR(bh = bclean(handle, sb, input->inode_bitmap))) {
276 err = PTR_ERR(bh);
277 goto exit_journal;
278 }
279
Mingming Cao617ba132006-10-11 01:20:53 -0700280 mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700281 bh->b_data);
Mingming Cao617ba132006-10-11 01:20:53 -0700282 ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700283exit_bh:
284 brelse(bh);
285
286exit_journal:
287 unlock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700288 if ((err2 = ext4_journal_stop(handle)) && !err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700289 err = err2;
290
291 return err;
292}
293
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700294
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700295/*
296 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
Mingming Cao617ba132006-10-11 01:20:53 -0700297 * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700298 * calling this for the first time. In a sparse filesystem it will be the
299 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
300 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
301 */
Mingming Cao617ba132006-10-11 01:20:53 -0700302static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700303 unsigned *five, unsigned *seven)
304{
305 unsigned *min = three;
306 int mult = 3;
307 unsigned ret;
308
Mingming Cao617ba132006-10-11 01:20:53 -0700309 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
310 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700311 ret = *min;
312 *min += 1;
313 return ret;
314 }
315
316 if (*five < *min) {
317 min = five;
318 mult = 5;
319 }
320 if (*seven < *min) {
321 min = seven;
322 mult = 7;
323 }
324
325 ret = *min;
326 *min *= mult;
327
328 return ret;
329}
330
331/*
332 * Check that all of the backup GDT blocks are held in the primary GDT block.
333 * It is assumed that they are stored in group order. Returns the number of
334 * groups in current filesystem that have BACKUPS, or -ve error code.
335 */
336static int verify_reserved_gdb(struct super_block *sb,
337 struct buffer_head *primary)
338{
Mingming Cao617ba132006-10-11 01:20:53 -0700339 const ext4_fsblk_t blk = primary->b_blocknr;
340 const unsigned long end = EXT4_SB(sb)->s_groups_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700341 unsigned three = 1;
342 unsigned five = 5;
343 unsigned seven = 7;
344 unsigned grp;
345 __le32 *p = (__le32 *)primary->b_data;
346 int gdbackups = 0;
347
Mingming Cao617ba132006-10-11 01:20:53 -0700348 while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700349 if (le32_to_cpu(*p++) !=
350 grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
Mingming Cao617ba132006-10-11 01:20:53 -0700351 ext4_warning(sb, __FUNCTION__,
Mingming Cao2ae02102006-10-11 01:21:11 -0700352 "reserved GDT %llu"
353 " missing grp %d (%llu)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700354 blk, grp,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700355 grp *
356 (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
357 blk);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700358 return -EINVAL;
359 }
Mingming Cao617ba132006-10-11 01:20:53 -0700360 if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700361 return -EFBIG;
362 }
363
364 return gdbackups;
365}
366
367/*
368 * Called when we need to bring a reserved group descriptor table block into
369 * use from the resize inode. The primary copy of the new GDT block currently
370 * is an indirect block (under the double indirect block in the resize inode).
371 * The new backup GDT blocks will be stored as leaf blocks in this indirect
372 * block, in group order. Even though we know all the block numbers we need,
373 * we check to ensure that the resize inode has actually reserved these blocks.
374 *
375 * Don't need to update the block bitmaps because the blocks are still in use.
376 *
377 * We get all of the error cases out of the way, so that we are sure to not
378 * fail once we start modifying the data on disk, because JBD has no rollback.
379 */
380static int add_new_gdb(handle_t *handle, struct inode *inode,
Mingming Cao617ba132006-10-11 01:20:53 -0700381 struct ext4_new_group_data *input,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700382 struct buffer_head **primary)
383{
384 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -0700385 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
386 unsigned long gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
387 ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700388 struct buffer_head **o_group_desc, **n_group_desc;
389 struct buffer_head *dind;
390 int gdbackups;
Mingming Cao617ba132006-10-11 01:20:53 -0700391 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700392 __le32 *data;
393 int err;
394
395 if (test_opt(sb, DEBUG))
396 printk(KERN_DEBUG
Mingming Cao617ba132006-10-11 01:20:53 -0700397 "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700398 gdb_num);
399
400 /*
401 * If we are not using the primary superblock/GDT copy don't resize,
402 * because the user tools have no way of handling this. Probably a
403 * bad time to do it anyways.
404 */
Mingming Cao617ba132006-10-11 01:20:53 -0700405 if (EXT4_SB(sb)->s_sbh->b_blocknr !=
406 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
407 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700408 "won't resize using backup superblock at %llu",
Mingming Cao617ba132006-10-11 01:20:53 -0700409 (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700410 return -EPERM;
411 }
412
413 *primary = sb_bread(sb, gdblock);
414 if (!*primary)
415 return -EIO;
416
417 if ((gdbackups = verify_reserved_gdb(sb, *primary)) < 0) {
418 err = gdbackups;
419 goto exit_bh;
420 }
421
Mingming Cao617ba132006-10-11 01:20:53 -0700422 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700423 dind = sb_bread(sb, le32_to_cpu(*data));
424 if (!dind) {
425 err = -EIO;
426 goto exit_bh;
427 }
428
429 data = (__le32 *)dind->b_data;
Mingming Cao617ba132006-10-11 01:20:53 -0700430 if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
431 ext4_warning(sb, __FUNCTION__,
Mingming Cao2ae02102006-10-11 01:21:11 -0700432 "new group %u GDT block %llu not reserved",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700433 input->group, gdblock);
434 err = -EINVAL;
435 goto exit_dind;
436 }
437
Mingming Cao617ba132006-10-11 01:20:53 -0700438 if ((err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700439 goto exit_dind;
440
Mingming Cao617ba132006-10-11 01:20:53 -0700441 if ((err = ext4_journal_get_write_access(handle, *primary)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700442 goto exit_sbh;
443
Mingming Cao617ba132006-10-11 01:20:53 -0700444 if ((err = ext4_journal_get_write_access(handle, dind)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700445 goto exit_primary;
446
Mingming Cao617ba132006-10-11 01:20:53 -0700447 /* ext4_reserve_inode_write() gets a reference on the iloc */
448 if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700449 goto exit_dindj;
450
451 n_group_desc = kmalloc((gdb_num + 1) * sizeof(struct buffer_head *),
452 GFP_KERNEL);
453 if (!n_group_desc) {
454 err = -ENOMEM;
Mingming Cao617ba132006-10-11 01:20:53 -0700455 ext4_warning (sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700456 "not enough memory for %lu groups", gdb_num + 1);
457 goto exit_inode;
458 }
459
460 /*
461 * Finally, we have all of the possible failures behind us...
462 *
463 * Remove new GDT block from inode double-indirect block and clear out
464 * the new GDT block for use (which also "frees" the backup GDT blocks
465 * from the reserved inode). We don't need to change the bitmaps for
466 * these blocks, because they are marked as in-use from being in the
467 * reserved inode, and will become GDT blocks (primary and backup).
468 */
Mingming Cao617ba132006-10-11 01:20:53 -0700469 data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
470 ext4_journal_dirty_metadata(handle, dind);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700471 brelse(dind);
472 inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
Mingming Cao617ba132006-10-11 01:20:53 -0700473 ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700474 memset((*primary)->b_data, 0, sb->s_blocksize);
Mingming Cao617ba132006-10-11 01:20:53 -0700475 ext4_journal_dirty_metadata(handle, *primary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700476
Mingming Cao617ba132006-10-11 01:20:53 -0700477 o_group_desc = EXT4_SB(sb)->s_group_desc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700478 memcpy(n_group_desc, o_group_desc,
Mingming Cao617ba132006-10-11 01:20:53 -0700479 EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700480 n_group_desc[gdb_num] = *primary;
Mingming Cao617ba132006-10-11 01:20:53 -0700481 EXT4_SB(sb)->s_group_desc = n_group_desc;
482 EXT4_SB(sb)->s_gdb_count++;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700483 kfree(o_group_desc);
484
485 es->s_reserved_gdt_blocks =
486 cpu_to_le16(le16_to_cpu(es->s_reserved_gdt_blocks) - 1);
Mingming Cao617ba132006-10-11 01:20:53 -0700487 ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700488
489 return 0;
490
491exit_inode:
Mingming Cao617ba132006-10-11 01:20:53 -0700492 //ext4_journal_release_buffer(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700493 brelse(iloc.bh);
494exit_dindj:
Mingming Cao617ba132006-10-11 01:20:53 -0700495 //ext4_journal_release_buffer(handle, dind);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700496exit_primary:
Mingming Cao617ba132006-10-11 01:20:53 -0700497 //ext4_journal_release_buffer(handle, *primary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700498exit_sbh:
Mingming Cao617ba132006-10-11 01:20:53 -0700499 //ext4_journal_release_buffer(handle, *primary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500exit_dind:
501 brelse(dind);
502exit_bh:
503 brelse(*primary);
504
Mingming Cao617ba132006-10-11 01:20:53 -0700505 ext4_debug("leaving with error %d\n", err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700506 return err;
507}
508
509/*
510 * Called when we are adding a new group which has a backup copy of each of
511 * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
512 * We need to add these reserved backup GDT blocks to the resize inode, so
513 * that they are kept for future resizing and not allocated to files.
514 *
515 * Each reserved backup GDT block will go into a different indirect block.
516 * The indirect blocks are actually the primary reserved GDT blocks,
517 * so we know in advance what their block numbers are. We only get the
518 * double-indirect block to verify it is pointing to the primary reserved
519 * GDT blocks so we don't overwrite a data block by accident. The reserved
520 * backup GDT blocks are stored in their reserved primary GDT block.
521 */
522static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
Mingming Cao617ba132006-10-11 01:20:53 -0700523 struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700524{
525 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -0700526 int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700527 struct buffer_head **primary;
528 struct buffer_head *dind;
Mingming Cao617ba132006-10-11 01:20:53 -0700529 struct ext4_iloc iloc;
530 ext4_fsblk_t blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700531 __le32 *data, *end;
532 int gdbackups = 0;
533 int res, i;
534 int err;
535
536 primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_KERNEL);
537 if (!primary)
538 return -ENOMEM;
539
Mingming Cao617ba132006-10-11 01:20:53 -0700540 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700541 dind = sb_bread(sb, le32_to_cpu(*data));
542 if (!dind) {
543 err = -EIO;
544 goto exit_free;
545 }
546
Mingming Cao617ba132006-10-11 01:20:53 -0700547 blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count;
548 data = (__le32 *)dind->b_data + EXT4_SB(sb)->s_gdb_count;
549 end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700550
551 /* Get each reserved primary GDT block and verify it holds backups */
552 for (res = 0; res < reserved_gdb; res++, blk++) {
553 if (le32_to_cpu(*data) != blk) {
Mingming Cao617ba132006-10-11 01:20:53 -0700554 ext4_warning(sb, __FUNCTION__,
Mingming Cao2ae02102006-10-11 01:21:11 -0700555 "reserved block %llu"
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700556 " not at offset %ld",
557 blk,
558 (long)(data - (__le32 *)dind->b_data));
559 err = -EINVAL;
560 goto exit_bh;
561 }
562 primary[res] = sb_bread(sb, blk);
563 if (!primary[res]) {
564 err = -EIO;
565 goto exit_bh;
566 }
567 if ((gdbackups = verify_reserved_gdb(sb, primary[res])) < 0) {
568 brelse(primary[res]);
569 err = gdbackups;
570 goto exit_bh;
571 }
572 if (++data >= end)
573 data = (__le32 *)dind->b_data;
574 }
575
576 for (i = 0; i < reserved_gdb; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -0700577 if ((err = ext4_journal_get_write_access(handle, primary[i]))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700578 /*
579 int j;
580 for (j = 0; j < i; j++)
Mingming Cao617ba132006-10-11 01:20:53 -0700581 ext4_journal_release_buffer(handle, primary[j]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700582 */
583 goto exit_bh;
584 }
585 }
586
Mingming Cao617ba132006-10-11 01:20:53 -0700587 if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700588 goto exit_bh;
589
590 /*
591 * Finally we can add each of the reserved backup GDT blocks from
592 * the new group to its reserved primary GDT block.
593 */
Mingming Cao617ba132006-10-11 01:20:53 -0700594 blk = input->group * EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700595 for (i = 0; i < reserved_gdb; i++) {
596 int err2;
597 data = (__le32 *)primary[i]->b_data;
598 /* printk("reserving backup %lu[%u] = %lu\n",
599 primary[i]->b_blocknr, gdbackups,
600 blk + primary[i]->b_blocknr); */
601 data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
Mingming Cao617ba132006-10-11 01:20:53 -0700602 err2 = ext4_journal_dirty_metadata(handle, primary[i]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700603 if (!err)
604 err = err2;
605 }
606 inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
Mingming Cao617ba132006-10-11 01:20:53 -0700607 ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700608
609exit_bh:
610 while (--res >= 0)
611 brelse(primary[res]);
612 brelse(dind);
613
614exit_free:
615 kfree(primary);
616
617 return err;
618}
619
620/*
Mingming Cao617ba132006-10-11 01:20:53 -0700621 * Update the backup copies of the ext4 metadata. These don't need to be part
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700622 * of the main resize transaction, because e2fsck will re-write them if there
623 * is a problem (basically only OOM will cause a problem). However, we
624 * _should_ update the backups if possible, in case the primary gets trashed
625 * for some reason and we need to run e2fsck from a backup superblock. The
626 * important part is that the new block and inode counts are in the backup
627 * superblocks, and the location of the new group metadata in the GDT backups.
628 *
629 * We do not need lock_super() for this, because these blocks are not
630 * otherwise touched by the filesystem code when it is mounted. We don't
631 * need to worry about last changing from sbi->s_groups_count, because the
632 * worst that can happen is that we do not copy the full number of backups
633 * at this time. The resize which changed s_groups_count will backup again.
634 */
635static void update_backups(struct super_block *sb,
636 int blk_off, char *data, int size)
637{
Mingming Cao617ba132006-10-11 01:20:53 -0700638 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700639 const unsigned long last = sbi->s_groups_count;
Mingming Cao617ba132006-10-11 01:20:53 -0700640 const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700641 unsigned three = 1;
642 unsigned five = 5;
643 unsigned seven = 7;
644 unsigned group;
645 int rest = sb->s_blocksize - size;
646 handle_t *handle;
647 int err = 0, err2;
648
Mingming Cao617ba132006-10-11 01:20:53 -0700649 handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700650 if (IS_ERR(handle)) {
651 group = 1;
652 err = PTR_ERR(handle);
653 goto exit_err;
654 }
655
Mingming Cao617ba132006-10-11 01:20:53 -0700656 while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700657 struct buffer_head *bh;
658
659 /* Out of journal space, and can't get more - abort - so sad */
660 if (handle->h_buffer_credits == 0 &&
Mingming Cao617ba132006-10-11 01:20:53 -0700661 ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) &&
662 (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700663 break;
664
665 bh = sb_getblk(sb, group * bpg + blk_off);
666 if (!bh) {
667 err = -EIO;
668 break;
669 }
Mingming Cao617ba132006-10-11 01:20:53 -0700670 ext4_debug("update metadata backup %#04lx\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700671 (unsigned long)bh->b_blocknr);
Mingming Cao617ba132006-10-11 01:20:53 -0700672 if ((err = ext4_journal_get_write_access(handle, bh)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700673 break;
674 lock_buffer(bh);
675 memcpy(bh->b_data, data, size);
676 if (rest)
677 memset(bh->b_data + size, 0, rest);
678 set_buffer_uptodate(bh);
679 unlock_buffer(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700680 ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700681 brelse(bh);
682 }
Mingming Cao617ba132006-10-11 01:20:53 -0700683 if ((err2 = ext4_journal_stop(handle)) && !err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700684 err = err2;
685
686 /*
687 * Ugh! Need to have e2fsck write the backup copies. It is too
688 * late to revert the resize, we shouldn't fail just because of
689 * the backup copies (they are only needed in case of corruption).
690 *
691 * However, if we got here we have a journal problem too, so we
692 * can't really start a transaction to mark the superblock.
693 * Chicken out and just set the flag on the hope it will be written
694 * to disk, and if not - we will simply wait until next fsck.
695 */
696exit_err:
697 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -0700698 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700699 "can't update backup for group %d (err %d), "
700 "forcing fsck on next reboot", group, err);
Mingming Cao617ba132006-10-11 01:20:53 -0700701 sbi->s_mount_state &= ~EXT4_VALID_FS;
702 sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700703 mark_buffer_dirty(sbi->s_sbh);
704 }
705}
706
707/* Add group descriptor data to an existing or new group descriptor block.
708 * Ensure we handle all possible error conditions _before_ we start modifying
709 * the filesystem, because we cannot abort the transaction and not have it
710 * write the data to disk.
711 *
712 * If we are on a GDT block boundary, we need to get the reserved GDT block.
713 * Otherwise, we may need to add backup GDT blocks for a sparse group.
714 *
715 * We only need to hold the superblock lock while we are actually adding
716 * in the new group's counts to the superblock. Prior to that we have
717 * not really "added" the group at all. We re-check that we are still
718 * adding in the last group in case things have changed since verifying.
719 */
Mingming Cao617ba132006-10-11 01:20:53 -0700720int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700721{
Mingming Cao617ba132006-10-11 01:20:53 -0700722 struct ext4_sb_info *sbi = EXT4_SB(sb);
723 struct ext4_super_block *es = sbi->s_es;
724 int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700725 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
726 struct buffer_head *primary = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700727 struct ext4_group_desc *gdp;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700728 struct inode *inode = NULL;
729 handle_t *handle;
730 int gdb_off, gdb_num;
731 int err, err2;
732
Mingming Cao617ba132006-10-11 01:20:53 -0700733 gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
734 gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700735
Mingming Cao617ba132006-10-11 01:20:53 -0700736 if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb,
737 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
738 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700739 "Can't resize non-sparse filesystem further");
740 return -EPERM;
741 }
742
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700743 if (ext4_blocks_count(es) + input->blocks_count <
744 ext4_blocks_count(es)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700745 ext4_warning(sb, __FUNCTION__, "blocks_count overflow\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700746 return -EINVAL;
747 }
748
Mingming Cao617ba132006-10-11 01:20:53 -0700749 if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700750 le32_to_cpu(es->s_inodes_count)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700751 ext4_warning(sb, __FUNCTION__, "inodes_count overflow\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700752 return -EINVAL;
753 }
754
755 if (reserved_gdb || gdb_off == 0) {
Mingming Cao617ba132006-10-11 01:20:53 -0700756 if (!EXT4_HAS_COMPAT_FEATURE(sb,
757 EXT4_FEATURE_COMPAT_RESIZE_INODE)){
758 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700759 "No reserved GDT blocks, can't resize");
760 return -EPERM;
761 }
Mingming Cao617ba132006-10-11 01:20:53 -0700762 inode = iget(sb, EXT4_RESIZE_INO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700763 if (!inode || is_bad_inode(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700764 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700765 "Error opening resize inode");
766 iput(inode);
767 return -ENOENT;
768 }
769 }
770
771 if ((err = verify_group_input(sb, input)))
772 goto exit_put;
773
774 if ((err = setup_new_group_blocks(sb, input)))
775 goto exit_put;
776
777 /*
778 * We will always be modifying at least the superblock and a GDT
779 * block. If we are adding a group past the last current GDT block,
780 * we will also modify the inode and the dindirect block. If we
781 * are adding a group with superblock/GDT backups we will also
782 * modify each of the reserved GDT dindirect blocks.
783 */
Mingming Cao617ba132006-10-11 01:20:53 -0700784 handle = ext4_journal_start_sb(sb,
785 ext4_bg_has_super(sb, input->group) ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700786 3 + reserved_gdb : 4);
787 if (IS_ERR(handle)) {
788 err = PTR_ERR(handle);
789 goto exit_put;
790 }
791
792 lock_super(sb);
793 if (input->group != sbi->s_groups_count) {
Mingming Cao617ba132006-10-11 01:20:53 -0700794 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700795 "multiple resizers run on filesystem!");
796 err = -EBUSY;
797 goto exit_journal;
798 }
799
Mingming Cao617ba132006-10-11 01:20:53 -0700800 if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700801 goto exit_journal;
802
803 /*
804 * We will only either add reserved group blocks to a backup group
805 * or remove reserved blocks for the first group in a new group block.
806 * Doing both would be mean more complex code, and sane people don't
807 * use non-sparse filesystems anymore. This is already checked above.
808 */
809 if (gdb_off) {
810 primary = sbi->s_group_desc[gdb_num];
Mingming Cao617ba132006-10-11 01:20:53 -0700811 if ((err = ext4_journal_get_write_access(handle, primary)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700812 goto exit_journal;
813
Mingming Cao617ba132006-10-11 01:20:53 -0700814 if (reserved_gdb && ext4_bg_num_gdb(sb, input->group) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700815 (err = reserve_backup_gdb(handle, inode, input)))
816 goto exit_journal;
817 } else if ((err = add_new_gdb(handle, inode, input, &primary)))
818 goto exit_journal;
819
820 /*
821 * OK, now we've set up the new group. Time to make it active.
822 *
823 * Current kernels don't lock all allocations via lock_super(),
824 * so we have to be safe wrt. concurrent accesses the group
825 * data. So we need to be careful to set all of the relevant
826 * group descriptor data etc. *before* we enable the group.
827 *
828 * The key field here is sbi->s_groups_count: as long as
829 * that retains its old value, nobody is going to access the new
830 * group.
831 *
832 * So first we update all the descriptor metadata for the new
833 * group; then we update the total disk blocks count; then we
834 * update the groups count to enable the group; then finally we
835 * update the free space counts so that the system can start
836 * using the new disk blocks.
837 */
838
839 /* Update group descriptor block for new group */
Mingming Cao617ba132006-10-11 01:20:53 -0700840 gdp = (struct ext4_group_desc *)primary->b_data + gdb_off;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700841
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700842 ext4_block_bitmap_set(sb, gdp, input->block_bitmap); /* LV FIXME */
843 ext4_inode_bitmap_set(sb, gdp, input->inode_bitmap); /* LV FIXME */
844 ext4_inode_table_set(sb, gdp, input->inode_table); /* LV FIXME */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700845 gdp->bg_free_blocks_count = cpu_to_le16(input->free_blocks_count);
Mingming Cao617ba132006-10-11 01:20:53 -0700846 gdp->bg_free_inodes_count = cpu_to_le16(EXT4_INODES_PER_GROUP(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700847
848 /*
849 * Make the new blocks and inodes valid next. We do this before
850 * increasing the group count so that once the group is enabled,
851 * all of its blocks and inodes are already valid.
852 *
853 * We always allocate group-by-group, then block-by-block or
854 * inode-by-inode within a group, so enabling these
855 * blocks/inodes before the group is live won't actually let us
856 * allocate the new space yet.
857 */
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700858 ext4_blocks_count_set(es, ext4_blocks_count(es) +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700859 input->blocks_count);
860 es->s_inodes_count = cpu_to_le32(le32_to_cpu(es->s_inodes_count) +
Mingming Cao617ba132006-10-11 01:20:53 -0700861 EXT4_INODES_PER_GROUP(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700862
863 /*
864 * We need to protect s_groups_count against other CPUs seeing
865 * inconsistent state in the superblock.
866 *
867 * The precise rules we use are:
868 *
869 * * Writers of s_groups_count *must* hold lock_super
870 * AND
871 * * Writers must perform a smp_wmb() after updating all dependent
872 * data and before modifying the groups count
873 *
874 * * Readers must hold lock_super() over the access
875 * OR
876 * * Readers must perform an smp_rmb() after reading the groups count
877 * and before reading any dependent data.
878 *
879 * NB. These rules can be relaxed when checking the group count
880 * while freeing data, as we can only allocate from a block
881 * group after serialising against the group count, and we can
882 * only then free after serialising in turn against that
883 * allocation.
884 */
885 smp_wmb();
886
887 /* Update the global fs size fields */
888 sbi->s_groups_count++;
889
Mingming Cao617ba132006-10-11 01:20:53 -0700890 ext4_journal_dirty_metadata(handle, primary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700891
892 /* Update the reserved block counts only once the new group is
893 * active. */
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700894 ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700895 input->reserved_blocks);
896
897 /* Update the free space counts */
898 percpu_counter_mod(&sbi->s_freeblocks_counter,
899 input->free_blocks_count);
900 percpu_counter_mod(&sbi->s_freeinodes_counter,
Mingming Cao617ba132006-10-11 01:20:53 -0700901 EXT4_INODES_PER_GROUP(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700902
Mingming Cao617ba132006-10-11 01:20:53 -0700903 ext4_journal_dirty_metadata(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700904 sb->s_dirt = 1;
905
906exit_journal:
907 unlock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700908 if ((err2 = ext4_journal_stop(handle)) && !err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700909 err = err2;
910 if (!err) {
911 update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
Mingming Cao617ba132006-10-11 01:20:53 -0700912 sizeof(struct ext4_super_block));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700913 update_backups(sb, primary->b_blocknr, primary->b_data,
914 primary->b_size);
915 }
916exit_put:
917 iput(inode);
918 return err;
Mingming Cao617ba132006-10-11 01:20:53 -0700919} /* ext4_group_add */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700920
921/* Extend the filesystem to the new number of blocks specified. This entry
922 * point is only used to extend the current filesystem to the end of the last
923 * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
924 * for emergencies (because it has no dependencies on reserved blocks).
925 *
Mingming Cao617ba132006-10-11 01:20:53 -0700926 * If we _really_ wanted, we could use default values to call ext4_group_add()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700927 * allow the "remount" trick to work for arbitrary resizing, assuming enough
928 * GDT blocks are reserved to grow to the desired size.
929 */
Mingming Cao617ba132006-10-11 01:20:53 -0700930int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
931 ext4_fsblk_t n_blocks_count)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700932{
Mingming Cao617ba132006-10-11 01:20:53 -0700933 ext4_fsblk_t o_blocks_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700934 unsigned long o_groups_count;
Mingming Cao617ba132006-10-11 01:20:53 -0700935 ext4_grpblk_t last;
936 ext4_grpblk_t add;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700937 struct buffer_head * bh;
938 handle_t *handle;
939 int err;
940 unsigned long freed_blocks;
941
942 /* We don't need to worry about locking wrt other resizers just
943 * yet: we're going to revalidate es->s_blocks_count after
944 * taking lock_super() below. */
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700945 o_blocks_count = ext4_blocks_count(es);
Mingming Cao617ba132006-10-11 01:20:53 -0700946 o_groups_count = EXT4_SB(sb)->s_groups_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700947
948 if (test_opt(sb, DEBUG))
Mingming Cao2ae02102006-10-11 01:21:11 -0700949 printk(KERN_DEBUG "EXT4-fs: extending last group from %llu uto %llu blocks\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700950 o_blocks_count, n_blocks_count);
951
952 if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
953 return 0;
954
955 if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700956 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
Mingming Cao2ae02102006-10-11 01:21:11 -0700957 " too large to resize to %llu blocks safely\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700958 sb->s_id, n_blocks_count);
959 if (sizeof(sector_t) < 8)
Mingming Cao617ba132006-10-11 01:20:53 -0700960 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700961 "CONFIG_LBD not enabled\n");
962 return -EINVAL;
963 }
964
965 if (n_blocks_count < o_blocks_count) {
Mingming Cao617ba132006-10-11 01:20:53 -0700966 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700967 "can't shrink FS - resize aborted");
968 return -EBUSY;
969 }
970
971 /* Handle the remaining blocks in the last group only. */
Mingming Cao3a5b2ec2006-10-11 01:21:05 -0700972 ext4_get_group_no_and_offset(sb, o_blocks_count, NULL, &last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700973
974 if (last == 0) {
Mingming Cao617ba132006-10-11 01:20:53 -0700975 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700976 "need to use ext2online to resize further");
977 return -EPERM;
978 }
979
Mingming Cao617ba132006-10-11 01:20:53 -0700980 add = EXT4_BLOCKS_PER_GROUP(sb) - last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700981
982 if (o_blocks_count + add < o_blocks_count) {
Mingming Cao617ba132006-10-11 01:20:53 -0700983 ext4_warning(sb, __FUNCTION__, "blocks_count overflow");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700984 return -EINVAL;
985 }
986
987 if (o_blocks_count + add > n_blocks_count)
988 add = n_blocks_count - o_blocks_count;
989
990 if (o_blocks_count + add < n_blocks_count)
Mingming Cao617ba132006-10-11 01:20:53 -0700991 ext4_warning(sb, __FUNCTION__,
Mingming Cao2ae02102006-10-11 01:21:11 -0700992 "will only finish group (%llu"
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993 " blocks, %u new)",
994 o_blocks_count + add, add);
995
996 /* See if the device is actually as big as what was requested */
997 bh = sb_bread(sb, o_blocks_count + add -1);
998 if (!bh) {
Mingming Cao617ba132006-10-11 01:20:53 -0700999 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001000 "can't read last block, resize aborted");
1001 return -ENOSPC;
1002 }
1003 brelse(bh);
1004
1005 /* We will update the superblock, one block bitmap, and
Mingming Cao617ba132006-10-11 01:20:53 -07001006 * one group descriptor via ext4_free_blocks().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001007 */
Mingming Cao617ba132006-10-11 01:20:53 -07001008 handle = ext4_journal_start_sb(sb, 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001009 if (IS_ERR(handle)) {
1010 err = PTR_ERR(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07001011 ext4_warning(sb, __FUNCTION__, "error %d on journal start",err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001012 goto exit_put;
1013 }
1014
1015 lock_super(sb);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001016 if (o_blocks_count != ext4_blocks_count(es)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001017 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001018 "multiple resizers run on filesystem!");
1019 unlock_super(sb);
1020 err = -EBUSY;
1021 goto exit_put;
1022 }
1023
Mingming Cao617ba132006-10-11 01:20:53 -07001024 if ((err = ext4_journal_get_write_access(handle,
1025 EXT4_SB(sb)->s_sbh))) {
1026 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001027 "error %d on journal write access", err);
1028 unlock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07001029 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001030 goto exit_put;
1031 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001032 ext4_blocks_count_set(es, o_blocks_count + add);
Mingming Cao617ba132006-10-11 01:20:53 -07001033 ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001034 sb->s_dirt = 1;
1035 unlock_super(sb);
Mingming Cao2ae02102006-10-11 01:21:11 -07001036 ext4_debug("freeing blocks %lu through %llu\n", o_blocks_count,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001037 o_blocks_count + add);
Mingming Cao617ba132006-10-11 01:20:53 -07001038 ext4_free_blocks_sb(handle, sb, o_blocks_count, add, &freed_blocks);
Mingming Cao2ae02102006-10-11 01:21:11 -07001039 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001040 o_blocks_count + add);
Mingming Cao617ba132006-10-11 01:20:53 -07001041 if ((err = ext4_journal_stop(handle)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001042 goto exit_put;
1043 if (test_opt(sb, DEBUG))
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001044 printk(KERN_DEBUG "EXT4-fs: extended group to %llu blocks\n",
1045 ext4_blocks_count(es));
Mingming Cao617ba132006-10-11 01:20:53 -07001046 update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es,
1047 sizeof(struct ext4_super_block));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001048exit_put:
1049 return err;
Mingming Cao617ba132006-10-11 01:20:53 -07001050} /* ext4_group_extend */