1/*
2 * resize2fs.c --- ext2 main routine
3 *
4 * Copyright (C) 1997, 1998 by Theodore Ts'o and
5 * 	PowerQuest, Inc.
6 *
7 * Copyright (C) 1999, 2000 by Theosore Ts'o
8 *
9 * %Begin-Header%
10 * This file may be redistributed under the terms of the GNU Public
11 * License.
12 * %End-Header%
13 */
14
15/*
16 * Resizing a filesystem consists of the following phases:
17 *
18 *	1.  Adjust superblock and write out new parts of the inode
19 * 		table
20 * 	2.  Determine blocks which need to be relocated, and copy the
21 * 		contents of blocks from their old locations to the new ones.
22 * 	3.  Scan the inode table, doing the following:
23 * 		a.  If blocks have been moved, update the block
24 * 			pointers in the inodes and indirect blocks to
25 * 			point at the new block locations.
26 * 		b.  If parts of the inode table need to be evacuated,
27 * 			copy inodes from their old locations to their
28 * 			new ones.
29 * 		c.  If (b) needs to be done, note which blocks contain
30 * 			directory information, since we will need to
31 * 			update the directory information.
32 * 	4.  Update the directory blocks with the new inode locations.
33 * 	5.  Move the inode tables, if necessary.
34 */
35
36#include "resize2fs.h"
37#include <time.h>
38
39#ifdef __linux__			/* Kludge for debugging */
40#define RESIZE2FS_DEBUG
41#endif
42
43static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size);
44static errcode_t blocks_to_move(ext2_resize_t rfs);
45static errcode_t block_mover(ext2_resize_t rfs);
46static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
47static errcode_t inode_ref_fix(ext2_resize_t rfs);
48static errcode_t move_itables(ext2_resize_t rfs);
49static errcode_t fix_resize_inode(ext2_filsys fs);
50static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs);
51
52/*
53 * Some helper CPP macros
54 */
55#define FS_BLOCK_BM(fs, i) ((fs)->group_desc[(i)].bg_block_bitmap)
56#define FS_INODE_BM(fs, i) ((fs)->group_desc[(i)].bg_inode_bitmap)
57#define FS_INODE_TB(fs, i) ((fs)->group_desc[(i)].bg_inode_table)
58
59#define IS_BLOCK_BM(fs, i, blk) ((blk) == FS_BLOCK_BM((fs),(i)))
60#define IS_INODE_BM(fs, i, blk) ((blk) == FS_INODE_BM((fs),(i)))
61
62#define IS_INODE_TB(fs, i, blk) (((blk) >= FS_INODE_TB((fs), (i))) && \
63				 ((blk) < (FS_INODE_TB((fs), (i)) + \
64					   (fs)->inode_blocks_per_group)))
65
66
67
68/*
69 * This is the top-level routine which does the dirty deed....
70 */
71errcode_t resize_fs(ext2_filsys fs, blk_t *new_size, int flags,
72		    errcode_t (*progress)(ext2_resize_t rfs, int pass,
73				     unsigned long cur,
74				     unsigned long max_val))
75{
76	ext2_resize_t	rfs;
77	errcode_t	retval;
78
79	retval = ext2fs_read_bitmaps(fs);
80	if (retval)
81		return retval;
82
83	/*
84	 * Create the data structure
85	 */
86	retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
87	if (retval)
88		return retval;
89	memset(rfs, 0, sizeof(struct ext2_resize_struct));
90
91	rfs->old_fs = fs;
92	rfs->flags = flags;
93	rfs->itable_buf	 = 0;
94	rfs->progress = progress;
95	retval = ext2fs_dup_handle(fs, &rfs->new_fs);
96	if (retval)
97		goto errout;
98
99	retval = adjust_superblock(rfs, *new_size);
100	if (retval)
101		goto errout;
102
103	*new_size = rfs->new_fs->super->s_blocks_count;
104
105	retval = blocks_to_move(rfs);
106	if (retval)
107		goto errout;
108
109#ifdef RESIZE2FS_DEBUG
110	if (rfs->flags & RESIZE_DEBUG_BMOVE)
111		printf("Number of free blocks: %u/%u, Needed: %d\n",
112		       rfs->old_fs->super->s_free_blocks_count,
113		       rfs->new_fs->super->s_free_blocks_count,
114		       rfs->needed_blocks);
115#endif
116
117	retval = block_mover(rfs);
118	if (retval)
119		goto errout;
120
121	retval = inode_scan_and_fix(rfs);
122	if (retval)
123		goto errout;
124
125	retval = inode_ref_fix(rfs);
126	if (retval)
127		goto errout;
128
129	retval = move_itables(rfs);
130	if (retval)
131		goto errout;
132
133	retval = ext2fs_calculate_summary_stats(rfs->new_fs);
134	if (retval)
135		goto errout;
136
137	retval = fix_resize_inode(rfs->new_fs);
138	if (retval)
139		goto errout;
140
141	rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
142	retval = ext2fs_close(rfs->new_fs);
143	if (retval)
144		goto errout;
145
146	rfs->flags = flags;
147
148	ext2fs_free(rfs->old_fs);
149	if (rfs->itable_buf)
150		ext2fs_free_mem(&rfs->itable_buf);
151	ext2fs_free_mem(&rfs);
152
153	return 0;
154
155errout:
156	if (rfs->new_fs)
157		ext2fs_free(rfs->new_fs);
158	if (rfs->itable_buf)
159		ext2fs_free_mem(&rfs->itable_buf);
160	ext2fs_free_mem(&rfs);
161	return retval;
162}
163
164/* --------------------------------------------------------------------
165 *
166 * Resize processing, phase 1.
167 *
168 * In this phase we adjust the in-memory superblock information, and
169 * initialize any new parts of the inode table.  The new parts of the
170 * inode table are created in virgin disk space, so we can abort here
171 * without any side effects.
172 * --------------------------------------------------------------------
173 */
174
175/*
176 * This routine is shared by the online and offline resize routines.
177 * All of the information which is adjusted in memory is done here.
178 */
179errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs, blk_t new_size)
180{
181	errcode_t	retval;
182	int		overhead = 0;
183	int		rem;
184	blk_t		blk, group_block;
185	ext2_ino_t	real_end;
186	int		adj, old_numblocks, numblocks, adjblocks;
187	unsigned long	i, j, old_desc_blocks, max_group;
188	unsigned int	meta_bg, meta_bg_size;
189	int		has_super;
190	unsigned long long new_inodes;	/* u64 to check for overflow */
191
192	fs->super->s_blocks_count = new_size;
193
194retry:
195	fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count -
196				       fs->super->s_first_data_block,
197				       EXT2_BLOCKS_PER_GROUP(fs->super));
198	if (fs->group_desc_count == 0)
199		return EXT2_ET_TOOSMALL;
200	fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
201					  EXT2_DESC_PER_BLOCK(fs->super));
202
203	/*
204	 * Overhead is the number of bookkeeping blocks per group.  It
205	 * includes the superblock backup, the group descriptor
206	 * backups, the inode bitmap, the block bitmap, and the inode
207	 * table.
208	 */
209	overhead = (int) (2 + fs->inode_blocks_per_group);
210
211	if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
212		overhead += 1 + fs->desc_blocks +
213			fs->super->s_reserved_gdt_blocks;
214
215	/*
216	 * See if the last group is big enough to support the
217	 * necessary data structures.  If not, we need to get rid of
218	 * it.
219	 */
220	rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
221		fs->super->s_blocks_per_group;
222	if ((fs->group_desc_count == 1) && rem && (rem < overhead))
223		return EXT2_ET_TOOSMALL;
224	if (rem && (rem < overhead+50)) {
225		fs->super->s_blocks_count -= rem;
226		goto retry;
227	}
228	/*
229	 * Adjust the number of inodes
230	 */
231	new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
232	if (new_inodes > ~0U) {
233		fprintf(stderr, _("inodes (%llu) must be less than %u"),
234				   new_inodes, ~0U);
235		return EXT2_ET_TOO_MANY_INODES;
236	}
237	fs->super->s_inodes_count = fs->super->s_inodes_per_group *
238		fs->group_desc_count;
239
240	/*
241	 * Adjust the number of free blocks
242	 */
243	blk = old_fs->super->s_blocks_count;
244	if (blk > fs->super->s_blocks_count)
245		fs->super->s_free_blocks_count -=
246			(blk - fs->super->s_blocks_count);
247	else
248		fs->super->s_free_blocks_count +=
249			(fs->super->s_blocks_count - blk);
250
251	/*
252	 * Adjust the number of reserved blocks
253	 */
254	blk = (__u64)old_fs->super->s_r_blocks_count * 100 /
255		old_fs->super->s_blocks_count;
256	fs->super->s_r_blocks_count = e2p_percent(blk,
257						  fs->super->s_blocks_count);
258
259	/*
260	 * Adjust the bitmaps for size
261	 */
262	retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count,
263					    fs->super->s_inodes_count,
264					    fs->inode_map);
265	if (retval) goto errout;
266
267	real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
268		     * fs->group_desc_count)) - 1 +
269			     fs->super->s_first_data_block;
270	retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1,
271					    real_end, fs->block_map);
272
273	if (retval) goto errout;
274
275	/*
276	 * Reallocate the group descriptors as necessary.
277	 */
278	if (old_fs->desc_blocks != fs->desc_blocks) {
279		retval = ext2fs_resize_mem(old_fs->desc_blocks *
280					   fs->blocksize,
281					   fs->desc_blocks * fs->blocksize,
282					   &fs->group_desc);
283		if (retval)
284			goto errout;
285		if (fs->desc_blocks > old_fs->desc_blocks)
286			memset((char *) fs->group_desc +
287			       (old_fs->desc_blocks * fs->blocksize), 0,
288			       (fs->desc_blocks - old_fs->desc_blocks) *
289			       fs->blocksize);
290	}
291
292	/*
293	 * If the resize_inode feature is set, and we are changing the
294	 * number of descriptor blocks, then adjust
295	 * s_reserved_gdt_blocks if possible to avoid needing to move
296	 * the inode table either now or in the future.
297	 */
298	if ((fs->super->s_feature_compat &
299	     EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
300	    (old_fs->desc_blocks != fs->desc_blocks)) {
301		int new;
302
303		new = ((int) fs->super->s_reserved_gdt_blocks) +
304			(old_fs->desc_blocks - fs->desc_blocks);
305		if (new < 0)
306			new = 0;
307		if (new > (int) fs->blocksize/4)
308			new = fs->blocksize/4;
309		fs->super->s_reserved_gdt_blocks = new;
310		if (new == 0)
311			fs->super->s_feature_compat &=
312				~EXT2_FEATURE_COMPAT_RESIZE_INODE;
313	}
314
315	/*
316	 * If we are shrinking the number block groups, we're done and
317	 * can exit now.
318	 */
319	if (old_fs->group_desc_count > fs->group_desc_count) {
320		retval = 0;
321		goto errout;
322	}
323
324	/*
325	 * Fix the count of the last (old) block group
326	 */
327	old_numblocks = (old_fs->super->s_blocks_count -
328			 old_fs->super->s_first_data_block) %
329				 old_fs->super->s_blocks_per_group;
330	if (!old_numblocks)
331		old_numblocks = old_fs->super->s_blocks_per_group;
332	if (old_fs->group_desc_count == fs->group_desc_count) {
333		numblocks = (fs->super->s_blocks_count -
334			     fs->super->s_first_data_block) %
335			fs->super->s_blocks_per_group;
336		if (!numblocks)
337			numblocks = fs->super->s_blocks_per_group;
338	} else
339		numblocks = fs->super->s_blocks_per_group;
340	i = old_fs->group_desc_count - 1;
341	fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
342
343	/*
344	 * If the number of block groups is staying the same, we're
345	 * done and can exit now.  (If the number block groups is
346	 * shrinking, we had exited earlier.)
347	 */
348	if (old_fs->group_desc_count >= fs->group_desc_count) {
349		retval = 0;
350		goto errout;
351	}
352
353	/*
354	 * Initialize the new block group descriptors
355	 */
356	group_block = fs->super->s_first_data_block +
357		old_fs->group_desc_count * fs->super->s_blocks_per_group;
358
359	adj = old_fs->group_desc_count;
360	max_group = fs->group_desc_count - adj;
361	if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
362		old_desc_blocks = fs->super->s_first_meta_bg;
363	else
364		old_desc_blocks = fs->desc_blocks +
365			fs->super->s_reserved_gdt_blocks;
366	for (i = old_fs->group_desc_count;
367	     i < fs->group_desc_count; i++) {
368		memset(&fs->group_desc[i], 0,
369		       sizeof(struct ext2_group_desc));
370		adjblocks = 0;
371
372		if (i == fs->group_desc_count-1) {
373			numblocks = (fs->super->s_blocks_count -
374				     fs->super->s_first_data_block) %
375					     fs->super->s_blocks_per_group;
376			if (!numblocks)
377				numblocks = fs->super->s_blocks_per_group;
378		} else
379			numblocks = fs->super->s_blocks_per_group;
380
381		has_super = ext2fs_bg_has_super(fs, i);
382		if (has_super) {
383			ext2fs_mark_block_bitmap(fs->block_map, group_block);
384			adjblocks++;
385		}
386		meta_bg_size = (fs->blocksize /
387				sizeof (struct ext2_group_desc));
388		meta_bg = i / meta_bg_size;
389		if (!(fs->super->s_feature_incompat &
390		      EXT2_FEATURE_INCOMPAT_META_BG) ||
391		    (meta_bg < fs->super->s_first_meta_bg)) {
392			if (has_super) {
393				for (j=0; j < old_desc_blocks; j++)
394					ext2fs_mark_block_bitmap(fs->block_map,
395							 group_block + 1 + j);
396				adjblocks += old_desc_blocks;
397			}
398		} else {
399			if (has_super)
400				has_super = 1;
401			if (((i % meta_bg_size) == 0) ||
402			    ((i % meta_bg_size) == 1) ||
403			    ((i % meta_bg_size) == (meta_bg_size-1)))
404				ext2fs_mark_block_bitmap(fs->block_map,
405						 group_block + has_super);
406		}
407
408		adjblocks += 2 + fs->inode_blocks_per_group;
409
410		numblocks -= adjblocks;
411		fs->super->s_free_blocks_count -= adjblocks;
412		fs->super->s_free_inodes_count +=
413			fs->super->s_inodes_per_group;
414		fs->group_desc[i].bg_free_blocks_count = numblocks;
415		fs->group_desc[i].bg_free_inodes_count =
416			fs->super->s_inodes_per_group;
417		fs->group_desc[i].bg_used_dirs_count = 0;
418
419		retval = ext2fs_allocate_group_table(fs, i, 0);
420		if (retval) goto errout;
421
422		group_block += fs->super->s_blocks_per_group;
423	}
424	retval = 0;
425
426errout:
427	return (retval);
428}
429
430/*
431 * This routine adjusts the superblock and other data structures, both
432 * in disk as well as in memory...
433 */
434static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
435{
436	ext2_filsys fs;
437	int		adj = 0;
438	errcode_t	retval;
439	blk_t		group_block;
440	unsigned long	i;
441	unsigned long	max_group;
442
443	fs = rfs->new_fs;
444	ext2fs_mark_super_dirty(fs);
445	ext2fs_mark_bb_dirty(fs);
446	ext2fs_mark_ib_dirty(fs);
447
448	retval = adjust_fs_info(fs, rfs->old_fs, new_size);
449	if (retval)
450		goto errout;
451
452	/*
453	 * Check to make sure there are enough inodes
454	 */
455	if ((rfs->old_fs->super->s_inodes_count -
456	     rfs->old_fs->super->s_free_inodes_count) >
457	    rfs->new_fs->super->s_inodes_count) {
458		retval = ENOSPC;
459		goto errout;
460	}
461
462	/*
463	 * If we are shrinking the number block groups, we're done and
464	 * can exit now.
465	 */
466	if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
467		retval = 0;
468		goto errout;
469	}
470
471	/*
472	 * If the number of block groups is staying the same, we're
473	 * done and can exit now.  (If the number block groups is
474	 * shrinking, we had exited earlier.)
475	 */
476	if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
477		retval = 0;
478		goto errout;
479	}
480
481	/*
482	 * Initialize the new block group descriptors
483	 */
484	retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
485				&rfs->itable_buf);
486	if (retval)
487		goto errout;
488
489	memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
490	group_block = fs->super->s_first_data_block +
491		rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
492
493	adj = rfs->old_fs->group_desc_count;
494	max_group = fs->group_desc_count - adj;
495	if (rfs->progress) {
496		retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
497				       0, max_group);
498		if (retval)
499			goto errout;
500	}
501	for (i = rfs->old_fs->group_desc_count;
502	     i < fs->group_desc_count; i++) {
503		/*
504		 * Write out the new inode table
505		 */
506		retval = io_channel_write_blk(fs->io,
507					      fs->group_desc[i].bg_inode_table,
508					      fs->inode_blocks_per_group,
509					      rfs->itable_buf);
510		if (retval) goto errout;
511
512		io_channel_flush(fs->io);
513		if (rfs->progress) {
514			retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
515					       i - adj + 1, max_group);
516			if (retval)
517				goto errout;
518		}
519		group_block += fs->super->s_blocks_per_group;
520	}
521	io_channel_flush(fs->io);
522	retval = 0;
523
524errout:
525	return retval;
526}
527
528/* --------------------------------------------------------------------
529 *
530 * Resize processing, phase 2.
531 *
532 * In this phase we adjust determine which blocks need to be moved, in
533 * blocks_to_move().  We then copy the blocks to their ultimate new
534 * destinations using block_mover().  Since we are copying blocks to
535 * their new locations, again during this pass we can abort without
536 * any problems.
537 * --------------------------------------------------------------------
538 */
539
540/*
541 * This helper function creates a block bitmap with all of the
542 * filesystem meta-data blocks.
543 */
544static errcode_t mark_table_blocks(ext2_filsys fs,
545				   ext2fs_block_bitmap bmap)
546{
547	blk_t			b;
548	unsigned int		j;
549	dgrp_t			i;
550	unsigned long		meta_bg_size;
551	unsigned int		old_desc_blocks;
552
553	meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
554	if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
555		old_desc_blocks = fs->super->s_first_meta_bg;
556	else
557		old_desc_blocks = fs->desc_blocks +
558			fs->super->s_reserved_gdt_blocks;
559	for (i = 0; i < fs->group_desc_count; i++) {
560		ext2fs_reserve_super_and_bgd(fs, i, bmap);
561
562		/*
563		 * Mark the blocks used for the inode table
564		 */
565		for (j = 0, b = fs->group_desc[i].bg_inode_table;
566		     j < (unsigned int) fs->inode_blocks_per_group;
567		     j++, b++)
568			ext2fs_mark_block_bitmap(bmap, b);
569
570		/*
571		 * Mark block used for the block bitmap
572		 */
573		ext2fs_mark_block_bitmap(bmap,
574					 fs->group_desc[i].bg_block_bitmap);
575
576		/*
577		 * Mark block used for the inode bitmap
578		 */
579		ext2fs_mark_block_bitmap(bmap,
580					 fs->group_desc[i].bg_inode_bitmap);
581	}
582	return 0;
583}
584
585/*
586 * This function checks to see if a particular block (either a
587 * superblock or a block group descriptor) overlaps with an inode or
588 * block bitmap block, or with the inode table.
589 */
590static void mark_fs_metablock(ext2_resize_t rfs,
591			      ext2fs_block_bitmap meta_bmap,
592			      int group, blk_t blk)
593{
594	ext2_filsys 	fs = rfs->new_fs;
595
596	ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
597	ext2fs_mark_block_bitmap(fs->block_map, blk);
598
599	/*
600	 * Check to see if we overlap with the inode or block bitmap,
601	 * or the inode tables.  If not, and the block is in use, then
602	 * mark it as a block to be moved.
603	 */
604	if (IS_BLOCK_BM(fs, group, blk)) {
605		FS_BLOCK_BM(fs, group) = 0;
606		rfs->needed_blocks++;
607	} else if (IS_INODE_BM(fs, group, blk)) {
608		FS_INODE_BM(fs, group) = 0;
609		rfs->needed_blocks++;
610	} else if (IS_INODE_TB(fs, group, blk)) {
611		FS_INODE_TB(fs, group) = 0;
612		rfs->needed_blocks++;
613	} else if (ext2fs_test_block_bitmap(rfs->old_fs->block_map, blk) &&
614		   !ext2fs_test_block_bitmap(meta_bmap, blk)) {
615		ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
616		rfs->needed_blocks++;
617	}
618}
619
620
621/*
622 * This routine marks and unmarks reserved blocks in the new block
623 * bitmap.  It also determines which blocks need to be moved and
624 * places this information into the move_blocks bitmap.
625 */
626static errcode_t blocks_to_move(ext2_resize_t rfs)
627{
628	int		j, has_super;
629	dgrp_t		i, max_groups;
630	blk_t		blk, group_blk;
631	unsigned long	old_blocks, new_blocks;
632	unsigned int	meta_bg, meta_bg_size;
633	errcode_t	retval;
634	ext2_filsys 	fs, old_fs;
635	ext2fs_block_bitmap	meta_bmap;
636
637	fs = rfs->new_fs;
638	old_fs = rfs->old_fs;
639	if (old_fs->super->s_blocks_count > fs->super->s_blocks_count)
640		fs = rfs->old_fs;
641
642	retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
643					      &rfs->reserve_blocks);
644	if (retval)
645		return retval;
646
647	retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
648					      &rfs->move_blocks);
649	if (retval)
650		return retval;
651
652	retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
653					      &meta_bmap);
654	if (retval)
655		return retval;
656
657	retval = mark_table_blocks(old_fs, meta_bmap);
658	if (retval)
659		return retval;
660
661	fs = rfs->new_fs;
662
663	/*
664	 * If we're shrinking the filesystem, we need to move all of
665	 * the blocks that don't fit any more
666	 */
667	for (blk = fs->super->s_blocks_count;
668	     blk < old_fs->super->s_blocks_count; blk++) {
669		if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
670		    !ext2fs_test_block_bitmap(meta_bmap, blk)) {
671			ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
672			rfs->needed_blocks++;
673		}
674		ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
675	}
676
677	if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
678		old_blocks = old_fs->super->s_first_meta_bg;
679		new_blocks = fs->super->s_first_meta_bg;
680	} else {
681		old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
682		new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
683	}
684
685	if (old_blocks == new_blocks) {
686		retval = 0;
687		goto errout;
688	}
689
690	max_groups = fs->group_desc_count;
691	if (max_groups > old_fs->group_desc_count)
692		max_groups = old_fs->group_desc_count;
693	group_blk = old_fs->super->s_first_data_block;
694	/*
695	 * If we're reducing the number of descriptor blocks, this
696	 * makes life easy.  :-)   We just have to mark some extra
697	 * blocks as free.
698	 */
699	if (old_blocks > new_blocks) {
700		for (i = 0; i < max_groups; i++) {
701			if (!ext2fs_bg_has_super(fs, i)) {
702				group_blk += fs->super->s_blocks_per_group;
703				continue;
704			}
705			for (blk = group_blk+1+new_blocks;
706			     blk < group_blk+1+old_blocks; blk++) {
707				ext2fs_unmark_block_bitmap(fs->block_map,
708							   blk);
709				rfs->needed_blocks--;
710			}
711			group_blk += fs->super->s_blocks_per_group;
712		}
713		retval = 0;
714		goto errout;
715	}
716	/*
717	 * If we're increasing the number of descriptor blocks, life
718	 * gets interesting....
719	 */
720	meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
721	for (i = 0; i < max_groups; i++) {
722		has_super = ext2fs_bg_has_super(fs, i);
723		if (has_super)
724			mark_fs_metablock(rfs, meta_bmap, i, group_blk);
725
726		meta_bg = i / meta_bg_size;
727		if (!(fs->super->s_feature_incompat &
728		      EXT2_FEATURE_INCOMPAT_META_BG) ||
729		    (meta_bg < fs->super->s_first_meta_bg)) {
730			if (has_super) {
731				for (blk = group_blk+1;
732				     blk < group_blk + 1 + new_blocks; blk++)
733					mark_fs_metablock(rfs, meta_bmap,
734							  i, blk);
735			}
736		} else {
737			if (has_super)
738				has_super = 1;
739			if (((i % meta_bg_size) == 0) ||
740			    ((i % meta_bg_size) == 1) ||
741			    ((i % meta_bg_size) == (meta_bg_size-1)))
742				mark_fs_metablock(rfs, meta_bmap, i,
743						  group_blk + has_super);
744		}
745
746		if (fs->group_desc[i].bg_inode_table &&
747		    fs->group_desc[i].bg_inode_bitmap &&
748		    fs->group_desc[i].bg_block_bitmap)
749			goto next_group;
750
751		/*
752		 * Reserve the existing meta blocks that we know
753		 * aren't to be moved.
754		 */
755		if (fs->group_desc[i].bg_block_bitmap)
756			ext2fs_mark_block_bitmap(rfs->reserve_blocks,
757				 fs->group_desc[i].bg_block_bitmap);
758		if (fs->group_desc[i].bg_inode_bitmap)
759			ext2fs_mark_block_bitmap(rfs->reserve_blocks,
760				 fs->group_desc[i].bg_inode_bitmap);
761		if (fs->group_desc[i].bg_inode_table)
762			for (blk = fs->group_desc[i].bg_inode_table, j=0;
763			     j < fs->inode_blocks_per_group ; j++, blk++)
764				ext2fs_mark_block_bitmap(rfs->reserve_blocks,
765							 blk);
766
767		/*
768		 * Allocate the missing data structures
769		 */
770		retval = ext2fs_allocate_group_table(fs, i,
771						     rfs->reserve_blocks);
772		if (retval)
773			goto errout;
774
775		/*
776		 * For those structures that have changed, we need to
777		 * do bookkeepping.
778		 */
779		if (FS_BLOCK_BM(old_fs, i) !=
780		    (blk = FS_BLOCK_BM(fs, i))) {
781			ext2fs_mark_block_bitmap(fs->block_map, blk);
782			if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
783			    !ext2fs_test_block_bitmap(meta_bmap, blk))
784				ext2fs_mark_block_bitmap(rfs->move_blocks,
785							 blk);
786		}
787		if (FS_INODE_BM(old_fs, i) !=
788		    (blk = FS_INODE_BM(fs, i))) {
789			ext2fs_mark_block_bitmap(fs->block_map, blk);
790			if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
791			    !ext2fs_test_block_bitmap(meta_bmap, blk))
792				ext2fs_mark_block_bitmap(rfs->move_blocks,
793							 blk);
794		}
795
796		/*
797		 * The inode table, if we need to relocate it, is
798		 * handled specially.  We have to reserve the blocks
799		 * for both the old and the new inode table, since we
800		 * can't have the inode table be destroyed during the
801		 * block relocation phase.
802		 */
803		if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i))
804			goto next_group; /* inode table not moved */
805
806		rfs->needed_blocks += fs->inode_blocks_per_group;
807
808		/*
809		 * Mark the new inode table as in use in the new block
810		 * allocation bitmap, and move any blocks that might
811		 * be necessary.
812		 */
813		for (blk = fs->group_desc[i].bg_inode_table, j=0;
814		     j < fs->inode_blocks_per_group ; j++, blk++) {
815			ext2fs_mark_block_bitmap(fs->block_map, blk);
816			if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
817			    !ext2fs_test_block_bitmap(meta_bmap, blk))
818				ext2fs_mark_block_bitmap(rfs->move_blocks,
819							 blk);
820		}
821
822		/*
823		 * Make sure the old inode table is reserved in the
824		 * block reservation bitmap.
825		 */
826		for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
827		     j < fs->inode_blocks_per_group ; j++, blk++)
828			ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
829
830	next_group:
831		group_blk += rfs->new_fs->super->s_blocks_per_group;
832	}
833	retval = 0;
834
835errout:
836	if (meta_bmap)
837		ext2fs_free_block_bitmap(meta_bmap);
838
839	return retval;
840}
841
842/*
843 * This helper function tries to allocate a new block.  We try to
844 * avoid hitting the original group descriptor blocks at least at
845 * first, since we want to make it possible to recover from a badly
846 * aborted resize operation as much as possible.
847 *
848 * In the future, I may further modify this routine to balance out
849 * where we get the new blocks across the various block groups.
850 * Ideally we would allocate blocks that corresponded with the block
851 * group of the containing inode, and keep contiguous blocks
852 * together.  However, this very difficult to do efficiently, since we
853 * don't have the necessary information up front.
854 */
855
856#define AVOID_OLD	1
857#define DESPERATION	2
858
859static void init_block_alloc(ext2_resize_t rfs)
860{
861	rfs->alloc_state = AVOID_OLD;
862	rfs->new_blk = rfs->new_fs->super->s_first_data_block;
863#if 0
864	/* HACK for testing */
865	if (rfs->new_fs->super->s_blocks_count >
866	    rfs->old_fs->super->s_blocks_count)
867		rfs->new_blk = rfs->old_fs->super->s_blocks_count;
868#endif
869}
870
871static blk_t get_new_block(ext2_resize_t rfs)
872{
873	ext2_filsys	fs = rfs->new_fs;
874
875	while (1) {
876		if (rfs->new_blk >= fs->super->s_blocks_count) {
877			if (rfs->alloc_state == DESPERATION)
878				return 0;
879
880#ifdef RESIZE2FS_DEBUG
881			if (rfs->flags & RESIZE_DEBUG_BMOVE)
882				printf("Going into desperation mode "
883				       "for block allocations\n");
884#endif
885			rfs->alloc_state = DESPERATION;
886			rfs->new_blk = fs->super->s_first_data_block;
887			continue;
888		}
889		if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) ||
890		    ext2fs_test_block_bitmap(rfs->reserve_blocks,
891					     rfs->new_blk) ||
892		    ((rfs->alloc_state == AVOID_OLD) &&
893		     (rfs->new_blk < rfs->old_fs->super->s_blocks_count) &&
894		     ext2fs_test_block_bitmap(rfs->old_fs->block_map,
895					      rfs->new_blk))) {
896			rfs->new_blk++;
897			continue;
898		}
899		return rfs->new_blk;
900	}
901}
902
903static errcode_t block_mover(ext2_resize_t rfs)
904{
905	blk_t			blk, old_blk, new_blk;
906	ext2_filsys		fs = rfs->new_fs;
907	ext2_filsys		old_fs = rfs->old_fs;
908	errcode_t		retval;
909	int			size, c;
910	int			to_move, moved;
911	ext2_badblocks_list	badblock_list = 0;
912	int			bb_modified = 0;
913
914	retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
915	if (retval)
916		return retval;
917
918	new_blk = fs->super->s_first_data_block;
919	if (!rfs->itable_buf) {
920		retval = ext2fs_get_array(fs->blocksize,
921					fs->inode_blocks_per_group,
922					&rfs->itable_buf);
923		if (retval)
924			return retval;
925	}
926	retval = ext2fs_create_extent_table(&rfs->bmap, 0);
927	if (retval)
928		return retval;
929
930	/*
931	 * The first step is to figure out where all of the blocks
932	 * will go.
933	 */
934	to_move = moved = 0;
935	init_block_alloc(rfs);
936	for (blk = old_fs->super->s_first_data_block;
937	     blk < old_fs->super->s_blocks_count; blk++) {
938		if (!ext2fs_test_block_bitmap(old_fs->block_map, blk))
939			continue;
940		if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk))
941			continue;
942		if (ext2fs_badblocks_list_test(badblock_list, blk)) {
943			ext2fs_badblocks_list_del(badblock_list, blk);
944			bb_modified++;
945			continue;
946		}
947
948		new_blk = get_new_block(rfs);
949		if (!new_blk) {
950			retval = ENOSPC;
951			goto errout;
952		}
953		ext2fs_mark_block_bitmap(fs->block_map, new_blk);
954		ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
955		to_move++;
956	}
957
958	if (to_move == 0) {
959		if (rfs->bmap) {
960			ext2fs_free_extent_table(rfs->bmap);
961			rfs->bmap = 0;
962		}
963		retval = 0;
964		goto errout;
965	}
966
967	/*
968	 * Step two is to actually move the blocks
969	 */
970	retval =  ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
971	if (retval) goto errout;
972
973	if (rfs->progress) {
974		retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
975					 0, to_move);
976		if (retval)
977			goto errout;
978	}
979	while (1) {
980		retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
981		if (retval) goto errout;
982		if (!size)
983			break;
984#ifdef RESIZE2FS_DEBUG
985		if (rfs->flags & RESIZE_DEBUG_BMOVE)
986			printf("Moving %d blocks %u->%u\n",
987			       size, old_blk, new_blk);
988#endif
989		do {
990			c = size;
991			if (c > fs->inode_blocks_per_group)
992				c = fs->inode_blocks_per_group;
993			retval = io_channel_read_blk(fs->io, old_blk, c,
994						     rfs->itable_buf);
995			if (retval) goto errout;
996			retval = io_channel_write_blk(fs->io, new_blk, c,
997						      rfs->itable_buf);
998			if (retval) goto errout;
999			size -= c;
1000			new_blk += c;
1001			old_blk += c;
1002			moved += c;
1003			if (rfs->progress) {
1004				io_channel_flush(fs->io);
1005				retval = (rfs->progress)(rfs,
1006						E2_RSZ_BLOCK_RELOC_PASS,
1007						moved, to_move);
1008				if (retval)
1009					goto errout;
1010			}
1011		} while (size > 0);
1012		io_channel_flush(fs->io);
1013	}
1014
1015errout:
1016	if (badblock_list) {
1017		if (!retval && bb_modified)
1018			retval = ext2fs_update_bb_inode(old_fs,
1019							badblock_list);
1020		ext2fs_badblocks_list_free(badblock_list);
1021	}
1022	return retval;
1023}
1024
1025
1026/* --------------------------------------------------------------------
1027 *
1028 * Resize processing, phase 3
1029 *
1030 * --------------------------------------------------------------------
1031 */
1032
1033
1034struct process_block_struct {
1035	ext2_resize_t 		rfs;
1036	ext2_ino_t		ino;
1037	struct ext2_inode *	inode;
1038	errcode_t		error;
1039	int			is_dir;
1040	int			changed;
1041};
1042
1043static int process_block(ext2_filsys fs, blk_t	*block_nr,
1044			 e2_blkcnt_t blockcnt,
1045			 blk_t ref_block EXT2FS_ATTR((unused)),
1046			 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1047{
1048	struct process_block_struct *pb;
1049	errcode_t	retval;
1050	blk_t		block, new_block;
1051	int		ret = 0;
1052
1053	pb = (struct process_block_struct *) priv_data;
1054	block = *block_nr;
1055	if (pb->rfs->bmap) {
1056		new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
1057		if (new_block) {
1058			*block_nr = new_block;
1059			ret |= BLOCK_CHANGED;
1060			pb->changed = 1;
1061#ifdef RESIZE2FS_DEBUG
1062			if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1063				printf("ino=%u, blockcnt=%lld, %u->%u\n",
1064				       pb->ino, blockcnt, block, new_block);
1065#endif
1066			block = new_block;
1067		}
1068	}
1069	if (pb->is_dir) {
1070		retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
1071					      block, (int) blockcnt);
1072		if (retval) {
1073			pb->error = retval;
1074			ret |= BLOCK_ABORT;
1075		}
1076	}
1077	return ret;
1078}
1079
1080/*
1081 * Progress callback
1082 */
1083static errcode_t progress_callback(ext2_filsys fs,
1084				   ext2_inode_scan scan EXT2FS_ATTR((unused)),
1085				   dgrp_t group, void * priv_data)
1086{
1087	ext2_resize_t rfs = (ext2_resize_t) priv_data;
1088	errcode_t		retval;
1089
1090	/*
1091	 * This check is to protect against old ext2 libraries.  It
1092	 * shouldn't be needed against new libraries.
1093	 */
1094	if ((group+1) == 0)
1095		return 0;
1096
1097	if (rfs->progress) {
1098		io_channel_flush(fs->io);
1099		retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1100					 group+1, fs->group_desc_count);
1101		if (retval)
1102			return retval;
1103	}
1104
1105	return 0;
1106}
1107
1108static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1109{
1110	struct process_block_struct	pb;
1111	ext2_ino_t		ino, new_inode;
1112	struct ext2_inode 	*inode = NULL;
1113	ext2_inode_scan 	scan = NULL;
1114	errcode_t		retval;
1115	int			group;
1116	char			*block_buf = 0;
1117	ext2_ino_t		start_to_move;
1118	blk_t			orig_size, new_block;
1119	int			inode_size;
1120
1121	if ((rfs->old_fs->group_desc_count <=
1122	     rfs->new_fs->group_desc_count) &&
1123	    !rfs->bmap)
1124		return 0;
1125
1126	/*
1127	 * Save the original size of the old filesystem, and
1128	 * temporarily set the size to be the new size if the new size
1129	 * is larger.  We need to do this to avoid catching an error
1130	 * by the block iterator routines
1131	 */
1132	orig_size = rfs->old_fs->super->s_blocks_count;
1133	if (orig_size < rfs->new_fs->super->s_blocks_count)
1134		rfs->old_fs->super->s_blocks_count =
1135			rfs->new_fs->super->s_blocks_count;
1136
1137	retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1138	if (retval) goto errout;
1139
1140	retval = ext2fs_init_dblist(rfs->old_fs, 0);
1141	if (retval) goto errout;
1142	retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1143	if (retval) goto errout;
1144
1145	start_to_move = (rfs->new_fs->group_desc_count *
1146			 rfs->new_fs->super->s_inodes_per_group);
1147
1148	if (rfs->progress) {
1149		retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1150					 0, rfs->old_fs->group_desc_count);
1151		if (retval)
1152			goto errout;
1153	}
1154	ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1155	pb.rfs = rfs;
1156	pb.inode = inode;
1157	pb.error = 0;
1158	new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1159	inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
1160	inode = malloc(inode_size);
1161	if (!inode) {
1162		retval = ENOMEM;
1163		goto errout;
1164	}
1165	/*
1166	 * First, copy all of the inodes that need to be moved
1167	 * elsewhere in the inode table
1168	 */
1169	while (1) {
1170		retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
1171		if (retval) goto errout;
1172		if (!ino)
1173			break;
1174
1175		if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
1176			continue; /* inode not in use */
1177
1178		pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1179		pb.changed = 0;
1180
1181		if (inode->i_file_acl && rfs->bmap) {
1182			new_block = ext2fs_extent_translate(rfs->bmap,
1183							    inode->i_file_acl);
1184			if (new_block) {
1185				inode->i_file_acl = new_block;
1186				retval = ext2fs_write_inode_full(rfs->old_fs,
1187							    ino, inode, inode_size);
1188				if (retval) goto errout;
1189			}
1190		}
1191
1192		if (ext2fs_inode_has_valid_blocks(inode) &&
1193		    (rfs->bmap || pb.is_dir)) {
1194			pb.ino = ino;
1195			retval = ext2fs_block_iterate2(rfs->old_fs,
1196						       ino, 0, block_buf,
1197						       process_block, &pb);
1198			if (retval)
1199				goto errout;
1200			if (pb.error) {
1201				retval = pb.error;
1202				goto errout;
1203			}
1204		}
1205
1206		if (ino <= start_to_move)
1207			continue; /* Don't need to move it. */
1208
1209		/*
1210		 * Find a new inode
1211		 */
1212		while (1) {
1213			if (!ext2fs_test_inode_bitmap(rfs->new_fs->inode_map,
1214						      new_inode))
1215				break;
1216			new_inode++;
1217			if (new_inode > rfs->new_fs->super->s_inodes_count) {
1218				retval = ENOSPC;
1219				goto errout;
1220			}
1221		}
1222		ext2fs_mark_inode_bitmap(rfs->new_fs->inode_map, new_inode);
1223		if (pb.changed) {
1224			/* Get the new version of the inode */
1225			retval = ext2fs_read_inode_full(rfs->old_fs, ino,
1226						inode, inode_size);
1227			if (retval) goto errout;
1228		}
1229		inode->i_ctime = time(0);
1230		retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
1231						inode, inode_size);
1232		if (retval) goto errout;
1233
1234		group = (new_inode-1) / EXT2_INODES_PER_GROUP(rfs->new_fs->super);
1235		if (LINUX_S_ISDIR(inode->i_mode))
1236			rfs->new_fs->group_desc[group].bg_used_dirs_count++;
1237
1238#ifdef RESIZE2FS_DEBUG
1239		if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1240			printf("Inode moved %u->%u\n", ino, new_inode);
1241#endif
1242		if (!rfs->imap) {
1243			retval = ext2fs_create_extent_table(&rfs->imap, 0);
1244			if (retval)
1245				goto errout;
1246		}
1247		ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1248	}
1249	io_channel_flush(rfs->old_fs->io);
1250
1251errout:
1252	rfs->old_fs->super->s_blocks_count = orig_size;
1253	if (rfs->bmap) {
1254		ext2fs_free_extent_table(rfs->bmap);
1255		rfs->bmap = 0;
1256	}
1257	if (scan)
1258		ext2fs_close_inode_scan(scan);
1259	if (block_buf)
1260		ext2fs_free_mem(&block_buf);
1261	if (inode)
1262		free(inode);
1263	return retval;
1264}
1265
1266/* --------------------------------------------------------------------
1267 *
1268 * Resize processing, phase 4.
1269 *
1270 * --------------------------------------------------------------------
1271 */
1272
1273struct istruct {
1274	ext2_resize_t rfs;
1275	errcode_t	err;
1276	unsigned long	max_dirs;
1277	int		num;
1278};
1279
1280static int check_and_change_inodes(ext2_ino_t dir,
1281				   int entry EXT2FS_ATTR((unused)),
1282				   struct ext2_dir_entry *dirent, int offset,
1283				   int	blocksize EXT2FS_ATTR((unused)),
1284				   char *buf EXT2FS_ATTR((unused)),
1285				   void *priv_data)
1286{
1287	struct istruct *is = (struct istruct *) priv_data;
1288	struct ext2_inode 	inode;
1289	ext2_ino_t		new_inode;
1290	errcode_t		retval;
1291
1292	if (is->rfs->progress && offset == 0) {
1293		io_channel_flush(is->rfs->old_fs->io);
1294		is->err = (is->rfs->progress)(is->rfs,
1295					      E2_RSZ_INODE_REF_UPD_PASS,
1296					      ++is->num, is->max_dirs);
1297		if (is->err)
1298			return DIRENT_ABORT;
1299	}
1300
1301	if (!dirent->inode)
1302		return 0;
1303
1304	new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1305
1306	if (!new_inode)
1307		return 0;
1308#ifdef RESIZE2FS_DEBUG
1309	if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1310		printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1311		       dir, dirent->name_len&0xFF, dirent->name,
1312		       dirent->inode, new_inode);
1313#endif
1314
1315	dirent->inode = new_inode;
1316
1317	/* Update the directory mtime and ctime */
1318	retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1319	if (retval == 0) {
1320		inode.i_mtime = inode.i_ctime = time(0);
1321		is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1322		if (is->err)
1323			return DIRENT_ABORT;
1324	}
1325
1326	return DIRENT_CHANGED;
1327}
1328
1329static errcode_t inode_ref_fix(ext2_resize_t rfs)
1330{
1331	errcode_t		retval;
1332	struct istruct 		is;
1333
1334	if (!rfs->imap)
1335		return 0;
1336
1337	/*
1338	 * Now, we iterate over all of the directories to update the
1339	 * inode references
1340	 */
1341	is.num = 0;
1342	is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
1343	is.rfs = rfs;
1344	is.err = 0;
1345
1346	if (rfs->progress) {
1347		retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1348					 0, is.max_dirs);
1349		if (retval)
1350			goto errout;
1351	}
1352
1353	retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1354					   DIRENT_FLAG_INCLUDE_EMPTY, 0,
1355					   check_and_change_inodes, &is);
1356	if (retval)
1357		goto errout;
1358	if (is.err) {
1359		retval = is.err;
1360		goto errout;
1361	}
1362
1363errout:
1364	ext2fs_free_extent_table(rfs->imap);
1365	rfs->imap = 0;
1366	return retval;
1367}
1368
1369
1370/* --------------------------------------------------------------------
1371 *
1372 * Resize processing, phase 5.
1373 *
1374 * In this phase we actually move the inode table around, and then
1375 * update the summary statistics.  This is scary, since aborting here
1376 * will potentially scramble the filesystem.  (We are moving the
1377 * inode tables around in place, and so the potential for lost data,
1378 * or at the very least scrambling the mapping between filenames and
1379 * inode numbers is very high in case of a power failure here.)
1380 * --------------------------------------------------------------------
1381 */
1382
1383
1384/*
1385 * A very scary routine --- this one moves the inode table around!!!
1386 *
1387 * After this you have to use the rfs->new_fs file handle to read and
1388 * write inodes.
1389 */
1390static errcode_t move_itables(ext2_resize_t rfs)
1391{
1392	int		n, num, size, diff;
1393	dgrp_t		i, max_groups;
1394	ext2_filsys	fs = rfs->new_fs;
1395	char		*cp;
1396	blk_t		old_blk, new_blk, blk;
1397	errcode_t	retval;
1398	int		j, to_move, moved;
1399
1400	max_groups = fs->group_desc_count;
1401	if (max_groups > rfs->old_fs->group_desc_count)
1402		max_groups = rfs->old_fs->group_desc_count;
1403
1404	size = fs->blocksize * fs->inode_blocks_per_group;
1405	if (!rfs->itable_buf) {
1406		retval = ext2fs_get_mem(size, &rfs->itable_buf);
1407		if (retval)
1408			return retval;
1409	}
1410
1411	/*
1412	 * Figure out how many inode tables we need to move
1413	 */
1414	to_move = moved = 0;
1415	for (i=0; i < max_groups; i++)
1416		if (rfs->old_fs->group_desc[i].bg_inode_table !=
1417		    fs->group_desc[i].bg_inode_table)
1418			to_move++;
1419
1420	if (to_move == 0)
1421		return 0;
1422
1423	if (rfs->progress) {
1424		retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1425				       0, to_move);
1426		if (retval)
1427			goto errout;
1428	}
1429
1430	rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1431
1432	for (i=0; i < max_groups; i++) {
1433		old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1434		new_blk = fs->group_desc[i].bg_inode_table;
1435		diff = new_blk - old_blk;
1436
1437#ifdef RESIZE2FS_DEBUG
1438		if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1439			printf("Itable move group %d block %u->%u (diff %d)\n",
1440			       i, old_blk, new_blk, diff);
1441#endif
1442
1443		if (!diff)
1444			continue;
1445
1446		retval = io_channel_read_blk(fs->io, old_blk,
1447					     fs->inode_blocks_per_group,
1448					     rfs->itable_buf);
1449		if (retval)
1450			goto errout;
1451		/*
1452		 * The end of the inode table segment often contains
1453		 * all zeros, and we're often only moving the inode
1454		 * table down a block or two.  If so, we can optimize
1455		 * things by not rewriting blocks that we know to be zero
1456		 * already.
1457		 */
1458		for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1459			if (*cp)
1460				break;
1461		n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1462#ifdef RESIZE2FS_DEBUG
1463		if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1464			printf("%d blocks of zeros...\n", n);
1465#endif
1466		num = fs->inode_blocks_per_group;
1467		if (n > diff)
1468			num -= n;
1469
1470		retval = io_channel_write_blk(fs->io, new_blk,
1471					      num, rfs->itable_buf);
1472		if (retval) {
1473			io_channel_write_blk(fs->io, old_blk,
1474					     num, rfs->itable_buf);
1475			goto errout;
1476		}
1477		if (n > diff) {
1478			retval = io_channel_write_blk(fs->io,
1479			      old_blk + fs->inode_blocks_per_group,
1480			      diff, (rfs->itable_buf +
1481				     (fs->inode_blocks_per_group - diff) *
1482				     fs->blocksize));
1483			if (retval)
1484				goto errout;
1485		}
1486
1487		for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
1488		     j < fs->inode_blocks_per_group ; j++, blk++)
1489			ext2fs_unmark_block_bitmap(fs->block_map, blk);
1490
1491		rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1492		ext2fs_mark_super_dirty(rfs->old_fs);
1493		ext2fs_flush(rfs->old_fs);
1494
1495		if (rfs->progress) {
1496			retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1497					       ++moved, to_move);
1498			if (retval)
1499				goto errout;
1500		}
1501	}
1502	mark_table_blocks(fs, fs->block_map);
1503	ext2fs_flush(fs);
1504#ifdef RESIZE2FS_DEBUG
1505	if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1506		printf("Inode table move finished.\n");
1507#endif
1508	return 0;
1509
1510errout:
1511	return retval;
1512}
1513
1514/*
1515 * Fix the resize inode
1516 */
1517static errcode_t fix_resize_inode(ext2_filsys fs)
1518{
1519	struct ext2_inode	inode;
1520	errcode_t		retval;
1521	char *			block_buf;
1522
1523	if (!(fs->super->s_feature_compat &
1524	      EXT2_FEATURE_COMPAT_RESIZE_INODE))
1525		return 0;
1526
1527	retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1528	if (retval) goto errout;
1529
1530	retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1531	if (retval) goto errout;
1532
1533	inode.i_blocks = fs->blocksize/512;
1534
1535	retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1536	if (retval) goto errout;
1537
1538	if (!inode.i_block[EXT2_DIND_BLOCK]) {
1539		/*
1540		 * Avoid zeroing out block #0; that's rude.  This
1541		 * should never happen anyway since the filesystem
1542		 * should be fsck'ed and we assume it is consistent.
1543		 */
1544		fprintf(stderr,
1545			_("Should never happen: resize inode corrupt!\n"));
1546		exit(1);
1547	}
1548
1549	memset(block_buf, 0, fs->blocksize);
1550
1551	retval = io_channel_write_blk(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1552				      1, block_buf);
1553	if (retval) goto errout;
1554
1555	retval = ext2fs_create_resize_inode(fs);
1556	if (retval)
1557		goto errout;
1558
1559errout:
1560	if (block_buf)
1561		ext2fs_free_mem(&block_buf);
1562	return retval;
1563}
1564
1565/*
1566 * Finally, recalculate the summary information
1567 */
1568static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1569{
1570	blk_t		blk;
1571	ext2_ino_t	ino;
1572	unsigned int	group = 0;
1573	unsigned int	count = 0;
1574	int		total_free = 0;
1575	int		group_free = 0;
1576
1577	/*
1578	 * First calculate the block statistics
1579	 */
1580	for (blk = fs->super->s_first_data_block;
1581	     blk < fs->super->s_blocks_count; blk++) {
1582		if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1583			group_free++;
1584			total_free++;
1585		}
1586		count++;
1587		if ((count == fs->super->s_blocks_per_group) ||
1588		    (blk == fs->super->s_blocks_count-1)) {
1589			fs->group_desc[group++].bg_free_blocks_count =
1590				group_free;
1591			count = 0;
1592			group_free = 0;
1593		}
1594	}
1595	fs->super->s_free_blocks_count = total_free;
1596
1597	/*
1598	 * Next, calculate the inode statistics
1599	 */
1600	group_free = 0;
1601	total_free = 0;
1602	count = 0;
1603	group = 0;
1604
1605	/* Protect loop from wrap-around if s_inodes_count maxed */
1606	for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1607		if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1608			group_free++;
1609			total_free++;
1610		}
1611		count++;
1612		if ((count == fs->super->s_inodes_per_group) ||
1613		    (ino == fs->super->s_inodes_count)) {
1614			fs->group_desc[group++].bg_free_inodes_count =
1615				group_free;
1616			count = 0;
1617			group_free = 0;
1618		}
1619	}
1620	fs->super->s_free_inodes_count = total_free;
1621	ext2fs_mark_super_dirty(fs);
1622	return 0;
1623}
1624