blob: e8ded13b5cb133ab22d731d5a3cc351d8d0c3272 [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
Dave Kleikampac27a0e2006-10-11 01:20:50 -070014#include <linux/errno.h>
15#include <linux/slab.h>
16
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040017#include "ext4_jbd2.h"
Andreas Dilger717d50e2007-10-16 18:38:25 -040018#include "group.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070019
20#define outside(b, first, last) ((b) < (first) || (b) >= (last))
21#define inside(b, first, last) ((b) >= (first) && (b) < (last))
22
23static int verify_group_input(struct super_block *sb,
Mingming Cao617ba132006-10-11 01:20:53 -070024 struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070025{
Mingming Cao617ba132006-10-11 01:20:53 -070026 struct ext4_sb_info *sbi = EXT4_SB(sb);
27 struct ext4_super_block *es = sbi->s_es;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070028 ext4_fsblk_t start = ext4_blocks_count(es);
Mingming Cao617ba132006-10-11 01:20:53 -070029 ext4_fsblk_t end = start + input->blocks_count;
Avantika Mathurfd2d4292008-01-28 23:58:27 -050030 ext4_group_t group = input->group;
Mingming Cao617ba132006-10-11 01:20:53 -070031 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 Kleikampac27a0e2006-10-11 01:20:50 -070034 le16_to_cpu(es->s_reserved_gdt_blocks)) : 0;
Mingming Cao617ba132006-10-11 01:20:53 -070035 ext4_fsblk_t metaend = start + overhead;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070036 struct buffer_head *bh = NULL;
Mingming Cao3a5b2ec2006-10-11 01:21:05 -070037 ext4_grpblk_t free_blocks_count, offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070038 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 Cao617ba132006-10-11 01:20:53 -070044 printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks "
Dave Kleikampac27a0e2006-10-11 01:20:50 -070045 "(%d free, %u reserved)\n",
Mingming Cao617ba132006-10-11 01:20:53 -070046 ext4_bg_has_super(sb, input->group) ? "normal" :
Dave Kleikampac27a0e2006-10-11 01:20:50 -070047 "no-super", input->group, input->blocks_count,
48 free_blocks_count, input->reserved_blocks);
49
Mingming Cao3a5b2ec2006-10-11 01:21:05 -070050 ext4_get_group_no_and_offset(sb, start, NULL, &offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070051 if (group != sbi->s_groups_count)
Harvey Harrison46e665e2008-04-17 10:38:59 -040052 ext4_warning(sb, __func__,
Theodore Ts'oa9df9a42009-01-05 22:18:16 -050053 "Cannot add at group %u (only %u groups)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -070054 input->group, sbi->s_groups_count);
Mingming Cao3a5b2ec2006-10-11 01:21:05 -070055 else if (offset != 0)
Harvey Harrison46e665e2008-04-17 10:38:59 -040056 ext4_warning(sb, __func__, "Last group not full");
Dave Kleikampac27a0e2006-10-11 01:20:50 -070057 else if (input->reserved_blocks > input->blocks_count / 5)
Harvey Harrison46e665e2008-04-17 10:38:59 -040058 ext4_warning(sb, __func__, "Reserved blocks too high (%u)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -070059 input->reserved_blocks);
60 else if (free_blocks_count < 0)
Harvey Harrison46e665e2008-04-17 10:38:59 -040061 ext4_warning(sb, __func__, "Bad blocks count %u",
Dave Kleikampac27a0e2006-10-11 01:20:50 -070062 input->blocks_count);
63 else if (!(bh = sb_bread(sb, end - 1)))
Harvey Harrison46e665e2008-04-17 10:38:59 -040064 ext4_warning(sb, __func__,
Mingming Cao2ae02102006-10-11 01:21:11 -070065 "Cannot read last block (%llu)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -070066 end - 1);
67 else if (outside(input->block_bitmap, start, end))
Harvey Harrison46e665e2008-04-17 10:38:59 -040068 ext4_warning(sb, __func__,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070069 "Block bitmap not in group (block %llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -070070 (unsigned long long)input->block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070071 else if (outside(input->inode_bitmap, start, end))
Harvey Harrison46e665e2008-04-17 10:38:59 -040072 ext4_warning(sb, __func__,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070073 "Inode bitmap not in group (block %llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -070074 (unsigned long long)input->inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070075 else if (outside(input->inode_table, start, end) ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040076 outside(itend - 1, start, end))
Harvey Harrison46e665e2008-04-17 10:38:59 -040077 ext4_warning(sb, __func__,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070078 "Inode table not in group (blocks %llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -070079 (unsigned long long)input->inode_table, itend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070080 else if (input->inode_bitmap == input->block_bitmap)
Harvey Harrison46e665e2008-04-17 10:38:59 -040081 ext4_warning(sb, __func__,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070082 "Block bitmap same as inode bitmap (%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -070083 (unsigned long long)input->block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070084 else if (inside(input->block_bitmap, input->inode_table, itend))
Harvey Harrison46e665e2008-04-17 10:38:59 -040085 ext4_warning(sb, __func__,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070086 "Block bitmap (%llu) in inode table (%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -070087 (unsigned long long)input->block_bitmap,
88 (unsigned long long)input->inode_table, itend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070089 else if (inside(input->inode_bitmap, input->inode_table, itend))
Harvey Harrison46e665e2008-04-17 10:38:59 -040090 ext4_warning(sb, __func__,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070091 "Inode bitmap (%llu) in inode table (%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -070092 (unsigned long long)input->inode_bitmap,
93 (unsigned long long)input->inode_table, itend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070094 else if (inside(input->block_bitmap, start, metaend))
Harvey Harrison46e665e2008-04-17 10:38:59 -040095 ext4_warning(sb, __func__,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070096 "Block bitmap (%llu) in GDT table"
Mingming Cao2ae02102006-10-11 01:21:11 -070097 " (%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -070098 (unsigned long long)input->block_bitmap,
99 start, metaend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700100 else if (inside(input->inode_bitmap, start, metaend))
Harvey Harrison46e665e2008-04-17 10:38:59 -0400101 ext4_warning(sb, __func__,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700102 "Inode bitmap (%llu) in GDT table"
Mingming Cao2ae02102006-10-11 01:21:11 -0700103 " (%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700104 (unsigned long long)input->inode_bitmap,
105 start, metaend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700106 else if (inside(input->inode_table, start, metaend) ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400107 inside(itend - 1, start, metaend))
Harvey Harrison46e665e2008-04-17 10:38:59 -0400108 ext4_warning(sb, __func__,
Mingming Cao2ae02102006-10-11 01:21:11 -0700109 "Inode table (%llu-%llu) overlaps"
110 "GDT table (%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700111 (unsigned long long)input->inode_table,
112 itend - 1, start, metaend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700113 else
114 err = 0;
115 brelse(bh);
116
117 return err;
118}
119
120static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
Mingming Cao617ba132006-10-11 01:20:53 -0700121 ext4_fsblk_t blk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700122{
123 struct buffer_head *bh;
124 int err;
125
126 bh = sb_getblk(sb, blk);
127 if (!bh)
128 return ERR_PTR(-EIO);
Mingming Cao617ba132006-10-11 01:20:53 -0700129 if ((err = ext4_journal_get_write_access(handle, bh))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700130 brelse(bh);
131 bh = ERR_PTR(err);
132 } else {
133 lock_buffer(bh);
134 memset(bh->b_data, 0, sb->s_blocksize);
135 set_buffer_uptodate(bh);
136 unlock_buffer(bh);
137 }
138
139 return bh;
140}
141
142/*
Eric Sandeen14904102007-10-16 18:38:25 -0400143 * If we have fewer than thresh credits, extend by EXT4_MAX_TRANS_DATA.
144 * If that fails, restart the transaction & regain write access for the
145 * buffer head which is used for block_bitmap modifications.
146 */
147static int extend_or_restart_transaction(handle_t *handle, int thresh,
148 struct buffer_head *bh)
149{
150 int err;
151
Frank Mayhar03901312009-01-07 00:06:22 -0500152 if (ext4_handle_has_enough_credits(handle, thresh))
Eric Sandeen14904102007-10-16 18:38:25 -0400153 return 0;
154
155 err = ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA);
156 if (err < 0)
157 return err;
158 if (err) {
159 if ((err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
160 return err;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400161 if ((err = ext4_journal_get_write_access(handle, bh)))
Eric Sandeen14904102007-10-16 18:38:25 -0400162 return err;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400163 }
Eric Sandeen14904102007-10-16 18:38:25 -0400164
165 return 0;
166}
167
168/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700169 * Set up the block and inode bitmaps, and the inode table for the new group.
170 * This doesn't need to be part of the main transaction, since we are only
171 * changing blocks outside the actual filesystem. We still do journaling to
172 * ensure the recovery is correct in case of a failure just after resize.
173 * If any part of this fails, we simply abort the resize.
174 */
175static int setup_new_group_blocks(struct super_block *sb,
Mingming Cao617ba132006-10-11 01:20:53 -0700176 struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700177{
Mingming Cao617ba132006-10-11 01:20:53 -0700178 struct ext4_sb_info *sbi = EXT4_SB(sb);
179 ext4_fsblk_t start = ext4_group_first_block_no(sb, input->group);
180 int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700181 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700182 unsigned long gdblocks = ext4_bg_num_gdb(sb, input->group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700183 struct buffer_head *bh;
184 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -0700185 ext4_fsblk_t block;
186 ext4_grpblk_t bit;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700187 int i;
188 int err = 0, err2;
189
Eric Sandeen14904102007-10-16 18:38:25 -0400190 /* This transaction may be extended/restarted along the way */
191 handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
192
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700193 if (IS_ERR(handle))
194 return PTR_ERR(handle);
195
Theodore Ts'o32ed5052009-04-25 22:53:39 -0400196 mutex_lock(&sbi->s_resize_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700197 if (input->group != sbi->s_groups_count) {
198 err = -EBUSY;
199 goto exit_journal;
200 }
201
202 if (IS_ERR(bh = bclean(handle, sb, input->block_bitmap))) {
203 err = PTR_ERR(bh);
204 goto exit_journal;
205 }
206
Mingming Cao617ba132006-10-11 01:20:53 -0700207 if (ext4_bg_has_super(sb, input->group)) {
Eric Sandeenc549a952008-01-28 23:58:27 -0500208 ext4_debug("mark backup superblock %#04llx (+0)\n", start);
Mingming Cao617ba132006-10-11 01:20:53 -0700209 ext4_set_bit(0, bh->b_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700210 }
211
212 /* Copy all of the GDT blocks into the backup in this group */
213 for (i = 0, bit = 1, block = start + 1;
214 i < gdblocks; i++, block++, bit++) {
215 struct buffer_head *gdb;
216
Eric Sandeenc549a952008-01-28 23:58:27 -0500217 ext4_debug("update backup group %#04llx (+%d)\n", block, bit);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700218
Eric Sandeen14904102007-10-16 18:38:25 -0400219 if ((err = extend_or_restart_transaction(handle, 1, bh)))
220 goto exit_bh;
221
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700222 gdb = sb_getblk(sb, block);
223 if (!gdb) {
224 err = -EIO;
225 goto exit_bh;
226 }
Mingming Cao617ba132006-10-11 01:20:53 -0700227 if ((err = ext4_journal_get_write_access(handle, gdb))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700228 brelse(gdb);
229 goto exit_bh;
230 }
Eric Sandeen5b615282007-10-16 18:38:25 -0400231 lock_buffer(gdb);
232 memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, gdb->b_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700233 set_buffer_uptodate(gdb);
Eric Sandeen5b615282007-10-16 18:38:25 -0400234 unlock_buffer(gdb);
Frank Mayhar03901312009-01-07 00:06:22 -0500235 ext4_handle_dirty_metadata(handle, NULL, gdb);
Mingming Cao617ba132006-10-11 01:20:53 -0700236 ext4_set_bit(bit, bh->b_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700237 brelse(gdb);
238 }
239
240 /* Zero out all of the reserved backup group descriptor table blocks */
241 for (i = 0, bit = gdblocks + 1, block = start + bit;
242 i < reserved_gdb; i++, block++, bit++) {
243 struct buffer_head *gdb;
244
Eric Sandeenc549a952008-01-28 23:58:27 -0500245 ext4_debug("clear reserved block %#04llx (+%d)\n", block, bit);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700246
Eric Sandeen14904102007-10-16 18:38:25 -0400247 if ((err = extend_or_restart_transaction(handle, 1, bh)))
248 goto exit_bh;
249
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700250 if (IS_ERR(gdb = bclean(handle, sb, block))) {
251 err = PTR_ERR(bh);
252 goto exit_bh;
253 }
Frank Mayhar03901312009-01-07 00:06:22 -0500254 ext4_handle_dirty_metadata(handle, NULL, gdb);
Mingming Cao617ba132006-10-11 01:20:53 -0700255 ext4_set_bit(bit, bh->b_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700256 brelse(gdb);
257 }
Eric Sandeenc549a952008-01-28 23:58:27 -0500258 ext4_debug("mark block bitmap %#04llx (+%llu)\n", input->block_bitmap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700259 input->block_bitmap - start);
Mingming Cao617ba132006-10-11 01:20:53 -0700260 ext4_set_bit(input->block_bitmap - start, bh->b_data);
Eric Sandeenc549a952008-01-28 23:58:27 -0500261 ext4_debug("mark inode bitmap %#04llx (+%llu)\n", input->inode_bitmap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700262 input->inode_bitmap - start);
Mingming Cao617ba132006-10-11 01:20:53 -0700263 ext4_set_bit(input->inode_bitmap - start, bh->b_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700264
265 /* Zero out all of the inode table blocks */
266 for (i = 0, block = input->inode_table, bit = block - start;
267 i < sbi->s_itb_per_group; i++, bit++, block++) {
268 struct buffer_head *it;
269
Eric Sandeenc549a952008-01-28 23:58:27 -0500270 ext4_debug("clear inode block %#04llx (+%d)\n", block, bit);
Eric Sandeen14904102007-10-16 18:38:25 -0400271
272 if ((err = extend_or_restart_transaction(handle, 1, bh)))
273 goto exit_bh;
274
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700275 if (IS_ERR(it = bclean(handle, sb, block))) {
276 err = PTR_ERR(it);
277 goto exit_bh;
278 }
Frank Mayhar03901312009-01-07 00:06:22 -0500279 ext4_handle_dirty_metadata(handle, NULL, it);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700280 brelse(it);
Mingming Cao617ba132006-10-11 01:20:53 -0700281 ext4_set_bit(bit, bh->b_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700282 }
Eric Sandeen14904102007-10-16 18:38:25 -0400283
284 if ((err = extend_or_restart_transaction(handle, 2, bh)))
285 goto exit_bh;
286
Aneesh Kumar K.V648f5872009-01-05 21:46:04 -0500287 mark_bitmap_end(input->blocks_count, sb->s_blocksize * 8, bh->b_data);
Frank Mayhar03901312009-01-07 00:06:22 -0500288 ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700289 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700290 /* Mark unused entries in inode bitmap used */
Eric Sandeenc549a952008-01-28 23:58:27 -0500291 ext4_debug("clear inode bitmap %#04llx (+%llu)\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700292 input->inode_bitmap, input->inode_bitmap - start);
293 if (IS_ERR(bh = bclean(handle, sb, input->inode_bitmap))) {
294 err = PTR_ERR(bh);
295 goto exit_journal;
296 }
297
Aneesh Kumar K.V648f5872009-01-05 21:46:04 -0500298 mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700299 bh->b_data);
Frank Mayhar03901312009-01-07 00:06:22 -0500300 ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700301exit_bh:
302 brelse(bh);
303
304exit_journal:
Theodore Ts'o32ed5052009-04-25 22:53:39 -0400305 mutex_unlock(&sbi->s_resize_lock);
Mingming Cao617ba132006-10-11 01:20:53 -0700306 if ((err2 = ext4_journal_stop(handle)) && !err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700307 err = err2;
308
309 return err;
310}
311
312/*
313 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
Mingming Cao617ba132006-10-11 01:20:53 -0700314 * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700315 * calling this for the first time. In a sparse filesystem it will be the
316 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
317 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
318 */
Mingming Cao617ba132006-10-11 01:20:53 -0700319static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700320 unsigned *five, unsigned *seven)
321{
322 unsigned *min = three;
323 int mult = 3;
324 unsigned ret;
325
Mingming Cao617ba132006-10-11 01:20:53 -0700326 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
327 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700328 ret = *min;
329 *min += 1;
330 return ret;
331 }
332
333 if (*five < *min) {
334 min = five;
335 mult = 5;
336 }
337 if (*seven < *min) {
338 min = seven;
339 mult = 7;
340 }
341
342 ret = *min;
343 *min *= mult;
344
345 return ret;
346}
347
348/*
349 * Check that all of the backup GDT blocks are held in the primary GDT block.
350 * It is assumed that they are stored in group order. Returns the number of
351 * groups in current filesystem that have BACKUPS, or -ve error code.
352 */
353static int verify_reserved_gdb(struct super_block *sb,
354 struct buffer_head *primary)
355{
Mingming Cao617ba132006-10-11 01:20:53 -0700356 const ext4_fsblk_t blk = primary->b_blocknr;
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500357 const ext4_group_t end = EXT4_SB(sb)->s_groups_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700358 unsigned three = 1;
359 unsigned five = 5;
360 unsigned seven = 7;
361 unsigned grp;
362 __le32 *p = (__le32 *)primary->b_data;
363 int gdbackups = 0;
364
Mingming Cao617ba132006-10-11 01:20:53 -0700365 while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700366 if (le32_to_cpu(*p++) !=
367 grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
Harvey Harrison46e665e2008-04-17 10:38:59 -0400368 ext4_warning(sb, __func__,
Mingming Cao2ae02102006-10-11 01:21:11 -0700369 "reserved GDT %llu"
370 " missing grp %d (%llu)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700371 blk, grp,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700372 grp *
373 (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
374 blk);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700375 return -EINVAL;
376 }
Mingming Cao617ba132006-10-11 01:20:53 -0700377 if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700378 return -EFBIG;
379 }
380
381 return gdbackups;
382}
383
384/*
385 * Called when we need to bring a reserved group descriptor table block into
386 * use from the resize inode. The primary copy of the new GDT block currently
387 * is an indirect block (under the double indirect block in the resize inode).
388 * The new backup GDT blocks will be stored as leaf blocks in this indirect
389 * block, in group order. Even though we know all the block numbers we need,
390 * we check to ensure that the resize inode has actually reserved these blocks.
391 *
392 * Don't need to update the block bitmaps because the blocks are still in use.
393 *
394 * We get all of the error cases out of the way, so that we are sure to not
395 * fail once we start modifying the data on disk, because JBD has no rollback.
396 */
397static int add_new_gdb(handle_t *handle, struct inode *inode,
Mingming Cao617ba132006-10-11 01:20:53 -0700398 struct ext4_new_group_data *input,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700399 struct buffer_head **primary)
400{
401 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -0700402 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
403 unsigned long gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
404 ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700405 struct buffer_head **o_group_desc, **n_group_desc;
406 struct buffer_head *dind;
407 int gdbackups;
Mingming Cao617ba132006-10-11 01:20:53 -0700408 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700409 __le32 *data;
410 int err;
411
412 if (test_opt(sb, DEBUG))
413 printk(KERN_DEBUG
Mingming Cao617ba132006-10-11 01:20:53 -0700414 "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700415 gdb_num);
416
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400417 /*
418 * If we are not using the primary superblock/GDT copy don't resize,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400419 * because the user tools have no way of handling this. Probably a
420 * bad time to do it anyways.
421 */
Mingming Cao617ba132006-10-11 01:20:53 -0700422 if (EXT4_SB(sb)->s_sbh->b_blocknr !=
423 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400424 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700425 "won't resize using backup superblock at %llu",
Mingming Cao617ba132006-10-11 01:20:53 -0700426 (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700427 return -EPERM;
428 }
429
430 *primary = sb_bread(sb, gdblock);
431 if (!*primary)
432 return -EIO;
433
434 if ((gdbackups = verify_reserved_gdb(sb, *primary)) < 0) {
435 err = gdbackups;
436 goto exit_bh;
437 }
438
Mingming Cao617ba132006-10-11 01:20:53 -0700439 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700440 dind = sb_bread(sb, le32_to_cpu(*data));
441 if (!dind) {
442 err = -EIO;
443 goto exit_bh;
444 }
445
446 data = (__le32 *)dind->b_data;
Mingming Cao617ba132006-10-11 01:20:53 -0700447 if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400448 ext4_warning(sb, __func__,
Mingming Cao2ae02102006-10-11 01:21:11 -0700449 "new group %u GDT block %llu not reserved",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700450 input->group, gdblock);
451 err = -EINVAL;
452 goto exit_dind;
453 }
454
Mingming Cao617ba132006-10-11 01:20:53 -0700455 if ((err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700456 goto exit_dind;
457
Mingming Cao617ba132006-10-11 01:20:53 -0700458 if ((err = ext4_journal_get_write_access(handle, *primary)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700459 goto exit_sbh;
460
Mingming Cao617ba132006-10-11 01:20:53 -0700461 if ((err = ext4_journal_get_write_access(handle, dind)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700462 goto exit_primary;
463
Mingming Cao617ba132006-10-11 01:20:53 -0700464 /* ext4_reserve_inode_write() gets a reference on the iloc */
465 if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700466 goto exit_dindj;
467
468 n_group_desc = kmalloc((gdb_num + 1) * sizeof(struct buffer_head *),
Josef Bacik216553c2008-04-29 22:02:02 -0400469 GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700470 if (!n_group_desc) {
471 err = -ENOMEM;
Harvey Harrison46e665e2008-04-17 10:38:59 -0400472 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700473 "not enough memory for %lu groups", gdb_num + 1);
474 goto exit_inode;
475 }
476
477 /*
478 * Finally, we have all of the possible failures behind us...
479 *
480 * Remove new GDT block from inode double-indirect block and clear out
481 * the new GDT block for use (which also "frees" the backup GDT blocks
482 * from the reserved inode). We don't need to change the bitmaps for
483 * these blocks, because they are marked as in-use from being in the
484 * reserved inode, and will become GDT blocks (primary and backup).
485 */
Mingming Cao617ba132006-10-11 01:20:53 -0700486 data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
Frank Mayhar03901312009-01-07 00:06:22 -0500487 ext4_handle_dirty_metadata(handle, NULL, dind);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700488 brelse(dind);
489 inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
Mingming Cao617ba132006-10-11 01:20:53 -0700490 ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700491 memset((*primary)->b_data, 0, sb->s_blocksize);
Frank Mayhar03901312009-01-07 00:06:22 -0500492 ext4_handle_dirty_metadata(handle, NULL, *primary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700493
Mingming Cao617ba132006-10-11 01:20:53 -0700494 o_group_desc = EXT4_SB(sb)->s_group_desc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700495 memcpy(n_group_desc, o_group_desc,
Mingming Cao617ba132006-10-11 01:20:53 -0700496 EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700497 n_group_desc[gdb_num] = *primary;
Mingming Cao617ba132006-10-11 01:20:53 -0700498 EXT4_SB(sb)->s_group_desc = n_group_desc;
499 EXT4_SB(sb)->s_gdb_count++;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500 kfree(o_group_desc);
501
Marcin Slusarze8546d02008-04-17 10:38:59 -0400502 le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
Frank Mayhar03901312009-01-07 00:06:22 -0500503 ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700504
505 return 0;
506
507exit_inode:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400508 /* ext4_journal_release_buffer(handle, iloc.bh); */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700509 brelse(iloc.bh);
510exit_dindj:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400511 /* ext4_journal_release_buffer(handle, dind); */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700512exit_primary:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400513 /* ext4_journal_release_buffer(handle, *primary); */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700514exit_sbh:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400515 /* ext4_journal_release_buffer(handle, *primary); */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700516exit_dind:
517 brelse(dind);
518exit_bh:
519 brelse(*primary);
520
Mingming Cao617ba132006-10-11 01:20:53 -0700521 ext4_debug("leaving with error %d\n", err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700522 return err;
523}
524
525/*
526 * Called when we are adding a new group which has a backup copy of each of
527 * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
528 * We need to add these reserved backup GDT blocks to the resize inode, so
529 * that they are kept for future resizing and not allocated to files.
530 *
531 * Each reserved backup GDT block will go into a different indirect block.
532 * The indirect blocks are actually the primary reserved GDT blocks,
533 * so we know in advance what their block numbers are. We only get the
534 * double-indirect block to verify it is pointing to the primary reserved
535 * GDT blocks so we don't overwrite a data block by accident. The reserved
536 * backup GDT blocks are stored in their reserved primary GDT block.
537 */
538static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
Mingming Cao617ba132006-10-11 01:20:53 -0700539 struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700540{
541 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -0700542 int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700543 struct buffer_head **primary;
544 struct buffer_head *dind;
Mingming Cao617ba132006-10-11 01:20:53 -0700545 struct ext4_iloc iloc;
546 ext4_fsblk_t blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700547 __le32 *data, *end;
548 int gdbackups = 0;
549 int res, i;
550 int err;
551
Josef Bacik216553c2008-04-29 22:02:02 -0400552 primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700553 if (!primary)
554 return -ENOMEM;
555
Mingming Cao617ba132006-10-11 01:20:53 -0700556 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700557 dind = sb_bread(sb, le32_to_cpu(*data));
558 if (!dind) {
559 err = -EIO;
560 goto exit_free;
561 }
562
Mingming Cao617ba132006-10-11 01:20:53 -0700563 blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count;
Josef Bacik94460092008-06-06 18:05:52 -0400564 data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count %
565 EXT4_ADDR_PER_BLOCK(sb));
Mingming Cao617ba132006-10-11 01:20:53 -0700566 end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700567
568 /* Get each reserved primary GDT block and verify it holds backups */
569 for (res = 0; res < reserved_gdb; res++, blk++) {
570 if (le32_to_cpu(*data) != blk) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400571 ext4_warning(sb, __func__,
Mingming Cao2ae02102006-10-11 01:21:11 -0700572 "reserved block %llu"
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700573 " not at offset %ld",
574 blk,
575 (long)(data - (__le32 *)dind->b_data));
576 err = -EINVAL;
577 goto exit_bh;
578 }
579 primary[res] = sb_bread(sb, blk);
580 if (!primary[res]) {
581 err = -EIO;
582 goto exit_bh;
583 }
584 if ((gdbackups = verify_reserved_gdb(sb, primary[res])) < 0) {
585 brelse(primary[res]);
586 err = gdbackups;
587 goto exit_bh;
588 }
589 if (++data >= end)
590 data = (__le32 *)dind->b_data;
591 }
592
593 for (i = 0; i < reserved_gdb; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -0700594 if ((err = ext4_journal_get_write_access(handle, primary[i]))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700595 /*
596 int j;
597 for (j = 0; j < i; j++)
Mingming Cao617ba132006-10-11 01:20:53 -0700598 ext4_journal_release_buffer(handle, primary[j]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700599 */
600 goto exit_bh;
601 }
602 }
603
Mingming Cao617ba132006-10-11 01:20:53 -0700604 if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700605 goto exit_bh;
606
607 /*
608 * Finally we can add each of the reserved backup GDT blocks from
609 * the new group to its reserved primary GDT block.
610 */
Mingming Cao617ba132006-10-11 01:20:53 -0700611 blk = input->group * EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700612 for (i = 0; i < reserved_gdb; i++) {
613 int err2;
614 data = (__le32 *)primary[i]->b_data;
615 /* printk("reserving backup %lu[%u] = %lu\n",
616 primary[i]->b_blocknr, gdbackups,
617 blk + primary[i]->b_blocknr); */
618 data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
Frank Mayhar03901312009-01-07 00:06:22 -0500619 err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700620 if (!err)
621 err = err2;
622 }
623 inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
Mingming Cao617ba132006-10-11 01:20:53 -0700624 ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700625
626exit_bh:
627 while (--res >= 0)
628 brelse(primary[res]);
629 brelse(dind);
630
631exit_free:
632 kfree(primary);
633
634 return err;
635}
636
637/*
Mingming Cao617ba132006-10-11 01:20:53 -0700638 * Update the backup copies of the ext4 metadata. These don't need to be part
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700639 * of the main resize transaction, because e2fsck will re-write them if there
640 * is a problem (basically only OOM will cause a problem). However, we
641 * _should_ update the backups if possible, in case the primary gets trashed
642 * for some reason and we need to run e2fsck from a backup superblock. The
643 * important part is that the new block and inode counts are in the backup
644 * superblocks, and the location of the new group metadata in the GDT backups.
645 *
Theodore Ts'o32ed5052009-04-25 22:53:39 -0400646 * We do not need take the s_resize_lock for this, because these
647 * blocks are not otherwise touched by the filesystem code when it is
648 * mounted. We don't need to worry about last changing from
649 * sbi->s_groups_count, because the worst that can happen is that we
650 * do not copy the full number of backups at this time. The resize
651 * which changed s_groups_count will backup again.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700652 */
653static void update_backups(struct super_block *sb,
654 int blk_off, char *data, int size)
655{
Mingming Cao617ba132006-10-11 01:20:53 -0700656 struct ext4_sb_info *sbi = EXT4_SB(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500657 const ext4_group_t last = sbi->s_groups_count;
Mingming Cao617ba132006-10-11 01:20:53 -0700658 const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700659 unsigned three = 1;
660 unsigned five = 5;
661 unsigned seven = 7;
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500662 ext4_group_t group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700663 int rest = sb->s_blocksize - size;
664 handle_t *handle;
665 int err = 0, err2;
666
Mingming Cao617ba132006-10-11 01:20:53 -0700667 handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700668 if (IS_ERR(handle)) {
669 group = 1;
670 err = PTR_ERR(handle);
671 goto exit_err;
672 }
673
Mingming Cao617ba132006-10-11 01:20:53 -0700674 while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700675 struct buffer_head *bh;
676
677 /* Out of journal space, and can't get more - abort - so sad */
Frank Mayhar03901312009-01-07 00:06:22 -0500678 if (ext4_handle_valid(handle) &&
679 handle->h_buffer_credits == 0 &&
Mingming Cao617ba132006-10-11 01:20:53 -0700680 ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) &&
681 (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700682 break;
683
684 bh = sb_getblk(sb, group * bpg + blk_off);
685 if (!bh) {
686 err = -EIO;
687 break;
688 }
Mingming Cao617ba132006-10-11 01:20:53 -0700689 ext4_debug("update metadata backup %#04lx\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700690 (unsigned long)bh->b_blocknr);
Mingming Cao617ba132006-10-11 01:20:53 -0700691 if ((err = ext4_journal_get_write_access(handle, bh)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700692 break;
693 lock_buffer(bh);
694 memcpy(bh->b_data, data, size);
695 if (rest)
696 memset(bh->b_data + size, 0, rest);
697 set_buffer_uptodate(bh);
698 unlock_buffer(bh);
Frank Mayhar03901312009-01-07 00:06:22 -0500699 ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700700 brelse(bh);
701 }
Mingming Cao617ba132006-10-11 01:20:53 -0700702 if ((err2 = ext4_journal_stop(handle)) && !err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700703 err = err2;
704
705 /*
706 * Ugh! Need to have e2fsck write the backup copies. It is too
707 * late to revert the resize, we shouldn't fail just because of
708 * the backup copies (they are only needed in case of corruption).
709 *
710 * However, if we got here we have a journal problem too, so we
711 * can't really start a transaction to mark the superblock.
712 * Chicken out and just set the flag on the hope it will be written
713 * to disk, and if not - we will simply wait until next fsck.
714 */
715exit_err:
716 if (err) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400717 ext4_warning(sb, __func__,
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500718 "can't update backup for group %u (err %d), "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700719 "forcing fsck on next reboot", group, err);
Mingming Cao617ba132006-10-11 01:20:53 -0700720 sbi->s_mount_state &= ~EXT4_VALID_FS;
721 sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700722 mark_buffer_dirty(sbi->s_sbh);
723 }
724}
725
726/* Add group descriptor data to an existing or new group descriptor block.
727 * Ensure we handle all possible error conditions _before_ we start modifying
728 * the filesystem, because we cannot abort the transaction and not have it
729 * write the data to disk.
730 *
731 * If we are on a GDT block boundary, we need to get the reserved GDT block.
732 * Otherwise, we may need to add backup GDT blocks for a sparse group.
733 *
734 * We only need to hold the superblock lock while we are actually adding
735 * in the new group's counts to the superblock. Prior to that we have
736 * not really "added" the group at all. We re-check that we are still
737 * adding in the last group in case things have changed since verifying.
738 */
Mingming Cao617ba132006-10-11 01:20:53 -0700739int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700740{
Mingming Cao617ba132006-10-11 01:20:53 -0700741 struct ext4_sb_info *sbi = EXT4_SB(sb);
742 struct ext4_super_block *es = sbi->s_es;
743 int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700744 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
745 struct buffer_head *primary = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700746 struct ext4_group_desc *gdp;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700747 struct inode *inode = NULL;
748 handle_t *handle;
749 int gdb_off, gdb_num;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500750 int num_grp_locked = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700751 int err, err2;
752
Mingming Cao617ba132006-10-11 01:20:53 -0700753 gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
754 gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700755
Mingming Cao617ba132006-10-11 01:20:53 -0700756 if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb,
757 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400758 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700759 "Can't resize non-sparse filesystem further");
760 return -EPERM;
761 }
762
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700763 if (ext4_blocks_count(es) + input->blocks_count <
764 ext4_blocks_count(es)) {
Theodore Ts'ofde4d952009-01-05 22:17:35 -0500765 ext4_warning(sb, __func__, "blocks_count overflow");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700766 return -EINVAL;
767 }
768
Mingming Cao617ba132006-10-11 01:20:53 -0700769 if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700770 le32_to_cpu(es->s_inodes_count)) {
Theodore Ts'ofde4d952009-01-05 22:17:35 -0500771 ext4_warning(sb, __func__, "inodes_count overflow");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700772 return -EINVAL;
773 }
774
775 if (reserved_gdb || gdb_off == 0) {
Mingming Cao617ba132006-10-11 01:20:53 -0700776 if (!EXT4_HAS_COMPAT_FEATURE(sb,
Josef Bacik37609fd2008-08-19 22:13:41 -0400777 EXT4_FEATURE_COMPAT_RESIZE_INODE)
778 || !le16_to_cpu(es->s_reserved_gdt_blocks)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400779 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700780 "No reserved GDT blocks, can't resize");
781 return -EPERM;
782 }
David Howells1d1fe1e2008-02-07 00:15:37 -0800783 inode = ext4_iget(sb, EXT4_RESIZE_INO);
784 if (IS_ERR(inode)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400785 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700786 "Error opening resize inode");
David Howells1d1fe1e2008-02-07 00:15:37 -0800787 return PTR_ERR(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700788 }
789 }
790
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500791
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700792 if ((err = verify_group_input(sb, input)))
793 goto exit_put;
794
795 if ((err = setup_new_group_blocks(sb, input)))
796 goto exit_put;
797
798 /*
799 * We will always be modifying at least the superblock and a GDT
800 * block. If we are adding a group past the last current GDT block,
801 * we will also modify the inode and the dindirect block. If we
802 * are adding a group with superblock/GDT backups we will also
803 * modify each of the reserved GDT dindirect blocks.
804 */
Mingming Cao617ba132006-10-11 01:20:53 -0700805 handle = ext4_journal_start_sb(sb,
806 ext4_bg_has_super(sb, input->group) ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700807 3 + reserved_gdb : 4);
808 if (IS_ERR(handle)) {
809 err = PTR_ERR(handle);
810 goto exit_put;
811 }
812
Theodore Ts'o32ed5052009-04-25 22:53:39 -0400813 mutex_lock(&sbi->s_resize_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700814 if (input->group != sbi->s_groups_count) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400815 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700816 "multiple resizers run on filesystem!");
817 err = -EBUSY;
818 goto exit_journal;
819 }
820
Mingming Cao617ba132006-10-11 01:20:53 -0700821 if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700822 goto exit_journal;
823
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400824 /*
825 * We will only either add reserved group blocks to a backup group
826 * or remove reserved blocks for the first group in a new group block.
827 * Doing both would be mean more complex code, and sane people don't
828 * use non-sparse filesystems anymore. This is already checked above.
829 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700830 if (gdb_off) {
831 primary = sbi->s_group_desc[gdb_num];
Mingming Cao617ba132006-10-11 01:20:53 -0700832 if ((err = ext4_journal_get_write_access(handle, primary)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700833 goto exit_journal;
834
Mingming Cao617ba132006-10-11 01:20:53 -0700835 if (reserved_gdb && ext4_bg_num_gdb(sb, input->group) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700836 (err = reserve_backup_gdb(handle, inode, input)))
837 goto exit_journal;
838 } else if ((err = add_new_gdb(handle, inode, input, &primary)))
839 goto exit_journal;
840
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400841 /*
842 * OK, now we've set up the new group. Time to make it active.
843 *
Theodore Ts'o32ed5052009-04-25 22:53:39 -0400844 * We do not lock all allocations via s_resize_lock
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400845 * so we have to be safe wrt. concurrent accesses the group
846 * data. So we need to be careful to set all of the relevant
847 * group descriptor data etc. *before* we enable the group.
848 *
849 * The key field here is sbi->s_groups_count: as long as
850 * that retains its old value, nobody is going to access the new
851 * group.
852 *
853 * So first we update all the descriptor metadata for the new
854 * group; then we update the total disk blocks count; then we
855 * update the groups count to enable the group; then finally we
856 * update the free space counts so that the system can start
857 * using the new disk blocks.
858 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700859
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500860 num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, input->group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700861 /* Update group descriptor block for new group */
Frederic Bohe28569222008-06-20 11:48:48 -0400862 gdp = (struct ext4_group_desc *)((char *)primary->b_data +
863 gdb_off * EXT4_DESC_SIZE(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700864
Theodore Ts'ofdff73f2009-01-26 19:06:41 -0500865 memset(gdp, 0, EXT4_DESC_SIZE(sb));
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700866 ext4_block_bitmap_set(sb, gdp, input->block_bitmap); /* LV FIXME */
867 ext4_inode_bitmap_set(sb, gdp, input->inode_bitmap); /* LV FIXME */
868 ext4_inode_table_set(sb, gdp, input->inode_table); /* LV FIXME */
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500869 ext4_free_blks_set(sb, gdp, input->free_blocks_count);
870 ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
Theodore Ts'ofdff73f2009-01-26 19:06:41 -0500871 gdp->bg_flags = cpu_to_le16(EXT4_BG_INODE_ZEROED);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400872 gdp->bg_checksum = ext4_group_desc_csum(sbi, input->group, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700873
874 /*
Frederic Bohe5f21b0e2008-07-11 19:27:31 -0400875 * We can allocate memory for mb_alloc based on the new group
876 * descriptor
877 */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500878 err = ext4_mb_add_groupinfo(sb, input->group, gdp);
879 if (err) {
880 ext4_mb_put_buddy_cache_lock(sb, input->group, num_grp_locked);
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400881 goto exit_journal;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500882 }
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400883
Frederic Bohe5f21b0e2008-07-11 19:27:31 -0400884 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700885 * Make the new blocks and inodes valid next. We do this before
886 * increasing the group count so that once the group is enabled,
887 * all of its blocks and inodes are already valid.
888 *
889 * We always allocate group-by-group, then block-by-block or
890 * inode-by-inode within a group, so enabling these
891 * blocks/inodes before the group is live won't actually let us
892 * allocate the new space yet.
893 */
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700894 ext4_blocks_count_set(es, ext4_blocks_count(es) +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700895 input->blocks_count);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400896 le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700897
898 /*
899 * We need to protect s_groups_count against other CPUs seeing
900 * inconsistent state in the superblock.
901 *
902 * The precise rules we use are:
903 *
Theodore Ts'o32ed5052009-04-25 22:53:39 -0400904 * * Writers of s_groups_count *must* hold s_resize_lock
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700905 * AND
906 * * Writers must perform a smp_wmb() after updating all dependent
907 * data and before modifying the groups count
908 *
Theodore Ts'o32ed5052009-04-25 22:53:39 -0400909 * * Readers must hold s_resize_lock over the access
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700910 * OR
911 * * Readers must perform an smp_rmb() after reading the groups count
912 * and before reading any dependent data.
913 *
914 * NB. These rules can be relaxed when checking the group count
915 * while freeing data, as we can only allocate from a block
916 * group after serialising against the group count, and we can
917 * only then free after serialising in turn against that
918 * allocation.
919 */
920 smp_wmb();
921
922 /* Update the global fs size fields */
923 sbi->s_groups_count++;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500924 ext4_mb_put_buddy_cache_lock(sb, input->group, num_grp_locked);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700925
Frank Mayhar03901312009-01-07 00:06:22 -0500926 ext4_handle_dirty_metadata(handle, NULL, primary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700927
928 /* Update the reserved block counts only once the new group is
929 * active. */
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700930 ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700931 input->reserved_blocks);
932
933 /* Update the free space counts */
Peter Zijlstraaa0dff22007-10-16 23:25:42 -0700934 percpu_counter_add(&sbi->s_freeblocks_counter,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700935 input->free_blocks_count);
Peter Zijlstraaa0dff22007-10-16 23:25:42 -0700936 percpu_counter_add(&sbi->s_freeinodes_counter,
Mingming Cao617ba132006-10-11 01:20:53 -0700937 EXT4_INODES_PER_GROUP(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700938
Frederic Bohec62a11f2008-09-08 10:20:24 -0400939 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
940 ext4_group_t flex_group;
941 flex_group = ext4_flex_group(sbi, input->group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -0500942 atomic_add(input->free_blocks_count,
943 &sbi->s_flex_groups[flex_group].free_blocks);
944 atomic_add(EXT4_INODES_PER_GROUP(sb),
945 &sbi->s_flex_groups[flex_group].free_inodes);
Frederic Bohec62a11f2008-09-08 10:20:24 -0400946 }
947
Frank Mayhar03901312009-01-07 00:06:22 -0500948 ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700949 sb->s_dirt = 1;
950
951exit_journal:
Theodore Ts'o32ed5052009-04-25 22:53:39 -0400952 mutex_unlock(&sbi->s_resize_lock);
Mingming Cao617ba132006-10-11 01:20:53 -0700953 if ((err2 = ext4_journal_stop(handle)) && !err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700954 err = err2;
955 if (!err) {
956 update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
Mingming Cao617ba132006-10-11 01:20:53 -0700957 sizeof(struct ext4_super_block));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700958 update_backups(sb, primary->b_blocknr, primary->b_data,
959 primary->b_size);
960 }
961exit_put:
962 iput(inode);
963 return err;
Mingming Cao617ba132006-10-11 01:20:53 -0700964} /* ext4_group_add */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700965
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400966/*
967 * Extend the filesystem to the new number of blocks specified. This entry
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700968 * point is only used to extend the current filesystem to the end of the last
969 * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
970 * for emergencies (because it has no dependencies on reserved blocks).
971 *
Mingming Cao617ba132006-10-11 01:20:53 -0700972 * If we _really_ wanted, we could use default values to call ext4_group_add()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700973 * allow the "remount" trick to work for arbitrary resizing, assuming enough
974 * GDT blocks are reserved to grow to the desired size.
975 */
Mingming Cao617ba132006-10-11 01:20:53 -0700976int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
977 ext4_fsblk_t n_blocks_count)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700978{
Mingming Cao617ba132006-10-11 01:20:53 -0700979 ext4_fsblk_t o_blocks_count;
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500980 ext4_group_t o_groups_count;
Mingming Cao617ba132006-10-11 01:20:53 -0700981 ext4_grpblk_t last;
982 ext4_grpblk_t add;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400983 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700984 handle_t *handle;
985 int err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -0400986 ext4_group_t group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700987
988 /* We don't need to worry about locking wrt other resizers just
989 * yet: we're going to revalidate es->s_blocks_count after
Theodore Ts'o32ed5052009-04-25 22:53:39 -0400990 * taking the s_resize_lock below. */
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700991 o_blocks_count = ext4_blocks_count(es);
Mingming Cao617ba132006-10-11 01:20:53 -0700992 o_groups_count = EXT4_SB(sb)->s_groups_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993
994 if (test_opt(sb, DEBUG))
Mingming Cao2ae02102006-10-11 01:21:11 -0700995 printk(KERN_DEBUG "EXT4-fs: extending last group from %llu uto %llu blocks\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700996 o_blocks_count, n_blocks_count);
997
998 if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
999 return 0;
1000
1001 if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001002 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
Mingming Cao2ae02102006-10-11 01:21:11 -07001003 " too large to resize to %llu blocks safely\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001004 sb->s_id, n_blocks_count);
1005 if (sizeof(sector_t) < 8)
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001006 ext4_warning(sb, __func__, "CONFIG_LBD not enabled");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001007 return -EINVAL;
1008 }
1009
1010 if (n_blocks_count < o_blocks_count) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04001011 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001012 "can't shrink FS - resize aborted");
1013 return -EBUSY;
1014 }
1015
1016 /* Handle the remaining blocks in the last group only. */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04001017 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001018
1019 if (last == 0) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04001020 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001021 "need to use ext2online to resize further");
1022 return -EPERM;
1023 }
1024
Mingming Cao617ba132006-10-11 01:20:53 -07001025 add = EXT4_BLOCKS_PER_GROUP(sb) - last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001026
1027 if (o_blocks_count + add < o_blocks_count) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04001028 ext4_warning(sb, __func__, "blocks_count overflow");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001029 return -EINVAL;
1030 }
1031
1032 if (o_blocks_count + add > n_blocks_count)
1033 add = n_blocks_count - o_blocks_count;
1034
1035 if (o_blocks_count + add < n_blocks_count)
Harvey Harrison46e665e2008-04-17 10:38:59 -04001036 ext4_warning(sb, __func__,
Mingming Cao2ae02102006-10-11 01:21:11 -07001037 "will only finish group (%llu"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001038 " blocks, %u new)",
1039 o_blocks_count + add, add);
1040
1041 /* See if the device is actually as big as what was requested */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001042 bh = sb_bread(sb, o_blocks_count + add - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001043 if (!bh) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04001044 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001045 "can't read last block, resize aborted");
1046 return -ENOSPC;
1047 }
1048 brelse(bh);
1049
1050 /* We will update the superblock, one block bitmap, and
Mingming Cao617ba132006-10-11 01:20:53 -07001051 * one group descriptor via ext4_free_blocks().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001052 */
Mingming Cao617ba132006-10-11 01:20:53 -07001053 handle = ext4_journal_start_sb(sb, 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001054 if (IS_ERR(handle)) {
1055 err = PTR_ERR(handle);
Harvey Harrison46e665e2008-04-17 10:38:59 -04001056 ext4_warning(sb, __func__, "error %d on journal start", err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001057 goto exit_put;
1058 }
1059
Theodore Ts'o32ed5052009-04-25 22:53:39 -04001060 mutex_lock(&EXT4_SB(sb)->s_resize_lock);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001061 if (o_blocks_count != ext4_blocks_count(es)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04001062 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001063 "multiple resizers run on filesystem!");
Theodore Ts'o32ed5052009-04-25 22:53:39 -04001064 mutex_unlock(&EXT4_SB(sb)->s_resize_lock);
Akinobu Mita5606bf52008-02-25 15:37:42 -05001065 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001066 err = -EBUSY;
1067 goto exit_put;
1068 }
1069
Mingming Cao617ba132006-10-11 01:20:53 -07001070 if ((err = ext4_journal_get_write_access(handle,
1071 EXT4_SB(sb)->s_sbh))) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04001072 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001073 "error %d on journal write access", err);
Theodore Ts'o32ed5052009-04-25 22:53:39 -04001074 mutex_unlock(&EXT4_SB(sb)->s_resize_lock);
Mingming Cao617ba132006-10-11 01:20:53 -07001075 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001076 goto exit_put;
1077 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001078 ext4_blocks_count_set(es, o_blocks_count + add);
Frank Mayhar03901312009-01-07 00:06:22 -05001079 ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001080 sb->s_dirt = 1;
Theodore Ts'o32ed5052009-04-25 22:53:39 -04001081 mutex_unlock(&EXT4_SB(sb)->s_resize_lock);
Eric Sandeenc549a952008-01-28 23:58:27 -05001082 ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001083 o_blocks_count + add);
Aneesh Kumar K.Ve21675d2009-01-05 21:36:02 -05001084 /* We add the blocks to the bitmap and set the group need init bit */
1085 ext4_add_groupblocks(handle, sb, o_blocks_count, add);
Mingming Cao2ae02102006-10-11 01:21:11 -07001086 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001087 o_blocks_count + add);
Mingming Cao617ba132006-10-11 01:20:53 -07001088 if ((err = ext4_journal_stop(handle)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001089 goto exit_put;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04001090
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001091 if (test_opt(sb, DEBUG))
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001092 printk(KERN_DEBUG "EXT4-fs: extended group to %llu blocks\n",
1093 ext4_blocks_count(es));
Mingming Cao617ba132006-10-11 01:20:53 -07001094 update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es,
1095 sizeof(struct ext4_super_block));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001096exit_put:
1097 return err;
Mingming Cao617ba132006-10-11 01:20:53 -07001098} /* ext4_group_extend */