blob: 6076d5e4b5135f80b598e1f965fdc93ecf43c9d0 [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/resize.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
Mingming Cao617ba132006-10-11 01:20:53 -07004 * Support for resizing an ext4 filesystem while it is mounted.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005 *
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
Mingming Cao617ba132006-10-11 01:20:53 -070012#define EXT4FS_DEBUG
Dave Kleikampac27a0e2006-10-11 01:20:50 -070013
Dave Kleikampac27a0e2006-10-11 01:20:50 -070014#include <linux/errno.h>
15#include <linux/slab.h>
16
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040017#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070018
Yongqiang Yang8f82f842011-07-26 21:35:44 -040019int ext4_resize_begin(struct super_block *sb)
20{
21 int ret = 0;
22
23 if (!capable(CAP_SYS_RESOURCE))
24 return -EPERM;
25
Yongqiang Yangce723c32011-07-26 21:39:09 -040026 /*
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
Yongqiang Yang8f82f842011-07-26 21:35:44 -040036 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
Dave Kleikampac27a0e2006-10-11 01:20:50 -070048#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,
Mingming Cao617ba132006-10-11 01:20:53 -070052 struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070053{
Mingming Cao617ba132006-10-11 01:20:53 -070054 struct ext4_sb_info *sbi = EXT4_SB(sb);
55 struct ext4_super_block *es = sbi->s_es;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070056 ext4_fsblk_t start = ext4_blocks_count(es);
Mingming Cao617ba132006-10-11 01:20:53 -070057 ext4_fsblk_t end = start + input->blocks_count;
Avantika Mathurfd2d4292008-01-28 23:58:27 -050058 ext4_group_t group = input->group;
Mingming Cao617ba132006-10-11 01:20:53 -070059 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) +
Dave Kleikampac27a0e2006-10-11 01:20:50 -070062 le16_to_cpu(es->s_reserved_gdt_blocks)) : 0;
Mingming Cao617ba132006-10-11 01:20:53 -070063 ext4_fsblk_t metaend = start + overhead;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070064 struct buffer_head *bh = NULL;
Mingming Cao3a5b2ec2006-10-11 01:21:05 -070065 ext4_grpblk_t free_blocks_count, offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070066 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))
Mingming Cao617ba132006-10-11 01:20:53 -070072 printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks "
Dave Kleikampac27a0e2006-10-11 01:20:50 -070073 "(%d free, %u reserved)\n",
Mingming Cao617ba132006-10-11 01:20:53 -070074 ext4_bg_has_super(sb, input->group) ? "normal" :
Dave Kleikampac27a0e2006-10-11 01:20:50 -070075 "no-super", input->group, input->blocks_count,
76 free_blocks_count, input->reserved_blocks);
77
Mingming Cao3a5b2ec2006-10-11 01:21:05 -070078 ext4_get_group_no_and_offset(sb, start, NULL, &offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070079 if (group != sbi->s_groups_count)
Eric Sandeen12062dd2010-02-15 14:19:27 -050080 ext4_warning(sb, "Cannot add at group %u (only %u groups)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -070081 input->group, sbi->s_groups_count);
Mingming Cao3a5b2ec2006-10-11 01:21:05 -070082 else if (offset != 0)
Eric Sandeen12062dd2010-02-15 14:19:27 -050083 ext4_warning(sb, "Last group not full");
Dave Kleikampac27a0e2006-10-11 01:20:50 -070084 else if (input->reserved_blocks > input->blocks_count / 5)
Eric Sandeen12062dd2010-02-15 14:19:27 -050085 ext4_warning(sb, "Reserved blocks too high (%u)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -070086 input->reserved_blocks);
87 else if (free_blocks_count < 0)
Eric Sandeen12062dd2010-02-15 14:19:27 -050088 ext4_warning(sb, "Bad blocks count %u",
Dave Kleikampac27a0e2006-10-11 01:20:50 -070089 input->blocks_count);
90 else if (!(bh = sb_bread(sb, end - 1)))
Eric Sandeen12062dd2010-02-15 14:19:27 -050091 ext4_warning(sb, "Cannot read last block (%llu)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -070092 end - 1);
93 else if (outside(input->block_bitmap, start, end))
Eric Sandeen12062dd2010-02-15 14:19:27 -050094 ext4_warning(sb, "Block bitmap not in group (block %llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -070095 (unsigned long long)input->block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070096 else if (outside(input->inode_bitmap, start, end))
Eric Sandeen12062dd2010-02-15 14:19:27 -050097 ext4_warning(sb, "Inode bitmap not in group (block %llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -070098 (unsigned long long)input->inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070099 else if (outside(input->inode_table, start, end) ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400100 outside(itend - 1, start, end))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500101 ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700102 (unsigned long long)input->inode_table, itend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700103 else if (input->inode_bitmap == input->block_bitmap)
Eric Sandeen12062dd2010-02-15 14:19:27 -0500104 ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700105 (unsigned long long)input->block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700106 else if (inside(input->block_bitmap, input->inode_table, itend))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500107 ext4_warning(sb, "Block bitmap (%llu) in inode table "
108 "(%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700109 (unsigned long long)input->block_bitmap,
110 (unsigned long long)input->inode_table, itend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700111 else if (inside(input->inode_bitmap, input->inode_table, itend))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500112 ext4_warning(sb, "Inode bitmap (%llu) in inode table "
113 "(%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700114 (unsigned long long)input->inode_bitmap,
115 (unsigned long long)input->inode_table, itend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700116 else if (inside(input->block_bitmap, start, metaend))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500117 ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700118 (unsigned long long)input->block_bitmap,
119 start, metaend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700120 else if (inside(input->inode_bitmap, start, metaend))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500121 ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700122 (unsigned long long)input->inode_bitmap,
123 start, metaend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700124 else if (inside(input->inode_table, start, metaend) ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400125 inside(itend - 1, start, metaend))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500126 ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table "
127 "(%llu-%llu)",
Randy Dunlap1939e492006-10-28 10:38:26 -0700128 (unsigned long long)input->inode_table,
129 itend - 1, start, metaend - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700130 else
131 err = 0;
132 brelse(bh);
133
134 return err;
135}
136
Yongqiang Yang28c7bac2012-01-03 23:22:50 -0500137/*
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
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700192static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
Mingming Cao617ba132006-10-11 01:20:53 -0700193 ext4_fsblk_t blk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700194{
195 struct buffer_head *bh;
196 int err;
197
198 bh = sb_getblk(sb, blk);
199 if (!bh)
200 return ERR_PTR(-EIO);
Mingming Cao617ba132006-10-11 01:20:53 -0700201 if ((err = ext4_journal_get_write_access(handle, bh))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700202 brelse(bh);
203 bh = ERR_PTR(err);
204 } else {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700205 memset(bh->b_data, 0, sb->s_blocksize);
206 set_buffer_uptodate(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700207 }
208
209 return bh;
210}
211
212/*
Eric Sandeen14904102007-10-16 18:38:25 -0400213 * 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 */
Yongqiang Yang6d40bc52011-07-26 22:24:41 -0400217static int extend_or_restart_transaction(handle_t *handle, int thresh)
Eric Sandeen14904102007-10-16 18:38:25 -0400218{
219 int err;
220
Frank Mayhar03901312009-01-07 00:06:22 -0500221 if (ext4_handle_has_enough_credits(handle, thresh))
Eric Sandeen14904102007-10-16 18:38:25 -0400222 return 0;
223
224 err = ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA);
225 if (err < 0)
226 return err;
227 if (err) {
Yongqiang Yang6d40bc52011-07-26 22:24:41 -0400228 err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA);
229 if (err)
Eric Sandeen14904102007-10-16 18:38:25 -0400230 return err;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400231 }
Eric Sandeen14904102007-10-16 18:38:25 -0400232
233 return 0;
234}
235
236/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700237 * Set up the block and inode bitmaps, and the inode table for the new group.
238 * This doesn't need to be part of the main transaction, since we are only
239 * changing blocks outside the actual filesystem. We still do journaling to
240 * ensure the recovery is correct in case of a failure just after resize.
241 * If any part of this fails, we simply abort the resize.
242 */
243static int setup_new_group_blocks(struct super_block *sb,
Mingming Cao617ba132006-10-11 01:20:53 -0700244 struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700245{
Mingming Cao617ba132006-10-11 01:20:53 -0700246 struct ext4_sb_info *sbi = EXT4_SB(sb);
247 ext4_fsblk_t start = ext4_group_first_block_no(sb, input->group);
248 int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700249 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700250 unsigned long gdblocks = ext4_bg_num_gdb(sb, input->group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700251 struct buffer_head *bh;
252 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -0700253 ext4_fsblk_t block;
254 ext4_grpblk_t bit;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700255 int i;
256 int err = 0, err2;
257
Eric Sandeen14904102007-10-16 18:38:25 -0400258 /* This transaction may be extended/restarted along the way */
259 handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
260
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700261 if (IS_ERR(handle))
262 return PTR_ERR(handle);
263
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400264 BUG_ON(input->group != sbi->s_groups_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700265
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266 /* Copy all of the GDT blocks into the backup in this group */
267 for (i = 0, bit = 1, block = start + 1;
268 i < gdblocks; i++, block++, bit++) {
269 struct buffer_head *gdb;
270
Eric Sandeenc549a952008-01-28 23:58:27 -0500271 ext4_debug("update backup group %#04llx (+%d)\n", block, bit);
Yongqiang Yang6d40bc52011-07-26 22:24:41 -0400272 err = extend_or_restart_transaction(handle, 1);
273 if (err)
274 goto exit_journal;
Eric Sandeen14904102007-10-16 18:38:25 -0400275
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700276 gdb = sb_getblk(sb, block);
277 if (!gdb) {
278 err = -EIO;
Yongqiang Yang6d40bc52011-07-26 22:24:41 -0400279 goto exit_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700280 }
Mingming Cao617ba132006-10-11 01:20:53 -0700281 if ((err = ext4_journal_get_write_access(handle, gdb))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700282 brelse(gdb);
Yongqiang Yang6d40bc52011-07-26 22:24:41 -0400283 goto exit_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700284 }
Eric Sandeen5b615282007-10-16 18:38:25 -0400285 memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, gdb->b_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700286 set_buffer_uptodate(gdb);
Theodore Ts'ob4097142011-01-10 12:46:59 -0500287 err = ext4_handle_dirty_metadata(handle, NULL, gdb);
288 if (unlikely(err)) {
289 brelse(gdb);
Yongqiang Yang6d40bc52011-07-26 22:24:41 -0400290 goto exit_journal;
Theodore Ts'ob4097142011-01-10 12:46:59 -0500291 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700292 brelse(gdb);
293 }
294
295 /* Zero out all of the reserved backup group descriptor table blocks */
Theodore Ts'oda4889452011-02-21 20:39:58 -0500296 ext4_debug("clear inode table blocks %#04llx -> %#04lx\n",
Lukas Czernera31437b2010-10-27 21:30:05 -0400297 block, sbi->s_itb_per_group);
298 err = sb_issue_zeroout(sb, gdblocks + start + 1, reserved_gdb,
Theodore Ts'oa107e5a2010-10-27 23:44:47 -0400299 GFP_NOFS);
Lukas Czernera31437b2010-10-27 21:30:05 -0400300 if (err)
Yongqiang Yang6d40bc52011-07-26 22:24:41 -0400301 goto exit_journal;
302
303 err = extend_or_restart_transaction(handle, 2);
304 if (err)
305 goto exit_journal;
306
307 bh = bclean(handle, sb, input->block_bitmap);
308 if (IS_ERR(bh)) {
309 err = PTR_ERR(bh);
310 goto exit_journal;
311 }
Yongqiang Yangc3e94d12011-07-26 22:05:53 -0400312
313 if (ext4_bg_has_super(sb, input->group)) {
314 ext4_debug("mark backup group tables %#04llx (+0)\n", start);
315 ext4_set_bits(bh->b_data, 0, gdblocks + reserved_gdb + 1);
316 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700317
Eric Sandeenc549a952008-01-28 23:58:27 -0500318 ext4_debug("mark block bitmap %#04llx (+%llu)\n", input->block_bitmap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700319 input->block_bitmap - start);
Mingming Cao617ba132006-10-11 01:20:53 -0700320 ext4_set_bit(input->block_bitmap - start, bh->b_data);
Eric Sandeenc549a952008-01-28 23:58:27 -0500321 ext4_debug("mark inode bitmap %#04llx (+%llu)\n", input->inode_bitmap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700322 input->inode_bitmap - start);
Mingming Cao617ba132006-10-11 01:20:53 -0700323 ext4_set_bit(input->inode_bitmap - start, bh->b_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700324
325 /* Zero out all of the inode table blocks */
Lukas Czernera31437b2010-10-27 21:30:05 -0400326 block = input->inode_table;
Theodore Ts'oda4889452011-02-21 20:39:58 -0500327 ext4_debug("clear inode table blocks %#04llx -> %#04lx\n",
Lukas Czernera31437b2010-10-27 21:30:05 -0400328 block, sbi->s_itb_per_group);
Theodore Ts'oa107e5a2010-10-27 23:44:47 -0400329 err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group, GFP_NOFS);
Lukas Czernera31437b2010-10-27 21:30:05 -0400330 if (err)
331 goto exit_bh;
Yongqiang Yangc3e94d12011-07-26 22:05:53 -0400332 ext4_set_bits(bh->b_data, input->inode_table - start,
333 sbi->s_itb_per_group);
Eric Sandeen14904102007-10-16 18:38:25 -0400334
Eric Sandeen14904102007-10-16 18:38:25 -0400335
Theodore Ts'o61d08672010-10-27 21:30:15 -0400336 ext4_mark_bitmap_end(input->blocks_count, sb->s_blocksize * 8,
337 bh->b_data);
Theodore Ts'ob4097142011-01-10 12:46:59 -0500338 err = ext4_handle_dirty_metadata(handle, NULL, bh);
339 if (unlikely(err)) {
340 ext4_std_error(sb, err);
341 goto exit_bh;
342 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700343 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700344 /* Mark unused entries in inode bitmap used */
Eric Sandeenc549a952008-01-28 23:58:27 -0500345 ext4_debug("clear inode bitmap %#04llx (+%llu)\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700346 input->inode_bitmap, input->inode_bitmap - start);
347 if (IS_ERR(bh = bclean(handle, sb, input->inode_bitmap))) {
348 err = PTR_ERR(bh);
349 goto exit_journal;
350 }
351
Theodore Ts'o61d08672010-10-27 21:30:15 -0400352 ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8,
353 bh->b_data);
Theodore Ts'ob4097142011-01-10 12:46:59 -0500354 err = ext4_handle_dirty_metadata(handle, NULL, bh);
355 if (unlikely(err))
356 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700357exit_bh:
358 brelse(bh);
359
360exit_journal:
Mingming Cao617ba132006-10-11 01:20:53 -0700361 if ((err2 = ext4_journal_stop(handle)) && !err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700362 err = err2;
363
364 return err;
365}
366
367/*
368 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
Mingming Cao617ba132006-10-11 01:20:53 -0700369 * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700370 * calling this for the first time. In a sparse filesystem it will be the
371 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
372 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
373 */
Mingming Cao617ba132006-10-11 01:20:53 -0700374static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700375 unsigned *five, unsigned *seven)
376{
377 unsigned *min = three;
378 int mult = 3;
379 unsigned ret;
380
Mingming Cao617ba132006-10-11 01:20:53 -0700381 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
382 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700383 ret = *min;
384 *min += 1;
385 return ret;
386 }
387
388 if (*five < *min) {
389 min = five;
390 mult = 5;
391 }
392 if (*seven < *min) {
393 min = seven;
394 mult = 7;
395 }
396
397 ret = *min;
398 *min *= mult;
399
400 return ret;
401}
402
403/*
404 * Check that all of the backup GDT blocks are held in the primary GDT block.
405 * It is assumed that they are stored in group order. Returns the number of
406 * groups in current filesystem that have BACKUPS, or -ve error code.
407 */
408static int verify_reserved_gdb(struct super_block *sb,
409 struct buffer_head *primary)
410{
Mingming Cao617ba132006-10-11 01:20:53 -0700411 const ext4_fsblk_t blk = primary->b_blocknr;
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500412 const ext4_group_t end = EXT4_SB(sb)->s_groups_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700413 unsigned three = 1;
414 unsigned five = 5;
415 unsigned seven = 7;
416 unsigned grp;
417 __le32 *p = (__le32 *)primary->b_data;
418 int gdbackups = 0;
419
Mingming Cao617ba132006-10-11 01:20:53 -0700420 while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700421 if (le32_to_cpu(*p++) !=
422 grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
Eric Sandeen12062dd2010-02-15 14:19:27 -0500423 ext4_warning(sb, "reserved GDT %llu"
Mingming Cao2ae02102006-10-11 01:21:11 -0700424 " missing grp %d (%llu)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700425 blk, grp,
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700426 grp *
427 (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
428 blk);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700429 return -EINVAL;
430 }
Mingming Cao617ba132006-10-11 01:20:53 -0700431 if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700432 return -EFBIG;
433 }
434
435 return gdbackups;
436}
437
438/*
439 * Called when we need to bring a reserved group descriptor table block into
440 * use from the resize inode. The primary copy of the new GDT block currently
441 * is an indirect block (under the double indirect block in the resize inode).
442 * The new backup GDT blocks will be stored as leaf blocks in this indirect
443 * block, in group order. Even though we know all the block numbers we need,
444 * we check to ensure that the resize inode has actually reserved these blocks.
445 *
446 * Don't need to update the block bitmaps because the blocks are still in use.
447 *
448 * We get all of the error cases out of the way, so that we are sure to not
449 * fail once we start modifying the data on disk, because JBD has no rollback.
450 */
451static int add_new_gdb(handle_t *handle, struct inode *inode,
Yongqiang Yang2f919712011-07-27 21:16:33 -0400452 ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700453{
454 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -0700455 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Yongqiang Yang2f919712011-07-27 21:16:33 -0400456 unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700457 ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700458 struct buffer_head **o_group_desc, **n_group_desc;
459 struct buffer_head *dind;
Yongqiang Yang2f919712011-07-27 21:16:33 -0400460 struct buffer_head *gdb_bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700461 int gdbackups;
Mingming Cao617ba132006-10-11 01:20:53 -0700462 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700463 __le32 *data;
464 int err;
465
466 if (test_opt(sb, DEBUG))
467 printk(KERN_DEBUG
Mingming Cao617ba132006-10-11 01:20:53 -0700468 "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700469 gdb_num);
470
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400471 /*
472 * If we are not using the primary superblock/GDT copy don't resize,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400473 * because the user tools have no way of handling this. Probably a
474 * bad time to do it anyways.
475 */
Mingming Cao617ba132006-10-11 01:20:53 -0700476 if (EXT4_SB(sb)->s_sbh->b_blocknr !=
477 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500478 ext4_warning(sb, "won't resize using backup superblock at %llu",
Mingming Cao617ba132006-10-11 01:20:53 -0700479 (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700480 return -EPERM;
481 }
482
Yongqiang Yang2f919712011-07-27 21:16:33 -0400483 gdb_bh = sb_bread(sb, gdblock);
484 if (!gdb_bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700485 return -EIO;
486
Yongqiang Yang2f919712011-07-27 21:16:33 -0400487 gdbackups = verify_reserved_gdb(sb, gdb_bh);
488 if (gdbackups < 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700489 err = gdbackups;
490 goto exit_bh;
491 }
492
Mingming Cao617ba132006-10-11 01:20:53 -0700493 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700494 dind = sb_bread(sb, le32_to_cpu(*data));
495 if (!dind) {
496 err = -EIO;
497 goto exit_bh;
498 }
499
500 data = (__le32 *)dind->b_data;
Mingming Cao617ba132006-10-11 01:20:53 -0700501 if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500502 ext4_warning(sb, "new group %u GDT block %llu not reserved",
Yongqiang Yang2f919712011-07-27 21:16:33 -0400503 group, gdblock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700504 err = -EINVAL;
505 goto exit_dind;
506 }
507
Theodore Ts'ob4097142011-01-10 12:46:59 -0500508 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
509 if (unlikely(err))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700510 goto exit_dind;
511
Yongqiang Yang2f919712011-07-27 21:16:33 -0400512 err = ext4_journal_get_write_access(handle, gdb_bh);
Theodore Ts'ob4097142011-01-10 12:46:59 -0500513 if (unlikely(err))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700514 goto exit_sbh;
515
Theodore Ts'ob4097142011-01-10 12:46:59 -0500516 err = ext4_journal_get_write_access(handle, dind);
517 if (unlikely(err))
518 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700519
Mingming Cao617ba132006-10-11 01:20:53 -0700520 /* ext4_reserve_inode_write() gets a reference on the iloc */
Theodore Ts'ob4097142011-01-10 12:46:59 -0500521 err = ext4_reserve_inode_write(handle, inode, &iloc);
522 if (unlikely(err))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523 goto exit_dindj;
524
Theodore Ts'of18a5f22011-08-01 08:45:38 -0400525 n_group_desc = ext4_kvmalloc((gdb_num + 1) *
526 sizeof(struct buffer_head *),
527 GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700528 if (!n_group_desc) {
529 err = -ENOMEM;
Theodore Ts'of18a5f22011-08-01 08:45:38 -0400530 ext4_warning(sb, "not enough memory for %lu groups",
531 gdb_num + 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700532 goto exit_inode;
533 }
534
535 /*
536 * Finally, we have all of the possible failures behind us...
537 *
538 * Remove new GDT block from inode double-indirect block and clear out
539 * the new GDT block for use (which also "frees" the backup GDT blocks
540 * from the reserved inode). We don't need to change the bitmaps for
541 * these blocks, because they are marked as in-use from being in the
542 * reserved inode, and will become GDT blocks (primary and backup).
543 */
Mingming Cao617ba132006-10-11 01:20:53 -0700544 data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
Theodore Ts'ob4097142011-01-10 12:46:59 -0500545 err = ext4_handle_dirty_metadata(handle, NULL, dind);
546 if (unlikely(err)) {
547 ext4_std_error(sb, err);
548 goto exit_inode;
549 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700550 inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
Mingming Cao617ba132006-10-11 01:20:53 -0700551 ext4_mark_iloc_dirty(handle, inode, &iloc);
Yongqiang Yang2f919712011-07-27 21:16:33 -0400552 memset(gdb_bh->b_data, 0, sb->s_blocksize);
553 err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
Theodore Ts'ob4097142011-01-10 12:46:59 -0500554 if (unlikely(err)) {
555 ext4_std_error(sb, err);
556 goto exit_inode;
557 }
558 brelse(dind);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559
Mingming Cao617ba132006-10-11 01:20:53 -0700560 o_group_desc = EXT4_SB(sb)->s_group_desc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561 memcpy(n_group_desc, o_group_desc,
Mingming Cao617ba132006-10-11 01:20:53 -0700562 EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
Yongqiang Yang2f919712011-07-27 21:16:33 -0400563 n_group_desc[gdb_num] = gdb_bh;
Mingming Cao617ba132006-10-11 01:20:53 -0700564 EXT4_SB(sb)->s_group_desc = n_group_desc;
565 EXT4_SB(sb)->s_gdb_count++;
Theodore Ts'of18a5f22011-08-01 08:45:38 -0400566 ext4_kvfree(o_group_desc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700567
Marcin Slusarze8546d02008-04-17 10:38:59 -0400568 le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
Theodore Ts'ob4097142011-01-10 12:46:59 -0500569 err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
570 if (err)
571 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700572
Theodore Ts'ob4097142011-01-10 12:46:59 -0500573 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700574
575exit_inode:
Theodore Ts'of18a5f22011-08-01 08:45:38 -0400576 ext4_kvfree(n_group_desc);
Amir Goldstein537a0312011-03-20 22:57:02 -0400577 /* ext4_handle_release_buffer(handle, iloc.bh); */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700578 brelse(iloc.bh);
579exit_dindj:
Amir Goldstein537a0312011-03-20 22:57:02 -0400580 /* ext4_handle_release_buffer(handle, dind); */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700581exit_sbh:
Amir Goldstein537a0312011-03-20 22:57:02 -0400582 /* ext4_handle_release_buffer(handle, EXT4_SB(sb)->s_sbh); */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700583exit_dind:
584 brelse(dind);
585exit_bh:
Yongqiang Yang2f919712011-07-27 21:16:33 -0400586 brelse(gdb_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700587
Mingming Cao617ba132006-10-11 01:20:53 -0700588 ext4_debug("leaving with error %d\n", err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700589 return err;
590}
591
592/*
593 * Called when we are adding a new group which has a backup copy of each of
594 * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
595 * We need to add these reserved backup GDT blocks to the resize inode, so
596 * that they are kept for future resizing and not allocated to files.
597 *
598 * Each reserved backup GDT block will go into a different indirect block.
599 * The indirect blocks are actually the primary reserved GDT blocks,
600 * so we know in advance what their block numbers are. We only get the
601 * double-indirect block to verify it is pointing to the primary reserved
602 * GDT blocks so we don't overwrite a data block by accident. The reserved
603 * backup GDT blocks are stored in their reserved primary GDT block.
604 */
605static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
Yongqiang Yang668f4dc2011-07-27 21:23:13 -0400606 ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700607{
608 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -0700609 int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700610 struct buffer_head **primary;
611 struct buffer_head *dind;
Mingming Cao617ba132006-10-11 01:20:53 -0700612 struct ext4_iloc iloc;
613 ext4_fsblk_t blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700614 __le32 *data, *end;
615 int gdbackups = 0;
616 int res, i;
617 int err;
618
Josef Bacik216553c2008-04-29 22:02:02 -0400619 primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700620 if (!primary)
621 return -ENOMEM;
622
Mingming Cao617ba132006-10-11 01:20:53 -0700623 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700624 dind = sb_bread(sb, le32_to_cpu(*data));
625 if (!dind) {
626 err = -EIO;
627 goto exit_free;
628 }
629
Mingming Cao617ba132006-10-11 01:20:53 -0700630 blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count;
Josef Bacik94460092008-06-06 18:05:52 -0400631 data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count %
632 EXT4_ADDR_PER_BLOCK(sb));
Mingming Cao617ba132006-10-11 01:20:53 -0700633 end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700634
635 /* Get each reserved primary GDT block and verify it holds backups */
636 for (res = 0; res < reserved_gdb; res++, blk++) {
637 if (le32_to_cpu(*data) != blk) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500638 ext4_warning(sb, "reserved block %llu"
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700639 " not at offset %ld",
640 blk,
641 (long)(data - (__le32 *)dind->b_data));
642 err = -EINVAL;
643 goto exit_bh;
644 }
645 primary[res] = sb_bread(sb, blk);
646 if (!primary[res]) {
647 err = -EIO;
648 goto exit_bh;
649 }
650 if ((gdbackups = verify_reserved_gdb(sb, primary[res])) < 0) {
651 brelse(primary[res]);
652 err = gdbackups;
653 goto exit_bh;
654 }
655 if (++data >= end)
656 data = (__le32 *)dind->b_data;
657 }
658
659 for (i = 0; i < reserved_gdb; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -0700660 if ((err = ext4_journal_get_write_access(handle, primary[i]))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700661 /*
662 int j;
663 for (j = 0; j < i; j++)
Amir Goldstein537a0312011-03-20 22:57:02 -0400664 ext4_handle_release_buffer(handle, primary[j]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700665 */
666 goto exit_bh;
667 }
668 }
669
Mingming Cao617ba132006-10-11 01:20:53 -0700670 if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700671 goto exit_bh;
672
673 /*
674 * Finally we can add each of the reserved backup GDT blocks from
675 * the new group to its reserved primary GDT block.
676 */
Yongqiang Yang668f4dc2011-07-27 21:23:13 -0400677 blk = group * EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700678 for (i = 0; i < reserved_gdb; i++) {
679 int err2;
680 data = (__le32 *)primary[i]->b_data;
681 /* printk("reserving backup %lu[%u] = %lu\n",
682 primary[i]->b_blocknr, gdbackups,
683 blk + primary[i]->b_blocknr); */
684 data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
Frank Mayhar03901312009-01-07 00:06:22 -0500685 err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700686 if (!err)
687 err = err2;
688 }
689 inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
Mingming Cao617ba132006-10-11 01:20:53 -0700690 ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700691
692exit_bh:
693 while (--res >= 0)
694 brelse(primary[res]);
695 brelse(dind);
696
697exit_free:
698 kfree(primary);
699
700 return err;
701}
702
703/*
Mingming Cao617ba132006-10-11 01:20:53 -0700704 * Update the backup copies of the ext4 metadata. These don't need to be part
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700705 * of the main resize transaction, because e2fsck will re-write them if there
706 * is a problem (basically only OOM will cause a problem). However, we
707 * _should_ update the backups if possible, in case the primary gets trashed
708 * for some reason and we need to run e2fsck from a backup superblock. The
709 * important part is that the new block and inode counts are in the backup
710 * superblocks, and the location of the new group metadata in the GDT backups.
711 *
Theodore Ts'o32ed5052009-04-25 22:53:39 -0400712 * We do not need take the s_resize_lock for this, because these
713 * blocks are not otherwise touched by the filesystem code when it is
714 * mounted. We don't need to worry about last changing from
715 * sbi->s_groups_count, because the worst that can happen is that we
716 * do not copy the full number of backups at this time. The resize
717 * which changed s_groups_count will backup again.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700718 */
719static void update_backups(struct super_block *sb,
720 int blk_off, char *data, int size)
721{
Mingming Cao617ba132006-10-11 01:20:53 -0700722 struct ext4_sb_info *sbi = EXT4_SB(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500723 const ext4_group_t last = sbi->s_groups_count;
Mingming Cao617ba132006-10-11 01:20:53 -0700724 const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700725 unsigned three = 1;
726 unsigned five = 5;
727 unsigned seven = 7;
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500728 ext4_group_t group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700729 int rest = sb->s_blocksize - size;
730 handle_t *handle;
731 int err = 0, err2;
732
Mingming Cao617ba132006-10-11 01:20:53 -0700733 handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700734 if (IS_ERR(handle)) {
735 group = 1;
736 err = PTR_ERR(handle);
737 goto exit_err;
738 }
739
Mingming Cao617ba132006-10-11 01:20:53 -0700740 while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700741 struct buffer_head *bh;
742
743 /* Out of journal space, and can't get more - abort - so sad */
Frank Mayhar03901312009-01-07 00:06:22 -0500744 if (ext4_handle_valid(handle) &&
745 handle->h_buffer_credits == 0 &&
Mingming Cao617ba132006-10-11 01:20:53 -0700746 ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) &&
747 (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700748 break;
749
750 bh = sb_getblk(sb, group * bpg + blk_off);
751 if (!bh) {
752 err = -EIO;
753 break;
754 }
Mingming Cao617ba132006-10-11 01:20:53 -0700755 ext4_debug("update metadata backup %#04lx\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700756 (unsigned long)bh->b_blocknr);
Mingming Cao617ba132006-10-11 01:20:53 -0700757 if ((err = ext4_journal_get_write_access(handle, bh)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700758 break;
759 lock_buffer(bh);
760 memcpy(bh->b_data, data, size);
761 if (rest)
762 memset(bh->b_data + size, 0, rest);
763 set_buffer_uptodate(bh);
764 unlock_buffer(bh);
Theodore Ts'ob4097142011-01-10 12:46:59 -0500765 err = ext4_handle_dirty_metadata(handle, NULL, bh);
766 if (unlikely(err))
767 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700768 brelse(bh);
769 }
Mingming Cao617ba132006-10-11 01:20:53 -0700770 if ((err2 = ext4_journal_stop(handle)) && !err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700771 err = err2;
772
773 /*
774 * Ugh! Need to have e2fsck write the backup copies. It is too
775 * late to revert the resize, we shouldn't fail just because of
776 * the backup copies (they are only needed in case of corruption).
777 *
778 * However, if we got here we have a journal problem too, so we
779 * can't really start a transaction to mark the superblock.
780 * Chicken out and just set the flag on the hope it will be written
781 * to disk, and if not - we will simply wait until next fsck.
782 */
783exit_err:
784 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500785 ext4_warning(sb, "can't update backup for group %u (err %d), "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700786 "forcing fsck on next reboot", group, err);
Mingming Cao617ba132006-10-11 01:20:53 -0700787 sbi->s_mount_state &= ~EXT4_VALID_FS;
788 sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700789 mark_buffer_dirty(sbi->s_sbh);
790 }
791}
792
Yongqiang Yangbb08c1e2012-01-03 23:20:50 -0500793/*
794 * ext4_add_new_descs() adds @count group descriptor of groups
795 * starting at @group
796 *
797 * @handle: journal handle
798 * @sb: super block
799 * @group: the group no. of the first group desc to be added
800 * @resize_inode: the resize inode
801 * @count: number of group descriptors to be added
802 */
803static int ext4_add_new_descs(handle_t *handle, struct super_block *sb,
804 ext4_group_t group, struct inode *resize_inode,
805 ext4_group_t count)
806{
807 struct ext4_sb_info *sbi = EXT4_SB(sb);
808 struct ext4_super_block *es = sbi->s_es;
809 struct buffer_head *gdb_bh;
810 int i, gdb_off, gdb_num, err = 0;
811
812 for (i = 0; i < count; i++, group++) {
813 int reserved_gdb = ext4_bg_has_super(sb, group) ?
814 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
815
816 gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
817 gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
818
819 /*
820 * We will only either add reserved group blocks to a backup group
821 * or remove reserved blocks for the first group in a new group block.
822 * Doing both would be mean more complex code, and sane people don't
823 * use non-sparse filesystems anymore. This is already checked above.
824 */
825 if (gdb_off) {
826 gdb_bh = sbi->s_group_desc[gdb_num];
827 err = ext4_journal_get_write_access(handle, gdb_bh);
828
829 if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group))
830 err = reserve_backup_gdb(handle, resize_inode, group);
831 } else
832 err = add_new_gdb(handle, resize_inode, group);
833 if (err)
834 break;
835 }
836 return err;
837}
838
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700839/* Add group descriptor data to an existing or new group descriptor block.
840 * Ensure we handle all possible error conditions _before_ we start modifying
841 * the filesystem, because we cannot abort the transaction and not have it
842 * write the data to disk.
843 *
844 * If we are on a GDT block boundary, we need to get the reserved GDT block.
845 * Otherwise, we may need to add backup GDT blocks for a sparse group.
846 *
847 * We only need to hold the superblock lock while we are actually adding
848 * in the new group's counts to the superblock. Prior to that we have
849 * not really "added" the group at all. We re-check that we are still
850 * adding in the last group in case things have changed since verifying.
851 */
Mingming Cao617ba132006-10-11 01:20:53 -0700852int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700853{
Mingming Cao617ba132006-10-11 01:20:53 -0700854 struct ext4_sb_info *sbi = EXT4_SB(sb);
855 struct ext4_super_block *es = sbi->s_es;
856 int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700857 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
858 struct buffer_head *primary = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700859 struct ext4_group_desc *gdp;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700860 struct inode *inode = NULL;
861 handle_t *handle;
862 int gdb_off, gdb_num;
863 int err, err2;
864
Mingming Cao617ba132006-10-11 01:20:53 -0700865 gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
866 gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700867
Mingming Cao617ba132006-10-11 01:20:53 -0700868 if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb,
869 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500870 ext4_warning(sb, "Can't resize non-sparse filesystem further");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700871 return -EPERM;
872 }
873
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700874 if (ext4_blocks_count(es) + input->blocks_count <
875 ext4_blocks_count(es)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500876 ext4_warning(sb, "blocks_count overflow");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700877 return -EINVAL;
878 }
879
Mingming Cao617ba132006-10-11 01:20:53 -0700880 if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700881 le32_to_cpu(es->s_inodes_count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500882 ext4_warning(sb, "inodes_count overflow");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700883 return -EINVAL;
884 }
885
886 if (reserved_gdb || gdb_off == 0) {
Mingming Cao617ba132006-10-11 01:20:53 -0700887 if (!EXT4_HAS_COMPAT_FEATURE(sb,
Josef Bacik37609fd2008-08-19 22:13:41 -0400888 EXT4_FEATURE_COMPAT_RESIZE_INODE)
889 || !le16_to_cpu(es->s_reserved_gdt_blocks)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500890 ext4_warning(sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700891 "No reserved GDT blocks, can't resize");
892 return -EPERM;
893 }
David Howells1d1fe1e2008-02-07 00:15:37 -0800894 inode = ext4_iget(sb, EXT4_RESIZE_INO);
895 if (IS_ERR(inode)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500896 ext4_warning(sb, "Error opening resize inode");
David Howells1d1fe1e2008-02-07 00:15:37 -0800897 return PTR_ERR(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700898 }
899 }
900
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500901
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700902 if ((err = verify_group_input(sb, input)))
903 goto exit_put;
904
905 if ((err = setup_new_group_blocks(sb, input)))
906 goto exit_put;
907
908 /*
909 * We will always be modifying at least the superblock and a GDT
910 * block. If we are adding a group past the last current GDT block,
911 * we will also modify the inode and the dindirect block. If we
912 * are adding a group with superblock/GDT backups we will also
913 * modify each of the reserved GDT dindirect blocks.
914 */
Mingming Cao617ba132006-10-11 01:20:53 -0700915 handle = ext4_journal_start_sb(sb,
916 ext4_bg_has_super(sb, input->group) ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700917 3 + reserved_gdb : 4);
918 if (IS_ERR(handle)) {
919 err = PTR_ERR(handle);
920 goto exit_put;
921 }
922
Mingming Cao617ba132006-10-11 01:20:53 -0700923 if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700924 goto exit_journal;
925
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400926 /*
927 * We will only either add reserved group blocks to a backup group
928 * or remove reserved blocks for the first group in a new group block.
929 * Doing both would be mean more complex code, and sane people don't
930 * use non-sparse filesystems anymore. This is already checked above.
931 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700932 if (gdb_off) {
933 primary = sbi->s_group_desc[gdb_num];
Mingming Cao617ba132006-10-11 01:20:53 -0700934 if ((err = ext4_journal_get_write_access(handle, primary)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700935 goto exit_journal;
936
Yongqiang Yang668f4dc2011-07-27 21:23:13 -0400937 if (reserved_gdb && ext4_bg_num_gdb(sb, input->group)) {
938 err = reserve_backup_gdb(handle, inode, input->group);
939 if (err)
940 goto exit_journal;
941 }
Yongqiang Yang2f919712011-07-27 21:16:33 -0400942 } else {
943 /*
944 * Note that we can access new group descriptor block safely
945 * only if add_new_gdb() succeeds.
946 */
947 err = add_new_gdb(handle, inode, input->group);
948 if (err)
949 goto exit_journal;
950 primary = sbi->s_group_desc[gdb_num];
951 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700952
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400953 /*
954 * OK, now we've set up the new group. Time to make it active.
955 *
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400956 * so we have to be safe wrt. concurrent accesses the group
957 * data. So we need to be careful to set all of the relevant
958 * group descriptor data etc. *before* we enable the group.
959 *
960 * The key field here is sbi->s_groups_count: as long as
961 * that retains its old value, nobody is going to access the new
962 * group.
963 *
964 * So first we update all the descriptor metadata for the new
965 * group; then we update the total disk blocks count; then we
966 * update the groups count to enable the group; then finally we
967 * update the free space counts so that the system can start
968 * using the new disk blocks.
969 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700970
971 /* Update group descriptor block for new group */
Frederic Bohe28569222008-06-20 11:48:48 -0400972 gdp = (struct ext4_group_desc *)((char *)primary->b_data +
973 gdb_off * EXT4_DESC_SIZE(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700974
Theodore Ts'ofdff73f2009-01-26 19:06:41 -0500975 memset(gdp, 0, EXT4_DESC_SIZE(sb));
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700976 ext4_block_bitmap_set(sb, gdp, input->block_bitmap); /* LV FIXME */
977 ext4_inode_bitmap_set(sb, gdp, input->inode_bitmap); /* LV FIXME */
978 ext4_inode_table_set(sb, gdp, input->inode_table); /* LV FIXME */
Theodore Ts'o021b65b2011-09-09 19:08:51 -0400979 ext4_free_group_clusters_set(sb, gdp, input->free_blocks_count);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500980 ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
Theodore Ts'ofdff73f2009-01-26 19:06:41 -0500981 gdp->bg_flags = cpu_to_le16(EXT4_BG_INODE_ZEROED);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400982 gdp->bg_checksum = ext4_group_desc_csum(sbi, input->group, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700983
984 /*
Frederic Bohe5f21b0e2008-07-11 19:27:31 -0400985 * We can allocate memory for mb_alloc based on the new group
986 * descriptor
987 */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500988 err = ext4_mb_add_groupinfo(sb, input->group, gdp);
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -0400989 if (err)
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400990 goto exit_journal;
991
Frederic Bohe5f21b0e2008-07-11 19:27:31 -0400992 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993 * Make the new blocks and inodes valid next. We do this before
994 * increasing the group count so that once the group is enabled,
995 * all of its blocks and inodes are already valid.
996 *
997 * We always allocate group-by-group, then block-by-block or
998 * inode-by-inode within a group, so enabling these
999 * blocks/inodes before the group is live won't actually let us
1000 * allocate the new space yet.
1001 */
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001002 ext4_blocks_count_set(es, ext4_blocks_count(es) +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001003 input->blocks_count);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001004 le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001005
1006 /*
1007 * We need to protect s_groups_count against other CPUs seeing
1008 * inconsistent state in the superblock.
1009 *
1010 * The precise rules we use are:
1011 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001012 * * Writers must perform a smp_wmb() after updating all dependent
1013 * data and before modifying the groups count
1014 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001015 * * Readers must perform an smp_rmb() after reading the groups count
1016 * and before reading any dependent data.
1017 *
1018 * NB. These rules can be relaxed when checking the group count
1019 * while freeing data, as we can only allocate from a block
1020 * group after serialising against the group count, and we can
1021 * only then free after serialising in turn against that
1022 * allocation.
1023 */
1024 smp_wmb();
1025
1026 /* Update the global fs size fields */
1027 sbi->s_groups_count++;
1028
Theodore Ts'ob4097142011-01-10 12:46:59 -05001029 err = ext4_handle_dirty_metadata(handle, NULL, primary);
1030 if (unlikely(err)) {
1031 ext4_std_error(sb, err);
1032 goto exit_journal;
1033 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001034
1035 /* Update the reserved block counts only once the new group is
1036 * active. */
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001037 ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001038 input->reserved_blocks);
1039
1040 /* Update the free space counts */
Theodore Ts'o57042652011-09-09 18:56:51 -04001041 percpu_counter_add(&sbi->s_freeclusters_counter,
1042 EXT4_B2C(sbi, input->free_blocks_count));
Peter Zijlstraaa0dff22007-10-16 23:25:42 -07001043 percpu_counter_add(&sbi->s_freeinodes_counter,
Mingming Cao617ba132006-10-11 01:20:53 -07001044 EXT4_INODES_PER_GROUP(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001045
Eric Sandeen42007ef2010-05-16 01:00:00 -04001046 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
1047 sbi->s_log_groups_per_flex) {
Frederic Bohec62a11f2008-09-08 10:20:24 -04001048 ext4_group_t flex_group;
1049 flex_group = ext4_flex_group(sbi, input->group);
Theodore Ts'o24aaa8e2011-09-09 18:58:51 -04001050 atomic_add(EXT4_B2C(sbi, input->free_blocks_count),
1051 &sbi->s_flex_groups[flex_group].free_clusters);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05001052 atomic_add(EXT4_INODES_PER_GROUP(sb),
1053 &sbi->s_flex_groups[flex_group].free_inodes);
Frederic Bohec62a11f2008-09-08 10:20:24 -04001054 }
1055
Theodore Ts'oa0375152010-06-11 23:14:04 -04001056 ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001057
1058exit_journal:
Mingming Cao617ba132006-10-11 01:20:53 -07001059 if ((err2 = ext4_journal_stop(handle)) && !err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001060 err = err2;
Yongqiang Yang2f919712011-07-27 21:16:33 -04001061 if (!err && primary) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001062 update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
Mingming Cao617ba132006-10-11 01:20:53 -07001063 sizeof(struct ext4_super_block));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001064 update_backups(sb, primary->b_blocknr, primary->b_data,
1065 primary->b_size);
1066 }
1067exit_put:
1068 iput(inode);
1069 return err;
Mingming Cao617ba132006-10-11 01:20:53 -07001070} /* ext4_group_add */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001071
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001072/*
Yongqiang Yang18e31432012-01-03 23:18:50 -05001073 * extend a group without checking assuming that checking has been done.
1074 */
1075static int ext4_group_extend_no_check(struct super_block *sb,
1076 ext4_fsblk_t o_blocks_count, ext4_grpblk_t add)
1077{
1078 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
1079 handle_t *handle;
1080 int err = 0, err2;
1081
1082 /* We will update the superblock, one block bitmap, and
1083 * one group descriptor via ext4_group_add_blocks().
1084 */
1085 handle = ext4_journal_start_sb(sb, 3);
1086 if (IS_ERR(handle)) {
1087 err = PTR_ERR(handle);
1088 ext4_warning(sb, "error %d on journal start", err);
1089 return err;
1090 }
1091
1092 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
1093 if (err) {
1094 ext4_warning(sb, "error %d on journal write access", err);
1095 goto errout;
1096 }
1097
1098 ext4_blocks_count_set(es, o_blocks_count + add);
1099 ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
1100 o_blocks_count + add);
1101 /* We add the blocks to the bitmap and set the group need init bit */
1102 err = ext4_group_add_blocks(handle, sb, o_blocks_count, add);
1103 if (err)
1104 goto errout;
1105 ext4_handle_dirty_super(handle, sb);
1106 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
1107 o_blocks_count + add);
1108errout:
1109 err2 = ext4_journal_stop(handle);
1110 if (err2 && !err)
1111 err = err2;
1112
1113 if (!err) {
1114 if (test_opt(sb, DEBUG))
1115 printk(KERN_DEBUG "EXT4-fs: extended group to %llu "
1116 "blocks\n", ext4_blocks_count(es));
1117 update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es,
1118 sizeof(struct ext4_super_block));
1119 }
1120 return err;
1121}
1122
1123/*
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001124 * Extend the filesystem to the new number of blocks specified. This entry
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001125 * point is only used to extend the current filesystem to the end of the last
1126 * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
1127 * for emergencies (because it has no dependencies on reserved blocks).
1128 *
Mingming Cao617ba132006-10-11 01:20:53 -07001129 * If we _really_ wanted, we could use default values to call ext4_group_add()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001130 * allow the "remount" trick to work for arbitrary resizing, assuming enough
1131 * GDT blocks are reserved to grow to the desired size.
1132 */
Mingming Cao617ba132006-10-11 01:20:53 -07001133int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
1134 ext4_fsblk_t n_blocks_count)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001135{
Mingming Cao617ba132006-10-11 01:20:53 -07001136 ext4_fsblk_t o_blocks_count;
Mingming Cao617ba132006-10-11 01:20:53 -07001137 ext4_grpblk_t last;
1138 ext4_grpblk_t add;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001139 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001140 handle_t *handle;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04001141 int err, err2;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04001142 ext4_group_t group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001143
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001144 o_blocks_count = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001145
1146 if (test_opt(sb, DEBUG))
Yongqiang Yang2b79b092011-07-26 21:53:35 -04001147 printk(KERN_DEBUG "EXT4-fs: extending last group from %llu to %llu blocks\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001148 o_blocks_count, n_blocks_count);
1149
1150 if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
1151 return 0;
1152
1153 if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001154 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
Mingming Cao2ae02102006-10-11 01:21:11 -07001155 " too large to resize to %llu blocks safely\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001156 sb->s_id, n_blocks_count);
1157 if (sizeof(sector_t) < 8)
Eric Sandeen12062dd2010-02-15 14:19:27 -05001158 ext4_warning(sb, "CONFIG_LBDAF not enabled");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001159 return -EINVAL;
1160 }
1161
1162 if (n_blocks_count < o_blocks_count) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001163 ext4_warning(sb, "can't shrink FS - resize aborted");
Yongqiang Yang8f82f842011-07-26 21:35:44 -04001164 return -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001165 }
1166
1167 /* Handle the remaining blocks in the last group only. */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04001168 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001169
1170 if (last == 0) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001171 ext4_warning(sb, "need to use ext2online to resize further");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001172 return -EPERM;
1173 }
1174
Mingming Cao617ba132006-10-11 01:20:53 -07001175 add = EXT4_BLOCKS_PER_GROUP(sb) - last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001176
1177 if (o_blocks_count + add < o_blocks_count) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001178 ext4_warning(sb, "blocks_count overflow");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001179 return -EINVAL;
1180 }
1181
1182 if (o_blocks_count + add > n_blocks_count)
1183 add = n_blocks_count - o_blocks_count;
1184
1185 if (o_blocks_count + add < n_blocks_count)
Eric Sandeen12062dd2010-02-15 14:19:27 -05001186 ext4_warning(sb, "will only finish group (%llu blocks, %u new)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001187 o_blocks_count + add, add);
1188
1189 /* See if the device is actually as big as what was requested */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001190 bh = sb_bread(sb, o_blocks_count + add - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001191 if (!bh) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001192 ext4_warning(sb, "can't read last block, resize aborted");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001193 return -ENOSPC;
1194 }
1195 brelse(bh);
1196
1197 /* We will update the superblock, one block bitmap, and
Mingming Cao617ba132006-10-11 01:20:53 -07001198 * one group descriptor via ext4_free_blocks().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001199 */
Mingming Cao617ba132006-10-11 01:20:53 -07001200 handle = ext4_journal_start_sb(sb, 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001201 if (IS_ERR(handle)) {
1202 err = PTR_ERR(handle);
Eric Sandeen12062dd2010-02-15 14:19:27 -05001203 ext4_warning(sb, "error %d on journal start", err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001204 goto exit_put;
1205 }
1206
Mingming Cao617ba132006-10-11 01:20:53 -07001207 if ((err = ext4_journal_get_write_access(handle,
1208 EXT4_SB(sb)->s_sbh))) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001209 ext4_warning(sb, "error %d on journal write access", err);
Mingming Cao617ba132006-10-11 01:20:53 -07001210 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001211 goto exit_put;
1212 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001213 ext4_blocks_count_set(es, o_blocks_count + add);
Eric Sandeenc549a952008-01-28 23:58:27 -05001214 ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001215 o_blocks_count + add);
Aneesh Kumar K.Ve21675d2009-01-05 21:36:02 -05001216 /* We add the blocks to the bitmap and set the group need init bit */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04001217 err = ext4_group_add_blocks(handle, sb, o_blocks_count, add);
Theodore Ts'oa0375152010-06-11 23:14:04 -04001218 ext4_handle_dirty_super(handle, sb);
Mingming Cao2ae02102006-10-11 01:21:11 -07001219 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001220 o_blocks_count + add);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04001221 err2 = ext4_journal_stop(handle);
1222 if (!err && err2)
1223 err = err2;
1224
1225 if (err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001226 goto exit_put;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04001227
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001228 if (test_opt(sb, DEBUG))
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001229 printk(KERN_DEBUG "EXT4-fs: extended group to %llu blocks\n",
1230 ext4_blocks_count(es));
Mingming Cao617ba132006-10-11 01:20:53 -07001231 update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es,
1232 sizeof(struct ext4_super_block));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001233exit_put:
1234 return err;
Mingming Cao617ba132006-10-11 01:20:53 -07001235} /* ext4_group_extend */