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