blob: b63cb88ccdaede8e6fa8629a2fe544a2920c0c60 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002/*
Mingming Cao617ba132006-10-11 01:20:53 -07003 * linux/fs/ext4/resize.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004 *
Mingming Cao617ba132006-10-11 01:20:53 -07005 * Support for resizing an ext4 filesystem while it is mounted.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006 *
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
Mingming Cao617ba132006-10-11 01:20:53 -070013#define EXT4FS_DEBUG
Dave Kleikampac27a0e2006-10-11 01:20:50 -070014
Dave Kleikampac27a0e2006-10-11 01:20:50 -070015#include <linux/errno.h>
16#include <linux/slab.h>
17
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040018#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070019
Theodore Ts'o1d0c3922020-02-15 16:40:37 -050020struct ext4_rcu_ptr {
21 struct rcu_head rcu;
22 void *ptr;
23};
24
25static 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
34void 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
Yongqiang Yang8f82f842011-07-26 21:35:44 -040047int ext4_resize_begin(struct super_block *sb)
48{
Theodore Ts'o5f8c1092018-09-03 22:25:01 -040049 struct ext4_sb_info *sbi = EXT4_SB(sb);
Yongqiang Yang8f82f842011-07-26 21:35:44 -040050 int ret = 0;
51
52 if (!capable(CAP_SYS_RESOURCE))
53 return -EPERM;
54
Yongqiang Yangce723c32011-07-26 21:39:09 -040055 /*
Theodore Ts'o011fa992014-12-26 23:58:21 -050056 * If we are not using the primary superblock/GDT copy don't resize,
57 * because the user tools have no way of handling this. Probably a
58 * bad time to do it anyways.
59 */
Theodore Ts'o5f8c1092018-09-03 22:25:01 -040060 if (EXT4_B2C(sbi, sbi->s_sbh->b_blocknr) !=
Theodore Ts'o011fa992014-12-26 23:58:21 -050061 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
62 ext4_warning(sb, "won't resize using backup superblock at %llu",
63 (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
64 return -EPERM;
65 }
66
67 /*
Yongqiang Yangce723c32011-07-26 21:39:09 -040068 * We are not allowed to do online-resizing on a filesystem mounted
69 * with error, because it can destroy the filesystem easily.
70 */
71 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
72 ext4_warning(sb, "There are errors in the filesystem, "
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -040073 "so online resizing is not allowed");
Yongqiang Yangce723c32011-07-26 21:39:09 -040074 return -EPERM;
75 }
76
Josh Triplettb1489182021-06-07 12:15:24 -070077 if (ext4_has_feature_sparse_super2(sb)) {
78 ext4_msg(sb, KERN_ERR, "Online resizing not supported with sparse_super2");
79 return -EOPNOTSUPP;
80 }
Josh Triplette9f9f612021-06-07 12:15:08 -070081
Theodore Ts'o9549a162017-02-05 01:27:48 -050082 if (test_and_set_bit_lock(EXT4_FLAGS_RESIZING,
83 &EXT4_SB(sb)->s_ext4_flags))
Yongqiang Yang8f82f842011-07-26 21:35:44 -040084 ret = -EBUSY;
85
86 return ret;
87}
88
89void ext4_resize_end(struct super_block *sb)
90{
Theodore Ts'o9549a162017-02-05 01:27:48 -050091 clear_bit_unlock(EXT4_FLAGS_RESIZING, &EXT4_SB(sb)->s_ext4_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +010092 smp_mb__after_atomic();
Yongqiang Yang8f82f842011-07-26 21:35:44 -040093}
94
Yongqiang Yang01f795f2012-09-05 01:33:50 -040095static ext4_group_t ext4_meta_bg_first_group(struct super_block *sb,
96 ext4_group_t group) {
97 return (group >> EXT4_DESC_PER_BLOCK_BITS(sb)) <<
98 EXT4_DESC_PER_BLOCK_BITS(sb);
99}
100
101static ext4_fsblk_t ext4_meta_bg_first_block_no(struct super_block *sb,
102 ext4_group_t group) {
103 group = ext4_meta_bg_first_group(sb, group);
104 return ext4_group_first_block_no(sb, group);
105}
106
107static ext4_grpblk_t ext4_group_overhead_blocks(struct super_block *sb,
108 ext4_group_t group) {
109 ext4_grpblk_t overhead;
110 overhead = ext4_bg_num_gdb(sb, group);
111 if (ext4_bg_has_super(sb, group))
112 overhead += 1 +
113 le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
114 return overhead;
115}
116
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700117#define outside(b, first, last) ((b) < (first) || (b) >= (last))
118#define inside(b, first, last) ((b) >= (first) && (b) < (last))
119
120static int verify_group_input(struct super_block *sb,
Mingming Cao617ba132006-10-11 01:20:53 -0700121 struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700122{
Mingming Cao617ba132006-10-11 01:20:53 -0700123 struct ext4_sb_info *sbi = EXT4_SB(sb);
124 struct ext4_super_block *es = sbi->s_es;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700125 ext4_fsblk_t start = ext4_blocks_count(es);
Mingming Cao617ba132006-10-11 01:20:53 -0700126 ext4_fsblk_t end = start + input->blocks_count;
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500127 ext4_group_t group = input->group;
Mingming Cao617ba132006-10-11 01:20:53 -0700128 ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group;
Theodore Ts'ob302ef22013-06-06 11:14:31 -0400129 unsigned overhead;
130 ext4_fsblk_t metaend;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700131 struct buffer_head *bh = NULL;
Mingming Cao3a5b2ec2006-10-11 01:21:05 -0700132 ext4_grpblk_t free_blocks_count, offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700133 int err = -EINVAL;
134
Theodore Ts'ob302ef22013-06-06 11:14:31 -0400135 if (group != sbi->s_groups_count) {
136 ext4_warning(sb, "Cannot add at group %u (only %u groups)",
137 input->group, sbi->s_groups_count);
138 return -EINVAL;
139 }
140
141 overhead = ext4_group_overhead_blocks(sb, group);
142 metaend = start + overhead;
harshadsd77147f2017-10-29 09:38:46 -0400143 input->free_clusters_count = free_blocks_count =
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700144 input->blocks_count - 2 - overhead - sbi->s_itb_per_group;
145
146 if (test_opt(sb, DEBUG))
Mingming Cao617ba132006-10-11 01:20:53 -0700147 printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700148 "(%d free, %u reserved)\n",
Mingming Cao617ba132006-10-11 01:20:53 -0700149 ext4_bg_has_super(sb, input->group) ? "normal" :
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700150 "no-super", input->group, input->blocks_count,
151 free_blocks_count, input->reserved_blocks);
152
Mingming Cao3a5b2ec2006-10-11 01:21:05 -0700153 ext4_get_group_no_and_offset(sb, start, NULL, &offset);
Theodore Ts'ob302ef22013-06-06 11:14:31 -0400154 if (offset != 0)
Eric Sandeen12062dd2010-02-15 14:19:27 -0500155 ext4_warning(sb, "Last group not full");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700156 else if (input->reserved_blocks > input->blocks_count / 5)
Eric Sandeen12062dd2010-02-15 14:19:27 -0500157 ext4_warning(sb, "Reserved blocks too high (%u)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700158 input->reserved_blocks);
159 else if (free_blocks_count < 0)
Eric Sandeen12062dd2010-02-15 14:19:27 -0500160 ext4_warning(sb, "Bad blocks count %u",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700161 input->blocks_count);
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500162 else if (IS_ERR(bh = ext4_sb_bread(sb, end - 1, 0))) {
163 err = PTR_ERR(bh);
164 bh = NULL;
Eric Sandeen12062dd2010-02-15 14:19:27 -0500165 ext4_warning(sb, "Cannot read last block (%llu)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700166 end - 1);
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500167 } else if (outside(input->block_bitmap, start, end))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500168 ext4_warning(sb, "Block bitmap not in group (block %llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700169 (unsigned long long)input->block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170 else if (outside(input->inode_bitmap, start, end))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500171 ext4_warning(sb, "Inode bitmap not in group (block %llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700172 (unsigned long long)input->inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700173 else if (outside(input->inode_table, start, end) ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400174 outside(itend - 1, start, end))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500175 ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700176 (unsigned long long)input->inode_table, itend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700177 else if (input->inode_bitmap == input->block_bitmap)
Eric Sandeen12062dd2010-02-15 14:19:27 -0500178 ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700179 (unsigned long long)input->block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180 else if (inside(input->block_bitmap, input->inode_table, itend))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500181 ext4_warning(sb, "Block bitmap (%llu) in inode table "
182 "(%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700183 (unsigned long long)input->block_bitmap,
184 (unsigned long long)input->inode_table, itend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185 else if (inside(input->inode_bitmap, input->inode_table, itend))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500186 ext4_warning(sb, "Inode bitmap (%llu) in inode table "
187 "(%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700188 (unsigned long long)input->inode_bitmap,
189 (unsigned long long)input->inode_table, itend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700190 else if (inside(input->block_bitmap, start, metaend))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500191 ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700192 (unsigned long long)input->block_bitmap,
193 start, metaend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700194 else if (inside(input->inode_bitmap, start, metaend))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500195 ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700196 (unsigned long long)input->inode_bitmap,
197 start, metaend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700198 else if (inside(input->inode_table, start, metaend) ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400199 inside(itend - 1, start, metaend))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500200 ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table "
201 "(%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700202 (unsigned long long)input->inode_table,
203 itend - 1, start, metaend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700204 else
205 err = 0;
206 brelse(bh);
207
208 return err;
209}
210
Yongqiang Yang28c7bac2012-01-03 23:22:50 -0500211/*
212 * ext4_new_flex_group_data is used by 64bit-resize interface to add a flex
213 * group each time.
214 */
215struct ext4_new_flex_group_data {
216 struct ext4_new_group_data *groups; /* new_group_data for groups
217 in the flex group */
218 __u16 *bg_flags; /* block group flags of groups
219 in @groups */
220 ext4_group_t count; /* number of groups in @groups
221 */
222};
223
224/*
225 * alloc_flex_gd() allocates a ext4_new_flex_group_data with size of
226 * @flexbg_size.
227 *
228 * Returns NULL on failure otherwise address of the allocated structure.
229 */
230static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size)
231{
232 struct ext4_new_flex_group_data *flex_gd;
233
234 flex_gd = kmalloc(sizeof(*flex_gd), GFP_NOFS);
235 if (flex_gd == NULL)
236 goto out3;
237
Insu Yun46901762016-02-12 01:15:59 -0500238 if (flexbg_size >= UINT_MAX / sizeof(struct ext4_new_group_data))
Haogang Chen967ac8a2012-05-28 14:21:55 -0400239 goto out2;
Yongqiang Yang28c7bac2012-01-03 23:22:50 -0500240 flex_gd->count = flexbg_size;
241
Kees Cook6da2ec52018-06-12 13:55:00 -0700242 flex_gd->groups = kmalloc_array(flexbg_size,
243 sizeof(struct ext4_new_group_data),
244 GFP_NOFS);
Yongqiang Yang28c7bac2012-01-03 23:22:50 -0500245 if (flex_gd->groups == NULL)
246 goto out2;
247
Kees Cook6da2ec52018-06-12 13:55:00 -0700248 flex_gd->bg_flags = kmalloc_array(flexbg_size, sizeof(__u16),
249 GFP_NOFS);
Yongqiang Yang28c7bac2012-01-03 23:22:50 -0500250 if (flex_gd->bg_flags == NULL)
251 goto out1;
252
253 return flex_gd;
254
255out1:
256 kfree(flex_gd->groups);
257out2:
258 kfree(flex_gd);
259out3:
260 return NULL;
261}
262
263static void free_flex_gd(struct ext4_new_flex_group_data *flex_gd)
264{
265 kfree(flex_gd->bg_flags);
266 kfree(flex_gd->groups);
267 kfree(flex_gd);
268}
269
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500270/*
271 * ext4_alloc_group_tables() allocates block bitmaps, inode bitmaps
272 * and inode tables for a flex group.
273 *
274 * This function is used by 64bit-resize. Note that this function allocates
275 * group tables from the 1st group of groups contained by @flexgd, which may
276 * be a partial of a flex group.
277 *
278 * @sb: super block of fs to which the groups belongs
Yongqiang Yang03c1c292012-09-05 01:21:50 -0400279 *
280 * Returns 0 on a successful allocation of the metadata blocks in the
281 * block group.
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500282 */
Yongqiang Yang03c1c292012-09-05 01:21:50 -0400283static int ext4_alloc_group_tables(struct super_block *sb,
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500284 struct ext4_new_flex_group_data *flex_gd,
285 int flexbg_size)
286{
287 struct ext4_new_group_data *group_data = flex_gd->groups;
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500288 ext4_fsblk_t start_blk;
289 ext4_fsblk_t last_blk;
290 ext4_group_t src_group;
291 ext4_group_t bb_index = 0;
292 ext4_group_t ib_index = 0;
293 ext4_group_t it_index = 0;
294 ext4_group_t group;
295 ext4_group_t last_group;
296 unsigned overhead;
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500297 __u16 uninit_mask = (flexbg_size > 1) ? ~EXT4_BG_BLOCK_UNINIT : ~0;
harshadsd77147f2017-10-29 09:38:46 -0400298 int i;
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500299
300 BUG_ON(flex_gd->count == 0 || group_data == NULL);
301
302 src_group = group_data[0].group;
303 last_group = src_group + flex_gd->count - 1;
304
305 BUG_ON((flexbg_size > 1) && ((src_group & ~(flexbg_size - 1)) !=
306 (last_group & ~(flexbg_size - 1))));
307next_group:
308 group = group_data[0].group;
Yongqiang Yang03c1c292012-09-05 01:21:50 -0400309 if (src_group >= group_data[0].group + flex_gd->count)
310 return -ENOSPC;
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500311 start_blk = ext4_group_first_block_no(sb, src_group);
312 last_blk = start_blk + group_data[src_group - group].blocks_count;
313
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400314 overhead = ext4_group_overhead_blocks(sb, src_group);
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500315
316 start_blk += overhead;
317
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500318 /* We collect contiguous blocks as much as possible. */
319 src_group++;
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400320 for (; src_group <= last_group; src_group++) {
321 overhead = ext4_group_overhead_blocks(sb, src_group);
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500322 if (overhead == 0)
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500323 last_blk += group_data[src_group - group].blocks_count;
324 else
325 break;
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400326 }
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500327
328 /* Allocate block bitmaps */
329 for (; bb_index < flex_gd->count; bb_index++) {
330 if (start_blk >= last_blk)
331 goto next_group;
332 group_data[bb_index].block_bitmap = start_blk++;
Lukas Czernerbd862982013-04-03 23:32:34 -0400333 group = ext4_get_group_number(sb, start_blk - 1);
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500334 group -= group_data[0].group;
harshadsd77147f2017-10-29 09:38:46 -0400335 group_data[group].mdata_blocks++;
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500336 flex_gd->bg_flags[group] &= uninit_mask;
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500337 }
338
339 /* Allocate inode bitmaps */
340 for (; ib_index < flex_gd->count; ib_index++) {
341 if (start_blk >= last_blk)
342 goto next_group;
343 group_data[ib_index].inode_bitmap = start_blk++;
Lukas Czernerbd862982013-04-03 23:32:34 -0400344 group = ext4_get_group_number(sb, start_blk - 1);
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500345 group -= group_data[0].group;
harshadsd77147f2017-10-29 09:38:46 -0400346 group_data[group].mdata_blocks++;
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500347 flex_gd->bg_flags[group] &= uninit_mask;
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500348 }
349
350 /* Allocate inode tables */
351 for (; it_index < flex_gd->count; it_index++) {
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500352 unsigned int itb = EXT4_SB(sb)->s_itb_per_group;
353 ext4_fsblk_t next_group_start;
354
355 if (start_blk + itb > last_blk)
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500356 goto next_group;
357 group_data[it_index].inode_table = start_blk;
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500358 group = ext4_get_group_number(sb, start_blk);
359 next_group_start = ext4_group_first_block_no(sb, group + 1);
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500360 group -= group_data[0].group;
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500361
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500362 if (start_blk + itb > next_group_start) {
363 flex_gd->bg_flags[group + 1] &= uninit_mask;
364 overhead = start_blk + itb - next_group_start;
harshadsd77147f2017-10-29 09:38:46 -0400365 group_data[group + 1].mdata_blocks += overhead;
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500366 itb -= overhead;
367 }
368
harshadsd77147f2017-10-29 09:38:46 -0400369 group_data[group].mdata_blocks += itb;
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500370 flex_gd->bg_flags[group] &= uninit_mask;
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500371 start_blk += EXT4_SB(sb)->s_itb_per_group;
372 }
373
harshadsd77147f2017-10-29 09:38:46 -0400374 /* Update free clusters count to exclude metadata blocks */
375 for (i = 0; i < flex_gd->count; i++) {
376 group_data[i].free_clusters_count -=
377 EXT4_NUM_B2C(EXT4_SB(sb),
378 group_data[i].mdata_blocks);
379 }
380
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500381 if (test_opt(sb, DEBUG)) {
382 int i;
383 group = group_data[0].group;
384
385 printk(KERN_DEBUG "EXT4-fs: adding a flex group with "
386 "%d groups, flexbg size is %d:\n", flex_gd->count,
387 flexbg_size);
388
389 for (i = 0; i < flex_gd->count; i++) {
harshadsd77147f2017-10-29 09:38:46 -0400390 ext4_debug(
391 "adding %s group %u: %u blocks (%d free, %d mdata blocks)\n",
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500392 ext4_bg_has_super(sb, group + i) ? "normal" :
393 "no-super", group + i,
394 group_data[i].blocks_count,
harshadsd77147f2017-10-29 09:38:46 -0400395 group_data[i].free_clusters_count,
396 group_data[i].mdata_blocks);
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500397 }
398 }
Yongqiang Yang03c1c292012-09-05 01:21:50 -0400399 return 0;
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500400}
401
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700402static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
Mingming Cao617ba132006-10-11 01:20:53 -0700403 ext4_fsblk_t blk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700404{
405 struct buffer_head *bh;
406 int err;
407
408 bh = sb_getblk(sb, blk);
Wang Shilongaebf0242013-01-12 16:28:47 -0500409 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500410 return ERR_PTR(-ENOMEM);
liang xie5d601252014-05-12 22:06:43 -0400411 BUFFER_TRACE(bh, "get_write_access");
Jan Kara188c2992021-08-16 11:57:04 +0200412 err = ext4_journal_get_write_access(handle, sb, bh, EXT4_JTR_NONE);
413 if (err) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700414 brelse(bh);
415 bh = ERR_PTR(err);
416 } else {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700417 memset(bh->b_data, 0, sb->s_blocksize);
418 set_buffer_uptodate(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700419 }
420
421 return bh;
422}
423
Jan Kara83448bd2019-11-05 17:44:29 +0100424static int ext4_resize_ensure_credits_batch(handle_t *handle, int credits)
425{
426 return ext4_journal_ensure_credits_fn(handle, credits,
427 EXT4_MAX_TRANS_DATA, 0, 0);
428}
429
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700430/*
harshadsd77147f2017-10-29 09:38:46 -0400431 * set_flexbg_block_bitmap() mark clusters [@first_cluster, @last_cluster] used.
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500432 *
433 * Helper function for ext4_setup_new_group_blocks() which set .
434 *
435 * @sb: super block
436 * @handle: journal handle
437 * @flex_gd: flex group data
438 */
439static int set_flexbg_block_bitmap(struct super_block *sb, handle_t *handle,
440 struct ext4_new_flex_group_data *flex_gd,
harshadsd77147f2017-10-29 09:38:46 -0400441 ext4_fsblk_t first_cluster, ext4_fsblk_t last_cluster)
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500442{
harshadsd77147f2017-10-29 09:38:46 -0400443 struct ext4_sb_info *sbi = EXT4_SB(sb);
444 ext4_group_t count = last_cluster - first_cluster + 1;
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500445 ext4_group_t count2;
446
harshadsd77147f2017-10-29 09:38:46 -0400447 ext4_debug("mark clusters [%llu-%llu] used\n", first_cluster,
448 last_cluster);
449 for (count2 = count; count > 0;
450 count -= count2, first_cluster += count2) {
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500451 ext4_fsblk_t start;
452 struct buffer_head *bh;
453 ext4_group_t group;
454 int err;
455
harshadsd77147f2017-10-29 09:38:46 -0400456 group = ext4_get_group_number(sb, EXT4_C2B(sbi, first_cluster));
457 start = EXT4_B2C(sbi, ext4_group_first_block_no(sb, group));
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500458 group -= flex_gd->groups[0].group;
459
harshadsd77147f2017-10-29 09:38:46 -0400460 count2 = EXT4_CLUSTERS_PER_GROUP(sb) - (first_cluster - start);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500461 if (count2 > count)
462 count2 = count;
463
464 if (flex_gd->bg_flags[group] & EXT4_BG_BLOCK_UNINIT) {
465 BUG_ON(flex_gd->count > 1);
466 continue;
467 }
468
Jan Kara83448bd2019-11-05 17:44:29 +0100469 err = ext4_resize_ensure_credits_batch(handle, 1);
Jan Karaa4130362019-11-05 17:44:16 +0100470 if (err < 0)
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500471 return err;
472
473 bh = sb_getblk(sb, flex_gd->groups[group].block_bitmap);
Wang Shilongaebf0242013-01-12 16:28:47 -0500474 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500475 return -ENOMEM;
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500476
liang xie5d601252014-05-12 22:06:43 -0400477 BUFFER_TRACE(bh, "get_write_access");
Jan Kara188c2992021-08-16 11:57:04 +0200478 err = ext4_journal_get_write_access(handle, sb, bh,
479 EXT4_JTR_NONE);
Vasily Averincea57942018-11-03 16:22:10 -0400480 if (err) {
481 brelse(bh);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500482 return err;
Vasily Averincea57942018-11-03 16:22:10 -0400483 }
harshadsd77147f2017-10-29 09:38:46 -0400484 ext4_debug("mark block bitmap %#04llx (+%llu/%u)\n",
485 first_cluster, first_cluster - start, count2);
486 ext4_set_bits(bh->b_data, first_cluster - start, count2);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500487
488 err = ext4_handle_dirty_metadata(handle, NULL, bh);
Vasily Averincea57942018-11-03 16:22:10 -0400489 brelse(bh);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500490 if (unlikely(err))
491 return err;
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500492 }
493
494 return 0;
495}
496
497/*
498 * Set up the block and inode bitmaps, and the inode table for the new groups.
499 * This doesn't need to be part of the main transaction, since we are only
500 * changing blocks outside the actual filesystem. We still do journaling to
501 * ensure the recovery is correct in case of a failure just after resize.
502 * If any part of this fails, we simply abort the resize.
503 *
504 * setup_new_flex_group_blocks handles a flex group as follow:
505 * 1. copy super block and GDT, and initialize group tables if necessary.
506 * In this step, we only set bits in blocks bitmaps for blocks taken by
507 * super block and GDT.
508 * 2. allocate group tables in block bitmaps, that is, set bits in block
509 * bitmap for blocks taken by group tables.
510 */
511static int setup_new_flex_group_blocks(struct super_block *sb,
512 struct ext4_new_flex_group_data *flex_gd)
513{
514 int group_table_count[] = {1, 1, EXT4_SB(sb)->s_itb_per_group};
515 ext4_fsblk_t start;
516 ext4_fsblk_t block;
517 struct ext4_sb_info *sbi = EXT4_SB(sb);
518 struct ext4_super_block *es = sbi->s_es;
519 struct ext4_new_group_data *group_data = flex_gd->groups;
520 __u16 *bg_flags = flex_gd->bg_flags;
521 handle_t *handle;
522 ext4_group_t group, count;
523 struct buffer_head *bh = NULL;
524 int reserved_gdb, i, j, err = 0, err2;
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400525 int meta_bg;
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500526
527 BUG_ON(!flex_gd->count || !group_data ||
528 group_data[0].group != sbi->s_groups_count);
529
530 reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400531 meta_bg = ext4_has_feature_meta_bg(sb);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500532
533 /* This transaction may be extended/restarted along the way */
Theodore Ts'o9924a922013-02-08 21:59:22 -0500534 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500535 if (IS_ERR(handle))
536 return PTR_ERR(handle);
537
538 group = group_data[0].group;
539 for (i = 0; i < flex_gd->count; i++, group++) {
540 unsigned long gdblocks;
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400541 ext4_grpblk_t overhead;
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500542
543 gdblocks = ext4_bg_num_gdb(sb, group);
544 start = ext4_group_first_block_no(sb, group);
545
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400546 if (meta_bg == 0 && !ext4_bg_has_super(sb, group))
Yongqiang Yang6df935a2012-09-05 01:25:50 -0400547 goto handle_itb;
548
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400549 if (meta_bg == 1) {
550 ext4_group_t first_group;
551 first_group = ext4_meta_bg_first_group(sb, group);
552 if (first_group != group + 1 &&
553 first_group != group + EXT4_DESC_PER_BLOCK(sb) - 1)
554 goto handle_itb;
555 }
556
557 block = start + ext4_bg_has_super(sb, group);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500558 /* Copy all of the GDT blocks into the backup in this group */
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400559 for (j = 0; j < gdblocks; j++, block++) {
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500560 struct buffer_head *gdb;
561
562 ext4_debug("update backup group %#04llx\n", block);
Jan Kara83448bd2019-11-05 17:44:29 +0100563 err = ext4_resize_ensure_credits_batch(handle, 1);
Jan Karaa4130362019-11-05 17:44:16 +0100564 if (err < 0)
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500565 goto out;
566
567 gdb = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -0500568 if (unlikely(!gdb)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500569 err = -ENOMEM;
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500570 goto out;
571 }
572
liang xie5d601252014-05-12 22:06:43 -0400573 BUFFER_TRACE(gdb, "get_write_access");
Jan Kara188c2992021-08-16 11:57:04 +0200574 err = ext4_journal_get_write_access(handle, sb, gdb,
575 EXT4_JTR_NONE);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500576 if (err) {
577 brelse(gdb);
578 goto out;
579 }
Theodore Ts'o1d0c3922020-02-15 16:40:37 -0500580 memcpy(gdb->b_data, sbi_array_rcu_deref(sbi,
581 s_group_desc, j)->b_data, gdb->b_size);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500582 set_buffer_uptodate(gdb);
583
584 err = ext4_handle_dirty_metadata(handle, NULL, gdb);
585 if (unlikely(err)) {
586 brelse(gdb);
587 goto out;
588 }
589 brelse(gdb);
590 }
591
592 /* Zero out all of the reserved backup group descriptor
593 * table blocks
594 */
595 if (ext4_bg_has_super(sb, group)) {
596 err = sb_issue_zeroout(sb, gdblocks + start + 1,
597 reserved_gdb, GFP_NOFS);
598 if (err)
599 goto out;
600 }
601
Yongqiang Yang6df935a2012-09-05 01:25:50 -0400602handle_itb:
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500603 /* Initialize group tables of the grop @group */
604 if (!(bg_flags[i] & EXT4_BG_INODE_ZEROED))
605 goto handle_bb;
606
607 /* Zero out all of the inode table blocks */
608 block = group_data[i].inode_table;
609 ext4_debug("clear inode table blocks %#04llx -> %#04lx\n",
610 block, sbi->s_itb_per_group);
611 err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group,
612 GFP_NOFS);
613 if (err)
614 goto out;
615
616handle_bb:
617 if (bg_flags[i] & EXT4_BG_BLOCK_UNINIT)
618 goto handle_ib;
619
620 /* Initialize block bitmap of the @group */
621 block = group_data[i].block_bitmap;
Jan Kara83448bd2019-11-05 17:44:29 +0100622 err = ext4_resize_ensure_credits_batch(handle, 1);
Jan Karaa4130362019-11-05 17:44:16 +0100623 if (err < 0)
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500624 goto out;
625
626 bh = bclean(handle, sb, block);
627 if (IS_ERR(bh)) {
628 err = PTR_ERR(bh);
629 goto out;
630 }
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400631 overhead = ext4_group_overhead_blocks(sb, group);
632 if (overhead != 0) {
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500633 ext4_debug("mark backup superblock %#04llx (+0)\n",
634 start);
harshadsd77147f2017-10-29 09:38:46 -0400635 ext4_set_bits(bh->b_data, 0,
636 EXT4_NUM_B2C(sbi, overhead));
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500637 }
harshadsd77147f2017-10-29 09:38:46 -0400638 ext4_mark_bitmap_end(EXT4_B2C(sbi, group_data[i].blocks_count),
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500639 sb->s_blocksize * 8, bh->b_data);
640 err = ext4_handle_dirty_metadata(handle, NULL, bh);
Vasily Averin9e402892018-11-03 16:13:17 -0400641 brelse(bh);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500642 if (err)
643 goto out;
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500644
645handle_ib:
646 if (bg_flags[i] & EXT4_BG_INODE_UNINIT)
647 continue;
648
649 /* Initialize inode bitmap of the @group */
650 block = group_data[i].inode_bitmap;
Jan Kara83448bd2019-11-05 17:44:29 +0100651 err = ext4_resize_ensure_credits_batch(handle, 1);
Jan Karaa4130362019-11-05 17:44:16 +0100652 if (err < 0)
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500653 goto out;
654 /* Mark unused entries in inode bitmap used */
655 bh = bclean(handle, sb, block);
656 if (IS_ERR(bh)) {
657 err = PTR_ERR(bh);
658 goto out;
659 }
660
661 ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb),
662 sb->s_blocksize * 8, bh->b_data);
663 err = ext4_handle_dirty_metadata(handle, NULL, bh);
Vasily Averin9e402892018-11-03 16:13:17 -0400664 brelse(bh);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500665 if (err)
666 goto out;
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500667 }
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500668
669 /* Mark group tables in block bitmap */
670 for (j = 0; j < GROUP_TABLE_COUNT; j++) {
671 count = group_table_count[j];
672 start = (&group_data[0].block_bitmap)[j];
673 block = start;
674 for (i = 1; i < flex_gd->count; i++) {
675 block += group_table_count[j];
676 if (block == (&group_data[i].block_bitmap)[j]) {
677 count += group_table_count[j];
678 continue;
679 }
680 err = set_flexbg_block_bitmap(sb, handle,
harshadsd77147f2017-10-29 09:38:46 -0400681 flex_gd,
682 EXT4_B2C(sbi, start),
683 EXT4_B2C(sbi,
684 start + count
685 - 1));
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500686 if (err)
687 goto out;
688 count = group_table_count[j];
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500689 start = (&group_data[i].block_bitmap)[j];
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500690 block = start;
691 }
692
693 if (count) {
694 err = set_flexbg_block_bitmap(sb, handle,
harshadsd77147f2017-10-29 09:38:46 -0400695 flex_gd,
696 EXT4_B2C(sbi, start),
697 EXT4_B2C(sbi,
698 start + count
699 - 1));
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500700 if (err)
701 goto out;
702 }
703 }
704
705out:
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500706 err2 = ext4_journal_stop(handle);
707 if (err2 && !err)
708 err = err2;
709
710 return err;
711}
712
713/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700714 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
Mingming Cao617ba132006-10-11 01:20:53 -0700715 * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700716 * calling this for the first time. In a sparse filesystem it will be the
717 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
718 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
719 */
Mingming Cao617ba132006-10-11 01:20:53 -0700720static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700721 unsigned *five, unsigned *seven)
722{
723 unsigned *min = three;
724 int mult = 3;
725 unsigned ret;
726
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400727 if (!ext4_has_feature_sparse_super(sb)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700728 ret = *min;
729 *min += 1;
730 return ret;
731 }
732
733 if (*five < *min) {
734 min = five;
735 mult = 5;
736 }
737 if (*seven < *min) {
738 min = seven;
739 mult = 7;
740 }
741
742 ret = *min;
743 *min *= mult;
744
745 return ret;
746}
747
748/*
749 * Check that all of the backup GDT blocks are held in the primary GDT block.
750 * It is assumed that they are stored in group order. Returns the number of
751 * groups in current filesystem that have BACKUPS, or -ve error code.
752 */
753static int verify_reserved_gdb(struct super_block *sb,
Yongqiang Yangc72df9f2012-01-03 23:43:39 -0500754 ext4_group_t end,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700755 struct buffer_head *primary)
756{
Mingming Cao617ba132006-10-11 01:20:53 -0700757 const ext4_fsblk_t blk = primary->b_blocknr;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700758 unsigned three = 1;
759 unsigned five = 5;
760 unsigned seven = 7;
761 unsigned grp;
762 __le32 *p = (__le32 *)primary->b_data;
763 int gdbackups = 0;
764
Mingming Cao617ba132006-10-11 01:20:53 -0700765 while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700766 if (le32_to_cpu(*p++) !=
767 grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
Eric Sandeen12062dd2010-02-15 14:19:27 -0500768 ext4_warning(sb, "reserved GDT %llu"
Mingming Cao2ae02102006-10-11 01:21:11 -0700769 " missing grp %d (%llu)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700770 blk, grp,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700771 grp *
772 (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
773 blk);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700774 return -EINVAL;
775 }
Mingming Cao617ba132006-10-11 01:20:53 -0700776 if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700777 return -EFBIG;
778 }
779
780 return gdbackups;
781}
782
783/*
784 * Called when we need to bring a reserved group descriptor table block into
785 * use from the resize inode. The primary copy of the new GDT block currently
786 * is an indirect block (under the double indirect block in the resize inode).
787 * The new backup GDT blocks will be stored as leaf blocks in this indirect
788 * block, in group order. Even though we know all the block numbers we need,
789 * we check to ensure that the resize inode has actually reserved these blocks.
790 *
791 * Don't need to update the block bitmaps because the blocks are still in use.
792 *
793 * We get all of the error cases out of the way, so that we are sure to not
794 * fail once we start modifying the data on disk, because JBD has no rollback.
795 */
796static int add_new_gdb(handle_t *handle, struct inode *inode,
Yongqiang Yang2f919712011-07-27 21:16:33 -0400797 ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700798{
799 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -0700800 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Yongqiang Yang2f919712011-07-27 21:16:33 -0400801 unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700802 ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500803 struct buffer_head **o_group_desc, **n_group_desc = NULL;
804 struct buffer_head *dind = NULL;
805 struct buffer_head *gdb_bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700806 int gdbackups;
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500807 struct ext4_iloc iloc = { .bh = NULL };
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700808 __le32 *data;
809 int err;
810
811 if (test_opt(sb, DEBUG))
812 printk(KERN_DEBUG
Mingming Cao617ba132006-10-11 01:20:53 -0700813 "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700814 gdb_num);
815
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500816 gdb_bh = ext4_sb_bread(sb, gdblock, 0);
817 if (IS_ERR(gdb_bh))
818 return PTR_ERR(gdb_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700819
Yongqiang Yangc72df9f2012-01-03 23:43:39 -0500820 gdbackups = verify_reserved_gdb(sb, group, gdb_bh);
Yongqiang Yang2f919712011-07-27 21:16:33 -0400821 if (gdbackups < 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700822 err = gdbackups;
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500823 goto errout;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700824 }
825
Mingming Cao617ba132006-10-11 01:20:53 -0700826 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500827 dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0);
828 if (IS_ERR(dind)) {
829 err = PTR_ERR(dind);
830 dind = NULL;
831 goto errout;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700832 }
833
834 data = (__le32 *)dind->b_data;
Mingming Cao617ba132006-10-11 01:20:53 -0700835 if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500836 ext4_warning(sb, "new group %u GDT block %llu not reserved",
Yongqiang Yang2f919712011-07-27 21:16:33 -0400837 group, gdblock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700838 err = -EINVAL;
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500839 goto errout;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700840 }
841
liang xie5d601252014-05-12 22:06:43 -0400842 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Jan Kara188c2992021-08-16 11:57:04 +0200843 err = ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh,
844 EXT4_JTR_NONE);
Theodore Ts'ob4097142011-01-10 12:46:59 -0500845 if (unlikely(err))
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500846 goto errout;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700847
liang xie5d601252014-05-12 22:06:43 -0400848 BUFFER_TRACE(gdb_bh, "get_write_access");
Jan Kara188c2992021-08-16 11:57:04 +0200849 err = ext4_journal_get_write_access(handle, sb, gdb_bh, EXT4_JTR_NONE);
Theodore Ts'ob4097142011-01-10 12:46:59 -0500850 if (unlikely(err))
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500851 goto errout;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700852
liang xie5d601252014-05-12 22:06:43 -0400853 BUFFER_TRACE(dind, "get_write_access");
Jan Kara188c2992021-08-16 11:57:04 +0200854 err = ext4_journal_get_write_access(handle, sb, dind, EXT4_JTR_NONE);
Dinghao Liuc9e87162020-08-29 10:54:02 +0800855 if (unlikely(err)) {
Theodore Ts'ob4097142011-01-10 12:46:59 -0500856 ext4_std_error(sb, err);
Dinghao Liuc9e87162020-08-29 10:54:02 +0800857 goto errout;
858 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700859
Mingming Cao617ba132006-10-11 01:20:53 -0700860 /* ext4_reserve_inode_write() gets a reference on the iloc */
Theodore Ts'ob4097142011-01-10 12:46:59 -0500861 err = ext4_reserve_inode_write(handle, inode, &iloc);
862 if (unlikely(err))
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500863 goto errout;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700864
Theodore Ts'o71b565c2020-01-16 10:08:16 -0500865 n_group_desc = kvmalloc((gdb_num + 1) * sizeof(struct buffer_head *),
866 GFP_KERNEL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700867 if (!n_group_desc) {
868 err = -ENOMEM;
Theodore Ts'of18a5f22011-08-01 08:45:38 -0400869 ext4_warning(sb, "not enough memory for %lu groups",
870 gdb_num + 1);
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500871 goto errout;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700872 }
873
874 /*
875 * Finally, we have all of the possible failures behind us...
876 *
877 * Remove new GDT block from inode double-indirect block and clear out
878 * the new GDT block for use (which also "frees" the backup GDT blocks
879 * from the reserved inode). We don't need to change the bitmaps for
880 * these blocks, because they are marked as in-use from being in the
881 * reserved inode, and will become GDT blocks (primary and backup).
882 */
Mingming Cao617ba132006-10-11 01:20:53 -0700883 data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
Theodore Ts'ob4097142011-01-10 12:46:59 -0500884 err = ext4_handle_dirty_metadata(handle, NULL, dind);
885 if (unlikely(err)) {
886 ext4_std_error(sb, err);
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500887 goto errout;
Theodore Ts'ob4097142011-01-10 12:46:59 -0500888 }
harshadsd77147f2017-10-29 09:38:46 -0400889 inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >>
890 (9 - EXT4_SB(sb)->s_cluster_bits);
Mingming Cao617ba132006-10-11 01:20:53 -0700891 ext4_mark_iloc_dirty(handle, inode, &iloc);
Yongqiang Yang2f919712011-07-27 21:16:33 -0400892 memset(gdb_bh->b_data, 0, sb->s_blocksize);
893 err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
Theodore Ts'ob4097142011-01-10 12:46:59 -0500894 if (unlikely(err)) {
895 ext4_std_error(sb, err);
Pan Bian8c380ab2019-04-25 11:44:15 -0400896 iloc.bh = NULL;
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500897 goto errout;
Theodore Ts'ob4097142011-01-10 12:46:59 -0500898 }
899 brelse(dind);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700900
Theodore Ts'o1d0c3922020-02-15 16:40:37 -0500901 rcu_read_lock();
902 o_group_desc = rcu_dereference(EXT4_SB(sb)->s_group_desc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700903 memcpy(n_group_desc, o_group_desc,
Mingming Cao617ba132006-10-11 01:20:53 -0700904 EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
Theodore Ts'o1d0c3922020-02-15 16:40:37 -0500905 rcu_read_unlock();
Yongqiang Yang2f919712011-07-27 21:16:33 -0400906 n_group_desc[gdb_num] = gdb_bh;
Theodore Ts'o1d0c3922020-02-15 16:40:37 -0500907 rcu_assign_pointer(EXT4_SB(sb)->s_group_desc, n_group_desc);
Mingming Cao617ba132006-10-11 01:20:53 -0700908 EXT4_SB(sb)->s_gdb_count++;
Theodore Ts'o1d0c3922020-02-15 16:40:37 -0500909 ext4_kvfree_array_rcu(o_group_desc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700910
Jan Kara05c2c002020-12-16 11:18:39 +0100911 lock_buffer(EXT4_SB(sb)->s_sbh);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400912 le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
Jan Kara05c2c002020-12-16 11:18:39 +0100913 ext4_superblock_csum_set(sb);
914 unlock_buffer(EXT4_SB(sb)->s_sbh);
Jan Karaa3f5cf12020-12-16 11:18:44 +0100915 err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
Theodore Ts'ob4097142011-01-10 12:46:59 -0500916 if (err)
917 ext4_std_error(sb, err);
Theodore Ts'ob4097142011-01-10 12:46:59 -0500918 return err;
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500919errout:
Al Virob93b41d2014-11-20 12:19:11 -0500920 kvfree(n_group_desc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700921 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700922 brelse(dind);
Yongqiang Yang2f919712011-07-27 21:16:33 -0400923 brelse(gdb_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700924
Mingming Cao617ba132006-10-11 01:20:53 -0700925 ext4_debug("leaving with error %d\n", err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700926 return err;
927}
928
929/*
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400930 * add_new_gdb_meta_bg is the sister of add_new_gdb.
931 */
932static int add_new_gdb_meta_bg(struct super_block *sb,
933 handle_t *handle, ext4_group_t group) {
934 ext4_fsblk_t gdblock;
935 struct buffer_head *gdb_bh;
936 struct buffer_head **o_group_desc, **n_group_desc;
937 unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
938 int err;
939
940 gdblock = ext4_meta_bg_first_block_no(sb, group) +
941 ext4_bg_has_super(sb, group);
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500942 gdb_bh = ext4_sb_bread(sb, gdblock, 0);
943 if (IS_ERR(gdb_bh))
944 return PTR_ERR(gdb_bh);
Theodore Ts'o71b565c2020-01-16 10:08:16 -0500945 n_group_desc = kvmalloc((gdb_num + 1) * sizeof(struct buffer_head *),
946 GFP_KERNEL);
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400947 if (!n_group_desc) {
Vasily Averin61a9c112018-11-03 16:50:08 -0400948 brelse(gdb_bh);
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400949 err = -ENOMEM;
950 ext4_warning(sb, "not enough memory for %lu groups",
951 gdb_num + 1);
952 return err;
953 }
954
Theodore Ts'o1d0c3922020-02-15 16:40:37 -0500955 rcu_read_lock();
956 o_group_desc = rcu_dereference(EXT4_SB(sb)->s_group_desc);
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400957 memcpy(n_group_desc, o_group_desc,
958 EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
Theodore Ts'o1d0c3922020-02-15 16:40:37 -0500959 rcu_read_unlock();
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400960 n_group_desc[gdb_num] = gdb_bh;
Lukas Czernerd64264d2019-03-15 00:15:32 -0400961
962 BUFFER_TRACE(gdb_bh, "get_write_access");
Jan Kara188c2992021-08-16 11:57:04 +0200963 err = ext4_journal_get_write_access(handle, sb, gdb_bh, EXT4_JTR_NONE);
Lukas Czernerd64264d2019-03-15 00:15:32 -0400964 if (err) {
965 kvfree(n_group_desc);
966 brelse(gdb_bh);
967 return err;
968 }
969
Theodore Ts'o1d0c3922020-02-15 16:40:37 -0500970 rcu_assign_pointer(EXT4_SB(sb)->s_group_desc, n_group_desc);
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400971 EXT4_SB(sb)->s_gdb_count++;
Theodore Ts'o1d0c3922020-02-15 16:40:37 -0500972 ext4_kvfree_array_rcu(o_group_desc);
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400973 return err;
974}
975
976/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700977 * Called when we are adding a new group which has a backup copy of each of
978 * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
979 * We need to add these reserved backup GDT blocks to the resize inode, so
980 * that they are kept for future resizing and not allocated to files.
981 *
982 * Each reserved backup GDT block will go into a different indirect block.
983 * The indirect blocks are actually the primary reserved GDT blocks,
984 * so we know in advance what their block numbers are. We only get the
985 * double-indirect block to verify it is pointing to the primary reserved
986 * GDT blocks so we don't overwrite a data block by accident. The reserved
987 * backup GDT blocks are stored in their reserved primary GDT block.
988 */
989static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
Yongqiang Yang668f4dc2011-07-27 21:23:13 -0400990 ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700991{
992 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -0700993 int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
harshadsd77147f2017-10-29 09:38:46 -0400994 int cluster_bits = EXT4_SB(sb)->s_cluster_bits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700995 struct buffer_head **primary;
996 struct buffer_head *dind;
Mingming Cao617ba132006-10-11 01:20:53 -0700997 struct ext4_iloc iloc;
998 ext4_fsblk_t blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700999 __le32 *data, *end;
1000 int gdbackups = 0;
1001 int res, i;
1002 int err;
1003
Kees Cook6da2ec52018-06-12 13:55:00 -07001004 primary = kmalloc_array(reserved_gdb, sizeof(*primary), GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001005 if (!primary)
1006 return -ENOMEM;
1007
Mingming Cao617ba132006-10-11 01:20:53 -07001008 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
Theodore Ts'ofb265c92018-11-25 17:20:31 -05001009 dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0);
1010 if (IS_ERR(dind)) {
1011 err = PTR_ERR(dind);
1012 dind = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001013 goto exit_free;
1014 }
1015
Mingming Cao617ba132006-10-11 01:20:53 -07001016 blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count;
Josef Bacik94460092008-06-06 18:05:52 -04001017 data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count %
1018 EXT4_ADDR_PER_BLOCK(sb));
Mingming Cao617ba132006-10-11 01:20:53 -07001019 end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001020
1021 /* Get each reserved primary GDT block and verify it holds backups */
1022 for (res = 0; res < reserved_gdb; res++, blk++) {
1023 if (le32_to_cpu(*data) != blk) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001024 ext4_warning(sb, "reserved block %llu"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001025 " not at offset %ld",
1026 blk,
1027 (long)(data - (__le32 *)dind->b_data));
1028 err = -EINVAL;
1029 goto exit_bh;
1030 }
Theodore Ts'ofb265c92018-11-25 17:20:31 -05001031 primary[res] = ext4_sb_bread(sb, blk, 0);
1032 if (IS_ERR(primary[res])) {
1033 err = PTR_ERR(primary[res]);
1034 primary[res] = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001035 goto exit_bh;
1036 }
Yongqiang Yangc72df9f2012-01-03 23:43:39 -05001037 gdbackups = verify_reserved_gdb(sb, group, primary[res]);
1038 if (gdbackups < 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001039 brelse(primary[res]);
1040 err = gdbackups;
1041 goto exit_bh;
1042 }
1043 if (++data >= end)
1044 data = (__le32 *)dind->b_data;
1045 }
1046
1047 for (i = 0; i < reserved_gdb; i++) {
liang xie5d601252014-05-12 22:06:43 -04001048 BUFFER_TRACE(primary[i], "get_write_access");
Jan Kara188c2992021-08-16 11:57:04 +02001049 if ((err = ext4_journal_get_write_access(handle, sb, primary[i],
1050 EXT4_JTR_NONE)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001051 goto exit_bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001052 }
1053
Mingming Cao617ba132006-10-11 01:20:53 -07001054 if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001055 goto exit_bh;
1056
1057 /*
1058 * Finally we can add each of the reserved backup GDT blocks from
1059 * the new group to its reserved primary GDT block.
1060 */
Yongqiang Yang668f4dc2011-07-27 21:23:13 -04001061 blk = group * EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001062 for (i = 0; i < reserved_gdb; i++) {
1063 int err2;
1064 data = (__le32 *)primary[i]->b_data;
1065 /* printk("reserving backup %lu[%u] = %lu\n",
1066 primary[i]->b_blocknr, gdbackups,
1067 blk + primary[i]->b_blocknr); */
1068 data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
Frank Mayhar03901312009-01-07 00:06:22 -05001069 err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001070 if (!err)
1071 err = err2;
1072 }
harshadsd77147f2017-10-29 09:38:46 -04001073
1074 inode->i_blocks += reserved_gdb * sb->s_blocksize >> (9 - cluster_bits);
Mingming Cao617ba132006-10-11 01:20:53 -07001075 ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001076
1077exit_bh:
1078 while (--res >= 0)
1079 brelse(primary[res]);
1080 brelse(dind);
1081
1082exit_free:
1083 kfree(primary);
1084
1085 return err;
1086}
1087
1088/*
Mingming Cao617ba132006-10-11 01:20:53 -07001089 * Update the backup copies of the ext4 metadata. These don't need to be part
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001090 * of the main resize transaction, because e2fsck will re-write them if there
1091 * is a problem (basically only OOM will cause a problem). However, we
1092 * _should_ update the backups if possible, in case the primary gets trashed
1093 * for some reason and we need to run e2fsck from a backup superblock. The
1094 * important part is that the new block and inode counts are in the backup
1095 * superblocks, and the location of the new group metadata in the GDT backups.
1096 *
Theodore Ts'o32ed5052009-04-25 22:53:39 -04001097 * We do not need take the s_resize_lock for this, because these
1098 * blocks are not otherwise touched by the filesystem code when it is
1099 * mounted. We don't need to worry about last changing from
1100 * sbi->s_groups_count, because the worst that can happen is that we
1101 * do not copy the full number of backups at this time. The resize
1102 * which changed s_groups_count will backup again.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001103 */
Andy Leiserson904dad42015-10-18 00:36:29 -04001104static void update_backups(struct super_block *sb, sector_t blk_off, char *data,
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001105 int size, int meta_bg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001106{
Mingming Cao617ba132006-10-11 01:20:53 -07001107 struct ext4_sb_info *sbi = EXT4_SB(sb);
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001108 ext4_group_t last;
Mingming Cao617ba132006-10-11 01:20:53 -07001109 const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001110 unsigned three = 1;
1111 unsigned five = 5;
1112 unsigned seven = 7;
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001113 ext4_group_t group = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001114 int rest = sb->s_blocksize - size;
1115 handle_t *handle;
1116 int err = 0, err2;
1117
Theodore Ts'o9924a922013-02-08 21:59:22 -05001118 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001119 if (IS_ERR(handle)) {
1120 group = 1;
1121 err = PTR_ERR(handle);
1122 goto exit_err;
1123 }
1124
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001125 if (meta_bg == 0) {
1126 group = ext4_list_backups(sb, &three, &five, &seven);
1127 last = sbi->s_groups_count;
1128 } else {
Andy Leiserson904dad42015-10-18 00:36:29 -04001129 group = ext4_get_group_number(sb, blk_off) + 1;
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001130 last = (ext4_group_t)(group + EXT4_DESC_PER_BLOCK(sb) - 2);
1131 }
1132
1133 while (group < sbi->s_groups_count) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001134 struct buffer_head *bh;
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001135 ext4_fsblk_t backup_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001136
1137 /* Out of journal space, and can't get more - abort - so sad */
Jan Kara83448bd2019-11-05 17:44:29 +01001138 err = ext4_resize_ensure_credits_batch(handle, 1);
Jan Karaa4130362019-11-05 17:44:16 +01001139 if (err < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001140 break;
1141
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001142 if (meta_bg == 0)
Jan Kara9378c672014-10-30 10:52:57 -04001143 backup_block = ((ext4_fsblk_t)group) * bpg + blk_off;
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001144 else
1145 backup_block = (ext4_group_first_block_no(sb, group) +
1146 ext4_bg_has_super(sb, group));
1147
1148 bh = sb_getblk(sb, backup_block);
Wang Shilongaebf0242013-01-12 16:28:47 -05001149 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001150 err = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001151 break;
1152 }
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001153 ext4_debug("update metadata backup %llu(+%llu)\n",
1154 backup_block, backup_block -
1155 ext4_group_first_block_no(sb, group));
liang xie5d601252014-05-12 22:06:43 -04001156 BUFFER_TRACE(bh, "get_write_access");
Jan Kara188c2992021-08-16 11:57:04 +02001157 if ((err = ext4_journal_get_write_access(handle, sb, bh,
1158 EXT4_JTR_NONE)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001159 break;
1160 lock_buffer(bh);
1161 memcpy(bh->b_data, data, size);
1162 if (rest)
1163 memset(bh->b_data + size, 0, rest);
1164 set_buffer_uptodate(bh);
1165 unlock_buffer(bh);
Theodore Ts'ob4097142011-01-10 12:46:59 -05001166 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1167 if (unlikely(err))
1168 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001169 brelse(bh);
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001170
1171 if (meta_bg == 0)
1172 group = ext4_list_backups(sb, &three, &five, &seven);
1173 else if (group == last)
1174 break;
1175 else
1176 group = last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001177 }
Mingming Cao617ba132006-10-11 01:20:53 -07001178 if ((err2 = ext4_journal_stop(handle)) && !err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001179 err = err2;
1180
1181 /*
1182 * Ugh! Need to have e2fsck write the backup copies. It is too
1183 * late to revert the resize, we shouldn't fail just because of
1184 * the backup copies (they are only needed in case of corruption).
1185 *
1186 * However, if we got here we have a journal problem too, so we
1187 * can't really start a transaction to mark the superblock.
1188 * Chicken out and just set the flag on the hope it will be written
1189 * to disk, and if not - we will simply wait until next fsck.
1190 */
1191exit_err:
1192 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001193 ext4_warning(sb, "can't update backup for group %u (err %d), "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001194 "forcing fsck on next reboot", group, err);
Mingming Cao617ba132006-10-11 01:20:53 -07001195 sbi->s_mount_state &= ~EXT4_VALID_FS;
1196 sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001197 mark_buffer_dirty(sbi->s_sbh);
1198 }
1199}
1200
Yongqiang Yangbb08c1e2012-01-03 23:20:50 -05001201/*
1202 * ext4_add_new_descs() adds @count group descriptor of groups
1203 * starting at @group
1204 *
1205 * @handle: journal handle
1206 * @sb: super block
1207 * @group: the group no. of the first group desc to be added
1208 * @resize_inode: the resize inode
1209 * @count: number of group descriptors to be added
1210 */
1211static int ext4_add_new_descs(handle_t *handle, struct super_block *sb,
1212 ext4_group_t group, struct inode *resize_inode,
1213 ext4_group_t count)
1214{
1215 struct ext4_sb_info *sbi = EXT4_SB(sb);
1216 struct ext4_super_block *es = sbi->s_es;
1217 struct buffer_head *gdb_bh;
1218 int i, gdb_off, gdb_num, err = 0;
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001219 int meta_bg;
Yongqiang Yangbb08c1e2012-01-03 23:20:50 -05001220
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001221 meta_bg = ext4_has_feature_meta_bg(sb);
Yongqiang Yangbb08c1e2012-01-03 23:20:50 -05001222 for (i = 0; i < count; i++, group++) {
1223 int reserved_gdb = ext4_bg_has_super(sb, group) ?
1224 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
1225
1226 gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
1227 gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
1228
1229 /*
1230 * We will only either add reserved group blocks to a backup group
1231 * or remove reserved blocks for the first group in a new group block.
1232 * Doing both would be mean more complex code, and sane people don't
1233 * use non-sparse filesystems anymore. This is already checked above.
1234 */
1235 if (gdb_off) {
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05001236 gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc,
1237 gdb_num);
liang xie5d601252014-05-12 22:06:43 -04001238 BUFFER_TRACE(gdb_bh, "get_write_access");
Jan Kara188c2992021-08-16 11:57:04 +02001239 err = ext4_journal_get_write_access(handle, sb, gdb_bh,
1240 EXT4_JTR_NONE);
Yongqiang Yangbb08c1e2012-01-03 23:20:50 -05001241
1242 if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group))
1243 err = reserve_backup_gdb(handle, resize_inode, group);
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001244 } else if (meta_bg != 0) {
1245 err = add_new_gdb_meta_bg(sb, handle, group);
1246 } else {
Yongqiang Yangbb08c1e2012-01-03 23:20:50 -05001247 err = add_new_gdb(handle, resize_inode, group);
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001248 }
Yongqiang Yangbb08c1e2012-01-03 23:20:50 -05001249 if (err)
1250 break;
1251 }
1252 return err;
1253}
1254
Darrick J. Wong41a246d2012-04-29 18:33:10 -04001255static struct buffer_head *ext4_get_bitmap(struct super_block *sb, __u64 block)
1256{
1257 struct buffer_head *bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05001258 if (unlikely(!bh))
Darrick J. Wong41a246d2012-04-29 18:33:10 -04001259 return NULL;
Dmitry Monakhov7f1468d2012-09-25 23:19:25 -04001260 if (!bh_uptodate_or_lock(bh)) {
zhangyi (F)2d069c02020-09-24 15:33:33 +08001261 if (ext4_read_bh(bh, 0, NULL) < 0) {
Dmitry Monakhov7f1468d2012-09-25 23:19:25 -04001262 brelse(bh);
1263 return NULL;
1264 }
Darrick J. Wong41a246d2012-04-29 18:33:10 -04001265 }
Darrick J. Wong41a246d2012-04-29 18:33:10 -04001266
1267 return bh;
1268}
1269
1270static int ext4_set_bitmap_checksums(struct super_block *sb,
1271 ext4_group_t group,
1272 struct ext4_group_desc *gdp,
1273 struct ext4_new_group_data *group_data)
1274{
1275 struct buffer_head *bh;
1276
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04001277 if (!ext4_has_metadata_csum(sb))
Darrick J. Wong41a246d2012-04-29 18:33:10 -04001278 return 0;
1279
1280 bh = ext4_get_bitmap(sb, group_data->inode_bitmap);
1281 if (!bh)
1282 return -EIO;
1283 ext4_inode_bitmap_csum_set(sb, group, gdp, bh,
1284 EXT4_INODES_PER_GROUP(sb) / 8);
1285 brelse(bh);
1286
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -04001287 bh = ext4_get_bitmap(sb, group_data->block_bitmap);
1288 if (!bh)
1289 return -EIO;
Tao Ma79f1ba42012-10-22 00:34:32 -04001290 ext4_block_bitmap_csum_set(sb, group, gdp, bh);
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -04001291 brelse(bh);
1292
Darrick J. Wong41a246d2012-04-29 18:33:10 -04001293 return 0;
1294}
1295
Yongqiang Yang083f5b22012-01-03 23:37:31 -05001296/*
1297 * ext4_setup_new_descs() will set up the group descriptor descriptors of a flex bg
1298 */
1299static int ext4_setup_new_descs(handle_t *handle, struct super_block *sb,
1300 struct ext4_new_flex_group_data *flex_gd)
1301{
1302 struct ext4_new_group_data *group_data = flex_gd->groups;
1303 struct ext4_group_desc *gdp;
1304 struct ext4_sb_info *sbi = EXT4_SB(sb);
1305 struct buffer_head *gdb_bh;
1306 ext4_group_t group;
1307 __u16 *bg_flags = flex_gd->bg_flags;
1308 int i, gdb_off, gdb_num, err = 0;
harshadsd77147f2017-10-29 09:38:46 -04001309
Yongqiang Yang083f5b22012-01-03 23:37:31 -05001310
1311 for (i = 0; i < flex_gd->count; i++, group_data++, bg_flags++) {
1312 group = group_data->group;
1313
1314 gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
1315 gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
1316
1317 /*
1318 * get_write_access() has been called on gdb_bh by ext4_add_new_desc().
1319 */
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05001320 gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc, gdb_num);
Yongqiang Yang083f5b22012-01-03 23:37:31 -05001321 /* Update group descriptor block for new group */
Theodore Ts'o2716b802012-05-28 17:47:52 -04001322 gdp = (struct ext4_group_desc *)(gdb_bh->b_data +
Yongqiang Yang083f5b22012-01-03 23:37:31 -05001323 gdb_off * EXT4_DESC_SIZE(sb));
1324
1325 memset(gdp, 0, EXT4_DESC_SIZE(sb));
1326 ext4_block_bitmap_set(sb, gdp, group_data->block_bitmap);
1327 ext4_inode_bitmap_set(sb, gdp, group_data->inode_bitmap);
Darrick J. Wong41a246d2012-04-29 18:33:10 -04001328 err = ext4_set_bitmap_checksums(sb, group, gdp, group_data);
1329 if (err) {
1330 ext4_std_error(sb, err);
1331 break;
1332 }
1333
Yongqiang Yang083f5b22012-01-03 23:37:31 -05001334 ext4_inode_table_set(sb, gdp, group_data->inode_table);
1335 ext4_free_group_clusters_set(sb, gdp,
harshadsd77147f2017-10-29 09:38:46 -04001336 group_data->free_clusters_count);
Yongqiang Yang083f5b22012-01-03 23:37:31 -05001337 ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
Theodore Ts'o93f90522012-09-12 14:32:42 -04001338 if (ext4_has_group_desc_csum(sb))
1339 ext4_itable_unused_set(sb, gdp,
1340 EXT4_INODES_PER_GROUP(sb));
Yongqiang Yang083f5b22012-01-03 23:37:31 -05001341 gdp->bg_flags = cpu_to_le16(*bg_flags);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04001342 ext4_group_desc_csum_set(sb, group, gdp);
Yongqiang Yang083f5b22012-01-03 23:37:31 -05001343
1344 err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
1345 if (unlikely(err)) {
1346 ext4_std_error(sb, err);
1347 break;
1348 }
1349
1350 /*
1351 * We can allocate memory for mb_alloc based on the new group
1352 * descriptor
1353 */
1354 err = ext4_mb_add_groupinfo(sb, group, gdp);
1355 if (err)
1356 break;
1357 }
1358 return err;
1359}
1360
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001361/*
1362 * ext4_update_super() updates the super block so that the newly added
1363 * groups can be seen by the filesystem.
1364 *
1365 * @sb: super block
1366 * @flex_gd: new added groups
1367 */
1368static void ext4_update_super(struct super_block *sb,
1369 struct ext4_new_flex_group_data *flex_gd)
1370{
1371 ext4_fsblk_t blocks_count = 0;
1372 ext4_fsblk_t free_blocks = 0;
1373 ext4_fsblk_t reserved_blocks = 0;
1374 struct ext4_new_group_data *group_data = flex_gd->groups;
1375 struct ext4_sb_info *sbi = EXT4_SB(sb);
1376 struct ext4_super_block *es = sbi->s_es;
Theodore Ts'o8a991842012-07-22 20:23:31 -04001377 int i;
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001378
1379 BUG_ON(flex_gd->count == 0 || group_data == NULL);
1380 /*
1381 * Make the new blocks and inodes valid next. We do this before
1382 * increasing the group count so that once the group is enabled,
1383 * all of its blocks and inodes are already valid.
1384 *
1385 * We always allocate group-by-group, then block-by-block or
1386 * inode-by-inode within a group, so enabling these
1387 * blocks/inodes before the group is live won't actually let us
1388 * allocate the new space yet.
1389 */
1390 for (i = 0; i < flex_gd->count; i++) {
1391 blocks_count += group_data[i].blocks_count;
harshadsd77147f2017-10-29 09:38:46 -04001392 free_blocks += EXT4_C2B(sbi, group_data[i].free_clusters_count);
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001393 }
1394
1395 reserved_blocks = ext4_r_blocks_count(es) * 100;
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001396 reserved_blocks = div64_u64(reserved_blocks, ext4_blocks_count(es));
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001397 reserved_blocks *= blocks_count;
1398 do_div(reserved_blocks, 100);
1399
Jan Kara05c2c002020-12-16 11:18:39 +01001400 lock_buffer(sbi->s_sbh);
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001401 ext4_blocks_count_set(es, ext4_blocks_count(es) + blocks_count);
Darrick J. Wong636d7e2e2012-03-20 15:46:11 -04001402 ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + free_blocks);
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001403 le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb) *
1404 flex_gd->count);
Darrick J. Wong636d7e2e2012-03-20 15:46:11 -04001405 le32_add_cpu(&es->s_free_inodes_count, EXT4_INODES_PER_GROUP(sb) *
1406 flex_gd->count);
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001407
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001408 ext4_debug("free blocks count %llu", ext4_free_blocks_count(es));
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001409 /*
1410 * We need to protect s_groups_count against other CPUs seeing
1411 * inconsistent state in the superblock.
1412 *
1413 * The precise rules we use are:
1414 *
1415 * * Writers must perform a smp_wmb() after updating all
1416 * dependent data and before modifying the groups count
1417 *
1418 * * Readers must perform an smp_rmb() after reading the groups
1419 * count and before reading any dependent data.
1420 *
1421 * NB. These rules can be relaxed when checking the group count
1422 * while freeing data, as we can only allocate from a block
1423 * group after serialising against the group count, and we can
1424 * only then free after serialising in turn against that
1425 * allocation.
1426 */
1427 smp_wmb();
1428
1429 /* Update the global fs size fields */
1430 sbi->s_groups_count += flex_gd->count;
Theodore Ts'oc5c72d82013-04-21 20:19:43 -04001431 sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
1432 (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001433
1434 /* Update the reserved block counts only once the new group is
1435 * active. */
1436 ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
1437 reserved_blocks);
Jan Kara05c2c002020-12-16 11:18:39 +01001438 ext4_superblock_csum_set(sb);
1439 unlock_buffer(sbi->s_sbh);
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001440
1441 /* Update the free space counts */
1442 percpu_counter_add(&sbi->s_freeclusters_counter,
Lukas Czerner810da242013-03-02 17:18:58 -05001443 EXT4_NUM_B2C(sbi, free_blocks));
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001444 percpu_counter_add(&sbi->s_freeinodes_counter,
1445 EXT4_INODES_PER_GROUP(sb) * flex_gd->count);
1446
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001447 ext4_debug("free blocks count %llu",
1448 percpu_counter_read(&sbi->s_freeclusters_counter));
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001449 if (ext4_has_feature_flex_bg(sb) && sbi->s_log_groups_per_flex) {
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001450 ext4_group_t flex_group;
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08001451 struct flex_groups *fg;
1452
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001453 flex_group = ext4_flex_group(sbi, group_data[0].group);
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08001454 fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04001455 atomic64_add(EXT4_NUM_B2C(sbi, free_blocks),
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08001456 &fg->free_clusters);
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001457 atomic_add(EXT4_INODES_PER_GROUP(sb) * flex_gd->count,
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08001458 &fg->free_inodes);
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001459 }
1460
Theodore Ts'o952fc182012-07-09 16:27:05 -04001461 /*
1462 * Update the fs overhead information
1463 */
1464 ext4_calculate_overhead(sb);
1465
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001466 if (test_opt(sb, DEBUG))
1467 printk(KERN_DEBUG "EXT4-fs: added group %u:"
1468 "%llu blocks(%llu free %llu reserved)\n", flex_gd->count,
1469 blocks_count, free_blocks, reserved_blocks);
1470}
1471
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001472/* Add a flex group to an fs. Ensure we handle all possible error conditions
1473 * _before_ we start modifying the filesystem, because we cannot abort the
1474 * transaction and not have it write the data to disk.
1475 */
1476static int ext4_flex_group_add(struct super_block *sb,
1477 struct inode *resize_inode,
1478 struct ext4_new_flex_group_data *flex_gd)
1479{
1480 struct ext4_sb_info *sbi = EXT4_SB(sb);
1481 struct ext4_super_block *es = sbi->s_es;
1482 ext4_fsblk_t o_blocks_count;
1483 ext4_grpblk_t last;
1484 ext4_group_t group;
1485 handle_t *handle;
1486 unsigned reserved_gdb;
1487 int err = 0, err2 = 0, credit;
1488
1489 BUG_ON(!flex_gd->count || !flex_gd->groups || !flex_gd->bg_flags);
1490
1491 reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks);
1492 o_blocks_count = ext4_blocks_count(es);
1493 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1494 BUG_ON(last);
1495
1496 err = setup_new_flex_group_blocks(sb, flex_gd);
1497 if (err)
1498 goto exit;
1499 /*
1500 * We will always be modifying at least the superblock and GDT
Jan Kara2c869b22015-05-02 23:58:32 -04001501 * blocks. If we are adding a group past the last current GDT block,
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001502 * we will also modify the inode and the dindirect block. If we
1503 * are adding a group with superblock/GDT backups we will also
1504 * modify each of the reserved GDT dindirect blocks.
1505 */
Jan Kara2c869b22015-05-02 23:58:32 -04001506 credit = 3; /* sb, resize inode, resize inode dindirect */
1507 /* GDT blocks */
1508 credit += 1 + DIV_ROUND_UP(flex_gd->count, EXT4_DESC_PER_BLOCK(sb));
1509 credit += reserved_gdb; /* Reserved GDT dindirect blocks */
Theodore Ts'o9924a922013-02-08 21:59:22 -05001510 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credit);
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001511 if (IS_ERR(handle)) {
1512 err = PTR_ERR(handle);
1513 goto exit;
1514 }
1515
liang xie5d601252014-05-12 22:06:43 -04001516 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
Jan Kara188c2992021-08-16 11:57:04 +02001517 err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh,
1518 EXT4_JTR_NONE);
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001519 if (err)
1520 goto exit_journal;
1521
1522 group = flex_gd->groups[0].group;
Jun Piao49598e02018-01-11 13:17:49 -05001523 BUG_ON(group != sbi->s_groups_count);
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001524 err = ext4_add_new_descs(handle, sb, group,
1525 resize_inode, flex_gd->count);
1526 if (err)
1527 goto exit_journal;
1528
1529 err = ext4_setup_new_descs(handle, sb, flex_gd);
1530 if (err)
1531 goto exit_journal;
1532
1533 ext4_update_super(sb, flex_gd);
1534
Jan Karaa3f5cf12020-12-16 11:18:44 +01001535 err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001536
1537exit_journal:
1538 err2 = ext4_journal_stop(handle);
1539 if (!err)
1540 err = err2;
1541
1542 if (!err) {
Yongqiang Yang2ebd1702012-09-05 01:27:50 -04001543 int gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
1544 int gdb_num_end = ((group + flex_gd->count - 1) /
1545 EXT4_DESC_PER_BLOCK(sb));
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001546 int meta_bg = ext4_has_feature_meta_bg(sb);
Tao Ma0acdb882012-09-26 00:08:57 -04001547 sector_t old_gdb = 0;
Yongqiang Yang2ebd1702012-09-05 01:27:50 -04001548
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001549 update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001550 sizeof(struct ext4_super_block), 0);
Yongqiang Yang2ebd1702012-09-05 01:27:50 -04001551 for (; gdb_num <= gdb_num_end; gdb_num++) {
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001552 struct buffer_head *gdb_bh;
Yongqiang Yang2ebd1702012-09-05 01:27:50 -04001553
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05001554 gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc,
1555 gdb_num);
Tao Ma0acdb882012-09-26 00:08:57 -04001556 if (old_gdb == gdb_bh->b_blocknr)
1557 continue;
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001558 update_backups(sb, gdb_bh->b_blocknr, gdb_bh->b_data,
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001559 gdb_bh->b_size, meta_bg);
Tao Ma0acdb882012-09-26 00:08:57 -04001560 old_gdb = gdb_bh->b_blocknr;
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001561 }
1562 }
1563exit:
1564 return err;
1565}
1566
Yongqiang Yang19c52462012-01-04 17:09:44 -05001567static int ext4_setup_next_flex_gd(struct super_block *sb,
1568 struct ext4_new_flex_group_data *flex_gd,
1569 ext4_fsblk_t n_blocks_count,
1570 unsigned long flexbg_size)
1571{
harshadsd77147f2017-10-29 09:38:46 -04001572 struct ext4_sb_info *sbi = EXT4_SB(sb);
1573 struct ext4_super_block *es = sbi->s_es;
Yongqiang Yang19c52462012-01-04 17:09:44 -05001574 struct ext4_new_group_data *group_data = flex_gd->groups;
1575 ext4_fsblk_t o_blocks_count;
1576 ext4_group_t n_group;
1577 ext4_group_t group;
1578 ext4_group_t last_group;
1579 ext4_grpblk_t last;
harshadsd77147f2017-10-29 09:38:46 -04001580 ext4_grpblk_t clusters_per_group;
Yongqiang Yang19c52462012-01-04 17:09:44 -05001581 unsigned long i;
1582
harshadsd77147f2017-10-29 09:38:46 -04001583 clusters_per_group = EXT4_CLUSTERS_PER_GROUP(sb);
Yongqiang Yang19c52462012-01-04 17:09:44 -05001584
1585 o_blocks_count = ext4_blocks_count(es);
1586
1587 if (o_blocks_count == n_blocks_count)
1588 return 0;
1589
1590 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1591 BUG_ON(last);
1592 ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &last);
1593
1594 last_group = group | (flexbg_size - 1);
1595 if (last_group > n_group)
1596 last_group = n_group;
1597
1598 flex_gd->count = last_group - group + 1;
1599
1600 for (i = 0; i < flex_gd->count; i++) {
1601 int overhead;
1602
1603 group_data[i].group = group + i;
harshadsd77147f2017-10-29 09:38:46 -04001604 group_data[i].blocks_count = EXT4_BLOCKS_PER_GROUP(sb);
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001605 overhead = ext4_group_overhead_blocks(sb, group + i);
harshadsd77147f2017-10-29 09:38:46 -04001606 group_data[i].mdata_blocks = overhead;
1607 group_data[i].free_clusters_count = EXT4_CLUSTERS_PER_GROUP(sb);
Theodore Ts'o7f511862013-01-13 08:41:45 -05001608 if (ext4_has_group_desc_csum(sb)) {
Yongqiang Yang19c52462012-01-04 17:09:44 -05001609 flex_gd->bg_flags[i] = EXT4_BG_BLOCK_UNINIT |
1610 EXT4_BG_INODE_UNINIT;
Theodore Ts'o7f511862013-01-13 08:41:45 -05001611 if (!test_opt(sb, INIT_INODE_TABLE))
1612 flex_gd->bg_flags[i] |= EXT4_BG_INODE_ZEROED;
1613 } else
Yongqiang Yang19c52462012-01-04 17:09:44 -05001614 flex_gd->bg_flags[i] = EXT4_BG_INODE_ZEROED;
1615 }
1616
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04001617 if (last_group == n_group && ext4_has_group_desc_csum(sb))
Yongqiang Yang19c52462012-01-04 17:09:44 -05001618 /* We need to initialize block bitmap of last group. */
1619 flex_gd->bg_flags[i - 1] &= ~EXT4_BG_BLOCK_UNINIT;
1620
harshadsd77147f2017-10-29 09:38:46 -04001621 if ((last_group == n_group) && (last != clusters_per_group - 1)) {
1622 group_data[i - 1].blocks_count = EXT4_C2B(sbi, last + 1);
1623 group_data[i - 1].free_clusters_count -= clusters_per_group -
1624 last - 1;
Yongqiang Yang19c52462012-01-04 17:09:44 -05001625 }
1626
1627 return 1;
1628}
1629
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001630/* Add group descriptor data to an existing or new group descriptor block.
1631 * Ensure we handle all possible error conditions _before_ we start modifying
1632 * the filesystem, because we cannot abort the transaction and not have it
1633 * write the data to disk.
1634 *
1635 * If we are on a GDT block boundary, we need to get the reserved GDT block.
1636 * Otherwise, we may need to add backup GDT blocks for a sparse group.
1637 *
1638 * We only need to hold the superblock lock while we are actually adding
1639 * in the new group's counts to the superblock. Prior to that we have
1640 * not really "added" the group at all. We re-check that we are still
1641 * adding in the last group in case things have changed since verifying.
1642 */
Mingming Cao617ba132006-10-11 01:20:53 -07001643int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001644{
Yongqiang Yang61f296c2012-01-04 17:09:50 -05001645 struct ext4_new_flex_group_data flex_gd;
Mingming Cao617ba132006-10-11 01:20:53 -07001646 struct ext4_sb_info *sbi = EXT4_SB(sb);
1647 struct ext4_super_block *es = sbi->s_es;
1648 int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001649 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001650 struct inode *inode = NULL;
Jon Ernst03b40e32013-06-17 08:56:26 -04001651 int gdb_off;
Yongqiang Yang61f296c2012-01-04 17:09:50 -05001652 int err;
1653 __u16 bg_flags = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001654
Mingming Cao617ba132006-10-11 01:20:53 -07001655 gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001656
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001657 if (gdb_off == 0 && !ext4_has_feature_sparse_super(sb)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001658 ext4_warning(sb, "Can't resize non-sparse filesystem further");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001659 return -EPERM;
1660 }
1661
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001662 if (ext4_blocks_count(es) + input->blocks_count <
1663 ext4_blocks_count(es)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001664 ext4_warning(sb, "blocks_count overflow");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001665 return -EINVAL;
1666 }
1667
Mingming Cao617ba132006-10-11 01:20:53 -07001668 if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001669 le32_to_cpu(es->s_inodes_count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001670 ext4_warning(sb, "inodes_count overflow");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001671 return -EINVAL;
1672 }
1673
1674 if (reserved_gdb || gdb_off == 0) {
ruippan (潘睿)e647e292018-12-04 01:04:12 -05001675 if (!ext4_has_feature_resize_inode(sb) ||
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001676 !le16_to_cpu(es->s_reserved_gdt_blocks)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001677 ext4_warning(sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001678 "No reserved GDT blocks, can't resize");
1679 return -EPERM;
1680 }
Theodore Ts'o8a363972018-12-19 12:29:13 -05001681 inode = ext4_iget(sb, EXT4_RESIZE_INO, EXT4_IGET_SPECIAL);
David Howells1d1fe1e2008-02-07 00:15:37 -08001682 if (IS_ERR(inode)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001683 ext4_warning(sb, "Error opening resize inode");
David Howells1d1fe1e2008-02-07 00:15:37 -08001684 return PTR_ERR(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001685 }
1686 }
1687
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001688
Yongqiang Yang61f296c2012-01-04 17:09:50 -05001689 err = verify_group_input(sb, input);
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -04001690 if (err)
Yongqiang Yang61f296c2012-01-04 17:09:50 -05001691 goto out;
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04001692
Theodore Ts'o117fff12012-09-05 01:29:50 -04001693 err = ext4_alloc_flex_bg_array(sb, input->group + 1);
1694 if (err)
Theodore Ts'o7f511862013-01-13 08:41:45 -05001695 goto out;
Theodore Ts'o117fff12012-09-05 01:29:50 -04001696
Theodore Ts'o28623c22012-09-05 01:31:50 -04001697 err = ext4_mb_alloc_groupinfo(sb, input->group + 1);
1698 if (err)
1699 goto out;
1700
Yongqiang Yang61f296c2012-01-04 17:09:50 -05001701 flex_gd.count = 1;
1702 flex_gd.groups = input;
1703 flex_gd.bg_flags = &bg_flags;
1704 err = ext4_flex_group_add(sb, inode, &flex_gd);
1705out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001706 iput(inode);
1707 return err;
Mingming Cao617ba132006-10-11 01:20:53 -07001708} /* ext4_group_add */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001709
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001710/*
Yongqiang Yang18e31432012-01-03 23:18:50 -05001711 * extend a group without checking assuming that checking has been done.
1712 */
1713static int ext4_group_extend_no_check(struct super_block *sb,
1714 ext4_fsblk_t o_blocks_count, ext4_grpblk_t add)
1715{
1716 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
1717 handle_t *handle;
1718 int err = 0, err2;
1719
1720 /* We will update the superblock, one block bitmap, and
1721 * one group descriptor via ext4_group_add_blocks().
1722 */
Theodore Ts'o9924a922013-02-08 21:59:22 -05001723 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, 3);
Yongqiang Yang18e31432012-01-03 23:18:50 -05001724 if (IS_ERR(handle)) {
1725 err = PTR_ERR(handle);
1726 ext4_warning(sb, "error %d on journal start", err);
1727 return err;
1728 }
1729
liang xie5d601252014-05-12 22:06:43 -04001730 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Jan Kara188c2992021-08-16 11:57:04 +02001731 err = ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh,
1732 EXT4_JTR_NONE);
Yongqiang Yang18e31432012-01-03 23:18:50 -05001733 if (err) {
1734 ext4_warning(sb, "error %d on journal write access", err);
1735 goto errout;
1736 }
1737
Jan Kara05c2c002020-12-16 11:18:39 +01001738 lock_buffer(EXT4_SB(sb)->s_sbh);
Yongqiang Yang18e31432012-01-03 23:18:50 -05001739 ext4_blocks_count_set(es, o_blocks_count + add);
Darrick J. Wong636d7e2e2012-03-20 15:46:11 -04001740 ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + add);
Jan Kara05c2c002020-12-16 11:18:39 +01001741 ext4_superblock_csum_set(sb);
1742 unlock_buffer(EXT4_SB(sb)->s_sbh);
Yongqiang Yang18e31432012-01-03 23:18:50 -05001743 ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
1744 o_blocks_count + add);
1745 /* We add the blocks to the bitmap and set the group need init bit */
1746 err = ext4_group_add_blocks(handle, sb, o_blocks_count, add);
1747 if (err)
1748 goto errout;
Jan Karaa3f5cf12020-12-16 11:18:44 +01001749 ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
Yongqiang Yang18e31432012-01-03 23:18:50 -05001750 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
1751 o_blocks_count + add);
1752errout:
1753 err2 = ext4_journal_stop(handle);
1754 if (err2 && !err)
1755 err = err2;
1756
1757 if (!err) {
1758 if (test_opt(sb, DEBUG))
1759 printk(KERN_DEBUG "EXT4-fs: extended group to %llu "
1760 "blocks\n", ext4_blocks_count(es));
Maarten ter Huurne6ca792e2013-07-01 08:12:08 -04001761 update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr,
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001762 (char *)es, sizeof(struct ext4_super_block), 0);
Yongqiang Yang18e31432012-01-03 23:18:50 -05001763 }
1764 return err;
1765}
1766
1767/*
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001768 * Extend the filesystem to the new number of blocks specified. This entry
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001769 * point is only used to extend the current filesystem to the end of the last
1770 * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
1771 * for emergencies (because it has no dependencies on reserved blocks).
1772 *
Mingming Cao617ba132006-10-11 01:20:53 -07001773 * If we _really_ wanted, we could use default values to call ext4_group_add()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001774 * allow the "remount" trick to work for arbitrary resizing, assuming enough
1775 * GDT blocks are reserved to grow to the desired size.
1776 */
Mingming Cao617ba132006-10-11 01:20:53 -07001777int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
1778 ext4_fsblk_t n_blocks_count)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001779{
Mingming Cao617ba132006-10-11 01:20:53 -07001780 ext4_fsblk_t o_blocks_count;
Mingming Cao617ba132006-10-11 01:20:53 -07001781 ext4_grpblk_t last;
1782 ext4_grpblk_t add;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001783 struct buffer_head *bh;
Yongqiang Yangd89651c2012-01-04 17:09:48 -05001784 int err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04001785 ext4_group_t group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001786
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001787 o_blocks_count = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001788
1789 if (test_opt(sb, DEBUG))
Theodore Ts'o92b97812012-03-19 23:41:49 -04001790 ext4_msg(sb, KERN_DEBUG,
1791 "extending last group from %llu to %llu blocks",
1792 o_blocks_count, n_blocks_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001793
1794 if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
1795 return 0;
1796
1797 if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Theodore Ts'o92b97812012-03-19 23:41:49 -04001798 ext4_msg(sb, KERN_ERR,
1799 "filesystem too large to resize to %llu blocks safely",
1800 n_blocks_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001801 return -EINVAL;
1802 }
1803
1804 if (n_blocks_count < o_blocks_count) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001805 ext4_warning(sb, "can't shrink FS - resize aborted");
Yongqiang Yang8f82f842011-07-26 21:35:44 -04001806 return -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001807 }
1808
1809 /* Handle the remaining blocks in the last group only. */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04001810 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001811
1812 if (last == 0) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001813 ext4_warning(sb, "need to use ext2online to resize further");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001814 return -EPERM;
1815 }
1816
Mingming Cao617ba132006-10-11 01:20:53 -07001817 add = EXT4_BLOCKS_PER_GROUP(sb) - last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001818
1819 if (o_blocks_count + add < o_blocks_count) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001820 ext4_warning(sb, "blocks_count overflow");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001821 return -EINVAL;
1822 }
1823
1824 if (o_blocks_count + add > n_blocks_count)
1825 add = n_blocks_count - o_blocks_count;
1826
1827 if (o_blocks_count + add < n_blocks_count)
Eric Sandeen12062dd2010-02-15 14:19:27 -05001828 ext4_warning(sb, "will only finish group (%llu blocks, %u new)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001829 o_blocks_count + add, add);
1830
1831 /* See if the device is actually as big as what was requested */
zhangyi (F)0a846f42020-09-24 15:33:36 +08001832 bh = ext4_sb_bread(sb, o_blocks_count + add - 1, 0);
1833 if (IS_ERR(bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001834 ext4_warning(sb, "can't read last block, resize aborted");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001835 return -ENOSPC;
1836 }
1837 brelse(bh);
1838
Yongqiang Yangd89651c2012-01-04 17:09:48 -05001839 err = ext4_group_extend_no_check(sb, o_blocks_count, add);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001840 return err;
Mingming Cao617ba132006-10-11 01:20:53 -07001841} /* ext4_group_extend */
Yongqiang Yang19c52462012-01-04 17:09:44 -05001842
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001843
1844static int num_desc_blocks(struct super_block *sb, ext4_group_t groups)
1845{
1846 return (groups + EXT4_DESC_PER_BLOCK(sb) - 1) / EXT4_DESC_PER_BLOCK(sb);
1847}
1848
1849/*
1850 * Release the resize inode and drop the resize_inode feature if there
1851 * are no more reserved gdt blocks, and then convert the file system
1852 * to enable meta_bg
1853 */
1854static int ext4_convert_meta_bg(struct super_block *sb, struct inode *inode)
1855{
1856 handle_t *handle;
1857 struct ext4_sb_info *sbi = EXT4_SB(sb);
1858 struct ext4_super_block *es = sbi->s_es;
Theodore Ts'o59e31c12012-09-19 00:55:56 -04001859 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001860 ext4_fsblk_t nr;
1861 int i, ret, err = 0;
1862 int credits = 1;
1863
1864 ext4_msg(sb, KERN_INFO, "Converting file system to meta_bg");
Theodore Ts'o59e31c12012-09-19 00:55:56 -04001865 if (inode) {
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001866 if (es->s_reserved_gdt_blocks) {
1867 ext4_error(sb, "Unexpected non-zero "
1868 "s_reserved_gdt_blocks");
1869 return -EPERM;
1870 }
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001871
1872 /* Do a quick sanity check of the resize inode */
harshadsd77147f2017-10-29 09:38:46 -04001873 if (inode->i_blocks != 1 << (inode->i_blkbits -
1874 (9 - sbi->s_cluster_bits)))
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001875 goto invalid_resize_inode;
1876 for (i = 0; i < EXT4_N_BLOCKS; i++) {
1877 if (i == EXT4_DIND_BLOCK) {
1878 if (ei->i_data[i])
1879 continue;
1880 else
1881 goto invalid_resize_inode;
1882 }
1883 if (ei->i_data[i])
1884 goto invalid_resize_inode;
1885 }
1886 credits += 3; /* block bitmap, bg descriptor, resize inode */
1887 }
1888
Theodore Ts'o9924a922013-02-08 21:59:22 -05001889 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credits);
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001890 if (IS_ERR(handle))
1891 return PTR_ERR(handle);
1892
liang xie5d601252014-05-12 22:06:43 -04001893 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
Jan Kara188c2992021-08-16 11:57:04 +02001894 err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh,
1895 EXT4_JTR_NONE);
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001896 if (err)
1897 goto errout;
1898
Jan Kara05c2c002020-12-16 11:18:39 +01001899 lock_buffer(sbi->s_sbh);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001900 ext4_clear_feature_resize_inode(sb);
1901 ext4_set_feature_meta_bg(sb);
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001902 sbi->s_es->s_first_meta_bg =
1903 cpu_to_le32(num_desc_blocks(sb, sbi->s_groups_count));
Jan Kara05c2c002020-12-16 11:18:39 +01001904 ext4_superblock_csum_set(sb);
1905 unlock_buffer(sbi->s_sbh);
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001906
Jan Karaa3f5cf12020-12-16 11:18:44 +01001907 err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001908 if (err) {
1909 ext4_std_error(sb, err);
1910 goto errout;
1911 }
1912
1913 if (inode) {
1914 nr = le32_to_cpu(ei->i_data[EXT4_DIND_BLOCK]);
1915 ext4_free_blocks(handle, inode, NULL, nr, 1,
1916 EXT4_FREE_BLOCKS_METADATA |
1917 EXT4_FREE_BLOCKS_FORGET);
1918 ei->i_data[EXT4_DIND_BLOCK] = 0;
1919 inode->i_blocks = 0;
1920
1921 err = ext4_mark_inode_dirty(handle, inode);
1922 if (err)
1923 ext4_std_error(sb, err);
1924 }
1925
1926errout:
1927 ret = ext4_journal_stop(handle);
1928 if (!err)
1929 err = ret;
1930 return ret;
1931
1932invalid_resize_inode:
1933 ext4_error(sb, "corrupted/inconsistent resize inode");
1934 return -EINVAL;
1935}
1936
Yongqiang Yang19c52462012-01-04 17:09:44 -05001937/*
1938 * ext4_resize_fs() resizes a fs to new size specified by @n_blocks_count
1939 *
1940 * @sb: super block of the fs to be resized
1941 * @n_blocks_count: the number of blocks resides in the resized fs
1942 */
1943int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count)
1944{
1945 struct ext4_new_flex_group_data *flex_gd = NULL;
1946 struct ext4_sb_info *sbi = EXT4_SB(sb);
1947 struct ext4_super_block *es = sbi->s_es;
1948 struct buffer_head *bh;
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001949 struct inode *resize_inode = NULL;
1950 ext4_grpblk_t add, offset;
Yongqiang Yang19c52462012-01-04 17:09:44 -05001951 unsigned long n_desc_blocks;
1952 unsigned long o_desc_blocks;
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001953 ext4_group_t o_group;
1954 ext4_group_t n_group;
1955 ext4_fsblk_t o_blocks_count;
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001956 ext4_fsblk_t n_blocks_count_retry = 0;
Theodore Ts'o4da4a562012-09-13 10:24:21 -04001957 unsigned long last_update_time = 0;
Theodore Ts'o117fff12012-09-05 01:29:50 -04001958 int err = 0, flexbg_size = 1 << sbi->s_log_groups_per_flex;
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001959 int meta_bg;
Yongqiang Yang19c52462012-01-04 17:09:44 -05001960
Theodore Ts'o59e31c12012-09-19 00:55:56 -04001961 /* See if the device is actually as big as what was requested */
zhangyi (F)0a846f42020-09-24 15:33:36 +08001962 bh = ext4_sb_bread(sb, n_blocks_count - 1, 0);
1963 if (IS_ERR(bh)) {
Theodore Ts'o59e31c12012-09-19 00:55:56 -04001964 ext4_warning(sb, "can't read last block, resize aborted");
1965 return -ENOSPC;
1966 }
1967 brelse(bh);
1968
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001969retry:
Yongqiang Yang19c52462012-01-04 17:09:44 -05001970 o_blocks_count = ext4_blocks_count(es);
1971
Theodore Ts'o59e31c12012-09-19 00:55:56 -04001972 ext4_msg(sb, KERN_INFO, "resizing filesystem from %llu "
1973 "to %llu blocks", o_blocks_count, n_blocks_count);
Yongqiang Yang19c52462012-01-04 17:09:44 -05001974
1975 if (n_blocks_count < o_blocks_count) {
1976 /* On-line shrinking not supported */
1977 ext4_warning(sb, "can't shrink FS - resize aborted");
1978 return -EINVAL;
1979 }
1980
1981 if (n_blocks_count == o_blocks_count)
1982 /* Nothing need to do */
1983 return 0;
1984
Lukas Czernerbd862982013-04-03 23:32:34 -04001985 n_group = ext4_get_group_number(sb, n_blocks_count - 1);
Jan Kara4f2f76f72018-05-25 12:51:25 -04001986 if (n_group >= (0xFFFFFFFFUL / EXT4_INODES_PER_GROUP(sb))) {
Theodore Ts'o3f8a6412013-04-21 22:56:32 -04001987 ext4_warning(sb, "resize would cause inodes_count overflow");
1988 return -EINVAL;
1989 }
Lukas Czernera0ade1d2012-02-20 23:02:06 -05001990 ext4_get_group_no_and_offset(sb, o_blocks_count - 1, &o_group, &offset);
Yongqiang Yang19c52462012-01-04 17:09:44 -05001991
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001992 n_desc_blocks = num_desc_blocks(sb, n_group + 1);
1993 o_desc_blocks = num_desc_blocks(sb, sbi->s_groups_count);
Yongqiang Yang19c52462012-01-04 17:09:44 -05001994
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001995 meta_bg = ext4_has_feature_meta_bg(sb);
Yongqiang Yang19c52462012-01-04 17:09:44 -05001996
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001997 if (ext4_has_feature_resize_inode(sb)) {
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001998 if (meta_bg) {
1999 ext4_error(sb, "resize_inode and meta_bg enabled "
2000 "simultaneously");
2001 return -EINVAL;
2002 }
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04002003 if (n_desc_blocks > o_desc_blocks +
2004 le16_to_cpu(es->s_reserved_gdt_blocks)) {
2005 n_blocks_count_retry = n_blocks_count;
2006 n_desc_blocks = o_desc_blocks +
2007 le16_to_cpu(es->s_reserved_gdt_blocks);
2008 n_group = n_desc_blocks * EXT4_DESC_PER_BLOCK(sb);
Jerry Leeaec51752017-08-06 01:18:31 -04002009 n_blocks_count = (ext4_fsblk_t)n_group *
Jan Karaf96c3ac2019-02-11 13:30:32 -05002010 EXT4_BLOCKS_PER_GROUP(sb) +
2011 le32_to_cpu(es->s_first_data_block);
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04002012 n_group--; /* set to last group number */
Yongqiang Yang01f795f2012-09-05 01:33:50 -04002013 }
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04002014
2015 if (!resize_inode)
Theodore Ts'o8a363972018-12-19 12:29:13 -05002016 resize_inode = ext4_iget(sb, EXT4_RESIZE_INO,
2017 EXT4_IGET_SPECIAL);
Yongqiang Yang01f795f2012-09-05 01:33:50 -04002018 if (IS_ERR(resize_inode)) {
2019 ext4_warning(sb, "Error opening resize inode");
2020 return PTR_ERR(resize_inode);
2021 }
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04002022 }
2023
Theodore Ts'o59e31c12012-09-19 00:55:56 -04002024 if ((!resize_inode && !meta_bg) || n_blocks_count == o_blocks_count) {
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04002025 err = ext4_convert_meta_bg(sb, resize_inode);
2026 if (err)
2027 goto out;
2028 if (resize_inode) {
2029 iput(resize_inode);
2030 resize_inode = NULL;
2031 }
2032 if (n_blocks_count_retry) {
2033 n_blocks_count = n_blocks_count_retry;
2034 n_blocks_count_retry = 0;
2035 goto retry;
2036 }
Yongqiang Yang19c52462012-01-04 17:09:44 -05002037 }
2038
Theodore Ts'of0a459d2018-09-03 22:19:43 -04002039 /*
2040 * Make sure the last group has enough space so that it's
2041 * guaranteed to have enough space for all metadata blocks
2042 * that it might need to hold. (We might not need to store
2043 * the inode table blocks in the last block group, but there
2044 * will be cases where this might be needed.)
2045 */
2046 if ((ext4_group_first_block_no(sb, n_group) +
2047 ext4_group_overhead_blocks(sb, n_group) + 2 +
2048 sbi->s_itb_per_group + sbi->s_cluster_ratio) >= n_blocks_count) {
2049 n_blocks_count = ext4_group_first_block_no(sb, n_group);
2050 n_group--;
2051 n_blocks_count_retry = 0;
2052 if (resize_inode) {
2053 iput(resize_inode);
2054 resize_inode = NULL;
2055 }
2056 goto retry;
2057 }
2058
Lukas Czernera0ade1d2012-02-20 23:02:06 -05002059 /* extend the last group */
2060 if (n_group == o_group)
2061 add = n_blocks_count - o_blocks_count;
2062 else
harshadsd77147f2017-10-29 09:38:46 -04002063 add = EXT4_C2B(sbi, EXT4_CLUSTERS_PER_GROUP(sb) - (offset + 1));
Lukas Czernera0ade1d2012-02-20 23:02:06 -05002064 if (add > 0) {
Yongqiang Yang19c52462012-01-04 17:09:44 -05002065 err = ext4_group_extend_no_check(sb, o_blocks_count, add);
2066 if (err)
2067 goto out;
2068 }
2069
Yongqiang Yangd7574ad2012-09-05 01:23:50 -04002070 if (ext4_blocks_count(es) == n_blocks_count)
Yongqiang Yang19c52462012-01-04 17:09:44 -05002071 goto out;
2072
Theodore Ts'o117fff12012-09-05 01:29:50 -04002073 err = ext4_alloc_flex_bg_array(sb, n_group + 1);
2074 if (err)
Vasily Averinf348e222018-11-06 16:16:01 -05002075 goto out;
Theodore Ts'o117fff12012-09-05 01:29:50 -04002076
Theodore Ts'o28623c22012-09-05 01:31:50 -04002077 err = ext4_mb_alloc_groupinfo(sb, n_group + 1);
2078 if (err)
2079 goto out;
2080
Yongqiang Yang19c52462012-01-04 17:09:44 -05002081 flex_gd = alloc_flex_gd(flexbg_size);
2082 if (flex_gd == NULL) {
2083 err = -ENOMEM;
2084 goto out;
2085 }
2086
2087 /* Add flex groups. Note that a regular group is a
2088 * flex group with 1 group.
2089 */
2090 while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count,
2091 flexbg_size)) {
Theodore Ts'o4da4a562012-09-13 10:24:21 -04002092 if (jiffies - last_update_time > HZ * 10) {
2093 if (last_update_time)
2094 ext4_msg(sb, KERN_INFO,
2095 "resized to %llu blocks",
2096 ext4_blocks_count(es));
2097 last_update_time = jiffies;
2098 }
Yongqiang Yang03c1c292012-09-05 01:21:50 -04002099 if (ext4_alloc_group_tables(sb, flex_gd, flexbg_size) != 0)
2100 break;
Yongqiang Yang19c52462012-01-04 17:09:44 -05002101 err = ext4_flex_group_add(sb, resize_inode, flex_gd);
2102 if (unlikely(err))
2103 break;
2104 }
2105
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04002106 if (!err && n_blocks_count_retry) {
2107 n_blocks_count = n_blocks_count_retry;
2108 n_blocks_count_retry = 0;
2109 free_flex_gd(flex_gd);
2110 flex_gd = NULL;
Vasily Averindb6aee62018-11-06 16:20:40 -05002111 if (resize_inode) {
2112 iput(resize_inode);
2113 resize_inode = NULL;
2114 }
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04002115 goto retry;
2116 }
2117
Yongqiang Yang19c52462012-01-04 17:09:44 -05002118out:
2119 if (flex_gd)
2120 free_flex_gd(flex_gd);
Yongqiang Yang01f795f2012-09-05 01:33:50 -04002121 if (resize_inode != NULL)
2122 iput(resize_inode);
Lukas Czerner6c732842019-03-15 00:22:28 -04002123 if (err)
2124 ext4_warning(sb, "error (%d) occurred during "
2125 "file system resize", err);
2126 ext4_msg(sb, KERN_INFO, "resized filesystem to %llu",
2127 ext4_blocks_count(es));
Yongqiang Yang19c52462012-01-04 17:09:44 -05002128 return err;
2129}