blob: 27105655502cf90a0fa3bb05020698cebfda429a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ext3/resize.c
3 *
4 * Support for resizing an ext3 filesystem while it is mounted.
5 *
6 * Copyright (C) 2001, 2002 Andreas Dilger <adilger@clusterfs.com>
7 *
8 * This could probably be made into a module, because it is not often in use.
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#define EXT3FS_DEBUG
13
Al Viro4613ad12012-03-29 22:30:07 -040014#include "ext3.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16
17#define outside(b, first, last) ((b) < (first) || (b) >= (last))
18#define inside(b, first, last) ((b) >= (first) && (b) < (last))
19
20static int verify_group_input(struct super_block *sb,
21 struct ext3_new_group_data *input)
22{
23 struct ext3_sb_info *sbi = EXT3_SB(sb);
24 struct ext3_super_block *es = sbi->s_es;
Mingming Cao1c2bf372006-06-25 05:48:06 -070025 ext3_fsblk_t start = le32_to_cpu(es->s_blocks_count);
26 ext3_fsblk_t end = start + input->blocks_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 unsigned group = input->group;
Mingming Cao1c2bf372006-06-25 05:48:06 -070028 ext3_fsblk_t itend = input->inode_table + sbi->s_itb_per_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 unsigned overhead = ext3_bg_has_super(sb, group) ?
30 (1 + ext3_bg_num_gdb(sb, group) +
31 le16_to_cpu(es->s_reserved_gdt_blocks)) : 0;
Mingming Cao1c2bf372006-06-25 05:48:06 -070032 ext3_fsblk_t metaend = start + overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 struct buffer_head *bh = NULL;
Mingming Cao1c2bf372006-06-25 05:48:06 -070034 ext3_grpblk_t free_blocks_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 int err = -EINVAL;
36
37 input->free_blocks_count = free_blocks_count =
38 input->blocks_count - 2 - overhead - sbi->s_itb_per_group;
39
40 if (test_opt(sb, DEBUG))
41 printk(KERN_DEBUG "EXT3-fs: adding %s group %u: %u blocks "
42 "(%d free, %u reserved)\n",
43 ext3_bg_has_super(sb, input->group) ? "normal" :
44 "no-super", input->group, input->blocks_count,
45 free_blocks_count, input->reserved_blocks);
46
47 if (group != sbi->s_groups_count)
Harvey Harrisone05b6b52008-04-28 02:16:15 -070048 ext3_warning(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 "Cannot add at group %u (only %lu groups)",
50 input->group, sbi->s_groups_count);
51 else if ((start - le32_to_cpu(es->s_first_data_block)) %
52 EXT3_BLOCKS_PER_GROUP(sb))
Harvey Harrisone05b6b52008-04-28 02:16:15 -070053 ext3_warning(sb, __func__, "Last group not full");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 else if (input->reserved_blocks > input->blocks_count / 5)
Harvey Harrisone05b6b52008-04-28 02:16:15 -070055 ext3_warning(sb, __func__, "Reserved blocks too high (%u)",
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 input->reserved_blocks);
57 else if (free_blocks_count < 0)
Harvey Harrisone05b6b52008-04-28 02:16:15 -070058 ext3_warning(sb, __func__, "Bad blocks count %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 input->blocks_count);
60 else if (!(bh = sb_bread(sb, end - 1)))
Harvey Harrisone05b6b52008-04-28 02:16:15 -070061 ext3_warning(sb, __func__,
Mingming Cao1c2bf372006-06-25 05:48:06 -070062 "Cannot read last block ("E3FSBLK")",
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 end - 1);
64 else if (outside(input->block_bitmap, start, end))
Harvey Harrisone05b6b52008-04-28 02:16:15 -070065 ext3_warning(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 "Block bitmap not in group (block %u)",
67 input->block_bitmap);
68 else if (outside(input->inode_bitmap, start, end))
Harvey Harrisone05b6b52008-04-28 02:16:15 -070069 ext3_warning(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 "Inode bitmap not in group (block %u)",
71 input->inode_bitmap);
72 else if (outside(input->inode_table, start, end) ||
73 outside(itend - 1, start, end))
Harvey Harrisone05b6b52008-04-28 02:16:15 -070074 ext3_warning(sb, __func__,
Mingming Cao1c2bf372006-06-25 05:48:06 -070075 "Inode table not in group (blocks %u-"E3FSBLK")",
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 input->inode_table, itend - 1);
77 else if (input->inode_bitmap == input->block_bitmap)
Harvey Harrisone05b6b52008-04-28 02:16:15 -070078 ext3_warning(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 "Block bitmap same as inode bitmap (%u)",
80 input->block_bitmap);
81 else if (inside(input->block_bitmap, input->inode_table, itend))
Harvey Harrisone05b6b52008-04-28 02:16:15 -070082 ext3_warning(sb, __func__,
Mingming Cao1c2bf372006-06-25 05:48:06 -070083 "Block bitmap (%u) in inode table (%u-"E3FSBLK")",
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 input->block_bitmap, input->inode_table, itend-1);
85 else if (inside(input->inode_bitmap, input->inode_table, itend))
Harvey Harrisone05b6b52008-04-28 02:16:15 -070086 ext3_warning(sb, __func__,
Mingming Cao1c2bf372006-06-25 05:48:06 -070087 "Inode bitmap (%u) in inode table (%u-"E3FSBLK")",
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 input->inode_bitmap, input->inode_table, itend-1);
89 else if (inside(input->block_bitmap, start, metaend))
Harvey Harrisone05b6b52008-04-28 02:16:15 -070090 ext3_warning(sb, __func__,
Mingming Cao1c2bf372006-06-25 05:48:06 -070091 "Block bitmap (%u) in GDT table"
92 " ("E3FSBLK"-"E3FSBLK")",
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 input->block_bitmap, start, metaend - 1);
94 else if (inside(input->inode_bitmap, start, metaend))
Harvey Harrisone05b6b52008-04-28 02:16:15 -070095 ext3_warning(sb, __func__,
Mingming Cao1c2bf372006-06-25 05:48:06 -070096 "Inode bitmap (%u) in GDT table"
97 " ("E3FSBLK"-"E3FSBLK")",
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 input->inode_bitmap, start, metaend - 1);
99 else if (inside(input->inode_table, start, metaend) ||
100 inside(itend - 1, start, metaend))
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700101 ext3_warning(sb, __func__,
Mingming Cao1c2bf372006-06-25 05:48:06 -0700102 "Inode table (%u-"E3FSBLK") overlaps"
103 "GDT table ("E3FSBLK"-"E3FSBLK")",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 input->inode_table, itend - 1, start, metaend - 1);
105 else
106 err = 0;
107 brelse(bh);
108
109 return err;
110}
111
112static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
Mingming Cao43d23f92006-06-25 05:48:07 -0700113 ext3_fsblk_t blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 struct buffer_head *bh;
116 int err;
117
118 bh = sb_getblk(sb, blk);
Wang Shilong1b7d76e2013-01-12 01:36:17 -0800119 if (unlikely(!bh))
Wang Shilongc04e88e2013-01-15 21:20:01 -0800120 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if ((err = ext3_journal_get_write_access(handle, bh))) {
122 brelse(bh);
123 bh = ERR_PTR(err);
124 } else {
125 lock_buffer(bh);
126 memset(bh->b_data, 0, sb->s_blocksize);
127 set_buffer_uptodate(bh);
128 unlock_buffer(bh);
129 }
130
131 return bh;
132}
133
134/*
135 * To avoid calling the atomic setbit hundreds or thousands of times, we only
136 * need to use it within a single byte (to ensure we get endianness right).
137 * We can use memset for the rest of the bitmap as there are no other users.
138 */
139static void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
140{
141 int i;
142
143 if (start_bit >= end_bit)
144 return;
145
146 ext3_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
147 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
148 ext3_set_bit(i, bitmap);
149 if (i < end_bit)
150 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
151}
152
153/*
Eric Sandeen1ad6ecf2007-10-16 23:30:28 -0700154 * If we have fewer than thresh credits, extend by EXT3_MAX_TRANS_DATA.
155 * If that fails, restart the transaction & regain write access for the
156 * buffer head which is used for block_bitmap modifications.
157 */
158static int extend_or_restart_transaction(handle_t *handle, int thresh,
159 struct buffer_head *bh)
160{
161 int err;
162
163 if (handle->h_buffer_credits >= thresh)
164 return 0;
165
166 err = ext3_journal_extend(handle, EXT3_MAX_TRANS_DATA);
167 if (err < 0)
168 return err;
169 if (err) {
170 err = ext3_journal_restart(handle, EXT3_MAX_TRANS_DATA);
171 if (err)
172 return err;
173 err = ext3_journal_get_write_access(handle, bh);
174 if (err)
175 return err;
176 }
177
178 return 0;
179}
180
181/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * Set up the block and inode bitmaps, and the inode table for the new group.
183 * This doesn't need to be part of the main transaction, since we are only
184 * changing blocks outside the actual filesystem. We still do journaling to
185 * ensure the recovery is correct in case of a failure just after resize.
186 * If any part of this fails, we simply abort the resize.
187 */
188static int setup_new_group_blocks(struct super_block *sb,
189 struct ext3_new_group_data *input)
190{
191 struct ext3_sb_info *sbi = EXT3_SB(sb);
Mingming Cao43d23f92006-06-25 05:48:07 -0700192 ext3_fsblk_t start = ext3_group_first_block_no(sb, input->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int reserved_gdb = ext3_bg_has_super(sb, input->group) ?
194 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0;
195 unsigned long gdblocks = ext3_bg_num_gdb(sb, input->group);
196 struct buffer_head *bh;
197 handle_t *handle;
Mingming Cao43d23f92006-06-25 05:48:07 -0700198 ext3_fsblk_t block;
Mingming Cao1c2bf372006-06-25 05:48:06 -0700199 ext3_grpblk_t bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int i;
201 int err = 0, err2;
202
Eric Sandeen1ad6ecf2007-10-16 23:30:28 -0700203 /* This transaction may be extended/restarted along the way */
204 handle = ext3_journal_start_sb(sb, EXT3_MAX_TRANS_DATA);
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (IS_ERR(handle))
207 return PTR_ERR(handle);
208
Eric Sandeen96d2a492009-12-14 13:01:05 -0600209 mutex_lock(&sbi->s_resize_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (input->group != sbi->s_groups_count) {
211 err = -EBUSY;
212 goto exit_journal;
213 }
214
215 if (IS_ERR(bh = bclean(handle, sb, input->block_bitmap))) {
216 err = PTR_ERR(bh);
217 goto exit_journal;
218 }
219
220 if (ext3_bg_has_super(sb, input->group)) {
221 ext3_debug("mark backup superblock %#04lx (+0)\n", start);
222 ext3_set_bit(0, bh->b_data);
223 }
224
225 /* Copy all of the GDT blocks into the backup in this group */
226 for (i = 0, bit = 1, block = start + 1;
227 i < gdblocks; i++, block++, bit++) {
228 struct buffer_head *gdb;
229
230 ext3_debug("update backup group %#04lx (+%d)\n", block, bit);
231
Eric Sandeen1ad6ecf2007-10-16 23:30:28 -0700232 err = extend_or_restart_transaction(handle, 1, bh);
233 if (err)
234 goto exit_bh;
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 gdb = sb_getblk(sb, block);
Wang Shilong1b7d76e2013-01-12 01:36:17 -0800237 if (unlikely(!gdb)) {
Wang Shilongc04e88e2013-01-15 21:20:01 -0800238 err = -ENOMEM;
Glauber de Oliveira Costa2973dfd2005-10-30 15:03:05 -0800239 goto exit_bh;
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if ((err = ext3_journal_get_write_access(handle, gdb))) {
242 brelse(gdb);
243 goto exit_bh;
244 }
Eric Sandeen42a2b6a2007-10-18 03:06:57 -0700245 lock_buffer(gdb);
246 memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, gdb->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 set_buffer_uptodate(gdb);
Eric Sandeen42a2b6a2007-10-18 03:06:57 -0700248 unlock_buffer(gdb);
Namhyung Kim41dc6382010-11-25 01:53:12 +0900249 err = ext3_journal_dirty_metadata(handle, gdb);
250 if (err) {
251 brelse(gdb);
252 goto exit_bh;
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 ext3_set_bit(bit, bh->b_data);
255 brelse(gdb);
256 }
257
258 /* Zero out all of the reserved backup group descriptor table blocks */
259 for (i = 0, bit = gdblocks + 1, block = start + bit;
260 i < reserved_gdb; i++, block++, bit++) {
261 struct buffer_head *gdb;
262
263 ext3_debug("clear reserved block %#04lx (+%d)\n", block, bit);
264
Eric Sandeen1ad6ecf2007-10-16 23:30:28 -0700265 err = extend_or_restart_transaction(handle, 1, bh);
266 if (err)
267 goto exit_bh;
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (IS_ERR(gdb = bclean(handle, sb, block))) {
Roel Kluin8e0eb402009-12-07 14:50:11 +0100270 err = PTR_ERR(gdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 goto exit_bh;
272 }
Namhyung Kim41dc6382010-11-25 01:53:12 +0900273 err = ext3_journal_dirty_metadata(handle, gdb);
274 if (err) {
275 brelse(gdb);
276 goto exit_bh;
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 ext3_set_bit(bit, bh->b_data);
279 brelse(gdb);
280 }
281 ext3_debug("mark block bitmap %#04x (+%ld)\n", input->block_bitmap,
282 input->block_bitmap - start);
283 ext3_set_bit(input->block_bitmap - start, bh->b_data);
284 ext3_debug("mark inode bitmap %#04x (+%ld)\n", input->inode_bitmap,
285 input->inode_bitmap - start);
286 ext3_set_bit(input->inode_bitmap - start, bh->b_data);
287
288 /* Zero out all of the inode table blocks */
289 for (i = 0, block = input->inode_table, bit = block - start;
290 i < sbi->s_itb_per_group; i++, bit++, block++) {
291 struct buffer_head *it;
292
Glauber de Oliveira Costa8bdac5d2005-09-22 21:44:26 -0700293 ext3_debug("clear inode block %#04lx (+%d)\n", block, bit);
Eric Sandeen1ad6ecf2007-10-16 23:30:28 -0700294
295 err = extend_or_restart_transaction(handle, 1, bh);
296 if (err)
297 goto exit_bh;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (IS_ERR(it = bclean(handle, sb, block))) {
300 err = PTR_ERR(it);
301 goto exit_bh;
302 }
Namhyung Kim41dc6382010-11-25 01:53:12 +0900303 err = ext3_journal_dirty_metadata(handle, it);
304 if (err) {
305 brelse(it);
306 goto exit_bh;
307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 brelse(it);
309 ext3_set_bit(bit, bh->b_data);
310 }
Eric Sandeen1ad6ecf2007-10-16 23:30:28 -0700311
312 err = extend_or_restart_transaction(handle, 2, bh);
313 if (err)
314 goto exit_bh;
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 mark_bitmap_end(input->blocks_count, EXT3_BLOCKS_PER_GROUP(sb),
317 bh->b_data);
Namhyung Kim41dc6382010-11-25 01:53:12 +0900318 err = ext3_journal_dirty_metadata(handle, bh);
319 if (err)
320 goto exit_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 brelse(bh);
322
323 /* Mark unused entries in inode bitmap used */
324 ext3_debug("clear inode bitmap %#04x (+%ld)\n",
325 input->inode_bitmap, input->inode_bitmap - start);
326 if (IS_ERR(bh = bclean(handle, sb, input->inode_bitmap))) {
327 err = PTR_ERR(bh);
328 goto exit_journal;
329 }
330
331 mark_bitmap_end(EXT3_INODES_PER_GROUP(sb), EXT3_BLOCKS_PER_GROUP(sb),
332 bh->b_data);
Namhyung Kim41dc6382010-11-25 01:53:12 +0900333 err = ext3_journal_dirty_metadata(handle, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334exit_bh:
335 brelse(bh);
336
337exit_journal:
Eric Sandeen96d2a492009-12-14 13:01:05 -0600338 mutex_unlock(&sbi->s_resize_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if ((err2 = ext3_journal_stop(handle)) && !err)
340 err = err2;
341
342 return err;
343}
344
345/*
346 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
347 * ext3 filesystem. The counters should be initialized to 1, 5, and 7 before
348 * calling this for the first time. In a sparse filesystem it will be the
349 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
350 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
351 */
352static unsigned ext3_list_backups(struct super_block *sb, unsigned *three,
353 unsigned *five, unsigned *seven)
354{
355 unsigned *min = three;
356 int mult = 3;
357 unsigned ret;
358
359 if (!EXT3_HAS_RO_COMPAT_FEATURE(sb,
360 EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
361 ret = *min;
362 *min += 1;
363 return ret;
364 }
365
366 if (*five < *min) {
367 min = five;
368 mult = 5;
369 }
370 if (*seven < *min) {
371 min = seven;
372 mult = 7;
373 }
374
375 ret = *min;
376 *min *= mult;
377
378 return ret;
379}
380
381/*
382 * Check that all of the backup GDT blocks are held in the primary GDT block.
383 * It is assumed that they are stored in group order. Returns the number of
384 * groups in current filesystem that have BACKUPS, or -ve error code.
385 */
386static int verify_reserved_gdb(struct super_block *sb,
387 struct buffer_head *primary)
388{
Mingming Cao43d23f92006-06-25 05:48:07 -0700389 const ext3_fsblk_t blk = primary->b_blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 const unsigned long end = EXT3_SB(sb)->s_groups_count;
391 unsigned three = 1;
392 unsigned five = 5;
393 unsigned seven = 7;
394 unsigned grp;
Dave Kleikampa4e4de32006-09-27 01:49:36 -0700395 __le32 *p = (__le32 *)primary->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int gdbackups = 0;
397
398 while ((grp = ext3_list_backups(sb, &three, &five, &seven)) < end) {
399 if (le32_to_cpu(*p++) != grp * EXT3_BLOCKS_PER_GROUP(sb) + blk){
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700400 ext3_warning(sb, __func__,
Mingming Cao43d23f92006-06-25 05:48:07 -0700401 "reserved GDT "E3FSBLK
402 " missing grp %d ("E3FSBLK")",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 blk, grp,
404 grp * EXT3_BLOCKS_PER_GROUP(sb) + blk);
405 return -EINVAL;
406 }
407 if (++gdbackups > EXT3_ADDR_PER_BLOCK(sb))
408 return -EFBIG;
409 }
410
411 return gdbackups;
412}
413
414/*
415 * Called when we need to bring a reserved group descriptor table block into
416 * use from the resize inode. The primary copy of the new GDT block currently
417 * is an indirect block (under the double indirect block in the resize inode).
418 * The new backup GDT blocks will be stored as leaf blocks in this indirect
419 * block, in group order. Even though we know all the block numbers we need,
420 * we check to ensure that the resize inode has actually reserved these blocks.
421 *
422 * Don't need to update the block bitmaps because the blocks are still in use.
423 *
424 * We get all of the error cases out of the way, so that we are sure to not
425 * fail once we start modifying the data on disk, because JBD has no rollback.
426 */
427static int add_new_gdb(handle_t *handle, struct inode *inode,
428 struct ext3_new_group_data *input,
429 struct buffer_head **primary)
430{
431 struct super_block *sb = inode->i_sb;
432 struct ext3_super_block *es = EXT3_SB(sb)->s_es;
433 unsigned long gdb_num = input->group / EXT3_DESC_PER_BLOCK(sb);
Mingming Cao43d23f92006-06-25 05:48:07 -0700434 ext3_fsblk_t gdblock = EXT3_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 struct buffer_head **o_group_desc, **n_group_desc;
436 struct buffer_head *dind;
437 int gdbackups;
438 struct ext3_iloc iloc;
Dave Kleikampa4e4de32006-09-27 01:49:36 -0700439 __le32 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 int err;
441
442 if (test_opt(sb, DEBUG))
443 printk(KERN_DEBUG
444 "EXT3-fs: ext3_add_new_gdb: adding group block %lu\n",
445 gdb_num);
446
447 /*
448 * If we are not using the primary superblock/GDT copy don't resize,
449 * because the user tools have no way of handling this. Probably a
450 * bad time to do it anyways.
451 */
452 if (EXT3_SB(sb)->s_sbh->b_blocknr !=
453 le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block)) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700454 ext3_warning(sb, __func__,
Glauber de Oliveira Costa9f406682006-01-08 01:03:22 -0800455 "won't resize using backup superblock at %llu",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 (unsigned long long)EXT3_SB(sb)->s_sbh->b_blocknr);
457 return -EPERM;
458 }
459
460 *primary = sb_bread(sb, gdblock);
461 if (!*primary)
462 return -EIO;
463
464 if ((gdbackups = verify_reserved_gdb(sb, *primary)) < 0) {
465 err = gdbackups;
466 goto exit_bh;
467 }
468
469 data = EXT3_I(inode)->i_data + EXT3_DIND_BLOCK;
470 dind = sb_bread(sb, le32_to_cpu(*data));
471 if (!dind) {
472 err = -EIO;
473 goto exit_bh;
474 }
475
Dave Kleikampa4e4de32006-09-27 01:49:36 -0700476 data = (__le32 *)dind->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (le32_to_cpu(data[gdb_num % EXT3_ADDR_PER_BLOCK(sb)]) != gdblock) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700478 ext3_warning(sb, __func__,
Mingming Cao43d23f92006-06-25 05:48:07 -0700479 "new group %u GDT block "E3FSBLK" not reserved",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 input->group, gdblock);
481 err = -EINVAL;
482 goto exit_dind;
483 }
484
485 if ((err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh)))
486 goto exit_dind;
487
488 if ((err = ext3_journal_get_write_access(handle, *primary)))
489 goto exit_sbh;
490
491 if ((err = ext3_journal_get_write_access(handle, dind)))
492 goto exit_primary;
493
494 /* ext3_reserve_inode_write() gets a reference on the iloc */
495 if ((err = ext3_reserve_inode_write(handle, inode, &iloc)))
496 goto exit_dindj;
497
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700498 n_group_desc = kmalloc((gdb_num + 1) * sizeof(struct buffer_head *),
Josef Bacikc587f0c2008-03-19 17:00:49 -0700499 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (!n_group_desc) {
501 err = -ENOMEM;
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700502 ext3_warning (sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 "not enough memory for %lu groups", gdb_num + 1);
504 goto exit_inode;
505 }
506
507 /*
508 * Finally, we have all of the possible failures behind us...
509 *
510 * Remove new GDT block from inode double-indirect block and clear out
511 * the new GDT block for use (which also "frees" the backup GDT blocks
512 * from the reserved inode). We don't need to change the bitmaps for
513 * these blocks, because they are marked as in-use from being in the
514 * reserved inode, and will become GDT blocks (primary and backup).
515 */
516 data[gdb_num % EXT3_ADDR_PER_BLOCK(sb)] = 0;
Namhyung Kim41dc6382010-11-25 01:53:12 +0900517 err = ext3_journal_dirty_metadata(handle, dind);
518 if (err)
519 goto exit_group_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 brelse(dind);
Namhyung Kim41dc6382010-11-25 01:53:12 +0900521 dind = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
Namhyung Kim41dc6382010-11-25 01:53:12 +0900523 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
524 if (err)
525 goto exit_group_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 memset((*primary)->b_data, 0, sb->s_blocksize);
Namhyung Kim41dc6382010-11-25 01:53:12 +0900527 err = ext3_journal_dirty_metadata(handle, *primary);
528 if (err)
529 goto exit_group_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 o_group_desc = EXT3_SB(sb)->s_group_desc;
532 memcpy(n_group_desc, o_group_desc,
533 EXT3_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
534 n_group_desc[gdb_num] = *primary;
535 EXT3_SB(sb)->s_group_desc = n_group_desc;
536 EXT3_SB(sb)->s_gdb_count++;
537 kfree(o_group_desc);
538
Marcin Slusarz50e8a282008-02-08 04:20:13 -0800539 le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
Namhyung Kim41dc6382010-11-25 01:53:12 +0900540 err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
541 if (err)
542 goto exit_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 return 0;
545
Namhyung Kim41dc6382010-11-25 01:53:12 +0900546exit_group_desc:
547 kfree(n_group_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548exit_inode:
549 //ext3_journal_release_buffer(handle, iloc.bh);
550 brelse(iloc.bh);
551exit_dindj:
552 //ext3_journal_release_buffer(handle, dind);
553exit_primary:
554 //ext3_journal_release_buffer(handle, *primary);
555exit_sbh:
556 //ext3_journal_release_buffer(handle, *primary);
557exit_dind:
558 brelse(dind);
559exit_bh:
560 brelse(*primary);
561
562 ext3_debug("leaving with error %d\n", err);
563 return err;
564}
565
566/*
567 * Called when we are adding a new group which has a backup copy of each of
568 * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
569 * We need to add these reserved backup GDT blocks to the resize inode, so
570 * that they are kept for future resizing and not allocated to files.
571 *
572 * Each reserved backup GDT block will go into a different indirect block.
573 * The indirect blocks are actually the primary reserved GDT blocks,
574 * so we know in advance what their block numbers are. We only get the
575 * double-indirect block to verify it is pointing to the primary reserved
576 * GDT blocks so we don't overwrite a data block by accident. The reserved
577 * backup GDT blocks are stored in their reserved primary GDT block.
578 */
579static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
580 struct ext3_new_group_data *input)
581{
582 struct super_block *sb = inode->i_sb;
583 int reserved_gdb =le16_to_cpu(EXT3_SB(sb)->s_es->s_reserved_gdt_blocks);
584 struct buffer_head **primary;
585 struct buffer_head *dind;
586 struct ext3_iloc iloc;
Mingming Cao43d23f92006-06-25 05:48:07 -0700587 ext3_fsblk_t blk;
Dave Kleikampa4e4de32006-09-27 01:49:36 -0700588 __le32 *data, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 int gdbackups = 0;
590 int res, i;
591 int err;
592
Josef Bacikc587f0c2008-03-19 17:00:49 -0700593 primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (!primary)
595 return -ENOMEM;
596
597 data = EXT3_I(inode)->i_data + EXT3_DIND_BLOCK;
598 dind = sb_bread(sb, le32_to_cpu(*data));
599 if (!dind) {
600 err = -EIO;
601 goto exit_free;
602 }
603
604 blk = EXT3_SB(sb)->s_sbh->b_blocknr + 1 + EXT3_SB(sb)->s_gdb_count;
Josef Bacik9bb91782008-06-05 22:46:47 -0700605 data = (__le32 *)dind->b_data + (EXT3_SB(sb)->s_gdb_count %
606 EXT3_ADDR_PER_BLOCK(sb));
Dave Kleikampa4e4de32006-09-27 01:49:36 -0700607 end = (__le32 *)dind->b_data + EXT3_ADDR_PER_BLOCK(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /* Get each reserved primary GDT block and verify it holds backups */
610 for (res = 0; res < reserved_gdb; res++, blk++) {
611 if (le32_to_cpu(*data) != blk) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700612 ext3_warning(sb, __func__,
Mingming Cao43d23f92006-06-25 05:48:07 -0700613 "reserved block "E3FSBLK
614 " not at offset %ld",
Dave Kleikampa4e4de32006-09-27 01:49:36 -0700615 blk,
616 (long)(data - (__le32 *)dind->b_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 err = -EINVAL;
618 goto exit_bh;
619 }
620 primary[res] = sb_bread(sb, blk);
621 if (!primary[res]) {
622 err = -EIO;
623 goto exit_bh;
624 }
625 if ((gdbackups = verify_reserved_gdb(sb, primary[res])) < 0) {
626 brelse(primary[res]);
627 err = gdbackups;
628 goto exit_bh;
629 }
630 if (++data >= end)
Dave Kleikampa4e4de32006-09-27 01:49:36 -0700631 data = (__le32 *)dind->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
634 for (i = 0; i < reserved_gdb; i++) {
635 if ((err = ext3_journal_get_write_access(handle, primary[i]))) {
636 /*
637 int j;
638 for (j = 0; j < i; j++)
639 ext3_journal_release_buffer(handle, primary[j]);
640 */
641 goto exit_bh;
642 }
643 }
644
645 if ((err = ext3_reserve_inode_write(handle, inode, &iloc)))
646 goto exit_bh;
647
648 /*
649 * Finally we can add each of the reserved backup GDT blocks from
650 * the new group to its reserved primary GDT block.
651 */
652 blk = input->group * EXT3_BLOCKS_PER_GROUP(sb);
653 for (i = 0; i < reserved_gdb; i++) {
654 int err2;
Dave Kleikampa4e4de32006-09-27 01:49:36 -0700655 data = (__le32 *)primary[i]->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* printk("reserving backup %lu[%u] = %lu\n",
657 primary[i]->b_blocknr, gdbackups,
658 blk + primary[i]->b_blocknr); */
659 data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
660 err2 = ext3_journal_dirty_metadata(handle, primary[i]);
661 if (!err)
662 err = err2;
663 }
664 inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
665 ext3_mark_iloc_dirty(handle, inode, &iloc);
666
667exit_bh:
668 while (--res >= 0)
669 brelse(primary[res]);
670 brelse(dind);
671
672exit_free:
673 kfree(primary);
674
675 return err;
676}
677
678/*
679 * Update the backup copies of the ext3 metadata. These don't need to be part
680 * of the main resize transaction, because e2fsck will re-write them if there
681 * is a problem (basically only OOM will cause a problem). However, we
682 * _should_ update the backups if possible, in case the primary gets trashed
683 * for some reason and we need to run e2fsck from a backup superblock. The
684 * important part is that the new block and inode counts are in the backup
685 * superblocks, and the location of the new group metadata in the GDT backups.
686 *
Eric Sandeen96d2a492009-12-14 13:01:05 -0600687 * We do not need take the s_resize_lock for this, because these
688 * blocks are not otherwise touched by the filesystem code when it is
689 * mounted. We don't need to worry about last changing from
690 * sbi->s_groups_count, because the worst that can happen is that we
691 * do not copy the full number of backups at this time. The resize
692 * which changed s_groups_count will backup again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
694static void update_backups(struct super_block *sb,
695 int blk_off, char *data, int size)
696{
697 struct ext3_sb_info *sbi = EXT3_SB(sb);
698 const unsigned long last = sbi->s_groups_count;
699 const int bpg = EXT3_BLOCKS_PER_GROUP(sb);
700 unsigned three = 1;
701 unsigned five = 5;
702 unsigned seven = 7;
703 unsigned group;
704 int rest = sb->s_blocksize - size;
705 handle_t *handle;
706 int err = 0, err2;
707
708 handle = ext3_journal_start_sb(sb, EXT3_MAX_TRANS_DATA);
709 if (IS_ERR(handle)) {
710 group = 1;
711 err = PTR_ERR(handle);
712 goto exit_err;
713 }
714
715 while ((group = ext3_list_backups(sb, &three, &five, &seven)) < last) {
716 struct buffer_head *bh;
717
718 /* Out of journal space, and can't get more - abort - so sad */
719 if (handle->h_buffer_credits == 0 &&
720 ext3_journal_extend(handle, EXT3_MAX_TRANS_DATA) &&
721 (err = ext3_journal_restart(handle, EXT3_MAX_TRANS_DATA)))
722 break;
723
724 bh = sb_getblk(sb, group * bpg + blk_off);
Wang Shilong1b7d76e2013-01-12 01:36:17 -0800725 if (unlikely(!bh)) {
Wang Shilongc04e88e2013-01-15 21:20:01 -0800726 err = -ENOMEM;
Glauber de Oliveira Costa2973dfd2005-10-30 15:03:05 -0800727 break;
728 }
Glauber de Oliveira Costa8bdac5d2005-09-22 21:44:26 -0700729 ext3_debug("update metadata backup %#04lx\n",
730 (unsigned long)bh->b_blocknr);
Namhyung Kim41dc6382010-11-25 01:53:12 +0900731 if ((err = ext3_journal_get_write_access(handle, bh))) {
732 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 break;
Namhyung Kim41dc6382010-11-25 01:53:12 +0900734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 lock_buffer(bh);
736 memcpy(bh->b_data, data, size);
737 if (rest)
738 memset(bh->b_data + size, 0, rest);
739 set_buffer_uptodate(bh);
740 unlock_buffer(bh);
Namhyung Kim41dc6382010-11-25 01:53:12 +0900741 err = ext3_journal_dirty_metadata(handle, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 brelse(bh);
Namhyung Kim41dc6382010-11-25 01:53:12 +0900743 if (err)
744 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 if ((err2 = ext3_journal_stop(handle)) && !err)
747 err = err2;
748
749 /*
750 * Ugh! Need to have e2fsck write the backup copies. It is too
751 * late to revert the resize, we shouldn't fail just because of
752 * the backup copies (they are only needed in case of corruption).
753 *
754 * However, if we got here we have a journal problem too, so we
755 * can't really start a transaction to mark the superblock.
756 * Chicken out and just set the flag on the hope it will be written
757 * to disk, and if not - we will simply wait until next fsck.
758 */
759exit_err:
760 if (err) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700761 ext3_warning(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 "can't update backup for group %d (err %d), "
Glauber de Oliveira Costa9f406682006-01-08 01:03:22 -0800763 "forcing fsck on next reboot", group, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 sbi->s_mount_state &= ~EXT3_VALID_FS;
Dave Kleikampa4e4de32006-09-27 01:49:36 -0700765 sbi->s_es->s_state &= cpu_to_le16(~EXT3_VALID_FS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 mark_buffer_dirty(sbi->s_sbh);
767 }
768}
769
770/* Add group descriptor data to an existing or new group descriptor block.
771 * Ensure we handle all possible error conditions _before_ we start modifying
772 * the filesystem, because we cannot abort the transaction and not have it
773 * write the data to disk.
774 *
775 * If we are on a GDT block boundary, we need to get the reserved GDT block.
776 * Otherwise, we may need to add backup GDT blocks for a sparse group.
777 *
778 * We only need to hold the superblock lock while we are actually adding
779 * in the new group's counts to the superblock. Prior to that we have
780 * not really "added" the group at all. We re-check that we are still
781 * adding in the last group in case things have changed since verifying.
782 */
783int ext3_group_add(struct super_block *sb, struct ext3_new_group_data *input)
784{
785 struct ext3_sb_info *sbi = EXT3_SB(sb);
786 struct ext3_super_block *es = sbi->s_es;
787 int reserved_gdb = ext3_bg_has_super(sb, input->group) ?
788 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
789 struct buffer_head *primary = NULL;
790 struct ext3_group_desc *gdp;
791 struct inode *inode = NULL;
792 handle_t *handle;
793 int gdb_off, gdb_num;
794 int err, err2;
795
796 gdb_num = input->group / EXT3_DESC_PER_BLOCK(sb);
797 gdb_off = input->group % EXT3_DESC_PER_BLOCK(sb);
798
799 if (gdb_off == 0 && !EXT3_HAS_RO_COMPAT_FEATURE(sb,
800 EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700801 ext3_warning(sb, __func__,
Glauber de Oliveira Costa9f406682006-01-08 01:03:22 -0800802 "Can't resize non-sparse filesystem further");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return -EPERM;
804 }
805
Eric Sandeen32c2d2b2006-09-27 01:49:36 -0700806 if (le32_to_cpu(es->s_blocks_count) + input->blocks_count <
807 le32_to_cpu(es->s_blocks_count)) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700808 ext3_warning(sb, __func__, "blocks_count overflow\n");
Eric Sandeen32c2d2b2006-09-27 01:49:36 -0700809 return -EINVAL;
810 }
811
812 if (le32_to_cpu(es->s_inodes_count) + EXT3_INODES_PER_GROUP(sb) <
813 le32_to_cpu(es->s_inodes_count)) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700814 ext3_warning(sb, __func__, "inodes_count overflow\n");
Eric Sandeen32c2d2b2006-09-27 01:49:36 -0700815 return -EINVAL;
816 }
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (reserved_gdb || gdb_off == 0) {
819 if (!EXT3_HAS_COMPAT_FEATURE(sb,
Josef Bacik972fbf72008-10-18 20:27:55 -0700820 EXT3_FEATURE_COMPAT_RESIZE_INODE)
821 || !le16_to_cpu(es->s_reserved_gdt_blocks)) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700822 ext3_warning(sb, __func__,
Glauber de Oliveira Costa9f406682006-01-08 01:03:22 -0800823 "No reserved GDT blocks, can't resize");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return -EPERM;
825 }
David Howells473043d2008-02-07 00:15:36 -0800826 inode = ext3_iget(sb, EXT3_RESIZE_INO);
827 if (IS_ERR(inode)) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700828 ext3_warning(sb, __func__,
Glauber de Oliveira Costa9f406682006-01-08 01:03:22 -0800829 "Error opening resize inode");
David Howells473043d2008-02-07 00:15:36 -0800830 return PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832 }
833
834 if ((err = verify_group_input(sb, input)))
835 goto exit_put;
836
837 if ((err = setup_new_group_blocks(sb, input)))
838 goto exit_put;
839
840 /*
841 * We will always be modifying at least the superblock and a GDT
842 * block. If we are adding a group past the last current GDT block,
843 * we will also modify the inode and the dindirect block. If we
844 * are adding a group with superblock/GDT backups we will also
845 * modify each of the reserved GDT dindirect blocks.
846 */
847 handle = ext3_journal_start_sb(sb,
848 ext3_bg_has_super(sb, input->group) ?
849 3 + reserved_gdb : 4);
850 if (IS_ERR(handle)) {
851 err = PTR_ERR(handle);
852 goto exit_put;
853 }
854
Eric Sandeen96d2a492009-12-14 13:01:05 -0600855 mutex_lock(&sbi->s_resize_lock);
Glauber de Oliveira Costa29ba1722006-01-08 01:03:23 -0800856 if (input->group != sbi->s_groups_count) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700857 ext3_warning(sb, __func__,
Glauber de Oliveira Costa9f406682006-01-08 01:03:22 -0800858 "multiple resizers run on filesystem!");
Glauber de Oliveira Costaaa877b32005-11-28 13:44:02 -0800859 err = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 goto exit_journal;
861 }
862
863 if ((err = ext3_journal_get_write_access(handle, sbi->s_sbh)))
864 goto exit_journal;
865
866 /*
867 * We will only either add reserved group blocks to a backup group
868 * or remove reserved blocks for the first group in a new group block.
869 * Doing both would be mean more complex code, and sane people don't
870 * use non-sparse filesystems anymore. This is already checked above.
871 */
872 if (gdb_off) {
873 primary = sbi->s_group_desc[gdb_num];
874 if ((err = ext3_journal_get_write_access(handle, primary)))
875 goto exit_journal;
876
877 if (reserved_gdb && ext3_bg_num_gdb(sb, input->group) &&
878 (err = reserve_backup_gdb(handle, inode, input)))
879 goto exit_journal;
880 } else if ((err = add_new_gdb(handle, inode, input, &primary)))
881 goto exit_journal;
882
883 /*
884 * OK, now we've set up the new group. Time to make it active.
885 *
Eric Sandeen96d2a492009-12-14 13:01:05 -0600886 * We do not lock all allocations via s_resize_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 * so we have to be safe wrt. concurrent accesses the group
888 * data. So we need to be careful to set all of the relevant
889 * group descriptor data etc. *before* we enable the group.
890 *
Glauber de Oliveira Costa29ba1722006-01-08 01:03:23 -0800891 * The key field here is sbi->s_groups_count: as long as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 * that retains its old value, nobody is going to access the new
893 * group.
894 *
895 * So first we update all the descriptor metadata for the new
896 * group; then we update the total disk blocks count; then we
897 * update the groups count to enable the group; then finally we
898 * update the free space counts so that the system can start
899 * using the new disk blocks.
900 */
901
902 /* Update group descriptor block for new group */
903 gdp = (struct ext3_group_desc *)primary->b_data + gdb_off;
904
905 gdp->bg_block_bitmap = cpu_to_le32(input->block_bitmap);
906 gdp->bg_inode_bitmap = cpu_to_le32(input->inode_bitmap);
907 gdp->bg_inode_table = cpu_to_le32(input->inode_table);
908 gdp->bg_free_blocks_count = cpu_to_le16(input->free_blocks_count);
909 gdp->bg_free_inodes_count = cpu_to_le16(EXT3_INODES_PER_GROUP(sb));
910
911 /*
912 * Make the new blocks and inodes valid next. We do this before
913 * increasing the group count so that once the group is enabled,
914 * all of its blocks and inodes are already valid.
915 *
916 * We always allocate group-by-group, then block-by-block or
917 * inode-by-inode within a group, so enabling these
918 * blocks/inodes before the group is live won't actually let us
919 * allocate the new space yet.
920 */
Marcin Slusarz50e8a282008-02-08 04:20:13 -0800921 le32_add_cpu(&es->s_blocks_count, input->blocks_count);
922 le32_add_cpu(&es->s_inodes_count, EXT3_INODES_PER_GROUP(sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 /*
925 * We need to protect s_groups_count against other CPUs seeing
926 * inconsistent state in the superblock.
927 *
928 * The precise rules we use are:
929 *
Eric Sandeen96d2a492009-12-14 13:01:05 -0600930 * * Writers of s_groups_count *must* hold s_resize_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 * AND
932 * * Writers must perform a smp_wmb() after updating all dependent
933 * data and before modifying the groups count
934 *
Eric Sandeen96d2a492009-12-14 13:01:05 -0600935 * * Readers must hold s_resize_lock over the access
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 * OR
937 * * Readers must perform an smp_rmb() after reading the groups count
938 * and before reading any dependent data.
939 *
940 * NB. These rules can be relaxed when checking the group count
941 * while freeing data, as we can only allocate from a block
942 * group after serialising against the group count, and we can
943 * only then free after serialising in turn against that
944 * allocation.
945 */
946 smp_wmb();
947
948 /* Update the global fs size fields */
Glauber de Oliveira Costa29ba1722006-01-08 01:03:23 -0800949 sbi->s_groups_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Namhyung Kim41dc6382010-11-25 01:53:12 +0900951 err = ext3_journal_dirty_metadata(handle, primary);
952 if (err)
953 goto exit_journal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 /* Update the reserved block counts only once the new group is
956 * active. */
Marcin Slusarz50e8a282008-02-08 04:20:13 -0800957 le32_add_cpu(&es->s_r_blocks_count, input->reserved_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /* Update the free space counts */
Peter Zijlstraaa0dff22007-10-16 23:25:42 -0700960 percpu_counter_add(&sbi->s_freeblocks_counter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 input->free_blocks_count);
Peter Zijlstraaa0dff22007-10-16 23:25:42 -0700962 percpu_counter_add(&sbi->s_freeinodes_counter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 EXT3_INODES_PER_GROUP(sb));
964
Namhyung Kim41dc6382010-11-25 01:53:12 +0900965 err = ext3_journal_dirty_metadata(handle, sbi->s_sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967exit_journal:
Eric Sandeen96d2a492009-12-14 13:01:05 -0600968 mutex_unlock(&sbi->s_resize_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if ((err2 = ext3_journal_stop(handle)) && !err)
970 err = err2;
971 if (!err) {
972 update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
973 sizeof(struct ext3_super_block));
974 update_backups(sb, primary->b_blocknr, primary->b_data,
975 primary->b_size);
976 }
977exit_put:
978 iput(inode);
979 return err;
980} /* ext3_group_add */
981
982/* Extend the filesystem to the new number of blocks specified. This entry
983 * point is only used to extend the current filesystem to the end of the last
984 * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
985 * for emergencies (because it has no dependencies on reserved blocks).
986 *
987 * If we _really_ wanted, we could use default values to call ext3_group_add()
988 * allow the "remount" trick to work for arbitrary resizing, assuming enough
989 * GDT blocks are reserved to grow to the desired size.
990 */
991int ext3_group_extend(struct super_block *sb, struct ext3_super_block *es,
Mingming Cao43d23f92006-06-25 05:48:07 -0700992 ext3_fsblk_t n_blocks_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Mingming Cao43d23f92006-06-25 05:48:07 -0700994 ext3_fsblk_t o_blocks_count;
Mingming Cao1c2bf372006-06-25 05:48:06 -0700995 ext3_grpblk_t last;
996 ext3_grpblk_t add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 struct buffer_head * bh;
998 handle_t *handle;
Mingming Cao1c2bf372006-06-25 05:48:06 -0700999 int err;
1000 unsigned long freed_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /* We don't need to worry about locking wrt other resizers just
1003 * yet: we're going to revalidate es->s_blocks_count after
Eric Sandeen96d2a492009-12-14 13:01:05 -06001004 * taking the s_resize_lock below. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 o_blocks_count = le32_to_cpu(es->s_blocks_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 if (test_opt(sb, DEBUG))
Namhyung Kimdb50d202010-10-17 01:11:23 +09001008 printk(KERN_DEBUG "EXT3-fs: extending last group from "E3FSBLK
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001009 " up to "E3FSBLK" blocks\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 o_blocks_count, n_blocks_count);
1011
1012 if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
1013 return 0;
1014
Mingming Caofcd5df32006-06-25 05:47:50 -07001015 if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
1016 printk(KERN_ERR "EXT3-fs: filesystem on %s:"
Namhyung Kimdb50d202010-10-17 01:11:23 +09001017 " too large to resize to "E3FSBLK" blocks safely\n",
Mingming Caofcd5df32006-06-25 05:47:50 -07001018 sb->s_id, n_blocks_count);
1019 if (sizeof(sector_t) < 8)
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001020 ext3_warning(sb, __func__,
Bartlomiej Zolnierkiewicz90c699a2009-06-19 08:08:50 +02001021 "CONFIG_LBDAF not enabled\n");
Mingming Caofcd5df32006-06-25 05:47:50 -07001022 return -EINVAL;
1023 }
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (n_blocks_count < o_blocks_count) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001026 ext3_warning(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 "can't shrink FS - resize aborted");
1028 return -EBUSY;
1029 }
1030
1031 /* Handle the remaining blocks in the last group only. */
1032 last = (o_blocks_count - le32_to_cpu(es->s_first_data_block)) %
1033 EXT3_BLOCKS_PER_GROUP(sb);
1034
1035 if (last == 0) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001036 ext3_warning(sb, __func__,
Glauber de Oliveira Costa9f406682006-01-08 01:03:22 -08001037 "need to use ext2online to resize further");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return -EPERM;
1039 }
1040
1041 add = EXT3_BLOCKS_PER_GROUP(sb) - last;
1042
Eric Sandeen32c2d2b2006-09-27 01:49:36 -07001043 if (o_blocks_count + add < o_blocks_count) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001044 ext3_warning(sb, __func__, "blocks_count overflow");
Eric Sandeen32c2d2b2006-09-27 01:49:36 -07001045 return -EINVAL;
1046 }
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (o_blocks_count + add > n_blocks_count)
1049 add = n_blocks_count - o_blocks_count;
1050
1051 if (o_blocks_count + add < n_blocks_count)
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001052 ext3_warning(sb, __func__,
Mingming Cao43d23f92006-06-25 05:48:07 -07001053 "will only finish group ("E3FSBLK
1054 " blocks, %u new)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 o_blocks_count + add, add);
1056
1057 /* See if the device is actually as big as what was requested */
1058 bh = sb_bread(sb, o_blocks_count + add -1);
1059 if (!bh) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001060 ext3_warning(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 "can't read last block, resize aborted");
1062 return -ENOSPC;
1063 }
1064 brelse(bh);
1065
1066 /* We will update the superblock, one block bitmap, and
1067 * one group descriptor via ext3_free_blocks().
1068 */
1069 handle = ext3_journal_start_sb(sb, 3);
1070 if (IS_ERR(handle)) {
1071 err = PTR_ERR(handle);
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001072 ext3_warning(sb, __func__, "error %d on journal start",err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 goto exit_put;
1074 }
1075
Eric Sandeen96d2a492009-12-14 13:01:05 -06001076 mutex_lock(&EXT3_SB(sb)->s_resize_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (o_blocks_count != le32_to_cpu(es->s_blocks_count)) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001078 ext3_warning(sb, __func__,
Glauber de Oliveira Costa9f406682006-01-08 01:03:22 -08001079 "multiple resizers run on filesystem!");
Eric Sandeen96d2a492009-12-14 13:01:05 -06001080 mutex_unlock(&EXT3_SB(sb)->s_resize_lock);
Akinobu Mita22a5daf2008-04-28 02:16:07 -07001081 ext3_journal_stop(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 err = -EBUSY;
1083 goto exit_put;
1084 }
1085
1086 if ((err = ext3_journal_get_write_access(handle,
1087 EXT3_SB(sb)->s_sbh))) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001088 ext3_warning(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 "error %d on journal write access", err);
Eric Sandeen96d2a492009-12-14 13:01:05 -06001090 mutex_unlock(&EXT3_SB(sb)->s_resize_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 ext3_journal_stop(handle);
1092 goto exit_put;
1093 }
1094 es->s_blocks_count = cpu_to_le32(o_blocks_count + add);
Namhyung Kim41dc6382010-11-25 01:53:12 +09001095 err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
Eric Sandeen96d2a492009-12-14 13:01:05 -06001096 mutex_unlock(&EXT3_SB(sb)->s_resize_lock);
Namhyung Kim41dc6382010-11-25 01:53:12 +09001097 if (err) {
1098 ext3_warning(sb, __func__,
1099 "error %d on journal dirty metadata", err);
1100 ext3_journal_stop(handle);
1101 goto exit_put;
1102 }
Namhyung Kimdb50d202010-10-17 01:11:23 +09001103 ext3_debug("freeing blocks "E3FSBLK" through "E3FSBLK"\n",
1104 o_blocks_count, o_blocks_count + add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 ext3_free_blocks_sb(handle, sb, o_blocks_count, add, &freed_blocks);
Namhyung Kimdb50d202010-10-17 01:11:23 +09001106 ext3_debug("freed blocks "E3FSBLK" through "E3FSBLK"\n",
1107 o_blocks_count, o_blocks_count + add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if ((err = ext3_journal_stop(handle)))
1109 goto exit_put;
1110 if (test_opt(sb, DEBUG))
1111 printk(KERN_DEBUG "EXT3-fs: extended group to %u blocks\n",
1112 le32_to_cpu(es->s_blocks_count));
1113 update_backups(sb, EXT3_SB(sb)->s_sbh->b_blocknr, (char *)es,
1114 sizeof(struct ext3_super_block));
1115exit_put:
1116 return err;
1117} /* ext3_group_extend */