Whamcloud - gitweb
Fix more spelling errors found by translators and add pluralization
[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         if (!(fs->super->s_feature_compat &
60               EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
61             new_desc_blocks != fs->desc_blocks) {
62                 com_err(program_name, 0,
63                         _("Filesystem does not support online resizing"));
64                 exit(1);
65         }
66
67         fd = open(mtpt, O_RDONLY);
68         if (fd < 0) {
69                 com_err(program_name, errno,
70                         _("while trying to open mountpoint %s"), mtpt);
71                 exit(1);
72         }
73
74         if (ioctl(fd, EXT4_IOC_RESIZE_FS, new_size)) {
75                 if (errno != EINVAL) {
76                         if (errno == EPERM)
77                                 com_err(program_name, 0,
78                                 _("Permission denied to resize filesystem"));
79                         else
80                                 com_err(program_name, errno,
81                                 _("While checking for on-line resizing "
82                                   "support"));
83                         exit(1);
84                 }
85         } else {
86                 close(fd);
87                 return 0;
88         }
89
90         if ((ext2fs_blocks_count(sb) > MAX_32_NUM) ||
91             (*new_size > MAX_32_NUM)) {
92                 com_err(program_name, 0,
93                         _("Kernel does not support resizing a file system "
94                           "this large"));
95                 exit(1);
96         }
97         size = ext2fs_blocks_count(sb);
98
99         if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) {
100                 if (errno == EPERM)
101                         com_err(program_name, 0,
102                                 _("Permission denied to resize filesystem"));
103                 else if (errno == ENOTTY)
104                         com_err(program_name, 0,
105                         _("Kernel does not support online resizing"));
106                 else
107                         com_err(program_name, errno,
108                         _("While checking for on-line resizing support"));
109                 exit(1);
110         }
111
112         percent = (ext2fs_r_blocks_count(sb) * 100.0) /
113                 ext2fs_blocks_count(sb);
114
115         retval = ext2fs_read_bitmaps(fs);
116         if (retval)
117                 return retval;
118
119         retval = ext2fs_dup_handle(fs, &new_fs);
120         if (retval)
121                 return retval;
122
123         /* The current method of adding one block group at a time to a
124          * mounted filesystem means it is impossible to accomodate the
125          * flex_bg allocation method of placing the metadata together
126          * in a single block group.  For now we "fix" this issue by
127          * using the traditional layout for new block groups, where
128          * each block group is self-contained and contains its own
129          * bitmap blocks and inode tables.  This means we don't get
130          * the layout advantages of flex_bg in the new block groups,
131          * but at least it allows on-line resizing to function.
132          */
133         new_fs->super->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_FLEX_BG;
134         retval = adjust_fs_info(new_fs, fs, 0, *new_size);
135         if (retval)
136                 return retval;
137
138         printf(_("Performing an on-line resize of %s to %llu (%dk) blocks.\n"),
139                fs->device_name, *new_size, fs->blocksize / 1024);
140
141         size = fs->group_desc_count * sb->s_blocks_per_group +
142                 sb->s_first_data_block;
143         if (size > *new_size)
144                 size = *new_size;
145
146         if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) {
147                 com_err(program_name, errno,
148                         _("While trying to extend the last group"));
149                 exit(1);
150         }
151
152         for (i = fs->group_desc_count;
153              i < new_fs->group_desc_count; i++) {
154
155                 overhead = (int) (2 + new_fs->inode_blocks_per_group);
156
157                 if (ext2fs_bg_has_super(new_fs, new_fs->group_desc_count - 1))
158                         overhead += 1 + new_fs->desc_blocks +
159                                 new_fs->super->s_reserved_gdt_blocks;
160
161                 input.group = i;
162                 input.block_bitmap = ext2fs_block_bitmap_loc(new_fs, i);
163                 input.inode_bitmap = ext2fs_inode_bitmap_loc(new_fs, i);
164                 input.inode_table = ext2fs_inode_table_loc(new_fs, i);
165                 input.blocks_count = ext2fs_group_blocks_count(new_fs, i);
166                 input.reserved_blocks = (blk_t) (percent * input.blocks_count
167                                                  / 100.0);
168
169 #if 0
170                 printf("new block bitmap is at 0x%04x\n", input.block_bitmap);
171                 printf("new inode bitmap is at 0x%04x\n", input.inode_bitmap);
172                 printf("new inode table is at 0x%04x-0x%04x\n",
173                        input.inode_table,
174                        input.inode_table + new_fs->inode_blocks_per_group-1);
175                 printf("new group has %u blocks\n", input.blocks_count);
176                 printf("new group will reserve %d blocks\n",
177                        input.reserved_blocks);
178                 printf("new group has %d free blocks\n",
179                        ext2fs_bg_free_blocks_count(new_fs, i),
180                 printf("new group has %d free inodes (%d blocks)\n",
181                        ext2fs_bg_free_inodes_count(new_fs, i),
182                        new_fs->inode_blocks_per_group);
183                 printf("Adding group #%d\n", input.group);
184 #endif
185
186                 if (use_old_ioctl &&
187                     ioctl(fd, EXT2_IOC_GROUP_ADD, &input) == 0)
188                         continue;
189                 else
190                         use_old_ioctl = 0;
191
192                 input64.group = input.group;
193                 input64.block_bitmap = input.block_bitmap;
194                 input64.inode_bitmap = input.inode_bitmap;
195                 input64.inode_table = input.inode_table;
196                 input64.blocks_count = input.blocks_count;
197                 input64.reserved_blocks = input.reserved_blocks;
198                 input64.unused = input.unused;
199
200                 if (ioctl(fd, EXT4_IOC_GROUP_ADD, &input64) < 0) {
201                         com_err(program_name, errno,
202                                 _("While trying to add group #%d"),
203                                 input.group);
204                         exit(1);
205                 }
206         }
207
208         ext2fs_free(new_fs);
209         close(fd);
210
211         return 0;
212 #else
213         printf(_("Filesystem at %s is mounted on %s, and on-line resizing is "
214                  "not supported on this system.\n"), fs->device_name, mtpt);
215         exit(1);
216 #endif
217 }