Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * main.c --- ext2 resizer main program |
| 3 | * |
Theodore Ts'o | 0cee8a5 | 2000-04-06 21:38:34 +0000 | [diff] [blame] | 4 | * Copyright (C) 1997, 1998 by Theodore Ts'o and |
| 5 | * PowerQuest, Inc. |
| 6 | * |
Theodore Ts'o | 55f4cbd | 2005-01-05 03:01:06 -0500 | [diff] [blame] | 7 | * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 by Theodore Ts'o |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 8 | * |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 9 | * %Begin-Header% |
Theodore Ts'o | 0cee8a5 | 2000-04-06 21:38:34 +0000 | [diff] [blame] | 10 | * This file may be redistributed under the terms of the GNU Public |
| 11 | * License. |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 12 | * %End-Header% |
| 13 | */ |
| 14 | |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 15 | #define _LARGEFILE_SOURCE |
| 16 | #define _LARGEFILE64_SOURCE |
| 17 | |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 18 | #ifdef HAVE_GETOPT_H |
| 19 | #include <getopt.h> |
Theodore Ts'o | 373b833 | 2000-04-03 16:22:35 +0000 | [diff] [blame] | 20 | #else |
| 21 | extern char *optarg; |
| 22 | extern int optind; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 23 | #endif |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 24 | #include <unistd.h> |
Theodore Ts'o | f38cf3c | 2008-09-01 11:17:29 -0400 | [diff] [blame] | 25 | #ifdef HAVE_STDLIB_H |
| 26 | #include <stdlib.h> |
| 27 | #endif |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 28 | #include <sys/types.h> |
Theodore Ts'o | 116db1b | 2002-04-01 01:28:30 -0500 | [diff] [blame] | 29 | #include <sys/stat.h> |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 30 | #include <fcntl.h> |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 31 | |
Theodore Ts'o | 55f4cbd | 2005-01-05 03:01:06 -0500 | [diff] [blame] | 32 | #include "e2p/e2p.h" |
| 33 | |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 34 | #include "resize2fs.h" |
| 35 | |
Theodore Ts'o | 0cee8a5 | 2000-04-06 21:38:34 +0000 | [diff] [blame] | 36 | #include "../version.h" |
Theodore Ts'o | 2e561e0 | 1998-04-08 06:18:37 +0000 | [diff] [blame] | 37 | |
Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 38 | char *program_name, *device_name, *io_options; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 39 | |
Theodore Ts'o | dfcdc32 | 2001-01-11 15:38:00 +0000 | [diff] [blame] | 40 | static void usage (char *prog) |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 41 | { |
Josef Bacik | 199ddaa | 2008-03-13 13:46:10 -0400 | [diff] [blame] | 42 | fprintf (stderr, _("Usage: %s [-d debug_flags] [-f] [-F] [-M] [-P] " |
| 43 | "[-p] device [new_size]\n\n"), prog); |
Theodore Ts'o | 2e561e0 | 1998-04-08 06:18:37 +0000 | [diff] [blame] | 44 | |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 45 | exit (1); |
| 46 | } |
| 47 | |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 48 | static errcode_t resize_progress_func(ext2_resize_t rfs, int pass, |
| 49 | unsigned long cur, unsigned long max) |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 50 | { |
| 51 | ext2_sim_progmeter progress; |
| 52 | const char *label; |
| 53 | errcode_t retval; |
| 54 | |
| 55 | progress = (ext2_sim_progmeter) rfs->prog_data; |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 56 | if (max == 0) |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 57 | return 0; |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 58 | if (cur == 0) { |
| 59 | if (progress) |
| 60 | ext2fs_progress_close(progress); |
| 61 | progress = 0; |
| 62 | switch (pass) { |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 63 | case E2_RSZ_EXTEND_ITABLE_PASS: |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 64 | label = _("Extending the inode table"); |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 65 | break; |
| 66 | case E2_RSZ_BLOCK_RELOC_PASS: |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 67 | label = _("Relocating blocks"); |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 68 | break; |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 69 | case E2_RSZ_INODE_SCAN_PASS: |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 70 | label = _("Scanning inode table"); |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 71 | break; |
| 72 | case E2_RSZ_INODE_REF_UPD_PASS: |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 73 | label = _("Updating inode references"); |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 74 | break; |
| 75 | case E2_RSZ_MOVE_ITABLE_PASS: |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 76 | label = _("Moving inode table"); |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 77 | break; |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 78 | default: |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 79 | label = _("Unknown pass?!?"); |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 80 | break; |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 81 | } |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 82 | printf(_("Begin pass %d (max = %lu)\n"), pass, max); |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 83 | retval = ext2fs_progress_init(&progress, label, 30, |
| 84 | 40, max, 0); |
| 85 | if (retval) |
| 86 | progress = 0; |
| 87 | rfs->prog_data = (void *) progress; |
| 88 | } |
| 89 | if (progress) |
| 90 | ext2fs_progress_update(progress, cur); |
| 91 | if (cur >= max) { |
| 92 | if (progress) |
| 93 | ext2fs_progress_close(progress); |
| 94 | progress = 0; |
| 95 | rfs->prog_data = 0; |
| 96 | } |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 97 | return 0; |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Theodore Ts'o | 46c5490 | 2006-05-14 15:33:57 -0400 | [diff] [blame] | 100 | static void determine_fs_stride(ext2_filsys fs) |
| 101 | { |
| 102 | unsigned int group; |
| 103 | unsigned long long sum; |
| 104 | unsigned int has_sb, prev_has_sb, num; |
| 105 | int i_stride, b_stride; |
| 106 | |
Theodore Ts'o | 96c6a3a | 2007-05-18 22:06:53 -0400 | [diff] [blame] | 107 | if (fs->stride) |
| 108 | return; |
Theodore Ts'o | 46c5490 | 2006-05-14 15:33:57 -0400 | [diff] [blame] | 109 | num = 0; sum = 0; |
| 110 | for (group = 0; group < fs->group_desc_count; group++) { |
| 111 | has_sb = ext2fs_bg_has_super(fs, group); |
| 112 | if (group == 0 || has_sb != prev_has_sb) |
| 113 | goto next; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 114 | b_stride = fs->group_desc[group].bg_block_bitmap - |
| 115 | fs->group_desc[group-1].bg_block_bitmap - |
Theodore Ts'o | 46c5490 | 2006-05-14 15:33:57 -0400 | [diff] [blame] | 116 | fs->super->s_blocks_per_group; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 117 | i_stride = fs->group_desc[group].bg_inode_bitmap - |
| 118 | fs->group_desc[group-1].bg_inode_bitmap - |
Theodore Ts'o | 46c5490 | 2006-05-14 15:33:57 -0400 | [diff] [blame] | 119 | fs->super->s_blocks_per_group; |
| 120 | if (b_stride != i_stride || |
| 121 | b_stride < 0) |
| 122 | goto next; |
| 123 | |
| 124 | /* printf("group %d has stride %d\n", group, b_stride); */ |
| 125 | sum += b_stride; |
| 126 | num++; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 127 | |
Theodore Ts'o | 46c5490 | 2006-05-14 15:33:57 -0400 | [diff] [blame] | 128 | next: |
| 129 | prev_has_sb = has_sb; |
| 130 | } |
| 131 | |
| 132 | if (fs->group_desc_count > 12 && num < 3) |
| 133 | sum = 0; |
| 134 | |
| 135 | if (num) |
| 136 | fs->stride = sum / num; |
| 137 | else |
| 138 | fs->stride = 0; |
| 139 | |
Theodore Ts'o | 96c6a3a | 2007-05-18 22:06:53 -0400 | [diff] [blame] | 140 | fs->super->s_raid_stride = fs->stride; |
| 141 | ext2fs_mark_super_dirty(fs); |
| 142 | |
Theodore Ts'o | 46c5490 | 2006-05-14 15:33:57 -0400 | [diff] [blame] | 143 | #if 0 |
| 144 | if (fs->stride) |
| 145 | printf("Using RAID stride of %d\n", fs->stride); |
| 146 | #endif |
| 147 | } |
| 148 | |
Theodore Ts'o | 0a61731 | 2000-02-02 19:14:36 +0000 | [diff] [blame] | 149 | int main (int argc, char ** argv) |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 150 | { |
| 151 | errcode_t retval; |
| 152 | ext2_filsys fs; |
| 153 | int c; |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 154 | int flags = 0; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 155 | int flush = 0; |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 156 | int force = 0; |
Theodore Ts'o | 5d2ef12 | 2006-03-18 20:03:47 -0500 | [diff] [blame] | 157 | int io_flags = 0; |
Josef Bacik | 199ddaa | 2008-03-13 13:46:10 -0400 | [diff] [blame] | 158 | int force_min_size = 0; |
| 159 | int print_min_size = 0; |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 160 | int fd, ret; |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 161 | blk_t new_size = 0; |
| 162 | blk_t max_size = 0; |
Eric Sandeen | 0d04d88 | 2009-05-18 17:11:28 -0500 | [diff] [blame] | 163 | blk_t min_size = 0; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 164 | io_manager io_ptr; |
Theodore Ts'o | 55f4cbd | 2005-01-05 03:01:06 -0500 | [diff] [blame] | 165 | char *new_size_str = 0; |
Theodore Ts'o | 46c5490 | 2006-05-14 15:33:57 -0400 | [diff] [blame] | 166 | int use_stride = -1; |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 167 | #ifdef HAVE_FSTAT64 |
| 168 | struct stat64 st_buf; |
| 169 | #else |
Theodore Ts'o | 116db1b | 2002-04-01 01:28:30 -0500 | [diff] [blame] | 170 | struct stat st_buf; |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 171 | #endif |
| 172 | __s64 new_file_size; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 173 | unsigned int sys_page_size = 4096; |
Theodore Ts'o | a7ccdff | 2003-07-08 18:03:48 -0400 | [diff] [blame] | 174 | long sysval; |
Theodore Ts'o | bf69235 | 2006-03-17 19:45:45 -0500 | [diff] [blame] | 175 | int len, mount_flags; |
| 176 | char *mtpt; |
Theodore Ts'o | ddc32a0 | 2003-05-03 18:45:55 -0400 | [diff] [blame] | 177 | |
| 178 | #ifdef ENABLE_NLS |
| 179 | setlocale(LC_MESSAGES, ""); |
| 180 | setlocale(LC_CTYPE, ""); |
| 181 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); |
| 182 | textdomain(NLS_CAT_NAME); |
| 183 | #endif |
| 184 | |
Theodore Ts'o | a6d8302 | 2006-12-26 03:38:07 -0500 | [diff] [blame] | 185 | add_error_table(&et_ext2_error_table); |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 186 | |
Theodore Ts'o | f35fd3d | 2005-05-09 16:22:17 -0400 | [diff] [blame] | 187 | fprintf (stderr, "resize2fs %s (%s)\n", |
Theodore Ts'o | ba0af75 | 1998-03-09 17:41:53 +0000 | [diff] [blame] | 188 | E2FSPROGS_VERSION, E2FSPROGS_DATE); |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 189 | if (argc && *argv) |
| 190 | program_name = *argv; |
Theodore Ts'o | 2e561e0 | 1998-04-08 06:18:37 +0000 | [diff] [blame] | 191 | |
Josef Bacik | 199ddaa | 2008-03-13 13:46:10 -0400 | [diff] [blame] | 192 | while ((c = getopt (argc, argv, "d:fFhMPpS:")) != EOF) { |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 193 | switch (c) { |
| 194 | case 'h': |
| 195 | usage(program_name); |
| 196 | break; |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 197 | case 'f': |
| 198 | force = 1; |
| 199 | break; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 200 | case 'F': |
| 201 | flush = 1; |
| 202 | break; |
Josef Bacik | 199ddaa | 2008-03-13 13:46:10 -0400 | [diff] [blame] | 203 | case 'M': |
| 204 | force_min_size = 1; |
| 205 | break; |
| 206 | case 'P': |
| 207 | print_min_size = 1; |
| 208 | break; |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 209 | case 'd': |
| 210 | flags |= atoi(optarg); |
| 211 | break; |
| 212 | case 'p': |
| 213 | flags |= RESIZE_PERCENT_COMPLETE; |
| 214 | break; |
Theodore Ts'o | 46c5490 | 2006-05-14 15:33:57 -0400 | [diff] [blame] | 215 | case 'S': |
| 216 | use_stride = atoi(optarg); |
| 217 | break; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 218 | default: |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 219 | usage(program_name); |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 222 | if (optind == argc) |
| 223 | usage(program_name); |
Theodore Ts'o | 2e561e0 | 1998-04-08 06:18:37 +0000 | [diff] [blame] | 224 | |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 225 | device_name = argv[optind++]; |
Theodore Ts'o | 55f4cbd | 2005-01-05 03:01:06 -0500 | [diff] [blame] | 226 | if (optind < argc) |
| 227 | new_size_str = argv[optind++]; |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 228 | if (optind < argc) |
| 229 | usage(program_name); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 230 | |
Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 231 | io_options = strchr(device_name, '?'); |
| 232 | if (io_options) |
| 233 | *io_options++ = 0; |
| 234 | |
Theodore Ts'o | bf69235 | 2006-03-17 19:45:45 -0500 | [diff] [blame] | 235 | /* |
| 236 | * Figure out whether or not the device is mounted, and if it is |
| 237 | * where it is mounted. |
| 238 | */ |
| 239 | len=80; |
| 240 | while (1) { |
| 241 | mtpt = malloc(len); |
| 242 | if (!mtpt) |
| 243 | return ENOMEM; |
| 244 | mtpt[len-1] = 0; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 245 | retval = ext2fs_check_mount_point(device_name, &mount_flags, |
Theodore Ts'o | bf69235 | 2006-03-17 19:45:45 -0500 | [diff] [blame] | 246 | mtpt, len); |
| 247 | if (retval) { |
| 248 | com_err("ext2fs_check_mount_point", retval, |
| 249 | _("while determining whether %s is mounted."), |
| 250 | device_name); |
| 251 | exit(1); |
| 252 | } |
Theodore Ts'o | 3a4d986 | 2009-07-02 13:54:22 -0400 | [diff] [blame] | 253 | if (!(mount_flags & EXT2_MF_MOUNTED) || (mtpt[len-1] == 0)) |
Theodore Ts'o | bf69235 | 2006-03-17 19:45:45 -0500 | [diff] [blame] | 254 | break; |
| 255 | free(mtpt); |
| 256 | len = 2 * len; |
| 257 | } |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 258 | |
| 259 | #ifdef HAVE_OPEN64 |
| 260 | fd = open64(device_name, O_RDWR); |
| 261 | #else |
| 262 | fd = open(device_name, O_RDWR); |
| 263 | #endif |
| 264 | if (fd < 0) { |
| 265 | com_err("open", errno, _("while opening %s"), |
| 266 | device_name); |
| 267 | exit(1); |
| 268 | } |
| 269 | |
| 270 | #ifdef HAVE_FSTAT64 |
| 271 | ret = fstat64(fd, &st_buf); |
| 272 | #else |
| 273 | ret = fstat(fd, &st_buf); |
| 274 | #endif |
| 275 | if (ret < 0) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 276 | com_err("open", errno, |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 277 | _("while getting stat information for %s"), |
| 278 | device_name); |
| 279 | exit(1); |
| 280 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 281 | |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 282 | if (flush) { |
Theodore Ts'o | 48e08e0 | 2001-01-11 16:11:11 +0000 | [diff] [blame] | 283 | retval = ext2fs_sync_device(fd, 1); |
| 284 | if (retval) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 285 | com_err(argv[0], retval, |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 286 | _("while trying to flush %s"), |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 287 | device_name); |
| 288 | exit(1); |
| 289 | } |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | if (!S_ISREG(st_buf.st_mode )) { |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 293 | close(fd); |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 294 | fd = -1; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Theodore Ts'o | f38cf3c | 2008-09-01 11:17:29 -0400 | [diff] [blame] | 297 | #ifdef CONFIG_TESTIO_DEBUG |
| 298 | if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) { |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 299 | io_ptr = test_io_manager; |
| 300 | test_io_backing_manager = unix_io_manager; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 301 | } else |
Theodore Ts'o | f38cf3c | 2008-09-01 11:17:29 -0400 | [diff] [blame] | 302 | #endif |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 303 | io_ptr = unix_io_manager; |
| 304 | |
Theodore Ts'o | 5d2ef12 | 2006-03-18 20:03:47 -0500 | [diff] [blame] | 305 | if (!(mount_flags & EXT2_MF_MOUNTED)) |
| 306 | io_flags = EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 307 | retval = ext2fs_open2(device_name, io_options, io_flags, |
Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 308 | 0, 0, io_ptr, &fs); |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 309 | if (retval) { |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 310 | com_err (program_name, retval, _("while trying to open %s"), |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 311 | device_name); |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 312 | printf (_("Couldn't find valid filesystem superblock.\n")); |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 313 | exit (1); |
| 314 | } |
Jose R. Santos | 236efed | 2007-10-21 21:03:52 -0500 | [diff] [blame] | 315 | |
Theodore Ts'o | 101c84f | 1998-03-24 16:27:11 +0000 | [diff] [blame] | 316 | /* |
| 317 | * Check for compatibility with the feature sets. We need to |
| 318 | * be more stringent than ext2fs_open(). |
| 319 | */ |
Theodore Ts'o | 76dd5e5 | 2002-10-30 23:07:21 -0500 | [diff] [blame] | 320 | if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) { |
Theodore Ts'o | 101c84f | 1998-03-24 16:27:11 +0000 | [diff] [blame] | 321 | com_err(program_name, EXT2_ET_UNSUPP_FEATURE, |
| 322 | "(%s)", device_name); |
| 323 | exit(1); |
| 324 | } |
Theodore Ts'o | a8d632a | 2008-06-17 01:53:53 -0400 | [diff] [blame] | 325 | |
| 326 | /* |
| 327 | * XXXX The combination of flex_bg and !resize_inode causes |
| 328 | * major problems for resize2fs, since when the group descriptors |
| 329 | * grow in size this can potentially require multiple inode |
| 330 | * tables to be moved aside to make room, and resize2fs chokes |
| 331 | * rather badly in this scenario. It's a rare combination, |
| 332 | * except when a filesystem is expanded more than a certain |
| 333 | * size, so for now, we'll just prohibit that combination. |
| 334 | * This is something we should fix eventually, though. |
| 335 | */ |
| 336 | if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) && |
| 337 | !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) { |
| 338 | com_err(program_name, 0, _("%s: The combination of flex_bg " |
| 339 | "and\n\t!resize_inode features " |
| 340 | "is not supported by resize2fs.\n"), |
| 341 | device_name); |
| 342 | exit(1); |
| 343 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 344 | |
Eric Sandeen | 0d04d88 | 2009-05-18 17:11:28 -0500 | [diff] [blame] | 345 | min_size = calculate_minimum_resize_size(fs); |
| 346 | |
Josef Bacik | 199ddaa | 2008-03-13 13:46:10 -0400 | [diff] [blame] | 347 | if (print_min_size) { |
Theodore Ts'o | a8d632a | 2008-06-17 01:53:53 -0400 | [diff] [blame] | 348 | printf(_("Estimated minimum size of the filesystem: %u\n"), |
Eric Sandeen | 0d04d88 | 2009-05-18 17:11:28 -0500 | [diff] [blame] | 349 | min_size); |
Josef Bacik | 199ddaa | 2008-03-13 13:46:10 -0400 | [diff] [blame] | 350 | exit(0); |
| 351 | } |
| 352 | |
Theodore Ts'o | a7ccdff | 2003-07-08 18:03:48 -0400 | [diff] [blame] | 353 | /* Determine the system page size if possible */ |
| 354 | #ifdef HAVE_SYSCONF |
| 355 | #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE)) |
| 356 | #define _SC_PAGESIZE _SC_PAGE_SIZE |
| 357 | #endif |
| 358 | #ifdef _SC_PAGESIZE |
| 359 | sysval = sysconf(_SC_PAGESIZE); |
| 360 | if (sysval > 0) |
| 361 | sys_page_size = sysval; |
| 362 | #endif /* _SC_PAGESIZE */ |
| 363 | #endif /* HAVE_SYSCONF */ |
| 364 | |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 365 | /* |
| 366 | * Get the size of the containing partition, and use this for |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 367 | * defaults and for making sure the new filesystem doesn't |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 368 | * exceed the partition size. |
| 369 | */ |
| 370 | retval = ext2fs_get_device_size(device_name, fs->blocksize, |
| 371 | &max_size); |
| 372 | if (retval) { |
| 373 | com_err(program_name, retval, |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 374 | _("while trying to determine filesystem size")); |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 375 | exit(1); |
| 376 | } |
Josef Bacik | 199ddaa | 2008-03-13 13:46:10 -0400 | [diff] [blame] | 377 | if (force_min_size) |
| 378 | new_size = calculate_minimum_resize_size(fs); |
| 379 | else if (new_size_str) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 380 | new_size = parse_num_blocks(new_size_str, |
Theodore Ts'o | 55f4cbd | 2005-01-05 03:01:06 -0500 | [diff] [blame] | 381 | fs->super->s_log_block_size); |
Eric Sandeen | 279a000 | 2008-10-10 17:38:15 -0500 | [diff] [blame] | 382 | if (new_size == 0) { |
| 383 | com_err(program_name, 0, |
| 384 | _("Invalid new size: %s\n"), new_size_str); |
| 385 | exit(1); |
| 386 | } |
Theodore Ts'o | 55f4cbd | 2005-01-05 03:01:06 -0500 | [diff] [blame] | 387 | } else { |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 388 | new_size = max_size; |
Theodore Ts'o | a7ccdff | 2003-07-08 18:03:48 -0400 | [diff] [blame] | 389 | /* Round down to an even multiple of a pagesize */ |
| 390 | if (sys_page_size > fs->blocksize) |
| 391 | new_size &= ~((sys_page_size / fs->blocksize)-1); |
| 392 | } |
Theodore Ts'o | 46c5490 | 2006-05-14 15:33:57 -0400 | [diff] [blame] | 393 | |
Eric Sandeen | 0d04d88 | 2009-05-18 17:11:28 -0500 | [diff] [blame] | 394 | if (!force && new_size < min_size) { |
| 395 | com_err(program_name, 0, |
| 396 | _("New size smaller than minimum (%u)\n"), min_size); |
| 397 | exit(1); |
| 398 | } |
Theodore Ts'o | 46c5490 | 2006-05-14 15:33:57 -0400 | [diff] [blame] | 399 | if (use_stride >= 0) { |
Andreas Dilger | de8f3a7 | 2007-05-25 11:18:11 -0400 | [diff] [blame] | 400 | if (use_stride >= (int) fs->super->s_blocks_per_group) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 401 | com_err(program_name, 0, |
Theodore Ts'o | 46c5490 | 2006-05-14 15:33:57 -0400 | [diff] [blame] | 402 | _("Invalid stride length")); |
| 403 | exit(1); |
| 404 | } |
Theodore Ts'o | 96c6a3a | 2007-05-18 22:06:53 -0400 | [diff] [blame] | 405 | fs->stride = fs->super->s_raid_stride = use_stride; |
| 406 | ext2fs_mark_super_dirty(fs); |
Theodore Ts'o | 46c5490 | 2006-05-14 15:33:57 -0400 | [diff] [blame] | 407 | } else |
| 408 | determine_fs_stride(fs); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 409 | |
Theodore Ts'o | 116db1b | 2002-04-01 01:28:30 -0500 | [diff] [blame] | 410 | /* |
| 411 | * If we are resizing a plain file, and it's not big enough, |
| 412 | * automatically extend it in a sparse fashion by writing the |
| 413 | * last requested block. |
| 414 | */ |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 415 | new_file_size = ((__u64) new_size) * fs->blocksize; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 416 | if ((__u64) new_file_size > |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 417 | (((__u64) 1) << (sizeof(st_buf.st_size)*8 - 1)) - 1) |
| 418 | fd = -1; |
| 419 | if ((new_file_size > st_buf.st_size) && |
| 420 | (fd > 0)) { |
| 421 | if ((ext2fs_llseek(fd, new_file_size-1, SEEK_SET) >= 0) && |
| 422 | (write(fd, "0", 1) == 1)) |
Theodore Ts'o | 116db1b | 2002-04-01 01:28:30 -0500 | [diff] [blame] | 423 | max_size = new_size; |
Theodore Ts'o | 116db1b | 2002-04-01 01:28:30 -0500 | [diff] [blame] | 424 | } |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 425 | if (!force && (new_size > max_size)) { |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 426 | fprintf(stderr, _("The containing partition (or device)" |
Takashi Sato | 8deb80a | 2006-03-18 21:43:46 -0500 | [diff] [blame] | 427 | " is only %u (%dk) blocks.\nYou requested a new size" |
| 428 | " of %u blocks.\n\n"), max_size, |
Theodore Ts'o | 792a088 | 2003-05-13 23:32:59 -0400 | [diff] [blame] | 429 | fs->blocksize / 1024, new_size); |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 430 | exit(1); |
| 431 | } |
| 432 | if (new_size == fs->super->s_blocks_count) { |
Takashi Sato | 8deb80a | 2006-03-18 21:43:46 -0500 | [diff] [blame] | 433 | fprintf(stderr, _("The filesystem is already %u blocks " |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 434 | "long. Nothing to do!\n\n"), new_size); |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 435 | exit(0); |
| 436 | } |
Theodore Ts'o | bf69235 | 2006-03-17 19:45:45 -0500 | [diff] [blame] | 437 | if (mount_flags & EXT2_MF_MOUNTED) { |
| 438 | retval = online_resize_fs(fs, mtpt, &new_size, flags); |
| 439 | } else { |
| 440 | if (!force && ((fs->super->s_lastcheck < fs->super->s_mtime) || |
| 441 | (fs->super->s_state & EXT2_ERROR_FS) || |
| 442 | ((fs->super->s_state & EXT2_VALID_FS) == 0))) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 443 | fprintf(stderr, |
Theodore Ts'o | bf69235 | 2006-03-17 19:45:45 -0500 | [diff] [blame] | 444 | _("Please run 'e2fsck -f %s' first.\n\n"), |
| 445 | device_name); |
| 446 | exit(1); |
| 447 | } |
Theodore Ts'o | 96cdb37 | 2009-07-10 13:14:00 -0400 | [diff] [blame^] | 448 | printf(_("Resizing the filesystem on " |
| 449 | "%s to %u (%dk) blocks.\n"), |
Theodore Ts'o | bf69235 | 2006-03-17 19:45:45 -0500 | [diff] [blame] | 450 | device_name, new_size, fs->blocksize / 1024); |
| 451 | retval = resize_fs(fs, &new_size, flags, |
| 452 | ((flags & RESIZE_PERCENT_COMPLETE) ? |
| 453 | resize_progress_func : 0)); |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 454 | } |
Theodore Ts'o | 3a4d986 | 2009-07-02 13:54:22 -0400 | [diff] [blame] | 455 | free(mtpt); |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 456 | if (retval) { |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 457 | com_err(program_name, retval, _("while trying to resize %s"), |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 458 | device_name); |
Theodore Ts'o | 96cdb37 | 2009-07-10 13:14:00 -0400 | [diff] [blame^] | 459 | fprintf(stderr, |
| 460 | _("Please run 'e2fsck -fy %s' to fix the filesystem\n" |
| 461 | "after the aborted resize operation.\n"), |
| 462 | device_name); |
| 463 | ext2fs_close(fs); |
Theodore Ts'o | 1608211 | 2002-04-09 12:46:19 -0400 | [diff] [blame] | 464 | exit(1); |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 465 | } |
Takashi Sato | 8deb80a | 2006-03-18 21:43:46 -0500 | [diff] [blame] | 466 | printf(_("The filesystem on %s is now %u blocks long.\n\n"), |
Theodore Ts'o | 7e71e4c | 1998-09-30 00:54:35 +0000 | [diff] [blame] | 467 | device_name, new_size); |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 468 | |
| 469 | if ((st_buf.st_size > new_file_size) && |
| 470 | (fd > 0)) { |
Theodore Ts'o | 261cd39 | 2008-01-21 13:45:00 -0500 | [diff] [blame] | 471 | #ifdef HAVE_FTRUNCATE64 |
Theodore Ts'o | fb72556 | 2009-04-22 15:10:36 -0400 | [diff] [blame] | 472 | retval = ftruncate64(fd, new_file_size); |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 473 | #else |
Theodore Ts'o | fb72556 | 2009-04-22 15:10:36 -0400 | [diff] [blame] | 474 | retval = 0; |
Theodore Ts'o | 98dca6c | 2008-01-21 13:43:18 -0500 | [diff] [blame] | 475 | /* Only truncate if new_file_size doesn't overflow off_t */ |
| 476 | if (((off_t) new_file_size) == new_file_size) |
Theodore Ts'o | fb72556 | 2009-04-22 15:10:36 -0400 | [diff] [blame] | 477 | retval = ftruncate(fd, (off_t) new_file_size); |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 478 | #endif |
Theodore Ts'o | fb72556 | 2009-04-22 15:10:36 -0400 | [diff] [blame] | 479 | if (retval) |
| 480 | com_err(program_name, retval, |
| 481 | _("while trying to truncate %s"), |
| 482 | device_name); |
Theodore Ts'o | fe07357 | 2005-12-29 15:39:40 -0500 | [diff] [blame] | 483 | } |
| 484 | if (fd > 0) |
| 485 | close(fd); |
Theodore Ts'o | a6d8302 | 2006-12-26 03:38:07 -0500 | [diff] [blame] | 486 | remove_error_table(&et_ext2_error_table); |
Theodore Ts'o | 0a61731 | 2000-02-02 19:14:36 +0000 | [diff] [blame] | 487 | return (0); |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 488 | } |