blob: aa11d7dbe9700106725f79c682aebda39a97bc9f [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
Mingming Caodab291a2006-10-11 01:21:01 -070014#include <linux/ext4_jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070015
16#include <linux/errno.h>
17#include <linux/slab.h>
18
19
20#define outside(b, first, last) ((b) < (first) || (b) >= (last))
21#define inside(b, first, last) ((b) >= (first) && (b) < (last))
22
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;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070030 unsigned 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)
Mingming Cao617ba132006-10-11 01:20:53 -070052 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070053 "Cannot add at group %u (only %lu groups)",
54 input->group, sbi->s_groups_count);
Mingming Cao3a5b2ec2006-10-11 01:21:05 -070055 else if (offset != 0)
56 ext4_warning(sb, __FUNCTION__, "Last group not full");
Dave Kleikampac27a0e2006-10-11 01:20:50 -070057 else if (input->reserved_blocks > input->blocks_count / 5)
Mingming Cao617ba132006-10-11 01:20:53 -070058 ext4_warning(sb, __FUNCTION__, "Reserved blocks too high (%u)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -070059 input->reserved_blocks);
60 else if (free_blocks_count < 0)
Mingming Cao617ba132006-10-11 01:20:53 -070061 ext4_warning(sb, __FUNCTION__, "Bad blocks count %u",
Dave Kleikampac27a0e2006-10-11 01:20:50 -070062 input->blocks_count);
63 else if (!(bh = sb_bread(sb, end - 1)))
Mingming Cao617ba132006-10-11 01:20:53 -070064 ext4_warning(sb, __FUNCTION__,
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))
Mingming Cao617ba132006-10-11 01:20:53 -070068 ext4_warning(sb, __FUNCTION__,
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))
Mingming Cao617ba132006-10-11 01:20:53 -070072 ext4_warning(sb, __FUNCTION__,
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) ||
76 outside(itend - 1, start, end))
Mingming Cao617ba132006-10-11 01:20:53 -070077 ext4_warning(sb, __FUNCTION__,
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)
Mingming Cao617ba132006-10-11 01:20:53 -070081 ext4_warning(sb, __FUNCTION__,
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))
Mingming Cao617ba132006-10-11 01:20:53 -070085 ext4_warning(sb, __FUNCTION__,
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))
Mingming Cao617ba132006-10-11 01:20:53 -070090 ext4_warning(sb, __FUNCTION__,
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))
Mingming Cao617ba132006-10-11 01:20:53 -070095 ext4_warning(sb, __FUNCTION__,
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))
Mingming Cao617ba132006-10-11 01:20:53 -0700101 ext4_warning(sb, __FUNCTION__,
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) ||
107 inside(itend - 1, start, metaend))
Mingming Cao617ba132006-10-11 01:20:53 -0700108 ext4_warning(sb, __FUNCTION__,
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/*
143 * To avoid calling the atomic setbit hundreds or thousands of times, we only
144 * need to use it within a single byte (to ensure we get endianness right).
145 * We can use memset for the rest of the bitmap as there are no other users.
146 */
147static void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
148{
149 int i;
150
151 if (start_bit >= end_bit)
152 return;
153
Mingming Cao617ba132006-10-11 01:20:53 -0700154 ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700155 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
Mingming Cao617ba132006-10-11 01:20:53 -0700156 ext4_set_bit(i, bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700157 if (i < end_bit)
158 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
159}
160
161/*
162 * Set up the block and inode bitmaps, and the inode table for the new group.
163 * This doesn't need to be part of the main transaction, since we are only
164 * changing blocks outside the actual filesystem. We still do journaling to
165 * ensure the recovery is correct in case of a failure just after resize.
166 * If any part of this fails, we simply abort the resize.
167 */
168static int setup_new_group_blocks(struct super_block *sb,
Mingming Cao617ba132006-10-11 01:20:53 -0700169 struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170{
Mingming Cao617ba132006-10-11 01:20:53 -0700171 struct ext4_sb_info *sbi = EXT4_SB(sb);
172 ext4_fsblk_t start = ext4_group_first_block_no(sb, input->group);
173 int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700175 unsigned long gdblocks = ext4_bg_num_gdb(sb, input->group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700176 struct buffer_head *bh;
177 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -0700178 ext4_fsblk_t block;
179 ext4_grpblk_t bit;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180 int i;
181 int err = 0, err2;
182
Mingming Cao617ba132006-10-11 01:20:53 -0700183 handle = ext4_journal_start_sb(sb, reserved_gdb + gdblocks +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184 2 + sbi->s_itb_per_group);
185 if (IS_ERR(handle))
186 return PTR_ERR(handle);
187
188 lock_super(sb);
189 if (input->group != sbi->s_groups_count) {
190 err = -EBUSY;
191 goto exit_journal;
192 }
193
194 if (IS_ERR(bh = bclean(handle, sb, input->block_bitmap))) {
195 err = PTR_ERR(bh);
196 goto exit_journal;
197 }
198
Mingming Cao617ba132006-10-11 01:20:53 -0700199 if (ext4_bg_has_super(sb, input->group)) {
200 ext4_debug("mark backup superblock %#04lx (+0)\n", start);
201 ext4_set_bit(0, bh->b_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700202 }
203
204 /* Copy all of the GDT blocks into the backup in this group */
205 for (i = 0, bit = 1, block = start + 1;
206 i < gdblocks; i++, block++, bit++) {
207 struct buffer_head *gdb;
208
Mingming Cao617ba132006-10-11 01:20:53 -0700209 ext4_debug("update backup group %#04lx (+%d)\n", block, bit);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700210
211 gdb = sb_getblk(sb, block);
212 if (!gdb) {
213 err = -EIO;
214 goto exit_bh;
215 }
Mingming Cao617ba132006-10-11 01:20:53 -0700216 if ((err = ext4_journal_get_write_access(handle, gdb))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700217 brelse(gdb);
218 goto exit_bh;
219 }
220 lock_buffer(bh);
221 memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, bh->b_size);
222 set_buffer_uptodate(gdb);
223 unlock_buffer(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700224 ext4_journal_dirty_metadata(handle, gdb);
225 ext4_set_bit(bit, bh->b_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700226 brelse(gdb);
227 }
228
229 /* Zero out all of the reserved backup group descriptor table blocks */
230 for (i = 0, bit = gdblocks + 1, block = start + bit;
231 i < reserved_gdb; i++, block++, bit++) {
232 struct buffer_head *gdb;
233
Mingming Cao617ba132006-10-11 01:20:53 -0700234 ext4_debug("clear reserved block %#04lx (+%d)\n", block, bit);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700235
236 if (IS_ERR(gdb = bclean(handle, sb, block))) {
237 err = PTR_ERR(bh);
238 goto exit_bh;
239 }
Mingming Cao617ba132006-10-11 01:20:53 -0700240 ext4_journal_dirty_metadata(handle, gdb);
241 ext4_set_bit(bit, bh->b_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700242 brelse(gdb);
243 }
Mingming Cao617ba132006-10-11 01:20:53 -0700244 ext4_debug("mark block bitmap %#04x (+%ld)\n", input->block_bitmap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700245 input->block_bitmap - start);
Mingming Cao617ba132006-10-11 01:20:53 -0700246 ext4_set_bit(input->block_bitmap - start, bh->b_data);
247 ext4_debug("mark inode bitmap %#04x (+%ld)\n", input->inode_bitmap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700248 input->inode_bitmap - start);
Mingming Cao617ba132006-10-11 01:20:53 -0700249 ext4_set_bit(input->inode_bitmap - start, bh->b_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700250
251 /* Zero out all of the inode table blocks */
252 for (i = 0, block = input->inode_table, bit = block - start;
253 i < sbi->s_itb_per_group; i++, bit++, block++) {
254 struct buffer_head *it;
255
Mingming Cao617ba132006-10-11 01:20:53 -0700256 ext4_debug("clear inode block %#04lx (+%d)\n", block, bit);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700257 if (IS_ERR(it = bclean(handle, sb, block))) {
258 err = PTR_ERR(it);
259 goto exit_bh;
260 }
Mingming Cao617ba132006-10-11 01:20:53 -0700261 ext4_journal_dirty_metadata(handle, it);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700262 brelse(it);
Mingming Cao617ba132006-10-11 01:20:53 -0700263 ext4_set_bit(bit, bh->b_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700264 }
Mingming Cao617ba132006-10-11 01:20:53 -0700265 mark_bitmap_end(input->blocks_count, EXT4_BLOCKS_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266 bh->b_data);
Mingming Cao617ba132006-10-11 01:20:53 -0700267 ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700268 brelse(bh);
269
270 /* Mark unused entries in inode bitmap used */
Mingming Cao617ba132006-10-11 01:20:53 -0700271 ext4_debug("clear inode bitmap %#04x (+%ld)\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700272 input->inode_bitmap, input->inode_bitmap - start);
273 if (IS_ERR(bh = bclean(handle, sb, input->inode_bitmap))) {
274 err = PTR_ERR(bh);
275 goto exit_journal;
276 }
277
Mingming Cao617ba132006-10-11 01:20:53 -0700278 mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700279 bh->b_data);
Mingming Cao617ba132006-10-11 01:20:53 -0700280 ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700281exit_bh:
282 brelse(bh);
283
284exit_journal:
285 unlock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700286 if ((err2 = ext4_journal_stop(handle)) && !err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700287 err = err2;
288
289 return err;
290}
291
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700292
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700293/*
294 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
Mingming Cao617ba132006-10-11 01:20:53 -0700295 * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700296 * calling this for the first time. In a sparse filesystem it will be the
297 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
298 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
299 */
Mingming Cao617ba132006-10-11 01:20:53 -0700300static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700301 unsigned *five, unsigned *seven)
302{
303 unsigned *min = three;
304 int mult = 3;
305 unsigned ret;
306
Mingming Cao617ba132006-10-11 01:20:53 -0700307 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
308 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700309 ret = *min;
310 *min += 1;
311 return ret;
312 }
313
314 if (*five < *min) {
315 min = five;
316 mult = 5;
317 }
318 if (*seven < *min) {
319 min = seven;
320 mult = 7;
321 }
322
323 ret = *min;
324 *min *= mult;
325
326 return ret;
327}
328
329/*
330 * Check that all of the backup GDT blocks are held in the primary GDT block.
331 * It is assumed that they are stored in group order. Returns the number of
332 * groups in current filesystem that have BACKUPS, or -ve error code.
333 */
334static int verify_reserved_gdb(struct super_block *sb,
335 struct buffer_head *primary)
336{
Mingming Cao617ba132006-10-11 01:20:53 -0700337 const ext4_fsblk_t blk = primary->b_blocknr;
338 const unsigned long end = EXT4_SB(sb)->s_groups_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700339 unsigned three = 1;
340 unsigned five = 5;
341 unsigned seven = 7;
342 unsigned grp;
343 __le32 *p = (__le32 *)primary->b_data;
344 int gdbackups = 0;
345
Mingming Cao617ba132006-10-11 01:20:53 -0700346 while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700347 if (le32_to_cpu(*p++) !=
348 grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
Mingming Cao617ba132006-10-11 01:20:53 -0700349 ext4_warning(sb, __FUNCTION__,
Mingming Cao2ae02102006-10-11 01:21:11 -0700350 "reserved GDT %llu"
351 " missing grp %d (%llu)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700352 blk, grp,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700353 grp *
354 (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
355 blk);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700356 return -EINVAL;
357 }
Mingming Cao617ba132006-10-11 01:20:53 -0700358 if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700359 return -EFBIG;
360 }
361
362 return gdbackups;
363}
364
365/*
366 * Called when we need to bring a reserved group descriptor table block into
367 * use from the resize inode. The primary copy of the new GDT block currently
368 * is an indirect block (under the double indirect block in the resize inode).
369 * The new backup GDT blocks will be stored as leaf blocks in this indirect
370 * block, in group order. Even though we know all the block numbers we need,
371 * we check to ensure that the resize inode has actually reserved these blocks.
372 *
373 * Don't need to update the block bitmaps because the blocks are still in use.
374 *
375 * We get all of the error cases out of the way, so that we are sure to not
376 * fail once we start modifying the data on disk, because JBD has no rollback.
377 */
378static int add_new_gdb(handle_t *handle, struct inode *inode,
Mingming Cao617ba132006-10-11 01:20:53 -0700379 struct ext4_new_group_data *input,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700380 struct buffer_head **primary)
381{
382 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -0700383 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
384 unsigned long gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
385 ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700386 struct buffer_head **o_group_desc, **n_group_desc;
387 struct buffer_head *dind;
388 int gdbackups;
Mingming Cao617ba132006-10-11 01:20:53 -0700389 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700390 __le32 *data;
391 int err;
392
393 if (test_opt(sb, DEBUG))
394 printk(KERN_DEBUG
Mingming Cao617ba132006-10-11 01:20:53 -0700395 "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700396 gdb_num);
397
398 /*
399 * If we are not using the primary superblock/GDT copy don't resize,
400 * because the user tools have no way of handling this. Probably a
401 * bad time to do it anyways.
402 */
Mingming Cao617ba132006-10-11 01:20:53 -0700403 if (EXT4_SB(sb)->s_sbh->b_blocknr !=
404 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
405 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700406 "won't resize using backup superblock at %llu",
Mingming Cao617ba132006-10-11 01:20:53 -0700407 (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700408 return -EPERM;
409 }
410
411 *primary = sb_bread(sb, gdblock);
412 if (!*primary)
413 return -EIO;
414
415 if ((gdbackups = verify_reserved_gdb(sb, *primary)) < 0) {
416 err = gdbackups;
417 goto exit_bh;
418 }
419
Mingming Cao617ba132006-10-11 01:20:53 -0700420 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700421 dind = sb_bread(sb, le32_to_cpu(*data));
422 if (!dind) {
423 err = -EIO;
424 goto exit_bh;
425 }
426
427 data = (__le32 *)dind->b_data;
Mingming Cao617ba132006-10-11 01:20:53 -0700428 if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
429 ext4_warning(sb, __FUNCTION__,
Mingming Cao2ae02102006-10-11 01:21:11 -0700430 "new group %u GDT block %llu not reserved",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700431 input->group, gdblock);
432 err = -EINVAL;
433 goto exit_dind;
434 }
435
Mingming Cao617ba132006-10-11 01:20:53 -0700436 if ((err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700437 goto exit_dind;
438
Mingming Cao617ba132006-10-11 01:20:53 -0700439 if ((err = ext4_journal_get_write_access(handle, *primary)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700440 goto exit_sbh;
441
Mingming Cao617ba132006-10-11 01:20:53 -0700442 if ((err = ext4_journal_get_write_access(handle, dind)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700443 goto exit_primary;
444
Mingming Cao617ba132006-10-11 01:20:53 -0700445 /* ext4_reserve_inode_write() gets a reference on the iloc */
446 if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700447 goto exit_dindj;
448
449 n_group_desc = kmalloc((gdb_num + 1) * sizeof(struct buffer_head *),
450 GFP_KERNEL);
451 if (!n_group_desc) {
452 err = -ENOMEM;
Mingming Cao617ba132006-10-11 01:20:53 -0700453 ext4_warning (sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700454 "not enough memory for %lu groups", gdb_num + 1);
455 goto exit_inode;
456 }
457
458 /*
459 * Finally, we have all of the possible failures behind us...
460 *
461 * Remove new GDT block from inode double-indirect block and clear out
462 * the new GDT block for use (which also "frees" the backup GDT blocks
463 * from the reserved inode). We don't need to change the bitmaps for
464 * these blocks, because they are marked as in-use from being in the
465 * reserved inode, and will become GDT blocks (primary and backup).
466 */
Mingming Cao617ba132006-10-11 01:20:53 -0700467 data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
468 ext4_journal_dirty_metadata(handle, dind);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700469 brelse(dind);
470 inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
Mingming Cao617ba132006-10-11 01:20:53 -0700471 ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700472 memset((*primary)->b_data, 0, sb->s_blocksize);
Mingming Cao617ba132006-10-11 01:20:53 -0700473 ext4_journal_dirty_metadata(handle, *primary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700474
Mingming Cao617ba132006-10-11 01:20:53 -0700475 o_group_desc = EXT4_SB(sb)->s_group_desc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700476 memcpy(n_group_desc, o_group_desc,
Mingming Cao617ba132006-10-11 01:20:53 -0700477 EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700478 n_group_desc[gdb_num] = *primary;
Mingming Cao617ba132006-10-11 01:20:53 -0700479 EXT4_SB(sb)->s_group_desc = n_group_desc;
480 EXT4_SB(sb)->s_gdb_count++;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700481 kfree(o_group_desc);
482
483 es->s_reserved_gdt_blocks =
484 cpu_to_le16(le16_to_cpu(es->s_reserved_gdt_blocks) - 1);
Mingming Cao617ba132006-10-11 01:20:53 -0700485 ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700486
487 return 0;
488
489exit_inode:
Mingming Cao617ba132006-10-11 01:20:53 -0700490 //ext4_journal_release_buffer(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700491 brelse(iloc.bh);
492exit_dindj:
Mingming Cao617ba132006-10-11 01:20:53 -0700493 //ext4_journal_release_buffer(handle, dind);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700494exit_primary:
Mingming Cao617ba132006-10-11 01:20:53 -0700495 //ext4_journal_release_buffer(handle, *primary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700496exit_sbh:
Mingming Cao617ba132006-10-11 01:20:53 -0700497 //ext4_journal_release_buffer(handle, *primary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700498exit_dind:
499 brelse(dind);
500exit_bh:
501 brelse(*primary);
502
Mingming Cao617ba132006-10-11 01:20:53 -0700503 ext4_debug("leaving with error %d\n", err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700504 return err;
505}
506
507/*
508 * Called when we are adding a new group which has a backup copy of each of
509 * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
510 * We need to add these reserved backup GDT blocks to the resize inode, so
511 * that they are kept for future resizing and not allocated to files.
512 *
513 * Each reserved backup GDT block will go into a different indirect block.
514 * The indirect blocks are actually the primary reserved GDT blocks,
515 * so we know in advance what their block numbers are. We only get the
516 * double-indirect block to verify it is pointing to the primary reserved
517 * GDT blocks so we don't overwrite a data block by accident. The reserved
518 * backup GDT blocks are stored in their reserved primary GDT block.
519 */
520static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
Mingming Cao617ba132006-10-11 01:20:53 -0700521 struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700522{
523 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -0700524 int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700525 struct buffer_head **primary;
526 struct buffer_head *dind;
Mingming Cao617ba132006-10-11 01:20:53 -0700527 struct ext4_iloc iloc;
528 ext4_fsblk_t blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700529 __le32 *data, *end;
530 int gdbackups = 0;
531 int res, i;
532 int err;
533
534 primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_KERNEL);
535 if (!primary)
536 return -ENOMEM;
537
Mingming Cao617ba132006-10-11 01:20:53 -0700538 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700539 dind = sb_bread(sb, le32_to_cpu(*data));
540 if (!dind) {
541 err = -EIO;
542 goto exit_free;
543 }
544
Mingming Cao617ba132006-10-11 01:20:53 -0700545 blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count;
546 data = (__le32 *)dind->b_data + EXT4_SB(sb)->s_gdb_count;
547 end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700548
549 /* Get each reserved primary GDT block and verify it holds backups */
550 for (res = 0; res < reserved_gdb; res++, blk++) {
551 if (le32_to_cpu(*data) != blk) {
Mingming Cao617ba132006-10-11 01:20:53 -0700552 ext4_warning(sb, __FUNCTION__,
Mingming Cao2ae02102006-10-11 01:21:11 -0700553 "reserved block %llu"
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554 " not at offset %ld",
555 blk,
556 (long)(data - (__le32 *)dind->b_data));
557 err = -EINVAL;
558 goto exit_bh;
559 }
560 primary[res] = sb_bread(sb, blk);
561 if (!primary[res]) {
562 err = -EIO;
563 goto exit_bh;
564 }
565 if ((gdbackups = verify_reserved_gdb(sb, primary[res])) < 0) {
566 brelse(primary[res]);
567 err = gdbackups;
568 goto exit_bh;
569 }
570 if (++data >= end)
571 data = (__le32 *)dind->b_data;
572 }
573
574 for (i = 0; i < reserved_gdb; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -0700575 if ((err = ext4_journal_get_write_access(handle, primary[i]))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700576 /*
577 int j;
578 for (j = 0; j < i; j++)
Mingming Cao617ba132006-10-11 01:20:53 -0700579 ext4_journal_release_buffer(handle, primary[j]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700580 */
581 goto exit_bh;
582 }
583 }
584
Mingming Cao617ba132006-10-11 01:20:53 -0700585 if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700586 goto exit_bh;
587
588 /*
589 * Finally we can add each of the reserved backup GDT blocks from
590 * the new group to its reserved primary GDT block.
591 */
Mingming Cao617ba132006-10-11 01:20:53 -0700592 blk = input->group * EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700593 for (i = 0; i < reserved_gdb; i++) {
594 int err2;
595 data = (__le32 *)primary[i]->b_data;
596 /* printk("reserving backup %lu[%u] = %lu\n",
597 primary[i]->b_blocknr, gdbackups,
598 blk + primary[i]->b_blocknr); */
599 data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
Mingming Cao617ba132006-10-11 01:20:53 -0700600 err2 = ext4_journal_dirty_metadata(handle, primary[i]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700601 if (!err)
602 err = err2;
603 }
604 inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
Mingming Cao617ba132006-10-11 01:20:53 -0700605 ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700606
607exit_bh:
608 while (--res >= 0)
609 brelse(primary[res]);
610 brelse(dind);
611
612exit_free:
613 kfree(primary);
614
615 return err;
616}
617
618/*
Mingming Cao617ba132006-10-11 01:20:53 -0700619 * Update the backup copies of the ext4 metadata. These don't need to be part
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700620 * of the main resize transaction, because e2fsck will re-write them if there
621 * is a problem (basically only OOM will cause a problem). However, we
622 * _should_ update the backups if possible, in case the primary gets trashed
623 * for some reason and we need to run e2fsck from a backup superblock. The
624 * important part is that the new block and inode counts are in the backup
625 * superblocks, and the location of the new group metadata in the GDT backups.
626 *
627 * We do not need lock_super() for this, because these blocks are not
628 * otherwise touched by the filesystem code when it is mounted. We don't
629 * need to worry about last changing from sbi->s_groups_count, because the
630 * worst that can happen is that we do not copy the full number of backups
631 * at this time. The resize which changed s_groups_count will backup again.
632 */
633static void update_backups(struct super_block *sb,
634 int blk_off, char *data, int size)
635{
Mingming Cao617ba132006-10-11 01:20:53 -0700636 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700637 const unsigned long last = sbi->s_groups_count;
Mingming Cao617ba132006-10-11 01:20:53 -0700638 const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700639 unsigned three = 1;
640 unsigned five = 5;
641 unsigned seven = 7;
642 unsigned group;
643 int rest = sb->s_blocksize - size;
644 handle_t *handle;
645 int err = 0, err2;
646
Mingming Cao617ba132006-10-11 01:20:53 -0700647 handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700648 if (IS_ERR(handle)) {
649 group = 1;
650 err = PTR_ERR(handle);
651 goto exit_err;
652 }
653
Mingming Cao617ba132006-10-11 01:20:53 -0700654 while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700655 struct buffer_head *bh;
656
657 /* Out of journal space, and can't get more - abort - so sad */
658 if (handle->h_buffer_credits == 0 &&
Mingming Cao617ba132006-10-11 01:20:53 -0700659 ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) &&
660 (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700661 break;
662
663 bh = sb_getblk(sb, group * bpg + blk_off);
664 if (!bh) {
665 err = -EIO;
666 break;
667 }
Mingming Cao617ba132006-10-11 01:20:53 -0700668 ext4_debug("update metadata backup %#04lx\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700669 (unsigned long)bh->b_blocknr);
Mingming Cao617ba132006-10-11 01:20:53 -0700670 if ((err = ext4_journal_get_write_access(handle, bh)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700671 break;
672 lock_buffer(bh);
673 memcpy(bh->b_data, data, size);
674 if (rest)
675 memset(bh->b_data + size, 0, rest);
676 set_buffer_uptodate(bh);
677 unlock_buffer(bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700678 ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700679 brelse(bh);
680 }
Mingming Cao617ba132006-10-11 01:20:53 -0700681 if ((err2 = ext4_journal_stop(handle)) && !err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700682 err = err2;
683
684 /*
685 * Ugh! Need to have e2fsck write the backup copies. It is too
686 * late to revert the resize, we shouldn't fail just because of
687 * the backup copies (they are only needed in case of corruption).
688 *
689 * However, if we got here we have a journal problem too, so we
690 * can't really start a transaction to mark the superblock.
691 * Chicken out and just set the flag on the hope it will be written
692 * to disk, and if not - we will simply wait until next fsck.
693 */
694exit_err:
695 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -0700696 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700697 "can't update backup for group %d (err %d), "
698 "forcing fsck on next reboot", group, err);
Mingming Cao617ba132006-10-11 01:20:53 -0700699 sbi->s_mount_state &= ~EXT4_VALID_FS;
700 sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700701 mark_buffer_dirty(sbi->s_sbh);
702 }
703}
704
705/* Add group descriptor data to an existing or new group descriptor block.
706 * Ensure we handle all possible error conditions _before_ we start modifying
707 * the filesystem, because we cannot abort the transaction and not have it
708 * write the data to disk.
709 *
710 * If we are on a GDT block boundary, we need to get the reserved GDT block.
711 * Otherwise, we may need to add backup GDT blocks for a sparse group.
712 *
713 * We only need to hold the superblock lock while we are actually adding
714 * in the new group's counts to the superblock. Prior to that we have
715 * not really "added" the group at all. We re-check that we are still
716 * adding in the last group in case things have changed since verifying.
717 */
Mingming Cao617ba132006-10-11 01:20:53 -0700718int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700719{
Mingming Cao617ba132006-10-11 01:20:53 -0700720 struct ext4_sb_info *sbi = EXT4_SB(sb);
721 struct ext4_super_block *es = sbi->s_es;
722 int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700723 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
724 struct buffer_head *primary = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700725 struct ext4_group_desc *gdp;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700726 struct inode *inode = NULL;
727 handle_t *handle;
728 int gdb_off, gdb_num;
729 int err, err2;
730
Mingming Cao617ba132006-10-11 01:20:53 -0700731 gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
732 gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700733
Mingming Cao617ba132006-10-11 01:20:53 -0700734 if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb,
735 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
736 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700737 "Can't resize non-sparse filesystem further");
738 return -EPERM;
739 }
740
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700741 if (ext4_blocks_count(es) + input->blocks_count <
742 ext4_blocks_count(es)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700743 ext4_warning(sb, __FUNCTION__, "blocks_count overflow\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700744 return -EINVAL;
745 }
746
Mingming Cao617ba132006-10-11 01:20:53 -0700747 if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700748 le32_to_cpu(es->s_inodes_count)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700749 ext4_warning(sb, __FUNCTION__, "inodes_count overflow\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700750 return -EINVAL;
751 }
752
753 if (reserved_gdb || gdb_off == 0) {
Mingming Cao617ba132006-10-11 01:20:53 -0700754 if (!EXT4_HAS_COMPAT_FEATURE(sb,
755 EXT4_FEATURE_COMPAT_RESIZE_INODE)){
756 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700757 "No reserved GDT blocks, can't resize");
758 return -EPERM;
759 }
Mingming Cao617ba132006-10-11 01:20:53 -0700760 inode = iget(sb, EXT4_RESIZE_INO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700761 if (!inode || is_bad_inode(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700762 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700763 "Error opening resize inode");
764 iput(inode);
765 return -ENOENT;
766 }
767 }
768
769 if ((err = verify_group_input(sb, input)))
770 goto exit_put;
771
772 if ((err = setup_new_group_blocks(sb, input)))
773 goto exit_put;
774
775 /*
776 * We will always be modifying at least the superblock and a GDT
777 * block. If we are adding a group past the last current GDT block,
778 * we will also modify the inode and the dindirect block. If we
779 * are adding a group with superblock/GDT backups we will also
780 * modify each of the reserved GDT dindirect blocks.
781 */
Mingming Cao617ba132006-10-11 01:20:53 -0700782 handle = ext4_journal_start_sb(sb,
783 ext4_bg_has_super(sb, input->group) ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700784 3 + reserved_gdb : 4);
785 if (IS_ERR(handle)) {
786 err = PTR_ERR(handle);
787 goto exit_put;
788 }
789
790 lock_super(sb);
791 if (input->group != sbi->s_groups_count) {
Mingming Cao617ba132006-10-11 01:20:53 -0700792 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700793 "multiple resizers run on filesystem!");
794 err = -EBUSY;
795 goto exit_journal;
796 }
797
Mingming Cao617ba132006-10-11 01:20:53 -0700798 if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700799 goto exit_journal;
800
801 /*
802 * We will only either add reserved group blocks to a backup group
803 * or remove reserved blocks for the first group in a new group block.
804 * Doing both would be mean more complex code, and sane people don't
805 * use non-sparse filesystems anymore. This is already checked above.
806 */
807 if (gdb_off) {
808 primary = sbi->s_group_desc[gdb_num];
Mingming Cao617ba132006-10-11 01:20:53 -0700809 if ((err = ext4_journal_get_write_access(handle, primary)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700810 goto exit_journal;
811
Mingming Cao617ba132006-10-11 01:20:53 -0700812 if (reserved_gdb && ext4_bg_num_gdb(sb, input->group) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700813 (err = reserve_backup_gdb(handle, inode, input)))
814 goto exit_journal;
815 } else if ((err = add_new_gdb(handle, inode, input, &primary)))
816 goto exit_journal;
817
818 /*
819 * OK, now we've set up the new group. Time to make it active.
820 *
821 * Current kernels don't lock all allocations via lock_super(),
822 * so we have to be safe wrt. concurrent accesses the group
823 * data. So we need to be careful to set all of the relevant
824 * group descriptor data etc. *before* we enable the group.
825 *
826 * The key field here is sbi->s_groups_count: as long as
827 * that retains its old value, nobody is going to access the new
828 * group.
829 *
830 * So first we update all the descriptor metadata for the new
831 * group; then we update the total disk blocks count; then we
832 * update the groups count to enable the group; then finally we
833 * update the free space counts so that the system can start
834 * using the new disk blocks.
835 */
836
837 /* Update group descriptor block for new group */
Mingming Cao617ba132006-10-11 01:20:53 -0700838 gdp = (struct ext4_group_desc *)primary->b_data + gdb_off;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700839
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700840 ext4_block_bitmap_set(sb, gdp, input->block_bitmap); /* LV FIXME */
841 ext4_inode_bitmap_set(sb, gdp, input->inode_bitmap); /* LV FIXME */
842 ext4_inode_table_set(sb, gdp, input->inode_table); /* LV FIXME */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700843 gdp->bg_free_blocks_count = cpu_to_le16(input->free_blocks_count);
Mingming Cao617ba132006-10-11 01:20:53 -0700844 gdp->bg_free_inodes_count = cpu_to_le16(EXT4_INODES_PER_GROUP(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700845
846 /*
847 * Make the new blocks and inodes valid next. We do this before
848 * increasing the group count so that once the group is enabled,
849 * all of its blocks and inodes are already valid.
850 *
851 * We always allocate group-by-group, then block-by-block or
852 * inode-by-inode within a group, so enabling these
853 * blocks/inodes before the group is live won't actually let us
854 * allocate the new space yet.
855 */
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700856 ext4_blocks_count_set(es, ext4_blocks_count(es) +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700857 input->blocks_count);
858 es->s_inodes_count = cpu_to_le32(le32_to_cpu(es->s_inodes_count) +
Mingming Cao617ba132006-10-11 01:20:53 -0700859 EXT4_INODES_PER_GROUP(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700860
861 /*
862 * We need to protect s_groups_count against other CPUs seeing
863 * inconsistent state in the superblock.
864 *
865 * The precise rules we use are:
866 *
867 * * Writers of s_groups_count *must* hold lock_super
868 * AND
869 * * Writers must perform a smp_wmb() after updating all dependent
870 * data and before modifying the groups count
871 *
872 * * Readers must hold lock_super() over the access
873 * OR
874 * * Readers must perform an smp_rmb() after reading the groups count
875 * and before reading any dependent data.
876 *
877 * NB. These rules can be relaxed when checking the group count
878 * while freeing data, as we can only allocate from a block
879 * group after serialising against the group count, and we can
880 * only then free after serialising in turn against that
881 * allocation.
882 */
883 smp_wmb();
884
885 /* Update the global fs size fields */
886 sbi->s_groups_count++;
887
Mingming Cao617ba132006-10-11 01:20:53 -0700888 ext4_journal_dirty_metadata(handle, primary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700889
890 /* Update the reserved block counts only once the new group is
891 * active. */
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700892 ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700893 input->reserved_blocks);
894
895 /* Update the free space counts */
896 percpu_counter_mod(&sbi->s_freeblocks_counter,
897 input->free_blocks_count);
898 percpu_counter_mod(&sbi->s_freeinodes_counter,
Mingming Cao617ba132006-10-11 01:20:53 -0700899 EXT4_INODES_PER_GROUP(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700900
Mingming Cao617ba132006-10-11 01:20:53 -0700901 ext4_journal_dirty_metadata(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700902 sb->s_dirt = 1;
903
904exit_journal:
905 unlock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700906 if ((err2 = ext4_journal_stop(handle)) && !err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700907 err = err2;
908 if (!err) {
909 update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
Mingming Cao617ba132006-10-11 01:20:53 -0700910 sizeof(struct ext4_super_block));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700911 update_backups(sb, primary->b_blocknr, primary->b_data,
912 primary->b_size);
913 }
914exit_put:
915 iput(inode);
916 return err;
Mingming Cao617ba132006-10-11 01:20:53 -0700917} /* ext4_group_add */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700918
919/* Extend the filesystem to the new number of blocks specified. This entry
920 * point is only used to extend the current filesystem to the end of the last
921 * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
922 * for emergencies (because it has no dependencies on reserved blocks).
923 *
Mingming Cao617ba132006-10-11 01:20:53 -0700924 * If we _really_ wanted, we could use default values to call ext4_group_add()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700925 * allow the "remount" trick to work for arbitrary resizing, assuming enough
926 * GDT blocks are reserved to grow to the desired size.
927 */
Mingming Cao617ba132006-10-11 01:20:53 -0700928int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
929 ext4_fsblk_t n_blocks_count)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700930{
Mingming Cao617ba132006-10-11 01:20:53 -0700931 ext4_fsblk_t o_blocks_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700932 unsigned long o_groups_count;
Mingming Cao617ba132006-10-11 01:20:53 -0700933 ext4_grpblk_t last;
934 ext4_grpblk_t add;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700935 struct buffer_head * bh;
936 handle_t *handle;
937 int err;
938 unsigned long freed_blocks;
939
940 /* We don't need to worry about locking wrt other resizers just
941 * yet: we're going to revalidate es->s_blocks_count after
942 * taking lock_super() below. */
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700943 o_blocks_count = ext4_blocks_count(es);
Mingming Cao617ba132006-10-11 01:20:53 -0700944 o_groups_count = EXT4_SB(sb)->s_groups_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700945
946 if (test_opt(sb, DEBUG))
Mingming Cao2ae02102006-10-11 01:21:11 -0700947 printk(KERN_DEBUG "EXT4-fs: extending last group from %llu uto %llu blocks\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700948 o_blocks_count, n_blocks_count);
949
950 if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
951 return 0;
952
953 if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700954 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
Mingming Cao2ae02102006-10-11 01:21:11 -0700955 " too large to resize to %llu blocks safely\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700956 sb->s_id, n_blocks_count);
957 if (sizeof(sector_t) < 8)
Mingming Cao617ba132006-10-11 01:20:53 -0700958 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700959 "CONFIG_LBD not enabled\n");
960 return -EINVAL;
961 }
962
963 if (n_blocks_count < o_blocks_count) {
Mingming Cao617ba132006-10-11 01:20:53 -0700964 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700965 "can't shrink FS - resize aborted");
966 return -EBUSY;
967 }
968
969 /* Handle the remaining blocks in the last group only. */
Mingming Cao3a5b2ec2006-10-11 01:21:05 -0700970 ext4_get_group_no_and_offset(sb, o_blocks_count, NULL, &last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700971
972 if (last == 0) {
Mingming Cao617ba132006-10-11 01:20:53 -0700973 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700974 "need to use ext2online to resize further");
975 return -EPERM;
976 }
977
Mingming Cao617ba132006-10-11 01:20:53 -0700978 add = EXT4_BLOCKS_PER_GROUP(sb) - last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700979
980 if (o_blocks_count + add < o_blocks_count) {
Mingming Cao617ba132006-10-11 01:20:53 -0700981 ext4_warning(sb, __FUNCTION__, "blocks_count overflow");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700982 return -EINVAL;
983 }
984
985 if (o_blocks_count + add > n_blocks_count)
986 add = n_blocks_count - o_blocks_count;
987
988 if (o_blocks_count + add < n_blocks_count)
Mingming Cao617ba132006-10-11 01:20:53 -0700989 ext4_warning(sb, __FUNCTION__,
Mingming Cao2ae02102006-10-11 01:21:11 -0700990 "will only finish group (%llu"
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700991 " blocks, %u new)",
992 o_blocks_count + add, add);
993
994 /* See if the device is actually as big as what was requested */
995 bh = sb_bread(sb, o_blocks_count + add -1);
996 if (!bh) {
Mingming Cao617ba132006-10-11 01:20:53 -0700997 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700998 "can't read last block, resize aborted");
999 return -ENOSPC;
1000 }
1001 brelse(bh);
1002
1003 /* We will update the superblock, one block bitmap, and
Mingming Cao617ba132006-10-11 01:20:53 -07001004 * one group descriptor via ext4_free_blocks().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001005 */
Mingming Cao617ba132006-10-11 01:20:53 -07001006 handle = ext4_journal_start_sb(sb, 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001007 if (IS_ERR(handle)) {
1008 err = PTR_ERR(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07001009 ext4_warning(sb, __FUNCTION__, "error %d on journal start",err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001010 goto exit_put;
1011 }
1012
1013 lock_super(sb);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001014 if (o_blocks_count != ext4_blocks_count(es)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001015 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001016 "multiple resizers run on filesystem!");
1017 unlock_super(sb);
1018 err = -EBUSY;
1019 goto exit_put;
1020 }
1021
Mingming Cao617ba132006-10-11 01:20:53 -07001022 if ((err = ext4_journal_get_write_access(handle,
1023 EXT4_SB(sb)->s_sbh))) {
1024 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001025 "error %d on journal write access", err);
1026 unlock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07001027 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001028 goto exit_put;
1029 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001030 ext4_blocks_count_set(es, o_blocks_count + add);
Mingming Cao617ba132006-10-11 01:20:53 -07001031 ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001032 sb->s_dirt = 1;
1033 unlock_super(sb);
Mingming Cao2ae02102006-10-11 01:21:11 -07001034 ext4_debug("freeing blocks %lu through %llu\n", o_blocks_count,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001035 o_blocks_count + add);
Mingming Cao617ba132006-10-11 01:20:53 -07001036 ext4_free_blocks_sb(handle, sb, o_blocks_count, add, &freed_blocks);
Mingming Cao2ae02102006-10-11 01:21:11 -07001037 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001038 o_blocks_count + add);
Mingming Cao617ba132006-10-11 01:20:53 -07001039 if ((err = ext4_journal_stop(handle)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001040 goto exit_put;
1041 if (test_opt(sb, DEBUG))
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001042 printk(KERN_DEBUG "EXT4-fs: extended group to %llu blocks\n",
1043 ext4_blocks_count(es));
Mingming Cao617ba132006-10-11 01:20:53 -07001044 update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es,
1045 sizeof(struct ext4_super_block));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001046exit_put:
1047 return err;
Mingming Cao617ba132006-10-11 01:20:53 -07001048} /* ext4_group_extend */