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