1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/ext4/resize.c
5 * Support for resizing an ext4 filesystem while it is mounted.
7 * Copyright (C) 2001, 2002 Andreas Dilger <adilger@clusterfs.com>
9 * This could probably be made into a module, because it is not often in use.
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/jiffies.h>
19 #include "ext4_jbd2.h"
26 static void ext4_rcu_ptr_callback(struct rcu_head *head)
28 struct ext4_rcu_ptr *ptr;
30 ptr = container_of(head, struct ext4_rcu_ptr, rcu);
35 void ext4_kvfree_array_rcu(void *to_free)
37 struct ext4_rcu_ptr *ptr = kzalloc(sizeof(*ptr), GFP_KERNEL);
41 call_rcu(&ptr->rcu, ext4_rcu_ptr_callback);
48 int ext4_resize_begin(struct super_block *sb)
50 struct ext4_sb_info *sbi = EXT4_SB(sb);
53 if (!capable(CAP_SYS_RESOURCE))
57 * If the reserved GDT blocks is non-zero, the resize_inode feature
58 * should always be set.
60 if (EXT4_SB(sb)->s_es->s_reserved_gdt_blocks &&
61 !ext4_has_feature_resize_inode(sb)) {
62 ext4_error(sb, "resize_inode disabled but reserved GDT blocks non-zero");
67 * If we are not using the primary superblock/GDT copy don't resize,
68 * because the user tools have no way of handling this. Probably a
69 * bad time to do it anyways.
71 if (EXT4_B2C(sbi, sbi->s_sbh->b_blocknr) !=
72 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
73 ext4_warning(sb, "won't resize using backup superblock at %llu",
74 (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
79 * We are not allowed to do online-resizing on a filesystem mounted
80 * with error, because it can destroy the filesystem easily.
82 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
83 ext4_warning(sb, "There are errors in the filesystem, "
84 "so online resizing is not allowed");
88 if (ext4_has_feature_sparse_super2(sb)) {
89 ext4_msg(sb, KERN_ERR, "Online resizing not supported with sparse_super2");
93 if (test_and_set_bit_lock(EXT4_FLAGS_RESIZING,
94 &EXT4_SB(sb)->s_ext4_flags))
100 void ext4_resize_end(struct super_block *sb)
102 clear_bit_unlock(EXT4_FLAGS_RESIZING, &EXT4_SB(sb)->s_ext4_flags);
103 smp_mb__after_atomic();
106 static ext4_group_t ext4_meta_bg_first_group(struct super_block *sb,
107 ext4_group_t group) {
108 return (group >> EXT4_DESC_PER_BLOCK_BITS(sb)) <<
109 EXT4_DESC_PER_BLOCK_BITS(sb);
112 static ext4_fsblk_t ext4_meta_bg_first_block_no(struct super_block *sb,
113 ext4_group_t group) {
114 group = ext4_meta_bg_first_group(sb, group);
115 return ext4_group_first_block_no(sb, group);
118 static ext4_grpblk_t ext4_group_overhead_blocks(struct super_block *sb,
119 ext4_group_t group) {
120 ext4_grpblk_t overhead;
121 overhead = ext4_bg_num_gdb(sb, group);
122 if (ext4_bg_has_super(sb, group))
124 le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
128 #define outside(b, first, last) ((b) < (first) || (b) >= (last))
129 #define inside(b, first, last) ((b) >= (first) && (b) < (last))
131 static int verify_group_input(struct super_block *sb,
132 struct ext4_new_group_data *input)
134 struct ext4_sb_info *sbi = EXT4_SB(sb);
135 struct ext4_super_block *es = sbi->s_es;
136 ext4_fsblk_t start = ext4_blocks_count(es);
137 ext4_fsblk_t end = start + input->blocks_count;
138 ext4_group_t group = input->group;
139 ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group;
141 ext4_fsblk_t metaend;
142 struct buffer_head *bh = NULL;
143 ext4_grpblk_t free_blocks_count, offset;
146 if (group != sbi->s_groups_count) {
147 ext4_warning(sb, "Cannot add at group %u (only %u groups)",
148 input->group, sbi->s_groups_count);
152 overhead = ext4_group_overhead_blocks(sb, group);
153 metaend = start + overhead;
154 input->free_clusters_count = free_blocks_count =
155 input->blocks_count - 2 - overhead - sbi->s_itb_per_group;
157 if (test_opt(sb, DEBUG))
158 printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks "
159 "(%d free, %u reserved)\n",
160 ext4_bg_has_super(sb, input->group) ? "normal" :
161 "no-super", input->group, input->blocks_count,
162 free_blocks_count, input->reserved_blocks);
164 ext4_get_group_no_and_offset(sb, start, NULL, &offset);
166 ext4_warning(sb, "Last group not full");
167 else if (input->reserved_blocks > input->blocks_count / 5)
168 ext4_warning(sb, "Reserved blocks too high (%u)",
169 input->reserved_blocks);
170 else if (free_blocks_count < 0)
171 ext4_warning(sb, "Bad blocks count %u",
172 input->blocks_count);
173 else if (IS_ERR(bh = ext4_sb_bread(sb, end - 1, 0))) {
176 ext4_warning(sb, "Cannot read last block (%llu)",
178 } else if (outside(input->block_bitmap, start, end))
179 ext4_warning(sb, "Block bitmap not in group (block %llu)",
180 (unsigned long long)input->block_bitmap);
181 else if (outside(input->inode_bitmap, start, end))
182 ext4_warning(sb, "Inode bitmap not in group (block %llu)",
183 (unsigned long long)input->inode_bitmap);
184 else if (outside(input->inode_table, start, end) ||
185 outside(itend - 1, start, end))
186 ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)",
187 (unsigned long long)input->inode_table, itend - 1);
188 else if (input->inode_bitmap == input->block_bitmap)
189 ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)",
190 (unsigned long long)input->block_bitmap);
191 else if (inside(input->block_bitmap, input->inode_table, itend))
192 ext4_warning(sb, "Block bitmap (%llu) in inode table "
194 (unsigned long long)input->block_bitmap,
195 (unsigned long long)input->inode_table, itend - 1);
196 else if (inside(input->inode_bitmap, input->inode_table, itend))
197 ext4_warning(sb, "Inode bitmap (%llu) in inode table "
199 (unsigned long long)input->inode_bitmap,
200 (unsigned long long)input->inode_table, itend - 1);
201 else if (inside(input->block_bitmap, start, metaend))
202 ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)",
203 (unsigned long long)input->block_bitmap,
205 else if (inside(input->inode_bitmap, start, metaend))
206 ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)",
207 (unsigned long long)input->inode_bitmap,
209 else if (inside(input->inode_table, start, metaend) ||
210 inside(itend - 1, start, metaend))
211 ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table "
213 (unsigned long long)input->inode_table,
214 itend - 1, start, metaend - 1);
223 * ext4_new_flex_group_data is used by 64bit-resize interface to add a flex
226 struct ext4_new_flex_group_data {
227 struct ext4_new_group_data *groups; /* new_group_data for groups
229 __u16 *bg_flags; /* block group flags of groups
231 ext4_group_t count; /* number of groups in @groups
236 * alloc_flex_gd() allocates a ext4_new_flex_group_data with size of
239 * Returns NULL on failure otherwise address of the allocated structure.
241 static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size)
243 struct ext4_new_flex_group_data *flex_gd;
245 flex_gd = kmalloc(sizeof(*flex_gd), GFP_NOFS);
249 if (flexbg_size >= UINT_MAX / sizeof(struct ext4_new_group_data))
251 flex_gd->count = flexbg_size;
253 flex_gd->groups = kmalloc_array(flexbg_size,
254 sizeof(struct ext4_new_group_data),
256 if (flex_gd->groups == NULL)
259 flex_gd->bg_flags = kmalloc_array(flexbg_size, sizeof(__u16),
261 if (flex_gd->bg_flags == NULL)
267 kfree(flex_gd->groups);
274 static void free_flex_gd(struct ext4_new_flex_group_data *flex_gd)
276 kfree(flex_gd->bg_flags);
277 kfree(flex_gd->groups);
282 * ext4_alloc_group_tables() allocates block bitmaps, inode bitmaps
283 * and inode tables for a flex group.
285 * This function is used by 64bit-resize. Note that this function allocates
286 * group tables from the 1st group of groups contained by @flexgd, which may
287 * be a partial of a flex group.
289 * @sb: super block of fs to which the groups belongs
291 * Returns 0 on a successful allocation of the metadata blocks in the
294 static int ext4_alloc_group_tables(struct super_block *sb,
295 struct ext4_new_flex_group_data *flex_gd,
298 struct ext4_new_group_data *group_data = flex_gd->groups;
299 ext4_fsblk_t start_blk;
300 ext4_fsblk_t last_blk;
301 ext4_group_t src_group;
302 ext4_group_t bb_index = 0;
303 ext4_group_t ib_index = 0;
304 ext4_group_t it_index = 0;
306 ext4_group_t last_group;
308 __u16 uninit_mask = (flexbg_size > 1) ? ~EXT4_BG_BLOCK_UNINIT : ~0;
311 BUG_ON(flex_gd->count == 0 || group_data == NULL);
313 src_group = group_data[0].group;
314 last_group = src_group + flex_gd->count - 1;
316 BUG_ON((flexbg_size > 1) && ((src_group & ~(flexbg_size - 1)) !=
317 (last_group & ~(flexbg_size - 1))));
319 group = group_data[0].group;
320 if (src_group >= group_data[0].group + flex_gd->count)
322 start_blk = ext4_group_first_block_no(sb, src_group);
323 last_blk = start_blk + group_data[src_group - group].blocks_count;
325 overhead = ext4_group_overhead_blocks(sb, src_group);
327 start_blk += overhead;
329 /* We collect contiguous blocks as much as possible. */
331 for (; src_group <= last_group; src_group++) {
332 overhead = ext4_group_overhead_blocks(sb, src_group);
334 last_blk += group_data[src_group - group].blocks_count;
339 /* Allocate block bitmaps */
340 for (; bb_index < flex_gd->count; bb_index++) {
341 if (start_blk >= last_blk)
343 group_data[bb_index].block_bitmap = start_blk++;
344 group = ext4_get_group_number(sb, start_blk - 1);
345 group -= group_data[0].group;
346 group_data[group].mdata_blocks++;
347 flex_gd->bg_flags[group] &= uninit_mask;
350 /* Allocate inode bitmaps */
351 for (; ib_index < flex_gd->count; ib_index++) {
352 if (start_blk >= last_blk)
354 group_data[ib_index].inode_bitmap = start_blk++;
355 group = ext4_get_group_number(sb, start_blk - 1);
356 group -= group_data[0].group;
357 group_data[group].mdata_blocks++;
358 flex_gd->bg_flags[group] &= uninit_mask;
361 /* Allocate inode tables */
362 for (; it_index < flex_gd->count; it_index++) {
363 unsigned int itb = EXT4_SB(sb)->s_itb_per_group;
364 ext4_fsblk_t next_group_start;
366 if (start_blk + itb > last_blk)
368 group_data[it_index].inode_table = start_blk;
369 group = ext4_get_group_number(sb, start_blk);
370 next_group_start = ext4_group_first_block_no(sb, group + 1);
371 group -= group_data[0].group;
373 if (start_blk + itb > next_group_start) {
374 flex_gd->bg_flags[group + 1] &= uninit_mask;
375 overhead = start_blk + itb - next_group_start;
376 group_data[group + 1].mdata_blocks += overhead;
380 group_data[group].mdata_blocks += itb;
381 flex_gd->bg_flags[group] &= uninit_mask;
382 start_blk += EXT4_SB(sb)->s_itb_per_group;
385 /* Update free clusters count to exclude metadata blocks */
386 for (i = 0; i < flex_gd->count; i++) {
387 group_data[i].free_clusters_count -=
388 EXT4_NUM_B2C(EXT4_SB(sb),
389 group_data[i].mdata_blocks);
392 if (test_opt(sb, DEBUG)) {
394 group = group_data[0].group;
396 printk(KERN_DEBUG "EXT4-fs: adding a flex group with "
397 "%d groups, flexbg size is %d:\n", flex_gd->count,
400 for (i = 0; i < flex_gd->count; i++) {
402 "adding %s group %u: %u blocks (%d free, %d mdata blocks)\n",
403 ext4_bg_has_super(sb, group + i) ? "normal" :
404 "no-super", group + i,
405 group_data[i].blocks_count,
406 group_data[i].free_clusters_count,
407 group_data[i].mdata_blocks);
413 static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
416 struct buffer_head *bh;
419 bh = sb_getblk(sb, blk);
421 return ERR_PTR(-ENOMEM);
422 BUFFER_TRACE(bh, "get_write_access");
423 err = ext4_journal_get_write_access(handle, sb, bh, EXT4_JTR_NONE);
428 memset(bh->b_data, 0, sb->s_blocksize);
429 set_buffer_uptodate(bh);
435 static int ext4_resize_ensure_credits_batch(handle_t *handle, int credits)
437 return ext4_journal_ensure_credits_fn(handle, credits,
438 EXT4_MAX_TRANS_DATA, 0, 0);
442 * set_flexbg_block_bitmap() mark clusters [@first_cluster, @last_cluster] used.
444 * Helper function for ext4_setup_new_group_blocks() which set .
447 * @handle: journal handle
448 * @flex_gd: flex group data
450 static int set_flexbg_block_bitmap(struct super_block *sb, handle_t *handle,
451 struct ext4_new_flex_group_data *flex_gd,
452 ext4_fsblk_t first_cluster, ext4_fsblk_t last_cluster)
454 struct ext4_sb_info *sbi = EXT4_SB(sb);
455 ext4_group_t count = last_cluster - first_cluster + 1;
458 ext4_debug("mark clusters [%llu-%llu] used\n", first_cluster,
460 for (count2 = count; count > 0;
461 count -= count2, first_cluster += count2) {
463 struct buffer_head *bh;
467 group = ext4_get_group_number(sb, EXT4_C2B(sbi, first_cluster));
468 start = EXT4_B2C(sbi, ext4_group_first_block_no(sb, group));
469 group -= flex_gd->groups[0].group;
471 count2 = EXT4_CLUSTERS_PER_GROUP(sb) - (first_cluster - start);
475 if (flex_gd->bg_flags[group] & EXT4_BG_BLOCK_UNINIT) {
476 BUG_ON(flex_gd->count > 1);
480 err = ext4_resize_ensure_credits_batch(handle, 1);
484 bh = sb_getblk(sb, flex_gd->groups[group].block_bitmap);
488 BUFFER_TRACE(bh, "get_write_access");
489 err = ext4_journal_get_write_access(handle, sb, bh,
495 ext4_debug("mark block bitmap %#04llx (+%llu/%u)\n",
496 first_cluster, first_cluster - start, count2);
497 mb_set_bits(bh->b_data, first_cluster - start, count2);
499 err = ext4_handle_dirty_metadata(handle, NULL, bh);
509 * Set up the block and inode bitmaps, and the inode table for the new groups.
510 * This doesn't need to be part of the main transaction, since we are only
511 * changing blocks outside the actual filesystem. We still do journaling to
512 * ensure the recovery is correct in case of a failure just after resize.
513 * If any part of this fails, we simply abort the resize.
515 * setup_new_flex_group_blocks handles a flex group as follow:
516 * 1. copy super block and GDT, and initialize group tables if necessary.
517 * In this step, we only set bits in blocks bitmaps for blocks taken by
518 * super block and GDT.
519 * 2. allocate group tables in block bitmaps, that is, set bits in block
520 * bitmap for blocks taken by group tables.
522 static int setup_new_flex_group_blocks(struct super_block *sb,
523 struct ext4_new_flex_group_data *flex_gd)
525 int group_table_count[] = {1, 1, EXT4_SB(sb)->s_itb_per_group};
528 struct ext4_sb_info *sbi = EXT4_SB(sb);
529 struct ext4_super_block *es = sbi->s_es;
530 struct ext4_new_group_data *group_data = flex_gd->groups;
531 __u16 *bg_flags = flex_gd->bg_flags;
533 ext4_group_t group, count;
534 struct buffer_head *bh = NULL;
535 int reserved_gdb, i, j, err = 0, err2;
538 BUG_ON(!flex_gd->count || !group_data ||
539 group_data[0].group != sbi->s_groups_count);
541 reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks);
542 meta_bg = ext4_has_feature_meta_bg(sb);
544 /* This transaction may be extended/restarted along the way */
545 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA);
547 return PTR_ERR(handle);
549 group = group_data[0].group;
550 for (i = 0; i < flex_gd->count; i++, group++) {
551 unsigned long gdblocks;
552 ext4_grpblk_t overhead;
554 gdblocks = ext4_bg_num_gdb(sb, group);
555 start = ext4_group_first_block_no(sb, group);
557 if (meta_bg == 0 && !ext4_bg_has_super(sb, group))
561 ext4_group_t first_group;
562 first_group = ext4_meta_bg_first_group(sb, group);
563 if (first_group != group + 1 &&
564 first_group != group + EXT4_DESC_PER_BLOCK(sb) - 1)
568 block = start + ext4_bg_has_super(sb, group);
569 /* Copy all of the GDT blocks into the backup in this group */
570 for (j = 0; j < gdblocks; j++, block++) {
571 struct buffer_head *gdb;
573 ext4_debug("update backup group %#04llx\n", block);
574 err = ext4_resize_ensure_credits_batch(handle, 1);
578 gdb = sb_getblk(sb, block);
579 if (unlikely(!gdb)) {
584 BUFFER_TRACE(gdb, "get_write_access");
585 err = ext4_journal_get_write_access(handle, sb, gdb,
591 memcpy(gdb->b_data, sbi_array_rcu_deref(sbi,
592 s_group_desc, j)->b_data, gdb->b_size);
593 set_buffer_uptodate(gdb);
595 err = ext4_handle_dirty_metadata(handle, NULL, gdb);
603 /* Zero out all of the reserved backup group descriptor
606 if (ext4_bg_has_super(sb, group)) {
607 err = sb_issue_zeroout(sb, gdblocks + start + 1,
608 reserved_gdb, GFP_NOFS);
614 /* Initialize group tables of the grop @group */
615 if (!(bg_flags[i] & EXT4_BG_INODE_ZEROED))
618 /* Zero out all of the inode table blocks */
619 block = group_data[i].inode_table;
620 ext4_debug("clear inode table blocks %#04llx -> %#04lx\n",
621 block, sbi->s_itb_per_group);
622 err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group,
628 if (bg_flags[i] & EXT4_BG_BLOCK_UNINIT)
631 /* Initialize block bitmap of the @group */
632 block = group_data[i].block_bitmap;
633 err = ext4_resize_ensure_credits_batch(handle, 1);
637 bh = bclean(handle, sb, block);
642 overhead = ext4_group_overhead_blocks(sb, group);
644 ext4_debug("mark backup superblock %#04llx (+0)\n",
646 mb_set_bits(bh->b_data, 0,
647 EXT4_NUM_B2C(sbi, overhead));
649 ext4_mark_bitmap_end(EXT4_B2C(sbi, group_data[i].blocks_count),
650 sb->s_blocksize * 8, bh->b_data);
651 err = ext4_handle_dirty_metadata(handle, NULL, bh);
657 if (bg_flags[i] & EXT4_BG_INODE_UNINIT)
660 /* Initialize inode bitmap of the @group */
661 block = group_data[i].inode_bitmap;
662 err = ext4_resize_ensure_credits_batch(handle, 1);
665 /* Mark unused entries in inode bitmap used */
666 bh = bclean(handle, sb, block);
672 ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb),
673 sb->s_blocksize * 8, bh->b_data);
674 err = ext4_handle_dirty_metadata(handle, NULL, bh);
680 /* Mark group tables in block bitmap */
681 for (j = 0; j < GROUP_TABLE_COUNT; j++) {
682 count = group_table_count[j];
683 start = (&group_data[0].block_bitmap)[j];
685 for (i = 1; i < flex_gd->count; i++) {
686 block += group_table_count[j];
687 if (block == (&group_data[i].block_bitmap)[j]) {
688 count += group_table_count[j];
691 err = set_flexbg_block_bitmap(sb, handle,
693 EXT4_B2C(sbi, start),
699 count = group_table_count[j];
700 start = (&group_data[i].block_bitmap)[j];
705 err = set_flexbg_block_bitmap(sb, handle,
707 EXT4_B2C(sbi, start),
717 err2 = ext4_journal_stop(handle);
725 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
726 * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
727 * calling this for the first time. In a sparse filesystem it will be the
728 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
729 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
731 unsigned int ext4_list_backups(struct super_block *sb, unsigned int *three,
732 unsigned int *five, unsigned int *seven)
734 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
735 unsigned int *min = three;
739 if (ext4_has_feature_sparse_super2(sb)) {
743 ret = le32_to_cpu(es->s_backup_bgs[*min - 1]);
749 if (!ext4_has_feature_sparse_super(sb)) {
771 * Check that all of the backup GDT blocks are held in the primary GDT block.
772 * It is assumed that they are stored in group order. Returns the number of
773 * groups in current filesystem that have BACKUPS, or -ve error code.
775 static int verify_reserved_gdb(struct super_block *sb,
777 struct buffer_head *primary)
779 const ext4_fsblk_t blk = primary->b_blocknr;
784 __le32 *p = (__le32 *)primary->b_data;
787 while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
788 if (le32_to_cpu(*p++) !=
789 grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
790 ext4_warning(sb, "reserved GDT %llu"
791 " missing grp %d (%llu)",
794 (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
798 if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
806 * Called when we need to bring a reserved group descriptor table block into
807 * use from the resize inode. The primary copy of the new GDT block currently
808 * is an indirect block (under the double indirect block in the resize inode).
809 * The new backup GDT blocks will be stored as leaf blocks in this indirect
810 * block, in group order. Even though we know all the block numbers we need,
811 * we check to ensure that the resize inode has actually reserved these blocks.
813 * Don't need to update the block bitmaps because the blocks are still in use.
815 * We get all of the error cases out of the way, so that we are sure to not
816 * fail once we start modifying the data on disk, because JBD has no rollback.
818 static int add_new_gdb(handle_t *handle, struct inode *inode,
821 struct super_block *sb = inode->i_sb;
822 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
823 unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
824 ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
825 struct buffer_head **o_group_desc, **n_group_desc = NULL;
826 struct buffer_head *dind = NULL;
827 struct buffer_head *gdb_bh = NULL;
829 struct ext4_iloc iloc = { .bh = NULL };
833 if (test_opt(sb, DEBUG))
835 "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
838 gdb_bh = ext4_sb_bread(sb, gdblock, 0);
840 return PTR_ERR(gdb_bh);
842 gdbackups = verify_reserved_gdb(sb, group, gdb_bh);
848 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
849 dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0);
856 data = (__le32 *)dind->b_data;
857 if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
858 ext4_warning(sb, "new group %u GDT block %llu not reserved",
864 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
865 err = ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh,
870 BUFFER_TRACE(gdb_bh, "get_write_access");
871 err = ext4_journal_get_write_access(handle, sb, gdb_bh, EXT4_JTR_NONE);
875 BUFFER_TRACE(dind, "get_write_access");
876 err = ext4_journal_get_write_access(handle, sb, dind, EXT4_JTR_NONE);
878 ext4_std_error(sb, err);
882 /* ext4_reserve_inode_write() gets a reference on the iloc */
883 err = ext4_reserve_inode_write(handle, inode, &iloc);
887 n_group_desc = kvmalloc((gdb_num + 1) * sizeof(struct buffer_head *),
891 ext4_warning(sb, "not enough memory for %lu groups",
897 * Finally, we have all of the possible failures behind us...
899 * Remove new GDT block from inode double-indirect block and clear out
900 * the new GDT block for use (which also "frees" the backup GDT blocks
901 * from the reserved inode). We don't need to change the bitmaps for
902 * these blocks, because they are marked as in-use from being in the
903 * reserved inode, and will become GDT blocks (primary and backup).
905 data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
906 err = ext4_handle_dirty_metadata(handle, NULL, dind);
908 ext4_std_error(sb, err);
911 inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >>
912 (9 - EXT4_SB(sb)->s_cluster_bits);
913 ext4_mark_iloc_dirty(handle, inode, &iloc);
914 memset(gdb_bh->b_data, 0, sb->s_blocksize);
915 err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
917 ext4_std_error(sb, err);
924 o_group_desc = rcu_dereference(EXT4_SB(sb)->s_group_desc);
925 memcpy(n_group_desc, o_group_desc,
926 EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
928 n_group_desc[gdb_num] = gdb_bh;
929 rcu_assign_pointer(EXT4_SB(sb)->s_group_desc, n_group_desc);
930 EXT4_SB(sb)->s_gdb_count++;
931 ext4_kvfree_array_rcu(o_group_desc);
933 lock_buffer(EXT4_SB(sb)->s_sbh);
934 le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
935 ext4_superblock_csum_set(sb);
936 unlock_buffer(EXT4_SB(sb)->s_sbh);
937 err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
939 ext4_std_error(sb, err);
942 kvfree(n_group_desc);
947 ext4_debug("leaving with error %d\n", err);
952 * add_new_gdb_meta_bg is the sister of add_new_gdb.
954 static int add_new_gdb_meta_bg(struct super_block *sb,
955 handle_t *handle, ext4_group_t group) {
956 ext4_fsblk_t gdblock;
957 struct buffer_head *gdb_bh;
958 struct buffer_head **o_group_desc, **n_group_desc;
959 unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
962 gdblock = ext4_meta_bg_first_block_no(sb, group) +
963 ext4_bg_has_super(sb, group);
964 gdb_bh = ext4_sb_bread(sb, gdblock, 0);
966 return PTR_ERR(gdb_bh);
967 n_group_desc = kvmalloc((gdb_num + 1) * sizeof(struct buffer_head *),
972 ext4_warning(sb, "not enough memory for %lu groups",
978 o_group_desc = rcu_dereference(EXT4_SB(sb)->s_group_desc);
979 memcpy(n_group_desc, o_group_desc,
980 EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
982 n_group_desc[gdb_num] = gdb_bh;
984 BUFFER_TRACE(gdb_bh, "get_write_access");
985 err = ext4_journal_get_write_access(handle, sb, gdb_bh, EXT4_JTR_NONE);
987 kvfree(n_group_desc);
992 rcu_assign_pointer(EXT4_SB(sb)->s_group_desc, n_group_desc);
993 EXT4_SB(sb)->s_gdb_count++;
994 ext4_kvfree_array_rcu(o_group_desc);
999 * Called when we are adding a new group which has a backup copy of each of
1000 * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
1001 * We need to add these reserved backup GDT blocks to the resize inode, so
1002 * that they are kept for future resizing and not allocated to files.
1004 * Each reserved backup GDT block will go into a different indirect block.
1005 * The indirect blocks are actually the primary reserved GDT blocks,
1006 * so we know in advance what their block numbers are. We only get the
1007 * double-indirect block to verify it is pointing to the primary reserved
1008 * GDT blocks so we don't overwrite a data block by accident. The reserved
1009 * backup GDT blocks are stored in their reserved primary GDT block.
1011 static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
1014 struct super_block *sb = inode->i_sb;
1015 int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
1016 int cluster_bits = EXT4_SB(sb)->s_cluster_bits;
1017 struct buffer_head **primary;
1018 struct buffer_head *dind;
1019 struct ext4_iloc iloc;
1026 primary = kmalloc_array(reserved_gdb, sizeof(*primary), GFP_NOFS);
1030 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
1031 dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0);
1033 err = PTR_ERR(dind);
1038 blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count;
1039 data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count %
1040 EXT4_ADDR_PER_BLOCK(sb));
1041 end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb);
1043 /* Get each reserved primary GDT block and verify it holds backups */
1044 for (res = 0; res < reserved_gdb; res++, blk++) {
1045 if (le32_to_cpu(*data) != blk) {
1046 ext4_warning(sb, "reserved block %llu"
1047 " not at offset %ld",
1049 (long)(data - (__le32 *)dind->b_data));
1053 primary[res] = ext4_sb_bread(sb, blk, 0);
1054 if (IS_ERR(primary[res])) {
1055 err = PTR_ERR(primary[res]);
1056 primary[res] = NULL;
1059 gdbackups = verify_reserved_gdb(sb, group, primary[res]);
1060 if (gdbackups < 0) {
1061 brelse(primary[res]);
1066 data = (__le32 *)dind->b_data;
1069 for (i = 0; i < reserved_gdb; i++) {
1070 BUFFER_TRACE(primary[i], "get_write_access");
1071 if ((err = ext4_journal_get_write_access(handle, sb, primary[i],
1076 if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
1080 * Finally we can add each of the reserved backup GDT blocks from
1081 * the new group to its reserved primary GDT block.
1083 blk = group * EXT4_BLOCKS_PER_GROUP(sb);
1084 for (i = 0; i < reserved_gdb; i++) {
1086 data = (__le32 *)primary[i]->b_data;
1087 /* printk("reserving backup %lu[%u] = %lu\n",
1088 primary[i]->b_blocknr, gdbackups,
1089 blk + primary[i]->b_blocknr); */
1090 data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
1091 err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]);
1096 inode->i_blocks += reserved_gdb * sb->s_blocksize >> (9 - cluster_bits);
1097 ext4_mark_iloc_dirty(handle, inode, &iloc);
1101 brelse(primary[res]);
1111 * Update the backup copies of the ext4 metadata. These don't need to be part
1112 * of the main resize transaction, because e2fsck will re-write them if there
1113 * is a problem (basically only OOM will cause a problem). However, we
1114 * _should_ update the backups if possible, in case the primary gets trashed
1115 * for some reason and we need to run e2fsck from a backup superblock. The
1116 * important part is that the new block and inode counts are in the backup
1117 * superblocks, and the location of the new group metadata in the GDT backups.
1119 * We do not need take the s_resize_lock for this, because these
1120 * blocks are not otherwise touched by the filesystem code when it is
1121 * mounted. We don't need to worry about last changing from
1122 * sbi->s_groups_count, because the worst that can happen is that we
1123 * do not copy the full number of backups at this time. The resize
1124 * which changed s_groups_count will backup again.
1126 static void update_backups(struct super_block *sb, sector_t blk_off, char *data,
1127 int size, int meta_bg)
1129 struct ext4_sb_info *sbi = EXT4_SB(sb);
1131 const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
1135 ext4_group_t group = 0;
1136 int rest = sb->s_blocksize - size;
1140 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA);
1141 if (IS_ERR(handle)) {
1143 err = PTR_ERR(handle);
1148 group = ext4_list_backups(sb, &three, &five, &seven);
1149 last = sbi->s_groups_count;
1151 group = ext4_get_group_number(sb, blk_off) + 1;
1152 last = (ext4_group_t)(group + EXT4_DESC_PER_BLOCK(sb) - 2);
1155 while (group < sbi->s_groups_count) {
1156 struct buffer_head *bh;
1157 ext4_fsblk_t backup_block;
1159 /* Out of journal space, and can't get more - abort - so sad */
1160 err = ext4_resize_ensure_credits_batch(handle, 1);
1165 backup_block = ((ext4_fsblk_t)group) * bpg + blk_off;
1167 backup_block = (ext4_group_first_block_no(sb, group) +
1168 ext4_bg_has_super(sb, group));
1170 bh = sb_getblk(sb, backup_block);
1171 if (unlikely(!bh)) {
1175 ext4_debug("update metadata backup %llu(+%llu)\n",
1176 backup_block, backup_block -
1177 ext4_group_first_block_no(sb, group));
1178 BUFFER_TRACE(bh, "get_write_access");
1179 if ((err = ext4_journal_get_write_access(handle, sb, bh,
1183 memcpy(bh->b_data, data, size);
1185 memset(bh->b_data + size, 0, rest);
1186 set_buffer_uptodate(bh);
1188 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1190 ext4_std_error(sb, err);
1194 group = ext4_list_backups(sb, &three, &five, &seven);
1195 else if (group == last)
1200 if ((err2 = ext4_journal_stop(handle)) && !err)
1204 * Ugh! Need to have e2fsck write the backup copies. It is too
1205 * late to revert the resize, we shouldn't fail just because of
1206 * the backup copies (they are only needed in case of corruption).
1208 * However, if we got here we have a journal problem too, so we
1209 * can't really start a transaction to mark the superblock.
1210 * Chicken out and just set the flag on the hope it will be written
1211 * to disk, and if not - we will simply wait until next fsck.
1215 ext4_warning(sb, "can't update backup for group %u (err %d), "
1216 "forcing fsck on next reboot", group, err);
1217 sbi->s_mount_state &= ~EXT4_VALID_FS;
1218 sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1219 mark_buffer_dirty(sbi->s_sbh);
1224 * ext4_add_new_descs() adds @count group descriptor of groups
1225 * starting at @group
1227 * @handle: journal handle
1229 * @group: the group no. of the first group desc to be added
1230 * @resize_inode: the resize inode
1231 * @count: number of group descriptors to be added
1233 static int ext4_add_new_descs(handle_t *handle, struct super_block *sb,
1234 ext4_group_t group, struct inode *resize_inode,
1237 struct ext4_sb_info *sbi = EXT4_SB(sb);
1238 struct ext4_super_block *es = sbi->s_es;
1239 struct buffer_head *gdb_bh;
1240 int i, gdb_off, gdb_num, err = 0;
1243 meta_bg = ext4_has_feature_meta_bg(sb);
1244 for (i = 0; i < count; i++, group++) {
1245 int reserved_gdb = ext4_bg_has_super(sb, group) ?
1246 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
1248 gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
1249 gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
1252 * We will only either add reserved group blocks to a backup group
1253 * or remove reserved blocks for the first group in a new group block.
1254 * Doing both would be mean more complex code, and sane people don't
1255 * use non-sparse filesystems anymore. This is already checked above.
1258 gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc,
1260 BUFFER_TRACE(gdb_bh, "get_write_access");
1261 err = ext4_journal_get_write_access(handle, sb, gdb_bh,
1264 if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group))
1265 err = reserve_backup_gdb(handle, resize_inode, group);
1266 } else if (meta_bg != 0) {
1267 err = add_new_gdb_meta_bg(sb, handle, group);
1269 err = add_new_gdb(handle, resize_inode, group);
1277 static struct buffer_head *ext4_get_bitmap(struct super_block *sb, __u64 block)
1279 struct buffer_head *bh = sb_getblk(sb, block);
1282 if (!bh_uptodate_or_lock(bh)) {
1283 if (ext4_read_bh(bh, 0, NULL) < 0) {
1292 static int ext4_set_bitmap_checksums(struct super_block *sb,
1294 struct ext4_group_desc *gdp,
1295 struct ext4_new_group_data *group_data)
1297 struct buffer_head *bh;
1299 if (!ext4_has_metadata_csum(sb))
1302 bh = ext4_get_bitmap(sb, group_data->inode_bitmap);
1305 ext4_inode_bitmap_csum_set(sb, group, gdp, bh,
1306 EXT4_INODES_PER_GROUP(sb) / 8);
1309 bh = ext4_get_bitmap(sb, group_data->block_bitmap);
1312 ext4_block_bitmap_csum_set(sb, group, gdp, bh);
1319 * ext4_setup_new_descs() will set up the group descriptor descriptors of a flex bg
1321 static int ext4_setup_new_descs(handle_t *handle, struct super_block *sb,
1322 struct ext4_new_flex_group_data *flex_gd)
1324 struct ext4_new_group_data *group_data = flex_gd->groups;
1325 struct ext4_group_desc *gdp;
1326 struct ext4_sb_info *sbi = EXT4_SB(sb);
1327 struct buffer_head *gdb_bh;
1329 __u16 *bg_flags = flex_gd->bg_flags;
1330 int i, gdb_off, gdb_num, err = 0;
1333 for (i = 0; i < flex_gd->count; i++, group_data++, bg_flags++) {
1334 group = group_data->group;
1336 gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
1337 gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
1340 * get_write_access() has been called on gdb_bh by ext4_add_new_desc().
1342 gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc, gdb_num);
1343 /* Update group descriptor block for new group */
1344 gdp = (struct ext4_group_desc *)(gdb_bh->b_data +
1345 gdb_off * EXT4_DESC_SIZE(sb));
1347 memset(gdp, 0, EXT4_DESC_SIZE(sb));
1348 ext4_block_bitmap_set(sb, gdp, group_data->block_bitmap);
1349 ext4_inode_bitmap_set(sb, gdp, group_data->inode_bitmap);
1350 err = ext4_set_bitmap_checksums(sb, group, gdp, group_data);
1352 ext4_std_error(sb, err);
1356 ext4_inode_table_set(sb, gdp, group_data->inode_table);
1357 ext4_free_group_clusters_set(sb, gdp,
1358 group_data->free_clusters_count);
1359 ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
1360 if (ext4_has_group_desc_csum(sb))
1361 ext4_itable_unused_set(sb, gdp,
1362 EXT4_INODES_PER_GROUP(sb));
1363 gdp->bg_flags = cpu_to_le16(*bg_flags);
1364 ext4_group_desc_csum_set(sb, group, gdp);
1366 err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
1367 if (unlikely(err)) {
1368 ext4_std_error(sb, err);
1373 * We can allocate memory for mb_alloc based on the new group
1376 err = ext4_mb_add_groupinfo(sb, group, gdp);
1384 * ext4_update_super() updates the super block so that the newly added
1385 * groups can be seen by the filesystem.
1388 * @flex_gd: new added groups
1390 static void ext4_update_super(struct super_block *sb,
1391 struct ext4_new_flex_group_data *flex_gd)
1393 ext4_fsblk_t blocks_count = 0;
1394 ext4_fsblk_t free_blocks = 0;
1395 ext4_fsblk_t reserved_blocks = 0;
1396 struct ext4_new_group_data *group_data = flex_gd->groups;
1397 struct ext4_sb_info *sbi = EXT4_SB(sb);
1398 struct ext4_super_block *es = sbi->s_es;
1401 BUG_ON(flex_gd->count == 0 || group_data == NULL);
1403 * Make the new blocks and inodes valid next. We do this before
1404 * increasing the group count so that once the group is enabled,
1405 * all of its blocks and inodes are already valid.
1407 * We always allocate group-by-group, then block-by-block or
1408 * inode-by-inode within a group, so enabling these
1409 * blocks/inodes before the group is live won't actually let us
1410 * allocate the new space yet.
1412 for (i = 0; i < flex_gd->count; i++) {
1413 blocks_count += group_data[i].blocks_count;
1414 free_blocks += EXT4_C2B(sbi, group_data[i].free_clusters_count);
1417 reserved_blocks = ext4_r_blocks_count(es) * 100;
1418 reserved_blocks = div64_u64(reserved_blocks, ext4_blocks_count(es));
1419 reserved_blocks *= blocks_count;
1420 do_div(reserved_blocks, 100);
1422 lock_buffer(sbi->s_sbh);
1423 ext4_blocks_count_set(es, ext4_blocks_count(es) + blocks_count);
1424 ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + free_blocks);
1425 le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb) *
1427 le32_add_cpu(&es->s_free_inodes_count, EXT4_INODES_PER_GROUP(sb) *
1430 ext4_debug("free blocks count %llu", ext4_free_blocks_count(es));
1432 * We need to protect s_groups_count against other CPUs seeing
1433 * inconsistent state in the superblock.
1435 * The precise rules we use are:
1437 * * Writers must perform a smp_wmb() after updating all
1438 * dependent data and before modifying the groups count
1440 * * Readers must perform an smp_rmb() after reading the groups
1441 * count and before reading any dependent data.
1443 * NB. These rules can be relaxed when checking the group count
1444 * while freeing data, as we can only allocate from a block
1445 * group after serialising against the group count, and we can
1446 * only then free after serialising in turn against that
1451 /* Update the global fs size fields */
1452 sbi->s_groups_count += flex_gd->count;
1453 sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
1454 (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
1456 /* Update the reserved block counts only once the new group is
1458 ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
1460 ext4_superblock_csum_set(sb);
1461 unlock_buffer(sbi->s_sbh);
1463 /* Update the free space counts */
1464 percpu_counter_add(&sbi->s_freeclusters_counter,
1465 EXT4_NUM_B2C(sbi, free_blocks));
1466 percpu_counter_add(&sbi->s_freeinodes_counter,
1467 EXT4_INODES_PER_GROUP(sb) * flex_gd->count);
1469 ext4_debug("free blocks count %llu",
1470 percpu_counter_read(&sbi->s_freeclusters_counter));
1471 if (ext4_has_feature_flex_bg(sb) && sbi->s_log_groups_per_flex) {
1472 ext4_group_t flex_group;
1473 struct flex_groups *fg;
1475 flex_group = ext4_flex_group(sbi, group_data[0].group);
1476 fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
1477 atomic64_add(EXT4_NUM_B2C(sbi, free_blocks),
1478 &fg->free_clusters);
1479 atomic_add(EXT4_INODES_PER_GROUP(sb) * flex_gd->count,
1484 * Update the fs overhead information
1486 ext4_calculate_overhead(sb);
1488 if (test_opt(sb, DEBUG))
1489 printk(KERN_DEBUG "EXT4-fs: added group %u:"
1490 "%llu blocks(%llu free %llu reserved)\n", flex_gd->count,
1491 blocks_count, free_blocks, reserved_blocks);
1494 /* Add a flex group to an fs. Ensure we handle all possible error conditions
1495 * _before_ we start modifying the filesystem, because we cannot abort the
1496 * transaction and not have it write the data to disk.
1498 static int ext4_flex_group_add(struct super_block *sb,
1499 struct inode *resize_inode,
1500 struct ext4_new_flex_group_data *flex_gd)
1502 struct ext4_sb_info *sbi = EXT4_SB(sb);
1503 struct ext4_super_block *es = sbi->s_es;
1504 ext4_fsblk_t o_blocks_count;
1508 unsigned reserved_gdb;
1509 int err = 0, err2 = 0, credit;
1511 BUG_ON(!flex_gd->count || !flex_gd->groups || !flex_gd->bg_flags);
1513 reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks);
1514 o_blocks_count = ext4_blocks_count(es);
1515 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1518 err = setup_new_flex_group_blocks(sb, flex_gd);
1522 * We will always be modifying at least the superblock and GDT
1523 * blocks. If we are adding a group past the last current GDT block,
1524 * we will also modify the inode and the dindirect block. If we
1525 * are adding a group with superblock/GDT backups we will also
1526 * modify each of the reserved GDT dindirect blocks.
1528 credit = 3; /* sb, resize inode, resize inode dindirect */
1530 credit += 1 + DIV_ROUND_UP(flex_gd->count, EXT4_DESC_PER_BLOCK(sb));
1531 credit += reserved_gdb; /* Reserved GDT dindirect blocks */
1532 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credit);
1533 if (IS_ERR(handle)) {
1534 err = PTR_ERR(handle);
1538 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
1539 err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh,
1544 group = flex_gd->groups[0].group;
1545 BUG_ON(group != sbi->s_groups_count);
1546 err = ext4_add_new_descs(handle, sb, group,
1547 resize_inode, flex_gd->count);
1551 err = ext4_setup_new_descs(handle, sb, flex_gd);
1555 ext4_update_super(sb, flex_gd);
1557 err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
1560 err2 = ext4_journal_stop(handle);
1565 int gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
1566 int gdb_num_end = ((group + flex_gd->count - 1) /
1567 EXT4_DESC_PER_BLOCK(sb));
1568 int meta_bg = ext4_has_feature_meta_bg(sb);
1569 sector_t old_gdb = 0;
1571 update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
1572 sizeof(struct ext4_super_block), 0);
1573 for (; gdb_num <= gdb_num_end; gdb_num++) {
1574 struct buffer_head *gdb_bh;
1576 gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc,
1578 if (old_gdb == gdb_bh->b_blocknr)
1580 update_backups(sb, gdb_bh->b_blocknr, gdb_bh->b_data,
1581 gdb_bh->b_size, meta_bg);
1582 old_gdb = gdb_bh->b_blocknr;
1589 static int ext4_setup_next_flex_gd(struct super_block *sb,
1590 struct ext4_new_flex_group_data *flex_gd,
1591 ext4_fsblk_t n_blocks_count,
1592 unsigned long flexbg_size)
1594 struct ext4_sb_info *sbi = EXT4_SB(sb);
1595 struct ext4_super_block *es = sbi->s_es;
1596 struct ext4_new_group_data *group_data = flex_gd->groups;
1597 ext4_fsblk_t o_blocks_count;
1598 ext4_group_t n_group;
1600 ext4_group_t last_group;
1602 ext4_grpblk_t clusters_per_group;
1605 clusters_per_group = EXT4_CLUSTERS_PER_GROUP(sb);
1607 o_blocks_count = ext4_blocks_count(es);
1609 if (o_blocks_count == n_blocks_count)
1612 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1614 ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &last);
1616 last_group = group | (flexbg_size - 1);
1617 if (last_group > n_group)
1618 last_group = n_group;
1620 flex_gd->count = last_group - group + 1;
1622 for (i = 0; i < flex_gd->count; i++) {
1625 group_data[i].group = group + i;
1626 group_data[i].blocks_count = EXT4_BLOCKS_PER_GROUP(sb);
1627 overhead = ext4_group_overhead_blocks(sb, group + i);
1628 group_data[i].mdata_blocks = overhead;
1629 group_data[i].free_clusters_count = EXT4_CLUSTERS_PER_GROUP(sb);
1630 if (ext4_has_group_desc_csum(sb)) {
1631 flex_gd->bg_flags[i] = EXT4_BG_BLOCK_UNINIT |
1632 EXT4_BG_INODE_UNINIT;
1633 if (!test_opt(sb, INIT_INODE_TABLE))
1634 flex_gd->bg_flags[i] |= EXT4_BG_INODE_ZEROED;
1636 flex_gd->bg_flags[i] = EXT4_BG_INODE_ZEROED;
1639 if (last_group == n_group && ext4_has_group_desc_csum(sb))
1640 /* We need to initialize block bitmap of last group. */
1641 flex_gd->bg_flags[i - 1] &= ~EXT4_BG_BLOCK_UNINIT;
1643 if ((last_group == n_group) && (last != clusters_per_group - 1)) {
1644 group_data[i - 1].blocks_count = EXT4_C2B(sbi, last + 1);
1645 group_data[i - 1].free_clusters_count -= clusters_per_group -
1652 /* Add group descriptor data to an existing or new group descriptor block.
1653 * Ensure we handle all possible error conditions _before_ we start modifying
1654 * the filesystem, because we cannot abort the transaction and not have it
1655 * write the data to disk.
1657 * If we are on a GDT block boundary, we need to get the reserved GDT block.
1658 * Otherwise, we may need to add backup GDT blocks for a sparse group.
1660 * We only need to hold the superblock lock while we are actually adding
1661 * in the new group's counts to the superblock. Prior to that we have
1662 * not really "added" the group at all. We re-check that we are still
1663 * adding in the last group in case things have changed since verifying.
1665 int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
1667 struct ext4_new_flex_group_data flex_gd;
1668 struct ext4_sb_info *sbi = EXT4_SB(sb);
1669 struct ext4_super_block *es = sbi->s_es;
1670 int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
1671 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
1672 struct inode *inode = NULL;
1677 gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
1679 if (gdb_off == 0 && !ext4_has_feature_sparse_super(sb)) {
1680 ext4_warning(sb, "Can't resize non-sparse filesystem further");
1684 if (ext4_blocks_count(es) + input->blocks_count <
1685 ext4_blocks_count(es)) {
1686 ext4_warning(sb, "blocks_count overflow");
1690 if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
1691 le32_to_cpu(es->s_inodes_count)) {
1692 ext4_warning(sb, "inodes_count overflow");
1696 if (reserved_gdb || gdb_off == 0) {
1697 if (!ext4_has_feature_resize_inode(sb) ||
1698 !le16_to_cpu(es->s_reserved_gdt_blocks)) {
1700 "No reserved GDT blocks, can't resize");
1703 inode = ext4_iget(sb, EXT4_RESIZE_INO, EXT4_IGET_SPECIAL);
1704 if (IS_ERR(inode)) {
1705 ext4_warning(sb, "Error opening resize inode");
1706 return PTR_ERR(inode);
1711 err = verify_group_input(sb, input);
1715 err = ext4_alloc_flex_bg_array(sb, input->group + 1);
1719 err = ext4_mb_alloc_groupinfo(sb, input->group + 1);
1724 flex_gd.groups = input;
1725 flex_gd.bg_flags = &bg_flags;
1726 err = ext4_flex_group_add(sb, inode, &flex_gd);
1730 } /* ext4_group_add */
1733 * extend a group without checking assuming that checking has been done.
1735 static int ext4_group_extend_no_check(struct super_block *sb,
1736 ext4_fsblk_t o_blocks_count, ext4_grpblk_t add)
1738 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
1742 /* We will update the superblock, one block bitmap, and
1743 * one group descriptor via ext4_group_add_blocks().
1745 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, 3);
1746 if (IS_ERR(handle)) {
1747 err = PTR_ERR(handle);
1748 ext4_warning(sb, "error %d on journal start", err);
1752 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
1753 err = ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh,
1756 ext4_warning(sb, "error %d on journal write access", err);
1760 lock_buffer(EXT4_SB(sb)->s_sbh);
1761 ext4_blocks_count_set(es, o_blocks_count + add);
1762 ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + add);
1763 ext4_superblock_csum_set(sb);
1764 unlock_buffer(EXT4_SB(sb)->s_sbh);
1765 ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
1766 o_blocks_count + add);
1767 /* We add the blocks to the bitmap and set the group need init bit */
1768 err = ext4_group_add_blocks(handle, sb, o_blocks_count, add);
1771 ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
1772 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
1773 o_blocks_count + add);
1775 err2 = ext4_journal_stop(handle);
1780 if (test_opt(sb, DEBUG))
1781 printk(KERN_DEBUG "EXT4-fs: extended group to %llu "
1782 "blocks\n", ext4_blocks_count(es));
1783 update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr,
1784 (char *)es, sizeof(struct ext4_super_block), 0);
1790 * Extend the filesystem to the new number of blocks specified. This entry
1791 * point is only used to extend the current filesystem to the end of the last
1792 * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
1793 * for emergencies (because it has no dependencies on reserved blocks).
1795 * If we _really_ wanted, we could use default values to call ext4_group_add()
1796 * allow the "remount" trick to work for arbitrary resizing, assuming enough
1797 * GDT blocks are reserved to grow to the desired size.
1799 int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
1800 ext4_fsblk_t n_blocks_count)
1802 ext4_fsblk_t o_blocks_count;
1805 struct buffer_head *bh;
1809 o_blocks_count = ext4_blocks_count(es);
1811 if (test_opt(sb, DEBUG))
1812 ext4_msg(sb, KERN_DEBUG,
1813 "extending last group from %llu to %llu blocks",
1814 o_blocks_count, n_blocks_count);
1816 if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
1819 if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
1820 ext4_msg(sb, KERN_ERR,
1821 "filesystem too large to resize to %llu blocks safely",
1826 if (n_blocks_count < o_blocks_count) {
1827 ext4_warning(sb, "can't shrink FS - resize aborted");
1831 /* Handle the remaining blocks in the last group only. */
1832 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1835 ext4_warning(sb, "need to use ext2online to resize further");
1839 add = EXT4_BLOCKS_PER_GROUP(sb) - last;
1841 if (o_blocks_count + add < o_blocks_count) {
1842 ext4_warning(sb, "blocks_count overflow");
1846 if (o_blocks_count + add > n_blocks_count)
1847 add = n_blocks_count - o_blocks_count;
1849 if (o_blocks_count + add < n_blocks_count)
1850 ext4_warning(sb, "will only finish group (%llu blocks, %u new)",
1851 o_blocks_count + add, add);
1853 /* See if the device is actually as big as what was requested */
1854 bh = ext4_sb_bread(sb, o_blocks_count + add - 1, 0);
1856 ext4_warning(sb, "can't read last block, resize aborted");
1861 err = ext4_group_extend_no_check(sb, o_blocks_count, add);
1863 } /* ext4_group_extend */
1866 static int num_desc_blocks(struct super_block *sb, ext4_group_t groups)
1868 return (groups + EXT4_DESC_PER_BLOCK(sb) - 1) / EXT4_DESC_PER_BLOCK(sb);
1872 * Release the resize inode and drop the resize_inode feature if there
1873 * are no more reserved gdt blocks, and then convert the file system
1876 static int ext4_convert_meta_bg(struct super_block *sb, struct inode *inode)
1879 struct ext4_sb_info *sbi = EXT4_SB(sb);
1880 struct ext4_super_block *es = sbi->s_es;
1881 struct ext4_inode_info *ei = EXT4_I(inode);
1883 int i, ret, err = 0;
1886 ext4_msg(sb, KERN_INFO, "Converting file system to meta_bg");
1888 if (es->s_reserved_gdt_blocks) {
1889 ext4_error(sb, "Unexpected non-zero "
1890 "s_reserved_gdt_blocks");
1894 /* Do a quick sanity check of the resize inode */
1895 if (inode->i_blocks != 1 << (inode->i_blkbits -
1896 (9 - sbi->s_cluster_bits)))
1897 goto invalid_resize_inode;
1898 for (i = 0; i < EXT4_N_BLOCKS; i++) {
1899 if (i == EXT4_DIND_BLOCK) {
1903 goto invalid_resize_inode;
1906 goto invalid_resize_inode;
1908 credits += 3; /* block bitmap, bg descriptor, resize inode */
1911 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credits);
1913 return PTR_ERR(handle);
1915 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
1916 err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh,
1921 lock_buffer(sbi->s_sbh);
1922 ext4_clear_feature_resize_inode(sb);
1923 ext4_set_feature_meta_bg(sb);
1924 sbi->s_es->s_first_meta_bg =
1925 cpu_to_le32(num_desc_blocks(sb, sbi->s_groups_count));
1926 ext4_superblock_csum_set(sb);
1927 unlock_buffer(sbi->s_sbh);
1929 err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
1931 ext4_std_error(sb, err);
1936 nr = le32_to_cpu(ei->i_data[EXT4_DIND_BLOCK]);
1937 ext4_free_blocks(handle, inode, NULL, nr, 1,
1938 EXT4_FREE_BLOCKS_METADATA |
1939 EXT4_FREE_BLOCKS_FORGET);
1940 ei->i_data[EXT4_DIND_BLOCK] = 0;
1941 inode->i_blocks = 0;
1943 err = ext4_mark_inode_dirty(handle, inode);
1945 ext4_std_error(sb, err);
1949 ret = ext4_journal_stop(handle);
1954 invalid_resize_inode:
1955 ext4_error(sb, "corrupted/inconsistent resize inode");
1960 * ext4_resize_fs() resizes a fs to new size specified by @n_blocks_count
1962 * @sb: super block of the fs to be resized
1963 * @n_blocks_count: the number of blocks resides in the resized fs
1965 int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count)
1967 struct ext4_new_flex_group_data *flex_gd = NULL;
1968 struct ext4_sb_info *sbi = EXT4_SB(sb);
1969 struct ext4_super_block *es = sbi->s_es;
1970 struct buffer_head *bh;
1971 struct inode *resize_inode = NULL;
1972 ext4_grpblk_t add, offset;
1973 unsigned long n_desc_blocks;
1974 unsigned long o_desc_blocks;
1975 ext4_group_t o_group;
1976 ext4_group_t n_group;
1977 ext4_fsblk_t o_blocks_count;
1978 ext4_fsblk_t n_blocks_count_retry = 0;
1979 unsigned long last_update_time = 0;
1980 int err = 0, flexbg_size = 1 << sbi->s_log_groups_per_flex;
1983 /* See if the device is actually as big as what was requested */
1984 bh = ext4_sb_bread(sb, n_blocks_count - 1, 0);
1986 ext4_warning(sb, "can't read last block, resize aborted");
1992 o_blocks_count = ext4_blocks_count(es);
1994 ext4_msg(sb, KERN_INFO, "resizing filesystem from %llu "
1995 "to %llu blocks", o_blocks_count, n_blocks_count);
1997 if (n_blocks_count < o_blocks_count) {
1998 /* On-line shrinking not supported */
1999 ext4_warning(sb, "can't shrink FS - resize aborted");
2003 if (n_blocks_count == o_blocks_count)
2004 /* Nothing need to do */
2007 n_group = ext4_get_group_number(sb, n_blocks_count - 1);
2008 if (n_group >= (0xFFFFFFFFUL / EXT4_INODES_PER_GROUP(sb))) {
2009 ext4_warning(sb, "resize would cause inodes_count overflow");
2012 ext4_get_group_no_and_offset(sb, o_blocks_count - 1, &o_group, &offset);
2014 n_desc_blocks = num_desc_blocks(sb, n_group + 1);
2015 o_desc_blocks = num_desc_blocks(sb, sbi->s_groups_count);
2017 meta_bg = ext4_has_feature_meta_bg(sb);
2019 if (ext4_has_feature_resize_inode(sb)) {
2021 ext4_error(sb, "resize_inode and meta_bg enabled "
2025 if (n_desc_blocks > o_desc_blocks +
2026 le16_to_cpu(es->s_reserved_gdt_blocks)) {
2027 n_blocks_count_retry = n_blocks_count;
2028 n_desc_blocks = o_desc_blocks +
2029 le16_to_cpu(es->s_reserved_gdt_blocks);
2030 n_group = n_desc_blocks * EXT4_DESC_PER_BLOCK(sb);
2031 n_blocks_count = (ext4_fsblk_t)n_group *
2032 EXT4_BLOCKS_PER_GROUP(sb) +
2033 le32_to_cpu(es->s_first_data_block);
2034 n_group--; /* set to last group number */
2038 resize_inode = ext4_iget(sb, EXT4_RESIZE_INO,
2040 if (IS_ERR(resize_inode)) {
2041 ext4_warning(sb, "Error opening resize inode");
2042 return PTR_ERR(resize_inode);
2046 if ((!resize_inode && !meta_bg) || n_blocks_count == o_blocks_count) {
2047 err = ext4_convert_meta_bg(sb, resize_inode);
2052 resize_inode = NULL;
2054 if (n_blocks_count_retry) {
2055 n_blocks_count = n_blocks_count_retry;
2056 n_blocks_count_retry = 0;
2062 * Make sure the last group has enough space so that it's
2063 * guaranteed to have enough space for all metadata blocks
2064 * that it might need to hold. (We might not need to store
2065 * the inode table blocks in the last block group, but there
2066 * will be cases where this might be needed.)
2068 if ((ext4_group_first_block_no(sb, n_group) +
2069 ext4_group_overhead_blocks(sb, n_group) + 2 +
2070 sbi->s_itb_per_group + sbi->s_cluster_ratio) >= n_blocks_count) {
2071 n_blocks_count = ext4_group_first_block_no(sb, n_group);
2073 n_blocks_count_retry = 0;
2076 resize_inode = NULL;
2081 /* extend the last group */
2082 if (n_group == o_group)
2083 add = n_blocks_count - o_blocks_count;
2085 add = EXT4_C2B(sbi, EXT4_CLUSTERS_PER_GROUP(sb) - (offset + 1));
2087 err = ext4_group_extend_no_check(sb, o_blocks_count, add);
2092 if (ext4_blocks_count(es) == n_blocks_count)
2095 err = ext4_alloc_flex_bg_array(sb, n_group + 1);
2099 err = ext4_mb_alloc_groupinfo(sb, n_group + 1);
2103 flex_gd = alloc_flex_gd(flexbg_size);
2104 if (flex_gd == NULL) {
2109 /* Add flex groups. Note that a regular group is a
2110 * flex group with 1 group.
2112 while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count,
2114 if (time_is_before_jiffies(last_update_time + HZ * 10)) {
2115 if (last_update_time)
2116 ext4_msg(sb, KERN_INFO,
2117 "resized to %llu blocks",
2118 ext4_blocks_count(es));
2119 last_update_time = jiffies;
2121 if (ext4_alloc_group_tables(sb, flex_gd, flexbg_size) != 0)
2123 err = ext4_flex_group_add(sb, resize_inode, flex_gd);
2128 if (!err && n_blocks_count_retry) {
2129 n_blocks_count = n_blocks_count_retry;
2130 n_blocks_count_retry = 0;
2131 free_flex_gd(flex_gd);
2135 resize_inode = NULL;
2142 free_flex_gd(flex_gd);
2143 if (resize_inode != NULL)
2146 ext4_warning(sb, "error (%d) occurred during "
2147 "file system resize", err);
2148 ext4_msg(sb, KERN_INFO, "resized filesystem to %llu",
2149 ext4_blocks_count(es));