Whamcloud - gitweb
resize2fs: Add support to use the ext4 online resize ioctl's
[tools/e2fsprogs.git] / resize / online.c
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
19 extern char *program_name;
20
21 errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt, 
22                            blk_t *new_size, int flags EXT2FS_ATTR((unused)))
23 {
24 #ifdef __linux__
25         struct ext2_new_group_input input;
26         struct ext4_new_group_input input64;
27         struct ext2_super_block *sb = fs->super;
28         unsigned long           new_desc_blocks;
29         ext2_filsys             new_fs;
30         errcode_t               retval;
31         dgrp_t                  i;
32         blk_t                   size;
33         int                     fd, r_frac, overhead;
34         int                     use_old_ioctl = 1;
35
36         printf(_("Filesystem at %s is mounted on %s; "
37                  "on-line resizing required\n"), fs->device_name, mtpt);
38
39         if (*new_size < sb->s_blocks_count) {
40                 printf(_("On-line shrinking from %u to %u not supported.\n"),
41                        sb->s_blocks_count, *new_size);
42                 exit(1);
43         }
44
45         /*
46          * If the number of descriptor blocks is going to increase, 
47          * the on-line resizing inode must be present.
48          */
49         new_desc_blocks = ext2fs_div_ceil(
50                 ext2fs_div_ceil(*new_size -
51                                 fs->super->s_first_data_block,
52                                 EXT2_BLOCKS_PER_GROUP(fs->super)),
53                 EXT2_DESC_PER_BLOCK(fs->super));
54         printf("old desc_blocks = %lu, new_desc_blocks = %lu\n", 
55                fs->desc_blocks, new_desc_blocks);
56         if (!(fs->super->s_feature_compat & 
57               EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
58             new_desc_blocks != fs->desc_blocks) {
59                 com_err(program_name, 0, 
60                         _("Filesystem does not support online resizing"));
61                 exit(1);
62         }
63
64         fd = open(mtpt, O_RDONLY);
65         if (fd < 0) {
66                 com_err(program_name, errno, 
67                         _("while trying to open mountpoint %s"), mtpt);
68                 exit(1);
69         }
70
71         size=sb->s_blocks_count;
72         if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) {
73                 if (errno == EPERM)
74                         com_err(program_name, 0, 
75                                 _("Permission denied to resize filesystem"));
76                 else if (errno == ENOTTY)
77                         com_err(program_name, 0, 
78                         _("Kernel does not support online resizing"));
79                 else 
80                         com_err(program_name, errno, 
81                         _("While checking for on-line resizing support"));
82                 exit(1);
83         }
84
85         r_frac = ext2fs_div_ceil(100 * sb->s_r_blocks_count, sb->s_blocks_count);
86
87         retval = ext2fs_read_bitmaps(fs);
88         if (retval)
89                 return retval;
90
91         retval = ext2fs_dup_handle(fs, &new_fs);
92         if (retval)
93                 return retval;
94
95         retval = adjust_fs_info(new_fs, fs, *new_size);
96         if (retval)
97                 return retval;
98
99         printf(_("Performing an on-line resize of %s to %u (%dk) blocks.\n"),
100                fs->device_name, *new_size, fs->blocksize / 1024);
101
102         size = fs->group_desc_count * sb->s_blocks_per_group + 
103                 sb->s_first_data_block;
104         if (size > *new_size)
105                 size = *new_size;
106
107         if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) {
108                 com_err(program_name, errno, 
109                         _("While trying to extend the last group"));
110                 exit(1);
111         }
112
113         for (i = fs->group_desc_count;
114              i < new_fs->group_desc_count; i++) {
115
116                 overhead = (int) (2 + new_fs->inode_blocks_per_group);
117
118                 if (ext2fs_bg_has_super(new_fs, new_fs->group_desc_count - 1))
119                         overhead += 1 + new_fs->desc_blocks + 
120                                 new_fs->super->s_reserved_gdt_blocks;
121
122                 input.group = i;
123                 input.block_bitmap = new_fs->group_desc[i].bg_block_bitmap;
124                 input.inode_bitmap = new_fs->group_desc[i].bg_inode_bitmap;
125                 input.inode_table = new_fs->group_desc[i].bg_inode_table;
126                 input.blocks_count = sb->s_blocks_per_group;
127                 if (i == new_fs->group_desc_count-1) {
128                         input.blocks_count = new_fs->super->s_blocks_count -
129                                 sb->s_first_data_block - 
130                                 (i * sb->s_blocks_per_group);
131                 }
132                 input.reserved_blocks = e2p_percent(r_frac, 
133                                                     input.blocks_count);
134
135 #if 0
136                 printf("new block bitmap is at 0x%04x\n", input.block_bitmap);
137                 printf("new inode bitmap is at 0x%04x\n", input.inode_bitmap);
138                 printf("new inode table is at 0x%04x-0x%04x\n", 
139                        input.inode_table,
140                        input.inode_table + new_fs->inode_blocks_per_group-1);
141                 printf("new group has %u blocks\n", input.blocks_count);
142                 printf("new group will reserve %d blocks\n", 
143                        input.reserved_blocks);
144                 printf("new group has %d free blocks\n", 
145                        new_fs->group_desc[i].bg_free_blocks_count);
146                 printf("new group has %d free inodes (%d blocks)\n",
147                        new_fs->group_desc[i].bg_free_inodes_count, 
148                        new_fs->inode_blocks_per_group);
149                 printf("Adding group #%d\n", input.group);
150 #endif
151
152                 if (use_old_ioctl &&
153                     ioctl(fd, EXT2_IOC_GROUP_ADD, &input) == 0)
154                         continue;
155                 else
156                         use_old_ioctl = 1;
157
158                 input64.group = input.group;
159                 input64.block_bitmap = input.block_bitmap;
160                 input64.inode_bitmap = input.inode_bitmap;
161                 input64.inode_table = input.inode_table;
162                 input64.blocks_count = input.blocks_count;
163                 input64.reserved_blocks = input.reserved_blocks;
164                 input64.unused = input.unused;
165
166                 if (ioctl(fd, EXT4_IOC_GROUP_ADD, &input64) < 0) {
167                         com_err(program_name, errno, 
168                                 _("While trying to add group #%d"), 
169                                 input.group);
170                         exit(1);
171                 }
172         }
173
174         ext2fs_free(new_fs);
175         close(fd);
176
177         return 0;
178 #else
179         printf(_("Filesystem at %s is mounted on %s, and on-line resizing is"
180                  "not supported on this system.\n"), fs->device_name, mtpt);
181         exit(1);
182 #endif
183 }