ext4: add a function which sets up group blocks of a flex bg
[GitHub/mt8127/android_kernel_alcatel_ttab.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
ac27a0ec
DK
1089/* Add group descriptor data to an existing or new group descriptor block.
1090 * Ensure we handle all possible error conditions _before_ we start modifying
1091 * the filesystem, because we cannot abort the transaction and not have it
1092 * write the data to disk.
1093 *
1094 * If we are on a GDT block boundary, we need to get the reserved GDT block.
1095 * Otherwise, we may need to add backup GDT blocks for a sparse group.
1096 *
1097 * We only need to hold the superblock lock while we are actually adding
1098 * in the new group's counts to the superblock. Prior to that we have
1099 * not really "added" the group at all. We re-check that we are still
1100 * adding in the last group in case things have changed since verifying.
1101 */
617ba13b 1102int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
ac27a0ec 1103{
617ba13b
MC
1104 struct ext4_sb_info *sbi = EXT4_SB(sb);
1105 struct ext4_super_block *es = sbi->s_es;
1106 int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
ac27a0ec
DK
1107 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
1108 struct buffer_head *primary = NULL;
617ba13b 1109 struct ext4_group_desc *gdp;
ac27a0ec
DK
1110 struct inode *inode = NULL;
1111 handle_t *handle;
1112 int gdb_off, gdb_num;
1113 int err, err2;
1114
617ba13b
MC
1115 gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
1116 gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
ac27a0ec 1117
617ba13b
MC
1118 if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb,
1119 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
12062ddd 1120 ext4_warning(sb, "Can't resize non-sparse filesystem further");
ac27a0ec
DK
1121 return -EPERM;
1122 }
1123
bd81d8ee
LV
1124 if (ext4_blocks_count(es) + input->blocks_count <
1125 ext4_blocks_count(es)) {
12062ddd 1126 ext4_warning(sb, "blocks_count overflow");
ac27a0ec
DK
1127 return -EINVAL;
1128 }
1129
617ba13b 1130 if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
ac27a0ec 1131 le32_to_cpu(es->s_inodes_count)) {
12062ddd 1132 ext4_warning(sb, "inodes_count overflow");
ac27a0ec
DK
1133 return -EINVAL;
1134 }
1135
1136 if (reserved_gdb || gdb_off == 0) {
617ba13b 1137 if (!EXT4_HAS_COMPAT_FEATURE(sb,
37609fd5
JB
1138 EXT4_FEATURE_COMPAT_RESIZE_INODE)
1139 || !le16_to_cpu(es->s_reserved_gdt_blocks)) {
12062ddd 1140 ext4_warning(sb,
ac27a0ec
DK
1141 "No reserved GDT blocks, can't resize");
1142 return -EPERM;
1143 }
1d1fe1ee
DH
1144 inode = ext4_iget(sb, EXT4_RESIZE_INO);
1145 if (IS_ERR(inode)) {
12062ddd 1146 ext4_warning(sb, "Error opening resize inode");
1d1fe1ee 1147 return PTR_ERR(inode);
ac27a0ec
DK
1148 }
1149 }
1150
920313a7 1151
ac27a0ec
DK
1152 if ((err = verify_group_input(sb, input)))
1153 goto exit_put;
1154
1155 if ((err = setup_new_group_blocks(sb, input)))
1156 goto exit_put;
1157
1158 /*
1159 * We will always be modifying at least the superblock and a GDT
1160 * block. If we are adding a group past the last current GDT block,
1161 * we will also modify the inode and the dindirect block. If we
1162 * are adding a group with superblock/GDT backups we will also
1163 * modify each of the reserved GDT dindirect blocks.
1164 */
617ba13b
MC
1165 handle = ext4_journal_start_sb(sb,
1166 ext4_bg_has_super(sb, input->group) ?
ac27a0ec
DK
1167 3 + reserved_gdb : 4);
1168 if (IS_ERR(handle)) {
1169 err = PTR_ERR(handle);
1170 goto exit_put;
1171 }
1172
617ba13b 1173 if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh)))
ac27a0ec
DK
1174 goto exit_journal;
1175
2b2d6d01
TT
1176 /*
1177 * We will only either add reserved group blocks to a backup group
1178 * or remove reserved blocks for the first group in a new group block.
1179 * Doing both would be mean more complex code, and sane people don't
1180 * use non-sparse filesystems anymore. This is already checked above.
1181 */
ac27a0ec
DK
1182 if (gdb_off) {
1183 primary = sbi->s_group_desc[gdb_num];
617ba13b 1184 if ((err = ext4_journal_get_write_access(handle, primary)))
ac27a0ec
DK
1185 goto exit_journal;
1186
668f4dc5
YY
1187 if (reserved_gdb && ext4_bg_num_gdb(sb, input->group)) {
1188 err = reserve_backup_gdb(handle, inode, input->group);
1189 if (err)
1190 goto exit_journal;
1191 }
2f919710
YY
1192 } else {
1193 /*
1194 * Note that we can access new group descriptor block safely
1195 * only if add_new_gdb() succeeds.
1196 */
1197 err = add_new_gdb(handle, inode, input->group);
1198 if (err)
1199 goto exit_journal;
1200 primary = sbi->s_group_desc[gdb_num];
1201 }
ac27a0ec 1202
2b2d6d01
TT
1203 /*
1204 * OK, now we've set up the new group. Time to make it active.
1205 *
2b2d6d01
TT
1206 * so we have to be safe wrt. concurrent accesses the group
1207 * data. So we need to be careful to set all of the relevant
1208 * group descriptor data etc. *before* we enable the group.
1209 *
1210 * The key field here is sbi->s_groups_count: as long as
1211 * that retains its old value, nobody is going to access the new
1212 * group.
1213 *
1214 * So first we update all the descriptor metadata for the new
1215 * group; then we update the total disk blocks count; then we
1216 * update the groups count to enable the group; then finally we
1217 * update the free space counts so that the system can start
1218 * using the new disk blocks.
1219 */
ac27a0ec
DK
1220
1221 /* Update group descriptor block for new group */
2856922c
FB
1222 gdp = (struct ext4_group_desc *)((char *)primary->b_data +
1223 gdb_off * EXT4_DESC_SIZE(sb));
ac27a0ec 1224
fdff73f0 1225 memset(gdp, 0, EXT4_DESC_SIZE(sb));
8fadc143
AR
1226 ext4_block_bitmap_set(sb, gdp, input->block_bitmap); /* LV FIXME */
1227 ext4_inode_bitmap_set(sb, gdp, input->inode_bitmap); /* LV FIXME */
1228 ext4_inode_table_set(sb, gdp, input->inode_table); /* LV FIXME */
021b65bb 1229 ext4_free_group_clusters_set(sb, gdp, input->free_blocks_count);
560671a0 1230 ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
fdff73f0 1231 gdp->bg_flags = cpu_to_le16(EXT4_BG_INODE_ZEROED);
717d50e4 1232 gdp->bg_checksum = ext4_group_desc_csum(sbi, input->group, gdp);
ac27a0ec 1233
5f21b0e6
FB
1234 /*
1235 * We can allocate memory for mb_alloc based on the new group
1236 * descriptor
1237 */
920313a7 1238 err = ext4_mb_add_groupinfo(sb, input->group, gdp);
08c3a813 1239 if (err)
c2ea3fde
TT
1240 goto exit_journal;
1241
ac27a0ec
DK
1242 /*
1243 * Make the new blocks and inodes valid next. We do this before
1244 * increasing the group count so that once the group is enabled,
1245 * all of its blocks and inodes are already valid.
1246 *
1247 * We always allocate group-by-group, then block-by-block or
1248 * inode-by-inode within a group, so enabling these
1249 * blocks/inodes before the group is live won't actually let us
1250 * allocate the new space yet.
1251 */
bd81d8ee 1252 ext4_blocks_count_set(es, ext4_blocks_count(es) +
ac27a0ec 1253 input->blocks_count);
e8546d06 1254 le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb));
ac27a0ec
DK
1255
1256 /*
1257 * We need to protect s_groups_count against other CPUs seeing
1258 * inconsistent state in the superblock.
1259 *
1260 * The precise rules we use are:
1261 *
ac27a0ec
DK
1262 * * Writers must perform a smp_wmb() after updating all dependent
1263 * data and before modifying the groups count
1264 *
ac27a0ec
DK
1265 * * Readers must perform an smp_rmb() after reading the groups count
1266 * and before reading any dependent data.
1267 *
1268 * NB. These rules can be relaxed when checking the group count
1269 * while freeing data, as we can only allocate from a block
1270 * group after serialising against the group count, and we can
1271 * only then free after serialising in turn against that
1272 * allocation.
1273 */
1274 smp_wmb();
1275
1276 /* Update the global fs size fields */
1277 sbi->s_groups_count++;
1278
b4097142
TT
1279 err = ext4_handle_dirty_metadata(handle, NULL, primary);
1280 if (unlikely(err)) {
1281 ext4_std_error(sb, err);
1282 goto exit_journal;
1283 }
ac27a0ec
DK
1284
1285 /* Update the reserved block counts only once the new group is
1286 * active. */
bd81d8ee 1287 ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
ac27a0ec
DK
1288 input->reserved_blocks);
1289
1290 /* Update the free space counts */
57042651
TT
1291 percpu_counter_add(&sbi->s_freeclusters_counter,
1292 EXT4_B2C(sbi, input->free_blocks_count));
aa0dff2d 1293 percpu_counter_add(&sbi->s_freeinodes_counter,
617ba13b 1294 EXT4_INODES_PER_GROUP(sb));
ac27a0ec 1295
42007efd
ES
1296 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
1297 sbi->s_log_groups_per_flex) {
c62a11fd
FB
1298 ext4_group_t flex_group;
1299 flex_group = ext4_flex_group(sbi, input->group);
24aaa8ef
TT
1300 atomic_add(EXT4_B2C(sbi, input->free_blocks_count),
1301 &sbi->s_flex_groups[flex_group].free_clusters);
9f24e420
TT
1302 atomic_add(EXT4_INODES_PER_GROUP(sb),
1303 &sbi->s_flex_groups[flex_group].free_inodes);
c62a11fd
FB
1304 }
1305
a0375156 1306 ext4_handle_dirty_super(handle, sb);
ac27a0ec
DK
1307
1308exit_journal:
617ba13b 1309 if ((err2 = ext4_journal_stop(handle)) && !err)
ac27a0ec 1310 err = err2;
2f919710 1311 if (!err && primary) {
ac27a0ec 1312 update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
617ba13b 1313 sizeof(struct ext4_super_block));
ac27a0ec
DK
1314 update_backups(sb, primary->b_blocknr, primary->b_data,
1315 primary->b_size);
1316 }
1317exit_put:
1318 iput(inode);
1319 return err;
617ba13b 1320} /* ext4_group_add */
ac27a0ec 1321
18e31438
YY
1322/*
1323 * extend a group without checking assuming that checking has been done.
1324 */
1325static int ext4_group_extend_no_check(struct super_block *sb,
1326 ext4_fsblk_t o_blocks_count, ext4_grpblk_t add)
1327{
1328 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
1329 handle_t *handle;
1330 int err = 0, err2;
1331
1332 /* We will update the superblock, one block bitmap, and
1333 * one group descriptor via ext4_group_add_blocks().
1334 */
1335 handle = ext4_journal_start_sb(sb, 3);
1336 if (IS_ERR(handle)) {
1337 err = PTR_ERR(handle);
1338 ext4_warning(sb, "error %d on journal start", err);
1339 return err;
1340 }
1341
1342 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
1343 if (err) {
1344 ext4_warning(sb, "error %d on journal write access", err);
1345 goto errout;
1346 }
1347
1348 ext4_blocks_count_set(es, o_blocks_count + add);
1349 ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
1350 o_blocks_count + add);
1351 /* We add the blocks to the bitmap and set the group need init bit */
1352 err = ext4_group_add_blocks(handle, sb, o_blocks_count, add);
1353 if (err)
1354 goto errout;
1355 ext4_handle_dirty_super(handle, sb);
1356 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
1357 o_blocks_count + add);
1358errout:
1359 err2 = ext4_journal_stop(handle);
1360 if (err2 && !err)
1361 err = err2;
1362
1363 if (!err) {
1364 if (test_opt(sb, DEBUG))
1365 printk(KERN_DEBUG "EXT4-fs: extended group to %llu "
1366 "blocks\n", ext4_blocks_count(es));
1367 update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es,
1368 sizeof(struct ext4_super_block));
1369 }
1370 return err;
1371}
1372
2b2d6d01
TT
1373/*
1374 * Extend the filesystem to the new number of blocks specified. This entry
ac27a0ec
DK
1375 * point is only used to extend the current filesystem to the end of the last
1376 * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
1377 * for emergencies (because it has no dependencies on reserved blocks).
1378 *
617ba13b 1379 * If we _really_ wanted, we could use default values to call ext4_group_add()
ac27a0ec
DK
1380 * allow the "remount" trick to work for arbitrary resizing, assuming enough
1381 * GDT blocks are reserved to grow to the desired size.
1382 */
617ba13b
MC
1383int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
1384 ext4_fsblk_t n_blocks_count)
ac27a0ec 1385{
617ba13b 1386 ext4_fsblk_t o_blocks_count;
617ba13b
MC
1387 ext4_grpblk_t last;
1388 ext4_grpblk_t add;
af5bc92d 1389 struct buffer_head *bh;
ac27a0ec 1390 handle_t *handle;
cc7365df 1391 int err, err2;
5f21b0e6 1392 ext4_group_t group;
ac27a0ec 1393
bd81d8ee 1394 o_blocks_count = ext4_blocks_count(es);
ac27a0ec
DK
1395
1396 if (test_opt(sb, DEBUG))
2b79b09d 1397 printk(KERN_DEBUG "EXT4-fs: extending last group from %llu to %llu blocks\n",
ac27a0ec
DK
1398 o_blocks_count, n_blocks_count);
1399
1400 if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
1401 return 0;
1402
1403 if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
617ba13b 1404 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
2ae02107 1405 " too large to resize to %llu blocks safely\n",
ac27a0ec
DK
1406 sb->s_id, n_blocks_count);
1407 if (sizeof(sector_t) < 8)
12062ddd 1408 ext4_warning(sb, "CONFIG_LBDAF not enabled");
ac27a0ec
DK
1409 return -EINVAL;
1410 }
1411
1412 if (n_blocks_count < o_blocks_count) {
12062ddd 1413 ext4_warning(sb, "can't shrink FS - resize aborted");
8f82f840 1414 return -EINVAL;
ac27a0ec
DK
1415 }
1416
1417 /* Handle the remaining blocks in the last group only. */
5f21b0e6 1418 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
ac27a0ec
DK
1419
1420 if (last == 0) {
12062ddd 1421 ext4_warning(sb, "need to use ext2online to resize further");
ac27a0ec
DK
1422 return -EPERM;
1423 }
1424
617ba13b 1425 add = EXT4_BLOCKS_PER_GROUP(sb) - last;
ac27a0ec
DK
1426
1427 if (o_blocks_count + add < o_blocks_count) {
12062ddd 1428 ext4_warning(sb, "blocks_count overflow");
ac27a0ec
DK
1429 return -EINVAL;
1430 }
1431
1432 if (o_blocks_count + add > n_blocks_count)
1433 add = n_blocks_count - o_blocks_count;
1434
1435 if (o_blocks_count + add < n_blocks_count)
12062ddd 1436 ext4_warning(sb, "will only finish group (%llu blocks, %u new)",
ac27a0ec
DK
1437 o_blocks_count + add, add);
1438
1439 /* See if the device is actually as big as what was requested */
2b2d6d01 1440 bh = sb_bread(sb, o_blocks_count + add - 1);
ac27a0ec 1441 if (!bh) {
12062ddd 1442 ext4_warning(sb, "can't read last block, resize aborted");
ac27a0ec
DK
1443 return -ENOSPC;
1444 }
1445 brelse(bh);
1446
1447 /* We will update the superblock, one block bitmap, and
617ba13b 1448 * one group descriptor via ext4_free_blocks().
ac27a0ec 1449 */
617ba13b 1450 handle = ext4_journal_start_sb(sb, 3);
ac27a0ec
DK
1451 if (IS_ERR(handle)) {
1452 err = PTR_ERR(handle);
12062ddd 1453 ext4_warning(sb, "error %d on journal start", err);
ac27a0ec
DK
1454 goto exit_put;
1455 }
1456
617ba13b
MC
1457 if ((err = ext4_journal_get_write_access(handle,
1458 EXT4_SB(sb)->s_sbh))) {
12062ddd 1459 ext4_warning(sb, "error %d on journal write access", err);
617ba13b 1460 ext4_journal_stop(handle);
ac27a0ec
DK
1461 goto exit_put;
1462 }
bd81d8ee 1463 ext4_blocks_count_set(es, o_blocks_count + add);
c549a95d 1464 ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
ac27a0ec 1465 o_blocks_count + add);
e21675d4 1466 /* We add the blocks to the bitmap and set the group need init bit */
cc7365df 1467 err = ext4_group_add_blocks(handle, sb, o_blocks_count, add);
a0375156 1468 ext4_handle_dirty_super(handle, sb);
2ae02107 1469 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
ac27a0ec 1470 o_blocks_count + add);
cc7365df
YY
1471 err2 = ext4_journal_stop(handle);
1472 if (!err && err2)
1473 err = err2;
1474
1475 if (err)
ac27a0ec 1476 goto exit_put;
5f21b0e6 1477
ac27a0ec 1478 if (test_opt(sb, DEBUG))
bd81d8ee
LV
1479 printk(KERN_DEBUG "EXT4-fs: extended group to %llu blocks\n",
1480 ext4_blocks_count(es));
617ba13b
MC
1481 update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es,
1482 sizeof(struct ext4_super_block));
ac27a0ec
DK
1483exit_put:
1484 return err;
617ba13b 1485} /* ext4_group_extend */