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