ext4: add a structure which will be used by 64bit-resize interface
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / ext4 / resize.c
CommitLineData
ac27a0ec 1/*
617ba13b 2 * linux/fs/ext4/resize.c
ac27a0ec 3 *
617ba13b 4 * Support for resizing an ext4 filesystem while it is mounted.
ac27a0ec
DK
5 *
6 * Copyright (C) 2001, 2002 Andreas Dilger <adilger@clusterfs.com>
7 *
8 * This could probably be made into a module, because it is not often in use.
9 */
10
11
617ba13b 12#define EXT4FS_DEBUG
ac27a0ec 13
ac27a0ec
DK
14#include <linux/errno.h>
15#include <linux/slab.h>
16
3dcf5451 17#include "ext4_jbd2.h"
ac27a0ec 18
8f82f840
YY
19int ext4_resize_begin(struct super_block *sb)
20{
21 int ret = 0;
22
23 if (!capable(CAP_SYS_RESOURCE))
24 return -EPERM;
25
ce723c31
YY
26 /*
27 * We are not allowed to do online-resizing on a filesystem mounted
28 * with error, because it can destroy the filesystem easily.
29 */
30 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
31 ext4_warning(sb, "There are errors in the filesystem, "
32 "so online resizing is not allowed\n");
33 return -EPERM;
34 }
35
8f82f840
YY
36 if (test_and_set_bit_lock(EXT4_RESIZING, &EXT4_SB(sb)->s_resize_flags))
37 ret = -EBUSY;
38
39 return ret;
40}
41
42void ext4_resize_end(struct super_block *sb)
43{
44 clear_bit_unlock(EXT4_RESIZING, &EXT4_SB(sb)->s_resize_flags);
45 smp_mb__after_clear_bit();
46}
47
ac27a0ec
DK
48#define outside(b, first, last) ((b) < (first) || (b) >= (last))
49#define inside(b, first, last) ((b) >= (first) && (b) < (last))
50
51static int verify_group_input(struct super_block *sb,
617ba13b 52 struct ext4_new_group_data *input)
ac27a0ec 53{
617ba13b
MC
54 struct ext4_sb_info *sbi = EXT4_SB(sb);
55 struct ext4_super_block *es = sbi->s_es;
bd81d8ee 56 ext4_fsblk_t start = ext4_blocks_count(es);
617ba13b 57 ext4_fsblk_t end = start + input->blocks_count;
fd2d4291 58 ext4_group_t group = input->group;
617ba13b
MC
59 ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group;
60 unsigned overhead = ext4_bg_has_super(sb, group) ?
61 (1 + ext4_bg_num_gdb(sb, group) +
ac27a0ec 62 le16_to_cpu(es->s_reserved_gdt_blocks)) : 0;
617ba13b 63 ext4_fsblk_t metaend = start + overhead;
ac27a0ec 64 struct buffer_head *bh = NULL;
3a5b2ecd 65 ext4_grpblk_t free_blocks_count, offset;
ac27a0ec
DK
66 int err = -EINVAL;
67
68 input->free_blocks_count = free_blocks_count =
69 input->blocks_count - 2 - overhead - sbi->s_itb_per_group;
70
71 if (test_opt(sb, DEBUG))
617ba13b 72 printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks "
ac27a0ec 73 "(%d free, %u reserved)\n",
617ba13b 74 ext4_bg_has_super(sb, input->group) ? "normal" :
ac27a0ec
DK
75 "no-super", input->group, input->blocks_count,
76 free_blocks_count, input->reserved_blocks);
77
3a5b2ecd 78 ext4_get_group_no_and_offset(sb, start, NULL, &offset);
ac27a0ec 79 if (group != sbi->s_groups_count)
12062ddd 80 ext4_warning(sb, "Cannot add at group %u (only %u groups)",
ac27a0ec 81 input->group, sbi->s_groups_count);
3a5b2ecd 82 else if (offset != 0)
12062ddd 83 ext4_warning(sb, "Last group not full");
ac27a0ec 84 else if (input->reserved_blocks > input->blocks_count / 5)
12062ddd 85 ext4_warning(sb, "Reserved blocks too high (%u)",
ac27a0ec
DK
86 input->reserved_blocks);
87 else if (free_blocks_count < 0)
12062ddd 88 ext4_warning(sb, "Bad blocks count %u",
ac27a0ec
DK
89 input->blocks_count);
90 else if (!(bh = sb_bread(sb, end - 1)))
12062ddd 91 ext4_warning(sb, "Cannot read last block (%llu)",
ac27a0ec
DK
92 end - 1);
93 else if (outside(input->block_bitmap, start, end))
12062ddd 94 ext4_warning(sb, "Block bitmap not in group (block %llu)",
1939e49a 95 (unsigned long long)input->block_bitmap);
ac27a0ec 96 else if (outside(input->inode_bitmap, start, end))
12062ddd 97 ext4_warning(sb, "Inode bitmap not in group (block %llu)",
1939e49a 98 (unsigned long long)input->inode_bitmap);
ac27a0ec 99 else if (outside(input->inode_table, start, end) ||
2b2d6d01 100 outside(itend - 1, start, end))
12062ddd 101 ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)",
1939e49a 102 (unsigned long long)input->inode_table, itend - 1);
ac27a0ec 103 else if (input->inode_bitmap == input->block_bitmap)
12062ddd 104 ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)",
1939e49a 105 (unsigned long long)input->block_bitmap);
ac27a0ec 106 else if (inside(input->block_bitmap, input->inode_table, itend))
12062ddd
ES
107 ext4_warning(sb, "Block bitmap (%llu) in inode table "
108 "(%llu-%llu)",
1939e49a
RD
109 (unsigned long long)input->block_bitmap,
110 (unsigned long long)input->inode_table, itend - 1);
ac27a0ec 111 else if (inside(input->inode_bitmap, input->inode_table, itend))
12062ddd
ES
112 ext4_warning(sb, "Inode bitmap (%llu) in inode table "
113 "(%llu-%llu)",
1939e49a
RD
114 (unsigned long long)input->inode_bitmap,
115 (unsigned long long)input->inode_table, itend - 1);
ac27a0ec 116 else if (inside(input->block_bitmap, start, metaend))
12062ddd 117 ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)",
1939e49a
RD
118 (unsigned long long)input->block_bitmap,
119 start, metaend - 1);
ac27a0ec 120 else if (inside(input->inode_bitmap, start, metaend))
12062ddd 121 ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)",
1939e49a
RD
122 (unsigned long long)input->inode_bitmap,
123 start, metaend - 1);
ac27a0ec 124 else if (inside(input->inode_table, start, metaend) ||
2b2d6d01 125 inside(itend - 1, start, metaend))
12062ddd
ES
126 ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table "
127 "(%llu-%llu)",
1939e49a
RD
128 (unsigned long long)input->inode_table,
129 itend - 1, start, metaend - 1);
ac27a0ec
DK
130 else
131 err = 0;
132 brelse(bh);
133
134 return err;
135}
136
28c7bac0
YY
137/*
138 * ext4_new_flex_group_data is used by 64bit-resize interface to add a flex
139 * group each time.
140 */
141struct ext4_new_flex_group_data {
142 struct ext4_new_group_data *groups; /* new_group_data for groups
143 in the flex group */
144 __u16 *bg_flags; /* block group flags of groups
145 in @groups */
146 ext4_group_t count; /* number of groups in @groups
147 */
148};
149
150/*
151 * alloc_flex_gd() allocates a ext4_new_flex_group_data with size of
152 * @flexbg_size.
153 *
154 * Returns NULL on failure otherwise address of the allocated structure.
155 */
156static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size)
157{
158 struct ext4_new_flex_group_data *flex_gd;
159
160 flex_gd = kmalloc(sizeof(*flex_gd), GFP_NOFS);
161 if (flex_gd == NULL)
162 goto out3;
163
164 flex_gd->count = flexbg_size;
165
166 flex_gd->groups = kmalloc(sizeof(struct ext4_new_group_data) *
167 flexbg_size, GFP_NOFS);
168 if (flex_gd->groups == NULL)
169 goto out2;
170
171 flex_gd->bg_flags = kmalloc(flexbg_size * sizeof(__u16), GFP_NOFS);
172 if (flex_gd->bg_flags == NULL)
173 goto out1;
174
175 return flex_gd;
176
177out1:
178 kfree(flex_gd->groups);
179out2:
180 kfree(flex_gd);
181out3:
182 return NULL;
183}
184
185static void free_flex_gd(struct ext4_new_flex_group_data *flex_gd)
186{
187 kfree(flex_gd->bg_flags);
188 kfree(flex_gd->groups);
189 kfree(flex_gd);
190}
191
ac27a0ec 192static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
617ba13b 193 ext4_fsblk_t blk)
ac27a0ec
DK
194{
195 struct buffer_head *bh;
196 int err;
197
198 bh = sb_getblk(sb, blk);
199 if (!bh)
200 return ERR_PTR(-EIO);
617ba13b 201 if ((err = ext4_journal_get_write_access(handle, bh))) {
ac27a0ec
DK
202 brelse(bh);
203 bh = ERR_PTR(err);
204 } else {
ac27a0ec
DK
205 memset(bh->b_data, 0, sb->s_blocksize);
206 set_buffer_uptodate(bh);
ac27a0ec
DK
207 }
208
209 return bh;
210}
211
14904107
ES
212/*
213 * If we have fewer than thresh credits, extend by EXT4_MAX_TRANS_DATA.
214 * If that fails, restart the transaction & regain write access for the
215 * buffer head which is used for block_bitmap modifications.
216 */
6d40bc5a 217static int extend_or_restart_transaction(handle_t *handle, int thresh)
14904107
ES
218{
219 int err;
220
0390131b 221 if (ext4_handle_has_enough_credits(handle, thresh))
14904107
ES
222 return 0;
223
224 err = ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA);
225 if (err < 0)
226 return err;
227 if (err) {
6d40bc5a
YY
228 err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA);
229 if (err)
14904107 230 return err;
2b2d6d01 231 }
14904107
ES
232
233 return 0;
234}
235
ac27a0ec
DK
236/*
237 * 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,
617ba13b 244 struct ext4_new_group_data *input)
ac27a0ec 245{
617ba13b
MC
246 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) ?
ac27a0ec 249 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0;
617ba13b 250 unsigned long gdblocks = ext4_bg_num_gdb(sb, input->group);
ac27a0ec
DK
251 struct buffer_head *bh;
252 handle_t *handle;
617ba13b
MC
253 ext4_fsblk_t block;
254 ext4_grpblk_t bit;
ac27a0ec
DK
255 int i;
256 int err = 0, err2;
257
14904107
ES
258 /* This transaction may be extended/restarted along the way */
259 handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
260
ac27a0ec
DK
261 if (IS_ERR(handle))
262 return PTR_ERR(handle);
263
8f82f840 264 BUG_ON(input->group != sbi->s_groups_count);
ac27a0ec 265
ac27a0ec
DK
266 /* 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
c549a95d 271 ext4_debug("update backup group %#04llx (+%d)\n", block, bit);
6d40bc5a
YY
272 err = extend_or_restart_transaction(handle, 1);
273 if (err)
274 goto exit_journal;
14904107 275
ac27a0ec
DK
276 gdb = sb_getblk(sb, block);
277 if (!gdb) {
278 err = -EIO;
6d40bc5a 279 goto exit_journal;
ac27a0ec 280 }
617ba13b 281 if ((err = ext4_journal_get_write_access(handle, gdb))) {
ac27a0ec 282 brelse(gdb);
6d40bc5a 283 goto exit_journal;
ac27a0ec 284 }
5b615287 285 memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, gdb->b_size);
ac27a0ec 286 set_buffer_uptodate(gdb);
b4097142
TT
287 err = ext4_handle_dirty_metadata(handle, NULL, gdb);
288 if (unlikely(err)) {
289 brelse(gdb);
6d40bc5a 290 goto exit_journal;
b4097142 291 }
ac27a0ec
DK
292 brelse(gdb);
293 }
294
295 /* Zero out all of the reserved backup group descriptor table blocks */
da488945 296 ext4_debug("clear inode table blocks %#04llx -> %#04lx\n",
a31437b8
LC
297 block, sbi->s_itb_per_group);
298 err = sb_issue_zeroout(sb, gdblocks + start + 1, reserved_gdb,
a107e5a3 299 GFP_NOFS);
a31437b8 300 if (err)
6d40bc5a
YY
301 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 }
c3e94d1d
YY
312
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 }
14904107 317
c549a95d 318 ext4_debug("mark block bitmap %#04llx (+%llu)\n", input->block_bitmap,
ac27a0ec 319 input->block_bitmap - start);
617ba13b 320 ext4_set_bit(input->block_bitmap - start, bh->b_data);
c549a95d 321 ext4_debug("mark inode bitmap %#04llx (+%llu)\n", input->inode_bitmap,
ac27a0ec 322 input->inode_bitmap - start);
617ba13b 323 ext4_set_bit(input->inode_bitmap - start, bh->b_data);
ac27a0ec
DK
324
325 /* Zero out all of the inode table blocks */
a31437b8 326 block = input->inode_table;
da488945 327 ext4_debug("clear inode table blocks %#04llx -> %#04lx\n",
a31437b8 328 block, sbi->s_itb_per_group);
a107e5a3 329 err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group, GFP_NOFS);
a31437b8
LC
330 if (err)
331 goto exit_bh;
c3e94d1d
YY
332 ext4_set_bits(bh->b_data, input->inode_table - start,
333 sbi->s_itb_per_group);
14904107 334
14904107 335
61d08673
TT
336 ext4_mark_bitmap_end(input->blocks_count, sb->s_blocksize * 8,
337 bh->b_data);
b4097142
TT
338 err = ext4_handle_dirty_metadata(handle, NULL, bh);
339 if (unlikely(err)) {
340 ext4_std_error(sb, err);
341 goto exit_bh;
342 }
ac27a0ec 343 brelse(bh);
ac27a0ec 344 /* Mark unused entries in inode bitmap used */
c549a95d 345 ext4_debug("clear inode bitmap %#04llx (+%llu)\n",
ac27a0ec
DK
346 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
61d08673
TT
352 ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8,
353 bh->b_data);
b4097142
TT
354 err = ext4_handle_dirty_metadata(handle, NULL, bh);
355 if (unlikely(err))
356 ext4_std_error(sb, err);
ac27a0ec
DK
357exit_bh:
358 brelse(bh);
359
360exit_journal:
617ba13b 361 if ((err2 = ext4_journal_stop(handle)) && !err)
ac27a0ec
DK
362 err = err2;
363
364 return err;
365}
366
367/*
368 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
617ba13b 369 * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
ac27a0ec
DK
370 * 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 */
617ba13b 374static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
ac27a0ec
DK
375 unsigned *five, unsigned *seven)
376{
377 unsigned *min = three;
378 int mult = 3;
379 unsigned ret;
380
617ba13b
MC
381 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
382 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
ac27a0ec
DK
383 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{
617ba13b 411 const ext4_fsblk_t blk = primary->b_blocknr;
fd2d4291 412 const ext4_group_t end = EXT4_SB(sb)->s_groups_count;
ac27a0ec
DK
413 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
617ba13b 420 while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
bd81d8ee
LV
421 if (le32_to_cpu(*p++) !=
422 grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
12062ddd 423 ext4_warning(sb, "reserved GDT %llu"
2ae02107 424 " missing grp %d (%llu)",
ac27a0ec 425 blk, grp,
bd81d8ee
LV
426 grp *
427 (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
428 blk);
ac27a0ec
DK
429 return -EINVAL;
430 }
617ba13b 431 if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
ac27a0ec
DK
432 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,
2f919710 452 ext4_group_t group)
ac27a0ec
DK
453{
454 struct super_block *sb = inode->i_sb;
617ba13b 455 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
2f919710 456 unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
617ba13b 457 ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
ac27a0ec
DK
458 struct buffer_head **o_group_desc, **n_group_desc;
459 struct buffer_head *dind;
2f919710 460 struct buffer_head *gdb_bh;
ac27a0ec 461 int gdbackups;
617ba13b 462 struct ext4_iloc iloc;
ac27a0ec
DK
463 __le32 *data;
464 int err;
465
466 if (test_opt(sb, DEBUG))
467 printk(KERN_DEBUG
617ba13b 468 "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
ac27a0ec
DK
469 gdb_num);
470
af5bc92d
TT
471 /*
472 * If we are not using the primary superblock/GDT copy don't resize,
2b2d6d01
TT
473 * because the user tools have no way of handling this. Probably a
474 * bad time to do it anyways.
475 */
617ba13b
MC
476 if (EXT4_SB(sb)->s_sbh->b_blocknr !=
477 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
12062ddd 478 ext4_warning(sb, "won't resize using backup superblock at %llu",
617ba13b 479 (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
ac27a0ec
DK
480 return -EPERM;
481 }
482
2f919710
YY
483 gdb_bh = sb_bread(sb, gdblock);
484 if (!gdb_bh)
ac27a0ec
DK
485 return -EIO;
486
2f919710
YY
487 gdbackups = verify_reserved_gdb(sb, gdb_bh);
488 if (gdbackups < 0) {
ac27a0ec
DK
489 err = gdbackups;
490 goto exit_bh;
491 }
492
617ba13b 493 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
ac27a0ec
DK
494 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;
617ba13b 501 if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
12062ddd 502 ext4_warning(sb, "new group %u GDT block %llu not reserved",
2f919710 503 group, gdblock);
ac27a0ec
DK
504 err = -EINVAL;
505 goto exit_dind;
506 }
507
b4097142
TT
508 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
509 if (unlikely(err))
ac27a0ec
DK
510 goto exit_dind;
511
2f919710 512 err = ext4_journal_get_write_access(handle, gdb_bh);
b4097142 513 if (unlikely(err))
ac27a0ec
DK
514 goto exit_sbh;
515
b4097142
TT
516 err = ext4_journal_get_write_access(handle, dind);
517 if (unlikely(err))
518 ext4_std_error(sb, err);
ac27a0ec 519
617ba13b 520 /* ext4_reserve_inode_write() gets a reference on the iloc */
b4097142
TT
521 err = ext4_reserve_inode_write(handle, inode, &iloc);
522 if (unlikely(err))
ac27a0ec
DK
523 goto exit_dindj;
524
f18a5f21
TT
525 n_group_desc = ext4_kvmalloc((gdb_num + 1) *
526 sizeof(struct buffer_head *),
527 GFP_NOFS);
ac27a0ec
DK
528 if (!n_group_desc) {
529 err = -ENOMEM;
f18a5f21
TT
530 ext4_warning(sb, "not enough memory for %lu groups",
531 gdb_num + 1);
ac27a0ec
DK
532 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 */
617ba13b 544 data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
b4097142
TT
545 err = ext4_handle_dirty_metadata(handle, NULL, dind);
546 if (unlikely(err)) {
547 ext4_std_error(sb, err);
548 goto exit_inode;
549 }
ac27a0ec 550 inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
617ba13b 551 ext4_mark_iloc_dirty(handle, inode, &iloc);
2f919710
YY
552 memset(gdb_bh->b_data, 0, sb->s_blocksize);
553 err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
b4097142
TT
554 if (unlikely(err)) {
555 ext4_std_error(sb, err);
556 goto exit_inode;
557 }
558 brelse(dind);
ac27a0ec 559
617ba13b 560 o_group_desc = EXT4_SB(sb)->s_group_desc;
ac27a0ec 561 memcpy(n_group_desc, o_group_desc,
617ba13b 562 EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
2f919710 563 n_group_desc[gdb_num] = gdb_bh;
617ba13b
MC
564 EXT4_SB(sb)->s_group_desc = n_group_desc;
565 EXT4_SB(sb)->s_gdb_count++;
f18a5f21 566 ext4_kvfree(o_group_desc);
ac27a0ec 567
e8546d06 568 le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
b4097142
TT
569 err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
570 if (err)
571 ext4_std_error(sb, err);
ac27a0ec 572
b4097142 573 return err;
ac27a0ec
DK
574
575exit_inode:
f18a5f21 576 ext4_kvfree(n_group_desc);
537a0310 577 /* ext4_handle_release_buffer(handle, iloc.bh); */
ac27a0ec
DK
578 brelse(iloc.bh);
579exit_dindj:
537a0310 580 /* ext4_handle_release_buffer(handle, dind); */
ac27a0ec 581exit_sbh:
537a0310 582 /* ext4_handle_release_buffer(handle, EXT4_SB(sb)->s_sbh); */
ac27a0ec
DK
583exit_dind:
584 brelse(dind);
585exit_bh:
2f919710 586 brelse(gdb_bh);
ac27a0ec 587
617ba13b 588 ext4_debug("leaving with error %d\n", err);
ac27a0ec
DK
589 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,
668f4dc5 606 ext4_group_t group)
ac27a0ec
DK
607{
608 struct super_block *sb = inode->i_sb;
617ba13b 609 int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
ac27a0ec
DK
610 struct buffer_head **primary;
611 struct buffer_head *dind;
617ba13b
MC
612 struct ext4_iloc iloc;
613 ext4_fsblk_t blk;
ac27a0ec
DK
614 __le32 *data, *end;
615 int gdbackups = 0;
616 int res, i;
617 int err;
618
216553c4 619 primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_NOFS);
ac27a0ec
DK
620 if (!primary)
621 return -ENOMEM;
622
617ba13b 623 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
ac27a0ec
DK
624 dind = sb_bread(sb, le32_to_cpu(*data));
625 if (!dind) {
626 err = -EIO;
627 goto exit_free;
628 }
629
617ba13b 630 blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count;
94460093
JB
631 data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count %
632 EXT4_ADDR_PER_BLOCK(sb));
617ba13b 633 end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb);
ac27a0ec
DK
634
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) {
12062ddd 638 ext4_warning(sb, "reserved block %llu"
ac27a0ec
DK
639 " 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++) {
617ba13b 660 if ((err = ext4_journal_get_write_access(handle, primary[i]))) {
ac27a0ec
DK
661 /*
662 int j;
663 for (j = 0; j < i; j++)
537a0310 664 ext4_handle_release_buffer(handle, primary[j]);
ac27a0ec
DK
665 */
666 goto exit_bh;
667 }
668 }
669
617ba13b 670 if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
ac27a0ec
DK
671 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 */
668f4dc5 677 blk = group * EXT4_BLOCKS_PER_GROUP(sb);
ac27a0ec
DK
678 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);
0390131b 685 err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]);
ac27a0ec
DK
686 if (!err)
687 err = err2;
688 }
689 inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
617ba13b 690 ext4_mark_iloc_dirty(handle, inode, &iloc);
ac27a0ec
DK
691
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/*
617ba13b 704 * Update the backup copies of the ext4 metadata. These don't need to be part
ac27a0ec
DK
705 * 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 *
32ed5058
TT
712 * 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.
ac27a0ec
DK
718 */
719static void update_backups(struct super_block *sb,
720 int blk_off, char *data, int size)
721{
617ba13b 722 struct ext4_sb_info *sbi = EXT4_SB(sb);
fd2d4291 723 const ext4_group_t last = sbi->s_groups_count;
617ba13b 724 const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
ac27a0ec
DK
725 unsigned three = 1;
726 unsigned five = 5;
727 unsigned seven = 7;
fd2d4291 728 ext4_group_t group;
ac27a0ec
DK
729 int rest = sb->s_blocksize - size;
730 handle_t *handle;
731 int err = 0, err2;
732
617ba13b 733 handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
ac27a0ec
DK
734 if (IS_ERR(handle)) {
735 group = 1;
736 err = PTR_ERR(handle);
737 goto exit_err;
738 }
739
617ba13b 740 while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) {
ac27a0ec
DK
741 struct buffer_head *bh;
742
743 /* Out of journal space, and can't get more - abort - so sad */
0390131b
FM
744 if (ext4_handle_valid(handle) &&
745 handle->h_buffer_credits == 0 &&
617ba13b
MC
746 ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) &&
747 (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
ac27a0ec
DK
748 break;
749
750 bh = sb_getblk(sb, group * bpg + blk_off);
751 if (!bh) {
752 err = -EIO;
753 break;
754 }
617ba13b 755 ext4_debug("update metadata backup %#04lx\n",
ac27a0ec 756 (unsigned long)bh->b_blocknr);
617ba13b 757 if ((err = ext4_journal_get_write_access(handle, bh)))
ac27a0ec
DK
758 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);
b4097142
TT
765 err = ext4_handle_dirty_metadata(handle, NULL, bh);
766 if (unlikely(err))
767 ext4_std_error(sb, err);
ac27a0ec
DK
768 brelse(bh);
769 }
617ba13b 770 if ((err2 = ext4_journal_stop(handle)) && !err)
ac27a0ec
DK
771 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) {
12062ddd 785 ext4_warning(sb, "can't update backup for group %u (err %d), "
ac27a0ec 786 "forcing fsck on next reboot", group, err);
617ba13b
MC
787 sbi->s_mount_state &= ~EXT4_VALID_FS;
788 sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
ac27a0ec
DK
789 mark_buffer_dirty(sbi->s_sbh);
790 }
791}
792
bb08c1e7
YY
793/*
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
ac27a0ec
DK
839/* 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 */
617ba13b 852int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
ac27a0ec 853{
617ba13b
MC
854 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) ?
ac27a0ec
DK
857 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
858 struct buffer_head *primary = NULL;
617ba13b 859 struct ext4_group_desc *gdp;
ac27a0ec
DK
860 struct inode *inode = NULL;
861 handle_t *handle;
862 int gdb_off, gdb_num;
863 int err, err2;
864
617ba13b
MC
865 gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
866 gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
ac27a0ec 867
617ba13b
MC
868 if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb,
869 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
12062ddd 870 ext4_warning(sb, "Can't resize non-sparse filesystem further");
ac27a0ec
DK
871 return -EPERM;
872 }
873
bd81d8ee
LV
874 if (ext4_blocks_count(es) + input->blocks_count <
875 ext4_blocks_count(es)) {
12062ddd 876 ext4_warning(sb, "blocks_count overflow");
ac27a0ec
DK
877 return -EINVAL;
878 }
879
617ba13b 880 if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
ac27a0ec 881 le32_to_cpu(es->s_inodes_count)) {
12062ddd 882 ext4_warning(sb, "inodes_count overflow");
ac27a0ec
DK
883 return -EINVAL;
884 }
885
886 if (reserved_gdb || gdb_off == 0) {
617ba13b 887 if (!EXT4_HAS_COMPAT_FEATURE(sb,
37609fd5
JB
888 EXT4_FEATURE_COMPAT_RESIZE_INODE)
889 || !le16_to_cpu(es->s_reserved_gdt_blocks)) {
12062ddd 890 ext4_warning(sb,
ac27a0ec
DK
891 "No reserved GDT blocks, can't resize");
892 return -EPERM;
893 }
1d1fe1ee
DH
894 inode = ext4_iget(sb, EXT4_RESIZE_INO);
895 if (IS_ERR(inode)) {
12062ddd 896 ext4_warning(sb, "Error opening resize inode");
1d1fe1ee 897 return PTR_ERR(inode);
ac27a0ec
DK
898 }
899 }
900
920313a7 901
ac27a0ec
DK
902 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 */
617ba13b
MC
915 handle = ext4_journal_start_sb(sb,
916 ext4_bg_has_super(sb, input->group) ?
ac27a0ec
DK
917 3 + reserved_gdb : 4);
918 if (IS_ERR(handle)) {
919 err = PTR_ERR(handle);
920 goto exit_put;
921 }
922
617ba13b 923 if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh)))
ac27a0ec
DK
924 goto exit_journal;
925
2b2d6d01
TT
926 /*
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 */
ac27a0ec
DK
932 if (gdb_off) {
933 primary = sbi->s_group_desc[gdb_num];
617ba13b 934 if ((err = ext4_journal_get_write_access(handle, primary)))
ac27a0ec
DK
935 goto exit_journal;
936
668f4dc5
YY
937 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 }
2f919710
YY
942 } 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 }
ac27a0ec 952
2b2d6d01
TT
953 /*
954 * OK, now we've set up the new group. Time to make it active.
955 *
2b2d6d01
TT
956 * 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 */
ac27a0ec
DK
970
971 /* Update group descriptor block for new group */
2856922c
FB
972 gdp = (struct ext4_group_desc *)((char *)primary->b_data +
973 gdb_off * EXT4_DESC_SIZE(sb));
ac27a0ec 974
fdff73f0 975 memset(gdp, 0, EXT4_DESC_SIZE(sb));
8fadc143
AR
976 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 */
021b65bb 979 ext4_free_group_clusters_set(sb, gdp, input->free_blocks_count);
560671a0 980 ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
fdff73f0 981 gdp->bg_flags = cpu_to_le16(EXT4_BG_INODE_ZEROED);
717d50e4 982 gdp->bg_checksum = ext4_group_desc_csum(sbi, input->group, gdp);
ac27a0ec 983
5f21b0e6
FB
984 /*
985 * We can allocate memory for mb_alloc based on the new group
986 * descriptor
987 */
920313a7 988 err = ext4_mb_add_groupinfo(sb, input->group, gdp);
08c3a813 989 if (err)
c2ea3fde
TT
990 goto exit_journal;
991
ac27a0ec
DK
992 /*
993 * 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 */
bd81d8ee 1002 ext4_blocks_count_set(es, ext4_blocks_count(es) +
ac27a0ec 1003 input->blocks_count);
e8546d06 1004 le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb));
ac27a0ec
DK
1005
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 *
ac27a0ec
DK
1012 * * Writers must perform a smp_wmb() after updating all dependent
1013 * data and before modifying the groups count
1014 *
ac27a0ec
DK
1015 * * 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
b4097142
TT
1029 err = ext4_handle_dirty_metadata(handle, NULL, primary);
1030 if (unlikely(err)) {
1031 ext4_std_error(sb, err);
1032 goto exit_journal;
1033 }
ac27a0ec
DK
1034
1035 /* Update the reserved block counts only once the new group is
1036 * active. */
bd81d8ee 1037 ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
ac27a0ec
DK
1038 input->reserved_blocks);
1039
1040 /* Update the free space counts */
57042651
TT
1041 percpu_counter_add(&sbi->s_freeclusters_counter,
1042 EXT4_B2C(sbi, input->free_blocks_count));
aa0dff2d 1043 percpu_counter_add(&sbi->s_freeinodes_counter,
617ba13b 1044 EXT4_INODES_PER_GROUP(sb));
ac27a0ec 1045
42007efd
ES
1046 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
1047 sbi->s_log_groups_per_flex) {
c62a11fd
FB
1048 ext4_group_t flex_group;
1049 flex_group = ext4_flex_group(sbi, input->group);
24aaa8ef
TT
1050 atomic_add(EXT4_B2C(sbi, input->free_blocks_count),
1051 &sbi->s_flex_groups[flex_group].free_clusters);
9f24e420
TT
1052 atomic_add(EXT4_INODES_PER_GROUP(sb),
1053 &sbi->s_flex_groups[flex_group].free_inodes);
c62a11fd
FB
1054 }
1055
a0375156 1056 ext4_handle_dirty_super(handle, sb);
ac27a0ec
DK
1057
1058exit_journal:
617ba13b 1059 if ((err2 = ext4_journal_stop(handle)) && !err)
ac27a0ec 1060 err = err2;
2f919710 1061 if (!err && primary) {
ac27a0ec 1062 update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
617ba13b 1063 sizeof(struct ext4_super_block));
ac27a0ec
DK
1064 update_backups(sb, primary->b_blocknr, primary->b_data,
1065 primary->b_size);
1066 }
1067exit_put:
1068 iput(inode);
1069 return err;
617ba13b 1070} /* ext4_group_add */
ac27a0ec 1071
18e31438
YY
1072/*
1073 * 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
2b2d6d01
TT
1123/*
1124 * Extend the filesystem to the new number of blocks specified. This entry
ac27a0ec
DK
1125 * 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 *
617ba13b 1129 * If we _really_ wanted, we could use default values to call ext4_group_add()
ac27a0ec
DK
1130 * allow the "remount" trick to work for arbitrary resizing, assuming enough
1131 * GDT blocks are reserved to grow to the desired size.
1132 */
617ba13b
MC
1133int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
1134 ext4_fsblk_t n_blocks_count)
ac27a0ec 1135{
617ba13b 1136 ext4_fsblk_t o_blocks_count;
617ba13b
MC
1137 ext4_grpblk_t last;
1138 ext4_grpblk_t add;
af5bc92d 1139 struct buffer_head *bh;
ac27a0ec 1140 handle_t *handle;
cc7365df 1141 int err, err2;
5f21b0e6 1142 ext4_group_t group;
ac27a0ec 1143
bd81d8ee 1144 o_blocks_count = ext4_blocks_count(es);
ac27a0ec
DK
1145
1146 if (test_opt(sb, DEBUG))
2b79b09d 1147 printk(KERN_DEBUG "EXT4-fs: extending last group from %llu to %llu blocks\n",
ac27a0ec
DK
1148 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)) {
617ba13b 1154 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
2ae02107 1155 " too large to resize to %llu blocks safely\n",
ac27a0ec
DK
1156 sb->s_id, n_blocks_count);
1157 if (sizeof(sector_t) < 8)
12062ddd 1158 ext4_warning(sb, "CONFIG_LBDAF not enabled");
ac27a0ec
DK
1159 return -EINVAL;
1160 }
1161
1162 if (n_blocks_count < o_blocks_count) {
12062ddd 1163 ext4_warning(sb, "can't shrink FS - resize aborted");
8f82f840 1164 return -EINVAL;
ac27a0ec
DK
1165 }
1166
1167 /* Handle the remaining blocks in the last group only. */
5f21b0e6 1168 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
ac27a0ec
DK
1169
1170 if (last == 0) {
12062ddd 1171 ext4_warning(sb, "need to use ext2online to resize further");
ac27a0ec
DK
1172 return -EPERM;
1173 }
1174
617ba13b 1175 add = EXT4_BLOCKS_PER_GROUP(sb) - last;
ac27a0ec
DK
1176
1177 if (o_blocks_count + add < o_blocks_count) {
12062ddd 1178 ext4_warning(sb, "blocks_count overflow");
ac27a0ec
DK
1179 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)
12062ddd 1186 ext4_warning(sb, "will only finish group (%llu blocks, %u new)",
ac27a0ec
DK
1187 o_blocks_count + add, add);
1188
1189 /* See if the device is actually as big as what was requested */
2b2d6d01 1190 bh = sb_bread(sb, o_blocks_count + add - 1);
ac27a0ec 1191 if (!bh) {
12062ddd 1192 ext4_warning(sb, "can't read last block, resize aborted");
ac27a0ec
DK
1193 return -ENOSPC;
1194 }
1195 brelse(bh);
1196
1197 /* We will update the superblock, one block bitmap, and
617ba13b 1198 * one group descriptor via ext4_free_blocks().
ac27a0ec 1199 */
617ba13b 1200 handle = ext4_journal_start_sb(sb, 3);
ac27a0ec
DK
1201 if (IS_ERR(handle)) {
1202 err = PTR_ERR(handle);
12062ddd 1203 ext4_warning(sb, "error %d on journal start", err);
ac27a0ec
DK
1204 goto exit_put;
1205 }
1206
617ba13b
MC
1207 if ((err = ext4_journal_get_write_access(handle,
1208 EXT4_SB(sb)->s_sbh))) {
12062ddd 1209 ext4_warning(sb, "error %d on journal write access", err);
617ba13b 1210 ext4_journal_stop(handle);
ac27a0ec
DK
1211 goto exit_put;
1212 }
bd81d8ee 1213 ext4_blocks_count_set(es, o_blocks_count + add);
c549a95d 1214 ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
ac27a0ec 1215 o_blocks_count + add);
e21675d4 1216 /* We add the blocks to the bitmap and set the group need init bit */
cc7365df 1217 err = ext4_group_add_blocks(handle, sb, o_blocks_count, add);
a0375156 1218 ext4_handle_dirty_super(handle, sb);
2ae02107 1219 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
ac27a0ec 1220 o_blocks_count + add);
cc7365df
YY
1221 err2 = ext4_journal_stop(handle);
1222 if (!err && err2)
1223 err = err2;
1224
1225 if (err)
ac27a0ec 1226 goto exit_put;
5f21b0e6 1227
ac27a0ec 1228 if (test_opt(sb, DEBUG))
bd81d8ee
LV
1229 printk(KERN_DEBUG "EXT4-fs: extended group to %llu blocks\n",
1230 ext4_blocks_count(es));
617ba13b
MC
1231 update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es,
1232 sizeof(struct ext4_super_block));
ac27a0ec
DK
1233exit_put:
1234 return err;
617ba13b 1235} /* ext4_group_extend */