xref: /OK3568_Linux_fs/kernel/fs/ext4/resize.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  linux/fs/ext4/resize.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Support for resizing an ext4 filesystem while it is mounted.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (C) 2001, 2002 Andreas Dilger <adilger@clusterfs.com>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This could probably be made into a module, because it is not often in use.
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #define EXT4FS_DEBUG
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <linux/errno.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include "ext4_jbd2.h"
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun struct ext4_rcu_ptr {
21*4882a593Smuzhiyun 	struct rcu_head rcu;
22*4882a593Smuzhiyun 	void *ptr;
23*4882a593Smuzhiyun };
24*4882a593Smuzhiyun 
ext4_rcu_ptr_callback(struct rcu_head * head)25*4882a593Smuzhiyun static void ext4_rcu_ptr_callback(struct rcu_head *head)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun 	struct ext4_rcu_ptr *ptr;
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun 	ptr = container_of(head, struct ext4_rcu_ptr, rcu);
30*4882a593Smuzhiyun 	kvfree(ptr->ptr);
31*4882a593Smuzhiyun 	kfree(ptr);
32*4882a593Smuzhiyun }
33*4882a593Smuzhiyun 
ext4_kvfree_array_rcu(void * to_free)34*4882a593Smuzhiyun void ext4_kvfree_array_rcu(void *to_free)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	struct ext4_rcu_ptr *ptr = kzalloc(sizeof(*ptr), GFP_KERNEL);
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	if (ptr) {
39*4882a593Smuzhiyun 		ptr->ptr = to_free;
40*4882a593Smuzhiyun 		call_rcu(&ptr->rcu, ext4_rcu_ptr_callback);
41*4882a593Smuzhiyun 		return;
42*4882a593Smuzhiyun 	}
43*4882a593Smuzhiyun 	synchronize_rcu();
44*4882a593Smuzhiyun 	kvfree(to_free);
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun 
ext4_resize_begin(struct super_block * sb)47*4882a593Smuzhiyun int ext4_resize_begin(struct super_block *sb)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun 	struct ext4_sb_info *sbi = EXT4_SB(sb);
50*4882a593Smuzhiyun 	int ret = 0;
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	if (!capable(CAP_SYS_RESOURCE))
53*4882a593Smuzhiyun 		return -EPERM;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	/*
56*4882a593Smuzhiyun 	 * If the reserved GDT blocks is non-zero, the resize_inode feature
57*4882a593Smuzhiyun 	 * should always be set.
58*4882a593Smuzhiyun 	 */
59*4882a593Smuzhiyun 	if (EXT4_SB(sb)->s_es->s_reserved_gdt_blocks &&
60*4882a593Smuzhiyun 	    !ext4_has_feature_resize_inode(sb)) {
61*4882a593Smuzhiyun 		ext4_error(sb, "resize_inode disabled but reserved GDT blocks non-zero");
62*4882a593Smuzhiyun 		return -EFSCORRUPTED;
63*4882a593Smuzhiyun 	}
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	/*
66*4882a593Smuzhiyun 	 * If we are not using the primary superblock/GDT copy don't resize,
67*4882a593Smuzhiyun          * because the user tools have no way of handling this.  Probably a
68*4882a593Smuzhiyun          * bad time to do it anyways.
69*4882a593Smuzhiyun          */
70*4882a593Smuzhiyun 	if (EXT4_B2C(sbi, sbi->s_sbh->b_blocknr) !=
71*4882a593Smuzhiyun 	    le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
72*4882a593Smuzhiyun 		ext4_warning(sb, "won't resize using backup superblock at %llu",
73*4882a593Smuzhiyun 			(unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
74*4882a593Smuzhiyun 		return -EPERM;
75*4882a593Smuzhiyun 	}
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	/*
78*4882a593Smuzhiyun 	 * We are not allowed to do online-resizing on a filesystem mounted
79*4882a593Smuzhiyun 	 * with error, because it can destroy the filesystem easily.
80*4882a593Smuzhiyun 	 */
81*4882a593Smuzhiyun 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
82*4882a593Smuzhiyun 		ext4_warning(sb, "There are errors in the filesystem, "
83*4882a593Smuzhiyun 			     "so online resizing is not allowed");
84*4882a593Smuzhiyun 		return -EPERM;
85*4882a593Smuzhiyun 	}
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	if (ext4_has_feature_sparse_super2(sb)) {
88*4882a593Smuzhiyun 		ext4_msg(sb, KERN_ERR, "Online resizing not supported with sparse_super2");
89*4882a593Smuzhiyun 		return -EOPNOTSUPP;
90*4882a593Smuzhiyun 	}
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	if (test_and_set_bit_lock(EXT4_FLAGS_RESIZING,
93*4882a593Smuzhiyun 				  &EXT4_SB(sb)->s_ext4_flags))
94*4882a593Smuzhiyun 		ret = -EBUSY;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	return ret;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
ext4_resize_end(struct super_block * sb)99*4882a593Smuzhiyun void ext4_resize_end(struct super_block *sb)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	clear_bit_unlock(EXT4_FLAGS_RESIZING, &EXT4_SB(sb)->s_ext4_flags);
102*4882a593Smuzhiyun 	smp_mb__after_atomic();
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun 
ext4_meta_bg_first_group(struct super_block * sb,ext4_group_t group)105*4882a593Smuzhiyun static ext4_group_t ext4_meta_bg_first_group(struct super_block *sb,
106*4882a593Smuzhiyun 					     ext4_group_t group) {
107*4882a593Smuzhiyun 	return (group >> EXT4_DESC_PER_BLOCK_BITS(sb)) <<
108*4882a593Smuzhiyun 	       EXT4_DESC_PER_BLOCK_BITS(sb);
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun 
ext4_meta_bg_first_block_no(struct super_block * sb,ext4_group_t group)111*4882a593Smuzhiyun static ext4_fsblk_t ext4_meta_bg_first_block_no(struct super_block *sb,
112*4882a593Smuzhiyun 					     ext4_group_t group) {
113*4882a593Smuzhiyun 	group = ext4_meta_bg_first_group(sb, group);
114*4882a593Smuzhiyun 	return ext4_group_first_block_no(sb, group);
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
ext4_group_overhead_blocks(struct super_block * sb,ext4_group_t group)117*4882a593Smuzhiyun static ext4_grpblk_t ext4_group_overhead_blocks(struct super_block *sb,
118*4882a593Smuzhiyun 						ext4_group_t group) {
119*4882a593Smuzhiyun 	ext4_grpblk_t overhead;
120*4882a593Smuzhiyun 	overhead = ext4_bg_num_gdb(sb, group);
121*4882a593Smuzhiyun 	if (ext4_bg_has_super(sb, group))
122*4882a593Smuzhiyun 		overhead += 1 +
123*4882a593Smuzhiyun 			  le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
124*4882a593Smuzhiyun 	return overhead;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun #define outside(b, first, last)	((b) < (first) || (b) >= (last))
128*4882a593Smuzhiyun #define inside(b, first, last)	((b) >= (first) && (b) < (last))
129*4882a593Smuzhiyun 
verify_group_input(struct super_block * sb,struct ext4_new_group_data * input)130*4882a593Smuzhiyun static int verify_group_input(struct super_block *sb,
131*4882a593Smuzhiyun 			      struct ext4_new_group_data *input)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	struct ext4_sb_info *sbi = EXT4_SB(sb);
134*4882a593Smuzhiyun 	struct ext4_super_block *es = sbi->s_es;
135*4882a593Smuzhiyun 	ext4_fsblk_t start = ext4_blocks_count(es);
136*4882a593Smuzhiyun 	ext4_fsblk_t end = start + input->blocks_count;
137*4882a593Smuzhiyun 	ext4_group_t group = input->group;
138*4882a593Smuzhiyun 	ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group;
139*4882a593Smuzhiyun 	unsigned overhead;
140*4882a593Smuzhiyun 	ext4_fsblk_t metaend;
141*4882a593Smuzhiyun 	struct buffer_head *bh = NULL;
142*4882a593Smuzhiyun 	ext4_grpblk_t free_blocks_count, offset;
143*4882a593Smuzhiyun 	int err = -EINVAL;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	if (group != sbi->s_groups_count) {
146*4882a593Smuzhiyun 		ext4_warning(sb, "Cannot add at group %u (only %u groups)",
147*4882a593Smuzhiyun 			     input->group, sbi->s_groups_count);
148*4882a593Smuzhiyun 		return -EINVAL;
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	overhead = ext4_group_overhead_blocks(sb, group);
152*4882a593Smuzhiyun 	metaend = start + overhead;
153*4882a593Smuzhiyun 	input->free_clusters_count = free_blocks_count =
154*4882a593Smuzhiyun 		input->blocks_count - 2 - overhead - sbi->s_itb_per_group;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	if (test_opt(sb, DEBUG))
157*4882a593Smuzhiyun 		printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks "
158*4882a593Smuzhiyun 		       "(%d free, %u reserved)\n",
159*4882a593Smuzhiyun 		       ext4_bg_has_super(sb, input->group) ? "normal" :
160*4882a593Smuzhiyun 		       "no-super", input->group, input->blocks_count,
161*4882a593Smuzhiyun 		       free_blocks_count, input->reserved_blocks);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	ext4_get_group_no_and_offset(sb, start, NULL, &offset);
164*4882a593Smuzhiyun 	if (offset != 0)
165*4882a593Smuzhiyun 			ext4_warning(sb, "Last group not full");
166*4882a593Smuzhiyun 	else if (input->reserved_blocks > input->blocks_count / 5)
167*4882a593Smuzhiyun 		ext4_warning(sb, "Reserved blocks too high (%u)",
168*4882a593Smuzhiyun 			     input->reserved_blocks);
169*4882a593Smuzhiyun 	else if (free_blocks_count < 0)
170*4882a593Smuzhiyun 		ext4_warning(sb, "Bad blocks count %u",
171*4882a593Smuzhiyun 			     input->blocks_count);
172*4882a593Smuzhiyun 	else if (IS_ERR(bh = ext4_sb_bread(sb, end - 1, 0))) {
173*4882a593Smuzhiyun 		err = PTR_ERR(bh);
174*4882a593Smuzhiyun 		bh = NULL;
175*4882a593Smuzhiyun 		ext4_warning(sb, "Cannot read last block (%llu)",
176*4882a593Smuzhiyun 			     end - 1);
177*4882a593Smuzhiyun 	} else if (outside(input->block_bitmap, start, end))
178*4882a593Smuzhiyun 		ext4_warning(sb, "Block bitmap not in group (block %llu)",
179*4882a593Smuzhiyun 			     (unsigned long long)input->block_bitmap);
180*4882a593Smuzhiyun 	else if (outside(input->inode_bitmap, start, end))
181*4882a593Smuzhiyun 		ext4_warning(sb, "Inode bitmap not in group (block %llu)",
182*4882a593Smuzhiyun 			     (unsigned long long)input->inode_bitmap);
183*4882a593Smuzhiyun 	else if (outside(input->inode_table, start, end) ||
184*4882a593Smuzhiyun 		 outside(itend - 1, start, end))
185*4882a593Smuzhiyun 		ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)",
186*4882a593Smuzhiyun 			     (unsigned long long)input->inode_table, itend - 1);
187*4882a593Smuzhiyun 	else if (input->inode_bitmap == input->block_bitmap)
188*4882a593Smuzhiyun 		ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)",
189*4882a593Smuzhiyun 			     (unsigned long long)input->block_bitmap);
190*4882a593Smuzhiyun 	else if (inside(input->block_bitmap, input->inode_table, itend))
191*4882a593Smuzhiyun 		ext4_warning(sb, "Block bitmap (%llu) in inode table "
192*4882a593Smuzhiyun 			     "(%llu-%llu)",
193*4882a593Smuzhiyun 			     (unsigned long long)input->block_bitmap,
194*4882a593Smuzhiyun 			     (unsigned long long)input->inode_table, itend - 1);
195*4882a593Smuzhiyun 	else if (inside(input->inode_bitmap, input->inode_table, itend))
196*4882a593Smuzhiyun 		ext4_warning(sb, "Inode bitmap (%llu) in inode table "
197*4882a593Smuzhiyun 			     "(%llu-%llu)",
198*4882a593Smuzhiyun 			     (unsigned long long)input->inode_bitmap,
199*4882a593Smuzhiyun 			     (unsigned long long)input->inode_table, itend - 1);
200*4882a593Smuzhiyun 	else if (inside(input->block_bitmap, start, metaend))
201*4882a593Smuzhiyun 		ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)",
202*4882a593Smuzhiyun 			     (unsigned long long)input->block_bitmap,
203*4882a593Smuzhiyun 			     start, metaend - 1);
204*4882a593Smuzhiyun 	else if (inside(input->inode_bitmap, start, metaend))
205*4882a593Smuzhiyun 		ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)",
206*4882a593Smuzhiyun 			     (unsigned long long)input->inode_bitmap,
207*4882a593Smuzhiyun 			     start, metaend - 1);
208*4882a593Smuzhiyun 	else if (inside(input->inode_table, start, metaend) ||
209*4882a593Smuzhiyun 		 inside(itend - 1, start, metaend))
210*4882a593Smuzhiyun 		ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table "
211*4882a593Smuzhiyun 			     "(%llu-%llu)",
212*4882a593Smuzhiyun 			     (unsigned long long)input->inode_table,
213*4882a593Smuzhiyun 			     itend - 1, start, metaend - 1);
214*4882a593Smuzhiyun 	else
215*4882a593Smuzhiyun 		err = 0;
216*4882a593Smuzhiyun 	brelse(bh);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	return err;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun /*
222*4882a593Smuzhiyun  * ext4_new_flex_group_data is used by 64bit-resize interface to add a flex
223*4882a593Smuzhiyun  * group each time.
224*4882a593Smuzhiyun  */
225*4882a593Smuzhiyun struct ext4_new_flex_group_data {
226*4882a593Smuzhiyun 	struct ext4_new_group_data *groups;	/* new_group_data for groups
227*4882a593Smuzhiyun 						   in the flex group */
228*4882a593Smuzhiyun 	__u16 *bg_flags;			/* block group flags of groups
229*4882a593Smuzhiyun 						   in @groups */
230*4882a593Smuzhiyun 	ext4_group_t count;			/* number of groups in @groups
231*4882a593Smuzhiyun 						 */
232*4882a593Smuzhiyun };
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun /*
235*4882a593Smuzhiyun  * alloc_flex_gd() allocates a ext4_new_flex_group_data with size of
236*4882a593Smuzhiyun  * @flexbg_size.
237*4882a593Smuzhiyun  *
238*4882a593Smuzhiyun  * Returns NULL on failure otherwise address of the allocated structure.
239*4882a593Smuzhiyun  */
alloc_flex_gd(unsigned long flexbg_size)240*4882a593Smuzhiyun static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun 	struct ext4_new_flex_group_data *flex_gd;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	flex_gd = kmalloc(sizeof(*flex_gd), GFP_NOFS);
245*4882a593Smuzhiyun 	if (flex_gd == NULL)
246*4882a593Smuzhiyun 		goto out3;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	if (flexbg_size >= UINT_MAX / sizeof(struct ext4_new_group_data))
249*4882a593Smuzhiyun 		goto out2;
250*4882a593Smuzhiyun 	flex_gd->count = flexbg_size;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	flex_gd->groups = kmalloc_array(flexbg_size,
253*4882a593Smuzhiyun 					sizeof(struct ext4_new_group_data),
254*4882a593Smuzhiyun 					GFP_NOFS);
255*4882a593Smuzhiyun 	if (flex_gd->groups == NULL)
256*4882a593Smuzhiyun 		goto out2;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	flex_gd->bg_flags = kmalloc_array(flexbg_size, sizeof(__u16),
259*4882a593Smuzhiyun 					  GFP_NOFS);
260*4882a593Smuzhiyun 	if (flex_gd->bg_flags == NULL)
261*4882a593Smuzhiyun 		goto out1;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	return flex_gd;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun out1:
266*4882a593Smuzhiyun 	kfree(flex_gd->groups);
267*4882a593Smuzhiyun out2:
268*4882a593Smuzhiyun 	kfree(flex_gd);
269*4882a593Smuzhiyun out3:
270*4882a593Smuzhiyun 	return NULL;
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun 
free_flex_gd(struct ext4_new_flex_group_data * flex_gd)273*4882a593Smuzhiyun static void free_flex_gd(struct ext4_new_flex_group_data *flex_gd)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun 	kfree(flex_gd->bg_flags);
276*4882a593Smuzhiyun 	kfree(flex_gd->groups);
277*4882a593Smuzhiyun 	kfree(flex_gd);
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun /*
281*4882a593Smuzhiyun  * ext4_alloc_group_tables() allocates block bitmaps, inode bitmaps
282*4882a593Smuzhiyun  * and inode tables for a flex group.
283*4882a593Smuzhiyun  *
284*4882a593Smuzhiyun  * This function is used by 64bit-resize.  Note that this function allocates
285*4882a593Smuzhiyun  * group tables from the 1st group of groups contained by @flexgd, which may
286*4882a593Smuzhiyun  * be a partial of a flex group.
287*4882a593Smuzhiyun  *
288*4882a593Smuzhiyun  * @sb: super block of fs to which the groups belongs
289*4882a593Smuzhiyun  *
290*4882a593Smuzhiyun  * Returns 0 on a successful allocation of the metadata blocks in the
291*4882a593Smuzhiyun  * block group.
292*4882a593Smuzhiyun  */
ext4_alloc_group_tables(struct super_block * sb,struct ext4_new_flex_group_data * flex_gd,int flexbg_size)293*4882a593Smuzhiyun static int ext4_alloc_group_tables(struct super_block *sb,
294*4882a593Smuzhiyun 				struct ext4_new_flex_group_data *flex_gd,
295*4882a593Smuzhiyun 				int flexbg_size)
296*4882a593Smuzhiyun {
297*4882a593Smuzhiyun 	struct ext4_new_group_data *group_data = flex_gd->groups;
298*4882a593Smuzhiyun 	ext4_fsblk_t start_blk;
299*4882a593Smuzhiyun 	ext4_fsblk_t last_blk;
300*4882a593Smuzhiyun 	ext4_group_t src_group;
301*4882a593Smuzhiyun 	ext4_group_t bb_index = 0;
302*4882a593Smuzhiyun 	ext4_group_t ib_index = 0;
303*4882a593Smuzhiyun 	ext4_group_t it_index = 0;
304*4882a593Smuzhiyun 	ext4_group_t group;
305*4882a593Smuzhiyun 	ext4_group_t last_group;
306*4882a593Smuzhiyun 	unsigned overhead;
307*4882a593Smuzhiyun 	__u16 uninit_mask = (flexbg_size > 1) ? ~EXT4_BG_BLOCK_UNINIT : ~0;
308*4882a593Smuzhiyun 	int i;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	BUG_ON(flex_gd->count == 0 || group_data == NULL);
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	src_group = group_data[0].group;
313*4882a593Smuzhiyun 	last_group  = src_group + flex_gd->count - 1;
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	BUG_ON((flexbg_size > 1) && ((src_group & ~(flexbg_size - 1)) !=
316*4882a593Smuzhiyun 	       (last_group & ~(flexbg_size - 1))));
317*4882a593Smuzhiyun next_group:
318*4882a593Smuzhiyun 	group = group_data[0].group;
319*4882a593Smuzhiyun 	if (src_group >= group_data[0].group + flex_gd->count)
320*4882a593Smuzhiyun 		return -ENOSPC;
321*4882a593Smuzhiyun 	start_blk = ext4_group_first_block_no(sb, src_group);
322*4882a593Smuzhiyun 	last_blk = start_blk + group_data[src_group - group].blocks_count;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	overhead = ext4_group_overhead_blocks(sb, src_group);
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	start_blk += overhead;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	/* We collect contiguous blocks as much as possible. */
329*4882a593Smuzhiyun 	src_group++;
330*4882a593Smuzhiyun 	for (; src_group <= last_group; src_group++) {
331*4882a593Smuzhiyun 		overhead = ext4_group_overhead_blocks(sb, src_group);
332*4882a593Smuzhiyun 		if (overhead == 0)
333*4882a593Smuzhiyun 			last_blk += group_data[src_group - group].blocks_count;
334*4882a593Smuzhiyun 		else
335*4882a593Smuzhiyun 			break;
336*4882a593Smuzhiyun 	}
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	/* Allocate block bitmaps */
339*4882a593Smuzhiyun 	for (; bb_index < flex_gd->count; bb_index++) {
340*4882a593Smuzhiyun 		if (start_blk >= last_blk)
341*4882a593Smuzhiyun 			goto next_group;
342*4882a593Smuzhiyun 		group_data[bb_index].block_bitmap = start_blk++;
343*4882a593Smuzhiyun 		group = ext4_get_group_number(sb, start_blk - 1);
344*4882a593Smuzhiyun 		group -= group_data[0].group;
345*4882a593Smuzhiyun 		group_data[group].mdata_blocks++;
346*4882a593Smuzhiyun 		flex_gd->bg_flags[group] &= uninit_mask;
347*4882a593Smuzhiyun 	}
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	/* Allocate inode bitmaps */
350*4882a593Smuzhiyun 	for (; ib_index < flex_gd->count; ib_index++) {
351*4882a593Smuzhiyun 		if (start_blk >= last_blk)
352*4882a593Smuzhiyun 			goto next_group;
353*4882a593Smuzhiyun 		group_data[ib_index].inode_bitmap = start_blk++;
354*4882a593Smuzhiyun 		group = ext4_get_group_number(sb, start_blk - 1);
355*4882a593Smuzhiyun 		group -= group_data[0].group;
356*4882a593Smuzhiyun 		group_data[group].mdata_blocks++;
357*4882a593Smuzhiyun 		flex_gd->bg_flags[group] &= uninit_mask;
358*4882a593Smuzhiyun 	}
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	/* Allocate inode tables */
361*4882a593Smuzhiyun 	for (; it_index < flex_gd->count; it_index++) {
362*4882a593Smuzhiyun 		unsigned int itb = EXT4_SB(sb)->s_itb_per_group;
363*4882a593Smuzhiyun 		ext4_fsblk_t next_group_start;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 		if (start_blk + itb > last_blk)
366*4882a593Smuzhiyun 			goto next_group;
367*4882a593Smuzhiyun 		group_data[it_index].inode_table = start_blk;
368*4882a593Smuzhiyun 		group = ext4_get_group_number(sb, start_blk);
369*4882a593Smuzhiyun 		next_group_start = ext4_group_first_block_no(sb, group + 1);
370*4882a593Smuzhiyun 		group -= group_data[0].group;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 		if (start_blk + itb > next_group_start) {
373*4882a593Smuzhiyun 			flex_gd->bg_flags[group + 1] &= uninit_mask;
374*4882a593Smuzhiyun 			overhead = start_blk + itb - next_group_start;
375*4882a593Smuzhiyun 			group_data[group + 1].mdata_blocks += overhead;
376*4882a593Smuzhiyun 			itb -= overhead;
377*4882a593Smuzhiyun 		}
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 		group_data[group].mdata_blocks += itb;
380*4882a593Smuzhiyun 		flex_gd->bg_flags[group] &= uninit_mask;
381*4882a593Smuzhiyun 		start_blk += EXT4_SB(sb)->s_itb_per_group;
382*4882a593Smuzhiyun 	}
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	/* Update free clusters count to exclude metadata blocks */
385*4882a593Smuzhiyun 	for (i = 0; i < flex_gd->count; i++) {
386*4882a593Smuzhiyun 		group_data[i].free_clusters_count -=
387*4882a593Smuzhiyun 				EXT4_NUM_B2C(EXT4_SB(sb),
388*4882a593Smuzhiyun 					     group_data[i].mdata_blocks);
389*4882a593Smuzhiyun 	}
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	if (test_opt(sb, DEBUG)) {
392*4882a593Smuzhiyun 		int i;
393*4882a593Smuzhiyun 		group = group_data[0].group;
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 		printk(KERN_DEBUG "EXT4-fs: adding a flex group with "
396*4882a593Smuzhiyun 		       "%d groups, flexbg size is %d:\n", flex_gd->count,
397*4882a593Smuzhiyun 		       flexbg_size);
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 		for (i = 0; i < flex_gd->count; i++) {
400*4882a593Smuzhiyun 			ext4_debug(
401*4882a593Smuzhiyun 			       "adding %s group %u: %u blocks (%d free, %d mdata blocks)\n",
402*4882a593Smuzhiyun 			       ext4_bg_has_super(sb, group + i) ? "normal" :
403*4882a593Smuzhiyun 			       "no-super", group + i,
404*4882a593Smuzhiyun 			       group_data[i].blocks_count,
405*4882a593Smuzhiyun 			       group_data[i].free_clusters_count,
406*4882a593Smuzhiyun 			       group_data[i].mdata_blocks);
407*4882a593Smuzhiyun 		}
408*4882a593Smuzhiyun 	}
409*4882a593Smuzhiyun 	return 0;
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun 
bclean(handle_t * handle,struct super_block * sb,ext4_fsblk_t blk)412*4882a593Smuzhiyun static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
413*4882a593Smuzhiyun 				  ext4_fsblk_t blk)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun 	struct buffer_head *bh;
416*4882a593Smuzhiyun 	int err;
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	bh = sb_getblk(sb, blk);
419*4882a593Smuzhiyun 	if (unlikely(!bh))
420*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
421*4882a593Smuzhiyun 	BUFFER_TRACE(bh, "get_write_access");
422*4882a593Smuzhiyun 	if ((err = ext4_journal_get_write_access(handle, bh))) {
423*4882a593Smuzhiyun 		brelse(bh);
424*4882a593Smuzhiyun 		bh = ERR_PTR(err);
425*4882a593Smuzhiyun 	} else {
426*4882a593Smuzhiyun 		memset(bh->b_data, 0, sb->s_blocksize);
427*4882a593Smuzhiyun 		set_buffer_uptodate(bh);
428*4882a593Smuzhiyun 	}
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	return bh;
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun 
ext4_resize_ensure_credits_batch(handle_t * handle,int credits)433*4882a593Smuzhiyun static int ext4_resize_ensure_credits_batch(handle_t *handle, int credits)
434*4882a593Smuzhiyun {
435*4882a593Smuzhiyun 	return ext4_journal_ensure_credits_fn(handle, credits,
436*4882a593Smuzhiyun 		EXT4_MAX_TRANS_DATA, 0, 0);
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun /*
440*4882a593Smuzhiyun  * set_flexbg_block_bitmap() mark clusters [@first_cluster, @last_cluster] used.
441*4882a593Smuzhiyun  *
442*4882a593Smuzhiyun  * Helper function for ext4_setup_new_group_blocks() which set .
443*4882a593Smuzhiyun  *
444*4882a593Smuzhiyun  * @sb: super block
445*4882a593Smuzhiyun  * @handle: journal handle
446*4882a593Smuzhiyun  * @flex_gd: flex group data
447*4882a593Smuzhiyun  */
set_flexbg_block_bitmap(struct super_block * sb,handle_t * handle,struct ext4_new_flex_group_data * flex_gd,ext4_fsblk_t first_cluster,ext4_fsblk_t last_cluster)448*4882a593Smuzhiyun static int set_flexbg_block_bitmap(struct super_block *sb, handle_t *handle,
449*4882a593Smuzhiyun 			struct ext4_new_flex_group_data *flex_gd,
450*4882a593Smuzhiyun 			ext4_fsblk_t first_cluster, ext4_fsblk_t last_cluster)
451*4882a593Smuzhiyun {
452*4882a593Smuzhiyun 	struct ext4_sb_info *sbi = EXT4_SB(sb);
453*4882a593Smuzhiyun 	ext4_group_t count = last_cluster - first_cluster + 1;
454*4882a593Smuzhiyun 	ext4_group_t count2;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	ext4_debug("mark clusters [%llu-%llu] used\n", first_cluster,
457*4882a593Smuzhiyun 		   last_cluster);
458*4882a593Smuzhiyun 	for (count2 = count; count > 0;
459*4882a593Smuzhiyun 	     count -= count2, first_cluster += count2) {
460*4882a593Smuzhiyun 		ext4_fsblk_t start;
461*4882a593Smuzhiyun 		struct buffer_head *bh;
462*4882a593Smuzhiyun 		ext4_group_t group;
463*4882a593Smuzhiyun 		int err;
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 		group = ext4_get_group_number(sb, EXT4_C2B(sbi, first_cluster));
466*4882a593Smuzhiyun 		start = EXT4_B2C(sbi, ext4_group_first_block_no(sb, group));
467*4882a593Smuzhiyun 		group -= flex_gd->groups[0].group;
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 		count2 = EXT4_CLUSTERS_PER_GROUP(sb) - (first_cluster - start);
470*4882a593Smuzhiyun 		if (count2 > count)
471*4882a593Smuzhiyun 			count2 = count;
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 		if (flex_gd->bg_flags[group] & EXT4_BG_BLOCK_UNINIT) {
474*4882a593Smuzhiyun 			BUG_ON(flex_gd->count > 1);
475*4882a593Smuzhiyun 			continue;
476*4882a593Smuzhiyun 		}
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 		err = ext4_resize_ensure_credits_batch(handle, 1);
479*4882a593Smuzhiyun 		if (err < 0)
480*4882a593Smuzhiyun 			return err;
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 		bh = sb_getblk(sb, flex_gd->groups[group].block_bitmap);
483*4882a593Smuzhiyun 		if (unlikely(!bh))
484*4882a593Smuzhiyun 			return -ENOMEM;
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 		BUFFER_TRACE(bh, "get_write_access");
487*4882a593Smuzhiyun 		err = ext4_journal_get_write_access(handle, bh);
488*4882a593Smuzhiyun 		if (err) {
489*4882a593Smuzhiyun 			brelse(bh);
490*4882a593Smuzhiyun 			return err;
491*4882a593Smuzhiyun 		}
492*4882a593Smuzhiyun 		ext4_debug("mark block bitmap %#04llx (+%llu/%u)\n",
493*4882a593Smuzhiyun 			   first_cluster, first_cluster - start, count2);
494*4882a593Smuzhiyun 		ext4_set_bits(bh->b_data, first_cluster - start, count2);
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 		err = ext4_handle_dirty_metadata(handle, NULL, bh);
497*4882a593Smuzhiyun 		brelse(bh);
498*4882a593Smuzhiyun 		if (unlikely(err))
499*4882a593Smuzhiyun 			return err;
500*4882a593Smuzhiyun 	}
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	return 0;
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun /*
506*4882a593Smuzhiyun  * Set up the block and inode bitmaps, and the inode table for the new groups.
507*4882a593Smuzhiyun  * This doesn't need to be part of the main transaction, since we are only
508*4882a593Smuzhiyun  * changing blocks outside the actual filesystem.  We still do journaling to
509*4882a593Smuzhiyun  * ensure the recovery is correct in case of a failure just after resize.
510*4882a593Smuzhiyun  * If any part of this fails, we simply abort the resize.
511*4882a593Smuzhiyun  *
512*4882a593Smuzhiyun  * setup_new_flex_group_blocks handles a flex group as follow:
513*4882a593Smuzhiyun  *  1. copy super block and GDT, and initialize group tables if necessary.
514*4882a593Smuzhiyun  *     In this step, we only set bits in blocks bitmaps for blocks taken by
515*4882a593Smuzhiyun  *     super block and GDT.
516*4882a593Smuzhiyun  *  2. allocate group tables in block bitmaps, that is, set bits in block
517*4882a593Smuzhiyun  *     bitmap for blocks taken by group tables.
518*4882a593Smuzhiyun  */
setup_new_flex_group_blocks(struct super_block * sb,struct ext4_new_flex_group_data * flex_gd)519*4882a593Smuzhiyun static int setup_new_flex_group_blocks(struct super_block *sb,
520*4882a593Smuzhiyun 				struct ext4_new_flex_group_data *flex_gd)
521*4882a593Smuzhiyun {
522*4882a593Smuzhiyun 	int group_table_count[] = {1, 1, EXT4_SB(sb)->s_itb_per_group};
523*4882a593Smuzhiyun 	ext4_fsblk_t start;
524*4882a593Smuzhiyun 	ext4_fsblk_t block;
525*4882a593Smuzhiyun 	struct ext4_sb_info *sbi = EXT4_SB(sb);
526*4882a593Smuzhiyun 	struct ext4_super_block *es = sbi->s_es;
527*4882a593Smuzhiyun 	struct ext4_new_group_data *group_data = flex_gd->groups;
528*4882a593Smuzhiyun 	__u16 *bg_flags = flex_gd->bg_flags;
529*4882a593Smuzhiyun 	handle_t *handle;
530*4882a593Smuzhiyun 	ext4_group_t group, count;
531*4882a593Smuzhiyun 	struct buffer_head *bh = NULL;
532*4882a593Smuzhiyun 	int reserved_gdb, i, j, err = 0, err2;
533*4882a593Smuzhiyun 	int meta_bg;
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	BUG_ON(!flex_gd->count || !group_data ||
536*4882a593Smuzhiyun 	       group_data[0].group != sbi->s_groups_count);
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks);
539*4882a593Smuzhiyun 	meta_bg = ext4_has_feature_meta_bg(sb);
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	/* This transaction may be extended/restarted along the way */
542*4882a593Smuzhiyun 	handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA);
543*4882a593Smuzhiyun 	if (IS_ERR(handle))
544*4882a593Smuzhiyun 		return PTR_ERR(handle);
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	group = group_data[0].group;
547*4882a593Smuzhiyun 	for (i = 0; i < flex_gd->count; i++, group++) {
548*4882a593Smuzhiyun 		unsigned long gdblocks;
549*4882a593Smuzhiyun 		ext4_grpblk_t overhead;
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 		gdblocks = ext4_bg_num_gdb(sb, group);
552*4882a593Smuzhiyun 		start = ext4_group_first_block_no(sb, group);
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 		if (meta_bg == 0 && !ext4_bg_has_super(sb, group))
555*4882a593Smuzhiyun 			goto handle_itb;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 		if (meta_bg == 1) {
558*4882a593Smuzhiyun 			ext4_group_t first_group;
559*4882a593Smuzhiyun 			first_group = ext4_meta_bg_first_group(sb, group);
560*4882a593Smuzhiyun 			if (first_group != group + 1 &&
561*4882a593Smuzhiyun 			    first_group != group + EXT4_DESC_PER_BLOCK(sb) - 1)
562*4882a593Smuzhiyun 				goto handle_itb;
563*4882a593Smuzhiyun 		}
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 		block = start + ext4_bg_has_super(sb, group);
566*4882a593Smuzhiyun 		/* Copy all of the GDT blocks into the backup in this group */
567*4882a593Smuzhiyun 		for (j = 0; j < gdblocks; j++, block++) {
568*4882a593Smuzhiyun 			struct buffer_head *gdb;
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 			ext4_debug("update backup group %#04llx\n", block);
571*4882a593Smuzhiyun 			err = ext4_resize_ensure_credits_batch(handle, 1);
572*4882a593Smuzhiyun 			if (err < 0)
573*4882a593Smuzhiyun 				goto out;
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 			gdb = sb_getblk(sb, block);
576*4882a593Smuzhiyun 			if (unlikely(!gdb)) {
577*4882a593Smuzhiyun 				err = -ENOMEM;
578*4882a593Smuzhiyun 				goto out;
579*4882a593Smuzhiyun 			}
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 			BUFFER_TRACE(gdb, "get_write_access");
582*4882a593Smuzhiyun 			err = ext4_journal_get_write_access(handle, gdb);
583*4882a593Smuzhiyun 			if (err) {
584*4882a593Smuzhiyun 				brelse(gdb);
585*4882a593Smuzhiyun 				goto out;
586*4882a593Smuzhiyun 			}
587*4882a593Smuzhiyun 			memcpy(gdb->b_data, sbi_array_rcu_deref(sbi,
588*4882a593Smuzhiyun 				s_group_desc, j)->b_data, gdb->b_size);
589*4882a593Smuzhiyun 			set_buffer_uptodate(gdb);
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 			err = ext4_handle_dirty_metadata(handle, NULL, gdb);
592*4882a593Smuzhiyun 			if (unlikely(err)) {
593*4882a593Smuzhiyun 				brelse(gdb);
594*4882a593Smuzhiyun 				goto out;
595*4882a593Smuzhiyun 			}
596*4882a593Smuzhiyun 			brelse(gdb);
597*4882a593Smuzhiyun 		}
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 		/* Zero out all of the reserved backup group descriptor
600*4882a593Smuzhiyun 		 * table blocks
601*4882a593Smuzhiyun 		 */
602*4882a593Smuzhiyun 		if (ext4_bg_has_super(sb, group)) {
603*4882a593Smuzhiyun 			err = sb_issue_zeroout(sb, gdblocks + start + 1,
604*4882a593Smuzhiyun 					reserved_gdb, GFP_NOFS);
605*4882a593Smuzhiyun 			if (err)
606*4882a593Smuzhiyun 				goto out;
607*4882a593Smuzhiyun 		}
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun handle_itb:
610*4882a593Smuzhiyun 		/* Initialize group tables of the grop @group */
611*4882a593Smuzhiyun 		if (!(bg_flags[i] & EXT4_BG_INODE_ZEROED))
612*4882a593Smuzhiyun 			goto handle_bb;
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 		/* Zero out all of the inode table blocks */
615*4882a593Smuzhiyun 		block = group_data[i].inode_table;
616*4882a593Smuzhiyun 		ext4_debug("clear inode table blocks %#04llx -> %#04lx\n",
617*4882a593Smuzhiyun 			   block, sbi->s_itb_per_group);
618*4882a593Smuzhiyun 		err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group,
619*4882a593Smuzhiyun 				       GFP_NOFS);
620*4882a593Smuzhiyun 		if (err)
621*4882a593Smuzhiyun 			goto out;
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun handle_bb:
624*4882a593Smuzhiyun 		if (bg_flags[i] & EXT4_BG_BLOCK_UNINIT)
625*4882a593Smuzhiyun 			goto handle_ib;
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 		/* Initialize block bitmap of the @group */
628*4882a593Smuzhiyun 		block = group_data[i].block_bitmap;
629*4882a593Smuzhiyun 		err = ext4_resize_ensure_credits_batch(handle, 1);
630*4882a593Smuzhiyun 		if (err < 0)
631*4882a593Smuzhiyun 			goto out;
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 		bh = bclean(handle, sb, block);
634*4882a593Smuzhiyun 		if (IS_ERR(bh)) {
635*4882a593Smuzhiyun 			err = PTR_ERR(bh);
636*4882a593Smuzhiyun 			goto out;
637*4882a593Smuzhiyun 		}
638*4882a593Smuzhiyun 		overhead = ext4_group_overhead_blocks(sb, group);
639*4882a593Smuzhiyun 		if (overhead != 0) {
640*4882a593Smuzhiyun 			ext4_debug("mark backup superblock %#04llx (+0)\n",
641*4882a593Smuzhiyun 				   start);
642*4882a593Smuzhiyun 			ext4_set_bits(bh->b_data, 0,
643*4882a593Smuzhiyun 				      EXT4_NUM_B2C(sbi, overhead));
644*4882a593Smuzhiyun 		}
645*4882a593Smuzhiyun 		ext4_mark_bitmap_end(EXT4_B2C(sbi, group_data[i].blocks_count),
646*4882a593Smuzhiyun 				     sb->s_blocksize * 8, bh->b_data);
647*4882a593Smuzhiyun 		err = ext4_handle_dirty_metadata(handle, NULL, bh);
648*4882a593Smuzhiyun 		brelse(bh);
649*4882a593Smuzhiyun 		if (err)
650*4882a593Smuzhiyun 			goto out;
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun handle_ib:
653*4882a593Smuzhiyun 		if (bg_flags[i] & EXT4_BG_INODE_UNINIT)
654*4882a593Smuzhiyun 			continue;
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 		/* Initialize inode bitmap of the @group */
657*4882a593Smuzhiyun 		block = group_data[i].inode_bitmap;
658*4882a593Smuzhiyun 		err = ext4_resize_ensure_credits_batch(handle, 1);
659*4882a593Smuzhiyun 		if (err < 0)
660*4882a593Smuzhiyun 			goto out;
661*4882a593Smuzhiyun 		/* Mark unused entries in inode bitmap used */
662*4882a593Smuzhiyun 		bh = bclean(handle, sb, block);
663*4882a593Smuzhiyun 		if (IS_ERR(bh)) {
664*4882a593Smuzhiyun 			err = PTR_ERR(bh);
665*4882a593Smuzhiyun 			goto out;
666*4882a593Smuzhiyun 		}
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 		ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb),
669*4882a593Smuzhiyun 				     sb->s_blocksize * 8, bh->b_data);
670*4882a593Smuzhiyun 		err = ext4_handle_dirty_metadata(handle, NULL, bh);
671*4882a593Smuzhiyun 		brelse(bh);
672*4882a593Smuzhiyun 		if (err)
673*4882a593Smuzhiyun 			goto out;
674*4882a593Smuzhiyun 	}
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	/* Mark group tables in block bitmap */
677*4882a593Smuzhiyun 	for (j = 0; j < GROUP_TABLE_COUNT; j++) {
678*4882a593Smuzhiyun 		count = group_table_count[j];
679*4882a593Smuzhiyun 		start = (&group_data[0].block_bitmap)[j];
680*4882a593Smuzhiyun 		block = start;
681*4882a593Smuzhiyun 		for (i = 1; i < flex_gd->count; i++) {
682*4882a593Smuzhiyun 			block += group_table_count[j];
683*4882a593Smuzhiyun 			if (block == (&group_data[i].block_bitmap)[j]) {
684*4882a593Smuzhiyun 				count += group_table_count[j];
685*4882a593Smuzhiyun 				continue;
686*4882a593Smuzhiyun 			}
687*4882a593Smuzhiyun 			err = set_flexbg_block_bitmap(sb, handle,
688*4882a593Smuzhiyun 						      flex_gd,
689*4882a593Smuzhiyun 						      EXT4_B2C(sbi, start),
690*4882a593Smuzhiyun 						      EXT4_B2C(sbi,
691*4882a593Smuzhiyun 							       start + count
692*4882a593Smuzhiyun 							       - 1));
693*4882a593Smuzhiyun 			if (err)
694*4882a593Smuzhiyun 				goto out;
695*4882a593Smuzhiyun 			count = group_table_count[j];
696*4882a593Smuzhiyun 			start = (&group_data[i].block_bitmap)[j];
697*4882a593Smuzhiyun 			block = start;
698*4882a593Smuzhiyun 		}
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 		if (count) {
701*4882a593Smuzhiyun 			err = set_flexbg_block_bitmap(sb, handle,
702*4882a593Smuzhiyun 						      flex_gd,
703*4882a593Smuzhiyun 						      EXT4_B2C(sbi, start),
704*4882a593Smuzhiyun 						      EXT4_B2C(sbi,
705*4882a593Smuzhiyun 							       start + count
706*4882a593Smuzhiyun 							       - 1));
707*4882a593Smuzhiyun 			if (err)
708*4882a593Smuzhiyun 				goto out;
709*4882a593Smuzhiyun 		}
710*4882a593Smuzhiyun 	}
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun out:
713*4882a593Smuzhiyun 	err2 = ext4_journal_stop(handle);
714*4882a593Smuzhiyun 	if (err2 && !err)
715*4882a593Smuzhiyun 		err = err2;
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	return err;
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun /*
721*4882a593Smuzhiyun  * Iterate through the groups which hold BACKUP superblock/GDT copies in an
722*4882a593Smuzhiyun  * ext4 filesystem.  The counters should be initialized to 1, 5, and 7 before
723*4882a593Smuzhiyun  * calling this for the first time.  In a sparse filesystem it will be the
724*4882a593Smuzhiyun  * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
725*4882a593Smuzhiyun  * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
726*4882a593Smuzhiyun  */
ext4_list_backups(struct super_block * sb,unsigned * three,unsigned * five,unsigned * seven)727*4882a593Smuzhiyun static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
728*4882a593Smuzhiyun 				  unsigned *five, unsigned *seven)
729*4882a593Smuzhiyun {
730*4882a593Smuzhiyun 	unsigned *min = three;
731*4882a593Smuzhiyun 	int mult = 3;
732*4882a593Smuzhiyun 	unsigned ret;
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	if (!ext4_has_feature_sparse_super(sb)) {
735*4882a593Smuzhiyun 		ret = *min;
736*4882a593Smuzhiyun 		*min += 1;
737*4882a593Smuzhiyun 		return ret;
738*4882a593Smuzhiyun 	}
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun 	if (*five < *min) {
741*4882a593Smuzhiyun 		min = five;
742*4882a593Smuzhiyun 		mult = 5;
743*4882a593Smuzhiyun 	}
744*4882a593Smuzhiyun 	if (*seven < *min) {
745*4882a593Smuzhiyun 		min = seven;
746*4882a593Smuzhiyun 		mult = 7;
747*4882a593Smuzhiyun 	}
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 	ret = *min;
750*4882a593Smuzhiyun 	*min *= mult;
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	return ret;
753*4882a593Smuzhiyun }
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun /*
756*4882a593Smuzhiyun  * Check that all of the backup GDT blocks are held in the primary GDT block.
757*4882a593Smuzhiyun  * It is assumed that they are stored in group order.  Returns the number of
758*4882a593Smuzhiyun  * groups in current filesystem that have BACKUPS, or -ve error code.
759*4882a593Smuzhiyun  */
verify_reserved_gdb(struct super_block * sb,ext4_group_t end,struct buffer_head * primary)760*4882a593Smuzhiyun static int verify_reserved_gdb(struct super_block *sb,
761*4882a593Smuzhiyun 			       ext4_group_t end,
762*4882a593Smuzhiyun 			       struct buffer_head *primary)
763*4882a593Smuzhiyun {
764*4882a593Smuzhiyun 	const ext4_fsblk_t blk = primary->b_blocknr;
765*4882a593Smuzhiyun 	unsigned three = 1;
766*4882a593Smuzhiyun 	unsigned five = 5;
767*4882a593Smuzhiyun 	unsigned seven = 7;
768*4882a593Smuzhiyun 	unsigned grp;
769*4882a593Smuzhiyun 	__le32 *p = (__le32 *)primary->b_data;
770*4882a593Smuzhiyun 	int gdbackups = 0;
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 	while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
773*4882a593Smuzhiyun 		if (le32_to_cpu(*p++) !=
774*4882a593Smuzhiyun 		    grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
775*4882a593Smuzhiyun 			ext4_warning(sb, "reserved GDT %llu"
776*4882a593Smuzhiyun 				     " missing grp %d (%llu)",
777*4882a593Smuzhiyun 				     blk, grp,
778*4882a593Smuzhiyun 				     grp *
779*4882a593Smuzhiyun 				     (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
780*4882a593Smuzhiyun 				     blk);
781*4882a593Smuzhiyun 			return -EINVAL;
782*4882a593Smuzhiyun 		}
783*4882a593Smuzhiyun 		if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
784*4882a593Smuzhiyun 			return -EFBIG;
785*4882a593Smuzhiyun 	}
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	return gdbackups;
788*4882a593Smuzhiyun }
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun /*
791*4882a593Smuzhiyun  * Called when we need to bring a reserved group descriptor table block into
792*4882a593Smuzhiyun  * use from the resize inode.  The primary copy of the new GDT block currently
793*4882a593Smuzhiyun  * is an indirect block (under the double indirect block in the resize inode).
794*4882a593Smuzhiyun  * The new backup GDT blocks will be stored as leaf blocks in this indirect
795*4882a593Smuzhiyun  * block, in group order.  Even though we know all the block numbers we need,
796*4882a593Smuzhiyun  * we check to ensure that the resize inode has actually reserved these blocks.
797*4882a593Smuzhiyun  *
798*4882a593Smuzhiyun  * Don't need to update the block bitmaps because the blocks are still in use.
799*4882a593Smuzhiyun  *
800*4882a593Smuzhiyun  * We get all of the error cases out of the way, so that we are sure to not
801*4882a593Smuzhiyun  * fail once we start modifying the data on disk, because JBD has no rollback.
802*4882a593Smuzhiyun  */
add_new_gdb(handle_t * handle,struct inode * inode,ext4_group_t group)803*4882a593Smuzhiyun static int add_new_gdb(handle_t *handle, struct inode *inode,
804*4882a593Smuzhiyun 		       ext4_group_t group)
805*4882a593Smuzhiyun {
806*4882a593Smuzhiyun 	struct super_block *sb = inode->i_sb;
807*4882a593Smuzhiyun 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
808*4882a593Smuzhiyun 	unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
809*4882a593Smuzhiyun 	ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
810*4882a593Smuzhiyun 	struct buffer_head **o_group_desc, **n_group_desc = NULL;
811*4882a593Smuzhiyun 	struct buffer_head *dind = NULL;
812*4882a593Smuzhiyun 	struct buffer_head *gdb_bh = NULL;
813*4882a593Smuzhiyun 	int gdbackups;
814*4882a593Smuzhiyun 	struct ext4_iloc iloc = { .bh = NULL };
815*4882a593Smuzhiyun 	__le32 *data;
816*4882a593Smuzhiyun 	int err;
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 	if (test_opt(sb, DEBUG))
819*4882a593Smuzhiyun 		printk(KERN_DEBUG
820*4882a593Smuzhiyun 		       "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
821*4882a593Smuzhiyun 		       gdb_num);
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	gdb_bh = ext4_sb_bread(sb, gdblock, 0);
824*4882a593Smuzhiyun 	if (IS_ERR(gdb_bh))
825*4882a593Smuzhiyun 		return PTR_ERR(gdb_bh);
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 	gdbackups = verify_reserved_gdb(sb, group, gdb_bh);
828*4882a593Smuzhiyun 	if (gdbackups < 0) {
829*4882a593Smuzhiyun 		err = gdbackups;
830*4882a593Smuzhiyun 		goto errout;
831*4882a593Smuzhiyun 	}
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 	data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
834*4882a593Smuzhiyun 	dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0);
835*4882a593Smuzhiyun 	if (IS_ERR(dind)) {
836*4882a593Smuzhiyun 		err = PTR_ERR(dind);
837*4882a593Smuzhiyun 		dind = NULL;
838*4882a593Smuzhiyun 		goto errout;
839*4882a593Smuzhiyun 	}
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	data = (__le32 *)dind->b_data;
842*4882a593Smuzhiyun 	if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
843*4882a593Smuzhiyun 		ext4_warning(sb, "new group %u GDT block %llu not reserved",
844*4882a593Smuzhiyun 			     group, gdblock);
845*4882a593Smuzhiyun 		err = -EINVAL;
846*4882a593Smuzhiyun 		goto errout;
847*4882a593Smuzhiyun 	}
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
850*4882a593Smuzhiyun 	err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
851*4882a593Smuzhiyun 	if (unlikely(err))
852*4882a593Smuzhiyun 		goto errout;
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 	BUFFER_TRACE(gdb_bh, "get_write_access");
855*4882a593Smuzhiyun 	err = ext4_journal_get_write_access(handle, gdb_bh);
856*4882a593Smuzhiyun 	if (unlikely(err))
857*4882a593Smuzhiyun 		goto errout;
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun 	BUFFER_TRACE(dind, "get_write_access");
860*4882a593Smuzhiyun 	err = ext4_journal_get_write_access(handle, dind);
861*4882a593Smuzhiyun 	if (unlikely(err)) {
862*4882a593Smuzhiyun 		ext4_std_error(sb, err);
863*4882a593Smuzhiyun 		goto errout;
864*4882a593Smuzhiyun 	}
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	/* ext4_reserve_inode_write() gets a reference on the iloc */
867*4882a593Smuzhiyun 	err = ext4_reserve_inode_write(handle, inode, &iloc);
868*4882a593Smuzhiyun 	if (unlikely(err))
869*4882a593Smuzhiyun 		goto errout;
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	n_group_desc = kvmalloc((gdb_num + 1) * sizeof(struct buffer_head *),
872*4882a593Smuzhiyun 				GFP_KERNEL);
873*4882a593Smuzhiyun 	if (!n_group_desc) {
874*4882a593Smuzhiyun 		err = -ENOMEM;
875*4882a593Smuzhiyun 		ext4_warning(sb, "not enough memory for %lu groups",
876*4882a593Smuzhiyun 			     gdb_num + 1);
877*4882a593Smuzhiyun 		goto errout;
878*4882a593Smuzhiyun 	}
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun 	/*
881*4882a593Smuzhiyun 	 * Finally, we have all of the possible failures behind us...
882*4882a593Smuzhiyun 	 *
883*4882a593Smuzhiyun 	 * Remove new GDT block from inode double-indirect block and clear out
884*4882a593Smuzhiyun 	 * the new GDT block for use (which also "frees" the backup GDT blocks
885*4882a593Smuzhiyun 	 * from the reserved inode).  We don't need to change the bitmaps for
886*4882a593Smuzhiyun 	 * these blocks, because they are marked as in-use from being in the
887*4882a593Smuzhiyun 	 * reserved inode, and will become GDT blocks (primary and backup).
888*4882a593Smuzhiyun 	 */
889*4882a593Smuzhiyun 	data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
890*4882a593Smuzhiyun 	err = ext4_handle_dirty_metadata(handle, NULL, dind);
891*4882a593Smuzhiyun 	if (unlikely(err)) {
892*4882a593Smuzhiyun 		ext4_std_error(sb, err);
893*4882a593Smuzhiyun 		goto errout;
894*4882a593Smuzhiyun 	}
895*4882a593Smuzhiyun 	inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >>
896*4882a593Smuzhiyun 			   (9 - EXT4_SB(sb)->s_cluster_bits);
897*4882a593Smuzhiyun 	ext4_mark_iloc_dirty(handle, inode, &iloc);
898*4882a593Smuzhiyun 	memset(gdb_bh->b_data, 0, sb->s_blocksize);
899*4882a593Smuzhiyun 	err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
900*4882a593Smuzhiyun 	if (unlikely(err)) {
901*4882a593Smuzhiyun 		ext4_std_error(sb, err);
902*4882a593Smuzhiyun 		iloc.bh = NULL;
903*4882a593Smuzhiyun 		goto errout;
904*4882a593Smuzhiyun 	}
905*4882a593Smuzhiyun 	brelse(dind);
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	rcu_read_lock();
908*4882a593Smuzhiyun 	o_group_desc = rcu_dereference(EXT4_SB(sb)->s_group_desc);
909*4882a593Smuzhiyun 	memcpy(n_group_desc, o_group_desc,
910*4882a593Smuzhiyun 	       EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
911*4882a593Smuzhiyun 	rcu_read_unlock();
912*4882a593Smuzhiyun 	n_group_desc[gdb_num] = gdb_bh;
913*4882a593Smuzhiyun 	rcu_assign_pointer(EXT4_SB(sb)->s_group_desc, n_group_desc);
914*4882a593Smuzhiyun 	EXT4_SB(sb)->s_gdb_count++;
915*4882a593Smuzhiyun 	ext4_kvfree_array_rcu(o_group_desc);
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
918*4882a593Smuzhiyun 	err = ext4_handle_dirty_super(handle, sb);
919*4882a593Smuzhiyun 	if (err)
920*4882a593Smuzhiyun 		ext4_std_error(sb, err);
921*4882a593Smuzhiyun 	return err;
922*4882a593Smuzhiyun errout:
923*4882a593Smuzhiyun 	kvfree(n_group_desc);
924*4882a593Smuzhiyun 	brelse(iloc.bh);
925*4882a593Smuzhiyun 	brelse(dind);
926*4882a593Smuzhiyun 	brelse(gdb_bh);
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 	ext4_debug("leaving with error %d\n", err);
929*4882a593Smuzhiyun 	return err;
930*4882a593Smuzhiyun }
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun /*
933*4882a593Smuzhiyun  * add_new_gdb_meta_bg is the sister of add_new_gdb.
934*4882a593Smuzhiyun  */
add_new_gdb_meta_bg(struct super_block * sb,handle_t * handle,ext4_group_t group)935*4882a593Smuzhiyun static int add_new_gdb_meta_bg(struct super_block *sb,
936*4882a593Smuzhiyun 			       handle_t *handle, ext4_group_t group) {
937*4882a593Smuzhiyun 	ext4_fsblk_t gdblock;
938*4882a593Smuzhiyun 	struct buffer_head *gdb_bh;
939*4882a593Smuzhiyun 	struct buffer_head **o_group_desc, **n_group_desc;
940*4882a593Smuzhiyun 	unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
941*4882a593Smuzhiyun 	int err;
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun 	gdblock = ext4_meta_bg_first_block_no(sb, group) +
944*4882a593Smuzhiyun 		   ext4_bg_has_super(sb, group);
945*4882a593Smuzhiyun 	gdb_bh = ext4_sb_bread(sb, gdblock, 0);
946*4882a593Smuzhiyun 	if (IS_ERR(gdb_bh))
947*4882a593Smuzhiyun 		return PTR_ERR(gdb_bh);
948*4882a593Smuzhiyun 	n_group_desc = kvmalloc((gdb_num + 1) * sizeof(struct buffer_head *),
949*4882a593Smuzhiyun 				GFP_KERNEL);
950*4882a593Smuzhiyun 	if (!n_group_desc) {
951*4882a593Smuzhiyun 		brelse(gdb_bh);
952*4882a593Smuzhiyun 		err = -ENOMEM;
953*4882a593Smuzhiyun 		ext4_warning(sb, "not enough memory for %lu groups",
954*4882a593Smuzhiyun 			     gdb_num + 1);
955*4882a593Smuzhiyun 		return err;
956*4882a593Smuzhiyun 	}
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 	rcu_read_lock();
959*4882a593Smuzhiyun 	o_group_desc = rcu_dereference(EXT4_SB(sb)->s_group_desc);
960*4882a593Smuzhiyun 	memcpy(n_group_desc, o_group_desc,
961*4882a593Smuzhiyun 	       EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
962*4882a593Smuzhiyun 	rcu_read_unlock();
963*4882a593Smuzhiyun 	n_group_desc[gdb_num] = gdb_bh;
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun 	BUFFER_TRACE(gdb_bh, "get_write_access");
966*4882a593Smuzhiyun 	err = ext4_journal_get_write_access(handle, gdb_bh);
967*4882a593Smuzhiyun 	if (err) {
968*4882a593Smuzhiyun 		kvfree(n_group_desc);
969*4882a593Smuzhiyun 		brelse(gdb_bh);
970*4882a593Smuzhiyun 		return err;
971*4882a593Smuzhiyun 	}
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	rcu_assign_pointer(EXT4_SB(sb)->s_group_desc, n_group_desc);
974*4882a593Smuzhiyun 	EXT4_SB(sb)->s_gdb_count++;
975*4882a593Smuzhiyun 	ext4_kvfree_array_rcu(o_group_desc);
976*4882a593Smuzhiyun 	return err;
977*4882a593Smuzhiyun }
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun /*
980*4882a593Smuzhiyun  * Called when we are adding a new group which has a backup copy of each of
981*4882a593Smuzhiyun  * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
982*4882a593Smuzhiyun  * We need to add these reserved backup GDT blocks to the resize inode, so
983*4882a593Smuzhiyun  * that they are kept for future resizing and not allocated to files.
984*4882a593Smuzhiyun  *
985*4882a593Smuzhiyun  * Each reserved backup GDT block will go into a different indirect block.
986*4882a593Smuzhiyun  * The indirect blocks are actually the primary reserved GDT blocks,
987*4882a593Smuzhiyun  * so we know in advance what their block numbers are.  We only get the
988*4882a593Smuzhiyun  * double-indirect block to verify it is pointing to the primary reserved
989*4882a593Smuzhiyun  * GDT blocks so we don't overwrite a data block by accident.  The reserved
990*4882a593Smuzhiyun  * backup GDT blocks are stored in their reserved primary GDT block.
991*4882a593Smuzhiyun  */
reserve_backup_gdb(handle_t * handle,struct inode * inode,ext4_group_t group)992*4882a593Smuzhiyun static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
993*4882a593Smuzhiyun 			      ext4_group_t group)
994*4882a593Smuzhiyun {
995*4882a593Smuzhiyun 	struct super_block *sb = inode->i_sb;
996*4882a593Smuzhiyun 	int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
997*4882a593Smuzhiyun 	int cluster_bits = EXT4_SB(sb)->s_cluster_bits;
998*4882a593Smuzhiyun 	struct buffer_head **primary;
999*4882a593Smuzhiyun 	struct buffer_head *dind;
1000*4882a593Smuzhiyun 	struct ext4_iloc iloc;
1001*4882a593Smuzhiyun 	ext4_fsblk_t blk;
1002*4882a593Smuzhiyun 	__le32 *data, *end;
1003*4882a593Smuzhiyun 	int gdbackups = 0;
1004*4882a593Smuzhiyun 	int res, i;
1005*4882a593Smuzhiyun 	int err;
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 	primary = kmalloc_array(reserved_gdb, sizeof(*primary), GFP_NOFS);
1008*4882a593Smuzhiyun 	if (!primary)
1009*4882a593Smuzhiyun 		return -ENOMEM;
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
1012*4882a593Smuzhiyun 	dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0);
1013*4882a593Smuzhiyun 	if (IS_ERR(dind)) {
1014*4882a593Smuzhiyun 		err = PTR_ERR(dind);
1015*4882a593Smuzhiyun 		dind = NULL;
1016*4882a593Smuzhiyun 		goto exit_free;
1017*4882a593Smuzhiyun 	}
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun 	blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count;
1020*4882a593Smuzhiyun 	data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count %
1021*4882a593Smuzhiyun 					 EXT4_ADDR_PER_BLOCK(sb));
1022*4882a593Smuzhiyun 	end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb);
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun 	/* Get each reserved primary GDT block and verify it holds backups */
1025*4882a593Smuzhiyun 	for (res = 0; res < reserved_gdb; res++, blk++) {
1026*4882a593Smuzhiyun 		if (le32_to_cpu(*data) != blk) {
1027*4882a593Smuzhiyun 			ext4_warning(sb, "reserved block %llu"
1028*4882a593Smuzhiyun 				     " not at offset %ld",
1029*4882a593Smuzhiyun 				     blk,
1030*4882a593Smuzhiyun 				     (long)(data - (__le32 *)dind->b_data));
1031*4882a593Smuzhiyun 			err = -EINVAL;
1032*4882a593Smuzhiyun 			goto exit_bh;
1033*4882a593Smuzhiyun 		}
1034*4882a593Smuzhiyun 		primary[res] = ext4_sb_bread(sb, blk, 0);
1035*4882a593Smuzhiyun 		if (IS_ERR(primary[res])) {
1036*4882a593Smuzhiyun 			err = PTR_ERR(primary[res]);
1037*4882a593Smuzhiyun 			primary[res] = NULL;
1038*4882a593Smuzhiyun 			goto exit_bh;
1039*4882a593Smuzhiyun 		}
1040*4882a593Smuzhiyun 		gdbackups = verify_reserved_gdb(sb, group, primary[res]);
1041*4882a593Smuzhiyun 		if (gdbackups < 0) {
1042*4882a593Smuzhiyun 			brelse(primary[res]);
1043*4882a593Smuzhiyun 			err = gdbackups;
1044*4882a593Smuzhiyun 			goto exit_bh;
1045*4882a593Smuzhiyun 		}
1046*4882a593Smuzhiyun 		if (++data >= end)
1047*4882a593Smuzhiyun 			data = (__le32 *)dind->b_data;
1048*4882a593Smuzhiyun 	}
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 	for (i = 0; i < reserved_gdb; i++) {
1051*4882a593Smuzhiyun 		BUFFER_TRACE(primary[i], "get_write_access");
1052*4882a593Smuzhiyun 		if ((err = ext4_journal_get_write_access(handle, primary[i])))
1053*4882a593Smuzhiyun 			goto exit_bh;
1054*4882a593Smuzhiyun 	}
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 	if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
1057*4882a593Smuzhiyun 		goto exit_bh;
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun 	/*
1060*4882a593Smuzhiyun 	 * Finally we can add each of the reserved backup GDT blocks from
1061*4882a593Smuzhiyun 	 * the new group to its reserved primary GDT block.
1062*4882a593Smuzhiyun 	 */
1063*4882a593Smuzhiyun 	blk = group * EXT4_BLOCKS_PER_GROUP(sb);
1064*4882a593Smuzhiyun 	for (i = 0; i < reserved_gdb; i++) {
1065*4882a593Smuzhiyun 		int err2;
1066*4882a593Smuzhiyun 		data = (__le32 *)primary[i]->b_data;
1067*4882a593Smuzhiyun 		/* printk("reserving backup %lu[%u] = %lu\n",
1068*4882a593Smuzhiyun 		       primary[i]->b_blocknr, gdbackups,
1069*4882a593Smuzhiyun 		       blk + primary[i]->b_blocknr); */
1070*4882a593Smuzhiyun 		data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
1071*4882a593Smuzhiyun 		err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]);
1072*4882a593Smuzhiyun 		if (!err)
1073*4882a593Smuzhiyun 			err = err2;
1074*4882a593Smuzhiyun 	}
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun 	inode->i_blocks += reserved_gdb * sb->s_blocksize >> (9 - cluster_bits);
1077*4882a593Smuzhiyun 	ext4_mark_iloc_dirty(handle, inode, &iloc);
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun exit_bh:
1080*4882a593Smuzhiyun 	while (--res >= 0)
1081*4882a593Smuzhiyun 		brelse(primary[res]);
1082*4882a593Smuzhiyun 	brelse(dind);
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun exit_free:
1085*4882a593Smuzhiyun 	kfree(primary);
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun 	return err;
1088*4882a593Smuzhiyun }
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun /*
1091*4882a593Smuzhiyun  * Update the backup copies of the ext4 metadata.  These don't need to be part
1092*4882a593Smuzhiyun  * of the main resize transaction, because e2fsck will re-write them if there
1093*4882a593Smuzhiyun  * is a problem (basically only OOM will cause a problem).  However, we
1094*4882a593Smuzhiyun  * _should_ update the backups if possible, in case the primary gets trashed
1095*4882a593Smuzhiyun  * for some reason and we need to run e2fsck from a backup superblock.  The
1096*4882a593Smuzhiyun  * important part is that the new block and inode counts are in the backup
1097*4882a593Smuzhiyun  * superblocks, and the location of the new group metadata in the GDT backups.
1098*4882a593Smuzhiyun  *
1099*4882a593Smuzhiyun  * We do not need take the s_resize_lock for this, because these
1100*4882a593Smuzhiyun  * blocks are not otherwise touched by the filesystem code when it is
1101*4882a593Smuzhiyun  * mounted.  We don't need to worry about last changing from
1102*4882a593Smuzhiyun  * sbi->s_groups_count, because the worst that can happen is that we
1103*4882a593Smuzhiyun  * do not copy the full number of backups at this time.  The resize
1104*4882a593Smuzhiyun  * which changed s_groups_count will backup again.
1105*4882a593Smuzhiyun  */
update_backups(struct super_block * sb,sector_t blk_off,char * data,int size,int meta_bg)1106*4882a593Smuzhiyun static void update_backups(struct super_block *sb, sector_t blk_off, char *data,
1107*4882a593Smuzhiyun 			   int size, int meta_bg)
1108*4882a593Smuzhiyun {
1109*4882a593Smuzhiyun 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1110*4882a593Smuzhiyun 	ext4_group_t last;
1111*4882a593Smuzhiyun 	const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
1112*4882a593Smuzhiyun 	unsigned three = 1;
1113*4882a593Smuzhiyun 	unsigned five = 5;
1114*4882a593Smuzhiyun 	unsigned seven = 7;
1115*4882a593Smuzhiyun 	ext4_group_t group = 0;
1116*4882a593Smuzhiyun 	int rest = sb->s_blocksize - size;
1117*4882a593Smuzhiyun 	handle_t *handle;
1118*4882a593Smuzhiyun 	int err = 0, err2;
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun 	handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA);
1121*4882a593Smuzhiyun 	if (IS_ERR(handle)) {
1122*4882a593Smuzhiyun 		group = 1;
1123*4882a593Smuzhiyun 		err = PTR_ERR(handle);
1124*4882a593Smuzhiyun 		goto exit_err;
1125*4882a593Smuzhiyun 	}
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun 	if (meta_bg == 0) {
1128*4882a593Smuzhiyun 		group = ext4_list_backups(sb, &three, &five, &seven);
1129*4882a593Smuzhiyun 		last = sbi->s_groups_count;
1130*4882a593Smuzhiyun 	} else {
1131*4882a593Smuzhiyun 		group = ext4_get_group_number(sb, blk_off) + 1;
1132*4882a593Smuzhiyun 		last = (ext4_group_t)(group + EXT4_DESC_PER_BLOCK(sb) - 2);
1133*4882a593Smuzhiyun 	}
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun 	while (group < sbi->s_groups_count) {
1136*4882a593Smuzhiyun 		struct buffer_head *bh;
1137*4882a593Smuzhiyun 		ext4_fsblk_t backup_block;
1138*4882a593Smuzhiyun 
1139*4882a593Smuzhiyun 		/* Out of journal space, and can't get more - abort - so sad */
1140*4882a593Smuzhiyun 		err = ext4_resize_ensure_credits_batch(handle, 1);
1141*4882a593Smuzhiyun 		if (err < 0)
1142*4882a593Smuzhiyun 			break;
1143*4882a593Smuzhiyun 
1144*4882a593Smuzhiyun 		if (meta_bg == 0)
1145*4882a593Smuzhiyun 			backup_block = ((ext4_fsblk_t)group) * bpg + blk_off;
1146*4882a593Smuzhiyun 		else
1147*4882a593Smuzhiyun 			backup_block = (ext4_group_first_block_no(sb, group) +
1148*4882a593Smuzhiyun 					ext4_bg_has_super(sb, group));
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun 		bh = sb_getblk(sb, backup_block);
1151*4882a593Smuzhiyun 		if (unlikely(!bh)) {
1152*4882a593Smuzhiyun 			err = -ENOMEM;
1153*4882a593Smuzhiyun 			break;
1154*4882a593Smuzhiyun 		}
1155*4882a593Smuzhiyun 		ext4_debug("update metadata backup %llu(+%llu)\n",
1156*4882a593Smuzhiyun 			   backup_block, backup_block -
1157*4882a593Smuzhiyun 			   ext4_group_first_block_no(sb, group));
1158*4882a593Smuzhiyun 		BUFFER_TRACE(bh, "get_write_access");
1159*4882a593Smuzhiyun 		if ((err = ext4_journal_get_write_access(handle, bh))) {
1160*4882a593Smuzhiyun 			brelse(bh);
1161*4882a593Smuzhiyun 			break;
1162*4882a593Smuzhiyun 		}
1163*4882a593Smuzhiyun 		lock_buffer(bh);
1164*4882a593Smuzhiyun 		memcpy(bh->b_data, data, size);
1165*4882a593Smuzhiyun 		if (rest)
1166*4882a593Smuzhiyun 			memset(bh->b_data + size, 0, rest);
1167*4882a593Smuzhiyun 		set_buffer_uptodate(bh);
1168*4882a593Smuzhiyun 		unlock_buffer(bh);
1169*4882a593Smuzhiyun 		err = ext4_handle_dirty_metadata(handle, NULL, bh);
1170*4882a593Smuzhiyun 		if (unlikely(err))
1171*4882a593Smuzhiyun 			ext4_std_error(sb, err);
1172*4882a593Smuzhiyun 		brelse(bh);
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun 		if (meta_bg == 0)
1175*4882a593Smuzhiyun 			group = ext4_list_backups(sb, &three, &five, &seven);
1176*4882a593Smuzhiyun 		else if (group == last)
1177*4882a593Smuzhiyun 			break;
1178*4882a593Smuzhiyun 		else
1179*4882a593Smuzhiyun 			group = last;
1180*4882a593Smuzhiyun 	}
1181*4882a593Smuzhiyun 	if ((err2 = ext4_journal_stop(handle)) && !err)
1182*4882a593Smuzhiyun 		err = err2;
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun 	/*
1185*4882a593Smuzhiyun 	 * Ugh! Need to have e2fsck write the backup copies.  It is too
1186*4882a593Smuzhiyun 	 * late to revert the resize, we shouldn't fail just because of
1187*4882a593Smuzhiyun 	 * the backup copies (they are only needed in case of corruption).
1188*4882a593Smuzhiyun 	 *
1189*4882a593Smuzhiyun 	 * However, if we got here we have a journal problem too, so we
1190*4882a593Smuzhiyun 	 * can't really start a transaction to mark the superblock.
1191*4882a593Smuzhiyun 	 * Chicken out and just set the flag on the hope it will be written
1192*4882a593Smuzhiyun 	 * to disk, and if not - we will simply wait until next fsck.
1193*4882a593Smuzhiyun 	 */
1194*4882a593Smuzhiyun exit_err:
1195*4882a593Smuzhiyun 	if (err) {
1196*4882a593Smuzhiyun 		ext4_warning(sb, "can't update backup for group %u (err %d), "
1197*4882a593Smuzhiyun 			     "forcing fsck on next reboot", group, err);
1198*4882a593Smuzhiyun 		sbi->s_mount_state &= ~EXT4_VALID_FS;
1199*4882a593Smuzhiyun 		sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1200*4882a593Smuzhiyun 		mark_buffer_dirty(sbi->s_sbh);
1201*4882a593Smuzhiyun 	}
1202*4882a593Smuzhiyun }
1203*4882a593Smuzhiyun 
1204*4882a593Smuzhiyun /*
1205*4882a593Smuzhiyun  * ext4_add_new_descs() adds @count group descriptor of groups
1206*4882a593Smuzhiyun  * starting at @group
1207*4882a593Smuzhiyun  *
1208*4882a593Smuzhiyun  * @handle: journal handle
1209*4882a593Smuzhiyun  * @sb: super block
1210*4882a593Smuzhiyun  * @group: the group no. of the first group desc to be added
1211*4882a593Smuzhiyun  * @resize_inode: the resize inode
1212*4882a593Smuzhiyun  * @count: number of group descriptors to be added
1213*4882a593Smuzhiyun  */
ext4_add_new_descs(handle_t * handle,struct super_block * sb,ext4_group_t group,struct inode * resize_inode,ext4_group_t count)1214*4882a593Smuzhiyun static int ext4_add_new_descs(handle_t *handle, struct super_block *sb,
1215*4882a593Smuzhiyun 			      ext4_group_t group, struct inode *resize_inode,
1216*4882a593Smuzhiyun 			      ext4_group_t count)
1217*4882a593Smuzhiyun {
1218*4882a593Smuzhiyun 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1219*4882a593Smuzhiyun 	struct ext4_super_block *es = sbi->s_es;
1220*4882a593Smuzhiyun 	struct buffer_head *gdb_bh;
1221*4882a593Smuzhiyun 	int i, gdb_off, gdb_num, err = 0;
1222*4882a593Smuzhiyun 	int meta_bg;
1223*4882a593Smuzhiyun 
1224*4882a593Smuzhiyun 	meta_bg = ext4_has_feature_meta_bg(sb);
1225*4882a593Smuzhiyun 	for (i = 0; i < count; i++, group++) {
1226*4882a593Smuzhiyun 		int reserved_gdb = ext4_bg_has_super(sb, group) ?
1227*4882a593Smuzhiyun 			le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
1228*4882a593Smuzhiyun 
1229*4882a593Smuzhiyun 		gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
1230*4882a593Smuzhiyun 		gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun 		/*
1233*4882a593Smuzhiyun 		 * We will only either add reserved group blocks to a backup group
1234*4882a593Smuzhiyun 		 * or remove reserved blocks for the first group in a new group block.
1235*4882a593Smuzhiyun 		 * Doing both would be mean more complex code, and sane people don't
1236*4882a593Smuzhiyun 		 * use non-sparse filesystems anymore.  This is already checked above.
1237*4882a593Smuzhiyun 		 */
1238*4882a593Smuzhiyun 		if (gdb_off) {
1239*4882a593Smuzhiyun 			gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc,
1240*4882a593Smuzhiyun 						     gdb_num);
1241*4882a593Smuzhiyun 			BUFFER_TRACE(gdb_bh, "get_write_access");
1242*4882a593Smuzhiyun 			err = ext4_journal_get_write_access(handle, gdb_bh);
1243*4882a593Smuzhiyun 
1244*4882a593Smuzhiyun 			if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group))
1245*4882a593Smuzhiyun 				err = reserve_backup_gdb(handle, resize_inode, group);
1246*4882a593Smuzhiyun 		} else if (meta_bg != 0) {
1247*4882a593Smuzhiyun 			err = add_new_gdb_meta_bg(sb, handle, group);
1248*4882a593Smuzhiyun 		} else {
1249*4882a593Smuzhiyun 			err = add_new_gdb(handle, resize_inode, group);
1250*4882a593Smuzhiyun 		}
1251*4882a593Smuzhiyun 		if (err)
1252*4882a593Smuzhiyun 			break;
1253*4882a593Smuzhiyun 	}
1254*4882a593Smuzhiyun 	return err;
1255*4882a593Smuzhiyun }
1256*4882a593Smuzhiyun 
ext4_get_bitmap(struct super_block * sb,__u64 block)1257*4882a593Smuzhiyun static struct buffer_head *ext4_get_bitmap(struct super_block *sb, __u64 block)
1258*4882a593Smuzhiyun {
1259*4882a593Smuzhiyun 	struct buffer_head *bh = sb_getblk(sb, block);
1260*4882a593Smuzhiyun 	if (unlikely(!bh))
1261*4882a593Smuzhiyun 		return NULL;
1262*4882a593Smuzhiyun 	if (!bh_uptodate_or_lock(bh)) {
1263*4882a593Smuzhiyun 		if (ext4_read_bh(bh, 0, NULL) < 0) {
1264*4882a593Smuzhiyun 			brelse(bh);
1265*4882a593Smuzhiyun 			return NULL;
1266*4882a593Smuzhiyun 		}
1267*4882a593Smuzhiyun 	}
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 	return bh;
1270*4882a593Smuzhiyun }
1271*4882a593Smuzhiyun 
ext4_set_bitmap_checksums(struct super_block * sb,ext4_group_t group,struct ext4_group_desc * gdp,struct ext4_new_group_data * group_data)1272*4882a593Smuzhiyun static int ext4_set_bitmap_checksums(struct super_block *sb,
1273*4882a593Smuzhiyun 				     ext4_group_t group,
1274*4882a593Smuzhiyun 				     struct ext4_group_desc *gdp,
1275*4882a593Smuzhiyun 				     struct ext4_new_group_data *group_data)
1276*4882a593Smuzhiyun {
1277*4882a593Smuzhiyun 	struct buffer_head *bh;
1278*4882a593Smuzhiyun 
1279*4882a593Smuzhiyun 	if (!ext4_has_metadata_csum(sb))
1280*4882a593Smuzhiyun 		return 0;
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun 	bh = ext4_get_bitmap(sb, group_data->inode_bitmap);
1283*4882a593Smuzhiyun 	if (!bh)
1284*4882a593Smuzhiyun 		return -EIO;
1285*4882a593Smuzhiyun 	ext4_inode_bitmap_csum_set(sb, group, gdp, bh,
1286*4882a593Smuzhiyun 				   EXT4_INODES_PER_GROUP(sb) / 8);
1287*4882a593Smuzhiyun 	brelse(bh);
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun 	bh = ext4_get_bitmap(sb, group_data->block_bitmap);
1290*4882a593Smuzhiyun 	if (!bh)
1291*4882a593Smuzhiyun 		return -EIO;
1292*4882a593Smuzhiyun 	ext4_block_bitmap_csum_set(sb, group, gdp, bh);
1293*4882a593Smuzhiyun 	brelse(bh);
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun 	return 0;
1296*4882a593Smuzhiyun }
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun /*
1299*4882a593Smuzhiyun  * ext4_setup_new_descs() will set up the group descriptor descriptors of a flex bg
1300*4882a593Smuzhiyun  */
ext4_setup_new_descs(handle_t * handle,struct super_block * sb,struct ext4_new_flex_group_data * flex_gd)1301*4882a593Smuzhiyun static int ext4_setup_new_descs(handle_t *handle, struct super_block *sb,
1302*4882a593Smuzhiyun 				struct ext4_new_flex_group_data *flex_gd)
1303*4882a593Smuzhiyun {
1304*4882a593Smuzhiyun 	struct ext4_new_group_data	*group_data = flex_gd->groups;
1305*4882a593Smuzhiyun 	struct ext4_group_desc		*gdp;
1306*4882a593Smuzhiyun 	struct ext4_sb_info		*sbi = EXT4_SB(sb);
1307*4882a593Smuzhiyun 	struct buffer_head		*gdb_bh;
1308*4882a593Smuzhiyun 	ext4_group_t			group;
1309*4882a593Smuzhiyun 	__u16				*bg_flags = flex_gd->bg_flags;
1310*4882a593Smuzhiyun 	int				i, gdb_off, gdb_num, err = 0;
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun 
1313*4882a593Smuzhiyun 	for (i = 0; i < flex_gd->count; i++, group_data++, bg_flags++) {
1314*4882a593Smuzhiyun 		group = group_data->group;
1315*4882a593Smuzhiyun 
1316*4882a593Smuzhiyun 		gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
1317*4882a593Smuzhiyun 		gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun 		/*
1320*4882a593Smuzhiyun 		 * get_write_access() has been called on gdb_bh by ext4_add_new_desc().
1321*4882a593Smuzhiyun 		 */
1322*4882a593Smuzhiyun 		gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc, gdb_num);
1323*4882a593Smuzhiyun 		/* Update group descriptor block for new group */
1324*4882a593Smuzhiyun 		gdp = (struct ext4_group_desc *)(gdb_bh->b_data +
1325*4882a593Smuzhiyun 						 gdb_off * EXT4_DESC_SIZE(sb));
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun 		memset(gdp, 0, EXT4_DESC_SIZE(sb));
1328*4882a593Smuzhiyun 		ext4_block_bitmap_set(sb, gdp, group_data->block_bitmap);
1329*4882a593Smuzhiyun 		ext4_inode_bitmap_set(sb, gdp, group_data->inode_bitmap);
1330*4882a593Smuzhiyun 		err = ext4_set_bitmap_checksums(sb, group, gdp, group_data);
1331*4882a593Smuzhiyun 		if (err) {
1332*4882a593Smuzhiyun 			ext4_std_error(sb, err);
1333*4882a593Smuzhiyun 			break;
1334*4882a593Smuzhiyun 		}
1335*4882a593Smuzhiyun 
1336*4882a593Smuzhiyun 		ext4_inode_table_set(sb, gdp, group_data->inode_table);
1337*4882a593Smuzhiyun 		ext4_free_group_clusters_set(sb, gdp,
1338*4882a593Smuzhiyun 					     group_data->free_clusters_count);
1339*4882a593Smuzhiyun 		ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
1340*4882a593Smuzhiyun 		if (ext4_has_group_desc_csum(sb))
1341*4882a593Smuzhiyun 			ext4_itable_unused_set(sb, gdp,
1342*4882a593Smuzhiyun 					       EXT4_INODES_PER_GROUP(sb));
1343*4882a593Smuzhiyun 		gdp->bg_flags = cpu_to_le16(*bg_flags);
1344*4882a593Smuzhiyun 		ext4_group_desc_csum_set(sb, group, gdp);
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun 		err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
1347*4882a593Smuzhiyun 		if (unlikely(err)) {
1348*4882a593Smuzhiyun 			ext4_std_error(sb, err);
1349*4882a593Smuzhiyun 			break;
1350*4882a593Smuzhiyun 		}
1351*4882a593Smuzhiyun 
1352*4882a593Smuzhiyun 		/*
1353*4882a593Smuzhiyun 		 * We can allocate memory for mb_alloc based on the new group
1354*4882a593Smuzhiyun 		 * descriptor
1355*4882a593Smuzhiyun 		 */
1356*4882a593Smuzhiyun 		err = ext4_mb_add_groupinfo(sb, group, gdp);
1357*4882a593Smuzhiyun 		if (err)
1358*4882a593Smuzhiyun 			break;
1359*4882a593Smuzhiyun 	}
1360*4882a593Smuzhiyun 	return err;
1361*4882a593Smuzhiyun }
1362*4882a593Smuzhiyun 
1363*4882a593Smuzhiyun /*
1364*4882a593Smuzhiyun  * ext4_update_super() updates the super block so that the newly added
1365*4882a593Smuzhiyun  * groups can be seen by the filesystem.
1366*4882a593Smuzhiyun  *
1367*4882a593Smuzhiyun  * @sb: super block
1368*4882a593Smuzhiyun  * @flex_gd: new added groups
1369*4882a593Smuzhiyun  */
ext4_update_super(struct super_block * sb,struct ext4_new_flex_group_data * flex_gd)1370*4882a593Smuzhiyun static void ext4_update_super(struct super_block *sb,
1371*4882a593Smuzhiyun 			     struct ext4_new_flex_group_data *flex_gd)
1372*4882a593Smuzhiyun {
1373*4882a593Smuzhiyun 	ext4_fsblk_t blocks_count = 0;
1374*4882a593Smuzhiyun 	ext4_fsblk_t free_blocks = 0;
1375*4882a593Smuzhiyun 	ext4_fsblk_t reserved_blocks = 0;
1376*4882a593Smuzhiyun 	struct ext4_new_group_data *group_data = flex_gd->groups;
1377*4882a593Smuzhiyun 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1378*4882a593Smuzhiyun 	struct ext4_super_block *es = sbi->s_es;
1379*4882a593Smuzhiyun 	int i;
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun 	BUG_ON(flex_gd->count == 0 || group_data == NULL);
1382*4882a593Smuzhiyun 	/*
1383*4882a593Smuzhiyun 	 * Make the new blocks and inodes valid next.  We do this before
1384*4882a593Smuzhiyun 	 * increasing the group count so that once the group is enabled,
1385*4882a593Smuzhiyun 	 * all of its blocks and inodes are already valid.
1386*4882a593Smuzhiyun 	 *
1387*4882a593Smuzhiyun 	 * We always allocate group-by-group, then block-by-block or
1388*4882a593Smuzhiyun 	 * inode-by-inode within a group, so enabling these
1389*4882a593Smuzhiyun 	 * blocks/inodes before the group is live won't actually let us
1390*4882a593Smuzhiyun 	 * allocate the new space yet.
1391*4882a593Smuzhiyun 	 */
1392*4882a593Smuzhiyun 	for (i = 0; i < flex_gd->count; i++) {
1393*4882a593Smuzhiyun 		blocks_count += group_data[i].blocks_count;
1394*4882a593Smuzhiyun 		free_blocks += EXT4_C2B(sbi, group_data[i].free_clusters_count);
1395*4882a593Smuzhiyun 	}
1396*4882a593Smuzhiyun 
1397*4882a593Smuzhiyun 	reserved_blocks = ext4_r_blocks_count(es) * 100;
1398*4882a593Smuzhiyun 	reserved_blocks = div64_u64(reserved_blocks, ext4_blocks_count(es));
1399*4882a593Smuzhiyun 	reserved_blocks *= blocks_count;
1400*4882a593Smuzhiyun 	do_div(reserved_blocks, 100);
1401*4882a593Smuzhiyun 
1402*4882a593Smuzhiyun 	ext4_blocks_count_set(es, ext4_blocks_count(es) + blocks_count);
1403*4882a593Smuzhiyun 	ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + free_blocks);
1404*4882a593Smuzhiyun 	le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb) *
1405*4882a593Smuzhiyun 		     flex_gd->count);
1406*4882a593Smuzhiyun 	le32_add_cpu(&es->s_free_inodes_count, EXT4_INODES_PER_GROUP(sb) *
1407*4882a593Smuzhiyun 		     flex_gd->count);
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun 	ext4_debug("free blocks count %llu", ext4_free_blocks_count(es));
1410*4882a593Smuzhiyun 	/*
1411*4882a593Smuzhiyun 	 * We need to protect s_groups_count against other CPUs seeing
1412*4882a593Smuzhiyun 	 * inconsistent state in the superblock.
1413*4882a593Smuzhiyun 	 *
1414*4882a593Smuzhiyun 	 * The precise rules we use are:
1415*4882a593Smuzhiyun 	 *
1416*4882a593Smuzhiyun 	 * * Writers must perform a smp_wmb() after updating all
1417*4882a593Smuzhiyun 	 *   dependent data and before modifying the groups count
1418*4882a593Smuzhiyun 	 *
1419*4882a593Smuzhiyun 	 * * Readers must perform an smp_rmb() after reading the groups
1420*4882a593Smuzhiyun 	 *   count and before reading any dependent data.
1421*4882a593Smuzhiyun 	 *
1422*4882a593Smuzhiyun 	 * NB. These rules can be relaxed when checking the group count
1423*4882a593Smuzhiyun 	 * while freeing data, as we can only allocate from a block
1424*4882a593Smuzhiyun 	 * group after serialising against the group count, and we can
1425*4882a593Smuzhiyun 	 * only then free after serialising in turn against that
1426*4882a593Smuzhiyun 	 * allocation.
1427*4882a593Smuzhiyun 	 */
1428*4882a593Smuzhiyun 	smp_wmb();
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 	/* Update the global fs size fields */
1431*4882a593Smuzhiyun 	sbi->s_groups_count += flex_gd->count;
1432*4882a593Smuzhiyun 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
1433*4882a593Smuzhiyun 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
1434*4882a593Smuzhiyun 
1435*4882a593Smuzhiyun 	/* Update the reserved block counts only once the new group is
1436*4882a593Smuzhiyun 	 * active. */
1437*4882a593Smuzhiyun 	ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
1438*4882a593Smuzhiyun 				reserved_blocks);
1439*4882a593Smuzhiyun 
1440*4882a593Smuzhiyun 	/* Update the free space counts */
1441*4882a593Smuzhiyun 	percpu_counter_add(&sbi->s_freeclusters_counter,
1442*4882a593Smuzhiyun 			   EXT4_NUM_B2C(sbi, free_blocks));
1443*4882a593Smuzhiyun 	percpu_counter_add(&sbi->s_freeinodes_counter,
1444*4882a593Smuzhiyun 			   EXT4_INODES_PER_GROUP(sb) * flex_gd->count);
1445*4882a593Smuzhiyun 
1446*4882a593Smuzhiyun 	ext4_debug("free blocks count %llu",
1447*4882a593Smuzhiyun 		   percpu_counter_read(&sbi->s_freeclusters_counter));
1448*4882a593Smuzhiyun 	if (ext4_has_feature_flex_bg(sb) && sbi->s_log_groups_per_flex) {
1449*4882a593Smuzhiyun 		ext4_group_t flex_group;
1450*4882a593Smuzhiyun 		struct flex_groups *fg;
1451*4882a593Smuzhiyun 
1452*4882a593Smuzhiyun 		flex_group = ext4_flex_group(sbi, group_data[0].group);
1453*4882a593Smuzhiyun 		fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
1454*4882a593Smuzhiyun 		atomic64_add(EXT4_NUM_B2C(sbi, free_blocks),
1455*4882a593Smuzhiyun 			     &fg->free_clusters);
1456*4882a593Smuzhiyun 		atomic_add(EXT4_INODES_PER_GROUP(sb) * flex_gd->count,
1457*4882a593Smuzhiyun 			   &fg->free_inodes);
1458*4882a593Smuzhiyun 	}
1459*4882a593Smuzhiyun 
1460*4882a593Smuzhiyun 	/*
1461*4882a593Smuzhiyun 	 * Update the fs overhead information
1462*4882a593Smuzhiyun 	 */
1463*4882a593Smuzhiyun 	ext4_calculate_overhead(sb);
1464*4882a593Smuzhiyun 	es->s_overhead_clusters = cpu_to_le32(sbi->s_overhead);
1465*4882a593Smuzhiyun 
1466*4882a593Smuzhiyun 	if (test_opt(sb, DEBUG))
1467*4882a593Smuzhiyun 		printk(KERN_DEBUG "EXT4-fs: added group %u:"
1468*4882a593Smuzhiyun 		       "%llu blocks(%llu free %llu reserved)\n", flex_gd->count,
1469*4882a593Smuzhiyun 		       blocks_count, free_blocks, reserved_blocks);
1470*4882a593Smuzhiyun }
1471*4882a593Smuzhiyun 
1472*4882a593Smuzhiyun /* Add a flex group to an fs. Ensure we handle all possible error conditions
1473*4882a593Smuzhiyun  * _before_ we start modifying the filesystem, because we cannot abort the
1474*4882a593Smuzhiyun  * transaction and not have it write the data to disk.
1475*4882a593Smuzhiyun  */
ext4_flex_group_add(struct super_block * sb,struct inode * resize_inode,struct ext4_new_flex_group_data * flex_gd)1476*4882a593Smuzhiyun static int ext4_flex_group_add(struct super_block *sb,
1477*4882a593Smuzhiyun 			       struct inode *resize_inode,
1478*4882a593Smuzhiyun 			       struct ext4_new_flex_group_data *flex_gd)
1479*4882a593Smuzhiyun {
1480*4882a593Smuzhiyun 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1481*4882a593Smuzhiyun 	struct ext4_super_block *es = sbi->s_es;
1482*4882a593Smuzhiyun 	ext4_fsblk_t o_blocks_count;
1483*4882a593Smuzhiyun 	ext4_grpblk_t last;
1484*4882a593Smuzhiyun 	ext4_group_t group;
1485*4882a593Smuzhiyun 	handle_t *handle;
1486*4882a593Smuzhiyun 	unsigned reserved_gdb;
1487*4882a593Smuzhiyun 	int err = 0, err2 = 0, credit;
1488*4882a593Smuzhiyun 
1489*4882a593Smuzhiyun 	BUG_ON(!flex_gd->count || !flex_gd->groups || !flex_gd->bg_flags);
1490*4882a593Smuzhiyun 
1491*4882a593Smuzhiyun 	reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks);
1492*4882a593Smuzhiyun 	o_blocks_count = ext4_blocks_count(es);
1493*4882a593Smuzhiyun 	ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1494*4882a593Smuzhiyun 	BUG_ON(last);
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun 	err = setup_new_flex_group_blocks(sb, flex_gd);
1497*4882a593Smuzhiyun 	if (err)
1498*4882a593Smuzhiyun 		goto exit;
1499*4882a593Smuzhiyun 	/*
1500*4882a593Smuzhiyun 	 * We will always be modifying at least the superblock and  GDT
1501*4882a593Smuzhiyun 	 * blocks.  If we are adding a group past the last current GDT block,
1502*4882a593Smuzhiyun 	 * we will also modify the inode and the dindirect block.  If we
1503*4882a593Smuzhiyun 	 * are adding a group with superblock/GDT backups  we will also
1504*4882a593Smuzhiyun 	 * modify each of the reserved GDT dindirect blocks.
1505*4882a593Smuzhiyun 	 */
1506*4882a593Smuzhiyun 	credit = 3;	/* sb, resize inode, resize inode dindirect */
1507*4882a593Smuzhiyun 	/* GDT blocks */
1508*4882a593Smuzhiyun 	credit += 1 + DIV_ROUND_UP(flex_gd->count, EXT4_DESC_PER_BLOCK(sb));
1509*4882a593Smuzhiyun 	credit += reserved_gdb;	/* Reserved GDT dindirect blocks */
1510*4882a593Smuzhiyun 	handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credit);
1511*4882a593Smuzhiyun 	if (IS_ERR(handle)) {
1512*4882a593Smuzhiyun 		err = PTR_ERR(handle);
1513*4882a593Smuzhiyun 		goto exit;
1514*4882a593Smuzhiyun 	}
1515*4882a593Smuzhiyun 
1516*4882a593Smuzhiyun 	BUFFER_TRACE(sbi->s_sbh, "get_write_access");
1517*4882a593Smuzhiyun 	err = ext4_journal_get_write_access(handle, sbi->s_sbh);
1518*4882a593Smuzhiyun 	if (err)
1519*4882a593Smuzhiyun 		goto exit_journal;
1520*4882a593Smuzhiyun 
1521*4882a593Smuzhiyun 	group = flex_gd->groups[0].group;
1522*4882a593Smuzhiyun 	BUG_ON(group != sbi->s_groups_count);
1523*4882a593Smuzhiyun 	err = ext4_add_new_descs(handle, sb, group,
1524*4882a593Smuzhiyun 				resize_inode, flex_gd->count);
1525*4882a593Smuzhiyun 	if (err)
1526*4882a593Smuzhiyun 		goto exit_journal;
1527*4882a593Smuzhiyun 
1528*4882a593Smuzhiyun 	err = ext4_setup_new_descs(handle, sb, flex_gd);
1529*4882a593Smuzhiyun 	if (err)
1530*4882a593Smuzhiyun 		goto exit_journal;
1531*4882a593Smuzhiyun 
1532*4882a593Smuzhiyun 	ext4_update_super(sb, flex_gd);
1533*4882a593Smuzhiyun 
1534*4882a593Smuzhiyun 	err = ext4_handle_dirty_super(handle, sb);
1535*4882a593Smuzhiyun 
1536*4882a593Smuzhiyun exit_journal:
1537*4882a593Smuzhiyun 	err2 = ext4_journal_stop(handle);
1538*4882a593Smuzhiyun 	if (!err)
1539*4882a593Smuzhiyun 		err = err2;
1540*4882a593Smuzhiyun 
1541*4882a593Smuzhiyun 	if (!err) {
1542*4882a593Smuzhiyun 		int gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
1543*4882a593Smuzhiyun 		int gdb_num_end = ((group + flex_gd->count - 1) /
1544*4882a593Smuzhiyun 				   EXT4_DESC_PER_BLOCK(sb));
1545*4882a593Smuzhiyun 		int meta_bg = ext4_has_feature_meta_bg(sb);
1546*4882a593Smuzhiyun 		sector_t old_gdb = 0;
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun 		update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
1549*4882a593Smuzhiyun 			       sizeof(struct ext4_super_block), 0);
1550*4882a593Smuzhiyun 		for (; gdb_num <= gdb_num_end; gdb_num++) {
1551*4882a593Smuzhiyun 			struct buffer_head *gdb_bh;
1552*4882a593Smuzhiyun 
1553*4882a593Smuzhiyun 			gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc,
1554*4882a593Smuzhiyun 						     gdb_num);
1555*4882a593Smuzhiyun 			if (old_gdb == gdb_bh->b_blocknr)
1556*4882a593Smuzhiyun 				continue;
1557*4882a593Smuzhiyun 			update_backups(sb, gdb_bh->b_blocknr, gdb_bh->b_data,
1558*4882a593Smuzhiyun 				       gdb_bh->b_size, meta_bg);
1559*4882a593Smuzhiyun 			old_gdb = gdb_bh->b_blocknr;
1560*4882a593Smuzhiyun 		}
1561*4882a593Smuzhiyun 	}
1562*4882a593Smuzhiyun exit:
1563*4882a593Smuzhiyun 	return err;
1564*4882a593Smuzhiyun }
1565*4882a593Smuzhiyun 
ext4_setup_next_flex_gd(struct super_block * sb,struct ext4_new_flex_group_data * flex_gd,ext4_fsblk_t n_blocks_count,unsigned long flexbg_size)1566*4882a593Smuzhiyun static int ext4_setup_next_flex_gd(struct super_block *sb,
1567*4882a593Smuzhiyun 				    struct ext4_new_flex_group_data *flex_gd,
1568*4882a593Smuzhiyun 				    ext4_fsblk_t n_blocks_count,
1569*4882a593Smuzhiyun 				    unsigned long flexbg_size)
1570*4882a593Smuzhiyun {
1571*4882a593Smuzhiyun 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1572*4882a593Smuzhiyun 	struct ext4_super_block *es = sbi->s_es;
1573*4882a593Smuzhiyun 	struct ext4_new_group_data *group_data = flex_gd->groups;
1574*4882a593Smuzhiyun 	ext4_fsblk_t o_blocks_count;
1575*4882a593Smuzhiyun 	ext4_group_t n_group;
1576*4882a593Smuzhiyun 	ext4_group_t group;
1577*4882a593Smuzhiyun 	ext4_group_t last_group;
1578*4882a593Smuzhiyun 	ext4_grpblk_t last;
1579*4882a593Smuzhiyun 	ext4_grpblk_t clusters_per_group;
1580*4882a593Smuzhiyun 	unsigned long i;
1581*4882a593Smuzhiyun 
1582*4882a593Smuzhiyun 	clusters_per_group = EXT4_CLUSTERS_PER_GROUP(sb);
1583*4882a593Smuzhiyun 
1584*4882a593Smuzhiyun 	o_blocks_count = ext4_blocks_count(es);
1585*4882a593Smuzhiyun 
1586*4882a593Smuzhiyun 	if (o_blocks_count == n_blocks_count)
1587*4882a593Smuzhiyun 		return 0;
1588*4882a593Smuzhiyun 
1589*4882a593Smuzhiyun 	ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1590*4882a593Smuzhiyun 	BUG_ON(last);
1591*4882a593Smuzhiyun 	ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &last);
1592*4882a593Smuzhiyun 
1593*4882a593Smuzhiyun 	last_group = group | (flexbg_size - 1);
1594*4882a593Smuzhiyun 	if (last_group > n_group)
1595*4882a593Smuzhiyun 		last_group = n_group;
1596*4882a593Smuzhiyun 
1597*4882a593Smuzhiyun 	flex_gd->count = last_group - group + 1;
1598*4882a593Smuzhiyun 
1599*4882a593Smuzhiyun 	for (i = 0; i < flex_gd->count; i++) {
1600*4882a593Smuzhiyun 		int overhead;
1601*4882a593Smuzhiyun 
1602*4882a593Smuzhiyun 		group_data[i].group = group + i;
1603*4882a593Smuzhiyun 		group_data[i].blocks_count = EXT4_BLOCKS_PER_GROUP(sb);
1604*4882a593Smuzhiyun 		overhead = ext4_group_overhead_blocks(sb, group + i);
1605*4882a593Smuzhiyun 		group_data[i].mdata_blocks = overhead;
1606*4882a593Smuzhiyun 		group_data[i].free_clusters_count = EXT4_CLUSTERS_PER_GROUP(sb);
1607*4882a593Smuzhiyun 		if (ext4_has_group_desc_csum(sb)) {
1608*4882a593Smuzhiyun 			flex_gd->bg_flags[i] = EXT4_BG_BLOCK_UNINIT |
1609*4882a593Smuzhiyun 					       EXT4_BG_INODE_UNINIT;
1610*4882a593Smuzhiyun 			if (!test_opt(sb, INIT_INODE_TABLE))
1611*4882a593Smuzhiyun 				flex_gd->bg_flags[i] |= EXT4_BG_INODE_ZEROED;
1612*4882a593Smuzhiyun 		} else
1613*4882a593Smuzhiyun 			flex_gd->bg_flags[i] = EXT4_BG_INODE_ZEROED;
1614*4882a593Smuzhiyun 	}
1615*4882a593Smuzhiyun 
1616*4882a593Smuzhiyun 	if (last_group == n_group && ext4_has_group_desc_csum(sb))
1617*4882a593Smuzhiyun 		/* We need to initialize block bitmap of last group. */
1618*4882a593Smuzhiyun 		flex_gd->bg_flags[i - 1] &= ~EXT4_BG_BLOCK_UNINIT;
1619*4882a593Smuzhiyun 
1620*4882a593Smuzhiyun 	if ((last_group == n_group) && (last != clusters_per_group - 1)) {
1621*4882a593Smuzhiyun 		group_data[i - 1].blocks_count = EXT4_C2B(sbi, last + 1);
1622*4882a593Smuzhiyun 		group_data[i - 1].free_clusters_count -= clusters_per_group -
1623*4882a593Smuzhiyun 						       last - 1;
1624*4882a593Smuzhiyun 	}
1625*4882a593Smuzhiyun 
1626*4882a593Smuzhiyun 	return 1;
1627*4882a593Smuzhiyun }
1628*4882a593Smuzhiyun 
1629*4882a593Smuzhiyun /* Add group descriptor data to an existing or new group descriptor block.
1630*4882a593Smuzhiyun  * Ensure we handle all possible error conditions _before_ we start modifying
1631*4882a593Smuzhiyun  * the filesystem, because we cannot abort the transaction and not have it
1632*4882a593Smuzhiyun  * write the data to disk.
1633*4882a593Smuzhiyun  *
1634*4882a593Smuzhiyun  * If we are on a GDT block boundary, we need to get the reserved GDT block.
1635*4882a593Smuzhiyun  * Otherwise, we may need to add backup GDT blocks for a sparse group.
1636*4882a593Smuzhiyun  *
1637*4882a593Smuzhiyun  * We only need to hold the superblock lock while we are actually adding
1638*4882a593Smuzhiyun  * in the new group's counts to the superblock.  Prior to that we have
1639*4882a593Smuzhiyun  * not really "added" the group at all.  We re-check that we are still
1640*4882a593Smuzhiyun  * adding in the last group in case things have changed since verifying.
1641*4882a593Smuzhiyun  */
ext4_group_add(struct super_block * sb,struct ext4_new_group_data * input)1642*4882a593Smuzhiyun int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
1643*4882a593Smuzhiyun {
1644*4882a593Smuzhiyun 	struct ext4_new_flex_group_data flex_gd;
1645*4882a593Smuzhiyun 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1646*4882a593Smuzhiyun 	struct ext4_super_block *es = sbi->s_es;
1647*4882a593Smuzhiyun 	int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
1648*4882a593Smuzhiyun 		le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
1649*4882a593Smuzhiyun 	struct inode *inode = NULL;
1650*4882a593Smuzhiyun 	int gdb_off;
1651*4882a593Smuzhiyun 	int err;
1652*4882a593Smuzhiyun 	__u16 bg_flags = 0;
1653*4882a593Smuzhiyun 
1654*4882a593Smuzhiyun 	gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
1655*4882a593Smuzhiyun 
1656*4882a593Smuzhiyun 	if (gdb_off == 0 && !ext4_has_feature_sparse_super(sb)) {
1657*4882a593Smuzhiyun 		ext4_warning(sb, "Can't resize non-sparse filesystem further");
1658*4882a593Smuzhiyun 		return -EPERM;
1659*4882a593Smuzhiyun 	}
1660*4882a593Smuzhiyun 
1661*4882a593Smuzhiyun 	if (ext4_blocks_count(es) + input->blocks_count <
1662*4882a593Smuzhiyun 	    ext4_blocks_count(es)) {
1663*4882a593Smuzhiyun 		ext4_warning(sb, "blocks_count overflow");
1664*4882a593Smuzhiyun 		return -EINVAL;
1665*4882a593Smuzhiyun 	}
1666*4882a593Smuzhiyun 
1667*4882a593Smuzhiyun 	if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
1668*4882a593Smuzhiyun 	    le32_to_cpu(es->s_inodes_count)) {
1669*4882a593Smuzhiyun 		ext4_warning(sb, "inodes_count overflow");
1670*4882a593Smuzhiyun 		return -EINVAL;
1671*4882a593Smuzhiyun 	}
1672*4882a593Smuzhiyun 
1673*4882a593Smuzhiyun 	if (reserved_gdb || gdb_off == 0) {
1674*4882a593Smuzhiyun 		if (!ext4_has_feature_resize_inode(sb) ||
1675*4882a593Smuzhiyun 		    !le16_to_cpu(es->s_reserved_gdt_blocks)) {
1676*4882a593Smuzhiyun 			ext4_warning(sb,
1677*4882a593Smuzhiyun 				     "No reserved GDT blocks, can't resize");
1678*4882a593Smuzhiyun 			return -EPERM;
1679*4882a593Smuzhiyun 		}
1680*4882a593Smuzhiyun 		inode = ext4_iget(sb, EXT4_RESIZE_INO, EXT4_IGET_SPECIAL);
1681*4882a593Smuzhiyun 		if (IS_ERR(inode)) {
1682*4882a593Smuzhiyun 			ext4_warning(sb, "Error opening resize inode");
1683*4882a593Smuzhiyun 			return PTR_ERR(inode);
1684*4882a593Smuzhiyun 		}
1685*4882a593Smuzhiyun 	}
1686*4882a593Smuzhiyun 
1687*4882a593Smuzhiyun 
1688*4882a593Smuzhiyun 	err = verify_group_input(sb, input);
1689*4882a593Smuzhiyun 	if (err)
1690*4882a593Smuzhiyun 		goto out;
1691*4882a593Smuzhiyun 
1692*4882a593Smuzhiyun 	err = ext4_alloc_flex_bg_array(sb, input->group + 1);
1693*4882a593Smuzhiyun 	if (err)
1694*4882a593Smuzhiyun 		goto out;
1695*4882a593Smuzhiyun 
1696*4882a593Smuzhiyun 	err = ext4_mb_alloc_groupinfo(sb, input->group + 1);
1697*4882a593Smuzhiyun 	if (err)
1698*4882a593Smuzhiyun 		goto out;
1699*4882a593Smuzhiyun 
1700*4882a593Smuzhiyun 	flex_gd.count = 1;
1701*4882a593Smuzhiyun 	flex_gd.groups = input;
1702*4882a593Smuzhiyun 	flex_gd.bg_flags = &bg_flags;
1703*4882a593Smuzhiyun 	err = ext4_flex_group_add(sb, inode, &flex_gd);
1704*4882a593Smuzhiyun out:
1705*4882a593Smuzhiyun 	iput(inode);
1706*4882a593Smuzhiyun 	return err;
1707*4882a593Smuzhiyun } /* ext4_group_add */
1708*4882a593Smuzhiyun 
1709*4882a593Smuzhiyun /*
1710*4882a593Smuzhiyun  * extend a group without checking assuming that checking has been done.
1711*4882a593Smuzhiyun  */
ext4_group_extend_no_check(struct super_block * sb,ext4_fsblk_t o_blocks_count,ext4_grpblk_t add)1712*4882a593Smuzhiyun static int ext4_group_extend_no_check(struct super_block *sb,
1713*4882a593Smuzhiyun 				      ext4_fsblk_t o_blocks_count, ext4_grpblk_t add)
1714*4882a593Smuzhiyun {
1715*4882a593Smuzhiyun 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
1716*4882a593Smuzhiyun 	handle_t *handle;
1717*4882a593Smuzhiyun 	int err = 0, err2;
1718*4882a593Smuzhiyun 
1719*4882a593Smuzhiyun 	/* We will update the superblock, one block bitmap, and
1720*4882a593Smuzhiyun 	 * one group descriptor via ext4_group_add_blocks().
1721*4882a593Smuzhiyun 	 */
1722*4882a593Smuzhiyun 	handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, 3);
1723*4882a593Smuzhiyun 	if (IS_ERR(handle)) {
1724*4882a593Smuzhiyun 		err = PTR_ERR(handle);
1725*4882a593Smuzhiyun 		ext4_warning(sb, "error %d on journal start", err);
1726*4882a593Smuzhiyun 		return err;
1727*4882a593Smuzhiyun 	}
1728*4882a593Smuzhiyun 
1729*4882a593Smuzhiyun 	BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
1730*4882a593Smuzhiyun 	err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
1731*4882a593Smuzhiyun 	if (err) {
1732*4882a593Smuzhiyun 		ext4_warning(sb, "error %d on journal write access", err);
1733*4882a593Smuzhiyun 		goto errout;
1734*4882a593Smuzhiyun 	}
1735*4882a593Smuzhiyun 
1736*4882a593Smuzhiyun 	ext4_blocks_count_set(es, o_blocks_count + add);
1737*4882a593Smuzhiyun 	ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + add);
1738*4882a593Smuzhiyun 	ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
1739*4882a593Smuzhiyun 		   o_blocks_count + add);
1740*4882a593Smuzhiyun 	/* We add the blocks to the bitmap and set the group need init bit */
1741*4882a593Smuzhiyun 	err = ext4_group_add_blocks(handle, sb, o_blocks_count, add);
1742*4882a593Smuzhiyun 	if (err)
1743*4882a593Smuzhiyun 		goto errout;
1744*4882a593Smuzhiyun 	ext4_handle_dirty_super(handle, sb);
1745*4882a593Smuzhiyun 	ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
1746*4882a593Smuzhiyun 		   o_blocks_count + add);
1747*4882a593Smuzhiyun errout:
1748*4882a593Smuzhiyun 	err2 = ext4_journal_stop(handle);
1749*4882a593Smuzhiyun 	if (err2 && !err)
1750*4882a593Smuzhiyun 		err = err2;
1751*4882a593Smuzhiyun 
1752*4882a593Smuzhiyun 	if (!err) {
1753*4882a593Smuzhiyun 		if (test_opt(sb, DEBUG))
1754*4882a593Smuzhiyun 			printk(KERN_DEBUG "EXT4-fs: extended group to %llu "
1755*4882a593Smuzhiyun 			       "blocks\n", ext4_blocks_count(es));
1756*4882a593Smuzhiyun 		update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr,
1757*4882a593Smuzhiyun 			       (char *)es, sizeof(struct ext4_super_block), 0);
1758*4882a593Smuzhiyun 	}
1759*4882a593Smuzhiyun 	return err;
1760*4882a593Smuzhiyun }
1761*4882a593Smuzhiyun 
1762*4882a593Smuzhiyun /*
1763*4882a593Smuzhiyun  * Extend the filesystem to the new number of blocks specified.  This entry
1764*4882a593Smuzhiyun  * point is only used to extend the current filesystem to the end of the last
1765*4882a593Smuzhiyun  * existing group.  It can be accessed via ioctl, or by "remount,resize=<size>"
1766*4882a593Smuzhiyun  * for emergencies (because it has no dependencies on reserved blocks).
1767*4882a593Smuzhiyun  *
1768*4882a593Smuzhiyun  * If we _really_ wanted, we could use default values to call ext4_group_add()
1769*4882a593Smuzhiyun  * allow the "remount" trick to work for arbitrary resizing, assuming enough
1770*4882a593Smuzhiyun  * GDT blocks are reserved to grow to the desired size.
1771*4882a593Smuzhiyun  */
ext4_group_extend(struct super_block * sb,struct ext4_super_block * es,ext4_fsblk_t n_blocks_count)1772*4882a593Smuzhiyun int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
1773*4882a593Smuzhiyun 		      ext4_fsblk_t n_blocks_count)
1774*4882a593Smuzhiyun {
1775*4882a593Smuzhiyun 	ext4_fsblk_t o_blocks_count;
1776*4882a593Smuzhiyun 	ext4_grpblk_t last;
1777*4882a593Smuzhiyun 	ext4_grpblk_t add;
1778*4882a593Smuzhiyun 	struct buffer_head *bh;
1779*4882a593Smuzhiyun 	int err;
1780*4882a593Smuzhiyun 	ext4_group_t group;
1781*4882a593Smuzhiyun 
1782*4882a593Smuzhiyun 	o_blocks_count = ext4_blocks_count(es);
1783*4882a593Smuzhiyun 
1784*4882a593Smuzhiyun 	if (test_opt(sb, DEBUG))
1785*4882a593Smuzhiyun 		ext4_msg(sb, KERN_DEBUG,
1786*4882a593Smuzhiyun 			 "extending last group from %llu to %llu blocks",
1787*4882a593Smuzhiyun 			 o_blocks_count, n_blocks_count);
1788*4882a593Smuzhiyun 
1789*4882a593Smuzhiyun 	if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
1790*4882a593Smuzhiyun 		return 0;
1791*4882a593Smuzhiyun 
1792*4882a593Smuzhiyun 	if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
1793*4882a593Smuzhiyun 		ext4_msg(sb, KERN_ERR,
1794*4882a593Smuzhiyun 			 "filesystem too large to resize to %llu blocks safely",
1795*4882a593Smuzhiyun 			 n_blocks_count);
1796*4882a593Smuzhiyun 		return -EINVAL;
1797*4882a593Smuzhiyun 	}
1798*4882a593Smuzhiyun 
1799*4882a593Smuzhiyun 	if (n_blocks_count < o_blocks_count) {
1800*4882a593Smuzhiyun 		ext4_warning(sb, "can't shrink FS - resize aborted");
1801*4882a593Smuzhiyun 		return -EINVAL;
1802*4882a593Smuzhiyun 	}
1803*4882a593Smuzhiyun 
1804*4882a593Smuzhiyun 	/* Handle the remaining blocks in the last group only. */
1805*4882a593Smuzhiyun 	ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1806*4882a593Smuzhiyun 
1807*4882a593Smuzhiyun 	if (last == 0) {
1808*4882a593Smuzhiyun 		ext4_warning(sb, "need to use ext2online to resize further");
1809*4882a593Smuzhiyun 		return -EPERM;
1810*4882a593Smuzhiyun 	}
1811*4882a593Smuzhiyun 
1812*4882a593Smuzhiyun 	add = EXT4_BLOCKS_PER_GROUP(sb) - last;
1813*4882a593Smuzhiyun 
1814*4882a593Smuzhiyun 	if (o_blocks_count + add < o_blocks_count) {
1815*4882a593Smuzhiyun 		ext4_warning(sb, "blocks_count overflow");
1816*4882a593Smuzhiyun 		return -EINVAL;
1817*4882a593Smuzhiyun 	}
1818*4882a593Smuzhiyun 
1819*4882a593Smuzhiyun 	if (o_blocks_count + add > n_blocks_count)
1820*4882a593Smuzhiyun 		add = n_blocks_count - o_blocks_count;
1821*4882a593Smuzhiyun 
1822*4882a593Smuzhiyun 	if (o_blocks_count + add < n_blocks_count)
1823*4882a593Smuzhiyun 		ext4_warning(sb, "will only finish group (%llu blocks, %u new)",
1824*4882a593Smuzhiyun 			     o_blocks_count + add, add);
1825*4882a593Smuzhiyun 
1826*4882a593Smuzhiyun 	/* See if the device is actually as big as what was requested */
1827*4882a593Smuzhiyun 	bh = ext4_sb_bread(sb, o_blocks_count + add - 1, 0);
1828*4882a593Smuzhiyun 	if (IS_ERR(bh)) {
1829*4882a593Smuzhiyun 		ext4_warning(sb, "can't read last block, resize aborted");
1830*4882a593Smuzhiyun 		return -ENOSPC;
1831*4882a593Smuzhiyun 	}
1832*4882a593Smuzhiyun 	brelse(bh);
1833*4882a593Smuzhiyun 
1834*4882a593Smuzhiyun 	err = ext4_group_extend_no_check(sb, o_blocks_count, add);
1835*4882a593Smuzhiyun 	return err;
1836*4882a593Smuzhiyun } /* ext4_group_extend */
1837*4882a593Smuzhiyun 
1838*4882a593Smuzhiyun 
num_desc_blocks(struct super_block * sb,ext4_group_t groups)1839*4882a593Smuzhiyun static int num_desc_blocks(struct super_block *sb, ext4_group_t groups)
1840*4882a593Smuzhiyun {
1841*4882a593Smuzhiyun 	return (groups + EXT4_DESC_PER_BLOCK(sb) - 1) / EXT4_DESC_PER_BLOCK(sb);
1842*4882a593Smuzhiyun }
1843*4882a593Smuzhiyun 
1844*4882a593Smuzhiyun /*
1845*4882a593Smuzhiyun  * Release the resize inode and drop the resize_inode feature if there
1846*4882a593Smuzhiyun  * are no more reserved gdt blocks, and then convert the file system
1847*4882a593Smuzhiyun  * to enable meta_bg
1848*4882a593Smuzhiyun  */
ext4_convert_meta_bg(struct super_block * sb,struct inode * inode)1849*4882a593Smuzhiyun static int ext4_convert_meta_bg(struct super_block *sb, struct inode *inode)
1850*4882a593Smuzhiyun {
1851*4882a593Smuzhiyun 	handle_t *handle;
1852*4882a593Smuzhiyun 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1853*4882a593Smuzhiyun 	struct ext4_super_block *es = sbi->s_es;
1854*4882a593Smuzhiyun 	struct ext4_inode_info *ei = EXT4_I(inode);
1855*4882a593Smuzhiyun 	ext4_fsblk_t nr;
1856*4882a593Smuzhiyun 	int i, ret, err = 0;
1857*4882a593Smuzhiyun 	int credits = 1;
1858*4882a593Smuzhiyun 
1859*4882a593Smuzhiyun 	ext4_msg(sb, KERN_INFO, "Converting file system to meta_bg");
1860*4882a593Smuzhiyun 	if (inode) {
1861*4882a593Smuzhiyun 		if (es->s_reserved_gdt_blocks) {
1862*4882a593Smuzhiyun 			ext4_error(sb, "Unexpected non-zero "
1863*4882a593Smuzhiyun 				   "s_reserved_gdt_blocks");
1864*4882a593Smuzhiyun 			return -EPERM;
1865*4882a593Smuzhiyun 		}
1866*4882a593Smuzhiyun 
1867*4882a593Smuzhiyun 		/* Do a quick sanity check of the resize inode */
1868*4882a593Smuzhiyun 		if (inode->i_blocks != 1 << (inode->i_blkbits -
1869*4882a593Smuzhiyun 					     (9 - sbi->s_cluster_bits)))
1870*4882a593Smuzhiyun 			goto invalid_resize_inode;
1871*4882a593Smuzhiyun 		for (i = 0; i < EXT4_N_BLOCKS; i++) {
1872*4882a593Smuzhiyun 			if (i == EXT4_DIND_BLOCK) {
1873*4882a593Smuzhiyun 				if (ei->i_data[i])
1874*4882a593Smuzhiyun 					continue;
1875*4882a593Smuzhiyun 				else
1876*4882a593Smuzhiyun 					goto invalid_resize_inode;
1877*4882a593Smuzhiyun 			}
1878*4882a593Smuzhiyun 			if (ei->i_data[i])
1879*4882a593Smuzhiyun 				goto invalid_resize_inode;
1880*4882a593Smuzhiyun 		}
1881*4882a593Smuzhiyun 		credits += 3;	/* block bitmap, bg descriptor, resize inode */
1882*4882a593Smuzhiyun 	}
1883*4882a593Smuzhiyun 
1884*4882a593Smuzhiyun 	handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credits);
1885*4882a593Smuzhiyun 	if (IS_ERR(handle))
1886*4882a593Smuzhiyun 		return PTR_ERR(handle);
1887*4882a593Smuzhiyun 
1888*4882a593Smuzhiyun 	BUFFER_TRACE(sbi->s_sbh, "get_write_access");
1889*4882a593Smuzhiyun 	err = ext4_journal_get_write_access(handle, sbi->s_sbh);
1890*4882a593Smuzhiyun 	if (err)
1891*4882a593Smuzhiyun 		goto errout;
1892*4882a593Smuzhiyun 
1893*4882a593Smuzhiyun 	ext4_clear_feature_resize_inode(sb);
1894*4882a593Smuzhiyun 	ext4_set_feature_meta_bg(sb);
1895*4882a593Smuzhiyun 	sbi->s_es->s_first_meta_bg =
1896*4882a593Smuzhiyun 		cpu_to_le32(num_desc_blocks(sb, sbi->s_groups_count));
1897*4882a593Smuzhiyun 
1898*4882a593Smuzhiyun 	err = ext4_handle_dirty_super(handle, sb);
1899*4882a593Smuzhiyun 	if (err) {
1900*4882a593Smuzhiyun 		ext4_std_error(sb, err);
1901*4882a593Smuzhiyun 		goto errout;
1902*4882a593Smuzhiyun 	}
1903*4882a593Smuzhiyun 
1904*4882a593Smuzhiyun 	if (inode) {
1905*4882a593Smuzhiyun 		nr = le32_to_cpu(ei->i_data[EXT4_DIND_BLOCK]);
1906*4882a593Smuzhiyun 		ext4_free_blocks(handle, inode, NULL, nr, 1,
1907*4882a593Smuzhiyun 				 EXT4_FREE_BLOCKS_METADATA |
1908*4882a593Smuzhiyun 				 EXT4_FREE_BLOCKS_FORGET);
1909*4882a593Smuzhiyun 		ei->i_data[EXT4_DIND_BLOCK] = 0;
1910*4882a593Smuzhiyun 		inode->i_blocks = 0;
1911*4882a593Smuzhiyun 
1912*4882a593Smuzhiyun 		err = ext4_mark_inode_dirty(handle, inode);
1913*4882a593Smuzhiyun 		if (err)
1914*4882a593Smuzhiyun 			ext4_std_error(sb, err);
1915*4882a593Smuzhiyun 	}
1916*4882a593Smuzhiyun 
1917*4882a593Smuzhiyun errout:
1918*4882a593Smuzhiyun 	ret = ext4_journal_stop(handle);
1919*4882a593Smuzhiyun 	if (!err)
1920*4882a593Smuzhiyun 		err = ret;
1921*4882a593Smuzhiyun 	return ret;
1922*4882a593Smuzhiyun 
1923*4882a593Smuzhiyun invalid_resize_inode:
1924*4882a593Smuzhiyun 	ext4_error(sb, "corrupted/inconsistent resize inode");
1925*4882a593Smuzhiyun 	return -EINVAL;
1926*4882a593Smuzhiyun }
1927*4882a593Smuzhiyun 
1928*4882a593Smuzhiyun /*
1929*4882a593Smuzhiyun  * ext4_resize_fs() resizes a fs to new size specified by @n_blocks_count
1930*4882a593Smuzhiyun  *
1931*4882a593Smuzhiyun  * @sb: super block of the fs to be resized
1932*4882a593Smuzhiyun  * @n_blocks_count: the number of blocks resides in the resized fs
1933*4882a593Smuzhiyun  */
ext4_resize_fs(struct super_block * sb,ext4_fsblk_t n_blocks_count)1934*4882a593Smuzhiyun int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count)
1935*4882a593Smuzhiyun {
1936*4882a593Smuzhiyun 	struct ext4_new_flex_group_data *flex_gd = NULL;
1937*4882a593Smuzhiyun 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1938*4882a593Smuzhiyun 	struct ext4_super_block *es = sbi->s_es;
1939*4882a593Smuzhiyun 	struct buffer_head *bh;
1940*4882a593Smuzhiyun 	struct inode *resize_inode = NULL;
1941*4882a593Smuzhiyun 	ext4_grpblk_t add, offset;
1942*4882a593Smuzhiyun 	unsigned long n_desc_blocks;
1943*4882a593Smuzhiyun 	unsigned long o_desc_blocks;
1944*4882a593Smuzhiyun 	ext4_group_t o_group;
1945*4882a593Smuzhiyun 	ext4_group_t n_group;
1946*4882a593Smuzhiyun 	ext4_fsblk_t o_blocks_count;
1947*4882a593Smuzhiyun 	ext4_fsblk_t n_blocks_count_retry = 0;
1948*4882a593Smuzhiyun 	unsigned long last_update_time = 0;
1949*4882a593Smuzhiyun 	int err = 0, flexbg_size = 1 << sbi->s_log_groups_per_flex;
1950*4882a593Smuzhiyun 	int meta_bg;
1951*4882a593Smuzhiyun 
1952*4882a593Smuzhiyun 	/* See if the device is actually as big as what was requested */
1953*4882a593Smuzhiyun 	bh = ext4_sb_bread(sb, n_blocks_count - 1, 0);
1954*4882a593Smuzhiyun 	if (IS_ERR(bh)) {
1955*4882a593Smuzhiyun 		ext4_warning(sb, "can't read last block, resize aborted");
1956*4882a593Smuzhiyun 		return -ENOSPC;
1957*4882a593Smuzhiyun 	}
1958*4882a593Smuzhiyun 	brelse(bh);
1959*4882a593Smuzhiyun 
1960*4882a593Smuzhiyun 	/*
1961*4882a593Smuzhiyun 	 * For bigalloc, trim the requested size to the nearest cluster
1962*4882a593Smuzhiyun 	 * boundary to avoid creating an unusable filesystem. We do this
1963*4882a593Smuzhiyun 	 * silently, instead of returning an error, to avoid breaking
1964*4882a593Smuzhiyun 	 * callers that blindly resize the filesystem to the full size of
1965*4882a593Smuzhiyun 	 * the underlying block device.
1966*4882a593Smuzhiyun 	 */
1967*4882a593Smuzhiyun 	if (ext4_has_feature_bigalloc(sb))
1968*4882a593Smuzhiyun 		n_blocks_count &= ~((1 << EXT4_CLUSTER_BITS(sb)) - 1);
1969*4882a593Smuzhiyun 
1970*4882a593Smuzhiyun retry:
1971*4882a593Smuzhiyun 	o_blocks_count = ext4_blocks_count(es);
1972*4882a593Smuzhiyun 
1973*4882a593Smuzhiyun 	ext4_msg(sb, KERN_INFO, "resizing filesystem from %llu "
1974*4882a593Smuzhiyun 		 "to %llu blocks", o_blocks_count, n_blocks_count);
1975*4882a593Smuzhiyun 
1976*4882a593Smuzhiyun 	if (n_blocks_count < o_blocks_count) {
1977*4882a593Smuzhiyun 		/* On-line shrinking not supported */
1978*4882a593Smuzhiyun 		ext4_warning(sb, "can't shrink FS - resize aborted");
1979*4882a593Smuzhiyun 		return -EINVAL;
1980*4882a593Smuzhiyun 	}
1981*4882a593Smuzhiyun 
1982*4882a593Smuzhiyun 	if (n_blocks_count == o_blocks_count)
1983*4882a593Smuzhiyun 		/* Nothing need to do */
1984*4882a593Smuzhiyun 		return 0;
1985*4882a593Smuzhiyun 
1986*4882a593Smuzhiyun 	n_group = ext4_get_group_number(sb, n_blocks_count - 1);
1987*4882a593Smuzhiyun 	if (n_group >= (0xFFFFFFFFUL / EXT4_INODES_PER_GROUP(sb))) {
1988*4882a593Smuzhiyun 		ext4_warning(sb, "resize would cause inodes_count overflow");
1989*4882a593Smuzhiyun 		return -EINVAL;
1990*4882a593Smuzhiyun 	}
1991*4882a593Smuzhiyun 	ext4_get_group_no_and_offset(sb, o_blocks_count - 1, &o_group, &offset);
1992*4882a593Smuzhiyun 
1993*4882a593Smuzhiyun 	n_desc_blocks = num_desc_blocks(sb, n_group + 1);
1994*4882a593Smuzhiyun 	o_desc_blocks = num_desc_blocks(sb, sbi->s_groups_count);
1995*4882a593Smuzhiyun 
1996*4882a593Smuzhiyun 	meta_bg = ext4_has_feature_meta_bg(sb);
1997*4882a593Smuzhiyun 
1998*4882a593Smuzhiyun 	if (ext4_has_feature_resize_inode(sb)) {
1999*4882a593Smuzhiyun 		if (meta_bg) {
2000*4882a593Smuzhiyun 			ext4_error(sb, "resize_inode and meta_bg enabled "
2001*4882a593Smuzhiyun 				   "simultaneously");
2002*4882a593Smuzhiyun 			return -EINVAL;
2003*4882a593Smuzhiyun 		}
2004*4882a593Smuzhiyun 		if (n_desc_blocks > o_desc_blocks +
2005*4882a593Smuzhiyun 		    le16_to_cpu(es->s_reserved_gdt_blocks)) {
2006*4882a593Smuzhiyun 			n_blocks_count_retry = n_blocks_count;
2007*4882a593Smuzhiyun 			n_desc_blocks = o_desc_blocks +
2008*4882a593Smuzhiyun 				le16_to_cpu(es->s_reserved_gdt_blocks);
2009*4882a593Smuzhiyun 			n_group = n_desc_blocks * EXT4_DESC_PER_BLOCK(sb);
2010*4882a593Smuzhiyun 			n_blocks_count = (ext4_fsblk_t)n_group *
2011*4882a593Smuzhiyun 				EXT4_BLOCKS_PER_GROUP(sb) +
2012*4882a593Smuzhiyun 				le32_to_cpu(es->s_first_data_block);
2013*4882a593Smuzhiyun 			n_group--; /* set to last group number */
2014*4882a593Smuzhiyun 		}
2015*4882a593Smuzhiyun 
2016*4882a593Smuzhiyun 		if (!resize_inode)
2017*4882a593Smuzhiyun 			resize_inode = ext4_iget(sb, EXT4_RESIZE_INO,
2018*4882a593Smuzhiyun 						 EXT4_IGET_SPECIAL);
2019*4882a593Smuzhiyun 		if (IS_ERR(resize_inode)) {
2020*4882a593Smuzhiyun 			ext4_warning(sb, "Error opening resize inode");
2021*4882a593Smuzhiyun 			return PTR_ERR(resize_inode);
2022*4882a593Smuzhiyun 		}
2023*4882a593Smuzhiyun 	}
2024*4882a593Smuzhiyun 
2025*4882a593Smuzhiyun 	if ((!resize_inode && !meta_bg) || n_blocks_count == o_blocks_count) {
2026*4882a593Smuzhiyun 		err = ext4_convert_meta_bg(sb, resize_inode);
2027*4882a593Smuzhiyun 		if (err)
2028*4882a593Smuzhiyun 			goto out;
2029*4882a593Smuzhiyun 		if (resize_inode) {
2030*4882a593Smuzhiyun 			iput(resize_inode);
2031*4882a593Smuzhiyun 			resize_inode = NULL;
2032*4882a593Smuzhiyun 		}
2033*4882a593Smuzhiyun 		if (n_blocks_count_retry) {
2034*4882a593Smuzhiyun 			n_blocks_count = n_blocks_count_retry;
2035*4882a593Smuzhiyun 			n_blocks_count_retry = 0;
2036*4882a593Smuzhiyun 			goto retry;
2037*4882a593Smuzhiyun 		}
2038*4882a593Smuzhiyun 	}
2039*4882a593Smuzhiyun 
2040*4882a593Smuzhiyun 	/*
2041*4882a593Smuzhiyun 	 * Make sure the last group has enough space so that it's
2042*4882a593Smuzhiyun 	 * guaranteed to have enough space for all metadata blocks
2043*4882a593Smuzhiyun 	 * that it might need to hold.  (We might not need to store
2044*4882a593Smuzhiyun 	 * the inode table blocks in the last block group, but there
2045*4882a593Smuzhiyun 	 * will be cases where this might be needed.)
2046*4882a593Smuzhiyun 	 */
2047*4882a593Smuzhiyun 	if ((ext4_group_first_block_no(sb, n_group) +
2048*4882a593Smuzhiyun 	     ext4_group_overhead_blocks(sb, n_group) + 2 +
2049*4882a593Smuzhiyun 	     sbi->s_itb_per_group + sbi->s_cluster_ratio) >= n_blocks_count) {
2050*4882a593Smuzhiyun 		n_blocks_count = ext4_group_first_block_no(sb, n_group);
2051*4882a593Smuzhiyun 		n_group--;
2052*4882a593Smuzhiyun 		n_blocks_count_retry = 0;
2053*4882a593Smuzhiyun 		if (resize_inode) {
2054*4882a593Smuzhiyun 			iput(resize_inode);
2055*4882a593Smuzhiyun 			resize_inode = NULL;
2056*4882a593Smuzhiyun 		}
2057*4882a593Smuzhiyun 		goto retry;
2058*4882a593Smuzhiyun 	}
2059*4882a593Smuzhiyun 
2060*4882a593Smuzhiyun 	/* extend the last group */
2061*4882a593Smuzhiyun 	if (n_group == o_group)
2062*4882a593Smuzhiyun 		add = n_blocks_count - o_blocks_count;
2063*4882a593Smuzhiyun 	else
2064*4882a593Smuzhiyun 		add = EXT4_C2B(sbi, EXT4_CLUSTERS_PER_GROUP(sb) - (offset + 1));
2065*4882a593Smuzhiyun 	if (add > 0) {
2066*4882a593Smuzhiyun 		err = ext4_group_extend_no_check(sb, o_blocks_count, add);
2067*4882a593Smuzhiyun 		if (err)
2068*4882a593Smuzhiyun 			goto out;
2069*4882a593Smuzhiyun 	}
2070*4882a593Smuzhiyun 
2071*4882a593Smuzhiyun 	if (ext4_blocks_count(es) == n_blocks_count && n_blocks_count_retry == 0)
2072*4882a593Smuzhiyun 		goto out;
2073*4882a593Smuzhiyun 
2074*4882a593Smuzhiyun 	err = ext4_alloc_flex_bg_array(sb, n_group + 1);
2075*4882a593Smuzhiyun 	if (err)
2076*4882a593Smuzhiyun 		goto out;
2077*4882a593Smuzhiyun 
2078*4882a593Smuzhiyun 	err = ext4_mb_alloc_groupinfo(sb, n_group + 1);
2079*4882a593Smuzhiyun 	if (err)
2080*4882a593Smuzhiyun 		goto out;
2081*4882a593Smuzhiyun 
2082*4882a593Smuzhiyun 	flex_gd = alloc_flex_gd(flexbg_size);
2083*4882a593Smuzhiyun 	if (flex_gd == NULL) {
2084*4882a593Smuzhiyun 		err = -ENOMEM;
2085*4882a593Smuzhiyun 		goto out;
2086*4882a593Smuzhiyun 	}
2087*4882a593Smuzhiyun 
2088*4882a593Smuzhiyun 	/* Add flex groups. Note that a regular group is a
2089*4882a593Smuzhiyun 	 * flex group with 1 group.
2090*4882a593Smuzhiyun 	 */
2091*4882a593Smuzhiyun 	while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count,
2092*4882a593Smuzhiyun 					      flexbg_size)) {
2093*4882a593Smuzhiyun 		if (jiffies - last_update_time > HZ * 10) {
2094*4882a593Smuzhiyun 			if (last_update_time)
2095*4882a593Smuzhiyun 				ext4_msg(sb, KERN_INFO,
2096*4882a593Smuzhiyun 					 "resized to %llu blocks",
2097*4882a593Smuzhiyun 					 ext4_blocks_count(es));
2098*4882a593Smuzhiyun 			last_update_time = jiffies;
2099*4882a593Smuzhiyun 		}
2100*4882a593Smuzhiyun 		if (ext4_alloc_group_tables(sb, flex_gd, flexbg_size) != 0)
2101*4882a593Smuzhiyun 			break;
2102*4882a593Smuzhiyun 		err = ext4_flex_group_add(sb, resize_inode, flex_gd);
2103*4882a593Smuzhiyun 		if (unlikely(err))
2104*4882a593Smuzhiyun 			break;
2105*4882a593Smuzhiyun 	}
2106*4882a593Smuzhiyun 
2107*4882a593Smuzhiyun 	if (!err && n_blocks_count_retry) {
2108*4882a593Smuzhiyun 		n_blocks_count = n_blocks_count_retry;
2109*4882a593Smuzhiyun 		n_blocks_count_retry = 0;
2110*4882a593Smuzhiyun 		free_flex_gd(flex_gd);
2111*4882a593Smuzhiyun 		flex_gd = NULL;
2112*4882a593Smuzhiyun 		if (resize_inode) {
2113*4882a593Smuzhiyun 			iput(resize_inode);
2114*4882a593Smuzhiyun 			resize_inode = NULL;
2115*4882a593Smuzhiyun 		}
2116*4882a593Smuzhiyun 		goto retry;
2117*4882a593Smuzhiyun 	}
2118*4882a593Smuzhiyun 
2119*4882a593Smuzhiyun out:
2120*4882a593Smuzhiyun 	if (flex_gd)
2121*4882a593Smuzhiyun 		free_flex_gd(flex_gd);
2122*4882a593Smuzhiyun 	if (resize_inode != NULL)
2123*4882a593Smuzhiyun 		iput(resize_inode);
2124*4882a593Smuzhiyun 	if (err)
2125*4882a593Smuzhiyun 		ext4_warning(sb, "error (%d) occurred during "
2126*4882a593Smuzhiyun 			     "file system resize", err);
2127*4882a593Smuzhiyun 	ext4_msg(sb, KERN_INFO, "resized filesystem to %llu",
2128*4882a593Smuzhiyun 		 ext4_blocks_count(es));
2129*4882a593Smuzhiyun 	return err;
2130*4882a593Smuzhiyun }
2131