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