Merge branch develop-3.10-next
[firefly-linux-kernel-4.4.55.git] / fs / ext4 / resize.c
1 /*
2  *  linux/fs/ext4/resize.c
3  *
4  * Support for resizing an ext4 filesystem while it is mounted.
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
12 #define EXT4FS_DEBUG
13
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16
17 #include "ext4_jbd2.h"
18
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
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
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
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
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,
74                               struct ext4_new_group_data *input)
75 {
76         struct ext4_sb_info *sbi = EXT4_SB(sb);
77         struct ext4_super_block *es = sbi->s_es;
78         ext4_fsblk_t start = ext4_blocks_count(es);
79         ext4_fsblk_t end = start + input->blocks_count;
80         ext4_group_t group = input->group;
81         ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group;
82         unsigned overhead = ext4_group_overhead_blocks(sb, group);
83         ext4_fsblk_t metaend = start + overhead;
84         struct buffer_head *bh = NULL;
85         ext4_grpblk_t free_blocks_count, offset;
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))
92                 printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks "
93                        "(%d free, %u reserved)\n",
94                        ext4_bg_has_super(sb, input->group) ? "normal" :
95                        "no-super", input->group, input->blocks_count,
96                        free_blocks_count, input->reserved_blocks);
97
98         ext4_get_group_no_and_offset(sb, start, NULL, &offset);
99         if (group != sbi->s_groups_count)
100                 ext4_warning(sb, "Cannot add at group %u (only %u groups)",
101                              input->group, sbi->s_groups_count);
102         else if (offset != 0)
103                         ext4_warning(sb, "Last group not full");
104         else if (input->reserved_blocks > input->blocks_count / 5)
105                 ext4_warning(sb, "Reserved blocks too high (%u)",
106                              input->reserved_blocks);
107         else if (free_blocks_count < 0)
108                 ext4_warning(sb, "Bad blocks count %u",
109                              input->blocks_count);
110         else if (!(bh = sb_bread(sb, end - 1)))
111                 ext4_warning(sb, "Cannot read last block (%llu)",
112                              end - 1);
113         else if (outside(input->block_bitmap, start, end))
114                 ext4_warning(sb, "Block bitmap not in group (block %llu)",
115                              (unsigned long long)input->block_bitmap);
116         else if (outside(input->inode_bitmap, start, end))
117                 ext4_warning(sb, "Inode bitmap not in group (block %llu)",
118                              (unsigned long long)input->inode_bitmap);
119         else if (outside(input->inode_table, start, end) ||
120                  outside(itend - 1, start, end))
121                 ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)",
122                              (unsigned long long)input->inode_table, itend - 1);
123         else if (input->inode_bitmap == input->block_bitmap)
124                 ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)",
125                              (unsigned long long)input->block_bitmap);
126         else if (inside(input->block_bitmap, input->inode_table, itend))
127                 ext4_warning(sb, "Block bitmap (%llu) in inode table "
128                              "(%llu-%llu)",
129                              (unsigned long long)input->block_bitmap,
130                              (unsigned long long)input->inode_table, itend - 1);
131         else if (inside(input->inode_bitmap, input->inode_table, itend))
132                 ext4_warning(sb, "Inode bitmap (%llu) in inode table "
133                              "(%llu-%llu)",
134                              (unsigned long long)input->inode_bitmap,
135                              (unsigned long long)input->inode_table, itend - 1);
136         else if (inside(input->block_bitmap, start, metaend))
137                 ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)",
138                              (unsigned long long)input->block_bitmap,
139                              start, metaend - 1);
140         else if (inside(input->inode_bitmap, start, metaend))
141                 ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)",
142                              (unsigned long long)input->inode_bitmap,
143                              start, metaend - 1);
144         else if (inside(input->inode_table, start, metaend) ||
145                  inside(itend - 1, start, metaend))
146                 ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table "
147                              "(%llu-%llu)",
148                              (unsigned long long)input->inode_table,
149                              itend - 1, start, metaend - 1);
150         else
151                 err = 0;
152         brelse(bh);
153
154         return err;
155 }
156
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
184         if (flexbg_size >= UINT_MAX / sizeof(struct ext4_new_flex_group_data))
185                 goto out2;
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
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
223  *
224  * Returns 0 on a successful allocation of the metadata blocks in the
225  * block group.
226  */
227 static int ext4_alloc_group_tables(struct super_block *sb,
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;
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;
241         __u16 uninit_mask = (flexbg_size > 1) ? ~EXT4_BG_BLOCK_UNINIT : ~0;
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;
252         if (src_group >= group_data[0].group + flex_gd->count)
253                 return -ENOSPC;
254         start_blk = ext4_group_first_block_no(sb, src_group);
255         last_blk = start_blk + group_data[src_group - group].blocks_count;
256
257         overhead = ext4_group_overhead_blocks(sb, src_group);
258
259         start_blk += overhead;
260
261         /* We collect contiguous blocks as much as possible. */
262         src_group++;
263         for (; src_group <= last_group; src_group++) {
264                 overhead = ext4_group_overhead_blocks(sb, src_group);
265                 if (overhead == 0)
266                         last_blk += group_data[src_group - group].blocks_count;
267                 else
268                         break;
269         }
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++;
276                 group = ext4_get_group_number(sb, start_blk - 1);
277                 group -= group_data[0].group;
278                 group_data[group].free_blocks_count--;
279                 flex_gd->bg_flags[group] &= uninit_mask;
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++;
287                 group = ext4_get_group_number(sb, start_blk - 1);
288                 group -= group_data[0].group;
289                 group_data[group].free_blocks_count--;
290                 flex_gd->bg_flags[group] &= uninit_mask;
291         }
292
293         /* Allocate inode tables */
294         for (; it_index < flex_gd->count; it_index++) {
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)
299                         goto next_group;
300                 group_data[it_index].inode_table = start_blk;
301                 group = ext4_get_group_number(sb, start_blk);
302                 next_group_start = ext4_group_first_block_no(sb, group + 1);
303                 group -= group_data[0].group;
304
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;
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         }
334         return 0;
335 }
336
337 static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
338                                   ext4_fsblk_t blk)
339 {
340         struct buffer_head *bh;
341         int err;
342
343         bh = sb_getblk(sb, blk);
344         if (unlikely(!bh))
345                 return ERR_PTR(-ENOMEM);
346         if ((err = ext4_journal_get_write_access(handle, bh))) {
347                 brelse(bh);
348                 bh = ERR_PTR(err);
349         } else {
350                 memset(bh->b_data, 0, sb->s_blocksize);
351                 set_buffer_uptodate(bh);
352         }
353
354         return bh;
355 }
356
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  */
362 static int extend_or_restart_transaction(handle_t *handle, int thresh)
363 {
364         int err;
365
366         if (ext4_handle_has_enough_credits(handle, thresh))
367                 return 0;
368
369         err = ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA);
370         if (err < 0)
371                 return err;
372         if (err) {
373                 err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA);
374                 if (err)
375                         return err;
376         }
377
378         return 0;
379 }
380
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
403                 group = ext4_get_group_number(sb, block);
404                 start = ext4_group_first_block_no(sb, group);
405                 group -= flex_gd->groups[0].group;
406
407                 count2 = EXT4_BLOCKS_PER_GROUP(sb) - (block - start);
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);
421                 if (unlikely(!bh))
422                         return -ENOMEM;
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;
468         int meta_bg;
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);
474         meta_bg = EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG);
475
476         /* This transaction may be extended/restarted along the way */
477         handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA);
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;
484                 ext4_grpblk_t overhead;
485
486                 gdblocks = ext4_bg_num_gdb(sb, group);
487                 start = ext4_group_first_block_no(sb, group);
488
489                 if (meta_bg == 0 && !ext4_bg_has_super(sb, group))
490                         goto handle_itb;
491
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);
501                 /* Copy all of the GDT blocks into the backup in this group */
502                 for (j = 0; j < gdblocks; j++, block++) {
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);
511                         if (unlikely(!gdb)) {
512                                 err = -ENOMEM;
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
543 handle_itb:
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                 }
572                 overhead = ext4_group_overhead_blocks(sb, group);
573                 if (overhead != 0) {
574                         ext4_debug("mark backup superblock %#04llx (+0)\n",
575                                    start);
576                         ext4_set_bits(bh->b_data, 0, overhead);
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];
626                         start = (&group_data[i].block_bitmap)[j];
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
647 /*
648  * Iterate through the groups which hold BACKUP superblock/GDT copies in an
649  * ext4 filesystem.  The counters should be initialized to 1, 5, and 7 before
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  */
654 static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
655                                   unsigned *five, unsigned *seven)
656 {
657         unsigned *min = three;
658         int mult = 3;
659         unsigned ret;
660
661         if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
662                                         EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
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,
689                                ext4_group_t end,
690                                struct buffer_head *primary)
691 {
692         const ext4_fsblk_t blk = primary->b_blocknr;
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
700         while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
701                 if (le32_to_cpu(*p++) !=
702                     grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
703                         ext4_warning(sb, "reserved GDT %llu"
704                                      " missing grp %d (%llu)",
705                                      blk, grp,
706                                      grp *
707                                      (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
708                                      blk);
709                         return -EINVAL;
710                 }
711                 if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
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,
732                        ext4_group_t group)
733 {
734         struct super_block *sb = inode->i_sb;
735         struct ext4_super_block *es = EXT4_SB(sb)->s_es;
736         unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
737         ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
738         struct buffer_head **o_group_desc, **n_group_desc;
739         struct buffer_head *dind;
740         struct buffer_head *gdb_bh;
741         int gdbackups;
742         struct ext4_iloc iloc;
743         __le32 *data;
744         int err;
745
746         if (test_opt(sb, DEBUG))
747                 printk(KERN_DEBUG
748                        "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
749                        gdb_num);
750
751         /*
752          * If we are not using the primary superblock/GDT copy don't resize,
753          * because the user tools have no way of handling this.  Probably a
754          * bad time to do it anyways.
755          */
756         if (EXT4_SB(sb)->s_sbh->b_blocknr !=
757             le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
758                 ext4_warning(sb, "won't resize using backup superblock at %llu",
759                         (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
760                 return -EPERM;
761         }
762
763         gdb_bh = sb_bread(sb, gdblock);
764         if (!gdb_bh)
765                 return -EIO;
766
767         gdbackups = verify_reserved_gdb(sb, group, gdb_bh);
768         if (gdbackups < 0) {
769                 err = gdbackups;
770                 goto exit_bh;
771         }
772
773         data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
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;
781         if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
782                 ext4_warning(sb, "new group %u GDT block %llu not reserved",
783                              group, gdblock);
784                 err = -EINVAL;
785                 goto exit_dind;
786         }
787
788         err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
789         if (unlikely(err))
790                 goto exit_dind;
791
792         err = ext4_journal_get_write_access(handle, gdb_bh);
793         if (unlikely(err))
794                 goto exit_dind;
795
796         err = ext4_journal_get_write_access(handle, dind);
797         if (unlikely(err))
798                 ext4_std_error(sb, err);
799
800         /* ext4_reserve_inode_write() gets a reference on the iloc */
801         err = ext4_reserve_inode_write(handle, inode, &iloc);
802         if (unlikely(err))
803                 goto exit_dind;
804
805         n_group_desc = ext4_kvmalloc((gdb_num + 1) *
806                                      sizeof(struct buffer_head *),
807                                      GFP_NOFS);
808         if (!n_group_desc) {
809                 err = -ENOMEM;
810                 ext4_warning(sb, "not enough memory for %lu groups",
811                              gdb_num + 1);
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          */
824         data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
825         err = ext4_handle_dirty_metadata(handle, NULL, dind);
826         if (unlikely(err)) {
827                 ext4_std_error(sb, err);
828                 goto exit_inode;
829         }
830         inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
831         ext4_mark_iloc_dirty(handle, inode, &iloc);
832         memset(gdb_bh->b_data, 0, sb->s_blocksize);
833         err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
834         if (unlikely(err)) {
835                 ext4_std_error(sb, err);
836                 goto exit_inode;
837         }
838         brelse(dind);
839
840         o_group_desc = EXT4_SB(sb)->s_group_desc;
841         memcpy(n_group_desc, o_group_desc,
842                EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
843         n_group_desc[gdb_num] = gdb_bh;
844         EXT4_SB(sb)->s_group_desc = n_group_desc;
845         EXT4_SB(sb)->s_gdb_count++;
846         ext4_kvfree(o_group_desc);
847
848         le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
849         err = ext4_handle_dirty_super(handle, sb);
850         if (err)
851                 ext4_std_error(sb, err);
852
853         return err;
854
855 exit_inode:
856         ext4_kvfree(n_group_desc);
857         brelse(iloc.bh);
858 exit_dind:
859         brelse(dind);
860 exit_bh:
861         brelse(gdb_bh);
862
863         ext4_debug("leaving with error %d\n", err);
864         return err;
865 }
866
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
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,
920                               ext4_group_t group)
921 {
922         struct super_block *sb = inode->i_sb;
923         int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
924         struct buffer_head **primary;
925         struct buffer_head *dind;
926         struct ext4_iloc iloc;
927         ext4_fsblk_t blk;
928         __le32 *data, *end;
929         int gdbackups = 0;
930         int res, i;
931         int err;
932
933         primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_NOFS);
934         if (!primary)
935                 return -ENOMEM;
936
937         data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
938         dind = sb_bread(sb, le32_to_cpu(*data));
939         if (!dind) {
940                 err = -EIO;
941                 goto exit_free;
942         }
943
944         blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count;
945         data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count %
946                                          EXT4_ADDR_PER_BLOCK(sb));
947         end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb);
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) {
952                         ext4_warning(sb, "reserved block %llu"
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                 }
964                 gdbackups = verify_reserved_gdb(sb, group, primary[res]);
965                 if (gdbackups < 0) {
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++) {
975                 if ((err = ext4_journal_get_write_access(handle, primary[i])))
976                         goto exit_bh;
977         }
978
979         if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
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          */
986         blk = group * EXT4_BLOCKS_PER_GROUP(sb);
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);
994                 err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]);
995                 if (!err)
996                         err = err2;
997         }
998         inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
999         ext4_mark_iloc_dirty(handle, inode, &iloc);
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 /*
1013  * Update the backup copies of the ext4 metadata.  These don't need to be part
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  *
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.
1027  */
1028 static void update_backups(struct super_block *sb, int blk_off, char *data,
1029                            int size, int meta_bg)
1030 {
1031         struct ext4_sb_info *sbi = EXT4_SB(sb);
1032         ext4_group_t last;
1033         const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
1034         unsigned three = 1;
1035         unsigned five = 5;
1036         unsigned seven = 7;
1037         ext4_group_t group = 0;
1038         int rest = sb->s_blocksize - size;
1039         handle_t *handle;
1040         int err = 0, err2;
1041
1042         handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA);
1043         if (IS_ERR(handle)) {
1044                 group = 1;
1045                 err = PTR_ERR(handle);
1046                 goto exit_err;
1047         }
1048
1049         if (meta_bg == 0) {
1050                 group = ext4_list_backups(sb, &three, &five, &seven);
1051                 last = sbi->s_groups_count;
1052         } else {
1053                 group = ext4_meta_bg_first_group(sb, group) + 1;
1054                 last = (ext4_group_t)(group + EXT4_DESC_PER_BLOCK(sb) - 2);
1055         }
1056
1057         while (group < sbi->s_groups_count) {
1058                 struct buffer_head *bh;
1059                 ext4_fsblk_t backup_block;
1060
1061                 /* Out of journal space, and can't get more - abort - so sad */
1062                 if (ext4_handle_valid(handle) &&
1063                     handle->h_buffer_credits == 0 &&
1064                     ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) &&
1065                     (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
1066                         break;
1067
1068                 if (meta_bg == 0)
1069                         backup_block = ((ext4_fsblk_t)group) * bpg + blk_off;
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);
1075                 if (unlikely(!bh)) {
1076                         err = -ENOMEM;
1077                         break;
1078                 }
1079                 ext4_debug("update metadata backup %llu(+%llu)\n",
1080                            backup_block, backup_block -
1081                            ext4_group_first_block_no(sb, group));
1082                 if ((err = ext4_journal_get_write_access(handle, bh)))
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);
1090                 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1091                 if (unlikely(err))
1092                         ext4_std_error(sb, err);
1093                 brelse(bh);
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;
1101         }
1102         if ((err2 = ext4_journal_stop(handle)) && !err)
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) {
1117                 ext4_warning(sb, "can't update backup for group %u (err %d), "
1118                              "forcing fsck on next reboot", group, err);
1119                 sbi->s_mount_state &= ~EXT4_VALID_FS;
1120                 sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1121                 mark_buffer_dirty(sbi->s_sbh);
1122         }
1123 }
1124
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;
1143         int meta_bg;
1144
1145         meta_bg = EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG);
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);
1165                 } else if (meta_bg != 0) {
1166                         err = add_new_gdb_meta_bg(sb, handle, group);
1167                 } else {
1168                         err = add_new_gdb(handle, resize_inode, group);
1169                 }
1170                 if (err)
1171                         break;
1172         }
1173         return err;
1174 }
1175
1176 static struct buffer_head *ext4_get_bitmap(struct super_block *sb, __u64 block)
1177 {
1178         struct buffer_head *bh = sb_getblk(sb, block);
1179         if (unlikely(!bh))
1180                 return NULL;
1181         if (!bh_uptodate_or_lock(bh)) {
1182                 if (bh_submit_read(bh) < 0) {
1183                         brelse(bh);
1184                         return NULL;
1185                 }
1186         }
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
1209         bh = ext4_get_bitmap(sb, group_data->block_bitmap);
1210         if (!bh)
1211                 return -EIO;
1212         ext4_block_bitmap_csum_set(sb, group, gdp, bh);
1213         brelse(bh);
1214
1215         return 0;
1216 }
1217
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 */
1244                 gdp = (struct ext4_group_desc *)(gdb_bh->b_data +
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);
1250                 err = ext4_set_bitmap_checksums(sb, group, gdp, group_data);
1251                 if (err) {
1252                         ext4_std_error(sb, err);
1253                         break;
1254                 }
1255
1256                 ext4_inode_table_set(sb, gdp, group_data->inode_table);
1257                 ext4_free_group_clusters_set(sb, gdp,
1258                         EXT4_NUM_B2C(sbi, group_data->free_blocks_count));
1259                 ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
1260                 if (ext4_has_group_desc_csum(sb))
1261                         ext4_itable_unused_set(sb, gdp,
1262                                                EXT4_INODES_PER_GROUP(sb));
1263                 gdp->bg_flags = cpu_to_le16(*bg_flags);
1264                 ext4_group_desc_csum_set(sb, group, gdp);
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
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;
1299         int i;
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;
1318         reserved_blocks = div64_u64(reserved_blocks, ext4_blocks_count(es));
1319         reserved_blocks *= blocks_count;
1320         do_div(reserved_blocks, 100);
1321
1322         ext4_blocks_count_set(es, ext4_blocks_count(es) + blocks_count);
1323         ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + free_blocks);
1324         le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb) *
1325                      flex_gd->count);
1326         le32_add_cpu(&es->s_free_inodes_count, EXT4_INODES_PER_GROUP(sb) *
1327                      flex_gd->count);
1328
1329         ext4_debug("free blocks count %llu", ext4_free_blocks_count(es));
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;
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)));
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,
1362                            EXT4_NUM_B2C(sbi, free_blocks));
1363         percpu_counter_add(&sbi->s_freeinodes_counter,
1364                            EXT4_INODES_PER_GROUP(sb) * flex_gd->count);
1365
1366         ext4_debug("free blocks count %llu",
1367                    percpu_counter_read(&sbi->s_freeclusters_counter));
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);
1373                 atomic64_add(EXT4_NUM_B2C(sbi, free_blocks),
1374                              &sbi->s_flex_groups[flex_group].free_clusters);
1375                 atomic_add(EXT4_INODES_PER_GROUP(sb) * flex_gd->count,
1376                            &sbi->s_flex_groups[flex_group].free_inodes);
1377         }
1378
1379         /*
1380          * Update the fs overhead information
1381          */
1382         ext4_calculate_overhead(sb);
1383
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
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;
1425         handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credit);
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) {
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));
1459                 int meta_bg = EXT4_HAS_INCOMPAT_FEATURE(sb,
1460                                 EXT4_FEATURE_INCOMPAT_META_BG);
1461                 sector_t old_gdb = 0;
1462
1463                 update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
1464                                sizeof(struct ext4_super_block), 0);
1465                 for (; gdb_num <= gdb_num_end; gdb_num++) {
1466                         struct buffer_head *gdb_bh;
1467
1468                         gdb_bh = sbi->s_group_desc[gdb_num];
1469                         if (old_gdb == gdb_bh->b_blocknr)
1470                                 continue;
1471                         update_backups(sb, gdb_bh->b_blocknr, gdb_bh->b_data,
1472                                        gdb_bh->b_size, meta_bg);
1473                         old_gdb = gdb_bh->b_blocknr;
1474                 }
1475         }
1476 exit:
1477         return err;
1478 }
1479
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;
1517                 overhead = ext4_group_overhead_blocks(sb, group + i);
1518                 group_data[i].free_blocks_count = blocks_per_group - overhead;
1519                 if (ext4_has_group_desc_csum(sb)) {
1520                         flex_gd->bg_flags[i] = EXT4_BG_BLOCK_UNINIT |
1521                                                EXT4_BG_INODE_UNINIT;
1522                         if (!test_opt(sb, INIT_INODE_TABLE))
1523                                 flex_gd->bg_flags[i] |= EXT4_BG_INODE_ZEROED;
1524                 } else
1525                         flex_gd->bg_flags[i] = EXT4_BG_INODE_ZEROED;
1526         }
1527
1528         if (last_group == n_group && ext4_has_group_desc_csum(sb))
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
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  */
1554 int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
1555 {
1556         struct ext4_new_flex_group_data flex_gd;
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) ?
1560                 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
1561         struct inode *inode = NULL;
1562         int gdb_off, gdb_num;
1563         int err;
1564         __u16 bg_flags = 0;
1565
1566         gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
1567         gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
1568
1569         if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb,
1570                                         EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1571                 ext4_warning(sb, "Can't resize non-sparse filesystem further");
1572                 return -EPERM;
1573         }
1574
1575         if (ext4_blocks_count(es) + input->blocks_count <
1576             ext4_blocks_count(es)) {
1577                 ext4_warning(sb, "blocks_count overflow");
1578                 return -EINVAL;
1579         }
1580
1581         if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
1582             le32_to_cpu(es->s_inodes_count)) {
1583                 ext4_warning(sb, "inodes_count overflow");
1584                 return -EINVAL;
1585         }
1586
1587         if (reserved_gdb || gdb_off == 0) {
1588                 if (!EXT4_HAS_COMPAT_FEATURE(sb,
1589                                              EXT4_FEATURE_COMPAT_RESIZE_INODE)
1590                     || !le16_to_cpu(es->s_reserved_gdt_blocks)) {
1591                         ext4_warning(sb,
1592                                      "No reserved GDT blocks, can't resize");
1593                         return -EPERM;
1594                 }
1595                 inode = ext4_iget(sb, EXT4_RESIZE_INO);
1596                 if (IS_ERR(inode)) {
1597                         ext4_warning(sb, "Error opening resize inode");
1598                         return PTR_ERR(inode);
1599                 }
1600         }
1601
1602
1603         err = verify_group_input(sb, input);
1604         if (err)
1605                 goto out;
1606
1607         err = ext4_alloc_flex_bg_array(sb, input->group + 1);
1608         if (err)
1609                 goto out;
1610
1611         err = ext4_mb_alloc_groupinfo(sb, input->group + 1);
1612         if (err)
1613                 goto out;
1614
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:
1620         iput(inode);
1621         return err;
1622 } /* ext4_group_add */
1623
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          */
1637         handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, 3);
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);
1651         ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + add);
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));
1670                 update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr,
1671                                (char *)es, sizeof(struct ext4_super_block), 0);
1672         }
1673         return err;
1674 }
1675
1676 /*
1677  * Extend the filesystem to the new number of blocks specified.  This entry
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  *
1682  * If we _really_ wanted, we could use default values to call ext4_group_add()
1683  * allow the "remount" trick to work for arbitrary resizing, assuming enough
1684  * GDT blocks are reserved to grow to the desired size.
1685  */
1686 int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
1687                       ext4_fsblk_t n_blocks_count)
1688 {
1689         ext4_fsblk_t o_blocks_count;
1690         ext4_grpblk_t last;
1691         ext4_grpblk_t add;
1692         struct buffer_head *bh;
1693         int err;
1694         ext4_group_t group;
1695
1696         o_blocks_count = ext4_blocks_count(es);
1697
1698         if (test_opt(sb, DEBUG))
1699                 ext4_msg(sb, KERN_DEBUG,
1700                          "extending last group from %llu to %llu blocks",
1701                          o_blocks_count, n_blocks_count);
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)) {
1707                 ext4_msg(sb, KERN_ERR,
1708                          "filesystem too large to resize to %llu blocks safely",
1709                          n_blocks_count);
1710                 if (sizeof(sector_t) < 8)
1711                         ext4_warning(sb, "CONFIG_LBDAF not enabled");
1712                 return -EINVAL;
1713         }
1714
1715         if (n_blocks_count < o_blocks_count) {
1716                 ext4_warning(sb, "can't shrink FS - resize aborted");
1717                 return -EINVAL;
1718         }
1719
1720         /* Handle the remaining blocks in the last group only. */
1721         ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1722
1723         if (last == 0) {
1724                 ext4_warning(sb, "need to use ext2online to resize further");
1725                 return -EPERM;
1726         }
1727
1728         add = EXT4_BLOCKS_PER_GROUP(sb) - last;
1729
1730         if (o_blocks_count + add < o_blocks_count) {
1731                 ext4_warning(sb, "blocks_count overflow");
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)
1739                 ext4_warning(sb, "will only finish group (%llu blocks, %u new)",
1740                              o_blocks_count + add, add);
1741
1742         /* See if the device is actually as big as what was requested */
1743         bh = sb_bread(sb, o_blocks_count + add - 1);
1744         if (!bh) {
1745                 ext4_warning(sb, "can't read last block, resize aborted");
1746                 return -ENOSPC;
1747         }
1748         brelse(bh);
1749
1750         err = ext4_group_extend_no_check(sb, o_blocks_count, add);
1751         return err;
1752 } /* ext4_group_extend */
1753
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;
1770         struct ext4_inode_info *ei = EXT4_I(inode);
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");
1776         if (inode) {
1777                 if (es->s_reserved_gdt_blocks) {
1778                         ext4_error(sb, "Unexpected non-zero "
1779                                    "s_reserved_gdt_blocks");
1780                         return -EPERM;
1781                 }
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
1799         handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credits);
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
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;
1854         struct inode *resize_inode = NULL;
1855         ext4_grpblk_t add, offset;
1856         unsigned long n_desc_blocks;
1857         unsigned long o_desc_blocks;
1858         ext4_group_t o_group;
1859         ext4_group_t n_group;
1860         ext4_fsblk_t o_blocks_count;
1861         ext4_fsblk_t n_blocks_count_retry = 0;
1862         unsigned long last_update_time = 0;
1863         int err = 0, flexbg_size = 1 << sbi->s_log_groups_per_flex;
1864         int meta_bg;
1865
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
1874 retry:
1875         o_blocks_count = ext4_blocks_count(es);
1876
1877         ext4_msg(sb, KERN_INFO, "resizing filesystem from %llu "
1878                  "to %llu blocks", o_blocks_count, n_blocks_count);
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
1890         n_group = ext4_get_group_number(sb, n_blocks_count - 1);
1891         if (n_group > (0xFFFFFFFFUL / EXT4_INODES_PER_GROUP(sb))) {
1892                 ext4_warning(sb, "resize would cause inodes_count overflow");
1893                 return -EINVAL;
1894         }
1895         ext4_get_group_no_and_offset(sb, o_blocks_count - 1, &o_group, &offset);
1896
1897         n_desc_blocks = num_desc_blocks(sb, n_group + 1);
1898         o_desc_blocks = num_desc_blocks(sb, sbi->s_groups_count);
1899
1900         meta_bg = EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG);
1901
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                 }
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);
1914                         n_blocks_count = n_group * EXT4_BLOCKS_PER_GROUP(sb);
1915                         n_group--; /* set to last group number */
1916                 }
1917
1918                 if (!resize_inode)
1919                         resize_inode = ext4_iget(sb, EXT4_RESIZE_INO);
1920                 if (IS_ERR(resize_inode)) {
1921                         ext4_warning(sb, "Error opening resize inode");
1922                         return PTR_ERR(resize_inode);
1923                 }
1924         }
1925
1926         if ((!resize_inode && !meta_bg) || n_blocks_count == o_blocks_count) {
1927                 err = ext4_convert_meta_bg(sb, resize_inode);
1928                 if (err)
1929                         goto out;
1930                 if (resize_inode) {
1931                         iput(resize_inode);
1932                         resize_inode = NULL;
1933                 }
1934                 if (n_blocks_count_retry) {
1935                         n_blocks_count = n_blocks_count_retry;
1936                         n_blocks_count_retry = 0;
1937                         goto retry;
1938                 }
1939         }
1940
1941         /* extend the last group */
1942         if (n_group == o_group)
1943                 add = n_blocks_count - o_blocks_count;
1944         else
1945                 add = EXT4_BLOCKS_PER_GROUP(sb) - (offset + 1);
1946         if (add > 0) {
1947                 err = ext4_group_extend_no_check(sb, o_blocks_count, add);
1948                 if (err)
1949                         goto out;
1950         }
1951
1952         if (ext4_blocks_count(es) == n_blocks_count)
1953                 goto out;
1954
1955         err = ext4_alloc_flex_bg_array(sb, n_group + 1);
1956         if (err)
1957                 return err;
1958
1959         err = ext4_mb_alloc_groupinfo(sb, n_group + 1);
1960         if (err)
1961                 goto out;
1962
1963         flex_gd = alloc_flex_gd(flexbg_size);
1964         if (flex_gd == NULL) {
1965                 err = -ENOMEM;
1966                 goto out;
1967         }
1968
1969         /* Add flex groups. Note that a regular group is a
1970          * flex group with 1 group.
1971          */
1972         while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count,
1973                                               flexbg_size)) {
1974                 if (jiffies - last_update_time > HZ * 10) {
1975                         if (last_update_time)
1976                                 ext4_msg(sb, KERN_INFO,
1977                                          "resized to %llu blocks",
1978                                          ext4_blocks_count(es));
1979                         last_update_time = jiffies;
1980                 }
1981                 if (ext4_alloc_group_tables(sb, flex_gd, flexbg_size) != 0)
1982                         break;
1983                 err = ext4_flex_group_add(sb, resize_inode, flex_gd);
1984                 if (unlikely(err))
1985                         break;
1986         }
1987
1988         if (!err && n_blocks_count_retry) {
1989                 n_blocks_count = n_blocks_count_retry;
1990                 n_blocks_count_retry = 0;
1991                 free_flex_gd(flex_gd);
1992                 flex_gd = NULL;
1993                 goto retry;
1994         }
1995
1996 out:
1997         if (flex_gd)
1998                 free_flex_gd(flex_gd);
1999         if (resize_inode != NULL)
2000                 iput(resize_inode);
2001         ext4_msg(sb, KERN_INFO, "resized filesystem to %llu", n_blocks_count);
2002         return err;
2003 }