blob: daa68c5b8c8650c08a161a4e933d89a9506a2f6a [file] [log] [blame]
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +00001/*
2 * main.c --- ext2 resizer main program
3 *
Theodore Ts'o0cee8a52000-04-06 21:38:34 +00004 * Copyright (C) 1997, 1998 by Theodore Ts'o and
5 * PowerQuest, Inc.
6 *
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05007 * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 by Theodore Ts'o
Theodore Ts'oefc6f622008-08-27 23:07:54 -04008 *
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +00009 * %Begin-Header%
Theodore Ts'o0cee8a52000-04-06 21:38:34 +000010 * This file may be redistributed under the terms of the GNU Public
11 * License.
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000012 * %End-Header%
13 */
14
Theodore Ts'ofe073572005-12-29 15:39:40 -050015#define _LARGEFILE_SOURCE
16#define _LARGEFILE64_SOURCE
17
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000018#ifdef HAVE_GETOPT_H
19#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000020#else
21extern char *optarg;
22extern int optind;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000023#endif
Theodore Ts'ofe073572005-12-29 15:39:40 -050024#include <unistd.h>
Theodore Ts'of38cf3c2008-09-01 11:17:29 -040025#ifdef HAVE_STDLIB_H
26#include <stdlib.h>
27#endif
Theodore Ts'ofe073572005-12-29 15:39:40 -050028#include <sys/types.h>
Theodore Ts'o116db1b2002-04-01 01:28:30 -050029#include <sys/stat.h>
Theodore Ts'ofe073572005-12-29 15:39:40 -050030#include <fcntl.h>
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000031
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -050032#include "e2p/e2p.h"
33
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000034#include "resize2fs.h"
35
Theodore Ts'o0cee8a52000-04-06 21:38:34 +000036#include "../version.h"
Theodore Ts'o2e561e01998-04-08 06:18:37 +000037
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050038char *program_name, *device_name, *io_options;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000039
Theodore Ts'odfcdc322001-01-11 15:38:00 +000040static void usage (char *prog)
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000041{
Josef Bacik199ddaa2008-03-13 13:46:10 -040042 fprintf (stderr, _("Usage: %s [-d debug_flags] [-f] [-F] [-M] [-P] "
43 "[-p] device [new_size]\n\n"), prog);
Theodore Ts'o2e561e01998-04-08 06:18:37 +000044
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000045 exit (1);
46}
47
Theodore Ts'o3b627e81998-02-24 20:24:49 +000048static errcode_t resize_progress_func(ext2_resize_t rfs, int pass,
49 unsigned long cur, unsigned long max)
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000050{
51 ext2_sim_progmeter progress;
52 const char *label;
53 errcode_t retval;
54
55 progress = (ext2_sim_progmeter) rfs->prog_data;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +000056 if (max == 0)
Theodore Ts'o3b627e81998-02-24 20:24:49 +000057 return 0;
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000058 if (cur == 0) {
59 if (progress)
60 ext2fs_progress_close(progress);
61 progress = 0;
62 switch (pass) {
Theodore Ts'oa8519a21998-02-16 22:16:20 +000063 case E2_RSZ_EXTEND_ITABLE_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000064 label = _("Extending the inode table");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000065 break;
66 case E2_RSZ_BLOCK_RELOC_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000067 label = _("Relocating blocks");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000068 break;
Theodore Ts'oa8519a21998-02-16 22:16:20 +000069 case E2_RSZ_INODE_SCAN_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000070 label = _("Scanning inode table");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000071 break;
72 case E2_RSZ_INODE_REF_UPD_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000073 label = _("Updating inode references");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000074 break;
75 case E2_RSZ_MOVE_ITABLE_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000076 label = _("Moving inode table");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000077 break;
Theodore Ts'oa8519a21998-02-16 22:16:20 +000078 default:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000079 label = _("Unknown pass?!?");
Theodore Ts'oa8519a21998-02-16 22:16:20 +000080 break;
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000081 }
Theodore Ts'oa13575f2000-06-12 22:06:16 +000082 printf(_("Begin pass %d (max = %lu)\n"), pass, max);
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000083 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'o3b627e81998-02-24 20:24:49 +000097 return 0;
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000098}
99
Theodore Ts'o46c54902006-05-14 15:33:57 -0400100static 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'o96c6a3a2007-05-18 22:06:53 -0400107 if (fs->stride)
108 return;
Theodore Ts'o46c54902006-05-14 15:33:57 -0400109 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;
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400114 b_stride = ext2fs_block_bitmap_loc(fs, group) -
115 ext2fs_block_bitmap_loc(fs, group - 1) -
Theodore Ts'o46c54902006-05-14 15:33:57 -0400116 fs->super->s_blocks_per_group;
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400117 i_stride = ext2fs_inode_bitmap_loc(fs, group) -
118 ext2fs_inode_bitmap_loc(fs, group - 1) -
Theodore Ts'o46c54902006-05-14 15:33:57 -0400119 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'oefc6f622008-08-27 23:07:54 -0400127
Theodore Ts'o46c54902006-05-14 15:33:57 -0400128 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'o96c6a3a2007-05-18 22:06:53 -0400140 fs->super->s_raid_stride = fs->stride;
141 ext2fs_mark_super_dirty(fs);
142
Theodore Ts'o46c54902006-05-14 15:33:57 -0400143#if 0
144 if (fs->stride)
145 printf("Using RAID stride of %d\n", fs->stride);
146#endif
147}
148
Theodore Ts'o0a617312000-02-02 19:14:36 +0000149int main (int argc, char ** argv)
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000150{
151 errcode_t retval;
152 ext2_filsys fs;
153 int c;
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000154 int flags = 0;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000155 int flush = 0;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000156 int force = 0;
Theodore Ts'o5d2ef122006-03-18 20:03:47 -0500157 int io_flags = 0;
Josef Bacik199ddaa2008-03-13 13:46:10 -0400158 int force_min_size = 0;
159 int print_min_size = 0;
Theodore Ts'ofe073572005-12-29 15:39:40 -0500160 int fd, ret;
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400161 blk64_t new_size = 0;
162 blk64_t max_size = 0;
163 blk64_t min_size = 0;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000164 io_manager io_ptr;
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500165 char *new_size_str = 0;
Theodore Ts'o46c54902006-05-14 15:33:57 -0400166 int use_stride = -1;
Theodore Ts'ofe073572005-12-29 15:39:40 -0500167#ifdef HAVE_FSTAT64
168 struct stat64 st_buf;
169#else
Theodore Ts'o116db1b2002-04-01 01:28:30 -0500170 struct stat st_buf;
Theodore Ts'ofe073572005-12-29 15:39:40 -0500171#endif
172 __s64 new_file_size;
Theodore Ts'o54434922003-12-07 01:28:50 -0500173 unsigned int sys_page_size = 4096;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -0400174 long sysval;
Theodore Ts'obf692352006-03-17 19:45:45 -0500175 int len, mount_flags;
176 char *mtpt;
Theodore Ts'oddc32a02003-05-03 18:45:55 -0400177
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'oa6d83022006-12-26 03:38:07 -0500185 add_error_table(&et_ext2_error_table);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000186
Theodore Ts'of35fd3d2005-05-09 16:22:17 -0400187 fprintf (stderr, "resize2fs %s (%s)\n",
Theodore Ts'oba0af751998-03-09 17:41:53 +0000188 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000189 if (argc && *argv)
190 program_name = *argv;
Theodore Ts'o2e561e01998-04-08 06:18:37 +0000191
Josef Bacik199ddaa2008-03-13 13:46:10 -0400192 while ((c = getopt (argc, argv, "d:fFhMPpS:")) != EOF) {
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000193 switch (c) {
194 case 'h':
195 usage(program_name);
196 break;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000197 case 'f':
198 force = 1;
199 break;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000200 case 'F':
201 flush = 1;
202 break;
Josef Bacik199ddaa2008-03-13 13:46:10 -0400203 case 'M':
204 force_min_size = 1;
205 break;
206 case 'P':
207 print_min_size = 1;
208 break;
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000209 case 'd':
210 flags |= atoi(optarg);
211 break;
212 case 'p':
213 flags |= RESIZE_PERCENT_COMPLETE;
214 break;
Theodore Ts'o46c54902006-05-14 15:33:57 -0400215 case 'S':
216 use_stride = atoi(optarg);
217 break;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000218 default:
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000219 usage(program_name);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000220 }
221 }
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000222 if (optind == argc)
223 usage(program_name);
Theodore Ts'o2e561e01998-04-08 06:18:37 +0000224
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000225 device_name = argv[optind++];
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500226 if (optind < argc)
227 new_size_str = argv[optind++];
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000228 if (optind < argc)
229 usage(program_name);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400230
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500231 io_options = strchr(device_name, '?');
232 if (io_options)
233 *io_options++ = 0;
234
Theodore Ts'obf692352006-03-17 19:45:45 -0500235 /*
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'oefc6f622008-08-27 23:07:54 -0400245 retval = ext2fs_check_mount_point(device_name, &mount_flags,
Theodore Ts'obf692352006-03-17 19:45:45 -0500246 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'o3a4d9862009-07-02 13:54:22 -0400253 if (!(mount_flags & EXT2_MF_MOUNTED) || (mtpt[len-1] == 0))
Theodore Ts'obf692352006-03-17 19:45:45 -0500254 break;
255 free(mtpt);
256 len = 2 * len;
257 }
Theodore Ts'ofe073572005-12-29 15:39:40 -0500258
Lukas Czernerc859cb12011-09-15 23:44:48 -0400259 fd = ext2fs_open_file(device_name, O_RDWR);
Theodore Ts'ofe073572005-12-29 15:39:40 -0500260 if (fd < 0) {
261 com_err("open", errno, _("while opening %s"),
262 device_name);
263 exit(1);
264 }
265
266#ifdef HAVE_FSTAT64
267 ret = fstat64(fd, &st_buf);
268#else
269 ret = fstat(fd, &st_buf);
270#endif
271 if (ret < 0) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400272 com_err("open", errno,
Theodore Ts'ofe073572005-12-29 15:39:40 -0500273 _("while getting stat information for %s"),
274 device_name);
275 exit(1);
276 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400277
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000278 if (flush) {
Theodore Ts'o48e08e02001-01-11 16:11:11 +0000279 retval = ext2fs_sync_device(fd, 1);
280 if (retval) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400281 com_err(argv[0], retval,
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000282 _("while trying to flush %s"),
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000283 device_name);
284 exit(1);
285 }
Theodore Ts'ofe073572005-12-29 15:39:40 -0500286 }
287
288 if (!S_ISREG(st_buf.st_mode )) {
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000289 close(fd);
Theodore Ts'ofe073572005-12-29 15:39:40 -0500290 fd = -1;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000291 }
292
Theodore Ts'of38cf3c2008-09-01 11:17:29 -0400293#ifdef CONFIG_TESTIO_DEBUG
294 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000295 io_ptr = test_io_manager;
296 test_io_backing_manager = unix_io_manager;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400297 } else
Theodore Ts'of38cf3c2008-09-01 11:17:29 -0400298#endif
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000299 io_ptr = unix_io_manager;
300
Theodore Ts'o5d2ef122006-03-18 20:03:47 -0500301 if (!(mount_flags & EXT2_MF_MOUNTED))
302 io_flags = EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400303 retval = ext2fs_open2(device_name, io_options, io_flags,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500304 0, 0, io_ptr, &fs);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000305 if (retval) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000306 com_err (program_name, retval, _("while trying to open %s"),
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000307 device_name);
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000308 printf (_("Couldn't find valid filesystem superblock.\n"));
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000309 exit (1);
310 }
Jose R. Santos236efed2007-10-21 21:03:52 -0500311
Theodore Ts'o101c84f1998-03-24 16:27:11 +0000312 /*
313 * Check for compatibility with the feature sets. We need to
314 * be more stringent than ext2fs_open().
315 */
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500316 if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) {
Theodore Ts'o101c84f1998-03-24 16:27:11 +0000317 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
318 "(%s)", device_name);
319 exit(1);
320 }
Theodore Ts'oa8d632a2008-06-17 01:53:53 -0400321
322 /*
323 * XXXX The combination of flex_bg and !resize_inode causes
324 * major problems for resize2fs, since when the group descriptors
325 * grow in size this can potentially require multiple inode
326 * tables to be moved aside to make room, and resize2fs chokes
327 * rather badly in this scenario. It's a rare combination,
328 * except when a filesystem is expanded more than a certain
329 * size, so for now, we'll just prohibit that combination.
330 * This is something we should fix eventually, though.
331 */
332 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
333 !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
334 com_err(program_name, 0, _("%s: The combination of flex_bg "
335 "and\n\t!resize_inode features "
336 "is not supported by resize2fs.\n"),
337 device_name);
338 exit(1);
339 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400340
Eric Sandeen0d04d882009-05-18 17:11:28 -0500341 min_size = calculate_minimum_resize_size(fs);
342
Josef Bacik199ddaa2008-03-13 13:46:10 -0400343 if (print_min_size) {
Eric Sandeen8587d392010-07-12 13:27:44 -0500344 if (!force && ((fs->super->s_state & EXT2_ERROR_FS) ||
Eric Sandeen5fa92bc2010-03-31 14:28:42 -0500345 ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
346 fprintf(stderr,
347 _("Please run 'e2fsck -f %s' first.\n\n"),
348 device_name);
349 exit(1);
350 }
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400351 printf(_("Estimated minimum size of the filesystem: %llu\n"),
Eric Sandeen0d04d882009-05-18 17:11:28 -0500352 min_size);
Josef Bacik199ddaa2008-03-13 13:46:10 -0400353 exit(0);
354 }
355
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -0400356 /* Determine the system page size if possible */
357#ifdef HAVE_SYSCONF
358#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
359#define _SC_PAGESIZE _SC_PAGE_SIZE
360#endif
361#ifdef _SC_PAGESIZE
362 sysval = sysconf(_SC_PAGESIZE);
363 if (sysval > 0)
364 sys_page_size = sysval;
365#endif /* _SC_PAGESIZE */
366#endif /* HAVE_SYSCONF */
367
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000368 /*
369 * Get the size of the containing partition, and use this for
Theodore Ts'ofe073572005-12-29 15:39:40 -0500370 * defaults and for making sure the new filesystem doesn't
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000371 * exceed the partition size.
372 */
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400373 retval = ext2fs_get_device_size2(device_name, fs->blocksize,
374 &max_size);
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000375 if (retval) {
376 com_err(program_name, retval,
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000377 _("while trying to determine filesystem size"));
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000378 exit(1);
379 }
Josef Bacik199ddaa2008-03-13 13:46:10 -0400380 if (force_min_size)
Peng Tao1d8af182009-09-04 15:22:39 +0800381 new_size = min_size;
Josef Bacik199ddaa2008-03-13 13:46:10 -0400382 else if (new_size_str) {
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400383 new_size = parse_num_blocks2(new_size_str,
384 fs->super->s_log_block_size);
Eric Sandeen279a0002008-10-10 17:38:15 -0500385 if (new_size == 0) {
386 com_err(program_name, 0,
387 _("Invalid new size: %s\n"), new_size_str);
388 exit(1);
389 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500390 } else {
Eric Sandeen248eb832010-12-15 22:37:34 -0600391 /* Take down devices exactly 16T to 2^32-1 blocks */
392 if (max_size == (1ULL << 32))
393 max_size--;
394 else if (max_size > (1ULL << 32)) {
395 com_err(program_name, 0, _("New size too large to be "
396 "expressed in 32 bits\n"));
397 exit(1);
398 }
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000399 new_size = max_size;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -0400400 /* Round down to an even multiple of a pagesize */
401 if (sys_page_size > fs->blocksize)
402 new_size &= ~((sys_page_size / fs->blocksize)-1);
403 }
Theodore Ts'o46c54902006-05-14 15:33:57 -0400404
Eric Sandeen0d04d882009-05-18 17:11:28 -0500405 if (!force && new_size < min_size) {
406 com_err(program_name, 0,
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400407 _("New size smaller than minimum (%llu)\n"), min_size);
Eric Sandeen0d04d882009-05-18 17:11:28 -0500408 exit(1);
409 }
Theodore Ts'o46c54902006-05-14 15:33:57 -0400410 if (use_stride >= 0) {
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400411 if (use_stride >= (int) fs->super->s_blocks_per_group) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400412 com_err(program_name, 0,
Theodore Ts'o46c54902006-05-14 15:33:57 -0400413 _("Invalid stride length"));
414 exit(1);
415 }
Theodore Ts'o96c6a3a2007-05-18 22:06:53 -0400416 fs->stride = fs->super->s_raid_stride = use_stride;
417 ext2fs_mark_super_dirty(fs);
Theodore Ts'o46c54902006-05-14 15:33:57 -0400418 } else
419 determine_fs_stride(fs);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400420
Theodore Ts'o116db1b2002-04-01 01:28:30 -0500421 /*
422 * If we are resizing a plain file, and it's not big enough,
423 * automatically extend it in a sparse fashion by writing the
424 * last requested block.
425 */
Theodore Ts'ofe073572005-12-29 15:39:40 -0500426 new_file_size = ((__u64) new_size) * fs->blocksize;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400427 if ((__u64) new_file_size >
Theodore Ts'ofe073572005-12-29 15:39:40 -0500428 (((__u64) 1) << (sizeof(st_buf.st_size)*8 - 1)) - 1)
429 fd = -1;
430 if ((new_file_size > st_buf.st_size) &&
431 (fd > 0)) {
432 if ((ext2fs_llseek(fd, new_file_size-1, SEEK_SET) >= 0) &&
433 (write(fd, "0", 1) == 1))
Theodore Ts'o116db1b2002-04-01 01:28:30 -0500434 max_size = new_size;
Theodore Ts'o116db1b2002-04-01 01:28:30 -0500435 }
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000436 if (!force && (new_size > max_size)) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000437 fprintf(stderr, _("The containing partition (or device)"
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400438 " is only %llu (%dk) blocks.\nYou requested a new size"
439 " of %llu blocks.\n\n"), max_size,
Theodore Ts'o792a0882003-05-13 23:32:59 -0400440 fs->blocksize / 1024, new_size);
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000441 exit(1);
442 }
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400443 if (new_size == ext2fs_blocks_count(fs->super)) {
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400444 fprintf(stderr, _("The filesystem is already %llu blocks "
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000445 "long. Nothing to do!\n\n"), new_size);
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000446 exit(0);
447 }
Theodore Ts'obf692352006-03-17 19:45:45 -0500448 if (mount_flags & EXT2_MF_MOUNTED) {
449 retval = online_resize_fs(fs, mtpt, &new_size, flags);
450 } else {
451 if (!force && ((fs->super->s_lastcheck < fs->super->s_mtime) ||
452 (fs->super->s_state & EXT2_ERROR_FS) ||
453 ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400454 fprintf(stderr,
Theodore Ts'obf692352006-03-17 19:45:45 -0500455 _("Please run 'e2fsck -f %s' first.\n\n"),
456 device_name);
457 exit(1);
458 }
Theodore Ts'o96cdb372009-07-10 13:14:00 -0400459 printf(_("Resizing the filesystem on "
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400460 "%s to %llu (%dk) blocks.\n"),
Theodore Ts'obf692352006-03-17 19:45:45 -0500461 device_name, new_size, fs->blocksize / 1024);
462 retval = resize_fs(fs, &new_size, flags,
463 ((flags & RESIZE_PERCENT_COMPLETE) ?
464 resize_progress_func : 0));
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000465 }
Theodore Ts'o3a4d9862009-07-02 13:54:22 -0400466 free(mtpt);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000467 if (retval) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000468 com_err(program_name, retval, _("while trying to resize %s"),
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000469 device_name);
Theodore Ts'o96cdb372009-07-10 13:14:00 -0400470 fprintf(stderr,
471 _("Please run 'e2fsck -fy %s' to fix the filesystem\n"
472 "after the aborted resize operation.\n"),
473 device_name);
474 ext2fs_close(fs);
Theodore Ts'o16082112002-04-09 12:46:19 -0400475 exit(1);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000476 }
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400477 printf(_("The filesystem on %s is now %llu blocks long.\n\n"),
Theodore Ts'o7e71e4c1998-09-30 00:54:35 +0000478 device_name, new_size);
Theodore Ts'ofe073572005-12-29 15:39:40 -0500479
480 if ((st_buf.st_size > new_file_size) &&
481 (fd > 0)) {
Theodore Ts'o261cd392008-01-21 13:45:00 -0500482#ifdef HAVE_FTRUNCATE64
Theodore Ts'ofb725562009-04-22 15:10:36 -0400483 retval = ftruncate64(fd, new_file_size);
Theodore Ts'ofe073572005-12-29 15:39:40 -0500484#else
Theodore Ts'ofb725562009-04-22 15:10:36 -0400485 retval = 0;
Theodore Ts'o98dca6c2008-01-21 13:43:18 -0500486 /* Only truncate if new_file_size doesn't overflow off_t */
487 if (((off_t) new_file_size) == new_file_size)
Theodore Ts'ofb725562009-04-22 15:10:36 -0400488 retval = ftruncate(fd, (off_t) new_file_size);
Theodore Ts'ofe073572005-12-29 15:39:40 -0500489#endif
Theodore Ts'ofb725562009-04-22 15:10:36 -0400490 if (retval)
491 com_err(program_name, retval,
492 _("while trying to truncate %s"),
493 device_name);
Theodore Ts'ofe073572005-12-29 15:39:40 -0500494 }
495 if (fd > 0)
496 close(fd);
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500497 remove_error_table(&et_ext2_error_table);
Theodore Ts'o0a617312000-02-02 19:14:36 +0000498 return (0);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000499}