blob: fc885914c88a3d15908d7db19c1e1045018f9107 [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 Triplette9f9f612021-06-07 12:15:08 -070077 if (ext4_has_feature_bigalloc(sb)) {
78 ext4_msg(sb, KERN_ERR, "Online resizing not supported with bigalloc");
79 return -EOPNOTSUPP;
80 }
Josh Triplettb1489182021-06-07 12:15:24 -070081 if (ext4_has_feature_sparse_super2(sb)) {
82 ext4_msg(sb, KERN_ERR, "Online resizing not supported with sparse_super2");
83 return -EOPNOTSUPP;
84 }
Josh Triplette9f9f612021-06-07 12:15:08 -070085
Theodore Ts'o9549a162017-02-05 01:27:48 -050086 if (test_and_set_bit_lock(EXT4_FLAGS_RESIZING,
87 &EXT4_SB(sb)->s_ext4_flags))
Yongqiang Yang8f82f842011-07-26 21:35:44 -040088 ret = -EBUSY;
89
90 return ret;
91}
92
93void ext4_resize_end(struct super_block *sb)
94{
Theodore Ts'o9549a162017-02-05 01:27:48 -050095 clear_bit_unlock(EXT4_FLAGS_RESIZING, &EXT4_SB(sb)->s_ext4_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +010096 smp_mb__after_atomic();
Yongqiang Yang8f82f842011-07-26 21:35:44 -040097}
98
Yongqiang Yang01f795f2012-09-05 01:33:50 -040099static ext4_group_t ext4_meta_bg_first_group(struct super_block *sb,
100 ext4_group_t group) {
101 return (group >> EXT4_DESC_PER_BLOCK_BITS(sb)) <<
102 EXT4_DESC_PER_BLOCK_BITS(sb);
103}
104
105static ext4_fsblk_t ext4_meta_bg_first_block_no(struct super_block *sb,
106 ext4_group_t group) {
107 group = ext4_meta_bg_first_group(sb, group);
108 return ext4_group_first_block_no(sb, group);
109}
110
111static ext4_grpblk_t ext4_group_overhead_blocks(struct super_block *sb,
112 ext4_group_t group) {
113 ext4_grpblk_t overhead;
114 overhead = ext4_bg_num_gdb(sb, group);
115 if (ext4_bg_has_super(sb, group))
116 overhead += 1 +
117 le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
118 return overhead;
119}
120
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700121#define outside(b, first, last) ((b) < (first) || (b) >= (last))
122#define inside(b, first, last) ((b) >= (first) && (b) < (last))
123
124static int verify_group_input(struct super_block *sb,
Mingming Cao617ba132006-10-11 01:20:53 -0700125 struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700126{
Mingming Cao617ba132006-10-11 01:20:53 -0700127 struct ext4_sb_info *sbi = EXT4_SB(sb);
128 struct ext4_super_block *es = sbi->s_es;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700129 ext4_fsblk_t start = ext4_blocks_count(es);
Mingming Cao617ba132006-10-11 01:20:53 -0700130 ext4_fsblk_t end = start + input->blocks_count;
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500131 ext4_group_t group = input->group;
Mingming Cao617ba132006-10-11 01:20:53 -0700132 ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group;
Theodore Ts'ob302ef22013-06-06 11:14:31 -0400133 unsigned overhead;
134 ext4_fsblk_t metaend;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700135 struct buffer_head *bh = NULL;
Mingming Cao3a5b2ec2006-10-11 01:21:05 -0700136 ext4_grpblk_t free_blocks_count, offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700137 int err = -EINVAL;
138
Theodore Ts'ob302ef22013-06-06 11:14:31 -0400139 if (group != sbi->s_groups_count) {
140 ext4_warning(sb, "Cannot add at group %u (only %u groups)",
141 input->group, sbi->s_groups_count);
142 return -EINVAL;
143 }
144
145 overhead = ext4_group_overhead_blocks(sb, group);
146 metaend = start + overhead;
harshadsd77147f2017-10-29 09:38:46 -0400147 input->free_clusters_count = free_blocks_count =
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700148 input->blocks_count - 2 - overhead - sbi->s_itb_per_group;
149
150 if (test_opt(sb, DEBUG))
Mingming Cao617ba132006-10-11 01:20:53 -0700151 printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700152 "(%d free, %u reserved)\n",
Mingming Cao617ba132006-10-11 01:20:53 -0700153 ext4_bg_has_super(sb, input->group) ? "normal" :
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700154 "no-super", input->group, input->blocks_count,
155 free_blocks_count, input->reserved_blocks);
156
Mingming Cao3a5b2ec2006-10-11 01:21:05 -0700157 ext4_get_group_no_and_offset(sb, start, NULL, &offset);
Theodore Ts'ob302ef22013-06-06 11:14:31 -0400158 if (offset != 0)
Eric Sandeen12062dd2010-02-15 14:19:27 -0500159 ext4_warning(sb, "Last group not full");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700160 else if (input->reserved_blocks > input->blocks_count / 5)
Eric Sandeen12062dd2010-02-15 14:19:27 -0500161 ext4_warning(sb, "Reserved blocks too high (%u)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700162 input->reserved_blocks);
163 else if (free_blocks_count < 0)
Eric Sandeen12062dd2010-02-15 14:19:27 -0500164 ext4_warning(sb, "Bad blocks count %u",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700165 input->blocks_count);
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500166 else if (IS_ERR(bh = ext4_sb_bread(sb, end - 1, 0))) {
167 err = PTR_ERR(bh);
168 bh = NULL;
Eric Sandeen12062dd2010-02-15 14:19:27 -0500169 ext4_warning(sb, "Cannot read last block (%llu)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170 end - 1);
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500171 } else if (outside(input->block_bitmap, start, end))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500172 ext4_warning(sb, "Block bitmap not in group (block %llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700173 (unsigned long long)input->block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174 else if (outside(input->inode_bitmap, start, end))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500175 ext4_warning(sb, "Inode bitmap not in group (block %llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700176 (unsigned long long)input->inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700177 else if (outside(input->inode_table, start, end) ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400178 outside(itend - 1, start, end))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500179 ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700180 (unsigned long long)input->inode_table, itend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700181 else if (input->inode_bitmap == input->block_bitmap)
Eric Sandeen12062dd2010-02-15 14:19:27 -0500182 ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700183 (unsigned long long)input->block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184 else if (inside(input->block_bitmap, input->inode_table, itend))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500185 ext4_warning(sb, "Block bitmap (%llu) in inode table "
186 "(%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700187 (unsigned long long)input->block_bitmap,
188 (unsigned long long)input->inode_table, itend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700189 else if (inside(input->inode_bitmap, input->inode_table, itend))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500190 ext4_warning(sb, "Inode bitmap (%llu) in inode table "
191 "(%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700192 (unsigned long long)input->inode_bitmap,
193 (unsigned long long)input->inode_table, itend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700194 else if (inside(input->block_bitmap, start, metaend))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500195 ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700196 (unsigned long long)input->block_bitmap,
197 start, metaend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700198 else if (inside(input->inode_bitmap, start, metaend))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500199 ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700200 (unsigned long long)input->inode_bitmap,
201 start, metaend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700202 else if (inside(input->inode_table, start, metaend) ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400203 inside(itend - 1, start, metaend))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500204 ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table "
205 "(%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700206 (unsigned long long)input->inode_table,
207 itend - 1, start, metaend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700208 else
209 err = 0;
210 brelse(bh);
211
212 return err;
213}
214
Yongqiang Yang28c7bac2012-01-03 23:22:50 -0500215/*
216 * ext4_new_flex_group_data is used by 64bit-resize interface to add a flex
217 * group each time.
218 */
219struct ext4_new_flex_group_data {
220 struct ext4_new_group_data *groups; /* new_group_data for groups
221 in the flex group */
222 __u16 *bg_flags; /* block group flags of groups
223 in @groups */
224 ext4_group_t count; /* number of groups in @groups
225 */
226};
227
228/*
229 * alloc_flex_gd() allocates a ext4_new_flex_group_data with size of
230 * @flexbg_size.
231 *
232 * Returns NULL on failure otherwise address of the allocated structure.
233 */
234static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size)
235{
236 struct ext4_new_flex_group_data *flex_gd;
237
238 flex_gd = kmalloc(sizeof(*flex_gd), GFP_NOFS);
239 if (flex_gd == NULL)
240 goto out3;
241
Insu Yun46901762016-02-12 01:15:59 -0500242 if (flexbg_size >= UINT_MAX / sizeof(struct ext4_new_group_data))
Haogang Chen967ac8a2012-05-28 14:21:55 -0400243 goto out2;
Yongqiang Yang28c7bac2012-01-03 23:22:50 -0500244 flex_gd->count = flexbg_size;
245
Kees Cook6da2ec52018-06-12 13:55:00 -0700246 flex_gd->groups = kmalloc_array(flexbg_size,
247 sizeof(struct ext4_new_group_data),
248 GFP_NOFS);
Yongqiang Yang28c7bac2012-01-03 23:22:50 -0500249 if (flex_gd->groups == NULL)
250 goto out2;
251
Kees Cook6da2ec52018-06-12 13:55:00 -0700252 flex_gd->bg_flags = kmalloc_array(flexbg_size, sizeof(__u16),
253 GFP_NOFS);
Yongqiang Yang28c7bac2012-01-03 23:22:50 -0500254 if (flex_gd->bg_flags == NULL)
255 goto out1;
256
257 return flex_gd;
258
259out1:
260 kfree(flex_gd->groups);
261out2:
262 kfree(flex_gd);
263out3:
264 return NULL;
265}
266
267static void free_flex_gd(struct ext4_new_flex_group_data *flex_gd)
268{
269 kfree(flex_gd->bg_flags);
270 kfree(flex_gd->groups);
271 kfree(flex_gd);
272}
273
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500274/*
275 * ext4_alloc_group_tables() allocates block bitmaps, inode bitmaps
276 * and inode tables for a flex group.
277 *
278 * This function is used by 64bit-resize. Note that this function allocates
279 * group tables from the 1st group of groups contained by @flexgd, which may
280 * be a partial of a flex group.
281 *
282 * @sb: super block of fs to which the groups belongs
Yongqiang Yang03c1c292012-09-05 01:21:50 -0400283 *
284 * Returns 0 on a successful allocation of the metadata blocks in the
285 * block group.
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500286 */
Yongqiang Yang03c1c292012-09-05 01:21:50 -0400287static int ext4_alloc_group_tables(struct super_block *sb,
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500288 struct ext4_new_flex_group_data *flex_gd,
289 int flexbg_size)
290{
291 struct ext4_new_group_data *group_data = flex_gd->groups;
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500292 ext4_fsblk_t start_blk;
293 ext4_fsblk_t last_blk;
294 ext4_group_t src_group;
295 ext4_group_t bb_index = 0;
296 ext4_group_t ib_index = 0;
297 ext4_group_t it_index = 0;
298 ext4_group_t group;
299 ext4_group_t last_group;
300 unsigned overhead;
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500301 __u16 uninit_mask = (flexbg_size > 1) ? ~EXT4_BG_BLOCK_UNINIT : ~0;
harshadsd77147f2017-10-29 09:38:46 -0400302 int i;
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500303
304 BUG_ON(flex_gd->count == 0 || group_data == NULL);
305
306 src_group = group_data[0].group;
307 last_group = src_group + flex_gd->count - 1;
308
309 BUG_ON((flexbg_size > 1) && ((src_group & ~(flexbg_size - 1)) !=
310 (last_group & ~(flexbg_size - 1))));
311next_group:
312 group = group_data[0].group;
Yongqiang Yang03c1c292012-09-05 01:21:50 -0400313 if (src_group >= group_data[0].group + flex_gd->count)
314 return -ENOSPC;
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500315 start_blk = ext4_group_first_block_no(sb, src_group);
316 last_blk = start_blk + group_data[src_group - group].blocks_count;
317
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400318 overhead = ext4_group_overhead_blocks(sb, src_group);
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500319
320 start_blk += overhead;
321
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500322 /* We collect contiguous blocks as much as possible. */
323 src_group++;
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400324 for (; src_group <= last_group; src_group++) {
325 overhead = ext4_group_overhead_blocks(sb, src_group);
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500326 if (overhead == 0)
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500327 last_blk += group_data[src_group - group].blocks_count;
328 else
329 break;
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400330 }
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500331
332 /* Allocate block bitmaps */
333 for (; bb_index < flex_gd->count; bb_index++) {
334 if (start_blk >= last_blk)
335 goto next_group;
336 group_data[bb_index].block_bitmap = start_blk++;
Lukas Czernerbd862982013-04-03 23:32:34 -0400337 group = ext4_get_group_number(sb, start_blk - 1);
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500338 group -= group_data[0].group;
harshadsd77147f2017-10-29 09:38:46 -0400339 group_data[group].mdata_blocks++;
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500340 flex_gd->bg_flags[group] &= uninit_mask;
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500341 }
342
343 /* Allocate inode bitmaps */
344 for (; ib_index < flex_gd->count; ib_index++) {
345 if (start_blk >= last_blk)
346 goto next_group;
347 group_data[ib_index].inode_bitmap = start_blk++;
Lukas Czernerbd862982013-04-03 23:32:34 -0400348 group = ext4_get_group_number(sb, start_blk - 1);
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500349 group -= group_data[0].group;
harshadsd77147f2017-10-29 09:38:46 -0400350 group_data[group].mdata_blocks++;
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500351 flex_gd->bg_flags[group] &= uninit_mask;
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500352 }
353
354 /* Allocate inode tables */
355 for (; it_index < flex_gd->count; it_index++) {
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500356 unsigned int itb = EXT4_SB(sb)->s_itb_per_group;
357 ext4_fsblk_t next_group_start;
358
359 if (start_blk + itb > last_blk)
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500360 goto next_group;
361 group_data[it_index].inode_table = start_blk;
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500362 group = ext4_get_group_number(sb, start_blk);
363 next_group_start = ext4_group_first_block_no(sb, group + 1);
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500364 group -= group_data[0].group;
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500365
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500366 if (start_blk + itb > next_group_start) {
367 flex_gd->bg_flags[group + 1] &= uninit_mask;
368 overhead = start_blk + itb - next_group_start;
harshadsd77147f2017-10-29 09:38:46 -0400369 group_data[group + 1].mdata_blocks += overhead;
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500370 itb -= overhead;
371 }
372
harshadsd77147f2017-10-29 09:38:46 -0400373 group_data[group].mdata_blocks += itb;
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500374 flex_gd->bg_flags[group] &= uninit_mask;
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500375 start_blk += EXT4_SB(sb)->s_itb_per_group;
376 }
377
harshadsd77147f2017-10-29 09:38:46 -0400378 /* Update free clusters count to exclude metadata blocks */
379 for (i = 0; i < flex_gd->count; i++) {
380 group_data[i].free_clusters_count -=
381 EXT4_NUM_B2C(EXT4_SB(sb),
382 group_data[i].mdata_blocks);
383 }
384
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500385 if (test_opt(sb, DEBUG)) {
386 int i;
387 group = group_data[0].group;
388
389 printk(KERN_DEBUG "EXT4-fs: adding a flex group with "
390 "%d groups, flexbg size is %d:\n", flex_gd->count,
391 flexbg_size);
392
393 for (i = 0; i < flex_gd->count; i++) {
harshadsd77147f2017-10-29 09:38:46 -0400394 ext4_debug(
395 "adding %s group %u: %u blocks (%d free, %d mdata blocks)\n",
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500396 ext4_bg_has_super(sb, group + i) ? "normal" :
397 "no-super", group + i,
398 group_data[i].blocks_count,
harshadsd77147f2017-10-29 09:38:46 -0400399 group_data[i].free_clusters_count,
400 group_data[i].mdata_blocks);
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500401 }
402 }
Yongqiang Yang03c1c292012-09-05 01:21:50 -0400403 return 0;
Yongqiang Yang3fbea4b2012-01-03 23:44:38 -0500404}
405
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700406static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
Mingming Cao617ba132006-10-11 01:20:53 -0700407 ext4_fsblk_t blk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700408{
409 struct buffer_head *bh;
410 int err;
411
412 bh = sb_getblk(sb, blk);
Wang Shilongaebf0242013-01-12 16:28:47 -0500413 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500414 return ERR_PTR(-ENOMEM);
liang xie5d601252014-05-12 22:06:43 -0400415 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700416 if ((err = ext4_journal_get_write_access(handle, bh))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700417 brelse(bh);
418 bh = ERR_PTR(err);
419 } else {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700420 memset(bh->b_data, 0, sb->s_blocksize);
421 set_buffer_uptodate(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700422 }
423
424 return bh;
425}
426
Jan Kara83448bd2019-11-05 17:44:29 +0100427static int ext4_resize_ensure_credits_batch(handle_t *handle, int credits)
428{
429 return ext4_journal_ensure_credits_fn(handle, credits,
430 EXT4_MAX_TRANS_DATA, 0, 0);
431}
432
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700433/*
harshadsd77147f2017-10-29 09:38:46 -0400434 * set_flexbg_block_bitmap() mark clusters [@first_cluster, @last_cluster] used.
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500435 *
436 * Helper function for ext4_setup_new_group_blocks() which set .
437 *
438 * @sb: super block
439 * @handle: journal handle
440 * @flex_gd: flex group data
441 */
442static int set_flexbg_block_bitmap(struct super_block *sb, handle_t *handle,
443 struct ext4_new_flex_group_data *flex_gd,
harshadsd77147f2017-10-29 09:38:46 -0400444 ext4_fsblk_t first_cluster, ext4_fsblk_t last_cluster)
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500445{
harshadsd77147f2017-10-29 09:38:46 -0400446 struct ext4_sb_info *sbi = EXT4_SB(sb);
447 ext4_group_t count = last_cluster - first_cluster + 1;
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500448 ext4_group_t count2;
449
harshadsd77147f2017-10-29 09:38:46 -0400450 ext4_debug("mark clusters [%llu-%llu] used\n", first_cluster,
451 last_cluster);
452 for (count2 = count; count > 0;
453 count -= count2, first_cluster += count2) {
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500454 ext4_fsblk_t start;
455 struct buffer_head *bh;
456 ext4_group_t group;
457 int err;
458
harshadsd77147f2017-10-29 09:38:46 -0400459 group = ext4_get_group_number(sb, EXT4_C2B(sbi, first_cluster));
460 start = EXT4_B2C(sbi, ext4_group_first_block_no(sb, group));
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500461 group -= flex_gd->groups[0].group;
462
harshadsd77147f2017-10-29 09:38:46 -0400463 count2 = EXT4_CLUSTERS_PER_GROUP(sb) - (first_cluster - start);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500464 if (count2 > count)
465 count2 = count;
466
467 if (flex_gd->bg_flags[group] & EXT4_BG_BLOCK_UNINIT) {
468 BUG_ON(flex_gd->count > 1);
469 continue;
470 }
471
Jan Kara83448bd2019-11-05 17:44:29 +0100472 err = ext4_resize_ensure_credits_batch(handle, 1);
Jan Karaa4130362019-11-05 17:44:16 +0100473 if (err < 0)
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500474 return err;
475
476 bh = sb_getblk(sb, flex_gd->groups[group].block_bitmap);
Wang Shilongaebf0242013-01-12 16:28:47 -0500477 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500478 return -ENOMEM;
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500479
liang xie5d601252014-05-12 22:06:43 -0400480 BUFFER_TRACE(bh, "get_write_access");
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500481 err = ext4_journal_get_write_access(handle, bh);
Vasily Averincea57942018-11-03 16:22:10 -0400482 if (err) {
483 brelse(bh);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500484 return err;
Vasily Averincea57942018-11-03 16:22:10 -0400485 }
harshadsd77147f2017-10-29 09:38:46 -0400486 ext4_debug("mark block bitmap %#04llx (+%llu/%u)\n",
487 first_cluster, first_cluster - start, count2);
488 ext4_set_bits(bh->b_data, first_cluster - start, count2);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500489
490 err = ext4_handle_dirty_metadata(handle, NULL, bh);
Vasily Averincea57942018-11-03 16:22:10 -0400491 brelse(bh);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500492 if (unlikely(err))
493 return err;
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500494 }
495
496 return 0;
497}
498
499/*
500 * Set up the block and inode bitmaps, and the inode table for the new groups.
501 * This doesn't need to be part of the main transaction, since we are only
502 * changing blocks outside the actual filesystem. We still do journaling to
503 * ensure the recovery is correct in case of a failure just after resize.
504 * If any part of this fails, we simply abort the resize.
505 *
506 * setup_new_flex_group_blocks handles a flex group as follow:
507 * 1. copy super block and GDT, and initialize group tables if necessary.
508 * In this step, we only set bits in blocks bitmaps for blocks taken by
509 * super block and GDT.
510 * 2. allocate group tables in block bitmaps, that is, set bits in block
511 * bitmap for blocks taken by group tables.
512 */
513static int setup_new_flex_group_blocks(struct super_block *sb,
514 struct ext4_new_flex_group_data *flex_gd)
515{
516 int group_table_count[] = {1, 1, EXT4_SB(sb)->s_itb_per_group};
517 ext4_fsblk_t start;
518 ext4_fsblk_t block;
519 struct ext4_sb_info *sbi = EXT4_SB(sb);
520 struct ext4_super_block *es = sbi->s_es;
521 struct ext4_new_group_data *group_data = flex_gd->groups;
522 __u16 *bg_flags = flex_gd->bg_flags;
523 handle_t *handle;
524 ext4_group_t group, count;
525 struct buffer_head *bh = NULL;
526 int reserved_gdb, i, j, err = 0, err2;
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400527 int meta_bg;
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500528
529 BUG_ON(!flex_gd->count || !group_data ||
530 group_data[0].group != sbi->s_groups_count);
531
532 reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400533 meta_bg = ext4_has_feature_meta_bg(sb);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500534
535 /* This transaction may be extended/restarted along the way */
Theodore Ts'o9924a922013-02-08 21:59:22 -0500536 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500537 if (IS_ERR(handle))
538 return PTR_ERR(handle);
539
540 group = group_data[0].group;
541 for (i = 0; i < flex_gd->count; i++, group++) {
542 unsigned long gdblocks;
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400543 ext4_grpblk_t overhead;
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500544
545 gdblocks = ext4_bg_num_gdb(sb, group);
546 start = ext4_group_first_block_no(sb, group);
547
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400548 if (meta_bg == 0 && !ext4_bg_has_super(sb, group))
Yongqiang Yang6df935a2012-09-05 01:25:50 -0400549 goto handle_itb;
550
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400551 if (meta_bg == 1) {
552 ext4_group_t first_group;
553 first_group = ext4_meta_bg_first_group(sb, group);
554 if (first_group != group + 1 &&
555 first_group != group + EXT4_DESC_PER_BLOCK(sb) - 1)
556 goto handle_itb;
557 }
558
559 block = start + ext4_bg_has_super(sb, group);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500560 /* Copy all of the GDT blocks into the backup in this group */
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400561 for (j = 0; j < gdblocks; j++, block++) {
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500562 struct buffer_head *gdb;
563
564 ext4_debug("update backup group %#04llx\n", block);
Jan Kara83448bd2019-11-05 17:44:29 +0100565 err = ext4_resize_ensure_credits_batch(handle, 1);
Jan Karaa4130362019-11-05 17:44:16 +0100566 if (err < 0)
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500567 goto out;
568
569 gdb = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -0500570 if (unlikely(!gdb)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500571 err = -ENOMEM;
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500572 goto out;
573 }
574
liang xie5d601252014-05-12 22:06:43 -0400575 BUFFER_TRACE(gdb, "get_write_access");
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500576 err = ext4_journal_get_write_access(handle, gdb);
577 if (err) {
578 brelse(gdb);
579 goto out;
580 }
Theodore Ts'o1d0c3922020-02-15 16:40:37 -0500581 memcpy(gdb->b_data, sbi_array_rcu_deref(sbi,
582 s_group_desc, j)->b_data, gdb->b_size);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500583 set_buffer_uptodate(gdb);
584
585 err = ext4_handle_dirty_metadata(handle, NULL, gdb);
586 if (unlikely(err)) {
587 brelse(gdb);
588 goto out;
589 }
590 brelse(gdb);
591 }
592
593 /* Zero out all of the reserved backup group descriptor
594 * table blocks
595 */
596 if (ext4_bg_has_super(sb, group)) {
597 err = sb_issue_zeroout(sb, gdblocks + start + 1,
598 reserved_gdb, GFP_NOFS);
599 if (err)
600 goto out;
601 }
602
Yongqiang Yang6df935a2012-09-05 01:25:50 -0400603handle_itb:
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500604 /* Initialize group tables of the grop @group */
605 if (!(bg_flags[i] & EXT4_BG_INODE_ZEROED))
606 goto handle_bb;
607
608 /* Zero out all of the inode table blocks */
609 block = group_data[i].inode_table;
610 ext4_debug("clear inode table blocks %#04llx -> %#04lx\n",
611 block, sbi->s_itb_per_group);
612 err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group,
613 GFP_NOFS);
614 if (err)
615 goto out;
616
617handle_bb:
618 if (bg_flags[i] & EXT4_BG_BLOCK_UNINIT)
619 goto handle_ib;
620
621 /* Initialize block bitmap of the @group */
622 block = group_data[i].block_bitmap;
Jan Kara83448bd2019-11-05 17:44:29 +0100623 err = ext4_resize_ensure_credits_batch(handle, 1);
Jan Karaa4130362019-11-05 17:44:16 +0100624 if (err < 0)
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500625 goto out;
626
627 bh = bclean(handle, sb, block);
628 if (IS_ERR(bh)) {
629 err = PTR_ERR(bh);
630 goto out;
631 }
Yongqiang Yang01f795f2012-09-05 01:33:50 -0400632 overhead = ext4_group_overhead_blocks(sb, group);
633 if (overhead != 0) {
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500634 ext4_debug("mark backup superblock %#04llx (+0)\n",
635 start);
harshadsd77147f2017-10-29 09:38:46 -0400636 ext4_set_bits(bh->b_data, 0,
637 EXT4_NUM_B2C(sbi, overhead));
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500638 }
harshadsd77147f2017-10-29 09:38:46 -0400639 ext4_mark_bitmap_end(EXT4_B2C(sbi, group_data[i].blocks_count),
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500640 sb->s_blocksize * 8, bh->b_data);
641 err = ext4_handle_dirty_metadata(handle, NULL, bh);
Vasily Averin9e402892018-11-03 16:13:17 -0400642 brelse(bh);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500643 if (err)
644 goto out;
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500645
646handle_ib:
647 if (bg_flags[i] & EXT4_BG_INODE_UNINIT)
648 continue;
649
650 /* Initialize inode bitmap of the @group */
651 block = group_data[i].inode_bitmap;
Jan Kara83448bd2019-11-05 17:44:29 +0100652 err = ext4_resize_ensure_credits_batch(handle, 1);
Jan Karaa4130362019-11-05 17:44:16 +0100653 if (err < 0)
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500654 goto out;
655 /* Mark unused entries in inode bitmap used */
656 bh = bclean(handle, sb, block);
657 if (IS_ERR(bh)) {
658 err = PTR_ERR(bh);
659 goto out;
660 }
661
662 ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb),
663 sb->s_blocksize * 8, bh->b_data);
664 err = ext4_handle_dirty_metadata(handle, NULL, bh);
Vasily Averin9e402892018-11-03 16:13:17 -0400665 brelse(bh);
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500666 if (err)
667 goto out;
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500668 }
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500669
670 /* Mark group tables in block bitmap */
671 for (j = 0; j < GROUP_TABLE_COUNT; j++) {
672 count = group_table_count[j];
673 start = (&group_data[0].block_bitmap)[j];
674 block = start;
675 for (i = 1; i < flex_gd->count; i++) {
676 block += group_table_count[j];
677 if (block == (&group_data[i].block_bitmap)[j]) {
678 count += group_table_count[j];
679 continue;
680 }
681 err = set_flexbg_block_bitmap(sb, handle,
harshadsd77147f2017-10-29 09:38:46 -0400682 flex_gd,
683 EXT4_B2C(sbi, start),
684 EXT4_B2C(sbi,
685 start + count
686 - 1));
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500687 if (err)
688 goto out;
689 count = group_table_count[j];
Theodore Ts'ob93c9532014-02-15 21:33:13 -0500690 start = (&group_data[i].block_bitmap)[j];
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500691 block = start;
692 }
693
694 if (count) {
695 err = set_flexbg_block_bitmap(sb, handle,
harshadsd77147f2017-10-29 09:38:46 -0400696 flex_gd,
697 EXT4_B2C(sbi, start),
698 EXT4_B2C(sbi,
699 start + count
700 - 1));
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500701 if (err)
702 goto out;
703 }
704 }
705
706out:
Yongqiang Yang33afdcc2012-01-03 23:32:52 -0500707 err2 = ext4_journal_stop(handle);
708 if (err2 && !err)
709 err = err2;
710
711 return err;
712}
713
714/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700715 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
Mingming Cao617ba132006-10-11 01:20:53 -0700716 * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700717 * calling this for the first time. In a sparse filesystem it will be the
718 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
719 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
720 */
Mingming Cao617ba132006-10-11 01:20:53 -0700721static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700722 unsigned *five, unsigned *seven)
723{
724 unsigned *min = three;
725 int mult = 3;
726 unsigned ret;
727
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400728 if (!ext4_has_feature_sparse_super(sb)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700729 ret = *min;
730 *min += 1;
731 return ret;
732 }
733
734 if (*five < *min) {
735 min = five;
736 mult = 5;
737 }
738 if (*seven < *min) {
739 min = seven;
740 mult = 7;
741 }
742
743 ret = *min;
744 *min *= mult;
745
746 return ret;
747}
748
749/*
750 * Check that all of the backup GDT blocks are held in the primary GDT block.
751 * It is assumed that they are stored in group order. Returns the number of
752 * groups in current filesystem that have BACKUPS, or -ve error code.
753 */
754static int verify_reserved_gdb(struct super_block *sb,
Yongqiang Yangc72df9f2012-01-03 23:43:39 -0500755 ext4_group_t end,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700756 struct buffer_head *primary)
757{
Mingming Cao617ba132006-10-11 01:20:53 -0700758 const ext4_fsblk_t blk = primary->b_blocknr;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700759 unsigned three = 1;
760 unsigned five = 5;
761 unsigned seven = 7;
762 unsigned grp;
763 __le32 *p = (__le32 *)primary->b_data;
764 int gdbackups = 0;
765
Mingming Cao617ba132006-10-11 01:20:53 -0700766 while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700767 if (le32_to_cpu(*p++) !=
768 grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
Eric Sandeen12062dd2010-02-15 14:19:27 -0500769 ext4_warning(sb, "reserved GDT %llu"
Mingming Cao2ae02102006-10-11 01:21:11 -0700770 " missing grp %d (%llu)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700771 blk, grp,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700772 grp *
773 (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
774 blk);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700775 return -EINVAL;
776 }
Mingming Cao617ba132006-10-11 01:20:53 -0700777 if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700778 return -EFBIG;
779 }
780
781 return gdbackups;
782}
783
784/*
785 * Called when we need to bring a reserved group descriptor table block into
786 * use from the resize inode. The primary copy of the new GDT block currently
787 * is an indirect block (under the double indirect block in the resize inode).
788 * The new backup GDT blocks will be stored as leaf blocks in this indirect
789 * block, in group order. Even though we know all the block numbers we need,
790 * we check to ensure that the resize inode has actually reserved these blocks.
791 *
792 * Don't need to update the block bitmaps because the blocks are still in use.
793 *
794 * We get all of the error cases out of the way, so that we are sure to not
795 * fail once we start modifying the data on disk, because JBD has no rollback.
796 */
797static int add_new_gdb(handle_t *handle, struct inode *inode,
Yongqiang Yang2f919712011-07-27 21:16:33 -0400798 ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700799{
800 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -0700801 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Yongqiang Yang2f919712011-07-27 21:16:33 -0400802 unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700803 ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500804 struct buffer_head **o_group_desc, **n_group_desc = NULL;
805 struct buffer_head *dind = NULL;
806 struct buffer_head *gdb_bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700807 int gdbackups;
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500808 struct ext4_iloc iloc = { .bh = NULL };
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700809 __le32 *data;
810 int err;
811
812 if (test_opt(sb, DEBUG))
813 printk(KERN_DEBUG
Mingming Cao617ba132006-10-11 01:20:53 -0700814 "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700815 gdb_num);
816
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500817 gdb_bh = ext4_sb_bread(sb, gdblock, 0);
818 if (IS_ERR(gdb_bh))
819 return PTR_ERR(gdb_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700820
Yongqiang Yangc72df9f2012-01-03 23:43:39 -0500821 gdbackups = verify_reserved_gdb(sb, group, gdb_bh);
Yongqiang Yang2f919712011-07-27 21:16:33 -0400822 if (gdbackups < 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700823 err = gdbackups;
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500824 goto errout;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700825 }
826
Mingming Cao617ba132006-10-11 01:20:53 -0700827 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500828 dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0);
829 if (IS_ERR(dind)) {
830 err = PTR_ERR(dind);
831 dind = NULL;
832 goto errout;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700833 }
834
835 data = (__le32 *)dind->b_data;
Mingming Cao617ba132006-10-11 01:20:53 -0700836 if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500837 ext4_warning(sb, "new group %u GDT block %llu not reserved",
Yongqiang Yang2f919712011-07-27 21:16:33 -0400838 group, gdblock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700839 err = -EINVAL;
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500840 goto errout;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700841 }
842
liang xie5d601252014-05-12 22:06:43 -0400843 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Theodore Ts'ob4097142011-01-10 12:46:59 -0500844 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
845 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");
Yongqiang Yang2f919712011-07-27 21:16:33 -0400849 err = ext4_journal_get_write_access(handle, gdb_bh);
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");
Theodore Ts'ob4097142011-01-10 12:46:59 -0500854 err = ext4_journal_get_write_access(handle, dind);
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");
963 err = ext4_journal_get_write_access(handle, gdb_bh);
964 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");
Eric Sandeen37be2f52012-11-08 11:22:46 -05001049 if ((err = ext4_journal_get_write_access(handle, primary[i])))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001050 goto exit_bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001051 }
1052
Mingming Cao617ba132006-10-11 01:20:53 -07001053 if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001054 goto exit_bh;
1055
1056 /*
1057 * Finally we can add each of the reserved backup GDT blocks from
1058 * the new group to its reserved primary GDT block.
1059 */
Yongqiang Yang668f4dc2011-07-27 21:23:13 -04001060 blk = group * EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001061 for (i = 0; i < reserved_gdb; i++) {
1062 int err2;
1063 data = (__le32 *)primary[i]->b_data;
1064 /* printk("reserving backup %lu[%u] = %lu\n",
1065 primary[i]->b_blocknr, gdbackups,
1066 blk + primary[i]->b_blocknr); */
1067 data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
Frank Mayhar03901312009-01-07 00:06:22 -05001068 err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001069 if (!err)
1070 err = err2;
1071 }
harshadsd77147f2017-10-29 09:38:46 -04001072
1073 inode->i_blocks += reserved_gdb * sb->s_blocksize >> (9 - cluster_bits);
Mingming Cao617ba132006-10-11 01:20:53 -07001074 ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001075
1076exit_bh:
1077 while (--res >= 0)
1078 brelse(primary[res]);
1079 brelse(dind);
1080
1081exit_free:
1082 kfree(primary);
1083
1084 return err;
1085}
1086
1087/*
Mingming Cao617ba132006-10-11 01:20:53 -07001088 * Update the backup copies of the ext4 metadata. These don't need to be part
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001089 * of the main resize transaction, because e2fsck will re-write them if there
1090 * is a problem (basically only OOM will cause a problem). However, we
1091 * _should_ update the backups if possible, in case the primary gets trashed
1092 * for some reason and we need to run e2fsck from a backup superblock. The
1093 * important part is that the new block and inode counts are in the backup
1094 * superblocks, and the location of the new group metadata in the GDT backups.
1095 *
Theodore Ts'o32ed5052009-04-25 22:53:39 -04001096 * We do not need take the s_resize_lock for this, because these
1097 * blocks are not otherwise touched by the filesystem code when it is
1098 * mounted. We don't need to worry about last changing from
1099 * sbi->s_groups_count, because the worst that can happen is that we
1100 * do not copy the full number of backups at this time. The resize
1101 * which changed s_groups_count will backup again.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001102 */
Andy Leiserson904dad42015-10-18 00:36:29 -04001103static void update_backups(struct super_block *sb, sector_t blk_off, char *data,
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001104 int size, int meta_bg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001105{
Mingming Cao617ba132006-10-11 01:20:53 -07001106 struct ext4_sb_info *sbi = EXT4_SB(sb);
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001107 ext4_group_t last;
Mingming Cao617ba132006-10-11 01:20:53 -07001108 const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001109 unsigned three = 1;
1110 unsigned five = 5;
1111 unsigned seven = 7;
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001112 ext4_group_t group = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001113 int rest = sb->s_blocksize - size;
1114 handle_t *handle;
1115 int err = 0, err2;
1116
Theodore Ts'o9924a922013-02-08 21:59:22 -05001117 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001118 if (IS_ERR(handle)) {
1119 group = 1;
1120 err = PTR_ERR(handle);
1121 goto exit_err;
1122 }
1123
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001124 if (meta_bg == 0) {
1125 group = ext4_list_backups(sb, &three, &five, &seven);
1126 last = sbi->s_groups_count;
1127 } else {
Andy Leiserson904dad42015-10-18 00:36:29 -04001128 group = ext4_get_group_number(sb, blk_off) + 1;
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001129 last = (ext4_group_t)(group + EXT4_DESC_PER_BLOCK(sb) - 2);
1130 }
1131
1132 while (group < sbi->s_groups_count) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001133 struct buffer_head *bh;
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001134 ext4_fsblk_t backup_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001135
1136 /* Out of journal space, and can't get more - abort - so sad */
Jan Kara83448bd2019-11-05 17:44:29 +01001137 err = ext4_resize_ensure_credits_batch(handle, 1);
Jan Karaa4130362019-11-05 17:44:16 +01001138 if (err < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001139 break;
1140
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001141 if (meta_bg == 0)
Jan Kara9378c672014-10-30 10:52:57 -04001142 backup_block = ((ext4_fsblk_t)group) * bpg + blk_off;
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001143 else
1144 backup_block = (ext4_group_first_block_no(sb, group) +
1145 ext4_bg_has_super(sb, group));
1146
1147 bh = sb_getblk(sb, backup_block);
Wang Shilongaebf0242013-01-12 16:28:47 -05001148 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001149 err = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001150 break;
1151 }
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001152 ext4_debug("update metadata backup %llu(+%llu)\n",
1153 backup_block, backup_block -
1154 ext4_group_first_block_no(sb, group));
liang xie5d601252014-05-12 22:06:43 -04001155 BUFFER_TRACE(bh, "get_write_access");
Vasily Averinea0abbb2018-11-03 17:11:19 -04001156 if ((err = ext4_journal_get_write_access(handle, bh))) {
1157 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001158 break;
Vasily Averinea0abbb2018-11-03 17:11:19 -04001159 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001160 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");
Yongqiang Yangbb08c1e2012-01-03 23:20:50 -05001239 err = ext4_journal_get_write_access(handle, gdb_bh);
1240
1241 if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group))
1242 err = reserve_backup_gdb(handle, resize_inode, group);
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001243 } else if (meta_bg != 0) {
1244 err = add_new_gdb_meta_bg(sb, handle, group);
1245 } else {
Yongqiang Yangbb08c1e2012-01-03 23:20:50 -05001246 err = add_new_gdb(handle, resize_inode, group);
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001247 }
Yongqiang Yangbb08c1e2012-01-03 23:20:50 -05001248 if (err)
1249 break;
1250 }
1251 return err;
1252}
1253
Darrick J. Wong41a246d2012-04-29 18:33:10 -04001254static struct buffer_head *ext4_get_bitmap(struct super_block *sb, __u64 block)
1255{
1256 struct buffer_head *bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05001257 if (unlikely(!bh))
Darrick J. Wong41a246d2012-04-29 18:33:10 -04001258 return NULL;
Dmitry Monakhov7f1468d2012-09-25 23:19:25 -04001259 if (!bh_uptodate_or_lock(bh)) {
zhangyi (F)2d069c02020-09-24 15:33:33 +08001260 if (ext4_read_bh(bh, 0, NULL) < 0) {
Dmitry Monakhov7f1468d2012-09-25 23:19:25 -04001261 brelse(bh);
1262 return NULL;
1263 }
Darrick J. Wong41a246d2012-04-29 18:33:10 -04001264 }
Darrick J. Wong41a246d2012-04-29 18:33:10 -04001265
1266 return bh;
1267}
1268
1269static int ext4_set_bitmap_checksums(struct super_block *sb,
1270 ext4_group_t group,
1271 struct ext4_group_desc *gdp,
1272 struct ext4_new_group_data *group_data)
1273{
1274 struct buffer_head *bh;
1275
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04001276 if (!ext4_has_metadata_csum(sb))
Darrick J. Wong41a246d2012-04-29 18:33:10 -04001277 return 0;
1278
1279 bh = ext4_get_bitmap(sb, group_data->inode_bitmap);
1280 if (!bh)
1281 return -EIO;
1282 ext4_inode_bitmap_csum_set(sb, group, gdp, bh,
1283 EXT4_INODES_PER_GROUP(sb) / 8);
1284 brelse(bh);
1285
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -04001286 bh = ext4_get_bitmap(sb, group_data->block_bitmap);
1287 if (!bh)
1288 return -EIO;
Tao Ma79f1ba42012-10-22 00:34:32 -04001289 ext4_block_bitmap_csum_set(sb, group, gdp, bh);
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -04001290 brelse(bh);
1291
Darrick J. Wong41a246d2012-04-29 18:33:10 -04001292 return 0;
1293}
1294
Yongqiang Yang083f5b22012-01-03 23:37:31 -05001295/*
1296 * ext4_setup_new_descs() will set up the group descriptor descriptors of a flex bg
1297 */
1298static int ext4_setup_new_descs(handle_t *handle, struct super_block *sb,
1299 struct ext4_new_flex_group_data *flex_gd)
1300{
1301 struct ext4_new_group_data *group_data = flex_gd->groups;
1302 struct ext4_group_desc *gdp;
1303 struct ext4_sb_info *sbi = EXT4_SB(sb);
1304 struct buffer_head *gdb_bh;
1305 ext4_group_t group;
1306 __u16 *bg_flags = flex_gd->bg_flags;
1307 int i, gdb_off, gdb_num, err = 0;
harshadsd77147f2017-10-29 09:38:46 -04001308
Yongqiang Yang083f5b22012-01-03 23:37:31 -05001309
1310 for (i = 0; i < flex_gd->count; i++, group_data++, bg_flags++) {
1311 group = group_data->group;
1312
1313 gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
1314 gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
1315
1316 /*
1317 * get_write_access() has been called on gdb_bh by ext4_add_new_desc().
1318 */
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05001319 gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc, gdb_num);
Yongqiang Yang083f5b22012-01-03 23:37:31 -05001320 /* Update group descriptor block for new group */
Theodore Ts'o2716b802012-05-28 17:47:52 -04001321 gdp = (struct ext4_group_desc *)(gdb_bh->b_data +
Yongqiang Yang083f5b22012-01-03 23:37:31 -05001322 gdb_off * EXT4_DESC_SIZE(sb));
1323
1324 memset(gdp, 0, EXT4_DESC_SIZE(sb));
1325 ext4_block_bitmap_set(sb, gdp, group_data->block_bitmap);
1326 ext4_inode_bitmap_set(sb, gdp, group_data->inode_bitmap);
Darrick J. Wong41a246d2012-04-29 18:33:10 -04001327 err = ext4_set_bitmap_checksums(sb, group, gdp, group_data);
1328 if (err) {
1329 ext4_std_error(sb, err);
1330 break;
1331 }
1332
Yongqiang Yang083f5b22012-01-03 23:37:31 -05001333 ext4_inode_table_set(sb, gdp, group_data->inode_table);
1334 ext4_free_group_clusters_set(sb, gdp,
harshadsd77147f2017-10-29 09:38:46 -04001335 group_data->free_clusters_count);
Yongqiang Yang083f5b22012-01-03 23:37:31 -05001336 ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
Theodore Ts'o93f90522012-09-12 14:32:42 -04001337 if (ext4_has_group_desc_csum(sb))
1338 ext4_itable_unused_set(sb, gdp,
1339 EXT4_INODES_PER_GROUP(sb));
Yongqiang Yang083f5b22012-01-03 23:37:31 -05001340 gdp->bg_flags = cpu_to_le16(*bg_flags);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04001341 ext4_group_desc_csum_set(sb, group, gdp);
Yongqiang Yang083f5b22012-01-03 23:37:31 -05001342
1343 err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
1344 if (unlikely(err)) {
1345 ext4_std_error(sb, err);
1346 break;
1347 }
1348
1349 /*
1350 * We can allocate memory for mb_alloc based on the new group
1351 * descriptor
1352 */
1353 err = ext4_mb_add_groupinfo(sb, group, gdp);
1354 if (err)
1355 break;
1356 }
1357 return err;
1358}
1359
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001360/*
1361 * ext4_update_super() updates the super block so that the newly added
1362 * groups can be seen by the filesystem.
1363 *
1364 * @sb: super block
1365 * @flex_gd: new added groups
1366 */
1367static void ext4_update_super(struct super_block *sb,
1368 struct ext4_new_flex_group_data *flex_gd)
1369{
1370 ext4_fsblk_t blocks_count = 0;
1371 ext4_fsblk_t free_blocks = 0;
1372 ext4_fsblk_t reserved_blocks = 0;
1373 struct ext4_new_group_data *group_data = flex_gd->groups;
1374 struct ext4_sb_info *sbi = EXT4_SB(sb);
1375 struct ext4_super_block *es = sbi->s_es;
Theodore Ts'o8a991842012-07-22 20:23:31 -04001376 int i;
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001377
1378 BUG_ON(flex_gd->count == 0 || group_data == NULL);
1379 /*
1380 * Make the new blocks and inodes valid next. We do this before
1381 * increasing the group count so that once the group is enabled,
1382 * all of its blocks and inodes are already valid.
1383 *
1384 * We always allocate group-by-group, then block-by-block or
1385 * inode-by-inode within a group, so enabling these
1386 * blocks/inodes before the group is live won't actually let us
1387 * allocate the new space yet.
1388 */
1389 for (i = 0; i < flex_gd->count; i++) {
1390 blocks_count += group_data[i].blocks_count;
harshadsd77147f2017-10-29 09:38:46 -04001391 free_blocks += EXT4_C2B(sbi, group_data[i].free_clusters_count);
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001392 }
1393
1394 reserved_blocks = ext4_r_blocks_count(es) * 100;
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001395 reserved_blocks = div64_u64(reserved_blocks, ext4_blocks_count(es));
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001396 reserved_blocks *= blocks_count;
1397 do_div(reserved_blocks, 100);
1398
Jan Kara05c2c002020-12-16 11:18:39 +01001399 lock_buffer(sbi->s_sbh);
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001400 ext4_blocks_count_set(es, ext4_blocks_count(es) + blocks_count);
Darrick J. Wong636d7e2e2012-03-20 15:46:11 -04001401 ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + free_blocks);
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001402 le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb) *
1403 flex_gd->count);
Darrick J. Wong636d7e2e2012-03-20 15:46:11 -04001404 le32_add_cpu(&es->s_free_inodes_count, EXT4_INODES_PER_GROUP(sb) *
1405 flex_gd->count);
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001406
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001407 ext4_debug("free blocks count %llu", ext4_free_blocks_count(es));
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001408 /*
1409 * We need to protect s_groups_count against other CPUs seeing
1410 * inconsistent state in the superblock.
1411 *
1412 * The precise rules we use are:
1413 *
1414 * * Writers must perform a smp_wmb() after updating all
1415 * dependent data and before modifying the groups count
1416 *
1417 * * Readers must perform an smp_rmb() after reading the groups
1418 * count and before reading any dependent data.
1419 *
1420 * NB. These rules can be relaxed when checking the group count
1421 * while freeing data, as we can only allocate from a block
1422 * group after serialising against the group count, and we can
1423 * only then free after serialising in turn against that
1424 * allocation.
1425 */
1426 smp_wmb();
1427
1428 /* Update the global fs size fields */
1429 sbi->s_groups_count += flex_gd->count;
Theodore Ts'oc5c72d82013-04-21 20:19:43 -04001430 sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
1431 (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001432
1433 /* Update the reserved block counts only once the new group is
1434 * active. */
1435 ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
1436 reserved_blocks);
Jan Kara05c2c002020-12-16 11:18:39 +01001437 ext4_superblock_csum_set(sb);
1438 unlock_buffer(sbi->s_sbh);
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001439
1440 /* Update the free space counts */
1441 percpu_counter_add(&sbi->s_freeclusters_counter,
Lukas Czerner810da242013-03-02 17:18:58 -05001442 EXT4_NUM_B2C(sbi, free_blocks));
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001443 percpu_counter_add(&sbi->s_freeinodes_counter,
1444 EXT4_INODES_PER_GROUP(sb) * flex_gd->count);
1445
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001446 ext4_debug("free blocks count %llu",
1447 percpu_counter_read(&sbi->s_freeclusters_counter));
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001448 if (ext4_has_feature_flex_bg(sb) && sbi->s_log_groups_per_flex) {
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001449 ext4_group_t flex_group;
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08001450 struct flex_groups *fg;
1451
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001452 flex_group = ext4_flex_group(sbi, group_data[0].group);
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08001453 fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04001454 atomic64_add(EXT4_NUM_B2C(sbi, free_blocks),
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08001455 &fg->free_clusters);
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001456 atomic_add(EXT4_INODES_PER_GROUP(sb) * flex_gd->count,
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08001457 &fg->free_inodes);
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001458 }
1459
Theodore Ts'o952fc182012-07-09 16:27:05 -04001460 /*
1461 * Update the fs overhead information
1462 */
1463 ext4_calculate_overhead(sb);
1464
Yongqiang Yang2e10e2f2012-01-03 23:41:39 -05001465 if (test_opt(sb, DEBUG))
1466 printk(KERN_DEBUG "EXT4-fs: added group %u:"
1467 "%llu blocks(%llu free %llu reserved)\n", flex_gd->count,
1468 blocks_count, free_blocks, reserved_blocks);
1469}
1470
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001471/* Add a flex group to an fs. Ensure we handle all possible error conditions
1472 * _before_ we start modifying the filesystem, because we cannot abort the
1473 * transaction and not have it write the data to disk.
1474 */
1475static int ext4_flex_group_add(struct super_block *sb,
1476 struct inode *resize_inode,
1477 struct ext4_new_flex_group_data *flex_gd)
1478{
1479 struct ext4_sb_info *sbi = EXT4_SB(sb);
1480 struct ext4_super_block *es = sbi->s_es;
1481 ext4_fsblk_t o_blocks_count;
1482 ext4_grpblk_t last;
1483 ext4_group_t group;
1484 handle_t *handle;
1485 unsigned reserved_gdb;
1486 int err = 0, err2 = 0, credit;
1487
1488 BUG_ON(!flex_gd->count || !flex_gd->groups || !flex_gd->bg_flags);
1489
1490 reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks);
1491 o_blocks_count = ext4_blocks_count(es);
1492 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1493 BUG_ON(last);
1494
1495 err = setup_new_flex_group_blocks(sb, flex_gd);
1496 if (err)
1497 goto exit;
1498 /*
1499 * We will always be modifying at least the superblock and GDT
Jan Kara2c869b22015-05-02 23:58:32 -04001500 * blocks. If we are adding a group past the last current GDT block,
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001501 * we will also modify the inode and the dindirect block. If we
1502 * are adding a group with superblock/GDT backups we will also
1503 * modify each of the reserved GDT dindirect blocks.
1504 */
Jan Kara2c869b22015-05-02 23:58:32 -04001505 credit = 3; /* sb, resize inode, resize inode dindirect */
1506 /* GDT blocks */
1507 credit += 1 + DIV_ROUND_UP(flex_gd->count, EXT4_DESC_PER_BLOCK(sb));
1508 credit += reserved_gdb; /* Reserved GDT dindirect blocks */
Theodore Ts'o9924a922013-02-08 21:59:22 -05001509 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credit);
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001510 if (IS_ERR(handle)) {
1511 err = PTR_ERR(handle);
1512 goto exit;
1513 }
1514
liang xie5d601252014-05-12 22:06:43 -04001515 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001516 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
1517 if (err)
1518 goto exit_journal;
1519
1520 group = flex_gd->groups[0].group;
Jun Piao49598e02018-01-11 13:17:49 -05001521 BUG_ON(group != sbi->s_groups_count);
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001522 err = ext4_add_new_descs(handle, sb, group,
1523 resize_inode, flex_gd->count);
1524 if (err)
1525 goto exit_journal;
1526
1527 err = ext4_setup_new_descs(handle, sb, flex_gd);
1528 if (err)
1529 goto exit_journal;
1530
1531 ext4_update_super(sb, flex_gd);
1532
Jan Karaa3f5cf12020-12-16 11:18:44 +01001533 err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001534
1535exit_journal:
1536 err2 = ext4_journal_stop(handle);
1537 if (!err)
1538 err = err2;
1539
1540 if (!err) {
Yongqiang Yang2ebd1702012-09-05 01:27:50 -04001541 int gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
1542 int gdb_num_end = ((group + flex_gd->count - 1) /
1543 EXT4_DESC_PER_BLOCK(sb));
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001544 int meta_bg = ext4_has_feature_meta_bg(sb);
Tao Ma0acdb882012-09-26 00:08:57 -04001545 sector_t old_gdb = 0;
Yongqiang Yang2ebd1702012-09-05 01:27:50 -04001546
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001547 update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001548 sizeof(struct ext4_super_block), 0);
Yongqiang Yang2ebd1702012-09-05 01:27:50 -04001549 for (; gdb_num <= gdb_num_end; gdb_num++) {
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001550 struct buffer_head *gdb_bh;
Yongqiang Yang2ebd1702012-09-05 01:27:50 -04001551
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05001552 gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc,
1553 gdb_num);
Tao Ma0acdb882012-09-26 00:08:57 -04001554 if (old_gdb == gdb_bh->b_blocknr)
1555 continue;
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001556 update_backups(sb, gdb_bh->b_blocknr, gdb_bh->b_data,
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001557 gdb_bh->b_size, meta_bg);
Tao Ma0acdb882012-09-26 00:08:57 -04001558 old_gdb = gdb_bh->b_blocknr;
Yongqiang Yang4bac1f82012-01-03 23:44:38 -05001559 }
1560 }
1561exit:
1562 return err;
1563}
1564
Yongqiang Yang19c52462012-01-04 17:09:44 -05001565static int ext4_setup_next_flex_gd(struct super_block *sb,
1566 struct ext4_new_flex_group_data *flex_gd,
1567 ext4_fsblk_t n_blocks_count,
1568 unsigned long flexbg_size)
1569{
harshadsd77147f2017-10-29 09:38:46 -04001570 struct ext4_sb_info *sbi = EXT4_SB(sb);
1571 struct ext4_super_block *es = sbi->s_es;
Yongqiang Yang19c52462012-01-04 17:09:44 -05001572 struct ext4_new_group_data *group_data = flex_gd->groups;
1573 ext4_fsblk_t o_blocks_count;
1574 ext4_group_t n_group;
1575 ext4_group_t group;
1576 ext4_group_t last_group;
1577 ext4_grpblk_t last;
harshadsd77147f2017-10-29 09:38:46 -04001578 ext4_grpblk_t clusters_per_group;
Yongqiang Yang19c52462012-01-04 17:09:44 -05001579 unsigned long i;
1580
harshadsd77147f2017-10-29 09:38:46 -04001581 clusters_per_group = EXT4_CLUSTERS_PER_GROUP(sb);
Yongqiang Yang19c52462012-01-04 17:09:44 -05001582
1583 o_blocks_count = ext4_blocks_count(es);
1584
1585 if (o_blocks_count == n_blocks_count)
1586 return 0;
1587
1588 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1589 BUG_ON(last);
1590 ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &last);
1591
1592 last_group = group | (flexbg_size - 1);
1593 if (last_group > n_group)
1594 last_group = n_group;
1595
1596 flex_gd->count = last_group - group + 1;
1597
1598 for (i = 0; i < flex_gd->count; i++) {
1599 int overhead;
1600
1601 group_data[i].group = group + i;
harshadsd77147f2017-10-29 09:38:46 -04001602 group_data[i].blocks_count = EXT4_BLOCKS_PER_GROUP(sb);
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001603 overhead = ext4_group_overhead_blocks(sb, group + i);
harshadsd77147f2017-10-29 09:38:46 -04001604 group_data[i].mdata_blocks = overhead;
1605 group_data[i].free_clusters_count = EXT4_CLUSTERS_PER_GROUP(sb);
Theodore Ts'o7f511862013-01-13 08:41:45 -05001606 if (ext4_has_group_desc_csum(sb)) {
Yongqiang Yang19c52462012-01-04 17:09:44 -05001607 flex_gd->bg_flags[i] = EXT4_BG_BLOCK_UNINIT |
1608 EXT4_BG_INODE_UNINIT;
Theodore Ts'o7f511862013-01-13 08:41:45 -05001609 if (!test_opt(sb, INIT_INODE_TABLE))
1610 flex_gd->bg_flags[i] |= EXT4_BG_INODE_ZEROED;
1611 } else
Yongqiang Yang19c52462012-01-04 17:09:44 -05001612 flex_gd->bg_flags[i] = EXT4_BG_INODE_ZEROED;
1613 }
1614
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04001615 if (last_group == n_group && ext4_has_group_desc_csum(sb))
Yongqiang Yang19c52462012-01-04 17:09:44 -05001616 /* We need to initialize block bitmap of last group. */
1617 flex_gd->bg_flags[i - 1] &= ~EXT4_BG_BLOCK_UNINIT;
1618
harshadsd77147f2017-10-29 09:38:46 -04001619 if ((last_group == n_group) && (last != clusters_per_group - 1)) {
1620 group_data[i - 1].blocks_count = EXT4_C2B(sbi, last + 1);
1621 group_data[i - 1].free_clusters_count -= clusters_per_group -
1622 last - 1;
Yongqiang Yang19c52462012-01-04 17:09:44 -05001623 }
1624
1625 return 1;
1626}
1627
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001628/* Add group descriptor data to an existing or new group descriptor block.
1629 * Ensure we handle all possible error conditions _before_ we start modifying
1630 * the filesystem, because we cannot abort the transaction and not have it
1631 * write the data to disk.
1632 *
1633 * If we are on a GDT block boundary, we need to get the reserved GDT block.
1634 * Otherwise, we may need to add backup GDT blocks for a sparse group.
1635 *
1636 * We only need to hold the superblock lock while we are actually adding
1637 * in the new group's counts to the superblock. Prior to that we have
1638 * not really "added" the group at all. We re-check that we are still
1639 * adding in the last group in case things have changed since verifying.
1640 */
Mingming Cao617ba132006-10-11 01:20:53 -07001641int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001642{
Yongqiang Yang61f296c2012-01-04 17:09:50 -05001643 struct ext4_new_flex_group_data flex_gd;
Mingming Cao617ba132006-10-11 01:20:53 -07001644 struct ext4_sb_info *sbi = EXT4_SB(sb);
1645 struct ext4_super_block *es = sbi->s_es;
1646 int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001647 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001648 struct inode *inode = NULL;
Jon Ernst03b40e32013-06-17 08:56:26 -04001649 int gdb_off;
Yongqiang Yang61f296c2012-01-04 17:09:50 -05001650 int err;
1651 __u16 bg_flags = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001652
Mingming Cao617ba132006-10-11 01:20:53 -07001653 gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001654
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001655 if (gdb_off == 0 && !ext4_has_feature_sparse_super(sb)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001656 ext4_warning(sb, "Can't resize non-sparse filesystem further");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001657 return -EPERM;
1658 }
1659
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001660 if (ext4_blocks_count(es) + input->blocks_count <
1661 ext4_blocks_count(es)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001662 ext4_warning(sb, "blocks_count overflow");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001663 return -EINVAL;
1664 }
1665
Mingming Cao617ba132006-10-11 01:20:53 -07001666 if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001667 le32_to_cpu(es->s_inodes_count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001668 ext4_warning(sb, "inodes_count overflow");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001669 return -EINVAL;
1670 }
1671
1672 if (reserved_gdb || gdb_off == 0) {
ruippan (潘睿)e647e292018-12-04 01:04:12 -05001673 if (!ext4_has_feature_resize_inode(sb) ||
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001674 !le16_to_cpu(es->s_reserved_gdt_blocks)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001675 ext4_warning(sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001676 "No reserved GDT blocks, can't resize");
1677 return -EPERM;
1678 }
Theodore Ts'o8a363972018-12-19 12:29:13 -05001679 inode = ext4_iget(sb, EXT4_RESIZE_INO, EXT4_IGET_SPECIAL);
David Howells1d1fe1e2008-02-07 00:15:37 -08001680 if (IS_ERR(inode)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001681 ext4_warning(sb, "Error opening resize inode");
David Howells1d1fe1e2008-02-07 00:15:37 -08001682 return PTR_ERR(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001683 }
1684 }
1685
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001686
Yongqiang Yang61f296c2012-01-04 17:09:50 -05001687 err = verify_group_input(sb, input);
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -04001688 if (err)
Yongqiang Yang61f296c2012-01-04 17:09:50 -05001689 goto out;
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04001690
Theodore Ts'o117fff12012-09-05 01:29:50 -04001691 err = ext4_alloc_flex_bg_array(sb, input->group + 1);
1692 if (err)
Theodore Ts'o7f511862013-01-13 08:41:45 -05001693 goto out;
Theodore Ts'o117fff12012-09-05 01:29:50 -04001694
Theodore Ts'o28623c22012-09-05 01:31:50 -04001695 err = ext4_mb_alloc_groupinfo(sb, input->group + 1);
1696 if (err)
1697 goto out;
1698
Yongqiang Yang61f296c2012-01-04 17:09:50 -05001699 flex_gd.count = 1;
1700 flex_gd.groups = input;
1701 flex_gd.bg_flags = &bg_flags;
1702 err = ext4_flex_group_add(sb, inode, &flex_gd);
1703out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001704 iput(inode);
1705 return err;
Mingming Cao617ba132006-10-11 01:20:53 -07001706} /* ext4_group_add */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001707
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001708/*
Yongqiang Yang18e31432012-01-03 23:18:50 -05001709 * extend a group without checking assuming that checking has been done.
1710 */
1711static int ext4_group_extend_no_check(struct super_block *sb,
1712 ext4_fsblk_t o_blocks_count, ext4_grpblk_t add)
1713{
1714 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
1715 handle_t *handle;
1716 int err = 0, err2;
1717
1718 /* We will update the superblock, one block bitmap, and
1719 * one group descriptor via ext4_group_add_blocks().
1720 */
Theodore Ts'o9924a922013-02-08 21:59:22 -05001721 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, 3);
Yongqiang Yang18e31432012-01-03 23:18:50 -05001722 if (IS_ERR(handle)) {
1723 err = PTR_ERR(handle);
1724 ext4_warning(sb, "error %d on journal start", err);
1725 return err;
1726 }
1727
liang xie5d601252014-05-12 22:06:43 -04001728 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
Yongqiang Yang18e31432012-01-03 23:18:50 -05001729 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
1730 if (err) {
1731 ext4_warning(sb, "error %d on journal write access", err);
1732 goto errout;
1733 }
1734
Jan Kara05c2c002020-12-16 11:18:39 +01001735 lock_buffer(EXT4_SB(sb)->s_sbh);
Yongqiang Yang18e31432012-01-03 23:18:50 -05001736 ext4_blocks_count_set(es, o_blocks_count + add);
Darrick J. Wong636d7e2e2012-03-20 15:46:11 -04001737 ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + add);
Jan Kara05c2c002020-12-16 11:18:39 +01001738 ext4_superblock_csum_set(sb);
1739 unlock_buffer(EXT4_SB(sb)->s_sbh);
Yongqiang Yang18e31432012-01-03 23:18:50 -05001740 ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
1741 o_blocks_count + add);
1742 /* We add the blocks to the bitmap and set the group need init bit */
1743 err = ext4_group_add_blocks(handle, sb, o_blocks_count, add);
1744 if (err)
1745 goto errout;
Jan Karaa3f5cf12020-12-16 11:18:44 +01001746 ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
Yongqiang Yang18e31432012-01-03 23:18:50 -05001747 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
1748 o_blocks_count + add);
1749errout:
1750 err2 = ext4_journal_stop(handle);
1751 if (err2 && !err)
1752 err = err2;
1753
1754 if (!err) {
1755 if (test_opt(sb, DEBUG))
1756 printk(KERN_DEBUG "EXT4-fs: extended group to %llu "
1757 "blocks\n", ext4_blocks_count(es));
Maarten ter Huurne6ca792e2013-07-01 08:12:08 -04001758 update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr,
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001759 (char *)es, sizeof(struct ext4_super_block), 0);
Yongqiang Yang18e31432012-01-03 23:18:50 -05001760 }
1761 return err;
1762}
1763
1764/*
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001765 * Extend the filesystem to the new number of blocks specified. This entry
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001766 * point is only used to extend the current filesystem to the end of the last
1767 * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
1768 * for emergencies (because it has no dependencies on reserved blocks).
1769 *
Mingming Cao617ba132006-10-11 01:20:53 -07001770 * If we _really_ wanted, we could use default values to call ext4_group_add()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001771 * allow the "remount" trick to work for arbitrary resizing, assuming enough
1772 * GDT blocks are reserved to grow to the desired size.
1773 */
Mingming Cao617ba132006-10-11 01:20:53 -07001774int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
1775 ext4_fsblk_t n_blocks_count)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001776{
Mingming Cao617ba132006-10-11 01:20:53 -07001777 ext4_fsblk_t o_blocks_count;
Mingming Cao617ba132006-10-11 01:20:53 -07001778 ext4_grpblk_t last;
1779 ext4_grpblk_t add;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001780 struct buffer_head *bh;
Yongqiang Yangd89651c2012-01-04 17:09:48 -05001781 int err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04001782 ext4_group_t group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001783
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001784 o_blocks_count = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001785
1786 if (test_opt(sb, DEBUG))
Theodore Ts'o92b97812012-03-19 23:41:49 -04001787 ext4_msg(sb, KERN_DEBUG,
1788 "extending last group from %llu to %llu blocks",
1789 o_blocks_count, n_blocks_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001790
1791 if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
1792 return 0;
1793
1794 if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Theodore Ts'o92b97812012-03-19 23:41:49 -04001795 ext4_msg(sb, KERN_ERR,
1796 "filesystem too large to resize to %llu blocks safely",
1797 n_blocks_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001798 return -EINVAL;
1799 }
1800
1801 if (n_blocks_count < o_blocks_count) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001802 ext4_warning(sb, "can't shrink FS - resize aborted");
Yongqiang Yang8f82f842011-07-26 21:35:44 -04001803 return -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001804 }
1805
1806 /* Handle the remaining blocks in the last group only. */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04001807 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001808
1809 if (last == 0) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001810 ext4_warning(sb, "need to use ext2online to resize further");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001811 return -EPERM;
1812 }
1813
Mingming Cao617ba132006-10-11 01:20:53 -07001814 add = EXT4_BLOCKS_PER_GROUP(sb) - last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001815
1816 if (o_blocks_count + add < o_blocks_count) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001817 ext4_warning(sb, "blocks_count overflow");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001818 return -EINVAL;
1819 }
1820
1821 if (o_blocks_count + add > n_blocks_count)
1822 add = n_blocks_count - o_blocks_count;
1823
1824 if (o_blocks_count + add < n_blocks_count)
Eric Sandeen12062dd2010-02-15 14:19:27 -05001825 ext4_warning(sb, "will only finish group (%llu blocks, %u new)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001826 o_blocks_count + add, add);
1827
1828 /* See if the device is actually as big as what was requested */
zhangyi (F)0a846f42020-09-24 15:33:36 +08001829 bh = ext4_sb_bread(sb, o_blocks_count + add - 1, 0);
1830 if (IS_ERR(bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001831 ext4_warning(sb, "can't read last block, resize aborted");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001832 return -ENOSPC;
1833 }
1834 brelse(bh);
1835
Yongqiang Yangd89651c2012-01-04 17:09:48 -05001836 err = ext4_group_extend_no_check(sb, o_blocks_count, add);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001837 return err;
Mingming Cao617ba132006-10-11 01:20:53 -07001838} /* ext4_group_extend */
Yongqiang Yang19c52462012-01-04 17:09:44 -05001839
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001840
1841static int num_desc_blocks(struct super_block *sb, ext4_group_t groups)
1842{
1843 return (groups + EXT4_DESC_PER_BLOCK(sb) - 1) / EXT4_DESC_PER_BLOCK(sb);
1844}
1845
1846/*
1847 * Release the resize inode and drop the resize_inode feature if there
1848 * are no more reserved gdt blocks, and then convert the file system
1849 * to enable meta_bg
1850 */
1851static int ext4_convert_meta_bg(struct super_block *sb, struct inode *inode)
1852{
1853 handle_t *handle;
1854 struct ext4_sb_info *sbi = EXT4_SB(sb);
1855 struct ext4_super_block *es = sbi->s_es;
Theodore Ts'o59e31c12012-09-19 00:55:56 -04001856 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001857 ext4_fsblk_t nr;
1858 int i, ret, err = 0;
1859 int credits = 1;
1860
1861 ext4_msg(sb, KERN_INFO, "Converting file system to meta_bg");
Theodore Ts'o59e31c12012-09-19 00:55:56 -04001862 if (inode) {
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001863 if (es->s_reserved_gdt_blocks) {
1864 ext4_error(sb, "Unexpected non-zero "
1865 "s_reserved_gdt_blocks");
1866 return -EPERM;
1867 }
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001868
1869 /* Do a quick sanity check of the resize inode */
harshadsd77147f2017-10-29 09:38:46 -04001870 if (inode->i_blocks != 1 << (inode->i_blkbits -
1871 (9 - sbi->s_cluster_bits)))
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001872 goto invalid_resize_inode;
1873 for (i = 0; i < EXT4_N_BLOCKS; i++) {
1874 if (i == EXT4_DIND_BLOCK) {
1875 if (ei->i_data[i])
1876 continue;
1877 else
1878 goto invalid_resize_inode;
1879 }
1880 if (ei->i_data[i])
1881 goto invalid_resize_inode;
1882 }
1883 credits += 3; /* block bitmap, bg descriptor, resize inode */
1884 }
1885
Theodore Ts'o9924a922013-02-08 21:59:22 -05001886 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credits);
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001887 if (IS_ERR(handle))
1888 return PTR_ERR(handle);
1889
liang xie5d601252014-05-12 22:06:43 -04001890 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001891 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
1892 if (err)
1893 goto errout;
1894
Jan Kara05c2c002020-12-16 11:18:39 +01001895 lock_buffer(sbi->s_sbh);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001896 ext4_clear_feature_resize_inode(sb);
1897 ext4_set_feature_meta_bg(sb);
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001898 sbi->s_es->s_first_meta_bg =
1899 cpu_to_le32(num_desc_blocks(sb, sbi->s_groups_count));
Jan Kara05c2c002020-12-16 11:18:39 +01001900 ext4_superblock_csum_set(sb);
1901 unlock_buffer(sbi->s_sbh);
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001902
Jan Karaa3f5cf12020-12-16 11:18:44 +01001903 err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001904 if (err) {
1905 ext4_std_error(sb, err);
1906 goto errout;
1907 }
1908
1909 if (inode) {
1910 nr = le32_to_cpu(ei->i_data[EXT4_DIND_BLOCK]);
1911 ext4_free_blocks(handle, inode, NULL, nr, 1,
1912 EXT4_FREE_BLOCKS_METADATA |
1913 EXT4_FREE_BLOCKS_FORGET);
1914 ei->i_data[EXT4_DIND_BLOCK] = 0;
1915 inode->i_blocks = 0;
1916
1917 err = ext4_mark_inode_dirty(handle, inode);
1918 if (err)
1919 ext4_std_error(sb, err);
1920 }
1921
1922errout:
1923 ret = ext4_journal_stop(handle);
1924 if (!err)
1925 err = ret;
1926 return ret;
1927
1928invalid_resize_inode:
1929 ext4_error(sb, "corrupted/inconsistent resize inode");
1930 return -EINVAL;
1931}
1932
Yongqiang Yang19c52462012-01-04 17:09:44 -05001933/*
1934 * ext4_resize_fs() resizes a fs to new size specified by @n_blocks_count
1935 *
1936 * @sb: super block of the fs to be resized
1937 * @n_blocks_count: the number of blocks resides in the resized fs
1938 */
1939int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count)
1940{
1941 struct ext4_new_flex_group_data *flex_gd = NULL;
1942 struct ext4_sb_info *sbi = EXT4_SB(sb);
1943 struct ext4_super_block *es = sbi->s_es;
1944 struct buffer_head *bh;
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001945 struct inode *resize_inode = NULL;
1946 ext4_grpblk_t add, offset;
Yongqiang Yang19c52462012-01-04 17:09:44 -05001947 unsigned long n_desc_blocks;
1948 unsigned long o_desc_blocks;
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001949 ext4_group_t o_group;
1950 ext4_group_t n_group;
1951 ext4_fsblk_t o_blocks_count;
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001952 ext4_fsblk_t n_blocks_count_retry = 0;
Theodore Ts'o4da4a562012-09-13 10:24:21 -04001953 unsigned long last_update_time = 0;
Theodore Ts'o117fff12012-09-05 01:29:50 -04001954 int err = 0, flexbg_size = 1 << sbi->s_log_groups_per_flex;
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001955 int meta_bg;
Yongqiang Yang19c52462012-01-04 17:09:44 -05001956
Theodore Ts'o59e31c12012-09-19 00:55:56 -04001957 /* See if the device is actually as big as what was requested */
zhangyi (F)0a846f42020-09-24 15:33:36 +08001958 bh = ext4_sb_bread(sb, n_blocks_count - 1, 0);
1959 if (IS_ERR(bh)) {
Theodore Ts'o59e31c12012-09-19 00:55:56 -04001960 ext4_warning(sb, "can't read last block, resize aborted");
1961 return -ENOSPC;
1962 }
1963 brelse(bh);
1964
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001965retry:
Yongqiang Yang19c52462012-01-04 17:09:44 -05001966 o_blocks_count = ext4_blocks_count(es);
1967
Theodore Ts'o59e31c12012-09-19 00:55:56 -04001968 ext4_msg(sb, KERN_INFO, "resizing filesystem from %llu "
1969 "to %llu blocks", o_blocks_count, n_blocks_count);
Yongqiang Yang19c52462012-01-04 17:09:44 -05001970
1971 if (n_blocks_count < o_blocks_count) {
1972 /* On-line shrinking not supported */
1973 ext4_warning(sb, "can't shrink FS - resize aborted");
1974 return -EINVAL;
1975 }
1976
1977 if (n_blocks_count == o_blocks_count)
1978 /* Nothing need to do */
1979 return 0;
1980
Lukas Czernerbd862982013-04-03 23:32:34 -04001981 n_group = ext4_get_group_number(sb, n_blocks_count - 1);
Jan Kara4f2f76f72018-05-25 12:51:25 -04001982 if (n_group >= (0xFFFFFFFFUL / EXT4_INODES_PER_GROUP(sb))) {
Theodore Ts'o3f8a6412013-04-21 22:56:32 -04001983 ext4_warning(sb, "resize would cause inodes_count overflow");
1984 return -EINVAL;
1985 }
Lukas Czernera0ade1d2012-02-20 23:02:06 -05001986 ext4_get_group_no_and_offset(sb, o_blocks_count - 1, &o_group, &offset);
Yongqiang Yang19c52462012-01-04 17:09:44 -05001987
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001988 n_desc_blocks = num_desc_blocks(sb, n_group + 1);
1989 o_desc_blocks = num_desc_blocks(sb, sbi->s_groups_count);
Yongqiang Yang19c52462012-01-04 17:09:44 -05001990
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001991 meta_bg = ext4_has_feature_meta_bg(sb);
Yongqiang Yang19c52462012-01-04 17:09:44 -05001992
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001993 if (ext4_has_feature_resize_inode(sb)) {
Yongqiang Yang01f795f2012-09-05 01:33:50 -04001994 if (meta_bg) {
1995 ext4_error(sb, "resize_inode and meta_bg enabled "
1996 "simultaneously");
1997 return -EINVAL;
1998 }
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04001999 if (n_desc_blocks > o_desc_blocks +
2000 le16_to_cpu(es->s_reserved_gdt_blocks)) {
2001 n_blocks_count_retry = n_blocks_count;
2002 n_desc_blocks = o_desc_blocks +
2003 le16_to_cpu(es->s_reserved_gdt_blocks);
2004 n_group = n_desc_blocks * EXT4_DESC_PER_BLOCK(sb);
Jerry Leeaec51752017-08-06 01:18:31 -04002005 n_blocks_count = (ext4_fsblk_t)n_group *
Jan Karaf96c3ac2019-02-11 13:30:32 -05002006 EXT4_BLOCKS_PER_GROUP(sb) +
2007 le32_to_cpu(es->s_first_data_block);
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04002008 n_group--; /* set to last group number */
Yongqiang Yang01f795f2012-09-05 01:33:50 -04002009 }
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04002010
2011 if (!resize_inode)
Theodore Ts'o8a363972018-12-19 12:29:13 -05002012 resize_inode = ext4_iget(sb, EXT4_RESIZE_INO,
2013 EXT4_IGET_SPECIAL);
Yongqiang Yang01f795f2012-09-05 01:33:50 -04002014 if (IS_ERR(resize_inode)) {
2015 ext4_warning(sb, "Error opening resize inode");
2016 return PTR_ERR(resize_inode);
2017 }
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04002018 }
2019
Theodore Ts'o59e31c12012-09-19 00:55:56 -04002020 if ((!resize_inode && !meta_bg) || n_blocks_count == o_blocks_count) {
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04002021 err = ext4_convert_meta_bg(sb, resize_inode);
2022 if (err)
2023 goto out;
2024 if (resize_inode) {
2025 iput(resize_inode);
2026 resize_inode = NULL;
2027 }
2028 if (n_blocks_count_retry) {
2029 n_blocks_count = n_blocks_count_retry;
2030 n_blocks_count_retry = 0;
2031 goto retry;
2032 }
Yongqiang Yang19c52462012-01-04 17:09:44 -05002033 }
2034
Theodore Ts'of0a459d2018-09-03 22:19:43 -04002035 /*
2036 * Make sure the last group has enough space so that it's
2037 * guaranteed to have enough space for all metadata blocks
2038 * that it might need to hold. (We might not need to store
2039 * the inode table blocks in the last block group, but there
2040 * will be cases where this might be needed.)
2041 */
2042 if ((ext4_group_first_block_no(sb, n_group) +
2043 ext4_group_overhead_blocks(sb, n_group) + 2 +
2044 sbi->s_itb_per_group + sbi->s_cluster_ratio) >= n_blocks_count) {
2045 n_blocks_count = ext4_group_first_block_no(sb, n_group);
2046 n_group--;
2047 n_blocks_count_retry = 0;
2048 if (resize_inode) {
2049 iput(resize_inode);
2050 resize_inode = NULL;
2051 }
2052 goto retry;
2053 }
2054
Lukas Czernera0ade1d2012-02-20 23:02:06 -05002055 /* extend the last group */
2056 if (n_group == o_group)
2057 add = n_blocks_count - o_blocks_count;
2058 else
harshadsd77147f2017-10-29 09:38:46 -04002059 add = EXT4_C2B(sbi, EXT4_CLUSTERS_PER_GROUP(sb) - (offset + 1));
Lukas Czernera0ade1d2012-02-20 23:02:06 -05002060 if (add > 0) {
Yongqiang Yang19c52462012-01-04 17:09:44 -05002061 err = ext4_group_extend_no_check(sb, o_blocks_count, add);
2062 if (err)
2063 goto out;
2064 }
2065
Yongqiang Yangd7574ad2012-09-05 01:23:50 -04002066 if (ext4_blocks_count(es) == n_blocks_count)
Yongqiang Yang19c52462012-01-04 17:09:44 -05002067 goto out;
2068
Theodore Ts'o117fff12012-09-05 01:29:50 -04002069 err = ext4_alloc_flex_bg_array(sb, n_group + 1);
2070 if (err)
Vasily Averinf348e222018-11-06 16:16:01 -05002071 goto out;
Theodore Ts'o117fff12012-09-05 01:29:50 -04002072
Theodore Ts'o28623c22012-09-05 01:31:50 -04002073 err = ext4_mb_alloc_groupinfo(sb, n_group + 1);
2074 if (err)
2075 goto out;
2076
Yongqiang Yang19c52462012-01-04 17:09:44 -05002077 flex_gd = alloc_flex_gd(flexbg_size);
2078 if (flex_gd == NULL) {
2079 err = -ENOMEM;
2080 goto out;
2081 }
2082
2083 /* Add flex groups. Note that a regular group is a
2084 * flex group with 1 group.
2085 */
2086 while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count,
2087 flexbg_size)) {
Theodore Ts'o4da4a562012-09-13 10:24:21 -04002088 if (jiffies - last_update_time > HZ * 10) {
2089 if (last_update_time)
2090 ext4_msg(sb, KERN_INFO,
2091 "resized to %llu blocks",
2092 ext4_blocks_count(es));
2093 last_update_time = jiffies;
2094 }
Yongqiang Yang03c1c292012-09-05 01:21:50 -04002095 if (ext4_alloc_group_tables(sb, flex_gd, flexbg_size) != 0)
2096 break;
Yongqiang Yang19c52462012-01-04 17:09:44 -05002097 err = ext4_flex_group_add(sb, resize_inode, flex_gd);
2098 if (unlikely(err))
2099 break;
2100 }
2101
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04002102 if (!err && n_blocks_count_retry) {
2103 n_blocks_count = n_blocks_count_retry;
2104 n_blocks_count_retry = 0;
2105 free_flex_gd(flex_gd);
2106 flex_gd = NULL;
Vasily Averindb6aee62018-11-06 16:20:40 -05002107 if (resize_inode) {
2108 iput(resize_inode);
2109 resize_inode = NULL;
2110 }
Theodore Ts'o1c6bd712012-09-13 10:19:24 -04002111 goto retry;
2112 }
2113
Yongqiang Yang19c52462012-01-04 17:09:44 -05002114out:
2115 if (flex_gd)
2116 free_flex_gd(flex_gd);
Yongqiang Yang01f795f2012-09-05 01:33:50 -04002117 if (resize_inode != NULL)
2118 iput(resize_inode);
Lukas Czerner6c732842019-03-15 00:22:28 -04002119 if (err)
2120 ext4_warning(sb, "error (%d) occurred during "
2121 "file system resize", err);
2122 ext4_msg(sb, KERN_INFO, "resized filesystem to %llu",
2123 ext4_blocks_count(es));
Yongqiang Yang19c52462012-01-04 17:09:44 -05002124 return err;
2125}