2 * linux/fs/ext3/resize.c
4 * Support for resizing an ext3 filesystem while it is mounted.
6 * Copyright (C) 2001, 2002 Andreas Dilger <adilger@clusterfs.com>
8 * This could probably be made into a module, because it is not often in use.
14 #include <linux/ext3_jbd.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
20 #define outside(b, first, last) ((b) < (first) || (b) >= (last))
21 #define inside(b, first, last) ((b) >= (first) && (b) < (last))
23 static int verify_group_input(struct super_block
*sb
,
24 struct ext3_new_group_data
*input
)
26 struct ext3_sb_info
*sbi
= EXT3_SB(sb
);
27 struct ext3_super_block
*es
= sbi
->s_es
;
28 ext3_fsblk_t start
= le32_to_cpu(es
->s_blocks_count
);
29 ext3_fsblk_t end
= start
+ input
->blocks_count
;
30 unsigned group
= input
->group
;
31 ext3_fsblk_t itend
= input
->inode_table
+ sbi
->s_itb_per_group
;
32 unsigned overhead
= ext3_bg_has_super(sb
, group
) ?
33 (1 + ext3_bg_num_gdb(sb
, group
) +
34 le16_to_cpu(es
->s_reserved_gdt_blocks
)) : 0;
35 ext3_fsblk_t metaend
= start
+ overhead
;
36 struct buffer_head
*bh
= NULL
;
37 ext3_grpblk_t free_blocks_count
;
40 input
->free_blocks_count
= free_blocks_count
=
41 input
->blocks_count
- 2 - overhead
- sbi
->s_itb_per_group
;
43 if (test_opt(sb
, DEBUG
))
44 printk(KERN_DEBUG
"EXT3-fs: adding %s group %u: %u blocks "
45 "(%d free, %u reserved)\n",
46 ext3_bg_has_super(sb
, input
->group
) ? "normal" :
47 "no-super", input
->group
, input
->blocks_count
,
48 free_blocks_count
, input
->reserved_blocks
);
50 if (group
!= sbi
->s_groups_count
)
51 ext3_warning(sb
, __FUNCTION__
,
52 "Cannot add at group %u (only %lu groups)",
53 input
->group
, sbi
->s_groups_count
);
54 else if ((start
- le32_to_cpu(es
->s_first_data_block
)) %
55 EXT3_BLOCKS_PER_GROUP(sb
))
56 ext3_warning(sb
, __FUNCTION__
, "Last group not full");
57 else if (input
->reserved_blocks
> input
->blocks_count
/ 5)
58 ext3_warning(sb
, __FUNCTION__
, "Reserved blocks too high (%u)",
59 input
->reserved_blocks
);
60 else if (free_blocks_count
< 0)
61 ext3_warning(sb
, __FUNCTION__
, "Bad blocks count %u",
63 else if (!(bh
= sb_bread(sb
, end
- 1)))
64 ext3_warning(sb
, __FUNCTION__
,
65 "Cannot read last block ("E3FSBLK
")",
67 else if (outside(input
->block_bitmap
, start
, end
))
68 ext3_warning(sb
, __FUNCTION__
,
69 "Block bitmap not in group (block %u)",
71 else if (outside(input
->inode_bitmap
, start
, end
))
72 ext3_warning(sb
, __FUNCTION__
,
73 "Inode bitmap not in group (block %u)",
75 else if (outside(input
->inode_table
, start
, end
) ||
76 outside(itend
- 1, start
, end
))
77 ext3_warning(sb
, __FUNCTION__
,
78 "Inode table not in group (blocks %u-"E3FSBLK
")",
79 input
->inode_table
, itend
- 1);
80 else if (input
->inode_bitmap
== input
->block_bitmap
)
81 ext3_warning(sb
, __FUNCTION__
,
82 "Block bitmap same as inode bitmap (%u)",
84 else if (inside(input
->block_bitmap
, input
->inode_table
, itend
))
85 ext3_warning(sb
, __FUNCTION__
,
86 "Block bitmap (%u) in inode table (%u-"E3FSBLK
")",
87 input
->block_bitmap
, input
->inode_table
, itend
-1);
88 else if (inside(input
->inode_bitmap
, input
->inode_table
, itend
))
89 ext3_warning(sb
, __FUNCTION__
,
90 "Inode bitmap (%u) in inode table (%u-"E3FSBLK
")",
91 input
->inode_bitmap
, input
->inode_table
, itend
-1);
92 else if (inside(input
->block_bitmap
, start
, metaend
))
93 ext3_warning(sb
, __FUNCTION__
,
94 "Block bitmap (%u) in GDT table"
95 " ("E3FSBLK
"-"E3FSBLK
")",
96 input
->block_bitmap
, start
, metaend
- 1);
97 else if (inside(input
->inode_bitmap
, start
, metaend
))
98 ext3_warning(sb
, __FUNCTION__
,
99 "Inode bitmap (%u) in GDT table"
100 " ("E3FSBLK
"-"E3FSBLK
")",
101 input
->inode_bitmap
, start
, metaend
- 1);
102 else if (inside(input
->inode_table
, start
, metaend
) ||
103 inside(itend
- 1, start
, metaend
))
104 ext3_warning(sb
, __FUNCTION__
,
105 "Inode table (%u-"E3FSBLK
") overlaps"
106 "GDT table ("E3FSBLK
"-"E3FSBLK
")",
107 input
->inode_table
, itend
- 1, start
, metaend
- 1);
115 static struct buffer_head
*bclean(handle_t
*handle
, struct super_block
*sb
,
118 struct buffer_head
*bh
;
121 bh
= sb_getblk(sb
, blk
);
123 return ERR_PTR(-EIO
);
124 if ((err
= ext3_journal_get_write_access(handle
, bh
))) {
129 memset(bh
->b_data
, 0, sb
->s_blocksize
);
130 set_buffer_uptodate(bh
);
138 * To avoid calling the atomic setbit hundreds or thousands of times, we only
139 * need to use it within a single byte (to ensure we get endianness right).
140 * We can use memset for the rest of the bitmap as there are no other users.
142 static void mark_bitmap_end(int start_bit
, int end_bit
, char *bitmap
)
146 if (start_bit
>= end_bit
)
149 ext3_debug("mark end bits +%d through +%d used\n", start_bit
, end_bit
);
150 for (i
= start_bit
; i
< ((start_bit
+ 7) & ~7UL); i
++)
151 ext3_set_bit(i
, bitmap
);
153 memset(bitmap
+ (i
>> 3), 0xff, (end_bit
- i
) >> 3);
157 * Set up the block and inode bitmaps, and the inode table for the new group.
158 * This doesn't need to be part of the main transaction, since we are only
159 * changing blocks outside the actual filesystem. We still do journaling to
160 * ensure the recovery is correct in case of a failure just after resize.
161 * If any part of this fails, we simply abort the resize.
163 static int setup_new_group_blocks(struct super_block
*sb
,
164 struct ext3_new_group_data
*input
)
166 struct ext3_sb_info
*sbi
= EXT3_SB(sb
);
167 ext3_fsblk_t start
= ext3_group_first_block_no(sb
, input
->group
);
168 int reserved_gdb
= ext3_bg_has_super(sb
, input
->group
) ?
169 le16_to_cpu(sbi
->s_es
->s_reserved_gdt_blocks
) : 0;
170 unsigned long gdblocks
= ext3_bg_num_gdb(sb
, input
->group
);
171 struct buffer_head
*bh
;
178 handle
= ext3_journal_start_sb(sb
, reserved_gdb
+ gdblocks
+
179 2 + sbi
->s_itb_per_group
);
181 return PTR_ERR(handle
);
184 if (input
->group
!= sbi
->s_groups_count
) {
189 if (IS_ERR(bh
= bclean(handle
, sb
, input
->block_bitmap
))) {
194 if (ext3_bg_has_super(sb
, input
->group
)) {
195 ext3_debug("mark backup superblock %#04lx (+0)\n", start
);
196 ext3_set_bit(0, bh
->b_data
);
199 /* Copy all of the GDT blocks into the backup in this group */
200 for (i
= 0, bit
= 1, block
= start
+ 1;
201 i
< gdblocks
; i
++, block
++, bit
++) {
202 struct buffer_head
*gdb
;
204 ext3_debug("update backup group %#04lx (+%d)\n", block
, bit
);
206 gdb
= sb_getblk(sb
, block
);
211 if ((err
= ext3_journal_get_write_access(handle
, gdb
))) {
216 memcpy(gdb
->b_data
, sbi
->s_group_desc
[i
]->b_data
, bh
->b_size
);
217 set_buffer_uptodate(gdb
);
219 ext3_journal_dirty_metadata(handle
, gdb
);
220 ext3_set_bit(bit
, bh
->b_data
);
224 /* Zero out all of the reserved backup group descriptor table blocks */
225 for (i
= 0, bit
= gdblocks
+ 1, block
= start
+ bit
;
226 i
< reserved_gdb
; i
++, block
++, bit
++) {
227 struct buffer_head
*gdb
;
229 ext3_debug("clear reserved block %#04lx (+%d)\n", block
, bit
);
231 if (IS_ERR(gdb
= bclean(handle
, sb
, block
))) {
235 ext3_journal_dirty_metadata(handle
, gdb
);
236 ext3_set_bit(bit
, bh
->b_data
);
239 ext3_debug("mark block bitmap %#04x (+%ld)\n", input
->block_bitmap
,
240 input
->block_bitmap
- start
);
241 ext3_set_bit(input
->block_bitmap
- start
, bh
->b_data
);
242 ext3_debug("mark inode bitmap %#04x (+%ld)\n", input
->inode_bitmap
,
243 input
->inode_bitmap
- start
);
244 ext3_set_bit(input
->inode_bitmap
- start
, bh
->b_data
);
246 /* Zero out all of the inode table blocks */
247 for (i
= 0, block
= input
->inode_table
, bit
= block
- start
;
248 i
< sbi
->s_itb_per_group
; i
++, bit
++, block
++) {
249 struct buffer_head
*it
;
251 ext3_debug("clear inode block %#04lx (+%d)\n", block
, bit
);
252 if (IS_ERR(it
= bclean(handle
, sb
, block
))) {
256 ext3_journal_dirty_metadata(handle
, it
);
258 ext3_set_bit(bit
, bh
->b_data
);
260 mark_bitmap_end(input
->blocks_count
, EXT3_BLOCKS_PER_GROUP(sb
),
262 ext3_journal_dirty_metadata(handle
, bh
);
265 /* Mark unused entries in inode bitmap used */
266 ext3_debug("clear inode bitmap %#04x (+%ld)\n",
267 input
->inode_bitmap
, input
->inode_bitmap
- start
);
268 if (IS_ERR(bh
= bclean(handle
, sb
, input
->inode_bitmap
))) {
273 mark_bitmap_end(EXT3_INODES_PER_GROUP(sb
), EXT3_BLOCKS_PER_GROUP(sb
),
275 ext3_journal_dirty_metadata(handle
, bh
);
281 if ((err2
= ext3_journal_stop(handle
)) && !err
)
288 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
289 * ext3 filesystem. The counters should be initialized to 1, 5, and 7 before
290 * calling this for the first time. In a sparse filesystem it will be the
291 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
292 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
294 static unsigned ext3_list_backups(struct super_block
*sb
, unsigned *three
,
295 unsigned *five
, unsigned *seven
)
297 unsigned *min
= three
;
301 if (!EXT3_HAS_RO_COMPAT_FEATURE(sb
,
302 EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER
)) {
324 * Check that all of the backup GDT blocks are held in the primary GDT block.
325 * It is assumed that they are stored in group order. Returns the number of
326 * groups in current filesystem that have BACKUPS, or -ve error code.
328 static int verify_reserved_gdb(struct super_block
*sb
,
329 struct buffer_head
*primary
)
331 const ext3_fsblk_t blk
= primary
->b_blocknr
;
332 const unsigned long end
= EXT3_SB(sb
)->s_groups_count
;
337 __le32
*p
= (__le32
*)primary
->b_data
;
340 while ((grp
= ext3_list_backups(sb
, &three
, &five
, &seven
)) < end
) {
341 if (le32_to_cpu(*p
++) != grp
* EXT3_BLOCKS_PER_GROUP(sb
) + blk
){
342 ext3_warning(sb
, __FUNCTION__
,
343 "reserved GDT "E3FSBLK
344 " missing grp %d ("E3FSBLK
")",
346 grp
* EXT3_BLOCKS_PER_GROUP(sb
) + blk
);
349 if (++gdbackups
> EXT3_ADDR_PER_BLOCK(sb
))
357 * Called when we need to bring a reserved group descriptor table block into
358 * use from the resize inode. The primary copy of the new GDT block currently
359 * is an indirect block (under the double indirect block in the resize inode).
360 * The new backup GDT blocks will be stored as leaf blocks in this indirect
361 * block, in group order. Even though we know all the block numbers we need,
362 * we check to ensure that the resize inode has actually reserved these blocks.
364 * Don't need to update the block bitmaps because the blocks are still in use.
366 * We get all of the error cases out of the way, so that we are sure to not
367 * fail once we start modifying the data on disk, because JBD has no rollback.
369 static int add_new_gdb(handle_t
*handle
, struct inode
*inode
,
370 struct ext3_new_group_data
*input
,
371 struct buffer_head
**primary
)
373 struct super_block
*sb
= inode
->i_sb
;
374 struct ext3_super_block
*es
= EXT3_SB(sb
)->s_es
;
375 unsigned long gdb_num
= input
->group
/ EXT3_DESC_PER_BLOCK(sb
);
376 ext3_fsblk_t gdblock
= EXT3_SB(sb
)->s_sbh
->b_blocknr
+ 1 + gdb_num
;
377 struct buffer_head
**o_group_desc
, **n_group_desc
;
378 struct buffer_head
*dind
;
380 struct ext3_iloc iloc
;
384 if (test_opt(sb
, DEBUG
))
386 "EXT3-fs: ext3_add_new_gdb: adding group block %lu\n",
390 * If we are not using the primary superblock/GDT copy don't resize,
391 * because the user tools have no way of handling this. Probably a
392 * bad time to do it anyways.
394 if (EXT3_SB(sb
)->s_sbh
->b_blocknr
!=
395 le32_to_cpu(EXT3_SB(sb
)->s_es
->s_first_data_block
)) {
396 ext3_warning(sb
, __FUNCTION__
,
397 "won't resize using backup superblock at %llu",
398 (unsigned long long)EXT3_SB(sb
)->s_sbh
->b_blocknr
);
402 *primary
= sb_bread(sb
, gdblock
);
406 if ((gdbackups
= verify_reserved_gdb(sb
, *primary
)) < 0) {
411 data
= EXT3_I(inode
)->i_data
+ EXT3_DIND_BLOCK
;
412 dind
= sb_bread(sb
, le32_to_cpu(*data
));
418 data
= (__le32
*)dind
->b_data
;
419 if (le32_to_cpu(data
[gdb_num
% EXT3_ADDR_PER_BLOCK(sb
)]) != gdblock
) {
420 ext3_warning(sb
, __FUNCTION__
,
421 "new group %u GDT block "E3FSBLK
" not reserved",
422 input
->group
, gdblock
);
427 if ((err
= ext3_journal_get_write_access(handle
, EXT3_SB(sb
)->s_sbh
)))
430 if ((err
= ext3_journal_get_write_access(handle
, *primary
)))
433 if ((err
= ext3_journal_get_write_access(handle
, dind
)))
436 /* ext3_reserve_inode_write() gets a reference on the iloc */
437 if ((err
= ext3_reserve_inode_write(handle
, inode
, &iloc
)))
440 n_group_desc
= kmalloc((gdb_num
+ 1) * sizeof(struct buffer_head
*),
444 ext3_warning (sb
, __FUNCTION__
,
445 "not enough memory for %lu groups", gdb_num
+ 1);
450 * Finally, we have all of the possible failures behind us...
452 * Remove new GDT block from inode double-indirect block and clear out
453 * the new GDT block for use (which also "frees" the backup GDT blocks
454 * from the reserved inode). We don't need to change the bitmaps for
455 * these blocks, because they are marked as in-use from being in the
456 * reserved inode, and will become GDT blocks (primary and backup).
458 data
[gdb_num
% EXT3_ADDR_PER_BLOCK(sb
)] = 0;
459 ext3_journal_dirty_metadata(handle
, dind
);
461 inode
->i_blocks
-= (gdbackups
+ 1) * sb
->s_blocksize
>> 9;
462 ext3_mark_iloc_dirty(handle
, inode
, &iloc
);
463 memset((*primary
)->b_data
, 0, sb
->s_blocksize
);
464 ext3_journal_dirty_metadata(handle
, *primary
);
466 o_group_desc
= EXT3_SB(sb
)->s_group_desc
;
467 memcpy(n_group_desc
, o_group_desc
,
468 EXT3_SB(sb
)->s_gdb_count
* sizeof(struct buffer_head
*));
469 n_group_desc
[gdb_num
] = *primary
;
470 EXT3_SB(sb
)->s_group_desc
= n_group_desc
;
471 EXT3_SB(sb
)->s_gdb_count
++;
474 es
->s_reserved_gdt_blocks
=
475 cpu_to_le16(le16_to_cpu(es
->s_reserved_gdt_blocks
) - 1);
476 ext3_journal_dirty_metadata(handle
, EXT3_SB(sb
)->s_sbh
);
481 //ext3_journal_release_buffer(handle, iloc.bh);
484 //ext3_journal_release_buffer(handle, dind);
486 //ext3_journal_release_buffer(handle, *primary);
488 //ext3_journal_release_buffer(handle, *primary);
494 ext3_debug("leaving with error %d\n", err
);
499 * Called when we are adding a new group which has a backup copy of each of
500 * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
501 * We need to add these reserved backup GDT blocks to the resize inode, so
502 * that they are kept for future resizing and not allocated to files.
504 * Each reserved backup GDT block will go into a different indirect block.
505 * The indirect blocks are actually the primary reserved GDT blocks,
506 * so we know in advance what their block numbers are. We only get the
507 * double-indirect block to verify it is pointing to the primary reserved
508 * GDT blocks so we don't overwrite a data block by accident. The reserved
509 * backup GDT blocks are stored in their reserved primary GDT block.
511 static int reserve_backup_gdb(handle_t
*handle
, struct inode
*inode
,
512 struct ext3_new_group_data
*input
)
514 struct super_block
*sb
= inode
->i_sb
;
515 int reserved_gdb
=le16_to_cpu(EXT3_SB(sb
)->s_es
->s_reserved_gdt_blocks
);
516 struct buffer_head
**primary
;
517 struct buffer_head
*dind
;
518 struct ext3_iloc iloc
;
525 primary
= kmalloc(reserved_gdb
* sizeof(*primary
), GFP_KERNEL
);
529 data
= EXT3_I(inode
)->i_data
+ EXT3_DIND_BLOCK
;
530 dind
= sb_bread(sb
, le32_to_cpu(*data
));
536 blk
= EXT3_SB(sb
)->s_sbh
->b_blocknr
+ 1 + EXT3_SB(sb
)->s_gdb_count
;
537 data
= (__le32
*)dind
->b_data
+ EXT3_SB(sb
)->s_gdb_count
;
538 end
= (__le32
*)dind
->b_data
+ EXT3_ADDR_PER_BLOCK(sb
);
540 /* Get each reserved primary GDT block and verify it holds backups */
541 for (res
= 0; res
< reserved_gdb
; res
++, blk
++) {
542 if (le32_to_cpu(*data
) != blk
) {
543 ext3_warning(sb
, __FUNCTION__
,
544 "reserved block "E3FSBLK
545 " not at offset %ld",
547 (long)(data
- (__le32
*)dind
->b_data
));
551 primary
[res
] = sb_bread(sb
, blk
);
556 if ((gdbackups
= verify_reserved_gdb(sb
, primary
[res
])) < 0) {
557 brelse(primary
[res
]);
562 data
= (__le32
*)dind
->b_data
;
565 for (i
= 0; i
< reserved_gdb
; i
++) {
566 if ((err
= ext3_journal_get_write_access(handle
, primary
[i
]))) {
569 for (j = 0; j < i; j++)
570 ext3_journal_release_buffer(handle, primary[j]);
576 if ((err
= ext3_reserve_inode_write(handle
, inode
, &iloc
)))
580 * Finally we can add each of the reserved backup GDT blocks from
581 * the new group to its reserved primary GDT block.
583 blk
= input
->group
* EXT3_BLOCKS_PER_GROUP(sb
);
584 for (i
= 0; i
< reserved_gdb
; i
++) {
586 data
= (__le32
*)primary
[i
]->b_data
;
587 /* printk("reserving backup %lu[%u] = %lu\n",
588 primary[i]->b_blocknr, gdbackups,
589 blk + primary[i]->b_blocknr); */
590 data
[gdbackups
] = cpu_to_le32(blk
+ primary
[i
]->b_blocknr
);
591 err2
= ext3_journal_dirty_metadata(handle
, primary
[i
]);
595 inode
->i_blocks
+= reserved_gdb
* sb
->s_blocksize
>> 9;
596 ext3_mark_iloc_dirty(handle
, inode
, &iloc
);
600 brelse(primary
[res
]);
610 * Update the backup copies of the ext3 metadata. These don't need to be part
611 * of the main resize transaction, because e2fsck will re-write them if there
612 * is a problem (basically only OOM will cause a problem). However, we
613 * _should_ update the backups if possible, in case the primary gets trashed
614 * for some reason and we need to run e2fsck from a backup superblock. The
615 * important part is that the new block and inode counts are in the backup
616 * superblocks, and the location of the new group metadata in the GDT backups.
618 * We do not need lock_super() for this, because these blocks are not
619 * otherwise touched by the filesystem code when it is mounted. We don't
620 * need to worry about last changing from sbi->s_groups_count, because the
621 * worst that can happen is that we do not copy the full number of backups
622 * at this time. The resize which changed s_groups_count will backup again.
624 static void update_backups(struct super_block
*sb
,
625 int blk_off
, char *data
, int size
)
627 struct ext3_sb_info
*sbi
= EXT3_SB(sb
);
628 const unsigned long last
= sbi
->s_groups_count
;
629 const int bpg
= EXT3_BLOCKS_PER_GROUP(sb
);
634 int rest
= sb
->s_blocksize
- size
;
638 handle
= ext3_journal_start_sb(sb
, EXT3_MAX_TRANS_DATA
);
639 if (IS_ERR(handle
)) {
641 err
= PTR_ERR(handle
);
645 while ((group
= ext3_list_backups(sb
, &three
, &five
, &seven
)) < last
) {
646 struct buffer_head
*bh
;
648 /* Out of journal space, and can't get more - abort - so sad */
649 if (handle
->h_buffer_credits
== 0 &&
650 ext3_journal_extend(handle
, EXT3_MAX_TRANS_DATA
) &&
651 (err
= ext3_journal_restart(handle
, EXT3_MAX_TRANS_DATA
)))
654 bh
= sb_getblk(sb
, group
* bpg
+ blk_off
);
659 ext3_debug("update metadata backup %#04lx\n",
660 (unsigned long)bh
->b_blocknr
);
661 if ((err
= ext3_journal_get_write_access(handle
, bh
)))
664 memcpy(bh
->b_data
, data
, size
);
666 memset(bh
->b_data
+ size
, 0, rest
);
667 set_buffer_uptodate(bh
);
669 ext3_journal_dirty_metadata(handle
, bh
);
672 if ((err2
= ext3_journal_stop(handle
)) && !err
)
676 * Ugh! Need to have e2fsck write the backup copies. It is too
677 * late to revert the resize, we shouldn't fail just because of
678 * the backup copies (they are only needed in case of corruption).
680 * However, if we got here we have a journal problem too, so we
681 * can't really start a transaction to mark the superblock.
682 * Chicken out and just set the flag on the hope it will be written
683 * to disk, and if not - we will simply wait until next fsck.
687 ext3_warning(sb
, __FUNCTION__
,
688 "can't update backup for group %d (err %d), "
689 "forcing fsck on next reboot", group
, err
);
690 sbi
->s_mount_state
&= ~EXT3_VALID_FS
;
691 sbi
->s_es
->s_state
&= cpu_to_le16(~EXT3_VALID_FS
);
692 mark_buffer_dirty(sbi
->s_sbh
);
696 /* Add group descriptor data to an existing or new group descriptor block.
697 * Ensure we handle all possible error conditions _before_ we start modifying
698 * the filesystem, because we cannot abort the transaction and not have it
699 * write the data to disk.
701 * If we are on a GDT block boundary, we need to get the reserved GDT block.
702 * Otherwise, we may need to add backup GDT blocks for a sparse group.
704 * We only need to hold the superblock lock while we are actually adding
705 * in the new group's counts to the superblock. Prior to that we have
706 * not really "added" the group at all. We re-check that we are still
707 * adding in the last group in case things have changed since verifying.
709 int ext3_group_add(struct super_block
*sb
, struct ext3_new_group_data
*input
)
711 struct ext3_sb_info
*sbi
= EXT3_SB(sb
);
712 struct ext3_super_block
*es
= sbi
->s_es
;
713 int reserved_gdb
= ext3_bg_has_super(sb
, input
->group
) ?
714 le16_to_cpu(es
->s_reserved_gdt_blocks
) : 0;
715 struct buffer_head
*primary
= NULL
;
716 struct ext3_group_desc
*gdp
;
717 struct inode
*inode
= NULL
;
719 int gdb_off
, gdb_num
;
722 gdb_num
= input
->group
/ EXT3_DESC_PER_BLOCK(sb
);
723 gdb_off
= input
->group
% EXT3_DESC_PER_BLOCK(sb
);
725 if (gdb_off
== 0 && !EXT3_HAS_RO_COMPAT_FEATURE(sb
,
726 EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER
)) {
727 ext3_warning(sb
, __FUNCTION__
,
728 "Can't resize non-sparse filesystem further");
732 if (le32_to_cpu(es
->s_blocks_count
) + input
->blocks_count
<
733 le32_to_cpu(es
->s_blocks_count
)) {
734 ext3_warning(sb
, __FUNCTION__
, "blocks_count overflow\n");
738 if (le32_to_cpu(es
->s_inodes_count
) + EXT3_INODES_PER_GROUP(sb
) <
739 le32_to_cpu(es
->s_inodes_count
)) {
740 ext3_warning(sb
, __FUNCTION__
, "inodes_count overflow\n");
744 if (reserved_gdb
|| gdb_off
== 0) {
745 if (!EXT3_HAS_COMPAT_FEATURE(sb
,
746 EXT3_FEATURE_COMPAT_RESIZE_INODE
)){
747 ext3_warning(sb
, __FUNCTION__
,
748 "No reserved GDT blocks, can't resize");
751 inode
= iget(sb
, EXT3_RESIZE_INO
);
752 if (!inode
|| is_bad_inode(inode
)) {
753 ext3_warning(sb
, __FUNCTION__
,
754 "Error opening resize inode");
760 if ((err
= verify_group_input(sb
, input
)))
763 if ((err
= setup_new_group_blocks(sb
, input
)))
767 * We will always be modifying at least the superblock and a GDT
768 * block. If we are adding a group past the last current GDT block,
769 * we will also modify the inode and the dindirect block. If we
770 * are adding a group with superblock/GDT backups we will also
771 * modify each of the reserved GDT dindirect blocks.
773 handle
= ext3_journal_start_sb(sb
,
774 ext3_bg_has_super(sb
, input
->group
) ?
775 3 + reserved_gdb
: 4);
776 if (IS_ERR(handle
)) {
777 err
= PTR_ERR(handle
);
782 if (input
->group
!= sbi
->s_groups_count
) {
783 ext3_warning(sb
, __FUNCTION__
,
784 "multiple resizers run on filesystem!");
789 if ((err
= ext3_journal_get_write_access(handle
, sbi
->s_sbh
)))
793 * We will only either add reserved group blocks to a backup group
794 * or remove reserved blocks for the first group in a new group block.
795 * Doing both would be mean more complex code, and sane people don't
796 * use non-sparse filesystems anymore. This is already checked above.
799 primary
= sbi
->s_group_desc
[gdb_num
];
800 if ((err
= ext3_journal_get_write_access(handle
, primary
)))
803 if (reserved_gdb
&& ext3_bg_num_gdb(sb
, input
->group
) &&
804 (err
= reserve_backup_gdb(handle
, inode
, input
)))
806 } else if ((err
= add_new_gdb(handle
, inode
, input
, &primary
)))
810 * OK, now we've set up the new group. Time to make it active.
812 * Current kernels don't lock all allocations via lock_super(),
813 * so we have to be safe wrt. concurrent accesses the group
814 * data. So we need to be careful to set all of the relevant
815 * group descriptor data etc. *before* we enable the group.
817 * The key field here is sbi->s_groups_count: as long as
818 * that retains its old value, nobody is going to access the new
821 * So first we update all the descriptor metadata for the new
822 * group; then we update the total disk blocks count; then we
823 * update the groups count to enable the group; then finally we
824 * update the free space counts so that the system can start
825 * using the new disk blocks.
828 /* Update group descriptor block for new group */
829 gdp
= (struct ext3_group_desc
*)primary
->b_data
+ gdb_off
;
831 gdp
->bg_block_bitmap
= cpu_to_le32(input
->block_bitmap
);
832 gdp
->bg_inode_bitmap
= cpu_to_le32(input
->inode_bitmap
);
833 gdp
->bg_inode_table
= cpu_to_le32(input
->inode_table
);
834 gdp
->bg_free_blocks_count
= cpu_to_le16(input
->free_blocks_count
);
835 gdp
->bg_free_inodes_count
= cpu_to_le16(EXT3_INODES_PER_GROUP(sb
));
838 * Make the new blocks and inodes valid next. We do this before
839 * increasing the group count so that once the group is enabled,
840 * all of its blocks and inodes are already valid.
842 * We always allocate group-by-group, then block-by-block or
843 * inode-by-inode within a group, so enabling these
844 * blocks/inodes before the group is live won't actually let us
845 * allocate the new space yet.
847 es
->s_blocks_count
= cpu_to_le32(le32_to_cpu(es
->s_blocks_count
) +
848 input
->blocks_count
);
849 es
->s_inodes_count
= cpu_to_le32(le32_to_cpu(es
->s_inodes_count
) +
850 EXT3_INODES_PER_GROUP(sb
));
853 * We need to protect s_groups_count against other CPUs seeing
854 * inconsistent state in the superblock.
856 * The precise rules we use are:
858 * * Writers of s_groups_count *must* hold lock_super
860 * * Writers must perform a smp_wmb() after updating all dependent
861 * data and before modifying the groups count
863 * * Readers must hold lock_super() over the access
865 * * Readers must perform an smp_rmb() after reading the groups count
866 * and before reading any dependent data.
868 * NB. These rules can be relaxed when checking the group count
869 * while freeing data, as we can only allocate from a block
870 * group after serialising against the group count, and we can
871 * only then free after serialising in turn against that
876 /* Update the global fs size fields */
877 sbi
->s_groups_count
++;
879 ext3_journal_dirty_metadata(handle
, primary
);
881 /* Update the reserved block counts only once the new group is
883 es
->s_r_blocks_count
= cpu_to_le32(le32_to_cpu(es
->s_r_blocks_count
) +
884 input
->reserved_blocks
);
886 /* Update the free space counts */
887 percpu_counter_mod(&sbi
->s_freeblocks_counter
,
888 input
->free_blocks_count
);
889 percpu_counter_mod(&sbi
->s_freeinodes_counter
,
890 EXT3_INODES_PER_GROUP(sb
));
892 ext3_journal_dirty_metadata(handle
, sbi
->s_sbh
);
897 if ((err2
= ext3_journal_stop(handle
)) && !err
)
900 update_backups(sb
, sbi
->s_sbh
->b_blocknr
, (char *)es
,
901 sizeof(struct ext3_super_block
));
902 update_backups(sb
, primary
->b_blocknr
, primary
->b_data
,
908 } /* ext3_group_add */
910 /* Extend the filesystem to the new number of blocks specified. This entry
911 * point is only used to extend the current filesystem to the end of the last
912 * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
913 * for emergencies (because it has no dependencies on reserved blocks).
915 * If we _really_ wanted, we could use default values to call ext3_group_add()
916 * allow the "remount" trick to work for arbitrary resizing, assuming enough
917 * GDT blocks are reserved to grow to the desired size.
919 int ext3_group_extend(struct super_block
*sb
, struct ext3_super_block
*es
,
920 ext3_fsblk_t n_blocks_count
)
922 ext3_fsblk_t o_blocks_count
;
923 unsigned long o_groups_count
;
926 struct buffer_head
* bh
;
929 unsigned long freed_blocks
;
931 /* We don't need to worry about locking wrt other resizers just
932 * yet: we're going to revalidate es->s_blocks_count after
933 * taking lock_super() below. */
934 o_blocks_count
= le32_to_cpu(es
->s_blocks_count
);
935 o_groups_count
= EXT3_SB(sb
)->s_groups_count
;
937 if (test_opt(sb
, DEBUG
))
938 printk(KERN_DEBUG
"EXT3-fs: extending last group from "E3FSBLK
" uto "E3FSBLK
" blocks\n",
939 o_blocks_count
, n_blocks_count
);
941 if (n_blocks_count
== 0 || n_blocks_count
== o_blocks_count
)
944 if (n_blocks_count
> (sector_t
)(~0ULL) >> (sb
->s_blocksize_bits
- 9)) {
945 printk(KERN_ERR
"EXT3-fs: filesystem on %s:"
946 " too large to resize to %lu blocks safely\n",
947 sb
->s_id
, n_blocks_count
);
948 if (sizeof(sector_t
) < 8)
949 ext3_warning(sb
, __FUNCTION__
,
950 "CONFIG_LBD not enabled\n");
954 if (n_blocks_count
< o_blocks_count
) {
955 ext3_warning(sb
, __FUNCTION__
,
956 "can't shrink FS - resize aborted");
960 /* Handle the remaining blocks in the last group only. */
961 last
= (o_blocks_count
- le32_to_cpu(es
->s_first_data_block
)) %
962 EXT3_BLOCKS_PER_GROUP(sb
);
965 ext3_warning(sb
, __FUNCTION__
,
966 "need to use ext2online to resize further");
970 add
= EXT3_BLOCKS_PER_GROUP(sb
) - last
;
972 if (o_blocks_count
+ add
< o_blocks_count
) {
973 ext3_warning(sb
, __FUNCTION__
, "blocks_count overflow");
977 if (o_blocks_count
+ add
> n_blocks_count
)
978 add
= n_blocks_count
- o_blocks_count
;
980 if (o_blocks_count
+ add
< n_blocks_count
)
981 ext3_warning(sb
, __FUNCTION__
,
982 "will only finish group ("E3FSBLK
984 o_blocks_count
+ add
, add
);
986 /* See if the device is actually as big as what was requested */
987 bh
= sb_bread(sb
, o_blocks_count
+ add
-1);
989 ext3_warning(sb
, __FUNCTION__
,
990 "can't read last block, resize aborted");
995 /* We will update the superblock, one block bitmap, and
996 * one group descriptor via ext3_free_blocks().
998 handle
= ext3_journal_start_sb(sb
, 3);
999 if (IS_ERR(handle
)) {
1000 err
= PTR_ERR(handle
);
1001 ext3_warning(sb
, __FUNCTION__
, "error %d on journal start",err
);
1006 if (o_blocks_count
!= le32_to_cpu(es
->s_blocks_count
)) {
1007 ext3_warning(sb
, __FUNCTION__
,
1008 "multiple resizers run on filesystem!");
1014 if ((err
= ext3_journal_get_write_access(handle
,
1015 EXT3_SB(sb
)->s_sbh
))) {
1016 ext3_warning(sb
, __FUNCTION__
,
1017 "error %d on journal write access", err
);
1019 ext3_journal_stop(handle
);
1022 es
->s_blocks_count
= cpu_to_le32(o_blocks_count
+ add
);
1023 ext3_journal_dirty_metadata(handle
, EXT3_SB(sb
)->s_sbh
);
1026 ext3_debug("freeing blocks %lu through "E3FSBLK
"\n", o_blocks_count
,
1027 o_blocks_count
+ add
);
1028 ext3_free_blocks_sb(handle
, sb
, o_blocks_count
, add
, &freed_blocks
);
1029 ext3_debug("freed blocks "E3FSBLK
" through "E3FSBLK
"\n", o_blocks_count
,
1030 o_blocks_count
+ add
);
1031 if ((err
= ext3_journal_stop(handle
)))
1033 if (test_opt(sb
, DEBUG
))
1034 printk(KERN_DEBUG
"EXT3-fs: extended group to %u blocks\n",
1035 le32_to_cpu(es
->s_blocks_count
));
1036 update_backups(sb
, EXT3_SB(sb
)->s_sbh
->b_blocknr
, (char *)es
,
1037 sizeof(struct ext3_super_block
));
1040 } /* ext3_group_extend */