1 /*
2 * linux/fs/ext4/resize.c
3 *
4 * Support for resizing an ext4 filesystem while it is mounted.
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
12 #define EXT4FS_DEBUG
13
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16
17 #include "ext4_jbd2.h"
18
19 struct ext4_rcu_ptr {
20 struct rcu_head rcu;
21 void *ptr;
22 };
23
ext4_rcu_ptr_callback(struct rcu_head * head)24 static void ext4_rcu_ptr_callback(struct rcu_head *head)
25 {
26 struct ext4_rcu_ptr *ptr;
27
28 ptr = container_of(head, struct ext4_rcu_ptr, rcu);
29 kvfree(ptr->ptr);
30 kfree(ptr);
31 }
32
ext4_kvfree_array_rcu(void * to_free)33 void ext4_kvfree_array_rcu(void *to_free)
34 {
35 struct ext4_rcu_ptr *ptr = kzalloc(sizeof(*ptr), GFP_KERNEL);
36
37 if (ptr) {
38 ptr->ptr = to_free;
39 call_rcu(&ptr->rcu, ext4_rcu_ptr_callback);
40 return;
41 }
42 synchronize_rcu();
43 kvfree(to_free);
44 }
45
ext4_resize_begin(struct super_block * sb)46 int ext4_resize_begin(struct super_block *sb)
47 {
48 struct ext4_sb_info *sbi = EXT4_SB(sb);
49 int ret = 0;
50
51 if (!capable(CAP_SYS_RESOURCE))
52 return -EPERM;
53
54 /*
55 * If we are not using the primary superblock/GDT copy don't resize,
56 * because the user tools have no way of handling this. Probably a
57 * bad time to do it anyways.
58 */
59 if (EXT4_B2C(sbi, sbi->s_sbh->b_blocknr) !=
60 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
61 ext4_warning(sb, "won't resize using backup superblock at %llu",
62 (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
63 return -EPERM;
64 }
65
66 /*
67 * We are not allowed to do online-resizing on a filesystem mounted
68 * with error, because it can destroy the filesystem easily.
69 */
70 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
71 ext4_warning(sb, "There are errors in the filesystem, "
72 "so online resizing is not allowed\n");
73 return -EPERM;
74 }
75
76 if (test_and_set_bit_lock(EXT4_RESIZING, &EXT4_SB(sb)->s_resize_flags))
77 ret = -EBUSY;
78
79 return ret;
80 }
81
ext4_resize_end(struct super_block * sb)82 void ext4_resize_end(struct super_block *sb)
83 {
84 clear_bit_unlock(EXT4_RESIZING, &EXT4_SB(sb)->s_resize_flags);
85 smp_mb__after_atomic();
86 }
87
ext4_meta_bg_first_group(struct super_block * sb,ext4_group_t group)88 static ext4_group_t ext4_meta_bg_first_group(struct super_block *sb,
89 ext4_group_t group) {
90 return (group >> EXT4_DESC_PER_BLOCK_BITS(sb)) <<
91 EXT4_DESC_PER_BLOCK_BITS(sb);
92 }
93
ext4_meta_bg_first_block_no(struct super_block * sb,ext4_group_t group)94 static ext4_fsblk_t ext4_meta_bg_first_block_no(struct super_block *sb,
95 ext4_group_t group) {
96 group = ext4_meta_bg_first_group(sb, group);
97 return ext4_group_first_block_no(sb, group);
98 }
99
ext4_group_overhead_blocks(struct super_block * sb,ext4_group_t group)100 static ext4_grpblk_t ext4_group_overhead_blocks(struct super_block *sb,
101 ext4_group_t group) {
102 ext4_grpblk_t overhead;
103 overhead = ext4_bg_num_gdb(sb, group);
104 if (ext4_bg_has_super(sb, group))
105 overhead += 1 +
106 le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
107 return overhead;
108 }
109
110 #define outside(b, first, last) ((b) < (first) || (b) >= (last))
111 #define inside(b, first, last) ((b) >= (first) && (b) < (last))
112
verify_group_input(struct super_block * sb,struct ext4_new_group_data * input)113 static int verify_group_input(struct super_block *sb,
114 struct ext4_new_group_data *input)
115 {
116 struct ext4_sb_info *sbi = EXT4_SB(sb);
117 struct ext4_super_block *es = sbi->s_es;
118 ext4_fsblk_t start = ext4_blocks_count(es);
119 ext4_fsblk_t end = start + input->blocks_count;
120 ext4_group_t group = input->group;
121 ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group;
122 unsigned overhead;
123 ext4_fsblk_t metaend;
124 struct buffer_head *bh = NULL;
125 ext4_grpblk_t free_blocks_count, offset;
126 int err = -EINVAL;
127
128 if (group != sbi->s_groups_count) {
129 ext4_warning(sb, "Cannot add at group %u (only %u groups)",
130 input->group, sbi->s_groups_count);
131 return -EINVAL;
132 }
133
134 overhead = ext4_group_overhead_blocks(sb, group);
135 metaend = start + overhead;
136 input->free_blocks_count = free_blocks_count =
137 input->blocks_count - 2 - overhead - sbi->s_itb_per_group;
138
139 if (test_opt(sb, DEBUG))
140 printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks "
141 "(%d free, %u reserved)\n",
142 ext4_bg_has_super(sb, input->group) ? "normal" :
143 "no-super", input->group, input->blocks_count,
144 free_blocks_count, input->reserved_blocks);
145
146 ext4_get_group_no_and_offset(sb, start, NULL, &offset);
147 if (offset != 0)
148 ext4_warning(sb, "Last group not full");
149 else if (input->reserved_blocks > input->blocks_count / 5)
150 ext4_warning(sb, "Reserved blocks too high (%u)",
151 input->reserved_blocks);
152 else if (free_blocks_count < 0)
153 ext4_warning(sb, "Bad blocks count %u",
154 input->blocks_count);
155 else if (!(bh = sb_bread(sb, end - 1)))
156 ext4_warning(sb, "Cannot read last block (%llu)",
157 end - 1);
158 else if (outside(input->block_bitmap, start, end))
159 ext4_warning(sb, "Block bitmap not in group (block %llu)",
160 (unsigned long long)input->block_bitmap);
161 else if (outside(input->inode_bitmap, start, end))
162 ext4_warning(sb, "Inode bitmap not in group (block %llu)",
163 (unsigned long long)input->inode_bitmap);
164 else if (outside(input->inode_table, start, end) ||
165 outside(itend - 1, start, end))
166 ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)",
167 (unsigned long long)input->inode_table, itend - 1);
168 else if (input->inode_bitmap == input->block_bitmap)
169 ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)",
170 (unsigned long long)input->block_bitmap);
171 else if (inside(input->block_bitmap, input->inode_table, itend))
172 ext4_warning(sb, "Block bitmap (%llu) in inode table "
173 "(%llu-%llu)",
174 (unsigned long long)input->block_bitmap,
175 (unsigned long long)input->inode_table, itend - 1);
176 else if (inside(input->inode_bitmap, input->inode_table, itend))
177 ext4_warning(sb, "Inode bitmap (%llu) in inode table "
178 "(%llu-%llu)",
179 (unsigned long long)input->inode_bitmap,
180 (unsigned long long)input->inode_table, itend - 1);
181 else if (inside(input->block_bitmap, start, metaend))
182 ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)",
183 (unsigned long long)input->block_bitmap,
184 start, metaend - 1);
185 else if (inside(input->inode_bitmap, start, metaend))
186 ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)",
187 (unsigned long long)input->inode_bitmap,
188 start, metaend - 1);
189 else if (inside(input->inode_table, start, metaend) ||
190 inside(itend - 1, start, metaend))
191 ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table "
192 "(%llu-%llu)",
193 (unsigned long long)input->inode_table,
194 itend - 1, start, metaend - 1);
195 else
196 err = 0;
197 brelse(bh);
198
199 return err;
200 }
201
202 /*
203 * ext4_new_flex_group_data is used by 64bit-resize interface to add a flex
204 * group each time.
205 */
206 struct ext4_new_flex_group_data {
207 struct ext4_new_group_data *groups; /* new_group_data for groups
208 in the flex group */
209 __u16 *bg_flags; /* block group flags of groups
210 in @groups */
211 ext4_group_t count; /* number of groups in @groups
212 */
213 };
214
215 /*
216 * alloc_flex_gd() allocates a ext4_new_flex_group_data with size of
217 * @flexbg_size.
218 *
219 * Returns NULL on failure otherwise address of the allocated structure.
220 */
alloc_flex_gd(unsigned long flexbg_size)221 static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size)
222 {
223 struct ext4_new_flex_group_data *flex_gd;
224
225 flex_gd = kmalloc(sizeof(*flex_gd), GFP_NOFS);
226 if (flex_gd == NULL)
227 goto out3;
228
229 if (flexbg_size >= UINT_MAX / sizeof(struct ext4_new_group_data))
230 goto out2;
231 flex_gd->count = flexbg_size;
232
233 flex_gd->groups = kmalloc(sizeof(struct ext4_new_group_data) *
234 flexbg_size, GFP_NOFS);
235 if (flex_gd->groups == NULL)
236 goto out2;
237
238 flex_gd->bg_flags = kmalloc(flexbg_size * sizeof(__u16), GFP_NOFS);
239 if (flex_gd->bg_flags == NULL)
240 goto out1;
241
242 return flex_gd;
243
244 out1:
245 kfree(flex_gd->groups);
246 out2:
247 kfree(flex_gd);
248 out3:
249 return NULL;
250 }
251
free_flex_gd(struct ext4_new_flex_group_data * flex_gd)252 static void free_flex_gd(struct ext4_new_flex_group_data *flex_gd)
253 {
254 kfree(flex_gd->bg_flags);
255 kfree(flex_gd->groups);
256 kfree(flex_gd);
257 }
258
259 /*
260 * ext4_alloc_group_tables() allocates block bitmaps, inode bitmaps
261 * and inode tables for a flex group.
262 *
263 * This function is used by 64bit-resize. Note that this function allocates
264 * group tables from the 1st group of groups contained by @flexgd, which may
265 * be a partial of a flex group.
266 *
267 * @sb: super block of fs to which the groups belongs
268 *
269 * Returns 0 on a successful allocation of the metadata blocks in the
270 * block group.
271 */
ext4_alloc_group_tables(struct super_block * sb,struct ext4_new_flex_group_data * flex_gd,int flexbg_size)272 static int ext4_alloc_group_tables(struct super_block *sb,
273 struct ext4_new_flex_group_data *flex_gd,
274 int flexbg_size)
275 {
276 struct ext4_new_group_data *group_data = flex_gd->groups;
277 ext4_fsblk_t start_blk;
278 ext4_fsblk_t last_blk;
279 ext4_group_t src_group;
280 ext4_group_t bb_index = 0;
281 ext4_group_t ib_index = 0;
282 ext4_group_t it_index = 0;
283 ext4_group_t group;
284 ext4_group_t last_group;
285 unsigned overhead;
286 __u16 uninit_mask = (flexbg_size > 1) ? ~EXT4_BG_BLOCK_UNINIT : ~0;
287
288 BUG_ON(flex_gd->count == 0 || group_data == NULL);
289
290 src_group = group_data[0].group;
291 last_group = src_group + flex_gd->count - 1;
292
293 BUG_ON((flexbg_size > 1) && ((src_group & ~(flexbg_size - 1)) !=
294 (last_group & ~(flexbg_size - 1))));
295 next_group:
296 group = group_data[0].group;
297 if (src_group >= group_data[0].group + flex_gd->count)
298 return -ENOSPC;
299 start_blk = ext4_group_first_block_no(sb, src_group);
300 last_blk = start_blk + group_data[src_group - group].blocks_count;
301
302 overhead = ext4_group_overhead_blocks(sb, src_group);
303
304 start_blk += overhead;
305
306 /* We collect contiguous blocks as much as possible. */
307 src_group++;
308 for (; src_group <= last_group; src_group++) {
309 overhead = ext4_group_overhead_blocks(sb, src_group);
310 if (overhead == 0)
311 last_blk += group_data[src_group - group].blocks_count;
312 else
313 break;
314 }
315
316 /* Allocate block bitmaps */
317 for (; bb_index < flex_gd->count; bb_index++) {
318 if (start_blk >= last_blk)
319 goto next_group;
320 group_data[bb_index].block_bitmap = start_blk++;
321 group = ext4_get_group_number(sb, start_blk - 1);
322 group -= group_data[0].group;
323 group_data[group].free_blocks_count--;
324 flex_gd->bg_flags[group] &= uninit_mask;
325 }
326
327 /* Allocate inode bitmaps */
328 for (; ib_index < flex_gd->count; ib_index++) {
329 if (start_blk >= last_blk)
330 goto next_group;
331 group_data[ib_index].inode_bitmap = start_blk++;
332 group = ext4_get_group_number(sb, start_blk - 1);
333 group -= group_data[0].group;
334 group_data[group].free_blocks_count--;
335 flex_gd->bg_flags[group] &= uninit_mask;
336 }
337
338 /* Allocate inode tables */
339 for (; it_index < flex_gd->count; it_index++) {
340 unsigned int itb = EXT4_SB(sb)->s_itb_per_group;
341 ext4_fsblk_t next_group_start;
342
343 if (start_blk + itb > last_blk)
344 goto next_group;
345 group_data[it_index].inode_table = start_blk;
346 group = ext4_get_group_number(sb, start_blk);
347 next_group_start = ext4_group_first_block_no(sb, group + 1);
348 group -= group_data[0].group;
349
350 if (start_blk + itb > next_group_start) {
351 flex_gd->bg_flags[group + 1] &= uninit_mask;
352 overhead = start_blk + itb - next_group_start;
353 group_data[group + 1].free_blocks_count -= overhead;
354 itb -= overhead;
355 }
356
357 group_data[group].free_blocks_count -= itb;
358 flex_gd->bg_flags[group] &= uninit_mask;
359 start_blk += EXT4_SB(sb)->s_itb_per_group;
360 }
361
362 if (test_opt(sb, DEBUG)) {
363 int i;
364 group = group_data[0].group;
365
366 printk(KERN_DEBUG "EXT4-fs: adding a flex group with "
367 "%d groups, flexbg size is %d:\n", flex_gd->count,
368 flexbg_size);
369
370 for (i = 0; i < flex_gd->count; i++) {
371 printk(KERN_DEBUG "adding %s group %u: %u "
372 "blocks (%d free)\n",
373 ext4_bg_has_super(sb, group + i) ? "normal" :
374 "no-super", group + i,
375 group_data[i].blocks_count,
376 group_data[i].free_blocks_count);
377 }
378 }
379 return 0;
380 }
381
bclean(handle_t * handle,struct super_block * sb,ext4_fsblk_t blk)382 static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
383 ext4_fsblk_t blk)
384 {
385 struct buffer_head *bh;
386 int err;
387
388 bh = sb_getblk(sb, blk);
389 if (unlikely(!bh))
390 return ERR_PTR(-ENOMEM);
391 BUFFER_TRACE(bh, "get_write_access");
392 if ((err = ext4_journal_get_write_access(handle, bh))) {
393 brelse(bh);
394 bh = ERR_PTR(err);
395 } else {
396 memset(bh->b_data, 0, sb->s_blocksize);
397 set_buffer_uptodate(bh);
398 }
399
400 return bh;
401 }
402
403 /*
404 * If we have fewer than thresh credits, extend by EXT4_MAX_TRANS_DATA.
405 * If that fails, restart the transaction & regain write access for the
406 * buffer head which is used for block_bitmap modifications.
407 */
extend_or_restart_transaction(handle_t * handle,int thresh)408 static int extend_or_restart_transaction(handle_t *handle, int thresh)
409 {
410 int err;
411
412 if (ext4_handle_has_enough_credits(handle, thresh))
413 return 0;
414
415 err = ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA);
416 if (err < 0)
417 return err;
418 if (err) {
419 err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA);
420 if (err)
421 return err;
422 }
423
424 return 0;
425 }
426
427 /*
428 * set_flexbg_block_bitmap() mark @count blocks starting from @block used.
429 *
430 * Helper function for ext4_setup_new_group_blocks() which set .
431 *
432 * @sb: super block
433 * @handle: journal handle
434 * @flex_gd: flex group data
435 */
set_flexbg_block_bitmap(struct super_block * sb,handle_t * handle,struct ext4_new_flex_group_data * flex_gd,ext4_fsblk_t block,ext4_group_t count)436 static int set_flexbg_block_bitmap(struct super_block *sb, handle_t *handle,
437 struct ext4_new_flex_group_data *flex_gd,
438 ext4_fsblk_t block, ext4_group_t count)
439 {
440 ext4_group_t count2;
441
442 ext4_debug("mark blocks [%llu/%u] used\n", block, count);
443 for (count2 = count; count > 0; count -= count2, block += count2) {
444 ext4_fsblk_t start;
445 struct buffer_head *bh;
446 ext4_group_t group;
447 int err;
448
449 group = ext4_get_group_number(sb, block);
450 start = ext4_group_first_block_no(sb, group);
451 group -= flex_gd->groups[0].group;
452
453 count2 = EXT4_BLOCKS_PER_GROUP(sb) - (block - start);
454 if (count2 > count)
455 count2 = count;
456
457 if (flex_gd->bg_flags[group] & EXT4_BG_BLOCK_UNINIT) {
458 BUG_ON(flex_gd->count > 1);
459 continue;
460 }
461
462 err = extend_or_restart_transaction(handle, 1);
463 if (err)
464 return err;
465
466 bh = sb_getblk(sb, flex_gd->groups[group].block_bitmap);
467 if (unlikely(!bh))
468 return -ENOMEM;
469
470 BUFFER_TRACE(bh, "get_write_access");
471 err = ext4_journal_get_write_access(handle, bh);
472 if (err) {
473 brelse(bh);
474 return err;
475 }
476 ext4_debug("mark block bitmap %#04llx (+%llu/%u)\n", block,
477 block - start, count2);
478 ext4_set_bits(bh->b_data, block - start, count2);
479
480 err = ext4_handle_dirty_metadata(handle, NULL, bh);
481 brelse(bh);
482 if (unlikely(err))
483 return err;
484 }
485
486 return 0;
487 }
488
489 /*
490 * Set up the block and inode bitmaps, and the inode table for the new groups.
491 * This doesn't need to be part of the main transaction, since we are only
492 * changing blocks outside the actual filesystem. We still do journaling to
493 * ensure the recovery is correct in case of a failure just after resize.
494 * If any part of this fails, we simply abort the resize.
495 *
496 * setup_new_flex_group_blocks handles a flex group as follow:
497 * 1. copy super block and GDT, and initialize group tables if necessary.
498 * In this step, we only set bits in blocks bitmaps for blocks taken by
499 * super block and GDT.
500 * 2. allocate group tables in block bitmaps, that is, set bits in block
501 * bitmap for blocks taken by group tables.
502 */
setup_new_flex_group_blocks(struct super_block * sb,struct ext4_new_flex_group_data * flex_gd)503 static int setup_new_flex_group_blocks(struct super_block *sb,
504 struct ext4_new_flex_group_data *flex_gd)
505 {
506 int group_table_count[] = {1, 1, EXT4_SB(sb)->s_itb_per_group};
507 ext4_fsblk_t start;
508 ext4_fsblk_t block;
509 struct ext4_sb_info *sbi = EXT4_SB(sb);
510 struct ext4_super_block *es = sbi->s_es;
511 struct ext4_new_group_data *group_data = flex_gd->groups;
512 __u16 *bg_flags = flex_gd->bg_flags;
513 handle_t *handle;
514 ext4_group_t group, count;
515 struct buffer_head *bh = NULL;
516 int reserved_gdb, i, j, err = 0, err2;
517 int meta_bg;
518
519 BUG_ON(!flex_gd->count || !group_data ||
520 group_data[0].group != sbi->s_groups_count);
521
522 reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks);
523 meta_bg = ext4_has_feature_meta_bg(sb);
524
525 /* This transaction may be extended/restarted along the way */
526 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA);
527 if (IS_ERR(handle))
528 return PTR_ERR(handle);
529
530 group = group_data[0].group;
531 for (i = 0; i < flex_gd->count; i++, group++) {
532 unsigned long gdblocks;
533 ext4_grpblk_t overhead;
534
535 gdblocks = ext4_bg_num_gdb(sb, group);
536 start = ext4_group_first_block_no(sb, group);
537
538 if (meta_bg == 0 && !ext4_bg_has_super(sb, group))
539 goto handle_itb;
540
541 if (meta_bg == 1) {
542 ext4_group_t first_group;
543 first_group = ext4_meta_bg_first_group(sb, group);
544 if (first_group != group + 1 &&
545 first_group != group + EXT4_DESC_PER_BLOCK(sb) - 1)
546 goto handle_itb;
547 }
548
549 block = start + ext4_bg_has_super(sb, group);
550 /* Copy all of the GDT blocks into the backup in this group */
551 for (j = 0; j < gdblocks; j++, block++) {
552 struct buffer_head *gdb;
553
554 ext4_debug("update backup group %#04llx\n", block);
555 err = extend_or_restart_transaction(handle, 1);
556 if (err)
557 goto out;
558
559 gdb = sb_getblk(sb, block);
560 if (unlikely(!gdb)) {
561 err = -ENOMEM;
562 goto out;
563 }
564
565 BUFFER_TRACE(gdb, "get_write_access");
566 err = ext4_journal_get_write_access(handle, gdb);
567 if (err) {
568 brelse(gdb);
569 goto out;
570 }
571 memcpy(gdb->b_data, sbi_array_rcu_deref(sbi,
572 s_group_desc, j)->b_data, gdb->b_size);
573 set_buffer_uptodate(gdb);
574
575 err = ext4_handle_dirty_metadata(handle, NULL, gdb);
576 if (unlikely(err)) {
577 brelse(gdb);
578 goto out;
579 }
580 brelse(gdb);
581 }
582
583 /* Zero out all of the reserved backup group descriptor
584 * table blocks
585 */
586 if (ext4_bg_has_super(sb, group)) {
587 err = sb_issue_zeroout(sb, gdblocks + start + 1,
588 reserved_gdb, GFP_NOFS);
589 if (err)
590 goto out;
591 }
592
593 handle_itb:
594 /* Initialize group tables of the grop @group */
595 if (!(bg_flags[i] & EXT4_BG_INODE_ZEROED))
596 goto handle_bb;
597
598 /* Zero out all of the inode table blocks */
599 block = group_data[i].inode_table;
600 ext4_debug("clear inode table blocks %#04llx -> %#04lx\n",
601 block, sbi->s_itb_per_group);
602 err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group,
603 GFP_NOFS);
604 if (err)
605 goto out;
606
607 handle_bb:
608 if (bg_flags[i] & EXT4_BG_BLOCK_UNINIT)
609 goto handle_ib;
610
611 /* Initialize block bitmap of the @group */
612 block = group_data[i].block_bitmap;
613 err = extend_or_restart_transaction(handle, 1);
614 if (err)
615 goto out;
616
617 bh = bclean(handle, sb, block);
618 if (IS_ERR(bh)) {
619 err = PTR_ERR(bh);
620 goto out;
621 }
622 overhead = ext4_group_overhead_blocks(sb, group);
623 if (overhead != 0) {
624 ext4_debug("mark backup superblock %#04llx (+0)\n",
625 start);
626 ext4_set_bits(bh->b_data, 0, overhead);
627 }
628 ext4_mark_bitmap_end(group_data[i].blocks_count,
629 sb->s_blocksize * 8, bh->b_data);
630 err = ext4_handle_dirty_metadata(handle, NULL, bh);
631 brelse(bh);
632 if (err)
633 goto out;
634
635 handle_ib:
636 if (bg_flags[i] & EXT4_BG_INODE_UNINIT)
637 continue;
638
639 /* Initialize inode bitmap of the @group */
640 block = group_data[i].inode_bitmap;
641 err = extend_or_restart_transaction(handle, 1);
642 if (err)
643 goto out;
644 /* Mark unused entries in inode bitmap used */
645 bh = bclean(handle, sb, block);
646 if (IS_ERR(bh)) {
647 err = PTR_ERR(bh);
648 goto out;
649 }
650
651 ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb),
652 sb->s_blocksize * 8, bh->b_data);
653 err = ext4_handle_dirty_metadata(handle, NULL, bh);
654 brelse(bh);
655 if (err)
656 goto out;
657 }
658
659 /* Mark group tables in block bitmap */
660 for (j = 0; j < GROUP_TABLE_COUNT; j++) {
661 count = group_table_count[j];
662 start = (&group_data[0].block_bitmap)[j];
663 block = start;
664 for (i = 1; i < flex_gd->count; i++) {
665 block += group_table_count[j];
666 if (block == (&group_data[i].block_bitmap)[j]) {
667 count += group_table_count[j];
668 continue;
669 }
670 err = set_flexbg_block_bitmap(sb, handle,
671 flex_gd, start, count);
672 if (err)
673 goto out;
674 count = group_table_count[j];
675 start = (&group_data[i].block_bitmap)[j];
676 block = start;
677 }
678
679 if (count) {
680 err = set_flexbg_block_bitmap(sb, handle,
681 flex_gd, start, count);
682 if (err)
683 goto out;
684 }
685 }
686
687 out:
688 err2 = ext4_journal_stop(handle);
689 if (err2 && !err)
690 err = err2;
691
692 return err;
693 }
694
695 /*
696 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
697 * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
698 * calling this for the first time. In a sparse filesystem it will be the
699 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
700 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
701 */
ext4_list_backups(struct super_block * sb,unsigned * three,unsigned * five,unsigned * seven)702 static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
703 unsigned *five, unsigned *seven)
704 {
705 unsigned *min = three;
706 int mult = 3;
707 unsigned ret;
708
709 if (!ext4_has_feature_sparse_super(sb)) {
710 ret = *min;
711 *min += 1;
712 return ret;
713 }
714
715 if (*five < *min) {
716 min = five;
717 mult = 5;
718 }
719 if (*seven < *min) {
720 min = seven;
721 mult = 7;
722 }
723
724 ret = *min;
725 *min *= mult;
726
727 return ret;
728 }
729
730 /*
731 * Check that all of the backup GDT blocks are held in the primary GDT block.
732 * It is assumed that they are stored in group order. Returns the number of
733 * groups in current filesystem that have BACKUPS, or -ve error code.
734 */
verify_reserved_gdb(struct super_block * sb,ext4_group_t end,struct buffer_head * primary)735 static int verify_reserved_gdb(struct super_block *sb,
736 ext4_group_t end,
737 struct buffer_head *primary)
738 {
739 const ext4_fsblk_t blk = primary->b_blocknr;
740 unsigned three = 1;
741 unsigned five = 5;
742 unsigned seven = 7;
743 unsigned grp;
744 __le32 *p = (__le32 *)primary->b_data;
745 int gdbackups = 0;
746
747 while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
748 if (le32_to_cpu(*p++) !=
749 grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
750 ext4_warning(sb, "reserved GDT %llu"
751 " missing grp %d (%llu)",
752 blk, grp,
753 grp *
754 (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
755 blk);
756 return -EINVAL;
757 }
758 if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
759 return -EFBIG;
760 }
761
762 return gdbackups;
763 }
764
765 /*
766 * Called when we need to bring a reserved group descriptor table block into
767 * use from the resize inode. The primary copy of the new GDT block currently
768 * is an indirect block (under the double indirect block in the resize inode).
769 * The new backup GDT blocks will be stored as leaf blocks in this indirect
770 * block, in group order. Even though we know all the block numbers we need,
771 * we check to ensure that the resize inode has actually reserved these blocks.
772 *
773 * Don't need to update the block bitmaps because the blocks are still in use.
774 *
775 * We get all of the error cases out of the way, so that we are sure to not
776 * fail once we start modifying the data on disk, because JBD has no rollback.
777 */
add_new_gdb(handle_t * handle,struct inode * inode,ext4_group_t group)778 static int add_new_gdb(handle_t *handle, struct inode *inode,
779 ext4_group_t group)
780 {
781 struct super_block *sb = inode->i_sb;
782 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
783 unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
784 ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
785 struct buffer_head **o_group_desc, **n_group_desc;
786 struct buffer_head *dind;
787 struct buffer_head *gdb_bh;
788 int gdbackups;
789 struct ext4_iloc iloc;
790 __le32 *data;
791 int err;
792
793 if (test_opt(sb, DEBUG))
794 printk(KERN_DEBUG
795 "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
796 gdb_num);
797
798 gdb_bh = sb_bread(sb, gdblock);
799 if (!gdb_bh)
800 return -EIO;
801
802 gdbackups = verify_reserved_gdb(sb, group, gdb_bh);
803 if (gdbackups < 0) {
804 err = gdbackups;
805 goto exit_bh;
806 }
807
808 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
809 dind = sb_bread(sb, le32_to_cpu(*data));
810 if (!dind) {
811 err = -EIO;
812 goto exit_bh;
813 }
814
815 data = (__le32 *)dind->b_data;
816 if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
817 ext4_warning(sb, "new group %u GDT block %llu not reserved",
818 group, gdblock);
819 err = -EINVAL;
820 goto exit_dind;
821 }
822
823 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
824 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
825 if (unlikely(err))
826 goto exit_dind;
827
828 BUFFER_TRACE(gdb_bh, "get_write_access");
829 err = ext4_journal_get_write_access(handle, gdb_bh);
830 if (unlikely(err))
831 goto exit_dind;
832
833 BUFFER_TRACE(dind, "get_write_access");
834 err = ext4_journal_get_write_access(handle, dind);
835 if (unlikely(err))
836 ext4_std_error(sb, err);
837
838 /* ext4_reserve_inode_write() gets a reference on the iloc */
839 err = ext4_reserve_inode_write(handle, inode, &iloc);
840 if (unlikely(err))
841 goto exit_dind;
842
843 n_group_desc = ext4_kvmalloc((gdb_num + 1) *
844 sizeof(struct buffer_head *),
845 GFP_NOFS);
846 if (!n_group_desc) {
847 err = -ENOMEM;
848 ext4_warning(sb, "not enough memory for %lu groups",
849 gdb_num + 1);
850 goto exit_inode;
851 }
852
853 /*
854 * Finally, we have all of the possible failures behind us...
855 *
856 * Remove new GDT block from inode double-indirect block and clear out
857 * the new GDT block for use (which also "frees" the backup GDT blocks
858 * from the reserved inode). We don't need to change the bitmaps for
859 * these blocks, because they are marked as in-use from being in the
860 * reserved inode, and will become GDT blocks (primary and backup).
861 */
862 data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
863 err = ext4_handle_dirty_metadata(handle, NULL, dind);
864 if (unlikely(err)) {
865 ext4_std_error(sb, err);
866 goto exit_inode;
867 }
868 inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
869 ext4_mark_iloc_dirty(handle, inode, &iloc);
870 memset(gdb_bh->b_data, 0, sb->s_blocksize);
871 err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
872 if (unlikely(err)) {
873 ext4_std_error(sb, err);
874 iloc.bh = NULL;
875 goto exit_inode;
876 }
877 brelse(dind);
878
879 rcu_read_lock();
880 o_group_desc = rcu_dereference(EXT4_SB(sb)->s_group_desc);
881 memcpy(n_group_desc, o_group_desc,
882 EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
883 rcu_read_unlock();
884 n_group_desc[gdb_num] = gdb_bh;
885 rcu_assign_pointer(EXT4_SB(sb)->s_group_desc, n_group_desc);
886 EXT4_SB(sb)->s_gdb_count++;
887 ext4_kvfree_array_rcu(o_group_desc);
888
889 le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
890 err = ext4_handle_dirty_super(handle, sb);
891 if (err)
892 ext4_std_error(sb, err);
893
894 return err;
895
896 exit_inode:
897 kvfree(n_group_desc);
898 brelse(iloc.bh);
899 exit_dind:
900 brelse(dind);
901 exit_bh:
902 brelse(gdb_bh);
903
904 ext4_debug("leaving with error %d\n", err);
905 return err;
906 }
907
908 /*
909 * add_new_gdb_meta_bg is the sister of add_new_gdb.
910 */
add_new_gdb_meta_bg(struct super_block * sb,handle_t * handle,ext4_group_t group)911 static int add_new_gdb_meta_bg(struct super_block *sb,
912 handle_t *handle, ext4_group_t group) {
913 ext4_fsblk_t gdblock;
914 struct buffer_head *gdb_bh;
915 struct buffer_head **o_group_desc, **n_group_desc;
916 unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
917 int err;
918
919 gdblock = ext4_meta_bg_first_block_no(sb, group) +
920 ext4_bg_has_super(sb, group);
921 gdb_bh = sb_bread(sb, gdblock);
922 if (!gdb_bh)
923 return -EIO;
924 n_group_desc = ext4_kvmalloc((gdb_num + 1) *
925 sizeof(struct buffer_head *),
926 GFP_NOFS);
927 if (!n_group_desc) {
928 brelse(gdb_bh);
929 err = -ENOMEM;
930 ext4_warning(sb, "not enough memory for %lu groups",
931 gdb_num + 1);
932 return err;
933 }
934
935 rcu_read_lock();
936 o_group_desc = rcu_dereference(EXT4_SB(sb)->s_group_desc);
937 memcpy(n_group_desc, o_group_desc,
938 EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
939 rcu_read_unlock();
940 n_group_desc[gdb_num] = gdb_bh;
941
942 BUFFER_TRACE(gdb_bh, "get_write_access");
943 err = ext4_journal_get_write_access(handle, gdb_bh);
944 if (err) {
945 kvfree(n_group_desc);
946 brelse(gdb_bh);
947 return err;
948 }
949
950 rcu_assign_pointer(EXT4_SB(sb)->s_group_desc, n_group_desc);
951 EXT4_SB(sb)->s_gdb_count++;
952 ext4_kvfree_array_rcu(o_group_desc);
953 return err;
954 }
955
956 /*
957 * Called when we are adding a new group which has a backup copy of each of
958 * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
959 * We need to add these reserved backup GDT blocks to the resize inode, so
960 * that they are kept for future resizing and not allocated to files.
961 *
962 * Each reserved backup GDT block will go into a different indirect block.
963 * The indirect blocks are actually the primary reserved GDT blocks,
964 * so we know in advance what their block numbers are. We only get the
965 * double-indirect block to verify it is pointing to the primary reserved
966 * GDT blocks so we don't overwrite a data block by accident. The reserved
967 * backup GDT blocks are stored in their reserved primary GDT block.
968 */
reserve_backup_gdb(handle_t * handle,struct inode * inode,ext4_group_t group)969 static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
970 ext4_group_t group)
971 {
972 struct super_block *sb = inode->i_sb;
973 int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
974 struct buffer_head **primary;
975 struct buffer_head *dind;
976 struct ext4_iloc iloc;
977 ext4_fsblk_t blk;
978 __le32 *data, *end;
979 int gdbackups = 0;
980 int res, i;
981 int err;
982
983 primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_NOFS);
984 if (!primary)
985 return -ENOMEM;
986
987 data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
988 dind = sb_bread(sb, le32_to_cpu(*data));
989 if (!dind) {
990 err = -EIO;
991 goto exit_free;
992 }
993
994 blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count;
995 data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count %
996 EXT4_ADDR_PER_BLOCK(sb));
997 end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb);
998
999 /* Get each reserved primary GDT block and verify it holds backups */
1000 for (res = 0; res < reserved_gdb; res++, blk++) {
1001 if (le32_to_cpu(*data) != blk) {
1002 ext4_warning(sb, "reserved block %llu"
1003 " not at offset %ld",
1004 blk,
1005 (long)(data - (__le32 *)dind->b_data));
1006 err = -EINVAL;
1007 goto exit_bh;
1008 }
1009 primary[res] = sb_bread(sb, blk);
1010 if (!primary[res]) {
1011 err = -EIO;
1012 goto exit_bh;
1013 }
1014 gdbackups = verify_reserved_gdb(sb, group, primary[res]);
1015 if (gdbackups < 0) {
1016 brelse(primary[res]);
1017 err = gdbackups;
1018 goto exit_bh;
1019 }
1020 if (++data >= end)
1021 data = (__le32 *)dind->b_data;
1022 }
1023
1024 for (i = 0; i < reserved_gdb; i++) {
1025 BUFFER_TRACE(primary[i], "get_write_access");
1026 if ((err = ext4_journal_get_write_access(handle, primary[i])))
1027 goto exit_bh;
1028 }
1029
1030 if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
1031 goto exit_bh;
1032
1033 /*
1034 * Finally we can add each of the reserved backup GDT blocks from
1035 * the new group to its reserved primary GDT block.
1036 */
1037 blk = group * EXT4_BLOCKS_PER_GROUP(sb);
1038 for (i = 0; i < reserved_gdb; i++) {
1039 int err2;
1040 data = (__le32 *)primary[i]->b_data;
1041 /* printk("reserving backup %lu[%u] = %lu\n",
1042 primary[i]->b_blocknr, gdbackups,
1043 blk + primary[i]->b_blocknr); */
1044 data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
1045 err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]);
1046 if (!err)
1047 err = err2;
1048 }
1049 inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
1050 ext4_mark_iloc_dirty(handle, inode, &iloc);
1051
1052 exit_bh:
1053 while (--res >= 0)
1054 brelse(primary[res]);
1055 brelse(dind);
1056
1057 exit_free:
1058 kfree(primary);
1059
1060 return err;
1061 }
1062
1063 /*
1064 * Update the backup copies of the ext4 metadata. These don't need to be part
1065 * of the main resize transaction, because e2fsck will re-write them if there
1066 * is a problem (basically only OOM will cause a problem). However, we
1067 * _should_ update the backups if possible, in case the primary gets trashed
1068 * for some reason and we need to run e2fsck from a backup superblock. The
1069 * important part is that the new block and inode counts are in the backup
1070 * superblocks, and the location of the new group metadata in the GDT backups.
1071 *
1072 * We do not need take the s_resize_lock for this, because these
1073 * blocks are not otherwise touched by the filesystem code when it is
1074 * mounted. We don't need to worry about last changing from
1075 * sbi->s_groups_count, because the worst that can happen is that we
1076 * do not copy the full number of backups at this time. The resize
1077 * which changed s_groups_count will backup again.
1078 */
update_backups(struct super_block * sb,sector_t blk_off,char * data,int size,int meta_bg)1079 static void update_backups(struct super_block *sb, sector_t blk_off, char *data,
1080 int size, int meta_bg)
1081 {
1082 struct ext4_sb_info *sbi = EXT4_SB(sb);
1083 ext4_group_t last;
1084 const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
1085 unsigned three = 1;
1086 unsigned five = 5;
1087 unsigned seven = 7;
1088 ext4_group_t group = 0;
1089 int rest = sb->s_blocksize - size;
1090 handle_t *handle;
1091 int err = 0, err2;
1092
1093 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA);
1094 if (IS_ERR(handle)) {
1095 group = 1;
1096 err = PTR_ERR(handle);
1097 goto exit_err;
1098 }
1099
1100 if (meta_bg == 0) {
1101 group = ext4_list_backups(sb, &three, &five, &seven);
1102 last = sbi->s_groups_count;
1103 } else {
1104 group = ext4_get_group_number(sb, blk_off) + 1;
1105 last = (ext4_group_t)(group + EXT4_DESC_PER_BLOCK(sb) - 2);
1106 }
1107
1108 while (group < sbi->s_groups_count) {
1109 struct buffer_head *bh;
1110 ext4_fsblk_t backup_block;
1111
1112 /* Out of journal space, and can't get more - abort - so sad */
1113 if (ext4_handle_valid(handle) &&
1114 handle->h_buffer_credits == 0 &&
1115 ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) &&
1116 (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
1117 break;
1118
1119 if (meta_bg == 0)
1120 backup_block = ((ext4_fsblk_t)group) * bpg + blk_off;
1121 else
1122 backup_block = (ext4_group_first_block_no(sb, group) +
1123 ext4_bg_has_super(sb, group));
1124
1125 bh = sb_getblk(sb, backup_block);
1126 if (unlikely(!bh)) {
1127 err = -ENOMEM;
1128 break;
1129 }
1130 ext4_debug("update metadata backup %llu(+%llu)\n",
1131 backup_block, backup_block -
1132 ext4_group_first_block_no(sb, group));
1133 BUFFER_TRACE(bh, "get_write_access");
1134 if ((err = ext4_journal_get_write_access(handle, bh))) {
1135 brelse(bh);
1136 break;
1137 }
1138 lock_buffer(bh);
1139 memcpy(bh->b_data, data, size);
1140 if (rest)
1141 memset(bh->b_data + size, 0, rest);
1142 set_buffer_uptodate(bh);
1143 unlock_buffer(bh);
1144 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1145 if (unlikely(err))
1146 ext4_std_error(sb, err);
1147 brelse(bh);
1148
1149 if (meta_bg == 0)
1150 group = ext4_list_backups(sb, &three, &five, &seven);
1151 else if (group == last)
1152 break;
1153 else
1154 group = last;
1155 }
1156 if ((err2 = ext4_journal_stop(handle)) && !err)
1157 err = err2;
1158
1159 /*
1160 * Ugh! Need to have e2fsck write the backup copies. It is too
1161 * late to revert the resize, we shouldn't fail just because of
1162 * the backup copies (they are only needed in case of corruption).
1163 *
1164 * However, if we got here we have a journal problem too, so we
1165 * can't really start a transaction to mark the superblock.
1166 * Chicken out and just set the flag on the hope it will be written
1167 * to disk, and if not - we will simply wait until next fsck.
1168 */
1169 exit_err:
1170 if (err) {
1171 ext4_warning(sb, "can't update backup for group %u (err %d), "
1172 "forcing fsck on next reboot", group, err);
1173 sbi->s_mount_state &= ~EXT4_VALID_FS;
1174 sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1175 mark_buffer_dirty(sbi->s_sbh);
1176 }
1177 }
1178
1179 /*
1180 * ext4_add_new_descs() adds @count group descriptor of groups
1181 * starting at @group
1182 *
1183 * @handle: journal handle
1184 * @sb: super block
1185 * @group: the group no. of the first group desc to be added
1186 * @resize_inode: the resize inode
1187 * @count: number of group descriptors to be added
1188 */
ext4_add_new_descs(handle_t * handle,struct super_block * sb,ext4_group_t group,struct inode * resize_inode,ext4_group_t count)1189 static int ext4_add_new_descs(handle_t *handle, struct super_block *sb,
1190 ext4_group_t group, struct inode *resize_inode,
1191 ext4_group_t count)
1192 {
1193 struct ext4_sb_info *sbi = EXT4_SB(sb);
1194 struct ext4_super_block *es = sbi->s_es;
1195 struct buffer_head *gdb_bh;
1196 int i, gdb_off, gdb_num, err = 0;
1197 int meta_bg;
1198
1199 meta_bg = ext4_has_feature_meta_bg(sb);
1200 for (i = 0; i < count; i++, group++) {
1201 int reserved_gdb = ext4_bg_has_super(sb, group) ?
1202 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
1203
1204 gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
1205 gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
1206
1207 /*
1208 * We will only either add reserved group blocks to a backup group
1209 * or remove reserved blocks for the first group in a new group block.
1210 * Doing both would be mean more complex code, and sane people don't
1211 * use non-sparse filesystems anymore. This is already checked above.
1212 */
1213 if (gdb_off) {
1214 gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc,
1215 gdb_num);
1216 BUFFER_TRACE(gdb_bh, "get_write_access");
1217 err = ext4_journal_get_write_access(handle, gdb_bh);
1218
1219 if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group))
1220 err = reserve_backup_gdb(handle, resize_inode, group);
1221 } else if (meta_bg != 0) {
1222 err = add_new_gdb_meta_bg(sb, handle, group);
1223 } else {
1224 err = add_new_gdb(handle, resize_inode, group);
1225 }
1226 if (err)
1227 break;
1228 }
1229 return err;
1230 }
1231
ext4_get_bitmap(struct super_block * sb,__u64 block)1232 static struct buffer_head *ext4_get_bitmap(struct super_block *sb, __u64 block)
1233 {
1234 struct buffer_head *bh = sb_getblk(sb, block);
1235 if (unlikely(!bh))
1236 return NULL;
1237 if (!bh_uptodate_or_lock(bh)) {
1238 if (bh_submit_read(bh) < 0) {
1239 brelse(bh);
1240 return NULL;
1241 }
1242 }
1243
1244 return bh;
1245 }
1246
ext4_set_bitmap_checksums(struct super_block * sb,ext4_group_t group,struct ext4_group_desc * gdp,struct ext4_new_group_data * group_data)1247 static int ext4_set_bitmap_checksums(struct super_block *sb,
1248 ext4_group_t group,
1249 struct ext4_group_desc *gdp,
1250 struct ext4_new_group_data *group_data)
1251 {
1252 struct buffer_head *bh;
1253
1254 if (!ext4_has_metadata_csum(sb))
1255 return 0;
1256
1257 bh = ext4_get_bitmap(sb, group_data->inode_bitmap);
1258 if (!bh)
1259 return -EIO;
1260 ext4_inode_bitmap_csum_set(sb, group, gdp, bh,
1261 EXT4_INODES_PER_GROUP(sb) / 8);
1262 brelse(bh);
1263
1264 bh = ext4_get_bitmap(sb, group_data->block_bitmap);
1265 if (!bh)
1266 return -EIO;
1267 ext4_block_bitmap_csum_set(sb, group, gdp, bh);
1268 brelse(bh);
1269
1270 return 0;
1271 }
1272
1273 /*
1274 * ext4_setup_new_descs() will set up the group descriptor descriptors of a flex bg
1275 */
ext4_setup_new_descs(handle_t * handle,struct super_block * sb,struct ext4_new_flex_group_data * flex_gd)1276 static int ext4_setup_new_descs(handle_t *handle, struct super_block *sb,
1277 struct ext4_new_flex_group_data *flex_gd)
1278 {
1279 struct ext4_new_group_data *group_data = flex_gd->groups;
1280 struct ext4_group_desc *gdp;
1281 struct ext4_sb_info *sbi = EXT4_SB(sb);
1282 struct buffer_head *gdb_bh;
1283 ext4_group_t group;
1284 __u16 *bg_flags = flex_gd->bg_flags;
1285 int i, gdb_off, gdb_num, err = 0;
1286
1287
1288 for (i = 0; i < flex_gd->count; i++, group_data++, bg_flags++) {
1289 group = group_data->group;
1290
1291 gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
1292 gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
1293
1294 /*
1295 * get_write_access() has been called on gdb_bh by ext4_add_new_desc().
1296 */
1297 gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc, gdb_num);
1298 /* Update group descriptor block for new group */
1299 gdp = (struct ext4_group_desc *)(gdb_bh->b_data +
1300 gdb_off * EXT4_DESC_SIZE(sb));
1301
1302 memset(gdp, 0, EXT4_DESC_SIZE(sb));
1303 ext4_block_bitmap_set(sb, gdp, group_data->block_bitmap);
1304 ext4_inode_bitmap_set(sb, gdp, group_data->inode_bitmap);
1305 err = ext4_set_bitmap_checksums(sb, group, gdp, group_data);
1306 if (err) {
1307 ext4_std_error(sb, err);
1308 break;
1309 }
1310
1311 ext4_inode_table_set(sb, gdp, group_data->inode_table);
1312 ext4_free_group_clusters_set(sb, gdp,
1313 EXT4_NUM_B2C(sbi, group_data->free_blocks_count));
1314 ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
1315 if (ext4_has_group_desc_csum(sb))
1316 ext4_itable_unused_set(sb, gdp,
1317 EXT4_INODES_PER_GROUP(sb));
1318 gdp->bg_flags = cpu_to_le16(*bg_flags);
1319 ext4_group_desc_csum_set(sb, group, gdp);
1320
1321 err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
1322 if (unlikely(err)) {
1323 ext4_std_error(sb, err);
1324 break;
1325 }
1326
1327 /*
1328 * We can allocate memory for mb_alloc based on the new group
1329 * descriptor
1330 */
1331 err = ext4_mb_add_groupinfo(sb, group, gdp);
1332 if (err)
1333 break;
1334 }
1335 return err;
1336 }
1337
1338 /*
1339 * ext4_update_super() updates the super block so that the newly added
1340 * groups can be seen by the filesystem.
1341 *
1342 * @sb: super block
1343 * @flex_gd: new added groups
1344 */
ext4_update_super(struct super_block * sb,struct ext4_new_flex_group_data * flex_gd)1345 static void ext4_update_super(struct super_block *sb,
1346 struct ext4_new_flex_group_data *flex_gd)
1347 {
1348 ext4_fsblk_t blocks_count = 0;
1349 ext4_fsblk_t free_blocks = 0;
1350 ext4_fsblk_t reserved_blocks = 0;
1351 struct ext4_new_group_data *group_data = flex_gd->groups;
1352 struct ext4_sb_info *sbi = EXT4_SB(sb);
1353 struct ext4_super_block *es = sbi->s_es;
1354 int i;
1355
1356 BUG_ON(flex_gd->count == 0 || group_data == NULL);
1357 /*
1358 * Make the new blocks and inodes valid next. We do this before
1359 * increasing the group count so that once the group is enabled,
1360 * all of its blocks and inodes are already valid.
1361 *
1362 * We always allocate group-by-group, then block-by-block or
1363 * inode-by-inode within a group, so enabling these
1364 * blocks/inodes before the group is live won't actually let us
1365 * allocate the new space yet.
1366 */
1367 for (i = 0; i < flex_gd->count; i++) {
1368 blocks_count += group_data[i].blocks_count;
1369 free_blocks += group_data[i].free_blocks_count;
1370 }
1371
1372 reserved_blocks = ext4_r_blocks_count(es) * 100;
1373 reserved_blocks = div64_u64(reserved_blocks, ext4_blocks_count(es));
1374 reserved_blocks *= blocks_count;
1375 do_div(reserved_blocks, 100);
1376
1377 ext4_blocks_count_set(es, ext4_blocks_count(es) + blocks_count);
1378 ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + free_blocks);
1379 le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb) *
1380 flex_gd->count);
1381 le32_add_cpu(&es->s_free_inodes_count, EXT4_INODES_PER_GROUP(sb) *
1382 flex_gd->count);
1383
1384 ext4_debug("free blocks count %llu", ext4_free_blocks_count(es));
1385 /*
1386 * We need to protect s_groups_count against other CPUs seeing
1387 * inconsistent state in the superblock.
1388 *
1389 * The precise rules we use are:
1390 *
1391 * * Writers must perform a smp_wmb() after updating all
1392 * dependent data and before modifying the groups count
1393 *
1394 * * Readers must perform an smp_rmb() after reading the groups
1395 * count and before reading any dependent data.
1396 *
1397 * NB. These rules can be relaxed when checking the group count
1398 * while freeing data, as we can only allocate from a block
1399 * group after serialising against the group count, and we can
1400 * only then free after serialising in turn against that
1401 * allocation.
1402 */
1403 smp_wmb();
1404
1405 /* Update the global fs size fields */
1406 sbi->s_groups_count += flex_gd->count;
1407 sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
1408 (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
1409
1410 /* Update the reserved block counts only once the new group is
1411 * active. */
1412 ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
1413 reserved_blocks);
1414
1415 /* Update the free space counts */
1416 percpu_counter_add(&sbi->s_freeclusters_counter,
1417 EXT4_NUM_B2C(sbi, free_blocks));
1418 percpu_counter_add(&sbi->s_freeinodes_counter,
1419 EXT4_INODES_PER_GROUP(sb) * flex_gd->count);
1420
1421 ext4_debug("free blocks count %llu",
1422 percpu_counter_read(&sbi->s_freeclusters_counter));
1423 if (ext4_has_feature_flex_bg(sb) && sbi->s_log_groups_per_flex) {
1424 ext4_group_t flex_group;
1425 struct flex_groups *fg;
1426
1427 flex_group = ext4_flex_group(sbi, group_data[0].group);
1428 fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
1429 atomic64_add(EXT4_NUM_B2C(sbi, free_blocks),
1430 &fg->free_clusters);
1431 atomic_add(EXT4_INODES_PER_GROUP(sb) * flex_gd->count,
1432 &fg->free_inodes);
1433 }
1434
1435 /*
1436 * Update the fs overhead information
1437 */
1438 ext4_calculate_overhead(sb);
1439
1440 if (test_opt(sb, DEBUG))
1441 printk(KERN_DEBUG "EXT4-fs: added group %u:"
1442 "%llu blocks(%llu free %llu reserved)\n", flex_gd->count,
1443 blocks_count, free_blocks, reserved_blocks);
1444 }
1445
1446 /* Add a flex group to an fs. Ensure we handle all possible error conditions
1447 * _before_ we start modifying the filesystem, because we cannot abort the
1448 * transaction and not have it write the data to disk.
1449 */
ext4_flex_group_add(struct super_block * sb,struct inode * resize_inode,struct ext4_new_flex_group_data * flex_gd)1450 static int ext4_flex_group_add(struct super_block *sb,
1451 struct inode *resize_inode,
1452 struct ext4_new_flex_group_data *flex_gd)
1453 {
1454 struct ext4_sb_info *sbi = EXT4_SB(sb);
1455 struct ext4_super_block *es = sbi->s_es;
1456 ext4_fsblk_t o_blocks_count;
1457 ext4_grpblk_t last;
1458 ext4_group_t group;
1459 handle_t *handle;
1460 unsigned reserved_gdb;
1461 int err = 0, err2 = 0, credit;
1462
1463 BUG_ON(!flex_gd->count || !flex_gd->groups || !flex_gd->bg_flags);
1464
1465 reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks);
1466 o_blocks_count = ext4_blocks_count(es);
1467 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1468 BUG_ON(last);
1469
1470 err = setup_new_flex_group_blocks(sb, flex_gd);
1471 if (err)
1472 goto exit;
1473 /*
1474 * We will always be modifying at least the superblock and GDT
1475 * blocks. If we are adding a group past the last current GDT block,
1476 * we will also modify the inode and the dindirect block. If we
1477 * are adding a group with superblock/GDT backups we will also
1478 * modify each of the reserved GDT dindirect blocks.
1479 */
1480 credit = 3; /* sb, resize inode, resize inode dindirect */
1481 /* GDT blocks */
1482 credit += 1 + DIV_ROUND_UP(flex_gd->count, EXT4_DESC_PER_BLOCK(sb));
1483 credit += reserved_gdb; /* Reserved GDT dindirect blocks */
1484 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credit);
1485 if (IS_ERR(handle)) {
1486 err = PTR_ERR(handle);
1487 goto exit;
1488 }
1489
1490 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
1491 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
1492 if (err)
1493 goto exit_journal;
1494
1495 group = flex_gd->groups[0].group;
1496 BUG_ON(group != EXT4_SB(sb)->s_groups_count);
1497 err = ext4_add_new_descs(handle, sb, group,
1498 resize_inode, flex_gd->count);
1499 if (err)
1500 goto exit_journal;
1501
1502 err = ext4_setup_new_descs(handle, sb, flex_gd);
1503 if (err)
1504 goto exit_journal;
1505
1506 ext4_update_super(sb, flex_gd);
1507
1508 err = ext4_handle_dirty_super(handle, sb);
1509
1510 exit_journal:
1511 err2 = ext4_journal_stop(handle);
1512 if (!err)
1513 err = err2;
1514
1515 if (!err) {
1516 int gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
1517 int gdb_num_end = ((group + flex_gd->count - 1) /
1518 EXT4_DESC_PER_BLOCK(sb));
1519 int meta_bg = ext4_has_feature_meta_bg(sb);
1520 sector_t old_gdb = 0;
1521
1522 update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
1523 sizeof(struct ext4_super_block), 0);
1524 for (; gdb_num <= gdb_num_end; gdb_num++) {
1525 struct buffer_head *gdb_bh;
1526
1527 gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc,
1528 gdb_num);
1529 if (old_gdb == gdb_bh->b_blocknr)
1530 continue;
1531 update_backups(sb, gdb_bh->b_blocknr, gdb_bh->b_data,
1532 gdb_bh->b_size, meta_bg);
1533 old_gdb = gdb_bh->b_blocknr;
1534 }
1535 }
1536 exit:
1537 return err;
1538 }
1539
ext4_setup_next_flex_gd(struct super_block * sb,struct ext4_new_flex_group_data * flex_gd,ext4_fsblk_t n_blocks_count,unsigned long flexbg_size)1540 static int ext4_setup_next_flex_gd(struct super_block *sb,
1541 struct ext4_new_flex_group_data *flex_gd,
1542 ext4_fsblk_t n_blocks_count,
1543 unsigned long flexbg_size)
1544 {
1545 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
1546 struct ext4_new_group_data *group_data = flex_gd->groups;
1547 ext4_fsblk_t o_blocks_count;
1548 ext4_group_t n_group;
1549 ext4_group_t group;
1550 ext4_group_t last_group;
1551 ext4_grpblk_t last;
1552 ext4_grpblk_t blocks_per_group;
1553 unsigned long i;
1554
1555 blocks_per_group = EXT4_BLOCKS_PER_GROUP(sb);
1556
1557 o_blocks_count = ext4_blocks_count(es);
1558
1559 if (o_blocks_count == n_blocks_count)
1560 return 0;
1561
1562 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1563 BUG_ON(last);
1564 ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &last);
1565
1566 last_group = group | (flexbg_size - 1);
1567 if (last_group > n_group)
1568 last_group = n_group;
1569
1570 flex_gd->count = last_group - group + 1;
1571
1572 for (i = 0; i < flex_gd->count; i++) {
1573 int overhead;
1574
1575 group_data[i].group = group + i;
1576 group_data[i].blocks_count = blocks_per_group;
1577 overhead = ext4_group_overhead_blocks(sb, group + i);
1578 group_data[i].free_blocks_count = blocks_per_group - overhead;
1579 if (ext4_has_group_desc_csum(sb)) {
1580 flex_gd->bg_flags[i] = EXT4_BG_BLOCK_UNINIT |
1581 EXT4_BG_INODE_UNINIT;
1582 if (!test_opt(sb, INIT_INODE_TABLE))
1583 flex_gd->bg_flags[i] |= EXT4_BG_INODE_ZEROED;
1584 } else
1585 flex_gd->bg_flags[i] = EXT4_BG_INODE_ZEROED;
1586 }
1587
1588 if (last_group == n_group && ext4_has_group_desc_csum(sb))
1589 /* We need to initialize block bitmap of last group. */
1590 flex_gd->bg_flags[i - 1] &= ~EXT4_BG_BLOCK_UNINIT;
1591
1592 if ((last_group == n_group) && (last != blocks_per_group - 1)) {
1593 group_data[i - 1].blocks_count = last + 1;
1594 group_data[i - 1].free_blocks_count -= blocks_per_group-
1595 last - 1;
1596 }
1597
1598 return 1;
1599 }
1600
1601 /* Add group descriptor data to an existing or new group descriptor block.
1602 * Ensure we handle all possible error conditions _before_ we start modifying
1603 * the filesystem, because we cannot abort the transaction and not have it
1604 * write the data to disk.
1605 *
1606 * If we are on a GDT block boundary, we need to get the reserved GDT block.
1607 * Otherwise, we may need to add backup GDT blocks for a sparse group.
1608 *
1609 * We only need to hold the superblock lock while we are actually adding
1610 * in the new group's counts to the superblock. Prior to that we have
1611 * not really "added" the group at all. We re-check that we are still
1612 * adding in the last group in case things have changed since verifying.
1613 */
ext4_group_add(struct super_block * sb,struct ext4_new_group_data * input)1614 int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
1615 {
1616 struct ext4_new_flex_group_data flex_gd;
1617 struct ext4_sb_info *sbi = EXT4_SB(sb);
1618 struct ext4_super_block *es = sbi->s_es;
1619 int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
1620 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
1621 struct inode *inode = NULL;
1622 int gdb_off;
1623 int err;
1624 __u16 bg_flags = 0;
1625
1626 gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
1627
1628 if (gdb_off == 0 && !ext4_has_feature_sparse_super(sb)) {
1629 ext4_warning(sb, "Can't resize non-sparse filesystem further");
1630 return -EPERM;
1631 }
1632
1633 if (ext4_blocks_count(es) + input->blocks_count <
1634 ext4_blocks_count(es)) {
1635 ext4_warning(sb, "blocks_count overflow");
1636 return -EINVAL;
1637 }
1638
1639 if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
1640 le32_to_cpu(es->s_inodes_count)) {
1641 ext4_warning(sb, "inodes_count overflow");
1642 return -EINVAL;
1643 }
1644
1645 if (reserved_gdb || gdb_off == 0) {
1646 if (!ext4_has_feature_resize_inode(sb) ||
1647 !le16_to_cpu(es->s_reserved_gdt_blocks)) {
1648 ext4_warning(sb,
1649 "No reserved GDT blocks, can't resize");
1650 return -EPERM;
1651 }
1652 inode = ext4_iget(sb, EXT4_RESIZE_INO, EXT4_IGET_SPECIAL);
1653 if (IS_ERR(inode)) {
1654 ext4_warning(sb, "Error opening resize inode");
1655 return PTR_ERR(inode);
1656 }
1657 }
1658
1659
1660 err = verify_group_input(sb, input);
1661 if (err)
1662 goto out;
1663
1664 err = ext4_alloc_flex_bg_array(sb, input->group + 1);
1665 if (err)
1666 goto out;
1667
1668 err = ext4_mb_alloc_groupinfo(sb, input->group + 1);
1669 if (err)
1670 goto out;
1671
1672 flex_gd.count = 1;
1673 flex_gd.groups = input;
1674 flex_gd.bg_flags = &bg_flags;
1675 err = ext4_flex_group_add(sb, inode, &flex_gd);
1676 out:
1677 iput(inode);
1678 return err;
1679 } /* ext4_group_add */
1680
1681 /*
1682 * extend a group without checking assuming that checking has been done.
1683 */
ext4_group_extend_no_check(struct super_block * sb,ext4_fsblk_t o_blocks_count,ext4_grpblk_t add)1684 static int ext4_group_extend_no_check(struct super_block *sb,
1685 ext4_fsblk_t o_blocks_count, ext4_grpblk_t add)
1686 {
1687 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
1688 handle_t *handle;
1689 int err = 0, err2;
1690
1691 /* We will update the superblock, one block bitmap, and
1692 * one group descriptor via ext4_group_add_blocks().
1693 */
1694 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, 3);
1695 if (IS_ERR(handle)) {
1696 err = PTR_ERR(handle);
1697 ext4_warning(sb, "error %d on journal start", err);
1698 return err;
1699 }
1700
1701 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
1702 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
1703 if (err) {
1704 ext4_warning(sb, "error %d on journal write access", err);
1705 goto errout;
1706 }
1707
1708 ext4_blocks_count_set(es, o_blocks_count + add);
1709 ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + add);
1710 ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
1711 o_blocks_count + add);
1712 /* We add the blocks to the bitmap and set the group need init bit */
1713 err = ext4_group_add_blocks(handle, sb, o_blocks_count, add);
1714 if (err)
1715 goto errout;
1716 ext4_handle_dirty_super(handle, sb);
1717 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
1718 o_blocks_count + add);
1719 errout:
1720 err2 = ext4_journal_stop(handle);
1721 if (err2 && !err)
1722 err = err2;
1723
1724 if (!err) {
1725 if (test_opt(sb, DEBUG))
1726 printk(KERN_DEBUG "EXT4-fs: extended group to %llu "
1727 "blocks\n", ext4_blocks_count(es));
1728 update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr,
1729 (char *)es, sizeof(struct ext4_super_block), 0);
1730 }
1731 return err;
1732 }
1733
1734 /*
1735 * Extend the filesystem to the new number of blocks specified. This entry
1736 * point is only used to extend the current filesystem to the end of the last
1737 * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
1738 * for emergencies (because it has no dependencies on reserved blocks).
1739 *
1740 * If we _really_ wanted, we could use default values to call ext4_group_add()
1741 * allow the "remount" trick to work for arbitrary resizing, assuming enough
1742 * GDT blocks are reserved to grow to the desired size.
1743 */
ext4_group_extend(struct super_block * sb,struct ext4_super_block * es,ext4_fsblk_t n_blocks_count)1744 int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
1745 ext4_fsblk_t n_blocks_count)
1746 {
1747 ext4_fsblk_t o_blocks_count;
1748 ext4_grpblk_t last;
1749 ext4_grpblk_t add;
1750 struct buffer_head *bh;
1751 int err;
1752 ext4_group_t group;
1753
1754 o_blocks_count = ext4_blocks_count(es);
1755
1756 if (test_opt(sb, DEBUG))
1757 ext4_msg(sb, KERN_DEBUG,
1758 "extending last group from %llu to %llu blocks",
1759 o_blocks_count, n_blocks_count);
1760
1761 if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
1762 return 0;
1763
1764 if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
1765 ext4_msg(sb, KERN_ERR,
1766 "filesystem too large to resize to %llu blocks safely",
1767 n_blocks_count);
1768 if (sizeof(sector_t) < 8)
1769 ext4_warning(sb, "CONFIG_LBDAF not enabled");
1770 return -EINVAL;
1771 }
1772
1773 if (n_blocks_count < o_blocks_count) {
1774 ext4_warning(sb, "can't shrink FS - resize aborted");
1775 return -EINVAL;
1776 }
1777
1778 /* Handle the remaining blocks in the last group only. */
1779 ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
1780
1781 if (last == 0) {
1782 ext4_warning(sb, "need to use ext2online to resize further");
1783 return -EPERM;
1784 }
1785
1786 add = EXT4_BLOCKS_PER_GROUP(sb) - last;
1787
1788 if (o_blocks_count + add < o_blocks_count) {
1789 ext4_warning(sb, "blocks_count overflow");
1790 return -EINVAL;
1791 }
1792
1793 if (o_blocks_count + add > n_blocks_count)
1794 add = n_blocks_count - o_blocks_count;
1795
1796 if (o_blocks_count + add < n_blocks_count)
1797 ext4_warning(sb, "will only finish group (%llu blocks, %u new)",
1798 o_blocks_count + add, add);
1799
1800 /* See if the device is actually as big as what was requested */
1801 bh = sb_bread(sb, o_blocks_count + add - 1);
1802 if (!bh) {
1803 ext4_warning(sb, "can't read last block, resize aborted");
1804 return -ENOSPC;
1805 }
1806 brelse(bh);
1807
1808 err = ext4_group_extend_no_check(sb, o_blocks_count, add);
1809 return err;
1810 } /* ext4_group_extend */
1811
1812
num_desc_blocks(struct super_block * sb,ext4_group_t groups)1813 static int num_desc_blocks(struct super_block *sb, ext4_group_t groups)
1814 {
1815 return (groups + EXT4_DESC_PER_BLOCK(sb) - 1) / EXT4_DESC_PER_BLOCK(sb);
1816 }
1817
1818 /*
1819 * Release the resize inode and drop the resize_inode feature if there
1820 * are no more reserved gdt blocks, and then convert the file system
1821 * to enable meta_bg
1822 */
ext4_convert_meta_bg(struct super_block * sb,struct inode * inode)1823 static int ext4_convert_meta_bg(struct super_block *sb, struct inode *inode)
1824 {
1825 handle_t *handle;
1826 struct ext4_sb_info *sbi = EXT4_SB(sb);
1827 struct ext4_super_block *es = sbi->s_es;
1828 struct ext4_inode_info *ei = EXT4_I(inode);
1829 ext4_fsblk_t nr;
1830 int i, ret, err = 0;
1831 int credits = 1;
1832
1833 ext4_msg(sb, KERN_INFO, "Converting file system to meta_bg");
1834 if (inode) {
1835 if (es->s_reserved_gdt_blocks) {
1836 ext4_error(sb, "Unexpected non-zero "
1837 "s_reserved_gdt_blocks");
1838 return -EPERM;
1839 }
1840
1841 /* Do a quick sanity check of the resize inode */
1842 if (inode->i_blocks != 1 << (inode->i_blkbits - 9))
1843 goto invalid_resize_inode;
1844 for (i = 0; i < EXT4_N_BLOCKS; i++) {
1845 if (i == EXT4_DIND_BLOCK) {
1846 if (ei->i_data[i])
1847 continue;
1848 else
1849 goto invalid_resize_inode;
1850 }
1851 if (ei->i_data[i])
1852 goto invalid_resize_inode;
1853 }
1854 credits += 3; /* block bitmap, bg descriptor, resize inode */
1855 }
1856
1857 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credits);
1858 if (IS_ERR(handle))
1859 return PTR_ERR(handle);
1860
1861 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
1862 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
1863 if (err)
1864 goto errout;
1865
1866 ext4_clear_feature_resize_inode(sb);
1867 ext4_set_feature_meta_bg(sb);
1868 sbi->s_es->s_first_meta_bg =
1869 cpu_to_le32(num_desc_blocks(sb, sbi->s_groups_count));
1870
1871 err = ext4_handle_dirty_super(handle, sb);
1872 if (err) {
1873 ext4_std_error(sb, err);
1874 goto errout;
1875 }
1876
1877 if (inode) {
1878 nr = le32_to_cpu(ei->i_data[EXT4_DIND_BLOCK]);
1879 ext4_free_blocks(handle, inode, NULL, nr, 1,
1880 EXT4_FREE_BLOCKS_METADATA |
1881 EXT4_FREE_BLOCKS_FORGET);
1882 ei->i_data[EXT4_DIND_BLOCK] = 0;
1883 inode->i_blocks = 0;
1884
1885 err = ext4_mark_inode_dirty(handle, inode);
1886 if (err)
1887 ext4_std_error(sb, err);
1888 }
1889
1890 errout:
1891 ret = ext4_journal_stop(handle);
1892 if (!err)
1893 err = ret;
1894 return ret;
1895
1896 invalid_resize_inode:
1897 ext4_error(sb, "corrupted/inconsistent resize inode");
1898 return -EINVAL;
1899 }
1900
1901 /*
1902 * ext4_resize_fs() resizes a fs to new size specified by @n_blocks_count
1903 *
1904 * @sb: super block of the fs to be resized
1905 * @n_blocks_count: the number of blocks resides in the resized fs
1906 */
ext4_resize_fs(struct super_block * sb,ext4_fsblk_t n_blocks_count)1907 int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count)
1908 {
1909 struct ext4_new_flex_group_data *flex_gd = NULL;
1910 struct ext4_sb_info *sbi = EXT4_SB(sb);
1911 struct ext4_super_block *es = sbi->s_es;
1912 struct buffer_head *bh;
1913 struct inode *resize_inode = NULL;
1914 ext4_grpblk_t add, offset;
1915 unsigned long n_desc_blocks;
1916 unsigned long o_desc_blocks;
1917 ext4_group_t o_group;
1918 ext4_group_t n_group;
1919 ext4_fsblk_t o_blocks_count;
1920 ext4_fsblk_t n_blocks_count_retry = 0;
1921 unsigned long last_update_time = 0;
1922 int err = 0, flexbg_size = 1 << sbi->s_log_groups_per_flex;
1923 int meta_bg;
1924
1925 /* See if the device is actually as big as what was requested */
1926 bh = sb_bread(sb, n_blocks_count - 1);
1927 if (!bh) {
1928 ext4_warning(sb, "can't read last block, resize aborted");
1929 return -ENOSPC;
1930 }
1931 brelse(bh);
1932
1933 retry:
1934 o_blocks_count = ext4_blocks_count(es);
1935
1936 ext4_msg(sb, KERN_INFO, "resizing filesystem from %llu "
1937 "to %llu blocks", o_blocks_count, n_blocks_count);
1938
1939 if (n_blocks_count < o_blocks_count) {
1940 /* On-line shrinking not supported */
1941 ext4_warning(sb, "can't shrink FS - resize aborted");
1942 return -EINVAL;
1943 }
1944
1945 if (n_blocks_count == o_blocks_count)
1946 /* Nothing need to do */
1947 return 0;
1948
1949 n_group = ext4_get_group_number(sb, n_blocks_count - 1);
1950 if (n_group >= (0xFFFFFFFFUL / EXT4_INODES_PER_GROUP(sb))) {
1951 ext4_warning(sb, "resize would cause inodes_count overflow");
1952 return -EINVAL;
1953 }
1954 ext4_get_group_no_and_offset(sb, o_blocks_count - 1, &o_group, &offset);
1955
1956 n_desc_blocks = num_desc_blocks(sb, n_group + 1);
1957 o_desc_blocks = num_desc_blocks(sb, sbi->s_groups_count);
1958
1959 meta_bg = ext4_has_feature_meta_bg(sb);
1960
1961 if (ext4_has_feature_resize_inode(sb)) {
1962 if (meta_bg) {
1963 ext4_error(sb, "resize_inode and meta_bg enabled "
1964 "simultaneously");
1965 return -EINVAL;
1966 }
1967 if (n_desc_blocks > o_desc_blocks +
1968 le16_to_cpu(es->s_reserved_gdt_blocks)) {
1969 n_blocks_count_retry = n_blocks_count;
1970 n_desc_blocks = o_desc_blocks +
1971 le16_to_cpu(es->s_reserved_gdt_blocks);
1972 n_group = n_desc_blocks * EXT4_DESC_PER_BLOCK(sb);
1973 n_blocks_count = (ext4_fsblk_t)n_group *
1974 EXT4_BLOCKS_PER_GROUP(sb) +
1975 le32_to_cpu(es->s_first_data_block);
1976 n_group--; /* set to last group number */
1977 }
1978
1979 if (!resize_inode)
1980 resize_inode = ext4_iget(sb, EXT4_RESIZE_INO,
1981 EXT4_IGET_SPECIAL);
1982 if (IS_ERR(resize_inode)) {
1983 ext4_warning(sb, "Error opening resize inode");
1984 return PTR_ERR(resize_inode);
1985 }
1986 }
1987
1988 if ((!resize_inode && !meta_bg) || n_blocks_count == o_blocks_count) {
1989 err = ext4_convert_meta_bg(sb, resize_inode);
1990 if (err)
1991 goto out;
1992 if (resize_inode) {
1993 iput(resize_inode);
1994 resize_inode = NULL;
1995 }
1996 if (n_blocks_count_retry) {
1997 n_blocks_count = n_blocks_count_retry;
1998 n_blocks_count_retry = 0;
1999 goto retry;
2000 }
2001 }
2002
2003 /*
2004 * Make sure the last group has enough space so that it's
2005 * guaranteed to have enough space for all metadata blocks
2006 * that it might need to hold. (We might not need to store
2007 * the inode table blocks in the last block group, but there
2008 * will be cases where this might be needed.)
2009 */
2010 if ((ext4_group_first_block_no(sb, n_group) +
2011 ext4_group_overhead_blocks(sb, n_group) + 2 +
2012 sbi->s_itb_per_group + sbi->s_cluster_ratio) >= n_blocks_count) {
2013 n_blocks_count = ext4_group_first_block_no(sb, n_group);
2014 n_group--;
2015 n_blocks_count_retry = 0;
2016 if (resize_inode) {
2017 iput(resize_inode);
2018 resize_inode = NULL;
2019 }
2020 goto retry;
2021 }
2022
2023 /* extend the last group */
2024 if (n_group == o_group)
2025 add = n_blocks_count - o_blocks_count;
2026 else
2027 add = EXT4_BLOCKS_PER_GROUP(sb) - (offset + 1);
2028 if (add > 0) {
2029 err = ext4_group_extend_no_check(sb, o_blocks_count, add);
2030 if (err)
2031 goto out;
2032 }
2033
2034 if (ext4_blocks_count(es) == n_blocks_count)
2035 goto out;
2036
2037 err = ext4_alloc_flex_bg_array(sb, n_group + 1);
2038 if (err)
2039 goto out;
2040
2041 err = ext4_mb_alloc_groupinfo(sb, n_group + 1);
2042 if (err)
2043 goto out;
2044
2045 flex_gd = alloc_flex_gd(flexbg_size);
2046 if (flex_gd == NULL) {
2047 err = -ENOMEM;
2048 goto out;
2049 }
2050
2051 /* Add flex groups. Note that a regular group is a
2052 * flex group with 1 group.
2053 */
2054 while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count,
2055 flexbg_size)) {
2056 if (jiffies - last_update_time > HZ * 10) {
2057 if (last_update_time)
2058 ext4_msg(sb, KERN_INFO,
2059 "resized to %llu blocks",
2060 ext4_blocks_count(es));
2061 last_update_time = jiffies;
2062 }
2063 if (ext4_alloc_group_tables(sb, flex_gd, flexbg_size) != 0)
2064 break;
2065 err = ext4_flex_group_add(sb, resize_inode, flex_gd);
2066 if (unlikely(err))
2067 break;
2068 }
2069
2070 if (!err && n_blocks_count_retry) {
2071 n_blocks_count = n_blocks_count_retry;
2072 n_blocks_count_retry = 0;
2073 free_flex_gd(flex_gd);
2074 flex_gd = NULL;
2075 if (resize_inode) {
2076 iput(resize_inode);
2077 resize_inode = NULL;
2078 }
2079 goto retry;
2080 }
2081
2082 out:
2083 if (flex_gd)
2084 free_flex_gd(flex_gd);
2085 if (resize_inode != NULL)
2086 iput(resize_inode);
2087 if (err)
2088 ext4_warning(sb, "error (%d) occurred during "
2089 "file system resize", err);
2090 ext4_msg(sb, KERN_INFO, "resized filesystem to %llu",
2091 ext4_blocks_count(es));
2092 return err;
2093 }
2094