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