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.
11 #include <linux/config.h>
15 #include <linux/sched.h>
16 #include <linux/smp_lock.h>
17 #include <linux/ext3_jbd.h>
19 #include <linux/errno.h>
20 #include <linux/slab.h>
23 #define outside(b, first, last) ((b) < (first) || (b) >= (last))
24 #define inside(b, first, last) ((b) >= (first) && (b) < (last))
26 static int verify_group_input(struct super_block
*sb
,
27 struct ext3_new_group_data
*input
)
29 struct ext3_sb_info
*sbi
= EXT3_SB(sb
);
30 struct ext3_super_block
*es
= sbi
->s_es
;
31 ext3_fsblk_t start
= le32_to_cpu(es
->s_blocks_count
);
32 ext3_fsblk_t end
= start
+ input
->blocks_count
;
33 unsigned group
= input
->group
;
34 ext3_fsblk_t itend
= input
->inode_table
+ sbi
->s_itb_per_group
;
35 unsigned overhead
= ext3_bg_has_super(sb
, group
) ?
36 (1 + ext3_bg_num_gdb(sb
, group
) +
37 le16_to_cpu(es
->s_reserved_gdt_blocks
)) : 0;
38 ext3_fsblk_t metaend
= start
+ overhead
;
39 struct buffer_head
*bh
= NULL
;
40 ext3_grpblk_t free_blocks_count
;
43 input
->free_blocks_count
= free_blocks_count
=
44 input
->blocks_count
- 2 - overhead
- sbi
->s_itb_per_group
;
46 if (test_opt(sb
, DEBUG
))
47 printk(KERN_DEBUG
"EXT3-fs: adding %s group %u: %u blocks "
48 "(%d free, %u reserved)\n",
49 ext3_bg_has_super(sb
, input
->group
) ? "normal" :
50 "no-super", input
->group
, input
->blocks_count
,
51 free_blocks_count
, input
->reserved_blocks
);
53 if (group
!= sbi
->s_groups_count
)
54 ext3_warning(sb
, __FUNCTION__
,
55 "Cannot add at group %u (only %lu groups)",
56 input
->group
, sbi
->s_groups_count
);
57 else if ((start
- le32_to_cpu(es
->s_first_data_block
)) %
58 EXT3_BLOCKS_PER_GROUP(sb
))
59 ext3_warning(sb
, __FUNCTION__
, "Last group not full");
60 else if (input
->reserved_blocks
> input
->blocks_count
/ 5)
61 ext3_warning(sb
, __FUNCTION__
, "Reserved blocks too high (%u)",
62 input
->reserved_blocks
);
63 else if (free_blocks_count
< 0)
64 ext3_warning(sb
, __FUNCTION__
, "Bad blocks count %u",
66 else if (!(bh
= sb_bread(sb
, end
- 1)))
67 ext3_warning(sb
, __FUNCTION__
,
68 "Cannot read last block ("E3FSBLK
")",
70 else if (outside(input
->block_bitmap
, start
, end
))
71 ext3_warning(sb
, __FUNCTION__
,
72 "Block bitmap not in group (block %u)",
74 else if (outside(input
->inode_bitmap
, start
, end
))
75 ext3_warning(sb
, __FUNCTION__
,
76 "Inode bitmap not in group (block %u)",
78 else if (outside(input
->inode_table
, start
, end
) ||
79 outside(itend
- 1, start
, end
))
80 ext3_warning(sb
, __FUNCTION__
,
81 "Inode table not in group (blocks %u-"E3FSBLK
")",
82 input
->inode_table
, itend
- 1);
83 else if (input
->inode_bitmap
== input
->block_bitmap
)
84 ext3_warning(sb
, __FUNCTION__
,
85 "Block bitmap same as inode bitmap (%u)",
87 else if (inside(input
->block_bitmap
, input
->inode_table
, itend
))
88 ext3_warning(sb
, __FUNCTION__
,
89 "Block bitmap (%u) in inode table (%u-"E3FSBLK
")",
90 input
->block_bitmap
, input
->inode_table
, itend
-1);
91 else if (inside(input
->inode_bitmap
, input
->inode_table
, itend
))
92 ext3_warning(sb
, __FUNCTION__
,
93 "Inode bitmap (%u) in inode table (%u-"E3FSBLK
")",
94 input
->inode_bitmap
, input
->inode_table
, itend
-1);
95 else if (inside(input
->block_bitmap
, start
, metaend
))
96 ext3_warning(sb
, __FUNCTION__
,
97 "Block bitmap (%u) in GDT table"
98 " ("E3FSBLK
"-"E3FSBLK
")",
99 input
->block_bitmap
, start
, metaend
- 1);
100 else if (inside(input
->inode_bitmap
, start
, metaend
))
101 ext3_warning(sb
, __FUNCTION__
,
102 "Inode bitmap (%u) in GDT table"
103 " ("E3FSBLK
"-"E3FSBLK
")",
104 input
->inode_bitmap
, start
, metaend
- 1);
105 else if (inside(input
->inode_table
, start
, metaend
) ||
106 inside(itend
- 1, start
, metaend
))
107 ext3_warning(sb
, __FUNCTION__
,
108 "Inode table (%u-"E3FSBLK
") overlaps"
109 "GDT table ("E3FSBLK
"-"E3FSBLK
")",
110 input
->inode_table
, itend
- 1, start
, metaend
- 1);
118 static struct buffer_head
*bclean(handle_t
*handle
, struct super_block
*sb
,
121 struct buffer_head
*bh
;
124 bh
= sb_getblk(sb
, blk
);
126 return ERR_PTR(-EIO
);
127 if ((err
= ext3_journal_get_write_access(handle
, bh
))) {
132 memset(bh
->b_data
, 0, sb
->s_blocksize
);
133 set_buffer_uptodate(bh
);
141 * To avoid calling the atomic setbit hundreds or thousands of times, we only
142 * need to use it within a single byte (to ensure we get endianness right).
143 * We can use memset for the rest of the bitmap as there are no other users.
145 static void mark_bitmap_end(int start_bit
, int end_bit
, char *bitmap
)
149 if (start_bit
>= end_bit
)
152 ext3_debug("mark end bits +%d through +%d used\n", start_bit
, end_bit
);
153 for (i
= start_bit
; i
< ((start_bit
+ 7) & ~7UL); i
++)
154 ext3_set_bit(i
, bitmap
);
156 memset(bitmap
+ (i
>> 3), 0xff, (end_bit
- i
) >> 3);
160 * Set up the block and inode bitmaps, and the inode table for the new group.
161 * This doesn't need to be part of the main transaction, since we are only
162 * changing blocks outside the actual filesystem. We still do journaling to
163 * ensure the recovery is correct in case of a failure just after resize.
164 * If any part of this fails, we simply abort the resize.
166 static int setup_new_group_blocks(struct super_block
*sb
,
167 struct ext3_new_group_data
*input
)
169 struct ext3_sb_info
*sbi
= EXT3_SB(sb
);
170 ext3_fsblk_t start
= ext3_group_first_block_no(sb
, input
->group
);
171 int reserved_gdb
= ext3_bg_has_super(sb
, input
->group
) ?
172 le16_to_cpu(sbi
->s_es
->s_reserved_gdt_blocks
) : 0;
173 unsigned long gdblocks
= ext3_bg_num_gdb(sb
, input
->group
);
174 struct buffer_head
*bh
;
181 handle
= ext3_journal_start_sb(sb
, reserved_gdb
+ gdblocks
+
182 2 + sbi
->s_itb_per_group
);
184 return PTR_ERR(handle
);
187 if (input
->group
!= sbi
->s_groups_count
) {
192 if (IS_ERR(bh
= bclean(handle
, sb
, input
->block_bitmap
))) {
197 if (ext3_bg_has_super(sb
, input
->group
)) {
198 ext3_debug("mark backup superblock %#04lx (+0)\n", start
);
199 ext3_set_bit(0, bh
->b_data
);
202 /* Copy all of the GDT blocks into the backup in this group */
203 for (i
= 0, bit
= 1, block
= start
+ 1;
204 i
< gdblocks
; i
++, block
++, bit
++) {
205 struct buffer_head
*gdb
;
207 ext3_debug("update backup group %#04lx (+%d)\n", block
, bit
);
209 gdb
= sb_getblk(sb
, block
);
214 if ((err
= ext3_journal_get_write_access(handle
, gdb
))) {
219 memcpy(gdb
->b_data
, sbi
->s_group_desc
[i
]->b_data
, bh
->b_size
);
220 set_buffer_uptodate(gdb
);
222 ext3_journal_dirty_metadata(handle
, gdb
);
223 ext3_set_bit(bit
, bh
->b_data
);
227 /* Zero out all of the reserved backup group descriptor table blocks */
228 for (i
= 0, bit
= gdblocks
+ 1, block
= start
+ bit
;
229 i
< reserved_gdb
; i
++, block
++, bit
++) {
230 struct buffer_head
*gdb
;
232 ext3_debug("clear reserved block %#04lx (+%d)\n", block
, bit
);
234 if (IS_ERR(gdb
= bclean(handle
, sb
, block
))) {
238 ext3_journal_dirty_metadata(handle
, gdb
);
239 ext3_set_bit(bit
, bh
->b_data
);
242 ext3_debug("mark block bitmap %#04x (+%ld)\n", input
->block_bitmap
,
243 input
->block_bitmap
- start
);
244 ext3_set_bit(input
->block_bitmap
- start
, bh
->b_data
);
245 ext3_debug("mark inode bitmap %#04x (+%ld)\n", input
->inode_bitmap
,
246 input
->inode_bitmap
- start
);
247 ext3_set_bit(input
->inode_bitmap
- start
, bh
->b_data
);
249 /* Zero out all of the inode table blocks */
250 for (i
= 0, block
= input
->inode_table
, bit
= block
- start
;
251 i
< sbi
->s_itb_per_group
; i
++, bit
++, block
++) {
252 struct buffer_head
*it
;
254 ext3_debug("clear inode block %#04lx (+%d)\n", block
, bit
);
255 if (IS_ERR(it
= bclean(handle
, sb
, block
))) {
259 ext3_journal_dirty_metadata(handle
, it
);
261 ext3_set_bit(bit
, bh
->b_data
);
263 mark_bitmap_end(input
->blocks_count
, EXT3_BLOCKS_PER_GROUP(sb
),
265 ext3_journal_dirty_metadata(handle
, bh
);
268 /* Mark unused entries in inode bitmap used */
269 ext3_debug("clear inode bitmap %#04x (+%ld)\n",
270 input
->inode_bitmap
, input
->inode_bitmap
- start
);
271 if (IS_ERR(bh
= bclean(handle
, sb
, input
->inode_bitmap
))) {
276 mark_bitmap_end(EXT3_INODES_PER_GROUP(sb
), EXT3_BLOCKS_PER_GROUP(sb
),
278 ext3_journal_dirty_metadata(handle
, bh
);
284 if ((err2
= ext3_journal_stop(handle
)) && !err
)
291 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
292 * ext3 filesystem. The counters should be initialized to 1, 5, and 7 before
293 * calling this for the first time. In a sparse filesystem it will be the
294 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
295 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
297 static unsigned ext3_list_backups(struct super_block
*sb
, unsigned *three
,
298 unsigned *five
, unsigned *seven
)
300 unsigned *min
= three
;
304 if (!EXT3_HAS_RO_COMPAT_FEATURE(sb
,
305 EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER
)) {
327 * Check that all of the backup GDT blocks are held in the primary GDT block.
328 * It is assumed that they are stored in group order. Returns the number of
329 * groups in current filesystem that have BACKUPS, or -ve error code.
331 static int verify_reserved_gdb(struct super_block
*sb
,
332 struct buffer_head
*primary
)
334 const ext3_fsblk_t blk
= primary
->b_blocknr
;
335 const unsigned long end
= EXT3_SB(sb
)->s_groups_count
;
340 __u32
*p
= (__u32
*)primary
->b_data
;
343 while ((grp
= ext3_list_backups(sb
, &three
, &five
, &seven
)) < end
) {
344 if (le32_to_cpu(*p
++) != grp
* EXT3_BLOCKS_PER_GROUP(sb
) + blk
){
345 ext3_warning(sb
, __FUNCTION__
,
346 "reserved GDT "E3FSBLK
347 " missing grp %d ("E3FSBLK
")",
349 grp
* EXT3_BLOCKS_PER_GROUP(sb
) + blk
);
352 if (++gdbackups
> EXT3_ADDR_PER_BLOCK(sb
))
360 * Called when we need to bring a reserved group descriptor table block into
361 * use from the resize inode. The primary copy of the new GDT block currently
362 * is an indirect block (under the double indirect block in the resize inode).
363 * The new backup GDT blocks will be stored as leaf blocks in this indirect
364 * block, in group order. Even though we know all the block numbers we need,
365 * we check to ensure that the resize inode has actually reserved these blocks.
367 * Don't need to update the block bitmaps because the blocks are still in use.
369 * We get all of the error cases out of the way, so that we are sure to not
370 * fail once we start modifying the data on disk, because JBD has no rollback.
372 static int add_new_gdb(handle_t
*handle
, struct inode
*inode
,
373 struct ext3_new_group_data
*input
,
374 struct buffer_head
**primary
)
376 struct super_block
*sb
= inode
->i_sb
;
377 struct ext3_super_block
*es
= EXT3_SB(sb
)->s_es
;
378 unsigned long gdb_num
= input
->group
/ EXT3_DESC_PER_BLOCK(sb
);
379 ext3_fsblk_t gdblock
= EXT3_SB(sb
)->s_sbh
->b_blocknr
+ 1 + gdb_num
;
380 struct buffer_head
**o_group_desc
, **n_group_desc
;
381 struct buffer_head
*dind
;
383 struct ext3_iloc iloc
;
387 if (test_opt(sb
, DEBUG
))
389 "EXT3-fs: ext3_add_new_gdb: adding group block %lu\n",
393 * If we are not using the primary superblock/GDT copy don't resize,
394 * because the user tools have no way of handling this. Probably a
395 * bad time to do it anyways.
397 if (EXT3_SB(sb
)->s_sbh
->b_blocknr
!=
398 le32_to_cpu(EXT3_SB(sb
)->s_es
->s_first_data_block
)) {
399 ext3_warning(sb
, __FUNCTION__
,
400 "won't resize using backup superblock at %llu",
401 (unsigned long long)EXT3_SB(sb
)->s_sbh
->b_blocknr
);
405 *primary
= sb_bread(sb
, gdblock
);
409 if ((gdbackups
= verify_reserved_gdb(sb
, *primary
)) < 0) {
414 data
= EXT3_I(inode
)->i_data
+ EXT3_DIND_BLOCK
;
415 dind
= sb_bread(sb
, le32_to_cpu(*data
));
421 data
= (__u32
*)dind
->b_data
;
422 if (le32_to_cpu(data
[gdb_num
% EXT3_ADDR_PER_BLOCK(sb
)]) != gdblock
) {
423 ext3_warning(sb
, __FUNCTION__
,
424 "new group %u GDT block "E3FSBLK
" not reserved",
425 input
->group
, gdblock
);
430 if ((err
= ext3_journal_get_write_access(handle
, EXT3_SB(sb
)->s_sbh
)))
433 if ((err
= ext3_journal_get_write_access(handle
, *primary
)))
436 if ((err
= ext3_journal_get_write_access(handle
, dind
)))
439 /* ext3_reserve_inode_write() gets a reference on the iloc */
440 if ((err
= ext3_reserve_inode_write(handle
, inode
, &iloc
)))
443 n_group_desc
= (struct buffer_head
**)kmalloc((gdb_num
+ 1) *
444 sizeof(struct buffer_head
*), GFP_KERNEL
);
447 ext3_warning (sb
, __FUNCTION__
,
448 "not enough memory for %lu groups", gdb_num
+ 1);
453 * Finally, we have all of the possible failures behind us...
455 * Remove new GDT block from inode double-indirect block and clear out
456 * the new GDT block for use (which also "frees" the backup GDT blocks
457 * from the reserved inode). We don't need to change the bitmaps for
458 * these blocks, because they are marked as in-use from being in the
459 * reserved inode, and will become GDT blocks (primary and backup).
461 data
[gdb_num
% EXT3_ADDR_PER_BLOCK(sb
)] = 0;
462 ext3_journal_dirty_metadata(handle
, dind
);
464 inode
->i_blocks
-= (gdbackups
+ 1) * sb
->s_blocksize
>> 9;
465 ext3_mark_iloc_dirty(handle
, inode
, &iloc
);
466 memset((*primary
)->b_data
, 0, sb
->s_blocksize
);
467 ext3_journal_dirty_metadata(handle
, *primary
);
469 o_group_desc
= EXT3_SB(sb
)->s_group_desc
;
470 memcpy(n_group_desc
, o_group_desc
,
471 EXT3_SB(sb
)->s_gdb_count
* sizeof(struct buffer_head
*));
472 n_group_desc
[gdb_num
] = *primary
;
473 EXT3_SB(sb
)->s_group_desc
= n_group_desc
;
474 EXT3_SB(sb
)->s_gdb_count
++;
477 es
->s_reserved_gdt_blocks
=
478 cpu_to_le16(le16_to_cpu(es
->s_reserved_gdt_blocks
) - 1);
479 ext3_journal_dirty_metadata(handle
, EXT3_SB(sb
)->s_sbh
);
484 //ext3_journal_release_buffer(handle, iloc.bh);
487 //ext3_journal_release_buffer(handle, dind);
489 //ext3_journal_release_buffer(handle, *primary);
491 //ext3_journal_release_buffer(handle, *primary);
497 ext3_debug("leaving with error %d\n", err
);
502 * Called when we are adding a new group which has a backup copy of each of
503 * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
504 * We need to add these reserved backup GDT blocks to the resize inode, so
505 * that they are kept for future resizing and not allocated to files.
507 * Each reserved backup GDT block will go into a different indirect block.
508 * The indirect blocks are actually the primary reserved GDT blocks,
509 * so we know in advance what their block numbers are. We only get the
510 * double-indirect block to verify it is pointing to the primary reserved
511 * GDT blocks so we don't overwrite a data block by accident. The reserved
512 * backup GDT blocks are stored in their reserved primary GDT block.
514 static int reserve_backup_gdb(handle_t
*handle
, struct inode
*inode
,
515 struct ext3_new_group_data
*input
)
517 struct super_block
*sb
= inode
->i_sb
;
518 int reserved_gdb
=le16_to_cpu(EXT3_SB(sb
)->s_es
->s_reserved_gdt_blocks
);
519 struct buffer_head
**primary
;
520 struct buffer_head
*dind
;
521 struct ext3_iloc iloc
;
528 primary
= kmalloc(reserved_gdb
* sizeof(*primary
), GFP_KERNEL
);
532 data
= EXT3_I(inode
)->i_data
+ EXT3_DIND_BLOCK
;
533 dind
= sb_bread(sb
, le32_to_cpu(*data
));
539 blk
= EXT3_SB(sb
)->s_sbh
->b_blocknr
+ 1 + EXT3_SB(sb
)->s_gdb_count
;
540 data
= (__u32
*)dind
->b_data
+ EXT3_SB(sb
)->s_gdb_count
;
541 end
= (__u32
*)dind
->b_data
+ EXT3_ADDR_PER_BLOCK(sb
);
543 /* Get each reserved primary GDT block and verify it holds backups */
544 for (res
= 0; res
< reserved_gdb
; res
++, blk
++) {
545 if (le32_to_cpu(*data
) != blk
) {
546 ext3_warning(sb
, __FUNCTION__
,
547 "reserved block "E3FSBLK
548 " not at offset %ld",
549 blk
, (long)(data
- (__u32
*)dind
->b_data
));
553 primary
[res
] = sb_bread(sb
, blk
);
558 if ((gdbackups
= verify_reserved_gdb(sb
, primary
[res
])) < 0) {
559 brelse(primary
[res
]);
564 data
= (__u32
*)dind
->b_data
;
567 for (i
= 0; i
< reserved_gdb
; i
++) {
568 if ((err
= ext3_journal_get_write_access(handle
, primary
[i
]))) {
571 for (j = 0; j < i; j++)
572 ext3_journal_release_buffer(handle, primary[j]);
578 if ((err
= ext3_reserve_inode_write(handle
, inode
, &iloc
)))
582 * Finally we can add each of the reserved backup GDT blocks from
583 * the new group to its reserved primary GDT block.
585 blk
= input
->group
* EXT3_BLOCKS_PER_GROUP(sb
);
586 for (i
= 0; i
< reserved_gdb
; i
++) {
588 data
= (__u32
*)primary
[i
]->b_data
;
589 /* printk("reserving backup %lu[%u] = %lu\n",
590 primary[i]->b_blocknr, gdbackups,
591 blk + primary[i]->b_blocknr); */
592 data
[gdbackups
] = cpu_to_le32(blk
+ primary
[i
]->b_blocknr
);
593 err2
= ext3_journal_dirty_metadata(handle
, primary
[i
]);
597 inode
->i_blocks
+= reserved_gdb
* sb
->s_blocksize
>> 9;
598 ext3_mark_iloc_dirty(handle
, inode
, &iloc
);
602 brelse(primary
[res
]);
612 * Update the backup copies of the ext3 metadata. These don't need to be part
613 * of the main resize transaction, because e2fsck will re-write them if there
614 * is a problem (basically only OOM will cause a problem). However, we
615 * _should_ update the backups if possible, in case the primary gets trashed
616 * for some reason and we need to run e2fsck from a backup superblock. The
617 * important part is that the new block and inode counts are in the backup
618 * superblocks, and the location of the new group metadata in the GDT backups.
620 * We do not need lock_super() for this, because these blocks are not
621 * otherwise touched by the filesystem code when it is mounted. We don't
622 * need to worry about last changing from sbi->s_groups_count, because the
623 * worst that can happen is that we do not copy the full number of backups
624 * at this time. The resize which changed s_groups_count will backup again.
626 static void update_backups(struct super_block
*sb
,
627 int blk_off
, char *data
, int size
)
629 struct ext3_sb_info
*sbi
= EXT3_SB(sb
);
630 const unsigned long last
= sbi
->s_groups_count
;
631 const int bpg
= EXT3_BLOCKS_PER_GROUP(sb
);
636 int rest
= sb
->s_blocksize
- size
;
640 handle
= ext3_journal_start_sb(sb
, EXT3_MAX_TRANS_DATA
);
641 if (IS_ERR(handle
)) {
643 err
= PTR_ERR(handle
);
647 while ((group
= ext3_list_backups(sb
, &three
, &five
, &seven
)) < last
) {
648 struct buffer_head
*bh
;
650 /* Out of journal space, and can't get more - abort - so sad */
651 if (handle
->h_buffer_credits
== 0 &&
652 ext3_journal_extend(handle
, EXT3_MAX_TRANS_DATA
) &&
653 (err
= ext3_journal_restart(handle
, EXT3_MAX_TRANS_DATA
)))
656 bh
= sb_getblk(sb
, group
* bpg
+ blk_off
);
661 ext3_debug("update metadata backup %#04lx\n",
662 (unsigned long)bh
->b_blocknr
);
663 if ((err
= ext3_journal_get_write_access(handle
, bh
)))
666 memcpy(bh
->b_data
, data
, size
);
668 memset(bh
->b_data
+ size
, 0, rest
);
669 set_buffer_uptodate(bh
);
671 ext3_journal_dirty_metadata(handle
, bh
);
674 if ((err2
= ext3_journal_stop(handle
)) && !err
)
678 * Ugh! Need to have e2fsck write the backup copies. It is too
679 * late to revert the resize, we shouldn't fail just because of
680 * the backup copies (they are only needed in case of corruption).
682 * However, if we got here we have a journal problem too, so we
683 * can't really start a transaction to mark the superblock.
684 * Chicken out and just set the flag on the hope it will be written
685 * to disk, and if not - we will simply wait until next fsck.
689 ext3_warning(sb
, __FUNCTION__
,
690 "can't update backup for group %d (err %d), "
691 "forcing fsck on next reboot", group
, err
);
692 sbi
->s_mount_state
&= ~EXT3_VALID_FS
;
693 sbi
->s_es
->s_state
&= ~cpu_to_le16(EXT3_VALID_FS
);
694 mark_buffer_dirty(sbi
->s_sbh
);
698 /* Add group descriptor data to an existing or new group descriptor block.
699 * Ensure we handle all possible error conditions _before_ we start modifying
700 * the filesystem, because we cannot abort the transaction and not have it
701 * write the data to disk.
703 * If we are on a GDT block boundary, we need to get the reserved GDT block.
704 * Otherwise, we may need to add backup GDT blocks for a sparse group.
706 * We only need to hold the superblock lock while we are actually adding
707 * in the new group's counts to the superblock. Prior to that we have
708 * not really "added" the group at all. We re-check that we are still
709 * adding in the last group in case things have changed since verifying.
711 int ext3_group_add(struct super_block
*sb
, struct ext3_new_group_data
*input
)
713 struct ext3_sb_info
*sbi
= EXT3_SB(sb
);
714 struct ext3_super_block
*es
= sbi
->s_es
;
715 int reserved_gdb
= ext3_bg_has_super(sb
, input
->group
) ?
716 le16_to_cpu(es
->s_reserved_gdt_blocks
) : 0;
717 struct buffer_head
*primary
= NULL
;
718 struct ext3_group_desc
*gdp
;
719 struct inode
*inode
= NULL
;
721 int gdb_off
, gdb_num
;
724 gdb_num
= input
->group
/ EXT3_DESC_PER_BLOCK(sb
);
725 gdb_off
= input
->group
% EXT3_DESC_PER_BLOCK(sb
);
727 if (gdb_off
== 0 && !EXT3_HAS_RO_COMPAT_FEATURE(sb
,
728 EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER
)) {
729 ext3_warning(sb
, __FUNCTION__
,
730 "Can't resize non-sparse filesystem further");
734 if (reserved_gdb
|| gdb_off
== 0) {
735 if (!EXT3_HAS_COMPAT_FEATURE(sb
,
736 EXT3_FEATURE_COMPAT_RESIZE_INODE
)){
737 ext3_warning(sb
, __FUNCTION__
,
738 "No reserved GDT blocks, can't resize");
741 inode
= iget(sb
, EXT3_RESIZE_INO
);
742 if (!inode
|| is_bad_inode(inode
)) {
743 ext3_warning(sb
, __FUNCTION__
,
744 "Error opening resize inode");
750 if ((err
= verify_group_input(sb
, input
)))
753 if ((err
= setup_new_group_blocks(sb
, input
)))
757 * We will always be modifying at least the superblock and a GDT
758 * block. If we are adding a group past the last current GDT block,
759 * we will also modify the inode and the dindirect block. If we
760 * are adding a group with superblock/GDT backups we will also
761 * modify each of the reserved GDT dindirect blocks.
763 handle
= ext3_journal_start_sb(sb
,
764 ext3_bg_has_super(sb
, input
->group
) ?
765 3 + reserved_gdb
: 4);
766 if (IS_ERR(handle
)) {
767 err
= PTR_ERR(handle
);
772 if (input
->group
!= sbi
->s_groups_count
) {
773 ext3_warning(sb
, __FUNCTION__
,
774 "multiple resizers run on filesystem!");
779 if ((err
= ext3_journal_get_write_access(handle
, sbi
->s_sbh
)))
783 * We will only either add reserved group blocks to a backup group
784 * or remove reserved blocks for the first group in a new group block.
785 * Doing both would be mean more complex code, and sane people don't
786 * use non-sparse filesystems anymore. This is already checked above.
789 primary
= sbi
->s_group_desc
[gdb_num
];
790 if ((err
= ext3_journal_get_write_access(handle
, primary
)))
793 if (reserved_gdb
&& ext3_bg_num_gdb(sb
, input
->group
) &&
794 (err
= reserve_backup_gdb(handle
, inode
, input
)))
796 } else if ((err
= add_new_gdb(handle
, inode
, input
, &primary
)))
800 * OK, now we've set up the new group. Time to make it active.
802 * Current kernels don't lock all allocations via lock_super(),
803 * so we have to be safe wrt. concurrent accesses the group
804 * data. So we need to be careful to set all of the relevant
805 * group descriptor data etc. *before* we enable the group.
807 * The key field here is sbi->s_groups_count: as long as
808 * that retains its old value, nobody is going to access the new
811 * So first we update all the descriptor metadata for the new
812 * group; then we update the total disk blocks count; then we
813 * update the groups count to enable the group; then finally we
814 * update the free space counts so that the system can start
815 * using the new disk blocks.
818 /* Update group descriptor block for new group */
819 gdp
= (struct ext3_group_desc
*)primary
->b_data
+ gdb_off
;
821 gdp
->bg_block_bitmap
= cpu_to_le32(input
->block_bitmap
);
822 gdp
->bg_inode_bitmap
= cpu_to_le32(input
->inode_bitmap
);
823 gdp
->bg_inode_table
= cpu_to_le32(input
->inode_table
);
824 gdp
->bg_free_blocks_count
= cpu_to_le16(input
->free_blocks_count
);
825 gdp
->bg_free_inodes_count
= cpu_to_le16(EXT3_INODES_PER_GROUP(sb
));
828 * Make the new blocks and inodes valid next. We do this before
829 * increasing the group count so that once the group is enabled,
830 * all of its blocks and inodes are already valid.
832 * We always allocate group-by-group, then block-by-block or
833 * inode-by-inode within a group, so enabling these
834 * blocks/inodes before the group is live won't actually let us
835 * allocate the new space yet.
837 es
->s_blocks_count
= cpu_to_le32(le32_to_cpu(es
->s_blocks_count
) +
838 input
->blocks_count
);
839 es
->s_inodes_count
= cpu_to_le32(le32_to_cpu(es
->s_inodes_count
) +
840 EXT3_INODES_PER_GROUP(sb
));
843 * We need to protect s_groups_count against other CPUs seeing
844 * inconsistent state in the superblock.
846 * The precise rules we use are:
848 * * Writers of s_groups_count *must* hold lock_super
850 * * Writers must perform a smp_wmb() after updating all dependent
851 * data and before modifying the groups count
853 * * Readers must hold lock_super() over the access
855 * * Readers must perform an smp_rmb() after reading the groups count
856 * and before reading any dependent data.
858 * NB. These rules can be relaxed when checking the group count
859 * while freeing data, as we can only allocate from a block
860 * group after serialising against the group count, and we can
861 * only then free after serialising in turn against that
866 /* Update the global fs size fields */
867 sbi
->s_groups_count
++;
869 ext3_journal_dirty_metadata(handle
, primary
);
871 /* Update the reserved block counts only once the new group is
873 es
->s_r_blocks_count
= cpu_to_le32(le32_to_cpu(es
->s_r_blocks_count
) +
874 input
->reserved_blocks
);
876 /* Update the free space counts */
877 percpu_counter_mod(&sbi
->s_freeblocks_counter
,
878 input
->free_blocks_count
);
879 percpu_counter_mod(&sbi
->s_freeinodes_counter
,
880 EXT3_INODES_PER_GROUP(sb
));
882 ext3_journal_dirty_metadata(handle
, sbi
->s_sbh
);
887 if ((err2
= ext3_journal_stop(handle
)) && !err
)
890 update_backups(sb
, sbi
->s_sbh
->b_blocknr
, (char *)es
,
891 sizeof(struct ext3_super_block
));
892 update_backups(sb
, primary
->b_blocknr
, primary
->b_data
,
898 } /* ext3_group_add */
900 /* Extend the filesystem to the new number of blocks specified. This entry
901 * point is only used to extend the current filesystem to the end of the last
902 * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
903 * for emergencies (because it has no dependencies on reserved blocks).
905 * If we _really_ wanted, we could use default values to call ext3_group_add()
906 * allow the "remount" trick to work for arbitrary resizing, assuming enough
907 * GDT blocks are reserved to grow to the desired size.
909 int ext3_group_extend(struct super_block
*sb
, struct ext3_super_block
*es
,
910 ext3_fsblk_t n_blocks_count
)
912 ext3_fsblk_t o_blocks_count
;
913 unsigned long o_groups_count
;
916 struct buffer_head
* bh
;
919 unsigned long freed_blocks
;
921 /* We don't need to worry about locking wrt other resizers just
922 * yet: we're going to revalidate es->s_blocks_count after
923 * taking lock_super() below. */
924 o_blocks_count
= le32_to_cpu(es
->s_blocks_count
);
925 o_groups_count
= EXT3_SB(sb
)->s_groups_count
;
927 if (test_opt(sb
, DEBUG
))
928 printk(KERN_DEBUG
"EXT3-fs: extending last group from "E3FSBLK
" uto "E3FSBLK
" blocks\n",
929 o_blocks_count
, n_blocks_count
);
931 if (n_blocks_count
== 0 || n_blocks_count
== o_blocks_count
)
934 if (n_blocks_count
> (sector_t
)(~0ULL) >> (sb
->s_blocksize_bits
- 9)) {
935 printk(KERN_ERR
"EXT3-fs: filesystem on %s:"
936 " too large to resize to %lu blocks safely\n",
937 sb
->s_id
, n_blocks_count
);
938 if (sizeof(sector_t
) < 8)
939 ext3_warning(sb
, __FUNCTION__
,
940 "CONFIG_LBD not enabled\n");
944 if (n_blocks_count
< o_blocks_count
) {
945 ext3_warning(sb
, __FUNCTION__
,
946 "can't shrink FS - resize aborted");
950 /* Handle the remaining blocks in the last group only. */
951 last
= (o_blocks_count
- le32_to_cpu(es
->s_first_data_block
)) %
952 EXT3_BLOCKS_PER_GROUP(sb
);
955 ext3_warning(sb
, __FUNCTION__
,
956 "need to use ext2online to resize further");
960 add
= EXT3_BLOCKS_PER_GROUP(sb
) - last
;
962 if (o_blocks_count
+ add
> n_blocks_count
)
963 add
= n_blocks_count
- o_blocks_count
;
965 if (o_blocks_count
+ add
< n_blocks_count
)
966 ext3_warning(sb
, __FUNCTION__
,
967 "will only finish group ("E3FSBLK
969 o_blocks_count
+ add
, add
);
971 /* See if the device is actually as big as what was requested */
972 bh
= sb_bread(sb
, o_blocks_count
+ add
-1);
974 ext3_warning(sb
, __FUNCTION__
,
975 "can't read last block, resize aborted");
980 /* We will update the superblock, one block bitmap, and
981 * one group descriptor via ext3_free_blocks().
983 handle
= ext3_journal_start_sb(sb
, 3);
984 if (IS_ERR(handle
)) {
985 err
= PTR_ERR(handle
);
986 ext3_warning(sb
, __FUNCTION__
, "error %d on journal start",err
);
991 if (o_blocks_count
!= le32_to_cpu(es
->s_blocks_count
)) {
992 ext3_warning(sb
, __FUNCTION__
,
993 "multiple resizers run on filesystem!");
999 if ((err
= ext3_journal_get_write_access(handle
,
1000 EXT3_SB(sb
)->s_sbh
))) {
1001 ext3_warning(sb
, __FUNCTION__
,
1002 "error %d on journal write access", err
);
1004 ext3_journal_stop(handle
);
1007 es
->s_blocks_count
= cpu_to_le32(o_blocks_count
+ add
);
1008 ext3_journal_dirty_metadata(handle
, EXT3_SB(sb
)->s_sbh
);
1011 ext3_debug("freeing blocks %lu through "E3FSBLK
"\n", o_blocks_count
,
1012 o_blocks_count
+ add
);
1013 ext3_free_blocks_sb(handle
, sb
, o_blocks_count
, add
, &freed_blocks
);
1014 ext3_debug("freed blocks "E3FSBLK
" through "E3FSBLK
"\n", o_blocks_count
,
1015 o_blocks_count
+ add
);
1016 if ((err
= ext3_journal_stop(handle
)))
1018 if (test_opt(sb
, DEBUG
))
1019 printk(KERN_DEBUG
"EXT3-fs: extended group to %u blocks\n",
1020 le32_to_cpu(es
->s_blocks_count
));
1021 update_backups(sb
, EXT3_SB(sb
)->s_sbh
->b_blocknr
, (char *)es
,
1022 sizeof(struct ext3_super_block
));
1025 } /* ext3_group_extend */