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