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