xref: /openbmc/linux/fs/ext4/resize.c (revision 3dcf5451)
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 #include "group.h"
19 
20 #define outside(b, first, last)	((b) < (first) || (b) >= (last))
21 #define inside(b, first, last)	((b) >= (first) && (b) < (last))
22 
23 static int verify_group_input(struct super_block *sb,
24 			      struct ext4_new_group_data *input)
25 {
26 	struct ext4_sb_info *sbi = EXT4_SB(sb);
27 	struct ext4_super_block *es = sbi->s_es;
28 	ext4_fsblk_t start = ext4_blocks_count(es);
29 	ext4_fsblk_t end = start + input->blocks_count;
30 	ext4_group_t group = input->group;
31 	ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group;
32 	unsigned overhead = ext4_bg_has_super(sb, group) ?
33 		(1 + ext4_bg_num_gdb(sb, group) +
34 		 le16_to_cpu(es->s_reserved_gdt_blocks)) : 0;
35 	ext4_fsblk_t metaend = start + overhead;
36 	struct buffer_head *bh = NULL;
37 	ext4_grpblk_t free_blocks_count, offset;
38 	int err = -EINVAL;
39 
40 	input->free_blocks_count = free_blocks_count =
41 		input->blocks_count - 2 - overhead - sbi->s_itb_per_group;
42 
43 	if (test_opt(sb, DEBUG))
44 		printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks "
45 		       "(%d free, %u reserved)\n",
46 		       ext4_bg_has_super(sb, input->group) ? "normal" :
47 		       "no-super", input->group, input->blocks_count,
48 		       free_blocks_count, input->reserved_blocks);
49 
50 	ext4_get_group_no_and_offset(sb, start, NULL, &offset);
51 	if (group != sbi->s_groups_count)
52 		ext4_warning(sb, __func__,
53 			     "Cannot add at group %u (only %lu groups)",
54 			     input->group, sbi->s_groups_count);
55 	else if (offset != 0)
56 			ext4_warning(sb, __func__, "Last group not full");
57 	else if (input->reserved_blocks > input->blocks_count / 5)
58 		ext4_warning(sb, __func__, "Reserved blocks too high (%u)",
59 			     input->reserved_blocks);
60 	else if (free_blocks_count < 0)
61 		ext4_warning(sb, __func__, "Bad blocks count %u",
62 			     input->blocks_count);
63 	else if (!(bh = sb_bread(sb, end - 1)))
64 		ext4_warning(sb, __func__,
65 			     "Cannot read last block (%llu)",
66 			     end - 1);
67 	else if (outside(input->block_bitmap, start, end))
68 		ext4_warning(sb, __func__,
69 			     "Block bitmap not in group (block %llu)",
70 			     (unsigned long long)input->block_bitmap);
71 	else if (outside(input->inode_bitmap, start, end))
72 		ext4_warning(sb, __func__,
73 			     "Inode bitmap not in group (block %llu)",
74 			     (unsigned long long)input->inode_bitmap);
75 	else if (outside(input->inode_table, start, end) ||
76 	         outside(itend - 1, start, end))
77 		ext4_warning(sb, __func__,
78 			     "Inode table not in group (blocks %llu-%llu)",
79 			     (unsigned long long)input->inode_table, itend - 1);
80 	else if (input->inode_bitmap == input->block_bitmap)
81 		ext4_warning(sb, __func__,
82 			     "Block bitmap same as inode bitmap (%llu)",
83 			     (unsigned long long)input->block_bitmap);
84 	else if (inside(input->block_bitmap, input->inode_table, itend))
85 		ext4_warning(sb, __func__,
86 			     "Block bitmap (%llu) in inode table (%llu-%llu)",
87 			     (unsigned long long)input->block_bitmap,
88 			     (unsigned long long)input->inode_table, itend - 1);
89 	else if (inside(input->inode_bitmap, input->inode_table, itend))
90 		ext4_warning(sb, __func__,
91 			     "Inode bitmap (%llu) in inode table (%llu-%llu)",
92 			     (unsigned long long)input->inode_bitmap,
93 			     (unsigned long long)input->inode_table, itend - 1);
94 	else if (inside(input->block_bitmap, start, metaend))
95 		ext4_warning(sb, __func__,
96 			     "Block bitmap (%llu) in GDT table"
97 			     " (%llu-%llu)",
98 			     (unsigned long long)input->block_bitmap,
99 			     start, metaend - 1);
100 	else if (inside(input->inode_bitmap, start, metaend))
101 		ext4_warning(sb, __func__,
102 			     "Inode bitmap (%llu) in GDT table"
103 			     " (%llu-%llu)",
104 			     (unsigned long long)input->inode_bitmap,
105 			     start, metaend - 1);
106 	else if (inside(input->inode_table, start, metaend) ||
107 	         inside(itend - 1, start, metaend))
108 		ext4_warning(sb, __func__,
109 			     "Inode table (%llu-%llu) overlaps"
110 			     "GDT table (%llu-%llu)",
111 			     (unsigned long long)input->inode_table,
112 			     itend - 1, start, metaend - 1);
113 	else
114 		err = 0;
115 	brelse(bh);
116 
117 	return err;
118 }
119 
120 static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
121 				  ext4_fsblk_t blk)
122 {
123 	struct buffer_head *bh;
124 	int err;
125 
126 	bh = sb_getblk(sb, blk);
127 	if (!bh)
128 		return ERR_PTR(-EIO);
129 	if ((err = ext4_journal_get_write_access(handle, bh))) {
130 		brelse(bh);
131 		bh = ERR_PTR(err);
132 	} else {
133 		lock_buffer(bh);
134 		memset(bh->b_data, 0, sb->s_blocksize);
135 		set_buffer_uptodate(bh);
136 		unlock_buffer(bh);
137 	}
138 
139 	return bh;
140 }
141 
142 /*
143  * If we have fewer than thresh credits, extend by EXT4_MAX_TRANS_DATA.
144  * If that fails, restart the transaction & regain write access for the
145  * buffer head which is used for block_bitmap modifications.
146  */
147 static int extend_or_restart_transaction(handle_t *handle, int thresh,
148 					 struct buffer_head *bh)
149 {
150 	int err;
151 
152 	if (handle->h_buffer_credits >= thresh)
153 		return 0;
154 
155 	err = ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA);
156 	if (err < 0)
157 		return err;
158 	if (err) {
159 		if ((err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
160 			return err;
161 	        if ((err = ext4_journal_get_write_access(handle, bh)))
162 			return err;
163         }
164 
165 	return 0;
166 }
167 
168 /*
169  * Set up the block and inode bitmaps, and the inode table for the new group.
170  * This doesn't need to be part of the main transaction, since we are only
171  * changing blocks outside the actual filesystem.  We still do journaling to
172  * ensure the recovery is correct in case of a failure just after resize.
173  * If any part of this fails, we simply abort the resize.
174  */
175 static int setup_new_group_blocks(struct super_block *sb,
176 				  struct ext4_new_group_data *input)
177 {
178 	struct ext4_sb_info *sbi = EXT4_SB(sb);
179 	ext4_fsblk_t start = ext4_group_first_block_no(sb, input->group);
180 	int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
181 		le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0;
182 	unsigned long gdblocks = ext4_bg_num_gdb(sb, input->group);
183 	struct buffer_head *bh;
184 	handle_t *handle;
185 	ext4_fsblk_t block;
186 	ext4_grpblk_t bit;
187 	int i;
188 	int err = 0, err2;
189 
190 	/* This transaction may be extended/restarted along the way */
191 	handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
192 
193 	if (IS_ERR(handle))
194 		return PTR_ERR(handle);
195 
196 	lock_super(sb);
197 	if (input->group != sbi->s_groups_count) {
198 		err = -EBUSY;
199 		goto exit_journal;
200 	}
201 
202 	if (IS_ERR(bh = bclean(handle, sb, input->block_bitmap))) {
203 		err = PTR_ERR(bh);
204 		goto exit_journal;
205 	}
206 
207 	if (ext4_bg_has_super(sb, input->group)) {
208 		ext4_debug("mark backup superblock %#04llx (+0)\n", start);
209 		ext4_set_bit(0, bh->b_data);
210 	}
211 
212 	/* Copy all of the GDT blocks into the backup in this group */
213 	for (i = 0, bit = 1, block = start + 1;
214 	     i < gdblocks; i++, block++, bit++) {
215 		struct buffer_head *gdb;
216 
217 		ext4_debug("update backup group %#04llx (+%d)\n", block, bit);
218 
219 		if ((err = extend_or_restart_transaction(handle, 1, bh)))
220 			goto exit_bh;
221 
222 		gdb = sb_getblk(sb, block);
223 		if (!gdb) {
224 			err = -EIO;
225 			goto exit_bh;
226 		}
227 		if ((err = ext4_journal_get_write_access(handle, gdb))) {
228 			brelse(gdb);
229 			goto exit_bh;
230 		}
231 		lock_buffer(gdb);
232 		memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, gdb->b_size);
233 		set_buffer_uptodate(gdb);
234 		unlock_buffer(gdb);
235 		ext4_journal_dirty_metadata(handle, gdb);
236 		ext4_set_bit(bit, bh->b_data);
237 		brelse(gdb);
238 	}
239 
240 	/* Zero out all of the reserved backup group descriptor table blocks */
241 	for (i = 0, bit = gdblocks + 1, block = start + bit;
242 	     i < reserved_gdb; i++, block++, bit++) {
243 		struct buffer_head *gdb;
244 
245 		ext4_debug("clear reserved block %#04llx (+%d)\n", block, bit);
246 
247 		if ((err = extend_or_restart_transaction(handle, 1, bh)))
248 			goto exit_bh;
249 
250 		if (IS_ERR(gdb = bclean(handle, sb, block))) {
251 			err = PTR_ERR(bh);
252 			goto exit_bh;
253 		}
254 		ext4_journal_dirty_metadata(handle, gdb);
255 		ext4_set_bit(bit, bh->b_data);
256 		brelse(gdb);
257 	}
258 	ext4_debug("mark block bitmap %#04llx (+%llu)\n", input->block_bitmap,
259 		   input->block_bitmap - start);
260 	ext4_set_bit(input->block_bitmap - start, bh->b_data);
261 	ext4_debug("mark inode bitmap %#04llx (+%llu)\n", input->inode_bitmap,
262 		   input->inode_bitmap - start);
263 	ext4_set_bit(input->inode_bitmap - start, bh->b_data);
264 
265 	/* Zero out all of the inode table blocks */
266 	for (i = 0, block = input->inode_table, bit = block - start;
267 	     i < sbi->s_itb_per_group; i++, bit++, block++) {
268 		struct buffer_head *it;
269 
270 		ext4_debug("clear inode block %#04llx (+%d)\n", block, bit);
271 
272 		if ((err = extend_or_restart_transaction(handle, 1, bh)))
273 			goto exit_bh;
274 
275 		if (IS_ERR(it = bclean(handle, sb, block))) {
276 			err = PTR_ERR(it);
277 			goto exit_bh;
278 		}
279 		ext4_journal_dirty_metadata(handle, it);
280 		brelse(it);
281 		ext4_set_bit(bit, bh->b_data);
282 	}
283 
284 	if ((err = extend_or_restart_transaction(handle, 2, bh)))
285 		goto exit_bh;
286 
287 	mark_bitmap_end(input->blocks_count, EXT4_BLOCKS_PER_GROUP(sb),
288 			bh->b_data);
289 	ext4_journal_dirty_metadata(handle, bh);
290 	brelse(bh);
291 
292 	/* Mark unused entries in inode bitmap used */
293 	ext4_debug("clear inode bitmap %#04llx (+%llu)\n",
294 		   input->inode_bitmap, input->inode_bitmap - start);
295 	if (IS_ERR(bh = bclean(handle, sb, input->inode_bitmap))) {
296 		err = PTR_ERR(bh);
297 		goto exit_journal;
298 	}
299 
300 	mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb),
301 			bh->b_data);
302 	ext4_journal_dirty_metadata(handle, bh);
303 exit_bh:
304 	brelse(bh);
305 
306 exit_journal:
307 	unlock_super(sb);
308 	if ((err2 = ext4_journal_stop(handle)) && !err)
309 		err = err2;
310 
311 	return err;
312 }
313 
314 /*
315  * Iterate through the groups which hold BACKUP superblock/GDT copies in an
316  * ext4 filesystem.  The counters should be initialized to 1, 5, and 7 before
317  * calling this for the first time.  In a sparse filesystem it will be the
318  * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
319  * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
320  */
321 static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
322 				  unsigned *five, unsigned *seven)
323 {
324 	unsigned *min = three;
325 	int mult = 3;
326 	unsigned ret;
327 
328 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
329 					EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
330 		ret = *min;
331 		*min += 1;
332 		return ret;
333 	}
334 
335 	if (*five < *min) {
336 		min = five;
337 		mult = 5;
338 	}
339 	if (*seven < *min) {
340 		min = seven;
341 		mult = 7;
342 	}
343 
344 	ret = *min;
345 	*min *= mult;
346 
347 	return ret;
348 }
349 
350 /*
351  * Check that all of the backup GDT blocks are held in the primary GDT block.
352  * It is assumed that they are stored in group order.  Returns the number of
353  * groups in current filesystem that have BACKUPS, or -ve error code.
354  */
355 static int verify_reserved_gdb(struct super_block *sb,
356 			       struct buffer_head *primary)
357 {
358 	const ext4_fsblk_t blk = primary->b_blocknr;
359 	const ext4_group_t end = EXT4_SB(sb)->s_groups_count;
360 	unsigned three = 1;
361 	unsigned five = 5;
362 	unsigned seven = 7;
363 	unsigned grp;
364 	__le32 *p = (__le32 *)primary->b_data;
365 	int gdbackups = 0;
366 
367 	while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
368 		if (le32_to_cpu(*p++) !=
369 		    grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
370 			ext4_warning(sb, __func__,
371 				     "reserved GDT %llu"
372 				     " missing grp %d (%llu)",
373 				     blk, grp,
374 				     grp *
375 				     (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
376 				     blk);
377 			return -EINVAL;
378 		}
379 		if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
380 			return -EFBIG;
381 	}
382 
383 	return gdbackups;
384 }
385 
386 /*
387  * Called when we need to bring a reserved group descriptor table block into
388  * use from the resize inode.  The primary copy of the new GDT block currently
389  * is an indirect block (under the double indirect block in the resize inode).
390  * The new backup GDT blocks will be stored as leaf blocks in this indirect
391  * block, in group order.  Even though we know all the block numbers we need,
392  * we check to ensure that the resize inode has actually reserved these blocks.
393  *
394  * Don't need to update the block bitmaps because the blocks are still in use.
395  *
396  * We get all of the error cases out of the way, so that we are sure to not
397  * fail once we start modifying the data on disk, because JBD has no rollback.
398  */
399 static int add_new_gdb(handle_t *handle, struct inode *inode,
400 		       struct ext4_new_group_data *input,
401 		       struct buffer_head **primary)
402 {
403 	struct super_block *sb = inode->i_sb;
404 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
405 	unsigned long gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
406 	ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
407 	struct buffer_head **o_group_desc, **n_group_desc;
408 	struct buffer_head *dind;
409 	int gdbackups;
410 	struct ext4_iloc iloc;
411 	__le32 *data;
412 	int err;
413 
414 	if (test_opt(sb, DEBUG))
415 		printk(KERN_DEBUG
416 		       "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
417 		       gdb_num);
418 
419 	/*
420 	 * If we are not using the primary superblock/GDT copy don't resize,
421 	 * because the user tools have no way of handling this.  Probably a
422 	 * bad time to do it anyways.
423 	 */
424 	if (EXT4_SB(sb)->s_sbh->b_blocknr !=
425 	    le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
426 		ext4_warning(sb, __func__,
427 			"won't resize using backup superblock at %llu",
428 			(unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
429 		return -EPERM;
430 	}
431 
432 	*primary = sb_bread(sb, gdblock);
433 	if (!*primary)
434 		return -EIO;
435 
436 	if ((gdbackups = verify_reserved_gdb(sb, *primary)) < 0) {
437 		err = gdbackups;
438 		goto exit_bh;
439 	}
440 
441 	data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
442 	dind = sb_bread(sb, le32_to_cpu(*data));
443 	if (!dind) {
444 		err = -EIO;
445 		goto exit_bh;
446 	}
447 
448 	data = (__le32 *)dind->b_data;
449 	if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
450 		ext4_warning(sb, __func__,
451 			     "new group %u GDT block %llu not reserved",
452 			     input->group, gdblock);
453 		err = -EINVAL;
454 		goto exit_dind;
455 	}
456 
457 	if ((err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh)))
458 		goto exit_dind;
459 
460 	if ((err = ext4_journal_get_write_access(handle, *primary)))
461 		goto exit_sbh;
462 
463 	if ((err = ext4_journal_get_write_access(handle, dind)))
464 		goto exit_primary;
465 
466 	/* ext4_reserve_inode_write() gets a reference on the iloc */
467 	if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
468 		goto exit_dindj;
469 
470 	n_group_desc = kmalloc((gdb_num + 1) * sizeof(struct buffer_head *),
471 			GFP_NOFS);
472 	if (!n_group_desc) {
473 		err = -ENOMEM;
474 		ext4_warning(sb, __func__,
475 			      "not enough memory for %lu groups", gdb_num + 1);
476 		goto exit_inode;
477 	}
478 
479 	/*
480 	 * Finally, we have all of the possible failures behind us...
481 	 *
482 	 * Remove new GDT block from inode double-indirect block and clear out
483 	 * the new GDT block for use (which also "frees" the backup GDT blocks
484 	 * from the reserved inode).  We don't need to change the bitmaps for
485 	 * these blocks, because they are marked as in-use from being in the
486 	 * reserved inode, and will become GDT blocks (primary and backup).
487 	 */
488 	data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
489 	ext4_journal_dirty_metadata(handle, dind);
490 	brelse(dind);
491 	inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
492 	ext4_mark_iloc_dirty(handle, inode, &iloc);
493 	memset((*primary)->b_data, 0, sb->s_blocksize);
494 	ext4_journal_dirty_metadata(handle, *primary);
495 
496 	o_group_desc = EXT4_SB(sb)->s_group_desc;
497 	memcpy(n_group_desc, o_group_desc,
498 	       EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
499 	n_group_desc[gdb_num] = *primary;
500 	EXT4_SB(sb)->s_group_desc = n_group_desc;
501 	EXT4_SB(sb)->s_gdb_count++;
502 	kfree(o_group_desc);
503 
504 	le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
505 	ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
506 
507 	return 0;
508 
509 exit_inode:
510 	//ext4_journal_release_buffer(handle, iloc.bh);
511 	brelse(iloc.bh);
512 exit_dindj:
513 	//ext4_journal_release_buffer(handle, dind);
514 exit_primary:
515 	//ext4_journal_release_buffer(handle, *primary);
516 exit_sbh:
517 	//ext4_journal_release_buffer(handle, *primary);
518 exit_dind:
519 	brelse(dind);
520 exit_bh:
521 	brelse(*primary);
522 
523 	ext4_debug("leaving with error %d\n", err);
524 	return err;
525 }
526 
527 /*
528  * Called when we are adding a new group which has a backup copy of each of
529  * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
530  * We need to add these reserved backup GDT blocks to the resize inode, so
531  * that they are kept for future resizing and not allocated to files.
532  *
533  * Each reserved backup GDT block will go into a different indirect block.
534  * The indirect blocks are actually the primary reserved GDT blocks,
535  * so we know in advance what their block numbers are.  We only get the
536  * double-indirect block to verify it is pointing to the primary reserved
537  * GDT blocks so we don't overwrite a data block by accident.  The reserved
538  * backup GDT blocks are stored in their reserved primary GDT block.
539  */
540 static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
541 			      struct ext4_new_group_data *input)
542 {
543 	struct super_block *sb = inode->i_sb;
544 	int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
545 	struct buffer_head **primary;
546 	struct buffer_head *dind;
547 	struct ext4_iloc iloc;
548 	ext4_fsblk_t blk;
549 	__le32 *data, *end;
550 	int gdbackups = 0;
551 	int res, i;
552 	int err;
553 
554 	primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_NOFS);
555 	if (!primary)
556 		return -ENOMEM;
557 
558 	data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
559 	dind = sb_bread(sb, le32_to_cpu(*data));
560 	if (!dind) {
561 		err = -EIO;
562 		goto exit_free;
563 	}
564 
565 	blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count;
566 	data = (__le32 *)dind->b_data + EXT4_SB(sb)->s_gdb_count;
567 	end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb);
568 
569 	/* Get each reserved primary GDT block and verify it holds backups */
570 	for (res = 0; res < reserved_gdb; res++, blk++) {
571 		if (le32_to_cpu(*data) != blk) {
572 			ext4_warning(sb, __func__,
573 				     "reserved block %llu"
574 				     " not at offset %ld",
575 				     blk,
576 				     (long)(data - (__le32 *)dind->b_data));
577 			err = -EINVAL;
578 			goto exit_bh;
579 		}
580 		primary[res] = sb_bread(sb, blk);
581 		if (!primary[res]) {
582 			err = -EIO;
583 			goto exit_bh;
584 		}
585 		if ((gdbackups = verify_reserved_gdb(sb, primary[res])) < 0) {
586 			brelse(primary[res]);
587 			err = gdbackups;
588 			goto exit_bh;
589 		}
590 		if (++data >= end)
591 			data = (__le32 *)dind->b_data;
592 	}
593 
594 	for (i = 0; i < reserved_gdb; i++) {
595 		if ((err = ext4_journal_get_write_access(handle, primary[i]))) {
596 			/*
597 			int j;
598 			for (j = 0; j < i; j++)
599 				ext4_journal_release_buffer(handle, primary[j]);
600 			 */
601 			goto exit_bh;
602 		}
603 	}
604 
605 	if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
606 		goto exit_bh;
607 
608 	/*
609 	 * Finally we can add each of the reserved backup GDT blocks from
610 	 * the new group to its reserved primary GDT block.
611 	 */
612 	blk = input->group * EXT4_BLOCKS_PER_GROUP(sb);
613 	for (i = 0; i < reserved_gdb; i++) {
614 		int err2;
615 		data = (__le32 *)primary[i]->b_data;
616 		/* printk("reserving backup %lu[%u] = %lu\n",
617 		       primary[i]->b_blocknr, gdbackups,
618 		       blk + primary[i]->b_blocknr); */
619 		data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
620 		err2 = ext4_journal_dirty_metadata(handle, primary[i]);
621 		if (!err)
622 			err = err2;
623 	}
624 	inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
625 	ext4_mark_iloc_dirty(handle, inode, &iloc);
626 
627 exit_bh:
628 	while (--res >= 0)
629 		brelse(primary[res]);
630 	brelse(dind);
631 
632 exit_free:
633 	kfree(primary);
634 
635 	return err;
636 }
637 
638 /*
639  * Update the backup copies of the ext4 metadata.  These don't need to be part
640  * of the main resize transaction, because e2fsck will re-write them if there
641  * is a problem (basically only OOM will cause a problem).  However, we
642  * _should_ update the backups if possible, in case the primary gets trashed
643  * for some reason and we need to run e2fsck from a backup superblock.  The
644  * important part is that the new block and inode counts are in the backup
645  * superblocks, and the location of the new group metadata in the GDT backups.
646  *
647  * We do not need lock_super() for this, because these blocks are not
648  * otherwise touched by the filesystem code when it is mounted.  We don't
649  * need to worry about last changing from sbi->s_groups_count, because the
650  * worst that can happen is that we do not copy the full number of backups
651  * at this time.  The resize which changed s_groups_count will backup again.
652  */
653 static void update_backups(struct super_block *sb,
654 			   int blk_off, char *data, int size)
655 {
656 	struct ext4_sb_info *sbi = EXT4_SB(sb);
657 	const ext4_group_t last = sbi->s_groups_count;
658 	const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
659 	unsigned three = 1;
660 	unsigned five = 5;
661 	unsigned seven = 7;
662 	ext4_group_t group;
663 	int rest = sb->s_blocksize - size;
664 	handle_t *handle;
665 	int err = 0, err2;
666 
667 	handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
668 	if (IS_ERR(handle)) {
669 		group = 1;
670 		err = PTR_ERR(handle);
671 		goto exit_err;
672 	}
673 
674 	while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) {
675 		struct buffer_head *bh;
676 
677 		/* Out of journal space, and can't get more - abort - so sad */
678 		if (handle->h_buffer_credits == 0 &&
679 		    ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) &&
680 		    (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
681 			break;
682 
683 		bh = sb_getblk(sb, group * bpg + blk_off);
684 		if (!bh) {
685 			err = -EIO;
686 			break;
687 		}
688 		ext4_debug("update metadata backup %#04lx\n",
689 			  (unsigned long)bh->b_blocknr);
690 		if ((err = ext4_journal_get_write_access(handle, bh)))
691 			break;
692 		lock_buffer(bh);
693 		memcpy(bh->b_data, data, size);
694 		if (rest)
695 			memset(bh->b_data + size, 0, rest);
696 		set_buffer_uptodate(bh);
697 		unlock_buffer(bh);
698 		ext4_journal_dirty_metadata(handle, bh);
699 		brelse(bh);
700 	}
701 	if ((err2 = ext4_journal_stop(handle)) && !err)
702 		err = err2;
703 
704 	/*
705 	 * Ugh! Need to have e2fsck write the backup copies.  It is too
706 	 * late to revert the resize, we shouldn't fail just because of
707 	 * the backup copies (they are only needed in case of corruption).
708 	 *
709 	 * However, if we got here we have a journal problem too, so we
710 	 * can't really start a transaction to mark the superblock.
711 	 * Chicken out and just set the flag on the hope it will be written
712 	 * to disk, and if not - we will simply wait until next fsck.
713 	 */
714 exit_err:
715 	if (err) {
716 		ext4_warning(sb, __func__,
717 			     "can't update backup for group %lu (err %d), "
718 			     "forcing fsck on next reboot", group, err);
719 		sbi->s_mount_state &= ~EXT4_VALID_FS;
720 		sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
721 		mark_buffer_dirty(sbi->s_sbh);
722 	}
723 }
724 
725 /* Add group descriptor data to an existing or new group descriptor block.
726  * Ensure we handle all possible error conditions _before_ we start modifying
727  * the filesystem, because we cannot abort the transaction and not have it
728  * write the data to disk.
729  *
730  * If we are on a GDT block boundary, we need to get the reserved GDT block.
731  * Otherwise, we may need to add backup GDT blocks for a sparse group.
732  *
733  * We only need to hold the superblock lock while we are actually adding
734  * in the new group's counts to the superblock.  Prior to that we have
735  * not really "added" the group at all.  We re-check that we are still
736  * adding in the last group in case things have changed since verifying.
737  */
738 int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
739 {
740 	struct ext4_sb_info *sbi = EXT4_SB(sb);
741 	struct ext4_super_block *es = sbi->s_es;
742 	int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
743 		le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
744 	struct buffer_head *primary = NULL;
745 	struct ext4_group_desc *gdp;
746 	struct inode *inode = NULL;
747 	handle_t *handle;
748 	int gdb_off, gdb_num;
749 	int err, err2;
750 
751 	gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
752 	gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
753 
754 	if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb,
755 					EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
756 		ext4_warning(sb, __func__,
757 			     "Can't resize non-sparse filesystem further");
758 		return -EPERM;
759 	}
760 
761 	if (ext4_blocks_count(es) + input->blocks_count <
762 	    ext4_blocks_count(es)) {
763 		ext4_warning(sb, __func__, "blocks_count overflow\n");
764 		return -EINVAL;
765 	}
766 
767 	if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
768 	    le32_to_cpu(es->s_inodes_count)) {
769 		ext4_warning(sb, __func__, "inodes_count overflow\n");
770 		return -EINVAL;
771 	}
772 
773 	if (reserved_gdb || gdb_off == 0) {
774 		if (!EXT4_HAS_COMPAT_FEATURE(sb,
775 					     EXT4_FEATURE_COMPAT_RESIZE_INODE)){
776 			ext4_warning(sb, __func__,
777 				     "No reserved GDT blocks, can't resize");
778 			return -EPERM;
779 		}
780 		inode = ext4_iget(sb, EXT4_RESIZE_INO);
781 		if (IS_ERR(inode)) {
782 			ext4_warning(sb, __func__,
783 				     "Error opening resize inode");
784 			return PTR_ERR(inode);
785 		}
786 	}
787 
788 	if ((err = verify_group_input(sb, input)))
789 		goto exit_put;
790 
791 	if ((err = setup_new_group_blocks(sb, input)))
792 		goto exit_put;
793 
794 	/*
795 	 * We will always be modifying at least the superblock and a GDT
796 	 * block.  If we are adding a group past the last current GDT block,
797 	 * we will also modify the inode and the dindirect block.  If we
798 	 * are adding a group with superblock/GDT backups  we will also
799 	 * modify each of the reserved GDT dindirect blocks.
800 	 */
801 	handle = ext4_journal_start_sb(sb,
802 				       ext4_bg_has_super(sb, input->group) ?
803 				       3 + reserved_gdb : 4);
804 	if (IS_ERR(handle)) {
805 		err = PTR_ERR(handle);
806 		goto exit_put;
807 	}
808 
809 	lock_super(sb);
810 	if (input->group != sbi->s_groups_count) {
811 		ext4_warning(sb, __func__,
812 			     "multiple resizers run on filesystem!");
813 		err = -EBUSY;
814 		goto exit_journal;
815 	}
816 
817 	if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh)))
818 		goto exit_journal;
819 
820 	/*
821 	 * We will only either add reserved group blocks to a backup group
822 	 * or remove reserved blocks for the first group in a new group block.
823 	 * Doing both would be mean more complex code, and sane people don't
824 	 * use non-sparse filesystems anymore.  This is already checked above.
825 	 */
826 	if (gdb_off) {
827 		primary = sbi->s_group_desc[gdb_num];
828 		if ((err = ext4_journal_get_write_access(handle, primary)))
829 			goto exit_journal;
830 
831 		if (reserved_gdb && ext4_bg_num_gdb(sb, input->group) &&
832 		    (err = reserve_backup_gdb(handle, inode, input)))
833 			goto exit_journal;
834 	} else if ((err = add_new_gdb(handle, inode, input, &primary)))
835 		goto exit_journal;
836 
837 	/*
838 	 * OK, now we've set up the new group.  Time to make it active.
839 	 *
840 	 * Current kernels don't lock all allocations via lock_super(),
841 	 * so we have to be safe wrt. concurrent accesses the group
842 	 * data.  So we need to be careful to set all of the relevant
843 	 * group descriptor data etc. *before* we enable the group.
844 	 *
845 	 * The key field here is sbi->s_groups_count: as long as
846 	 * that retains its old value, nobody is going to access the new
847 	 * group.
848 	 *
849 	 * So first we update all the descriptor metadata for the new
850 	 * group; then we update the total disk blocks count; then we
851 	 * update the groups count to enable the group; then finally we
852 	 * update the free space counts so that the system can start
853 	 * using the new disk blocks.
854 	 */
855 
856 	/* Update group descriptor block for new group */
857 	gdp = (struct ext4_group_desc *)primary->b_data + gdb_off;
858 
859 	ext4_block_bitmap_set(sb, gdp, input->block_bitmap); /* LV FIXME */
860 	ext4_inode_bitmap_set(sb, gdp, input->inode_bitmap); /* LV FIXME */
861 	ext4_inode_table_set(sb, gdp, input->inode_table); /* LV FIXME */
862 	gdp->bg_free_blocks_count = cpu_to_le16(input->free_blocks_count);
863 	gdp->bg_free_inodes_count = cpu_to_le16(EXT4_INODES_PER_GROUP(sb));
864 	gdp->bg_checksum = ext4_group_desc_csum(sbi, input->group, gdp);
865 
866 	/*
867 	 * Make the new blocks and inodes valid next.  We do this before
868 	 * increasing the group count so that once the group is enabled,
869 	 * all of its blocks and inodes are already valid.
870 	 *
871 	 * We always allocate group-by-group, then block-by-block or
872 	 * inode-by-inode within a group, so enabling these
873 	 * blocks/inodes before the group is live won't actually let us
874 	 * allocate the new space yet.
875 	 */
876 	ext4_blocks_count_set(es, ext4_blocks_count(es) +
877 		input->blocks_count);
878 	le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb));
879 
880 	/*
881 	 * We need to protect s_groups_count against other CPUs seeing
882 	 * inconsistent state in the superblock.
883 	 *
884 	 * The precise rules we use are:
885 	 *
886 	 * * Writers of s_groups_count *must* hold lock_super
887 	 * AND
888 	 * * Writers must perform a smp_wmb() after updating all dependent
889 	 *   data and before modifying the groups count
890 	 *
891 	 * * Readers must hold lock_super() over the access
892 	 * OR
893 	 * * Readers must perform an smp_rmb() after reading the groups count
894 	 *   and before reading any dependent data.
895 	 *
896 	 * NB. These rules can be relaxed when checking the group count
897 	 * while freeing data, as we can only allocate from a block
898 	 * group after serialising against the group count, and we can
899 	 * only then free after serialising in turn against that
900 	 * allocation.
901 	 */
902 	smp_wmb();
903 
904 	/* Update the global fs size fields */
905 	sbi->s_groups_count++;
906 
907 	ext4_journal_dirty_metadata(handle, primary);
908 
909 	/* Update the reserved block counts only once the new group is
910 	 * active. */
911 	ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
912 		input->reserved_blocks);
913 
914 	/* Update the free space counts */
915 	percpu_counter_add(&sbi->s_freeblocks_counter,
916 			   input->free_blocks_count);
917 	percpu_counter_add(&sbi->s_freeinodes_counter,
918 			   EXT4_INODES_PER_GROUP(sb));
919 
920 	ext4_journal_dirty_metadata(handle, sbi->s_sbh);
921 	sb->s_dirt = 1;
922 
923 exit_journal:
924 	unlock_super(sb);
925 	if ((err2 = ext4_journal_stop(handle)) && !err)
926 		err = err2;
927 	if (!err) {
928 		update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
929 			       sizeof(struct ext4_super_block));
930 		update_backups(sb, primary->b_blocknr, primary->b_data,
931 			       primary->b_size);
932 	}
933 exit_put:
934 	iput(inode);
935 	return err;
936 } /* ext4_group_add */
937 
938 /* Extend the filesystem to the new number of blocks specified.  This entry
939  * point is only used to extend the current filesystem to the end of the last
940  * existing group.  It can be accessed via ioctl, or by "remount,resize=<size>"
941  * for emergencies (because it has no dependencies on reserved blocks).
942  *
943  * If we _really_ wanted, we could use default values to call ext4_group_add()
944  * allow the "remount" trick to work for arbitrary resizing, assuming enough
945  * GDT blocks are reserved to grow to the desired size.
946  */
947 int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
948 		      ext4_fsblk_t n_blocks_count)
949 {
950 	ext4_fsblk_t o_blocks_count;
951 	ext4_group_t o_groups_count;
952 	ext4_grpblk_t last;
953 	ext4_grpblk_t add;
954 	struct buffer_head * bh;
955 	handle_t *handle;
956 	int err;
957 	unsigned long freed_blocks;
958 
959 	/* We don't need to worry about locking wrt other resizers just
960 	 * yet: we're going to revalidate es->s_blocks_count after
961 	 * taking lock_super() below. */
962 	o_blocks_count = ext4_blocks_count(es);
963 	o_groups_count = EXT4_SB(sb)->s_groups_count;
964 
965 	if (test_opt(sb, DEBUG))
966 		printk(KERN_DEBUG "EXT4-fs: extending last group from %llu uto %llu blocks\n",
967 		       o_blocks_count, n_blocks_count);
968 
969 	if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
970 		return 0;
971 
972 	if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
973 		printk(KERN_ERR "EXT4-fs: filesystem on %s:"
974 			" too large to resize to %llu blocks safely\n",
975 			sb->s_id, n_blocks_count);
976 		if (sizeof(sector_t) < 8)
977 			ext4_warning(sb, __func__,
978 			"CONFIG_LBD not enabled\n");
979 		return -EINVAL;
980 	}
981 
982 	if (n_blocks_count < o_blocks_count) {
983 		ext4_warning(sb, __func__,
984 			     "can't shrink FS - resize aborted");
985 		return -EBUSY;
986 	}
987 
988 	/* Handle the remaining blocks in the last group only. */
989 	ext4_get_group_no_and_offset(sb, o_blocks_count, NULL, &last);
990 
991 	if (last == 0) {
992 		ext4_warning(sb, __func__,
993 			     "need to use ext2online to resize further");
994 		return -EPERM;
995 	}
996 
997 	add = EXT4_BLOCKS_PER_GROUP(sb) - last;
998 
999 	if (o_blocks_count + add < o_blocks_count) {
1000 		ext4_warning(sb, __func__, "blocks_count overflow");
1001 		return -EINVAL;
1002 	}
1003 
1004 	if (o_blocks_count + add > n_blocks_count)
1005 		add = n_blocks_count - o_blocks_count;
1006 
1007 	if (o_blocks_count + add < n_blocks_count)
1008 		ext4_warning(sb, __func__,
1009 			     "will only finish group (%llu"
1010 			     " blocks, %u new)",
1011 			     o_blocks_count + add, add);
1012 
1013 	/* See if the device is actually as big as what was requested */
1014 	bh = sb_bread(sb, o_blocks_count + add -1);
1015 	if (!bh) {
1016 		ext4_warning(sb, __func__,
1017 			     "can't read last block, resize aborted");
1018 		return -ENOSPC;
1019 	}
1020 	brelse(bh);
1021 
1022 	/* We will update the superblock, one block bitmap, and
1023 	 * one group descriptor via ext4_free_blocks().
1024 	 */
1025 	handle = ext4_journal_start_sb(sb, 3);
1026 	if (IS_ERR(handle)) {
1027 		err = PTR_ERR(handle);
1028 		ext4_warning(sb, __func__, "error %d on journal start", err);
1029 		goto exit_put;
1030 	}
1031 
1032 	lock_super(sb);
1033 	if (o_blocks_count != ext4_blocks_count(es)) {
1034 		ext4_warning(sb, __func__,
1035 			     "multiple resizers run on filesystem!");
1036 		unlock_super(sb);
1037 		ext4_journal_stop(handle);
1038 		err = -EBUSY;
1039 		goto exit_put;
1040 	}
1041 
1042 	if ((err = ext4_journal_get_write_access(handle,
1043 						 EXT4_SB(sb)->s_sbh))) {
1044 		ext4_warning(sb, __func__,
1045 			     "error %d on journal write access", err);
1046 		unlock_super(sb);
1047 		ext4_journal_stop(handle);
1048 		goto exit_put;
1049 	}
1050 	ext4_blocks_count_set(es, o_blocks_count + add);
1051 	ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
1052 	sb->s_dirt = 1;
1053 	unlock_super(sb);
1054 	ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
1055 		   o_blocks_count + add);
1056 	ext4_free_blocks_sb(handle, sb, o_blocks_count, add, &freed_blocks);
1057 	ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
1058 		   o_blocks_count + add);
1059 	if ((err = ext4_journal_stop(handle)))
1060 		goto exit_put;
1061 	if (test_opt(sb, DEBUG))
1062 		printk(KERN_DEBUG "EXT4-fs: extended group to %llu blocks\n",
1063 		       ext4_blocks_count(es));
1064 	update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es,
1065 		       sizeof(struct ext4_super_block));
1066 exit_put:
1067 	return err;
1068 } /* ext4_group_extend */
1069