online.c revision e0ed7404719a9ddd2ba427a80db5365c8bad18c0
1/*
2 * online.c --- Do on-line resizing of the ext3 filesystem
3 *
4 * Copyright (C) 2006 by Theodore Ts'o
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12#include "resize2fs.h"
13#ifdef HAVE_SYS_IOCTL_H
14#include <sys/ioctl.h>
15#endif
16#include <sys/stat.h>
17#include <fcntl.h>
18
19extern char *program_name;
20
21#define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
22
23#ifdef __linux__
24static int parse_version_number(const char *s)
25{
26	int	major, minor, rev;
27	char	*endptr;
28	const char *cp = s;
29
30	if (!s)
31		return 0;
32	major = strtol(cp, &endptr, 10);
33	if (cp == endptr || *endptr != '.')
34		return 0;
35	cp = endptr + 1;
36	minor = strtol(cp, &endptr, 10);
37	if (cp == endptr || *endptr != '.')
38		return 0;
39	cp = endptr + 1;
40	rev = strtol(cp, &endptr, 10);
41	if (cp == endptr)
42		return 0;
43	return ((((major * 256) + minor) * 256) + rev);
44}
45
46#define VERSION_CODE(a,b,c) (((a) << 16) + ((b) << 8) + (c))
47
48#endif
49
50errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
51			   blk64_t *new_size, int flags EXT2FS_ATTR((unused)))
52{
53#ifdef __linux__
54	struct ext2_new_group_input input;
55	struct ext4_new_group_input input64;
56	struct ext2_super_block *sb = fs->super;
57	unsigned long		new_desc_blocks;
58	ext2_filsys 		new_fs;
59	errcode_t 		retval;
60	double			percent;
61	dgrp_t			i;
62	blk_t			size;
63	int			fd, overhead;
64	int			use_old_ioctl = 1;
65	int			no_meta_bg_resize = 0;
66	int			no_resize_ioctl = 0;
67
68	if (getenv("RESIZE2FS_KERNEL_VERSION")) {
69		char *version_to_emulate = getenv("RESIZE2FS_KERNEL_VERSION");
70		int kvers = parse_version_number(version_to_emulate);
71
72		if (kvers < VERSION_CODE(3, 7, 0))
73			no_meta_bg_resize = 1;
74		if (kvers < VERSION_CODE(3, 3, 0))
75			no_resize_ioctl = 1;
76	}
77
78	printf(_("Filesystem at %s is mounted on %s; "
79		 "on-line resizing required\n"), fs->device_name, mtpt);
80
81	if (*new_size < ext2fs_blocks_count(sb)) {
82		com_err(program_name, 0, _("On-line shrinking not supported"));
83		exit(1);
84	}
85
86	/*
87	 * If the number of descriptor blocks is going to increase,
88	 * the on-line resizing inode must be present.
89	 */
90	new_desc_blocks = ext2fs_div_ceil(
91		ext2fs_div64_ceil(*new_size -
92				  fs->super->s_first_data_block,
93				  EXT2_BLOCKS_PER_GROUP(fs->super)),
94		EXT2_DESC_PER_BLOCK(fs->super));
95	printf("old_desc_blocks = %lu, new_desc_blocks = %lu\n",
96	       fs->desc_blocks, new_desc_blocks);
97
98	/*
99	 * Do error checking to make sure the resize will be successful.
100	 */
101	if ((access("/sys/fs/ext4/features/meta_bg_resize", R_OK) != 0) ||
102	    no_meta_bg_resize) {
103		if (!EXT2_HAS_COMPAT_FEATURE(fs->super,
104					EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
105		    (new_desc_blocks != fs->desc_blocks)) {
106			com_err(program_name, 0,
107				_("Filesystem does not support online resizing"));
108			exit(1);
109		}
110
111		if (EXT2_HAS_COMPAT_FEATURE(fs->super,
112					EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
113		    new_desc_blocks > (fs->desc_blocks +
114				       fs->super->s_reserved_gdt_blocks)) {
115			com_err(program_name, 0,
116				_("Not enough reserved gdt blocks for resizing"));
117			exit(1);
118		}
119
120		if ((ext2fs_blocks_count(sb) > MAX_32_NUM) ||
121		    (*new_size > MAX_32_NUM)) {
122			com_err(program_name, 0,
123				_("Kernel does not support resizing a file system this large"));
124			exit(1);
125		}
126	}
127
128	fd = open(mtpt, O_RDONLY);
129	if (fd < 0) {
130		com_err(program_name, errno,
131			_("while trying to open mountpoint %s"), mtpt);
132		exit(1);
133	}
134
135	if (no_resize_ioctl) {
136		printf(_("Old resize interface requested.\n"));
137	} else if (ioctl(fd, EXT4_IOC_RESIZE_FS, new_size)) {
138		/*
139		 * If kernel does not support EXT4_IOC_RESIZE_FS, use the
140		 * old online resize. Note that the old approach does not
141		 * handle >32 bit file systems
142		 *
143		 * Sigh, if we are running a 32-bit binary on a 64-bit
144		 * kernel (which happens all the time on the MIPS
145		 * architecture in Debian, but can happen on other CPU
146		 * architectures as well) we will get EINVAL returned
147		 * when an ioctl doesn't exist, at least up to Linux
148		 * 3.1.  See compat_sys_ioctl() in fs/compat_ioctl.c
149		 * in the kernel sources.  This is probably a kernel
150		 * bug, but work around it here.
151		 */
152		if ((errno != ENOTTY) && (errno != EINVAL)) {
153			if (errno == EPERM)
154				com_err(program_name, 0,
155				_("Permission denied to resize filesystem"));
156			else
157				com_err(program_name, errno,
158				_("While checking for on-line resizing "
159				  "support"));
160			exit(1);
161		}
162	} else {
163		close(fd);
164		return 0;
165	}
166
167	size = ext2fs_blocks_count(sb);
168
169	if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) {
170		if (errno == EPERM)
171			com_err(program_name, 0,
172				_("Permission denied to resize filesystem"));
173		else if (errno == ENOTTY)
174			com_err(program_name, 0,
175			_("Kernel does not support online resizing"));
176		else
177			com_err(program_name, errno,
178			_("While checking for on-line resizing support"));
179		exit(1);
180	}
181
182	percent = (ext2fs_r_blocks_count(sb) * 100.0) /
183		ext2fs_blocks_count(sb);
184
185	retval = ext2fs_read_bitmaps(fs);
186	if (retval) {
187		close(fd);
188		return retval;
189	}
190
191	retval = ext2fs_dup_handle(fs, &new_fs);
192	if (retval) {
193		close(fd);
194		return retval;
195	}
196
197	/* The current method of adding one block group at a time to a
198	 * mounted filesystem means it is impossible to accomodate the
199	 * flex_bg allocation method of placing the metadata together
200	 * in a single block group.  For now we "fix" this issue by
201	 * using the traditional layout for new block groups, where
202	 * each block group is self-contained and contains its own
203	 * bitmap blocks and inode tables.  This means we don't get
204	 * the layout advantages of flex_bg in the new block groups,
205	 * but at least it allows on-line resizing to function.
206	 */
207	new_fs->super->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_FLEX_BG;
208	retval = adjust_fs_info(new_fs, fs, 0, *new_size);
209	if (retval) {
210		close(fd);
211		return retval;
212	}
213
214	printf(_("Performing an on-line resize of %s to %llu (%dk) blocks.\n"),
215	       fs->device_name, *new_size, fs->blocksize / 1024);
216
217	size = fs->group_desc_count * sb->s_blocks_per_group +
218		sb->s_first_data_block;
219	if (size > *new_size)
220		size = *new_size;
221
222	if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) {
223		com_err(program_name, errno,
224			_("While trying to extend the last group"));
225		exit(1);
226	}
227
228	for (i = fs->group_desc_count;
229	     i < new_fs->group_desc_count; i++) {
230
231		overhead = (int) (2 + new_fs->inode_blocks_per_group);
232
233		if (ext2fs_bg_has_super(new_fs, new_fs->group_desc_count - 1))
234			overhead += 1 + new_fs->desc_blocks +
235				new_fs->super->s_reserved_gdt_blocks;
236
237		input.group = i;
238		input.block_bitmap = ext2fs_block_bitmap_loc(new_fs, i);
239		input.inode_bitmap = ext2fs_inode_bitmap_loc(new_fs, i);
240		input.inode_table = ext2fs_inode_table_loc(new_fs, i);
241		input.blocks_count = ext2fs_group_blocks_count(new_fs, i);
242		input.reserved_blocks = (blk_t) (percent * input.blocks_count
243						 / 100.0);
244
245#if 0
246		printf("new block bitmap is at 0x%04x\n", input.block_bitmap);
247		printf("new inode bitmap is at 0x%04x\n", input.inode_bitmap);
248		printf("new inode table is at 0x%04x-0x%04x\n",
249		       input.inode_table,
250		       input.inode_table + new_fs->inode_blocks_per_group-1);
251		printf("new group has %u blocks\n", input.blocks_count);
252		printf("new group will reserve %d blocks\n",
253		       input.reserved_blocks);
254		printf("new group has %d free blocks\n",
255		       ext2fs_bg_free_blocks_count(new_fs, i),
256		printf("new group has %d free inodes (%d blocks)\n",
257		       ext2fs_bg_free_inodes_count(new_fs, i),
258		       new_fs->inode_blocks_per_group);
259		printf("Adding group #%d\n", input.group);
260#endif
261
262		if (use_old_ioctl &&
263		    ioctl(fd, EXT2_IOC_GROUP_ADD, &input) == 0)
264			continue;
265		else
266			use_old_ioctl = 0;
267
268		input64.group = input.group;
269		input64.block_bitmap = input.block_bitmap;
270		input64.inode_bitmap = input.inode_bitmap;
271		input64.inode_table = input.inode_table;
272		input64.blocks_count = input.blocks_count;
273		input64.reserved_blocks = input.reserved_blocks;
274		input64.unused = input.unused;
275
276		if (ioctl(fd, EXT4_IOC_GROUP_ADD, &input64) < 0) {
277			com_err(program_name, errno,
278				_("While trying to add group #%d"),
279				input.group);
280			exit(1);
281		}
282	}
283
284	ext2fs_free(new_fs);
285	close(fd);
286
287	return 0;
288#else
289	printf(_("Filesystem at %s is mounted on %s, and on-line resizing is "
290		 "not supported on this system.\n"), fs->device_name, mtpt);
291	exit(1);
292#endif
293}
294