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