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