1
2
3
4
5
6
7
8
9
10
11
12#define EXT4FS_DEBUG
13
14#include <linux/errno.h>
15#include <linux/slab.h>
16
17#include "ext4_jbd2.h"
18
19#define outside(b, first, last) ((b) < (first) || (b) >= (last))
20#define inside(b, first, last) ((b) >= (first) && (b) < (last))
21
22static int verify_group_input(struct super_block *sb,
23 struct ext4_new_group_data *input)
24{
25 struct ext4_sb_info *sbi = EXT4_SB(sb);
26 struct ext4_super_block *es = sbi->s_es;
27 ext4_fsblk_t start = ext4_blocks_count(es);
28 ext4_fsblk_t end = start + input->blocks_count;
29 ext4_group_t group = input->group;
30 ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group;
31 unsigned overhead = ext4_bg_has_super(sb, group) ?
32 (1 + ext4_bg_num_gdb(sb, group) +
33 le16_to_cpu(es->s_reserved_gdt_blocks)) : 0;
34 ext4_fsblk_t metaend = start + overhead;
35 struct buffer_head *bh = NULL;
36 ext4_grpblk_t free_blocks_count, offset;
37 int err = -EINVAL;
38
39 input->free_blocks_count = free_blocks_count =
40 input->blocks_count - 2 - overhead - sbi->s_itb_per_group;
41
42 if (test_opt(sb, DEBUG))
43 printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks "
44 "(%d free, %u reserved)\n",
45 ext4_bg_has_super(sb, input->group) ? "normal" :
46 "no-super", input->group, input->blocks_count,
47 free_blocks_count, input->reserved_blocks);
48
49 ext4_get_group_no_and_offset(sb, start, NULL, &offset);
50 if (group != sbi->s_groups_count)
51 ext4_warning(sb, __func__,
52 "Cannot add at group %u (only %u groups)",
53 input->group, sbi->s_groups_count);
54 else if (offset != 0)
55 ext4_warning(sb, __func__, "Last group not full");
56 else if (input->reserved_blocks > input->blocks_count / 5)
57 ext4_warning(sb, __func__, "Reserved blocks too high (%u)",
58 input->reserved_blocks);
59 else if (free_blocks_count < 0)
60 ext4_warning(sb, __func__, "Bad blocks count %u",
61 input->blocks_count);
62 else if (!(bh = sb_bread(sb, end - 1)))
63 ext4_warning(sb, __func__,
64 "Cannot read last block (%llu)",
65 end - 1);
66 else if (outside(input->block_bitmap, start, end))
67 ext4_warning(sb, __func__,
68 "Block bitmap not in group (block %llu)",
69 (unsigned long long)input->block_bitmap);
70 else if (outside(input->inode_bitmap, start, end))
71 ext4_warning(sb, __func__,
72 "Inode bitmap not in group (block %llu)",
73 (unsigned long long)input->inode_bitmap);
74 else if (outside(input->inode_table, start, end) ||
75 outside(itend - 1, start, end))
76 ext4_warning(sb, __func__,
77 "Inode table not in group (blocks %llu-%llu)",
78 (unsigned long long)input->inode_table, itend - 1);
79 else if (input->inode_bitmap == input->block_bitmap)
80 ext4_warning(sb, __func__,
81 "Block bitmap same as inode bitmap (%llu)",
82 (unsigned long long)input->block_bitmap);
83 else if (inside(input->block_bitmap, input->inode_table, itend))
84 ext4_warning(sb, __func__,
85 "Block bitmap (%llu) in inode table (%llu-%llu)",
86 (unsigned long long)input->block_bitmap,
87 (unsigned long long)input->inode_table, itend - 1);
88 else if (inside(input->inode_bitmap, input->inode_table, itend))
89 ext4_warning(sb, __func__,
90 "Inode bitmap (%llu) in inode table (%llu-%llu)",
91 (unsigned long long)input->inode_bitmap,
92 (unsigned long long)input->inode_table, itend - 1);
93 else if (inside(input->block_bitmap, start, metaend))
94 ext4_warning(sb, __func__,
95 "Block bitmap (%llu) in GDT table"
96 " (%llu-%llu)",
97 (unsigned long long)input->block_bitmap,
98 start, metaend - 1);
99 else if (inside(input->inode_bitmap, start, metaend))
100 ext4_warning(sb, __func__,
101 "Inode bitmap (%llu) in GDT table"
102 " (%llu-%llu)",
103 (unsigned long long)input->inode_bitmap,
104 start, metaend - 1);
105 else if (inside(input->inode_table, start, metaend) ||
106 inside(itend - 1, start, metaend))
107 ext4_warning(sb, __func__,
108 "Inode table (%llu-%llu) overlaps"
109 "GDT table (%llu-%llu)",
110 (unsigned long long)input->inode_table,
111 itend - 1, start, metaend - 1);
112 else
113 err = 0;
114 brelse(bh);
115
116 return err;
117}
118
119static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
120 ext4_fsblk_t blk)
121{
122 struct buffer_head *bh;
123 int err;
124
125 bh = sb_getblk(sb, blk);
126 if (!bh)
127 return ERR_PTR(-EIO);
128 if ((err = ext4_journal_get_write_access(handle, bh))) {
129 brelse(bh);
130 bh = ERR_PTR(err);
131 } else {
132 lock_buffer(bh);
133 memset(bh->b_data, 0, sb->s_blocksize);
134 set_buffer_uptodate(bh);
135 unlock_buffer(bh);
136 }
137
138 return bh;
139}
140
141
142
143
144
145
146static int extend_or_restart_transaction(handle_t *handle, int thresh,
147 struct buffer_head *bh)
148{
149 int err;
150
151 if (ext4_handle_has_enough_credits(handle, thresh))
152 return 0;
153
154 err = ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA);
155 if (err < 0)
156 return err;
157 if (err) {
158 if ((err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
159 return err;
160 if ((err = ext4_journal_get_write_access(handle, bh)))
161 return err;
162 }
163
164 return 0;
165}
166
167
168
169
170
171
172
173
174static int setup_new_group_blocks(struct super_block *sb,
175 struct ext4_new_group_data *input)
176{
177 struct ext4_sb_info *sbi = EXT4_SB(sb);
178 ext4_fsblk_t start = ext4_group_first_block_no(sb, input->group);
179 int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
180 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0;
181 unsigned long gdblocks = ext4_bg_num_gdb(sb, input->group);
182 struct buffer_head *bh;
183 handle_t *handle;
184 ext4_fsblk_t block;
185 ext4_grpblk_t bit;
186 int i;
187 int err = 0, err2;
188
189
190 handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
191
192 if (IS_ERR(handle))
193 return PTR_ERR(handle);
194
195 mutex_lock(&sbi->s_resize_lock);
196 if (input->group != sbi->s_groups_count) {
197 err = -EBUSY;
198 goto exit_journal;
199 }
200
201 if (IS_ERR(bh = bclean(handle, sb, input->block_bitmap))) {
202 err = PTR_ERR(bh);
203 goto exit_journal;
204 }
205
206 if (ext4_bg_has_super(sb, input->group)) {
207 ext4_debug("mark backup superblock %#04llx (+0)\n", start);
208 ext4_set_bit(0, bh->b_data);
209 }
210
211
212 for (i = 0, bit = 1, block = start + 1;
213 i < gdblocks; i++, block++, bit++) {
214 struct buffer_head *gdb;
215
216 ext4_debug("update backup group %#04llx (+%d)\n", block, bit);
217
218 if ((err = extend_or_restart_transaction(handle, 1, bh)))
219 goto exit_bh;
220
221 gdb = sb_getblk(sb, block);
222 if (!gdb) {
223 err = -EIO;
224 goto exit_bh;
225 }
226 if ((err = ext4_journal_get_write_access(handle, gdb))) {
227 brelse(gdb);
228 goto exit_bh;
229 }
230 lock_buffer(gdb);
231 memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, gdb->b_size);
232 set_buffer_uptodate(gdb);
233 unlock_buffer(gdb);
234 ext4_handle_dirty_metadata(handle, NULL, gdb);
235 ext4_set_bit(bit, bh->b_data);
236 brelse(gdb);
237 }
238
239
240 for (i = 0, bit = gdblocks + 1, block = start + bit;
241 i < reserved_gdb; i++, block++, bit++) {
242 struct buffer_head *gdb;
243
244 ext4_debug("clear reserved block %#04llx (+%d)\n", block, bit);
245
246 if ((err = extend_or_restart_transaction(handle, 1, bh)))
247 goto exit_bh;
248
249 if (IS_ERR(gdb = bclean(handle, sb, block))) {
250 err = PTR_ERR(bh);
251 goto exit_bh;
252 }
253 ext4_handle_dirty_metadata(handle, NULL, gdb);
254 ext4_set_bit(bit, bh->b_data);
255 brelse(gdb);
256 }
257 ext4_debug("mark block bitmap %#04llx (+%llu)\n", input->block_bitmap,
258 input->block_bitmap - start);
259 ext4_set_bit(input->block_bitmap - start, bh->b_data);
260 ext4_debug("mark inode bitmap %#04llx (+%llu)\n", input->inode_bitmap,
261 input->inode_bitmap - start);
262 ext4_set_bit(input->inode_bitmap - start, bh->b_data);
263
264
265 for (i = 0, block = input->inode_table, bit = block - start;
266 i < sbi->s_itb_per_group; i++, bit++, block++) {
267 struct buffer_head *it;
268
269 ext4_debug("clear inode block %#04llx (+%d)\n", block, bit);
270
271 if ((err = extend_or_restart_transaction(handle, 1, bh)))
272 goto exit_bh;
273
274 if (IS_ERR(it = bclean(handle, sb, block))) {
275 err = PTR_ERR(it);
276 goto exit_bh;
277 }
278 ext4_handle_dirty_metadata(handle, NULL, it);
279 brelse(it);
280 ext4_set_bit(bit, bh->b_data);
281 }
282
283 if ((err = extend_or_restart_transaction(handle, 2, bh)))
284 goto exit_bh;
285
286 mark_bitmap_end(input->blocks_count, sb->s_blocksize * 8, bh->b_data);
287 ext4_handle_dirty_metadata(handle, NULL, bh);
288 brelse(bh);
289
290 ext4_debug("clear inode bitmap %#04llx (+%llu)\n",
291 input->inode_bitmap, input->inode_bitmap - start);
292 if (IS_ERR(bh = bclean(handle, sb, input->inode_bitmap))) {
293 err = PTR_ERR(bh);
294 goto exit_journal;
295 }
296
297 mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8,
298 bh->b_data);
299 ext4_handle_dirty_metadata(handle, NULL, bh);
300exit_bh:
301 brelse(bh);
302
303exit_journal:
304 mutex_unlock(&sbi->s_resize_lock);
305 if ((err2 = ext4_journal_stop(handle)) && !err)
306 err = err2;
307
308 return err;
309}
310
311
312
313
314
315
316
317
318static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
319 unsigned *five, unsigned *seven)
320{
321 unsigned *min = three;
322 int mult = 3;
323 unsigned ret;
324
325 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
326 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
327 ret = *min;
328 *min += 1;
329 return ret;
330 }
331
332 if (*five < *min) {
333 min = five;
334 mult = 5;
335 }
336 if (*seven < *min) {
337 min = seven;
338 mult = 7;
339 }
340
341 ret = *min;
342 *min *= mult;
343
344 return ret;
345}
346
347
348
349
350
351
352static int verify_reserved_gdb(struct super_block *sb,
353 struct buffer_head *primary)
354{
355 const ext4_fsblk_t blk = primary->b_blocknr;
356 const ext4_group_t end = EXT4_SB(sb)->s_groups_count;
357 unsigned three = 1;
358 unsigned five = 5;
359 unsigned seven = 7;
360 unsigned grp;
361 __le32 *p = (__le32 *)primary->b_data;
362 int gdbackups = 0;
363
364 while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
365 if (le32_to_cpu(*p++) !=
366 grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
367 ext4_warning(sb, __func__,
368 "reserved GDT %llu"
369 " missing grp %d (%llu)",
370 blk, grp,
371 grp *
372 (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
373 blk);
374 return -EINVAL;
375 }
376 if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
377 return -EFBIG;
378 }
379
380 return gdbackups;
381}
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396static int add_new_gdb(handle_t *handle, struct inode *inode,
397 struct ext4_new_group_data *input,
398 struct buffer_head **primary)
399{
400 struct super_block *sb = inode->i_sb;
401 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
402 unsigned long gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
403 ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
404 struct buffer_head **o_group_desc, **n_group_desc;
405 struct buffer_head *dind;
406 int gdbackups;
407 struct ext4_iloc iloc;
408 __le32 *data;
409 int err;
410
411 if (test_opt(sb, DEBUG))
412 printk(KERN_DEBUG
413 "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
414 gdb_num);
415
416
417
418
419
420
421 if (EXT4_SB(sb)->s_sbh->b_blocknr !=
422 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
423 ext4_warning(sb, __func__,
424 "won't resize using backup superblock at %llu",
425 (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
426 return -EPERM;
427 }
428
429 *primary = sb_bread(sb, gdblock);
430 if (!*primary)
431 return -EIO;
432
433 if ((gdbackups = verify_reserved_gdb(sb, *primary)) < 0) {
434 err = gdbackups;
435 goto exit_bh;
436 }
437
438 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
439 dind = sb_bread(sb, le32_to_cpu(*data));
440 if (!dind) {
441 err = -EIO;
442 goto exit_bh;
443 }
444
445 data = (__le32 *)dind->b_data;
446 if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
447 ext4_warning(sb, __func__,
448 "new group %u GDT block %llu not reserved",
449 input->group, gdblock);
450 err = -EINVAL;
451 goto exit_dind;
452 }
453
454 if ((err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh)))
455 goto exit_dind;
456
457 if ((err = ext4_journal_get_write_access(handle, *primary)))
458 goto exit_sbh;
459
460 if ((err = ext4_journal_get_write_access(handle, dind)))
461 goto exit_primary;
462
463
464 if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
465 goto exit_dindj;
466
467 n_group_desc = kmalloc((gdb_num + 1) * sizeof(struct buffer_head *),
468 GFP_NOFS);
469 if (!n_group_desc) {
470 err = -ENOMEM;
471 ext4_warning(sb, __func__,
472 "not enough memory for %lu groups", gdb_num + 1);
473 goto exit_inode;
474 }
475
476
477
478
479
480
481
482
483
484
485 data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
486 ext4_handle_dirty_metadata(handle, NULL, dind);
487 brelse(dind);
488 inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
489 ext4_mark_iloc_dirty(handle, inode, &iloc);
490 memset((*primary)->b_data, 0, sb->s_blocksize);
491 ext4_handle_dirty_metadata(handle, NULL, *primary);
492
493 o_group_desc = EXT4_SB(sb)->s_group_desc;
494 memcpy(n_group_desc, o_group_desc,
495 EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
496 n_group_desc[gdb_num] = *primary;
497 EXT4_SB(sb)->s_group_desc = n_group_desc;
498 EXT4_SB(sb)->s_gdb_count++;
499 kfree(o_group_desc);
500
501 le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
502 ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
503
504 return 0;
505
506exit_inode:
507
508 brelse(iloc.bh);
509exit_dindj:
510
511exit_primary:
512
513exit_sbh:
514
515exit_dind:
516 brelse(dind);
517exit_bh:
518 brelse(*primary);
519
520 ext4_debug("leaving with error %d\n", err);
521 return err;
522}
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
538 struct ext4_new_group_data *input)
539{
540 struct super_block *sb = inode->i_sb;
541 int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
542 struct buffer_head **primary;
543 struct buffer_head *dind;
544 struct ext4_iloc iloc;
545 ext4_fsblk_t blk;
546 __le32 *data, *end;
547 int gdbackups = 0;
548 int res, i;
549 int err;
550
551 primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_NOFS);
552 if (!primary)
553 return -ENOMEM;
554
555 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
556 dind = sb_bread(sb, le32_to_cpu(*data));
557 if (!dind) {
558 err = -EIO;
559 goto exit_free;
560 }
561
562 blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count;
563 data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count %
564 EXT4_ADDR_PER_BLOCK(sb));
565 end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb);
566
567
568 for (res = 0; res < reserved_gdb; res++, blk++) {
569 if (le32_to_cpu(*data) != blk) {
570 ext4_warning(sb, __func__,
571 "reserved block %llu"
572 " not at offset %ld",
573 blk,
574 (long)(data - (__le32 *)dind->b_data));
575 err = -EINVAL;
576 goto exit_bh;
577 }
578 primary[res] = sb_bread(sb, blk);
579 if (!primary[res]) {
580 err = -EIO;
581 goto exit_bh;
582 }
583 if ((gdbackups = verify_reserved_gdb(sb, primary[res])) < 0) {
584 brelse(primary[res]);
585 err = gdbackups;
586 goto exit_bh;
587 }
588 if (++data >= end)
589 data = (__le32 *)dind->b_data;
590 }
591
592 for (i = 0; i < reserved_gdb; i++) {
593 if ((err = ext4_journal_get_write_access(handle, primary[i]))) {
594
595
596
597
598
599 goto exit_bh;
600 }
601 }
602
603 if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
604 goto exit_bh;
605
606
607
608
609
610 blk = input->group * EXT4_BLOCKS_PER_GROUP(sb);
611 for (i = 0; i < reserved_gdb; i++) {
612 int err2;
613 data = (__le32 *)primary[i]->b_data;
614
615
616
617 data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
618 err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]);
619 if (!err)
620 err = err2;
621 }
622 inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
623 ext4_mark_iloc_dirty(handle, inode, &iloc);
624
625exit_bh:
626 while (--res >= 0)
627 brelse(primary[res]);
628 brelse(dind);
629
630exit_free:
631 kfree(primary);
632
633 return err;
634}
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652static void update_backups(struct super_block *sb,
653 int blk_off, char *data, int size)
654{
655 struct ext4_sb_info *sbi = EXT4_SB(sb);
656 const ext4_group_t last = sbi->s_groups_count;
657 const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
658 unsigned three = 1;
659 unsigned five = 5;
660 unsigned seven = 7;
661 ext4_group_t group;
662 int rest = sb->s_blocksize - size;
663 handle_t *handle;
664 int err = 0, err2;
665
666 handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
667 if (IS_ERR(handle)) {
668 group = 1;
669 err = PTR_ERR(handle);
670 goto exit_err;
671 }
672
673 while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) {
674 struct buffer_head *bh;
675
676
677 if (ext4_handle_valid(handle) &&
678 handle->h_buffer_credits == 0 &&
679 ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) &&
680 (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
681 break;
682
683 bh = sb_getblk(sb, group * bpg + blk_off);
684 if (!bh) {
685 err = -EIO;
686 break;
687 }
688 ext4_debug("update metadata backup %#04lx\n",
689 (unsigned long)bh->b_blocknr);
690 if ((err = ext4_journal_get_write_access(handle, bh)))
691 break;
692 lock_buffer(bh);
693 memcpy(bh->b_data, data, size);
694 if (rest)
695 memset(bh->b_data + size, 0, rest);
696 set_buffer_uptodate(bh);
697 unlock_buffer(bh);
698 ext4_handle_dirty_metadata(handle, NULL, bh);
699 brelse(bh);
700 }
701 if ((err2 = ext4_journal_stop(handle)) && !err)
702 err = err2;
703
704
705
706
707
708
709
710
711
712
713
714exit_err:
715 if (err) {
716 ext4_warning(sb, __func__,
717 "can't update backup for group %u (err %d), "
718 "forcing fsck on next reboot", group, err);
719 sbi->s_mount_state &= ~EXT4_VALID_FS;
720 sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
721 mark_buffer_dirty(sbi->s_sbh);
722 }
723}
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
739{
740 struct ext4_sb_info *sbi = EXT4_SB(sb);
741 struct ext4_super_block *es = sbi->s_es;
742 int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
743 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
744 struct buffer_head *primary = NULL;
745 struct ext4_group_desc *gdp;
746 struct inode *inode = NULL;
747 handle_t *handle;
748 int gdb_off, gdb_num;
749 int err, err2;
750
751 gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
752 gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
753
754 if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb,
755 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
756 ext4_warning(sb, __func__,
757 "Can't resize non-sparse filesystem further");
758 return -EPERM;
759 }
760
761 if (ext4_blocks_count(es) + input->blocks_count <
762 ext4_blocks_count(es)) {
763 ext4_warning(sb, __func__, "blocks_count overflow");
764 return -EINVAL;
765 }
766
767 if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
768 le32_to_cpu(es->s_inodes_count)) {
769 ext4_warning(sb, __func__, "inodes_count overflow");
770 return -EINVAL;
771 }
772
773 if (reserved_gdb || gdb_off == 0) {
774 if (!EXT4_HAS_COMPAT_FEATURE(sb,
775 EXT4_FEATURE_COMPAT_RESIZE_INODE)
776 || !le16_to_cpu(es->s_reserved_gdt_blocks)) {
777 ext4_warning(sb, __func__,
778 "No reserved GDT blocks, can't resize");
779 return -EPERM;
780 }
781 inode = ext4_iget(sb, EXT4_RESIZE_INO);
782 if (IS_ERR(inode)) {
783 ext4_warning(sb, __func__,
784 "Error opening resize inode");
785 return PTR_ERR(inode);
786 }
787 }
788
789
790 if ((err = verify_group_input(sb, input)))
791 goto exit_put;
792
793 if ((err = setup_new_group_blocks(sb, input)))
794 goto exit_put;
795
796
797
798
799
800
801
802
803 handle = ext4_journal_start_sb(sb,
804 ext4_bg_has_super(sb, input->group) ?
805 3 + reserved_gdb : 4);
806 if (IS_ERR(handle)) {
807 err = PTR_ERR(handle);
808 goto exit_put;
809 }
810
811 mutex_lock(&sbi->s_resize_lock);
812 if (input->group != sbi->s_groups_count) {
813 ext4_warning(sb, __func__,
814 "multiple resizers run on filesystem!");
815 err = -EBUSY;
816 goto exit_journal;
817 }
818
819 if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh)))
820 goto exit_journal;
821
822
823
824
825
826
827
828 if (gdb_off) {
829 primary = sbi->s_group_desc[gdb_num];
830 if ((err = ext4_journal_get_write_access(handle, primary)))
831 goto exit_journal;
832
833 if (reserved_gdb && ext4_bg_num_gdb(sb, input->group) &&
834 (err = reserve_backup_gdb(handle, inode, input)))
835 goto exit_journal;
836 } else if ((err = add_new_gdb(handle, inode, input, &primary)))
837 goto exit_journal;
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859 gdp = (struct ext4_group_desc *)((char *)primary->b_data +
860 gdb_off * EXT4_DESC_SIZE(sb));
861
862 memset(gdp, 0, EXT4_DESC_SIZE(sb));
863 ext4_block_bitmap_set(sb, gdp, input->block_bitmap);
864 ext4_inode_bitmap_set(sb, gdp, input->inode_bitmap);
865 ext4_inode_table_set(sb, gdp, input->inode_table);
866 ext4_free_blks_set(sb, gdp, input->free_blocks_count);
867 ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
868 gdp->bg_flags = cpu_to_le16(EXT4_BG_INODE_ZEROED);
869 gdp->bg_checksum = ext4_group_desc_csum(sbi, input->group, gdp);
870
871
872
873
874
875 err = ext4_mb_add_groupinfo(sb, input->group, gdp);
876 if (err)
877 goto exit_journal;
878
879
880
881
882
883
884
885
886
887
888
889 ext4_blocks_count_set(es, ext4_blocks_count(es) +
890 input->blocks_count);
891 le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb));
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915 smp_wmb();
916
917
918 sbi->s_groups_count++;
919
920 ext4_handle_dirty_metadata(handle, NULL, primary);
921
922
923
924 ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
925 input->reserved_blocks);
926
927
928 percpu_counter_add(&sbi->s_freeblocks_counter,
929 input->free_blocks_count);
930 percpu_counter_add(&sbi->s_freeinodes_counter,
931 EXT4_INODES_PER_GROUP(sb));
932
933 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
934 ext4_group_t flex_group;
935 flex_group = ext4_flex_group(sbi, input->group);
936 atomic_add(input->free_blocks_count,
937 &sbi->s_flex_groups[flex_group].free_blocks);
938 atomic_add(EXT4_INODES_PER_GROUP(sb),
939 &sbi->s_flex_groups[flex_group].free_inodes);
940 }
941
942 ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
943 sb->s_dirt = 1;
944
945exit_journal:
946 mutex_unlock(&sbi->s_resize_lock);
947 if ((err2 = ext4_journal_stop(handle)) && !err)
948 err = err2;
949 if (!err) {
950 update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
951 sizeof(struct ext4_super_block));
952 update_backups(sb, primary->b_blocknr, primary->b_data,
953 primary->b_size);
954 }
955exit_put:
956 iput(inode);
957 return err;
958}
959
960
961
962
963
964
965
966
967
968
969
970int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
971 ext4_fsblk_t n_blocks_count)
972{
973 ext4_fsblk_t o_blocks_count;
974 ext4_group_t o_groups_count;
975 ext4_grpblk_t last;
976 ext4_grpblk_t add;
977 struct buffer_head *bh;
978 handle_t *handle;
979 int err;
980 ext4_group_t group;
981
982
983
984
985 o_blocks_count = ext4_blocks_count(es);
986 o_groups_count = EXT4_SB(sb)->s_groups_count;
987
988 if (test_opt(sb, DEBUG))
989 printk(KERN_DEBUG "EXT4-fs: extending last group from %llu uto %llu blocks\n",
990 o_blocks_count, n_blocks_count);
991
992 if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
993 return 0;
994
995 if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
996 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
997 " too large to resize to %llu blocks safely\n",
998 sb->s_id, n_blocks_count);
999 if (sizeof(sector_t) < 8)
1000 ext4_warning(sb, __func__, "CONFIG_LBDAF not enabled");
1001 return -EINVAL;
1002 }
1003
1004 if (n_blocks_count < o_blocks_count) {
1005 ext4_warning(sb, __func__,
1006 "can't shrink FS - resize aborted");
1007 return -EBUSY;
1008 }
1009
1010
1011 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1012
1013 if (last == 0) {
1014 ext4_warning(sb, __func__,
1015 "need to use ext2online to resize further");
1016 return -EPERM;
1017 }
1018
1019 add = EXT4_BLOCKS_PER_GROUP(sb) - last;
1020
1021 if (o_blocks_count + add < o_blocks_count) {
1022 ext4_warning(sb, __func__, "blocks_count overflow");
1023 return -EINVAL;
1024 }
1025
1026 if (o_blocks_count + add > n_blocks_count)
1027 add = n_blocks_count - o_blocks_count;
1028
1029 if (o_blocks_count + add < n_blocks_count)
1030 ext4_warning(sb, __func__,
1031 "will only finish group (%llu"
1032 " blocks, %u new)",
1033 o_blocks_count + add, add);
1034
1035
1036 bh = sb_bread(sb, o_blocks_count + add - 1);
1037 if (!bh) {
1038 ext4_warning(sb, __func__,
1039 "can't read last block, resize aborted");
1040 return -ENOSPC;
1041 }
1042 brelse(bh);
1043
1044
1045
1046
1047 handle = ext4_journal_start_sb(sb, 3);
1048 if (IS_ERR(handle)) {
1049 err = PTR_ERR(handle);
1050 ext4_warning(sb, __func__, "error %d on journal start", err);
1051 goto exit_put;
1052 }
1053
1054 mutex_lock(&EXT4_SB(sb)->s_resize_lock);
1055 if (o_blocks_count != ext4_blocks_count(es)) {
1056 ext4_warning(sb, __func__,
1057 "multiple resizers run on filesystem!");
1058 mutex_unlock(&EXT4_SB(sb)->s_resize_lock);
1059 ext4_journal_stop(handle);
1060 err = -EBUSY;
1061 goto exit_put;
1062 }
1063
1064 if ((err = ext4_journal_get_write_access(handle,
1065 EXT4_SB(sb)->s_sbh))) {
1066 ext4_warning(sb, __func__,
1067 "error %d on journal write access", err);
1068 mutex_unlock(&EXT4_SB(sb)->s_resize_lock);
1069 ext4_journal_stop(handle);
1070 goto exit_put;
1071 }
1072 ext4_blocks_count_set(es, o_blocks_count + add);
1073 ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
1074 sb->s_dirt = 1;
1075 mutex_unlock(&EXT4_SB(sb)->s_resize_lock);
1076 ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
1077 o_blocks_count + add);
1078
1079 ext4_add_groupblocks(handle, sb, o_blocks_count, add);
1080 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
1081 o_blocks_count + add);
1082 if ((err = ext4_journal_stop(handle)))
1083 goto exit_put;
1084
1085 if (test_opt(sb, DEBUG))
1086 printk(KERN_DEBUG "EXT4-fs: extended group to %llu blocks\n",
1087 ext4_blocks_count(es));
1088 update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es,
1089 sizeof(struct ext4_super_block));
1090exit_put:
1091 return err;
1092}
1093