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