RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / fs / ext4 / resize.c
blobaa11d7dbe9700106725f79c682aebda39a97bc9f
1 /*
2 * linux/fs/ext4/resize.c
4 * Support for resizing an ext4 filesystem while it is mounted.
6 * Copyright (C) 2001, 2002 Andreas Dilger <adilger@clusterfs.com>
8 * This could probably be made into a module, because it is not often in use.
9 */
12 #define EXT4FS_DEBUG
14 #include <linux/ext4_jbd2.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
20 #define outside(b, first, last) ((b) < (first) || (b) >= (last))
21 #define inside(b, first, last) ((b) >= (first) && (b) < (last))
23 static int verify_group_input(struct super_block *sb,
24 struct ext4_new_group_data *input)
26 struct ext4_sb_info *sbi = EXT4_SB(sb);
27 struct ext4_super_block *es = sbi->s_es;
28 ext4_fsblk_t start = ext4_blocks_count(es);
29 ext4_fsblk_t end = start + input->blocks_count;
30 unsigned group = input->group;
31 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) +
34 le16_to_cpu(es->s_reserved_gdt_blocks)) : 0;
35 ext4_fsblk_t metaend = start + overhead;
36 struct buffer_head *bh = NULL;
37 ext4_grpblk_t free_blocks_count, offset;
38 int err = -EINVAL;
40 input->free_blocks_count = free_blocks_count =
41 input->blocks_count - 2 - overhead - sbi->s_itb_per_group;
43 if (test_opt(sb, DEBUG))
44 printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks "
45 "(%d free, %u reserved)\n",
46 ext4_bg_has_super(sb, input->group) ? "normal" :
47 "no-super", input->group, input->blocks_count,
48 free_blocks_count, input->reserved_blocks);
50 ext4_get_group_no_and_offset(sb, start, NULL, &offset);
51 if (group != sbi->s_groups_count)
52 ext4_warning(sb, __FUNCTION__,
53 "Cannot add at group %u (only %lu groups)",
54 input->group, sbi->s_groups_count);
55 else if (offset != 0)
56 ext4_warning(sb, __FUNCTION__, "Last group not full");
57 else if (input->reserved_blocks > input->blocks_count / 5)
58 ext4_warning(sb, __FUNCTION__, "Reserved blocks too high (%u)",
59 input->reserved_blocks);
60 else if (free_blocks_count < 0)
61 ext4_warning(sb, __FUNCTION__, "Bad blocks count %u",
62 input->blocks_count);
63 else if (!(bh = sb_bread(sb, end - 1)))
64 ext4_warning(sb, __FUNCTION__,
65 "Cannot read last block (%llu)",
66 end - 1);
67 else if (outside(input->block_bitmap, start, end))
68 ext4_warning(sb, __FUNCTION__,
69 "Block bitmap not in group (block %llu)",
70 (unsigned long long)input->block_bitmap);
71 else if (outside(input->inode_bitmap, start, end))
72 ext4_warning(sb, __FUNCTION__,
73 "Inode bitmap not in group (block %llu)",
74 (unsigned long long)input->inode_bitmap);
75 else if (outside(input->inode_table, start, end) ||
76 outside(itend - 1, start, end))
77 ext4_warning(sb, __FUNCTION__,
78 "Inode table not in group (blocks %llu-%llu)",
79 (unsigned long long)input->inode_table, itend - 1);
80 else if (input->inode_bitmap == input->block_bitmap)
81 ext4_warning(sb, __FUNCTION__,
82 "Block bitmap same as inode bitmap (%llu)",
83 (unsigned long long)input->block_bitmap);
84 else if (inside(input->block_bitmap, input->inode_table, itend))
85 ext4_warning(sb, __FUNCTION__,
86 "Block bitmap (%llu) in inode table (%llu-%llu)",
87 (unsigned long long)input->block_bitmap,
88 (unsigned long long)input->inode_table, itend - 1);
89 else if (inside(input->inode_bitmap, input->inode_table, itend))
90 ext4_warning(sb, __FUNCTION__,
91 "Inode bitmap (%llu) in inode table (%llu-%llu)",
92 (unsigned long long)input->inode_bitmap,
93 (unsigned long long)input->inode_table, itend - 1);
94 else if (inside(input->block_bitmap, start, metaend))
95 ext4_warning(sb, __FUNCTION__,
96 "Block bitmap (%llu) in GDT table"
97 " (%llu-%llu)",
98 (unsigned long long)input->block_bitmap,
99 start, metaend - 1);
100 else if (inside(input->inode_bitmap, start, metaend))
101 ext4_warning(sb, __FUNCTION__,
102 "Inode bitmap (%llu) in GDT table"
103 " (%llu-%llu)",
104 (unsigned long long)input->inode_bitmap,
105 start, metaend - 1);
106 else if (inside(input->inode_table, start, metaend) ||
107 inside(itend - 1, start, metaend))
108 ext4_warning(sb, __FUNCTION__,
109 "Inode table (%llu-%llu) overlaps"
110 "GDT table (%llu-%llu)",
111 (unsigned long long)input->inode_table,
112 itend - 1, start, metaend - 1);
113 else
114 err = 0;
115 brelse(bh);
117 return err;
120 static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
121 ext4_fsblk_t blk)
123 struct buffer_head *bh;
124 int err;
126 bh = sb_getblk(sb, blk);
127 if (!bh)
128 return ERR_PTR(-EIO);
129 if ((err = ext4_journal_get_write_access(handle, bh))) {
130 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);
139 return bh;
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.
147 static void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
149 int i;
151 if (start_bit >= end_bit)
152 return;
154 ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
155 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
156 ext4_set_bit(i, bitmap);
157 if (i < end_bit)
158 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
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.
168 static int setup_new_group_blocks(struct super_block *sb,
169 struct ext4_new_group_data *input)
171 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) ?
174 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0;
175 unsigned long gdblocks = ext4_bg_num_gdb(sb, input->group);
176 struct buffer_head *bh;
177 handle_t *handle;
178 ext4_fsblk_t block;
179 ext4_grpblk_t bit;
180 int i;
181 int err = 0, err2;
183 handle = ext4_journal_start_sb(sb, reserved_gdb + gdblocks +
184 2 + sbi->s_itb_per_group);
185 if (IS_ERR(handle))
186 return PTR_ERR(handle);
188 lock_super(sb);
189 if (input->group != sbi->s_groups_count) {
190 err = -EBUSY;
191 goto exit_journal;
194 if (IS_ERR(bh = bclean(handle, sb, input->block_bitmap))) {
195 err = PTR_ERR(bh);
196 goto exit_journal;
199 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);
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;
209 ext4_debug("update backup group %#04lx (+%d)\n", block, bit);
211 gdb = sb_getblk(sb, block);
212 if (!gdb) {
213 err = -EIO;
214 goto exit_bh;
216 if ((err = ext4_journal_get_write_access(handle, gdb))) {
217 brelse(gdb);
218 goto exit_bh;
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);
224 ext4_journal_dirty_metadata(handle, gdb);
225 ext4_set_bit(bit, bh->b_data);
226 brelse(gdb);
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;
234 ext4_debug("clear reserved block %#04lx (+%d)\n", block, bit);
236 if (IS_ERR(gdb = bclean(handle, sb, block))) {
237 err = PTR_ERR(bh);
238 goto exit_bh;
240 ext4_journal_dirty_metadata(handle, gdb);
241 ext4_set_bit(bit, bh->b_data);
242 brelse(gdb);
244 ext4_debug("mark block bitmap %#04x (+%ld)\n", input->block_bitmap,
245 input->block_bitmap - start);
246 ext4_set_bit(input->block_bitmap - start, bh->b_data);
247 ext4_debug("mark inode bitmap %#04x (+%ld)\n", input->inode_bitmap,
248 input->inode_bitmap - start);
249 ext4_set_bit(input->inode_bitmap - start, bh->b_data);
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;
256 ext4_debug("clear inode block %#04lx (+%d)\n", block, bit);
257 if (IS_ERR(it = bclean(handle, sb, block))) {
258 err = PTR_ERR(it);
259 goto exit_bh;
261 ext4_journal_dirty_metadata(handle, it);
262 brelse(it);
263 ext4_set_bit(bit, bh->b_data);
265 mark_bitmap_end(input->blocks_count, EXT4_BLOCKS_PER_GROUP(sb),
266 bh->b_data);
267 ext4_journal_dirty_metadata(handle, bh);
268 brelse(bh);
270 /* Mark unused entries in inode bitmap used */
271 ext4_debug("clear inode bitmap %#04x (+%ld)\n",
272 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;
278 mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb),
279 bh->b_data);
280 ext4_journal_dirty_metadata(handle, bh);
281 exit_bh:
282 brelse(bh);
284 exit_journal:
285 unlock_super(sb);
286 if ((err2 = ext4_journal_stop(handle)) && !err)
287 err = err2;
289 return err;
294 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
295 * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
296 * 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, ...
300 static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
301 unsigned *five, unsigned *seven)
303 unsigned *min = three;
304 int mult = 3;
305 unsigned ret;
307 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
308 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
309 ret = *min;
310 *min += 1;
311 return ret;
314 if (*five < *min) {
315 min = five;
316 mult = 5;
318 if (*seven < *min) {
319 min = seven;
320 mult = 7;
323 ret = *min;
324 *min *= mult;
326 return ret;
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.
334 static int verify_reserved_gdb(struct super_block *sb,
335 struct buffer_head *primary)
337 const ext4_fsblk_t blk = primary->b_blocknr;
338 const unsigned long end = EXT4_SB(sb)->s_groups_count;
339 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;
346 while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
347 if (le32_to_cpu(*p++) !=
348 grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
349 ext4_warning(sb, __FUNCTION__,
350 "reserved GDT %llu"
351 " missing grp %d (%llu)",
352 blk, grp,
353 grp *
354 (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
355 blk);
356 return -EINVAL;
358 if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
359 return -EFBIG;
362 return gdbackups;
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.
373 * Don't need to update the block bitmaps because the blocks are still in use.
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.
378 static int add_new_gdb(handle_t *handle, struct inode *inode,
379 struct ext4_new_group_data *input,
380 struct buffer_head **primary)
382 struct super_block *sb = inode->i_sb;
383 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;
386 struct buffer_head **o_group_desc, **n_group_desc;
387 struct buffer_head *dind;
388 int gdbackups;
389 struct ext4_iloc iloc;
390 __le32 *data;
391 int err;
393 if (test_opt(sb, DEBUG))
394 printk(KERN_DEBUG
395 "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
396 gdb_num);
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.
403 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__,
406 "won't resize using backup superblock at %llu",
407 (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
408 return -EPERM;
411 *primary = sb_bread(sb, gdblock);
412 if (!*primary)
413 return -EIO;
415 if ((gdbackups = verify_reserved_gdb(sb, *primary)) < 0) {
416 err = gdbackups;
417 goto exit_bh;
420 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
421 dind = sb_bread(sb, le32_to_cpu(*data));
422 if (!dind) {
423 err = -EIO;
424 goto exit_bh;
427 data = (__le32 *)dind->b_data;
428 if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
429 ext4_warning(sb, __FUNCTION__,
430 "new group %u GDT block %llu not reserved",
431 input->group, gdblock);
432 err = -EINVAL;
433 goto exit_dind;
436 if ((err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh)))
437 goto exit_dind;
439 if ((err = ext4_journal_get_write_access(handle, *primary)))
440 goto exit_sbh;
442 if ((err = ext4_journal_get_write_access(handle, dind)))
443 goto exit_primary;
445 /* ext4_reserve_inode_write() gets a reference on the iloc */
446 if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
447 goto exit_dindj;
449 n_group_desc = kmalloc((gdb_num + 1) * sizeof(struct buffer_head *),
450 GFP_KERNEL);
451 if (!n_group_desc) {
452 err = -ENOMEM;
453 ext4_warning (sb, __FUNCTION__,
454 "not enough memory for %lu groups", gdb_num + 1);
455 goto exit_inode;
459 * Finally, we have all of the possible failures behind us...
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).
467 data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
468 ext4_journal_dirty_metadata(handle, dind);
469 brelse(dind);
470 inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
471 ext4_mark_iloc_dirty(handle, inode, &iloc);
472 memset((*primary)->b_data, 0, sb->s_blocksize);
473 ext4_journal_dirty_metadata(handle, *primary);
475 o_group_desc = EXT4_SB(sb)->s_group_desc;
476 memcpy(n_group_desc, o_group_desc,
477 EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
478 n_group_desc[gdb_num] = *primary;
479 EXT4_SB(sb)->s_group_desc = n_group_desc;
480 EXT4_SB(sb)->s_gdb_count++;
481 kfree(o_group_desc);
483 es->s_reserved_gdt_blocks =
484 cpu_to_le16(le16_to_cpu(es->s_reserved_gdt_blocks) - 1);
485 ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
487 return 0;
489 exit_inode:
490 //ext4_journal_release_buffer(handle, iloc.bh);
491 brelse(iloc.bh);
492 exit_dindj:
493 //ext4_journal_release_buffer(handle, dind);
494 exit_primary:
495 //ext4_journal_release_buffer(handle, *primary);
496 exit_sbh:
497 //ext4_journal_release_buffer(handle, *primary);
498 exit_dind:
499 brelse(dind);
500 exit_bh:
501 brelse(*primary);
503 ext4_debug("leaving with error %d\n", err);
504 return err;
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.
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.
520 static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
521 struct ext4_new_group_data *input)
523 struct super_block *sb = inode->i_sb;
524 int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
525 struct buffer_head **primary;
526 struct buffer_head *dind;
527 struct ext4_iloc iloc;
528 ext4_fsblk_t blk;
529 __le32 *data, *end;
530 int gdbackups = 0;
531 int res, i;
532 int err;
534 primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_KERNEL);
535 if (!primary)
536 return -ENOMEM;
538 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
539 dind = sb_bread(sb, le32_to_cpu(*data));
540 if (!dind) {
541 err = -EIO;
542 goto exit_free;
545 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);
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) {
552 ext4_warning(sb, __FUNCTION__,
553 "reserved block %llu"
554 " not at offset %ld",
555 blk,
556 (long)(data - (__le32 *)dind->b_data));
557 err = -EINVAL;
558 goto exit_bh;
560 primary[res] = sb_bread(sb, blk);
561 if (!primary[res]) {
562 err = -EIO;
563 goto exit_bh;
565 if ((gdbackups = verify_reserved_gdb(sb, primary[res])) < 0) {
566 brelse(primary[res]);
567 err = gdbackups;
568 goto exit_bh;
570 if (++data >= end)
571 data = (__le32 *)dind->b_data;
574 for (i = 0; i < reserved_gdb; i++) {
575 if ((err = ext4_journal_get_write_access(handle, primary[i]))) {
577 int j;
578 for (j = 0; j < i; j++)
579 ext4_journal_release_buffer(handle, primary[j]);
581 goto exit_bh;
585 if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
586 goto exit_bh;
589 * Finally we can add each of the reserved backup GDT blocks from
590 * the new group to its reserved primary GDT block.
592 blk = input->group * EXT4_BLOCKS_PER_GROUP(sb);
593 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);
600 err2 = ext4_journal_dirty_metadata(handle, primary[i]);
601 if (!err)
602 err = err2;
604 inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
605 ext4_mark_iloc_dirty(handle, inode, &iloc);
607 exit_bh:
608 while (--res >= 0)
609 brelse(primary[res]);
610 brelse(dind);
612 exit_free:
613 kfree(primary);
615 return err;
619 * Update the backup copies of the ext4 metadata. These don't need to be part
620 * 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.
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.
633 static void update_backups(struct super_block *sb,
634 int blk_off, char *data, int size)
636 struct ext4_sb_info *sbi = EXT4_SB(sb);
637 const unsigned long last = sbi->s_groups_count;
638 const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
639 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;
647 handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
648 if (IS_ERR(handle)) {
649 group = 1;
650 err = PTR_ERR(handle);
651 goto exit_err;
654 while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) {
655 struct buffer_head *bh;
657 /* Out of journal space, and can't get more - abort - so sad */
658 if (handle->h_buffer_credits == 0 &&
659 ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) &&
660 (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
661 break;
663 bh = sb_getblk(sb, group * bpg + blk_off);
664 if (!bh) {
665 err = -EIO;
666 break;
668 ext4_debug("update metadata backup %#04lx\n",
669 (unsigned long)bh->b_blocknr);
670 if ((err = ext4_journal_get_write_access(handle, bh)))
671 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);
678 ext4_journal_dirty_metadata(handle, bh);
679 brelse(bh);
681 if ((err2 = ext4_journal_stop(handle)) && !err)
682 err = err2;
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).
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.
694 exit_err:
695 if (err) {
696 ext4_warning(sb, __FUNCTION__,
697 "can't update backup for group %d (err %d), "
698 "forcing fsck on next reboot", group, err);
699 sbi->s_mount_state &= ~EXT4_VALID_FS;
700 sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
701 mark_buffer_dirty(sbi->s_sbh);
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.
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.
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.
718 int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
720 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) ?
723 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
724 struct buffer_head *primary = NULL;
725 struct ext4_group_desc *gdp;
726 struct inode *inode = NULL;
727 handle_t *handle;
728 int gdb_off, gdb_num;
729 int err, err2;
731 gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
732 gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
734 if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb,
735 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
736 ext4_warning(sb, __FUNCTION__,
737 "Can't resize non-sparse filesystem further");
738 return -EPERM;
741 if (ext4_blocks_count(es) + input->blocks_count <
742 ext4_blocks_count(es)) {
743 ext4_warning(sb, __FUNCTION__, "blocks_count overflow\n");
744 return -EINVAL;
747 if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
748 le32_to_cpu(es->s_inodes_count)) {
749 ext4_warning(sb, __FUNCTION__, "inodes_count overflow\n");
750 return -EINVAL;
753 if (reserved_gdb || gdb_off == 0) {
754 if (!EXT4_HAS_COMPAT_FEATURE(sb,
755 EXT4_FEATURE_COMPAT_RESIZE_INODE)){
756 ext4_warning(sb, __FUNCTION__,
757 "No reserved GDT blocks, can't resize");
758 return -EPERM;
760 inode = iget(sb, EXT4_RESIZE_INO);
761 if (!inode || is_bad_inode(inode)) {
762 ext4_warning(sb, __FUNCTION__,
763 "Error opening resize inode");
764 iput(inode);
765 return -ENOENT;
769 if ((err = verify_group_input(sb, input)))
770 goto exit_put;
772 if ((err = setup_new_group_blocks(sb, input)))
773 goto exit_put;
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.
782 handle = ext4_journal_start_sb(sb,
783 ext4_bg_has_super(sb, input->group) ?
784 3 + reserved_gdb : 4);
785 if (IS_ERR(handle)) {
786 err = PTR_ERR(handle);
787 goto exit_put;
790 lock_super(sb);
791 if (input->group != sbi->s_groups_count) {
792 ext4_warning(sb, __FUNCTION__,
793 "multiple resizers run on filesystem!");
794 err = -EBUSY;
795 goto exit_journal;
798 if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh)))
799 goto exit_journal;
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.
807 if (gdb_off) {
808 primary = sbi->s_group_desc[gdb_num];
809 if ((err = ext4_journal_get_write_access(handle, primary)))
810 goto exit_journal;
812 if (reserved_gdb && ext4_bg_num_gdb(sb, input->group) &&
813 (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;
819 * OK, now we've set up the new group. Time to make it active.
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.
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.
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.
837 /* Update group descriptor block for new group */
838 gdp = (struct ext4_group_desc *)primary->b_data + gdb_off;
840 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 */
843 gdp->bg_free_blocks_count = cpu_to_le16(input->free_blocks_count);
844 gdp->bg_free_inodes_count = cpu_to_le16(EXT4_INODES_PER_GROUP(sb));
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.
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.
856 ext4_blocks_count_set(es, ext4_blocks_count(es) +
857 input->blocks_count);
858 es->s_inodes_count = cpu_to_le32(le32_to_cpu(es->s_inodes_count) +
859 EXT4_INODES_PER_GROUP(sb));
862 * We need to protect s_groups_count against other CPUs seeing
863 * inconsistent state in the superblock.
865 * The precise rules we use are:
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
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.
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.
883 smp_wmb();
885 /* Update the global fs size fields */
886 sbi->s_groups_count++;
888 ext4_journal_dirty_metadata(handle, primary);
890 /* Update the reserved block counts only once the new group is
891 * active. */
892 ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
893 input->reserved_blocks);
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,
899 EXT4_INODES_PER_GROUP(sb));
901 ext4_journal_dirty_metadata(handle, sbi->s_sbh);
902 sb->s_dirt = 1;
904 exit_journal:
905 unlock_super(sb);
906 if ((err2 = ext4_journal_stop(handle)) && !err)
907 err = err2;
908 if (!err) {
909 update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
910 sizeof(struct ext4_super_block));
911 update_backups(sb, primary->b_blocknr, primary->b_data,
912 primary->b_size);
914 exit_put:
915 iput(inode);
916 return err;
917 } /* ext4_group_add */
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).
924 * If we _really_ wanted, we could use default values to call ext4_group_add()
925 * allow the "remount" trick to work for arbitrary resizing, assuming enough
926 * GDT blocks are reserved to grow to the desired size.
928 int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
929 ext4_fsblk_t n_blocks_count)
931 ext4_fsblk_t o_blocks_count;
932 unsigned long o_groups_count;
933 ext4_grpblk_t last;
934 ext4_grpblk_t add;
935 struct buffer_head * bh;
936 handle_t *handle;
937 int err;
938 unsigned long freed_blocks;
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. */
943 o_blocks_count = ext4_blocks_count(es);
944 o_groups_count = EXT4_SB(sb)->s_groups_count;
946 if (test_opt(sb, DEBUG))
947 printk(KERN_DEBUG "EXT4-fs: extending last group from %llu uto %llu blocks\n",
948 o_blocks_count, n_blocks_count);
950 if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
951 return 0;
953 if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
954 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
955 " too large to resize to %llu blocks safely\n",
956 sb->s_id, n_blocks_count);
957 if (sizeof(sector_t) < 8)
958 ext4_warning(sb, __FUNCTION__,
959 "CONFIG_LBD not enabled\n");
960 return -EINVAL;
963 if (n_blocks_count < o_blocks_count) {
964 ext4_warning(sb, __FUNCTION__,
965 "can't shrink FS - resize aborted");
966 return -EBUSY;
969 /* Handle the remaining blocks in the last group only. */
970 ext4_get_group_no_and_offset(sb, o_blocks_count, NULL, &last);
972 if (last == 0) {
973 ext4_warning(sb, __FUNCTION__,
974 "need to use ext2online to resize further");
975 return -EPERM;
978 add = EXT4_BLOCKS_PER_GROUP(sb) - last;
980 if (o_blocks_count + add < o_blocks_count) {
981 ext4_warning(sb, __FUNCTION__, "blocks_count overflow");
982 return -EINVAL;
985 if (o_blocks_count + add > n_blocks_count)
986 add = n_blocks_count - o_blocks_count;
988 if (o_blocks_count + add < n_blocks_count)
989 ext4_warning(sb, __FUNCTION__,
990 "will only finish group (%llu"
991 " blocks, %u new)",
992 o_blocks_count + add, add);
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) {
997 ext4_warning(sb, __FUNCTION__,
998 "can't read last block, resize aborted");
999 return -ENOSPC;
1001 brelse(bh);
1003 /* We will update the superblock, one block bitmap, and
1004 * one group descriptor via ext4_free_blocks().
1006 handle = ext4_journal_start_sb(sb, 3);
1007 if (IS_ERR(handle)) {
1008 err = PTR_ERR(handle);
1009 ext4_warning(sb, __FUNCTION__, "error %d on journal start",err);
1010 goto exit_put;
1013 lock_super(sb);
1014 if (o_blocks_count != ext4_blocks_count(es)) {
1015 ext4_warning(sb, __FUNCTION__,
1016 "multiple resizers run on filesystem!");
1017 unlock_super(sb);
1018 err = -EBUSY;
1019 goto exit_put;
1022 if ((err = ext4_journal_get_write_access(handle,
1023 EXT4_SB(sb)->s_sbh))) {
1024 ext4_warning(sb, __FUNCTION__,
1025 "error %d on journal write access", err);
1026 unlock_super(sb);
1027 ext4_journal_stop(handle);
1028 goto exit_put;
1030 ext4_blocks_count_set(es, o_blocks_count + add);
1031 ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
1032 sb->s_dirt = 1;
1033 unlock_super(sb);
1034 ext4_debug("freeing blocks %lu through %llu\n", o_blocks_count,
1035 o_blocks_count + add);
1036 ext4_free_blocks_sb(handle, sb, o_blocks_count, add, &freed_blocks);
1037 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
1038 o_blocks_count + add);
1039 if ((err = ext4_journal_stop(handle)))
1040 goto exit_put;
1041 if (test_opt(sb, DEBUG))
1042 printk(KERN_DEBUG "EXT4-fs: extended group to %llu blocks\n",
1043 ext4_blocks_count(es));
1044 update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es,
1045 sizeof(struct ext4_super_block));
1046 exit_put:
1047 return err;
1048 } /* ext4_group_extend */