sctp: translate host order to network order when setting a hmacid
[linux/fpc-iii.git] / fs / ext4 / resize.c
bloba69bd74ed390eba9479653ad072cdcde0633a246
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/errno.h>
15 #include <linux/slab.h>
17 #include "ext4_jbd2.h"
19 int ext4_resize_begin(struct super_block *sb)
21 int ret = 0;
23 if (!capable(CAP_SYS_RESOURCE))
24 return -EPERM;
27 * We are not allowed to do online-resizing on a filesystem mounted
28 * with error, because it can destroy the filesystem easily.
30 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
31 ext4_warning(sb, "There are errors in the filesystem, "
32 "so online resizing is not allowed\n");
33 return -EPERM;
36 if (test_and_set_bit_lock(EXT4_RESIZING, &EXT4_SB(sb)->s_resize_flags))
37 ret = -EBUSY;
39 return ret;
42 void ext4_resize_end(struct super_block *sb)
44 clear_bit_unlock(EXT4_RESIZING, &EXT4_SB(sb)->s_resize_flags);
45 smp_mb__after_clear_bit();
48 static ext4_group_t ext4_meta_bg_first_group(struct super_block *sb,
49 ext4_group_t group) {
50 return (group >> EXT4_DESC_PER_BLOCK_BITS(sb)) <<
51 EXT4_DESC_PER_BLOCK_BITS(sb);
54 static ext4_fsblk_t ext4_meta_bg_first_block_no(struct super_block *sb,
55 ext4_group_t group) {
56 group = ext4_meta_bg_first_group(sb, group);
57 return ext4_group_first_block_no(sb, group);
60 static ext4_grpblk_t ext4_group_overhead_blocks(struct super_block *sb,
61 ext4_group_t group) {
62 ext4_grpblk_t overhead;
63 overhead = ext4_bg_num_gdb(sb, group);
64 if (ext4_bg_has_super(sb, group))
65 overhead += 1 +
66 le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
67 return overhead;
70 #define outside(b, first, last) ((b) < (first) || (b) >= (last))
71 #define inside(b, first, last) ((b) >= (first) && (b) < (last))
73 static int verify_group_input(struct super_block *sb,
74 struct ext4_new_group_data *input)
76 struct ext4_sb_info *sbi = EXT4_SB(sb);
77 struct ext4_super_block *es = sbi->s_es;
78 ext4_fsblk_t start = ext4_blocks_count(es);
79 ext4_fsblk_t end = start + input->blocks_count;
80 ext4_group_t group = input->group;
81 ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group;
82 unsigned overhead = ext4_group_overhead_blocks(sb, group);
83 ext4_fsblk_t metaend = start + overhead;
84 struct buffer_head *bh = NULL;
85 ext4_grpblk_t free_blocks_count, offset;
86 int err = -EINVAL;
88 input->free_blocks_count = free_blocks_count =
89 input->blocks_count - 2 - overhead - sbi->s_itb_per_group;
91 if (test_opt(sb, DEBUG))
92 printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks "
93 "(%d free, %u reserved)\n",
94 ext4_bg_has_super(sb, input->group) ? "normal" :
95 "no-super", input->group, input->blocks_count,
96 free_blocks_count, input->reserved_blocks);
98 ext4_get_group_no_and_offset(sb, start, NULL, &offset);
99 if (group != sbi->s_groups_count)
100 ext4_warning(sb, "Cannot add at group %u (only %u groups)",
101 input->group, sbi->s_groups_count);
102 else if (offset != 0)
103 ext4_warning(sb, "Last group not full");
104 else if (input->reserved_blocks > input->blocks_count / 5)
105 ext4_warning(sb, "Reserved blocks too high (%u)",
106 input->reserved_blocks);
107 else if (free_blocks_count < 0)
108 ext4_warning(sb, "Bad blocks count %u",
109 input->blocks_count);
110 else if (!(bh = sb_bread(sb, end - 1)))
111 ext4_warning(sb, "Cannot read last block (%llu)",
112 end - 1);
113 else if (outside(input->block_bitmap, start, end))
114 ext4_warning(sb, "Block bitmap not in group (block %llu)",
115 (unsigned long long)input->block_bitmap);
116 else if (outside(input->inode_bitmap, start, end))
117 ext4_warning(sb, "Inode bitmap not in group (block %llu)",
118 (unsigned long long)input->inode_bitmap);
119 else if (outside(input->inode_table, start, end) ||
120 outside(itend - 1, start, end))
121 ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)",
122 (unsigned long long)input->inode_table, itend - 1);
123 else if (input->inode_bitmap == input->block_bitmap)
124 ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)",
125 (unsigned long long)input->block_bitmap);
126 else if (inside(input->block_bitmap, input->inode_table, itend))
127 ext4_warning(sb, "Block bitmap (%llu) in inode table "
128 "(%llu-%llu)",
129 (unsigned long long)input->block_bitmap,
130 (unsigned long long)input->inode_table, itend - 1);
131 else if (inside(input->inode_bitmap, input->inode_table, itend))
132 ext4_warning(sb, "Inode bitmap (%llu) in inode table "
133 "(%llu-%llu)",
134 (unsigned long long)input->inode_bitmap,
135 (unsigned long long)input->inode_table, itend - 1);
136 else if (inside(input->block_bitmap, start, metaend))
137 ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)",
138 (unsigned long long)input->block_bitmap,
139 start, metaend - 1);
140 else if (inside(input->inode_bitmap, start, metaend))
141 ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)",
142 (unsigned long long)input->inode_bitmap,
143 start, metaend - 1);
144 else if (inside(input->inode_table, start, metaend) ||
145 inside(itend - 1, start, metaend))
146 ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table "
147 "(%llu-%llu)",
148 (unsigned long long)input->inode_table,
149 itend - 1, start, metaend - 1);
150 else
151 err = 0;
152 brelse(bh);
154 return err;
158 * ext4_new_flex_group_data is used by 64bit-resize interface to add a flex
159 * group each time.
161 struct ext4_new_flex_group_data {
162 struct ext4_new_group_data *groups; /* new_group_data for groups
163 in the flex group */
164 __u16 *bg_flags; /* block group flags of groups
165 in @groups */
166 ext4_group_t count; /* number of groups in @groups
171 * alloc_flex_gd() allocates a ext4_new_flex_group_data with size of
172 * @flexbg_size.
174 * Returns NULL on failure otherwise address of the allocated structure.
176 static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size)
178 struct ext4_new_flex_group_data *flex_gd;
180 flex_gd = kmalloc(sizeof(*flex_gd), GFP_NOFS);
181 if (flex_gd == NULL)
182 goto out3;
184 if (flexbg_size >= UINT_MAX / sizeof(struct ext4_new_flex_group_data))
185 goto out2;
186 flex_gd->count = flexbg_size;
188 flex_gd->groups = kmalloc(sizeof(struct ext4_new_group_data) *
189 flexbg_size, GFP_NOFS);
190 if (flex_gd->groups == NULL)
191 goto out2;
193 flex_gd->bg_flags = kmalloc(flexbg_size * sizeof(__u16), GFP_NOFS);
194 if (flex_gd->bg_flags == NULL)
195 goto out1;
197 return flex_gd;
199 out1:
200 kfree(flex_gd->groups);
201 out2:
202 kfree(flex_gd);
203 out3:
204 return NULL;
207 static void free_flex_gd(struct ext4_new_flex_group_data *flex_gd)
209 kfree(flex_gd->bg_flags);
210 kfree(flex_gd->groups);
211 kfree(flex_gd);
215 * ext4_alloc_group_tables() allocates block bitmaps, inode bitmaps
216 * and inode tables for a flex group.
218 * This function is used by 64bit-resize. Note that this function allocates
219 * group tables from the 1st group of groups contained by @flexgd, which may
220 * be a partial of a flex group.
222 * @sb: super block of fs to which the groups belongs
224 * Returns 0 on a successful allocation of the metadata blocks in the
225 * block group.
227 static int ext4_alloc_group_tables(struct super_block *sb,
228 struct ext4_new_flex_group_data *flex_gd,
229 int flexbg_size)
231 struct ext4_new_group_data *group_data = flex_gd->groups;
232 ext4_fsblk_t start_blk;
233 ext4_fsblk_t last_blk;
234 ext4_group_t src_group;
235 ext4_group_t bb_index = 0;
236 ext4_group_t ib_index = 0;
237 ext4_group_t it_index = 0;
238 ext4_group_t group;
239 ext4_group_t last_group;
240 unsigned overhead;
241 __u16 uninit_mask = (flexbg_size > 1) ? ~EXT4_BG_BLOCK_UNINIT : ~0;
243 BUG_ON(flex_gd->count == 0 || group_data == NULL);
245 src_group = group_data[0].group;
246 last_group = src_group + flex_gd->count - 1;
248 BUG_ON((flexbg_size > 1) && ((src_group & ~(flexbg_size - 1)) !=
249 (last_group & ~(flexbg_size - 1))));
250 next_group:
251 group = group_data[0].group;
252 if (src_group >= group_data[0].group + flex_gd->count)
253 return -ENOSPC;
254 start_blk = ext4_group_first_block_no(sb, src_group);
255 last_blk = start_blk + group_data[src_group - group].blocks_count;
257 overhead = ext4_group_overhead_blocks(sb, src_group);
259 start_blk += overhead;
261 /* We collect contiguous blocks as much as possible. */
262 src_group++;
263 for (; src_group <= last_group; src_group++) {
264 overhead = ext4_group_overhead_blocks(sb, src_group);
265 if (overhead == 0)
266 last_blk += group_data[src_group - group].blocks_count;
267 else
268 break;
271 /* Allocate block bitmaps */
272 for (; bb_index < flex_gd->count; bb_index++) {
273 if (start_blk >= last_blk)
274 goto next_group;
275 group_data[bb_index].block_bitmap = start_blk++;
276 group = ext4_get_group_number(sb, start_blk - 1);
277 group -= group_data[0].group;
278 group_data[group].free_blocks_count--;
279 flex_gd->bg_flags[group] &= uninit_mask;
282 /* Allocate inode bitmaps */
283 for (; ib_index < flex_gd->count; ib_index++) {
284 if (start_blk >= last_blk)
285 goto next_group;
286 group_data[ib_index].inode_bitmap = start_blk++;
287 group = ext4_get_group_number(sb, start_blk - 1);
288 group -= group_data[0].group;
289 group_data[group].free_blocks_count--;
290 flex_gd->bg_flags[group] &= uninit_mask;
293 /* Allocate inode tables */
294 for (; it_index < flex_gd->count; it_index++) {
295 unsigned int itb = EXT4_SB(sb)->s_itb_per_group;
296 ext4_fsblk_t next_group_start;
298 if (start_blk + itb > last_blk)
299 goto next_group;
300 group_data[it_index].inode_table = start_blk;
301 group = ext4_get_group_number(sb, start_blk);
302 next_group_start = ext4_group_first_block_no(sb, group + 1);
303 group -= group_data[0].group;
305 if (start_blk + itb > next_group_start) {
306 flex_gd->bg_flags[group + 1] &= uninit_mask;
307 overhead = start_blk + itb - next_group_start;
308 group_data[group + 1].free_blocks_count -= overhead;
309 itb -= overhead;
312 group_data[group].free_blocks_count -= itb;
313 flex_gd->bg_flags[group] &= uninit_mask;
314 start_blk += EXT4_SB(sb)->s_itb_per_group;
317 if (test_opt(sb, DEBUG)) {
318 int i;
319 group = group_data[0].group;
321 printk(KERN_DEBUG "EXT4-fs: adding a flex group with "
322 "%d groups, flexbg size is %d:\n", flex_gd->count,
323 flexbg_size);
325 for (i = 0; i < flex_gd->count; i++) {
326 printk(KERN_DEBUG "adding %s group %u: %u "
327 "blocks (%d free)\n",
328 ext4_bg_has_super(sb, group + i) ? "normal" :
329 "no-super", group + i,
330 group_data[i].blocks_count,
331 group_data[i].free_blocks_count);
334 return 0;
337 static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
338 ext4_fsblk_t blk)
340 struct buffer_head *bh;
341 int err;
343 bh = sb_getblk(sb, blk);
344 if (unlikely(!bh))
345 return ERR_PTR(-ENOMEM);
346 if ((err = ext4_journal_get_write_access(handle, bh))) {
347 brelse(bh);
348 bh = ERR_PTR(err);
349 } else {
350 memset(bh->b_data, 0, sb->s_blocksize);
351 set_buffer_uptodate(bh);
354 return bh;
358 * If we have fewer than thresh credits, extend by EXT4_MAX_TRANS_DATA.
359 * If that fails, restart the transaction & regain write access for the
360 * buffer head which is used for block_bitmap modifications.
362 static int extend_or_restart_transaction(handle_t *handle, int thresh)
364 int err;
366 if (ext4_handle_has_enough_credits(handle, thresh))
367 return 0;
369 err = ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA);
370 if (err < 0)
371 return err;
372 if (err) {
373 err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA);
374 if (err)
375 return err;
378 return 0;
382 * set_flexbg_block_bitmap() mark @count blocks starting from @block used.
384 * Helper function for ext4_setup_new_group_blocks() which set .
386 * @sb: super block
387 * @handle: journal handle
388 * @flex_gd: flex group data
390 static int set_flexbg_block_bitmap(struct super_block *sb, handle_t *handle,
391 struct ext4_new_flex_group_data *flex_gd,
392 ext4_fsblk_t block, ext4_group_t count)
394 ext4_group_t count2;
396 ext4_debug("mark blocks [%llu/%u] used\n", block, count);
397 for (count2 = count; count > 0; count -= count2, block += count2) {
398 ext4_fsblk_t start;
399 struct buffer_head *bh;
400 ext4_group_t group;
401 int err;
403 group = ext4_get_group_number(sb, block);
404 start = ext4_group_first_block_no(sb, group);
405 group -= flex_gd->groups[0].group;
407 count2 = EXT4_BLOCKS_PER_GROUP(sb) - (block - start);
408 if (count2 > count)
409 count2 = count;
411 if (flex_gd->bg_flags[group] & EXT4_BG_BLOCK_UNINIT) {
412 BUG_ON(flex_gd->count > 1);
413 continue;
416 err = extend_or_restart_transaction(handle, 1);
417 if (err)
418 return err;
420 bh = sb_getblk(sb, flex_gd->groups[group].block_bitmap);
421 if (unlikely(!bh))
422 return -ENOMEM;
424 err = ext4_journal_get_write_access(handle, bh);
425 if (err)
426 return err;
427 ext4_debug("mark block bitmap %#04llx (+%llu/%u)\n", block,
428 block - start, count2);
429 ext4_set_bits(bh->b_data, block - start, count2);
431 err = ext4_handle_dirty_metadata(handle, NULL, bh);
432 if (unlikely(err))
433 return err;
434 brelse(bh);
437 return 0;
441 * Set up the block and inode bitmaps, and the inode table for the new groups.
442 * This doesn't need to be part of the main transaction, since we are only
443 * changing blocks outside the actual filesystem. We still do journaling to
444 * ensure the recovery is correct in case of a failure just after resize.
445 * If any part of this fails, we simply abort the resize.
447 * setup_new_flex_group_blocks handles a flex group as follow:
448 * 1. copy super block and GDT, and initialize group tables if necessary.
449 * In this step, we only set bits in blocks bitmaps for blocks taken by
450 * super block and GDT.
451 * 2. allocate group tables in block bitmaps, that is, set bits in block
452 * bitmap for blocks taken by group tables.
454 static int setup_new_flex_group_blocks(struct super_block *sb,
455 struct ext4_new_flex_group_data *flex_gd)
457 int group_table_count[] = {1, 1, EXT4_SB(sb)->s_itb_per_group};
458 ext4_fsblk_t start;
459 ext4_fsblk_t block;
460 struct ext4_sb_info *sbi = EXT4_SB(sb);
461 struct ext4_super_block *es = sbi->s_es;
462 struct ext4_new_group_data *group_data = flex_gd->groups;
463 __u16 *bg_flags = flex_gd->bg_flags;
464 handle_t *handle;
465 ext4_group_t group, count;
466 struct buffer_head *bh = NULL;
467 int reserved_gdb, i, j, err = 0, err2;
468 int meta_bg;
470 BUG_ON(!flex_gd->count || !group_data ||
471 group_data[0].group != sbi->s_groups_count);
473 reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks);
474 meta_bg = EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG);
476 /* This transaction may be extended/restarted along the way */
477 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA);
478 if (IS_ERR(handle))
479 return PTR_ERR(handle);
481 group = group_data[0].group;
482 for (i = 0; i < flex_gd->count; i++, group++) {
483 unsigned long gdblocks;
484 ext4_grpblk_t overhead;
486 gdblocks = ext4_bg_num_gdb(sb, group);
487 start = ext4_group_first_block_no(sb, group);
489 if (meta_bg == 0 && !ext4_bg_has_super(sb, group))
490 goto handle_itb;
492 if (meta_bg == 1) {
493 ext4_group_t first_group;
494 first_group = ext4_meta_bg_first_group(sb, group);
495 if (first_group != group + 1 &&
496 first_group != group + EXT4_DESC_PER_BLOCK(sb) - 1)
497 goto handle_itb;
500 block = start + ext4_bg_has_super(sb, group);
501 /* Copy all of the GDT blocks into the backup in this group */
502 for (j = 0; j < gdblocks; j++, block++) {
503 struct buffer_head *gdb;
505 ext4_debug("update backup group %#04llx\n", block);
506 err = extend_or_restart_transaction(handle, 1);
507 if (err)
508 goto out;
510 gdb = sb_getblk(sb, block);
511 if (unlikely(!gdb)) {
512 err = -ENOMEM;
513 goto out;
516 err = ext4_journal_get_write_access(handle, gdb);
517 if (err) {
518 brelse(gdb);
519 goto out;
521 memcpy(gdb->b_data, sbi->s_group_desc[j]->b_data,
522 gdb->b_size);
523 set_buffer_uptodate(gdb);
525 err = ext4_handle_dirty_metadata(handle, NULL, gdb);
526 if (unlikely(err)) {
527 brelse(gdb);
528 goto out;
530 brelse(gdb);
533 /* Zero out all of the reserved backup group descriptor
534 * table blocks
536 if (ext4_bg_has_super(sb, group)) {
537 err = sb_issue_zeroout(sb, gdblocks + start + 1,
538 reserved_gdb, GFP_NOFS);
539 if (err)
540 goto out;
543 handle_itb:
544 /* Initialize group tables of the grop @group */
545 if (!(bg_flags[i] & EXT4_BG_INODE_ZEROED))
546 goto handle_bb;
548 /* Zero out all of the inode table blocks */
549 block = group_data[i].inode_table;
550 ext4_debug("clear inode table blocks %#04llx -> %#04lx\n",
551 block, sbi->s_itb_per_group);
552 err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group,
553 GFP_NOFS);
554 if (err)
555 goto out;
557 handle_bb:
558 if (bg_flags[i] & EXT4_BG_BLOCK_UNINIT)
559 goto handle_ib;
561 /* Initialize block bitmap of the @group */
562 block = group_data[i].block_bitmap;
563 err = extend_or_restart_transaction(handle, 1);
564 if (err)
565 goto out;
567 bh = bclean(handle, sb, block);
568 if (IS_ERR(bh)) {
569 err = PTR_ERR(bh);
570 goto out;
572 overhead = ext4_group_overhead_blocks(sb, group);
573 if (overhead != 0) {
574 ext4_debug("mark backup superblock %#04llx (+0)\n",
575 start);
576 ext4_set_bits(bh->b_data, 0, overhead);
578 ext4_mark_bitmap_end(group_data[i].blocks_count,
579 sb->s_blocksize * 8, bh->b_data);
580 err = ext4_handle_dirty_metadata(handle, NULL, bh);
581 if (err)
582 goto out;
583 brelse(bh);
585 handle_ib:
586 if (bg_flags[i] & EXT4_BG_INODE_UNINIT)
587 continue;
589 /* Initialize inode bitmap of the @group */
590 block = group_data[i].inode_bitmap;
591 err = extend_or_restart_transaction(handle, 1);
592 if (err)
593 goto out;
594 /* Mark unused entries in inode bitmap used */
595 bh = bclean(handle, sb, block);
596 if (IS_ERR(bh)) {
597 err = PTR_ERR(bh);
598 goto out;
601 ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb),
602 sb->s_blocksize * 8, bh->b_data);
603 err = ext4_handle_dirty_metadata(handle, NULL, bh);
604 if (err)
605 goto out;
606 brelse(bh);
608 bh = NULL;
610 /* Mark group tables in block bitmap */
611 for (j = 0; j < GROUP_TABLE_COUNT; j++) {
612 count = group_table_count[j];
613 start = (&group_data[0].block_bitmap)[j];
614 block = start;
615 for (i = 1; i < flex_gd->count; i++) {
616 block += group_table_count[j];
617 if (block == (&group_data[i].block_bitmap)[j]) {
618 count += group_table_count[j];
619 continue;
621 err = set_flexbg_block_bitmap(sb, handle,
622 flex_gd, start, count);
623 if (err)
624 goto out;
625 count = group_table_count[j];
626 start = (&group_data[i].block_bitmap)[j];
627 block = start;
630 if (count) {
631 err = set_flexbg_block_bitmap(sb, handle,
632 flex_gd, start, count);
633 if (err)
634 goto out;
638 out:
639 brelse(bh);
640 err2 = ext4_journal_stop(handle);
641 if (err2 && !err)
642 err = err2;
644 return err;
648 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
649 * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
650 * calling this for the first time. In a sparse filesystem it will be the
651 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
652 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
654 static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
655 unsigned *five, unsigned *seven)
657 unsigned *min = three;
658 int mult = 3;
659 unsigned ret;
661 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
662 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
663 ret = *min;
664 *min += 1;
665 return ret;
668 if (*five < *min) {
669 min = five;
670 mult = 5;
672 if (*seven < *min) {
673 min = seven;
674 mult = 7;
677 ret = *min;
678 *min *= mult;
680 return ret;
684 * Check that all of the backup GDT blocks are held in the primary GDT block.
685 * It is assumed that they are stored in group order. Returns the number of
686 * groups in current filesystem that have BACKUPS, or -ve error code.
688 static int verify_reserved_gdb(struct super_block *sb,
689 ext4_group_t end,
690 struct buffer_head *primary)
692 const ext4_fsblk_t blk = primary->b_blocknr;
693 unsigned three = 1;
694 unsigned five = 5;
695 unsigned seven = 7;
696 unsigned grp;
697 __le32 *p = (__le32 *)primary->b_data;
698 int gdbackups = 0;
700 while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
701 if (le32_to_cpu(*p++) !=
702 grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
703 ext4_warning(sb, "reserved GDT %llu"
704 " missing grp %d (%llu)",
705 blk, grp,
706 grp *
707 (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
708 blk);
709 return -EINVAL;
711 if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
712 return -EFBIG;
715 return gdbackups;
719 * Called when we need to bring a reserved group descriptor table block into
720 * use from the resize inode. The primary copy of the new GDT block currently
721 * is an indirect block (under the double indirect block in the resize inode).
722 * The new backup GDT blocks will be stored as leaf blocks in this indirect
723 * block, in group order. Even though we know all the block numbers we need,
724 * we check to ensure that the resize inode has actually reserved these blocks.
726 * Don't need to update the block bitmaps because the blocks are still in use.
728 * We get all of the error cases out of the way, so that we are sure to not
729 * fail once we start modifying the data on disk, because JBD has no rollback.
731 static int add_new_gdb(handle_t *handle, struct inode *inode,
732 ext4_group_t group)
734 struct super_block *sb = inode->i_sb;
735 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
736 unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
737 ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
738 struct buffer_head **o_group_desc, **n_group_desc;
739 struct buffer_head *dind;
740 struct buffer_head *gdb_bh;
741 int gdbackups;
742 struct ext4_iloc iloc;
743 __le32 *data;
744 int err;
746 if (test_opt(sb, DEBUG))
747 printk(KERN_DEBUG
748 "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
749 gdb_num);
752 * If we are not using the primary superblock/GDT copy don't resize,
753 * because the user tools have no way of handling this. Probably a
754 * bad time to do it anyways.
756 if (EXT4_SB(sb)->s_sbh->b_blocknr !=
757 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
758 ext4_warning(sb, "won't resize using backup superblock at %llu",
759 (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
760 return -EPERM;
763 gdb_bh = sb_bread(sb, gdblock);
764 if (!gdb_bh)
765 return -EIO;
767 gdbackups = verify_reserved_gdb(sb, group, gdb_bh);
768 if (gdbackups < 0) {
769 err = gdbackups;
770 goto exit_bh;
773 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
774 dind = sb_bread(sb, le32_to_cpu(*data));
775 if (!dind) {
776 err = -EIO;
777 goto exit_bh;
780 data = (__le32 *)dind->b_data;
781 if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
782 ext4_warning(sb, "new group %u GDT block %llu not reserved",
783 group, gdblock);
784 err = -EINVAL;
785 goto exit_dind;
788 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
789 if (unlikely(err))
790 goto exit_dind;
792 err = ext4_journal_get_write_access(handle, gdb_bh);
793 if (unlikely(err))
794 goto exit_dind;
796 err = ext4_journal_get_write_access(handle, dind);
797 if (unlikely(err))
798 ext4_std_error(sb, err);
800 /* ext4_reserve_inode_write() gets a reference on the iloc */
801 err = ext4_reserve_inode_write(handle, inode, &iloc);
802 if (unlikely(err))
803 goto exit_dind;
805 n_group_desc = ext4_kvmalloc((gdb_num + 1) *
806 sizeof(struct buffer_head *),
807 GFP_NOFS);
808 if (!n_group_desc) {
809 err = -ENOMEM;
810 ext4_warning(sb, "not enough memory for %lu groups",
811 gdb_num + 1);
812 goto exit_inode;
816 * Finally, we have all of the possible failures behind us...
818 * Remove new GDT block from inode double-indirect block and clear out
819 * the new GDT block for use (which also "frees" the backup GDT blocks
820 * from the reserved inode). We don't need to change the bitmaps for
821 * these blocks, because they are marked as in-use from being in the
822 * reserved inode, and will become GDT blocks (primary and backup).
824 data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
825 err = ext4_handle_dirty_metadata(handle, NULL, dind);
826 if (unlikely(err)) {
827 ext4_std_error(sb, err);
828 goto exit_inode;
830 inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
831 ext4_mark_iloc_dirty(handle, inode, &iloc);
832 memset(gdb_bh->b_data, 0, sb->s_blocksize);
833 err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
834 if (unlikely(err)) {
835 ext4_std_error(sb, err);
836 goto exit_inode;
838 brelse(dind);
840 o_group_desc = EXT4_SB(sb)->s_group_desc;
841 memcpy(n_group_desc, o_group_desc,
842 EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
843 n_group_desc[gdb_num] = gdb_bh;
844 EXT4_SB(sb)->s_group_desc = n_group_desc;
845 EXT4_SB(sb)->s_gdb_count++;
846 ext4_kvfree(o_group_desc);
848 le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
849 err = ext4_handle_dirty_super(handle, sb);
850 if (err)
851 ext4_std_error(sb, err);
853 return err;
855 exit_inode:
856 ext4_kvfree(n_group_desc);
857 brelse(iloc.bh);
858 exit_dind:
859 brelse(dind);
860 exit_bh:
861 brelse(gdb_bh);
863 ext4_debug("leaving with error %d\n", err);
864 return err;
868 * add_new_gdb_meta_bg is the sister of add_new_gdb.
870 static int add_new_gdb_meta_bg(struct super_block *sb,
871 handle_t *handle, ext4_group_t group) {
872 ext4_fsblk_t gdblock;
873 struct buffer_head *gdb_bh;
874 struct buffer_head **o_group_desc, **n_group_desc;
875 unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
876 int err;
878 gdblock = ext4_meta_bg_first_block_no(sb, group) +
879 ext4_bg_has_super(sb, group);
880 gdb_bh = sb_bread(sb, gdblock);
881 if (!gdb_bh)
882 return -EIO;
883 n_group_desc = ext4_kvmalloc((gdb_num + 1) *
884 sizeof(struct buffer_head *),
885 GFP_NOFS);
886 if (!n_group_desc) {
887 err = -ENOMEM;
888 ext4_warning(sb, "not enough memory for %lu groups",
889 gdb_num + 1);
890 return err;
893 o_group_desc = EXT4_SB(sb)->s_group_desc;
894 memcpy(n_group_desc, o_group_desc,
895 EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
896 n_group_desc[gdb_num] = gdb_bh;
897 EXT4_SB(sb)->s_group_desc = n_group_desc;
898 EXT4_SB(sb)->s_gdb_count++;
899 ext4_kvfree(o_group_desc);
900 err = ext4_journal_get_write_access(handle, gdb_bh);
901 if (unlikely(err))
902 brelse(gdb_bh);
903 return err;
907 * Called when we are adding a new group which has a backup copy of each of
908 * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
909 * We need to add these reserved backup GDT blocks to the resize inode, so
910 * that they are kept for future resizing and not allocated to files.
912 * Each reserved backup GDT block will go into a different indirect block.
913 * The indirect blocks are actually the primary reserved GDT blocks,
914 * so we know in advance what their block numbers are. We only get the
915 * double-indirect block to verify it is pointing to the primary reserved
916 * GDT blocks so we don't overwrite a data block by accident. The reserved
917 * backup GDT blocks are stored in their reserved primary GDT block.
919 static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
920 ext4_group_t group)
922 struct super_block *sb = inode->i_sb;
923 int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
924 struct buffer_head **primary;
925 struct buffer_head *dind;
926 struct ext4_iloc iloc;
927 ext4_fsblk_t blk;
928 __le32 *data, *end;
929 int gdbackups = 0;
930 int res, i;
931 int err;
933 primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_NOFS);
934 if (!primary)
935 return -ENOMEM;
937 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
938 dind = sb_bread(sb, le32_to_cpu(*data));
939 if (!dind) {
940 err = -EIO;
941 goto exit_free;
944 blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count;
945 data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count %
946 EXT4_ADDR_PER_BLOCK(sb));
947 end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb);
949 /* Get each reserved primary GDT block and verify it holds backups */
950 for (res = 0; res < reserved_gdb; res++, blk++) {
951 if (le32_to_cpu(*data) != blk) {
952 ext4_warning(sb, "reserved block %llu"
953 " not at offset %ld",
954 blk,
955 (long)(data - (__le32 *)dind->b_data));
956 err = -EINVAL;
957 goto exit_bh;
959 primary[res] = sb_bread(sb, blk);
960 if (!primary[res]) {
961 err = -EIO;
962 goto exit_bh;
964 gdbackups = verify_reserved_gdb(sb, group, primary[res]);
965 if (gdbackups < 0) {
966 brelse(primary[res]);
967 err = gdbackups;
968 goto exit_bh;
970 if (++data >= end)
971 data = (__le32 *)dind->b_data;
974 for (i = 0; i < reserved_gdb; i++) {
975 if ((err = ext4_journal_get_write_access(handle, primary[i])))
976 goto exit_bh;
979 if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
980 goto exit_bh;
983 * Finally we can add each of the reserved backup GDT blocks from
984 * the new group to its reserved primary GDT block.
986 blk = group * EXT4_BLOCKS_PER_GROUP(sb);
987 for (i = 0; i < reserved_gdb; i++) {
988 int err2;
989 data = (__le32 *)primary[i]->b_data;
990 /* printk("reserving backup %lu[%u] = %lu\n",
991 primary[i]->b_blocknr, gdbackups,
992 blk + primary[i]->b_blocknr); */
993 data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
994 err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]);
995 if (!err)
996 err = err2;
998 inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
999 ext4_mark_iloc_dirty(handle, inode, &iloc);
1001 exit_bh:
1002 while (--res >= 0)
1003 brelse(primary[res]);
1004 brelse(dind);
1006 exit_free:
1007 kfree(primary);
1009 return err;
1013 * Update the backup copies of the ext4 metadata. These don't need to be part
1014 * of the main resize transaction, because e2fsck will re-write them if there
1015 * is a problem (basically only OOM will cause a problem). However, we
1016 * _should_ update the backups if possible, in case the primary gets trashed
1017 * for some reason and we need to run e2fsck from a backup superblock. The
1018 * important part is that the new block and inode counts are in the backup
1019 * superblocks, and the location of the new group metadata in the GDT backups.
1021 * We do not need take the s_resize_lock for this, because these
1022 * blocks are not otherwise touched by the filesystem code when it is
1023 * mounted. We don't need to worry about last changing from
1024 * sbi->s_groups_count, because the worst that can happen is that we
1025 * do not copy the full number of backups at this time. The resize
1026 * which changed s_groups_count will backup again.
1028 static void update_backups(struct super_block *sb, int blk_off, char *data,
1029 int size, int meta_bg)
1031 struct ext4_sb_info *sbi = EXT4_SB(sb);
1032 ext4_group_t last;
1033 const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
1034 unsigned three = 1;
1035 unsigned five = 5;
1036 unsigned seven = 7;
1037 ext4_group_t group = 0;
1038 int rest = sb->s_blocksize - size;
1039 handle_t *handle;
1040 int err = 0, err2;
1042 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA);
1043 if (IS_ERR(handle)) {
1044 group = 1;
1045 err = PTR_ERR(handle);
1046 goto exit_err;
1049 if (meta_bg == 0) {
1050 group = ext4_list_backups(sb, &three, &five, &seven);
1051 last = sbi->s_groups_count;
1052 } else {
1053 group = ext4_meta_bg_first_group(sb, group) + 1;
1054 last = (ext4_group_t)(group + EXT4_DESC_PER_BLOCK(sb) - 2);
1057 while (group < sbi->s_groups_count) {
1058 struct buffer_head *bh;
1059 ext4_fsblk_t backup_block;
1061 /* Out of journal space, and can't get more - abort - so sad */
1062 if (ext4_handle_valid(handle) &&
1063 handle->h_buffer_credits == 0 &&
1064 ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) &&
1065 (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
1066 break;
1068 if (meta_bg == 0)
1069 backup_block = ((ext4_fsblk_t)group) * bpg + blk_off;
1070 else
1071 backup_block = (ext4_group_first_block_no(sb, group) +
1072 ext4_bg_has_super(sb, group));
1074 bh = sb_getblk(sb, backup_block);
1075 if (unlikely(!bh)) {
1076 err = -ENOMEM;
1077 break;
1079 ext4_debug("update metadata backup %llu(+%llu)\n",
1080 backup_block, backup_block -
1081 ext4_group_first_block_no(sb, group));
1082 if ((err = ext4_journal_get_write_access(handle, bh)))
1083 break;
1084 lock_buffer(bh);
1085 memcpy(bh->b_data, data, size);
1086 if (rest)
1087 memset(bh->b_data + size, 0, rest);
1088 set_buffer_uptodate(bh);
1089 unlock_buffer(bh);
1090 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1091 if (unlikely(err))
1092 ext4_std_error(sb, err);
1093 brelse(bh);
1095 if (meta_bg == 0)
1096 group = ext4_list_backups(sb, &three, &five, &seven);
1097 else if (group == last)
1098 break;
1099 else
1100 group = last;
1102 if ((err2 = ext4_journal_stop(handle)) && !err)
1103 err = err2;
1106 * Ugh! Need to have e2fsck write the backup copies. It is too
1107 * late to revert the resize, we shouldn't fail just because of
1108 * the backup copies (they are only needed in case of corruption).
1110 * However, if we got here we have a journal problem too, so we
1111 * can't really start a transaction to mark the superblock.
1112 * Chicken out and just set the flag on the hope it will be written
1113 * to disk, and if not - we will simply wait until next fsck.
1115 exit_err:
1116 if (err) {
1117 ext4_warning(sb, "can't update backup for group %u (err %d), "
1118 "forcing fsck on next reboot", group, err);
1119 sbi->s_mount_state &= ~EXT4_VALID_FS;
1120 sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1121 mark_buffer_dirty(sbi->s_sbh);
1126 * ext4_add_new_descs() adds @count group descriptor of groups
1127 * starting at @group
1129 * @handle: journal handle
1130 * @sb: super block
1131 * @group: the group no. of the first group desc to be added
1132 * @resize_inode: the resize inode
1133 * @count: number of group descriptors to be added
1135 static int ext4_add_new_descs(handle_t *handle, struct super_block *sb,
1136 ext4_group_t group, struct inode *resize_inode,
1137 ext4_group_t count)
1139 struct ext4_sb_info *sbi = EXT4_SB(sb);
1140 struct ext4_super_block *es = sbi->s_es;
1141 struct buffer_head *gdb_bh;
1142 int i, gdb_off, gdb_num, err = 0;
1143 int meta_bg;
1145 meta_bg = EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG);
1146 for (i = 0; i < count; i++, group++) {
1147 int reserved_gdb = ext4_bg_has_super(sb, group) ?
1148 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
1150 gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
1151 gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
1154 * We will only either add reserved group blocks to a backup group
1155 * or remove reserved blocks for the first group in a new group block.
1156 * Doing both would be mean more complex code, and sane people don't
1157 * use non-sparse filesystems anymore. This is already checked above.
1159 if (gdb_off) {
1160 gdb_bh = sbi->s_group_desc[gdb_num];
1161 err = ext4_journal_get_write_access(handle, gdb_bh);
1163 if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group))
1164 err = reserve_backup_gdb(handle, resize_inode, group);
1165 } else if (meta_bg != 0) {
1166 err = add_new_gdb_meta_bg(sb, handle, group);
1167 } else {
1168 err = add_new_gdb(handle, resize_inode, group);
1170 if (err)
1171 break;
1173 return err;
1176 static struct buffer_head *ext4_get_bitmap(struct super_block *sb, __u64 block)
1178 struct buffer_head *bh = sb_getblk(sb, block);
1179 if (unlikely(!bh))
1180 return NULL;
1181 if (!bh_uptodate_or_lock(bh)) {
1182 if (bh_submit_read(bh) < 0) {
1183 brelse(bh);
1184 return NULL;
1188 return bh;
1191 static int ext4_set_bitmap_checksums(struct super_block *sb,
1192 ext4_group_t group,
1193 struct ext4_group_desc *gdp,
1194 struct ext4_new_group_data *group_data)
1196 struct buffer_head *bh;
1198 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
1199 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1200 return 0;
1202 bh = ext4_get_bitmap(sb, group_data->inode_bitmap);
1203 if (!bh)
1204 return -EIO;
1205 ext4_inode_bitmap_csum_set(sb, group, gdp, bh,
1206 EXT4_INODES_PER_GROUP(sb) / 8);
1207 brelse(bh);
1209 bh = ext4_get_bitmap(sb, group_data->block_bitmap);
1210 if (!bh)
1211 return -EIO;
1212 ext4_block_bitmap_csum_set(sb, group, gdp, bh);
1213 brelse(bh);
1215 return 0;
1219 * ext4_setup_new_descs() will set up the group descriptor descriptors of a flex bg
1221 static int ext4_setup_new_descs(handle_t *handle, struct super_block *sb,
1222 struct ext4_new_flex_group_data *flex_gd)
1224 struct ext4_new_group_data *group_data = flex_gd->groups;
1225 struct ext4_group_desc *gdp;
1226 struct ext4_sb_info *sbi = EXT4_SB(sb);
1227 struct buffer_head *gdb_bh;
1228 ext4_group_t group;
1229 __u16 *bg_flags = flex_gd->bg_flags;
1230 int i, gdb_off, gdb_num, err = 0;
1233 for (i = 0; i < flex_gd->count; i++, group_data++, bg_flags++) {
1234 group = group_data->group;
1236 gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
1237 gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
1240 * get_write_access() has been called on gdb_bh by ext4_add_new_desc().
1242 gdb_bh = sbi->s_group_desc[gdb_num];
1243 /* Update group descriptor block for new group */
1244 gdp = (struct ext4_group_desc *)(gdb_bh->b_data +
1245 gdb_off * EXT4_DESC_SIZE(sb));
1247 memset(gdp, 0, EXT4_DESC_SIZE(sb));
1248 ext4_block_bitmap_set(sb, gdp, group_data->block_bitmap);
1249 ext4_inode_bitmap_set(sb, gdp, group_data->inode_bitmap);
1250 err = ext4_set_bitmap_checksums(sb, group, gdp, group_data);
1251 if (err) {
1252 ext4_std_error(sb, err);
1253 break;
1256 ext4_inode_table_set(sb, gdp, group_data->inode_table);
1257 ext4_free_group_clusters_set(sb, gdp,
1258 EXT4_NUM_B2C(sbi, group_data->free_blocks_count));
1259 ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
1260 if (ext4_has_group_desc_csum(sb))
1261 ext4_itable_unused_set(sb, gdp,
1262 EXT4_INODES_PER_GROUP(sb));
1263 gdp->bg_flags = cpu_to_le16(*bg_flags);
1264 ext4_group_desc_csum_set(sb, group, gdp);
1266 err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
1267 if (unlikely(err)) {
1268 ext4_std_error(sb, err);
1269 break;
1273 * We can allocate memory for mb_alloc based on the new group
1274 * descriptor
1276 err = ext4_mb_add_groupinfo(sb, group, gdp);
1277 if (err)
1278 break;
1280 return err;
1284 * ext4_update_super() updates the super block so that the newly added
1285 * groups can be seen by the filesystem.
1287 * @sb: super block
1288 * @flex_gd: new added groups
1290 static void ext4_update_super(struct super_block *sb,
1291 struct ext4_new_flex_group_data *flex_gd)
1293 ext4_fsblk_t blocks_count = 0;
1294 ext4_fsblk_t free_blocks = 0;
1295 ext4_fsblk_t reserved_blocks = 0;
1296 struct ext4_new_group_data *group_data = flex_gd->groups;
1297 struct ext4_sb_info *sbi = EXT4_SB(sb);
1298 struct ext4_super_block *es = sbi->s_es;
1299 int i;
1301 BUG_ON(flex_gd->count == 0 || group_data == NULL);
1303 * Make the new blocks and inodes valid next. We do this before
1304 * increasing the group count so that once the group is enabled,
1305 * all of its blocks and inodes are already valid.
1307 * We always allocate group-by-group, then block-by-block or
1308 * inode-by-inode within a group, so enabling these
1309 * blocks/inodes before the group is live won't actually let us
1310 * allocate the new space yet.
1312 for (i = 0; i < flex_gd->count; i++) {
1313 blocks_count += group_data[i].blocks_count;
1314 free_blocks += group_data[i].free_blocks_count;
1317 reserved_blocks = ext4_r_blocks_count(es) * 100;
1318 reserved_blocks = div64_u64(reserved_blocks, ext4_blocks_count(es));
1319 reserved_blocks *= blocks_count;
1320 do_div(reserved_blocks, 100);
1322 ext4_blocks_count_set(es, ext4_blocks_count(es) + blocks_count);
1323 ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + free_blocks);
1324 le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb) *
1325 flex_gd->count);
1326 le32_add_cpu(&es->s_free_inodes_count, EXT4_INODES_PER_GROUP(sb) *
1327 flex_gd->count);
1329 ext4_debug("free blocks count %llu", ext4_free_blocks_count(es));
1331 * We need to protect s_groups_count against other CPUs seeing
1332 * inconsistent state in the superblock.
1334 * The precise rules we use are:
1336 * * Writers must perform a smp_wmb() after updating all
1337 * dependent data and before modifying the groups count
1339 * * Readers must perform an smp_rmb() after reading the groups
1340 * count and before reading any dependent data.
1342 * NB. These rules can be relaxed when checking the group count
1343 * while freeing data, as we can only allocate from a block
1344 * group after serialising against the group count, and we can
1345 * only then free after serialising in turn against that
1346 * allocation.
1348 smp_wmb();
1350 /* Update the global fs size fields */
1351 sbi->s_groups_count += flex_gd->count;
1352 sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
1353 (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
1355 /* Update the reserved block counts only once the new group is
1356 * active. */
1357 ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
1358 reserved_blocks);
1360 /* Update the free space counts */
1361 percpu_counter_add(&sbi->s_freeclusters_counter,
1362 EXT4_NUM_B2C(sbi, free_blocks));
1363 percpu_counter_add(&sbi->s_freeinodes_counter,
1364 EXT4_INODES_PER_GROUP(sb) * flex_gd->count);
1366 ext4_debug("free blocks count %llu",
1367 percpu_counter_read(&sbi->s_freeclusters_counter));
1368 if (EXT4_HAS_INCOMPAT_FEATURE(sb,
1369 EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
1370 sbi->s_log_groups_per_flex) {
1371 ext4_group_t flex_group;
1372 flex_group = ext4_flex_group(sbi, group_data[0].group);
1373 atomic64_add(EXT4_NUM_B2C(sbi, free_blocks),
1374 &sbi->s_flex_groups[flex_group].free_clusters);
1375 atomic_add(EXT4_INODES_PER_GROUP(sb) * flex_gd->count,
1376 &sbi->s_flex_groups[flex_group].free_inodes);
1380 * Update the fs overhead information
1382 ext4_calculate_overhead(sb);
1384 if (test_opt(sb, DEBUG))
1385 printk(KERN_DEBUG "EXT4-fs: added group %u:"
1386 "%llu blocks(%llu free %llu reserved)\n", flex_gd->count,
1387 blocks_count, free_blocks, reserved_blocks);
1390 /* Add a flex group to an fs. Ensure we handle all possible error conditions
1391 * _before_ we start modifying the filesystem, because we cannot abort the
1392 * transaction and not have it write the data to disk.
1394 static int ext4_flex_group_add(struct super_block *sb,
1395 struct inode *resize_inode,
1396 struct ext4_new_flex_group_data *flex_gd)
1398 struct ext4_sb_info *sbi = EXT4_SB(sb);
1399 struct ext4_super_block *es = sbi->s_es;
1400 ext4_fsblk_t o_blocks_count;
1401 ext4_grpblk_t last;
1402 ext4_group_t group;
1403 handle_t *handle;
1404 unsigned reserved_gdb;
1405 int err = 0, err2 = 0, credit;
1407 BUG_ON(!flex_gd->count || !flex_gd->groups || !flex_gd->bg_flags);
1409 reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks);
1410 o_blocks_count = ext4_blocks_count(es);
1411 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1412 BUG_ON(last);
1414 err = setup_new_flex_group_blocks(sb, flex_gd);
1415 if (err)
1416 goto exit;
1418 * We will always be modifying at least the superblock and GDT
1419 * block. If we are adding a group past the last current GDT block,
1420 * we will also modify the inode and the dindirect block. If we
1421 * are adding a group with superblock/GDT backups we will also
1422 * modify each of the reserved GDT dindirect blocks.
1424 credit = flex_gd->count * 4 + reserved_gdb;
1425 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credit);
1426 if (IS_ERR(handle)) {
1427 err = PTR_ERR(handle);
1428 goto exit;
1431 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
1432 if (err)
1433 goto exit_journal;
1435 group = flex_gd->groups[0].group;
1436 BUG_ON(group != EXT4_SB(sb)->s_groups_count);
1437 err = ext4_add_new_descs(handle, sb, group,
1438 resize_inode, flex_gd->count);
1439 if (err)
1440 goto exit_journal;
1442 err = ext4_setup_new_descs(handle, sb, flex_gd);
1443 if (err)
1444 goto exit_journal;
1446 ext4_update_super(sb, flex_gd);
1448 err = ext4_handle_dirty_super(handle, sb);
1450 exit_journal:
1451 err2 = ext4_journal_stop(handle);
1452 if (!err)
1453 err = err2;
1455 if (!err) {
1456 int gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
1457 int gdb_num_end = ((group + flex_gd->count - 1) /
1458 EXT4_DESC_PER_BLOCK(sb));
1459 int meta_bg = EXT4_HAS_INCOMPAT_FEATURE(sb,
1460 EXT4_FEATURE_INCOMPAT_META_BG);
1461 sector_t old_gdb = 0;
1463 update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
1464 sizeof(struct ext4_super_block), 0);
1465 for (; gdb_num <= gdb_num_end; gdb_num++) {
1466 struct buffer_head *gdb_bh;
1468 gdb_bh = sbi->s_group_desc[gdb_num];
1469 if (old_gdb == gdb_bh->b_blocknr)
1470 continue;
1471 update_backups(sb, gdb_bh->b_blocknr, gdb_bh->b_data,
1472 gdb_bh->b_size, meta_bg);
1473 old_gdb = gdb_bh->b_blocknr;
1476 exit:
1477 return err;
1480 static int ext4_setup_next_flex_gd(struct super_block *sb,
1481 struct ext4_new_flex_group_data *flex_gd,
1482 ext4_fsblk_t n_blocks_count,
1483 unsigned long flexbg_size)
1485 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
1486 struct ext4_new_group_data *group_data = flex_gd->groups;
1487 ext4_fsblk_t o_blocks_count;
1488 ext4_group_t n_group;
1489 ext4_group_t group;
1490 ext4_group_t last_group;
1491 ext4_grpblk_t last;
1492 ext4_grpblk_t blocks_per_group;
1493 unsigned long i;
1495 blocks_per_group = EXT4_BLOCKS_PER_GROUP(sb);
1497 o_blocks_count = ext4_blocks_count(es);
1499 if (o_blocks_count == n_blocks_count)
1500 return 0;
1502 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1503 BUG_ON(last);
1504 ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &last);
1506 last_group = group | (flexbg_size - 1);
1507 if (last_group > n_group)
1508 last_group = n_group;
1510 flex_gd->count = last_group - group + 1;
1512 for (i = 0; i < flex_gd->count; i++) {
1513 int overhead;
1515 group_data[i].group = group + i;
1516 group_data[i].blocks_count = blocks_per_group;
1517 overhead = ext4_group_overhead_blocks(sb, group + i);
1518 group_data[i].free_blocks_count = blocks_per_group - overhead;
1519 if (ext4_has_group_desc_csum(sb)) {
1520 flex_gd->bg_flags[i] = EXT4_BG_BLOCK_UNINIT |
1521 EXT4_BG_INODE_UNINIT;
1522 if (!test_opt(sb, INIT_INODE_TABLE))
1523 flex_gd->bg_flags[i] |= EXT4_BG_INODE_ZEROED;
1524 } else
1525 flex_gd->bg_flags[i] = EXT4_BG_INODE_ZEROED;
1528 if (last_group == n_group && ext4_has_group_desc_csum(sb))
1529 /* We need to initialize block bitmap of last group. */
1530 flex_gd->bg_flags[i - 1] &= ~EXT4_BG_BLOCK_UNINIT;
1532 if ((last_group == n_group) && (last != blocks_per_group - 1)) {
1533 group_data[i - 1].blocks_count = last + 1;
1534 group_data[i - 1].free_blocks_count -= blocks_per_group-
1535 last - 1;
1538 return 1;
1541 /* Add group descriptor data to an existing or new group descriptor block.
1542 * Ensure we handle all possible error conditions _before_ we start modifying
1543 * the filesystem, because we cannot abort the transaction and not have it
1544 * write the data to disk.
1546 * If we are on a GDT block boundary, we need to get the reserved GDT block.
1547 * Otherwise, we may need to add backup GDT blocks for a sparse group.
1549 * We only need to hold the superblock lock while we are actually adding
1550 * in the new group's counts to the superblock. Prior to that we have
1551 * not really "added" the group at all. We re-check that we are still
1552 * adding in the last group in case things have changed since verifying.
1554 int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
1556 struct ext4_new_flex_group_data flex_gd;
1557 struct ext4_sb_info *sbi = EXT4_SB(sb);
1558 struct ext4_super_block *es = sbi->s_es;
1559 int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
1560 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
1561 struct inode *inode = NULL;
1562 int gdb_off, gdb_num;
1563 int err;
1564 __u16 bg_flags = 0;
1566 gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
1567 gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
1569 if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb,
1570 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1571 ext4_warning(sb, "Can't resize non-sparse filesystem further");
1572 return -EPERM;
1575 if (ext4_blocks_count(es) + input->blocks_count <
1576 ext4_blocks_count(es)) {
1577 ext4_warning(sb, "blocks_count overflow");
1578 return -EINVAL;
1581 if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
1582 le32_to_cpu(es->s_inodes_count)) {
1583 ext4_warning(sb, "inodes_count overflow");
1584 return -EINVAL;
1587 if (reserved_gdb || gdb_off == 0) {
1588 if (!EXT4_HAS_COMPAT_FEATURE(sb,
1589 EXT4_FEATURE_COMPAT_RESIZE_INODE)
1590 || !le16_to_cpu(es->s_reserved_gdt_blocks)) {
1591 ext4_warning(sb,
1592 "No reserved GDT blocks, can't resize");
1593 return -EPERM;
1595 inode = ext4_iget(sb, EXT4_RESIZE_INO);
1596 if (IS_ERR(inode)) {
1597 ext4_warning(sb, "Error opening resize inode");
1598 return PTR_ERR(inode);
1603 err = verify_group_input(sb, input);
1604 if (err)
1605 goto out;
1607 err = ext4_alloc_flex_bg_array(sb, input->group + 1);
1608 if (err)
1609 goto out;
1611 err = ext4_mb_alloc_groupinfo(sb, input->group + 1);
1612 if (err)
1613 goto out;
1615 flex_gd.count = 1;
1616 flex_gd.groups = input;
1617 flex_gd.bg_flags = &bg_flags;
1618 err = ext4_flex_group_add(sb, inode, &flex_gd);
1619 out:
1620 iput(inode);
1621 return err;
1622 } /* ext4_group_add */
1625 * extend a group without checking assuming that checking has been done.
1627 static int ext4_group_extend_no_check(struct super_block *sb,
1628 ext4_fsblk_t o_blocks_count, ext4_grpblk_t add)
1630 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
1631 handle_t *handle;
1632 int err = 0, err2;
1634 /* We will update the superblock, one block bitmap, and
1635 * one group descriptor via ext4_group_add_blocks().
1637 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, 3);
1638 if (IS_ERR(handle)) {
1639 err = PTR_ERR(handle);
1640 ext4_warning(sb, "error %d on journal start", err);
1641 return err;
1644 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
1645 if (err) {
1646 ext4_warning(sb, "error %d on journal write access", err);
1647 goto errout;
1650 ext4_blocks_count_set(es, o_blocks_count + add);
1651 ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + add);
1652 ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
1653 o_blocks_count + add);
1654 /* We add the blocks to the bitmap and set the group need init bit */
1655 err = ext4_group_add_blocks(handle, sb, o_blocks_count, add);
1656 if (err)
1657 goto errout;
1658 ext4_handle_dirty_super(handle, sb);
1659 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
1660 o_blocks_count + add);
1661 errout:
1662 err2 = ext4_journal_stop(handle);
1663 if (err2 && !err)
1664 err = err2;
1666 if (!err) {
1667 if (test_opt(sb, DEBUG))
1668 printk(KERN_DEBUG "EXT4-fs: extended group to %llu "
1669 "blocks\n", ext4_blocks_count(es));
1670 update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr,
1671 (char *)es, sizeof(struct ext4_super_block), 0);
1673 return err;
1677 * Extend the filesystem to the new number of blocks specified. This entry
1678 * point is only used to extend the current filesystem to the end of the last
1679 * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
1680 * for emergencies (because it has no dependencies on reserved blocks).
1682 * If we _really_ wanted, we could use default values to call ext4_group_add()
1683 * allow the "remount" trick to work for arbitrary resizing, assuming enough
1684 * GDT blocks are reserved to grow to the desired size.
1686 int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
1687 ext4_fsblk_t n_blocks_count)
1689 ext4_fsblk_t o_blocks_count;
1690 ext4_grpblk_t last;
1691 ext4_grpblk_t add;
1692 struct buffer_head *bh;
1693 int err;
1694 ext4_group_t group;
1696 o_blocks_count = ext4_blocks_count(es);
1698 if (test_opt(sb, DEBUG))
1699 ext4_msg(sb, KERN_DEBUG,
1700 "extending last group from %llu to %llu blocks",
1701 o_blocks_count, n_blocks_count);
1703 if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
1704 return 0;
1706 if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
1707 ext4_msg(sb, KERN_ERR,
1708 "filesystem too large to resize to %llu blocks safely",
1709 n_blocks_count);
1710 if (sizeof(sector_t) < 8)
1711 ext4_warning(sb, "CONFIG_LBDAF not enabled");
1712 return -EINVAL;
1715 if (n_blocks_count < o_blocks_count) {
1716 ext4_warning(sb, "can't shrink FS - resize aborted");
1717 return -EINVAL;
1720 /* Handle the remaining blocks in the last group only. */
1721 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1723 if (last == 0) {
1724 ext4_warning(sb, "need to use ext2online to resize further");
1725 return -EPERM;
1728 add = EXT4_BLOCKS_PER_GROUP(sb) - last;
1730 if (o_blocks_count + add < o_blocks_count) {
1731 ext4_warning(sb, "blocks_count overflow");
1732 return -EINVAL;
1735 if (o_blocks_count + add > n_blocks_count)
1736 add = n_blocks_count - o_blocks_count;
1738 if (o_blocks_count + add < n_blocks_count)
1739 ext4_warning(sb, "will only finish group (%llu blocks, %u new)",
1740 o_blocks_count + add, add);
1742 /* See if the device is actually as big as what was requested */
1743 bh = sb_bread(sb, o_blocks_count + add - 1);
1744 if (!bh) {
1745 ext4_warning(sb, "can't read last block, resize aborted");
1746 return -ENOSPC;
1748 brelse(bh);
1750 err = ext4_group_extend_no_check(sb, o_blocks_count, add);
1751 return err;
1752 } /* ext4_group_extend */
1755 static int num_desc_blocks(struct super_block *sb, ext4_group_t groups)
1757 return (groups + EXT4_DESC_PER_BLOCK(sb) - 1) / EXT4_DESC_PER_BLOCK(sb);
1761 * Release the resize inode and drop the resize_inode feature if there
1762 * are no more reserved gdt blocks, and then convert the file system
1763 * to enable meta_bg
1765 static int ext4_convert_meta_bg(struct super_block *sb, struct inode *inode)
1767 handle_t *handle;
1768 struct ext4_sb_info *sbi = EXT4_SB(sb);
1769 struct ext4_super_block *es = sbi->s_es;
1770 struct ext4_inode_info *ei = EXT4_I(inode);
1771 ext4_fsblk_t nr;
1772 int i, ret, err = 0;
1773 int credits = 1;
1775 ext4_msg(sb, KERN_INFO, "Converting file system to meta_bg");
1776 if (inode) {
1777 if (es->s_reserved_gdt_blocks) {
1778 ext4_error(sb, "Unexpected non-zero "
1779 "s_reserved_gdt_blocks");
1780 return -EPERM;
1783 /* Do a quick sanity check of the resize inode */
1784 if (inode->i_blocks != 1 << (inode->i_blkbits - 9))
1785 goto invalid_resize_inode;
1786 for (i = 0; i < EXT4_N_BLOCKS; i++) {
1787 if (i == EXT4_DIND_BLOCK) {
1788 if (ei->i_data[i])
1789 continue;
1790 else
1791 goto invalid_resize_inode;
1793 if (ei->i_data[i])
1794 goto invalid_resize_inode;
1796 credits += 3; /* block bitmap, bg descriptor, resize inode */
1799 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credits);
1800 if (IS_ERR(handle))
1801 return PTR_ERR(handle);
1803 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
1804 if (err)
1805 goto errout;
1807 EXT4_CLEAR_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_RESIZE_INODE);
1808 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG);
1809 sbi->s_es->s_first_meta_bg =
1810 cpu_to_le32(num_desc_blocks(sb, sbi->s_groups_count));
1812 err = ext4_handle_dirty_super(handle, sb);
1813 if (err) {
1814 ext4_std_error(sb, err);
1815 goto errout;
1818 if (inode) {
1819 nr = le32_to_cpu(ei->i_data[EXT4_DIND_BLOCK]);
1820 ext4_free_blocks(handle, inode, NULL, nr, 1,
1821 EXT4_FREE_BLOCKS_METADATA |
1822 EXT4_FREE_BLOCKS_FORGET);
1823 ei->i_data[EXT4_DIND_BLOCK] = 0;
1824 inode->i_blocks = 0;
1826 err = ext4_mark_inode_dirty(handle, inode);
1827 if (err)
1828 ext4_std_error(sb, err);
1831 errout:
1832 ret = ext4_journal_stop(handle);
1833 if (!err)
1834 err = ret;
1835 return ret;
1837 invalid_resize_inode:
1838 ext4_error(sb, "corrupted/inconsistent resize inode");
1839 return -EINVAL;
1843 * ext4_resize_fs() resizes a fs to new size specified by @n_blocks_count
1845 * @sb: super block of the fs to be resized
1846 * @n_blocks_count: the number of blocks resides in the resized fs
1848 int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count)
1850 struct ext4_new_flex_group_data *flex_gd = NULL;
1851 struct ext4_sb_info *sbi = EXT4_SB(sb);
1852 struct ext4_super_block *es = sbi->s_es;
1853 struct buffer_head *bh;
1854 struct inode *resize_inode = NULL;
1855 ext4_grpblk_t add, offset;
1856 unsigned long n_desc_blocks;
1857 unsigned long o_desc_blocks;
1858 ext4_group_t o_group;
1859 ext4_group_t n_group;
1860 ext4_fsblk_t o_blocks_count;
1861 ext4_fsblk_t n_blocks_count_retry = 0;
1862 unsigned long last_update_time = 0;
1863 int err = 0, flexbg_size = 1 << sbi->s_log_groups_per_flex;
1864 int meta_bg;
1866 /* See if the device is actually as big as what was requested */
1867 bh = sb_bread(sb, n_blocks_count - 1);
1868 if (!bh) {
1869 ext4_warning(sb, "can't read last block, resize aborted");
1870 return -ENOSPC;
1872 brelse(bh);
1874 retry:
1875 o_blocks_count = ext4_blocks_count(es);
1877 ext4_msg(sb, KERN_INFO, "resizing filesystem from %llu "
1878 "to %llu blocks", o_blocks_count, n_blocks_count);
1880 if (n_blocks_count < o_blocks_count) {
1881 /* On-line shrinking not supported */
1882 ext4_warning(sb, "can't shrink FS - resize aborted");
1883 return -EINVAL;
1886 if (n_blocks_count == o_blocks_count)
1887 /* Nothing need to do */
1888 return 0;
1890 n_group = ext4_get_group_number(sb, n_blocks_count - 1);
1891 if (n_group > (0xFFFFFFFFUL / EXT4_INODES_PER_GROUP(sb))) {
1892 ext4_warning(sb, "resize would cause inodes_count overflow");
1893 return -EINVAL;
1895 ext4_get_group_no_and_offset(sb, o_blocks_count - 1, &o_group, &offset);
1897 n_desc_blocks = num_desc_blocks(sb, n_group + 1);
1898 o_desc_blocks = num_desc_blocks(sb, sbi->s_groups_count);
1900 meta_bg = EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG);
1902 if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_RESIZE_INODE)) {
1903 if (meta_bg) {
1904 ext4_error(sb, "resize_inode and meta_bg enabled "
1905 "simultaneously");
1906 return -EINVAL;
1908 if (n_desc_blocks > o_desc_blocks +
1909 le16_to_cpu(es->s_reserved_gdt_blocks)) {
1910 n_blocks_count_retry = n_blocks_count;
1911 n_desc_blocks = o_desc_blocks +
1912 le16_to_cpu(es->s_reserved_gdt_blocks);
1913 n_group = n_desc_blocks * EXT4_DESC_PER_BLOCK(sb);
1914 n_blocks_count = n_group * EXT4_BLOCKS_PER_GROUP(sb);
1915 n_group--; /* set to last group number */
1918 if (!resize_inode)
1919 resize_inode = ext4_iget(sb, EXT4_RESIZE_INO);
1920 if (IS_ERR(resize_inode)) {
1921 ext4_warning(sb, "Error opening resize inode");
1922 return PTR_ERR(resize_inode);
1926 if ((!resize_inode && !meta_bg) || n_blocks_count == o_blocks_count) {
1927 err = ext4_convert_meta_bg(sb, resize_inode);
1928 if (err)
1929 goto out;
1930 if (resize_inode) {
1931 iput(resize_inode);
1932 resize_inode = NULL;
1934 if (n_blocks_count_retry) {
1935 n_blocks_count = n_blocks_count_retry;
1936 n_blocks_count_retry = 0;
1937 goto retry;
1941 /* extend the last group */
1942 if (n_group == o_group)
1943 add = n_blocks_count - o_blocks_count;
1944 else
1945 add = EXT4_BLOCKS_PER_GROUP(sb) - (offset + 1);
1946 if (add > 0) {
1947 err = ext4_group_extend_no_check(sb, o_blocks_count, add);
1948 if (err)
1949 goto out;
1952 if (ext4_blocks_count(es) == n_blocks_count)
1953 goto out;
1955 err = ext4_alloc_flex_bg_array(sb, n_group + 1);
1956 if (err)
1957 return err;
1959 err = ext4_mb_alloc_groupinfo(sb, n_group + 1);
1960 if (err)
1961 goto out;
1963 flex_gd = alloc_flex_gd(flexbg_size);
1964 if (flex_gd == NULL) {
1965 err = -ENOMEM;
1966 goto out;
1969 /* Add flex groups. Note that a regular group is a
1970 * flex group with 1 group.
1972 while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count,
1973 flexbg_size)) {
1974 if (jiffies - last_update_time > HZ * 10) {
1975 if (last_update_time)
1976 ext4_msg(sb, KERN_INFO,
1977 "resized to %llu blocks",
1978 ext4_blocks_count(es));
1979 last_update_time = jiffies;
1981 if (ext4_alloc_group_tables(sb, flex_gd, flexbg_size) != 0)
1982 break;
1983 err = ext4_flex_group_add(sb, resize_inode, flex_gd);
1984 if (unlikely(err))
1985 break;
1988 if (!err && n_blocks_count_retry) {
1989 n_blocks_count = n_blocks_count_retry;
1990 n_blocks_count_retry = 0;
1991 free_flex_gd(flex_gd);
1992 flex_gd = NULL;
1993 goto retry;
1996 out:
1997 if (flex_gd)
1998 free_flex_gd(flex_gd);
1999 if (resize_inode != NULL)
2000 iput(resize_inode);
2001 ext4_msg(sb, KERN_INFO, "resized filesystem to %llu", n_blocks_count);
2002 return err;