OSDN Git Service

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